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