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