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