xref: /openbmc/linux/net/ceph/ceph_common.c (revision 00498b994113a871a556f7ff24a4cf8a00611700)
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  */
4472fe25e3SAlex Elder bool libceph_compatible(void *data)
4572fe25e3SAlex Elder {
461e32d34cSAlex Elder 	return true;
4772fe25e3SAlex Elder }
4872fe25e3SAlex Elder EXPORT_SYMBOL(libceph_compatible);
493d14c5d2SYehuda Sadeh 
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 
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  */
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 
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 
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 
19310c12851SIlya Dryomov /*
19410c12851SIlya Dryomov  * kvmalloc() doesn't fall back to the vmalloc allocator unless flags are
19510c12851SIlya Dryomov  * compatible with (a superset of) GFP_KERNEL.  This is because while the
19610c12851SIlya Dryomov  * actual pages are allocated with the specified flags, the page table pages
197ed1f324cSChristoph Hellwig  * are always allocated with GFP_KERNEL.
19810c12851SIlya Dryomov  *
19910c12851SIlya Dryomov  * ceph_kvmalloc() may be called with GFP_KERNEL, GFP_NOFS or GFP_NOIO.
20010c12851SIlya Dryomov  */
201eeb0bed5SIlya Dryomov void *ceph_kvmalloc(size_t size, gfp_t flags)
202eeb0bed5SIlya Dryomov {
20310c12851SIlya Dryomov 	void *p;
20410c12851SIlya Dryomov 
20510c12851SIlya Dryomov 	if ((flags & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS)) {
20610c12851SIlya Dryomov 		p = kvmalloc(size, flags);
20710c12851SIlya Dryomov 	} else if ((flags & (__GFP_IO | __GFP_FS)) == __GFP_IO) {
20810c12851SIlya Dryomov 		unsigned int nofs_flag = memalloc_nofs_save();
20910c12851SIlya Dryomov 		p = kvmalloc(size, GFP_KERNEL);
21010c12851SIlya Dryomov 		memalloc_nofs_restore(nofs_flag);
21110c12851SIlya Dryomov 	} else {
21210c12851SIlya Dryomov 		unsigned int noio_flag = memalloc_noio_save();
21310c12851SIlya Dryomov 		p = kvmalloc(size, GFP_KERNEL);
21410c12851SIlya Dryomov 		memalloc_noio_restore(noio_flag);
215eeb0bed5SIlya Dryomov 	}
216eeb0bed5SIlya Dryomov 
21710c12851SIlya Dryomov 	return p;
218eeb0bed5SIlya Dryomov }
219eeb0bed5SIlya Dryomov 
2203d14c5d2SYehuda Sadeh static int parse_fsid(const char *str, struct ceph_fsid *fsid)
2213d14c5d2SYehuda Sadeh {
2223d14c5d2SYehuda Sadeh 	int i = 0;
2233d14c5d2SYehuda Sadeh 	char tmp[3];
2243d14c5d2SYehuda Sadeh 	int err = -EINVAL;
2253d14c5d2SYehuda Sadeh 	int d;
2263d14c5d2SYehuda Sadeh 
2273d14c5d2SYehuda Sadeh 	dout("parse_fsid '%s'\n", str);
2283d14c5d2SYehuda Sadeh 	tmp[2] = 0;
2293d14c5d2SYehuda Sadeh 	while (*str && i < 16) {
2303d14c5d2SYehuda Sadeh 		if (ispunct(*str)) {
2313d14c5d2SYehuda Sadeh 			str++;
2323d14c5d2SYehuda Sadeh 			continue;
2333d14c5d2SYehuda Sadeh 		}
2343d14c5d2SYehuda Sadeh 		if (!isxdigit(str[0]) || !isxdigit(str[1]))
2353d14c5d2SYehuda Sadeh 			break;
2363d14c5d2SYehuda Sadeh 		tmp[0] = str[0];
2373d14c5d2SYehuda Sadeh 		tmp[1] = str[1];
2383d14c5d2SYehuda Sadeh 		if (sscanf(tmp, "%x", &d) < 1)
2393d14c5d2SYehuda Sadeh 			break;
2403d14c5d2SYehuda Sadeh 		fsid->fsid[i] = d & 0xff;
2413d14c5d2SYehuda Sadeh 		i++;
2423d14c5d2SYehuda Sadeh 		str += 2;
2433d14c5d2SYehuda Sadeh 	}
2443d14c5d2SYehuda Sadeh 
2453d14c5d2SYehuda Sadeh 	if (i == 16)
2463d14c5d2SYehuda Sadeh 		err = 0;
2474c069a58SChengguang Xu 	dout("parse_fsid ret %d got fsid %pU\n", err, fsid);
2483d14c5d2SYehuda Sadeh 	return err;
2493d14c5d2SYehuda Sadeh }
2503d14c5d2SYehuda Sadeh 
2513d14c5d2SYehuda Sadeh /*
2523d14c5d2SYehuda Sadeh  * ceph options
2533d14c5d2SYehuda Sadeh  */
2543d14c5d2SYehuda Sadeh enum {
2553d14c5d2SYehuda Sadeh 	Opt_osdtimeout,
2563d14c5d2SYehuda Sadeh 	Opt_osdkeepalivetimeout,
2573d14c5d2SYehuda Sadeh 	Opt_mount_timeout,
2583d14c5d2SYehuda Sadeh 	Opt_osd_idle_ttl,
2597cc5e38fSIlya Dryomov 	Opt_osd_request_timeout,
2603d14c5d2SYehuda Sadeh 	/* int args above */
2613d14c5d2SYehuda Sadeh 	Opt_fsid,
2623d14c5d2SYehuda Sadeh 	Opt_name,
2633d14c5d2SYehuda Sadeh 	Opt_secret,
264e2c3d29bSTommi Virtanen 	Opt_key,
2653d14c5d2SYehuda Sadeh 	Opt_ip,
26645e6aa9fSIlya Dryomov 	Opt_crush_location,
2678ad44d5eSIlya Dryomov 	Opt_read_from_replica,
268*00498b99SIlya Dryomov 	Opt_ms_mode,
2693d14c5d2SYehuda Sadeh 	/* string args above */
270cffaba15SAlex Elder 	Opt_share,
271cffaba15SAlex Elder 	Opt_crc,
272a3fc9800SYan, Zheng 	Opt_cephx_require_signatures,
273a51983e4SIlya Dryomov 	Opt_cephx_sign_messages,
274ba988f87SChaitanya Huilgol 	Opt_tcp_nodelay,
27502b2f549SDongsheng Yang 	Opt_abort_on_full,
2763d14c5d2SYehuda Sadeh };
2773d14c5d2SYehuda Sadeh 
2788ad44d5eSIlya Dryomov enum {
2798ad44d5eSIlya Dryomov 	Opt_read_from_replica_no,
2808ad44d5eSIlya Dryomov 	Opt_read_from_replica_balance,
2818ad44d5eSIlya Dryomov 	Opt_read_from_replica_localize,
2828ad44d5eSIlya Dryomov };
2838ad44d5eSIlya Dryomov 
2848ad44d5eSIlya Dryomov static const struct constant_table ceph_param_read_from_replica[] = {
2858ad44d5eSIlya Dryomov 	{"no",		Opt_read_from_replica_no},
2868ad44d5eSIlya Dryomov 	{"balance",	Opt_read_from_replica_balance},
2878ad44d5eSIlya Dryomov 	{"localize",	Opt_read_from_replica_localize},
2888ad44d5eSIlya Dryomov 	{}
2898ad44d5eSIlya Dryomov };
2908ad44d5eSIlya Dryomov 
291*00498b99SIlya Dryomov enum ceph_ms_mode {
292*00498b99SIlya Dryomov 	Opt_ms_mode_legacy,
293*00498b99SIlya Dryomov 	Opt_ms_mode_crc,
294*00498b99SIlya Dryomov 	Opt_ms_mode_secure,
295*00498b99SIlya Dryomov 	Opt_ms_mode_prefer_crc,
296*00498b99SIlya Dryomov 	Opt_ms_mode_prefer_secure
297*00498b99SIlya Dryomov };
298*00498b99SIlya Dryomov 
299*00498b99SIlya Dryomov static const struct constant_table ceph_param_ms_mode[] = {
300*00498b99SIlya Dryomov 	{"legacy",		Opt_ms_mode_legacy},
301*00498b99SIlya Dryomov 	{"crc",			Opt_ms_mode_crc},
302*00498b99SIlya Dryomov 	{"secure",		Opt_ms_mode_secure},
303*00498b99SIlya Dryomov 	{"prefer-crc",		Opt_ms_mode_prefer_crc},
304*00498b99SIlya Dryomov 	{"prefer-secure",	Opt_ms_mode_prefer_secure},
305*00498b99SIlya Dryomov 	{}
306*00498b99SIlya Dryomov };
307*00498b99SIlya Dryomov 
308d7167b14SAl Viro static const struct fs_parameter_spec ceph_parameters[] = {
30982995cc6SDavid Howells 	fsparam_flag	("abort_on_full",		Opt_abort_on_full),
31082995cc6SDavid Howells 	fsparam_flag_no ("cephx_require_signatures",	Opt_cephx_require_signatures),
31182995cc6SDavid Howells 	fsparam_flag_no ("cephx_sign_messages",		Opt_cephx_sign_messages),
31282995cc6SDavid Howells 	fsparam_flag_no ("crc",				Opt_crc),
31345e6aa9fSIlya Dryomov 	fsparam_string	("crush_location",		Opt_crush_location),
31482995cc6SDavid Howells 	fsparam_string	("fsid",			Opt_fsid),
31582995cc6SDavid Howells 	fsparam_string	("ip",				Opt_ip),
31682995cc6SDavid Howells 	fsparam_string	("key",				Opt_key),
31782995cc6SDavid Howells 	fsparam_u32	("mount_timeout",		Opt_mount_timeout),
31882995cc6SDavid Howells 	fsparam_string	("name",			Opt_name),
31982995cc6SDavid Howells 	fsparam_u32	("osd_idle_ttl",		Opt_osd_idle_ttl),
32082995cc6SDavid Howells 	fsparam_u32	("osd_request_timeout",		Opt_osd_request_timeout),
32182995cc6SDavid Howells 	fsparam_u32	("osdkeepalive",		Opt_osdkeepalivetimeout),
32282995cc6SDavid Howells 	__fsparam	(fs_param_is_s32, "osdtimeout", Opt_osdtimeout,
3232710c957SAl Viro 			 fs_param_deprecated, NULL),
3248ad44d5eSIlya Dryomov 	fsparam_enum	("read_from_replica",		Opt_read_from_replica,
3258ad44d5eSIlya Dryomov 			 ceph_param_read_from_replica),
326*00498b99SIlya Dryomov 	fsparam_enum	("ms_mode",			Opt_ms_mode,
327*00498b99SIlya Dryomov 			 ceph_param_ms_mode),
32882995cc6SDavid Howells 	fsparam_string	("secret",			Opt_secret),
32982995cc6SDavid Howells 	fsparam_flag_no ("share",			Opt_share),
33082995cc6SDavid Howells 	fsparam_flag_no ("tcp_nodelay",			Opt_tcp_nodelay),
33182995cc6SDavid Howells 	{}
3323d14c5d2SYehuda Sadeh };
3333d14c5d2SYehuda Sadeh 
33482995cc6SDavid Howells struct ceph_options *ceph_alloc_options(void)
33582995cc6SDavid Howells {
33682995cc6SDavid Howells 	struct ceph_options *opt;
33782995cc6SDavid Howells 
33882995cc6SDavid Howells 	opt = kzalloc(sizeof(*opt), GFP_KERNEL);
33982995cc6SDavid Howells 	if (!opt)
34082995cc6SDavid Howells 		return NULL;
34182995cc6SDavid Howells 
34245e6aa9fSIlya Dryomov 	opt->crush_locs = RB_ROOT;
34382995cc6SDavid Howells 	opt->mon_addr = kcalloc(CEPH_MAX_MON, sizeof(*opt->mon_addr),
34482995cc6SDavid Howells 				GFP_KERNEL);
34582995cc6SDavid Howells 	if (!opt->mon_addr) {
34682995cc6SDavid Howells 		kfree(opt);
34782995cc6SDavid Howells 		return NULL;
34882995cc6SDavid Howells 	}
34982995cc6SDavid Howells 
35082995cc6SDavid Howells 	opt->flags = CEPH_OPT_DEFAULT;
35182995cc6SDavid Howells 	opt->osd_keepalive_timeout = CEPH_OSD_KEEPALIVE_DEFAULT;
35282995cc6SDavid Howells 	opt->mount_timeout = CEPH_MOUNT_TIMEOUT_DEFAULT;
35382995cc6SDavid Howells 	opt->osd_idle_ttl = CEPH_OSD_IDLE_TTL_DEFAULT;
35482995cc6SDavid Howells 	opt->osd_request_timeout = CEPH_OSD_REQUEST_TIMEOUT_DEFAULT;
35522d2cfdfSIlya Dryomov 	opt->read_from_replica = CEPH_READ_FROM_REPLICA_DEFAULT;
356*00498b99SIlya Dryomov 	opt->con_modes[0] = CEPH_CON_MODE_UNKNOWN;
357*00498b99SIlya Dryomov 	opt->con_modes[1] = CEPH_CON_MODE_UNKNOWN;
35882995cc6SDavid Howells 	return opt;
35982995cc6SDavid Howells }
36082995cc6SDavid Howells EXPORT_SYMBOL(ceph_alloc_options);
36182995cc6SDavid Howells 
3623d14c5d2SYehuda Sadeh void ceph_destroy_options(struct ceph_options *opt)
3633d14c5d2SYehuda Sadeh {
3643d14c5d2SYehuda Sadeh 	dout("destroy_options %p\n", opt);
36582995cc6SDavid Howells 	if (!opt)
36682995cc6SDavid Howells 		return;
36782995cc6SDavid Howells 
36845e6aa9fSIlya Dryomov 	ceph_clear_crush_locs(&opt->crush_locs);
3693d14c5d2SYehuda Sadeh 	kfree(opt->name);
3708323c3aaSTommi Virtanen 	if (opt->key) {
3718323c3aaSTommi Virtanen 		ceph_crypto_key_destroy(opt->key);
3728323c3aaSTommi Virtanen 		kfree(opt->key);
3738323c3aaSTommi Virtanen 	}
3741cad7893SNoah Watkins 	kfree(opt->mon_addr);
3753d14c5d2SYehuda Sadeh 	kfree(opt);
3763d14c5d2SYehuda Sadeh }
3773d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_destroy_options);
3783d14c5d2SYehuda Sadeh 
379e2c3d29bSTommi Virtanen /* get secret from key store */
38082995cc6SDavid Howells static int get_secret(struct ceph_crypto_key *dst, const char *name,
3812c3f3dc3SAl Viro 		      struct p_log *log)
38282995cc6SDavid Howells {
383e2c3d29bSTommi Virtanen 	struct key *ukey;
384e2c3d29bSTommi Virtanen 	int key_err;
385e2c3d29bSTommi Virtanen 	int err = 0;
3864b2a58abSTommi Virtanen 	struct ceph_crypto_key *ckey;
387e2c3d29bSTommi Virtanen 
3884b2a58abSTommi Virtanen 	ukey = request_key(&key_type_ceph, name, NULL);
389bad87216SYueHaibing 	if (IS_ERR(ukey)) {
390e2c3d29bSTommi Virtanen 		/* request_key errors don't map nicely to mount(2)
391e2c3d29bSTommi Virtanen 		   errors; don't even try, but still printk */
392e2c3d29bSTommi Virtanen 		key_err = PTR_ERR(ukey);
393e2c3d29bSTommi Virtanen 		switch (key_err) {
394e2c3d29bSTommi Virtanen 		case -ENOKEY:
3952c3f3dc3SAl Viro 			error_plog(log, "Failed due to key not found: %s",
396b9a67899SJoe Perches 			       name);
397e2c3d29bSTommi Virtanen 			break;
398e2c3d29bSTommi Virtanen 		case -EKEYEXPIRED:
3992c3f3dc3SAl Viro 			error_plog(log, "Failed due to expired key: %s",
400b9a67899SJoe Perches 			       name);
401e2c3d29bSTommi Virtanen 			break;
402e2c3d29bSTommi Virtanen 		case -EKEYREVOKED:
4032c3f3dc3SAl Viro 			error_plog(log, "Failed due to revoked key: %s",
404b9a67899SJoe Perches 			       name);
405e2c3d29bSTommi Virtanen 			break;
406e2c3d29bSTommi Virtanen 		default:
4072c3f3dc3SAl Viro 			error_plog(log, "Failed due to key error %d: %s",
408b9a67899SJoe Perches 			       key_err, name);
409e2c3d29bSTommi Virtanen 		}
410e2c3d29bSTommi Virtanen 		err = -EPERM;
411e2c3d29bSTommi Virtanen 		goto out;
412e2c3d29bSTommi Virtanen 	}
413e2c3d29bSTommi Virtanen 
414146aa8b1SDavid Howells 	ckey = ukey->payload.data[0];
4154b2a58abSTommi Virtanen 	err = ceph_crypto_key_clone(dst, ckey);
416e2c3d29bSTommi Virtanen 	if (err)
417e2c3d29bSTommi Virtanen 		goto out_key;
418e2c3d29bSTommi Virtanen 	/* pass through, err is 0 */
419e2c3d29bSTommi Virtanen 
420e2c3d29bSTommi Virtanen out_key:
421e2c3d29bSTommi Virtanen 	key_put(ukey);
422e2c3d29bSTommi Virtanen out:
423e2c3d29bSTommi Virtanen 	return err;
424e2c3d29bSTommi Virtanen }
425e2c3d29bSTommi Virtanen 
42682995cc6SDavid Howells int ceph_parse_mon_ips(const char *buf, size_t len, struct ceph_options *opt,
427c80c98f0SAl Viro 		       struct fc_log *l)
4283d14c5d2SYehuda Sadeh {
429c80c98f0SAl Viro 	struct p_log log = {.prefix = "libceph", .log = l};
43082995cc6SDavid Howells 	int ret;
4313d14c5d2SYehuda Sadeh 
4323d14c5d2SYehuda Sadeh 	/* ip1[:port1][,ip2[:port2]...] */
43382995cc6SDavid Howells 	ret = ceph_parse_ips(buf, buf + len, opt->mon_addr, CEPH_MAX_MON,
43482995cc6SDavid Howells 			     &opt->num_mon);
43582995cc6SDavid Howells 	if (ret) {
4362c3f3dc3SAl Viro 		error_plog(&log, "Failed to parse monitor IPs: %d", ret);
43782995cc6SDavid Howells 		return ret;
43882995cc6SDavid Howells 	}
4393d14c5d2SYehuda Sadeh 
44082995cc6SDavid Howells 	return 0;
4413d14c5d2SYehuda Sadeh }
44282995cc6SDavid Howells EXPORT_SYMBOL(ceph_parse_mon_ips);
44382995cc6SDavid Howells 
44482995cc6SDavid Howells int ceph_parse_param(struct fs_parameter *param, struct ceph_options *opt,
445c80c98f0SAl Viro 		     struct fc_log *l)
44682995cc6SDavid Howells {
44782995cc6SDavid Howells 	struct fs_parse_result result;
44882995cc6SDavid Howells 	int token, err;
449c80c98f0SAl Viro 	struct p_log log = {.prefix = "libceph", .log = l};
45082995cc6SDavid Howells 
451d7167b14SAl Viro 	token = __fs_parse(&log, ceph_parameters, param, &result);
45282995cc6SDavid Howells 	dout("%s fs_parse '%s' token %d\n", __func__, param->key, token);
45382995cc6SDavid Howells 	if (token < 0)
45482995cc6SDavid Howells 		return token;
45582995cc6SDavid Howells 
4563d14c5d2SYehuda Sadeh 	switch (token) {
4573d14c5d2SYehuda Sadeh 	case Opt_ip:
45882995cc6SDavid Howells 		err = ceph_parse_ips(param->string,
45982995cc6SDavid Howells 				     param->string + param->size,
4603d14c5d2SYehuda Sadeh 				     &opt->my_addr,
4613d14c5d2SYehuda Sadeh 				     1, NULL);
46282995cc6SDavid Howells 		if (err) {
4632c3f3dc3SAl Viro 			error_plog(&log, "Failed to parse ip: %d", err);
46482995cc6SDavid Howells 			return err;
46582995cc6SDavid Howells 		}
4663d14c5d2SYehuda Sadeh 		opt->flags |= CEPH_OPT_MYIP;
4673d14c5d2SYehuda Sadeh 		break;
4683d14c5d2SYehuda Sadeh 
4693d14c5d2SYehuda Sadeh 	case Opt_fsid:
47082995cc6SDavid Howells 		err = parse_fsid(param->string, &opt->fsid);
47182995cc6SDavid Howells 		if (err) {
4722c3f3dc3SAl Viro 			error_plog(&log, "Failed to parse fsid: %d", err);
47382995cc6SDavid Howells 			return err;
47482995cc6SDavid Howells 		}
4753d14c5d2SYehuda Sadeh 		opt->flags |= CEPH_OPT_FSID;
4763d14c5d2SYehuda Sadeh 		break;
4773d14c5d2SYehuda Sadeh 	case Opt_name:
478937441f3SChengguang Xu 		kfree(opt->name);
47982995cc6SDavid Howells 		opt->name = param->string;
48082995cc6SDavid Howells 		param->string = NULL;
4813d14c5d2SYehuda Sadeh 		break;
4823d14c5d2SYehuda Sadeh 	case Opt_secret:
483937441f3SChengguang Xu 		ceph_crypto_key_destroy(opt->key);
484937441f3SChengguang Xu 		kfree(opt->key);
485937441f3SChengguang Xu 
4868323c3aaSTommi Virtanen 		opt->key = kzalloc(sizeof(*opt->key), GFP_KERNEL);
48782995cc6SDavid Howells 		if (!opt->key)
48882995cc6SDavid Howells 			return -ENOMEM;
48982995cc6SDavid Howells 		err = ceph_crypto_key_unarmor(opt->key, param->string);
49082995cc6SDavid Howells 		if (err) {
4912c3f3dc3SAl Viro 			error_plog(&log, "Failed to parse secret: %d", err);
49282995cc6SDavid Howells 			return err;
4938323c3aaSTommi Virtanen 		}
4943d14c5d2SYehuda Sadeh 		break;
495e2c3d29bSTommi Virtanen 	case Opt_key:
496937441f3SChengguang Xu 		ceph_crypto_key_destroy(opt->key);
497937441f3SChengguang Xu 		kfree(opt->key);
498937441f3SChengguang Xu 
499e2c3d29bSTommi Virtanen 		opt->key = kzalloc(sizeof(*opt->key), GFP_KERNEL);
50082995cc6SDavid Howells 		if (!opt->key)
50182995cc6SDavid Howells 			return -ENOMEM;
5022c3f3dc3SAl Viro 		return get_secret(opt->key, param->string, &log);
50345e6aa9fSIlya Dryomov 	case Opt_crush_location:
50445e6aa9fSIlya Dryomov 		ceph_clear_crush_locs(&opt->crush_locs);
50545e6aa9fSIlya Dryomov 		err = ceph_parse_crush_location(param->string,
50645e6aa9fSIlya Dryomov 						&opt->crush_locs);
50745e6aa9fSIlya Dryomov 		if (err) {
50845e6aa9fSIlya Dryomov 			error_plog(&log, "Failed to parse CRUSH location: %d",
50945e6aa9fSIlya Dryomov 				   err);
51045e6aa9fSIlya Dryomov 			return err;
51145e6aa9fSIlya Dryomov 		}
51245e6aa9fSIlya Dryomov 		break;
5138ad44d5eSIlya Dryomov 	case Opt_read_from_replica:
5148ad44d5eSIlya Dryomov 		switch (result.uint_32) {
5158ad44d5eSIlya Dryomov 		case Opt_read_from_replica_no:
51622d2cfdfSIlya Dryomov 			opt->read_from_replica = 0;
5178ad44d5eSIlya Dryomov 			break;
5188ad44d5eSIlya Dryomov 		case Opt_read_from_replica_balance:
51922d2cfdfSIlya Dryomov 			opt->read_from_replica = CEPH_OSD_FLAG_BALANCE_READS;
5208ad44d5eSIlya Dryomov 			break;
5218ad44d5eSIlya Dryomov 		case Opt_read_from_replica_localize:
52222d2cfdfSIlya Dryomov 			opt->read_from_replica = CEPH_OSD_FLAG_LOCALIZE_READS;
5238ad44d5eSIlya Dryomov 			break;
5248ad44d5eSIlya Dryomov 		default:
5258ad44d5eSIlya Dryomov 			BUG();
5268ad44d5eSIlya Dryomov 		}
5278ad44d5eSIlya Dryomov 		break;
528*00498b99SIlya Dryomov 	case Opt_ms_mode:
529*00498b99SIlya Dryomov 		switch (result.uint_32) {
530*00498b99SIlya Dryomov 		case Opt_ms_mode_legacy:
531*00498b99SIlya Dryomov 			opt->con_modes[0] = CEPH_CON_MODE_UNKNOWN;
532*00498b99SIlya Dryomov 			opt->con_modes[1] = CEPH_CON_MODE_UNKNOWN;
533*00498b99SIlya Dryomov 			break;
534*00498b99SIlya Dryomov 		case Opt_ms_mode_crc:
535*00498b99SIlya Dryomov 			opt->con_modes[0] = CEPH_CON_MODE_CRC;
536*00498b99SIlya Dryomov 			opt->con_modes[1] = CEPH_CON_MODE_UNKNOWN;
537*00498b99SIlya Dryomov 			break;
538*00498b99SIlya Dryomov 		case Opt_ms_mode_secure:
539*00498b99SIlya Dryomov 			opt->con_modes[0] = CEPH_CON_MODE_SECURE;
540*00498b99SIlya Dryomov 			opt->con_modes[1] = CEPH_CON_MODE_UNKNOWN;
541*00498b99SIlya Dryomov 			break;
542*00498b99SIlya Dryomov 		case Opt_ms_mode_prefer_crc:
543*00498b99SIlya Dryomov 			opt->con_modes[0] = CEPH_CON_MODE_CRC;
544*00498b99SIlya Dryomov 			opt->con_modes[1] = CEPH_CON_MODE_SECURE;
545*00498b99SIlya Dryomov 			break;
546*00498b99SIlya Dryomov 		case Opt_ms_mode_prefer_secure:
547*00498b99SIlya Dryomov 			opt->con_modes[0] = CEPH_CON_MODE_SECURE;
548*00498b99SIlya Dryomov 			opt->con_modes[1] = CEPH_CON_MODE_CRC;
549*00498b99SIlya Dryomov 			break;
550*00498b99SIlya Dryomov 		default:
551*00498b99SIlya Dryomov 			BUG();
552*00498b99SIlya Dryomov 		}
553*00498b99SIlya Dryomov 		break;
5543d14c5d2SYehuda Sadeh 
5553d14c5d2SYehuda Sadeh 	case Opt_osdtimeout:
5562c3f3dc3SAl Viro 		warn_plog(&log, "Ignoring osdtimeout");
5573d14c5d2SYehuda Sadeh 		break;
5583d14c5d2SYehuda Sadeh 	case Opt_osdkeepalivetimeout:
559a319bf56SIlya Dryomov 		/* 0 isn't well defined right now, reject it */
56082995cc6SDavid Howells 		if (result.uint_32 < 1 || result.uint_32 > INT_MAX / 1000)
56182995cc6SDavid Howells 			goto out_of_range;
562a319bf56SIlya Dryomov 		opt->osd_keepalive_timeout =
56382995cc6SDavid Howells 		    msecs_to_jiffies(result.uint_32 * 1000);
5643d14c5d2SYehuda Sadeh 		break;
5653d14c5d2SYehuda Sadeh 	case Opt_osd_idle_ttl:
566a319bf56SIlya Dryomov 		/* 0 isn't well defined right now, reject it */
56782995cc6SDavid Howells 		if (result.uint_32 < 1 || result.uint_32 > INT_MAX / 1000)
56882995cc6SDavid Howells 			goto out_of_range;
56982995cc6SDavid Howells 		opt->osd_idle_ttl = msecs_to_jiffies(result.uint_32 * 1000);
5703d14c5d2SYehuda Sadeh 		break;
5713d14c5d2SYehuda Sadeh 	case Opt_mount_timeout:
572a319bf56SIlya Dryomov 		/* 0 is "wait forever" (i.e. infinite timeout) */
57382995cc6SDavid Howells 		if (result.uint_32 > INT_MAX / 1000)
57482995cc6SDavid Howells 			goto out_of_range;
57582995cc6SDavid Howells 		opt->mount_timeout = msecs_to_jiffies(result.uint_32 * 1000);
5763d14c5d2SYehuda Sadeh 		break;
5777cc5e38fSIlya Dryomov 	case Opt_osd_request_timeout:
5787cc5e38fSIlya Dryomov 		/* 0 is "wait forever" (i.e. infinite timeout) */
57982995cc6SDavid Howells 		if (result.uint_32 > INT_MAX / 1000)
58082995cc6SDavid Howells 			goto out_of_range;
58182995cc6SDavid Howells 		opt->osd_request_timeout =
58282995cc6SDavid Howells 		    msecs_to_jiffies(result.uint_32 * 1000);
5837cc5e38fSIlya Dryomov 		break;
5843d14c5d2SYehuda Sadeh 
585cffaba15SAlex Elder 	case Opt_share:
58682995cc6SDavid Howells 		if (!result.negated)
587cffaba15SAlex Elder 			opt->flags &= ~CEPH_OPT_NOSHARE;
58882995cc6SDavid Howells 		else
5893d14c5d2SYehuda Sadeh 			opt->flags |= CEPH_OPT_NOSHARE;
5903d14c5d2SYehuda Sadeh 		break;
591cffaba15SAlex Elder 	case Opt_crc:
59282995cc6SDavid Howells 		if (!result.negated)
593cffaba15SAlex Elder 			opt->flags &= ~CEPH_OPT_NOCRC;
59482995cc6SDavid Howells 		else
5953d14c5d2SYehuda Sadeh 			opt->flags |= CEPH_OPT_NOCRC;
5963d14c5d2SYehuda Sadeh 		break;
597a3fc9800SYan, Zheng 	case Opt_cephx_require_signatures:
59882995cc6SDavid Howells 		if (!result.negated)
599a3fc9800SYan, Zheng 			opt->flags &= ~CEPH_OPT_NOMSGAUTH;
60082995cc6SDavid Howells 		else
601a3fc9800SYan, Zheng 			opt->flags |= CEPH_OPT_NOMSGAUTH;
602a3fc9800SYan, Zheng 		break;
603a51983e4SIlya Dryomov 	case Opt_cephx_sign_messages:
60482995cc6SDavid Howells 		if (!result.negated)
605a51983e4SIlya Dryomov 			opt->flags &= ~CEPH_OPT_NOMSGSIGN;
60682995cc6SDavid Howells 		else
607a51983e4SIlya Dryomov 			opt->flags |= CEPH_OPT_NOMSGSIGN;
608a51983e4SIlya Dryomov 		break;
609ba988f87SChaitanya Huilgol 	case Opt_tcp_nodelay:
61082995cc6SDavid Howells 		if (!result.negated)
611ba988f87SChaitanya Huilgol 			opt->flags |= CEPH_OPT_TCP_NODELAY;
61282995cc6SDavid Howells 		else
613ba988f87SChaitanya Huilgol 			opt->flags &= ~CEPH_OPT_TCP_NODELAY;
614ba988f87SChaitanya Huilgol 		break;
615ba988f87SChaitanya Huilgol 
61602b2f549SDongsheng Yang 	case Opt_abort_on_full:
61702b2f549SDongsheng Yang 		opt->flags |= CEPH_OPT_ABORT_ON_FULL;
61802b2f549SDongsheng Yang 		break;
61902b2f549SDongsheng Yang 
6203d14c5d2SYehuda Sadeh 	default:
62182995cc6SDavid Howells 		BUG();
6223d14c5d2SYehuda Sadeh 	}
6233d14c5d2SYehuda Sadeh 
62482995cc6SDavid Howells 	return 0;
6253d14c5d2SYehuda Sadeh 
62682995cc6SDavid Howells out_of_range:
6272c3f3dc3SAl Viro 	return inval_plog(&log, "%s out of range", param->key);
6283d14c5d2SYehuda Sadeh }
62982995cc6SDavid Howells EXPORT_SYMBOL(ceph_parse_param);
6303d14c5d2SYehuda Sadeh 
63102b2f549SDongsheng Yang int ceph_print_client_options(struct seq_file *m, struct ceph_client *client,
63202b2f549SDongsheng Yang 			      bool show_all)
633ff40f9aeSIlya Dryomov {
634ff40f9aeSIlya Dryomov 	struct ceph_options *opt = client->options;
635ff40f9aeSIlya Dryomov 	size_t pos = m->count;
63645e6aa9fSIlya Dryomov 	struct rb_node *n;
637ff40f9aeSIlya Dryomov 
638a068acf2SKees Cook 	if (opt->name) {
639a068acf2SKees Cook 		seq_puts(m, "name=");
640a068acf2SKees Cook 		seq_escape(m, opt->name, ", \t\n\\");
641a068acf2SKees Cook 		seq_putc(m, ',');
642a068acf2SKees Cook 	}
643ff40f9aeSIlya Dryomov 	if (opt->key)
644ff40f9aeSIlya Dryomov 		seq_puts(m, "secret=<hidden>,");
645ff40f9aeSIlya Dryomov 
64645e6aa9fSIlya Dryomov 	if (!RB_EMPTY_ROOT(&opt->crush_locs)) {
64745e6aa9fSIlya Dryomov 		seq_puts(m, "crush_location=");
64845e6aa9fSIlya Dryomov 		for (n = rb_first(&opt->crush_locs); ; ) {
64945e6aa9fSIlya Dryomov 			struct crush_loc_node *loc =
65045e6aa9fSIlya Dryomov 			    rb_entry(n, struct crush_loc_node, cl_node);
65145e6aa9fSIlya Dryomov 
65245e6aa9fSIlya Dryomov 			seq_printf(m, "%s:%s", loc->cl_loc.cl_type_name,
65345e6aa9fSIlya Dryomov 				   loc->cl_loc.cl_name);
65445e6aa9fSIlya Dryomov 			n = rb_next(n);
65545e6aa9fSIlya Dryomov 			if (!n)
65645e6aa9fSIlya Dryomov 				break;
65745e6aa9fSIlya Dryomov 
65845e6aa9fSIlya Dryomov 			seq_putc(m, '|');
65945e6aa9fSIlya Dryomov 		}
66045e6aa9fSIlya Dryomov 		seq_putc(m, ',');
66145e6aa9fSIlya Dryomov 	}
66222d2cfdfSIlya Dryomov 	if (opt->read_from_replica == CEPH_OSD_FLAG_BALANCE_READS) {
6638ad44d5eSIlya Dryomov 		seq_puts(m, "read_from_replica=balance,");
66422d2cfdfSIlya Dryomov 	} else if (opt->read_from_replica == CEPH_OSD_FLAG_LOCALIZE_READS) {
6658ad44d5eSIlya Dryomov 		seq_puts(m, "read_from_replica=localize,");
6668ad44d5eSIlya Dryomov 	}
667*00498b99SIlya Dryomov 	if (opt->con_modes[0] != CEPH_CON_MODE_UNKNOWN) {
668*00498b99SIlya Dryomov 		if (opt->con_modes[0] == CEPH_CON_MODE_CRC &&
669*00498b99SIlya Dryomov 		    opt->con_modes[1] == CEPH_CON_MODE_UNKNOWN) {
670*00498b99SIlya Dryomov 			seq_puts(m, "ms_mode=crc,");
671*00498b99SIlya Dryomov 		} else if (opt->con_modes[0] == CEPH_CON_MODE_SECURE &&
672*00498b99SIlya Dryomov 			   opt->con_modes[1] == CEPH_CON_MODE_UNKNOWN) {
673*00498b99SIlya Dryomov 			seq_puts(m, "ms_mode=secure,");
674*00498b99SIlya Dryomov 		} else if (opt->con_modes[0] == CEPH_CON_MODE_CRC &&
675*00498b99SIlya Dryomov 			   opt->con_modes[1] == CEPH_CON_MODE_SECURE) {
676*00498b99SIlya Dryomov 			seq_puts(m, "ms_mode=prefer-crc,");
677*00498b99SIlya Dryomov 		} else if (opt->con_modes[0] == CEPH_CON_MODE_SECURE &&
678*00498b99SIlya Dryomov 			   opt->con_modes[1] == CEPH_CON_MODE_CRC) {
679*00498b99SIlya Dryomov 			seq_puts(m, "ms_mode=prefer-secure,");
680*00498b99SIlya Dryomov 		}
681*00498b99SIlya Dryomov 	}
68245e6aa9fSIlya Dryomov 
683ff40f9aeSIlya Dryomov 	if (opt->flags & CEPH_OPT_FSID)
684ff40f9aeSIlya Dryomov 		seq_printf(m, "fsid=%pU,", &opt->fsid);
685ff40f9aeSIlya Dryomov 	if (opt->flags & CEPH_OPT_NOSHARE)
686ff40f9aeSIlya Dryomov 		seq_puts(m, "noshare,");
687ff40f9aeSIlya Dryomov 	if (opt->flags & CEPH_OPT_NOCRC)
688ff40f9aeSIlya Dryomov 		seq_puts(m, "nocrc,");
689ff40f9aeSIlya Dryomov 	if (opt->flags & CEPH_OPT_NOMSGAUTH)
690ff40f9aeSIlya Dryomov 		seq_puts(m, "nocephx_require_signatures,");
691a51983e4SIlya Dryomov 	if (opt->flags & CEPH_OPT_NOMSGSIGN)
692a51983e4SIlya Dryomov 		seq_puts(m, "nocephx_sign_messages,");
693ff40f9aeSIlya Dryomov 	if ((opt->flags & CEPH_OPT_TCP_NODELAY) == 0)
694ff40f9aeSIlya Dryomov 		seq_puts(m, "notcp_nodelay,");
69502b2f549SDongsheng Yang 	if (show_all && (opt->flags & CEPH_OPT_ABORT_ON_FULL))
69602b2f549SDongsheng Yang 		seq_puts(m, "abort_on_full,");
697ff40f9aeSIlya Dryomov 
698ff40f9aeSIlya Dryomov 	if (opt->mount_timeout != CEPH_MOUNT_TIMEOUT_DEFAULT)
699a319bf56SIlya Dryomov 		seq_printf(m, "mount_timeout=%d,",
700a319bf56SIlya Dryomov 			   jiffies_to_msecs(opt->mount_timeout) / 1000);
701ff40f9aeSIlya Dryomov 	if (opt->osd_idle_ttl != CEPH_OSD_IDLE_TTL_DEFAULT)
702a319bf56SIlya Dryomov 		seq_printf(m, "osd_idle_ttl=%d,",
703a319bf56SIlya Dryomov 			   jiffies_to_msecs(opt->osd_idle_ttl) / 1000);
704ff40f9aeSIlya Dryomov 	if (opt->osd_keepalive_timeout != CEPH_OSD_KEEPALIVE_DEFAULT)
705ff40f9aeSIlya Dryomov 		seq_printf(m, "osdkeepalivetimeout=%d,",
706a319bf56SIlya Dryomov 		    jiffies_to_msecs(opt->osd_keepalive_timeout) / 1000);
7077cc5e38fSIlya Dryomov 	if (opt->osd_request_timeout != CEPH_OSD_REQUEST_TIMEOUT_DEFAULT)
7087cc5e38fSIlya Dryomov 		seq_printf(m, "osd_request_timeout=%d,",
7097cc5e38fSIlya Dryomov 			   jiffies_to_msecs(opt->osd_request_timeout) / 1000);
710ff40f9aeSIlya Dryomov 
711ff40f9aeSIlya Dryomov 	/* drop redundant comma */
712ff40f9aeSIlya Dryomov 	if (m->count != pos)
713ff40f9aeSIlya Dryomov 		m->count--;
714ff40f9aeSIlya Dryomov 
715ff40f9aeSIlya Dryomov 	return 0;
716ff40f9aeSIlya Dryomov }
717ff40f9aeSIlya Dryomov EXPORT_SYMBOL(ceph_print_client_options);
718ff40f9aeSIlya Dryomov 
719005a07bfSIlya Dryomov struct ceph_entity_addr *ceph_client_addr(struct ceph_client *client)
720005a07bfSIlya Dryomov {
721005a07bfSIlya Dryomov 	return &client->msgr.inst.addr;
722005a07bfSIlya Dryomov }
723005a07bfSIlya Dryomov EXPORT_SYMBOL(ceph_client_addr);
724005a07bfSIlya Dryomov 
725033268a5SIlya Dryomov u64 ceph_client_gid(struct ceph_client *client)
7263d14c5d2SYehuda Sadeh {
7273d14c5d2SYehuda Sadeh 	return client->monc.auth->global_id;
7283d14c5d2SYehuda Sadeh }
729033268a5SIlya Dryomov EXPORT_SYMBOL(ceph_client_gid);
7303d14c5d2SYehuda Sadeh 
7313d14c5d2SYehuda Sadeh /*
7323d14c5d2SYehuda Sadeh  * create a fresh client instance
7333d14c5d2SYehuda Sadeh  */
73474da4a0fSIlya Dryomov struct ceph_client *ceph_create_client(struct ceph_options *opt, void *private)
7353d14c5d2SYehuda Sadeh {
7363d14c5d2SYehuda Sadeh 	struct ceph_client *client;
7376ab00d46SSage Weil 	struct ceph_entity_addr *myaddr = NULL;
738ae5b806aSJason A. Donenfeld 	int err;
739ae5b806aSJason A. Donenfeld 
740ae5b806aSJason A. Donenfeld 	err = wait_for_random_bytes();
741ae5b806aSJason A. Donenfeld 	if (err < 0)
742ae5b806aSJason A. Donenfeld 		return ERR_PTR(err);
7433d14c5d2SYehuda Sadeh 
7443d14c5d2SYehuda Sadeh 	client = kzalloc(sizeof(*client), GFP_KERNEL);
7453d14c5d2SYehuda Sadeh 	if (client == NULL)
7463d14c5d2SYehuda Sadeh 		return ERR_PTR(-ENOMEM);
7473d14c5d2SYehuda Sadeh 
7483d14c5d2SYehuda Sadeh 	client->private = private;
7493d14c5d2SYehuda Sadeh 	client->options = opt;
7503d14c5d2SYehuda Sadeh 
7513d14c5d2SYehuda Sadeh 	mutex_init(&client->mount_mutex);
7523d14c5d2SYehuda Sadeh 	init_waitqueue_head(&client->auth_wq);
7533d14c5d2SYehuda Sadeh 	client->auth_err = 0;
7543d14c5d2SYehuda Sadeh 
7553d14c5d2SYehuda Sadeh 	client->extra_mon_dispatch = NULL;
75674da4a0fSIlya Dryomov 	client->supported_features = CEPH_FEATURES_SUPPORTED_DEFAULT;
75774da4a0fSIlya Dryomov 	client->required_features = CEPH_FEATURES_REQUIRED_DEFAULT;
75874da4a0fSIlya Dryomov 
75974da4a0fSIlya Dryomov 	if (!ceph_test_opt(client, NOMSGAUTH))
76074da4a0fSIlya Dryomov 		client->required_features |= CEPH_FEATURE_MSG_AUTH;
7613d14c5d2SYehuda Sadeh 
7626ab00d46SSage Weil 	/* msgr */
7636ab00d46SSage Weil 	if (ceph_test_opt(client, MYIP))
7646ab00d46SSage Weil 		myaddr = &client->options->my_addr;
765ba988f87SChaitanya Huilgol 
766859bff51SIlya Dryomov 	ceph_messenger_init(&client->msgr, myaddr);
7673d14c5d2SYehuda Sadeh 
7683d14c5d2SYehuda Sadeh 	/* subsystems */
7693d14c5d2SYehuda Sadeh 	err = ceph_monc_init(&client->monc, client);
7703d14c5d2SYehuda Sadeh 	if (err < 0)
77115d9882cSAlex Elder 		goto fail;
7723d14c5d2SYehuda Sadeh 	err = ceph_osdc_init(&client->osdc, client);
7733d14c5d2SYehuda Sadeh 	if (err < 0)
7743d14c5d2SYehuda Sadeh 		goto fail_monc;
7753d14c5d2SYehuda Sadeh 
7763d14c5d2SYehuda Sadeh 	return client;
7773d14c5d2SYehuda Sadeh 
7783d14c5d2SYehuda Sadeh fail_monc:
7793d14c5d2SYehuda Sadeh 	ceph_monc_stop(&client->monc);
7803d14c5d2SYehuda Sadeh fail:
781757856d2SIlya Dryomov 	ceph_messenger_fini(&client->msgr);
7823d14c5d2SYehuda Sadeh 	kfree(client);
7833d14c5d2SYehuda Sadeh 	return ERR_PTR(err);
7843d14c5d2SYehuda Sadeh }
7853d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_create_client);
7863d14c5d2SYehuda Sadeh 
7873d14c5d2SYehuda Sadeh void ceph_destroy_client(struct ceph_client *client)
7883d14c5d2SYehuda Sadeh {
7893d14c5d2SYehuda Sadeh 	dout("destroy_client %p\n", client);
7903d14c5d2SYehuda Sadeh 
791a2a32584SGuanjun He 	atomic_set(&client->msgr.stopping, 1);
792a2a32584SGuanjun He 
7933d14c5d2SYehuda Sadeh 	/* unmount */
7943d14c5d2SYehuda Sadeh 	ceph_osdc_stop(&client->osdc);
7953d14c5d2SYehuda Sadeh 	ceph_monc_stop(&client->monc);
796757856d2SIlya Dryomov 	ceph_messenger_fini(&client->msgr);
7973d14c5d2SYehuda Sadeh 
7983d14c5d2SYehuda Sadeh 	ceph_debugfs_client_cleanup(client);
7993d14c5d2SYehuda Sadeh 
8003d14c5d2SYehuda Sadeh 	ceph_destroy_options(client->options);
8013d14c5d2SYehuda Sadeh 
8023d14c5d2SYehuda Sadeh 	kfree(client);
8033d14c5d2SYehuda Sadeh 	dout("destroy_client %p done\n", client);
8043d14c5d2SYehuda Sadeh }
8053d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_destroy_client);
8063d14c5d2SYehuda Sadeh 
807120a75eaSYan, Zheng void ceph_reset_client_addr(struct ceph_client *client)
808120a75eaSYan, Zheng {
809120a75eaSYan, Zheng 	ceph_messenger_reset_nonce(&client->msgr);
810120a75eaSYan, Zheng 	ceph_monc_reopen_session(&client->monc);
811120a75eaSYan, Zheng 	ceph_osdc_reopen_osds(&client->osdc);
812120a75eaSYan, Zheng }
813120a75eaSYan, Zheng EXPORT_SYMBOL(ceph_reset_client_addr);
814120a75eaSYan, Zheng 
8153d14c5d2SYehuda Sadeh /*
8163d14c5d2SYehuda Sadeh  * true if we have the mon map (and have thus joined the cluster)
8173d14c5d2SYehuda Sadeh  */
8183b33f692SZhang Zhuoyu static bool have_mon_and_osd_map(struct ceph_client *client)
8193d14c5d2SYehuda Sadeh {
8203d14c5d2SYehuda Sadeh 	return client->monc.monmap && client->monc.monmap->epoch &&
8213d14c5d2SYehuda Sadeh 	       client->osdc.osdmap && client->osdc.osdmap->epoch;
8223d14c5d2SYehuda Sadeh }
8233d14c5d2SYehuda Sadeh 
8243d14c5d2SYehuda Sadeh /*
8253d14c5d2SYehuda Sadeh  * mount: join the ceph cluster, and open root directory.
8263d14c5d2SYehuda Sadeh  */
8273d14c5d2SYehuda Sadeh int __ceph_open_session(struct ceph_client *client, unsigned long started)
8283d14c5d2SYehuda Sadeh {
829a319bf56SIlya Dryomov 	unsigned long timeout = client->options->mount_timeout;
830216639ddSIlya Dryomov 	long err;
8313d14c5d2SYehuda Sadeh 
8323d14c5d2SYehuda Sadeh 	/* open session, and wait for mon and osd maps */
8333d14c5d2SYehuda Sadeh 	err = ceph_monc_open_session(&client->monc);
8343d14c5d2SYehuda Sadeh 	if (err < 0)
8353d14c5d2SYehuda Sadeh 		return err;
8363d14c5d2SYehuda Sadeh 
8373d14c5d2SYehuda Sadeh 	while (!have_mon_and_osd_map(client)) {
8383d14c5d2SYehuda Sadeh 		if (timeout && time_after_eq(jiffies, started + timeout))
839216639ddSIlya Dryomov 			return -ETIMEDOUT;
8403d14c5d2SYehuda Sadeh 
8413d14c5d2SYehuda Sadeh 		/* wait */
8423d14c5d2SYehuda Sadeh 		dout("mount waiting for mon_map\n");
8433d14c5d2SYehuda Sadeh 		err = wait_event_interruptible_timeout(client->auth_wq,
8443d14c5d2SYehuda Sadeh 			have_mon_and_osd_map(client) || (client->auth_err < 0),
845a319bf56SIlya Dryomov 			ceph_timeout_jiffies(timeout));
846216639ddSIlya Dryomov 		if (err < 0)
8473d14c5d2SYehuda Sadeh 			return err;
8483d14c5d2SYehuda Sadeh 		if (client->auth_err < 0)
8493d14c5d2SYehuda Sadeh 			return client->auth_err;
8503d14c5d2SYehuda Sadeh 	}
8513d14c5d2SYehuda Sadeh 
852033268a5SIlya Dryomov 	pr_info("client%llu fsid %pU\n", ceph_client_gid(client),
853033268a5SIlya Dryomov 		&client->fsid);
85402ac956cSIlya Dryomov 	ceph_debugfs_client_init(client);
85502ac956cSIlya Dryomov 
8563d14c5d2SYehuda Sadeh 	return 0;
8573d14c5d2SYehuda Sadeh }
8583d14c5d2SYehuda Sadeh EXPORT_SYMBOL(__ceph_open_session);
8593d14c5d2SYehuda Sadeh 
8603d14c5d2SYehuda Sadeh int ceph_open_session(struct ceph_client *client)
8613d14c5d2SYehuda Sadeh {
8623d14c5d2SYehuda Sadeh 	int ret;
8633d14c5d2SYehuda Sadeh 	unsigned long started = jiffies;  /* note the start time */
8643d14c5d2SYehuda Sadeh 
8653d14c5d2SYehuda Sadeh 	dout("open_session start\n");
8663d14c5d2SYehuda Sadeh 	mutex_lock(&client->mount_mutex);
8673d14c5d2SYehuda Sadeh 
8683d14c5d2SYehuda Sadeh 	ret = __ceph_open_session(client, started);
8693d14c5d2SYehuda Sadeh 
8703d14c5d2SYehuda Sadeh 	mutex_unlock(&client->mount_mutex);
8713d14c5d2SYehuda Sadeh 	return ret;
8723d14c5d2SYehuda Sadeh }
8733d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_open_session);
8743d14c5d2SYehuda Sadeh 
875bb229bbbSIlya Dryomov int ceph_wait_for_latest_osdmap(struct ceph_client *client,
876bb229bbbSIlya Dryomov 				unsigned long timeout)
877bb229bbbSIlya Dryomov {
878bb229bbbSIlya Dryomov 	u64 newest_epoch;
879bb229bbbSIlya Dryomov 	int ret;
880bb229bbbSIlya Dryomov 
881bb229bbbSIlya Dryomov 	ret = ceph_monc_get_version(&client->monc, "osdmap", &newest_epoch);
882bb229bbbSIlya Dryomov 	if (ret)
883bb229bbbSIlya Dryomov 		return ret;
884bb229bbbSIlya Dryomov 
885bb229bbbSIlya Dryomov 	if (client->osdc.osdmap->epoch >= newest_epoch)
886bb229bbbSIlya Dryomov 		return 0;
887bb229bbbSIlya Dryomov 
888bb229bbbSIlya Dryomov 	ceph_osdc_maybe_request_map(&client->osdc);
889bb229bbbSIlya Dryomov 	return ceph_monc_wait_osdmap(&client->monc, newest_epoch, timeout);
890bb229bbbSIlya Dryomov }
891bb229bbbSIlya Dryomov EXPORT_SYMBOL(ceph_wait_for_latest_osdmap);
8923d14c5d2SYehuda Sadeh 
8933d14c5d2SYehuda Sadeh static int __init init_ceph_lib(void)
8943d14c5d2SYehuda Sadeh {
8953d14c5d2SYehuda Sadeh 	int ret = 0;
8963d14c5d2SYehuda Sadeh 
8971a829ff2SGreg Kroah-Hartman 	ceph_debugfs_init();
8983d14c5d2SYehuda Sadeh 
8994b2a58abSTommi Virtanen 	ret = ceph_crypto_init();
9003d14c5d2SYehuda Sadeh 	if (ret < 0)
9013d14c5d2SYehuda Sadeh 		goto out_debugfs;
9023d14c5d2SYehuda Sadeh 
9034b2a58abSTommi Virtanen 	ret = ceph_msgr_init();
9044b2a58abSTommi Virtanen 	if (ret < 0)
9054b2a58abSTommi Virtanen 		goto out_crypto;
9064b2a58abSTommi Virtanen 
9075522ae0bSAlex Elder 	ret = ceph_osdc_setup();
9085522ae0bSAlex Elder 	if (ret < 0)
9095522ae0bSAlex Elder 		goto out_msgr;
9105522ae0bSAlex Elder 
9114f6a7e5eSSage Weil 	pr_info("loaded (mon/osd proto %d/%d)\n",
9124f6a7e5eSSage Weil 		CEPH_MONC_PROTOCOL, CEPH_OSDC_PROTOCOL);
9133d14c5d2SYehuda Sadeh 
9143d14c5d2SYehuda Sadeh 	return 0;
9153d14c5d2SYehuda Sadeh 
9165522ae0bSAlex Elder out_msgr:
9175522ae0bSAlex Elder 	ceph_msgr_exit();
9184b2a58abSTommi Virtanen out_crypto:
9194b2a58abSTommi Virtanen 	ceph_crypto_shutdown();
9203d14c5d2SYehuda Sadeh out_debugfs:
9213d14c5d2SYehuda Sadeh 	ceph_debugfs_cleanup();
9223d14c5d2SYehuda Sadeh 	return ret;
9233d14c5d2SYehuda Sadeh }
9243d14c5d2SYehuda Sadeh 
9253d14c5d2SYehuda Sadeh static void __exit exit_ceph_lib(void)
9263d14c5d2SYehuda Sadeh {
9273d14c5d2SYehuda Sadeh 	dout("exit_ceph_lib\n");
92851e92737SYan, Zheng 	WARN_ON(!ceph_strings_empty());
92951e92737SYan, Zheng 
9305522ae0bSAlex Elder 	ceph_osdc_cleanup();
9313d14c5d2SYehuda Sadeh 	ceph_msgr_exit();
9324b2a58abSTommi Virtanen 	ceph_crypto_shutdown();
9333d14c5d2SYehuda Sadeh 	ceph_debugfs_cleanup();
9343d14c5d2SYehuda Sadeh }
9353d14c5d2SYehuda Sadeh 
9363d14c5d2SYehuda Sadeh module_init(init_ceph_lib);
9373d14c5d2SYehuda Sadeh module_exit(exit_ceph_lib);
9383d14c5d2SYehuda Sadeh 
9393d14c5d2SYehuda Sadeh MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
9403d14c5d2SYehuda Sadeh MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
9413d14c5d2SYehuda Sadeh MODULE_AUTHOR("Patience Warnick <patience@newdream.net>");
9426c13a6bbSHong Zhiguo MODULE_DESCRIPTION("Ceph core library");
9433d14c5d2SYehuda Sadeh MODULE_LICENSE("GPL");
944