]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/ceph/ceph_common.c
Merge tag 'nfsd-5.3-1' of git://linux-nfs.org/~bfields/linux
[mirror_ubuntu-jammy-kernel.git] / net / ceph / ceph_common.c
CommitLineData
09c434b8 1// SPDX-License-Identifier: GPL-2.0-only
3d14c5d2
YS
2
3#include <linux/ceph/ceph_debug.h>
4#include <linux/backing-dev.h>
5#include <linux/ctype.h>
6#include <linux/fs.h>
7#include <linux/inet.h>
8#include <linux/in6.h>
e2c3d29b 9#include <linux/key.h>
4b2a58ab 10#include <keys/ceph-type.h>
3d14c5d2
YS
11#include <linux/module.h>
12#include <linux/mount.h>
757856d2 13#include <linux/nsproxy.h>
3d14c5d2
YS
14#include <linux/parser.h>
15#include <linux/sched.h>
16#include <linux/seq_file.h>
17#include <linux/slab.h>
18#include <linux/statfs.h>
19#include <linux/string.h>
eeb0bed5 20#include <linux/vmalloc.h>
3d14c5d2
YS
21
22
1fe60e51 23#include <linux/ceph/ceph_features.h>
3d14c5d2
YS
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>
8323c3aa 29#include "crypto.h"
3d14c5d2
YS
30
31
72fe25e3
AE
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 */
43bool libceph_compatible(void *data)
44{
1e32d34c 45 return true;
72fe25e3
AE
46}
47EXPORT_SYMBOL(libceph_compatible);
3d14c5d2 48
d6a3408a
ID
49static int param_get_supported_features(char *buffer,
50 const struct kernel_param *kp)
51{
52 return sprintf(buffer, "0x%llx", CEPH_FEATURES_SUPPORTED_DEFAULT);
53}
54static const struct kernel_param_ops param_ops_supported_features = {
55 .get = param_get_supported_features,
56};
57module_param_cb(supported_features, &param_ops_supported_features, NULL,
d6444062 58 0444);
d6a3408a 59
3d14c5d2
YS
60const char *ceph_msg_type_name(int type)
61{
62 switch (type) {
63 case CEPH_MSG_SHUTDOWN: return "shutdown";
64 case CEPH_MSG_PING: return "ping";
65 case CEPH_MSG_AUTH: return "auth";
66 case CEPH_MSG_AUTH_REPLY: return "auth_reply";
67 case CEPH_MSG_MON_MAP: return "mon_map";
68 case CEPH_MSG_MON_GET_MAP: return "mon_get_map";
69 case CEPH_MSG_MON_SUBSCRIBE: return "mon_subscribe";
70 case CEPH_MSG_MON_SUBSCRIBE_ACK: return "mon_subscribe_ack";
71 case CEPH_MSG_STATFS: return "statfs";
72 case CEPH_MSG_STATFS_REPLY: return "statfs_reply";
513a8243
ID
73 case CEPH_MSG_MON_GET_VERSION: return "mon_get_version";
74 case CEPH_MSG_MON_GET_VERSION_REPLY: return "mon_get_version_reply";
3d14c5d2 75 case CEPH_MSG_MDS_MAP: return "mds_map";
f2f87877 76 case CEPH_MSG_FS_MAP_USER: return "fs_map_user";
3d14c5d2
YS
77 case CEPH_MSG_CLIENT_SESSION: return "client_session";
78 case CEPH_MSG_CLIENT_RECONNECT: return "client_reconnect";
79 case CEPH_MSG_CLIENT_REQUEST: return "client_request";
80 case CEPH_MSG_CLIENT_REQUEST_FORWARD: return "client_request_forward";
81 case CEPH_MSG_CLIENT_REPLY: return "client_reply";
82 case CEPH_MSG_CLIENT_CAPS: return "client_caps";
83 case CEPH_MSG_CLIENT_CAPRELEASE: return "client_cap_release";
fb18a575 84 case CEPH_MSG_CLIENT_QUOTA: return "client_quota";
3d14c5d2
YS
85 case CEPH_MSG_CLIENT_SNAP: return "client_snap";
86 case CEPH_MSG_CLIENT_LEASE: return "client_lease";
f2f87877
CX
87 case CEPH_MSG_POOLOP_REPLY: return "poolop_reply";
88 case CEPH_MSG_POOLOP: return "poolop";
89 case CEPH_MSG_MON_COMMAND: return "mon_command";
90 case CEPH_MSG_MON_COMMAND_ACK: return "mon_command_ack";
3d14c5d2
YS
91 case CEPH_MSG_OSD_MAP: return "osd_map";
92 case CEPH_MSG_OSD_OP: return "osd_op";
93 case CEPH_MSG_OSD_OPREPLY: return "osd_opreply";
a40c4f10 94 case CEPH_MSG_WATCH_NOTIFY: return "watch_notify";
a02a946d 95 case CEPH_MSG_OSD_BACKOFF: return "osd_backoff";
3d14c5d2
YS
96 default: return "unknown";
97 }
98}
99EXPORT_SYMBOL(ceph_msg_type_name);
100
101/*
102 * Initially learn our fsid, or verify an fsid matches.
103 */
104int ceph_check_fsid(struct ceph_client *client, struct ceph_fsid *fsid)
105{
106 if (client->have_fsid) {
107 if (ceph_fsid_compare(&client->fsid, fsid)) {
108 pr_err("bad fsid, had %pU got %pU",
109 &client->fsid, fsid);
110 return -1;
111 }
112 } else {
3d14c5d2 113 memcpy(&client->fsid, fsid, sizeof(*fsid));
3d14c5d2
YS
114 }
115 return 0;
116}
117EXPORT_SYMBOL(ceph_check_fsid);
118
119static int strcmp_null(const char *s1, const char *s2)
120{
121 if (!s1 && !s2)
122 return 0;
123 if (s1 && !s2)
124 return -1;
125 if (!s1 && s2)
126 return 1;
127 return strcmp(s1, s2);
128}
129
130int ceph_compare_options(struct ceph_options *new_opt,
131 struct ceph_client *client)
132{
133 struct ceph_options *opt1 = new_opt;
134 struct ceph_options *opt2 = client->options;
135 int ofs = offsetof(struct ceph_options, mon_addr);
136 int i;
137 int ret;
138
757856d2
ID
139 /*
140 * Don't bother comparing options if network namespaces don't
141 * match.
142 */
143 if (!net_eq(current->nsproxy->net_ns, read_pnet(&client->msgr.net)))
144 return -1;
145
3d14c5d2
YS
146 ret = memcmp(opt1, opt2, ofs);
147 if (ret)
148 return ret;
149
150 ret = strcmp_null(opt1->name, opt2->name);
151 if (ret)
152 return ret;
153
8323c3aa
TV
154 if (opt1->key && !opt2->key)
155 return -1;
156 if (!opt1->key && opt2->key)
157 return 1;
158 if (opt1->key && opt2->key) {
159 if (opt1->key->type != opt2->key->type)
160 return -1;
161 if (opt1->key->created.tv_sec != opt2->key->created.tv_sec)
162 return -1;
163 if (opt1->key->created.tv_nsec != opt2->key->created.tv_nsec)
164 return -1;
165 if (opt1->key->len != opt2->key->len)
166 return -1;
167 if (opt1->key->key && !opt2->key->key)
168 return -1;
169 if (!opt1->key->key && opt2->key->key)
170 return 1;
171 if (opt1->key->key && opt2->key->key) {
172 ret = memcmp(opt1->key->key, opt2->key->key, opt1->key->len);
173 if (ret)
174 return ret;
175 }
176 }
3d14c5d2
YS
177
178 /* any matching mon ip implies a match */
179 for (i = 0; i < opt1->num_mon; i++) {
180 if (ceph_monmap_contains(client->monc.monmap,
181 &opt1->mon_addr[i]))
182 return 0;
183 }
184 return -1;
185}
186EXPORT_SYMBOL(ceph_compare_options);
187
eeb0bed5
ID
188void *ceph_kvmalloc(size_t size, gfp_t flags)
189{
190 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
191 void *ptr = kmalloc(size, flags | __GFP_NOWARN);
192 if (ptr)
193 return ptr;
194 }
195
19809c2d 196 return __vmalloc(size, flags, PAGE_KERNEL);
eeb0bed5
ID
197}
198
3d14c5d2
YS
199
200static int parse_fsid(const char *str, struct ceph_fsid *fsid)
201{
202 int i = 0;
203 char tmp[3];
204 int err = -EINVAL;
205 int d;
206
207 dout("parse_fsid '%s'\n", str);
208 tmp[2] = 0;
209 while (*str && i < 16) {
210 if (ispunct(*str)) {
211 str++;
212 continue;
213 }
214 if (!isxdigit(str[0]) || !isxdigit(str[1]))
215 break;
216 tmp[0] = str[0];
217 tmp[1] = str[1];
218 if (sscanf(tmp, "%x", &d) < 1)
219 break;
220 fsid->fsid[i] = d & 0xff;
221 i++;
222 str += 2;
223 }
224
225 if (i == 16)
226 err = 0;
4c069a58 227 dout("parse_fsid ret %d got fsid %pU\n", err, fsid);
3d14c5d2
YS
228 return err;
229}
230
231/*
232 * ceph options
233 */
234enum {
235 Opt_osdtimeout,
236 Opt_osdkeepalivetimeout,
237 Opt_mount_timeout,
238 Opt_osd_idle_ttl,
7cc5e38f 239 Opt_osd_request_timeout,
3d14c5d2
YS
240 Opt_last_int,
241 /* int args above */
242 Opt_fsid,
243 Opt_name,
244 Opt_secret,
e2c3d29b 245 Opt_key,
3d14c5d2
YS
246 Opt_ip,
247 Opt_last_string,
248 /* string args above */
cffaba15 249 Opt_share,
3d14c5d2 250 Opt_noshare,
cffaba15 251 Opt_crc,
3d14c5d2 252 Opt_nocrc,
a3fc9800
YZ
253 Opt_cephx_require_signatures,
254 Opt_nocephx_require_signatures,
a51983e4
ID
255 Opt_cephx_sign_messages,
256 Opt_nocephx_sign_messages,
ba988f87
CH
257 Opt_tcp_nodelay,
258 Opt_notcp_nodelay,
02b2f549 259 Opt_abort_on_full,
3d14c5d2
YS
260};
261
262static match_table_t opt_tokens = {
263 {Opt_osdtimeout, "osdtimeout=%d"},
264 {Opt_osdkeepalivetimeout, "osdkeepalive=%d"},
265 {Opt_mount_timeout, "mount_timeout=%d"},
266 {Opt_osd_idle_ttl, "osd_idle_ttl=%d"},
7cc5e38f 267 {Opt_osd_request_timeout, "osd_request_timeout=%d"},
3d14c5d2
YS
268 /* int args above */
269 {Opt_fsid, "fsid=%s"},
270 {Opt_name, "name=%s"},
271 {Opt_secret, "secret=%s"},
e2c3d29b 272 {Opt_key, "key=%s"},
3d14c5d2
YS
273 {Opt_ip, "ip=%s"},
274 /* string args above */
cffaba15 275 {Opt_share, "share"},
3d14c5d2 276 {Opt_noshare, "noshare"},
cffaba15 277 {Opt_crc, "crc"},
3d14c5d2 278 {Opt_nocrc, "nocrc"},
a3fc9800
YZ
279 {Opt_cephx_require_signatures, "cephx_require_signatures"},
280 {Opt_nocephx_require_signatures, "nocephx_require_signatures"},
a51983e4
ID
281 {Opt_cephx_sign_messages, "cephx_sign_messages"},
282 {Opt_nocephx_sign_messages, "nocephx_sign_messages"},
ba988f87
CH
283 {Opt_tcp_nodelay, "tcp_nodelay"},
284 {Opt_notcp_nodelay, "notcp_nodelay"},
02b2f549 285 {Opt_abort_on_full, "abort_on_full"},
3d14c5d2
YS
286 {-1, NULL}
287};
288
289void ceph_destroy_options(struct ceph_options *opt)
290{
291 dout("destroy_options %p\n", opt);
292 kfree(opt->name);
8323c3aa
TV
293 if (opt->key) {
294 ceph_crypto_key_destroy(opt->key);
295 kfree(opt->key);
296 }
1cad7893 297 kfree(opt->mon_addr);
3d14c5d2
YS
298 kfree(opt);
299}
300EXPORT_SYMBOL(ceph_destroy_options);
301
e2c3d29b
TV
302/* get secret from key store */
303static int get_secret(struct ceph_crypto_key *dst, const char *name) {
304 struct key *ukey;
305 int key_err;
306 int err = 0;
4b2a58ab 307 struct ceph_crypto_key *ckey;
e2c3d29b 308
4b2a58ab 309 ukey = request_key(&key_type_ceph, name, NULL);
bad87216 310 if (IS_ERR(ukey)) {
e2c3d29b
TV
311 /* request_key errors don't map nicely to mount(2)
312 errors; don't even try, but still printk */
313 key_err = PTR_ERR(ukey);
314 switch (key_err) {
315 case -ENOKEY:
b9a67899
JP
316 pr_warn("ceph: Mount failed due to key not found: %s\n",
317 name);
e2c3d29b
TV
318 break;
319 case -EKEYEXPIRED:
b9a67899
JP
320 pr_warn("ceph: Mount failed due to expired key: %s\n",
321 name);
e2c3d29b
TV
322 break;
323 case -EKEYREVOKED:
b9a67899
JP
324 pr_warn("ceph: Mount failed due to revoked key: %s\n",
325 name);
e2c3d29b
TV
326 break;
327 default:
b9a67899
JP
328 pr_warn("ceph: Mount failed due to unknown key error %d: %s\n",
329 key_err, name);
e2c3d29b
TV
330 }
331 err = -EPERM;
332 goto out;
333 }
334
146aa8b1 335 ckey = ukey->payload.data[0];
4b2a58ab 336 err = ceph_crypto_key_clone(dst, ckey);
e2c3d29b
TV
337 if (err)
338 goto out_key;
339 /* pass through, err is 0 */
340
341out_key:
342 key_put(ukey);
343out:
344 return err;
345}
346
ee57741c
AE
347struct ceph_options *
348ceph_parse_options(char *options, const char *dev_name,
349 const char *dev_name_end,
350 int (*parse_extra_token)(char *c, void *private),
351 void *private)
3d14c5d2
YS
352{
353 struct ceph_options *opt;
354 const char *c;
355 int err = -ENOMEM;
356 substring_t argstr[MAX_OPT_ARGS];
357
358 opt = kzalloc(sizeof(*opt), GFP_KERNEL);
359 if (!opt)
ee57741c 360 return ERR_PTR(-ENOMEM);
3d14c5d2
YS
361 opt->mon_addr = kcalloc(CEPH_MAX_MON, sizeof(*opt->mon_addr),
362 GFP_KERNEL);
363 if (!opt->mon_addr)
364 goto out;
365
366 dout("parse_options %p options '%s' dev_name '%s'\n", opt, options,
367 dev_name);
368
369 /* start with defaults */
370 opt->flags = CEPH_OPT_DEFAULT;
3d14c5d2 371 opt->osd_keepalive_timeout = CEPH_OSD_KEEPALIVE_DEFAULT;
a319bf56
ID
372 opt->mount_timeout = CEPH_MOUNT_TIMEOUT_DEFAULT;
373 opt->osd_idle_ttl = CEPH_OSD_IDLE_TTL_DEFAULT;
7cc5e38f 374 opt->osd_request_timeout = CEPH_OSD_REQUEST_TIMEOUT_DEFAULT;
3d14c5d2
YS
375
376 /* get mon ip(s) */
377 /* ip1[:port1][,ip2[:port2]...] */
378 err = ceph_parse_ips(dev_name, dev_name_end, opt->mon_addr,
379 CEPH_MAX_MON, &opt->num_mon);
380 if (err < 0)
381 goto out;
382
383 /* parse mount options */
384 while ((c = strsep(&options, ",")) != NULL) {
17173c82 385 int token, intval;
3d14c5d2
YS
386 if (!*c)
387 continue;
388 err = -EINVAL;
389 token = match_token((char *)c, opt_tokens, argstr);
010e3b48 390 if (token < 0 && parse_extra_token) {
3d14c5d2
YS
391 /* extra? */
392 err = parse_extra_token((char *)c, private);
393 if (err < 0) {
394 pr_err("bad option at '%s'\n", c);
395 goto out;
396 }
397 continue;
398 }
399 if (token < Opt_last_int) {
17173c82
CX
400 err = match_int(&argstr[0], &intval);
401 if (err < 0) {
2f56b6ba 402 pr_err("bad option arg (not int) at '%s'\n", c);
17173c82 403 goto out;
3d14c5d2
YS
404 }
405 dout("got int token %d val %d\n", token, intval);
406 } else if (token > Opt_last_int && token < Opt_last_string) {
407 dout("got string token %d val %s\n", token,
408 argstr[0].from);
409 } else {
410 dout("got token %d\n", token);
411 }
412 switch (token) {
413 case Opt_ip:
414 err = ceph_parse_ips(argstr[0].from,
415 argstr[0].to,
416 &opt->my_addr,
417 1, NULL);
418 if (err < 0)
419 goto out;
420 opt->flags |= CEPH_OPT_MYIP;
421 break;
422
423 case Opt_fsid:
424 err = parse_fsid(argstr[0].from, &opt->fsid);
425 if (err == 0)
426 opt->flags |= CEPH_OPT_FSID;
427 break;
428 case Opt_name:
937441f3 429 kfree(opt->name);
3d14c5d2
YS
430 opt->name = kstrndup(argstr[0].from,
431 argstr[0].to-argstr[0].from,
432 GFP_KERNEL);
affff077
CX
433 if (!opt->name) {
434 err = -ENOMEM;
435 goto out;
436 }
3d14c5d2
YS
437 break;
438 case Opt_secret:
937441f3
CX
439 ceph_crypto_key_destroy(opt->key);
440 kfree(opt->key);
441
8323c3aa
TV
442 opt->key = kzalloc(sizeof(*opt->key), GFP_KERNEL);
443 if (!opt->key) {
444 err = -ENOMEM;
445 goto out;
446 }
447 err = ceph_crypto_key_unarmor(opt->key, argstr[0].from);
448 if (err < 0)
449 goto out;
3d14c5d2 450 break;
e2c3d29b 451 case Opt_key:
937441f3
CX
452 ceph_crypto_key_destroy(opt->key);
453 kfree(opt->key);
454
e2c3d29b
TV
455 opt->key = kzalloc(sizeof(*opt->key), GFP_KERNEL);
456 if (!opt->key) {
457 err = -ENOMEM;
458 goto out;
459 }
460 err = get_secret(opt->key, argstr[0].from);
461 if (err < 0)
462 goto out;
463 break;
3d14c5d2
YS
464
465 /* misc */
466 case Opt_osdtimeout:
b9a67899 467 pr_warn("ignoring deprecated osdtimeout option\n");
3d14c5d2
YS
468 break;
469 case Opt_osdkeepalivetimeout:
a319bf56
ID
470 /* 0 isn't well defined right now, reject it */
471 if (intval < 1 || intval > INT_MAX / 1000) {
472 pr_err("osdkeepalive out of range\n");
473 err = -EINVAL;
474 goto out;
475 }
476 opt->osd_keepalive_timeout =
477 msecs_to_jiffies(intval * 1000);
3d14c5d2
YS
478 break;
479 case Opt_osd_idle_ttl:
a319bf56
ID
480 /* 0 isn't well defined right now, reject it */
481 if (intval < 1 || intval > INT_MAX / 1000) {
482 pr_err("osd_idle_ttl out of range\n");
483 err = -EINVAL;
484 goto out;
485 }
486 opt->osd_idle_ttl = msecs_to_jiffies(intval * 1000);
3d14c5d2
YS
487 break;
488 case Opt_mount_timeout:
a319bf56
ID
489 /* 0 is "wait forever" (i.e. infinite timeout) */
490 if (intval < 0 || intval > INT_MAX / 1000) {
491 pr_err("mount_timeout out of range\n");
492 err = -EINVAL;
493 goto out;
494 }
495 opt->mount_timeout = msecs_to_jiffies(intval * 1000);
3d14c5d2 496 break;
7cc5e38f
ID
497 case Opt_osd_request_timeout:
498 /* 0 is "wait forever" (i.e. infinite timeout) */
499 if (intval < 0 || intval > INT_MAX / 1000) {
500 pr_err("osd_request_timeout out of range\n");
501 err = -EINVAL;
502 goto out;
503 }
504 opt->osd_request_timeout = msecs_to_jiffies(intval * 1000);
505 break;
3d14c5d2 506
cffaba15
AE
507 case Opt_share:
508 opt->flags &= ~CEPH_OPT_NOSHARE;
509 break;
3d14c5d2
YS
510 case Opt_noshare:
511 opt->flags |= CEPH_OPT_NOSHARE;
512 break;
513
cffaba15
AE
514 case Opt_crc:
515 opt->flags &= ~CEPH_OPT_NOCRC;
516 break;
3d14c5d2
YS
517 case Opt_nocrc:
518 opt->flags |= CEPH_OPT_NOCRC;
519 break;
ba988f87 520
a3fc9800
YZ
521 case Opt_cephx_require_signatures:
522 opt->flags &= ~CEPH_OPT_NOMSGAUTH;
523 break;
524 case Opt_nocephx_require_signatures:
525 opt->flags |= CEPH_OPT_NOMSGAUTH;
526 break;
a51983e4
ID
527 case Opt_cephx_sign_messages:
528 opt->flags &= ~CEPH_OPT_NOMSGSIGN;
529 break;
530 case Opt_nocephx_sign_messages:
531 opt->flags |= CEPH_OPT_NOMSGSIGN;
532 break;
3d14c5d2 533
ba988f87
CH
534 case Opt_tcp_nodelay:
535 opt->flags |= CEPH_OPT_TCP_NODELAY;
536 break;
537 case Opt_notcp_nodelay:
538 opt->flags &= ~CEPH_OPT_TCP_NODELAY;
539 break;
540
02b2f549
DY
541 case Opt_abort_on_full:
542 opt->flags |= CEPH_OPT_ABORT_ON_FULL;
543 break;
544
3d14c5d2
YS
545 default:
546 BUG_ON(token);
547 }
548 }
549
550 /* success */
ee57741c 551 return opt;
3d14c5d2
YS
552
553out:
554 ceph_destroy_options(opt);
ee57741c 555 return ERR_PTR(err);
3d14c5d2
YS
556}
557EXPORT_SYMBOL(ceph_parse_options);
558
02b2f549
DY
559int ceph_print_client_options(struct seq_file *m, struct ceph_client *client,
560 bool show_all)
ff40f9ae
ID
561{
562 struct ceph_options *opt = client->options;
563 size_t pos = m->count;
564
a068acf2
KC
565 if (opt->name) {
566 seq_puts(m, "name=");
567 seq_escape(m, opt->name, ", \t\n\\");
568 seq_putc(m, ',');
569 }
ff40f9ae
ID
570 if (opt->key)
571 seq_puts(m, "secret=<hidden>,");
572
573 if (opt->flags & CEPH_OPT_FSID)
574 seq_printf(m, "fsid=%pU,", &opt->fsid);
575 if (opt->flags & CEPH_OPT_NOSHARE)
576 seq_puts(m, "noshare,");
577 if (opt->flags & CEPH_OPT_NOCRC)
578 seq_puts(m, "nocrc,");
579 if (opt->flags & CEPH_OPT_NOMSGAUTH)
580 seq_puts(m, "nocephx_require_signatures,");
a51983e4
ID
581 if (opt->flags & CEPH_OPT_NOMSGSIGN)
582 seq_puts(m, "nocephx_sign_messages,");
ff40f9ae
ID
583 if ((opt->flags & CEPH_OPT_TCP_NODELAY) == 0)
584 seq_puts(m, "notcp_nodelay,");
02b2f549
DY
585 if (show_all && (opt->flags & CEPH_OPT_ABORT_ON_FULL))
586 seq_puts(m, "abort_on_full,");
ff40f9ae
ID
587
588 if (opt->mount_timeout != CEPH_MOUNT_TIMEOUT_DEFAULT)
a319bf56
ID
589 seq_printf(m, "mount_timeout=%d,",
590 jiffies_to_msecs(opt->mount_timeout) / 1000);
ff40f9ae 591 if (opt->osd_idle_ttl != CEPH_OSD_IDLE_TTL_DEFAULT)
a319bf56
ID
592 seq_printf(m, "osd_idle_ttl=%d,",
593 jiffies_to_msecs(opt->osd_idle_ttl) / 1000);
ff40f9ae
ID
594 if (opt->osd_keepalive_timeout != CEPH_OSD_KEEPALIVE_DEFAULT)
595 seq_printf(m, "osdkeepalivetimeout=%d,",
a319bf56 596 jiffies_to_msecs(opt->osd_keepalive_timeout) / 1000);
7cc5e38f
ID
597 if (opt->osd_request_timeout != CEPH_OSD_REQUEST_TIMEOUT_DEFAULT)
598 seq_printf(m, "osd_request_timeout=%d,",
599 jiffies_to_msecs(opt->osd_request_timeout) / 1000);
ff40f9ae
ID
600
601 /* drop redundant comma */
602 if (m->count != pos)
603 m->count--;
604
605 return 0;
606}
607EXPORT_SYMBOL(ceph_print_client_options);
608
005a07bf
ID
609struct ceph_entity_addr *ceph_client_addr(struct ceph_client *client)
610{
611 return &client->msgr.inst.addr;
612}
613EXPORT_SYMBOL(ceph_client_addr);
614
033268a5 615u64 ceph_client_gid(struct ceph_client *client)
3d14c5d2
YS
616{
617 return client->monc.auth->global_id;
618}
033268a5 619EXPORT_SYMBOL(ceph_client_gid);
3d14c5d2
YS
620
621/*
622 * create a fresh client instance
623 */
74da4a0f 624struct ceph_client *ceph_create_client(struct ceph_options *opt, void *private)
3d14c5d2
YS
625{
626 struct ceph_client *client;
6ab00d46 627 struct ceph_entity_addr *myaddr = NULL;
ae5b806a
JD
628 int err;
629
630 err = wait_for_random_bytes();
631 if (err < 0)
632 return ERR_PTR(err);
3d14c5d2
YS
633
634 client = kzalloc(sizeof(*client), GFP_KERNEL);
635 if (client == NULL)
636 return ERR_PTR(-ENOMEM);
637
638 client->private = private;
639 client->options = opt;
640
641 mutex_init(&client->mount_mutex);
642 init_waitqueue_head(&client->auth_wq);
643 client->auth_err = 0;
644
645 client->extra_mon_dispatch = NULL;
74da4a0f
ID
646 client->supported_features = CEPH_FEATURES_SUPPORTED_DEFAULT;
647 client->required_features = CEPH_FEATURES_REQUIRED_DEFAULT;
648
649 if (!ceph_test_opt(client, NOMSGAUTH))
650 client->required_features |= CEPH_FEATURE_MSG_AUTH;
6ab00d46
SW
651
652 /* msgr */
653 if (ceph_test_opt(client, MYIP))
654 myaddr = &client->options->my_addr;
ba988f87 655
859bff51 656 ceph_messenger_init(&client->msgr, myaddr);
3d14c5d2
YS
657
658 /* subsystems */
659 err = ceph_monc_init(&client->monc, client);
660 if (err < 0)
15d9882c 661 goto fail;
3d14c5d2
YS
662 err = ceph_osdc_init(&client->osdc, client);
663 if (err < 0)
664 goto fail_monc;
665
666 return client;
667
668fail_monc:
669 ceph_monc_stop(&client->monc);
670fail:
757856d2 671 ceph_messenger_fini(&client->msgr);
3d14c5d2
YS
672 kfree(client);
673 return ERR_PTR(err);
674}
675EXPORT_SYMBOL(ceph_create_client);
676
677void ceph_destroy_client(struct ceph_client *client)
678{
679 dout("destroy_client %p\n", client);
680
a2a32584
GH
681 atomic_set(&client->msgr.stopping, 1);
682
3d14c5d2
YS
683 /* unmount */
684 ceph_osdc_stop(&client->osdc);
3d14c5d2 685 ceph_monc_stop(&client->monc);
757856d2 686 ceph_messenger_fini(&client->msgr);
3d14c5d2
YS
687
688 ceph_debugfs_client_cleanup(client);
689
3d14c5d2
YS
690 ceph_destroy_options(client->options);
691
692 kfree(client);
693 dout("destroy_client %p done\n", client);
694}
695EXPORT_SYMBOL(ceph_destroy_client);
696
697/*
698 * true if we have the mon map (and have thus joined the cluster)
699 */
3b33f692 700static bool have_mon_and_osd_map(struct ceph_client *client)
3d14c5d2
YS
701{
702 return client->monc.monmap && client->monc.monmap->epoch &&
703 client->osdc.osdmap && client->osdc.osdmap->epoch;
704}
705
706/*
707 * mount: join the ceph cluster, and open root directory.
708 */
709int __ceph_open_session(struct ceph_client *client, unsigned long started)
710{
a319bf56 711 unsigned long timeout = client->options->mount_timeout;
216639dd 712 long err;
3d14c5d2 713
3d14c5d2
YS
714 /* open session, and wait for mon and osd maps */
715 err = ceph_monc_open_session(&client->monc);
716 if (err < 0)
717 return err;
718
719 while (!have_mon_and_osd_map(client)) {
3d14c5d2 720 if (timeout && time_after_eq(jiffies, started + timeout))
216639dd 721 return -ETIMEDOUT;
3d14c5d2
YS
722
723 /* wait */
724 dout("mount waiting for mon_map\n");
725 err = wait_event_interruptible_timeout(client->auth_wq,
726 have_mon_and_osd_map(client) || (client->auth_err < 0),
a319bf56 727 ceph_timeout_jiffies(timeout));
216639dd 728 if (err < 0)
3d14c5d2
YS
729 return err;
730 if (client->auth_err < 0)
731 return client->auth_err;
732 }
733
033268a5
ID
734 pr_info("client%llu fsid %pU\n", ceph_client_gid(client),
735 &client->fsid);
02ac956c
ID
736 ceph_debugfs_client_init(client);
737
3d14c5d2
YS
738 return 0;
739}
740EXPORT_SYMBOL(__ceph_open_session);
741
3d14c5d2
YS
742int ceph_open_session(struct ceph_client *client)
743{
744 int ret;
745 unsigned long started = jiffies; /* note the start time */
746
747 dout("open_session start\n");
748 mutex_lock(&client->mount_mutex);
749
750 ret = __ceph_open_session(client, started);
751
752 mutex_unlock(&client->mount_mutex);
753 return ret;
754}
755EXPORT_SYMBOL(ceph_open_session);
756
bb229bbb
ID
757int ceph_wait_for_latest_osdmap(struct ceph_client *client,
758 unsigned long timeout)
759{
760 u64 newest_epoch;
761 int ret;
762
763 ret = ceph_monc_get_version(&client->monc, "osdmap", &newest_epoch);
764 if (ret)
765 return ret;
766
767 if (client->osdc.osdmap->epoch >= newest_epoch)
768 return 0;
769
770 ceph_osdc_maybe_request_map(&client->osdc);
771 return ceph_monc_wait_osdmap(&client->monc, newest_epoch, timeout);
772}
773EXPORT_SYMBOL(ceph_wait_for_latest_osdmap);
3d14c5d2
YS
774
775static int __init init_ceph_lib(void)
776{
777 int ret = 0;
778
1a829ff2 779 ceph_debugfs_init();
3d14c5d2 780
4b2a58ab 781 ret = ceph_crypto_init();
3d14c5d2
YS
782 if (ret < 0)
783 goto out_debugfs;
784
4b2a58ab
TV
785 ret = ceph_msgr_init();
786 if (ret < 0)
787 goto out_crypto;
788
5522ae0b
AE
789 ret = ceph_osdc_setup();
790 if (ret < 0)
791 goto out_msgr;
792
4f6a7e5e
SW
793 pr_info("loaded (mon/osd proto %d/%d)\n",
794 CEPH_MONC_PROTOCOL, CEPH_OSDC_PROTOCOL);
3d14c5d2
YS
795
796 return 0;
797
5522ae0b
AE
798out_msgr:
799 ceph_msgr_exit();
4b2a58ab
TV
800out_crypto:
801 ceph_crypto_shutdown();
3d14c5d2
YS
802out_debugfs:
803 ceph_debugfs_cleanup();
3d14c5d2
YS
804 return ret;
805}
806
807static void __exit exit_ceph_lib(void)
808{
809 dout("exit_ceph_lib\n");
51e92737
YZ
810 WARN_ON(!ceph_strings_empty());
811
5522ae0b 812 ceph_osdc_cleanup();
3d14c5d2 813 ceph_msgr_exit();
4b2a58ab 814 ceph_crypto_shutdown();
3d14c5d2
YS
815 ceph_debugfs_cleanup();
816}
817
818module_init(init_ceph_lib);
819module_exit(exit_ceph_lib);
820
821MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
822MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
823MODULE_AUTHOR("Patience Warnick <patience@newdream.net>");
6c13a6bb 824MODULE_DESCRIPTION("Ceph core library");
3d14c5d2 825MODULE_LICENSE("GPL");