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