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