xref: /openbmc/linux/net/ceph/ceph_common.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
23d14c5d2SYehuda Sadeh 
33d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h>
43d14c5d2SYehuda Sadeh #include <linux/backing-dev.h>
53d14c5d2SYehuda Sadeh #include <linux/ctype.h>
63d14c5d2SYehuda Sadeh #include <linux/fs.h>
73d14c5d2SYehuda Sadeh #include <linux/inet.h>
83d14c5d2SYehuda Sadeh #include <linux/in6.h>
9e2c3d29bSTommi Virtanen #include <linux/key.h>
104b2a58abSTommi Virtanen #include <keys/ceph-type.h>
113d14c5d2SYehuda Sadeh #include <linux/module.h>
123d14c5d2SYehuda Sadeh #include <linux/mount.h>
13757856d2SIlya Dryomov #include <linux/nsproxy.h>
1482995cc6SDavid Howells #include <linux/fs_parser.h>
153d14c5d2SYehuda Sadeh #include <linux/sched.h>
1610c12851SIlya Dryomov #include <linux/sched/mm.h>
173d14c5d2SYehuda Sadeh #include <linux/seq_file.h>
183d14c5d2SYehuda Sadeh #include <linux/slab.h>
193d14c5d2SYehuda Sadeh #include <linux/statfs.h>
203d14c5d2SYehuda Sadeh #include <linux/string.h>
21eeb0bed5SIlya Dryomov #include <linux/vmalloc.h>
223d14c5d2SYehuda Sadeh 
233d14c5d2SYehuda Sadeh 
241fe60e51SSage Weil #include <linux/ceph/ceph_features.h>
253d14c5d2SYehuda Sadeh #include <linux/ceph/libceph.h>
263d14c5d2SYehuda Sadeh #include <linux/ceph/debugfs.h>
273d14c5d2SYehuda Sadeh #include <linux/ceph/decode.h>
283d14c5d2SYehuda Sadeh #include <linux/ceph/mon_client.h>
293d14c5d2SYehuda Sadeh #include <linux/ceph/auth.h>
308323c3aaSTommi Virtanen #include "crypto.h"
313d14c5d2SYehuda Sadeh 
323d14c5d2SYehuda Sadeh 
3372fe25e3SAlex Elder /*
3472fe25e3SAlex Elder  * Module compatibility interface.  For now it doesn't do anything,
3572fe25e3SAlex Elder  * but its existence signals a certain level of functionality.
3672fe25e3SAlex Elder  *
3772fe25e3SAlex Elder  * The data buffer is used to pass information both to and from
3872fe25e3SAlex Elder  * libceph.  The return value indicates whether libceph determines
3972fe25e3SAlex Elder  * it is compatible with the caller (from another kernel module),
4072fe25e3SAlex Elder  * given the provided data.
4172fe25e3SAlex Elder  *
4272fe25e3SAlex Elder  * The data pointer can be null.
4372fe25e3SAlex Elder  */
libceph_compatible(void * data)4472fe25e3SAlex Elder bool libceph_compatible(void *data)
4572fe25e3SAlex Elder {
461e32d34cSAlex Elder 	return true;
4772fe25e3SAlex Elder }
4872fe25e3SAlex Elder EXPORT_SYMBOL(libceph_compatible);
493d14c5d2SYehuda Sadeh 
param_get_supported_features(char * buffer,const struct kernel_param * kp)50d6a3408aSIlya Dryomov static int param_get_supported_features(char *buffer,
51d6a3408aSIlya Dryomov 					const struct kernel_param *kp)
52d6a3408aSIlya Dryomov {
53d6a3408aSIlya Dryomov 	return sprintf(buffer, "0x%llx", CEPH_FEATURES_SUPPORTED_DEFAULT);
54d6a3408aSIlya Dryomov }
55d6a3408aSIlya Dryomov static const struct kernel_param_ops param_ops_supported_features = {
56d6a3408aSIlya Dryomov 	.get = param_get_supported_features,
57d6a3408aSIlya Dryomov };
58d6a3408aSIlya Dryomov module_param_cb(supported_features, &param_ops_supported_features, NULL,
59d6444062SJoe Perches 		0444);
60d6a3408aSIlya Dryomov 
ceph_msg_type_name(int type)613d14c5d2SYehuda Sadeh const char *ceph_msg_type_name(int type)
623d14c5d2SYehuda Sadeh {
633d14c5d2SYehuda Sadeh 	switch (type) {
643d14c5d2SYehuda Sadeh 	case CEPH_MSG_SHUTDOWN: return "shutdown";
653d14c5d2SYehuda Sadeh 	case CEPH_MSG_PING: return "ping";
663d14c5d2SYehuda Sadeh 	case CEPH_MSG_AUTH: return "auth";
673d14c5d2SYehuda Sadeh 	case CEPH_MSG_AUTH_REPLY: return "auth_reply";
683d14c5d2SYehuda Sadeh 	case CEPH_MSG_MON_MAP: return "mon_map";
693d14c5d2SYehuda Sadeh 	case CEPH_MSG_MON_GET_MAP: return "mon_get_map";
703d14c5d2SYehuda Sadeh 	case CEPH_MSG_MON_SUBSCRIBE: return "mon_subscribe";
713d14c5d2SYehuda Sadeh 	case CEPH_MSG_MON_SUBSCRIBE_ACK: return "mon_subscribe_ack";
723d14c5d2SYehuda Sadeh 	case CEPH_MSG_STATFS: return "statfs";
733d14c5d2SYehuda Sadeh 	case CEPH_MSG_STATFS_REPLY: return "statfs_reply";
74513a8243SIlya Dryomov 	case CEPH_MSG_MON_GET_VERSION: return "mon_get_version";
75513a8243SIlya Dryomov 	case CEPH_MSG_MON_GET_VERSION_REPLY: return "mon_get_version_reply";
763d14c5d2SYehuda Sadeh 	case CEPH_MSG_MDS_MAP: return "mds_map";
77f2f87877SChengguang Xu 	case CEPH_MSG_FS_MAP_USER: return "fs_map_user";
783d14c5d2SYehuda Sadeh 	case CEPH_MSG_CLIENT_SESSION: return "client_session";
793d14c5d2SYehuda Sadeh 	case CEPH_MSG_CLIENT_RECONNECT: return "client_reconnect";
803d14c5d2SYehuda Sadeh 	case CEPH_MSG_CLIENT_REQUEST: return "client_request";
813d14c5d2SYehuda Sadeh 	case CEPH_MSG_CLIENT_REQUEST_FORWARD: return "client_request_forward";
823d14c5d2SYehuda Sadeh 	case CEPH_MSG_CLIENT_REPLY: return "client_reply";
833d14c5d2SYehuda Sadeh 	case CEPH_MSG_CLIENT_CAPS: return "client_caps";
843d14c5d2SYehuda Sadeh 	case CEPH_MSG_CLIENT_CAPRELEASE: return "client_cap_release";
85fb18a575SLuis Henriques 	case CEPH_MSG_CLIENT_QUOTA: return "client_quota";
863d14c5d2SYehuda Sadeh 	case CEPH_MSG_CLIENT_SNAP: return "client_snap";
873d14c5d2SYehuda Sadeh 	case CEPH_MSG_CLIENT_LEASE: return "client_lease";
88f2f87877SChengguang Xu 	case CEPH_MSG_POOLOP_REPLY: return "poolop_reply";
89f2f87877SChengguang Xu 	case CEPH_MSG_POOLOP: return "poolop";
90f2f87877SChengguang Xu 	case CEPH_MSG_MON_COMMAND: return "mon_command";
91f2f87877SChengguang Xu 	case CEPH_MSG_MON_COMMAND_ACK: return "mon_command_ack";
923d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_MAP: return "osd_map";
933d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_OP: return "osd_op";
943d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_OPREPLY: return "osd_opreply";
95a40c4f10SYehuda Sadeh 	case CEPH_MSG_WATCH_NOTIFY: return "watch_notify";
96a02a946dSIlya Dryomov 	case CEPH_MSG_OSD_BACKOFF: return "osd_backoff";
973d14c5d2SYehuda Sadeh 	default: return "unknown";
983d14c5d2SYehuda Sadeh 	}
993d14c5d2SYehuda Sadeh }
1003d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_msg_type_name);
1013d14c5d2SYehuda Sadeh 
1023d14c5d2SYehuda Sadeh /*
1033d14c5d2SYehuda Sadeh  * Initially learn our fsid, or verify an fsid matches.
1043d14c5d2SYehuda Sadeh  */
ceph_check_fsid(struct ceph_client * client,struct ceph_fsid * fsid)1053d14c5d2SYehuda Sadeh int ceph_check_fsid(struct ceph_client *client, struct ceph_fsid *fsid)
1063d14c5d2SYehuda Sadeh {
1073d14c5d2SYehuda Sadeh 	if (client->have_fsid) {
1083d14c5d2SYehuda Sadeh 		if (ceph_fsid_compare(&client->fsid, fsid)) {
1093d14c5d2SYehuda Sadeh 			pr_err("bad fsid, had %pU got %pU",
1103d14c5d2SYehuda Sadeh 			       &client->fsid, fsid);
1113d14c5d2SYehuda Sadeh 			return -1;
1123d14c5d2SYehuda Sadeh 		}
1133d14c5d2SYehuda Sadeh 	} else {
1143d14c5d2SYehuda Sadeh 		memcpy(&client->fsid, fsid, sizeof(*fsid));
1153d14c5d2SYehuda Sadeh 	}
1163d14c5d2SYehuda Sadeh 	return 0;
1173d14c5d2SYehuda Sadeh }
1183d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_check_fsid);
1193d14c5d2SYehuda Sadeh 
strcmp_null(const char * s1,const char * s2)1203d14c5d2SYehuda Sadeh static int strcmp_null(const char *s1, const char *s2)
1213d14c5d2SYehuda Sadeh {
1223d14c5d2SYehuda Sadeh 	if (!s1 && !s2)
1233d14c5d2SYehuda Sadeh 		return 0;
1243d14c5d2SYehuda Sadeh 	if (s1 && !s2)
1253d14c5d2SYehuda Sadeh 		return -1;
1263d14c5d2SYehuda Sadeh 	if (!s1 && s2)
1273d14c5d2SYehuda Sadeh 		return 1;
1283d14c5d2SYehuda Sadeh 	return strcmp(s1, s2);
1293d14c5d2SYehuda Sadeh }
1303d14c5d2SYehuda Sadeh 
ceph_compare_options(struct ceph_options * new_opt,struct ceph_client * client)1313d14c5d2SYehuda Sadeh int ceph_compare_options(struct ceph_options *new_opt,
1323d14c5d2SYehuda Sadeh 			 struct ceph_client *client)
1333d14c5d2SYehuda Sadeh {
1343d14c5d2SYehuda Sadeh 	struct ceph_options *opt1 = new_opt;
1353d14c5d2SYehuda Sadeh 	struct ceph_options *opt2 = client->options;
1363d14c5d2SYehuda Sadeh 	int ofs = offsetof(struct ceph_options, mon_addr);
1373d14c5d2SYehuda Sadeh 	int i;
1383d14c5d2SYehuda Sadeh 	int ret;
1393d14c5d2SYehuda Sadeh 
140757856d2SIlya Dryomov 	/*
141757856d2SIlya Dryomov 	 * Don't bother comparing options if network namespaces don't
142757856d2SIlya Dryomov 	 * match.
143757856d2SIlya Dryomov 	 */
144757856d2SIlya Dryomov 	if (!net_eq(current->nsproxy->net_ns, read_pnet(&client->msgr.net)))
145757856d2SIlya Dryomov 		return -1;
146757856d2SIlya Dryomov 
1473d14c5d2SYehuda Sadeh 	ret = memcmp(opt1, opt2, ofs);
1483d14c5d2SYehuda Sadeh 	if (ret)
1493d14c5d2SYehuda Sadeh 		return ret;
1503d14c5d2SYehuda Sadeh 
1513d14c5d2SYehuda Sadeh 	ret = strcmp_null(opt1->name, opt2->name);
1523d14c5d2SYehuda Sadeh 	if (ret)
1533d14c5d2SYehuda Sadeh 		return ret;
1543d14c5d2SYehuda Sadeh 
1558323c3aaSTommi Virtanen 	if (opt1->key && !opt2->key)
1568323c3aaSTommi Virtanen 		return -1;
1578323c3aaSTommi Virtanen 	if (!opt1->key && opt2->key)
1588323c3aaSTommi Virtanen 		return 1;
1598323c3aaSTommi Virtanen 	if (opt1->key && opt2->key) {
1608323c3aaSTommi Virtanen 		if (opt1->key->type != opt2->key->type)
1618323c3aaSTommi Virtanen 			return -1;
1628323c3aaSTommi Virtanen 		if (opt1->key->created.tv_sec != opt2->key->created.tv_sec)
1638323c3aaSTommi Virtanen 			return -1;
1648323c3aaSTommi Virtanen 		if (opt1->key->created.tv_nsec != opt2->key->created.tv_nsec)
1658323c3aaSTommi Virtanen 			return -1;
1668323c3aaSTommi Virtanen 		if (opt1->key->len != opt2->key->len)
1678323c3aaSTommi Virtanen 			return -1;
1688323c3aaSTommi Virtanen 		if (opt1->key->key && !opt2->key->key)
1698323c3aaSTommi Virtanen 			return -1;
1708323c3aaSTommi Virtanen 		if (!opt1->key->key && opt2->key->key)
1718323c3aaSTommi Virtanen 			return 1;
1728323c3aaSTommi Virtanen 		if (opt1->key->key && opt2->key->key) {
1738323c3aaSTommi Virtanen 			ret = memcmp(opt1->key->key, opt2->key->key, opt1->key->len);
1743d14c5d2SYehuda Sadeh 			if (ret)
1753d14c5d2SYehuda Sadeh 				return ret;
1768323c3aaSTommi Virtanen 		}
1778323c3aaSTommi Virtanen 	}
1783d14c5d2SYehuda Sadeh 
17945e6aa9fSIlya Dryomov 	ret = ceph_compare_crush_locs(&opt1->crush_locs, &opt2->crush_locs);
18045e6aa9fSIlya Dryomov 	if (ret)
18145e6aa9fSIlya Dryomov 		return ret;
18245e6aa9fSIlya Dryomov 
1833d14c5d2SYehuda Sadeh 	/* any matching mon ip implies a match */
1843d14c5d2SYehuda Sadeh 	for (i = 0; i < opt1->num_mon; i++) {
1853d14c5d2SYehuda Sadeh 		if (ceph_monmap_contains(client->monc.monmap,
1863d14c5d2SYehuda Sadeh 				 &opt1->mon_addr[i]))
1873d14c5d2SYehuda Sadeh 			return 0;
1883d14c5d2SYehuda Sadeh 	}
1893d14c5d2SYehuda Sadeh 	return -1;
1903d14c5d2SYehuda Sadeh }
1913d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_compare_options);
1923d14c5d2SYehuda Sadeh 
ceph_parse_fsid(const char * str,struct ceph_fsid * fsid)1934153c7fcSVenky Shankar int ceph_parse_fsid(const char *str, struct ceph_fsid *fsid)
1943d14c5d2SYehuda Sadeh {
1953d14c5d2SYehuda Sadeh 	int i = 0;
1963d14c5d2SYehuda Sadeh 	char tmp[3];
1973d14c5d2SYehuda Sadeh 	int err = -EINVAL;
1983d14c5d2SYehuda Sadeh 	int d;
1993d14c5d2SYehuda Sadeh 
2004153c7fcSVenky Shankar 	dout("%s '%s'\n", __func__, str);
2013d14c5d2SYehuda Sadeh 	tmp[2] = 0;
2023d14c5d2SYehuda Sadeh 	while (*str && i < 16) {
2033d14c5d2SYehuda Sadeh 		if (ispunct(*str)) {
2043d14c5d2SYehuda Sadeh 			str++;
2053d14c5d2SYehuda Sadeh 			continue;
2063d14c5d2SYehuda Sadeh 		}
2073d14c5d2SYehuda Sadeh 		if (!isxdigit(str[0]) || !isxdigit(str[1]))
2083d14c5d2SYehuda Sadeh 			break;
2093d14c5d2SYehuda Sadeh 		tmp[0] = str[0];
2103d14c5d2SYehuda Sadeh 		tmp[1] = str[1];
2113d14c5d2SYehuda Sadeh 		if (sscanf(tmp, "%x", &d) < 1)
2123d14c5d2SYehuda Sadeh 			break;
2133d14c5d2SYehuda Sadeh 		fsid->fsid[i] = d & 0xff;
2143d14c5d2SYehuda Sadeh 		i++;
2153d14c5d2SYehuda Sadeh 		str += 2;
2163d14c5d2SYehuda Sadeh 	}
2173d14c5d2SYehuda Sadeh 
2183d14c5d2SYehuda Sadeh 	if (i == 16)
2193d14c5d2SYehuda Sadeh 		err = 0;
2204153c7fcSVenky Shankar 	dout("%s ret %d got fsid %pU\n", __func__, err, fsid);
2213d14c5d2SYehuda Sadeh 	return err;
2223d14c5d2SYehuda Sadeh }
2234153c7fcSVenky Shankar EXPORT_SYMBOL(ceph_parse_fsid);
2243d14c5d2SYehuda Sadeh 
2253d14c5d2SYehuda Sadeh /*
2263d14c5d2SYehuda Sadeh  * ceph options
2273d14c5d2SYehuda Sadeh  */
2283d14c5d2SYehuda Sadeh enum {
2293d14c5d2SYehuda Sadeh 	Opt_osdkeepalivetimeout,
2303d14c5d2SYehuda Sadeh 	Opt_mount_timeout,
2313d14c5d2SYehuda Sadeh 	Opt_osd_idle_ttl,
2327cc5e38fSIlya Dryomov 	Opt_osd_request_timeout,
2333d14c5d2SYehuda Sadeh 	/* int args above */
2343d14c5d2SYehuda Sadeh 	Opt_fsid,
2353d14c5d2SYehuda Sadeh 	Opt_name,
2363d14c5d2SYehuda Sadeh 	Opt_secret,
237e2c3d29bSTommi Virtanen 	Opt_key,
2383d14c5d2SYehuda Sadeh 	Opt_ip,
23945e6aa9fSIlya Dryomov 	Opt_crush_location,
2408ad44d5eSIlya Dryomov 	Opt_read_from_replica,
24100498b99SIlya Dryomov 	Opt_ms_mode,
2423d14c5d2SYehuda Sadeh 	/* string args above */
243cffaba15SAlex Elder 	Opt_share,
244cffaba15SAlex Elder 	Opt_crc,
245a3fc9800SYan, Zheng 	Opt_cephx_require_signatures,
246a51983e4SIlya Dryomov 	Opt_cephx_sign_messages,
247ba988f87SChaitanya Huilgol 	Opt_tcp_nodelay,
24802b2f549SDongsheng Yang 	Opt_abort_on_full,
249*038b8d1dSIlya Dryomov 	Opt_rxbounce,
2503d14c5d2SYehuda Sadeh };
2513d14c5d2SYehuda Sadeh 
2528ad44d5eSIlya Dryomov enum {
2538ad44d5eSIlya Dryomov 	Opt_read_from_replica_no,
2548ad44d5eSIlya Dryomov 	Opt_read_from_replica_balance,
2558ad44d5eSIlya Dryomov 	Opt_read_from_replica_localize,
2568ad44d5eSIlya Dryomov };
2578ad44d5eSIlya Dryomov 
2588ad44d5eSIlya Dryomov static const struct constant_table ceph_param_read_from_replica[] = {
2598ad44d5eSIlya Dryomov 	{"no",		Opt_read_from_replica_no},
2608ad44d5eSIlya Dryomov 	{"balance",	Opt_read_from_replica_balance},
2618ad44d5eSIlya Dryomov 	{"localize",	Opt_read_from_replica_localize},
2628ad44d5eSIlya Dryomov 	{}
2638ad44d5eSIlya Dryomov };
2648ad44d5eSIlya Dryomov 
26500498b99SIlya Dryomov enum ceph_ms_mode {
26600498b99SIlya Dryomov 	Opt_ms_mode_legacy,
26700498b99SIlya Dryomov 	Opt_ms_mode_crc,
26800498b99SIlya Dryomov 	Opt_ms_mode_secure,
26900498b99SIlya Dryomov 	Opt_ms_mode_prefer_crc,
27000498b99SIlya Dryomov 	Opt_ms_mode_prefer_secure
27100498b99SIlya Dryomov };
27200498b99SIlya Dryomov 
27300498b99SIlya Dryomov static const struct constant_table ceph_param_ms_mode[] = {
27400498b99SIlya Dryomov 	{"legacy",		Opt_ms_mode_legacy},
27500498b99SIlya Dryomov 	{"crc",			Opt_ms_mode_crc},
27600498b99SIlya Dryomov 	{"secure",		Opt_ms_mode_secure},
27700498b99SIlya Dryomov 	{"prefer-crc",		Opt_ms_mode_prefer_crc},
27800498b99SIlya Dryomov 	{"prefer-secure",	Opt_ms_mode_prefer_secure},
27900498b99SIlya Dryomov 	{}
28000498b99SIlya Dryomov };
28100498b99SIlya Dryomov 
282d7167b14SAl Viro static const struct fs_parameter_spec ceph_parameters[] = {
28382995cc6SDavid Howells 	fsparam_flag	("abort_on_full",		Opt_abort_on_full),
284afd56e78SIlya Dryomov 	__fsparam	(NULL, "cephx_require_signatures", Opt_cephx_require_signatures,
285afd56e78SIlya Dryomov 			 fs_param_neg_with_no|fs_param_deprecated, NULL),
28682995cc6SDavid Howells 	fsparam_flag_no ("cephx_sign_messages",		Opt_cephx_sign_messages),
28782995cc6SDavid Howells 	fsparam_flag_no ("crc",				Opt_crc),
28845e6aa9fSIlya Dryomov 	fsparam_string	("crush_location",		Opt_crush_location),
28982995cc6SDavid Howells 	fsparam_string	("fsid",			Opt_fsid),
29082995cc6SDavid Howells 	fsparam_string	("ip",				Opt_ip),
29182995cc6SDavid Howells 	fsparam_string	("key",				Opt_key),
29282995cc6SDavid Howells 	fsparam_u32	("mount_timeout",		Opt_mount_timeout),
29382995cc6SDavid Howells 	fsparam_string	("name",			Opt_name),
29482995cc6SDavid Howells 	fsparam_u32	("osd_idle_ttl",		Opt_osd_idle_ttl),
29582995cc6SDavid Howells 	fsparam_u32	("osd_request_timeout",		Opt_osd_request_timeout),
29682995cc6SDavid Howells 	fsparam_u32	("osdkeepalive",		Opt_osdkeepalivetimeout),
2978ad44d5eSIlya Dryomov 	fsparam_enum	("read_from_replica",		Opt_read_from_replica,
2988ad44d5eSIlya Dryomov 			 ceph_param_read_from_replica),
299*038b8d1dSIlya Dryomov 	fsparam_flag	("rxbounce",			Opt_rxbounce),
30000498b99SIlya Dryomov 	fsparam_enum	("ms_mode",			Opt_ms_mode,
30100498b99SIlya Dryomov 			 ceph_param_ms_mode),
30282995cc6SDavid Howells 	fsparam_string	("secret",			Opt_secret),
30382995cc6SDavid Howells 	fsparam_flag_no ("share",			Opt_share),
30482995cc6SDavid Howells 	fsparam_flag_no ("tcp_nodelay",			Opt_tcp_nodelay),
30582995cc6SDavid Howells 	{}
3063d14c5d2SYehuda Sadeh };
3073d14c5d2SYehuda Sadeh 
ceph_alloc_options(void)30882995cc6SDavid Howells struct ceph_options *ceph_alloc_options(void)
30982995cc6SDavid Howells {
31082995cc6SDavid Howells 	struct ceph_options *opt;
31182995cc6SDavid Howells 
31282995cc6SDavid Howells 	opt = kzalloc(sizeof(*opt), GFP_KERNEL);
31382995cc6SDavid Howells 	if (!opt)
31482995cc6SDavid Howells 		return NULL;
31582995cc6SDavid Howells 
31645e6aa9fSIlya Dryomov 	opt->crush_locs = RB_ROOT;
31782995cc6SDavid Howells 	opt->mon_addr = kcalloc(CEPH_MAX_MON, sizeof(*opt->mon_addr),
31882995cc6SDavid Howells 				GFP_KERNEL);
31982995cc6SDavid Howells 	if (!opt->mon_addr) {
32082995cc6SDavid Howells 		kfree(opt);
32182995cc6SDavid Howells 		return NULL;
32282995cc6SDavid Howells 	}
32382995cc6SDavid Howells 
32482995cc6SDavid Howells 	opt->flags = CEPH_OPT_DEFAULT;
32582995cc6SDavid Howells 	opt->osd_keepalive_timeout = CEPH_OSD_KEEPALIVE_DEFAULT;
32682995cc6SDavid Howells 	opt->mount_timeout = CEPH_MOUNT_TIMEOUT_DEFAULT;
32782995cc6SDavid Howells 	opt->osd_idle_ttl = CEPH_OSD_IDLE_TTL_DEFAULT;
32882995cc6SDavid Howells 	opt->osd_request_timeout = CEPH_OSD_REQUEST_TIMEOUT_DEFAULT;
32922d2cfdfSIlya Dryomov 	opt->read_from_replica = CEPH_READ_FROM_REPLICA_DEFAULT;
33000498b99SIlya Dryomov 	opt->con_modes[0] = CEPH_CON_MODE_UNKNOWN;
33100498b99SIlya Dryomov 	opt->con_modes[1] = CEPH_CON_MODE_UNKNOWN;
33282995cc6SDavid Howells 	return opt;
33382995cc6SDavid Howells }
33482995cc6SDavid Howells EXPORT_SYMBOL(ceph_alloc_options);
33582995cc6SDavid Howells 
ceph_destroy_options(struct ceph_options * opt)3363d14c5d2SYehuda Sadeh void ceph_destroy_options(struct ceph_options *opt)
3373d14c5d2SYehuda Sadeh {
3383d14c5d2SYehuda Sadeh 	dout("destroy_options %p\n", opt);
33982995cc6SDavid Howells 	if (!opt)
34082995cc6SDavid Howells 		return;
34182995cc6SDavid Howells 
34245e6aa9fSIlya Dryomov 	ceph_clear_crush_locs(&opt->crush_locs);
3433d14c5d2SYehuda Sadeh 	kfree(opt->name);
3448323c3aaSTommi Virtanen 	if (opt->key) {
3458323c3aaSTommi Virtanen 		ceph_crypto_key_destroy(opt->key);
3468323c3aaSTommi Virtanen 		kfree(opt->key);
3478323c3aaSTommi Virtanen 	}
3481cad7893SNoah Watkins 	kfree(opt->mon_addr);
3493d14c5d2SYehuda Sadeh 	kfree(opt);
3503d14c5d2SYehuda Sadeh }
3513d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_destroy_options);
3523d14c5d2SYehuda Sadeh 
353e2c3d29bSTommi Virtanen /* get secret from key store */
get_secret(struct ceph_crypto_key * dst,const char * name,struct p_log * log)35482995cc6SDavid Howells static int get_secret(struct ceph_crypto_key *dst, const char *name,
3552c3f3dc3SAl Viro 		      struct p_log *log)
35682995cc6SDavid Howells {
357e2c3d29bSTommi Virtanen 	struct key *ukey;
358e2c3d29bSTommi Virtanen 	int key_err;
359e2c3d29bSTommi Virtanen 	int err = 0;
3604b2a58abSTommi Virtanen 	struct ceph_crypto_key *ckey;
361e2c3d29bSTommi Virtanen 
3624b2a58abSTommi Virtanen 	ukey = request_key(&key_type_ceph, name, NULL);
363bad87216SYueHaibing 	if (IS_ERR(ukey)) {
364e2c3d29bSTommi Virtanen 		/* request_key errors don't map nicely to mount(2)
365e2c3d29bSTommi Virtanen 		   errors; don't even try, but still printk */
366e2c3d29bSTommi Virtanen 		key_err = PTR_ERR(ukey);
367e2c3d29bSTommi Virtanen 		switch (key_err) {
368e2c3d29bSTommi Virtanen 		case -ENOKEY:
3692c3f3dc3SAl Viro 			error_plog(log, "Failed due to key not found: %s",
370b9a67899SJoe Perches 			       name);
371e2c3d29bSTommi Virtanen 			break;
372e2c3d29bSTommi Virtanen 		case -EKEYEXPIRED:
3732c3f3dc3SAl Viro 			error_plog(log, "Failed due to expired key: %s",
374b9a67899SJoe Perches 			       name);
375e2c3d29bSTommi Virtanen 			break;
376e2c3d29bSTommi Virtanen 		case -EKEYREVOKED:
3772c3f3dc3SAl Viro 			error_plog(log, "Failed due to revoked key: %s",
378b9a67899SJoe Perches 			       name);
379e2c3d29bSTommi Virtanen 			break;
380e2c3d29bSTommi Virtanen 		default:
3812c3f3dc3SAl Viro 			error_plog(log, "Failed due to key error %d: %s",
382b9a67899SJoe Perches 			       key_err, name);
383e2c3d29bSTommi Virtanen 		}
384e2c3d29bSTommi Virtanen 		err = -EPERM;
385e2c3d29bSTommi Virtanen 		goto out;
386e2c3d29bSTommi Virtanen 	}
387e2c3d29bSTommi Virtanen 
388146aa8b1SDavid Howells 	ckey = ukey->payload.data[0];
3894b2a58abSTommi Virtanen 	err = ceph_crypto_key_clone(dst, ckey);
390e2c3d29bSTommi Virtanen 	if (err)
391e2c3d29bSTommi Virtanen 		goto out_key;
392e2c3d29bSTommi Virtanen 	/* pass through, err is 0 */
393e2c3d29bSTommi Virtanen 
394e2c3d29bSTommi Virtanen out_key:
395e2c3d29bSTommi Virtanen 	key_put(ukey);
396e2c3d29bSTommi Virtanen out:
397e2c3d29bSTommi Virtanen 	return err;
398e2c3d29bSTommi Virtanen }
399e2c3d29bSTommi Virtanen 
ceph_parse_mon_ips(const char * buf,size_t len,struct ceph_options * opt,struct fc_log * l,char delim)40082995cc6SDavid Howells int ceph_parse_mon_ips(const char *buf, size_t len, struct ceph_options *opt,
4012d7c86a8SVenky Shankar 		       struct fc_log *l, char delim)
4023d14c5d2SYehuda Sadeh {
403c80c98f0SAl Viro 	struct p_log log = {.prefix = "libceph", .log = l};
40482995cc6SDavid Howells 	int ret;
4053d14c5d2SYehuda Sadeh 
4062d7c86a8SVenky Shankar 	/* ip1[:port1][<delim>ip2[:port2]...] */
40782995cc6SDavid Howells 	ret = ceph_parse_ips(buf, buf + len, opt->mon_addr, CEPH_MAX_MON,
4082d7c86a8SVenky Shankar 			     &opt->num_mon, delim);
40982995cc6SDavid Howells 	if (ret) {
4102c3f3dc3SAl Viro 		error_plog(&log, "Failed to parse monitor IPs: %d", ret);
41182995cc6SDavid Howells 		return ret;
41282995cc6SDavid Howells 	}
4133d14c5d2SYehuda Sadeh 
41482995cc6SDavid Howells 	return 0;
4153d14c5d2SYehuda Sadeh }
41682995cc6SDavid Howells EXPORT_SYMBOL(ceph_parse_mon_ips);
41782995cc6SDavid Howells 
ceph_parse_param(struct fs_parameter * param,struct ceph_options * opt,struct fc_log * l)41882995cc6SDavid Howells int ceph_parse_param(struct fs_parameter *param, struct ceph_options *opt,
419c80c98f0SAl Viro 		     struct fc_log *l)
42082995cc6SDavid Howells {
42182995cc6SDavid Howells 	struct fs_parse_result result;
42282995cc6SDavid Howells 	int token, err;
423c80c98f0SAl Viro 	struct p_log log = {.prefix = "libceph", .log = l};
42482995cc6SDavid Howells 
425d7167b14SAl Viro 	token = __fs_parse(&log, ceph_parameters, param, &result);
42682995cc6SDavid Howells 	dout("%s fs_parse '%s' token %d\n", __func__, param->key, token);
42782995cc6SDavid Howells 	if (token < 0)
42882995cc6SDavid Howells 		return token;
42982995cc6SDavid Howells 
4303d14c5d2SYehuda Sadeh 	switch (token) {
4313d14c5d2SYehuda Sadeh 	case Opt_ip:
43282995cc6SDavid Howells 		err = ceph_parse_ips(param->string,
43382995cc6SDavid Howells 				     param->string + param->size,
4342d7c86a8SVenky Shankar 				     &opt->my_addr, 1, NULL, ',');
43582995cc6SDavid Howells 		if (err) {
4362c3f3dc3SAl Viro 			error_plog(&log, "Failed to parse ip: %d", err);
43782995cc6SDavid Howells 			return err;
43882995cc6SDavid Howells 		}
4393d14c5d2SYehuda Sadeh 		opt->flags |= CEPH_OPT_MYIP;
4403d14c5d2SYehuda Sadeh 		break;
4413d14c5d2SYehuda Sadeh 
4423d14c5d2SYehuda Sadeh 	case Opt_fsid:
4434153c7fcSVenky Shankar 		err = ceph_parse_fsid(param->string, &opt->fsid);
44482995cc6SDavid Howells 		if (err) {
4452c3f3dc3SAl Viro 			error_plog(&log, "Failed to parse fsid: %d", err);
44682995cc6SDavid Howells 			return err;
44782995cc6SDavid Howells 		}
4483d14c5d2SYehuda Sadeh 		opt->flags |= CEPH_OPT_FSID;
4493d14c5d2SYehuda Sadeh 		break;
4503d14c5d2SYehuda Sadeh 	case Opt_name:
451937441f3SChengguang Xu 		kfree(opt->name);
45282995cc6SDavid Howells 		opt->name = param->string;
45382995cc6SDavid Howells 		param->string = NULL;
4543d14c5d2SYehuda Sadeh 		break;
4553d14c5d2SYehuda Sadeh 	case Opt_secret:
456937441f3SChengguang Xu 		ceph_crypto_key_destroy(opt->key);
457937441f3SChengguang Xu 		kfree(opt->key);
458937441f3SChengguang Xu 
4598323c3aaSTommi Virtanen 		opt->key = kzalloc(sizeof(*opt->key), GFP_KERNEL);
46082995cc6SDavid Howells 		if (!opt->key)
46182995cc6SDavid Howells 			return -ENOMEM;
46282995cc6SDavid Howells 		err = ceph_crypto_key_unarmor(opt->key, param->string);
46382995cc6SDavid Howells 		if (err) {
4642c3f3dc3SAl Viro 			error_plog(&log, "Failed to parse secret: %d", err);
46582995cc6SDavid Howells 			return err;
4668323c3aaSTommi Virtanen 		}
4673d14c5d2SYehuda Sadeh 		break;
468e2c3d29bSTommi Virtanen 	case Opt_key:
469937441f3SChengguang Xu 		ceph_crypto_key_destroy(opt->key);
470937441f3SChengguang Xu 		kfree(opt->key);
471937441f3SChengguang Xu 
472e2c3d29bSTommi Virtanen 		opt->key = kzalloc(sizeof(*opt->key), GFP_KERNEL);
47382995cc6SDavid Howells 		if (!opt->key)
47482995cc6SDavid Howells 			return -ENOMEM;
4752c3f3dc3SAl Viro 		return get_secret(opt->key, param->string, &log);
47645e6aa9fSIlya Dryomov 	case Opt_crush_location:
47745e6aa9fSIlya Dryomov 		ceph_clear_crush_locs(&opt->crush_locs);
47845e6aa9fSIlya Dryomov 		err = ceph_parse_crush_location(param->string,
47945e6aa9fSIlya Dryomov 						&opt->crush_locs);
48045e6aa9fSIlya Dryomov 		if (err) {
48145e6aa9fSIlya Dryomov 			error_plog(&log, "Failed to parse CRUSH location: %d",
48245e6aa9fSIlya Dryomov 				   err);
48345e6aa9fSIlya Dryomov 			return err;
48445e6aa9fSIlya Dryomov 		}
48545e6aa9fSIlya Dryomov 		break;
4868ad44d5eSIlya Dryomov 	case Opt_read_from_replica:
4878ad44d5eSIlya Dryomov 		switch (result.uint_32) {
4888ad44d5eSIlya Dryomov 		case Opt_read_from_replica_no:
48922d2cfdfSIlya Dryomov 			opt->read_from_replica = 0;
4908ad44d5eSIlya Dryomov 			break;
4918ad44d5eSIlya Dryomov 		case Opt_read_from_replica_balance:
49222d2cfdfSIlya Dryomov 			opt->read_from_replica = CEPH_OSD_FLAG_BALANCE_READS;
4938ad44d5eSIlya Dryomov 			break;
4948ad44d5eSIlya Dryomov 		case Opt_read_from_replica_localize:
49522d2cfdfSIlya Dryomov 			opt->read_from_replica = CEPH_OSD_FLAG_LOCALIZE_READS;
4968ad44d5eSIlya Dryomov 			break;
4978ad44d5eSIlya Dryomov 		default:
4988ad44d5eSIlya Dryomov 			BUG();
4998ad44d5eSIlya Dryomov 		}
5008ad44d5eSIlya Dryomov 		break;
50100498b99SIlya Dryomov 	case Opt_ms_mode:
50200498b99SIlya Dryomov 		switch (result.uint_32) {
50300498b99SIlya Dryomov 		case Opt_ms_mode_legacy:
50400498b99SIlya Dryomov 			opt->con_modes[0] = CEPH_CON_MODE_UNKNOWN;
50500498b99SIlya Dryomov 			opt->con_modes[1] = CEPH_CON_MODE_UNKNOWN;
50600498b99SIlya Dryomov 			break;
50700498b99SIlya Dryomov 		case Opt_ms_mode_crc:
50800498b99SIlya Dryomov 			opt->con_modes[0] = CEPH_CON_MODE_CRC;
50900498b99SIlya Dryomov 			opt->con_modes[1] = CEPH_CON_MODE_UNKNOWN;
51000498b99SIlya Dryomov 			break;
51100498b99SIlya Dryomov 		case Opt_ms_mode_secure:
51200498b99SIlya Dryomov 			opt->con_modes[0] = CEPH_CON_MODE_SECURE;
51300498b99SIlya Dryomov 			opt->con_modes[1] = CEPH_CON_MODE_UNKNOWN;
51400498b99SIlya Dryomov 			break;
51500498b99SIlya Dryomov 		case Opt_ms_mode_prefer_crc:
51600498b99SIlya Dryomov 			opt->con_modes[0] = CEPH_CON_MODE_CRC;
51700498b99SIlya Dryomov 			opt->con_modes[1] = CEPH_CON_MODE_SECURE;
51800498b99SIlya Dryomov 			break;
51900498b99SIlya Dryomov 		case Opt_ms_mode_prefer_secure:
52000498b99SIlya Dryomov 			opt->con_modes[0] = CEPH_CON_MODE_SECURE;
52100498b99SIlya Dryomov 			opt->con_modes[1] = CEPH_CON_MODE_CRC;
52200498b99SIlya Dryomov 			break;
52300498b99SIlya Dryomov 		default:
52400498b99SIlya Dryomov 			BUG();
52500498b99SIlya Dryomov 		}
52600498b99SIlya Dryomov 		break;
5273d14c5d2SYehuda Sadeh 
5283d14c5d2SYehuda Sadeh 	case Opt_osdkeepalivetimeout:
529a319bf56SIlya Dryomov 		/* 0 isn't well defined right now, reject it */
53082995cc6SDavid Howells 		if (result.uint_32 < 1 || result.uint_32 > INT_MAX / 1000)
53182995cc6SDavid Howells 			goto out_of_range;
532a319bf56SIlya Dryomov 		opt->osd_keepalive_timeout =
53382995cc6SDavid Howells 		    msecs_to_jiffies(result.uint_32 * 1000);
5343d14c5d2SYehuda Sadeh 		break;
5353d14c5d2SYehuda Sadeh 	case Opt_osd_idle_ttl:
536a319bf56SIlya Dryomov 		/* 0 isn't well defined right now, reject it */
53782995cc6SDavid Howells 		if (result.uint_32 < 1 || result.uint_32 > INT_MAX / 1000)
53882995cc6SDavid Howells 			goto out_of_range;
53982995cc6SDavid Howells 		opt->osd_idle_ttl = msecs_to_jiffies(result.uint_32 * 1000);
5403d14c5d2SYehuda Sadeh 		break;
5413d14c5d2SYehuda Sadeh 	case Opt_mount_timeout:
542a319bf56SIlya Dryomov 		/* 0 is "wait forever" (i.e. infinite timeout) */
54382995cc6SDavid Howells 		if (result.uint_32 > INT_MAX / 1000)
54482995cc6SDavid Howells 			goto out_of_range;
54582995cc6SDavid Howells 		opt->mount_timeout = msecs_to_jiffies(result.uint_32 * 1000);
5463d14c5d2SYehuda Sadeh 		break;
5477cc5e38fSIlya Dryomov 	case Opt_osd_request_timeout:
5487cc5e38fSIlya Dryomov 		/* 0 is "wait forever" (i.e. infinite timeout) */
54982995cc6SDavid Howells 		if (result.uint_32 > INT_MAX / 1000)
55082995cc6SDavid Howells 			goto out_of_range;
55182995cc6SDavid Howells 		opt->osd_request_timeout =
55282995cc6SDavid Howells 		    msecs_to_jiffies(result.uint_32 * 1000);
5537cc5e38fSIlya Dryomov 		break;
5543d14c5d2SYehuda Sadeh 
555cffaba15SAlex Elder 	case Opt_share:
55682995cc6SDavid Howells 		if (!result.negated)
557cffaba15SAlex Elder 			opt->flags &= ~CEPH_OPT_NOSHARE;
55882995cc6SDavid Howells 		else
5593d14c5d2SYehuda Sadeh 			opt->flags |= CEPH_OPT_NOSHARE;
5603d14c5d2SYehuda Sadeh 		break;
561cffaba15SAlex Elder 	case Opt_crc:
56282995cc6SDavid Howells 		if (!result.negated)
563cffaba15SAlex Elder 			opt->flags &= ~CEPH_OPT_NOCRC;
56482995cc6SDavid Howells 		else
5653d14c5d2SYehuda Sadeh 			opt->flags |= CEPH_OPT_NOCRC;
5663d14c5d2SYehuda Sadeh 		break;
567a3fc9800SYan, Zheng 	case Opt_cephx_require_signatures:
56882995cc6SDavid Howells 		if (!result.negated)
569afd56e78SIlya Dryomov 			warn_plog(&log, "Ignoring cephx_require_signatures");
57082995cc6SDavid Howells 		else
571afd56e78SIlya Dryomov 			warn_plog(&log, "Ignoring nocephx_require_signatures, use nocephx_sign_messages");
572a3fc9800SYan, Zheng 		break;
573a51983e4SIlya Dryomov 	case Opt_cephx_sign_messages:
57482995cc6SDavid Howells 		if (!result.negated)
575a51983e4SIlya Dryomov 			opt->flags &= ~CEPH_OPT_NOMSGSIGN;
57682995cc6SDavid Howells 		else
577a51983e4SIlya Dryomov 			opt->flags |= CEPH_OPT_NOMSGSIGN;
578a51983e4SIlya Dryomov 		break;
579ba988f87SChaitanya Huilgol 	case Opt_tcp_nodelay:
58082995cc6SDavid Howells 		if (!result.negated)
581ba988f87SChaitanya Huilgol 			opt->flags |= CEPH_OPT_TCP_NODELAY;
58282995cc6SDavid Howells 		else
583ba988f87SChaitanya Huilgol 			opt->flags &= ~CEPH_OPT_TCP_NODELAY;
584ba988f87SChaitanya Huilgol 		break;
585ba988f87SChaitanya Huilgol 
58602b2f549SDongsheng Yang 	case Opt_abort_on_full:
58702b2f549SDongsheng Yang 		opt->flags |= CEPH_OPT_ABORT_ON_FULL;
58802b2f549SDongsheng Yang 		break;
589*038b8d1dSIlya Dryomov 	case Opt_rxbounce:
590*038b8d1dSIlya Dryomov 		opt->flags |= CEPH_OPT_RXBOUNCE;
591*038b8d1dSIlya Dryomov 		break;
59202b2f549SDongsheng Yang 
5933d14c5d2SYehuda Sadeh 	default:
59482995cc6SDavid Howells 		BUG();
5953d14c5d2SYehuda Sadeh 	}
5963d14c5d2SYehuda Sadeh 
59782995cc6SDavid Howells 	return 0;
5983d14c5d2SYehuda Sadeh 
59982995cc6SDavid Howells out_of_range:
6002c3f3dc3SAl Viro 	return inval_plog(&log, "%s out of range", param->key);
6013d14c5d2SYehuda Sadeh }
60282995cc6SDavid Howells EXPORT_SYMBOL(ceph_parse_param);
6033d14c5d2SYehuda Sadeh 
ceph_print_client_options(struct seq_file * m,struct ceph_client * client,bool show_all)60402b2f549SDongsheng Yang int ceph_print_client_options(struct seq_file *m, struct ceph_client *client,
60502b2f549SDongsheng Yang 			      bool show_all)
606ff40f9aeSIlya Dryomov {
607ff40f9aeSIlya Dryomov 	struct ceph_options *opt = client->options;
608ff40f9aeSIlya Dryomov 	size_t pos = m->count;
60945e6aa9fSIlya Dryomov 	struct rb_node *n;
610ff40f9aeSIlya Dryomov 
611a068acf2SKees Cook 	if (opt->name) {
612a068acf2SKees Cook 		seq_puts(m, "name=");
613a068acf2SKees Cook 		seq_escape(m, opt->name, ", \t\n\\");
614a068acf2SKees Cook 		seq_putc(m, ',');
615a068acf2SKees Cook 	}
616ff40f9aeSIlya Dryomov 	if (opt->key)
617ff40f9aeSIlya Dryomov 		seq_puts(m, "secret=<hidden>,");
618ff40f9aeSIlya Dryomov 
61945e6aa9fSIlya Dryomov 	if (!RB_EMPTY_ROOT(&opt->crush_locs)) {
62045e6aa9fSIlya Dryomov 		seq_puts(m, "crush_location=");
62145e6aa9fSIlya Dryomov 		for (n = rb_first(&opt->crush_locs); ; ) {
62245e6aa9fSIlya Dryomov 			struct crush_loc_node *loc =
62345e6aa9fSIlya Dryomov 			    rb_entry(n, struct crush_loc_node, cl_node);
62445e6aa9fSIlya Dryomov 
62545e6aa9fSIlya Dryomov 			seq_printf(m, "%s:%s", loc->cl_loc.cl_type_name,
62645e6aa9fSIlya Dryomov 				   loc->cl_loc.cl_name);
62745e6aa9fSIlya Dryomov 			n = rb_next(n);
62845e6aa9fSIlya Dryomov 			if (!n)
62945e6aa9fSIlya Dryomov 				break;
63045e6aa9fSIlya Dryomov 
63145e6aa9fSIlya Dryomov 			seq_putc(m, '|');
63245e6aa9fSIlya Dryomov 		}
63345e6aa9fSIlya Dryomov 		seq_putc(m, ',');
63445e6aa9fSIlya Dryomov 	}
63522d2cfdfSIlya Dryomov 	if (opt->read_from_replica == CEPH_OSD_FLAG_BALANCE_READS) {
6368ad44d5eSIlya Dryomov 		seq_puts(m, "read_from_replica=balance,");
63722d2cfdfSIlya Dryomov 	} else if (opt->read_from_replica == CEPH_OSD_FLAG_LOCALIZE_READS) {
6388ad44d5eSIlya Dryomov 		seq_puts(m, "read_from_replica=localize,");
6398ad44d5eSIlya Dryomov 	}
64000498b99SIlya Dryomov 	if (opt->con_modes[0] != CEPH_CON_MODE_UNKNOWN) {
64100498b99SIlya Dryomov 		if (opt->con_modes[0] == CEPH_CON_MODE_CRC &&
64200498b99SIlya Dryomov 		    opt->con_modes[1] == CEPH_CON_MODE_UNKNOWN) {
64300498b99SIlya Dryomov 			seq_puts(m, "ms_mode=crc,");
64400498b99SIlya Dryomov 		} else if (opt->con_modes[0] == CEPH_CON_MODE_SECURE &&
64500498b99SIlya Dryomov 			   opt->con_modes[1] == CEPH_CON_MODE_UNKNOWN) {
64600498b99SIlya Dryomov 			seq_puts(m, "ms_mode=secure,");
64700498b99SIlya Dryomov 		} else if (opt->con_modes[0] == CEPH_CON_MODE_CRC &&
64800498b99SIlya Dryomov 			   opt->con_modes[1] == CEPH_CON_MODE_SECURE) {
64900498b99SIlya Dryomov 			seq_puts(m, "ms_mode=prefer-crc,");
65000498b99SIlya Dryomov 		} else if (opt->con_modes[0] == CEPH_CON_MODE_SECURE &&
65100498b99SIlya Dryomov 			   opt->con_modes[1] == CEPH_CON_MODE_CRC) {
65200498b99SIlya Dryomov 			seq_puts(m, "ms_mode=prefer-secure,");
65300498b99SIlya Dryomov 		}
65400498b99SIlya Dryomov 	}
65545e6aa9fSIlya Dryomov 
656ff40f9aeSIlya Dryomov 	if (opt->flags & CEPH_OPT_FSID)
657ff40f9aeSIlya Dryomov 		seq_printf(m, "fsid=%pU,", &opt->fsid);
658ff40f9aeSIlya Dryomov 	if (opt->flags & CEPH_OPT_NOSHARE)
659ff40f9aeSIlya Dryomov 		seq_puts(m, "noshare,");
660ff40f9aeSIlya Dryomov 	if (opt->flags & CEPH_OPT_NOCRC)
661ff40f9aeSIlya Dryomov 		seq_puts(m, "nocrc,");
662a51983e4SIlya Dryomov 	if (opt->flags & CEPH_OPT_NOMSGSIGN)
663a51983e4SIlya Dryomov 		seq_puts(m, "nocephx_sign_messages,");
664ff40f9aeSIlya Dryomov 	if ((opt->flags & CEPH_OPT_TCP_NODELAY) == 0)
665ff40f9aeSIlya Dryomov 		seq_puts(m, "notcp_nodelay,");
66602b2f549SDongsheng Yang 	if (show_all && (opt->flags & CEPH_OPT_ABORT_ON_FULL))
66702b2f549SDongsheng Yang 		seq_puts(m, "abort_on_full,");
668*038b8d1dSIlya Dryomov 	if (opt->flags & CEPH_OPT_RXBOUNCE)
669*038b8d1dSIlya Dryomov 		seq_puts(m, "rxbounce,");
670ff40f9aeSIlya Dryomov 
671ff40f9aeSIlya Dryomov 	if (opt->mount_timeout != CEPH_MOUNT_TIMEOUT_DEFAULT)
672a319bf56SIlya Dryomov 		seq_printf(m, "mount_timeout=%d,",
673a319bf56SIlya Dryomov 			   jiffies_to_msecs(opt->mount_timeout) / 1000);
674ff40f9aeSIlya Dryomov 	if (opt->osd_idle_ttl != CEPH_OSD_IDLE_TTL_DEFAULT)
675a319bf56SIlya Dryomov 		seq_printf(m, "osd_idle_ttl=%d,",
676a319bf56SIlya Dryomov 			   jiffies_to_msecs(opt->osd_idle_ttl) / 1000);
677ff40f9aeSIlya Dryomov 	if (opt->osd_keepalive_timeout != CEPH_OSD_KEEPALIVE_DEFAULT)
678ff40f9aeSIlya Dryomov 		seq_printf(m, "osdkeepalivetimeout=%d,",
679a319bf56SIlya Dryomov 		    jiffies_to_msecs(opt->osd_keepalive_timeout) / 1000);
6807cc5e38fSIlya Dryomov 	if (opt->osd_request_timeout != CEPH_OSD_REQUEST_TIMEOUT_DEFAULT)
6817cc5e38fSIlya Dryomov 		seq_printf(m, "osd_request_timeout=%d,",
6827cc5e38fSIlya Dryomov 			   jiffies_to_msecs(opt->osd_request_timeout) / 1000);
683ff40f9aeSIlya Dryomov 
684ff40f9aeSIlya Dryomov 	/* drop redundant comma */
685ff40f9aeSIlya Dryomov 	if (m->count != pos)
686ff40f9aeSIlya Dryomov 		m->count--;
687ff40f9aeSIlya Dryomov 
688ff40f9aeSIlya Dryomov 	return 0;
689ff40f9aeSIlya Dryomov }
690ff40f9aeSIlya Dryomov EXPORT_SYMBOL(ceph_print_client_options);
691ff40f9aeSIlya Dryomov 
ceph_client_addr(struct ceph_client * client)692005a07bfSIlya Dryomov struct ceph_entity_addr *ceph_client_addr(struct ceph_client *client)
693005a07bfSIlya Dryomov {
694005a07bfSIlya Dryomov 	return &client->msgr.inst.addr;
695005a07bfSIlya Dryomov }
696005a07bfSIlya Dryomov EXPORT_SYMBOL(ceph_client_addr);
697005a07bfSIlya Dryomov 
ceph_client_gid(struct ceph_client * client)698033268a5SIlya Dryomov u64 ceph_client_gid(struct ceph_client *client)
6993d14c5d2SYehuda Sadeh {
7003d14c5d2SYehuda Sadeh 	return client->monc.auth->global_id;
7013d14c5d2SYehuda Sadeh }
702033268a5SIlya Dryomov EXPORT_SYMBOL(ceph_client_gid);
7033d14c5d2SYehuda Sadeh 
7043d14c5d2SYehuda Sadeh /*
7053d14c5d2SYehuda Sadeh  * create a fresh client instance
7063d14c5d2SYehuda Sadeh  */
ceph_create_client(struct ceph_options * opt,void * private)70774da4a0fSIlya Dryomov struct ceph_client *ceph_create_client(struct ceph_options *opt, void *private)
7083d14c5d2SYehuda Sadeh {
7093d14c5d2SYehuda Sadeh 	struct ceph_client *client;
7106ab00d46SSage Weil 	struct ceph_entity_addr *myaddr = NULL;
711ae5b806aSJason A. Donenfeld 	int err;
712ae5b806aSJason A. Donenfeld 
713ae5b806aSJason A. Donenfeld 	err = wait_for_random_bytes();
714ae5b806aSJason A. Donenfeld 	if (err < 0)
715ae5b806aSJason A. Donenfeld 		return ERR_PTR(err);
7163d14c5d2SYehuda Sadeh 
7173d14c5d2SYehuda Sadeh 	client = kzalloc(sizeof(*client), GFP_KERNEL);
7183d14c5d2SYehuda Sadeh 	if (client == NULL)
7193d14c5d2SYehuda Sadeh 		return ERR_PTR(-ENOMEM);
7203d14c5d2SYehuda Sadeh 
7213d14c5d2SYehuda Sadeh 	client->private = private;
7223d14c5d2SYehuda Sadeh 	client->options = opt;
7233d14c5d2SYehuda Sadeh 
7243d14c5d2SYehuda Sadeh 	mutex_init(&client->mount_mutex);
7253d14c5d2SYehuda Sadeh 	init_waitqueue_head(&client->auth_wq);
7263d14c5d2SYehuda Sadeh 	client->auth_err = 0;
7273d14c5d2SYehuda Sadeh 
7283d14c5d2SYehuda Sadeh 	client->extra_mon_dispatch = NULL;
72974da4a0fSIlya Dryomov 	client->supported_features = CEPH_FEATURES_SUPPORTED_DEFAULT;
73074da4a0fSIlya Dryomov 	client->required_features = CEPH_FEATURES_REQUIRED_DEFAULT;
73174da4a0fSIlya Dryomov 
732afd56e78SIlya Dryomov 	if (!ceph_test_opt(client, NOMSGSIGN))
73374da4a0fSIlya Dryomov 		client->required_features |= CEPH_FEATURE_MSG_AUTH;
7343d14c5d2SYehuda Sadeh 
7356ab00d46SSage Weil 	/* msgr */
7366ab00d46SSage Weil 	if (ceph_test_opt(client, MYIP))
7376ab00d46SSage Weil 		myaddr = &client->options->my_addr;
738ba988f87SChaitanya Huilgol 
739859bff51SIlya Dryomov 	ceph_messenger_init(&client->msgr, myaddr);
7403d14c5d2SYehuda Sadeh 
7413d14c5d2SYehuda Sadeh 	/* subsystems */
7423d14c5d2SYehuda Sadeh 	err = ceph_monc_init(&client->monc, client);
7433d14c5d2SYehuda Sadeh 	if (err < 0)
74415d9882cSAlex Elder 		goto fail;
7453d14c5d2SYehuda Sadeh 	err = ceph_osdc_init(&client->osdc, client);
7463d14c5d2SYehuda Sadeh 	if (err < 0)
7473d14c5d2SYehuda Sadeh 		goto fail_monc;
7483d14c5d2SYehuda Sadeh 
7493d14c5d2SYehuda Sadeh 	return client;
7503d14c5d2SYehuda Sadeh 
7513d14c5d2SYehuda Sadeh fail_monc:
7523d14c5d2SYehuda Sadeh 	ceph_monc_stop(&client->monc);
7533d14c5d2SYehuda Sadeh fail:
754757856d2SIlya Dryomov 	ceph_messenger_fini(&client->msgr);
7553d14c5d2SYehuda Sadeh 	kfree(client);
7563d14c5d2SYehuda Sadeh 	return ERR_PTR(err);
7573d14c5d2SYehuda Sadeh }
7583d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_create_client);
7593d14c5d2SYehuda Sadeh 
ceph_destroy_client(struct ceph_client * client)7603d14c5d2SYehuda Sadeh void ceph_destroy_client(struct ceph_client *client)
7613d14c5d2SYehuda Sadeh {
7623d14c5d2SYehuda Sadeh 	dout("destroy_client %p\n", client);
7633d14c5d2SYehuda Sadeh 
764a2a32584SGuanjun He 	atomic_set(&client->msgr.stopping, 1);
765a2a32584SGuanjun He 
7663d14c5d2SYehuda Sadeh 	/* unmount */
7673d14c5d2SYehuda Sadeh 	ceph_osdc_stop(&client->osdc);
7683d14c5d2SYehuda Sadeh 	ceph_monc_stop(&client->monc);
769757856d2SIlya Dryomov 	ceph_messenger_fini(&client->msgr);
7703d14c5d2SYehuda Sadeh 
7713d14c5d2SYehuda Sadeh 	ceph_debugfs_client_cleanup(client);
7723d14c5d2SYehuda Sadeh 
7733d14c5d2SYehuda Sadeh 	ceph_destroy_options(client->options);
7743d14c5d2SYehuda Sadeh 
7753d14c5d2SYehuda Sadeh 	kfree(client);
7763d14c5d2SYehuda Sadeh 	dout("destroy_client %p done\n", client);
7773d14c5d2SYehuda Sadeh }
7783d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_destroy_client);
7793d14c5d2SYehuda Sadeh 
ceph_reset_client_addr(struct ceph_client * client)780120a75eaSYan, Zheng void ceph_reset_client_addr(struct ceph_client *client)
781120a75eaSYan, Zheng {
782120a75eaSYan, Zheng 	ceph_messenger_reset_nonce(&client->msgr);
783120a75eaSYan, Zheng 	ceph_monc_reopen_session(&client->monc);
784120a75eaSYan, Zheng 	ceph_osdc_reopen_osds(&client->osdc);
785120a75eaSYan, Zheng }
786120a75eaSYan, Zheng EXPORT_SYMBOL(ceph_reset_client_addr);
787120a75eaSYan, Zheng 
7883d14c5d2SYehuda Sadeh /*
7893d14c5d2SYehuda Sadeh  * true if we have the mon map (and have thus joined the cluster)
7903d14c5d2SYehuda Sadeh  */
have_mon_and_osd_map(struct ceph_client * client)7913b33f692SZhang Zhuoyu static bool have_mon_and_osd_map(struct ceph_client *client)
7923d14c5d2SYehuda Sadeh {
7933d14c5d2SYehuda Sadeh 	return client->monc.monmap && client->monc.monmap->epoch &&
7943d14c5d2SYehuda Sadeh 	       client->osdc.osdmap && client->osdc.osdmap->epoch;
7953d14c5d2SYehuda Sadeh }
7963d14c5d2SYehuda Sadeh 
7973d14c5d2SYehuda Sadeh /*
7983d14c5d2SYehuda Sadeh  * mount: join the ceph cluster, and open root directory.
7993d14c5d2SYehuda Sadeh  */
__ceph_open_session(struct ceph_client * client,unsigned long started)8003d14c5d2SYehuda Sadeh int __ceph_open_session(struct ceph_client *client, unsigned long started)
8013d14c5d2SYehuda Sadeh {
802a319bf56SIlya Dryomov 	unsigned long timeout = client->options->mount_timeout;
803216639ddSIlya Dryomov 	long err;
8043d14c5d2SYehuda Sadeh 
8053d14c5d2SYehuda Sadeh 	/* open session, and wait for mon and osd maps */
8063d14c5d2SYehuda Sadeh 	err = ceph_monc_open_session(&client->monc);
8073d14c5d2SYehuda Sadeh 	if (err < 0)
8083d14c5d2SYehuda Sadeh 		return err;
8093d14c5d2SYehuda Sadeh 
8103d14c5d2SYehuda Sadeh 	while (!have_mon_and_osd_map(client)) {
8113d14c5d2SYehuda Sadeh 		if (timeout && time_after_eq(jiffies, started + timeout))
812216639ddSIlya Dryomov 			return -ETIMEDOUT;
8133d14c5d2SYehuda Sadeh 
8143d14c5d2SYehuda Sadeh 		/* wait */
8153d14c5d2SYehuda Sadeh 		dout("mount waiting for mon_map\n");
8163d14c5d2SYehuda Sadeh 		err = wait_event_interruptible_timeout(client->auth_wq,
8173d14c5d2SYehuda Sadeh 			have_mon_and_osd_map(client) || (client->auth_err < 0),
818a319bf56SIlya Dryomov 			ceph_timeout_jiffies(timeout));
819216639ddSIlya Dryomov 		if (err < 0)
8203d14c5d2SYehuda Sadeh 			return err;
8213d14c5d2SYehuda Sadeh 		if (client->auth_err < 0)
8223d14c5d2SYehuda Sadeh 			return client->auth_err;
8233d14c5d2SYehuda Sadeh 	}
8243d14c5d2SYehuda Sadeh 
825033268a5SIlya Dryomov 	pr_info("client%llu fsid %pU\n", ceph_client_gid(client),
826033268a5SIlya Dryomov 		&client->fsid);
82702ac956cSIlya Dryomov 	ceph_debugfs_client_init(client);
82802ac956cSIlya Dryomov 
8293d14c5d2SYehuda Sadeh 	return 0;
8303d14c5d2SYehuda Sadeh }
8313d14c5d2SYehuda Sadeh EXPORT_SYMBOL(__ceph_open_session);
8323d14c5d2SYehuda Sadeh 
ceph_open_session(struct ceph_client * client)8333d14c5d2SYehuda Sadeh int ceph_open_session(struct ceph_client *client)
8343d14c5d2SYehuda Sadeh {
8353d14c5d2SYehuda Sadeh 	int ret;
8363d14c5d2SYehuda Sadeh 	unsigned long started = jiffies;  /* note the start time */
8373d14c5d2SYehuda Sadeh 
8383d14c5d2SYehuda Sadeh 	dout("open_session start\n");
8393d14c5d2SYehuda Sadeh 	mutex_lock(&client->mount_mutex);
8403d14c5d2SYehuda Sadeh 
8413d14c5d2SYehuda Sadeh 	ret = __ceph_open_session(client, started);
8423d14c5d2SYehuda Sadeh 
8433d14c5d2SYehuda Sadeh 	mutex_unlock(&client->mount_mutex);
8443d14c5d2SYehuda Sadeh 	return ret;
8453d14c5d2SYehuda Sadeh }
8463d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_open_session);
8473d14c5d2SYehuda Sadeh 
ceph_wait_for_latest_osdmap(struct ceph_client * client,unsigned long timeout)848bb229bbbSIlya Dryomov int ceph_wait_for_latest_osdmap(struct ceph_client *client,
849bb229bbbSIlya Dryomov 				unsigned long timeout)
850bb229bbbSIlya Dryomov {
851bb229bbbSIlya Dryomov 	u64 newest_epoch;
852bb229bbbSIlya Dryomov 	int ret;
853bb229bbbSIlya Dryomov 
854bb229bbbSIlya Dryomov 	ret = ceph_monc_get_version(&client->monc, "osdmap", &newest_epoch);
855bb229bbbSIlya Dryomov 	if (ret)
856bb229bbbSIlya Dryomov 		return ret;
857bb229bbbSIlya Dryomov 
858bb229bbbSIlya Dryomov 	if (client->osdc.osdmap->epoch >= newest_epoch)
859bb229bbbSIlya Dryomov 		return 0;
860bb229bbbSIlya Dryomov 
861bb229bbbSIlya Dryomov 	ceph_osdc_maybe_request_map(&client->osdc);
862bb229bbbSIlya Dryomov 	return ceph_monc_wait_osdmap(&client->monc, newest_epoch, timeout);
863bb229bbbSIlya Dryomov }
864bb229bbbSIlya Dryomov EXPORT_SYMBOL(ceph_wait_for_latest_osdmap);
8653d14c5d2SYehuda Sadeh 
init_ceph_lib(void)8663d14c5d2SYehuda Sadeh static int __init init_ceph_lib(void)
8673d14c5d2SYehuda Sadeh {
8683d14c5d2SYehuda Sadeh 	int ret = 0;
8693d14c5d2SYehuda Sadeh 
8701a829ff2SGreg Kroah-Hartman 	ceph_debugfs_init();
8713d14c5d2SYehuda Sadeh 
8724b2a58abSTommi Virtanen 	ret = ceph_crypto_init();
8733d14c5d2SYehuda Sadeh 	if (ret < 0)
8743d14c5d2SYehuda Sadeh 		goto out_debugfs;
8753d14c5d2SYehuda Sadeh 
8764b2a58abSTommi Virtanen 	ret = ceph_msgr_init();
8774b2a58abSTommi Virtanen 	if (ret < 0)
8784b2a58abSTommi Virtanen 		goto out_crypto;
8794b2a58abSTommi Virtanen 
8805522ae0bSAlex Elder 	ret = ceph_osdc_setup();
8815522ae0bSAlex Elder 	if (ret < 0)
8825522ae0bSAlex Elder 		goto out_msgr;
8835522ae0bSAlex Elder 
8844f6a7e5eSSage Weil 	pr_info("loaded (mon/osd proto %d/%d)\n",
8854f6a7e5eSSage Weil 		CEPH_MONC_PROTOCOL, CEPH_OSDC_PROTOCOL);
8863d14c5d2SYehuda Sadeh 
8873d14c5d2SYehuda Sadeh 	return 0;
8883d14c5d2SYehuda Sadeh 
8895522ae0bSAlex Elder out_msgr:
8905522ae0bSAlex Elder 	ceph_msgr_exit();
8914b2a58abSTommi Virtanen out_crypto:
8924b2a58abSTommi Virtanen 	ceph_crypto_shutdown();
8933d14c5d2SYehuda Sadeh out_debugfs:
8943d14c5d2SYehuda Sadeh 	ceph_debugfs_cleanup();
8953d14c5d2SYehuda Sadeh 	return ret;
8963d14c5d2SYehuda Sadeh }
8973d14c5d2SYehuda Sadeh 
exit_ceph_lib(void)8983d14c5d2SYehuda Sadeh static void __exit exit_ceph_lib(void)
8993d14c5d2SYehuda Sadeh {
9003d14c5d2SYehuda Sadeh 	dout("exit_ceph_lib\n");
90151e92737SYan, Zheng 	WARN_ON(!ceph_strings_empty());
90251e92737SYan, Zheng 
9035522ae0bSAlex Elder 	ceph_osdc_cleanup();
9043d14c5d2SYehuda Sadeh 	ceph_msgr_exit();
9054b2a58abSTommi Virtanen 	ceph_crypto_shutdown();
9063d14c5d2SYehuda Sadeh 	ceph_debugfs_cleanup();
9073d14c5d2SYehuda Sadeh }
9083d14c5d2SYehuda Sadeh 
9093d14c5d2SYehuda Sadeh module_init(init_ceph_lib);
9103d14c5d2SYehuda Sadeh module_exit(exit_ceph_lib);
9113d14c5d2SYehuda Sadeh 
9123d14c5d2SYehuda Sadeh MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
9133d14c5d2SYehuda Sadeh MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
9143d14c5d2SYehuda Sadeh MODULE_AUTHOR("Patience Warnick <patience@newdream.net>");
9156c13a6bbSHong Zhiguo MODULE_DESCRIPTION("Ceph core library");
9163d14c5d2SYehuda Sadeh MODULE_LICENSE("GPL");
917