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