ceph_common.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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. /*
  177. * kvmalloc() doesn't fall back to the vmalloc allocator unless flags are
  178. * compatible with (a superset of) GFP_KERNEL. This is because while the
  179. * actual pages are allocated with the specified flags, the page table pages
  180. * are always allocated with GFP_KERNEL.
  181. *
  182. * ceph_kvmalloc() may be called with GFP_KERNEL, GFP_NOFS or GFP_NOIO.
  183. */
  184. void *ceph_kvmalloc(size_t size, gfp_t flags)
  185. {
  186. void *p;
  187. if ((flags & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS)) {
  188. p = kvmalloc(size, flags);
  189. } else if ((flags & (__GFP_IO | __GFP_FS)) == __GFP_IO) {
  190. unsigned int nofs_flag = memalloc_nofs_save();
  191. p = kvmalloc(size, GFP_KERNEL);
  192. memalloc_nofs_restore(nofs_flag);
  193. } else {
  194. unsigned int noio_flag = memalloc_noio_save();
  195. p = kvmalloc(size, GFP_KERNEL);
  196. memalloc_noio_restore(noio_flag);
  197. }
  198. return p;
  199. }
  200. static int parse_fsid(const char *str, struct ceph_fsid *fsid)
  201. {
  202. int i = 0;
  203. char tmp[3];
  204. int err = -EINVAL;
  205. int d;
  206. dout("parse_fsid '%s'\n", str);
  207. tmp[2] = 0;
  208. while (*str && i < 16) {
  209. if (ispunct(*str)) {
  210. str++;
  211. continue;
  212. }
  213. if (!isxdigit(str[0]) || !isxdigit(str[1]))
  214. break;
  215. tmp[0] = str[0];
  216. tmp[1] = str[1];
  217. if (sscanf(tmp, "%x", &d) < 1)
  218. break;
  219. fsid->fsid[i] = d & 0xff;
  220. i++;
  221. str += 2;
  222. }
  223. if (i == 16)
  224. err = 0;
  225. dout("parse_fsid ret %d got fsid %pU\n", err, fsid);
  226. return err;
  227. }
  228. /*
  229. * ceph options
  230. */
  231. enum {
  232. Opt_osdtimeout,
  233. Opt_osdkeepalivetimeout,
  234. Opt_mount_timeout,
  235. Opt_osd_idle_ttl,
  236. Opt_osd_request_timeout,
  237. /* int args above */
  238. Opt_fsid,
  239. Opt_name,
  240. Opt_secret,
  241. Opt_key,
  242. Opt_ip,
  243. Opt_crush_location,
  244. Opt_read_from_replica,
  245. /* string args above */
  246. Opt_share,
  247. Opt_crc,
  248. Opt_cephx_require_signatures,
  249. Opt_cephx_sign_messages,
  250. Opt_tcp_nodelay,
  251. Opt_abort_on_full,
  252. };
  253. enum {
  254. Opt_read_from_replica_no,
  255. Opt_read_from_replica_balance,
  256. Opt_read_from_replica_localize,
  257. };
  258. static const struct constant_table ceph_param_read_from_replica[] = {
  259. {"no", Opt_read_from_replica_no},
  260. {"balance", Opt_read_from_replica_balance},
  261. {"localize", Opt_read_from_replica_localize},
  262. {}
  263. };
  264. static const struct fs_parameter_spec ceph_parameters[] = {
  265. fsparam_flag ("abort_on_full", Opt_abort_on_full),
  266. fsparam_flag_no ("cephx_require_signatures", Opt_cephx_require_signatures),
  267. fsparam_flag_no ("cephx_sign_messages", Opt_cephx_sign_messages),
  268. fsparam_flag_no ("crc", Opt_crc),
  269. fsparam_string ("crush_location", Opt_crush_location),
  270. fsparam_string ("fsid", Opt_fsid),
  271. fsparam_string ("ip", Opt_ip),
  272. fsparam_string ("key", Opt_key),
  273. fsparam_u32 ("mount_timeout", Opt_mount_timeout),
  274. fsparam_string ("name", Opt_name),
  275. fsparam_u32 ("osd_idle_ttl", Opt_osd_idle_ttl),
  276. fsparam_u32 ("osd_request_timeout", Opt_osd_request_timeout),
  277. fsparam_u32 ("osdkeepalive", Opt_osdkeepalivetimeout),
  278. __fsparam (fs_param_is_s32, "osdtimeout", Opt_osdtimeout,
  279. fs_param_deprecated, NULL),
  280. fsparam_enum ("read_from_replica", Opt_read_from_replica,
  281. ceph_param_read_from_replica),
  282. fsparam_string ("secret", Opt_secret),
  283. fsparam_flag_no ("share", Opt_share),
  284. fsparam_flag_no ("tcp_nodelay", Opt_tcp_nodelay),
  285. {}
  286. };
  287. struct ceph_options *ceph_alloc_options(void)
  288. {
  289. struct ceph_options *opt;
  290. opt = kzalloc(sizeof(*opt), GFP_KERNEL);
  291. if (!opt)
  292. return NULL;
  293. opt->crush_locs = RB_ROOT;
  294. opt->mon_addr = kcalloc(CEPH_MAX_MON, sizeof(*opt->mon_addr),
  295. GFP_KERNEL);
  296. if (!opt->mon_addr) {
  297. kfree(opt);
  298. return NULL;
  299. }
  300. opt->flags = CEPH_OPT_DEFAULT;
  301. opt->osd_keepalive_timeout = CEPH_OSD_KEEPALIVE_DEFAULT;
  302. opt->mount_timeout = CEPH_MOUNT_TIMEOUT_DEFAULT;
  303. opt->osd_idle_ttl = CEPH_OSD_IDLE_TTL_DEFAULT;
  304. opt->osd_request_timeout = CEPH_OSD_REQUEST_TIMEOUT_DEFAULT;
  305. opt->read_from_replica = CEPH_READ_FROM_REPLICA_DEFAULT;
  306. return opt;
  307. }
  308. EXPORT_SYMBOL(ceph_alloc_options);
  309. void ceph_destroy_options(struct ceph_options *opt)
  310. {
  311. dout("destroy_options %p\n", opt);
  312. if (!opt)
  313. return;
  314. ceph_clear_crush_locs(&opt->crush_locs);
  315. kfree(opt->name);
  316. if (opt->key) {
  317. ceph_crypto_key_destroy(opt->key);
  318. kfree(opt->key);
  319. }
  320. kfree(opt->mon_addr);
  321. kfree(opt);
  322. }
  323. EXPORT_SYMBOL(ceph_destroy_options);
  324. /* get secret from key store */
  325. static int get_secret(struct ceph_crypto_key *dst, const char *name,
  326. struct p_log *log)
  327. {
  328. struct key *ukey;
  329. int key_err;
  330. int err = 0;
  331. struct ceph_crypto_key *ckey;
  332. ukey = request_key(&key_type_ceph, name, NULL);
  333. if (IS_ERR(ukey)) {
  334. /* request_key errors don't map nicely to mount(2)
  335. errors; don't even try, but still printk */
  336. key_err = PTR_ERR(ukey);
  337. switch (key_err) {
  338. case -ENOKEY:
  339. error_plog(log, "Failed due to key not found: %s",
  340. name);
  341. break;
  342. case -EKEYEXPIRED:
  343. error_plog(log, "Failed due to expired key: %s",
  344. name);
  345. break;
  346. case -EKEYREVOKED:
  347. error_plog(log, "Failed due to revoked key: %s",
  348. name);
  349. break;
  350. default:
  351. error_plog(log, "Failed due to key error %d: %s",
  352. key_err, name);
  353. }
  354. err = -EPERM;
  355. goto out;
  356. }
  357. ckey = ukey->payload.data[0];
  358. err = ceph_crypto_key_clone(dst, ckey);
  359. if (err)
  360. goto out_key;
  361. /* pass through, err is 0 */
  362. out_key:
  363. key_put(ukey);
  364. out:
  365. return err;
  366. }
  367. int ceph_parse_mon_ips(const char *buf, size_t len, struct ceph_options *opt,
  368. struct fc_log *l)
  369. {
  370. struct p_log log = {.prefix = "libceph", .log = l};
  371. int ret;
  372. /* ip1[:port1][,ip2[:port2]...] */
  373. ret = ceph_parse_ips(buf, buf + len, opt->mon_addr, CEPH_MAX_MON,
  374. &opt->num_mon);
  375. if (ret) {
  376. error_plog(&log, "Failed to parse monitor IPs: %d", ret);
  377. return ret;
  378. }
  379. return 0;
  380. }
  381. EXPORT_SYMBOL(ceph_parse_mon_ips);
  382. int ceph_parse_param(struct fs_parameter *param, struct ceph_options *opt,
  383. struct fc_log *l)
  384. {
  385. struct fs_parse_result result;
  386. int token, err;
  387. struct p_log log = {.prefix = "libceph", .log = l};
  388. token = __fs_parse(&log, ceph_parameters, param, &result);
  389. dout("%s fs_parse '%s' token %d\n", __func__, param->key, token);
  390. if (token < 0)
  391. return token;
  392. switch (token) {
  393. case Opt_ip:
  394. err = ceph_parse_ips(param->string,
  395. param->string + param->size,
  396. &opt->my_addr,
  397. 1, NULL);
  398. if (err) {
  399. error_plog(&log, "Failed to parse ip: %d", err);
  400. return err;
  401. }
  402. opt->flags |= CEPH_OPT_MYIP;
  403. break;
  404. case Opt_fsid:
  405. err = parse_fsid(param->string, &opt->fsid);
  406. if (err) {
  407. error_plog(&log, "Failed to parse fsid: %d", err);
  408. return err;
  409. }
  410. opt->flags |= CEPH_OPT_FSID;
  411. break;
  412. case Opt_name:
  413. kfree(opt->name);
  414. opt->name = param->string;
  415. param->string = NULL;
  416. break;
  417. case Opt_secret:
  418. ceph_crypto_key_destroy(opt->key);
  419. kfree(opt->key);
  420. opt->key = kzalloc(sizeof(*opt->key), GFP_KERNEL);
  421. if (!opt->key)
  422. return -ENOMEM;
  423. err = ceph_crypto_key_unarmor(opt->key, param->string);
  424. if (err) {
  425. error_plog(&log, "Failed to parse secret: %d", err);
  426. return err;
  427. }
  428. break;
  429. case Opt_key:
  430. ceph_crypto_key_destroy(opt->key);
  431. kfree(opt->key);
  432. opt->key = kzalloc(sizeof(*opt->key), GFP_KERNEL);
  433. if (!opt->key)
  434. return -ENOMEM;
  435. return get_secret(opt->key, param->string, &log);
  436. case Opt_crush_location:
  437. ceph_clear_crush_locs(&opt->crush_locs);
  438. err = ceph_parse_crush_location(param->string,
  439. &opt->crush_locs);
  440. if (err) {
  441. error_plog(&log, "Failed to parse CRUSH location: %d",
  442. err);
  443. return err;
  444. }
  445. break;
  446. case Opt_read_from_replica:
  447. switch (result.uint_32) {
  448. case Opt_read_from_replica_no:
  449. opt->read_from_replica = 0;
  450. break;
  451. case Opt_read_from_replica_balance:
  452. opt->read_from_replica = CEPH_OSD_FLAG_BALANCE_READS;
  453. break;
  454. case Opt_read_from_replica_localize:
  455. opt->read_from_replica = CEPH_OSD_FLAG_LOCALIZE_READS;
  456. break;
  457. default:
  458. BUG();
  459. }
  460. break;
  461. case Opt_osdtimeout:
  462. warn_plog(&log, "Ignoring osdtimeout");
  463. break;
  464. case Opt_osdkeepalivetimeout:
  465. /* 0 isn't well defined right now, reject it */
  466. if (result.uint_32 < 1 || result.uint_32 > INT_MAX / 1000)
  467. goto out_of_range;
  468. opt->osd_keepalive_timeout =
  469. msecs_to_jiffies(result.uint_32 * 1000);
  470. break;
  471. case Opt_osd_idle_ttl:
  472. /* 0 isn't well defined right now, reject it */
  473. if (result.uint_32 < 1 || result.uint_32 > INT_MAX / 1000)
  474. goto out_of_range;
  475. opt->osd_idle_ttl = msecs_to_jiffies(result.uint_32 * 1000);
  476. break;
  477. case Opt_mount_timeout:
  478. /* 0 is "wait forever" (i.e. infinite timeout) */
  479. if (result.uint_32 > INT_MAX / 1000)
  480. goto out_of_range;
  481. opt->mount_timeout = msecs_to_jiffies(result.uint_32 * 1000);
  482. break;
  483. case Opt_osd_request_timeout:
  484. /* 0 is "wait forever" (i.e. infinite timeout) */
  485. if (result.uint_32 > INT_MAX / 1000)
  486. goto out_of_range;
  487. opt->osd_request_timeout =
  488. msecs_to_jiffies(result.uint_32 * 1000);
  489. break;
  490. case Opt_share:
  491. if (!result.negated)
  492. opt->flags &= ~CEPH_OPT_NOSHARE;
  493. else
  494. opt->flags |= CEPH_OPT_NOSHARE;
  495. break;
  496. case Opt_crc:
  497. if (!result.negated)
  498. opt->flags &= ~CEPH_OPT_NOCRC;
  499. else
  500. opt->flags |= CEPH_OPT_NOCRC;
  501. break;
  502. case Opt_cephx_require_signatures:
  503. if (!result.negated)
  504. opt->flags &= ~CEPH_OPT_NOMSGAUTH;
  505. else
  506. opt->flags |= CEPH_OPT_NOMSGAUTH;
  507. break;
  508. case Opt_cephx_sign_messages:
  509. if (!result.negated)
  510. opt->flags &= ~CEPH_OPT_NOMSGSIGN;
  511. else
  512. opt->flags |= CEPH_OPT_NOMSGSIGN;
  513. break;
  514. case Opt_tcp_nodelay:
  515. if (!result.negated)
  516. opt->flags |= CEPH_OPT_TCP_NODELAY;
  517. else
  518. opt->flags &= ~CEPH_OPT_TCP_NODELAY;
  519. break;
  520. case Opt_abort_on_full:
  521. opt->flags |= CEPH_OPT_ABORT_ON_FULL;
  522. break;
  523. default:
  524. BUG();
  525. }
  526. return 0;
  527. out_of_range:
  528. return inval_plog(&log, "%s out of range", param->key);
  529. }
  530. EXPORT_SYMBOL(ceph_parse_param);
  531. int ceph_print_client_options(struct seq_file *m, struct ceph_client *client,
  532. bool show_all)
  533. {
  534. struct ceph_options *opt = client->options;
  535. size_t pos = m->count;
  536. struct rb_node *n;
  537. if (opt->name) {
  538. seq_puts(m, "name=");
  539. seq_escape(m, opt->name, ", \t\n\\");
  540. seq_putc(m, ',');
  541. }
  542. if (opt->key)
  543. seq_puts(m, "secret=<hidden>,");
  544. if (!RB_EMPTY_ROOT(&opt->crush_locs)) {
  545. seq_puts(m, "crush_location=");
  546. for (n = rb_first(&opt->crush_locs); ; ) {
  547. struct crush_loc_node *loc =
  548. rb_entry(n, struct crush_loc_node, cl_node);
  549. seq_printf(m, "%s:%s", loc->cl_loc.cl_type_name,
  550. loc->cl_loc.cl_name);
  551. n = rb_next(n);
  552. if (!n)
  553. break;
  554. seq_putc(m, '|');
  555. }
  556. seq_putc(m, ',');
  557. }
  558. if (opt->read_from_replica == CEPH_OSD_FLAG_BALANCE_READS) {
  559. seq_puts(m, "read_from_replica=balance,");
  560. } else if (opt->read_from_replica == CEPH_OSD_FLAG_LOCALIZE_READS) {
  561. seq_puts(m, "read_from_replica=localize,");
  562. }
  563. if (opt->flags & CEPH_OPT_FSID)
  564. seq_printf(m, "fsid=%pU,", &opt->fsid);
  565. if (opt->flags & CEPH_OPT_NOSHARE)
  566. seq_puts(m, "noshare,");
  567. if (opt->flags & CEPH_OPT_NOCRC)
  568. seq_puts(m, "nocrc,");
  569. if (opt->flags & CEPH_OPT_NOMSGAUTH)
  570. seq_puts(m, "nocephx_require_signatures,");
  571. if (opt->flags & CEPH_OPT_NOMSGSIGN)
  572. seq_puts(m, "nocephx_sign_messages,");
  573. if ((opt->flags & CEPH_OPT_TCP_NODELAY) == 0)
  574. seq_puts(m, "notcp_nodelay,");
  575. if (show_all && (opt->flags & CEPH_OPT_ABORT_ON_FULL))
  576. seq_puts(m, "abort_on_full,");
  577. if (opt->mount_timeout != CEPH_MOUNT_TIMEOUT_DEFAULT)
  578. seq_printf(m, "mount_timeout=%d,",
  579. jiffies_to_msecs(opt->mount_timeout) / 1000);
  580. if (opt->osd_idle_ttl != CEPH_OSD_IDLE_TTL_DEFAULT)
  581. seq_printf(m, "osd_idle_ttl=%d,",
  582. jiffies_to_msecs(opt->osd_idle_ttl) / 1000);
  583. if (opt->osd_keepalive_timeout != CEPH_OSD_KEEPALIVE_DEFAULT)
  584. seq_printf(m, "osdkeepalivetimeout=%d,",
  585. jiffies_to_msecs(opt->osd_keepalive_timeout) / 1000);
  586. if (opt->osd_request_timeout != CEPH_OSD_REQUEST_TIMEOUT_DEFAULT)
  587. seq_printf(m, "osd_request_timeout=%d,",
  588. jiffies_to_msecs(opt->osd_request_timeout) / 1000);
  589. /* drop redundant comma */
  590. if (m->count != pos)
  591. m->count--;
  592. return 0;
  593. }
  594. EXPORT_SYMBOL(ceph_print_client_options);
  595. struct ceph_entity_addr *ceph_client_addr(struct ceph_client *client)
  596. {
  597. return &client->msgr.inst.addr;
  598. }
  599. EXPORT_SYMBOL(ceph_client_addr);
  600. u64 ceph_client_gid(struct ceph_client *client)
  601. {
  602. return client->monc.auth->global_id;
  603. }
  604. EXPORT_SYMBOL(ceph_client_gid);
  605. /*
  606. * create a fresh client instance
  607. */
  608. struct ceph_client *ceph_create_client(struct ceph_options *opt, void *private)
  609. {
  610. struct ceph_client *client;
  611. struct ceph_entity_addr *myaddr = NULL;
  612. int err;
  613. err = wait_for_random_bytes();
  614. if (err < 0)
  615. return ERR_PTR(err);
  616. client = kzalloc(sizeof(*client), GFP_KERNEL);
  617. if (client == NULL)
  618. return ERR_PTR(-ENOMEM);
  619. client->private = private;
  620. client->options = opt;
  621. mutex_init(&client->mount_mutex);
  622. init_waitqueue_head(&client->auth_wq);
  623. client->auth_err = 0;
  624. client->extra_mon_dispatch = NULL;
  625. client->supported_features = CEPH_FEATURES_SUPPORTED_DEFAULT;
  626. client->required_features = CEPH_FEATURES_REQUIRED_DEFAULT;
  627. if (!ceph_test_opt(client, NOMSGAUTH))
  628. client->required_features |= CEPH_FEATURE_MSG_AUTH;
  629. /* msgr */
  630. if (ceph_test_opt(client, MYIP))
  631. myaddr = &client->options->my_addr;
  632. ceph_messenger_init(&client->msgr, myaddr);
  633. /* subsystems */
  634. err = ceph_monc_init(&client->monc, client);
  635. if (err < 0)
  636. goto fail;
  637. err = ceph_osdc_init(&client->osdc, client);
  638. if (err < 0)
  639. goto fail_monc;
  640. return client;
  641. fail_monc:
  642. ceph_monc_stop(&client->monc);
  643. fail:
  644. ceph_messenger_fini(&client->msgr);
  645. kfree(client);
  646. return ERR_PTR(err);
  647. }
  648. EXPORT_SYMBOL(ceph_create_client);
  649. void ceph_destroy_client(struct ceph_client *client)
  650. {
  651. dout("destroy_client %p\n", client);
  652. atomic_set(&client->msgr.stopping, 1);
  653. /* unmount */
  654. ceph_osdc_stop(&client->osdc);
  655. ceph_monc_stop(&client->monc);
  656. ceph_messenger_fini(&client->msgr);
  657. ceph_debugfs_client_cleanup(client);
  658. ceph_destroy_options(client->options);
  659. kfree(client);
  660. dout("destroy_client %p done\n", client);
  661. }
  662. EXPORT_SYMBOL(ceph_destroy_client);
  663. void ceph_reset_client_addr(struct ceph_client *client)
  664. {
  665. ceph_messenger_reset_nonce(&client->msgr);
  666. ceph_monc_reopen_session(&client->monc);
  667. ceph_osdc_reopen_osds(&client->osdc);
  668. }
  669. EXPORT_SYMBOL(ceph_reset_client_addr);
  670. /*
  671. * true if we have the mon map (and have thus joined the cluster)
  672. */
  673. static bool have_mon_and_osd_map(struct ceph_client *client)
  674. {
  675. return client->monc.monmap && client->monc.monmap->epoch &&
  676. client->osdc.osdmap && client->osdc.osdmap->epoch;
  677. }
  678. /*
  679. * mount: join the ceph cluster, and open root directory.
  680. */
  681. int __ceph_open_session(struct ceph_client *client, unsigned long started)
  682. {
  683. unsigned long timeout = client->options->mount_timeout;
  684. long err;
  685. /* open session, and wait for mon and osd maps */
  686. err = ceph_monc_open_session(&client->monc);
  687. if (err < 0)
  688. return err;
  689. while (!have_mon_and_osd_map(client)) {
  690. if (timeout && time_after_eq(jiffies, started + timeout))
  691. return -ETIMEDOUT;
  692. /* wait */
  693. dout("mount waiting for mon_map\n");
  694. err = wait_event_interruptible_timeout(client->auth_wq,
  695. have_mon_and_osd_map(client) || (client->auth_err < 0),
  696. ceph_timeout_jiffies(timeout));
  697. if (err < 0)
  698. return err;
  699. if (client->auth_err < 0)
  700. return client->auth_err;
  701. }
  702. pr_info("client%llu fsid %pU\n", ceph_client_gid(client),
  703. &client->fsid);
  704. ceph_debugfs_client_init(client);
  705. return 0;
  706. }
  707. EXPORT_SYMBOL(__ceph_open_session);
  708. int ceph_open_session(struct ceph_client *client)
  709. {
  710. int ret;
  711. unsigned long started = jiffies; /* note the start time */
  712. dout("open_session start\n");
  713. mutex_lock(&client->mount_mutex);
  714. ret = __ceph_open_session(client, started);
  715. mutex_unlock(&client->mount_mutex);
  716. return ret;
  717. }
  718. EXPORT_SYMBOL(ceph_open_session);
  719. int ceph_wait_for_latest_osdmap(struct ceph_client *client,
  720. unsigned long timeout)
  721. {
  722. u64 newest_epoch;
  723. int ret;
  724. ret = ceph_monc_get_version(&client->monc, "osdmap", &newest_epoch);
  725. if (ret)
  726. return ret;
  727. if (client->osdc.osdmap->epoch >= newest_epoch)
  728. return 0;
  729. ceph_osdc_maybe_request_map(&client->osdc);
  730. return ceph_monc_wait_osdmap(&client->monc, newest_epoch, timeout);
  731. }
  732. EXPORT_SYMBOL(ceph_wait_for_latest_osdmap);
  733. static int __init init_ceph_lib(void)
  734. {
  735. int ret = 0;
  736. ceph_debugfs_init();
  737. ret = ceph_crypto_init();
  738. if (ret < 0)
  739. goto out_debugfs;
  740. ret = ceph_msgr_init();
  741. if (ret < 0)
  742. goto out_crypto;
  743. ret = ceph_osdc_setup();
  744. if (ret < 0)
  745. goto out_msgr;
  746. pr_info("loaded (mon/osd proto %d/%d)\n",
  747. CEPH_MONC_PROTOCOL, CEPH_OSDC_PROTOCOL);
  748. return 0;
  749. out_msgr:
  750. ceph_msgr_exit();
  751. out_crypto:
  752. ceph_crypto_shutdown();
  753. out_debugfs:
  754. ceph_debugfs_cleanup();
  755. return ret;
  756. }
  757. static void __exit exit_ceph_lib(void)
  758. {
  759. dout("exit_ceph_lib\n");
  760. WARN_ON(!ceph_strings_empty());
  761. ceph_osdc_cleanup();
  762. ceph_msgr_exit();
  763. ceph_crypto_shutdown();
  764. ceph_debugfs_cleanup();
  765. }
  766. module_init(init_ceph_lib);
  767. module_exit(exit_ceph_lib);
  768. MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
  769. MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
  770. MODULE_AUTHOR("Patience Warnick <patience@newdream.net>");
  771. MODULE_DESCRIPTION("Ceph core library");
  772. MODULE_LICENSE("GPL");