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