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