ceph_common.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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_obj(*opt);
  286. if (!opt)
  287. return NULL;
  288. opt->crush_locs = RB_ROOT;
  289. opt->mon_addr = kzalloc_objs(*opt->mon_addr, CEPH_MAX_MON);
  290. if (!opt->mon_addr) {
  291. kfree(opt);
  292. return NULL;
  293. }
  294. opt->flags = CEPH_OPT_DEFAULT;
  295. opt->osd_keepalive_timeout = CEPH_OSD_KEEPALIVE_DEFAULT;
  296. opt->mount_timeout = CEPH_MOUNT_TIMEOUT_DEFAULT;
  297. opt->osd_idle_ttl = CEPH_OSD_IDLE_TTL_DEFAULT;
  298. opt->osd_request_timeout = CEPH_OSD_REQUEST_TIMEOUT_DEFAULT;
  299. opt->read_from_replica = CEPH_READ_FROM_REPLICA_DEFAULT;
  300. opt->con_modes[0] = CEPH_CON_MODE_UNKNOWN;
  301. opt->con_modes[1] = CEPH_CON_MODE_UNKNOWN;
  302. return opt;
  303. }
  304. EXPORT_SYMBOL(ceph_alloc_options);
  305. void ceph_destroy_options(struct ceph_options *opt)
  306. {
  307. dout("destroy_options %p\n", opt);
  308. if (!opt)
  309. return;
  310. ceph_clear_crush_locs(&opt->crush_locs);
  311. kfree(opt->name);
  312. if (opt->key) {
  313. ceph_crypto_key_destroy(opt->key);
  314. kfree(opt->key);
  315. }
  316. kfree(opt->mon_addr);
  317. kfree(opt);
  318. }
  319. EXPORT_SYMBOL(ceph_destroy_options);
  320. /* get secret from key store */
  321. static int get_secret(struct ceph_crypto_key *dst, const char *name,
  322. struct p_log *log)
  323. {
  324. struct key *ukey;
  325. int key_err;
  326. int err = 0;
  327. struct ceph_crypto_key *ckey;
  328. ukey = request_key(&key_type_ceph, name, NULL);
  329. if (IS_ERR(ukey)) {
  330. /* request_key errors don't map nicely to mount(2)
  331. errors; don't even try, but still printk */
  332. key_err = PTR_ERR(ukey);
  333. switch (key_err) {
  334. case -ENOKEY:
  335. error_plog(log, "Failed due to key not found: %s",
  336. name);
  337. break;
  338. case -EKEYEXPIRED:
  339. error_plog(log, "Failed due to expired key: %s",
  340. name);
  341. break;
  342. case -EKEYREVOKED:
  343. error_plog(log, "Failed due to revoked key: %s",
  344. name);
  345. break;
  346. default:
  347. error_plog(log, "Failed due to key error %d: %s",
  348. key_err, name);
  349. }
  350. err = -EPERM;
  351. goto out;
  352. }
  353. ckey = ukey->payload.data[0];
  354. err = ceph_crypto_key_clone(dst, ckey);
  355. if (err)
  356. goto out_key;
  357. /* pass through, err is 0 */
  358. out_key:
  359. key_put(ukey);
  360. out:
  361. return err;
  362. }
  363. int ceph_parse_mon_ips(const char *buf, size_t len, struct ceph_options *opt,
  364. struct fc_log *l, char delim)
  365. {
  366. struct p_log log = {.prefix = "libceph", .log = l};
  367. int ret;
  368. /* ip1[:port1][<delim>ip2[:port2]...] */
  369. ret = ceph_parse_ips(buf, buf + len, opt->mon_addr, CEPH_MAX_MON,
  370. &opt->num_mon, delim);
  371. if (ret) {
  372. error_plog(&log, "Failed to parse monitor IPs: %d", ret);
  373. return ret;
  374. }
  375. return 0;
  376. }
  377. EXPORT_SYMBOL(ceph_parse_mon_ips);
  378. int ceph_parse_param(struct fs_parameter *param, struct ceph_options *opt,
  379. struct fc_log *l)
  380. {
  381. struct fs_parse_result result;
  382. int token, err;
  383. struct p_log log = {.prefix = "libceph", .log = l};
  384. token = __fs_parse(&log, ceph_parameters, param, &result);
  385. dout("%s fs_parse '%s' token %d\n", __func__, param->key, token);
  386. if (token < 0)
  387. return token;
  388. switch (token) {
  389. case Opt_ip:
  390. err = ceph_parse_ips(param->string,
  391. param->string + param->size,
  392. &opt->my_addr, 1, NULL, ',');
  393. if (err) {
  394. error_plog(&log, "Failed to parse ip: %d", err);
  395. return err;
  396. }
  397. opt->flags |= CEPH_OPT_MYIP;
  398. break;
  399. case Opt_fsid:
  400. err = ceph_parse_fsid(param->string, &opt->fsid);
  401. if (err) {
  402. error_plog(&log, "Failed to parse fsid: %d", err);
  403. return err;
  404. }
  405. opt->flags |= CEPH_OPT_FSID;
  406. break;
  407. case Opt_name:
  408. kfree(opt->name);
  409. opt->name = param->string;
  410. param->string = NULL;
  411. break;
  412. case Opt_secret:
  413. ceph_crypto_key_destroy(opt->key);
  414. kfree(opt->key);
  415. opt->key = kzalloc_obj(*opt->key);
  416. if (!opt->key)
  417. return -ENOMEM;
  418. err = ceph_crypto_key_unarmor(opt->key, param->string);
  419. if (err) {
  420. error_plog(&log, "Failed to parse secret: %d", err);
  421. return err;
  422. }
  423. break;
  424. case Opt_key:
  425. ceph_crypto_key_destroy(opt->key);
  426. kfree(opt->key);
  427. opt->key = kzalloc_obj(*opt->key);
  428. if (!opt->key)
  429. return -ENOMEM;
  430. return get_secret(opt->key, param->string, &log);
  431. case Opt_crush_location:
  432. ceph_clear_crush_locs(&opt->crush_locs);
  433. err = ceph_parse_crush_location(param->string,
  434. &opt->crush_locs);
  435. if (err) {
  436. error_plog(&log, "Failed to parse CRUSH location: %d",
  437. err);
  438. return err;
  439. }
  440. break;
  441. case Opt_read_from_replica:
  442. switch (result.uint_32) {
  443. case Opt_read_from_replica_no:
  444. opt->read_from_replica = 0;
  445. break;
  446. case Opt_read_from_replica_balance:
  447. opt->read_from_replica = CEPH_OSD_FLAG_BALANCE_READS;
  448. break;
  449. case Opt_read_from_replica_localize:
  450. opt->read_from_replica = CEPH_OSD_FLAG_LOCALIZE_READS;
  451. break;
  452. default:
  453. BUG();
  454. }
  455. break;
  456. case Opt_ms_mode:
  457. switch (result.uint_32) {
  458. case Opt_ms_mode_legacy:
  459. opt->con_modes[0] = CEPH_CON_MODE_UNKNOWN;
  460. opt->con_modes[1] = CEPH_CON_MODE_UNKNOWN;
  461. break;
  462. case Opt_ms_mode_crc:
  463. opt->con_modes[0] = CEPH_CON_MODE_CRC;
  464. opt->con_modes[1] = CEPH_CON_MODE_UNKNOWN;
  465. break;
  466. case Opt_ms_mode_secure:
  467. opt->con_modes[0] = CEPH_CON_MODE_SECURE;
  468. opt->con_modes[1] = CEPH_CON_MODE_UNKNOWN;
  469. break;
  470. case Opt_ms_mode_prefer_crc:
  471. opt->con_modes[0] = CEPH_CON_MODE_CRC;
  472. opt->con_modes[1] = CEPH_CON_MODE_SECURE;
  473. break;
  474. case Opt_ms_mode_prefer_secure:
  475. opt->con_modes[0] = CEPH_CON_MODE_SECURE;
  476. opt->con_modes[1] = CEPH_CON_MODE_CRC;
  477. break;
  478. default:
  479. BUG();
  480. }
  481. break;
  482. case Opt_osdkeepalivetimeout:
  483. /* 0 isn't well defined right now, reject it */
  484. if (result.uint_32 < 1 || result.uint_32 > INT_MAX / 1000)
  485. goto out_of_range;
  486. opt->osd_keepalive_timeout =
  487. msecs_to_jiffies(result.uint_32 * 1000);
  488. break;
  489. case Opt_osd_idle_ttl:
  490. /* 0 isn't well defined right now, reject it */
  491. if (result.uint_32 < 1 || result.uint_32 > INT_MAX / 1000)
  492. goto out_of_range;
  493. opt->osd_idle_ttl = msecs_to_jiffies(result.uint_32 * 1000);
  494. break;
  495. case Opt_mount_timeout:
  496. /* 0 is "wait forever" (i.e. infinite timeout) */
  497. if (result.uint_32 > INT_MAX / 1000)
  498. goto out_of_range;
  499. opt->mount_timeout = msecs_to_jiffies(result.uint_32 * 1000);
  500. break;
  501. case Opt_osd_request_timeout:
  502. /* 0 is "wait forever" (i.e. infinite timeout) */
  503. if (result.uint_32 > INT_MAX / 1000)
  504. goto out_of_range;
  505. opt->osd_request_timeout =
  506. msecs_to_jiffies(result.uint_32 * 1000);
  507. break;
  508. case Opt_share:
  509. if (!result.negated)
  510. opt->flags &= ~CEPH_OPT_NOSHARE;
  511. else
  512. opt->flags |= CEPH_OPT_NOSHARE;
  513. break;
  514. case Opt_crc:
  515. if (!result.negated)
  516. opt->flags &= ~CEPH_OPT_NOCRC;
  517. else
  518. opt->flags |= CEPH_OPT_NOCRC;
  519. break;
  520. case Opt_cephx_require_signatures:
  521. if (!result.negated)
  522. warn_plog(&log, "Ignoring cephx_require_signatures");
  523. else
  524. warn_plog(&log, "Ignoring nocephx_require_signatures, use nocephx_sign_messages");
  525. break;
  526. case Opt_cephx_sign_messages:
  527. if (!result.negated)
  528. opt->flags &= ~CEPH_OPT_NOMSGSIGN;
  529. else
  530. opt->flags |= CEPH_OPT_NOMSGSIGN;
  531. break;
  532. case Opt_tcp_nodelay:
  533. if (!result.negated)
  534. opt->flags |= CEPH_OPT_TCP_NODELAY;
  535. else
  536. opt->flags &= ~CEPH_OPT_TCP_NODELAY;
  537. break;
  538. case Opt_abort_on_full:
  539. opt->flags |= CEPH_OPT_ABORT_ON_FULL;
  540. break;
  541. case Opt_rxbounce:
  542. opt->flags |= CEPH_OPT_RXBOUNCE;
  543. break;
  544. default:
  545. BUG();
  546. }
  547. return 0;
  548. out_of_range:
  549. return inval_plog(&log, "%s out of range", param->key);
  550. }
  551. EXPORT_SYMBOL(ceph_parse_param);
  552. int ceph_print_client_options(struct seq_file *m, struct ceph_client *client,
  553. bool show_all)
  554. {
  555. struct ceph_options *opt = client->options;
  556. size_t pos = m->count;
  557. struct rb_node *n;
  558. if (opt->name) {
  559. seq_puts(m, "name=");
  560. seq_escape(m, opt->name, ", \t\n\\");
  561. seq_putc(m, ',');
  562. }
  563. if (opt->key)
  564. seq_puts(m, "secret=<hidden>,");
  565. if (!RB_EMPTY_ROOT(&opt->crush_locs)) {
  566. seq_puts(m, "crush_location=");
  567. for (n = rb_first(&opt->crush_locs); ; ) {
  568. struct crush_loc_node *loc =
  569. rb_entry(n, struct crush_loc_node, cl_node);
  570. seq_printf(m, "%s:%s", loc->cl_loc.cl_type_name,
  571. loc->cl_loc.cl_name);
  572. n = rb_next(n);
  573. if (!n)
  574. break;
  575. seq_putc(m, '|');
  576. }
  577. seq_putc(m, ',');
  578. }
  579. if (opt->read_from_replica == CEPH_OSD_FLAG_BALANCE_READS) {
  580. seq_puts(m, "read_from_replica=balance,");
  581. } else if (opt->read_from_replica == CEPH_OSD_FLAG_LOCALIZE_READS) {
  582. seq_puts(m, "read_from_replica=localize,");
  583. }
  584. if (opt->con_modes[0] != CEPH_CON_MODE_UNKNOWN) {
  585. if (opt->con_modes[0] == CEPH_CON_MODE_CRC &&
  586. opt->con_modes[1] == CEPH_CON_MODE_UNKNOWN) {
  587. seq_puts(m, "ms_mode=crc,");
  588. } else if (opt->con_modes[0] == CEPH_CON_MODE_SECURE &&
  589. opt->con_modes[1] == CEPH_CON_MODE_UNKNOWN) {
  590. seq_puts(m, "ms_mode=secure,");
  591. } else if (opt->con_modes[0] == CEPH_CON_MODE_CRC &&
  592. opt->con_modes[1] == CEPH_CON_MODE_SECURE) {
  593. seq_puts(m, "ms_mode=prefer-crc,");
  594. } else if (opt->con_modes[0] == CEPH_CON_MODE_SECURE &&
  595. opt->con_modes[1] == CEPH_CON_MODE_CRC) {
  596. seq_puts(m, "ms_mode=prefer-secure,");
  597. }
  598. }
  599. if (opt->flags & CEPH_OPT_FSID)
  600. seq_printf(m, "fsid=%pU,", &opt->fsid);
  601. if (opt->flags & CEPH_OPT_NOSHARE)
  602. seq_puts(m, "noshare,");
  603. if (opt->flags & CEPH_OPT_NOCRC)
  604. seq_puts(m, "nocrc,");
  605. if (opt->flags & CEPH_OPT_NOMSGSIGN)
  606. seq_puts(m, "nocephx_sign_messages,");
  607. if ((opt->flags & CEPH_OPT_TCP_NODELAY) == 0)
  608. seq_puts(m, "notcp_nodelay,");
  609. if (show_all && (opt->flags & CEPH_OPT_ABORT_ON_FULL))
  610. seq_puts(m, "abort_on_full,");
  611. if (opt->flags & CEPH_OPT_RXBOUNCE)
  612. seq_puts(m, "rxbounce,");
  613. if (opt->mount_timeout != CEPH_MOUNT_TIMEOUT_DEFAULT)
  614. seq_printf(m, "mount_timeout=%d,",
  615. jiffies_to_msecs(opt->mount_timeout) / 1000);
  616. if (opt->osd_idle_ttl != CEPH_OSD_IDLE_TTL_DEFAULT)
  617. seq_printf(m, "osd_idle_ttl=%d,",
  618. jiffies_to_msecs(opt->osd_idle_ttl) / 1000);
  619. if (opt->osd_keepalive_timeout != CEPH_OSD_KEEPALIVE_DEFAULT)
  620. seq_printf(m, "osdkeepalivetimeout=%d,",
  621. jiffies_to_msecs(opt->osd_keepalive_timeout) / 1000);
  622. if (opt->osd_request_timeout != CEPH_OSD_REQUEST_TIMEOUT_DEFAULT)
  623. seq_printf(m, "osd_request_timeout=%d,",
  624. jiffies_to_msecs(opt->osd_request_timeout) / 1000);
  625. /* drop redundant comma */
  626. if (m->count != pos)
  627. m->count--;
  628. return 0;
  629. }
  630. EXPORT_SYMBOL(ceph_print_client_options);
  631. struct ceph_entity_addr *ceph_client_addr(struct ceph_client *client)
  632. {
  633. return &client->msgr.inst.addr;
  634. }
  635. EXPORT_SYMBOL(ceph_client_addr);
  636. u64 ceph_client_gid(struct ceph_client *client)
  637. {
  638. return client->monc.auth->global_id;
  639. }
  640. EXPORT_SYMBOL(ceph_client_gid);
  641. /*
  642. * create a fresh client instance
  643. */
  644. struct ceph_client *ceph_create_client(struct ceph_options *opt, void *private)
  645. {
  646. struct ceph_client *client;
  647. struct ceph_entity_addr *myaddr = NULL;
  648. int err;
  649. err = wait_for_random_bytes();
  650. if (err < 0)
  651. return ERR_PTR(err);
  652. client = kzalloc_obj(*client);
  653. if (client == NULL)
  654. return ERR_PTR(-ENOMEM);
  655. client->private = private;
  656. client->options = opt;
  657. mutex_init(&client->mount_mutex);
  658. init_waitqueue_head(&client->auth_wq);
  659. client->auth_err = 0;
  660. client->extra_mon_dispatch = NULL;
  661. client->supported_features = CEPH_FEATURES_SUPPORTED_DEFAULT;
  662. client->required_features = CEPH_FEATURES_REQUIRED_DEFAULT;
  663. if (!ceph_test_opt(client, NOMSGSIGN))
  664. client->required_features |= CEPH_FEATURE_MSG_AUTH;
  665. /* msgr */
  666. if (ceph_test_opt(client, MYIP))
  667. myaddr = &client->options->my_addr;
  668. ceph_messenger_init(&client->msgr, myaddr);
  669. /* subsystems */
  670. err = ceph_monc_init(&client->monc, client);
  671. if (err < 0)
  672. goto fail;
  673. err = ceph_osdc_init(&client->osdc, client);
  674. if (err < 0)
  675. goto fail_monc;
  676. return client;
  677. fail_monc:
  678. ceph_monc_stop(&client->monc);
  679. fail:
  680. ceph_messenger_fini(&client->msgr);
  681. kfree(client);
  682. return ERR_PTR(err);
  683. }
  684. EXPORT_SYMBOL(ceph_create_client);
  685. void ceph_destroy_client(struct ceph_client *client)
  686. {
  687. dout("destroy_client %p\n", client);
  688. atomic_set(&client->msgr.stopping, 1);
  689. /* unmount */
  690. ceph_osdc_stop(&client->osdc);
  691. ceph_monc_stop(&client->monc);
  692. ceph_messenger_fini(&client->msgr);
  693. ceph_debugfs_client_cleanup(client);
  694. ceph_destroy_options(client->options);
  695. kfree(client);
  696. dout("destroy_client %p done\n", client);
  697. }
  698. EXPORT_SYMBOL(ceph_destroy_client);
  699. void ceph_reset_client_addr(struct ceph_client *client)
  700. {
  701. ceph_messenger_reset_nonce(&client->msgr);
  702. ceph_monc_reopen_session(&client->monc);
  703. ceph_osdc_reopen_osds(&client->osdc);
  704. }
  705. EXPORT_SYMBOL(ceph_reset_client_addr);
  706. /*
  707. * mount: join the ceph cluster, and open root directory.
  708. */
  709. int __ceph_open_session(struct ceph_client *client)
  710. {
  711. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  712. long timeout = ceph_timeout_jiffies(client->options->mount_timeout);
  713. bool have_monmap, have_osdmap;
  714. int err;
  715. /* open session, and wait for mon and osd maps */
  716. err = ceph_monc_open_session(&client->monc);
  717. if (err < 0)
  718. return err;
  719. add_wait_queue(&client->auth_wq, &wait);
  720. for (;;) {
  721. mutex_lock(&client->monc.mutex);
  722. err = client->auth_err;
  723. have_monmap = client->monc.monmap && client->monc.monmap->epoch;
  724. mutex_unlock(&client->monc.mutex);
  725. down_read(&client->osdc.lock);
  726. have_osdmap = client->osdc.osdmap && client->osdc.osdmap->epoch;
  727. up_read(&client->osdc.lock);
  728. if (err || (have_monmap && have_osdmap))
  729. break;
  730. if (signal_pending(current)) {
  731. err = -ERESTARTSYS;
  732. break;
  733. }
  734. if (!timeout) {
  735. err = -ETIMEDOUT;
  736. break;
  737. }
  738. /* wait */
  739. dout("mount waiting for mon_map\n");
  740. timeout = wait_woken(&wait, TASK_INTERRUPTIBLE, timeout);
  741. }
  742. remove_wait_queue(&client->auth_wq, &wait);
  743. if (err)
  744. return err;
  745. pr_info("client%llu fsid %pU\n", ceph_client_gid(client),
  746. &client->fsid);
  747. ceph_debugfs_client_init(client);
  748. return 0;
  749. }
  750. EXPORT_SYMBOL(__ceph_open_session);
  751. int ceph_open_session(struct ceph_client *client)
  752. {
  753. int ret;
  754. dout("open_session start\n");
  755. mutex_lock(&client->mount_mutex);
  756. ret = __ceph_open_session(client);
  757. mutex_unlock(&client->mount_mutex);
  758. return ret;
  759. }
  760. EXPORT_SYMBOL(ceph_open_session);
  761. int ceph_wait_for_latest_osdmap(struct ceph_client *client,
  762. unsigned long timeout)
  763. {
  764. u64 newest_epoch;
  765. int ret;
  766. ret = ceph_monc_get_version(&client->monc, "osdmap", &newest_epoch);
  767. if (ret)
  768. return ret;
  769. if (client->osdc.osdmap->epoch >= newest_epoch)
  770. return 0;
  771. ceph_osdc_maybe_request_map(&client->osdc);
  772. return ceph_monc_wait_osdmap(&client->monc, newest_epoch, timeout);
  773. }
  774. EXPORT_SYMBOL(ceph_wait_for_latest_osdmap);
  775. static int __init init_ceph_lib(void)
  776. {
  777. int ret = 0;
  778. ceph_debugfs_init();
  779. ret = ceph_crypto_init();
  780. if (ret < 0)
  781. goto out_debugfs;
  782. ret = ceph_msgr_init();
  783. if (ret < 0)
  784. goto out_crypto;
  785. ret = ceph_osdc_setup();
  786. if (ret < 0)
  787. goto out_msgr;
  788. pr_info("loaded (mon/osd proto %d/%d)\n",
  789. CEPH_MONC_PROTOCOL, CEPH_OSDC_PROTOCOL);
  790. return 0;
  791. out_msgr:
  792. ceph_msgr_exit();
  793. out_crypto:
  794. ceph_crypto_shutdown();
  795. out_debugfs:
  796. ceph_debugfs_cleanup();
  797. return ret;
  798. }
  799. static void __exit exit_ceph_lib(void)
  800. {
  801. dout("exit_ceph_lib\n");
  802. WARN_ON(!ceph_strings_empty());
  803. ceph_osdc_cleanup();
  804. ceph_msgr_exit();
  805. ceph_crypto_shutdown();
  806. ceph_debugfs_cleanup();
  807. }
  808. module_init(init_ceph_lib);
  809. module_exit(exit_ceph_lib);
  810. MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
  811. MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
  812. MODULE_AUTHOR("Patience Warnick <patience@newdream.net>");
  813. MODULE_DESCRIPTION("Ceph core library");
  814. MODULE_LICENSE("GPL");