]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/ceph/mds_client.c
libceph: fix oops in ceph_msg_data_{pages,pagelist}_advance()
[mirror_ubuntu-bionic-kernel.git] / fs / ceph / mds_client.c
CommitLineData
3d14c5d2 1#include <linux/ceph/ceph_debug.h>
2f2dc053 2
496e5955 3#include <linux/fs.h>
2f2dc053 4#include <linux/wait.h>
5a0e3ad6 5#include <linux/slab.h>
2f2dc053 6#include <linux/sched.h>
3d14c5d2
YS
7#include <linux/debugfs.h>
8#include <linux/seq_file.h>
2f2dc053 9
2f2dc053 10#include "super.h"
3d14c5d2
YS
11#include "mds_client.h"
12
1fe60e51 13#include <linux/ceph/ceph_features.h>
3d14c5d2
YS
14#include <linux/ceph/messenger.h>
15#include <linux/ceph/decode.h>
16#include <linux/ceph/pagelist.h>
17#include <linux/ceph/auth.h>
18#include <linux/ceph/debugfs.h>
2f2dc053
SW
19
20/*
21 * A cluster of MDS (metadata server) daemons is responsible for
22 * managing the file system namespace (the directory hierarchy and
23 * inodes) and for coordinating shared access to storage. Metadata is
24 * partitioning hierarchically across a number of servers, and that
25 * partition varies over time as the cluster adjusts the distribution
26 * in order to balance load.
27 *
28 * The MDS client is primarily responsible to managing synchronous
29 * metadata requests for operations like open, unlink, and so forth.
30 * If there is a MDS failure, we find out about it when we (possibly
31 * request and) receive a new MDS map, and can resubmit affected
32 * requests.
33 *
34 * For the most part, though, we take advantage of a lossless
35 * communications channel to the MDS, and do not need to worry about
36 * timing out or resubmitting requests.
37 *
38 * We maintain a stateful "session" with each MDS we interact with.
39 * Within each session, we sent periodic heartbeat messages to ensure
40 * any capabilities or leases we have been issues remain valid. If
41 * the session times out and goes stale, our leases and capabilities
42 * are no longer valid.
43 */
44
20cb34ae 45struct ceph_reconnect_state {
44c99757 46 int nr_caps;
20cb34ae
SW
47 struct ceph_pagelist *pagelist;
48 bool flock;
49};
50
2f2dc053
SW
51static void __wake_requests(struct ceph_mds_client *mdsc,
52 struct list_head *head);
53
9e32789f 54static const struct ceph_connection_operations mds_con_ops;
2f2dc053
SW
55
56
57/*
58 * mds reply parsing
59 */
60
61/*
62 * parse individual inode info
63 */
64static int parse_reply_info_in(void **p, void *end,
14303d20 65 struct ceph_mds_reply_info_in *info,
12b4629a 66 u64 features)
2f2dc053
SW
67{
68 int err = -EIO;
69
70 info->in = *p;
71 *p += sizeof(struct ceph_mds_reply_inode) +
72 sizeof(*info->in->fragtree.splits) *
73 le32_to_cpu(info->in->fragtree.nsplits);
74
75 ceph_decode_32_safe(p, end, info->symlink_len, bad);
76 ceph_decode_need(p, end, info->symlink_len, bad);
77 info->symlink = *p;
78 *p += info->symlink_len;
79
14303d20
SW
80 if (features & CEPH_FEATURE_DIRLAYOUTHASH)
81 ceph_decode_copy_safe(p, end, &info->dir_layout,
82 sizeof(info->dir_layout), bad);
83 else
84 memset(&info->dir_layout, 0, sizeof(info->dir_layout));
85
2f2dc053
SW
86 ceph_decode_32_safe(p, end, info->xattr_len, bad);
87 ceph_decode_need(p, end, info->xattr_len, bad);
88 info->xattr_data = *p;
89 *p += info->xattr_len;
90 return 0;
91bad:
92 return err;
93}
94
95/*
96 * parse a normal reply, which may contain a (dir+)dentry and/or a
97 * target inode.
98 */
99static int parse_reply_info_trace(void **p, void *end,
14303d20 100 struct ceph_mds_reply_info_parsed *info,
12b4629a 101 u64 features)
2f2dc053
SW
102{
103 int err;
104
105 if (info->head->is_dentry) {
14303d20 106 err = parse_reply_info_in(p, end, &info->diri, features);
2f2dc053
SW
107 if (err < 0)
108 goto out_bad;
109
110 if (unlikely(*p + sizeof(*info->dirfrag) > end))
111 goto bad;
112 info->dirfrag = *p;
113 *p += sizeof(*info->dirfrag) +
114 sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
115 if (unlikely(*p > end))
116 goto bad;
117
118 ceph_decode_32_safe(p, end, info->dname_len, bad);
119 ceph_decode_need(p, end, info->dname_len, bad);
120 info->dname = *p;
121 *p += info->dname_len;
122 info->dlease = *p;
123 *p += sizeof(*info->dlease);
124 }
125
126 if (info->head->is_target) {
14303d20 127 err = parse_reply_info_in(p, end, &info->targeti, features);
2f2dc053
SW
128 if (err < 0)
129 goto out_bad;
130 }
131
132 if (unlikely(*p != end))
133 goto bad;
134 return 0;
135
136bad:
137 err = -EIO;
138out_bad:
139 pr_err("problem parsing mds trace %d\n", err);
140 return err;
141}
142
143/*
144 * parse readdir results
145 */
146static int parse_reply_info_dir(void **p, void *end,
14303d20 147 struct ceph_mds_reply_info_parsed *info,
12b4629a 148 u64 features)
2f2dc053
SW
149{
150 u32 num, i = 0;
151 int err;
152
153 info->dir_dir = *p;
154 if (*p + sizeof(*info->dir_dir) > end)
155 goto bad;
156 *p += sizeof(*info->dir_dir) +
157 sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
158 if (*p > end)
159 goto bad;
160
161 ceph_decode_need(p, end, sizeof(num) + 2, bad);
c89136ea
SW
162 num = ceph_decode_32(p);
163 info->dir_end = ceph_decode_8(p);
164 info->dir_complete = ceph_decode_8(p);
2f2dc053
SW
165 if (num == 0)
166 goto done;
167
168 /* alloc large array */
169 info->dir_nr = num;
170 info->dir_in = kcalloc(num, sizeof(*info->dir_in) +
171 sizeof(*info->dir_dname) +
172 sizeof(*info->dir_dname_len) +
173 sizeof(*info->dir_dlease),
174 GFP_NOFS);
175 if (info->dir_in == NULL) {
176 err = -ENOMEM;
177 goto out_bad;
178 }
179 info->dir_dname = (void *)(info->dir_in + num);
180 info->dir_dname_len = (void *)(info->dir_dname + num);
181 info->dir_dlease = (void *)(info->dir_dname_len + num);
182
183 while (num) {
184 /* dentry */
185 ceph_decode_need(p, end, sizeof(u32)*2, bad);
c89136ea 186 info->dir_dname_len[i] = ceph_decode_32(p);
2f2dc053
SW
187 ceph_decode_need(p, end, info->dir_dname_len[i], bad);
188 info->dir_dname[i] = *p;
189 *p += info->dir_dname_len[i];
190 dout("parsed dir dname '%.*s'\n", info->dir_dname_len[i],
191 info->dir_dname[i]);
192 info->dir_dlease[i] = *p;
193 *p += sizeof(struct ceph_mds_reply_lease);
194
195 /* inode */
14303d20 196 err = parse_reply_info_in(p, end, &info->dir_in[i], features);
2f2dc053
SW
197 if (err < 0)
198 goto out_bad;
199 i++;
200 num--;
201 }
202
203done:
204 if (*p != end)
205 goto bad;
206 return 0;
207
208bad:
209 err = -EIO;
210out_bad:
211 pr_err("problem parsing dir contents %d\n", err);
212 return err;
213}
214
25933abd
HS
215/*
216 * parse fcntl F_GETLK results
217 */
218static int parse_reply_info_filelock(void **p, void *end,
14303d20 219 struct ceph_mds_reply_info_parsed *info,
12b4629a 220 u64 features)
25933abd
HS
221{
222 if (*p + sizeof(*info->filelock_reply) > end)
223 goto bad;
224
225 info->filelock_reply = *p;
226 *p += sizeof(*info->filelock_reply);
227
228 if (unlikely(*p != end))
229 goto bad;
230 return 0;
231
232bad:
233 return -EIO;
234}
235
6e8575fa
SL
236/*
237 * parse create results
238 */
239static int parse_reply_info_create(void **p, void *end,
240 struct ceph_mds_reply_info_parsed *info,
12b4629a 241 u64 features)
6e8575fa
SL
242{
243 if (features & CEPH_FEATURE_REPLY_CREATE_INODE) {
244 if (*p == end) {
245 info->has_create_ino = false;
246 } else {
247 info->has_create_ino = true;
248 info->ino = ceph_decode_64(p);
249 }
250 }
251
252 if (unlikely(*p != end))
253 goto bad;
254 return 0;
255
256bad:
257 return -EIO;
258}
259
25933abd
HS
260/*
261 * parse extra results
262 */
263static int parse_reply_info_extra(void **p, void *end,
14303d20 264 struct ceph_mds_reply_info_parsed *info,
12b4629a 265 u64 features)
25933abd
HS
266{
267 if (info->head->op == CEPH_MDS_OP_GETFILELOCK)
14303d20 268 return parse_reply_info_filelock(p, end, info, features);
8a034497
YZ
269 else if (info->head->op == CEPH_MDS_OP_READDIR ||
270 info->head->op == CEPH_MDS_OP_LSSNAP)
14303d20 271 return parse_reply_info_dir(p, end, info, features);
6e8575fa
SL
272 else if (info->head->op == CEPH_MDS_OP_CREATE)
273 return parse_reply_info_create(p, end, info, features);
274 else
275 return -EIO;
25933abd
HS
276}
277
2f2dc053
SW
278/*
279 * parse entire mds reply
280 */
281static int parse_reply_info(struct ceph_msg *msg,
14303d20 282 struct ceph_mds_reply_info_parsed *info,
12b4629a 283 u64 features)
2f2dc053
SW
284{
285 void *p, *end;
286 u32 len;
287 int err;
288
289 info->head = msg->front.iov_base;
290 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
291 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
292
293 /* trace */
294 ceph_decode_32_safe(&p, end, len, bad);
295 if (len > 0) {
32852a81 296 ceph_decode_need(&p, end, len, bad);
14303d20 297 err = parse_reply_info_trace(&p, p+len, info, features);
2f2dc053
SW
298 if (err < 0)
299 goto out_bad;
300 }
301
25933abd 302 /* extra */
2f2dc053
SW
303 ceph_decode_32_safe(&p, end, len, bad);
304 if (len > 0) {
32852a81 305 ceph_decode_need(&p, end, len, bad);
14303d20 306 err = parse_reply_info_extra(&p, p+len, info, features);
2f2dc053
SW
307 if (err < 0)
308 goto out_bad;
309 }
310
311 /* snap blob */
312 ceph_decode_32_safe(&p, end, len, bad);
313 info->snapblob_len = len;
314 info->snapblob = p;
315 p += len;
316
317 if (p != end)
318 goto bad;
319 return 0;
320
321bad:
322 err = -EIO;
323out_bad:
324 pr_err("mds parse_reply err %d\n", err);
325 return err;
326}
327
328static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
329{
330 kfree(info->dir_in);
331}
332
333
334/*
335 * sessions
336 */
337static const char *session_state_name(int s)
338{
339 switch (s) {
340 case CEPH_MDS_SESSION_NEW: return "new";
341 case CEPH_MDS_SESSION_OPENING: return "opening";
342 case CEPH_MDS_SESSION_OPEN: return "open";
343 case CEPH_MDS_SESSION_HUNG: return "hung";
344 case CEPH_MDS_SESSION_CLOSING: return "closing";
44ca18f2 345 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
2f2dc053
SW
346 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
347 default: return "???";
348 }
349}
350
351static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
352{
353 if (atomic_inc_not_zero(&s->s_ref)) {
354 dout("mdsc get_session %p %d -> %d\n", s,
355 atomic_read(&s->s_ref)-1, atomic_read(&s->s_ref));
356 return s;
357 } else {
358 dout("mdsc get_session %p 0 -- FAIL", s);
359 return NULL;
360 }
361}
362
363void ceph_put_mds_session(struct ceph_mds_session *s)
364{
365 dout("mdsc put_session %p %d -> %d\n", s,
366 atomic_read(&s->s_ref), atomic_read(&s->s_ref)-1);
4e7a5dcd 367 if (atomic_dec_and_test(&s->s_ref)) {
6c4a1915 368 if (s->s_auth.authorizer)
27859f97
SW
369 ceph_auth_destroy_authorizer(
370 s->s_mdsc->fsc->client->monc.auth,
371 s->s_auth.authorizer);
2f2dc053 372 kfree(s);
4e7a5dcd 373 }
2f2dc053
SW
374}
375
376/*
377 * called under mdsc->mutex
378 */
379struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
380 int mds)
381{
382 struct ceph_mds_session *session;
383
384 if (mds >= mdsc->max_sessions || mdsc->sessions[mds] == NULL)
385 return NULL;
386 session = mdsc->sessions[mds];
387 dout("lookup_mds_session %p %d\n", session,
388 atomic_read(&session->s_ref));
389 get_session(session);
390 return session;
391}
392
393static bool __have_session(struct ceph_mds_client *mdsc, int mds)
394{
395 if (mds >= mdsc->max_sessions)
396 return false;
397 return mdsc->sessions[mds];
398}
399
2600d2dd
SW
400static int __verify_registered_session(struct ceph_mds_client *mdsc,
401 struct ceph_mds_session *s)
402{
403 if (s->s_mds >= mdsc->max_sessions ||
404 mdsc->sessions[s->s_mds] != s)
405 return -ENOENT;
406 return 0;
407}
408
2f2dc053
SW
409/*
410 * create+register a new session for given mds.
411 * called under mdsc->mutex.
412 */
413static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
414 int mds)
415{
416 struct ceph_mds_session *s;
417
c338c07c
NY
418 if (mds >= mdsc->mdsmap->m_max_mds)
419 return ERR_PTR(-EINVAL);
420
2f2dc053 421 s = kzalloc(sizeof(*s), GFP_NOFS);
4736b009
DC
422 if (!s)
423 return ERR_PTR(-ENOMEM);
2f2dc053
SW
424 s->s_mdsc = mdsc;
425 s->s_mds = mds;
426 s->s_state = CEPH_MDS_SESSION_NEW;
427 s->s_ttl = 0;
428 s->s_seq = 0;
429 mutex_init(&s->s_mutex);
430
b7a9e5dd 431 ceph_con_init(&s->s_con, s, &mds_con_ops, &mdsc->fsc->client->msgr);
2f2dc053 432
d8fb02ab 433 spin_lock_init(&s->s_gen_ttl_lock);
2f2dc053 434 s->s_cap_gen = 0;
1ce208a6 435 s->s_cap_ttl = jiffies - 1;
d8fb02ab
AE
436
437 spin_lock_init(&s->s_cap_lock);
2f2dc053
SW
438 s->s_renew_requested = 0;
439 s->s_renew_seq = 0;
440 INIT_LIST_HEAD(&s->s_caps);
441 s->s_nr_caps = 0;
5dacf091 442 s->s_trim_caps = 0;
2f2dc053
SW
443 atomic_set(&s->s_ref, 1);
444 INIT_LIST_HEAD(&s->s_waiting);
445 INIT_LIST_HEAD(&s->s_unsafe);
446 s->s_num_cap_releases = 0;
99a9c273 447 s->s_cap_reconnect = 0;
7c1332b8 448 s->s_cap_iterator = NULL;
2f2dc053
SW
449 INIT_LIST_HEAD(&s->s_cap_releases);
450 INIT_LIST_HEAD(&s->s_cap_releases_done);
451 INIT_LIST_HEAD(&s->s_cap_flushing);
452 INIT_LIST_HEAD(&s->s_cap_snaps_flushing);
453
454 dout("register_session mds%d\n", mds);
455 if (mds >= mdsc->max_sessions) {
456 int newmax = 1 << get_count_order(mds+1);
457 struct ceph_mds_session **sa;
458
459 dout("register_session realloc to %d\n", newmax);
460 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
461 if (sa == NULL)
42ce56e5 462 goto fail_realloc;
2f2dc053
SW
463 if (mdsc->sessions) {
464 memcpy(sa, mdsc->sessions,
465 mdsc->max_sessions * sizeof(void *));
466 kfree(mdsc->sessions);
467 }
468 mdsc->sessions = sa;
469 mdsc->max_sessions = newmax;
470 }
471 mdsc->sessions[mds] = s;
472 atomic_inc(&s->s_ref); /* one ref to sessions[], one to caller */
42ce56e5 473
b7a9e5dd
SW
474 ceph_con_open(&s->s_con, CEPH_ENTITY_TYPE_MDS, mds,
475 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
42ce56e5 476
2f2dc053 477 return s;
42ce56e5
SW
478
479fail_realloc:
480 kfree(s);
481 return ERR_PTR(-ENOMEM);
2f2dc053
SW
482}
483
484/*
485 * called under mdsc->mutex
486 */
2600d2dd 487static void __unregister_session(struct ceph_mds_client *mdsc,
42ce56e5 488 struct ceph_mds_session *s)
2f2dc053 489{
2600d2dd
SW
490 dout("__unregister_session mds%d %p\n", s->s_mds, s);
491 BUG_ON(mdsc->sessions[s->s_mds] != s);
42ce56e5
SW
492 mdsc->sessions[s->s_mds] = NULL;
493 ceph_con_close(&s->s_con);
494 ceph_put_mds_session(s);
2f2dc053
SW
495}
496
497/*
498 * drop session refs in request.
499 *
500 * should be last request ref, or hold mdsc->mutex
501 */
502static void put_request_session(struct ceph_mds_request *req)
503{
504 if (req->r_session) {
505 ceph_put_mds_session(req->r_session);
506 req->r_session = NULL;
507 }
508}
509
153c8e6b 510void ceph_mdsc_release_request(struct kref *kref)
2f2dc053 511{
153c8e6b
SW
512 struct ceph_mds_request *req = container_of(kref,
513 struct ceph_mds_request,
514 r_kref);
515 if (req->r_request)
516 ceph_msg_put(req->r_request);
517 if (req->r_reply) {
518 ceph_msg_put(req->r_reply);
519 destroy_reply_info(&req->r_reply_info);
520 }
521 if (req->r_inode) {
41b02e1f 522 ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
153c8e6b
SW
523 iput(req->r_inode);
524 }
525 if (req->r_locked_dir)
41b02e1f 526 ceph_put_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
153c8e6b
SW
527 if (req->r_target_inode)
528 iput(req->r_target_inode);
529 if (req->r_dentry)
530 dput(req->r_dentry);
844d87c3
SW
531 if (req->r_old_dentry)
532 dput(req->r_old_dentry);
533 if (req->r_old_dentry_dir) {
41b02e1f
SW
534 /*
535 * track (and drop pins for) r_old_dentry_dir
536 * separately, since r_old_dentry's d_parent may have
537 * changed between the dir mutex being dropped and
538 * this request being freed.
539 */
540 ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir),
541 CEPH_CAP_PIN);
41b02e1f 542 iput(req->r_old_dentry_dir);
2f2dc053 543 }
153c8e6b
SW
544 kfree(req->r_path1);
545 kfree(req->r_path2);
546 put_request_session(req);
37151668 547 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
153c8e6b 548 kfree(req);
2f2dc053
SW
549}
550
551/*
552 * lookup session, bump ref if found.
553 *
554 * called under mdsc->mutex.
555 */
556static struct ceph_mds_request *__lookup_request(struct ceph_mds_client *mdsc,
557 u64 tid)
558{
559 struct ceph_mds_request *req;
44ca18f2
SW
560 struct rb_node *n = mdsc->request_tree.rb_node;
561
562 while (n) {
563 req = rb_entry(n, struct ceph_mds_request, r_node);
564 if (tid < req->r_tid)
565 n = n->rb_left;
566 else if (tid > req->r_tid)
567 n = n->rb_right;
568 else {
569 ceph_mdsc_get_request(req);
570 return req;
571 }
572 }
573 return NULL;
574}
575
576static void __insert_request(struct ceph_mds_client *mdsc,
577 struct ceph_mds_request *new)
578{
579 struct rb_node **p = &mdsc->request_tree.rb_node;
580 struct rb_node *parent = NULL;
581 struct ceph_mds_request *req = NULL;
582
583 while (*p) {
584 parent = *p;
585 req = rb_entry(parent, struct ceph_mds_request, r_node);
586 if (new->r_tid < req->r_tid)
587 p = &(*p)->rb_left;
588 else if (new->r_tid > req->r_tid)
589 p = &(*p)->rb_right;
590 else
591 BUG();
592 }
593
594 rb_link_node(&new->r_node, parent, p);
595 rb_insert_color(&new->r_node, &mdsc->request_tree);
2f2dc053
SW
596}
597
598/*
599 * Register an in-flight request, and assign a tid. Link to directory
600 * are modifying (if any).
601 *
602 * Called under mdsc->mutex.
603 */
604static void __register_request(struct ceph_mds_client *mdsc,
605 struct ceph_mds_request *req,
606 struct inode *dir)
607{
608 req->r_tid = ++mdsc->last_tid;
609 if (req->r_num_caps)
37151668
YS
610 ceph_reserve_caps(mdsc, &req->r_caps_reservation,
611 req->r_num_caps);
2f2dc053
SW
612 dout("__register_request %p tid %lld\n", req, req->r_tid);
613 ceph_mdsc_get_request(req);
44ca18f2 614 __insert_request(mdsc, req);
2f2dc053 615
cb4276cc
SW
616 req->r_uid = current_fsuid();
617 req->r_gid = current_fsgid();
618
2f2dc053
SW
619 if (dir) {
620 struct ceph_inode_info *ci = ceph_inode(dir);
621
3b663780 622 ihold(dir);
2f2dc053
SW
623 spin_lock(&ci->i_unsafe_lock);
624 req->r_unsafe_dir = dir;
625 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
626 spin_unlock(&ci->i_unsafe_lock);
627 }
628}
629
630static void __unregister_request(struct ceph_mds_client *mdsc,
631 struct ceph_mds_request *req)
632{
633 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
44ca18f2 634 rb_erase(&req->r_node, &mdsc->request_tree);
80fc7314 635 RB_CLEAR_NODE(&req->r_node);
2f2dc053
SW
636
637 if (req->r_unsafe_dir) {
638 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
639
640 spin_lock(&ci->i_unsafe_lock);
641 list_del_init(&req->r_unsafe_dir_item);
642 spin_unlock(&ci->i_unsafe_lock);
3b663780
SW
643
644 iput(req->r_unsafe_dir);
645 req->r_unsafe_dir = NULL;
2f2dc053 646 }
94aa8ae1 647
fc55d2c9
YZ
648 complete_all(&req->r_safe_completion);
649
94aa8ae1 650 ceph_mdsc_put_request(req);
2f2dc053
SW
651}
652
653/*
654 * Choose mds to send request to next. If there is a hint set in the
655 * request (e.g., due to a prior forward hint from the mds), use that.
656 * Otherwise, consult frag tree and/or caps to identify the
657 * appropriate mds. If all else fails, choose randomly.
658 *
659 * Called under mdsc->mutex.
660 */
7fd7d101 661static struct dentry *get_nonsnap_parent(struct dentry *dentry)
eb6bb1c5 662{
d79698da
SW
663 /*
664 * we don't need to worry about protecting the d_parent access
665 * here because we never renaming inside the snapped namespace
666 * except to resplice to another snapdir, and either the old or new
667 * result is a valid result.
668 */
eb6bb1c5
SW
669 while (!IS_ROOT(dentry) && ceph_snap(dentry->d_inode) != CEPH_NOSNAP)
670 dentry = dentry->d_parent;
671 return dentry;
672}
673
2f2dc053
SW
674static int __choose_mds(struct ceph_mds_client *mdsc,
675 struct ceph_mds_request *req)
676{
677 struct inode *inode;
678 struct ceph_inode_info *ci;
679 struct ceph_cap *cap;
680 int mode = req->r_direct_mode;
681 int mds = -1;
682 u32 hash = req->r_direct_hash;
683 bool is_hash = req->r_direct_is_hash;
684
685 /*
686 * is there a specific mds we should try? ignore hint if we have
687 * no session and the mds is not up (active or recovering).
688 */
689 if (req->r_resend_mds >= 0 &&
690 (__have_session(mdsc, req->r_resend_mds) ||
691 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
692 dout("choose_mds using resend_mds mds%d\n",
693 req->r_resend_mds);
694 return req->r_resend_mds;
695 }
696
697 if (mode == USE_RANDOM_MDS)
698 goto random;
699
700 inode = NULL;
701 if (req->r_inode) {
702 inode = req->r_inode;
703 } else if (req->r_dentry) {
d79698da
SW
704 /* ignore race with rename; old or new d_parent is okay */
705 struct dentry *parent = req->r_dentry->d_parent;
706 struct inode *dir = parent->d_inode;
eb6bb1c5 707
3d14c5d2 708 if (dir->i_sb != mdsc->fsc->sb) {
eb6bb1c5
SW
709 /* not this fs! */
710 inode = req->r_dentry->d_inode;
711 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
712 /* direct snapped/virtual snapdir requests
713 * based on parent dir inode */
d79698da 714 struct dentry *dn = get_nonsnap_parent(parent);
eb6bb1c5
SW
715 inode = dn->d_inode;
716 dout("__choose_mds using nonsnap parent %p\n", inode);
ca18bede 717 } else {
eb6bb1c5 718 /* dentry target */
2f2dc053 719 inode = req->r_dentry->d_inode;
ca18bede
YZ
720 if (!inode || mode == USE_AUTH_MDS) {
721 /* dir + name */
722 inode = dir;
723 hash = ceph_dentry_hash(dir, req->r_dentry);
724 is_hash = true;
725 }
2f2dc053
SW
726 }
727 }
eb6bb1c5 728
2f2dc053
SW
729 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
730 (int)hash, mode);
731 if (!inode)
732 goto random;
733 ci = ceph_inode(inode);
734
735 if (is_hash && S_ISDIR(inode->i_mode)) {
736 struct ceph_inode_frag frag;
737 int found;
738
739 ceph_choose_frag(ci, hash, &frag, &found);
740 if (found) {
741 if (mode == USE_ANY_MDS && frag.ndist > 0) {
742 u8 r;
743
744 /* choose a random replica */
745 get_random_bytes(&r, 1);
746 r %= frag.ndist;
747 mds = frag.dist[r];
748 dout("choose_mds %p %llx.%llx "
749 "frag %u mds%d (%d/%d)\n",
750 inode, ceph_vinop(inode),
d66bbd44 751 frag.frag, mds,
2f2dc053 752 (int)r, frag.ndist);
d66bbd44
SW
753 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
754 CEPH_MDS_STATE_ACTIVE)
755 return mds;
2f2dc053
SW
756 }
757
758 /* since this file/dir wasn't known to be
759 * replicated, then we want to look for the
760 * authoritative mds. */
761 mode = USE_AUTH_MDS;
762 if (frag.mds >= 0) {
763 /* choose auth mds */
764 mds = frag.mds;
765 dout("choose_mds %p %llx.%llx "
766 "frag %u mds%d (auth)\n",
767 inode, ceph_vinop(inode), frag.frag, mds);
d66bbd44
SW
768 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
769 CEPH_MDS_STATE_ACTIVE)
770 return mds;
2f2dc053
SW
771 }
772 }
773 }
774
be655596 775 spin_lock(&ci->i_ceph_lock);
2f2dc053
SW
776 cap = NULL;
777 if (mode == USE_AUTH_MDS)
778 cap = ci->i_auth_cap;
779 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
780 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
781 if (!cap) {
be655596 782 spin_unlock(&ci->i_ceph_lock);
2f2dc053
SW
783 goto random;
784 }
785 mds = cap->session->s_mds;
786 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
787 inode, ceph_vinop(inode), mds,
788 cap == ci->i_auth_cap ? "auth " : "", cap);
be655596 789 spin_unlock(&ci->i_ceph_lock);
2f2dc053
SW
790 return mds;
791
792random:
793 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
794 dout("choose_mds chose random mds%d\n", mds);
795 return mds;
796}
797
798
799/*
800 * session messages
801 */
802static struct ceph_msg *create_session_msg(u32 op, u64 seq)
803{
804 struct ceph_msg *msg;
805 struct ceph_mds_session_head *h;
806
b61c2763
SW
807 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
808 false);
a79832f2 809 if (!msg) {
2f2dc053 810 pr_err("create_session_msg ENOMEM creating msg\n");
a79832f2 811 return NULL;
2f2dc053
SW
812 }
813 h = msg->front.iov_base;
814 h->op = cpu_to_le32(op);
815 h->seq = cpu_to_le64(seq);
816 return msg;
817}
818
819/*
820 * send session open request.
821 *
822 * called under mdsc->mutex
823 */
824static int __open_session(struct ceph_mds_client *mdsc,
825 struct ceph_mds_session *session)
826{
827 struct ceph_msg *msg;
828 int mstate;
829 int mds = session->s_mds;
2f2dc053
SW
830
831 /* wait for mds to go active? */
832 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
833 dout("open_session to mds%d (%s)\n", mds,
834 ceph_mds_state_name(mstate));
835 session->s_state = CEPH_MDS_SESSION_OPENING;
836 session->s_renew_requested = jiffies;
837
838 /* send connect message */
839 msg = create_session_msg(CEPH_SESSION_REQUEST_OPEN, session->s_seq);
a79832f2
SW
840 if (!msg)
841 return -ENOMEM;
2f2dc053 842 ceph_con_send(&session->s_con, msg);
2f2dc053
SW
843 return 0;
844}
845
ed0552a1
SW
846/*
847 * open sessions for any export targets for the given mds
848 *
849 * called under mdsc->mutex
850 */
5d72d13c
YZ
851static struct ceph_mds_session *
852__open_export_target_session(struct ceph_mds_client *mdsc, int target)
853{
854 struct ceph_mds_session *session;
855
856 session = __ceph_lookup_mds_session(mdsc, target);
857 if (!session) {
858 session = register_session(mdsc, target);
859 if (IS_ERR(session))
860 return session;
861 }
862 if (session->s_state == CEPH_MDS_SESSION_NEW ||
863 session->s_state == CEPH_MDS_SESSION_CLOSING)
864 __open_session(mdsc, session);
865
866 return session;
867}
868
869struct ceph_mds_session *
870ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target)
871{
872 struct ceph_mds_session *session;
873
874 dout("open_export_target_session to mds%d\n", target);
875
876 mutex_lock(&mdsc->mutex);
877 session = __open_export_target_session(mdsc, target);
878 mutex_unlock(&mdsc->mutex);
879
880 return session;
881}
882
ed0552a1
SW
883static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
884 struct ceph_mds_session *session)
885{
886 struct ceph_mds_info *mi;
887 struct ceph_mds_session *ts;
888 int i, mds = session->s_mds;
ed0552a1
SW
889
890 if (mds >= mdsc->mdsmap->m_max_mds)
891 return;
5d72d13c 892
ed0552a1
SW
893 mi = &mdsc->mdsmap->m_info[mds];
894 dout("open_export_target_sessions for mds%d (%d targets)\n",
895 session->s_mds, mi->num_export_targets);
896
897 for (i = 0; i < mi->num_export_targets; i++) {
5d72d13c
YZ
898 ts = __open_export_target_session(mdsc, mi->export_targets[i]);
899 if (!IS_ERR(ts))
900 ceph_put_mds_session(ts);
ed0552a1
SW
901 }
902}
903
154f42c2
SW
904void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
905 struct ceph_mds_session *session)
906{
907 mutex_lock(&mdsc->mutex);
908 __open_export_target_sessions(mdsc, session);
909 mutex_unlock(&mdsc->mutex);
910}
911
2f2dc053
SW
912/*
913 * session caps
914 */
915
916/*
917 * Free preallocated cap messages assigned to this session
918 */
919static void cleanup_cap_releases(struct ceph_mds_session *session)
920{
921 struct ceph_msg *msg;
922
923 spin_lock(&session->s_cap_lock);
924 while (!list_empty(&session->s_cap_releases)) {
925 msg = list_first_entry(&session->s_cap_releases,
926 struct ceph_msg, list_head);
927 list_del_init(&msg->list_head);
928 ceph_msg_put(msg);
929 }
930 while (!list_empty(&session->s_cap_releases_done)) {
931 msg = list_first_entry(&session->s_cap_releases_done,
932 struct ceph_msg, list_head);
933 list_del_init(&msg->list_head);
934 ceph_msg_put(msg);
935 }
936 spin_unlock(&session->s_cap_lock);
937}
938
939/*
f818a736
SW
940 * Helper to safely iterate over all caps associated with a session, with
941 * special care taken to handle a racing __ceph_remove_cap().
2f2dc053 942 *
f818a736 943 * Caller must hold session s_mutex.
2f2dc053
SW
944 */
945static int iterate_session_caps(struct ceph_mds_session *session,
946 int (*cb)(struct inode *, struct ceph_cap *,
947 void *), void *arg)
948{
7c1332b8
SW
949 struct list_head *p;
950 struct ceph_cap *cap;
951 struct inode *inode, *last_inode = NULL;
952 struct ceph_cap *old_cap = NULL;
2f2dc053
SW
953 int ret;
954
955 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
956 spin_lock(&session->s_cap_lock);
7c1332b8
SW
957 p = session->s_caps.next;
958 while (p != &session->s_caps) {
959 cap = list_entry(p, struct ceph_cap, session_caps);
2f2dc053 960 inode = igrab(&cap->ci->vfs_inode);
7c1332b8
SW
961 if (!inode) {
962 p = p->next;
2f2dc053 963 continue;
7c1332b8
SW
964 }
965 session->s_cap_iterator = cap;
2f2dc053 966 spin_unlock(&session->s_cap_lock);
7c1332b8
SW
967
968 if (last_inode) {
969 iput(last_inode);
970 last_inode = NULL;
971 }
972 if (old_cap) {
37151668 973 ceph_put_cap(session->s_mdsc, old_cap);
7c1332b8
SW
974 old_cap = NULL;
975 }
976
2f2dc053 977 ret = cb(inode, cap, arg);
7c1332b8
SW
978 last_inode = inode;
979
2f2dc053 980 spin_lock(&session->s_cap_lock);
7c1332b8
SW
981 p = p->next;
982 if (cap->ci == NULL) {
983 dout("iterate_session_caps finishing cap %p removal\n",
984 cap);
985 BUG_ON(cap->session != session);
986 list_del_init(&cap->session_caps);
987 session->s_nr_caps--;
988 cap->session = NULL;
989 old_cap = cap; /* put_cap it w/o locks held */
990 }
5dacf091
SW
991 if (ret < 0)
992 goto out;
2f2dc053 993 }
5dacf091
SW
994 ret = 0;
995out:
7c1332b8 996 session->s_cap_iterator = NULL;
2f2dc053 997 spin_unlock(&session->s_cap_lock);
7c1332b8
SW
998
999 if (last_inode)
1000 iput(last_inode);
1001 if (old_cap)
37151668 1002 ceph_put_cap(session->s_mdsc, old_cap);
7c1332b8 1003
5dacf091 1004 return ret;
2f2dc053
SW
1005}
1006
1007static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
6c99f254 1008 void *arg)
2f2dc053
SW
1009{
1010 struct ceph_inode_info *ci = ceph_inode(inode);
6c99f254
SW
1011 int drop = 0;
1012
2f2dc053
SW
1013 dout("removing cap %p, ci is %p, inode is %p\n",
1014 cap, ci, &ci->vfs_inode);
be655596 1015 spin_lock(&ci->i_ceph_lock);
a096b09a 1016 __ceph_remove_cap(cap, false);
6c99f254
SW
1017 if (!__ceph_is_any_real_caps(ci)) {
1018 struct ceph_mds_client *mdsc =
3d14c5d2 1019 ceph_sb_to_client(inode->i_sb)->mdsc;
6c99f254
SW
1020
1021 spin_lock(&mdsc->cap_dirty_lock);
1022 if (!list_empty(&ci->i_dirty_item)) {
1023 pr_info(" dropping dirty %s state for %p %lld\n",
1024 ceph_cap_string(ci->i_dirty_caps),
1025 inode, ceph_ino(inode));
1026 ci->i_dirty_caps = 0;
1027 list_del_init(&ci->i_dirty_item);
1028 drop = 1;
1029 }
1030 if (!list_empty(&ci->i_flushing_item)) {
1031 pr_info(" dropping dirty+flushing %s state for %p %lld\n",
1032 ceph_cap_string(ci->i_flushing_caps),
1033 inode, ceph_ino(inode));
1034 ci->i_flushing_caps = 0;
1035 list_del_init(&ci->i_flushing_item);
1036 mdsc->num_cap_flushing--;
1037 drop = 1;
1038 }
1039 if (drop && ci->i_wrbuffer_ref) {
1040 pr_info(" dropping dirty data for %p %lld\n",
1041 inode, ceph_ino(inode));
1042 ci->i_wrbuffer_ref = 0;
1043 ci->i_wrbuffer_ref_head = 0;
1044 drop++;
1045 }
1046 spin_unlock(&mdsc->cap_dirty_lock);
1047 }
be655596 1048 spin_unlock(&ci->i_ceph_lock);
6c99f254
SW
1049 while (drop--)
1050 iput(inode);
2f2dc053
SW
1051 return 0;
1052}
1053
1054/*
1055 * caller must hold session s_mutex
1056 */
1057static void remove_session_caps(struct ceph_mds_session *session)
1058{
1059 dout("remove_session_caps on %p\n", session);
1060 iterate_session_caps(session, remove_session_caps_cb, NULL);
6f60f889
YZ
1061
1062 spin_lock(&session->s_cap_lock);
1063 if (session->s_nr_caps > 0) {
1064 struct super_block *sb = session->s_mdsc->fsc->sb;
1065 struct inode *inode;
1066 struct ceph_cap *cap, *prev = NULL;
1067 struct ceph_vino vino;
1068 /*
1069 * iterate_session_caps() skips inodes that are being
1070 * deleted, we need to wait until deletions are complete.
1071 * __wait_on_freeing_inode() is designed for the job,
1072 * but it is not exported, so use lookup inode function
1073 * to access it.
1074 */
1075 while (!list_empty(&session->s_caps)) {
1076 cap = list_entry(session->s_caps.next,
1077 struct ceph_cap, session_caps);
1078 if (cap == prev)
1079 break;
1080 prev = cap;
1081 vino = cap->ci->i_vino;
1082 spin_unlock(&session->s_cap_lock);
1083
ed284c49 1084 inode = ceph_find_inode(sb, vino);
6f60f889
YZ
1085 iput(inode);
1086
1087 spin_lock(&session->s_cap_lock);
1088 }
1089 }
1090 spin_unlock(&session->s_cap_lock);
1091
2f2dc053 1092 BUG_ON(session->s_nr_caps > 0);
6c99f254 1093 BUG_ON(!list_empty(&session->s_cap_flushing));
2f2dc053
SW
1094 cleanup_cap_releases(session);
1095}
1096
1097/*
1098 * wake up any threads waiting on this session's caps. if the cap is
1099 * old (didn't get renewed on the client reconnect), remove it now.
1100 *
1101 * caller must hold s_mutex.
1102 */
1103static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1104 void *arg)
1105{
0dc2570f
SW
1106 struct ceph_inode_info *ci = ceph_inode(inode);
1107
03066f23 1108 wake_up_all(&ci->i_cap_wq);
0dc2570f 1109 if (arg) {
be655596 1110 spin_lock(&ci->i_ceph_lock);
0dc2570f
SW
1111 ci->i_wanted_max_size = 0;
1112 ci->i_requested_max_size = 0;
be655596 1113 spin_unlock(&ci->i_ceph_lock);
0dc2570f 1114 }
2f2dc053
SW
1115 return 0;
1116}
1117
0dc2570f
SW
1118static void wake_up_session_caps(struct ceph_mds_session *session,
1119 int reconnect)
2f2dc053
SW
1120{
1121 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
0dc2570f
SW
1122 iterate_session_caps(session, wake_up_session_cb,
1123 (void *)(unsigned long)reconnect);
2f2dc053
SW
1124}
1125
1126/*
1127 * Send periodic message to MDS renewing all currently held caps. The
1128 * ack will reset the expiration for all caps from this session.
1129 *
1130 * caller holds s_mutex
1131 */
1132static int send_renew_caps(struct ceph_mds_client *mdsc,
1133 struct ceph_mds_session *session)
1134{
1135 struct ceph_msg *msg;
1136 int state;
1137
1138 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1139 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1140 pr_info("mds%d caps stale\n", session->s_mds);
e4cb4cb8 1141 session->s_renew_requested = jiffies;
2f2dc053
SW
1142
1143 /* do not try to renew caps until a recovering mds has reconnected
1144 * with its clients. */
1145 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1146 if (state < CEPH_MDS_STATE_RECONNECT) {
1147 dout("send_renew_caps ignoring mds%d (%s)\n",
1148 session->s_mds, ceph_mds_state_name(state));
1149 return 0;
1150 }
1151
1152 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1153 ceph_mds_state_name(state));
2f2dc053
SW
1154 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1155 ++session->s_renew_seq);
a79832f2
SW
1156 if (!msg)
1157 return -ENOMEM;
2f2dc053
SW
1158 ceph_con_send(&session->s_con, msg);
1159 return 0;
1160}
1161
186e4f7a
YZ
1162static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
1163 struct ceph_mds_session *session, u64 seq)
1164{
1165 struct ceph_msg *msg;
1166
1167 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
1168 session->s_mds, session_state_name(session->s_state), seq);
1169 msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
1170 if (!msg)
1171 return -ENOMEM;
1172 ceph_con_send(&session->s_con, msg);
1173 return 0;
1174}
1175
1176
2f2dc053
SW
1177/*
1178 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
0dc2570f
SW
1179 *
1180 * Called under session->s_mutex
2f2dc053
SW
1181 */
1182static void renewed_caps(struct ceph_mds_client *mdsc,
1183 struct ceph_mds_session *session, int is_renew)
1184{
1185 int was_stale;
1186 int wake = 0;
1187
1188 spin_lock(&session->s_cap_lock);
1ce208a6 1189 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
2f2dc053
SW
1190
1191 session->s_cap_ttl = session->s_renew_requested +
1192 mdsc->mdsmap->m_session_timeout*HZ;
1193
1194 if (was_stale) {
1195 if (time_before(jiffies, session->s_cap_ttl)) {
1196 pr_info("mds%d caps renewed\n", session->s_mds);
1197 wake = 1;
1198 } else {
1199 pr_info("mds%d caps still stale\n", session->s_mds);
1200 }
1201 }
1202 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1203 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1204 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1205 spin_unlock(&session->s_cap_lock);
1206
1207 if (wake)
0dc2570f 1208 wake_up_session_caps(session, 0);
2f2dc053
SW
1209}
1210
1211/*
1212 * send a session close request
1213 */
1214static int request_close_session(struct ceph_mds_client *mdsc,
1215 struct ceph_mds_session *session)
1216{
1217 struct ceph_msg *msg;
2f2dc053
SW
1218
1219 dout("request_close_session mds%d state %s seq %lld\n",
1220 session->s_mds, session_state_name(session->s_state),
1221 session->s_seq);
1222 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
a79832f2
SW
1223 if (!msg)
1224 return -ENOMEM;
1225 ceph_con_send(&session->s_con, msg);
1226 return 0;
2f2dc053
SW
1227}
1228
1229/*
1230 * Called with s_mutex held.
1231 */
1232static int __close_session(struct ceph_mds_client *mdsc,
1233 struct ceph_mds_session *session)
1234{
1235 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1236 return 0;
1237 session->s_state = CEPH_MDS_SESSION_CLOSING;
1238 return request_close_session(mdsc, session);
1239}
1240
1241/*
1242 * Trim old(er) caps.
1243 *
1244 * Because we can't cache an inode without one or more caps, we do
1245 * this indirectly: if a cap is unused, we prune its aliases, at which
1246 * point the inode will hopefully get dropped to.
1247 *
1248 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1249 * memory pressure from the MDS, though, so it needn't be perfect.
1250 */
1251static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1252{
1253 struct ceph_mds_session *session = arg;
1254 struct ceph_inode_info *ci = ceph_inode(inode);
979abfdd 1255 int used, wanted, oissued, mine;
2f2dc053
SW
1256
1257 if (session->s_trim_caps <= 0)
1258 return -1;
1259
be655596 1260 spin_lock(&ci->i_ceph_lock);
2f2dc053
SW
1261 mine = cap->issued | cap->implemented;
1262 used = __ceph_caps_used(ci);
979abfdd 1263 wanted = __ceph_caps_file_wanted(ci);
2f2dc053
SW
1264 oissued = __ceph_caps_issued_other(ci, cap);
1265
979abfdd 1266 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
2f2dc053 1267 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
979abfdd
YZ
1268 ceph_cap_string(used), ceph_cap_string(wanted));
1269 if (cap == ci->i_auth_cap) {
1270 if (ci->i_dirty_caps | ci->i_flushing_caps)
1271 goto out;
1272 if ((used | wanted) & CEPH_CAP_ANY_WR)
1273 goto out;
1274 }
1275 if ((used | wanted) & ~oissued & mine)
2f2dc053
SW
1276 goto out; /* we need these caps */
1277
1278 session->s_trim_caps--;
1279 if (oissued) {
1280 /* we aren't the only cap.. just remove us */
a096b09a 1281 __ceph_remove_cap(cap, true);
2f2dc053
SW
1282 } else {
1283 /* try to drop referring dentries */
be655596 1284 spin_unlock(&ci->i_ceph_lock);
2f2dc053
SW
1285 d_prune_aliases(inode);
1286 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1287 inode, cap, atomic_read(&inode->i_count));
1288 return 0;
1289 }
1290
1291out:
be655596 1292 spin_unlock(&ci->i_ceph_lock);
2f2dc053
SW
1293 return 0;
1294}
1295
1296/*
1297 * Trim session cap count down to some max number.
1298 */
1299static int trim_caps(struct ceph_mds_client *mdsc,
1300 struct ceph_mds_session *session,
1301 int max_caps)
1302{
1303 int trim_caps = session->s_nr_caps - max_caps;
1304
1305 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1306 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1307 if (trim_caps > 0) {
1308 session->s_trim_caps = trim_caps;
1309 iterate_session_caps(session, trim_caps_cb, session);
1310 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1311 session->s_mds, session->s_nr_caps, max_caps,
1312 trim_caps - session->s_trim_caps);
5dacf091 1313 session->s_trim_caps = 0;
2f2dc053
SW
1314 }
1315 return 0;
1316}
1317
1318/*
1319 * Allocate cap_release messages. If there is a partially full message
1320 * in the queue, try to allocate enough to cover it's remainder, so that
1321 * we can send it immediately.
1322 *
1323 * Called under s_mutex.
1324 */
2b2300d6 1325int ceph_add_cap_releases(struct ceph_mds_client *mdsc,
ee6b272b 1326 struct ceph_mds_session *session)
2f2dc053 1327{
38e8883e 1328 struct ceph_msg *msg, *partial = NULL;
2f2dc053
SW
1329 struct ceph_mds_cap_release *head;
1330 int err = -ENOMEM;
3d14c5d2 1331 int extra = mdsc->fsc->mount_options->cap_release_safety;
38e8883e 1332 int num;
2f2dc053 1333
38e8883e
SW
1334 dout("add_cap_releases %p mds%d extra %d\n", session, session->s_mds,
1335 extra);
2f2dc053
SW
1336
1337 spin_lock(&session->s_cap_lock);
1338
1339 if (!list_empty(&session->s_cap_releases)) {
1340 msg = list_first_entry(&session->s_cap_releases,
1341 struct ceph_msg,
1342 list_head);
1343 head = msg->front.iov_base;
38e8883e
SW
1344 num = le32_to_cpu(head->num);
1345 if (num) {
1346 dout(" partial %p with (%d/%d)\n", msg, num,
1347 (int)CEPH_CAPS_PER_RELEASE);
1348 extra += CEPH_CAPS_PER_RELEASE - num;
1349 partial = msg;
1350 }
2f2dc053 1351 }
2f2dc053
SW
1352 while (session->s_num_cap_releases < session->s_nr_caps + extra) {
1353 spin_unlock(&session->s_cap_lock);
34d23762 1354 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE,
b61c2763 1355 GFP_NOFS, false);
2f2dc053
SW
1356 if (!msg)
1357 goto out_unlocked;
1358 dout("add_cap_releases %p msg %p now %d\n", session, msg,
1359 (int)msg->front.iov_len);
1360 head = msg->front.iov_base;
1361 head->num = cpu_to_le32(0);
1362 msg->front.iov_len = sizeof(*head);
1363 spin_lock(&session->s_cap_lock);
1364 list_add(&msg->list_head, &session->s_cap_releases);
1365 session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
1366 }
1367
38e8883e
SW
1368 if (partial) {
1369 head = partial->front.iov_base;
1370 num = le32_to_cpu(head->num);
1371 dout(" queueing partial %p with %d/%d\n", partial, num,
1372 (int)CEPH_CAPS_PER_RELEASE);
1373 list_move_tail(&partial->list_head,
1374 &session->s_cap_releases_done);
1375 session->s_num_cap_releases -= CEPH_CAPS_PER_RELEASE - num;
2f2dc053
SW
1376 }
1377 err = 0;
1378 spin_unlock(&session->s_cap_lock);
1379out_unlocked:
1380 return err;
1381}
1382
1383/*
1384 * flush all dirty inode data to disk.
1385 *
1386 * returns true if we've flushed through want_flush_seq
1387 */
1388static int check_cap_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
1389{
1390 int mds, ret = 1;
1391
1392 dout("check_cap_flush want %lld\n", want_flush_seq);
1393 mutex_lock(&mdsc->mutex);
1394 for (mds = 0; ret && mds < mdsc->max_sessions; mds++) {
1395 struct ceph_mds_session *session = mdsc->sessions[mds];
1396
1397 if (!session)
1398 continue;
1399 get_session(session);
1400 mutex_unlock(&mdsc->mutex);
1401
1402 mutex_lock(&session->s_mutex);
1403 if (!list_empty(&session->s_cap_flushing)) {
1404 struct ceph_inode_info *ci =
1405 list_entry(session->s_cap_flushing.next,
1406 struct ceph_inode_info,
1407 i_flushing_item);
1408 struct inode *inode = &ci->vfs_inode;
1409
be655596 1410 spin_lock(&ci->i_ceph_lock);
2f2dc053
SW
1411 if (ci->i_cap_flush_seq <= want_flush_seq) {
1412 dout("check_cap_flush still flushing %p "
1413 "seq %lld <= %lld to mds%d\n", inode,
1414 ci->i_cap_flush_seq, want_flush_seq,
1415 session->s_mds);
1416 ret = 0;
1417 }
be655596 1418 spin_unlock(&ci->i_ceph_lock);
2f2dc053
SW
1419 }
1420 mutex_unlock(&session->s_mutex);
1421 ceph_put_mds_session(session);
1422
1423 if (!ret)
1424 return ret;
1425 mutex_lock(&mdsc->mutex);
1426 }
1427
1428 mutex_unlock(&mdsc->mutex);
1429 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
1430 return ret;
1431}
1432
1433/*
1434 * called under s_mutex
1435 */
3d7ded4d
SW
1436void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1437 struct ceph_mds_session *session)
2f2dc053
SW
1438{
1439 struct ceph_msg *msg;
1440
1441 dout("send_cap_releases mds%d\n", session->s_mds);
0f8605f2
SW
1442 spin_lock(&session->s_cap_lock);
1443 while (!list_empty(&session->s_cap_releases_done)) {
2f2dc053
SW
1444 msg = list_first_entry(&session->s_cap_releases_done,
1445 struct ceph_msg, list_head);
1446 list_del_init(&msg->list_head);
1447 spin_unlock(&session->s_cap_lock);
1448 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1449 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1450 ceph_con_send(&session->s_con, msg);
0f8605f2 1451 spin_lock(&session->s_cap_lock);
2f2dc053
SW
1452 }
1453 spin_unlock(&session->s_cap_lock);
1454}
1455
e01a5946
SW
1456static void discard_cap_releases(struct ceph_mds_client *mdsc,
1457 struct ceph_mds_session *session)
1458{
1459 struct ceph_msg *msg;
1460 struct ceph_mds_cap_release *head;
1461 unsigned num;
1462
1463 dout("discard_cap_releases mds%d\n", session->s_mds);
e01a5946
SW
1464
1465 /* zero out the in-progress message */
1466 msg = list_first_entry(&session->s_cap_releases,
1467 struct ceph_msg, list_head);
1468 head = msg->front.iov_base;
1469 num = le32_to_cpu(head->num);
1470 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg, num);
1471 head->num = cpu_to_le32(0);
3803da49 1472 msg->front.iov_len = sizeof(*head);
e01a5946
SW
1473 session->s_num_cap_releases += num;
1474
1475 /* requeue completed messages */
1476 while (!list_empty(&session->s_cap_releases_done)) {
1477 msg = list_first_entry(&session->s_cap_releases_done,
1478 struct ceph_msg, list_head);
1479 list_del_init(&msg->list_head);
1480
1481 head = msg->front.iov_base;
1482 num = le32_to_cpu(head->num);
1483 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg,
1484 num);
1485 session->s_num_cap_releases += num;
1486 head->num = cpu_to_le32(0);
1487 msg->front.iov_len = sizeof(*head);
1488 list_add(&msg->list_head, &session->s_cap_releases);
1489 }
e01a5946
SW
1490}
1491
2f2dc053
SW
1492/*
1493 * requests
1494 */
1495
1496/*
1497 * Create an mds request.
1498 */
1499struct ceph_mds_request *
1500ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1501{
1502 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1503
1504 if (!req)
1505 return ERR_PTR(-ENOMEM);
1506
b4556396 1507 mutex_init(&req->r_fill_mutex);
37151668 1508 req->r_mdsc = mdsc;
2f2dc053
SW
1509 req->r_started = jiffies;
1510 req->r_resend_mds = -1;
1511 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1512 req->r_fmode = -1;
153c8e6b 1513 kref_init(&req->r_kref);
2f2dc053
SW
1514 INIT_LIST_HEAD(&req->r_wait);
1515 init_completion(&req->r_completion);
1516 init_completion(&req->r_safe_completion);
1517 INIT_LIST_HEAD(&req->r_unsafe_item);
1518
1519 req->r_op = op;
1520 req->r_direct_mode = mode;
1521 return req;
1522}
1523
1524/*
44ca18f2 1525 * return oldest (lowest) request, tid in request tree, 0 if none.
2f2dc053
SW
1526 *
1527 * called under mdsc->mutex.
1528 */
44ca18f2
SW
1529static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1530{
1531 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1532 return NULL;
1533 return rb_entry(rb_first(&mdsc->request_tree),
1534 struct ceph_mds_request, r_node);
1535}
1536
2f2dc053
SW
1537static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1538{
44ca18f2
SW
1539 struct ceph_mds_request *req = __get_oldest_req(mdsc);
1540
1541 if (req)
1542 return req->r_tid;
1543 return 0;
2f2dc053
SW
1544}
1545
1546/*
1547 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1548 * on build_path_from_dentry in fs/cifs/dir.c.
1549 *
1550 * If @stop_on_nosnap, generate path relative to the first non-snapped
1551 * inode.
1552 *
1553 * Encode hidden .snap dirs as a double /, i.e.
1554 * foo/.snap/bar -> foo//bar
1555 */
1556char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1557 int stop_on_nosnap)
1558{
1559 struct dentry *temp;
1560 char *path;
1561 int len, pos;
1b71fe2e 1562 unsigned seq;
2f2dc053
SW
1563
1564 if (dentry == NULL)
1565 return ERR_PTR(-EINVAL);
1566
1567retry:
1568 len = 0;
1b71fe2e
AV
1569 seq = read_seqbegin(&rename_lock);
1570 rcu_read_lock();
2f2dc053
SW
1571 for (temp = dentry; !IS_ROOT(temp);) {
1572 struct inode *inode = temp->d_inode;
1573 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1574 len++; /* slash only */
1575 else if (stop_on_nosnap && inode &&
1576 ceph_snap(inode) == CEPH_NOSNAP)
1577 break;
1578 else
1579 len += 1 + temp->d_name.len;
1580 temp = temp->d_parent;
2f2dc053 1581 }
1b71fe2e 1582 rcu_read_unlock();
2f2dc053
SW
1583 if (len)
1584 len--; /* no leading '/' */
1585
1586 path = kmalloc(len+1, GFP_NOFS);
1587 if (path == NULL)
1588 return ERR_PTR(-ENOMEM);
1589 pos = len;
1590 path[pos] = 0; /* trailing null */
1b71fe2e 1591 rcu_read_lock();
2f2dc053 1592 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
1b71fe2e 1593 struct inode *inode;
2f2dc053 1594
1b71fe2e
AV
1595 spin_lock(&temp->d_lock);
1596 inode = temp->d_inode;
2f2dc053 1597 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
104648ad 1598 dout("build_path path+%d: %p SNAPDIR\n",
2f2dc053
SW
1599 pos, temp);
1600 } else if (stop_on_nosnap && inode &&
1601 ceph_snap(inode) == CEPH_NOSNAP) {
9d5a09e6 1602 spin_unlock(&temp->d_lock);
2f2dc053
SW
1603 break;
1604 } else {
1605 pos -= temp->d_name.len;
1b71fe2e
AV
1606 if (pos < 0) {
1607 spin_unlock(&temp->d_lock);
2f2dc053 1608 break;
1b71fe2e 1609 }
2f2dc053
SW
1610 strncpy(path + pos, temp->d_name.name,
1611 temp->d_name.len);
2f2dc053 1612 }
1b71fe2e 1613 spin_unlock(&temp->d_lock);
2f2dc053
SW
1614 if (pos)
1615 path[--pos] = '/';
1616 temp = temp->d_parent;
2f2dc053 1617 }
1b71fe2e
AV
1618 rcu_read_unlock();
1619 if (pos != 0 || read_seqretry(&rename_lock, seq)) {
104648ad 1620 pr_err("build_path did not end path lookup where "
2f2dc053
SW
1621 "expected, namelen is %d, pos is %d\n", len, pos);
1622 /* presumably this is only possible if racing with a
1623 rename of one of the parent directories (we can not
1624 lock the dentries above us to prevent this, but
1625 retrying should be harmless) */
1626 kfree(path);
1627 goto retry;
1628 }
1629
1630 *base = ceph_ino(temp->d_inode);
1631 *plen = len;
104648ad 1632 dout("build_path on %p %d built %llx '%.*s'\n",
84d08fa8 1633 dentry, d_count(dentry), *base, len, path);
2f2dc053
SW
1634 return path;
1635}
1636
1637static int build_dentry_path(struct dentry *dentry,
1638 const char **ppath, int *ppathlen, u64 *pino,
1639 int *pfreepath)
1640{
1641 char *path;
1642
1643 if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) {
1644 *pino = ceph_ino(dentry->d_parent->d_inode);
1645 *ppath = dentry->d_name.name;
1646 *ppathlen = dentry->d_name.len;
1647 return 0;
1648 }
1649 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1650 if (IS_ERR(path))
1651 return PTR_ERR(path);
1652 *ppath = path;
1653 *pfreepath = 1;
1654 return 0;
1655}
1656
1657static int build_inode_path(struct inode *inode,
1658 const char **ppath, int *ppathlen, u64 *pino,
1659 int *pfreepath)
1660{
1661 struct dentry *dentry;
1662 char *path;
1663
1664 if (ceph_snap(inode) == CEPH_NOSNAP) {
1665 *pino = ceph_ino(inode);
1666 *ppathlen = 0;
1667 return 0;
1668 }
1669 dentry = d_find_alias(inode);
1670 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1671 dput(dentry);
1672 if (IS_ERR(path))
1673 return PTR_ERR(path);
1674 *ppath = path;
1675 *pfreepath = 1;
1676 return 0;
1677}
1678
1679/*
1680 * request arguments may be specified via an inode *, a dentry *, or
1681 * an explicit ino+path.
1682 */
1683static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1684 const char *rpath, u64 rino,
1685 const char **ppath, int *pathlen,
1686 u64 *ino, int *freepath)
1687{
1688 int r = 0;
1689
1690 if (rinode) {
1691 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1692 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1693 ceph_snap(rinode));
1694 } else if (rdentry) {
1695 r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
1696 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1697 *ppath);
795858db 1698 } else if (rpath || rino) {
2f2dc053
SW
1699 *ino = rino;
1700 *ppath = rpath;
b000056a 1701 *pathlen = rpath ? strlen(rpath) : 0;
2f2dc053
SW
1702 dout(" path %.*s\n", *pathlen, rpath);
1703 }
1704
1705 return r;
1706}
1707
1708/*
1709 * called under mdsc->mutex
1710 */
1711static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1712 struct ceph_mds_request *req,
1713 int mds)
1714{
1715 struct ceph_msg *msg;
1716 struct ceph_mds_request_head *head;
1717 const char *path1 = NULL;
1718 const char *path2 = NULL;
1719 u64 ino1 = 0, ino2 = 0;
1720 int pathlen1 = 0, pathlen2 = 0;
1721 int freepath1 = 0, freepath2 = 0;
1722 int len;
1723 u16 releases;
1724 void *p, *end;
1725 int ret;
1726
1727 ret = set_request_path_attr(req->r_inode, req->r_dentry,
1728 req->r_path1, req->r_ino1.ino,
1729 &path1, &pathlen1, &ino1, &freepath1);
1730 if (ret < 0) {
1731 msg = ERR_PTR(ret);
1732 goto out;
1733 }
1734
1735 ret = set_request_path_attr(NULL, req->r_old_dentry,
1736 req->r_path2, req->r_ino2.ino,
1737 &path2, &pathlen2, &ino2, &freepath2);
1738 if (ret < 0) {
1739 msg = ERR_PTR(ret);
1740 goto out_free1;
1741 }
1742
1743 len = sizeof(*head) +
ac8839d7 1744 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64));
2f2dc053
SW
1745
1746 /* calculate (max) length for cap releases */
1747 len += sizeof(struct ceph_mds_request_release) *
1748 (!!req->r_inode_drop + !!req->r_dentry_drop +
1749 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1750 if (req->r_dentry_drop)
1751 len += req->r_dentry->d_name.len;
1752 if (req->r_old_dentry_drop)
1753 len += req->r_old_dentry->d_name.len;
1754
b61c2763 1755 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS, false);
a79832f2
SW
1756 if (!msg) {
1757 msg = ERR_PTR(-ENOMEM);
2f2dc053 1758 goto out_free2;
a79832f2 1759 }
2f2dc053 1760
6df058c0
SW
1761 msg->hdr.tid = cpu_to_le64(req->r_tid);
1762
2f2dc053
SW
1763 head = msg->front.iov_base;
1764 p = msg->front.iov_base + sizeof(*head);
1765 end = msg->front.iov_base + msg->front.iov_len;
1766
1767 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1768 head->op = cpu_to_le32(req->r_op);
ff3d0046
EB
1769 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid));
1770 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid));
2f2dc053
SW
1771 head->args = req->r_args;
1772
1773 ceph_encode_filepath(&p, end, ino1, path1);
1774 ceph_encode_filepath(&p, end, ino2, path2);
1775
e979cf50
SW
1776 /* make note of release offset, in case we need to replay */
1777 req->r_request_release_offset = p - msg->front.iov_base;
1778
2f2dc053
SW
1779 /* cap releases */
1780 releases = 0;
1781 if (req->r_inode_drop)
1782 releases += ceph_encode_inode_release(&p,
1783 req->r_inode ? req->r_inode : req->r_dentry->d_inode,
1784 mds, req->r_inode_drop, req->r_inode_unless, 0);
1785 if (req->r_dentry_drop)
1786 releases += ceph_encode_dentry_release(&p, req->r_dentry,
1787 mds, req->r_dentry_drop, req->r_dentry_unless);
1788 if (req->r_old_dentry_drop)
1789 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
1790 mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
1791 if (req->r_old_inode_drop)
1792 releases += ceph_encode_inode_release(&p,
1793 req->r_old_dentry->d_inode,
1794 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
1795 head->num_releases = cpu_to_le16(releases);
1796
1797 BUG_ON(p > end);
1798 msg->front.iov_len = p - msg->front.iov_base;
1799 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1800
ebf18f47
AE
1801 if (req->r_data_len) {
1802 /* outbound data set only by ceph_sync_setxattr() */
1803 BUG_ON(!req->r_pages);
90af3602 1804 ceph_msg_data_add_pages(msg, req->r_pages, req->r_data_len, 0);
ebf18f47 1805 }
02afca6c 1806
2f2dc053
SW
1807 msg->hdr.data_len = cpu_to_le32(req->r_data_len);
1808 msg->hdr.data_off = cpu_to_le16(0);
1809
1810out_free2:
1811 if (freepath2)
1812 kfree((char *)path2);
1813out_free1:
1814 if (freepath1)
1815 kfree((char *)path1);
1816out:
1817 return msg;
1818}
1819
1820/*
1821 * called under mdsc->mutex if error, under no mutex if
1822 * success.
1823 */
1824static void complete_request(struct ceph_mds_client *mdsc,
1825 struct ceph_mds_request *req)
1826{
1827 if (req->r_callback)
1828 req->r_callback(mdsc, req);
1829 else
03066f23 1830 complete_all(&req->r_completion);
2f2dc053
SW
1831}
1832
1833/*
1834 * called under mdsc->mutex
1835 */
1836static int __prepare_send_request(struct ceph_mds_client *mdsc,
1837 struct ceph_mds_request *req,
1838 int mds)
1839{
1840 struct ceph_mds_request_head *rhead;
1841 struct ceph_msg *msg;
1842 int flags = 0;
1843
2f2dc053 1844 req->r_attempts++;
e55b71f8
GF
1845 if (req->r_inode) {
1846 struct ceph_cap *cap =
1847 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
1848
1849 if (cap)
1850 req->r_sent_on_mseq = cap->mseq;
1851 else
1852 req->r_sent_on_mseq = -1;
1853 }
2f2dc053
SW
1854 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
1855 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
1856
01a92f17
SW
1857 if (req->r_got_unsafe) {
1858 /*
1859 * Replay. Do not regenerate message (and rebuild
1860 * paths, etc.); just use the original message.
1861 * Rebuilding paths will break for renames because
1862 * d_move mangles the src name.
1863 */
1864 msg = req->r_request;
1865 rhead = msg->front.iov_base;
1866
1867 flags = le32_to_cpu(rhead->flags);
1868 flags |= CEPH_MDS_FLAG_REPLAY;
1869 rhead->flags = cpu_to_le32(flags);
1870
1871 if (req->r_target_inode)
1872 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
1873
1874 rhead->num_retry = req->r_attempts - 1;
e979cf50
SW
1875
1876 /* remove cap/dentry releases from message */
1877 rhead->num_releases = 0;
1878 msg->hdr.front_len = cpu_to_le32(req->r_request_release_offset);
1879 msg->front.iov_len = req->r_request_release_offset;
01a92f17
SW
1880 return 0;
1881 }
1882
2f2dc053
SW
1883 if (req->r_request) {
1884 ceph_msg_put(req->r_request);
1885 req->r_request = NULL;
1886 }
1887 msg = create_request_message(mdsc, req, mds);
1888 if (IS_ERR(msg)) {
e1518c7c 1889 req->r_err = PTR_ERR(msg);
2f2dc053 1890 complete_request(mdsc, req);
a79832f2 1891 return PTR_ERR(msg);
2f2dc053
SW
1892 }
1893 req->r_request = msg;
1894
1895 rhead = msg->front.iov_base;
2f2dc053
SW
1896 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
1897 if (req->r_got_unsafe)
1898 flags |= CEPH_MDS_FLAG_REPLAY;
1899 if (req->r_locked_dir)
1900 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
1901 rhead->flags = cpu_to_le32(flags);
1902 rhead->num_fwd = req->r_num_fwd;
1903 rhead->num_retry = req->r_attempts - 1;
01a92f17 1904 rhead->ino = 0;
2f2dc053
SW
1905
1906 dout(" r_locked_dir = %p\n", req->r_locked_dir);
2f2dc053
SW
1907 return 0;
1908}
1909
1910/*
1911 * send request, or put it on the appropriate wait list.
1912 */
1913static int __do_request(struct ceph_mds_client *mdsc,
1914 struct ceph_mds_request *req)
1915{
1916 struct ceph_mds_session *session = NULL;
1917 int mds = -1;
1918 int err = -EAGAIN;
1919
eb1b8af3
YZ
1920 if (req->r_err || req->r_got_result) {
1921 if (req->r_aborted)
1922 __unregister_request(mdsc, req);
2f2dc053 1923 goto out;
eb1b8af3 1924 }
2f2dc053
SW
1925
1926 if (req->r_timeout &&
1927 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
1928 dout("do_request timed out\n");
1929 err = -EIO;
1930 goto finish;
1931 }
1932
dc69e2e9
SW
1933 put_request_session(req);
1934
2f2dc053
SW
1935 mds = __choose_mds(mdsc, req);
1936 if (mds < 0 ||
1937 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
1938 dout("do_request no mds or not active, waiting for map\n");
1939 list_add(&req->r_wait, &mdsc->waiting_for_map);
1940 goto out;
1941 }
1942
1943 /* get, open session */
1944 session = __ceph_lookup_mds_session(mdsc, mds);
9c423956 1945 if (!session) {
2f2dc053 1946 session = register_session(mdsc, mds);
9c423956
SW
1947 if (IS_ERR(session)) {
1948 err = PTR_ERR(session);
1949 goto finish;
1950 }
1951 }
dc69e2e9
SW
1952 req->r_session = get_session(session);
1953
2f2dc053
SW
1954 dout("do_request mds%d session %p state %s\n", mds, session,
1955 session_state_name(session->s_state));
1956 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
1957 session->s_state != CEPH_MDS_SESSION_HUNG) {
1958 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1959 session->s_state == CEPH_MDS_SESSION_CLOSING)
1960 __open_session(mdsc, session);
1961 list_add(&req->r_wait, &session->s_waiting);
1962 goto out_session;
1963 }
1964
1965 /* send request */
2f2dc053
SW
1966 req->r_resend_mds = -1; /* forget any previous mds hint */
1967
1968 if (req->r_request_started == 0) /* note request start time */
1969 req->r_request_started = jiffies;
1970
1971 err = __prepare_send_request(mdsc, req, mds);
1972 if (!err) {
1973 ceph_msg_get(req->r_request);
1974 ceph_con_send(&session->s_con, req->r_request);
1975 }
1976
1977out_session:
1978 ceph_put_mds_session(session);
1979out:
1980 return err;
1981
1982finish:
e1518c7c 1983 req->r_err = err;
2f2dc053
SW
1984 complete_request(mdsc, req);
1985 goto out;
1986}
1987
1988/*
1989 * called under mdsc->mutex
1990 */
1991static void __wake_requests(struct ceph_mds_client *mdsc,
1992 struct list_head *head)
1993{
ed75ec2c
YZ
1994 struct ceph_mds_request *req;
1995 LIST_HEAD(tmp_list);
1996
1997 list_splice_init(head, &tmp_list);
2f2dc053 1998
ed75ec2c
YZ
1999 while (!list_empty(&tmp_list)) {
2000 req = list_entry(tmp_list.next,
2001 struct ceph_mds_request, r_wait);
2f2dc053 2002 list_del_init(&req->r_wait);
7971bd92 2003 dout(" wake request %p tid %llu\n", req, req->r_tid);
2f2dc053
SW
2004 __do_request(mdsc, req);
2005 }
2006}
2007
2008/*
2009 * Wake up threads with requests pending for @mds, so that they can
29790f26 2010 * resubmit their requests to a possibly different mds.
2f2dc053 2011 */
29790f26 2012static void kick_requests(struct ceph_mds_client *mdsc, int mds)
2f2dc053 2013{
44ca18f2
SW
2014 struct ceph_mds_request *req;
2015 struct rb_node *p;
2f2dc053
SW
2016
2017 dout("kick_requests mds%d\n", mds);
44ca18f2
SW
2018 for (p = rb_first(&mdsc->request_tree); p; p = rb_next(p)) {
2019 req = rb_entry(p, struct ceph_mds_request, r_node);
2020 if (req->r_got_unsafe)
2021 continue;
2022 if (req->r_session &&
2023 req->r_session->s_mds == mds) {
2024 dout(" kicking tid %llu\n", req->r_tid);
44ca18f2 2025 __do_request(mdsc, req);
2f2dc053
SW
2026 }
2027 }
2028}
2029
2030void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
2031 struct ceph_mds_request *req)
2032{
2033 dout("submit_request on %p\n", req);
2034 mutex_lock(&mdsc->mutex);
2035 __register_request(mdsc, req, NULL);
2036 __do_request(mdsc, req);
2037 mutex_unlock(&mdsc->mutex);
2038}
2039
2040/*
2041 * Synchrously perform an mds request. Take care of all of the
2042 * session setup, forwarding, retry details.
2043 */
2044int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
2045 struct inode *dir,
2046 struct ceph_mds_request *req)
2047{
2048 int err;
2049
2050 dout("do_request on %p\n", req);
2051
2052 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
2053 if (req->r_inode)
2054 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
2055 if (req->r_locked_dir)
2056 ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
844d87c3 2057 if (req->r_old_dentry_dir)
41b02e1f
SW
2058 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
2059 CEPH_CAP_PIN);
2f2dc053
SW
2060
2061 /* issue */
2062 mutex_lock(&mdsc->mutex);
2063 __register_request(mdsc, req, dir);
2064 __do_request(mdsc, req);
2065
e1518c7c
SW
2066 if (req->r_err) {
2067 err = req->r_err;
2068 __unregister_request(mdsc, req);
2069 dout("do_request early error %d\n", err);
2070 goto out;
2f2dc053
SW
2071 }
2072
e1518c7c
SW
2073 /* wait */
2074 mutex_unlock(&mdsc->mutex);
2075 dout("do_request waiting\n");
2076 if (req->r_timeout) {
aa91647c 2077 err = (long)wait_for_completion_killable_timeout(
e1518c7c
SW
2078 &req->r_completion, req->r_timeout);
2079 if (err == 0)
2080 err = -EIO;
2081 } else {
aa91647c 2082 err = wait_for_completion_killable(&req->r_completion);
e1518c7c
SW
2083 }
2084 dout("do_request waited, got %d\n", err);
2085 mutex_lock(&mdsc->mutex);
5b1daecd 2086
e1518c7c
SW
2087 /* only abort if we didn't race with a real reply */
2088 if (req->r_got_result) {
2089 err = le32_to_cpu(req->r_reply_info.head->result);
2090 } else if (err < 0) {
2091 dout("aborted request %lld with %d\n", req->r_tid, err);
b4556396
SW
2092
2093 /*
2094 * ensure we aren't running concurrently with
2095 * ceph_fill_trace or ceph_readdir_prepopulate, which
2096 * rely on locks (dir mutex) held by our caller.
2097 */
2098 mutex_lock(&req->r_fill_mutex);
e1518c7c
SW
2099 req->r_err = err;
2100 req->r_aborted = true;
b4556396 2101 mutex_unlock(&req->r_fill_mutex);
5b1daecd 2102
e1518c7c 2103 if (req->r_locked_dir &&
167c9e35
SW
2104 (req->r_op & CEPH_MDS_OP_WRITE))
2105 ceph_invalidate_dir_request(req);
2f2dc053 2106 } else {
e1518c7c 2107 err = req->r_err;
2f2dc053 2108 }
2f2dc053 2109
e1518c7c
SW
2110out:
2111 mutex_unlock(&mdsc->mutex);
2f2dc053
SW
2112 dout("do_request %p done, result %d\n", req, err);
2113 return err;
2114}
2115
167c9e35 2116/*
2f276c51 2117 * Invalidate dir's completeness, dentry lease state on an aborted MDS
167c9e35
SW
2118 * namespace request.
2119 */
2120void ceph_invalidate_dir_request(struct ceph_mds_request *req)
2121{
2122 struct inode *inode = req->r_locked_dir;
167c9e35 2123
2f276c51 2124 dout("invalidate_dir_request %p (complete, lease(s))\n", inode);
167c9e35 2125
2f276c51 2126 ceph_dir_clear_complete(inode);
167c9e35
SW
2127 if (req->r_dentry)
2128 ceph_invalidate_dentry_lease(req->r_dentry);
2129 if (req->r_old_dentry)
2130 ceph_invalidate_dentry_lease(req->r_old_dentry);
2131}
2132
2f2dc053
SW
2133/*
2134 * Handle mds reply.
2135 *
2136 * We take the session mutex and parse and process the reply immediately.
2137 * This preserves the logical ordering of replies, capabilities, etc., sent
2138 * by the MDS as they are applied to our local cache.
2139 */
2140static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2141{
2142 struct ceph_mds_client *mdsc = session->s_mdsc;
2143 struct ceph_mds_request *req;
2144 struct ceph_mds_reply_head *head = msg->front.iov_base;
2145 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
2146 u64 tid;
2147 int err, result;
2600d2dd 2148 int mds = session->s_mds;
2f2dc053 2149
2f2dc053
SW
2150 if (msg->front.iov_len < sizeof(*head)) {
2151 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
9ec7cab1 2152 ceph_msg_dump(msg);
2f2dc053
SW
2153 return;
2154 }
2155
2156 /* get request, session */
6df058c0 2157 tid = le64_to_cpu(msg->hdr.tid);
2f2dc053
SW
2158 mutex_lock(&mdsc->mutex);
2159 req = __lookup_request(mdsc, tid);
2160 if (!req) {
2161 dout("handle_reply on unknown tid %llu\n", tid);
2162 mutex_unlock(&mdsc->mutex);
2163 return;
2164 }
2165 dout("handle_reply %p\n", req);
2f2dc053
SW
2166
2167 /* correct session? */
d96d6049 2168 if (req->r_session != session) {
2f2dc053
SW
2169 pr_err("mdsc_handle_reply got %llu on session mds%d"
2170 " not mds%d\n", tid, session->s_mds,
2171 req->r_session ? req->r_session->s_mds : -1);
2172 mutex_unlock(&mdsc->mutex);
2173 goto out;
2174 }
2175
2176 /* dup? */
2177 if ((req->r_got_unsafe && !head->safe) ||
2178 (req->r_got_safe && head->safe)) {
2179 pr_warning("got a dup %s reply on %llu from mds%d\n",
2180 head->safe ? "safe" : "unsafe", tid, mds);
2181 mutex_unlock(&mdsc->mutex);
2182 goto out;
2183 }
85792d0d
SW
2184 if (req->r_got_safe && !head->safe) {
2185 pr_warning("got unsafe after safe on %llu from mds%d\n",
2186 tid, mds);
2187 mutex_unlock(&mdsc->mutex);
2188 goto out;
2189 }
2f2dc053
SW
2190
2191 result = le32_to_cpu(head->result);
2192
2193 /*
e55b71f8
GF
2194 * Handle an ESTALE
2195 * if we're not talking to the authority, send to them
2196 * if the authority has changed while we weren't looking,
2197 * send to new authority
2198 * Otherwise we just have to return an ESTALE
2f2dc053
SW
2199 */
2200 if (result == -ESTALE) {
e55b71f8 2201 dout("got ESTALE on request %llu", req->r_tid);
ca18bede 2202 if (req->r_direct_mode != USE_AUTH_MDS) {
e55b71f8
GF
2203 dout("not using auth, setting for that now");
2204 req->r_direct_mode = USE_AUTH_MDS;
2f2dc053
SW
2205 __do_request(mdsc, req);
2206 mutex_unlock(&mdsc->mutex);
2207 goto out;
e55b71f8 2208 } else {
ca18bede
YZ
2209 int mds = __choose_mds(mdsc, req);
2210 if (mds >= 0 && mds != req->r_session->s_mds) {
2211 dout("but auth changed, so resending");
e55b71f8
GF
2212 __do_request(mdsc, req);
2213 mutex_unlock(&mdsc->mutex);
2214 goto out;
2215 }
2f2dc053 2216 }
e55b71f8 2217 dout("have to return ESTALE on request %llu", req->r_tid);
2f2dc053
SW
2218 }
2219
e55b71f8 2220
2f2dc053
SW
2221 if (head->safe) {
2222 req->r_got_safe = true;
2223 __unregister_request(mdsc, req);
2f2dc053
SW
2224
2225 if (req->r_got_unsafe) {
2226 /*
2227 * We already handled the unsafe response, now do the
2228 * cleanup. No need to examine the response; the MDS
2229 * doesn't include any result info in the safe
2230 * response. And even if it did, there is nothing
2231 * useful we could do with a revised return value.
2232 */
2233 dout("got safe reply %llu, mds%d\n", tid, mds);
2234 list_del_init(&req->r_unsafe_item);
2235
2236 /* last unsafe request during umount? */
44ca18f2 2237 if (mdsc->stopping && !__get_oldest_req(mdsc))
03066f23 2238 complete_all(&mdsc->safe_umount_waiters);
2f2dc053
SW
2239 mutex_unlock(&mdsc->mutex);
2240 goto out;
2241 }
e1518c7c 2242 } else {
2f2dc053
SW
2243 req->r_got_unsafe = true;
2244 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
2245 }
2246
2247 dout("handle_reply tid %lld result %d\n", tid, result);
2248 rinfo = &req->r_reply_info;
14303d20 2249 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
2f2dc053
SW
2250 mutex_unlock(&mdsc->mutex);
2251
2252 mutex_lock(&session->s_mutex);
2253 if (err < 0) {
25933abd 2254 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
9ec7cab1 2255 ceph_msg_dump(msg);
2f2dc053
SW
2256 goto out_err;
2257 }
2258
2259 /* snap trace */
2260 if (rinfo->snapblob_len) {
2261 down_write(&mdsc->snap_rwsem);
2262 ceph_update_snap_trace(mdsc, rinfo->snapblob,
2263 rinfo->snapblob + rinfo->snapblob_len,
2264 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP);
2265 downgrade_write(&mdsc->snap_rwsem);
2266 } else {
2267 down_read(&mdsc->snap_rwsem);
2268 }
2269
2270 /* insert trace into our cache */
b4556396 2271 mutex_lock(&req->r_fill_mutex);
3d14c5d2 2272 err = ceph_fill_trace(mdsc->fsc->sb, req, req->r_session);
2f2dc053 2273 if (err == 0) {
6e8575fa 2274 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
81c6aea5 2275 req->r_op == CEPH_MDS_OP_LSSNAP))
2f2dc053 2276 ceph_readdir_prepopulate(req, req->r_session);
37151668 2277 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
2f2dc053 2278 }
b4556396 2279 mutex_unlock(&req->r_fill_mutex);
2f2dc053
SW
2280
2281 up_read(&mdsc->snap_rwsem);
2282out_err:
e1518c7c
SW
2283 mutex_lock(&mdsc->mutex);
2284 if (!req->r_aborted) {
2285 if (err) {
2286 req->r_err = err;
2287 } else {
2288 req->r_reply = msg;
2289 ceph_msg_get(msg);
2290 req->r_got_result = true;
2291 }
2f2dc053 2292 } else {
e1518c7c 2293 dout("reply arrived after request %lld was aborted\n", tid);
2f2dc053 2294 }
e1518c7c 2295 mutex_unlock(&mdsc->mutex);
2f2dc053 2296
ee6b272b 2297 ceph_add_cap_releases(mdsc, req->r_session);
2f2dc053
SW
2298 mutex_unlock(&session->s_mutex);
2299
2300 /* kick calling process */
2301 complete_request(mdsc, req);
2302out:
2303 ceph_mdsc_put_request(req);
2304 return;
2305}
2306
2307
2308
2309/*
2310 * handle mds notification that our request has been forwarded.
2311 */
2600d2dd
SW
2312static void handle_forward(struct ceph_mds_client *mdsc,
2313 struct ceph_mds_session *session,
2314 struct ceph_msg *msg)
2f2dc053
SW
2315{
2316 struct ceph_mds_request *req;
a1ea787c 2317 u64 tid = le64_to_cpu(msg->hdr.tid);
2f2dc053
SW
2318 u32 next_mds;
2319 u32 fwd_seq;
2f2dc053
SW
2320 int err = -EINVAL;
2321 void *p = msg->front.iov_base;
2322 void *end = p + msg->front.iov_len;
2f2dc053 2323
a1ea787c 2324 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
c89136ea
SW
2325 next_mds = ceph_decode_32(&p);
2326 fwd_seq = ceph_decode_32(&p);
2f2dc053
SW
2327
2328 mutex_lock(&mdsc->mutex);
2329 req = __lookup_request(mdsc, tid);
2330 if (!req) {
2a8e5e36 2331 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
2f2dc053
SW
2332 goto out; /* dup reply? */
2333 }
2334
2a8e5e36
SW
2335 if (req->r_aborted) {
2336 dout("forward tid %llu aborted, unregistering\n", tid);
2337 __unregister_request(mdsc, req);
2338 } else if (fwd_seq <= req->r_num_fwd) {
2339 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
2f2dc053
SW
2340 tid, next_mds, req->r_num_fwd, fwd_seq);
2341 } else {
2342 /* resend. forward race not possible; mds would drop */
2a8e5e36
SW
2343 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2344 BUG_ON(req->r_err);
2345 BUG_ON(req->r_got_result);
2f2dc053
SW
2346 req->r_num_fwd = fwd_seq;
2347 req->r_resend_mds = next_mds;
2348 put_request_session(req);
2349 __do_request(mdsc, req);
2350 }
2351 ceph_mdsc_put_request(req);
2352out:
2353 mutex_unlock(&mdsc->mutex);
2354 return;
2355
2356bad:
2357 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2358}
2359
2360/*
2361 * handle a mds session control message
2362 */
2363static void handle_session(struct ceph_mds_session *session,
2364 struct ceph_msg *msg)
2365{
2366 struct ceph_mds_client *mdsc = session->s_mdsc;
2367 u32 op;
2368 u64 seq;
2600d2dd 2369 int mds = session->s_mds;
2f2dc053
SW
2370 struct ceph_mds_session_head *h = msg->front.iov_base;
2371 int wake = 0;
2372
2f2dc053
SW
2373 /* decode */
2374 if (msg->front.iov_len != sizeof(*h))
2375 goto bad;
2376 op = le32_to_cpu(h->op);
2377 seq = le64_to_cpu(h->seq);
2378
2379 mutex_lock(&mdsc->mutex);
2600d2dd
SW
2380 if (op == CEPH_SESSION_CLOSE)
2381 __unregister_session(mdsc, session);
2f2dc053
SW
2382 /* FIXME: this ttl calculation is generous */
2383 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2384 mutex_unlock(&mdsc->mutex);
2385
2386 mutex_lock(&session->s_mutex);
2387
2388 dout("handle_session mds%d %s %p state %s seq %llu\n",
2389 mds, ceph_session_op_name(op), session,
2390 session_state_name(session->s_state), seq);
2391
2392 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2393 session->s_state = CEPH_MDS_SESSION_OPEN;
2394 pr_info("mds%d came back\n", session->s_mds);
2395 }
2396
2397 switch (op) {
2398 case CEPH_SESSION_OPEN:
29790f26
SW
2399 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2400 pr_info("mds%d reconnect success\n", session->s_mds);
2f2dc053
SW
2401 session->s_state = CEPH_MDS_SESSION_OPEN;
2402 renewed_caps(mdsc, session, 0);
2403 wake = 1;
2404 if (mdsc->stopping)
2405 __close_session(mdsc, session);
2406 break;
2407
2408 case CEPH_SESSION_RENEWCAPS:
2409 if (session->s_renew_seq == seq)
2410 renewed_caps(mdsc, session, 1);
2411 break;
2412
2413 case CEPH_SESSION_CLOSE:
29790f26
SW
2414 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2415 pr_info("mds%d reconnect denied\n", session->s_mds);
2f2dc053
SW
2416 remove_session_caps(session);
2417 wake = 1; /* for good measure */
f3c60c59 2418 wake_up_all(&mdsc->session_close_wq);
29790f26 2419 kick_requests(mdsc, mds);
2f2dc053
SW
2420 break;
2421
2422 case CEPH_SESSION_STALE:
2423 pr_info("mds%d caps went stale, renewing\n",
2424 session->s_mds);
d8fb02ab 2425 spin_lock(&session->s_gen_ttl_lock);
2f2dc053 2426 session->s_cap_gen++;
1ce208a6 2427 session->s_cap_ttl = jiffies - 1;
d8fb02ab 2428 spin_unlock(&session->s_gen_ttl_lock);
2f2dc053
SW
2429 send_renew_caps(mdsc, session);
2430 break;
2431
2432 case CEPH_SESSION_RECALL_STATE:
2433 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2434 break;
2435
186e4f7a
YZ
2436 case CEPH_SESSION_FLUSHMSG:
2437 send_flushmsg_ack(mdsc, session, seq);
2438 break;
2439
2f2dc053
SW
2440 default:
2441 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2442 WARN_ON(1);
2443 }
2444
2445 mutex_unlock(&session->s_mutex);
2446 if (wake) {
2447 mutex_lock(&mdsc->mutex);
2448 __wake_requests(mdsc, &session->s_waiting);
2449 mutex_unlock(&mdsc->mutex);
2450 }
2451 return;
2452
2453bad:
2454 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2455 (int)msg->front.iov_len);
9ec7cab1 2456 ceph_msg_dump(msg);
2f2dc053
SW
2457 return;
2458}
2459
2460
2461/*
2462 * called under session->mutex.
2463 */
2464static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2465 struct ceph_mds_session *session)
2466{
2467 struct ceph_mds_request *req, *nreq;
2468 int err;
2469
2470 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2471
2472 mutex_lock(&mdsc->mutex);
2473 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
2474 err = __prepare_send_request(mdsc, req, session->s_mds);
2475 if (!err) {
2476 ceph_msg_get(req->r_request);
2477 ceph_con_send(&session->s_con, req->r_request);
2478 }
2479 }
2480 mutex_unlock(&mdsc->mutex);
2481}
2482
2483/*
2484 * Encode information about a cap for a reconnect with the MDS.
2485 */
2f2dc053
SW
2486static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2487 void *arg)
2488{
20cb34ae
SW
2489 union {
2490 struct ceph_mds_cap_reconnect v2;
2491 struct ceph_mds_cap_reconnect_v1 v1;
2492 } rec;
2493 size_t reclen;
2f2dc053 2494 struct ceph_inode_info *ci;
20cb34ae
SW
2495 struct ceph_reconnect_state *recon_state = arg;
2496 struct ceph_pagelist *pagelist = recon_state->pagelist;
2f2dc053
SW
2497 char *path;
2498 int pathlen, err;
2499 u64 pathbase;
2500 struct dentry *dentry;
2501
2502 ci = cap->ci;
2503
2504 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2505 inode, ceph_vinop(inode), cap, cap->cap_id,
2506 ceph_cap_string(cap->issued));
93cea5be
SW
2507 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2508 if (err)
2509 return err;
2f2dc053
SW
2510
2511 dentry = d_find_alias(inode);
2512 if (dentry) {
2513 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2514 if (IS_ERR(path)) {
2515 err = PTR_ERR(path);
e072f8aa 2516 goto out_dput;
2f2dc053
SW
2517 }
2518 } else {
2519 path = NULL;
2520 pathlen = 0;
2521 }
93cea5be
SW
2522 err = ceph_pagelist_encode_string(pagelist, path, pathlen);
2523 if (err)
e072f8aa 2524 goto out_free;
2f2dc053 2525
be655596 2526 spin_lock(&ci->i_ceph_lock);
2f2dc053
SW
2527 cap->seq = 0; /* reset cap seq */
2528 cap->issue_seq = 0; /* and issue_seq */
667ca05c 2529 cap->mseq = 0; /* and migrate_seq */
99a9c273 2530 cap->cap_gen = cap->session->s_cap_gen;
20cb34ae
SW
2531
2532 if (recon_state->flock) {
2533 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
2534 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2535 rec.v2.issued = cpu_to_le32(cap->issued);
2536 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2537 rec.v2.pathbase = cpu_to_le64(pathbase);
2538 rec.v2.flock_len = 0;
2539 reclen = sizeof(rec.v2);
2540 } else {
2541 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
2542 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2543 rec.v1.issued = cpu_to_le32(cap->issued);
2544 rec.v1.size = cpu_to_le64(inode->i_size);
2545 ceph_encode_timespec(&rec.v1.mtime, &inode->i_mtime);
2546 ceph_encode_timespec(&rec.v1.atime, &inode->i_atime);
2547 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2548 rec.v1.pathbase = cpu_to_le64(pathbase);
2549 reclen = sizeof(rec.v1);
2550 }
be655596 2551 spin_unlock(&ci->i_ceph_lock);
2f2dc053 2552
40819f6f
GF
2553 if (recon_state->flock) {
2554 int num_fcntl_locks, num_flock_locks;
39be95e9
JS
2555 struct ceph_filelock *flocks;
2556
2557encode_again:
1c8c601a 2558 spin_lock(&inode->i_lock);
39be95e9 2559 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
1c8c601a 2560 spin_unlock(&inode->i_lock);
39be95e9
JS
2561 flocks = kmalloc((num_fcntl_locks+num_flock_locks) *
2562 sizeof(struct ceph_filelock), GFP_NOFS);
2563 if (!flocks) {
2564 err = -ENOMEM;
2565 goto out_free;
2566 }
1c8c601a 2567 spin_lock(&inode->i_lock);
39be95e9
JS
2568 err = ceph_encode_locks_to_buffer(inode, flocks,
2569 num_fcntl_locks,
2570 num_flock_locks);
1c8c601a 2571 spin_unlock(&inode->i_lock);
39be95e9
JS
2572 if (err) {
2573 kfree(flocks);
2574 if (err == -ENOSPC)
2575 goto encode_again;
2576 goto out_free;
2577 }
2578 /*
2579 * number of encoded locks is stable, so copy to pagelist
2580 */
2581 rec.v2.flock_len = cpu_to_le32(2*sizeof(u32) +
2582 (num_fcntl_locks+num_flock_locks) *
2583 sizeof(struct ceph_filelock));
2584 err = ceph_pagelist_append(pagelist, &rec, reclen);
2585 if (!err)
2586 err = ceph_locks_to_pagelist(flocks, pagelist,
2587 num_fcntl_locks,
2588 num_flock_locks);
2589 kfree(flocks);
3612abbd
SW
2590 } else {
2591 err = ceph_pagelist_append(pagelist, &rec, reclen);
40819f6f 2592 }
44c99757
YZ
2593
2594 recon_state->nr_caps++;
e072f8aa 2595out_free:
2f2dc053 2596 kfree(path);
e072f8aa 2597out_dput:
2f2dc053 2598 dput(dentry);
93cea5be 2599 return err;
2f2dc053
SW
2600}
2601
2602
2603/*
2604 * If an MDS fails and recovers, clients need to reconnect in order to
2605 * reestablish shared state. This includes all caps issued through
2606 * this session _and_ the snap_realm hierarchy. Because it's not
2607 * clear which snap realms the mds cares about, we send everything we
2608 * know about.. that ensures we'll then get any new info the
2609 * recovering MDS might have.
2610 *
2611 * This is a relatively heavyweight operation, but it's rare.
2612 *
2613 * called with mdsc->mutex held.
2614 */
34b6c855
SW
2615static void send_mds_reconnect(struct ceph_mds_client *mdsc,
2616 struct ceph_mds_session *session)
2f2dc053 2617{
2f2dc053 2618 struct ceph_msg *reply;
a105f00c 2619 struct rb_node *p;
34b6c855 2620 int mds = session->s_mds;
9abf82b8 2621 int err = -ENOMEM;
44c99757 2622 int s_nr_caps;
93cea5be 2623 struct ceph_pagelist *pagelist;
20cb34ae 2624 struct ceph_reconnect_state recon_state;
2f2dc053 2625
34b6c855 2626 pr_info("mds%d reconnect start\n", mds);
2f2dc053 2627
93cea5be
SW
2628 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
2629 if (!pagelist)
2630 goto fail_nopagelist;
2631 ceph_pagelist_init(pagelist);
2632
b61c2763 2633 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS, false);
a79832f2 2634 if (!reply)
93cea5be 2635 goto fail_nomsg;
93cea5be 2636
34b6c855
SW
2637 mutex_lock(&session->s_mutex);
2638 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
2639 session->s_seq = 0;
2f2dc053 2640
a53aab64 2641 ceph_con_close(&session->s_con);
34b6c855 2642 ceph_con_open(&session->s_con,
b7a9e5dd 2643 CEPH_ENTITY_TYPE_MDS, mds,
34b6c855 2644 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
2f2dc053 2645
34b6c855
SW
2646 /* replay unsafe requests */
2647 replay_unsafe_requests(mdsc, session);
2f2dc053
SW
2648
2649 down_read(&mdsc->snap_rwsem);
2650
2f2dc053
SW
2651 dout("session %p state %s\n", session,
2652 session_state_name(session->s_state));
2653
99a9c273
YZ
2654 spin_lock(&session->s_gen_ttl_lock);
2655 session->s_cap_gen++;
2656 spin_unlock(&session->s_gen_ttl_lock);
2657
2658 spin_lock(&session->s_cap_lock);
2659 /*
2660 * notify __ceph_remove_cap() that we are composing cap reconnect.
2661 * If a cap get released before being added to the cap reconnect,
2662 * __ceph_remove_cap() should skip queuing cap release.
2663 */
2664 session->s_cap_reconnect = 1;
e01a5946
SW
2665 /* drop old cap expires; we're about to reestablish that state */
2666 discard_cap_releases(mdsc, session);
99a9c273 2667 spin_unlock(&session->s_cap_lock);
e01a5946 2668
2f2dc053 2669 /* traverse this session's caps */
44c99757
YZ
2670 s_nr_caps = session->s_nr_caps;
2671 err = ceph_pagelist_encode_32(pagelist, s_nr_caps);
93cea5be
SW
2672 if (err)
2673 goto fail;
20cb34ae 2674
44c99757 2675 recon_state.nr_caps = 0;
20cb34ae
SW
2676 recon_state.pagelist = pagelist;
2677 recon_state.flock = session->s_con.peer_features & CEPH_FEATURE_FLOCK;
2678 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
2f2dc053 2679 if (err < 0)
9abf82b8 2680 goto fail;
2f2dc053 2681
99a9c273
YZ
2682 spin_lock(&session->s_cap_lock);
2683 session->s_cap_reconnect = 0;
2684 spin_unlock(&session->s_cap_lock);
2685
2f2dc053
SW
2686 /*
2687 * snaprealms. we provide mds with the ino, seq (version), and
2688 * parent for all of our realms. If the mds has any newer info,
2689 * it will tell us.
2690 */
a105f00c
SW
2691 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
2692 struct ceph_snap_realm *realm =
2693 rb_entry(p, struct ceph_snap_realm, node);
93cea5be 2694 struct ceph_mds_snaprealm_reconnect sr_rec;
2f2dc053
SW
2695
2696 dout(" adding snap realm %llx seq %lld parent %llx\n",
2697 realm->ino, realm->seq, realm->parent_ino);
93cea5be
SW
2698 sr_rec.ino = cpu_to_le64(realm->ino);
2699 sr_rec.seq = cpu_to_le64(realm->seq);
2700 sr_rec.parent = cpu_to_le64(realm->parent_ino);
2701 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
2702 if (err)
2703 goto fail;
2f2dc053 2704 }
2f2dc053 2705
20cb34ae
SW
2706 if (recon_state.flock)
2707 reply->hdr.version = cpu_to_le16(2);
44c99757
YZ
2708
2709 /* raced with cap release? */
2710 if (s_nr_caps != recon_state.nr_caps) {
2711 struct page *page = list_first_entry(&pagelist->head,
2712 struct page, lru);
2713 __le32 *addr = kmap_atomic(page);
2714 *addr = cpu_to_le32(recon_state.nr_caps);
2715 kunmap_atomic(addr);
ebf18f47 2716 }
44c99757
YZ
2717
2718 reply->hdr.data_len = cpu_to_le32(pagelist->length);
2719 ceph_msg_data_add_pagelist(reply, pagelist);
2f2dc053
SW
2720 ceph_con_send(&session->s_con, reply);
2721
9abf82b8
SW
2722 mutex_unlock(&session->s_mutex);
2723
2724 mutex_lock(&mdsc->mutex);
2725 __wake_requests(mdsc, &session->s_waiting);
2726 mutex_unlock(&mdsc->mutex);
2727
2f2dc053 2728 up_read(&mdsc->snap_rwsem);
2f2dc053
SW
2729 return;
2730
93cea5be 2731fail:
2f2dc053 2732 ceph_msg_put(reply);
9abf82b8
SW
2733 up_read(&mdsc->snap_rwsem);
2734 mutex_unlock(&session->s_mutex);
93cea5be
SW
2735fail_nomsg:
2736 ceph_pagelist_release(pagelist);
2737 kfree(pagelist);
2738fail_nopagelist:
9abf82b8 2739 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
9abf82b8 2740 return;
2f2dc053
SW
2741}
2742
2743
2744/*
2745 * compare old and new mdsmaps, kicking requests
2746 * and closing out old connections as necessary
2747 *
2748 * called under mdsc->mutex.
2749 */
2750static void check_new_map(struct ceph_mds_client *mdsc,
2751 struct ceph_mdsmap *newmap,
2752 struct ceph_mdsmap *oldmap)
2753{
2754 int i;
2755 int oldstate, newstate;
2756 struct ceph_mds_session *s;
2757
2758 dout("check_new_map new %u old %u\n",
2759 newmap->m_epoch, oldmap->m_epoch);
2760
2761 for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
2762 if (mdsc->sessions[i] == NULL)
2763 continue;
2764 s = mdsc->sessions[i];
2765 oldstate = ceph_mdsmap_get_state(oldmap, i);
2766 newstate = ceph_mdsmap_get_state(newmap, i);
2767
0deb01c9 2768 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
2f2dc053 2769 i, ceph_mds_state_name(oldstate),
0deb01c9 2770 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
2f2dc053 2771 ceph_mds_state_name(newstate),
0deb01c9 2772 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
2f2dc053
SW
2773 session_state_name(s->s_state));
2774
3e8f43a0
YZ
2775 if (i >= newmap->m_max_mds ||
2776 memcmp(ceph_mdsmap_get_addr(oldmap, i),
2f2dc053
SW
2777 ceph_mdsmap_get_addr(newmap, i),
2778 sizeof(struct ceph_entity_addr))) {
2779 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
2780 /* the session never opened, just close it
2781 * out now */
2782 __wake_requests(mdsc, &s->s_waiting);
2600d2dd 2783 __unregister_session(mdsc, s);
2f2dc053
SW
2784 } else {
2785 /* just close it */
2786 mutex_unlock(&mdsc->mutex);
2787 mutex_lock(&s->s_mutex);
2788 mutex_lock(&mdsc->mutex);
2789 ceph_con_close(&s->s_con);
2790 mutex_unlock(&s->s_mutex);
2791 s->s_state = CEPH_MDS_SESSION_RESTARTING;
2792 }
2793
2794 /* kick any requests waiting on the recovering mds */
29790f26 2795 kick_requests(mdsc, i);
2f2dc053
SW
2796 } else if (oldstate == newstate) {
2797 continue; /* nothing new with this mds */
2798 }
2799
2800 /*
2801 * send reconnect?
2802 */
2803 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
34b6c855
SW
2804 newstate >= CEPH_MDS_STATE_RECONNECT) {
2805 mutex_unlock(&mdsc->mutex);
2806 send_mds_reconnect(mdsc, s);
2807 mutex_lock(&mdsc->mutex);
2808 }
2f2dc053
SW
2809
2810 /*
29790f26 2811 * kick request on any mds that has gone active.
2f2dc053
SW
2812 */
2813 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
2814 newstate >= CEPH_MDS_STATE_ACTIVE) {
29790f26
SW
2815 if (oldstate != CEPH_MDS_STATE_CREATING &&
2816 oldstate != CEPH_MDS_STATE_STARTING)
2817 pr_info("mds%d recovery completed\n", s->s_mds);
2818 kick_requests(mdsc, i);
2f2dc053 2819 ceph_kick_flushing_caps(mdsc, s);
0dc2570f 2820 wake_up_session_caps(s, 1);
2f2dc053
SW
2821 }
2822 }
cb170a22
SW
2823
2824 for (i = 0; i < newmap->m_max_mds && i < mdsc->max_sessions; i++) {
2825 s = mdsc->sessions[i];
2826 if (!s)
2827 continue;
2828 if (!ceph_mdsmap_is_laggy(newmap, i))
2829 continue;
2830 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2831 s->s_state == CEPH_MDS_SESSION_HUNG ||
2832 s->s_state == CEPH_MDS_SESSION_CLOSING) {
2833 dout(" connecting to export targets of laggy mds%d\n",
2834 i);
2835 __open_export_target_sessions(mdsc, s);
2836 }
2837 }
2f2dc053
SW
2838}
2839
2840
2841
2842/*
2843 * leases
2844 */
2845
2846/*
2847 * caller must hold session s_mutex, dentry->d_lock
2848 */
2849void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
2850{
2851 struct ceph_dentry_info *di = ceph_dentry(dentry);
2852
2853 ceph_put_mds_session(di->lease_session);
2854 di->lease_session = NULL;
2855}
2856
2600d2dd
SW
2857static void handle_lease(struct ceph_mds_client *mdsc,
2858 struct ceph_mds_session *session,
2859 struct ceph_msg *msg)
2f2dc053 2860{
3d14c5d2 2861 struct super_block *sb = mdsc->fsc->sb;
2f2dc053 2862 struct inode *inode;
2f2dc053
SW
2863 struct dentry *parent, *dentry;
2864 struct ceph_dentry_info *di;
2600d2dd 2865 int mds = session->s_mds;
2f2dc053 2866 struct ceph_mds_lease *h = msg->front.iov_base;
1e5ea23d 2867 u32 seq;
2f2dc053 2868 struct ceph_vino vino;
2f2dc053
SW
2869 struct qstr dname;
2870 int release = 0;
2871
2f2dc053
SW
2872 dout("handle_lease from mds%d\n", mds);
2873
2874 /* decode */
2875 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
2876 goto bad;
2877 vino.ino = le64_to_cpu(h->ino);
2878 vino.snap = CEPH_NOSNAP;
1e5ea23d 2879 seq = le32_to_cpu(h->seq);
2f2dc053
SW
2880 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
2881 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
2882 if (dname.len != get_unaligned_le32(h+1))
2883 goto bad;
2884
2f2dc053
SW
2885 mutex_lock(&session->s_mutex);
2886 session->s_seq++;
2887
2888 /* lookup inode */
2889 inode = ceph_find_inode(sb, vino);
2f90b852
SW
2890 dout("handle_lease %s, ino %llx %p %.*s\n",
2891 ceph_lease_op_name(h->action), vino.ino, inode,
1e5ea23d 2892 dname.len, dname.name);
2f2dc053
SW
2893 if (inode == NULL) {
2894 dout("handle_lease no inode %llx\n", vino.ino);
2895 goto release;
2896 }
2f2dc053
SW
2897
2898 /* dentry */
2899 parent = d_find_alias(inode);
2900 if (!parent) {
2901 dout("no parent dentry on inode %p\n", inode);
2902 WARN_ON(1);
2903 goto release; /* hrm... */
2904 }
2905 dname.hash = full_name_hash(dname.name, dname.len);
2906 dentry = d_lookup(parent, &dname);
2907 dput(parent);
2908 if (!dentry)
2909 goto release;
2910
2911 spin_lock(&dentry->d_lock);
2912 di = ceph_dentry(dentry);
2913 switch (h->action) {
2914 case CEPH_MDS_LEASE_REVOKE:
3d8eb7a9 2915 if (di->lease_session == session) {
1e5ea23d
SW
2916 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
2917 h->seq = cpu_to_le32(di->lease_seq);
2f2dc053
SW
2918 __ceph_mdsc_drop_dentry_lease(dentry);
2919 }
2920 release = 1;
2921 break;
2922
2923 case CEPH_MDS_LEASE_RENEW:
3d8eb7a9 2924 if (di->lease_session == session &&
2f2dc053
SW
2925 di->lease_gen == session->s_cap_gen &&
2926 di->lease_renew_from &&
2927 di->lease_renew_after == 0) {
2928 unsigned long duration =
2929 le32_to_cpu(h->duration_ms) * HZ / 1000;
2930
1e5ea23d 2931 di->lease_seq = seq;
2f2dc053
SW
2932 dentry->d_time = di->lease_renew_from + duration;
2933 di->lease_renew_after = di->lease_renew_from +
2934 (duration >> 1);
2935 di->lease_renew_from = 0;
2936 }
2937 break;
2938 }
2939 spin_unlock(&dentry->d_lock);
2940 dput(dentry);
2941
2942 if (!release)
2943 goto out;
2944
2945release:
2946 /* let's just reuse the same message */
2947 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
2948 ceph_msg_get(msg);
2949 ceph_con_send(&session->s_con, msg);
2950
2951out:
2952 iput(inode);
2953 mutex_unlock(&session->s_mutex);
2f2dc053
SW
2954 return;
2955
2956bad:
2957 pr_err("corrupt lease message\n");
9ec7cab1 2958 ceph_msg_dump(msg);
2f2dc053
SW
2959}
2960
2961void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
2962 struct inode *inode,
2963 struct dentry *dentry, char action,
2964 u32 seq)
2965{
2966 struct ceph_msg *msg;
2967 struct ceph_mds_lease *lease;
2968 int len = sizeof(*lease) + sizeof(u32);
2969 int dnamelen = 0;
2970
2971 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
2972 inode, dentry, ceph_lease_op_name(action), session->s_mds);
2973 dnamelen = dentry->d_name.len;
2974 len += dnamelen;
2975
b61c2763 2976 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
a79832f2 2977 if (!msg)
2f2dc053
SW
2978 return;
2979 lease = msg->front.iov_base;
2980 lease->action = action;
2f2dc053
SW
2981 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
2982 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
2983 lease->seq = cpu_to_le32(seq);
2984 put_unaligned_le32(dnamelen, lease + 1);
2985 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
2986
2987 /*
2988 * if this is a preemptive lease RELEASE, no need to
2989 * flush request stream, since the actual request will
2990 * soon follow.
2991 */
2992 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
2993
2994 ceph_con_send(&session->s_con, msg);
2995}
2996
2997/*
2998 * Preemptively release a lease we expect to invalidate anyway.
2999 * Pass @inode always, @dentry is optional.
3000 */
3001void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
2f90b852 3002 struct dentry *dentry)
2f2dc053
SW
3003{
3004 struct ceph_dentry_info *di;
3005 struct ceph_mds_session *session;
3006 u32 seq;
3007
3008 BUG_ON(inode == NULL);
3009 BUG_ON(dentry == NULL);
2f2dc053
SW
3010
3011 /* is dentry lease valid? */
3012 spin_lock(&dentry->d_lock);
3013 di = ceph_dentry(dentry);
3014 if (!di || !di->lease_session ||
3015 di->lease_session->s_mds < 0 ||
3016 di->lease_gen != di->lease_session->s_cap_gen ||
3017 !time_before(jiffies, dentry->d_time)) {
3018 dout("lease_release inode %p dentry %p -- "
2f90b852
SW
3019 "no lease\n",
3020 inode, dentry);
2f2dc053
SW
3021 spin_unlock(&dentry->d_lock);
3022 return;
3023 }
3024
3025 /* we do have a lease on this dentry; note mds and seq */
3026 session = ceph_get_mds_session(di->lease_session);
3027 seq = di->lease_seq;
3028 __ceph_mdsc_drop_dentry_lease(dentry);
3029 spin_unlock(&dentry->d_lock);
3030
2f90b852
SW
3031 dout("lease_release inode %p dentry %p to mds%d\n",
3032 inode, dentry, session->s_mds);
2f2dc053
SW
3033 ceph_mdsc_lease_send_msg(session, inode, dentry,
3034 CEPH_MDS_LEASE_RELEASE, seq);
3035 ceph_put_mds_session(session);
3036}
3037
3038/*
3039 * drop all leases (and dentry refs) in preparation for umount
3040 */
3041static void drop_leases(struct ceph_mds_client *mdsc)
3042{
3043 int i;
3044
3045 dout("drop_leases\n");
3046 mutex_lock(&mdsc->mutex);
3047 for (i = 0; i < mdsc->max_sessions; i++) {
3048 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
3049 if (!s)
3050 continue;
3051 mutex_unlock(&mdsc->mutex);
3052 mutex_lock(&s->s_mutex);
3053 mutex_unlock(&s->s_mutex);
3054 ceph_put_mds_session(s);
3055 mutex_lock(&mdsc->mutex);
3056 }
3057 mutex_unlock(&mdsc->mutex);
3058}
3059
3060
3061
3062/*
3063 * delayed work -- periodically trim expired leases, renew caps with mds
3064 */
3065static void schedule_delayed(struct ceph_mds_client *mdsc)
3066{
3067 int delay = 5;
3068 unsigned hz = round_jiffies_relative(HZ * delay);
3069 schedule_delayed_work(&mdsc->delayed_work, hz);
3070}
3071
3072static void delayed_work(struct work_struct *work)
3073{
3074 int i;
3075 struct ceph_mds_client *mdsc =
3076 container_of(work, struct ceph_mds_client, delayed_work.work);
3077 int renew_interval;
3078 int renew_caps;
3079
3080 dout("mdsc delayed_work\n");
afcdaea3 3081 ceph_check_delayed_caps(mdsc);
2f2dc053
SW
3082
3083 mutex_lock(&mdsc->mutex);
3084 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
3085 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
3086 mdsc->last_renew_caps);
3087 if (renew_caps)
3088 mdsc->last_renew_caps = jiffies;
3089
3090 for (i = 0; i < mdsc->max_sessions; i++) {
3091 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
3092 if (s == NULL)
3093 continue;
3094 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
3095 dout("resending session close request for mds%d\n",
3096 s->s_mds);
3097 request_close_session(mdsc, s);
3098 ceph_put_mds_session(s);
3099 continue;
3100 }
3101 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
3102 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
3103 s->s_state = CEPH_MDS_SESSION_HUNG;
3104 pr_info("mds%d hung\n", s->s_mds);
3105 }
3106 }
3107 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
3108 /* this mds is failed or recovering, just wait */
3109 ceph_put_mds_session(s);
3110 continue;
3111 }
3112 mutex_unlock(&mdsc->mutex);
3113
3114 mutex_lock(&s->s_mutex);
3115 if (renew_caps)
3116 send_renew_caps(mdsc, s);
3117 else
3118 ceph_con_keepalive(&s->s_con);
ee6b272b 3119 ceph_add_cap_releases(mdsc, s);
aab53dd9
SW
3120 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3121 s->s_state == CEPH_MDS_SESSION_HUNG)
3d7ded4d 3122 ceph_send_cap_releases(mdsc, s);
2f2dc053
SW
3123 mutex_unlock(&s->s_mutex);
3124 ceph_put_mds_session(s);
3125
3126 mutex_lock(&mdsc->mutex);
3127 }
3128 mutex_unlock(&mdsc->mutex);
3129
3130 schedule_delayed(mdsc);
3131}
3132
3d14c5d2 3133int ceph_mdsc_init(struct ceph_fs_client *fsc)
2f2dc053 3134
2f2dc053 3135{
3d14c5d2
YS
3136 struct ceph_mds_client *mdsc;
3137
3138 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
3139 if (!mdsc)
3140 return -ENOMEM;
3141 mdsc->fsc = fsc;
3142 fsc->mdsc = mdsc;
2f2dc053
SW
3143 mutex_init(&mdsc->mutex);
3144 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
fb3101b6 3145 if (mdsc->mdsmap == NULL) {
3146 kfree(mdsc);
2d06eeb8 3147 return -ENOMEM;
fb3101b6 3148 }
2d06eeb8 3149
2f2dc053 3150 init_completion(&mdsc->safe_umount_waiters);
f3c60c59 3151 init_waitqueue_head(&mdsc->session_close_wq);
2f2dc053
SW
3152 INIT_LIST_HEAD(&mdsc->waiting_for_map);
3153 mdsc->sessions = NULL;
3154 mdsc->max_sessions = 0;
3155 mdsc->stopping = 0;
3156 init_rwsem(&mdsc->snap_rwsem);
a105f00c 3157 mdsc->snap_realms = RB_ROOT;
2f2dc053
SW
3158 INIT_LIST_HEAD(&mdsc->snap_empty);
3159 spin_lock_init(&mdsc->snap_empty_lock);
3160 mdsc->last_tid = 0;
44ca18f2 3161 mdsc->request_tree = RB_ROOT;
2f2dc053
SW
3162 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
3163 mdsc->last_renew_caps = jiffies;
3164 INIT_LIST_HEAD(&mdsc->cap_delay_list);
3165 spin_lock_init(&mdsc->cap_delay_lock);
3166 INIT_LIST_HEAD(&mdsc->snap_flush_list);
3167 spin_lock_init(&mdsc->snap_flush_lock);
3168 mdsc->cap_flush_seq = 0;
3169 INIT_LIST_HEAD(&mdsc->cap_dirty);
db354052 3170 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
2f2dc053
SW
3171 mdsc->num_cap_flushing = 0;
3172 spin_lock_init(&mdsc->cap_dirty_lock);
3173 init_waitqueue_head(&mdsc->cap_flushing_wq);
3174 spin_lock_init(&mdsc->dentry_lru_lock);
3175 INIT_LIST_HEAD(&mdsc->dentry_lru);
2d06eeb8 3176
37151668 3177 ceph_caps_init(mdsc);
3d14c5d2 3178 ceph_adjust_min_caps(mdsc, fsc->min_caps);
37151668 3179
5f44f142 3180 return 0;
2f2dc053
SW
3181}
3182
3183/*
3184 * Wait for safe replies on open mds requests. If we time out, drop
3185 * all requests from the tree to avoid dangling dentry refs.
3186 */
3187static void wait_requests(struct ceph_mds_client *mdsc)
3188{
3189 struct ceph_mds_request *req;
3d14c5d2 3190 struct ceph_fs_client *fsc = mdsc->fsc;
2f2dc053
SW
3191
3192 mutex_lock(&mdsc->mutex);
44ca18f2 3193 if (__get_oldest_req(mdsc)) {
2f2dc053 3194 mutex_unlock(&mdsc->mutex);
44ca18f2 3195
2f2dc053
SW
3196 dout("wait_requests waiting for requests\n");
3197 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
3d14c5d2 3198 fsc->client->options->mount_timeout * HZ);
2f2dc053
SW
3199
3200 /* tear down remaining requests */
44ca18f2
SW
3201 mutex_lock(&mdsc->mutex);
3202 while ((req = __get_oldest_req(mdsc))) {
2f2dc053
SW
3203 dout("wait_requests timed out on tid %llu\n",
3204 req->r_tid);
44ca18f2 3205 __unregister_request(mdsc, req);
2f2dc053
SW
3206 }
3207 }
3208 mutex_unlock(&mdsc->mutex);
3209 dout("wait_requests done\n");
3210}
3211
3212/*
3213 * called before mount is ro, and before dentries are torn down.
3214 * (hmm, does this still race with new lookups?)
3215 */
3216void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
3217{
3218 dout("pre_umount\n");
3219 mdsc->stopping = 1;
3220
3221 drop_leases(mdsc);
afcdaea3 3222 ceph_flush_dirty_caps(mdsc);
2f2dc053 3223 wait_requests(mdsc);
17c688c3
SW
3224
3225 /*
3226 * wait for reply handlers to drop their request refs and
3227 * their inode/dcache refs
3228 */
3229 ceph_msgr_flush();
2f2dc053
SW
3230}
3231
3232/*
3233 * wait for all write mds requests to flush.
3234 */
3235static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
3236{
80fc7314 3237 struct ceph_mds_request *req = NULL, *nextreq;
44ca18f2 3238 struct rb_node *n;
2f2dc053
SW
3239
3240 mutex_lock(&mdsc->mutex);
3241 dout("wait_unsafe_requests want %lld\n", want_tid);
80fc7314 3242restart:
44ca18f2
SW
3243 req = __get_oldest_req(mdsc);
3244 while (req && req->r_tid <= want_tid) {
80fc7314
SW
3245 /* find next request */
3246 n = rb_next(&req->r_node);
3247 if (n)
3248 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
3249 else
3250 nextreq = NULL;
44ca18f2
SW
3251 if ((req->r_op & CEPH_MDS_OP_WRITE)) {
3252 /* write op */
3253 ceph_mdsc_get_request(req);
80fc7314
SW
3254 if (nextreq)
3255 ceph_mdsc_get_request(nextreq);
44ca18f2
SW
3256 mutex_unlock(&mdsc->mutex);
3257 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3258 req->r_tid, want_tid);
3259 wait_for_completion(&req->r_safe_completion);
3260 mutex_lock(&mdsc->mutex);
44ca18f2 3261 ceph_mdsc_put_request(req);
80fc7314
SW
3262 if (!nextreq)
3263 break; /* next dne before, so we're done! */
3264 if (RB_EMPTY_NODE(&nextreq->r_node)) {
3265 /* next request was removed from tree */
3266 ceph_mdsc_put_request(nextreq);
3267 goto restart;
3268 }
3269 ceph_mdsc_put_request(nextreq); /* won't go away */
44ca18f2 3270 }
80fc7314 3271 req = nextreq;
2f2dc053
SW
3272 }
3273 mutex_unlock(&mdsc->mutex);
3274 dout("wait_unsafe_requests done\n");
3275}
3276
3277void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
3278{
3279 u64 want_tid, want_flush;
3280
3d14c5d2 3281 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
56b7cf95
SW
3282 return;
3283
2f2dc053
SW
3284 dout("sync\n");
3285 mutex_lock(&mdsc->mutex);
3286 want_tid = mdsc->last_tid;
3287 want_flush = mdsc->cap_flush_seq;
3288 mutex_unlock(&mdsc->mutex);
3289 dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
3290
afcdaea3 3291 ceph_flush_dirty_caps(mdsc);
2f2dc053
SW
3292
3293 wait_unsafe_requests(mdsc, want_tid);
3294 wait_event(mdsc->cap_flushing_wq, check_cap_flush(mdsc, want_flush));
3295}
3296
f3c60c59
SW
3297/*
3298 * true if all sessions are closed, or we force unmount
3299 */
7fd7d101 3300static bool done_closing_sessions(struct ceph_mds_client *mdsc)
f3c60c59
SW
3301{
3302 int i, n = 0;
3303
3d14c5d2 3304 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
f3c60c59
SW
3305 return true;
3306
3307 mutex_lock(&mdsc->mutex);
3308 for (i = 0; i < mdsc->max_sessions; i++)
3309 if (mdsc->sessions[i])
3310 n++;
3311 mutex_unlock(&mdsc->mutex);
3312 return n == 0;
3313}
2f2dc053
SW
3314
3315/*
3316 * called after sb is ro.
3317 */
3318void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
3319{
3320 struct ceph_mds_session *session;
3321 int i;
3d14c5d2
YS
3322 struct ceph_fs_client *fsc = mdsc->fsc;
3323 unsigned long timeout = fsc->client->options->mount_timeout * HZ;
2f2dc053
SW
3324
3325 dout("close_sessions\n");
3326
2f2dc053 3327 /* close sessions */
f3c60c59
SW
3328 mutex_lock(&mdsc->mutex);
3329 for (i = 0; i < mdsc->max_sessions; i++) {
3330 session = __ceph_lookup_mds_session(mdsc, i);
3331 if (!session)
3332 continue;
2f2dc053 3333 mutex_unlock(&mdsc->mutex);
f3c60c59
SW
3334 mutex_lock(&session->s_mutex);
3335 __close_session(mdsc, session);
3336 mutex_unlock(&session->s_mutex);
3337 ceph_put_mds_session(session);
2f2dc053
SW
3338 mutex_lock(&mdsc->mutex);
3339 }
f3c60c59
SW
3340 mutex_unlock(&mdsc->mutex);
3341
3342 dout("waiting for sessions to close\n");
3343 wait_event_timeout(mdsc->session_close_wq, done_closing_sessions(mdsc),
3344 timeout);
2f2dc053
SW
3345
3346 /* tear down remaining sessions */
f3c60c59 3347 mutex_lock(&mdsc->mutex);
2f2dc053
SW
3348 for (i = 0; i < mdsc->max_sessions; i++) {
3349 if (mdsc->sessions[i]) {
3350 session = get_session(mdsc->sessions[i]);
2600d2dd 3351 __unregister_session(mdsc, session);
2f2dc053
SW
3352 mutex_unlock(&mdsc->mutex);
3353 mutex_lock(&session->s_mutex);
3354 remove_session_caps(session);
3355 mutex_unlock(&session->s_mutex);
3356 ceph_put_mds_session(session);
3357 mutex_lock(&mdsc->mutex);
3358 }
3359 }
2f2dc053 3360 WARN_ON(!list_empty(&mdsc->cap_delay_list));
2f2dc053
SW
3361 mutex_unlock(&mdsc->mutex);
3362
3363 ceph_cleanup_empty_realms(mdsc);
3364
3365 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3366
3367 dout("stopped\n");
3368}
3369
3d14c5d2 3370static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
2f2dc053
SW
3371{
3372 dout("stop\n");
3373 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3374 if (mdsc->mdsmap)
3375 ceph_mdsmap_destroy(mdsc->mdsmap);
3376 kfree(mdsc->sessions);
37151668 3377 ceph_caps_finalize(mdsc);
2f2dc053
SW
3378}
3379
3d14c5d2
YS
3380void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
3381{
3382 struct ceph_mds_client *mdsc = fsc->mdsc;
3383
ef550f6f 3384 dout("mdsc_destroy %p\n", mdsc);
3d14c5d2 3385 ceph_mdsc_stop(mdsc);
ef550f6f
SW
3386
3387 /* flush out any connection work with references to us */
3388 ceph_msgr_flush();
3389
3d14c5d2
YS
3390 fsc->mdsc = NULL;
3391 kfree(mdsc);
ef550f6f 3392 dout("mdsc_destroy %p done\n", mdsc);
3d14c5d2
YS
3393}
3394
2f2dc053
SW
3395
3396/*
3397 * handle mds map update.
3398 */
3399void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3400{
3401 u32 epoch;
3402 u32 maplen;
3403 void *p = msg->front.iov_base;
3404 void *end = p + msg->front.iov_len;
3405 struct ceph_mdsmap *newmap, *oldmap;
3406 struct ceph_fsid fsid;
3407 int err = -EINVAL;
3408
3409 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
3410 ceph_decode_copy(&p, &fsid, sizeof(fsid));
3d14c5d2 3411 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
0743304d 3412 return;
c89136ea
SW
3413 epoch = ceph_decode_32(&p);
3414 maplen = ceph_decode_32(&p);
2f2dc053
SW
3415 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
3416
3417 /* do we need it? */
3d14c5d2 3418 ceph_monc_got_mdsmap(&mdsc->fsc->client->monc, epoch);
2f2dc053
SW
3419 mutex_lock(&mdsc->mutex);
3420 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
3421 dout("handle_map epoch %u <= our %u\n",
3422 epoch, mdsc->mdsmap->m_epoch);
3423 mutex_unlock(&mdsc->mutex);
3424 return;
3425 }
3426
3427 newmap = ceph_mdsmap_decode(&p, end);
3428 if (IS_ERR(newmap)) {
3429 err = PTR_ERR(newmap);
3430 goto bad_unlock;
3431 }
3432
3433 /* swap into place */
3434 if (mdsc->mdsmap) {
3435 oldmap = mdsc->mdsmap;
3436 mdsc->mdsmap = newmap;
3437 check_new_map(mdsc, newmap, oldmap);
3438 ceph_mdsmap_destroy(oldmap);
3439 } else {
3440 mdsc->mdsmap = newmap; /* first mds map */
3441 }
3d14c5d2 3442 mdsc->fsc->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
2f2dc053
SW
3443
3444 __wake_requests(mdsc, &mdsc->waiting_for_map);
3445
3446 mutex_unlock(&mdsc->mutex);
3447 schedule_delayed(mdsc);
3448 return;
3449
3450bad_unlock:
3451 mutex_unlock(&mdsc->mutex);
3452bad:
3453 pr_err("error decoding mdsmap %d\n", err);
3454 return;
3455}
3456
3457static struct ceph_connection *con_get(struct ceph_connection *con)
3458{
3459 struct ceph_mds_session *s = con->private;
3460
3461 if (get_session(s)) {
2600d2dd 3462 dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref));
2f2dc053
SW
3463 return con;
3464 }
3465 dout("mdsc con_get %p FAIL\n", s);
3466 return NULL;
3467}
3468
3469static void con_put(struct ceph_connection *con)
3470{
3471 struct ceph_mds_session *s = con->private;
3472
7d8e18a6 3473 dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref) - 1);
2f2dc053
SW
3474 ceph_put_mds_session(s);
3475}
3476
3477/*
3478 * if the client is unresponsive for long enough, the mds will kill
3479 * the session entirely.
3480 */
3481static void peer_reset(struct ceph_connection *con)
3482{
3483 struct ceph_mds_session *s = con->private;
7e70f0ed 3484 struct ceph_mds_client *mdsc = s->s_mdsc;
2f2dc053 3485
7e70f0ed
SW
3486 pr_warning("mds%d closed our session\n", s->s_mds);
3487 send_mds_reconnect(mdsc, s);
2f2dc053
SW
3488}
3489
3490static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
3491{
3492 struct ceph_mds_session *s = con->private;
3493 struct ceph_mds_client *mdsc = s->s_mdsc;
3494 int type = le16_to_cpu(msg->hdr.type);
3495
2600d2dd
SW
3496 mutex_lock(&mdsc->mutex);
3497 if (__verify_registered_session(mdsc, s) < 0) {
3498 mutex_unlock(&mdsc->mutex);
3499 goto out;
3500 }
3501 mutex_unlock(&mdsc->mutex);
3502
2f2dc053
SW
3503 switch (type) {
3504 case CEPH_MSG_MDS_MAP:
3505 ceph_mdsc_handle_map(mdsc, msg);
3506 break;
3507 case CEPH_MSG_CLIENT_SESSION:
3508 handle_session(s, msg);
3509 break;
3510 case CEPH_MSG_CLIENT_REPLY:
3511 handle_reply(s, msg);
3512 break;
3513 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
2600d2dd 3514 handle_forward(mdsc, s, msg);
2f2dc053
SW
3515 break;
3516 case CEPH_MSG_CLIENT_CAPS:
3517 ceph_handle_caps(s, msg);
3518 break;
3519 case CEPH_MSG_CLIENT_SNAP:
2600d2dd 3520 ceph_handle_snap(mdsc, s, msg);
2f2dc053
SW
3521 break;
3522 case CEPH_MSG_CLIENT_LEASE:
2600d2dd 3523 handle_lease(mdsc, s, msg);
2f2dc053
SW
3524 break;
3525
3526 default:
3527 pr_err("received unknown message type %d %s\n", type,
3528 ceph_msg_type_name(type));
3529 }
2600d2dd 3530out:
2f2dc053
SW
3531 ceph_msg_put(msg);
3532}
3533
4e7a5dcd
SW
3534/*
3535 * authentication
3536 */
a3530df3
AE
3537
3538/*
3539 * Note: returned pointer is the address of a structure that's
3540 * managed separately. Caller must *not* attempt to free it.
3541 */
3542static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
8f43fb53 3543 int *proto, int force_new)
4e7a5dcd
SW
3544{
3545 struct ceph_mds_session *s = con->private;
3546 struct ceph_mds_client *mdsc = s->s_mdsc;
3d14c5d2 3547 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
74f1869f 3548 struct ceph_auth_handshake *auth = &s->s_auth;
4e7a5dcd 3549
74f1869f 3550 if (force_new && auth->authorizer) {
27859f97 3551 ceph_auth_destroy_authorizer(ac, auth->authorizer);
74f1869f 3552 auth->authorizer = NULL;
4e7a5dcd 3553 }
27859f97
SW
3554 if (!auth->authorizer) {
3555 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
3556 auth);
0bed9b5c
SW
3557 if (ret)
3558 return ERR_PTR(ret);
27859f97
SW
3559 } else {
3560 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
3561 auth);
a255651d 3562 if (ret)
a3530df3 3563 return ERR_PTR(ret);
4e7a5dcd 3564 }
4e7a5dcd 3565 *proto = ac->protocol;
74f1869f 3566
a3530df3 3567 return auth;
4e7a5dcd
SW
3568}
3569
3570
3571static int verify_authorizer_reply(struct ceph_connection *con, int len)
3572{
3573 struct ceph_mds_session *s = con->private;
3574 struct ceph_mds_client *mdsc = s->s_mdsc;
3d14c5d2 3575 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
4e7a5dcd 3576
27859f97 3577 return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer, len);
4e7a5dcd
SW
3578}
3579
9bd2e6f8
SW
3580static int invalidate_authorizer(struct ceph_connection *con)
3581{
3582 struct ceph_mds_session *s = con->private;
3583 struct ceph_mds_client *mdsc = s->s_mdsc;
3d14c5d2 3584 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
9bd2e6f8 3585
27859f97 3586 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
9bd2e6f8 3587
3d14c5d2 3588 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
9bd2e6f8
SW
3589}
3590
53ded495
AE
3591static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
3592 struct ceph_msg_header *hdr, int *skip)
3593{
3594 struct ceph_msg *msg;
3595 int type = (int) le16_to_cpu(hdr->type);
3596 int front_len = (int) le32_to_cpu(hdr->front_len);
3597
3598 if (con->in_msg)
3599 return con->in_msg;
3600
3601 *skip = 0;
3602 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
3603 if (!msg) {
3604 pr_err("unable to allocate msg type %d len %d\n",
3605 type, front_len);
3606 return NULL;
3607 }
53ded495
AE
3608
3609 return msg;
3610}
3611
9e32789f 3612static const struct ceph_connection_operations mds_con_ops = {
2f2dc053
SW
3613 .get = con_get,
3614 .put = con_put,
3615 .dispatch = dispatch,
4e7a5dcd
SW
3616 .get_authorizer = get_authorizer,
3617 .verify_authorizer_reply = verify_authorizer_reply,
9bd2e6f8 3618 .invalidate_authorizer = invalidate_authorizer,
2f2dc053 3619 .peer_reset = peer_reset,
53ded495 3620 .alloc_msg = mds_alloc_msg,
2f2dc053
SW
3621};
3622
2f2dc053 3623/* eof */