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