xref: /openbmc/linux/net/ceph/ceph_common.c (revision d7167b149943e38ad610191ecbb0800c78bbced9)
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 
1793d14c5d2SYehuda Sadeh 	/* any matching mon ip implies a match */
1803d14c5d2SYehuda Sadeh 	for (i = 0; i < opt1->num_mon; i++) {
1813d14c5d2SYehuda Sadeh 		if (ceph_monmap_contains(client->monc.monmap,
1823d14c5d2SYehuda Sadeh 				 &opt1->mon_addr[i]))
1833d14c5d2SYehuda Sadeh 			return 0;
1843d14c5d2SYehuda Sadeh 	}
1853d14c5d2SYehuda Sadeh 	return -1;
1863d14c5d2SYehuda Sadeh }
1873d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_compare_options);
1883d14c5d2SYehuda Sadeh 
18910c12851SIlya Dryomov /*
19010c12851SIlya Dryomov  * kvmalloc() doesn't fall back to the vmalloc allocator unless flags are
19110c12851SIlya Dryomov  * compatible with (a superset of) GFP_KERNEL.  This is because while the
19210c12851SIlya Dryomov  * actual pages are allocated with the specified flags, the page table pages
19310c12851SIlya Dryomov  * are always allocated with GFP_KERNEL.  map_vm_area() doesn't even take
19410c12851SIlya Dryomov  * flags because GFP_KERNEL is hard-coded in {p4d,pud,pmd,pte}_alloc().
19510c12851SIlya Dryomov  *
19610c12851SIlya Dryomov  * ceph_kvmalloc() may be called with GFP_KERNEL, GFP_NOFS or GFP_NOIO.
19710c12851SIlya Dryomov  */
198eeb0bed5SIlya Dryomov void *ceph_kvmalloc(size_t size, gfp_t flags)
199eeb0bed5SIlya Dryomov {
20010c12851SIlya Dryomov 	void *p;
20110c12851SIlya Dryomov 
20210c12851SIlya Dryomov 	if ((flags & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS)) {
20310c12851SIlya Dryomov 		p = kvmalloc(size, flags);
20410c12851SIlya Dryomov 	} else if ((flags & (__GFP_IO | __GFP_FS)) == __GFP_IO) {
20510c12851SIlya Dryomov 		unsigned int nofs_flag = memalloc_nofs_save();
20610c12851SIlya Dryomov 		p = kvmalloc(size, GFP_KERNEL);
20710c12851SIlya Dryomov 		memalloc_nofs_restore(nofs_flag);
20810c12851SIlya Dryomov 	} else {
20910c12851SIlya Dryomov 		unsigned int noio_flag = memalloc_noio_save();
21010c12851SIlya Dryomov 		p = kvmalloc(size, GFP_KERNEL);
21110c12851SIlya Dryomov 		memalloc_noio_restore(noio_flag);
212eeb0bed5SIlya Dryomov 	}
213eeb0bed5SIlya Dryomov 
21410c12851SIlya Dryomov 	return p;
215eeb0bed5SIlya Dryomov }
216eeb0bed5SIlya Dryomov 
2173d14c5d2SYehuda Sadeh static int parse_fsid(const char *str, struct ceph_fsid *fsid)
2183d14c5d2SYehuda Sadeh {
2193d14c5d2SYehuda Sadeh 	int i = 0;
2203d14c5d2SYehuda Sadeh 	char tmp[3];
2213d14c5d2SYehuda Sadeh 	int err = -EINVAL;
2223d14c5d2SYehuda Sadeh 	int d;
2233d14c5d2SYehuda Sadeh 
2243d14c5d2SYehuda Sadeh 	dout("parse_fsid '%s'\n", str);
2253d14c5d2SYehuda Sadeh 	tmp[2] = 0;
2263d14c5d2SYehuda Sadeh 	while (*str && i < 16) {
2273d14c5d2SYehuda Sadeh 		if (ispunct(*str)) {
2283d14c5d2SYehuda Sadeh 			str++;
2293d14c5d2SYehuda Sadeh 			continue;
2303d14c5d2SYehuda Sadeh 		}
2313d14c5d2SYehuda Sadeh 		if (!isxdigit(str[0]) || !isxdigit(str[1]))
2323d14c5d2SYehuda Sadeh 			break;
2333d14c5d2SYehuda Sadeh 		tmp[0] = str[0];
2343d14c5d2SYehuda Sadeh 		tmp[1] = str[1];
2353d14c5d2SYehuda Sadeh 		if (sscanf(tmp, "%x", &d) < 1)
2363d14c5d2SYehuda Sadeh 			break;
2373d14c5d2SYehuda Sadeh 		fsid->fsid[i] = d & 0xff;
2383d14c5d2SYehuda Sadeh 		i++;
2393d14c5d2SYehuda Sadeh 		str += 2;
2403d14c5d2SYehuda Sadeh 	}
2413d14c5d2SYehuda Sadeh 
2423d14c5d2SYehuda Sadeh 	if (i == 16)
2433d14c5d2SYehuda Sadeh 		err = 0;
2444c069a58SChengguang Xu 	dout("parse_fsid ret %d got fsid %pU\n", err, fsid);
2453d14c5d2SYehuda Sadeh 	return err;
2463d14c5d2SYehuda Sadeh }
2473d14c5d2SYehuda Sadeh 
2483d14c5d2SYehuda Sadeh /*
2493d14c5d2SYehuda Sadeh  * ceph options
2503d14c5d2SYehuda Sadeh  */
2513d14c5d2SYehuda Sadeh enum {
2523d14c5d2SYehuda Sadeh 	Opt_osdtimeout,
2533d14c5d2SYehuda Sadeh 	Opt_osdkeepalivetimeout,
2543d14c5d2SYehuda Sadeh 	Opt_mount_timeout,
2553d14c5d2SYehuda Sadeh 	Opt_osd_idle_ttl,
2567cc5e38fSIlya Dryomov 	Opt_osd_request_timeout,
2573d14c5d2SYehuda Sadeh 	/* int args above */
2583d14c5d2SYehuda Sadeh 	Opt_fsid,
2593d14c5d2SYehuda Sadeh 	Opt_name,
2603d14c5d2SYehuda Sadeh 	Opt_secret,
261e2c3d29bSTommi Virtanen 	Opt_key,
2623d14c5d2SYehuda Sadeh 	Opt_ip,
2633d14c5d2SYehuda Sadeh 	/* string args above */
264cffaba15SAlex Elder 	Opt_share,
265cffaba15SAlex Elder 	Opt_crc,
266a3fc9800SYan, Zheng 	Opt_cephx_require_signatures,
267a51983e4SIlya Dryomov 	Opt_cephx_sign_messages,
268ba988f87SChaitanya Huilgol 	Opt_tcp_nodelay,
26902b2f549SDongsheng Yang 	Opt_abort_on_full,
2703d14c5d2SYehuda Sadeh };
2713d14c5d2SYehuda Sadeh 
272*d7167b14SAl Viro static const struct fs_parameter_spec ceph_parameters[] = {
27382995cc6SDavid Howells 	fsparam_flag	("abort_on_full",		Opt_abort_on_full),
27482995cc6SDavid Howells 	fsparam_flag_no ("cephx_require_signatures",	Opt_cephx_require_signatures),
27582995cc6SDavid Howells 	fsparam_flag_no ("cephx_sign_messages",		Opt_cephx_sign_messages),
27682995cc6SDavid Howells 	fsparam_flag_no ("crc",				Opt_crc),
27782995cc6SDavid Howells 	fsparam_string	("fsid",			Opt_fsid),
27882995cc6SDavid Howells 	fsparam_string	("ip",				Opt_ip),
27982995cc6SDavid Howells 	fsparam_string	("key",				Opt_key),
28082995cc6SDavid Howells 	fsparam_u32	("mount_timeout",		Opt_mount_timeout),
28182995cc6SDavid Howells 	fsparam_string	("name",			Opt_name),
28282995cc6SDavid Howells 	fsparam_u32	("osd_idle_ttl",		Opt_osd_idle_ttl),
28382995cc6SDavid Howells 	fsparam_u32	("osd_request_timeout",		Opt_osd_request_timeout),
28482995cc6SDavid Howells 	fsparam_u32	("osdkeepalive",		Opt_osdkeepalivetimeout),
28582995cc6SDavid Howells 	__fsparam	(fs_param_is_s32, "osdtimeout", Opt_osdtimeout,
2862710c957SAl Viro 			 fs_param_deprecated, NULL),
28782995cc6SDavid Howells 	fsparam_string	("secret",			Opt_secret),
28882995cc6SDavid Howells 	fsparam_flag_no ("share",			Opt_share),
28982995cc6SDavid Howells 	fsparam_flag_no ("tcp_nodelay",			Opt_tcp_nodelay),
29082995cc6SDavid Howells 	{}
2913d14c5d2SYehuda Sadeh };
2923d14c5d2SYehuda Sadeh 
29382995cc6SDavid Howells struct ceph_options *ceph_alloc_options(void)
29482995cc6SDavid Howells {
29582995cc6SDavid Howells 	struct ceph_options *opt;
29682995cc6SDavid Howells 
29782995cc6SDavid Howells 	opt = kzalloc(sizeof(*opt), GFP_KERNEL);
29882995cc6SDavid Howells 	if (!opt)
29982995cc6SDavid Howells 		return NULL;
30082995cc6SDavid Howells 
30182995cc6SDavid Howells 	opt->mon_addr = kcalloc(CEPH_MAX_MON, sizeof(*opt->mon_addr),
30282995cc6SDavid Howells 				GFP_KERNEL);
30382995cc6SDavid Howells 	if (!opt->mon_addr) {
30482995cc6SDavid Howells 		kfree(opt);
30582995cc6SDavid Howells 		return NULL;
30682995cc6SDavid Howells 	}
30782995cc6SDavid Howells 
30882995cc6SDavid Howells 	opt->flags = CEPH_OPT_DEFAULT;
30982995cc6SDavid Howells 	opt->osd_keepalive_timeout = CEPH_OSD_KEEPALIVE_DEFAULT;
31082995cc6SDavid Howells 	opt->mount_timeout = CEPH_MOUNT_TIMEOUT_DEFAULT;
31182995cc6SDavid Howells 	opt->osd_idle_ttl = CEPH_OSD_IDLE_TTL_DEFAULT;
31282995cc6SDavid Howells 	opt->osd_request_timeout = CEPH_OSD_REQUEST_TIMEOUT_DEFAULT;
31382995cc6SDavid Howells 	return opt;
31482995cc6SDavid Howells }
31582995cc6SDavid Howells EXPORT_SYMBOL(ceph_alloc_options);
31682995cc6SDavid Howells 
3173d14c5d2SYehuda Sadeh void ceph_destroy_options(struct ceph_options *opt)
3183d14c5d2SYehuda Sadeh {
3193d14c5d2SYehuda Sadeh 	dout("destroy_options %p\n", opt);
32082995cc6SDavid Howells 	if (!opt)
32182995cc6SDavid Howells 		return;
32282995cc6SDavid Howells 
3233d14c5d2SYehuda Sadeh 	kfree(opt->name);
3248323c3aaSTommi Virtanen 	if (opt->key) {
3258323c3aaSTommi Virtanen 		ceph_crypto_key_destroy(opt->key);
3268323c3aaSTommi Virtanen 		kfree(opt->key);
3278323c3aaSTommi Virtanen 	}
3281cad7893SNoah Watkins 	kfree(opt->mon_addr);
3293d14c5d2SYehuda Sadeh 	kfree(opt);
3303d14c5d2SYehuda Sadeh }
3313d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_destroy_options);
3323d14c5d2SYehuda Sadeh 
333e2c3d29bSTommi Virtanen /* get secret from key store */
33482995cc6SDavid Howells static int get_secret(struct ceph_crypto_key *dst, const char *name,
3352c3f3dc3SAl Viro 		      struct p_log *log)
33682995cc6SDavid Howells {
337e2c3d29bSTommi Virtanen 	struct key *ukey;
338e2c3d29bSTommi Virtanen 	int key_err;
339e2c3d29bSTommi Virtanen 	int err = 0;
3404b2a58abSTommi Virtanen 	struct ceph_crypto_key *ckey;
341e2c3d29bSTommi Virtanen 
3424b2a58abSTommi Virtanen 	ukey = request_key(&key_type_ceph, name, NULL);
343bad87216SYueHaibing 	if (IS_ERR(ukey)) {
344e2c3d29bSTommi Virtanen 		/* request_key errors don't map nicely to mount(2)
345e2c3d29bSTommi Virtanen 		   errors; don't even try, but still printk */
346e2c3d29bSTommi Virtanen 		key_err = PTR_ERR(ukey);
347e2c3d29bSTommi Virtanen 		switch (key_err) {
348e2c3d29bSTommi Virtanen 		case -ENOKEY:
3492c3f3dc3SAl Viro 			error_plog(log, "Failed due to key not found: %s",
350b9a67899SJoe Perches 			       name);
351e2c3d29bSTommi Virtanen 			break;
352e2c3d29bSTommi Virtanen 		case -EKEYEXPIRED:
3532c3f3dc3SAl Viro 			error_plog(log, "Failed due to expired key: %s",
354b9a67899SJoe Perches 			       name);
355e2c3d29bSTommi Virtanen 			break;
356e2c3d29bSTommi Virtanen 		case -EKEYREVOKED:
3572c3f3dc3SAl Viro 			error_plog(log, "Failed due to revoked key: %s",
358b9a67899SJoe Perches 			       name);
359e2c3d29bSTommi Virtanen 			break;
360e2c3d29bSTommi Virtanen 		default:
3612c3f3dc3SAl Viro 			error_plog(log, "Failed due to key error %d: %s",
362b9a67899SJoe Perches 			       key_err, name);
363e2c3d29bSTommi Virtanen 		}
364e2c3d29bSTommi Virtanen 		err = -EPERM;
365e2c3d29bSTommi Virtanen 		goto out;
366e2c3d29bSTommi Virtanen 	}
367e2c3d29bSTommi Virtanen 
368146aa8b1SDavid Howells 	ckey = ukey->payload.data[0];
3694b2a58abSTommi Virtanen 	err = ceph_crypto_key_clone(dst, ckey);
370e2c3d29bSTommi Virtanen 	if (err)
371e2c3d29bSTommi Virtanen 		goto out_key;
372e2c3d29bSTommi Virtanen 	/* pass through, err is 0 */
373e2c3d29bSTommi Virtanen 
374e2c3d29bSTommi Virtanen out_key:
375e2c3d29bSTommi Virtanen 	key_put(ukey);
376e2c3d29bSTommi Virtanen out:
377e2c3d29bSTommi Virtanen 	return err;
378e2c3d29bSTommi Virtanen }
379e2c3d29bSTommi Virtanen 
38082995cc6SDavid Howells int ceph_parse_mon_ips(const char *buf, size_t len, struct ceph_options *opt,
381c80c98f0SAl Viro 		       struct fc_log *l)
3823d14c5d2SYehuda Sadeh {
383c80c98f0SAl Viro 	struct p_log log = {.prefix = "libceph", .log = l};
38482995cc6SDavid Howells 	int ret;
3853d14c5d2SYehuda Sadeh 
3863d14c5d2SYehuda Sadeh 	/* ip1[:port1][,ip2[:port2]...] */
38782995cc6SDavid Howells 	ret = ceph_parse_ips(buf, buf + len, opt->mon_addr, CEPH_MAX_MON,
38882995cc6SDavid Howells 			     &opt->num_mon);
38982995cc6SDavid Howells 	if (ret) {
3902c3f3dc3SAl Viro 		error_plog(&log, "Failed to parse monitor IPs: %d", ret);
39182995cc6SDavid Howells 		return ret;
39282995cc6SDavid Howells 	}
3933d14c5d2SYehuda Sadeh 
39482995cc6SDavid Howells 	return 0;
3953d14c5d2SYehuda Sadeh }
39682995cc6SDavid Howells EXPORT_SYMBOL(ceph_parse_mon_ips);
39782995cc6SDavid Howells 
39882995cc6SDavid Howells int ceph_parse_param(struct fs_parameter *param, struct ceph_options *opt,
399c80c98f0SAl Viro 		     struct fc_log *l)
40082995cc6SDavid Howells {
40182995cc6SDavid Howells 	struct fs_parse_result result;
40282995cc6SDavid Howells 	int token, err;
403c80c98f0SAl Viro 	struct p_log log = {.prefix = "libceph", .log = l};
40482995cc6SDavid Howells 
405*d7167b14SAl Viro 	token = __fs_parse(&log, ceph_parameters, param, &result);
40682995cc6SDavid Howells 	dout("%s fs_parse '%s' token %d\n", __func__, param->key, token);
40782995cc6SDavid Howells 	if (token < 0)
40882995cc6SDavid Howells 		return token;
40982995cc6SDavid Howells 
4103d14c5d2SYehuda Sadeh 	switch (token) {
4113d14c5d2SYehuda Sadeh 	case Opt_ip:
41282995cc6SDavid Howells 		err = ceph_parse_ips(param->string,
41382995cc6SDavid Howells 				     param->string + param->size,
4143d14c5d2SYehuda Sadeh 				     &opt->my_addr,
4153d14c5d2SYehuda Sadeh 				     1, NULL);
41682995cc6SDavid Howells 		if (err) {
4172c3f3dc3SAl Viro 			error_plog(&log, "Failed to parse ip: %d", err);
41882995cc6SDavid Howells 			return err;
41982995cc6SDavid Howells 		}
4203d14c5d2SYehuda Sadeh 		opt->flags |= CEPH_OPT_MYIP;
4213d14c5d2SYehuda Sadeh 		break;
4223d14c5d2SYehuda Sadeh 
4233d14c5d2SYehuda Sadeh 	case Opt_fsid:
42482995cc6SDavid Howells 		err = parse_fsid(param->string, &opt->fsid);
42582995cc6SDavid Howells 		if (err) {
4262c3f3dc3SAl Viro 			error_plog(&log, "Failed to parse fsid: %d", err);
42782995cc6SDavid Howells 			return err;
42882995cc6SDavid Howells 		}
4293d14c5d2SYehuda Sadeh 		opt->flags |= CEPH_OPT_FSID;
4303d14c5d2SYehuda Sadeh 		break;
4313d14c5d2SYehuda Sadeh 	case Opt_name:
432937441f3SChengguang Xu 		kfree(opt->name);
43382995cc6SDavid Howells 		opt->name = param->string;
43482995cc6SDavid Howells 		param->string = NULL;
4353d14c5d2SYehuda Sadeh 		break;
4363d14c5d2SYehuda Sadeh 	case Opt_secret:
437937441f3SChengguang Xu 		ceph_crypto_key_destroy(opt->key);
438937441f3SChengguang Xu 		kfree(opt->key);
439937441f3SChengguang Xu 
4408323c3aaSTommi Virtanen 		opt->key = kzalloc(sizeof(*opt->key), GFP_KERNEL);
44182995cc6SDavid Howells 		if (!opt->key)
44282995cc6SDavid Howells 			return -ENOMEM;
44382995cc6SDavid Howells 		err = ceph_crypto_key_unarmor(opt->key, param->string);
44482995cc6SDavid Howells 		if (err) {
4452c3f3dc3SAl Viro 			error_plog(&log, "Failed to parse secret: %d", err);
44682995cc6SDavid Howells 			return err;
4478323c3aaSTommi Virtanen 		}
4483d14c5d2SYehuda Sadeh 		break;
449e2c3d29bSTommi Virtanen 	case Opt_key:
450937441f3SChengguang Xu 		ceph_crypto_key_destroy(opt->key);
451937441f3SChengguang Xu 		kfree(opt->key);
452937441f3SChengguang Xu 
453e2c3d29bSTommi Virtanen 		opt->key = kzalloc(sizeof(*opt->key), GFP_KERNEL);
45482995cc6SDavid Howells 		if (!opt->key)
45582995cc6SDavid Howells 			return -ENOMEM;
4562c3f3dc3SAl Viro 		return get_secret(opt->key, param->string, &log);
4573d14c5d2SYehuda Sadeh 
4583d14c5d2SYehuda Sadeh 	case Opt_osdtimeout:
4592c3f3dc3SAl Viro 		warn_plog(&log, "Ignoring osdtimeout");
4603d14c5d2SYehuda Sadeh 		break;
4613d14c5d2SYehuda Sadeh 	case Opt_osdkeepalivetimeout:
462a319bf56SIlya Dryomov 		/* 0 isn't well defined right now, reject it */
46382995cc6SDavid Howells 		if (result.uint_32 < 1 || result.uint_32 > INT_MAX / 1000)
46482995cc6SDavid Howells 			goto out_of_range;
465a319bf56SIlya Dryomov 		opt->osd_keepalive_timeout =
46682995cc6SDavid Howells 		    msecs_to_jiffies(result.uint_32 * 1000);
4673d14c5d2SYehuda Sadeh 		break;
4683d14c5d2SYehuda Sadeh 	case Opt_osd_idle_ttl:
469a319bf56SIlya Dryomov 		/* 0 isn't well defined right now, reject it */
47082995cc6SDavid Howells 		if (result.uint_32 < 1 || result.uint_32 > INT_MAX / 1000)
47182995cc6SDavid Howells 			goto out_of_range;
47282995cc6SDavid Howells 		opt->osd_idle_ttl = msecs_to_jiffies(result.uint_32 * 1000);
4733d14c5d2SYehuda Sadeh 		break;
4743d14c5d2SYehuda Sadeh 	case Opt_mount_timeout:
475a319bf56SIlya Dryomov 		/* 0 is "wait forever" (i.e. infinite timeout) */
47682995cc6SDavid Howells 		if (result.uint_32 > INT_MAX / 1000)
47782995cc6SDavid Howells 			goto out_of_range;
47882995cc6SDavid Howells 		opt->mount_timeout = msecs_to_jiffies(result.uint_32 * 1000);
4793d14c5d2SYehuda Sadeh 		break;
4807cc5e38fSIlya Dryomov 	case Opt_osd_request_timeout:
4817cc5e38fSIlya Dryomov 		/* 0 is "wait forever" (i.e. infinite timeout) */
48282995cc6SDavid Howells 		if (result.uint_32 > INT_MAX / 1000)
48382995cc6SDavid Howells 			goto out_of_range;
48482995cc6SDavid Howells 		opt->osd_request_timeout =
48582995cc6SDavid Howells 		    msecs_to_jiffies(result.uint_32 * 1000);
4867cc5e38fSIlya Dryomov 		break;
4873d14c5d2SYehuda Sadeh 
488cffaba15SAlex Elder 	case Opt_share:
48982995cc6SDavid Howells 		if (!result.negated)
490cffaba15SAlex Elder 			opt->flags &= ~CEPH_OPT_NOSHARE;
49182995cc6SDavid Howells 		else
4923d14c5d2SYehuda Sadeh 			opt->flags |= CEPH_OPT_NOSHARE;
4933d14c5d2SYehuda Sadeh 		break;
494cffaba15SAlex Elder 	case Opt_crc:
49582995cc6SDavid Howells 		if (!result.negated)
496cffaba15SAlex Elder 			opt->flags &= ~CEPH_OPT_NOCRC;
49782995cc6SDavid Howells 		else
4983d14c5d2SYehuda Sadeh 			opt->flags |= CEPH_OPT_NOCRC;
4993d14c5d2SYehuda Sadeh 		break;
500a3fc9800SYan, Zheng 	case Opt_cephx_require_signatures:
50182995cc6SDavid Howells 		if (!result.negated)
502a3fc9800SYan, Zheng 			opt->flags &= ~CEPH_OPT_NOMSGAUTH;
50382995cc6SDavid Howells 		else
504a3fc9800SYan, Zheng 			opt->flags |= CEPH_OPT_NOMSGAUTH;
505a3fc9800SYan, Zheng 		break;
506a51983e4SIlya Dryomov 	case Opt_cephx_sign_messages:
50782995cc6SDavid Howells 		if (!result.negated)
508a51983e4SIlya Dryomov 			opt->flags &= ~CEPH_OPT_NOMSGSIGN;
50982995cc6SDavid Howells 		else
510a51983e4SIlya Dryomov 			opt->flags |= CEPH_OPT_NOMSGSIGN;
511a51983e4SIlya Dryomov 		break;
512ba988f87SChaitanya Huilgol 	case Opt_tcp_nodelay:
51382995cc6SDavid Howells 		if (!result.negated)
514ba988f87SChaitanya Huilgol 			opt->flags |= CEPH_OPT_TCP_NODELAY;
51582995cc6SDavid Howells 		else
516ba988f87SChaitanya Huilgol 			opt->flags &= ~CEPH_OPT_TCP_NODELAY;
517ba988f87SChaitanya Huilgol 		break;
518ba988f87SChaitanya Huilgol 
51902b2f549SDongsheng Yang 	case Opt_abort_on_full:
52002b2f549SDongsheng Yang 		opt->flags |= CEPH_OPT_ABORT_ON_FULL;
52102b2f549SDongsheng Yang 		break;
52202b2f549SDongsheng Yang 
5233d14c5d2SYehuda Sadeh 	default:
52482995cc6SDavid Howells 		BUG();
5253d14c5d2SYehuda Sadeh 	}
5263d14c5d2SYehuda Sadeh 
52782995cc6SDavid Howells 	return 0;
5283d14c5d2SYehuda Sadeh 
52982995cc6SDavid Howells out_of_range:
5302c3f3dc3SAl Viro 	return inval_plog(&log, "%s out of range", param->key);
5313d14c5d2SYehuda Sadeh }
53282995cc6SDavid Howells EXPORT_SYMBOL(ceph_parse_param);
5333d14c5d2SYehuda Sadeh 
53402b2f549SDongsheng Yang int ceph_print_client_options(struct seq_file *m, struct ceph_client *client,
53502b2f549SDongsheng Yang 			      bool show_all)
536ff40f9aeSIlya Dryomov {
537ff40f9aeSIlya Dryomov 	struct ceph_options *opt = client->options;
538ff40f9aeSIlya Dryomov 	size_t pos = m->count;
539ff40f9aeSIlya Dryomov 
540a068acf2SKees Cook 	if (opt->name) {
541a068acf2SKees Cook 		seq_puts(m, "name=");
542a068acf2SKees Cook 		seq_escape(m, opt->name, ", \t\n\\");
543a068acf2SKees Cook 		seq_putc(m, ',');
544a068acf2SKees Cook 	}
545ff40f9aeSIlya Dryomov 	if (opt->key)
546ff40f9aeSIlya Dryomov 		seq_puts(m, "secret=<hidden>,");
547ff40f9aeSIlya Dryomov 
548ff40f9aeSIlya Dryomov 	if (opt->flags & CEPH_OPT_FSID)
549ff40f9aeSIlya Dryomov 		seq_printf(m, "fsid=%pU,", &opt->fsid);
550ff40f9aeSIlya Dryomov 	if (opt->flags & CEPH_OPT_NOSHARE)
551ff40f9aeSIlya Dryomov 		seq_puts(m, "noshare,");
552ff40f9aeSIlya Dryomov 	if (opt->flags & CEPH_OPT_NOCRC)
553ff40f9aeSIlya Dryomov 		seq_puts(m, "nocrc,");
554ff40f9aeSIlya Dryomov 	if (opt->flags & CEPH_OPT_NOMSGAUTH)
555ff40f9aeSIlya Dryomov 		seq_puts(m, "nocephx_require_signatures,");
556a51983e4SIlya Dryomov 	if (opt->flags & CEPH_OPT_NOMSGSIGN)
557a51983e4SIlya Dryomov 		seq_puts(m, "nocephx_sign_messages,");
558ff40f9aeSIlya Dryomov 	if ((opt->flags & CEPH_OPT_TCP_NODELAY) == 0)
559ff40f9aeSIlya Dryomov 		seq_puts(m, "notcp_nodelay,");
56002b2f549SDongsheng Yang 	if (show_all && (opt->flags & CEPH_OPT_ABORT_ON_FULL))
56102b2f549SDongsheng Yang 		seq_puts(m, "abort_on_full,");
562ff40f9aeSIlya Dryomov 
563ff40f9aeSIlya Dryomov 	if (opt->mount_timeout != CEPH_MOUNT_TIMEOUT_DEFAULT)
564a319bf56SIlya Dryomov 		seq_printf(m, "mount_timeout=%d,",
565a319bf56SIlya Dryomov 			   jiffies_to_msecs(opt->mount_timeout) / 1000);
566ff40f9aeSIlya Dryomov 	if (opt->osd_idle_ttl != CEPH_OSD_IDLE_TTL_DEFAULT)
567a319bf56SIlya Dryomov 		seq_printf(m, "osd_idle_ttl=%d,",
568a319bf56SIlya Dryomov 			   jiffies_to_msecs(opt->osd_idle_ttl) / 1000);
569ff40f9aeSIlya Dryomov 	if (opt->osd_keepalive_timeout != CEPH_OSD_KEEPALIVE_DEFAULT)
570ff40f9aeSIlya Dryomov 		seq_printf(m, "osdkeepalivetimeout=%d,",
571a319bf56SIlya Dryomov 		    jiffies_to_msecs(opt->osd_keepalive_timeout) / 1000);
5727cc5e38fSIlya Dryomov 	if (opt->osd_request_timeout != CEPH_OSD_REQUEST_TIMEOUT_DEFAULT)
5737cc5e38fSIlya Dryomov 		seq_printf(m, "osd_request_timeout=%d,",
5747cc5e38fSIlya Dryomov 			   jiffies_to_msecs(opt->osd_request_timeout) / 1000);
575ff40f9aeSIlya Dryomov 
576ff40f9aeSIlya Dryomov 	/* drop redundant comma */
577ff40f9aeSIlya Dryomov 	if (m->count != pos)
578ff40f9aeSIlya Dryomov 		m->count--;
579ff40f9aeSIlya Dryomov 
580ff40f9aeSIlya Dryomov 	return 0;
581ff40f9aeSIlya Dryomov }
582ff40f9aeSIlya Dryomov EXPORT_SYMBOL(ceph_print_client_options);
583ff40f9aeSIlya Dryomov 
584005a07bfSIlya Dryomov struct ceph_entity_addr *ceph_client_addr(struct ceph_client *client)
585005a07bfSIlya Dryomov {
586005a07bfSIlya Dryomov 	return &client->msgr.inst.addr;
587005a07bfSIlya Dryomov }
588005a07bfSIlya Dryomov EXPORT_SYMBOL(ceph_client_addr);
589005a07bfSIlya Dryomov 
590033268a5SIlya Dryomov u64 ceph_client_gid(struct ceph_client *client)
5913d14c5d2SYehuda Sadeh {
5923d14c5d2SYehuda Sadeh 	return client->monc.auth->global_id;
5933d14c5d2SYehuda Sadeh }
594033268a5SIlya Dryomov EXPORT_SYMBOL(ceph_client_gid);
5953d14c5d2SYehuda Sadeh 
5963d14c5d2SYehuda Sadeh /*
5973d14c5d2SYehuda Sadeh  * create a fresh client instance
5983d14c5d2SYehuda Sadeh  */
59974da4a0fSIlya Dryomov struct ceph_client *ceph_create_client(struct ceph_options *opt, void *private)
6003d14c5d2SYehuda Sadeh {
6013d14c5d2SYehuda Sadeh 	struct ceph_client *client;
6026ab00d46SSage Weil 	struct ceph_entity_addr *myaddr = NULL;
603ae5b806aSJason A. Donenfeld 	int err;
604ae5b806aSJason A. Donenfeld 
605ae5b806aSJason A. Donenfeld 	err = wait_for_random_bytes();
606ae5b806aSJason A. Donenfeld 	if (err < 0)
607ae5b806aSJason A. Donenfeld 		return ERR_PTR(err);
6083d14c5d2SYehuda Sadeh 
6093d14c5d2SYehuda Sadeh 	client = kzalloc(sizeof(*client), GFP_KERNEL);
6103d14c5d2SYehuda Sadeh 	if (client == NULL)
6113d14c5d2SYehuda Sadeh 		return ERR_PTR(-ENOMEM);
6123d14c5d2SYehuda Sadeh 
6133d14c5d2SYehuda Sadeh 	client->private = private;
6143d14c5d2SYehuda Sadeh 	client->options = opt;
6153d14c5d2SYehuda Sadeh 
6163d14c5d2SYehuda Sadeh 	mutex_init(&client->mount_mutex);
6173d14c5d2SYehuda Sadeh 	init_waitqueue_head(&client->auth_wq);
6183d14c5d2SYehuda Sadeh 	client->auth_err = 0;
6193d14c5d2SYehuda Sadeh 
6203d14c5d2SYehuda Sadeh 	client->extra_mon_dispatch = NULL;
62174da4a0fSIlya Dryomov 	client->supported_features = CEPH_FEATURES_SUPPORTED_DEFAULT;
62274da4a0fSIlya Dryomov 	client->required_features = CEPH_FEATURES_REQUIRED_DEFAULT;
62374da4a0fSIlya Dryomov 
62474da4a0fSIlya Dryomov 	if (!ceph_test_opt(client, NOMSGAUTH))
62574da4a0fSIlya Dryomov 		client->required_features |= CEPH_FEATURE_MSG_AUTH;
6263d14c5d2SYehuda Sadeh 
6276ab00d46SSage Weil 	/* msgr */
6286ab00d46SSage Weil 	if (ceph_test_opt(client, MYIP))
6296ab00d46SSage Weil 		myaddr = &client->options->my_addr;
630ba988f87SChaitanya Huilgol 
631859bff51SIlya Dryomov 	ceph_messenger_init(&client->msgr, myaddr);
6323d14c5d2SYehuda Sadeh 
6333d14c5d2SYehuda Sadeh 	/* subsystems */
6343d14c5d2SYehuda Sadeh 	err = ceph_monc_init(&client->monc, client);
6353d14c5d2SYehuda Sadeh 	if (err < 0)
63615d9882cSAlex Elder 		goto fail;
6373d14c5d2SYehuda Sadeh 	err = ceph_osdc_init(&client->osdc, client);
6383d14c5d2SYehuda Sadeh 	if (err < 0)
6393d14c5d2SYehuda Sadeh 		goto fail_monc;
6403d14c5d2SYehuda Sadeh 
6413d14c5d2SYehuda Sadeh 	return client;
6423d14c5d2SYehuda Sadeh 
6433d14c5d2SYehuda Sadeh fail_monc:
6443d14c5d2SYehuda Sadeh 	ceph_monc_stop(&client->monc);
6453d14c5d2SYehuda Sadeh fail:
646757856d2SIlya Dryomov 	ceph_messenger_fini(&client->msgr);
6473d14c5d2SYehuda Sadeh 	kfree(client);
6483d14c5d2SYehuda Sadeh 	return ERR_PTR(err);
6493d14c5d2SYehuda Sadeh }
6503d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_create_client);
6513d14c5d2SYehuda Sadeh 
6523d14c5d2SYehuda Sadeh void ceph_destroy_client(struct ceph_client *client)
6533d14c5d2SYehuda Sadeh {
6543d14c5d2SYehuda Sadeh 	dout("destroy_client %p\n", client);
6553d14c5d2SYehuda Sadeh 
656a2a32584SGuanjun He 	atomic_set(&client->msgr.stopping, 1);
657a2a32584SGuanjun He 
6583d14c5d2SYehuda Sadeh 	/* unmount */
6593d14c5d2SYehuda Sadeh 	ceph_osdc_stop(&client->osdc);
6603d14c5d2SYehuda Sadeh 	ceph_monc_stop(&client->monc);
661757856d2SIlya Dryomov 	ceph_messenger_fini(&client->msgr);
6623d14c5d2SYehuda Sadeh 
6633d14c5d2SYehuda Sadeh 	ceph_debugfs_client_cleanup(client);
6643d14c5d2SYehuda Sadeh 
6653d14c5d2SYehuda Sadeh 	ceph_destroy_options(client->options);
6663d14c5d2SYehuda Sadeh 
6673d14c5d2SYehuda Sadeh 	kfree(client);
6683d14c5d2SYehuda Sadeh 	dout("destroy_client %p done\n", client);
6693d14c5d2SYehuda Sadeh }
6703d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_destroy_client);
6713d14c5d2SYehuda Sadeh 
672120a75eaSYan, Zheng void ceph_reset_client_addr(struct ceph_client *client)
673120a75eaSYan, Zheng {
674120a75eaSYan, Zheng 	ceph_messenger_reset_nonce(&client->msgr);
675120a75eaSYan, Zheng 	ceph_monc_reopen_session(&client->monc);
676120a75eaSYan, Zheng 	ceph_osdc_reopen_osds(&client->osdc);
677120a75eaSYan, Zheng }
678120a75eaSYan, Zheng EXPORT_SYMBOL(ceph_reset_client_addr);
679120a75eaSYan, Zheng 
6803d14c5d2SYehuda Sadeh /*
6813d14c5d2SYehuda Sadeh  * true if we have the mon map (and have thus joined the cluster)
6823d14c5d2SYehuda Sadeh  */
6833b33f692SZhang Zhuoyu static bool have_mon_and_osd_map(struct ceph_client *client)
6843d14c5d2SYehuda Sadeh {
6853d14c5d2SYehuda Sadeh 	return client->monc.monmap && client->monc.monmap->epoch &&
6863d14c5d2SYehuda Sadeh 	       client->osdc.osdmap && client->osdc.osdmap->epoch;
6873d14c5d2SYehuda Sadeh }
6883d14c5d2SYehuda Sadeh 
6893d14c5d2SYehuda Sadeh /*
6903d14c5d2SYehuda Sadeh  * mount: join the ceph cluster, and open root directory.
6913d14c5d2SYehuda Sadeh  */
6923d14c5d2SYehuda Sadeh int __ceph_open_session(struct ceph_client *client, unsigned long started)
6933d14c5d2SYehuda Sadeh {
694a319bf56SIlya Dryomov 	unsigned long timeout = client->options->mount_timeout;
695216639ddSIlya Dryomov 	long err;
6963d14c5d2SYehuda Sadeh 
6973d14c5d2SYehuda Sadeh 	/* open session, and wait for mon and osd maps */
6983d14c5d2SYehuda Sadeh 	err = ceph_monc_open_session(&client->monc);
6993d14c5d2SYehuda Sadeh 	if (err < 0)
7003d14c5d2SYehuda Sadeh 		return err;
7013d14c5d2SYehuda Sadeh 
7023d14c5d2SYehuda Sadeh 	while (!have_mon_and_osd_map(client)) {
7033d14c5d2SYehuda Sadeh 		if (timeout && time_after_eq(jiffies, started + timeout))
704216639ddSIlya Dryomov 			return -ETIMEDOUT;
7053d14c5d2SYehuda Sadeh 
7063d14c5d2SYehuda Sadeh 		/* wait */
7073d14c5d2SYehuda Sadeh 		dout("mount waiting for mon_map\n");
7083d14c5d2SYehuda Sadeh 		err = wait_event_interruptible_timeout(client->auth_wq,
7093d14c5d2SYehuda Sadeh 			have_mon_and_osd_map(client) || (client->auth_err < 0),
710a319bf56SIlya Dryomov 			ceph_timeout_jiffies(timeout));
711216639ddSIlya Dryomov 		if (err < 0)
7123d14c5d2SYehuda Sadeh 			return err;
7133d14c5d2SYehuda Sadeh 		if (client->auth_err < 0)
7143d14c5d2SYehuda Sadeh 			return client->auth_err;
7153d14c5d2SYehuda Sadeh 	}
7163d14c5d2SYehuda Sadeh 
717033268a5SIlya Dryomov 	pr_info("client%llu fsid %pU\n", ceph_client_gid(client),
718033268a5SIlya Dryomov 		&client->fsid);
71902ac956cSIlya Dryomov 	ceph_debugfs_client_init(client);
72002ac956cSIlya Dryomov 
7213d14c5d2SYehuda Sadeh 	return 0;
7223d14c5d2SYehuda Sadeh }
7233d14c5d2SYehuda Sadeh EXPORT_SYMBOL(__ceph_open_session);
7243d14c5d2SYehuda Sadeh 
7253d14c5d2SYehuda Sadeh int ceph_open_session(struct ceph_client *client)
7263d14c5d2SYehuda Sadeh {
7273d14c5d2SYehuda Sadeh 	int ret;
7283d14c5d2SYehuda Sadeh 	unsigned long started = jiffies;  /* note the start time */
7293d14c5d2SYehuda Sadeh 
7303d14c5d2SYehuda Sadeh 	dout("open_session start\n");
7313d14c5d2SYehuda Sadeh 	mutex_lock(&client->mount_mutex);
7323d14c5d2SYehuda Sadeh 
7333d14c5d2SYehuda Sadeh 	ret = __ceph_open_session(client, started);
7343d14c5d2SYehuda Sadeh 
7353d14c5d2SYehuda Sadeh 	mutex_unlock(&client->mount_mutex);
7363d14c5d2SYehuda Sadeh 	return ret;
7373d14c5d2SYehuda Sadeh }
7383d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_open_session);
7393d14c5d2SYehuda Sadeh 
740bb229bbbSIlya Dryomov int ceph_wait_for_latest_osdmap(struct ceph_client *client,
741bb229bbbSIlya Dryomov 				unsigned long timeout)
742bb229bbbSIlya Dryomov {
743bb229bbbSIlya Dryomov 	u64 newest_epoch;
744bb229bbbSIlya Dryomov 	int ret;
745bb229bbbSIlya Dryomov 
746bb229bbbSIlya Dryomov 	ret = ceph_monc_get_version(&client->monc, "osdmap", &newest_epoch);
747bb229bbbSIlya Dryomov 	if (ret)
748bb229bbbSIlya Dryomov 		return ret;
749bb229bbbSIlya Dryomov 
750bb229bbbSIlya Dryomov 	if (client->osdc.osdmap->epoch >= newest_epoch)
751bb229bbbSIlya Dryomov 		return 0;
752bb229bbbSIlya Dryomov 
753bb229bbbSIlya Dryomov 	ceph_osdc_maybe_request_map(&client->osdc);
754bb229bbbSIlya Dryomov 	return ceph_monc_wait_osdmap(&client->monc, newest_epoch, timeout);
755bb229bbbSIlya Dryomov }
756bb229bbbSIlya Dryomov EXPORT_SYMBOL(ceph_wait_for_latest_osdmap);
7573d14c5d2SYehuda Sadeh 
7583d14c5d2SYehuda Sadeh static int __init init_ceph_lib(void)
7593d14c5d2SYehuda Sadeh {
7603d14c5d2SYehuda Sadeh 	int ret = 0;
7613d14c5d2SYehuda Sadeh 
7621a829ff2SGreg Kroah-Hartman 	ceph_debugfs_init();
7633d14c5d2SYehuda Sadeh 
7644b2a58abSTommi Virtanen 	ret = ceph_crypto_init();
7653d14c5d2SYehuda Sadeh 	if (ret < 0)
7663d14c5d2SYehuda Sadeh 		goto out_debugfs;
7673d14c5d2SYehuda Sadeh 
7684b2a58abSTommi Virtanen 	ret = ceph_msgr_init();
7694b2a58abSTommi Virtanen 	if (ret < 0)
7704b2a58abSTommi Virtanen 		goto out_crypto;
7714b2a58abSTommi Virtanen 
7725522ae0bSAlex Elder 	ret = ceph_osdc_setup();
7735522ae0bSAlex Elder 	if (ret < 0)
7745522ae0bSAlex Elder 		goto out_msgr;
7755522ae0bSAlex Elder 
7764f6a7e5eSSage Weil 	pr_info("loaded (mon/osd proto %d/%d)\n",
7774f6a7e5eSSage Weil 		CEPH_MONC_PROTOCOL, CEPH_OSDC_PROTOCOL);
7783d14c5d2SYehuda Sadeh 
7793d14c5d2SYehuda Sadeh 	return 0;
7803d14c5d2SYehuda Sadeh 
7815522ae0bSAlex Elder out_msgr:
7825522ae0bSAlex Elder 	ceph_msgr_exit();
7834b2a58abSTommi Virtanen out_crypto:
7844b2a58abSTommi Virtanen 	ceph_crypto_shutdown();
7853d14c5d2SYehuda Sadeh out_debugfs:
7863d14c5d2SYehuda Sadeh 	ceph_debugfs_cleanup();
7873d14c5d2SYehuda Sadeh 	return ret;
7883d14c5d2SYehuda Sadeh }
7893d14c5d2SYehuda Sadeh 
7903d14c5d2SYehuda Sadeh static void __exit exit_ceph_lib(void)
7913d14c5d2SYehuda Sadeh {
7923d14c5d2SYehuda Sadeh 	dout("exit_ceph_lib\n");
79351e92737SYan, Zheng 	WARN_ON(!ceph_strings_empty());
79451e92737SYan, Zheng 
7955522ae0bSAlex Elder 	ceph_osdc_cleanup();
7963d14c5d2SYehuda Sadeh 	ceph_msgr_exit();
7974b2a58abSTommi Virtanen 	ceph_crypto_shutdown();
7983d14c5d2SYehuda Sadeh 	ceph_debugfs_cleanup();
7993d14c5d2SYehuda Sadeh }
8003d14c5d2SYehuda Sadeh 
8013d14c5d2SYehuda Sadeh module_init(init_ceph_lib);
8023d14c5d2SYehuda Sadeh module_exit(exit_ceph_lib);
8033d14c5d2SYehuda Sadeh 
8043d14c5d2SYehuda Sadeh MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
8053d14c5d2SYehuda Sadeh MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
8063d14c5d2SYehuda Sadeh MODULE_AUTHOR("Patience Warnick <patience@newdream.net>");
8076c13a6bbSHong Zhiguo MODULE_DESCRIPTION("Ceph core library");
8083d14c5d2SYehuda Sadeh MODULE_LICENSE("GPL");
809