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