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