1 
2 #include <linux/ceph/ceph_debug.h>
3 #include <linux/backing-dev.h>
4 #include <linux/ctype.h>
5 #include <linux/fs.h>
6 #include <linux/inet.h>
7 #include <linux/in6.h>
8 #include <linux/key.h>
9 #include <keys/ceph-type.h>
10 #include <linux/module.h>
11 #include <linux/mount.h>
12 #include <linux/parser.h>
13 #include <linux/sched.h>
14 #include <linux/seq_file.h>
15 #include <linux/slab.h>
16 #include <linux/statfs.h>
17 #include <linux/string.h>
18 
19 
20 #include <linux/ceph/libceph.h>
21 #include <linux/ceph/debugfs.h>
22 #include <linux/ceph/decode.h>
23 #include <linux/ceph/mon_client.h>
24 #include <linux/ceph/auth.h>
25 #include "crypto.h"
26 
27 
28 
29 /*
30  * find filename portion of a path (/foo/bar/baz -> baz)
31  */
ceph_file_part(const char * s,int len)32 const char *ceph_file_part(const char *s, int len)
33 {
34 	const char *e = s + len;
35 
36 	while (e != s && *(e-1) != '/')
37 		e--;
38 	return e;
39 }
40 EXPORT_SYMBOL(ceph_file_part);
41 
ceph_msg_type_name(int type)42 const char *ceph_msg_type_name(int type)
43 {
44 	switch (type) {
45 	case CEPH_MSG_SHUTDOWN: return "shutdown";
46 	case CEPH_MSG_PING: return "ping";
47 	case CEPH_MSG_AUTH: return "auth";
48 	case CEPH_MSG_AUTH_REPLY: return "auth_reply";
49 	case CEPH_MSG_MON_MAP: return "mon_map";
50 	case CEPH_MSG_MON_GET_MAP: return "mon_get_map";
51 	case CEPH_MSG_MON_SUBSCRIBE: return "mon_subscribe";
52 	case CEPH_MSG_MON_SUBSCRIBE_ACK: return "mon_subscribe_ack";
53 	case CEPH_MSG_STATFS: return "statfs";
54 	case CEPH_MSG_STATFS_REPLY: return "statfs_reply";
55 	case CEPH_MSG_MDS_MAP: return "mds_map";
56 	case CEPH_MSG_CLIENT_SESSION: return "client_session";
57 	case CEPH_MSG_CLIENT_RECONNECT: return "client_reconnect";
58 	case CEPH_MSG_CLIENT_REQUEST: return "client_request";
59 	case CEPH_MSG_CLIENT_REQUEST_FORWARD: return "client_request_forward";
60 	case CEPH_MSG_CLIENT_REPLY: return "client_reply";
61 	case CEPH_MSG_CLIENT_CAPS: return "client_caps";
62 	case CEPH_MSG_CLIENT_CAPRELEASE: return "client_cap_release";
63 	case CEPH_MSG_CLIENT_SNAP: return "client_snap";
64 	case CEPH_MSG_CLIENT_LEASE: return "client_lease";
65 	case CEPH_MSG_OSD_MAP: return "osd_map";
66 	case CEPH_MSG_OSD_OP: return "osd_op";
67 	case CEPH_MSG_OSD_OPREPLY: return "osd_opreply";
68 	case CEPH_MSG_WATCH_NOTIFY: return "watch_notify";
69 	default: return "unknown";
70 	}
71 }
72 EXPORT_SYMBOL(ceph_msg_type_name);
73 
74 /*
75  * Initially learn our fsid, or verify an fsid matches.
76  */
ceph_check_fsid(struct ceph_client * client,struct ceph_fsid * fsid)77 int ceph_check_fsid(struct ceph_client *client, struct ceph_fsid *fsid)
78 {
79 	if (client->have_fsid) {
80 		if (ceph_fsid_compare(&client->fsid, fsid)) {
81 			pr_err("bad fsid, had %pU got %pU",
82 			       &client->fsid, fsid);
83 			return -1;
84 		}
85 	} else {
86 		memcpy(&client->fsid, fsid, sizeof(*fsid));
87 	}
88 	return 0;
89 }
90 EXPORT_SYMBOL(ceph_check_fsid);
91 
strcmp_null(const char * s1,const char * s2)92 static int strcmp_null(const char *s1, const char *s2)
93 {
94 	if (!s1 && !s2)
95 		return 0;
96 	if (s1 && !s2)
97 		return -1;
98 	if (!s1 && s2)
99 		return 1;
100 	return strcmp(s1, s2);
101 }
102 
ceph_compare_options(struct ceph_options * new_opt,struct ceph_client * client)103 int ceph_compare_options(struct ceph_options *new_opt,
104 			 struct ceph_client *client)
105 {
106 	struct ceph_options *opt1 = new_opt;
107 	struct ceph_options *opt2 = client->options;
108 	int ofs = offsetof(struct ceph_options, mon_addr);
109 	int i;
110 	int ret;
111 
112 	ret = memcmp(opt1, opt2, ofs);
113 	if (ret)
114 		return ret;
115 
116 	ret = strcmp_null(opt1->name, opt2->name);
117 	if (ret)
118 		return ret;
119 
120 	if (opt1->key && !opt2->key)
121 		return -1;
122 	if (!opt1->key && opt2->key)
123 		return 1;
124 	if (opt1->key && opt2->key) {
125 		if (opt1->key->type != opt2->key->type)
126 			return -1;
127 		if (opt1->key->created.tv_sec != opt2->key->created.tv_sec)
128 			return -1;
129 		if (opt1->key->created.tv_nsec != opt2->key->created.tv_nsec)
130 			return -1;
131 		if (opt1->key->len != opt2->key->len)
132 			return -1;
133 		if (opt1->key->key && !opt2->key->key)
134 			return -1;
135 		if (!opt1->key->key && opt2->key->key)
136 			return 1;
137 		if (opt1->key->key && opt2->key->key) {
138 			ret = memcmp(opt1->key->key, opt2->key->key, opt1->key->len);
139 			if (ret)
140 				return ret;
141 		}
142 	}
143 
144 	/* any matching mon ip implies a match */
145 	for (i = 0; i < opt1->num_mon; i++) {
146 		if (ceph_monmap_contains(client->monc.monmap,
147 				 &opt1->mon_addr[i]))
148 			return 0;
149 	}
150 	return -1;
151 }
152 EXPORT_SYMBOL(ceph_compare_options);
153 
154 
parse_fsid(const char * str,struct ceph_fsid * fsid)155 static int parse_fsid(const char *str, struct ceph_fsid *fsid)
156 {
157 	int i = 0;
158 	char tmp[3];
159 	int err = -EINVAL;
160 	int d;
161 
162 	dout("parse_fsid '%s'\n", str);
163 	tmp[2] = 0;
164 	while (*str && i < 16) {
165 		if (ispunct(*str)) {
166 			str++;
167 			continue;
168 		}
169 		if (!isxdigit(str[0]) || !isxdigit(str[1]))
170 			break;
171 		tmp[0] = str[0];
172 		tmp[1] = str[1];
173 		if (sscanf(tmp, "%x", &d) < 1)
174 			break;
175 		fsid->fsid[i] = d & 0xff;
176 		i++;
177 		str += 2;
178 	}
179 
180 	if (i == 16)
181 		err = 0;
182 	dout("parse_fsid ret %d got fsid %pU", err, fsid);
183 	return err;
184 }
185 
186 /*
187  * ceph options
188  */
189 enum {
190 	Opt_osdtimeout,
191 	Opt_osdkeepalivetimeout,
192 	Opt_mount_timeout,
193 	Opt_osd_idle_ttl,
194 	Opt_last_int,
195 	/* int args above */
196 	Opt_fsid,
197 	Opt_name,
198 	Opt_secret,
199 	Opt_key,
200 	Opt_ip,
201 	Opt_last_string,
202 	/* string args above */
203 	Opt_share,
204 	Opt_noshare,
205 	Opt_crc,
206 	Opt_nocrc,
207 };
208 
209 static match_table_t opt_tokens = {
210 	{Opt_osdtimeout, "osdtimeout=%d"},
211 	{Opt_osdkeepalivetimeout, "osdkeepalive=%d"},
212 	{Opt_mount_timeout, "mount_timeout=%d"},
213 	{Opt_osd_idle_ttl, "osd_idle_ttl=%d"},
214 	/* int args above */
215 	{Opt_fsid, "fsid=%s"},
216 	{Opt_name, "name=%s"},
217 	{Opt_secret, "secret=%s"},
218 	{Opt_key, "key=%s"},
219 	{Opt_ip, "ip=%s"},
220 	/* string args above */
221 	{Opt_share, "share"},
222 	{Opt_noshare, "noshare"},
223 	{Opt_crc, "crc"},
224 	{Opt_nocrc, "nocrc"},
225 	{-1, NULL}
226 };
227 
ceph_destroy_options(struct ceph_options * opt)228 void ceph_destroy_options(struct ceph_options *opt)
229 {
230 	dout("destroy_options %p\n", opt);
231 	kfree(opt->name);
232 	if (opt->key) {
233 		ceph_crypto_key_destroy(opt->key);
234 		kfree(opt->key);
235 	}
236 	kfree(opt->mon_addr);
237 	kfree(opt);
238 }
239 EXPORT_SYMBOL(ceph_destroy_options);
240 
241 /* get secret from key store */
get_secret(struct ceph_crypto_key * dst,const char * name)242 static int get_secret(struct ceph_crypto_key *dst, const char *name) {
243 	struct key *ukey;
244 	int key_err;
245 	int err = 0;
246 	struct ceph_crypto_key *ckey;
247 
248 	ukey = request_key(&key_type_ceph, name, NULL);
249 	if (!ukey || IS_ERR(ukey)) {
250 		/* request_key errors don't map nicely to mount(2)
251 		   errors; don't even try, but still printk */
252 		key_err = PTR_ERR(ukey);
253 		switch (key_err) {
254 		case -ENOKEY:
255 			pr_warning("ceph: Mount failed due to key not found: %s\n", name);
256 			break;
257 		case -EKEYEXPIRED:
258 			pr_warning("ceph: Mount failed due to expired key: %s\n", name);
259 			break;
260 		case -EKEYREVOKED:
261 			pr_warning("ceph: Mount failed due to revoked key: %s\n", name);
262 			break;
263 		default:
264 			pr_warning("ceph: Mount failed due to unknown key error"
265 			       " %d: %s\n", key_err, name);
266 		}
267 		err = -EPERM;
268 		goto out;
269 	}
270 
271 	ckey = ukey->payload.data;
272 	err = ceph_crypto_key_clone(dst, ckey);
273 	if (err)
274 		goto out_key;
275 	/* pass through, err is 0 */
276 
277 out_key:
278 	key_put(ukey);
279 out:
280 	return err;
281 }
282 
283 struct ceph_options *
ceph_parse_options(char * options,const char * dev_name,const char * dev_name_end,int (* parse_extra_token)(char * c,void * private),void * private)284 ceph_parse_options(char *options, const char *dev_name,
285 			const char *dev_name_end,
286 			int (*parse_extra_token)(char *c, void *private),
287 			void *private)
288 {
289 	struct ceph_options *opt;
290 	const char *c;
291 	int err = -ENOMEM;
292 	substring_t argstr[MAX_OPT_ARGS];
293 
294 	opt = kzalloc(sizeof(*opt), GFP_KERNEL);
295 	if (!opt)
296 		return ERR_PTR(-ENOMEM);
297 	opt->mon_addr = kcalloc(CEPH_MAX_MON, sizeof(*opt->mon_addr),
298 				GFP_KERNEL);
299 	if (!opt->mon_addr)
300 		goto out;
301 
302 	dout("parse_options %p options '%s' dev_name '%s'\n", opt, options,
303 	     dev_name);
304 
305 	/* start with defaults */
306 	opt->flags = CEPH_OPT_DEFAULT;
307 	opt->osd_keepalive_timeout = CEPH_OSD_KEEPALIVE_DEFAULT;
308 	opt->mount_timeout = CEPH_MOUNT_TIMEOUT_DEFAULT; /* seconds */
309 	opt->osd_idle_ttl = CEPH_OSD_IDLE_TTL_DEFAULT;   /* seconds */
310 
311 	/* get mon ip(s) */
312 	/* ip1[:port1][,ip2[:port2]...] */
313 	err = ceph_parse_ips(dev_name, dev_name_end, opt->mon_addr,
314 			     CEPH_MAX_MON, &opt->num_mon);
315 	if (err < 0)
316 		goto out;
317 
318 	/* parse mount options */
319 	while ((c = strsep(&options, ",")) != NULL) {
320 		int token, intval, ret;
321 		if (!*c)
322 			continue;
323 		err = -EINVAL;
324 		token = match_token((char *)c, opt_tokens, argstr);
325 		if (token < 0 && parse_extra_token) {
326 			/* extra? */
327 			err = parse_extra_token((char *)c, private);
328 			if (err < 0) {
329 				pr_err("bad option at '%s'\n", c);
330 				goto out;
331 			}
332 			continue;
333 		}
334 		if (token < Opt_last_int) {
335 			ret = match_int(&argstr[0], &intval);
336 			if (ret < 0) {
337 				pr_err("bad mount option arg (not int) "
338 				       "at '%s'\n", c);
339 				continue;
340 			}
341 			dout("got int token %d val %d\n", token, intval);
342 		} else if (token > Opt_last_int && token < Opt_last_string) {
343 			dout("got string token %d val %s\n", token,
344 			     argstr[0].from);
345 		} else {
346 			dout("got token %d\n", token);
347 		}
348 		switch (token) {
349 		case Opt_ip:
350 			err = ceph_parse_ips(argstr[0].from,
351 					     argstr[0].to,
352 					     &opt->my_addr,
353 					     1, NULL);
354 			if (err < 0)
355 				goto out;
356 			opt->flags |= CEPH_OPT_MYIP;
357 			break;
358 
359 		case Opt_fsid:
360 			err = parse_fsid(argstr[0].from, &opt->fsid);
361 			if (err == 0)
362 				opt->flags |= CEPH_OPT_FSID;
363 			break;
364 		case Opt_name:
365 			opt->name = kstrndup(argstr[0].from,
366 					      argstr[0].to-argstr[0].from,
367 					      GFP_KERNEL);
368 			break;
369 		case Opt_secret:
370 		        opt->key = kzalloc(sizeof(*opt->key), GFP_KERNEL);
371 			if (!opt->key) {
372 				err = -ENOMEM;
373 				goto out;
374 			}
375 			err = ceph_crypto_key_unarmor(opt->key, argstr[0].from);
376 			if (err < 0)
377 				goto out;
378 			break;
379 		case Opt_key:
380 		        opt->key = kzalloc(sizeof(*opt->key), GFP_KERNEL);
381 			if (!opt->key) {
382 				err = -ENOMEM;
383 				goto out;
384 			}
385 			err = get_secret(opt->key, argstr[0].from);
386 			if (err < 0)
387 				goto out;
388 			break;
389 
390 			/* misc */
391 		case Opt_osdtimeout:
392 			pr_warning("ignoring deprecated osdtimeout option\n");
393 			break;
394 		case Opt_osdkeepalivetimeout:
395 			opt->osd_keepalive_timeout = intval;
396 			break;
397 		case Opt_osd_idle_ttl:
398 			opt->osd_idle_ttl = intval;
399 			break;
400 		case Opt_mount_timeout:
401 			opt->mount_timeout = intval;
402 			break;
403 
404 		case Opt_share:
405 			opt->flags &= ~CEPH_OPT_NOSHARE;
406 			break;
407 		case Opt_noshare:
408 			opt->flags |= CEPH_OPT_NOSHARE;
409 			break;
410 
411 		case Opt_crc:
412 			opt->flags &= ~CEPH_OPT_NOCRC;
413 			break;
414 		case Opt_nocrc:
415 			opt->flags |= CEPH_OPT_NOCRC;
416 			break;
417 
418 		default:
419 			BUG_ON(token);
420 		}
421 	}
422 
423 	/* success */
424 	return opt;
425 
426 out:
427 	ceph_destroy_options(opt);
428 	return ERR_PTR(err);
429 }
430 EXPORT_SYMBOL(ceph_parse_options);
431 
ceph_client_id(struct ceph_client * client)432 u64 ceph_client_id(struct ceph_client *client)
433 {
434 	return client->monc.auth->global_id;
435 }
436 EXPORT_SYMBOL(ceph_client_id);
437 
438 /*
439  * create a fresh client instance
440  */
ceph_create_client(struct ceph_options * opt,void * private,unsigned supported_features,unsigned required_features)441 struct ceph_client *ceph_create_client(struct ceph_options *opt, void *private,
442 				       unsigned supported_features,
443 				       unsigned required_features)
444 {
445 	struct ceph_client *client;
446 	struct ceph_entity_addr *myaddr = NULL;
447 	int err = -ENOMEM;
448 
449 	client = kzalloc(sizeof(*client), GFP_KERNEL);
450 	if (client == NULL)
451 		return ERR_PTR(-ENOMEM);
452 
453 	client->private = private;
454 	client->options = opt;
455 
456 	mutex_init(&client->mount_mutex);
457 	init_waitqueue_head(&client->auth_wq);
458 	client->auth_err = 0;
459 
460 	client->extra_mon_dispatch = NULL;
461 	client->supported_features = CEPH_FEATURE_SUPPORTED_DEFAULT |
462 		supported_features;
463 	client->required_features = CEPH_FEATURE_REQUIRED_DEFAULT |
464 		required_features;
465 
466 	/* msgr */
467 	if (ceph_test_opt(client, MYIP))
468 		myaddr = &client->options->my_addr;
469 	ceph_messenger_init(&client->msgr, myaddr,
470 		client->supported_features,
471 		client->required_features,
472 		ceph_test_opt(client, NOCRC));
473 
474 	/* subsystems */
475 	err = ceph_monc_init(&client->monc, client);
476 	if (err < 0)
477 		goto fail;
478 	err = ceph_osdc_init(&client->osdc, client);
479 	if (err < 0)
480 		goto fail_monc;
481 
482 	return client;
483 
484 fail_monc:
485 	ceph_monc_stop(&client->monc);
486 fail:
487 	kfree(client);
488 	return ERR_PTR(err);
489 }
490 EXPORT_SYMBOL(ceph_create_client);
491 
ceph_destroy_client(struct ceph_client * client)492 void ceph_destroy_client(struct ceph_client *client)
493 {
494 	dout("destroy_client %p\n", client);
495 
496 	atomic_set(&client->msgr.stopping, 1);
497 
498 	/* unmount */
499 	ceph_osdc_stop(&client->osdc);
500 
501 	ceph_monc_stop(&client->monc);
502 
503 	ceph_debugfs_client_cleanup(client);
504 
505 	ceph_destroy_options(client->options);
506 
507 	kfree(client);
508 	dout("destroy_client %p done\n", client);
509 }
510 EXPORT_SYMBOL(ceph_destroy_client);
511 
512 /*
513  * true if we have the mon map (and have thus joined the cluster)
514  */
have_mon_and_osd_map(struct ceph_client * client)515 static int have_mon_and_osd_map(struct ceph_client *client)
516 {
517 	return client->monc.monmap && client->monc.monmap->epoch &&
518 	       client->osdc.osdmap && client->osdc.osdmap->epoch;
519 }
520 
521 /*
522  * mount: join the ceph cluster, and open root directory.
523  */
__ceph_open_session(struct ceph_client * client,unsigned long started)524 int __ceph_open_session(struct ceph_client *client, unsigned long started)
525 {
526 	int err;
527 	unsigned long timeout = client->options->mount_timeout * HZ;
528 
529 	/* open session, and wait for mon and osd maps */
530 	err = ceph_monc_open_session(&client->monc);
531 	if (err < 0)
532 		return err;
533 
534 	while (!have_mon_and_osd_map(client)) {
535 		err = -EIO;
536 		if (timeout && time_after_eq(jiffies, started + timeout))
537 			return err;
538 
539 		/* wait */
540 		dout("mount waiting for mon_map\n");
541 		err = wait_event_interruptible_timeout(client->auth_wq,
542 			have_mon_and_osd_map(client) || (client->auth_err < 0),
543 			timeout);
544 		if (err == -EINTR || err == -ERESTARTSYS)
545 			return err;
546 		if (client->auth_err < 0)
547 			return client->auth_err;
548 	}
549 
550 	return 0;
551 }
552 EXPORT_SYMBOL(__ceph_open_session);
553 
554 
ceph_open_session(struct ceph_client * client)555 int ceph_open_session(struct ceph_client *client)
556 {
557 	int ret;
558 	unsigned long started = jiffies;  /* note the start time */
559 
560 	dout("open_session start\n");
561 	mutex_lock(&client->mount_mutex);
562 
563 	ret = __ceph_open_session(client, started);
564 
565 	mutex_unlock(&client->mount_mutex);
566 	return ret;
567 }
568 EXPORT_SYMBOL(ceph_open_session);
569 
570 
init_ceph_lib(void)571 static int __init init_ceph_lib(void)
572 {
573 	int ret = 0;
574 
575 	ret = ceph_debugfs_init();
576 	if (ret < 0)
577 		goto out;
578 
579 	ret = ceph_crypto_init();
580 	if (ret < 0)
581 		goto out_debugfs;
582 
583 	ret = ceph_msgr_init();
584 	if (ret < 0)
585 		goto out_crypto;
586 
587 	pr_info("loaded (mon/osd proto %d/%d, osdmap %d/%d %d/%d)\n",
588 		CEPH_MONC_PROTOCOL, CEPH_OSDC_PROTOCOL,
589 		CEPH_OSDMAP_VERSION, CEPH_OSDMAP_VERSION_EXT,
590 		CEPH_OSDMAP_INC_VERSION, CEPH_OSDMAP_INC_VERSION_EXT);
591 
592 	return 0;
593 
594 out_crypto:
595 	ceph_crypto_shutdown();
596 out_debugfs:
597 	ceph_debugfs_cleanup();
598 out:
599 	return ret;
600 }
601 
exit_ceph_lib(void)602 static void __exit exit_ceph_lib(void)
603 {
604 	dout("exit_ceph_lib\n");
605 	ceph_msgr_exit();
606 	ceph_crypto_shutdown();
607 	ceph_debugfs_cleanup();
608 }
609 
610 module_init(init_ceph_lib);
611 module_exit(exit_ceph_lib);
612 
613 MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
614 MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
615 MODULE_AUTHOR("Patience Warnick <patience@newdream.net>");
616 MODULE_DESCRIPTION("Ceph filesystem for Linux");
617 MODULE_LICENSE("GPL");
618