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