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