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