xref: /openbmc/linux/net/ceph/ceph_common.c (revision f2f87877b8f29a4aa0000a2e3cdf806ac645500e)
13d14c5d2SYehuda Sadeh 
23d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h>
33d14c5d2SYehuda Sadeh #include <linux/backing-dev.h>
43d14c5d2SYehuda Sadeh #include <linux/ctype.h>
53d14c5d2SYehuda Sadeh #include <linux/fs.h>
63d14c5d2SYehuda Sadeh #include <linux/inet.h>
73d14c5d2SYehuda Sadeh #include <linux/in6.h>
8e2c3d29bSTommi Virtanen #include <linux/key.h>
94b2a58abSTommi Virtanen #include <keys/ceph-type.h>
103d14c5d2SYehuda Sadeh #include <linux/module.h>
113d14c5d2SYehuda Sadeh #include <linux/mount.h>
12757856d2SIlya Dryomov #include <linux/nsproxy.h>
133d14c5d2SYehuda Sadeh #include <linux/parser.h>
143d14c5d2SYehuda Sadeh #include <linux/sched.h>
153d14c5d2SYehuda Sadeh #include <linux/seq_file.h>
163d14c5d2SYehuda Sadeh #include <linux/slab.h>
173d14c5d2SYehuda Sadeh #include <linux/statfs.h>
183d14c5d2SYehuda Sadeh #include <linux/string.h>
19eeb0bed5SIlya Dryomov #include <linux/vmalloc.h>
203d14c5d2SYehuda Sadeh 
213d14c5d2SYehuda Sadeh 
221fe60e51SSage Weil #include <linux/ceph/ceph_features.h>
233d14c5d2SYehuda Sadeh #include <linux/ceph/libceph.h>
243d14c5d2SYehuda Sadeh #include <linux/ceph/debugfs.h>
253d14c5d2SYehuda Sadeh #include <linux/ceph/decode.h>
263d14c5d2SYehuda Sadeh #include <linux/ceph/mon_client.h>
273d14c5d2SYehuda Sadeh #include <linux/ceph/auth.h>
288323c3aaSTommi Virtanen #include "crypto.h"
293d14c5d2SYehuda Sadeh 
303d14c5d2SYehuda Sadeh 
3172fe25e3SAlex Elder /*
3272fe25e3SAlex Elder  * Module compatibility interface.  For now it doesn't do anything,
3372fe25e3SAlex Elder  * but its existence signals a certain level of functionality.
3472fe25e3SAlex Elder  *
3572fe25e3SAlex Elder  * The data buffer is used to pass information both to and from
3672fe25e3SAlex Elder  * libceph.  The return value indicates whether libceph determines
3772fe25e3SAlex Elder  * it is compatible with the caller (from another kernel module),
3872fe25e3SAlex Elder  * given the provided data.
3972fe25e3SAlex Elder  *
4072fe25e3SAlex Elder  * The data pointer can be null.
4172fe25e3SAlex Elder  */
4272fe25e3SAlex Elder bool libceph_compatible(void *data)
4372fe25e3SAlex Elder {
441e32d34cSAlex Elder 	return true;
4572fe25e3SAlex Elder }
4672fe25e3SAlex Elder EXPORT_SYMBOL(libceph_compatible);
473d14c5d2SYehuda Sadeh 
48d6a3408aSIlya Dryomov static int param_get_supported_features(char *buffer,
49d6a3408aSIlya Dryomov 					const struct kernel_param *kp)
50d6a3408aSIlya Dryomov {
51d6a3408aSIlya Dryomov 	return sprintf(buffer, "0x%llx", CEPH_FEATURES_SUPPORTED_DEFAULT);
52d6a3408aSIlya Dryomov }
53d6a3408aSIlya Dryomov static const struct kernel_param_ops param_ops_supported_features = {
54d6a3408aSIlya Dryomov 	.get = param_get_supported_features,
55d6a3408aSIlya Dryomov };
56d6a3408aSIlya Dryomov module_param_cb(supported_features, &param_ops_supported_features, NULL,
57d6a3408aSIlya Dryomov 		S_IRUGO);
58d6a3408aSIlya Dryomov 
593d14c5d2SYehuda Sadeh const char *ceph_msg_type_name(int type)
603d14c5d2SYehuda Sadeh {
613d14c5d2SYehuda Sadeh 	switch (type) {
623d14c5d2SYehuda Sadeh 	case CEPH_MSG_SHUTDOWN: return "shutdown";
633d14c5d2SYehuda Sadeh 	case CEPH_MSG_PING: return "ping";
643d14c5d2SYehuda Sadeh 	case CEPH_MSG_AUTH: return "auth";
653d14c5d2SYehuda Sadeh 	case CEPH_MSG_AUTH_REPLY: return "auth_reply";
663d14c5d2SYehuda Sadeh 	case CEPH_MSG_MON_MAP: return "mon_map";
673d14c5d2SYehuda Sadeh 	case CEPH_MSG_MON_GET_MAP: return "mon_get_map";
683d14c5d2SYehuda Sadeh 	case CEPH_MSG_MON_SUBSCRIBE: return "mon_subscribe";
693d14c5d2SYehuda Sadeh 	case CEPH_MSG_MON_SUBSCRIBE_ACK: return "mon_subscribe_ack";
703d14c5d2SYehuda Sadeh 	case CEPH_MSG_STATFS: return "statfs";
713d14c5d2SYehuda Sadeh 	case CEPH_MSG_STATFS_REPLY: return "statfs_reply";
72513a8243SIlya Dryomov 	case CEPH_MSG_MON_GET_VERSION: return "mon_get_version";
73513a8243SIlya Dryomov 	case CEPH_MSG_MON_GET_VERSION_REPLY: return "mon_get_version_reply";
743d14c5d2SYehuda Sadeh 	case CEPH_MSG_MDS_MAP: return "mds_map";
75*f2f87877SChengguang Xu 	case CEPH_MSG_FS_MAP_USER: return "fs_map_user";
763d14c5d2SYehuda Sadeh 	case CEPH_MSG_CLIENT_SESSION: return "client_session";
773d14c5d2SYehuda Sadeh 	case CEPH_MSG_CLIENT_RECONNECT: return "client_reconnect";
783d14c5d2SYehuda Sadeh 	case CEPH_MSG_CLIENT_REQUEST: return "client_request";
793d14c5d2SYehuda Sadeh 	case CEPH_MSG_CLIENT_REQUEST_FORWARD: return "client_request_forward";
803d14c5d2SYehuda Sadeh 	case CEPH_MSG_CLIENT_REPLY: return "client_reply";
813d14c5d2SYehuda Sadeh 	case CEPH_MSG_CLIENT_CAPS: return "client_caps";
823d14c5d2SYehuda Sadeh 	case CEPH_MSG_CLIENT_CAPRELEASE: return "client_cap_release";
833d14c5d2SYehuda Sadeh 	case CEPH_MSG_CLIENT_SNAP: return "client_snap";
843d14c5d2SYehuda Sadeh 	case CEPH_MSG_CLIENT_LEASE: return "client_lease";
85*f2f87877SChengguang Xu 	case CEPH_MSG_POOLOP_REPLY: return "poolop_reply";
86*f2f87877SChengguang Xu 	case CEPH_MSG_POOLOP: return "poolop";
87*f2f87877SChengguang Xu 	case CEPH_MSG_MON_COMMAND: return "mon_command";
88*f2f87877SChengguang Xu 	case CEPH_MSG_MON_COMMAND_ACK: return "mon_command_ack";
893d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_MAP: return "osd_map";
903d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_OP: return "osd_op";
913d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_OPREPLY: return "osd_opreply";
92a40c4f10SYehuda Sadeh 	case CEPH_MSG_WATCH_NOTIFY: return "watch_notify";
93a02a946dSIlya Dryomov 	case CEPH_MSG_OSD_BACKOFF: return "osd_backoff";
943d14c5d2SYehuda Sadeh 	default: return "unknown";
953d14c5d2SYehuda Sadeh 	}
963d14c5d2SYehuda Sadeh }
973d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_msg_type_name);
983d14c5d2SYehuda Sadeh 
993d14c5d2SYehuda Sadeh /*
1003d14c5d2SYehuda Sadeh  * Initially learn our fsid, or verify an fsid matches.
1013d14c5d2SYehuda Sadeh  */
1023d14c5d2SYehuda Sadeh int ceph_check_fsid(struct ceph_client *client, struct ceph_fsid *fsid)
1033d14c5d2SYehuda Sadeh {
1043d14c5d2SYehuda Sadeh 	if (client->have_fsid) {
1053d14c5d2SYehuda Sadeh 		if (ceph_fsid_compare(&client->fsid, fsid)) {
1063d14c5d2SYehuda Sadeh 			pr_err("bad fsid, had %pU got %pU",
1073d14c5d2SYehuda Sadeh 			       &client->fsid, fsid);
1083d14c5d2SYehuda Sadeh 			return -1;
1093d14c5d2SYehuda Sadeh 		}
1103d14c5d2SYehuda Sadeh 	} else {
1113d14c5d2SYehuda Sadeh 		memcpy(&client->fsid, fsid, sizeof(*fsid));
1123d14c5d2SYehuda Sadeh 	}
1133d14c5d2SYehuda Sadeh 	return 0;
1143d14c5d2SYehuda Sadeh }
1153d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_check_fsid);
1163d14c5d2SYehuda Sadeh 
1173d14c5d2SYehuda Sadeh static int strcmp_null(const char *s1, const char *s2)
1183d14c5d2SYehuda Sadeh {
1193d14c5d2SYehuda Sadeh 	if (!s1 && !s2)
1203d14c5d2SYehuda Sadeh 		return 0;
1213d14c5d2SYehuda Sadeh 	if (s1 && !s2)
1223d14c5d2SYehuda Sadeh 		return -1;
1233d14c5d2SYehuda Sadeh 	if (!s1 && s2)
1243d14c5d2SYehuda Sadeh 		return 1;
1253d14c5d2SYehuda Sadeh 	return strcmp(s1, s2);
1263d14c5d2SYehuda Sadeh }
1273d14c5d2SYehuda Sadeh 
1283d14c5d2SYehuda Sadeh int ceph_compare_options(struct ceph_options *new_opt,
1293d14c5d2SYehuda Sadeh 			 struct ceph_client *client)
1303d14c5d2SYehuda Sadeh {
1313d14c5d2SYehuda Sadeh 	struct ceph_options *opt1 = new_opt;
1323d14c5d2SYehuda Sadeh 	struct ceph_options *opt2 = client->options;
1333d14c5d2SYehuda Sadeh 	int ofs = offsetof(struct ceph_options, mon_addr);
1343d14c5d2SYehuda Sadeh 	int i;
1353d14c5d2SYehuda Sadeh 	int ret;
1363d14c5d2SYehuda Sadeh 
137757856d2SIlya Dryomov 	/*
138757856d2SIlya Dryomov 	 * Don't bother comparing options if network namespaces don't
139757856d2SIlya Dryomov 	 * match.
140757856d2SIlya Dryomov 	 */
141757856d2SIlya Dryomov 	if (!net_eq(current->nsproxy->net_ns, read_pnet(&client->msgr.net)))
142757856d2SIlya Dryomov 		return -1;
143757856d2SIlya Dryomov 
1443d14c5d2SYehuda Sadeh 	ret = memcmp(opt1, opt2, ofs);
1453d14c5d2SYehuda Sadeh 	if (ret)
1463d14c5d2SYehuda Sadeh 		return ret;
1473d14c5d2SYehuda Sadeh 
1483d14c5d2SYehuda Sadeh 	ret = strcmp_null(opt1->name, opt2->name);
1493d14c5d2SYehuda Sadeh 	if (ret)
1503d14c5d2SYehuda Sadeh 		return ret;
1513d14c5d2SYehuda Sadeh 
1528323c3aaSTommi Virtanen 	if (opt1->key && !opt2->key)
1538323c3aaSTommi Virtanen 		return -1;
1548323c3aaSTommi Virtanen 	if (!opt1->key && opt2->key)
1558323c3aaSTommi Virtanen 		return 1;
1568323c3aaSTommi Virtanen 	if (opt1->key && opt2->key) {
1578323c3aaSTommi Virtanen 		if (opt1->key->type != opt2->key->type)
1588323c3aaSTommi Virtanen 			return -1;
1598323c3aaSTommi Virtanen 		if (opt1->key->created.tv_sec != opt2->key->created.tv_sec)
1608323c3aaSTommi Virtanen 			return -1;
1618323c3aaSTommi Virtanen 		if (opt1->key->created.tv_nsec != opt2->key->created.tv_nsec)
1628323c3aaSTommi Virtanen 			return -1;
1638323c3aaSTommi Virtanen 		if (opt1->key->len != opt2->key->len)
1648323c3aaSTommi Virtanen 			return -1;
1658323c3aaSTommi Virtanen 		if (opt1->key->key && !opt2->key->key)
1668323c3aaSTommi Virtanen 			return -1;
1678323c3aaSTommi Virtanen 		if (!opt1->key->key && opt2->key->key)
1688323c3aaSTommi Virtanen 			return 1;
1698323c3aaSTommi Virtanen 		if (opt1->key->key && opt2->key->key) {
1708323c3aaSTommi Virtanen 			ret = memcmp(opt1->key->key, opt2->key->key, opt1->key->len);
1713d14c5d2SYehuda Sadeh 			if (ret)
1723d14c5d2SYehuda Sadeh 				return ret;
1738323c3aaSTommi Virtanen 		}
1748323c3aaSTommi Virtanen 	}
1753d14c5d2SYehuda Sadeh 
1763d14c5d2SYehuda Sadeh 	/* any matching mon ip implies a match */
1773d14c5d2SYehuda Sadeh 	for (i = 0; i < opt1->num_mon; i++) {
1783d14c5d2SYehuda Sadeh 		if (ceph_monmap_contains(client->monc.monmap,
1793d14c5d2SYehuda Sadeh 				 &opt1->mon_addr[i]))
1803d14c5d2SYehuda Sadeh 			return 0;
1813d14c5d2SYehuda Sadeh 	}
1823d14c5d2SYehuda Sadeh 	return -1;
1833d14c5d2SYehuda Sadeh }
1843d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_compare_options);
1853d14c5d2SYehuda Sadeh 
186eeb0bed5SIlya Dryomov void *ceph_kvmalloc(size_t size, gfp_t flags)
187eeb0bed5SIlya Dryomov {
188eeb0bed5SIlya Dryomov 	if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
189eeb0bed5SIlya Dryomov 		void *ptr = kmalloc(size, flags | __GFP_NOWARN);
190eeb0bed5SIlya Dryomov 		if (ptr)
191eeb0bed5SIlya Dryomov 			return ptr;
192eeb0bed5SIlya Dryomov 	}
193eeb0bed5SIlya Dryomov 
19419809c2dSMichal Hocko 	return __vmalloc(size, flags, PAGE_KERNEL);
195eeb0bed5SIlya Dryomov }
196eeb0bed5SIlya Dryomov 
1973d14c5d2SYehuda Sadeh 
1983d14c5d2SYehuda Sadeh static int parse_fsid(const char *str, struct ceph_fsid *fsid)
1993d14c5d2SYehuda Sadeh {
2003d14c5d2SYehuda Sadeh 	int i = 0;
2013d14c5d2SYehuda Sadeh 	char tmp[3];
2023d14c5d2SYehuda Sadeh 	int err = -EINVAL;
2033d14c5d2SYehuda Sadeh 	int d;
2043d14c5d2SYehuda Sadeh 
2053d14c5d2SYehuda Sadeh 	dout("parse_fsid '%s'\n", str);
2063d14c5d2SYehuda Sadeh 	tmp[2] = 0;
2073d14c5d2SYehuda Sadeh 	while (*str && i < 16) {
2083d14c5d2SYehuda Sadeh 		if (ispunct(*str)) {
2093d14c5d2SYehuda Sadeh 			str++;
2103d14c5d2SYehuda Sadeh 			continue;
2113d14c5d2SYehuda Sadeh 		}
2123d14c5d2SYehuda Sadeh 		if (!isxdigit(str[0]) || !isxdigit(str[1]))
2133d14c5d2SYehuda Sadeh 			break;
2143d14c5d2SYehuda Sadeh 		tmp[0] = str[0];
2153d14c5d2SYehuda Sadeh 		tmp[1] = str[1];
2163d14c5d2SYehuda Sadeh 		if (sscanf(tmp, "%x", &d) < 1)
2173d14c5d2SYehuda Sadeh 			break;
2183d14c5d2SYehuda Sadeh 		fsid->fsid[i] = d & 0xff;
2193d14c5d2SYehuda Sadeh 		i++;
2203d14c5d2SYehuda Sadeh 		str += 2;
2213d14c5d2SYehuda Sadeh 	}
2223d14c5d2SYehuda Sadeh 
2233d14c5d2SYehuda Sadeh 	if (i == 16)
2243d14c5d2SYehuda Sadeh 		err = 0;
2254c069a58SChengguang Xu 	dout("parse_fsid ret %d got fsid %pU\n", err, fsid);
2263d14c5d2SYehuda Sadeh 	return err;
2273d14c5d2SYehuda Sadeh }
2283d14c5d2SYehuda Sadeh 
2293d14c5d2SYehuda Sadeh /*
2303d14c5d2SYehuda Sadeh  * ceph options
2313d14c5d2SYehuda Sadeh  */
2323d14c5d2SYehuda Sadeh enum {
2333d14c5d2SYehuda Sadeh 	Opt_osdtimeout,
2343d14c5d2SYehuda Sadeh 	Opt_osdkeepalivetimeout,
2353d14c5d2SYehuda Sadeh 	Opt_mount_timeout,
2363d14c5d2SYehuda Sadeh 	Opt_osd_idle_ttl,
2377cc5e38fSIlya Dryomov 	Opt_osd_request_timeout,
2383d14c5d2SYehuda Sadeh 	Opt_last_int,
2393d14c5d2SYehuda Sadeh 	/* int args above */
2403d14c5d2SYehuda Sadeh 	Opt_fsid,
2413d14c5d2SYehuda Sadeh 	Opt_name,
2423d14c5d2SYehuda Sadeh 	Opt_secret,
243e2c3d29bSTommi Virtanen 	Opt_key,
2443d14c5d2SYehuda Sadeh 	Opt_ip,
2453d14c5d2SYehuda Sadeh 	Opt_last_string,
2463d14c5d2SYehuda Sadeh 	/* string args above */
247cffaba15SAlex Elder 	Opt_share,
2483d14c5d2SYehuda Sadeh 	Opt_noshare,
249cffaba15SAlex Elder 	Opt_crc,
2503d14c5d2SYehuda Sadeh 	Opt_nocrc,
251a3fc9800SYan, Zheng 	Opt_cephx_require_signatures,
252a3fc9800SYan, Zheng 	Opt_nocephx_require_signatures,
253a51983e4SIlya Dryomov 	Opt_cephx_sign_messages,
254a51983e4SIlya Dryomov 	Opt_nocephx_sign_messages,
255ba988f87SChaitanya Huilgol 	Opt_tcp_nodelay,
256ba988f87SChaitanya Huilgol 	Opt_notcp_nodelay,
2573d14c5d2SYehuda Sadeh };
2583d14c5d2SYehuda Sadeh 
2593d14c5d2SYehuda Sadeh static match_table_t opt_tokens = {
2603d14c5d2SYehuda Sadeh 	{Opt_osdtimeout, "osdtimeout=%d"},
2613d14c5d2SYehuda Sadeh 	{Opt_osdkeepalivetimeout, "osdkeepalive=%d"},
2623d14c5d2SYehuda Sadeh 	{Opt_mount_timeout, "mount_timeout=%d"},
2633d14c5d2SYehuda Sadeh 	{Opt_osd_idle_ttl, "osd_idle_ttl=%d"},
2647cc5e38fSIlya Dryomov 	{Opt_osd_request_timeout, "osd_request_timeout=%d"},
2653d14c5d2SYehuda Sadeh 	/* int args above */
2663d14c5d2SYehuda Sadeh 	{Opt_fsid, "fsid=%s"},
2673d14c5d2SYehuda Sadeh 	{Opt_name, "name=%s"},
2683d14c5d2SYehuda Sadeh 	{Opt_secret, "secret=%s"},
269e2c3d29bSTommi Virtanen 	{Opt_key, "key=%s"},
2703d14c5d2SYehuda Sadeh 	{Opt_ip, "ip=%s"},
2713d14c5d2SYehuda Sadeh 	/* string args above */
272cffaba15SAlex Elder 	{Opt_share, "share"},
2733d14c5d2SYehuda Sadeh 	{Opt_noshare, "noshare"},
274cffaba15SAlex Elder 	{Opt_crc, "crc"},
2753d14c5d2SYehuda Sadeh 	{Opt_nocrc, "nocrc"},
276a3fc9800SYan, Zheng 	{Opt_cephx_require_signatures, "cephx_require_signatures"},
277a3fc9800SYan, Zheng 	{Opt_nocephx_require_signatures, "nocephx_require_signatures"},
278a51983e4SIlya Dryomov 	{Opt_cephx_sign_messages, "cephx_sign_messages"},
279a51983e4SIlya Dryomov 	{Opt_nocephx_sign_messages, "nocephx_sign_messages"},
280ba988f87SChaitanya Huilgol 	{Opt_tcp_nodelay, "tcp_nodelay"},
281ba988f87SChaitanya Huilgol 	{Opt_notcp_nodelay, "notcp_nodelay"},
2823d14c5d2SYehuda Sadeh 	{-1, NULL}
2833d14c5d2SYehuda Sadeh };
2843d14c5d2SYehuda Sadeh 
2853d14c5d2SYehuda Sadeh void ceph_destroy_options(struct ceph_options *opt)
2863d14c5d2SYehuda Sadeh {
2873d14c5d2SYehuda Sadeh 	dout("destroy_options %p\n", opt);
2883d14c5d2SYehuda Sadeh 	kfree(opt->name);
2898323c3aaSTommi Virtanen 	if (opt->key) {
2908323c3aaSTommi Virtanen 		ceph_crypto_key_destroy(opt->key);
2918323c3aaSTommi Virtanen 		kfree(opt->key);
2928323c3aaSTommi Virtanen 	}
2931cad7893SNoah Watkins 	kfree(opt->mon_addr);
2943d14c5d2SYehuda Sadeh 	kfree(opt);
2953d14c5d2SYehuda Sadeh }
2963d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_destroy_options);
2973d14c5d2SYehuda Sadeh 
298e2c3d29bSTommi Virtanen /* get secret from key store */
299e2c3d29bSTommi Virtanen static int get_secret(struct ceph_crypto_key *dst, const char *name) {
300e2c3d29bSTommi Virtanen 	struct key *ukey;
301e2c3d29bSTommi Virtanen 	int key_err;
302e2c3d29bSTommi Virtanen 	int err = 0;
3034b2a58abSTommi Virtanen 	struct ceph_crypto_key *ckey;
304e2c3d29bSTommi Virtanen 
3054b2a58abSTommi Virtanen 	ukey = request_key(&key_type_ceph, name, NULL);
306e2c3d29bSTommi Virtanen 	if (!ukey || IS_ERR(ukey)) {
307e2c3d29bSTommi Virtanen 		/* request_key errors don't map nicely to mount(2)
308e2c3d29bSTommi Virtanen 		   errors; don't even try, but still printk */
309e2c3d29bSTommi Virtanen 		key_err = PTR_ERR(ukey);
310e2c3d29bSTommi Virtanen 		switch (key_err) {
311e2c3d29bSTommi Virtanen 		case -ENOKEY:
312b9a67899SJoe Perches 			pr_warn("ceph: Mount failed due to key not found: %s\n",
313b9a67899SJoe Perches 				name);
314e2c3d29bSTommi Virtanen 			break;
315e2c3d29bSTommi Virtanen 		case -EKEYEXPIRED:
316b9a67899SJoe Perches 			pr_warn("ceph: Mount failed due to expired key: %s\n",
317b9a67899SJoe Perches 				name);
318e2c3d29bSTommi Virtanen 			break;
319e2c3d29bSTommi Virtanen 		case -EKEYREVOKED:
320b9a67899SJoe Perches 			pr_warn("ceph: Mount failed due to revoked key: %s\n",
321b9a67899SJoe Perches 				name);
322e2c3d29bSTommi Virtanen 			break;
323e2c3d29bSTommi Virtanen 		default:
324b9a67899SJoe Perches 			pr_warn("ceph: Mount failed due to unknown key error %d: %s\n",
325b9a67899SJoe Perches 				key_err, name);
326e2c3d29bSTommi Virtanen 		}
327e2c3d29bSTommi Virtanen 		err = -EPERM;
328e2c3d29bSTommi Virtanen 		goto out;
329e2c3d29bSTommi Virtanen 	}
330e2c3d29bSTommi Virtanen 
331146aa8b1SDavid Howells 	ckey = ukey->payload.data[0];
3324b2a58abSTommi Virtanen 	err = ceph_crypto_key_clone(dst, ckey);
333e2c3d29bSTommi Virtanen 	if (err)
334e2c3d29bSTommi Virtanen 		goto out_key;
335e2c3d29bSTommi Virtanen 	/* pass through, err is 0 */
336e2c3d29bSTommi Virtanen 
337e2c3d29bSTommi Virtanen out_key:
338e2c3d29bSTommi Virtanen 	key_put(ukey);
339e2c3d29bSTommi Virtanen out:
340e2c3d29bSTommi Virtanen 	return err;
341e2c3d29bSTommi Virtanen }
342e2c3d29bSTommi Virtanen 
343ee57741cSAlex Elder struct ceph_options *
344ee57741cSAlex Elder ceph_parse_options(char *options, const char *dev_name,
345ee57741cSAlex Elder 			const char *dev_name_end,
3463d14c5d2SYehuda Sadeh 			int (*parse_extra_token)(char *c, void *private),
3473d14c5d2SYehuda Sadeh 			void *private)
3483d14c5d2SYehuda Sadeh {
3493d14c5d2SYehuda Sadeh 	struct ceph_options *opt;
3503d14c5d2SYehuda Sadeh 	const char *c;
3513d14c5d2SYehuda Sadeh 	int err = -ENOMEM;
3523d14c5d2SYehuda Sadeh 	substring_t argstr[MAX_OPT_ARGS];
3533d14c5d2SYehuda Sadeh 
3543d14c5d2SYehuda Sadeh 	opt = kzalloc(sizeof(*opt), GFP_KERNEL);
3553d14c5d2SYehuda Sadeh 	if (!opt)
356ee57741cSAlex Elder 		return ERR_PTR(-ENOMEM);
3573d14c5d2SYehuda Sadeh 	opt->mon_addr = kcalloc(CEPH_MAX_MON, sizeof(*opt->mon_addr),
3583d14c5d2SYehuda Sadeh 				GFP_KERNEL);
3593d14c5d2SYehuda Sadeh 	if (!opt->mon_addr)
3603d14c5d2SYehuda Sadeh 		goto out;
3613d14c5d2SYehuda Sadeh 
3623d14c5d2SYehuda Sadeh 	dout("parse_options %p options '%s' dev_name '%s'\n", opt, options,
3633d14c5d2SYehuda Sadeh 	     dev_name);
3643d14c5d2SYehuda Sadeh 
3653d14c5d2SYehuda Sadeh 	/* start with defaults */
3663d14c5d2SYehuda Sadeh 	opt->flags = CEPH_OPT_DEFAULT;
3673d14c5d2SYehuda Sadeh 	opt->osd_keepalive_timeout = CEPH_OSD_KEEPALIVE_DEFAULT;
368a319bf56SIlya Dryomov 	opt->mount_timeout = CEPH_MOUNT_TIMEOUT_DEFAULT;
369a319bf56SIlya Dryomov 	opt->osd_idle_ttl = CEPH_OSD_IDLE_TTL_DEFAULT;
3707cc5e38fSIlya Dryomov 	opt->osd_request_timeout = CEPH_OSD_REQUEST_TIMEOUT_DEFAULT;
3713d14c5d2SYehuda Sadeh 
3723d14c5d2SYehuda Sadeh 	/* get mon ip(s) */
3733d14c5d2SYehuda Sadeh 	/* ip1[:port1][,ip2[:port2]...] */
3743d14c5d2SYehuda Sadeh 	err = ceph_parse_ips(dev_name, dev_name_end, opt->mon_addr,
3753d14c5d2SYehuda Sadeh 			     CEPH_MAX_MON, &opt->num_mon);
3763d14c5d2SYehuda Sadeh 	if (err < 0)
3773d14c5d2SYehuda Sadeh 		goto out;
3783d14c5d2SYehuda Sadeh 
3793d14c5d2SYehuda Sadeh 	/* parse mount options */
3803d14c5d2SYehuda Sadeh 	while ((c = strsep(&options, ",")) != NULL) {
3813d14c5d2SYehuda Sadeh 		int token, intval, ret;
3823d14c5d2SYehuda Sadeh 		if (!*c)
3833d14c5d2SYehuda Sadeh 			continue;
3843d14c5d2SYehuda Sadeh 		err = -EINVAL;
3853d14c5d2SYehuda Sadeh 		token = match_token((char *)c, opt_tokens, argstr);
386010e3b48SYehuda Sadeh 		if (token < 0 && parse_extra_token) {
3873d14c5d2SYehuda Sadeh 			/* extra? */
3883d14c5d2SYehuda Sadeh 			err = parse_extra_token((char *)c, private);
3893d14c5d2SYehuda Sadeh 			if (err < 0) {
3903d14c5d2SYehuda Sadeh 				pr_err("bad option at '%s'\n", c);
3913d14c5d2SYehuda Sadeh 				goto out;
3923d14c5d2SYehuda Sadeh 			}
3933d14c5d2SYehuda Sadeh 			continue;
3943d14c5d2SYehuda Sadeh 		}
3953d14c5d2SYehuda Sadeh 		if (token < Opt_last_int) {
3963d14c5d2SYehuda Sadeh 			ret = match_int(&argstr[0], &intval);
3973d14c5d2SYehuda Sadeh 			if (ret < 0) {
3983d14c5d2SYehuda Sadeh 				pr_err("bad mount option arg (not int) "
3993d14c5d2SYehuda Sadeh 				       "at '%s'\n", c);
4003d14c5d2SYehuda Sadeh 				continue;
4013d14c5d2SYehuda Sadeh 			}
4023d14c5d2SYehuda Sadeh 			dout("got int token %d val %d\n", token, intval);
4033d14c5d2SYehuda Sadeh 		} else if (token > Opt_last_int && token < Opt_last_string) {
4043d14c5d2SYehuda Sadeh 			dout("got string token %d val %s\n", token,
4053d14c5d2SYehuda Sadeh 			     argstr[0].from);
4063d14c5d2SYehuda Sadeh 		} else {
4073d14c5d2SYehuda Sadeh 			dout("got token %d\n", token);
4083d14c5d2SYehuda Sadeh 		}
4093d14c5d2SYehuda Sadeh 		switch (token) {
4103d14c5d2SYehuda Sadeh 		case Opt_ip:
4113d14c5d2SYehuda Sadeh 			err = ceph_parse_ips(argstr[0].from,
4123d14c5d2SYehuda Sadeh 					     argstr[0].to,
4133d14c5d2SYehuda Sadeh 					     &opt->my_addr,
4143d14c5d2SYehuda Sadeh 					     1, NULL);
4153d14c5d2SYehuda Sadeh 			if (err < 0)
4163d14c5d2SYehuda Sadeh 				goto out;
4173d14c5d2SYehuda Sadeh 			opt->flags |= CEPH_OPT_MYIP;
4183d14c5d2SYehuda Sadeh 			break;
4193d14c5d2SYehuda Sadeh 
4203d14c5d2SYehuda Sadeh 		case Opt_fsid:
4213d14c5d2SYehuda Sadeh 			err = parse_fsid(argstr[0].from, &opt->fsid);
4223d14c5d2SYehuda Sadeh 			if (err == 0)
4233d14c5d2SYehuda Sadeh 				opt->flags |= CEPH_OPT_FSID;
4243d14c5d2SYehuda Sadeh 			break;
4253d14c5d2SYehuda Sadeh 		case Opt_name:
426937441f3SChengguang Xu 			kfree(opt->name);
4273d14c5d2SYehuda Sadeh 			opt->name = kstrndup(argstr[0].from,
4283d14c5d2SYehuda Sadeh 					      argstr[0].to-argstr[0].from,
4293d14c5d2SYehuda Sadeh 					      GFP_KERNEL);
430affff077SChengguang Xu 			if (!opt->name) {
431affff077SChengguang Xu 				err = -ENOMEM;
432affff077SChengguang Xu 				goto out;
433affff077SChengguang Xu 			}
4343d14c5d2SYehuda Sadeh 			break;
4353d14c5d2SYehuda Sadeh 		case Opt_secret:
436937441f3SChengguang Xu 			ceph_crypto_key_destroy(opt->key);
437937441f3SChengguang Xu 			kfree(opt->key);
438937441f3SChengguang Xu 
4398323c3aaSTommi Virtanen 		        opt->key = kzalloc(sizeof(*opt->key), GFP_KERNEL);
4408323c3aaSTommi Virtanen 			if (!opt->key) {
4418323c3aaSTommi Virtanen 				err = -ENOMEM;
4428323c3aaSTommi Virtanen 				goto out;
4438323c3aaSTommi Virtanen 			}
4448323c3aaSTommi Virtanen 			err = ceph_crypto_key_unarmor(opt->key, argstr[0].from);
4458323c3aaSTommi Virtanen 			if (err < 0)
4468323c3aaSTommi Virtanen 				goto out;
4473d14c5d2SYehuda Sadeh 			break;
448e2c3d29bSTommi Virtanen 		case Opt_key:
449937441f3SChengguang Xu 			ceph_crypto_key_destroy(opt->key);
450937441f3SChengguang Xu 			kfree(opt->key);
451937441f3SChengguang Xu 
452e2c3d29bSTommi Virtanen 		        opt->key = kzalloc(sizeof(*opt->key), GFP_KERNEL);
453e2c3d29bSTommi Virtanen 			if (!opt->key) {
454e2c3d29bSTommi Virtanen 				err = -ENOMEM;
455e2c3d29bSTommi Virtanen 				goto out;
456e2c3d29bSTommi Virtanen 			}
457e2c3d29bSTommi Virtanen 			err = get_secret(opt->key, argstr[0].from);
458e2c3d29bSTommi Virtanen 			if (err < 0)
459e2c3d29bSTommi Virtanen 				goto out;
460e2c3d29bSTommi Virtanen 			break;
4613d14c5d2SYehuda Sadeh 
4623d14c5d2SYehuda Sadeh 			/* misc */
4633d14c5d2SYehuda Sadeh 		case Opt_osdtimeout:
464b9a67899SJoe Perches 			pr_warn("ignoring deprecated osdtimeout option\n");
4653d14c5d2SYehuda Sadeh 			break;
4663d14c5d2SYehuda Sadeh 		case Opt_osdkeepalivetimeout:
467a319bf56SIlya Dryomov 			/* 0 isn't well defined right now, reject it */
468a319bf56SIlya Dryomov 			if (intval < 1 || intval > INT_MAX / 1000) {
469a319bf56SIlya Dryomov 				pr_err("osdkeepalive out of range\n");
470a319bf56SIlya Dryomov 				err = -EINVAL;
471a319bf56SIlya Dryomov 				goto out;
472a319bf56SIlya Dryomov 			}
473a319bf56SIlya Dryomov 			opt->osd_keepalive_timeout =
474a319bf56SIlya Dryomov 					msecs_to_jiffies(intval * 1000);
4753d14c5d2SYehuda Sadeh 			break;
4763d14c5d2SYehuda Sadeh 		case Opt_osd_idle_ttl:
477a319bf56SIlya Dryomov 			/* 0 isn't well defined right now, reject it */
478a319bf56SIlya Dryomov 			if (intval < 1 || intval > INT_MAX / 1000) {
479a319bf56SIlya Dryomov 				pr_err("osd_idle_ttl out of range\n");
480a319bf56SIlya Dryomov 				err = -EINVAL;
481a319bf56SIlya Dryomov 				goto out;
482a319bf56SIlya Dryomov 			}
483a319bf56SIlya Dryomov 			opt->osd_idle_ttl = msecs_to_jiffies(intval * 1000);
4843d14c5d2SYehuda Sadeh 			break;
4853d14c5d2SYehuda Sadeh 		case Opt_mount_timeout:
486a319bf56SIlya Dryomov 			/* 0 is "wait forever" (i.e. infinite timeout) */
487a319bf56SIlya Dryomov 			if (intval < 0 || intval > INT_MAX / 1000) {
488a319bf56SIlya Dryomov 				pr_err("mount_timeout out of range\n");
489a319bf56SIlya Dryomov 				err = -EINVAL;
490a319bf56SIlya Dryomov 				goto out;
491a319bf56SIlya Dryomov 			}
492a319bf56SIlya Dryomov 			opt->mount_timeout = msecs_to_jiffies(intval * 1000);
4933d14c5d2SYehuda Sadeh 			break;
4947cc5e38fSIlya Dryomov 		case Opt_osd_request_timeout:
4957cc5e38fSIlya Dryomov 			/* 0 is "wait forever" (i.e. infinite timeout) */
4967cc5e38fSIlya Dryomov 			if (intval < 0 || intval > INT_MAX / 1000) {
4977cc5e38fSIlya Dryomov 				pr_err("osd_request_timeout out of range\n");
4987cc5e38fSIlya Dryomov 				err = -EINVAL;
4997cc5e38fSIlya Dryomov 				goto out;
5007cc5e38fSIlya Dryomov 			}
5017cc5e38fSIlya Dryomov 			opt->osd_request_timeout = msecs_to_jiffies(intval * 1000);
5027cc5e38fSIlya Dryomov 			break;
5033d14c5d2SYehuda Sadeh 
504cffaba15SAlex Elder 		case Opt_share:
505cffaba15SAlex Elder 			opt->flags &= ~CEPH_OPT_NOSHARE;
506cffaba15SAlex Elder 			break;
5073d14c5d2SYehuda Sadeh 		case Opt_noshare:
5083d14c5d2SYehuda Sadeh 			opt->flags |= CEPH_OPT_NOSHARE;
5093d14c5d2SYehuda Sadeh 			break;
5103d14c5d2SYehuda Sadeh 
511cffaba15SAlex Elder 		case Opt_crc:
512cffaba15SAlex Elder 			opt->flags &= ~CEPH_OPT_NOCRC;
513cffaba15SAlex Elder 			break;
5143d14c5d2SYehuda Sadeh 		case Opt_nocrc:
5153d14c5d2SYehuda Sadeh 			opt->flags |= CEPH_OPT_NOCRC;
5163d14c5d2SYehuda Sadeh 			break;
517ba988f87SChaitanya Huilgol 
518a3fc9800SYan, Zheng 		case Opt_cephx_require_signatures:
519a3fc9800SYan, Zheng 			opt->flags &= ~CEPH_OPT_NOMSGAUTH;
520a3fc9800SYan, Zheng 			break;
521a3fc9800SYan, Zheng 		case Opt_nocephx_require_signatures:
522a3fc9800SYan, Zheng 			opt->flags |= CEPH_OPT_NOMSGAUTH;
523a3fc9800SYan, Zheng 			break;
524a51983e4SIlya Dryomov 		case Opt_cephx_sign_messages:
525a51983e4SIlya Dryomov 			opt->flags &= ~CEPH_OPT_NOMSGSIGN;
526a51983e4SIlya Dryomov 			break;
527a51983e4SIlya Dryomov 		case Opt_nocephx_sign_messages:
528a51983e4SIlya Dryomov 			opt->flags |= CEPH_OPT_NOMSGSIGN;
529a51983e4SIlya Dryomov 			break;
5303d14c5d2SYehuda Sadeh 
531ba988f87SChaitanya Huilgol 		case Opt_tcp_nodelay:
532ba988f87SChaitanya Huilgol 			opt->flags |= CEPH_OPT_TCP_NODELAY;
533ba988f87SChaitanya Huilgol 			break;
534ba988f87SChaitanya Huilgol 		case Opt_notcp_nodelay:
535ba988f87SChaitanya Huilgol 			opt->flags &= ~CEPH_OPT_TCP_NODELAY;
536ba988f87SChaitanya Huilgol 			break;
537ba988f87SChaitanya Huilgol 
5383d14c5d2SYehuda Sadeh 		default:
5393d14c5d2SYehuda Sadeh 			BUG_ON(token);
5403d14c5d2SYehuda Sadeh 		}
5413d14c5d2SYehuda Sadeh 	}
5423d14c5d2SYehuda Sadeh 
5433d14c5d2SYehuda Sadeh 	/* success */
544ee57741cSAlex Elder 	return opt;
5453d14c5d2SYehuda Sadeh 
5463d14c5d2SYehuda Sadeh out:
5473d14c5d2SYehuda Sadeh 	ceph_destroy_options(opt);
548ee57741cSAlex Elder 	return ERR_PTR(err);
5493d14c5d2SYehuda Sadeh }
5503d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_parse_options);
5513d14c5d2SYehuda Sadeh 
552ff40f9aeSIlya Dryomov int ceph_print_client_options(struct seq_file *m, struct ceph_client *client)
553ff40f9aeSIlya Dryomov {
554ff40f9aeSIlya Dryomov 	struct ceph_options *opt = client->options;
555ff40f9aeSIlya Dryomov 	size_t pos = m->count;
556ff40f9aeSIlya Dryomov 
557a068acf2SKees Cook 	if (opt->name) {
558a068acf2SKees Cook 		seq_puts(m, "name=");
559a068acf2SKees Cook 		seq_escape(m, opt->name, ", \t\n\\");
560a068acf2SKees Cook 		seq_putc(m, ',');
561a068acf2SKees Cook 	}
562ff40f9aeSIlya Dryomov 	if (opt->key)
563ff40f9aeSIlya Dryomov 		seq_puts(m, "secret=<hidden>,");
564ff40f9aeSIlya Dryomov 
565ff40f9aeSIlya Dryomov 	if (opt->flags & CEPH_OPT_FSID)
566ff40f9aeSIlya Dryomov 		seq_printf(m, "fsid=%pU,", &opt->fsid);
567ff40f9aeSIlya Dryomov 	if (opt->flags & CEPH_OPT_NOSHARE)
568ff40f9aeSIlya Dryomov 		seq_puts(m, "noshare,");
569ff40f9aeSIlya Dryomov 	if (opt->flags & CEPH_OPT_NOCRC)
570ff40f9aeSIlya Dryomov 		seq_puts(m, "nocrc,");
571ff40f9aeSIlya Dryomov 	if (opt->flags & CEPH_OPT_NOMSGAUTH)
572ff40f9aeSIlya Dryomov 		seq_puts(m, "nocephx_require_signatures,");
573a51983e4SIlya Dryomov 	if (opt->flags & CEPH_OPT_NOMSGSIGN)
574a51983e4SIlya Dryomov 		seq_puts(m, "nocephx_sign_messages,");
575ff40f9aeSIlya Dryomov 	if ((opt->flags & CEPH_OPT_TCP_NODELAY) == 0)
576ff40f9aeSIlya Dryomov 		seq_puts(m, "notcp_nodelay,");
577ff40f9aeSIlya Dryomov 
578ff40f9aeSIlya Dryomov 	if (opt->mount_timeout != CEPH_MOUNT_TIMEOUT_DEFAULT)
579a319bf56SIlya Dryomov 		seq_printf(m, "mount_timeout=%d,",
580a319bf56SIlya Dryomov 			   jiffies_to_msecs(opt->mount_timeout) / 1000);
581ff40f9aeSIlya Dryomov 	if (opt->osd_idle_ttl != CEPH_OSD_IDLE_TTL_DEFAULT)
582a319bf56SIlya Dryomov 		seq_printf(m, "osd_idle_ttl=%d,",
583a319bf56SIlya Dryomov 			   jiffies_to_msecs(opt->osd_idle_ttl) / 1000);
584ff40f9aeSIlya Dryomov 	if (opt->osd_keepalive_timeout != CEPH_OSD_KEEPALIVE_DEFAULT)
585ff40f9aeSIlya Dryomov 		seq_printf(m, "osdkeepalivetimeout=%d,",
586a319bf56SIlya Dryomov 		    jiffies_to_msecs(opt->osd_keepalive_timeout) / 1000);
5877cc5e38fSIlya Dryomov 	if (opt->osd_request_timeout != CEPH_OSD_REQUEST_TIMEOUT_DEFAULT)
5887cc5e38fSIlya Dryomov 		seq_printf(m, "osd_request_timeout=%d,",
5897cc5e38fSIlya Dryomov 			   jiffies_to_msecs(opt->osd_request_timeout) / 1000);
590ff40f9aeSIlya Dryomov 
591ff40f9aeSIlya Dryomov 	/* drop redundant comma */
592ff40f9aeSIlya Dryomov 	if (m->count != pos)
593ff40f9aeSIlya Dryomov 		m->count--;
594ff40f9aeSIlya Dryomov 
595ff40f9aeSIlya Dryomov 	return 0;
596ff40f9aeSIlya Dryomov }
597ff40f9aeSIlya Dryomov EXPORT_SYMBOL(ceph_print_client_options);
598ff40f9aeSIlya Dryomov 
599005a07bfSIlya Dryomov struct ceph_entity_addr *ceph_client_addr(struct ceph_client *client)
600005a07bfSIlya Dryomov {
601005a07bfSIlya Dryomov 	return &client->msgr.inst.addr;
602005a07bfSIlya Dryomov }
603005a07bfSIlya Dryomov EXPORT_SYMBOL(ceph_client_addr);
604005a07bfSIlya Dryomov 
605033268a5SIlya Dryomov u64 ceph_client_gid(struct ceph_client *client)
6063d14c5d2SYehuda Sadeh {
6073d14c5d2SYehuda Sadeh 	return client->monc.auth->global_id;
6083d14c5d2SYehuda Sadeh }
609033268a5SIlya Dryomov EXPORT_SYMBOL(ceph_client_gid);
6103d14c5d2SYehuda Sadeh 
6113d14c5d2SYehuda Sadeh /*
6123d14c5d2SYehuda Sadeh  * create a fresh client instance
6133d14c5d2SYehuda Sadeh  */
61474da4a0fSIlya Dryomov struct ceph_client *ceph_create_client(struct ceph_options *opt, void *private)
6153d14c5d2SYehuda Sadeh {
6163d14c5d2SYehuda Sadeh 	struct ceph_client *client;
6176ab00d46SSage Weil 	struct ceph_entity_addr *myaddr = NULL;
618ae5b806aSJason A. Donenfeld 	int err;
619ae5b806aSJason A. Donenfeld 
620ae5b806aSJason A. Donenfeld 	err = wait_for_random_bytes();
621ae5b806aSJason A. Donenfeld 	if (err < 0)
622ae5b806aSJason A. Donenfeld 		return ERR_PTR(err);
6233d14c5d2SYehuda Sadeh 
6243d14c5d2SYehuda Sadeh 	client = kzalloc(sizeof(*client), GFP_KERNEL);
6253d14c5d2SYehuda Sadeh 	if (client == NULL)
6263d14c5d2SYehuda Sadeh 		return ERR_PTR(-ENOMEM);
6273d14c5d2SYehuda Sadeh 
6283d14c5d2SYehuda Sadeh 	client->private = private;
6293d14c5d2SYehuda Sadeh 	client->options = opt;
6303d14c5d2SYehuda Sadeh 
6313d14c5d2SYehuda Sadeh 	mutex_init(&client->mount_mutex);
6323d14c5d2SYehuda Sadeh 	init_waitqueue_head(&client->auth_wq);
6333d14c5d2SYehuda Sadeh 	client->auth_err = 0;
6343d14c5d2SYehuda Sadeh 
6353d14c5d2SYehuda Sadeh 	client->extra_mon_dispatch = NULL;
63674da4a0fSIlya Dryomov 	client->supported_features = CEPH_FEATURES_SUPPORTED_DEFAULT;
63774da4a0fSIlya Dryomov 	client->required_features = CEPH_FEATURES_REQUIRED_DEFAULT;
63874da4a0fSIlya Dryomov 
63974da4a0fSIlya Dryomov 	if (!ceph_test_opt(client, NOMSGAUTH))
64074da4a0fSIlya Dryomov 		client->required_features |= CEPH_FEATURE_MSG_AUTH;
6413d14c5d2SYehuda Sadeh 
6426ab00d46SSage Weil 	/* msgr */
6436ab00d46SSage Weil 	if (ceph_test_opt(client, MYIP))
6446ab00d46SSage Weil 		myaddr = &client->options->my_addr;
645ba988f87SChaitanya Huilgol 
646859bff51SIlya Dryomov 	ceph_messenger_init(&client->msgr, myaddr);
6473d14c5d2SYehuda Sadeh 
6483d14c5d2SYehuda Sadeh 	/* subsystems */
6493d14c5d2SYehuda Sadeh 	err = ceph_monc_init(&client->monc, client);
6503d14c5d2SYehuda Sadeh 	if (err < 0)
65115d9882cSAlex Elder 		goto fail;
6523d14c5d2SYehuda Sadeh 	err = ceph_osdc_init(&client->osdc, client);
6533d14c5d2SYehuda Sadeh 	if (err < 0)
6543d14c5d2SYehuda Sadeh 		goto fail_monc;
6553d14c5d2SYehuda Sadeh 
6563d14c5d2SYehuda Sadeh 	return client;
6573d14c5d2SYehuda Sadeh 
6583d14c5d2SYehuda Sadeh fail_monc:
6593d14c5d2SYehuda Sadeh 	ceph_monc_stop(&client->monc);
6603d14c5d2SYehuda Sadeh fail:
661757856d2SIlya Dryomov 	ceph_messenger_fini(&client->msgr);
6623d14c5d2SYehuda Sadeh 	kfree(client);
6633d14c5d2SYehuda Sadeh 	return ERR_PTR(err);
6643d14c5d2SYehuda Sadeh }
6653d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_create_client);
6663d14c5d2SYehuda Sadeh 
6673d14c5d2SYehuda Sadeh void ceph_destroy_client(struct ceph_client *client)
6683d14c5d2SYehuda Sadeh {
6693d14c5d2SYehuda Sadeh 	dout("destroy_client %p\n", client);
6703d14c5d2SYehuda Sadeh 
671a2a32584SGuanjun He 	atomic_set(&client->msgr.stopping, 1);
672a2a32584SGuanjun He 
6733d14c5d2SYehuda Sadeh 	/* unmount */
6743d14c5d2SYehuda Sadeh 	ceph_osdc_stop(&client->osdc);
6753d14c5d2SYehuda Sadeh 	ceph_monc_stop(&client->monc);
676757856d2SIlya Dryomov 	ceph_messenger_fini(&client->msgr);
6773d14c5d2SYehuda Sadeh 
6783d14c5d2SYehuda Sadeh 	ceph_debugfs_client_cleanup(client);
6793d14c5d2SYehuda Sadeh 
6803d14c5d2SYehuda Sadeh 	ceph_destroy_options(client->options);
6813d14c5d2SYehuda Sadeh 
6823d14c5d2SYehuda Sadeh 	kfree(client);
6833d14c5d2SYehuda Sadeh 	dout("destroy_client %p done\n", client);
6843d14c5d2SYehuda Sadeh }
6853d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_destroy_client);
6863d14c5d2SYehuda Sadeh 
6873d14c5d2SYehuda Sadeh /*
6883d14c5d2SYehuda Sadeh  * true if we have the mon map (and have thus joined the cluster)
6893d14c5d2SYehuda Sadeh  */
6903b33f692SZhang Zhuoyu static bool have_mon_and_osd_map(struct ceph_client *client)
6913d14c5d2SYehuda Sadeh {
6923d14c5d2SYehuda Sadeh 	return client->monc.monmap && client->monc.monmap->epoch &&
6933d14c5d2SYehuda Sadeh 	       client->osdc.osdmap && client->osdc.osdmap->epoch;
6943d14c5d2SYehuda Sadeh }
6953d14c5d2SYehuda Sadeh 
6963d14c5d2SYehuda Sadeh /*
6973d14c5d2SYehuda Sadeh  * mount: join the ceph cluster, and open root directory.
6983d14c5d2SYehuda Sadeh  */
6993d14c5d2SYehuda Sadeh int __ceph_open_session(struct ceph_client *client, unsigned long started)
7003d14c5d2SYehuda Sadeh {
701a319bf56SIlya Dryomov 	unsigned long timeout = client->options->mount_timeout;
702216639ddSIlya Dryomov 	long err;
7033d14c5d2SYehuda Sadeh 
7043d14c5d2SYehuda Sadeh 	/* open session, and wait for mon and osd maps */
7053d14c5d2SYehuda Sadeh 	err = ceph_monc_open_session(&client->monc);
7063d14c5d2SYehuda Sadeh 	if (err < 0)
7073d14c5d2SYehuda Sadeh 		return err;
7083d14c5d2SYehuda Sadeh 
7093d14c5d2SYehuda Sadeh 	while (!have_mon_and_osd_map(client)) {
7103d14c5d2SYehuda Sadeh 		if (timeout && time_after_eq(jiffies, started + timeout))
711216639ddSIlya Dryomov 			return -ETIMEDOUT;
7123d14c5d2SYehuda Sadeh 
7133d14c5d2SYehuda Sadeh 		/* wait */
7143d14c5d2SYehuda Sadeh 		dout("mount waiting for mon_map\n");
7153d14c5d2SYehuda Sadeh 		err = wait_event_interruptible_timeout(client->auth_wq,
7163d14c5d2SYehuda Sadeh 			have_mon_and_osd_map(client) || (client->auth_err < 0),
717a319bf56SIlya Dryomov 			ceph_timeout_jiffies(timeout));
718216639ddSIlya Dryomov 		if (err < 0)
7193d14c5d2SYehuda Sadeh 			return err;
7203d14c5d2SYehuda Sadeh 		if (client->auth_err < 0)
7213d14c5d2SYehuda Sadeh 			return client->auth_err;
7223d14c5d2SYehuda Sadeh 	}
7233d14c5d2SYehuda Sadeh 
724033268a5SIlya Dryomov 	pr_info("client%llu fsid %pU\n", ceph_client_gid(client),
725033268a5SIlya Dryomov 		&client->fsid);
72602ac956cSIlya Dryomov 	ceph_debugfs_client_init(client);
72702ac956cSIlya Dryomov 
7283d14c5d2SYehuda Sadeh 	return 0;
7293d14c5d2SYehuda Sadeh }
7303d14c5d2SYehuda Sadeh EXPORT_SYMBOL(__ceph_open_session);
7313d14c5d2SYehuda Sadeh 
7323d14c5d2SYehuda Sadeh 
7333d14c5d2SYehuda Sadeh int ceph_open_session(struct ceph_client *client)
7343d14c5d2SYehuda Sadeh {
7353d14c5d2SYehuda Sadeh 	int ret;
7363d14c5d2SYehuda Sadeh 	unsigned long started = jiffies;  /* note the start time */
7373d14c5d2SYehuda Sadeh 
7383d14c5d2SYehuda Sadeh 	dout("open_session start\n");
7393d14c5d2SYehuda Sadeh 	mutex_lock(&client->mount_mutex);
7403d14c5d2SYehuda Sadeh 
7413d14c5d2SYehuda Sadeh 	ret = __ceph_open_session(client, started);
7423d14c5d2SYehuda Sadeh 
7433d14c5d2SYehuda Sadeh 	mutex_unlock(&client->mount_mutex);
7443d14c5d2SYehuda Sadeh 	return ret;
7453d14c5d2SYehuda Sadeh }
7463d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_open_session);
7473d14c5d2SYehuda Sadeh 
7483d14c5d2SYehuda Sadeh 
7493d14c5d2SYehuda Sadeh static int __init init_ceph_lib(void)
7503d14c5d2SYehuda Sadeh {
7513d14c5d2SYehuda Sadeh 	int ret = 0;
7523d14c5d2SYehuda Sadeh 
7533d14c5d2SYehuda Sadeh 	ret = ceph_debugfs_init();
7543d14c5d2SYehuda Sadeh 	if (ret < 0)
7553d14c5d2SYehuda Sadeh 		goto out;
7563d14c5d2SYehuda Sadeh 
7574b2a58abSTommi Virtanen 	ret = ceph_crypto_init();
7583d14c5d2SYehuda Sadeh 	if (ret < 0)
7593d14c5d2SYehuda Sadeh 		goto out_debugfs;
7603d14c5d2SYehuda Sadeh 
7614b2a58abSTommi Virtanen 	ret = ceph_msgr_init();
7624b2a58abSTommi Virtanen 	if (ret < 0)
7634b2a58abSTommi Virtanen 		goto out_crypto;
7644b2a58abSTommi Virtanen 
7655522ae0bSAlex Elder 	ret = ceph_osdc_setup();
7665522ae0bSAlex Elder 	if (ret < 0)
7675522ae0bSAlex Elder 		goto out_msgr;
7685522ae0bSAlex Elder 
7694f6a7e5eSSage Weil 	pr_info("loaded (mon/osd proto %d/%d)\n",
7704f6a7e5eSSage Weil 		CEPH_MONC_PROTOCOL, CEPH_OSDC_PROTOCOL);
7713d14c5d2SYehuda Sadeh 
7723d14c5d2SYehuda Sadeh 	return 0;
7733d14c5d2SYehuda Sadeh 
7745522ae0bSAlex Elder out_msgr:
7755522ae0bSAlex Elder 	ceph_msgr_exit();
7764b2a58abSTommi Virtanen out_crypto:
7774b2a58abSTommi Virtanen 	ceph_crypto_shutdown();
7783d14c5d2SYehuda Sadeh out_debugfs:
7793d14c5d2SYehuda Sadeh 	ceph_debugfs_cleanup();
7803d14c5d2SYehuda Sadeh out:
7813d14c5d2SYehuda Sadeh 	return ret;
7823d14c5d2SYehuda Sadeh }
7833d14c5d2SYehuda Sadeh 
7843d14c5d2SYehuda Sadeh static void __exit exit_ceph_lib(void)
7853d14c5d2SYehuda Sadeh {
7863d14c5d2SYehuda Sadeh 	dout("exit_ceph_lib\n");
78751e92737SYan, Zheng 	WARN_ON(!ceph_strings_empty());
78851e92737SYan, Zheng 
7895522ae0bSAlex Elder 	ceph_osdc_cleanup();
7903d14c5d2SYehuda Sadeh 	ceph_msgr_exit();
7914b2a58abSTommi Virtanen 	ceph_crypto_shutdown();
7923d14c5d2SYehuda Sadeh 	ceph_debugfs_cleanup();
7933d14c5d2SYehuda Sadeh }
7943d14c5d2SYehuda Sadeh 
7953d14c5d2SYehuda Sadeh module_init(init_ceph_lib);
7963d14c5d2SYehuda Sadeh module_exit(exit_ceph_lib);
7973d14c5d2SYehuda Sadeh 
7983d14c5d2SYehuda Sadeh MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
7993d14c5d2SYehuda Sadeh MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
8003d14c5d2SYehuda Sadeh MODULE_AUTHOR("Patience Warnick <patience@newdream.net>");
8016c13a6bbSHong Zhiguo MODULE_DESCRIPTION("Ceph core library");
8023d14c5d2SYehuda Sadeh MODULE_LICENSE("GPL");
803