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