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