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