1 #include <linux/ceph/ceph_debug.h>
4 #include <linux/wait.h>
5 #include <linux/slab.h>
7 #include <linux/sched.h>
8 #include <linux/debugfs.h>
9 #include <linux/seq_file.h>
10 #include <linux/utsname.h>
13 #include "mds_client.h"
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>
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.
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
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.
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.
47 struct ceph_reconnect_state
{
49 struct ceph_pagelist
*pagelist
;
53 static void __wake_requests(struct ceph_mds_client
*mdsc
,
54 struct list_head
*head
);
56 static const struct ceph_connection_operations mds_con_ops
;
64 * parse individual inode info
66 static int parse_reply_info_in(void **p
, void *end
,
67 struct ceph_mds_reply_info_in
*info
,
73 *p
+= sizeof(struct ceph_mds_reply_inode
) +
74 sizeof(*info
->in
->fragtree
.splits
) *
75 le32_to_cpu(info
->in
->fragtree
.nsplits
);
77 ceph_decode_32_safe(p
, end
, info
->symlink_len
, bad
);
78 ceph_decode_need(p
, end
, info
->symlink_len
, bad
);
80 *p
+= info
->symlink_len
;
82 if (features
& CEPH_FEATURE_DIRLAYOUTHASH
)
83 ceph_decode_copy_safe(p
, end
, &info
->dir_layout
,
84 sizeof(info
->dir_layout
), bad
);
86 memset(&info
->dir_layout
, 0, sizeof(info
->dir_layout
));
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
;
93 if (features
& CEPH_FEATURE_MDS_INLINE_DATA
) {
94 ceph_decode_64_safe(p
, end
, info
->inline_version
, bad
);
95 ceph_decode_32_safe(p
, end
, info
->inline_len
, bad
);
96 ceph_decode_need(p
, end
, info
->inline_len
, bad
);
97 info
->inline_data
= *p
;
98 *p
+= info
->inline_len
;
100 info
->inline_version
= CEPH_INLINE_NONE
;
108 * parse a normal reply, which may contain a (dir+)dentry and/or a
111 static int parse_reply_info_trace(void **p
, void *end
,
112 struct ceph_mds_reply_info_parsed
*info
,
117 if (info
->head
->is_dentry
) {
118 err
= parse_reply_info_in(p
, end
, &info
->diri
, features
);
122 if (unlikely(*p
+ sizeof(*info
->dirfrag
) > end
))
125 *p
+= sizeof(*info
->dirfrag
) +
126 sizeof(u32
)*le32_to_cpu(info
->dirfrag
->ndist
);
127 if (unlikely(*p
> end
))
130 ceph_decode_32_safe(p
, end
, info
->dname_len
, bad
);
131 ceph_decode_need(p
, end
, info
->dname_len
, bad
);
133 *p
+= info
->dname_len
;
135 *p
+= sizeof(*info
->dlease
);
138 if (info
->head
->is_target
) {
139 err
= parse_reply_info_in(p
, end
, &info
->targeti
, features
);
144 if (unlikely(*p
!= end
))
151 pr_err("problem parsing mds trace %d\n", err
);
156 * parse readdir results
158 static int parse_reply_info_dir(void **p
, void *end
,
159 struct ceph_mds_reply_info_parsed
*info
,
166 if (*p
+ sizeof(*info
->dir_dir
) > end
)
168 *p
+= sizeof(*info
->dir_dir
) +
169 sizeof(u32
)*le32_to_cpu(info
->dir_dir
->ndist
);
173 ceph_decode_need(p
, end
, sizeof(num
) + 2, bad
);
174 num
= ceph_decode_32(p
);
175 info
->dir_end
= ceph_decode_8(p
);
176 info
->dir_complete
= ceph_decode_8(p
);
180 BUG_ON(!info
->dir_in
);
181 info
->dir_dname
= (void *)(info
->dir_in
+ num
);
182 info
->dir_dname_len
= (void *)(info
->dir_dname
+ num
);
183 info
->dir_dlease
= (void *)(info
->dir_dname_len
+ num
);
184 if ((unsigned long)(info
->dir_dlease
+ num
) >
185 (unsigned long)info
->dir_in
+ info
->dir_buf_size
) {
186 pr_err("dir contents are larger than expected\n");
194 ceph_decode_need(p
, end
, sizeof(u32
)*2, bad
);
195 info
->dir_dname_len
[i
] = ceph_decode_32(p
);
196 ceph_decode_need(p
, end
, info
->dir_dname_len
[i
], bad
);
197 info
->dir_dname
[i
] = *p
;
198 *p
+= info
->dir_dname_len
[i
];
199 dout("parsed dir dname '%.*s'\n", info
->dir_dname_len
[i
],
201 info
->dir_dlease
[i
] = *p
;
202 *p
+= sizeof(struct ceph_mds_reply_lease
);
205 err
= parse_reply_info_in(p
, end
, &info
->dir_in
[i
], features
);
220 pr_err("problem parsing dir contents %d\n", err
);
225 * parse fcntl F_GETLK results
227 static int parse_reply_info_filelock(void **p
, void *end
,
228 struct ceph_mds_reply_info_parsed
*info
,
231 if (*p
+ sizeof(*info
->filelock_reply
) > end
)
234 info
->filelock_reply
= *p
;
235 *p
+= sizeof(*info
->filelock_reply
);
237 if (unlikely(*p
!= end
))
246 * parse create results
248 static int parse_reply_info_create(void **p
, void *end
,
249 struct ceph_mds_reply_info_parsed
*info
,
252 if (features
& CEPH_FEATURE_REPLY_CREATE_INODE
) {
254 info
->has_create_ino
= false;
256 info
->has_create_ino
= true;
257 info
->ino
= ceph_decode_64(p
);
261 if (unlikely(*p
!= end
))
270 * parse extra results
272 static int parse_reply_info_extra(void **p
, void *end
,
273 struct ceph_mds_reply_info_parsed
*info
,
276 if (info
->head
->op
== CEPH_MDS_OP_GETFILELOCK
)
277 return parse_reply_info_filelock(p
, end
, info
, features
);
278 else if (info
->head
->op
== CEPH_MDS_OP_READDIR
||
279 info
->head
->op
== CEPH_MDS_OP_LSSNAP
)
280 return parse_reply_info_dir(p
, end
, info
, features
);
281 else if (info
->head
->op
== CEPH_MDS_OP_CREATE
)
282 return parse_reply_info_create(p
, end
, info
, features
);
288 * parse entire mds reply
290 static int parse_reply_info(struct ceph_msg
*msg
,
291 struct ceph_mds_reply_info_parsed
*info
,
298 info
->head
= msg
->front
.iov_base
;
299 p
= msg
->front
.iov_base
+ sizeof(struct ceph_mds_reply_head
);
300 end
= p
+ msg
->front
.iov_len
- sizeof(struct ceph_mds_reply_head
);
303 ceph_decode_32_safe(&p
, end
, len
, bad
);
305 ceph_decode_need(&p
, end
, len
, bad
);
306 err
= parse_reply_info_trace(&p
, p
+len
, info
, features
);
312 ceph_decode_32_safe(&p
, end
, len
, bad
);
314 ceph_decode_need(&p
, end
, len
, bad
);
315 err
= parse_reply_info_extra(&p
, p
+len
, info
, features
);
321 ceph_decode_32_safe(&p
, end
, len
, bad
);
322 info
->snapblob_len
= len
;
333 pr_err("mds parse_reply err %d\n", err
);
337 static void destroy_reply_info(struct ceph_mds_reply_info_parsed
*info
)
341 free_pages((unsigned long)info
->dir_in
, get_order(info
->dir_buf_size
));
348 const char *ceph_session_state_name(int s
)
351 case CEPH_MDS_SESSION_NEW
: return "new";
352 case CEPH_MDS_SESSION_OPENING
: return "opening";
353 case CEPH_MDS_SESSION_OPEN
: return "open";
354 case CEPH_MDS_SESSION_HUNG
: return "hung";
355 case CEPH_MDS_SESSION_CLOSING
: return "closing";
356 case CEPH_MDS_SESSION_RESTARTING
: return "restarting";
357 case CEPH_MDS_SESSION_RECONNECTING
: return "reconnecting";
358 default: return "???";
362 static struct ceph_mds_session
*get_session(struct ceph_mds_session
*s
)
364 if (atomic_inc_not_zero(&s
->s_ref
)) {
365 dout("mdsc get_session %p %d -> %d\n", s
,
366 atomic_read(&s
->s_ref
)-1, atomic_read(&s
->s_ref
));
369 dout("mdsc get_session %p 0 -- FAIL", s
);
374 void ceph_put_mds_session(struct ceph_mds_session
*s
)
376 dout("mdsc put_session %p %d -> %d\n", s
,
377 atomic_read(&s
->s_ref
), atomic_read(&s
->s_ref
)-1);
378 if (atomic_dec_and_test(&s
->s_ref
)) {
379 if (s
->s_auth
.authorizer
)
380 ceph_auth_destroy_authorizer(
381 s
->s_mdsc
->fsc
->client
->monc
.auth
,
382 s
->s_auth
.authorizer
);
388 * called under mdsc->mutex
390 struct ceph_mds_session
*__ceph_lookup_mds_session(struct ceph_mds_client
*mdsc
,
393 struct ceph_mds_session
*session
;
395 if (mds
>= mdsc
->max_sessions
|| mdsc
->sessions
[mds
] == NULL
)
397 session
= mdsc
->sessions
[mds
];
398 dout("lookup_mds_session %p %d\n", session
,
399 atomic_read(&session
->s_ref
));
400 get_session(session
);
404 static bool __have_session(struct ceph_mds_client
*mdsc
, int mds
)
406 if (mds
>= mdsc
->max_sessions
)
408 return mdsc
->sessions
[mds
];
411 static int __verify_registered_session(struct ceph_mds_client
*mdsc
,
412 struct ceph_mds_session
*s
)
414 if (s
->s_mds
>= mdsc
->max_sessions
||
415 mdsc
->sessions
[s
->s_mds
] != s
)
421 * create+register a new session for given mds.
422 * called under mdsc->mutex.
424 static struct ceph_mds_session
*register_session(struct ceph_mds_client
*mdsc
,
427 struct ceph_mds_session
*s
;
429 if (mds
>= mdsc
->mdsmap
->m_max_mds
)
430 return ERR_PTR(-EINVAL
);
432 s
= kzalloc(sizeof(*s
), GFP_NOFS
);
434 return ERR_PTR(-ENOMEM
);
437 s
->s_state
= CEPH_MDS_SESSION_NEW
;
440 mutex_init(&s
->s_mutex
);
442 ceph_con_init(&s
->s_con
, s
, &mds_con_ops
, &mdsc
->fsc
->client
->msgr
);
444 spin_lock_init(&s
->s_gen_ttl_lock
);
446 s
->s_cap_ttl
= jiffies
- 1;
448 spin_lock_init(&s
->s_cap_lock
);
449 s
->s_renew_requested
= 0;
451 INIT_LIST_HEAD(&s
->s_caps
);
454 atomic_set(&s
->s_ref
, 1);
455 INIT_LIST_HEAD(&s
->s_waiting
);
456 INIT_LIST_HEAD(&s
->s_unsafe
);
457 s
->s_num_cap_releases
= 0;
458 s
->s_cap_reconnect
= 0;
459 s
->s_cap_iterator
= NULL
;
460 INIT_LIST_HEAD(&s
->s_cap_releases
);
461 INIT_LIST_HEAD(&s
->s_cap_flushing
);
462 INIT_LIST_HEAD(&s
->s_cap_snaps_flushing
);
464 dout("register_session mds%d\n", mds
);
465 if (mds
>= mdsc
->max_sessions
) {
466 int newmax
= 1 << get_count_order(mds
+1);
467 struct ceph_mds_session
**sa
;
469 dout("register_session realloc to %d\n", newmax
);
470 sa
= kcalloc(newmax
, sizeof(void *), GFP_NOFS
);
473 if (mdsc
->sessions
) {
474 memcpy(sa
, mdsc
->sessions
,
475 mdsc
->max_sessions
* sizeof(void *));
476 kfree(mdsc
->sessions
);
479 mdsc
->max_sessions
= newmax
;
481 mdsc
->sessions
[mds
] = s
;
482 atomic_inc(&mdsc
->num_sessions
);
483 atomic_inc(&s
->s_ref
); /* one ref to sessions[], one to caller */
485 ceph_con_open(&s
->s_con
, CEPH_ENTITY_TYPE_MDS
, mds
,
486 ceph_mdsmap_get_addr(mdsc
->mdsmap
, mds
));
492 return ERR_PTR(-ENOMEM
);
496 * called under mdsc->mutex
498 static void __unregister_session(struct ceph_mds_client
*mdsc
,
499 struct ceph_mds_session
*s
)
501 dout("__unregister_session mds%d %p\n", s
->s_mds
, s
);
502 BUG_ON(mdsc
->sessions
[s
->s_mds
] != s
);
503 mdsc
->sessions
[s
->s_mds
] = NULL
;
504 ceph_con_close(&s
->s_con
);
505 ceph_put_mds_session(s
);
506 atomic_dec(&mdsc
->num_sessions
);
510 * drop session refs in request.
512 * should be last request ref, or hold mdsc->mutex
514 static void put_request_session(struct ceph_mds_request
*req
)
516 if (req
->r_session
) {
517 ceph_put_mds_session(req
->r_session
);
518 req
->r_session
= NULL
;
522 void ceph_mdsc_release_request(struct kref
*kref
)
524 struct ceph_mds_request
*req
= container_of(kref
,
525 struct ceph_mds_request
,
527 destroy_reply_info(&req
->r_reply_info
);
529 ceph_msg_put(req
->r_request
);
531 ceph_msg_put(req
->r_reply
);
533 ceph_put_cap_refs(ceph_inode(req
->r_inode
), CEPH_CAP_PIN
);
536 if (req
->r_locked_dir
)
537 ceph_put_cap_refs(ceph_inode(req
->r_locked_dir
), CEPH_CAP_PIN
);
538 iput(req
->r_target_inode
);
541 if (req
->r_old_dentry
)
542 dput(req
->r_old_dentry
);
543 if (req
->r_old_dentry_dir
) {
545 * track (and drop pins for) r_old_dentry_dir
546 * separately, since r_old_dentry's d_parent may have
547 * changed between the dir mutex being dropped and
548 * this request being freed.
550 ceph_put_cap_refs(ceph_inode(req
->r_old_dentry_dir
),
552 iput(req
->r_old_dentry_dir
);
557 ceph_pagelist_release(req
->r_pagelist
);
558 put_request_session(req
);
559 ceph_unreserve_caps(req
->r_mdsc
, &req
->r_caps_reservation
);
564 * lookup session, bump ref if found.
566 * called under mdsc->mutex.
568 static struct ceph_mds_request
*__lookup_request(struct ceph_mds_client
*mdsc
,
571 struct ceph_mds_request
*req
;
572 struct rb_node
*n
= mdsc
->request_tree
.rb_node
;
575 req
= rb_entry(n
, struct ceph_mds_request
, r_node
);
576 if (tid
< req
->r_tid
)
578 else if (tid
> req
->r_tid
)
581 ceph_mdsc_get_request(req
);
588 static void __insert_request(struct ceph_mds_client
*mdsc
,
589 struct ceph_mds_request
*new)
591 struct rb_node
**p
= &mdsc
->request_tree
.rb_node
;
592 struct rb_node
*parent
= NULL
;
593 struct ceph_mds_request
*req
= NULL
;
597 req
= rb_entry(parent
, struct ceph_mds_request
, r_node
);
598 if (new->r_tid
< req
->r_tid
)
600 else if (new->r_tid
> req
->r_tid
)
606 rb_link_node(&new->r_node
, parent
, p
);
607 rb_insert_color(&new->r_node
, &mdsc
->request_tree
);
611 * Register an in-flight request, and assign a tid. Link to directory
612 * are modifying (if any).
614 * Called under mdsc->mutex.
616 static void __register_request(struct ceph_mds_client
*mdsc
,
617 struct ceph_mds_request
*req
,
620 req
->r_tid
= ++mdsc
->last_tid
;
622 ceph_reserve_caps(mdsc
, &req
->r_caps_reservation
,
624 dout("__register_request %p tid %lld\n", req
, req
->r_tid
);
625 ceph_mdsc_get_request(req
);
626 __insert_request(mdsc
, req
);
628 req
->r_uid
= current_fsuid();
629 req
->r_gid
= current_fsgid();
632 struct ceph_inode_info
*ci
= ceph_inode(dir
);
635 spin_lock(&ci
->i_unsafe_lock
);
636 req
->r_unsafe_dir
= dir
;
637 list_add_tail(&req
->r_unsafe_dir_item
, &ci
->i_unsafe_dirops
);
638 spin_unlock(&ci
->i_unsafe_lock
);
642 static void __unregister_request(struct ceph_mds_client
*mdsc
,
643 struct ceph_mds_request
*req
)
645 dout("__unregister_request %p tid %lld\n", req
, req
->r_tid
);
646 rb_erase(&req
->r_node
, &mdsc
->request_tree
);
647 RB_CLEAR_NODE(&req
->r_node
);
649 if (req
->r_unsafe_dir
) {
650 struct ceph_inode_info
*ci
= ceph_inode(req
->r_unsafe_dir
);
652 spin_lock(&ci
->i_unsafe_lock
);
653 list_del_init(&req
->r_unsafe_dir_item
);
654 spin_unlock(&ci
->i_unsafe_lock
);
656 iput(req
->r_unsafe_dir
);
657 req
->r_unsafe_dir
= NULL
;
660 complete_all(&req
->r_safe_completion
);
662 ceph_mdsc_put_request(req
);
666 * Choose mds to send request to next. If there is a hint set in the
667 * request (e.g., due to a prior forward hint from the mds), use that.
668 * Otherwise, consult frag tree and/or caps to identify the
669 * appropriate mds. If all else fails, choose randomly.
671 * Called under mdsc->mutex.
673 static struct dentry
*get_nonsnap_parent(struct dentry
*dentry
)
676 * we don't need to worry about protecting the d_parent access
677 * here because we never renaming inside the snapped namespace
678 * except to resplice to another snapdir, and either the old or new
679 * result is a valid result.
681 while (!IS_ROOT(dentry
) && ceph_snap(d_inode(dentry
)) != CEPH_NOSNAP
)
682 dentry
= dentry
->d_parent
;
686 static int __choose_mds(struct ceph_mds_client
*mdsc
,
687 struct ceph_mds_request
*req
)
690 struct ceph_inode_info
*ci
;
691 struct ceph_cap
*cap
;
692 int mode
= req
->r_direct_mode
;
694 u32 hash
= req
->r_direct_hash
;
695 bool is_hash
= req
->r_direct_is_hash
;
698 * is there a specific mds we should try? ignore hint if we have
699 * no session and the mds is not up (active or recovering).
701 if (req
->r_resend_mds
>= 0 &&
702 (__have_session(mdsc
, req
->r_resend_mds
) ||
703 ceph_mdsmap_get_state(mdsc
->mdsmap
, req
->r_resend_mds
) > 0)) {
704 dout("choose_mds using resend_mds mds%d\n",
706 return req
->r_resend_mds
;
709 if (mode
== USE_RANDOM_MDS
)
714 inode
= req
->r_inode
;
715 } else if (req
->r_dentry
) {
716 /* ignore race with rename; old or new d_parent is okay */
717 struct dentry
*parent
= req
->r_dentry
->d_parent
;
718 struct inode
*dir
= d_inode(parent
);
720 if (dir
->i_sb
!= mdsc
->fsc
->sb
) {
722 inode
= d_inode(req
->r_dentry
);
723 } else if (ceph_snap(dir
) != CEPH_NOSNAP
) {
724 /* direct snapped/virtual snapdir requests
725 * based on parent dir inode */
726 struct dentry
*dn
= get_nonsnap_parent(parent
);
728 dout("__choose_mds using nonsnap parent %p\n", inode
);
731 inode
= d_inode(req
->r_dentry
);
732 if (!inode
|| mode
== USE_AUTH_MDS
) {
735 hash
= ceph_dentry_hash(dir
, req
->r_dentry
);
741 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode
, (int)is_hash
,
745 ci
= ceph_inode(inode
);
747 if (is_hash
&& S_ISDIR(inode
->i_mode
)) {
748 struct ceph_inode_frag frag
;
751 ceph_choose_frag(ci
, hash
, &frag
, &found
);
753 if (mode
== USE_ANY_MDS
&& frag
.ndist
> 0) {
756 /* choose a random replica */
757 get_random_bytes(&r
, 1);
760 dout("choose_mds %p %llx.%llx "
761 "frag %u mds%d (%d/%d)\n",
762 inode
, ceph_vinop(inode
),
765 if (ceph_mdsmap_get_state(mdsc
->mdsmap
, mds
) >=
766 CEPH_MDS_STATE_ACTIVE
)
770 /* since this file/dir wasn't known to be
771 * replicated, then we want to look for the
772 * authoritative mds. */
775 /* choose auth mds */
777 dout("choose_mds %p %llx.%llx "
778 "frag %u mds%d (auth)\n",
779 inode
, ceph_vinop(inode
), frag
.frag
, mds
);
780 if (ceph_mdsmap_get_state(mdsc
->mdsmap
, mds
) >=
781 CEPH_MDS_STATE_ACTIVE
)
787 spin_lock(&ci
->i_ceph_lock
);
789 if (mode
== USE_AUTH_MDS
)
790 cap
= ci
->i_auth_cap
;
791 if (!cap
&& !RB_EMPTY_ROOT(&ci
->i_caps
))
792 cap
= rb_entry(rb_first(&ci
->i_caps
), struct ceph_cap
, ci_node
);
794 spin_unlock(&ci
->i_ceph_lock
);
797 mds
= cap
->session
->s_mds
;
798 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
799 inode
, ceph_vinop(inode
), mds
,
800 cap
== ci
->i_auth_cap
? "auth " : "", cap
);
801 spin_unlock(&ci
->i_ceph_lock
);
805 mds
= ceph_mdsmap_get_random_mds(mdsc
->mdsmap
);
806 dout("choose_mds chose random mds%d\n", mds
);
814 static struct ceph_msg
*create_session_msg(u32 op
, u64 seq
)
816 struct ceph_msg
*msg
;
817 struct ceph_mds_session_head
*h
;
819 msg
= ceph_msg_new(CEPH_MSG_CLIENT_SESSION
, sizeof(*h
), GFP_NOFS
,
822 pr_err("create_session_msg ENOMEM creating msg\n");
825 h
= msg
->front
.iov_base
;
826 h
->op
= cpu_to_le32(op
);
827 h
->seq
= cpu_to_le64(seq
);
833 * session message, specialization for CEPH_SESSION_REQUEST_OPEN
834 * to include additional client metadata fields.
836 static struct ceph_msg
*create_session_open_msg(struct ceph_mds_client
*mdsc
, u64 seq
)
838 struct ceph_msg
*msg
;
839 struct ceph_mds_session_head
*h
;
841 int metadata_bytes
= 0;
842 int metadata_key_count
= 0;
843 struct ceph_options
*opt
= mdsc
->fsc
->client
->options
;
846 const char* metadata
[][2] = {
847 {"hostname", utsname()->nodename
},
848 {"kernel_version", utsname()->release
},
849 {"entity_id", opt
->name
? opt
->name
: ""},
853 /* Calculate serialized length of metadata */
854 metadata_bytes
= 4; /* map length */
855 for (i
= 0; metadata
[i
][0] != NULL
; ++i
) {
856 metadata_bytes
+= 8 + strlen(metadata
[i
][0]) +
857 strlen(metadata
[i
][1]);
858 metadata_key_count
++;
861 /* Allocate the message */
862 msg
= ceph_msg_new(CEPH_MSG_CLIENT_SESSION
, sizeof(*h
) + metadata_bytes
,
865 pr_err("create_session_msg ENOMEM creating msg\n");
868 h
= msg
->front
.iov_base
;
869 h
->op
= cpu_to_le32(CEPH_SESSION_REQUEST_OPEN
);
870 h
->seq
= cpu_to_le64(seq
);
873 * Serialize client metadata into waiting buffer space, using
874 * the format that userspace expects for map<string, string>
876 * ClientSession messages with metadata are v2
878 msg
->hdr
.version
= cpu_to_le16(2);
879 msg
->hdr
.compat_version
= cpu_to_le16(1);
881 /* The write pointer, following the session_head structure */
882 p
= msg
->front
.iov_base
+ sizeof(*h
);
884 /* Number of entries in the map */
885 ceph_encode_32(&p
, metadata_key_count
);
887 /* Two length-prefixed strings for each entry in the map */
888 for (i
= 0; metadata
[i
][0] != NULL
; ++i
) {
889 size_t const key_len
= strlen(metadata
[i
][0]);
890 size_t const val_len
= strlen(metadata
[i
][1]);
892 ceph_encode_32(&p
, key_len
);
893 memcpy(p
, metadata
[i
][0], key_len
);
895 ceph_encode_32(&p
, val_len
);
896 memcpy(p
, metadata
[i
][1], val_len
);
904 * send session open request.
906 * called under mdsc->mutex
908 static int __open_session(struct ceph_mds_client
*mdsc
,
909 struct ceph_mds_session
*session
)
911 struct ceph_msg
*msg
;
913 int mds
= session
->s_mds
;
915 /* wait for mds to go active? */
916 mstate
= ceph_mdsmap_get_state(mdsc
->mdsmap
, mds
);
917 dout("open_session to mds%d (%s)\n", mds
,
918 ceph_mds_state_name(mstate
));
919 session
->s_state
= CEPH_MDS_SESSION_OPENING
;
920 session
->s_renew_requested
= jiffies
;
922 /* send connect message */
923 msg
= create_session_open_msg(mdsc
, session
->s_seq
);
926 ceph_con_send(&session
->s_con
, msg
);
931 * open sessions for any export targets for the given mds
933 * called under mdsc->mutex
935 static struct ceph_mds_session
*
936 __open_export_target_session(struct ceph_mds_client
*mdsc
, int target
)
938 struct ceph_mds_session
*session
;
940 session
= __ceph_lookup_mds_session(mdsc
, target
);
942 session
= register_session(mdsc
, target
);
946 if (session
->s_state
== CEPH_MDS_SESSION_NEW
||
947 session
->s_state
== CEPH_MDS_SESSION_CLOSING
)
948 __open_session(mdsc
, session
);
953 struct ceph_mds_session
*
954 ceph_mdsc_open_export_target_session(struct ceph_mds_client
*mdsc
, int target
)
956 struct ceph_mds_session
*session
;
958 dout("open_export_target_session to mds%d\n", target
);
960 mutex_lock(&mdsc
->mutex
);
961 session
= __open_export_target_session(mdsc
, target
);
962 mutex_unlock(&mdsc
->mutex
);
967 static void __open_export_target_sessions(struct ceph_mds_client
*mdsc
,
968 struct ceph_mds_session
*session
)
970 struct ceph_mds_info
*mi
;
971 struct ceph_mds_session
*ts
;
972 int i
, mds
= session
->s_mds
;
974 if (mds
>= mdsc
->mdsmap
->m_max_mds
)
977 mi
= &mdsc
->mdsmap
->m_info
[mds
];
978 dout("open_export_target_sessions for mds%d (%d targets)\n",
979 session
->s_mds
, mi
->num_export_targets
);
981 for (i
= 0; i
< mi
->num_export_targets
; i
++) {
982 ts
= __open_export_target_session(mdsc
, mi
->export_targets
[i
]);
984 ceph_put_mds_session(ts
);
988 void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client
*mdsc
,
989 struct ceph_mds_session
*session
)
991 mutex_lock(&mdsc
->mutex
);
992 __open_export_target_sessions(mdsc
, session
);
993 mutex_unlock(&mdsc
->mutex
);
1000 /* caller holds s_cap_lock, we drop it */
1001 static void cleanup_cap_releases(struct ceph_mds_client
*mdsc
,
1002 struct ceph_mds_session
*session
)
1003 __releases(session
->s_cap_lock
)
1005 LIST_HEAD(tmp_list
);
1006 list_splice_init(&session
->s_cap_releases
, &tmp_list
);
1007 session
->s_num_cap_releases
= 0;
1008 spin_unlock(&session
->s_cap_lock
);
1010 dout("cleanup_cap_releases mds%d\n", session
->s_mds
);
1011 while (!list_empty(&tmp_list
)) {
1012 struct ceph_cap
*cap
;
1013 /* zero out the in-progress message */
1014 cap
= list_first_entry(&tmp_list
,
1015 struct ceph_cap
, session_caps
);
1016 list_del(&cap
->session_caps
);
1017 ceph_put_cap(mdsc
, cap
);
1021 static void cleanup_session_requests(struct ceph_mds_client
*mdsc
,
1022 struct ceph_mds_session
*session
)
1024 struct ceph_mds_request
*req
;
1027 dout("cleanup_session_requests mds%d\n", session
->s_mds
);
1028 mutex_lock(&mdsc
->mutex
);
1029 while (!list_empty(&session
->s_unsafe
)) {
1030 req
= list_first_entry(&session
->s_unsafe
,
1031 struct ceph_mds_request
, r_unsafe_item
);
1032 list_del_init(&req
->r_unsafe_item
);
1033 pr_info(" dropping unsafe request %llu\n", req
->r_tid
);
1034 __unregister_request(mdsc
, req
);
1036 /* zero r_attempts, so kick_requests() will re-send requests */
1037 p
= rb_first(&mdsc
->request_tree
);
1039 req
= rb_entry(p
, struct ceph_mds_request
, r_node
);
1041 if (req
->r_session
&&
1042 req
->r_session
->s_mds
== session
->s_mds
)
1043 req
->r_attempts
= 0;
1045 mutex_unlock(&mdsc
->mutex
);
1049 * Helper to safely iterate over all caps associated with a session, with
1050 * special care taken to handle a racing __ceph_remove_cap().
1052 * Caller must hold session s_mutex.
1054 static int iterate_session_caps(struct ceph_mds_session
*session
,
1055 int (*cb
)(struct inode
*, struct ceph_cap
*,
1058 struct list_head
*p
;
1059 struct ceph_cap
*cap
;
1060 struct inode
*inode
, *last_inode
= NULL
;
1061 struct ceph_cap
*old_cap
= NULL
;
1064 dout("iterate_session_caps %p mds%d\n", session
, session
->s_mds
);
1065 spin_lock(&session
->s_cap_lock
);
1066 p
= session
->s_caps
.next
;
1067 while (p
!= &session
->s_caps
) {
1068 cap
= list_entry(p
, struct ceph_cap
, session_caps
);
1069 inode
= igrab(&cap
->ci
->vfs_inode
);
1074 session
->s_cap_iterator
= cap
;
1075 spin_unlock(&session
->s_cap_lock
);
1082 ceph_put_cap(session
->s_mdsc
, old_cap
);
1086 ret
= cb(inode
, cap
, arg
);
1089 spin_lock(&session
->s_cap_lock
);
1091 if (cap
->ci
== NULL
) {
1092 dout("iterate_session_caps finishing cap %p removal\n",
1094 BUG_ON(cap
->session
!= session
);
1095 cap
->session
= NULL
;
1096 list_del_init(&cap
->session_caps
);
1097 session
->s_nr_caps
--;
1098 if (cap
->queue_release
) {
1099 list_add_tail(&cap
->session_caps
,
1100 &session
->s_cap_releases
);
1101 session
->s_num_cap_releases
++;
1103 old_cap
= cap
; /* put_cap it w/o locks held */
1111 session
->s_cap_iterator
= NULL
;
1112 spin_unlock(&session
->s_cap_lock
);
1116 ceph_put_cap(session
->s_mdsc
, old_cap
);
1121 static int remove_session_caps_cb(struct inode
*inode
, struct ceph_cap
*cap
,
1124 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1127 dout("removing cap %p, ci is %p, inode is %p\n",
1128 cap
, ci
, &ci
->vfs_inode
);
1129 spin_lock(&ci
->i_ceph_lock
);
1130 __ceph_remove_cap(cap
, false);
1131 if (!ci
->i_auth_cap
) {
1132 struct ceph_mds_client
*mdsc
=
1133 ceph_sb_to_client(inode
->i_sb
)->mdsc
;
1135 spin_lock(&mdsc
->cap_dirty_lock
);
1136 if (!list_empty(&ci
->i_dirty_item
)) {
1137 pr_info(" dropping dirty %s state for %p %lld\n",
1138 ceph_cap_string(ci
->i_dirty_caps
),
1139 inode
, ceph_ino(inode
));
1140 ci
->i_dirty_caps
= 0;
1141 list_del_init(&ci
->i_dirty_item
);
1144 if (!list_empty(&ci
->i_flushing_item
)) {
1145 pr_info(" dropping dirty+flushing %s state for %p %lld\n",
1146 ceph_cap_string(ci
->i_flushing_caps
),
1147 inode
, ceph_ino(inode
));
1148 ci
->i_flushing_caps
= 0;
1149 list_del_init(&ci
->i_flushing_item
);
1150 mdsc
->num_cap_flushing
--;
1153 spin_unlock(&mdsc
->cap_dirty_lock
);
1155 spin_unlock(&ci
->i_ceph_lock
);
1162 * caller must hold session s_mutex
1164 static void remove_session_caps(struct ceph_mds_session
*session
)
1166 dout("remove_session_caps on %p\n", session
);
1167 iterate_session_caps(session
, remove_session_caps_cb
, NULL
);
1169 spin_lock(&session
->s_cap_lock
);
1170 if (session
->s_nr_caps
> 0) {
1171 struct super_block
*sb
= session
->s_mdsc
->fsc
->sb
;
1172 struct inode
*inode
;
1173 struct ceph_cap
*cap
, *prev
= NULL
;
1174 struct ceph_vino vino
;
1176 * iterate_session_caps() skips inodes that are being
1177 * deleted, we need to wait until deletions are complete.
1178 * __wait_on_freeing_inode() is designed for the job,
1179 * but it is not exported, so use lookup inode function
1182 while (!list_empty(&session
->s_caps
)) {
1183 cap
= list_entry(session
->s_caps
.next
,
1184 struct ceph_cap
, session_caps
);
1188 vino
= cap
->ci
->i_vino
;
1189 spin_unlock(&session
->s_cap_lock
);
1191 inode
= ceph_find_inode(sb
, vino
);
1194 spin_lock(&session
->s_cap_lock
);
1198 // drop cap expires and unlock s_cap_lock
1199 cleanup_cap_releases(session
->s_mdsc
, session
);
1201 BUG_ON(session
->s_nr_caps
> 0);
1202 BUG_ON(!list_empty(&session
->s_cap_flushing
));
1206 * wake up any threads waiting on this session's caps. if the cap is
1207 * old (didn't get renewed on the client reconnect), remove it now.
1209 * caller must hold s_mutex.
1211 static int wake_up_session_cb(struct inode
*inode
, struct ceph_cap
*cap
,
1214 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1216 wake_up_all(&ci
->i_cap_wq
);
1218 spin_lock(&ci
->i_ceph_lock
);
1219 ci
->i_wanted_max_size
= 0;
1220 ci
->i_requested_max_size
= 0;
1221 spin_unlock(&ci
->i_ceph_lock
);
1226 static void wake_up_session_caps(struct ceph_mds_session
*session
,
1229 dout("wake_up_session_caps %p mds%d\n", session
, session
->s_mds
);
1230 iterate_session_caps(session
, wake_up_session_cb
,
1231 (void *)(unsigned long)reconnect
);
1235 * Send periodic message to MDS renewing all currently held caps. The
1236 * ack will reset the expiration for all caps from this session.
1238 * caller holds s_mutex
1240 static int send_renew_caps(struct ceph_mds_client
*mdsc
,
1241 struct ceph_mds_session
*session
)
1243 struct ceph_msg
*msg
;
1246 if (time_after_eq(jiffies
, session
->s_cap_ttl
) &&
1247 time_after_eq(session
->s_cap_ttl
, session
->s_renew_requested
))
1248 pr_info("mds%d caps stale\n", session
->s_mds
);
1249 session
->s_renew_requested
= jiffies
;
1251 /* do not try to renew caps until a recovering mds has reconnected
1252 * with its clients. */
1253 state
= ceph_mdsmap_get_state(mdsc
->mdsmap
, session
->s_mds
);
1254 if (state
< CEPH_MDS_STATE_RECONNECT
) {
1255 dout("send_renew_caps ignoring mds%d (%s)\n",
1256 session
->s_mds
, ceph_mds_state_name(state
));
1260 dout("send_renew_caps to mds%d (%s)\n", session
->s_mds
,
1261 ceph_mds_state_name(state
));
1262 msg
= create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS
,
1263 ++session
->s_renew_seq
);
1266 ceph_con_send(&session
->s_con
, msg
);
1270 static int send_flushmsg_ack(struct ceph_mds_client
*mdsc
,
1271 struct ceph_mds_session
*session
, u64 seq
)
1273 struct ceph_msg
*msg
;
1275 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
1276 session
->s_mds
, ceph_session_state_name(session
->s_state
), seq
);
1277 msg
= create_session_msg(CEPH_SESSION_FLUSHMSG_ACK
, seq
);
1280 ceph_con_send(&session
->s_con
, msg
);
1286 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
1288 * Called under session->s_mutex
1290 static void renewed_caps(struct ceph_mds_client
*mdsc
,
1291 struct ceph_mds_session
*session
, int is_renew
)
1296 spin_lock(&session
->s_cap_lock
);
1297 was_stale
= is_renew
&& time_after_eq(jiffies
, session
->s_cap_ttl
);
1299 session
->s_cap_ttl
= session
->s_renew_requested
+
1300 mdsc
->mdsmap
->m_session_timeout
*HZ
;
1303 if (time_before(jiffies
, session
->s_cap_ttl
)) {
1304 pr_info("mds%d caps renewed\n", session
->s_mds
);
1307 pr_info("mds%d caps still stale\n", session
->s_mds
);
1310 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1311 session
->s_mds
, session
->s_cap_ttl
, was_stale
? "stale" : "fresh",
1312 time_before(jiffies
, session
->s_cap_ttl
) ? "stale" : "fresh");
1313 spin_unlock(&session
->s_cap_lock
);
1316 wake_up_session_caps(session
, 0);
1320 * send a session close request
1322 static int request_close_session(struct ceph_mds_client
*mdsc
,
1323 struct ceph_mds_session
*session
)
1325 struct ceph_msg
*msg
;
1327 dout("request_close_session mds%d state %s seq %lld\n",
1328 session
->s_mds
, ceph_session_state_name(session
->s_state
),
1330 msg
= create_session_msg(CEPH_SESSION_REQUEST_CLOSE
, session
->s_seq
);
1333 ceph_con_send(&session
->s_con
, msg
);
1338 * Called with s_mutex held.
1340 static int __close_session(struct ceph_mds_client
*mdsc
,
1341 struct ceph_mds_session
*session
)
1343 if (session
->s_state
>= CEPH_MDS_SESSION_CLOSING
)
1345 session
->s_state
= CEPH_MDS_SESSION_CLOSING
;
1346 return request_close_session(mdsc
, session
);
1350 * Trim old(er) caps.
1352 * Because we can't cache an inode without one or more caps, we do
1353 * this indirectly: if a cap is unused, we prune its aliases, at which
1354 * point the inode will hopefully get dropped to.
1356 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1357 * memory pressure from the MDS, though, so it needn't be perfect.
1359 static int trim_caps_cb(struct inode
*inode
, struct ceph_cap
*cap
, void *arg
)
1361 struct ceph_mds_session
*session
= arg
;
1362 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1363 int used
, wanted
, oissued
, mine
;
1365 if (session
->s_trim_caps
<= 0)
1368 spin_lock(&ci
->i_ceph_lock
);
1369 mine
= cap
->issued
| cap
->implemented
;
1370 used
= __ceph_caps_used(ci
);
1371 wanted
= __ceph_caps_file_wanted(ci
);
1372 oissued
= __ceph_caps_issued_other(ci
, cap
);
1374 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
1375 inode
, cap
, ceph_cap_string(mine
), ceph_cap_string(oissued
),
1376 ceph_cap_string(used
), ceph_cap_string(wanted
));
1377 if (cap
== ci
->i_auth_cap
) {
1378 if (ci
->i_dirty_caps
|| ci
->i_flushing_caps
||
1379 !list_empty(&ci
->i_cap_snaps
))
1381 if ((used
| wanted
) & CEPH_CAP_ANY_WR
)
1384 if ((used
| wanted
) & ~oissued
& mine
)
1385 goto out
; /* we need these caps */
1387 session
->s_trim_caps
--;
1389 /* we aren't the only cap.. just remove us */
1390 __ceph_remove_cap(cap
, true);
1392 /* try to drop referring dentries */
1393 spin_unlock(&ci
->i_ceph_lock
);
1394 d_prune_aliases(inode
);
1395 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1396 inode
, cap
, atomic_read(&inode
->i_count
));
1401 spin_unlock(&ci
->i_ceph_lock
);
1406 * Trim session cap count down to some max number.
1408 static int trim_caps(struct ceph_mds_client
*mdsc
,
1409 struct ceph_mds_session
*session
,
1412 int trim_caps
= session
->s_nr_caps
- max_caps
;
1414 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1415 session
->s_mds
, session
->s_nr_caps
, max_caps
, trim_caps
);
1416 if (trim_caps
> 0) {
1417 session
->s_trim_caps
= trim_caps
;
1418 iterate_session_caps(session
, trim_caps_cb
, session
);
1419 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1420 session
->s_mds
, session
->s_nr_caps
, max_caps
,
1421 trim_caps
- session
->s_trim_caps
);
1422 session
->s_trim_caps
= 0;
1425 ceph_send_cap_releases(mdsc
, session
);
1429 static int check_cap_flush(struct ceph_inode_info
*ci
,
1430 u64 want_flush_seq
, u64 want_snap_seq
)
1432 int ret1
= 1, ret2
= 1;
1433 spin_lock(&ci
->i_ceph_lock
);
1434 if (want_flush_seq
> 0 && ci
->i_flushing_caps
)
1435 ret1
= ci
->i_cap_flush_seq
>= want_flush_seq
;
1437 if (want_snap_seq
> 0 && !list_empty(&ci
->i_cap_snaps
)) {
1438 struct ceph_cap_snap
*capsnap
=
1439 list_first_entry(&ci
->i_cap_snaps
,
1440 struct ceph_cap_snap
, ci_item
);
1441 ret2
= capsnap
->follows
>= want_snap_seq
;
1443 spin_unlock(&ci
->i_ceph_lock
);
1444 return ret1
&& ret2
;
1448 * flush all dirty inode data to disk.
1450 * returns true if we've flushed through want_flush_seq
1452 static void wait_caps_flush(struct ceph_mds_client
*mdsc
,
1453 u64 want_flush_seq
, u64 want_snap_seq
)
1457 dout("check_cap_flush want %lld\n", want_flush_seq
);
1458 mutex_lock(&mdsc
->mutex
);
1459 for (mds
= 0; mds
< mdsc
->max_sessions
; ) {
1460 struct ceph_mds_session
*session
= mdsc
->sessions
[mds
];
1461 struct inode
*inode1
= NULL
, *inode2
= NULL
;
1467 get_session(session
);
1468 mutex_unlock(&mdsc
->mutex
);
1470 mutex_lock(&session
->s_mutex
);
1471 if (!list_empty(&session
->s_cap_flushing
)) {
1472 struct ceph_inode_info
*ci
=
1473 list_first_entry(&session
->s_cap_flushing
,
1474 struct ceph_inode_info
,
1477 if (!check_cap_flush(ci
, want_flush_seq
, 0)) {
1478 dout("check_cap_flush still flushing %p "
1479 "seq %lld <= %lld to mds%d\n",
1480 &ci
->vfs_inode
, ci
->i_cap_flush_seq
,
1481 want_flush_seq
, mds
);
1482 inode1
= igrab(&ci
->vfs_inode
);
1485 if (!list_empty(&session
->s_cap_snaps_flushing
)) {
1486 struct ceph_cap_snap
*capsnap
=
1487 list_first_entry(&session
->s_cap_snaps_flushing
,
1488 struct ceph_cap_snap
,
1490 struct ceph_inode_info
*ci
= capsnap
->ci
;
1491 if (!check_cap_flush(ci
, 0, want_snap_seq
)) {
1492 dout("check_cap_flush still flushing snap %p "
1493 "follows %lld <= %lld to mds%d\n",
1494 &ci
->vfs_inode
, capsnap
->follows
,
1495 want_snap_seq
, mds
);
1496 inode2
= igrab(&ci
->vfs_inode
);
1499 mutex_unlock(&session
->s_mutex
);
1500 ceph_put_mds_session(session
);
1503 wait_event(mdsc
->cap_flushing_wq
,
1504 check_cap_flush(ceph_inode(inode1
),
1505 want_flush_seq
, 0));
1509 wait_event(mdsc
->cap_flushing_wq
,
1510 check_cap_flush(ceph_inode(inode2
),
1515 if (!inode1
&& !inode2
)
1518 mutex_lock(&mdsc
->mutex
);
1521 mutex_unlock(&mdsc
->mutex
);
1522 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq
);
1526 * called under s_mutex
1528 void ceph_send_cap_releases(struct ceph_mds_client
*mdsc
,
1529 struct ceph_mds_session
*session
)
1531 struct ceph_msg
*msg
= NULL
;
1532 struct ceph_mds_cap_release
*head
;
1533 struct ceph_mds_cap_item
*item
;
1534 struct ceph_cap
*cap
;
1535 LIST_HEAD(tmp_list
);
1536 int num_cap_releases
;
1538 spin_lock(&session
->s_cap_lock
);
1540 list_splice_init(&session
->s_cap_releases
, &tmp_list
);
1541 num_cap_releases
= session
->s_num_cap_releases
;
1542 session
->s_num_cap_releases
= 0;
1543 spin_unlock(&session
->s_cap_lock
);
1545 while (!list_empty(&tmp_list
)) {
1547 msg
= ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE
,
1548 PAGE_CACHE_SIZE
, GFP_NOFS
, false);
1551 head
= msg
->front
.iov_base
;
1552 head
->num
= cpu_to_le32(0);
1553 msg
->front
.iov_len
= sizeof(*head
);
1555 cap
= list_first_entry(&tmp_list
, struct ceph_cap
,
1557 list_del(&cap
->session_caps
);
1560 head
= msg
->front
.iov_base
;
1561 le32_add_cpu(&head
->num
, 1);
1562 item
= msg
->front
.iov_base
+ msg
->front
.iov_len
;
1563 item
->ino
= cpu_to_le64(cap
->cap_ino
);
1564 item
->cap_id
= cpu_to_le64(cap
->cap_id
);
1565 item
->migrate_seq
= cpu_to_le32(cap
->mseq
);
1566 item
->seq
= cpu_to_le32(cap
->issue_seq
);
1567 msg
->front
.iov_len
+= sizeof(*item
);
1569 ceph_put_cap(mdsc
, cap
);
1571 if (le32_to_cpu(head
->num
) == CEPH_CAPS_PER_RELEASE
) {
1572 msg
->hdr
.front_len
= cpu_to_le32(msg
->front
.iov_len
);
1573 dout("send_cap_releases mds%d %p\n", session
->s_mds
, msg
);
1574 ceph_con_send(&session
->s_con
, msg
);
1579 BUG_ON(num_cap_releases
!= 0);
1581 spin_lock(&session
->s_cap_lock
);
1582 if (!list_empty(&session
->s_cap_releases
))
1584 spin_unlock(&session
->s_cap_lock
);
1587 msg
->hdr
.front_len
= cpu_to_le32(msg
->front
.iov_len
);
1588 dout("send_cap_releases mds%d %p\n", session
->s_mds
, msg
);
1589 ceph_con_send(&session
->s_con
, msg
);
1593 pr_err("send_cap_releases mds%d, failed to allocate message\n",
1595 spin_lock(&session
->s_cap_lock
);
1596 list_splice(&tmp_list
, &session
->s_cap_releases
);
1597 session
->s_num_cap_releases
+= num_cap_releases
;
1598 spin_unlock(&session
->s_cap_lock
);
1605 int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request
*req
,
1608 struct ceph_inode_info
*ci
= ceph_inode(dir
);
1609 struct ceph_mds_reply_info_parsed
*rinfo
= &req
->r_reply_info
;
1610 struct ceph_mount_options
*opt
= req
->r_mdsc
->fsc
->mount_options
;
1611 size_t size
= sizeof(*rinfo
->dir_in
) + sizeof(*rinfo
->dir_dname_len
) +
1612 sizeof(*rinfo
->dir_dname
) + sizeof(*rinfo
->dir_dlease
);
1613 int order
, num_entries
;
1615 spin_lock(&ci
->i_ceph_lock
);
1616 num_entries
= ci
->i_files
+ ci
->i_subdirs
;
1617 spin_unlock(&ci
->i_ceph_lock
);
1618 num_entries
= max(num_entries
, 1);
1619 num_entries
= min(num_entries
, opt
->max_readdir
);
1621 order
= get_order(size
* num_entries
);
1622 while (order
>= 0) {
1623 rinfo
->dir_in
= (void*)__get_free_pages(GFP_NOFS
| __GFP_NOWARN
,
1632 num_entries
= (PAGE_SIZE
<< order
) / size
;
1633 num_entries
= min(num_entries
, opt
->max_readdir
);
1635 rinfo
->dir_buf_size
= PAGE_SIZE
<< order
;
1636 req
->r_num_caps
= num_entries
+ 1;
1637 req
->r_args
.readdir
.max_entries
= cpu_to_le32(num_entries
);
1638 req
->r_args
.readdir
.max_bytes
= cpu_to_le32(opt
->max_readdir_bytes
);
1643 * Create an mds request.
1645 struct ceph_mds_request
*
1646 ceph_mdsc_create_request(struct ceph_mds_client
*mdsc
, int op
, int mode
)
1648 struct ceph_mds_request
*req
= kzalloc(sizeof(*req
), GFP_NOFS
);
1651 return ERR_PTR(-ENOMEM
);
1653 mutex_init(&req
->r_fill_mutex
);
1655 req
->r_started
= jiffies
;
1656 req
->r_resend_mds
= -1;
1657 INIT_LIST_HEAD(&req
->r_unsafe_dir_item
);
1659 kref_init(&req
->r_kref
);
1660 INIT_LIST_HEAD(&req
->r_wait
);
1661 init_completion(&req
->r_completion
);
1662 init_completion(&req
->r_safe_completion
);
1663 INIT_LIST_HEAD(&req
->r_unsafe_item
);
1665 req
->r_stamp
= CURRENT_TIME
;
1668 req
->r_direct_mode
= mode
;
1673 * return oldest (lowest) request, tid in request tree, 0 if none.
1675 * called under mdsc->mutex.
1677 static struct ceph_mds_request
*__get_oldest_req(struct ceph_mds_client
*mdsc
)
1679 if (RB_EMPTY_ROOT(&mdsc
->request_tree
))
1681 return rb_entry(rb_first(&mdsc
->request_tree
),
1682 struct ceph_mds_request
, r_node
);
1685 static u64
__get_oldest_tid(struct ceph_mds_client
*mdsc
)
1687 struct ceph_mds_request
*req
= __get_oldest_req(mdsc
);
1695 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1696 * on build_path_from_dentry in fs/cifs/dir.c.
1698 * If @stop_on_nosnap, generate path relative to the first non-snapped
1701 * Encode hidden .snap dirs as a double /, i.e.
1702 * foo/.snap/bar -> foo//bar
1704 char *ceph_mdsc_build_path(struct dentry
*dentry
, int *plen
, u64
*base
,
1707 struct dentry
*temp
;
1713 return ERR_PTR(-EINVAL
);
1717 seq
= read_seqbegin(&rename_lock
);
1719 for (temp
= dentry
; !IS_ROOT(temp
);) {
1720 struct inode
*inode
= d_inode(temp
);
1721 if (inode
&& ceph_snap(inode
) == CEPH_SNAPDIR
)
1722 len
++; /* slash only */
1723 else if (stop_on_nosnap
&& inode
&&
1724 ceph_snap(inode
) == CEPH_NOSNAP
)
1727 len
+= 1 + temp
->d_name
.len
;
1728 temp
= temp
->d_parent
;
1732 len
--; /* no leading '/' */
1734 path
= kmalloc(len
+1, GFP_NOFS
);
1736 return ERR_PTR(-ENOMEM
);
1738 path
[pos
] = 0; /* trailing null */
1740 for (temp
= dentry
; !IS_ROOT(temp
) && pos
!= 0; ) {
1741 struct inode
*inode
;
1743 spin_lock(&temp
->d_lock
);
1744 inode
= d_inode(temp
);
1745 if (inode
&& ceph_snap(inode
) == CEPH_SNAPDIR
) {
1746 dout("build_path path+%d: %p SNAPDIR\n",
1748 } else if (stop_on_nosnap
&& inode
&&
1749 ceph_snap(inode
) == CEPH_NOSNAP
) {
1750 spin_unlock(&temp
->d_lock
);
1753 pos
-= temp
->d_name
.len
;
1755 spin_unlock(&temp
->d_lock
);
1758 strncpy(path
+ pos
, temp
->d_name
.name
,
1761 spin_unlock(&temp
->d_lock
);
1764 temp
= temp
->d_parent
;
1767 if (pos
!= 0 || read_seqretry(&rename_lock
, seq
)) {
1768 pr_err("build_path did not end path lookup where "
1769 "expected, namelen is %d, pos is %d\n", len
, pos
);
1770 /* presumably this is only possible if racing with a
1771 rename of one of the parent directories (we can not
1772 lock the dentries above us to prevent this, but
1773 retrying should be harmless) */
1778 *base
= ceph_ino(d_inode(temp
));
1780 dout("build_path on %p %d built %llx '%.*s'\n",
1781 dentry
, d_count(dentry
), *base
, len
, path
);
1785 static int build_dentry_path(struct dentry
*dentry
,
1786 const char **ppath
, int *ppathlen
, u64
*pino
,
1791 if (ceph_snap(d_inode(dentry
->d_parent
)) == CEPH_NOSNAP
) {
1792 *pino
= ceph_ino(d_inode(dentry
->d_parent
));
1793 *ppath
= dentry
->d_name
.name
;
1794 *ppathlen
= dentry
->d_name
.len
;
1797 path
= ceph_mdsc_build_path(dentry
, ppathlen
, pino
, 1);
1799 return PTR_ERR(path
);
1805 static int build_inode_path(struct inode
*inode
,
1806 const char **ppath
, int *ppathlen
, u64
*pino
,
1809 struct dentry
*dentry
;
1812 if (ceph_snap(inode
) == CEPH_NOSNAP
) {
1813 *pino
= ceph_ino(inode
);
1817 dentry
= d_find_alias(inode
);
1818 path
= ceph_mdsc_build_path(dentry
, ppathlen
, pino
, 1);
1821 return PTR_ERR(path
);
1828 * request arguments may be specified via an inode *, a dentry *, or
1829 * an explicit ino+path.
1831 static int set_request_path_attr(struct inode
*rinode
, struct dentry
*rdentry
,
1832 const char *rpath
, u64 rino
,
1833 const char **ppath
, int *pathlen
,
1834 u64
*ino
, int *freepath
)
1839 r
= build_inode_path(rinode
, ppath
, pathlen
, ino
, freepath
);
1840 dout(" inode %p %llx.%llx\n", rinode
, ceph_ino(rinode
),
1842 } else if (rdentry
) {
1843 r
= build_dentry_path(rdentry
, ppath
, pathlen
, ino
, freepath
);
1844 dout(" dentry %p %llx/%.*s\n", rdentry
, *ino
, *pathlen
,
1846 } else if (rpath
|| rino
) {
1849 *pathlen
= rpath
? strlen(rpath
) : 0;
1850 dout(" path %.*s\n", *pathlen
, rpath
);
1857 * called under mdsc->mutex
1859 static struct ceph_msg
*create_request_message(struct ceph_mds_client
*mdsc
,
1860 struct ceph_mds_request
*req
,
1861 int mds
, bool drop_cap_releases
)
1863 struct ceph_msg
*msg
;
1864 struct ceph_mds_request_head
*head
;
1865 const char *path1
= NULL
;
1866 const char *path2
= NULL
;
1867 u64 ino1
= 0, ino2
= 0;
1868 int pathlen1
= 0, pathlen2
= 0;
1869 int freepath1
= 0, freepath2
= 0;
1875 ret
= set_request_path_attr(req
->r_inode
, req
->r_dentry
,
1876 req
->r_path1
, req
->r_ino1
.ino
,
1877 &path1
, &pathlen1
, &ino1
, &freepath1
);
1883 ret
= set_request_path_attr(NULL
, req
->r_old_dentry
,
1884 req
->r_path2
, req
->r_ino2
.ino
,
1885 &path2
, &pathlen2
, &ino2
, &freepath2
);
1891 len
= sizeof(*head
) +
1892 pathlen1
+ pathlen2
+ 2*(1 + sizeof(u32
) + sizeof(u64
)) +
1893 sizeof(struct timespec
);
1895 /* calculate (max) length for cap releases */
1896 len
+= sizeof(struct ceph_mds_request_release
) *
1897 (!!req
->r_inode_drop
+ !!req
->r_dentry_drop
+
1898 !!req
->r_old_inode_drop
+ !!req
->r_old_dentry_drop
);
1899 if (req
->r_dentry_drop
)
1900 len
+= req
->r_dentry
->d_name
.len
;
1901 if (req
->r_old_dentry_drop
)
1902 len
+= req
->r_old_dentry
->d_name
.len
;
1904 msg
= ceph_msg_new(CEPH_MSG_CLIENT_REQUEST
, len
, GFP_NOFS
, false);
1906 msg
= ERR_PTR(-ENOMEM
);
1910 msg
->hdr
.version
= cpu_to_le16(2);
1911 msg
->hdr
.tid
= cpu_to_le64(req
->r_tid
);
1913 head
= msg
->front
.iov_base
;
1914 p
= msg
->front
.iov_base
+ sizeof(*head
);
1915 end
= msg
->front
.iov_base
+ msg
->front
.iov_len
;
1917 head
->mdsmap_epoch
= cpu_to_le32(mdsc
->mdsmap
->m_epoch
);
1918 head
->op
= cpu_to_le32(req
->r_op
);
1919 head
->caller_uid
= cpu_to_le32(from_kuid(&init_user_ns
, req
->r_uid
));
1920 head
->caller_gid
= cpu_to_le32(from_kgid(&init_user_ns
, req
->r_gid
));
1921 head
->args
= req
->r_args
;
1923 ceph_encode_filepath(&p
, end
, ino1
, path1
);
1924 ceph_encode_filepath(&p
, end
, ino2
, path2
);
1926 /* make note of release offset, in case we need to replay */
1927 req
->r_request_release_offset
= p
- msg
->front
.iov_base
;
1931 if (req
->r_inode_drop
)
1932 releases
+= ceph_encode_inode_release(&p
,
1933 req
->r_inode
? req
->r_inode
: d_inode(req
->r_dentry
),
1934 mds
, req
->r_inode_drop
, req
->r_inode_unless
, 0);
1935 if (req
->r_dentry_drop
)
1936 releases
+= ceph_encode_dentry_release(&p
, req
->r_dentry
,
1937 mds
, req
->r_dentry_drop
, req
->r_dentry_unless
);
1938 if (req
->r_old_dentry_drop
)
1939 releases
+= ceph_encode_dentry_release(&p
, req
->r_old_dentry
,
1940 mds
, req
->r_old_dentry_drop
, req
->r_old_dentry_unless
);
1941 if (req
->r_old_inode_drop
)
1942 releases
+= ceph_encode_inode_release(&p
,
1943 d_inode(req
->r_old_dentry
),
1944 mds
, req
->r_old_inode_drop
, req
->r_old_inode_unless
, 0);
1946 if (drop_cap_releases
) {
1948 p
= msg
->front
.iov_base
+ req
->r_request_release_offset
;
1951 head
->num_releases
= cpu_to_le16(releases
);
1955 struct ceph_timespec ts
;
1956 ceph_encode_timespec(&ts
, &req
->r_stamp
);
1957 ceph_encode_copy(&p
, &ts
, sizeof(ts
));
1961 msg
->front
.iov_len
= p
- msg
->front
.iov_base
;
1962 msg
->hdr
.front_len
= cpu_to_le32(msg
->front
.iov_len
);
1964 if (req
->r_pagelist
) {
1965 struct ceph_pagelist
*pagelist
= req
->r_pagelist
;
1966 atomic_inc(&pagelist
->refcnt
);
1967 ceph_msg_data_add_pagelist(msg
, pagelist
);
1968 msg
->hdr
.data_len
= cpu_to_le32(pagelist
->length
);
1970 msg
->hdr
.data_len
= 0;
1973 msg
->hdr
.data_off
= cpu_to_le16(0);
1977 kfree((char *)path2
);
1980 kfree((char *)path1
);
1986 * called under mdsc->mutex if error, under no mutex if
1989 static void complete_request(struct ceph_mds_client
*mdsc
,
1990 struct ceph_mds_request
*req
)
1992 if (req
->r_callback
)
1993 req
->r_callback(mdsc
, req
);
1995 complete_all(&req
->r_completion
);
1999 * called under mdsc->mutex
2001 static int __prepare_send_request(struct ceph_mds_client
*mdsc
,
2002 struct ceph_mds_request
*req
,
2003 int mds
, bool drop_cap_releases
)
2005 struct ceph_mds_request_head
*rhead
;
2006 struct ceph_msg
*msg
;
2011 struct ceph_cap
*cap
=
2012 ceph_get_cap_for_mds(ceph_inode(req
->r_inode
), mds
);
2015 req
->r_sent_on_mseq
= cap
->mseq
;
2017 req
->r_sent_on_mseq
= -1;
2019 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req
,
2020 req
->r_tid
, ceph_mds_op_name(req
->r_op
), req
->r_attempts
);
2022 if (req
->r_got_unsafe
) {
2025 * Replay. Do not regenerate message (and rebuild
2026 * paths, etc.); just use the original message.
2027 * Rebuilding paths will break for renames because
2028 * d_move mangles the src name.
2030 msg
= req
->r_request
;
2031 rhead
= msg
->front
.iov_base
;
2033 flags
= le32_to_cpu(rhead
->flags
);
2034 flags
|= CEPH_MDS_FLAG_REPLAY
;
2035 rhead
->flags
= cpu_to_le32(flags
);
2037 if (req
->r_target_inode
)
2038 rhead
->ino
= cpu_to_le64(ceph_ino(req
->r_target_inode
));
2040 rhead
->num_retry
= req
->r_attempts
- 1;
2042 /* remove cap/dentry releases from message */
2043 rhead
->num_releases
= 0;
2046 p
= msg
->front
.iov_base
+ req
->r_request_release_offset
;
2048 struct ceph_timespec ts
;
2049 ceph_encode_timespec(&ts
, &req
->r_stamp
);
2050 ceph_encode_copy(&p
, &ts
, sizeof(ts
));
2053 msg
->front
.iov_len
= p
- msg
->front
.iov_base
;
2054 msg
->hdr
.front_len
= cpu_to_le32(msg
->front
.iov_len
);
2058 if (req
->r_request
) {
2059 ceph_msg_put(req
->r_request
);
2060 req
->r_request
= NULL
;
2062 msg
= create_request_message(mdsc
, req
, mds
, drop_cap_releases
);
2064 req
->r_err
= PTR_ERR(msg
);
2065 complete_request(mdsc
, req
);
2066 return PTR_ERR(msg
);
2068 req
->r_request
= msg
;
2070 rhead
= msg
->front
.iov_base
;
2071 rhead
->oldest_client_tid
= cpu_to_le64(__get_oldest_tid(mdsc
));
2072 if (req
->r_got_unsafe
)
2073 flags
|= CEPH_MDS_FLAG_REPLAY
;
2074 if (req
->r_locked_dir
)
2075 flags
|= CEPH_MDS_FLAG_WANT_DENTRY
;
2076 rhead
->flags
= cpu_to_le32(flags
);
2077 rhead
->num_fwd
= req
->r_num_fwd
;
2078 rhead
->num_retry
= req
->r_attempts
- 1;
2081 dout(" r_locked_dir = %p\n", req
->r_locked_dir
);
2086 * send request, or put it on the appropriate wait list.
2088 static int __do_request(struct ceph_mds_client
*mdsc
,
2089 struct ceph_mds_request
*req
)
2091 struct ceph_mds_session
*session
= NULL
;
2095 if (req
->r_err
|| req
->r_got_result
) {
2097 __unregister_request(mdsc
, req
);
2101 if (req
->r_timeout
&&
2102 time_after_eq(jiffies
, req
->r_started
+ req
->r_timeout
)) {
2103 dout("do_request timed out\n");
2108 put_request_session(req
);
2110 mds
= __choose_mds(mdsc
, req
);
2112 ceph_mdsmap_get_state(mdsc
->mdsmap
, mds
) < CEPH_MDS_STATE_ACTIVE
) {
2113 dout("do_request no mds or not active, waiting for map\n");
2114 list_add(&req
->r_wait
, &mdsc
->waiting_for_map
);
2118 /* get, open session */
2119 session
= __ceph_lookup_mds_session(mdsc
, mds
);
2121 session
= register_session(mdsc
, mds
);
2122 if (IS_ERR(session
)) {
2123 err
= PTR_ERR(session
);
2127 req
->r_session
= get_session(session
);
2129 dout("do_request mds%d session %p state %s\n", mds
, session
,
2130 ceph_session_state_name(session
->s_state
));
2131 if (session
->s_state
!= CEPH_MDS_SESSION_OPEN
&&
2132 session
->s_state
!= CEPH_MDS_SESSION_HUNG
) {
2133 if (session
->s_state
== CEPH_MDS_SESSION_NEW
||
2134 session
->s_state
== CEPH_MDS_SESSION_CLOSING
)
2135 __open_session(mdsc
, session
);
2136 list_add(&req
->r_wait
, &session
->s_waiting
);
2141 req
->r_resend_mds
= -1; /* forget any previous mds hint */
2143 if (req
->r_request_started
== 0) /* note request start time */
2144 req
->r_request_started
= jiffies
;
2146 err
= __prepare_send_request(mdsc
, req
, mds
, false);
2148 ceph_msg_get(req
->r_request
);
2149 ceph_con_send(&session
->s_con
, req
->r_request
);
2153 ceph_put_mds_session(session
);
2159 complete_request(mdsc
, req
);
2164 * called under mdsc->mutex
2166 static void __wake_requests(struct ceph_mds_client
*mdsc
,
2167 struct list_head
*head
)
2169 struct ceph_mds_request
*req
;
2170 LIST_HEAD(tmp_list
);
2172 list_splice_init(head
, &tmp_list
);
2174 while (!list_empty(&tmp_list
)) {
2175 req
= list_entry(tmp_list
.next
,
2176 struct ceph_mds_request
, r_wait
);
2177 list_del_init(&req
->r_wait
);
2178 dout(" wake request %p tid %llu\n", req
, req
->r_tid
);
2179 __do_request(mdsc
, req
);
2184 * Wake up threads with requests pending for @mds, so that they can
2185 * resubmit their requests to a possibly different mds.
2187 static void kick_requests(struct ceph_mds_client
*mdsc
, int mds
)
2189 struct ceph_mds_request
*req
;
2190 struct rb_node
*p
= rb_first(&mdsc
->request_tree
);
2192 dout("kick_requests mds%d\n", mds
);
2194 req
= rb_entry(p
, struct ceph_mds_request
, r_node
);
2196 if (req
->r_got_unsafe
)
2198 if (req
->r_attempts
> 0)
2199 continue; /* only new requests */
2200 if (req
->r_session
&&
2201 req
->r_session
->s_mds
== mds
) {
2202 dout(" kicking tid %llu\n", req
->r_tid
);
2203 list_del_init(&req
->r_wait
);
2204 __do_request(mdsc
, req
);
2209 void ceph_mdsc_submit_request(struct ceph_mds_client
*mdsc
,
2210 struct ceph_mds_request
*req
)
2212 dout("submit_request on %p\n", req
);
2213 mutex_lock(&mdsc
->mutex
);
2214 __register_request(mdsc
, req
, NULL
);
2215 __do_request(mdsc
, req
);
2216 mutex_unlock(&mdsc
->mutex
);
2220 * Synchrously perform an mds request. Take care of all of the
2221 * session setup, forwarding, retry details.
2223 int ceph_mdsc_do_request(struct ceph_mds_client
*mdsc
,
2225 struct ceph_mds_request
*req
)
2229 dout("do_request on %p\n", req
);
2231 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
2233 ceph_get_cap_refs(ceph_inode(req
->r_inode
), CEPH_CAP_PIN
);
2234 if (req
->r_locked_dir
)
2235 ceph_get_cap_refs(ceph_inode(req
->r_locked_dir
), CEPH_CAP_PIN
);
2236 if (req
->r_old_dentry_dir
)
2237 ceph_get_cap_refs(ceph_inode(req
->r_old_dentry_dir
),
2241 mutex_lock(&mdsc
->mutex
);
2242 __register_request(mdsc
, req
, dir
);
2243 __do_request(mdsc
, req
);
2247 __unregister_request(mdsc
, req
);
2248 dout("do_request early error %d\n", err
);
2253 mutex_unlock(&mdsc
->mutex
);
2254 dout("do_request waiting\n");
2255 if (req
->r_timeout
) {
2256 err
= (long)wait_for_completion_killable_timeout(
2257 &req
->r_completion
, req
->r_timeout
);
2260 } else if (req
->r_wait_for_completion
) {
2261 err
= req
->r_wait_for_completion(mdsc
, req
);
2263 err
= wait_for_completion_killable(&req
->r_completion
);
2265 dout("do_request waited, got %d\n", err
);
2266 mutex_lock(&mdsc
->mutex
);
2268 /* only abort if we didn't race with a real reply */
2269 if (req
->r_got_result
) {
2270 err
= le32_to_cpu(req
->r_reply_info
.head
->result
);
2271 } else if (err
< 0) {
2272 dout("aborted request %lld with %d\n", req
->r_tid
, err
);
2275 * ensure we aren't running concurrently with
2276 * ceph_fill_trace or ceph_readdir_prepopulate, which
2277 * rely on locks (dir mutex) held by our caller.
2279 mutex_lock(&req
->r_fill_mutex
);
2281 req
->r_aborted
= true;
2282 mutex_unlock(&req
->r_fill_mutex
);
2284 if (req
->r_locked_dir
&&
2285 (req
->r_op
& CEPH_MDS_OP_WRITE
))
2286 ceph_invalidate_dir_request(req
);
2292 mutex_unlock(&mdsc
->mutex
);
2293 dout("do_request %p done, result %d\n", req
, err
);
2298 * Invalidate dir's completeness, dentry lease state on an aborted MDS
2299 * namespace request.
2301 void ceph_invalidate_dir_request(struct ceph_mds_request
*req
)
2303 struct inode
*inode
= req
->r_locked_dir
;
2305 dout("invalidate_dir_request %p (complete, lease(s))\n", inode
);
2307 ceph_dir_clear_complete(inode
);
2309 ceph_invalidate_dentry_lease(req
->r_dentry
);
2310 if (req
->r_old_dentry
)
2311 ceph_invalidate_dentry_lease(req
->r_old_dentry
);
2317 * We take the session mutex and parse and process the reply immediately.
2318 * This preserves the logical ordering of replies, capabilities, etc., sent
2319 * by the MDS as they are applied to our local cache.
2321 static void handle_reply(struct ceph_mds_session
*session
, struct ceph_msg
*msg
)
2323 struct ceph_mds_client
*mdsc
= session
->s_mdsc
;
2324 struct ceph_mds_request
*req
;
2325 struct ceph_mds_reply_head
*head
= msg
->front
.iov_base
;
2326 struct ceph_mds_reply_info_parsed
*rinfo
; /* parsed reply info */
2327 struct ceph_snap_realm
*realm
;
2330 int mds
= session
->s_mds
;
2332 if (msg
->front
.iov_len
< sizeof(*head
)) {
2333 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
2338 /* get request, session */
2339 tid
= le64_to_cpu(msg
->hdr
.tid
);
2340 mutex_lock(&mdsc
->mutex
);
2341 req
= __lookup_request(mdsc
, tid
);
2343 dout("handle_reply on unknown tid %llu\n", tid
);
2344 mutex_unlock(&mdsc
->mutex
);
2347 dout("handle_reply %p\n", req
);
2349 /* correct session? */
2350 if (req
->r_session
!= session
) {
2351 pr_err("mdsc_handle_reply got %llu on session mds%d"
2352 " not mds%d\n", tid
, session
->s_mds
,
2353 req
->r_session
? req
->r_session
->s_mds
: -1);
2354 mutex_unlock(&mdsc
->mutex
);
2359 if ((req
->r_got_unsafe
&& !head
->safe
) ||
2360 (req
->r_got_safe
&& head
->safe
)) {
2361 pr_warn("got a dup %s reply on %llu from mds%d\n",
2362 head
->safe
? "safe" : "unsafe", tid
, mds
);
2363 mutex_unlock(&mdsc
->mutex
);
2366 if (req
->r_got_safe
&& !head
->safe
) {
2367 pr_warn("got unsafe after safe on %llu from mds%d\n",
2369 mutex_unlock(&mdsc
->mutex
);
2373 result
= le32_to_cpu(head
->result
);
2377 * if we're not talking to the authority, send to them
2378 * if the authority has changed while we weren't looking,
2379 * send to new authority
2380 * Otherwise we just have to return an ESTALE
2382 if (result
== -ESTALE
) {
2383 dout("got ESTALE on request %llu", req
->r_tid
);
2384 req
->r_resend_mds
= -1;
2385 if (req
->r_direct_mode
!= USE_AUTH_MDS
) {
2386 dout("not using auth, setting for that now");
2387 req
->r_direct_mode
= USE_AUTH_MDS
;
2388 __do_request(mdsc
, req
);
2389 mutex_unlock(&mdsc
->mutex
);
2392 int mds
= __choose_mds(mdsc
, req
);
2393 if (mds
>= 0 && mds
!= req
->r_session
->s_mds
) {
2394 dout("but auth changed, so resending");
2395 __do_request(mdsc
, req
);
2396 mutex_unlock(&mdsc
->mutex
);
2400 dout("have to return ESTALE on request %llu", req
->r_tid
);
2405 req
->r_got_safe
= true;
2406 __unregister_request(mdsc
, req
);
2408 if (req
->r_got_unsafe
) {
2410 * We already handled the unsafe response, now do the
2411 * cleanup. No need to examine the response; the MDS
2412 * doesn't include any result info in the safe
2413 * response. And even if it did, there is nothing
2414 * useful we could do with a revised return value.
2416 dout("got safe reply %llu, mds%d\n", tid
, mds
);
2417 list_del_init(&req
->r_unsafe_item
);
2419 /* last unsafe request during umount? */
2420 if (mdsc
->stopping
&& !__get_oldest_req(mdsc
))
2421 complete_all(&mdsc
->safe_umount_waiters
);
2422 mutex_unlock(&mdsc
->mutex
);
2426 req
->r_got_unsafe
= true;
2427 list_add_tail(&req
->r_unsafe_item
, &req
->r_session
->s_unsafe
);
2430 dout("handle_reply tid %lld result %d\n", tid
, result
);
2431 rinfo
= &req
->r_reply_info
;
2432 err
= parse_reply_info(msg
, rinfo
, session
->s_con
.peer_features
);
2433 mutex_unlock(&mdsc
->mutex
);
2435 mutex_lock(&session
->s_mutex
);
2437 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds
, tid
);
2444 if (rinfo
->snapblob_len
) {
2445 down_write(&mdsc
->snap_rwsem
);
2446 ceph_update_snap_trace(mdsc
, rinfo
->snapblob
,
2447 rinfo
->snapblob
+ rinfo
->snapblob_len
,
2448 le32_to_cpu(head
->op
) == CEPH_MDS_OP_RMSNAP
,
2450 downgrade_write(&mdsc
->snap_rwsem
);
2452 down_read(&mdsc
->snap_rwsem
);
2455 /* insert trace into our cache */
2456 mutex_lock(&req
->r_fill_mutex
);
2457 err
= ceph_fill_trace(mdsc
->fsc
->sb
, req
, req
->r_session
);
2459 if (result
== 0 && (req
->r_op
== CEPH_MDS_OP_READDIR
||
2460 req
->r_op
== CEPH_MDS_OP_LSSNAP
))
2461 ceph_readdir_prepopulate(req
, req
->r_session
);
2462 ceph_unreserve_caps(mdsc
, &req
->r_caps_reservation
);
2464 mutex_unlock(&req
->r_fill_mutex
);
2466 up_read(&mdsc
->snap_rwsem
);
2468 ceph_put_snap_realm(mdsc
, realm
);
2470 mutex_lock(&mdsc
->mutex
);
2471 if (!req
->r_aborted
) {
2477 req
->r_got_result
= true;
2480 dout("reply arrived after request %lld was aborted\n", tid
);
2482 mutex_unlock(&mdsc
->mutex
);
2484 mutex_unlock(&session
->s_mutex
);
2486 /* kick calling process */
2487 complete_request(mdsc
, req
);
2489 ceph_mdsc_put_request(req
);
2496 * handle mds notification that our request has been forwarded.
2498 static void handle_forward(struct ceph_mds_client
*mdsc
,
2499 struct ceph_mds_session
*session
,
2500 struct ceph_msg
*msg
)
2502 struct ceph_mds_request
*req
;
2503 u64 tid
= le64_to_cpu(msg
->hdr
.tid
);
2507 void *p
= msg
->front
.iov_base
;
2508 void *end
= p
+ msg
->front
.iov_len
;
2510 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
2511 next_mds
= ceph_decode_32(&p
);
2512 fwd_seq
= ceph_decode_32(&p
);
2514 mutex_lock(&mdsc
->mutex
);
2515 req
= __lookup_request(mdsc
, tid
);
2517 dout("forward tid %llu to mds%d - req dne\n", tid
, next_mds
);
2518 goto out
; /* dup reply? */
2521 if (req
->r_aborted
) {
2522 dout("forward tid %llu aborted, unregistering\n", tid
);
2523 __unregister_request(mdsc
, req
);
2524 } else if (fwd_seq
<= req
->r_num_fwd
) {
2525 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
2526 tid
, next_mds
, req
->r_num_fwd
, fwd_seq
);
2528 /* resend. forward race not possible; mds would drop */
2529 dout("forward tid %llu to mds%d (we resend)\n", tid
, next_mds
);
2531 BUG_ON(req
->r_got_result
);
2532 req
->r_attempts
= 0;
2533 req
->r_num_fwd
= fwd_seq
;
2534 req
->r_resend_mds
= next_mds
;
2535 put_request_session(req
);
2536 __do_request(mdsc
, req
);
2538 ceph_mdsc_put_request(req
);
2540 mutex_unlock(&mdsc
->mutex
);
2544 pr_err("mdsc_handle_forward decode error err=%d\n", err
);
2548 * handle a mds session control message
2550 static void handle_session(struct ceph_mds_session
*session
,
2551 struct ceph_msg
*msg
)
2553 struct ceph_mds_client
*mdsc
= session
->s_mdsc
;
2556 int mds
= session
->s_mds
;
2557 struct ceph_mds_session_head
*h
= msg
->front
.iov_base
;
2561 if (msg
->front
.iov_len
!= sizeof(*h
))
2563 op
= le32_to_cpu(h
->op
);
2564 seq
= le64_to_cpu(h
->seq
);
2566 mutex_lock(&mdsc
->mutex
);
2567 if (op
== CEPH_SESSION_CLOSE
)
2568 __unregister_session(mdsc
, session
);
2569 /* FIXME: this ttl calculation is generous */
2570 session
->s_ttl
= jiffies
+ HZ
*mdsc
->mdsmap
->m_session_autoclose
;
2571 mutex_unlock(&mdsc
->mutex
);
2573 mutex_lock(&session
->s_mutex
);
2575 dout("handle_session mds%d %s %p state %s seq %llu\n",
2576 mds
, ceph_session_op_name(op
), session
,
2577 ceph_session_state_name(session
->s_state
), seq
);
2579 if (session
->s_state
== CEPH_MDS_SESSION_HUNG
) {
2580 session
->s_state
= CEPH_MDS_SESSION_OPEN
;
2581 pr_info("mds%d came back\n", session
->s_mds
);
2585 case CEPH_SESSION_OPEN
:
2586 if (session
->s_state
== CEPH_MDS_SESSION_RECONNECTING
)
2587 pr_info("mds%d reconnect success\n", session
->s_mds
);
2588 session
->s_state
= CEPH_MDS_SESSION_OPEN
;
2589 renewed_caps(mdsc
, session
, 0);
2592 __close_session(mdsc
, session
);
2595 case CEPH_SESSION_RENEWCAPS
:
2596 if (session
->s_renew_seq
== seq
)
2597 renewed_caps(mdsc
, session
, 1);
2600 case CEPH_SESSION_CLOSE
:
2601 if (session
->s_state
== CEPH_MDS_SESSION_RECONNECTING
)
2602 pr_info("mds%d reconnect denied\n", session
->s_mds
);
2603 cleanup_session_requests(mdsc
, session
);
2604 remove_session_caps(session
);
2605 wake
= 2; /* for good measure */
2606 wake_up_all(&mdsc
->session_close_wq
);
2609 case CEPH_SESSION_STALE
:
2610 pr_info("mds%d caps went stale, renewing\n",
2612 spin_lock(&session
->s_gen_ttl_lock
);
2613 session
->s_cap_gen
++;
2614 session
->s_cap_ttl
= jiffies
- 1;
2615 spin_unlock(&session
->s_gen_ttl_lock
);
2616 send_renew_caps(mdsc
, session
);
2619 case CEPH_SESSION_RECALL_STATE
:
2620 trim_caps(mdsc
, session
, le32_to_cpu(h
->max_caps
));
2623 case CEPH_SESSION_FLUSHMSG
:
2624 send_flushmsg_ack(mdsc
, session
, seq
);
2627 case CEPH_SESSION_FORCE_RO
:
2628 dout("force_session_readonly %p\n", session
);
2629 spin_lock(&session
->s_cap_lock
);
2630 session
->s_readonly
= true;
2631 spin_unlock(&session
->s_cap_lock
);
2632 wake_up_session_caps(session
, 0);
2636 pr_err("mdsc_handle_session bad op %d mds%d\n", op
, mds
);
2640 mutex_unlock(&session
->s_mutex
);
2642 mutex_lock(&mdsc
->mutex
);
2643 __wake_requests(mdsc
, &session
->s_waiting
);
2645 kick_requests(mdsc
, mds
);
2646 mutex_unlock(&mdsc
->mutex
);
2651 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds
,
2652 (int)msg
->front
.iov_len
);
2659 * called under session->mutex.
2661 static void replay_unsafe_requests(struct ceph_mds_client
*mdsc
,
2662 struct ceph_mds_session
*session
)
2664 struct ceph_mds_request
*req
, *nreq
;
2668 dout("replay_unsafe_requests mds%d\n", session
->s_mds
);
2670 mutex_lock(&mdsc
->mutex
);
2671 list_for_each_entry_safe(req
, nreq
, &session
->s_unsafe
, r_unsafe_item
) {
2672 err
= __prepare_send_request(mdsc
, req
, session
->s_mds
, true);
2674 ceph_msg_get(req
->r_request
);
2675 ceph_con_send(&session
->s_con
, req
->r_request
);
2680 * also re-send old requests when MDS enters reconnect stage. So that MDS
2681 * can process completed request in clientreplay stage.
2683 p
= rb_first(&mdsc
->request_tree
);
2685 req
= rb_entry(p
, struct ceph_mds_request
, r_node
);
2687 if (req
->r_got_unsafe
)
2689 if (req
->r_attempts
== 0)
2690 continue; /* only old requests */
2691 if (req
->r_session
&&
2692 req
->r_session
->s_mds
== session
->s_mds
) {
2693 err
= __prepare_send_request(mdsc
, req
,
2694 session
->s_mds
, true);
2696 ceph_msg_get(req
->r_request
);
2697 ceph_con_send(&session
->s_con
, req
->r_request
);
2701 mutex_unlock(&mdsc
->mutex
);
2705 * Encode information about a cap for a reconnect with the MDS.
2707 static int encode_caps_cb(struct inode
*inode
, struct ceph_cap
*cap
,
2711 struct ceph_mds_cap_reconnect v2
;
2712 struct ceph_mds_cap_reconnect_v1 v1
;
2715 struct ceph_inode_info
*ci
;
2716 struct ceph_reconnect_state
*recon_state
= arg
;
2717 struct ceph_pagelist
*pagelist
= recon_state
->pagelist
;
2721 struct dentry
*dentry
;
2725 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2726 inode
, ceph_vinop(inode
), cap
, cap
->cap_id
,
2727 ceph_cap_string(cap
->issued
));
2728 err
= ceph_pagelist_encode_64(pagelist
, ceph_ino(inode
));
2732 dentry
= d_find_alias(inode
);
2734 path
= ceph_mdsc_build_path(dentry
, &pathlen
, &pathbase
, 0);
2736 err
= PTR_ERR(path
);
2743 err
= ceph_pagelist_encode_string(pagelist
, path
, pathlen
);
2747 spin_lock(&ci
->i_ceph_lock
);
2748 cap
->seq
= 0; /* reset cap seq */
2749 cap
->issue_seq
= 0; /* and issue_seq */
2750 cap
->mseq
= 0; /* and migrate_seq */
2751 cap
->cap_gen
= cap
->session
->s_cap_gen
;
2753 if (recon_state
->flock
) {
2754 rec
.v2
.cap_id
= cpu_to_le64(cap
->cap_id
);
2755 rec
.v2
.wanted
= cpu_to_le32(__ceph_caps_wanted(ci
));
2756 rec
.v2
.issued
= cpu_to_le32(cap
->issued
);
2757 rec
.v2
.snaprealm
= cpu_to_le64(ci
->i_snap_realm
->ino
);
2758 rec
.v2
.pathbase
= cpu_to_le64(pathbase
);
2759 rec
.v2
.flock_len
= 0;
2760 reclen
= sizeof(rec
.v2
);
2762 rec
.v1
.cap_id
= cpu_to_le64(cap
->cap_id
);
2763 rec
.v1
.wanted
= cpu_to_le32(__ceph_caps_wanted(ci
));
2764 rec
.v1
.issued
= cpu_to_le32(cap
->issued
);
2765 rec
.v1
.size
= cpu_to_le64(inode
->i_size
);
2766 ceph_encode_timespec(&rec
.v1
.mtime
, &inode
->i_mtime
);
2767 ceph_encode_timespec(&rec
.v1
.atime
, &inode
->i_atime
);
2768 rec
.v1
.snaprealm
= cpu_to_le64(ci
->i_snap_realm
->ino
);
2769 rec
.v1
.pathbase
= cpu_to_le64(pathbase
);
2770 reclen
= sizeof(rec
.v1
);
2772 spin_unlock(&ci
->i_ceph_lock
);
2774 if (recon_state
->flock
) {
2775 int num_fcntl_locks
, num_flock_locks
;
2776 struct ceph_filelock
*flocks
;
2779 ceph_count_locks(inode
, &num_fcntl_locks
, &num_flock_locks
);
2780 flocks
= kmalloc((num_fcntl_locks
+num_flock_locks
) *
2781 sizeof(struct ceph_filelock
), GFP_NOFS
);
2786 err
= ceph_encode_locks_to_buffer(inode
, flocks
,
2796 * number of encoded locks is stable, so copy to pagelist
2798 rec
.v2
.flock_len
= cpu_to_le32(2*sizeof(u32
) +
2799 (num_fcntl_locks
+num_flock_locks
) *
2800 sizeof(struct ceph_filelock
));
2801 err
= ceph_pagelist_append(pagelist
, &rec
, reclen
);
2803 err
= ceph_locks_to_pagelist(flocks
, pagelist
,
2808 err
= ceph_pagelist_append(pagelist
, &rec
, reclen
);
2811 recon_state
->nr_caps
++;
2821 * If an MDS fails and recovers, clients need to reconnect in order to
2822 * reestablish shared state. This includes all caps issued through
2823 * this session _and_ the snap_realm hierarchy. Because it's not
2824 * clear which snap realms the mds cares about, we send everything we
2825 * know about.. that ensures we'll then get any new info the
2826 * recovering MDS might have.
2828 * This is a relatively heavyweight operation, but it's rare.
2830 * called with mdsc->mutex held.
2832 static void send_mds_reconnect(struct ceph_mds_client
*mdsc
,
2833 struct ceph_mds_session
*session
)
2835 struct ceph_msg
*reply
;
2837 int mds
= session
->s_mds
;
2840 struct ceph_pagelist
*pagelist
;
2841 struct ceph_reconnect_state recon_state
;
2843 pr_info("mds%d reconnect start\n", mds
);
2845 pagelist
= kmalloc(sizeof(*pagelist
), GFP_NOFS
);
2847 goto fail_nopagelist
;
2848 ceph_pagelist_init(pagelist
);
2850 reply
= ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT
, 0, GFP_NOFS
, false);
2854 mutex_lock(&session
->s_mutex
);
2855 session
->s_state
= CEPH_MDS_SESSION_RECONNECTING
;
2858 dout("session %p state %s\n", session
,
2859 ceph_session_state_name(session
->s_state
));
2861 spin_lock(&session
->s_gen_ttl_lock
);
2862 session
->s_cap_gen
++;
2863 spin_unlock(&session
->s_gen_ttl_lock
);
2865 spin_lock(&session
->s_cap_lock
);
2866 /* don't know if session is readonly */
2867 session
->s_readonly
= 0;
2869 * notify __ceph_remove_cap() that we are composing cap reconnect.
2870 * If a cap get released before being added to the cap reconnect,
2871 * __ceph_remove_cap() should skip queuing cap release.
2873 session
->s_cap_reconnect
= 1;
2874 /* drop old cap expires; we're about to reestablish that state */
2875 cleanup_cap_releases(mdsc
, session
);
2877 /* trim unused caps to reduce MDS's cache rejoin time */
2878 if (mdsc
->fsc
->sb
->s_root
)
2879 shrink_dcache_parent(mdsc
->fsc
->sb
->s_root
);
2881 ceph_con_close(&session
->s_con
);
2882 ceph_con_open(&session
->s_con
,
2883 CEPH_ENTITY_TYPE_MDS
, mds
,
2884 ceph_mdsmap_get_addr(mdsc
->mdsmap
, mds
));
2886 /* replay unsafe requests */
2887 replay_unsafe_requests(mdsc
, session
);
2889 down_read(&mdsc
->snap_rwsem
);
2891 /* traverse this session's caps */
2892 s_nr_caps
= session
->s_nr_caps
;
2893 err
= ceph_pagelist_encode_32(pagelist
, s_nr_caps
);
2897 recon_state
.nr_caps
= 0;
2898 recon_state
.pagelist
= pagelist
;
2899 recon_state
.flock
= session
->s_con
.peer_features
& CEPH_FEATURE_FLOCK
;
2900 err
= iterate_session_caps(session
, encode_caps_cb
, &recon_state
);
2904 spin_lock(&session
->s_cap_lock
);
2905 session
->s_cap_reconnect
= 0;
2906 spin_unlock(&session
->s_cap_lock
);
2909 * snaprealms. we provide mds with the ino, seq (version), and
2910 * parent for all of our realms. If the mds has any newer info,
2913 for (p
= rb_first(&mdsc
->snap_realms
); p
; p
= rb_next(p
)) {
2914 struct ceph_snap_realm
*realm
=
2915 rb_entry(p
, struct ceph_snap_realm
, node
);
2916 struct ceph_mds_snaprealm_reconnect sr_rec
;
2918 dout(" adding snap realm %llx seq %lld parent %llx\n",
2919 realm
->ino
, realm
->seq
, realm
->parent_ino
);
2920 sr_rec
.ino
= cpu_to_le64(realm
->ino
);
2921 sr_rec
.seq
= cpu_to_le64(realm
->seq
);
2922 sr_rec
.parent
= cpu_to_le64(realm
->parent_ino
);
2923 err
= ceph_pagelist_append(pagelist
, &sr_rec
, sizeof(sr_rec
));
2928 if (recon_state
.flock
)
2929 reply
->hdr
.version
= cpu_to_le16(2);
2931 /* raced with cap release? */
2932 if (s_nr_caps
!= recon_state
.nr_caps
) {
2933 struct page
*page
= list_first_entry(&pagelist
->head
,
2935 __le32
*addr
= kmap_atomic(page
);
2936 *addr
= cpu_to_le32(recon_state
.nr_caps
);
2937 kunmap_atomic(addr
);
2940 reply
->hdr
.data_len
= cpu_to_le32(pagelist
->length
);
2941 ceph_msg_data_add_pagelist(reply
, pagelist
);
2942 ceph_con_send(&session
->s_con
, reply
);
2944 mutex_unlock(&session
->s_mutex
);
2946 mutex_lock(&mdsc
->mutex
);
2947 __wake_requests(mdsc
, &session
->s_waiting
);
2948 mutex_unlock(&mdsc
->mutex
);
2950 up_read(&mdsc
->snap_rwsem
);
2954 ceph_msg_put(reply
);
2955 up_read(&mdsc
->snap_rwsem
);
2956 mutex_unlock(&session
->s_mutex
);
2958 ceph_pagelist_release(pagelist
);
2960 pr_err("error %d preparing reconnect for mds%d\n", err
, mds
);
2966 * compare old and new mdsmaps, kicking requests
2967 * and closing out old connections as necessary
2969 * called under mdsc->mutex.
2971 static void check_new_map(struct ceph_mds_client
*mdsc
,
2972 struct ceph_mdsmap
*newmap
,
2973 struct ceph_mdsmap
*oldmap
)
2976 int oldstate
, newstate
;
2977 struct ceph_mds_session
*s
;
2979 dout("check_new_map new %u old %u\n",
2980 newmap
->m_epoch
, oldmap
->m_epoch
);
2982 for (i
= 0; i
< oldmap
->m_max_mds
&& i
< mdsc
->max_sessions
; i
++) {
2983 if (mdsc
->sessions
[i
] == NULL
)
2985 s
= mdsc
->sessions
[i
];
2986 oldstate
= ceph_mdsmap_get_state(oldmap
, i
);
2987 newstate
= ceph_mdsmap_get_state(newmap
, i
);
2989 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
2990 i
, ceph_mds_state_name(oldstate
),
2991 ceph_mdsmap_is_laggy(oldmap
, i
) ? " (laggy)" : "",
2992 ceph_mds_state_name(newstate
),
2993 ceph_mdsmap_is_laggy(newmap
, i
) ? " (laggy)" : "",
2994 ceph_session_state_name(s
->s_state
));
2996 if (i
>= newmap
->m_max_mds
||
2997 memcmp(ceph_mdsmap_get_addr(oldmap
, i
),
2998 ceph_mdsmap_get_addr(newmap
, i
),
2999 sizeof(struct ceph_entity_addr
))) {
3000 if (s
->s_state
== CEPH_MDS_SESSION_OPENING
) {
3001 /* the session never opened, just close it
3003 __wake_requests(mdsc
, &s
->s_waiting
);
3004 __unregister_session(mdsc
, s
);
3007 mutex_unlock(&mdsc
->mutex
);
3008 mutex_lock(&s
->s_mutex
);
3009 mutex_lock(&mdsc
->mutex
);
3010 ceph_con_close(&s
->s_con
);
3011 mutex_unlock(&s
->s_mutex
);
3012 s
->s_state
= CEPH_MDS_SESSION_RESTARTING
;
3014 } else if (oldstate
== newstate
) {
3015 continue; /* nothing new with this mds */
3021 if (s
->s_state
== CEPH_MDS_SESSION_RESTARTING
&&
3022 newstate
>= CEPH_MDS_STATE_RECONNECT
) {
3023 mutex_unlock(&mdsc
->mutex
);
3024 send_mds_reconnect(mdsc
, s
);
3025 mutex_lock(&mdsc
->mutex
);
3029 * kick request on any mds that has gone active.
3031 if (oldstate
< CEPH_MDS_STATE_ACTIVE
&&
3032 newstate
>= CEPH_MDS_STATE_ACTIVE
) {
3033 if (oldstate
!= CEPH_MDS_STATE_CREATING
&&
3034 oldstate
!= CEPH_MDS_STATE_STARTING
)
3035 pr_info("mds%d recovery completed\n", s
->s_mds
);
3036 kick_requests(mdsc
, i
);
3037 ceph_kick_flushing_caps(mdsc
, s
);
3038 wake_up_session_caps(s
, 1);
3042 for (i
= 0; i
< newmap
->m_max_mds
&& i
< mdsc
->max_sessions
; i
++) {
3043 s
= mdsc
->sessions
[i
];
3046 if (!ceph_mdsmap_is_laggy(newmap
, i
))
3048 if (s
->s_state
== CEPH_MDS_SESSION_OPEN
||
3049 s
->s_state
== CEPH_MDS_SESSION_HUNG
||
3050 s
->s_state
== CEPH_MDS_SESSION_CLOSING
) {
3051 dout(" connecting to export targets of laggy mds%d\n",
3053 __open_export_target_sessions(mdsc
, s
);
3065 * caller must hold session s_mutex, dentry->d_lock
3067 void __ceph_mdsc_drop_dentry_lease(struct dentry
*dentry
)
3069 struct ceph_dentry_info
*di
= ceph_dentry(dentry
);
3071 ceph_put_mds_session(di
->lease_session
);
3072 di
->lease_session
= NULL
;
3075 static void handle_lease(struct ceph_mds_client
*mdsc
,
3076 struct ceph_mds_session
*session
,
3077 struct ceph_msg
*msg
)
3079 struct super_block
*sb
= mdsc
->fsc
->sb
;
3080 struct inode
*inode
;
3081 struct dentry
*parent
, *dentry
;
3082 struct ceph_dentry_info
*di
;
3083 int mds
= session
->s_mds
;
3084 struct ceph_mds_lease
*h
= msg
->front
.iov_base
;
3086 struct ceph_vino vino
;
3090 dout("handle_lease from mds%d\n", mds
);
3093 if (msg
->front
.iov_len
< sizeof(*h
) + sizeof(u32
))
3095 vino
.ino
= le64_to_cpu(h
->ino
);
3096 vino
.snap
= CEPH_NOSNAP
;
3097 seq
= le32_to_cpu(h
->seq
);
3098 dname
.name
= (void *)h
+ sizeof(*h
) + sizeof(u32
);
3099 dname
.len
= msg
->front
.iov_len
- sizeof(*h
) - sizeof(u32
);
3100 if (dname
.len
!= get_unaligned_le32(h
+1))
3104 inode
= ceph_find_inode(sb
, vino
);
3105 dout("handle_lease %s, ino %llx %p %.*s\n",
3106 ceph_lease_op_name(h
->action
), vino
.ino
, inode
,
3107 dname
.len
, dname
.name
);
3109 mutex_lock(&session
->s_mutex
);
3112 if (inode
== NULL
) {
3113 dout("handle_lease no inode %llx\n", vino
.ino
);
3118 parent
= d_find_alias(inode
);
3120 dout("no parent dentry on inode %p\n", inode
);
3122 goto release
; /* hrm... */
3124 dname
.hash
= full_name_hash(dname
.name
, dname
.len
);
3125 dentry
= d_lookup(parent
, &dname
);
3130 spin_lock(&dentry
->d_lock
);
3131 di
= ceph_dentry(dentry
);
3132 switch (h
->action
) {
3133 case CEPH_MDS_LEASE_REVOKE
:
3134 if (di
->lease_session
== session
) {
3135 if (ceph_seq_cmp(di
->lease_seq
, seq
) > 0)
3136 h
->seq
= cpu_to_le32(di
->lease_seq
);
3137 __ceph_mdsc_drop_dentry_lease(dentry
);
3142 case CEPH_MDS_LEASE_RENEW
:
3143 if (di
->lease_session
== session
&&
3144 di
->lease_gen
== session
->s_cap_gen
&&
3145 di
->lease_renew_from
&&
3146 di
->lease_renew_after
== 0) {
3147 unsigned long duration
=
3148 msecs_to_jiffies(le32_to_cpu(h
->duration_ms
));
3150 di
->lease_seq
= seq
;
3151 dentry
->d_time
= di
->lease_renew_from
+ duration
;
3152 di
->lease_renew_after
= di
->lease_renew_from
+
3154 di
->lease_renew_from
= 0;
3158 spin_unlock(&dentry
->d_lock
);
3165 /* let's just reuse the same message */
3166 h
->action
= CEPH_MDS_LEASE_REVOKE_ACK
;
3168 ceph_con_send(&session
->s_con
, msg
);
3172 mutex_unlock(&session
->s_mutex
);
3176 pr_err("corrupt lease message\n");
3180 void ceph_mdsc_lease_send_msg(struct ceph_mds_session
*session
,
3181 struct inode
*inode
,
3182 struct dentry
*dentry
, char action
,
3185 struct ceph_msg
*msg
;
3186 struct ceph_mds_lease
*lease
;
3187 int len
= sizeof(*lease
) + sizeof(u32
);
3190 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
3191 inode
, dentry
, ceph_lease_op_name(action
), session
->s_mds
);
3192 dnamelen
= dentry
->d_name
.len
;
3195 msg
= ceph_msg_new(CEPH_MSG_CLIENT_LEASE
, len
, GFP_NOFS
, false);
3198 lease
= msg
->front
.iov_base
;
3199 lease
->action
= action
;
3200 lease
->ino
= cpu_to_le64(ceph_vino(inode
).ino
);
3201 lease
->first
= lease
->last
= cpu_to_le64(ceph_vino(inode
).snap
);
3202 lease
->seq
= cpu_to_le32(seq
);
3203 put_unaligned_le32(dnamelen
, lease
+ 1);
3204 memcpy((void *)(lease
+ 1) + 4, dentry
->d_name
.name
, dnamelen
);
3207 * if this is a preemptive lease RELEASE, no need to
3208 * flush request stream, since the actual request will
3211 msg
->more_to_follow
= (action
== CEPH_MDS_LEASE_RELEASE
);
3213 ceph_con_send(&session
->s_con
, msg
);
3217 * Preemptively release a lease we expect to invalidate anyway.
3218 * Pass @inode always, @dentry is optional.
3220 void ceph_mdsc_lease_release(struct ceph_mds_client
*mdsc
, struct inode
*inode
,
3221 struct dentry
*dentry
)
3223 struct ceph_dentry_info
*di
;
3224 struct ceph_mds_session
*session
;
3227 BUG_ON(inode
== NULL
);
3228 BUG_ON(dentry
== NULL
);
3230 /* is dentry lease valid? */
3231 spin_lock(&dentry
->d_lock
);
3232 di
= ceph_dentry(dentry
);
3233 if (!di
|| !di
->lease_session
||
3234 di
->lease_session
->s_mds
< 0 ||
3235 di
->lease_gen
!= di
->lease_session
->s_cap_gen
||
3236 !time_before(jiffies
, dentry
->d_time
)) {
3237 dout("lease_release inode %p dentry %p -- "
3240 spin_unlock(&dentry
->d_lock
);
3244 /* we do have a lease on this dentry; note mds and seq */
3245 session
= ceph_get_mds_session(di
->lease_session
);
3246 seq
= di
->lease_seq
;
3247 __ceph_mdsc_drop_dentry_lease(dentry
);
3248 spin_unlock(&dentry
->d_lock
);
3250 dout("lease_release inode %p dentry %p to mds%d\n",
3251 inode
, dentry
, session
->s_mds
);
3252 ceph_mdsc_lease_send_msg(session
, inode
, dentry
,
3253 CEPH_MDS_LEASE_RELEASE
, seq
);
3254 ceph_put_mds_session(session
);
3258 * drop all leases (and dentry refs) in preparation for umount
3260 static void drop_leases(struct ceph_mds_client
*mdsc
)
3264 dout("drop_leases\n");
3265 mutex_lock(&mdsc
->mutex
);
3266 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
3267 struct ceph_mds_session
*s
= __ceph_lookup_mds_session(mdsc
, i
);
3270 mutex_unlock(&mdsc
->mutex
);
3271 mutex_lock(&s
->s_mutex
);
3272 mutex_unlock(&s
->s_mutex
);
3273 ceph_put_mds_session(s
);
3274 mutex_lock(&mdsc
->mutex
);
3276 mutex_unlock(&mdsc
->mutex
);
3282 * delayed work -- periodically trim expired leases, renew caps with mds
3284 static void schedule_delayed(struct ceph_mds_client
*mdsc
)
3287 unsigned hz
= round_jiffies_relative(HZ
* delay
);
3288 schedule_delayed_work(&mdsc
->delayed_work
, hz
);
3291 static void delayed_work(struct work_struct
*work
)
3294 struct ceph_mds_client
*mdsc
=
3295 container_of(work
, struct ceph_mds_client
, delayed_work
.work
);
3299 dout("mdsc delayed_work\n");
3300 ceph_check_delayed_caps(mdsc
);
3302 mutex_lock(&mdsc
->mutex
);
3303 renew_interval
= mdsc
->mdsmap
->m_session_timeout
>> 2;
3304 renew_caps
= time_after_eq(jiffies
, HZ
*renew_interval
+
3305 mdsc
->last_renew_caps
);
3307 mdsc
->last_renew_caps
= jiffies
;
3309 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
3310 struct ceph_mds_session
*s
= __ceph_lookup_mds_session(mdsc
, i
);
3313 if (s
->s_state
== CEPH_MDS_SESSION_CLOSING
) {
3314 dout("resending session close request for mds%d\n",
3316 request_close_session(mdsc
, s
);
3317 ceph_put_mds_session(s
);
3320 if (s
->s_ttl
&& time_after(jiffies
, s
->s_ttl
)) {
3321 if (s
->s_state
== CEPH_MDS_SESSION_OPEN
) {
3322 s
->s_state
= CEPH_MDS_SESSION_HUNG
;
3323 pr_info("mds%d hung\n", s
->s_mds
);
3326 if (s
->s_state
< CEPH_MDS_SESSION_OPEN
) {
3327 /* this mds is failed or recovering, just wait */
3328 ceph_put_mds_session(s
);
3331 mutex_unlock(&mdsc
->mutex
);
3333 mutex_lock(&s
->s_mutex
);
3335 send_renew_caps(mdsc
, s
);
3337 ceph_con_keepalive(&s
->s_con
);
3338 if (s
->s_state
== CEPH_MDS_SESSION_OPEN
||
3339 s
->s_state
== CEPH_MDS_SESSION_HUNG
)
3340 ceph_send_cap_releases(mdsc
, s
);
3341 mutex_unlock(&s
->s_mutex
);
3342 ceph_put_mds_session(s
);
3344 mutex_lock(&mdsc
->mutex
);
3346 mutex_unlock(&mdsc
->mutex
);
3348 schedule_delayed(mdsc
);
3351 int ceph_mdsc_init(struct ceph_fs_client
*fsc
)
3354 struct ceph_mds_client
*mdsc
;
3356 mdsc
= kzalloc(sizeof(struct ceph_mds_client
), GFP_NOFS
);
3361 mutex_init(&mdsc
->mutex
);
3362 mdsc
->mdsmap
= kzalloc(sizeof(*mdsc
->mdsmap
), GFP_NOFS
);
3363 if (mdsc
->mdsmap
== NULL
) {
3368 init_completion(&mdsc
->safe_umount_waiters
);
3369 init_waitqueue_head(&mdsc
->session_close_wq
);
3370 INIT_LIST_HEAD(&mdsc
->waiting_for_map
);
3371 mdsc
->sessions
= NULL
;
3372 atomic_set(&mdsc
->num_sessions
, 0);
3373 mdsc
->max_sessions
= 0;
3375 mdsc
->last_snap_seq
= 0;
3376 init_rwsem(&mdsc
->snap_rwsem
);
3377 mdsc
->snap_realms
= RB_ROOT
;
3378 INIT_LIST_HEAD(&mdsc
->snap_empty
);
3379 spin_lock_init(&mdsc
->snap_empty_lock
);
3381 mdsc
->request_tree
= RB_ROOT
;
3382 INIT_DELAYED_WORK(&mdsc
->delayed_work
, delayed_work
);
3383 mdsc
->last_renew_caps
= jiffies
;
3384 INIT_LIST_HEAD(&mdsc
->cap_delay_list
);
3385 spin_lock_init(&mdsc
->cap_delay_lock
);
3386 INIT_LIST_HEAD(&mdsc
->snap_flush_list
);
3387 spin_lock_init(&mdsc
->snap_flush_lock
);
3388 mdsc
->cap_flush_seq
= 0;
3389 INIT_LIST_HEAD(&mdsc
->cap_dirty
);
3390 INIT_LIST_HEAD(&mdsc
->cap_dirty_migrating
);
3391 mdsc
->num_cap_flushing
= 0;
3392 spin_lock_init(&mdsc
->cap_dirty_lock
);
3393 init_waitqueue_head(&mdsc
->cap_flushing_wq
);
3394 spin_lock_init(&mdsc
->dentry_lru_lock
);
3395 INIT_LIST_HEAD(&mdsc
->dentry_lru
);
3397 ceph_caps_init(mdsc
);
3398 ceph_adjust_min_caps(mdsc
, fsc
->min_caps
);
3400 init_rwsem(&mdsc
->pool_perm_rwsem
);
3401 mdsc
->pool_perm_tree
= RB_ROOT
;
3407 * Wait for safe replies on open mds requests. If we time out, drop
3408 * all requests from the tree to avoid dangling dentry refs.
3410 static void wait_requests(struct ceph_mds_client
*mdsc
)
3412 struct ceph_mds_request
*req
;
3413 struct ceph_fs_client
*fsc
= mdsc
->fsc
;
3415 mutex_lock(&mdsc
->mutex
);
3416 if (__get_oldest_req(mdsc
)) {
3417 mutex_unlock(&mdsc
->mutex
);
3419 dout("wait_requests waiting for requests\n");
3420 wait_for_completion_timeout(&mdsc
->safe_umount_waiters
,
3421 fsc
->client
->options
->mount_timeout
* HZ
);
3423 /* tear down remaining requests */
3424 mutex_lock(&mdsc
->mutex
);
3425 while ((req
= __get_oldest_req(mdsc
))) {
3426 dout("wait_requests timed out on tid %llu\n",
3428 __unregister_request(mdsc
, req
);
3431 mutex_unlock(&mdsc
->mutex
);
3432 dout("wait_requests done\n");
3436 * called before mount is ro, and before dentries are torn down.
3437 * (hmm, does this still race with new lookups?)
3439 void ceph_mdsc_pre_umount(struct ceph_mds_client
*mdsc
)
3441 dout("pre_umount\n");
3445 ceph_flush_dirty_caps(mdsc
);
3446 wait_requests(mdsc
);
3449 * wait for reply handlers to drop their request refs and
3450 * their inode/dcache refs
3456 * wait for all write mds requests to flush.
3458 static void wait_unsafe_requests(struct ceph_mds_client
*mdsc
, u64 want_tid
)
3460 struct ceph_mds_request
*req
= NULL
, *nextreq
;
3463 mutex_lock(&mdsc
->mutex
);
3464 dout("wait_unsafe_requests want %lld\n", want_tid
);
3466 req
= __get_oldest_req(mdsc
);
3467 while (req
&& req
->r_tid
<= want_tid
) {
3468 /* find next request */
3469 n
= rb_next(&req
->r_node
);
3471 nextreq
= rb_entry(n
, struct ceph_mds_request
, r_node
);
3474 if ((req
->r_op
& CEPH_MDS_OP_WRITE
)) {
3476 ceph_mdsc_get_request(req
);
3478 ceph_mdsc_get_request(nextreq
);
3479 mutex_unlock(&mdsc
->mutex
);
3480 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3481 req
->r_tid
, want_tid
);
3482 wait_for_completion(&req
->r_safe_completion
);
3483 mutex_lock(&mdsc
->mutex
);
3484 ceph_mdsc_put_request(req
);
3486 break; /* next dne before, so we're done! */
3487 if (RB_EMPTY_NODE(&nextreq
->r_node
)) {
3488 /* next request was removed from tree */
3489 ceph_mdsc_put_request(nextreq
);
3492 ceph_mdsc_put_request(nextreq
); /* won't go away */
3496 mutex_unlock(&mdsc
->mutex
);
3497 dout("wait_unsafe_requests done\n");
3500 void ceph_mdsc_sync(struct ceph_mds_client
*mdsc
)
3502 u64 want_tid
, want_flush
, want_snap
;
3504 if (mdsc
->fsc
->mount_state
== CEPH_MOUNT_SHUTDOWN
)
3508 mutex_lock(&mdsc
->mutex
);
3509 want_tid
= mdsc
->last_tid
;
3510 mutex_unlock(&mdsc
->mutex
);
3512 ceph_flush_dirty_caps(mdsc
);
3513 spin_lock(&mdsc
->cap_dirty_lock
);
3514 want_flush
= mdsc
->cap_flush_seq
;
3515 spin_unlock(&mdsc
->cap_dirty_lock
);
3517 down_read(&mdsc
->snap_rwsem
);
3518 want_snap
= mdsc
->last_snap_seq
;
3519 up_read(&mdsc
->snap_rwsem
);
3521 dout("sync want tid %lld flush_seq %lld snap_seq %lld\n",
3522 want_tid
, want_flush
, want_snap
);
3524 wait_unsafe_requests(mdsc
, want_tid
);
3525 wait_caps_flush(mdsc
, want_flush
, want_snap
);
3529 * true if all sessions are closed, or we force unmount
3531 static bool done_closing_sessions(struct ceph_mds_client
*mdsc
)
3533 if (mdsc
->fsc
->mount_state
== CEPH_MOUNT_SHUTDOWN
)
3535 return atomic_read(&mdsc
->num_sessions
) == 0;
3539 * called after sb is ro.
3541 void ceph_mdsc_close_sessions(struct ceph_mds_client
*mdsc
)
3543 struct ceph_mds_session
*session
;
3545 struct ceph_fs_client
*fsc
= mdsc
->fsc
;
3546 unsigned long timeout
= fsc
->client
->options
->mount_timeout
* HZ
;
3548 dout("close_sessions\n");
3550 /* close sessions */
3551 mutex_lock(&mdsc
->mutex
);
3552 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
3553 session
= __ceph_lookup_mds_session(mdsc
, i
);
3556 mutex_unlock(&mdsc
->mutex
);
3557 mutex_lock(&session
->s_mutex
);
3558 __close_session(mdsc
, session
);
3559 mutex_unlock(&session
->s_mutex
);
3560 ceph_put_mds_session(session
);
3561 mutex_lock(&mdsc
->mutex
);
3563 mutex_unlock(&mdsc
->mutex
);
3565 dout("waiting for sessions to close\n");
3566 wait_event_timeout(mdsc
->session_close_wq
, done_closing_sessions(mdsc
),
3569 /* tear down remaining sessions */
3570 mutex_lock(&mdsc
->mutex
);
3571 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
3572 if (mdsc
->sessions
[i
]) {
3573 session
= get_session(mdsc
->sessions
[i
]);
3574 __unregister_session(mdsc
, session
);
3575 mutex_unlock(&mdsc
->mutex
);
3576 mutex_lock(&session
->s_mutex
);
3577 remove_session_caps(session
);
3578 mutex_unlock(&session
->s_mutex
);
3579 ceph_put_mds_session(session
);
3580 mutex_lock(&mdsc
->mutex
);
3583 WARN_ON(!list_empty(&mdsc
->cap_delay_list
));
3584 mutex_unlock(&mdsc
->mutex
);
3586 ceph_cleanup_empty_realms(mdsc
);
3588 cancel_delayed_work_sync(&mdsc
->delayed_work
); /* cancel timer */
3593 static void ceph_mdsc_stop(struct ceph_mds_client
*mdsc
)
3596 cancel_delayed_work_sync(&mdsc
->delayed_work
); /* cancel timer */
3598 ceph_mdsmap_destroy(mdsc
->mdsmap
);
3599 kfree(mdsc
->sessions
);
3600 ceph_caps_finalize(mdsc
);
3601 ceph_pool_perm_destroy(mdsc
);
3604 void ceph_mdsc_destroy(struct ceph_fs_client
*fsc
)
3606 struct ceph_mds_client
*mdsc
= fsc
->mdsc
;
3608 dout("mdsc_destroy %p\n", mdsc
);
3609 ceph_mdsc_stop(mdsc
);
3611 /* flush out any connection work with references to us */
3616 dout("mdsc_destroy %p done\n", mdsc
);
3621 * handle mds map update.
3623 void ceph_mdsc_handle_map(struct ceph_mds_client
*mdsc
, struct ceph_msg
*msg
)
3627 void *p
= msg
->front
.iov_base
;
3628 void *end
= p
+ msg
->front
.iov_len
;
3629 struct ceph_mdsmap
*newmap
, *oldmap
;
3630 struct ceph_fsid fsid
;
3633 ceph_decode_need(&p
, end
, sizeof(fsid
)+2*sizeof(u32
), bad
);
3634 ceph_decode_copy(&p
, &fsid
, sizeof(fsid
));
3635 if (ceph_check_fsid(mdsc
->fsc
->client
, &fsid
) < 0)
3637 epoch
= ceph_decode_32(&p
);
3638 maplen
= ceph_decode_32(&p
);
3639 dout("handle_map epoch %u len %d\n", epoch
, (int)maplen
);
3641 /* do we need it? */
3642 ceph_monc_got_mdsmap(&mdsc
->fsc
->client
->monc
, epoch
);
3643 mutex_lock(&mdsc
->mutex
);
3644 if (mdsc
->mdsmap
&& epoch
<= mdsc
->mdsmap
->m_epoch
) {
3645 dout("handle_map epoch %u <= our %u\n",
3646 epoch
, mdsc
->mdsmap
->m_epoch
);
3647 mutex_unlock(&mdsc
->mutex
);
3651 newmap
= ceph_mdsmap_decode(&p
, end
);
3652 if (IS_ERR(newmap
)) {
3653 err
= PTR_ERR(newmap
);
3657 /* swap into place */
3659 oldmap
= mdsc
->mdsmap
;
3660 mdsc
->mdsmap
= newmap
;
3661 check_new_map(mdsc
, newmap
, oldmap
);
3662 ceph_mdsmap_destroy(oldmap
);
3664 mdsc
->mdsmap
= newmap
; /* first mds map */
3666 mdsc
->fsc
->sb
->s_maxbytes
= mdsc
->mdsmap
->m_max_file_size
;
3668 __wake_requests(mdsc
, &mdsc
->waiting_for_map
);
3670 mutex_unlock(&mdsc
->mutex
);
3671 schedule_delayed(mdsc
);
3675 mutex_unlock(&mdsc
->mutex
);
3677 pr_err("error decoding mdsmap %d\n", err
);
3681 static struct ceph_connection
*con_get(struct ceph_connection
*con
)
3683 struct ceph_mds_session
*s
= con
->private;
3685 if (get_session(s
)) {
3686 dout("mdsc con_get %p ok (%d)\n", s
, atomic_read(&s
->s_ref
));
3689 dout("mdsc con_get %p FAIL\n", s
);
3693 static void con_put(struct ceph_connection
*con
)
3695 struct ceph_mds_session
*s
= con
->private;
3697 dout("mdsc con_put %p (%d)\n", s
, atomic_read(&s
->s_ref
) - 1);
3698 ceph_put_mds_session(s
);
3702 * if the client is unresponsive for long enough, the mds will kill
3703 * the session entirely.
3705 static void peer_reset(struct ceph_connection
*con
)
3707 struct ceph_mds_session
*s
= con
->private;
3708 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
3710 pr_warn("mds%d closed our session\n", s
->s_mds
);
3711 send_mds_reconnect(mdsc
, s
);
3714 static void dispatch(struct ceph_connection
*con
, struct ceph_msg
*msg
)
3716 struct ceph_mds_session
*s
= con
->private;
3717 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
3718 int type
= le16_to_cpu(msg
->hdr
.type
);
3720 mutex_lock(&mdsc
->mutex
);
3721 if (__verify_registered_session(mdsc
, s
) < 0) {
3722 mutex_unlock(&mdsc
->mutex
);
3725 mutex_unlock(&mdsc
->mutex
);
3728 case CEPH_MSG_MDS_MAP
:
3729 ceph_mdsc_handle_map(mdsc
, msg
);
3731 case CEPH_MSG_CLIENT_SESSION
:
3732 handle_session(s
, msg
);
3734 case CEPH_MSG_CLIENT_REPLY
:
3735 handle_reply(s
, msg
);
3737 case CEPH_MSG_CLIENT_REQUEST_FORWARD
:
3738 handle_forward(mdsc
, s
, msg
);
3740 case CEPH_MSG_CLIENT_CAPS
:
3741 ceph_handle_caps(s
, msg
);
3743 case CEPH_MSG_CLIENT_SNAP
:
3744 ceph_handle_snap(mdsc
, s
, msg
);
3746 case CEPH_MSG_CLIENT_LEASE
:
3747 handle_lease(mdsc
, s
, msg
);
3751 pr_err("received unknown message type %d %s\n", type
,
3752 ceph_msg_type_name(type
));
3763 * Note: returned pointer is the address of a structure that's
3764 * managed separately. Caller must *not* attempt to free it.
3766 static struct ceph_auth_handshake
*get_authorizer(struct ceph_connection
*con
,
3767 int *proto
, int force_new
)
3769 struct ceph_mds_session
*s
= con
->private;
3770 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
3771 struct ceph_auth_client
*ac
= mdsc
->fsc
->client
->monc
.auth
;
3772 struct ceph_auth_handshake
*auth
= &s
->s_auth
;
3774 if (force_new
&& auth
->authorizer
) {
3775 ceph_auth_destroy_authorizer(ac
, auth
->authorizer
);
3776 auth
->authorizer
= NULL
;
3778 if (!auth
->authorizer
) {
3779 int ret
= ceph_auth_create_authorizer(ac
, CEPH_ENTITY_TYPE_MDS
,
3782 return ERR_PTR(ret
);
3784 int ret
= ceph_auth_update_authorizer(ac
, CEPH_ENTITY_TYPE_MDS
,
3787 return ERR_PTR(ret
);
3789 *proto
= ac
->protocol
;
3795 static int verify_authorizer_reply(struct ceph_connection
*con
, int len
)
3797 struct ceph_mds_session
*s
= con
->private;
3798 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
3799 struct ceph_auth_client
*ac
= mdsc
->fsc
->client
->monc
.auth
;
3801 return ceph_auth_verify_authorizer_reply(ac
, s
->s_auth
.authorizer
, len
);
3804 static int invalidate_authorizer(struct ceph_connection
*con
)
3806 struct ceph_mds_session
*s
= con
->private;
3807 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
3808 struct ceph_auth_client
*ac
= mdsc
->fsc
->client
->monc
.auth
;
3810 ceph_auth_invalidate_authorizer(ac
, CEPH_ENTITY_TYPE_MDS
);
3812 return ceph_monc_validate_auth(&mdsc
->fsc
->client
->monc
);
3815 static struct ceph_msg
*mds_alloc_msg(struct ceph_connection
*con
,
3816 struct ceph_msg_header
*hdr
, int *skip
)
3818 struct ceph_msg
*msg
;
3819 int type
= (int) le16_to_cpu(hdr
->type
);
3820 int front_len
= (int) le32_to_cpu(hdr
->front_len
);
3826 msg
= ceph_msg_new(type
, front_len
, GFP_NOFS
, false);
3828 pr_err("unable to allocate msg type %d len %d\n",
3836 static int sign_message(struct ceph_connection
*con
, struct ceph_msg
*msg
)
3838 struct ceph_mds_session
*s
= con
->private;
3839 struct ceph_auth_handshake
*auth
= &s
->s_auth
;
3840 return ceph_auth_sign_message(auth
, msg
);
3843 static int check_message_signature(struct ceph_connection
*con
, struct ceph_msg
*msg
)
3845 struct ceph_mds_session
*s
= con
->private;
3846 struct ceph_auth_handshake
*auth
= &s
->s_auth
;
3847 return ceph_auth_check_message_signature(auth
, msg
);
3850 static const struct ceph_connection_operations mds_con_ops
= {
3853 .dispatch
= dispatch
,
3854 .get_authorizer
= get_authorizer
,
3855 .verify_authorizer_reply
= verify_authorizer_reply
,
3856 .invalidate_authorizer
= invalidate_authorizer
,
3857 .peer_reset
= peer_reset
,
3858 .alloc_msg
= mds_alloc_msg
,
3859 .sign_message
= sign_message
,
3860 .check_message_signature
= check_message_signature
,