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