1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/ceph/ceph_debug.h>
4 #include <linux/backing-dev.h>
5 #include <linux/ctype.h>
7 #include <linux/inet.h>
10 #include <keys/ceph-type.h>
11 #include <linux/module.h>
12 #include <linux/mount.h>
13 #include <linux/nsproxy.h>
14 #include <linux/parser.h>
15 #include <linux/sched.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>
23 #include <linux/ceph/ceph_features.h>
24 #include <linux/ceph/libceph.h>
25 #include <linux/ceph/debugfs.h>
26 #include <linux/ceph/decode.h>
27 #include <linux/ceph/mon_client.h>
28 #include <linux/ceph/auth.h>
33 * Module compatibility interface. For now it doesn't do anything,
34 * but its existence signals a certain level of functionality.
36 * The data buffer is used to pass information both to and from
37 * libceph. The return value indicates whether libceph determines
38 * it is compatible with the caller (from another kernel module),
39 * given the provided data.
41 * The data pointer can be null.
43 bool libceph_compatible(void *data)
47 EXPORT_SYMBOL(libceph_compatible);
49 static int param_get_supported_features(char *buffer,
50 const struct kernel_param *kp)
52 return sprintf(buffer, "0x%llx", CEPH_FEATURES_SUPPORTED_DEFAULT);
54 static const struct kernel_param_ops param_ops_supported_features = {
55 .get = param_get_supported_features,
57 module_param_cb(supported_features, ¶m_ops_supported_features, NULL,
60 const char *ceph_msg_type_name(int type)
63 case CEPH_MSG_SHUTDOWN: return "shutdown";
64 case CEPH_MSG_PING: return "ping";
65 case CEPH_MSG_AUTH: return "auth";
66 case CEPH_MSG_AUTH_REPLY: return "auth_reply";
67 case CEPH_MSG_MON_MAP: return "mon_map";
68 case CEPH_MSG_MON_GET_MAP: return "mon_get_map";
69 case CEPH_MSG_MON_SUBSCRIBE: return "mon_subscribe";
70 case CEPH_MSG_MON_SUBSCRIBE_ACK: return "mon_subscribe_ack";
71 case CEPH_MSG_STATFS: return "statfs";
72 case CEPH_MSG_STATFS_REPLY: return "statfs_reply";
73 case CEPH_MSG_MON_GET_VERSION: return "mon_get_version";
74 case CEPH_MSG_MON_GET_VERSION_REPLY: return "mon_get_version_reply";
75 case CEPH_MSG_MDS_MAP: return "mds_map";
76 case CEPH_MSG_FS_MAP_USER: return "fs_map_user";
77 case CEPH_MSG_CLIENT_SESSION: return "client_session";
78 case CEPH_MSG_CLIENT_RECONNECT: return "client_reconnect";
79 case CEPH_MSG_CLIENT_REQUEST: return "client_request";
80 case CEPH_MSG_CLIENT_REQUEST_FORWARD: return "client_request_forward";
81 case CEPH_MSG_CLIENT_REPLY: return "client_reply";
82 case CEPH_MSG_CLIENT_CAPS: return "client_caps";
83 case CEPH_MSG_CLIENT_CAPRELEASE: return "client_cap_release";
84 case CEPH_MSG_CLIENT_QUOTA: return "client_quota";
85 case CEPH_MSG_CLIENT_SNAP: return "client_snap";
86 case CEPH_MSG_CLIENT_LEASE: return "client_lease";
87 case CEPH_MSG_POOLOP_REPLY: return "poolop_reply";
88 case CEPH_MSG_POOLOP: return "poolop";
89 case CEPH_MSG_MON_COMMAND: return "mon_command";
90 case CEPH_MSG_MON_COMMAND_ACK: return "mon_command_ack";
91 case CEPH_MSG_OSD_MAP: return "osd_map";
92 case CEPH_MSG_OSD_OP: return "osd_op";
93 case CEPH_MSG_OSD_OPREPLY: return "osd_opreply";
94 case CEPH_MSG_WATCH_NOTIFY: return "watch_notify";
95 case CEPH_MSG_OSD_BACKOFF: return "osd_backoff";
96 default: return "unknown";
99 EXPORT_SYMBOL(ceph_msg_type_name);
102 * Initially learn our fsid, or verify an fsid matches.
104 int ceph_check_fsid(struct ceph_client *client, struct ceph_fsid *fsid)
106 if (client->have_fsid) {
107 if (ceph_fsid_compare(&client->fsid, fsid)) {
108 pr_err("bad fsid, had %pU got %pU",
109 &client->fsid, fsid);
113 memcpy(&client->fsid, fsid, sizeof(*fsid));
117 EXPORT_SYMBOL(ceph_check_fsid);
119 static int strcmp_null(const char *s1, const char *s2)
127 return strcmp(s1, s2);
130 int ceph_compare_options(struct ceph_options *new_opt,
131 struct ceph_client *client)
133 struct ceph_options *opt1 = new_opt;
134 struct ceph_options *opt2 = client->options;
135 int ofs = offsetof(struct ceph_options, mon_addr);
140 * Don't bother comparing options if network namespaces don't
143 if (!net_eq(current->nsproxy->net_ns, read_pnet(&client->msgr.net)))
146 ret = memcmp(opt1, opt2, ofs);
150 ret = strcmp_null(opt1->name, opt2->name);
154 if (opt1->key && !opt2->key)
156 if (!opt1->key && opt2->key)
158 if (opt1->key && opt2->key) {
159 if (opt1->key->type != opt2->key->type)
161 if (opt1->key->created.tv_sec != opt2->key->created.tv_sec)
163 if (opt1->key->created.tv_nsec != opt2->key->created.tv_nsec)
165 if (opt1->key->len != opt2->key->len)
167 if (opt1->key->key && !opt2->key->key)
169 if (!opt1->key->key && opt2->key->key)
171 if (opt1->key->key && opt2->key->key) {
172 ret = memcmp(opt1->key->key, opt2->key->key, opt1->key->len);
178 /* any matching mon ip implies a match */
179 for (i = 0; i < opt1->num_mon; i++) {
180 if (ceph_monmap_contains(client->monc.monmap,
186 EXPORT_SYMBOL(ceph_compare_options);
188 void *ceph_kvmalloc(size_t size, gfp_t flags)
190 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
191 void *ptr = kmalloc(size, flags | __GFP_NOWARN);
196 return __vmalloc(size, flags, PAGE_KERNEL);
200 static int parse_fsid(const char *str, struct ceph_fsid *fsid)
207 dout("parse_fsid '%s'\n", str);
209 while (*str && i < 16) {
214 if (!isxdigit(str[0]) || !isxdigit(str[1]))
218 if (sscanf(tmp, "%x", &d) < 1)
220 fsid->fsid[i] = d & 0xff;
227 dout("parse_fsid ret %d got fsid %pU\n", err, fsid);
236 Opt_osdkeepalivetimeout,
239 Opt_osd_request_timeout,
248 /* string args above */
253 Opt_cephx_require_signatures,
254 Opt_nocephx_require_signatures,
255 Opt_cephx_sign_messages,
256 Opt_nocephx_sign_messages,
262 static match_table_t opt_tokens = {
263 {Opt_osdtimeout, "osdtimeout=%d"},
264 {Opt_osdkeepalivetimeout, "osdkeepalive=%d"},
265 {Opt_mount_timeout, "mount_timeout=%d"},
266 {Opt_osd_idle_ttl, "osd_idle_ttl=%d"},
267 {Opt_osd_request_timeout, "osd_request_timeout=%d"},
269 {Opt_fsid, "fsid=%s"},
270 {Opt_name, "name=%s"},
271 {Opt_secret, "secret=%s"},
274 /* string args above */
275 {Opt_share, "share"},
276 {Opt_noshare, "noshare"},
278 {Opt_nocrc, "nocrc"},
279 {Opt_cephx_require_signatures, "cephx_require_signatures"},
280 {Opt_nocephx_require_signatures, "nocephx_require_signatures"},
281 {Opt_cephx_sign_messages, "cephx_sign_messages"},
282 {Opt_nocephx_sign_messages, "nocephx_sign_messages"},
283 {Opt_tcp_nodelay, "tcp_nodelay"},
284 {Opt_notcp_nodelay, "notcp_nodelay"},
285 {Opt_abort_on_full, "abort_on_full"},
289 void ceph_destroy_options(struct ceph_options *opt)
291 dout("destroy_options %p\n", opt);
294 ceph_crypto_key_destroy(opt->key);
297 kfree(opt->mon_addr);
300 EXPORT_SYMBOL(ceph_destroy_options);
302 /* get secret from key store */
303 static int get_secret(struct ceph_crypto_key *dst, const char *name) {
307 struct ceph_crypto_key *ckey;
309 ukey = request_key(&key_type_ceph, name, NULL);
311 /* request_key errors don't map nicely to mount(2)
312 errors; don't even try, but still printk */
313 key_err = PTR_ERR(ukey);
316 pr_warn("ceph: Mount failed due to key not found: %s\n",
320 pr_warn("ceph: Mount failed due to expired key: %s\n",
324 pr_warn("ceph: Mount failed due to revoked key: %s\n",
328 pr_warn("ceph: Mount failed due to unknown key error %d: %s\n",
335 ckey = ukey->payload.data[0];
336 err = ceph_crypto_key_clone(dst, ckey);
339 /* pass through, err is 0 */
347 struct ceph_options *
348 ceph_parse_options(char *options, const char *dev_name,
349 const char *dev_name_end,
350 int (*parse_extra_token)(char *c, void *private),
353 struct ceph_options *opt;
356 substring_t argstr[MAX_OPT_ARGS];
358 opt = kzalloc(sizeof(*opt), GFP_KERNEL);
360 return ERR_PTR(-ENOMEM);
361 opt->mon_addr = kcalloc(CEPH_MAX_MON, sizeof(*opt->mon_addr),
366 dout("parse_options %p options '%s' dev_name '%s'\n", opt, options,
369 /* start with defaults */
370 opt->flags = CEPH_OPT_DEFAULT;
371 opt->osd_keepalive_timeout = CEPH_OSD_KEEPALIVE_DEFAULT;
372 opt->mount_timeout = CEPH_MOUNT_TIMEOUT_DEFAULT;
373 opt->osd_idle_ttl = CEPH_OSD_IDLE_TTL_DEFAULT;
374 opt->osd_request_timeout = CEPH_OSD_REQUEST_TIMEOUT_DEFAULT;
377 /* ip1[:port1][,ip2[:port2]...] */
378 err = ceph_parse_ips(dev_name, dev_name_end, opt->mon_addr,
379 CEPH_MAX_MON, &opt->num_mon);
383 /* parse mount options */
384 while ((c = strsep(&options, ",")) != NULL) {
389 token = match_token((char *)c, opt_tokens, argstr);
390 if (token < 0 && parse_extra_token) {
392 err = parse_extra_token((char *)c, private);
394 pr_err("bad option at '%s'\n", c);
399 if (token < Opt_last_int) {
400 err = match_int(&argstr[0], &intval);
402 pr_err("bad option arg (not int) at '%s'\n", c);
405 dout("got int token %d val %d\n", token, intval);
406 } else if (token > Opt_last_int && token < Opt_last_string) {
407 dout("got string token %d val %s\n", token,
410 dout("got token %d\n", token);
414 err = ceph_parse_ips(argstr[0].from,
420 opt->flags |= CEPH_OPT_MYIP;
424 err = parse_fsid(argstr[0].from, &opt->fsid);
426 opt->flags |= CEPH_OPT_FSID;
430 opt->name = kstrndup(argstr[0].from,
431 argstr[0].to-argstr[0].from,
439 ceph_crypto_key_destroy(opt->key);
442 opt->key = kzalloc(sizeof(*opt->key), GFP_KERNEL);
447 err = ceph_crypto_key_unarmor(opt->key, argstr[0].from);
452 ceph_crypto_key_destroy(opt->key);
455 opt->key = kzalloc(sizeof(*opt->key), GFP_KERNEL);
460 err = get_secret(opt->key, argstr[0].from);
467 pr_warn("ignoring deprecated osdtimeout option\n");
469 case Opt_osdkeepalivetimeout:
470 /* 0 isn't well defined right now, reject it */
471 if (intval < 1 || intval > INT_MAX / 1000) {
472 pr_err("osdkeepalive out of range\n");
476 opt->osd_keepalive_timeout =
477 msecs_to_jiffies(intval * 1000);
479 case Opt_osd_idle_ttl:
480 /* 0 isn't well defined right now, reject it */
481 if (intval < 1 || intval > INT_MAX / 1000) {
482 pr_err("osd_idle_ttl out of range\n");
486 opt->osd_idle_ttl = msecs_to_jiffies(intval * 1000);
488 case Opt_mount_timeout:
489 /* 0 is "wait forever" (i.e. infinite timeout) */
490 if (intval < 0 || intval > INT_MAX / 1000) {
491 pr_err("mount_timeout out of range\n");
495 opt->mount_timeout = msecs_to_jiffies(intval * 1000);
497 case Opt_osd_request_timeout:
498 /* 0 is "wait forever" (i.e. infinite timeout) */
499 if (intval < 0 || intval > INT_MAX / 1000) {
500 pr_err("osd_request_timeout out of range\n");
504 opt->osd_request_timeout = msecs_to_jiffies(intval * 1000);
508 opt->flags &= ~CEPH_OPT_NOSHARE;
511 opt->flags |= CEPH_OPT_NOSHARE;
515 opt->flags &= ~CEPH_OPT_NOCRC;
518 opt->flags |= CEPH_OPT_NOCRC;
521 case Opt_cephx_require_signatures:
522 opt->flags &= ~CEPH_OPT_NOMSGAUTH;
524 case Opt_nocephx_require_signatures:
525 opt->flags |= CEPH_OPT_NOMSGAUTH;
527 case Opt_cephx_sign_messages:
528 opt->flags &= ~CEPH_OPT_NOMSGSIGN;
530 case Opt_nocephx_sign_messages:
531 opt->flags |= CEPH_OPT_NOMSGSIGN;
534 case Opt_tcp_nodelay:
535 opt->flags |= CEPH_OPT_TCP_NODELAY;
537 case Opt_notcp_nodelay:
538 opt->flags &= ~CEPH_OPT_TCP_NODELAY;
541 case Opt_abort_on_full:
542 opt->flags |= CEPH_OPT_ABORT_ON_FULL;
554 ceph_destroy_options(opt);
557 EXPORT_SYMBOL(ceph_parse_options);
559 int ceph_print_client_options(struct seq_file *m, struct ceph_client *client,
562 struct ceph_options *opt = client->options;
563 size_t pos = m->count;
566 seq_puts(m, "name=");
567 seq_escape(m, opt->name, ", \t\n\\");
571 seq_puts(m, "secret=<hidden>,");
573 if (opt->flags & CEPH_OPT_FSID)
574 seq_printf(m, "fsid=%pU,", &opt->fsid);
575 if (opt->flags & CEPH_OPT_NOSHARE)
576 seq_puts(m, "noshare,");
577 if (opt->flags & CEPH_OPT_NOCRC)
578 seq_puts(m, "nocrc,");
579 if (opt->flags & CEPH_OPT_NOMSGAUTH)
580 seq_puts(m, "nocephx_require_signatures,");
581 if (opt->flags & CEPH_OPT_NOMSGSIGN)
582 seq_puts(m, "nocephx_sign_messages,");
583 if ((opt->flags & CEPH_OPT_TCP_NODELAY) == 0)
584 seq_puts(m, "notcp_nodelay,");
585 if (show_all && (opt->flags & CEPH_OPT_ABORT_ON_FULL))
586 seq_puts(m, "abort_on_full,");
588 if (opt->mount_timeout != CEPH_MOUNT_TIMEOUT_DEFAULT)
589 seq_printf(m, "mount_timeout=%d,",
590 jiffies_to_msecs(opt->mount_timeout) / 1000);
591 if (opt->osd_idle_ttl != CEPH_OSD_IDLE_TTL_DEFAULT)
592 seq_printf(m, "osd_idle_ttl=%d,",
593 jiffies_to_msecs(opt->osd_idle_ttl) / 1000);
594 if (opt->osd_keepalive_timeout != CEPH_OSD_KEEPALIVE_DEFAULT)
595 seq_printf(m, "osdkeepalivetimeout=%d,",
596 jiffies_to_msecs(opt->osd_keepalive_timeout) / 1000);
597 if (opt->osd_request_timeout != CEPH_OSD_REQUEST_TIMEOUT_DEFAULT)
598 seq_printf(m, "osd_request_timeout=%d,",
599 jiffies_to_msecs(opt->osd_request_timeout) / 1000);
601 /* drop redundant comma */
607 EXPORT_SYMBOL(ceph_print_client_options);
609 struct ceph_entity_addr *ceph_client_addr(struct ceph_client *client)
611 return &client->msgr.inst.addr;
613 EXPORT_SYMBOL(ceph_client_addr);
615 u64 ceph_client_gid(struct ceph_client *client)
617 return client->monc.auth->global_id;
619 EXPORT_SYMBOL(ceph_client_gid);
622 * create a fresh client instance
624 struct ceph_client *ceph_create_client(struct ceph_options *opt, void *private)
626 struct ceph_client *client;
627 struct ceph_entity_addr *myaddr = NULL;
630 err = wait_for_random_bytes();
634 client = kzalloc(sizeof(*client), GFP_KERNEL);
636 return ERR_PTR(-ENOMEM);
638 client->private = private;
639 client->options = opt;
641 mutex_init(&client->mount_mutex);
642 init_waitqueue_head(&client->auth_wq);
643 client->auth_err = 0;
645 client->extra_mon_dispatch = NULL;
646 client->supported_features = CEPH_FEATURES_SUPPORTED_DEFAULT;
647 client->required_features = CEPH_FEATURES_REQUIRED_DEFAULT;
649 if (!ceph_test_opt(client, NOMSGAUTH))
650 client->required_features |= CEPH_FEATURE_MSG_AUTH;
653 if (ceph_test_opt(client, MYIP))
654 myaddr = &client->options->my_addr;
656 ceph_messenger_init(&client->msgr, myaddr);
659 err = ceph_monc_init(&client->monc, client);
662 err = ceph_osdc_init(&client->osdc, client);
669 ceph_monc_stop(&client->monc);
671 ceph_messenger_fini(&client->msgr);
675 EXPORT_SYMBOL(ceph_create_client);
677 void ceph_destroy_client(struct ceph_client *client)
679 dout("destroy_client %p\n", client);
681 atomic_set(&client->msgr.stopping, 1);
684 ceph_osdc_stop(&client->osdc);
685 ceph_monc_stop(&client->monc);
686 ceph_messenger_fini(&client->msgr);
688 ceph_debugfs_client_cleanup(client);
690 ceph_destroy_options(client->options);
693 dout("destroy_client %p done\n", client);
695 EXPORT_SYMBOL(ceph_destroy_client);
698 * true if we have the mon map (and have thus joined the cluster)
700 static bool have_mon_and_osd_map(struct ceph_client *client)
702 return client->monc.monmap && client->monc.monmap->epoch &&
703 client->osdc.osdmap && client->osdc.osdmap->epoch;
707 * mount: join the ceph cluster, and open root directory.
709 int __ceph_open_session(struct ceph_client *client, unsigned long started)
711 unsigned long timeout = client->options->mount_timeout;
714 /* open session, and wait for mon and osd maps */
715 err = ceph_monc_open_session(&client->monc);
719 while (!have_mon_and_osd_map(client)) {
720 if (timeout && time_after_eq(jiffies, started + timeout))
724 dout("mount waiting for mon_map\n");
725 err = wait_event_interruptible_timeout(client->auth_wq,
726 have_mon_and_osd_map(client) || (client->auth_err < 0),
727 ceph_timeout_jiffies(timeout));
730 if (client->auth_err < 0)
731 return client->auth_err;
734 pr_info("client%llu fsid %pU\n", ceph_client_gid(client),
736 ceph_debugfs_client_init(client);
740 EXPORT_SYMBOL(__ceph_open_session);
742 int ceph_open_session(struct ceph_client *client)
745 unsigned long started = jiffies; /* note the start time */
747 dout("open_session start\n");
748 mutex_lock(&client->mount_mutex);
750 ret = __ceph_open_session(client, started);
752 mutex_unlock(&client->mount_mutex);
755 EXPORT_SYMBOL(ceph_open_session);
757 int ceph_wait_for_latest_osdmap(struct ceph_client *client,
758 unsigned long timeout)
763 ret = ceph_monc_get_version(&client->monc, "osdmap", &newest_epoch);
767 if (client->osdc.osdmap->epoch >= newest_epoch)
770 ceph_osdc_maybe_request_map(&client->osdc);
771 return ceph_monc_wait_osdmap(&client->monc, newest_epoch, timeout);
773 EXPORT_SYMBOL(ceph_wait_for_latest_osdmap);
775 static int __init init_ceph_lib(void)
779 ret = ceph_debugfs_init();
783 ret = ceph_crypto_init();
787 ret = ceph_msgr_init();
791 ret = ceph_osdc_setup();
795 pr_info("loaded (mon/osd proto %d/%d)\n",
796 CEPH_MONC_PROTOCOL, CEPH_OSDC_PROTOCOL);
803 ceph_crypto_shutdown();
805 ceph_debugfs_cleanup();
810 static void __exit exit_ceph_lib(void)
812 dout("exit_ceph_lib\n");
813 WARN_ON(!ceph_strings_empty());
817 ceph_crypto_shutdown();
818 ceph_debugfs_cleanup();
821 module_init(init_ceph_lib);
822 module_exit(exit_ceph_lib);
824 MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
825 MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
826 MODULE_AUTHOR("Patience Warnick <patience@newdream.net>");
827 MODULE_DESCRIPTION("Ceph core library");
828 MODULE_LICENSE("GPL");