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