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