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