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