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