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