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