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>
11 #include <linux/ratelimit.h>
14 #include "mds_client.h"
16 #include <linux/ceph/ceph_features.h>
17 #include <linux/ceph/messenger.h>
18 #include <linux/ceph/decode.h>
19 #include <linux/ceph/pagelist.h>
20 #include <linux/ceph/auth.h>
21 #include <linux/ceph/debugfs.h>
24 * A cluster of MDS (metadata server) daemons is responsible for
25 * managing the file system namespace (the directory hierarchy and
26 * inodes) and for coordinating shared access to storage. Metadata is
27 * partitioning hierarchically across a number of servers, and that
28 * partition varies over time as the cluster adjusts the distribution
29 * in order to balance load.
31 * The MDS client is primarily responsible to managing synchronous
32 * metadata requests for operations like open, unlink, and so forth.
33 * If there is a MDS failure, we find out about it when we (possibly
34 * request and) receive a new MDS map, and can resubmit affected
37 * For the most part, though, we take advantage of a lossless
38 * communications channel to the MDS, and do not need to worry about
39 * timing out or resubmitting requests.
41 * We maintain a stateful "session" with each MDS we interact with.
42 * Within each session, we sent periodic heartbeat messages to ensure
43 * any capabilities or leases we have been issues remain valid. If
44 * the session times out and goes stale, our leases and capabilities
45 * are no longer valid.
48 struct ceph_reconnect_state
{
50 struct ceph_pagelist
*pagelist
;
54 static void __wake_requests(struct ceph_mds_client
*mdsc
,
55 struct list_head
*head
);
57 static const struct ceph_connection_operations mds_con_ops
;
65 * parse individual inode info
67 static int parse_reply_info_in(void **p
, void *end
,
68 struct ceph_mds_reply_info_in
*info
,
74 *p
+= sizeof(struct ceph_mds_reply_inode
) +
75 sizeof(*info
->in
->fragtree
.splits
) *
76 le32_to_cpu(info
->in
->fragtree
.nsplits
);
78 ceph_decode_32_safe(p
, end
, info
->symlink_len
, bad
);
79 ceph_decode_need(p
, end
, info
->symlink_len
, bad
);
81 *p
+= info
->symlink_len
;
83 if (features
& CEPH_FEATURE_DIRLAYOUTHASH
)
84 ceph_decode_copy_safe(p
, end
, &info
->dir_layout
,
85 sizeof(info
->dir_layout
), bad
);
87 memset(&info
->dir_layout
, 0, sizeof(info
->dir_layout
));
89 ceph_decode_32_safe(p
, end
, info
->xattr_len
, bad
);
90 ceph_decode_need(p
, end
, info
->xattr_len
, bad
);
91 info
->xattr_data
= *p
;
92 *p
+= info
->xattr_len
;
94 if (features
& CEPH_FEATURE_MDS_INLINE_DATA
) {
95 ceph_decode_64_safe(p
, end
, info
->inline_version
, bad
);
96 ceph_decode_32_safe(p
, end
, info
->inline_len
, bad
);
97 ceph_decode_need(p
, end
, info
->inline_len
, bad
);
98 info
->inline_data
= *p
;
99 *p
+= info
->inline_len
;
101 info
->inline_version
= CEPH_INLINE_NONE
;
109 * parse a normal reply, which may contain a (dir+)dentry and/or a
112 static int parse_reply_info_trace(void **p
, void *end
,
113 struct ceph_mds_reply_info_parsed
*info
,
118 if (info
->head
->is_dentry
) {
119 err
= parse_reply_info_in(p
, end
, &info
->diri
, features
);
123 if (unlikely(*p
+ sizeof(*info
->dirfrag
) > end
))
126 *p
+= sizeof(*info
->dirfrag
) +
127 sizeof(u32
)*le32_to_cpu(info
->dirfrag
->ndist
);
128 if (unlikely(*p
> end
))
131 ceph_decode_32_safe(p
, end
, info
->dname_len
, bad
);
132 ceph_decode_need(p
, end
, info
->dname_len
, bad
);
134 *p
+= info
->dname_len
;
136 *p
+= sizeof(*info
->dlease
);
139 if (info
->head
->is_target
) {
140 err
= parse_reply_info_in(p
, end
, &info
->targeti
, features
);
145 if (unlikely(*p
!= end
))
152 pr_err("problem parsing mds trace %d\n", err
);
157 * parse readdir results
159 static int parse_reply_info_dir(void **p
, void *end
,
160 struct ceph_mds_reply_info_parsed
*info
,
167 if (*p
+ sizeof(*info
->dir_dir
) > end
)
169 *p
+= sizeof(*info
->dir_dir
) +
170 sizeof(u32
)*le32_to_cpu(info
->dir_dir
->ndist
);
174 ceph_decode_need(p
, end
, sizeof(num
) + 2, bad
);
175 num
= ceph_decode_32(p
);
176 info
->dir_end
= ceph_decode_8(p
);
177 info
->dir_complete
= ceph_decode_8(p
);
181 BUG_ON(!info
->dir_in
);
182 info
->dir_dname
= (void *)(info
->dir_in
+ num
);
183 info
->dir_dname_len
= (void *)(info
->dir_dname
+ num
);
184 info
->dir_dlease
= (void *)(info
->dir_dname_len
+ num
);
185 if ((unsigned long)(info
->dir_dlease
+ num
) >
186 (unsigned long)info
->dir_in
+ info
->dir_buf_size
) {
187 pr_err("dir contents are larger than expected\n");
195 ceph_decode_need(p
, end
, sizeof(u32
)*2, bad
);
196 info
->dir_dname_len
[i
] = ceph_decode_32(p
);
197 ceph_decode_need(p
, end
, info
->dir_dname_len
[i
], bad
);
198 info
->dir_dname
[i
] = *p
;
199 *p
+= info
->dir_dname_len
[i
];
200 dout("parsed dir dname '%.*s'\n", info
->dir_dname_len
[i
],
202 info
->dir_dlease
[i
] = *p
;
203 *p
+= sizeof(struct ceph_mds_reply_lease
);
206 err
= parse_reply_info_in(p
, end
, &info
->dir_in
[i
], features
);
221 pr_err("problem parsing dir contents %d\n", err
);
226 * parse fcntl F_GETLK results
228 static int parse_reply_info_filelock(void **p
, void *end
,
229 struct ceph_mds_reply_info_parsed
*info
,
232 if (*p
+ sizeof(*info
->filelock_reply
) > end
)
235 info
->filelock_reply
= *p
;
236 *p
+= sizeof(*info
->filelock_reply
);
238 if (unlikely(*p
!= end
))
247 * parse create results
249 static int parse_reply_info_create(void **p
, void *end
,
250 struct ceph_mds_reply_info_parsed
*info
,
253 if (features
& CEPH_FEATURE_REPLY_CREATE_INODE
) {
255 info
->has_create_ino
= false;
257 info
->has_create_ino
= true;
258 info
->ino
= ceph_decode_64(p
);
262 if (unlikely(*p
!= end
))
271 * parse extra results
273 static int parse_reply_info_extra(void **p
, void *end
,
274 struct ceph_mds_reply_info_parsed
*info
,
277 u32 op
= le32_to_cpu(info
->head
->op
);
279 if (op
== CEPH_MDS_OP_GETFILELOCK
)
280 return parse_reply_info_filelock(p
, end
, info
, features
);
281 else if (op
== CEPH_MDS_OP_READDIR
|| op
== CEPH_MDS_OP_LSSNAP
)
282 return parse_reply_info_dir(p
, end
, info
, features
);
283 else if (op
== CEPH_MDS_OP_CREATE
)
284 return parse_reply_info_create(p
, end
, info
, features
);
290 * parse entire mds reply
292 static int parse_reply_info(struct ceph_msg
*msg
,
293 struct ceph_mds_reply_info_parsed
*info
,
300 info
->head
= msg
->front
.iov_base
;
301 p
= msg
->front
.iov_base
+ sizeof(struct ceph_mds_reply_head
);
302 end
= p
+ msg
->front
.iov_len
- sizeof(struct ceph_mds_reply_head
);
305 ceph_decode_32_safe(&p
, end
, len
, bad
);
307 ceph_decode_need(&p
, end
, len
, bad
);
308 err
= parse_reply_info_trace(&p
, p
+len
, info
, features
);
314 ceph_decode_32_safe(&p
, end
, len
, bad
);
316 ceph_decode_need(&p
, end
, len
, bad
);
317 err
= parse_reply_info_extra(&p
, p
+len
, info
, features
);
323 ceph_decode_32_safe(&p
, end
, len
, bad
);
324 info
->snapblob_len
= len
;
335 pr_err("mds parse_reply err %d\n", err
);
339 static void destroy_reply_info(struct ceph_mds_reply_info_parsed
*info
)
343 free_pages((unsigned long)info
->dir_in
, get_order(info
->dir_buf_size
));
350 const char *ceph_session_state_name(int s
)
353 case CEPH_MDS_SESSION_NEW
: return "new";
354 case CEPH_MDS_SESSION_OPENING
: return "opening";
355 case CEPH_MDS_SESSION_OPEN
: return "open";
356 case CEPH_MDS_SESSION_HUNG
: return "hung";
357 case CEPH_MDS_SESSION_CLOSING
: return "closing";
358 case CEPH_MDS_SESSION_RESTARTING
: return "restarting";
359 case CEPH_MDS_SESSION_RECONNECTING
: return "reconnecting";
360 default: return "???";
364 static struct ceph_mds_session
*get_session(struct ceph_mds_session
*s
)
366 if (atomic_inc_not_zero(&s
->s_ref
)) {
367 dout("mdsc get_session %p %d -> %d\n", s
,
368 atomic_read(&s
->s_ref
)-1, atomic_read(&s
->s_ref
));
371 dout("mdsc get_session %p 0 -- FAIL", s
);
376 void ceph_put_mds_session(struct ceph_mds_session
*s
)
378 dout("mdsc put_session %p %d -> %d\n", s
,
379 atomic_read(&s
->s_ref
), atomic_read(&s
->s_ref
)-1);
380 if (atomic_dec_and_test(&s
->s_ref
)) {
381 if (s
->s_auth
.authorizer
)
382 ceph_auth_destroy_authorizer(
383 s
->s_mdsc
->fsc
->client
->monc
.auth
,
384 s
->s_auth
.authorizer
);
390 * called under mdsc->mutex
392 struct ceph_mds_session
*__ceph_lookup_mds_session(struct ceph_mds_client
*mdsc
,
395 struct ceph_mds_session
*session
;
397 if (mds
>= mdsc
->max_sessions
|| mdsc
->sessions
[mds
] == NULL
)
399 session
= mdsc
->sessions
[mds
];
400 dout("lookup_mds_session %p %d\n", session
,
401 atomic_read(&session
->s_ref
));
402 get_session(session
);
406 static bool __have_session(struct ceph_mds_client
*mdsc
, int mds
)
408 if (mds
>= mdsc
->max_sessions
)
410 return mdsc
->sessions
[mds
];
413 static int __verify_registered_session(struct ceph_mds_client
*mdsc
,
414 struct ceph_mds_session
*s
)
416 if (s
->s_mds
>= mdsc
->max_sessions
||
417 mdsc
->sessions
[s
->s_mds
] != s
)
423 * create+register a new session for given mds.
424 * called under mdsc->mutex.
426 static struct ceph_mds_session
*register_session(struct ceph_mds_client
*mdsc
,
429 struct ceph_mds_session
*s
;
431 if (mds
>= mdsc
->mdsmap
->m_max_mds
)
432 return ERR_PTR(-EINVAL
);
434 s
= kzalloc(sizeof(*s
), GFP_NOFS
);
436 return ERR_PTR(-ENOMEM
);
439 s
->s_state
= CEPH_MDS_SESSION_NEW
;
442 mutex_init(&s
->s_mutex
);
444 ceph_con_init(&s
->s_con
, s
, &mds_con_ops
, &mdsc
->fsc
->client
->msgr
);
446 spin_lock_init(&s
->s_gen_ttl_lock
);
448 s
->s_cap_ttl
= jiffies
- 1;
450 spin_lock_init(&s
->s_cap_lock
);
451 s
->s_renew_requested
= 0;
453 INIT_LIST_HEAD(&s
->s_caps
);
456 atomic_set(&s
->s_ref
, 1);
457 INIT_LIST_HEAD(&s
->s_waiting
);
458 INIT_LIST_HEAD(&s
->s_unsafe
);
459 s
->s_num_cap_releases
= 0;
460 s
->s_cap_reconnect
= 0;
461 s
->s_cap_iterator
= NULL
;
462 INIT_LIST_HEAD(&s
->s_cap_releases
);
463 INIT_LIST_HEAD(&s
->s_cap_flushing
);
464 INIT_LIST_HEAD(&s
->s_cap_snaps_flushing
);
466 dout("register_session mds%d\n", mds
);
467 if (mds
>= mdsc
->max_sessions
) {
468 int newmax
= 1 << get_count_order(mds
+1);
469 struct ceph_mds_session
**sa
;
471 dout("register_session realloc to %d\n", newmax
);
472 sa
= kcalloc(newmax
, sizeof(void *), GFP_NOFS
);
475 if (mdsc
->sessions
) {
476 memcpy(sa
, mdsc
->sessions
,
477 mdsc
->max_sessions
* sizeof(void *));
478 kfree(mdsc
->sessions
);
481 mdsc
->max_sessions
= newmax
;
483 mdsc
->sessions
[mds
] = s
;
484 atomic_inc(&mdsc
->num_sessions
);
485 atomic_inc(&s
->s_ref
); /* one ref to sessions[], one to caller */
487 ceph_con_open(&s
->s_con
, CEPH_ENTITY_TYPE_MDS
, mds
,
488 ceph_mdsmap_get_addr(mdsc
->mdsmap
, mds
));
494 return ERR_PTR(-ENOMEM
);
498 * called under mdsc->mutex
500 static void __unregister_session(struct ceph_mds_client
*mdsc
,
501 struct ceph_mds_session
*s
)
503 dout("__unregister_session mds%d %p\n", s
->s_mds
, s
);
504 BUG_ON(mdsc
->sessions
[s
->s_mds
] != s
);
505 mdsc
->sessions
[s
->s_mds
] = NULL
;
506 ceph_con_close(&s
->s_con
);
507 ceph_put_mds_session(s
);
508 atomic_dec(&mdsc
->num_sessions
);
512 * drop session refs in request.
514 * should be last request ref, or hold mdsc->mutex
516 static void put_request_session(struct ceph_mds_request
*req
)
518 if (req
->r_session
) {
519 ceph_put_mds_session(req
->r_session
);
520 req
->r_session
= NULL
;
524 void ceph_mdsc_release_request(struct kref
*kref
)
526 struct ceph_mds_request
*req
= container_of(kref
,
527 struct ceph_mds_request
,
529 destroy_reply_info(&req
->r_reply_info
);
531 ceph_msg_put(req
->r_request
);
533 ceph_msg_put(req
->r_reply
);
535 ceph_put_cap_refs(ceph_inode(req
->r_inode
), CEPH_CAP_PIN
);
538 if (req
->r_locked_dir
)
539 ceph_put_cap_refs(ceph_inode(req
->r_locked_dir
), CEPH_CAP_PIN
);
540 iput(req
->r_target_inode
);
543 if (req
->r_old_dentry
)
544 dput(req
->r_old_dentry
);
545 if (req
->r_old_dentry_dir
) {
547 * track (and drop pins for) r_old_dentry_dir
548 * separately, since r_old_dentry's d_parent may have
549 * changed between the dir mutex being dropped and
550 * this request being freed.
552 ceph_put_cap_refs(ceph_inode(req
->r_old_dentry_dir
),
554 iput(req
->r_old_dentry_dir
);
559 ceph_pagelist_release(req
->r_pagelist
);
560 put_request_session(req
);
561 ceph_unreserve_caps(req
->r_mdsc
, &req
->r_caps_reservation
);
566 * lookup session, bump ref if found.
568 * called under mdsc->mutex.
570 static struct ceph_mds_request
*__lookup_request(struct ceph_mds_client
*mdsc
,
573 struct ceph_mds_request
*req
;
574 struct rb_node
*n
= mdsc
->request_tree
.rb_node
;
577 req
= rb_entry(n
, struct ceph_mds_request
, r_node
);
578 if (tid
< req
->r_tid
)
580 else if (tid
> req
->r_tid
)
583 ceph_mdsc_get_request(req
);
590 static void __insert_request(struct ceph_mds_client
*mdsc
,
591 struct ceph_mds_request
*new)
593 struct rb_node
**p
= &mdsc
->request_tree
.rb_node
;
594 struct rb_node
*parent
= NULL
;
595 struct ceph_mds_request
*req
= NULL
;
599 req
= rb_entry(parent
, struct ceph_mds_request
, r_node
);
600 if (new->r_tid
< req
->r_tid
)
602 else if (new->r_tid
> req
->r_tid
)
608 rb_link_node(&new->r_node
, parent
, p
);
609 rb_insert_color(&new->r_node
, &mdsc
->request_tree
);
613 * Register an in-flight request, and assign a tid. Link to directory
614 * are modifying (if any).
616 * Called under mdsc->mutex.
618 static void __register_request(struct ceph_mds_client
*mdsc
,
619 struct ceph_mds_request
*req
,
622 req
->r_tid
= ++mdsc
->last_tid
;
624 ceph_reserve_caps(mdsc
, &req
->r_caps_reservation
,
626 dout("__register_request %p tid %lld\n", req
, req
->r_tid
);
627 ceph_mdsc_get_request(req
);
628 __insert_request(mdsc
, req
);
630 req
->r_uid
= current_fsuid();
631 req
->r_gid
= current_fsgid();
633 if (mdsc
->oldest_tid
== 0 && req
->r_op
!= CEPH_MDS_OP_SETFILELOCK
)
634 mdsc
->oldest_tid
= req
->r_tid
;
638 req
->r_unsafe_dir
= dir
;
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
);
647 /* Never leave an unregistered request on an unsafe list! */
648 list_del_init(&req
->r_unsafe_item
);
650 if (req
->r_tid
== mdsc
->oldest_tid
) {
651 struct rb_node
*p
= rb_next(&req
->r_node
);
652 mdsc
->oldest_tid
= 0;
654 struct ceph_mds_request
*next_req
=
655 rb_entry(p
, struct ceph_mds_request
, r_node
);
656 if (next_req
->r_op
!= CEPH_MDS_OP_SETFILELOCK
) {
657 mdsc
->oldest_tid
= next_req
->r_tid
;
664 rb_erase(&req
->r_node
, &mdsc
->request_tree
);
665 RB_CLEAR_NODE(&req
->r_node
);
667 if (req
->r_unsafe_dir
&& req
->r_got_unsafe
) {
668 struct ceph_inode_info
*ci
= ceph_inode(req
->r_unsafe_dir
);
669 spin_lock(&ci
->i_unsafe_lock
);
670 list_del_init(&req
->r_unsafe_dir_item
);
671 spin_unlock(&ci
->i_unsafe_lock
);
673 if (req
->r_target_inode
&& req
->r_got_unsafe
) {
674 struct ceph_inode_info
*ci
= ceph_inode(req
->r_target_inode
);
675 spin_lock(&ci
->i_unsafe_lock
);
676 list_del_init(&req
->r_unsafe_target_item
);
677 spin_unlock(&ci
->i_unsafe_lock
);
680 if (req
->r_unsafe_dir
) {
681 iput(req
->r_unsafe_dir
);
682 req
->r_unsafe_dir
= NULL
;
685 complete_all(&req
->r_safe_completion
);
687 ceph_mdsc_put_request(req
);
691 * Choose mds to send request to next. If there is a hint set in the
692 * request (e.g., due to a prior forward hint from the mds), use that.
693 * Otherwise, consult frag tree and/or caps to identify the
694 * appropriate mds. If all else fails, choose randomly.
696 * Called under mdsc->mutex.
698 static struct dentry
*get_nonsnap_parent(struct dentry
*dentry
)
701 * we don't need to worry about protecting the d_parent access
702 * here because we never renaming inside the snapped namespace
703 * except to resplice to another snapdir, and either the old or new
704 * result is a valid result.
706 while (!IS_ROOT(dentry
) && ceph_snap(d_inode(dentry
)) != CEPH_NOSNAP
)
707 dentry
= dentry
->d_parent
;
711 static int __choose_mds(struct ceph_mds_client
*mdsc
,
712 struct ceph_mds_request
*req
)
715 struct ceph_inode_info
*ci
;
716 struct ceph_cap
*cap
;
717 int mode
= req
->r_direct_mode
;
719 u32 hash
= req
->r_direct_hash
;
720 bool is_hash
= req
->r_direct_is_hash
;
723 * is there a specific mds we should try? ignore hint if we have
724 * no session and the mds is not up (active or recovering).
726 if (req
->r_resend_mds
>= 0 &&
727 (__have_session(mdsc
, req
->r_resend_mds
) ||
728 ceph_mdsmap_get_state(mdsc
->mdsmap
, req
->r_resend_mds
) > 0)) {
729 dout("choose_mds using resend_mds mds%d\n",
731 return req
->r_resend_mds
;
734 if (mode
== USE_RANDOM_MDS
)
739 inode
= req
->r_inode
;
740 } else if (req
->r_dentry
) {
741 /* ignore race with rename; old or new d_parent is okay */
742 struct dentry
*parent
= req
->r_dentry
->d_parent
;
743 struct inode
*dir
= d_inode(parent
);
745 if (dir
->i_sb
!= mdsc
->fsc
->sb
) {
747 inode
= d_inode(req
->r_dentry
);
748 } else if (ceph_snap(dir
) != CEPH_NOSNAP
) {
749 /* direct snapped/virtual snapdir requests
750 * based on parent dir inode */
751 struct dentry
*dn
= get_nonsnap_parent(parent
);
753 dout("__choose_mds using nonsnap parent %p\n", inode
);
756 inode
= d_inode(req
->r_dentry
);
757 if (!inode
|| mode
== USE_AUTH_MDS
) {
760 hash
= ceph_dentry_hash(dir
, req
->r_dentry
);
766 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode
, (int)is_hash
,
770 ci
= ceph_inode(inode
);
772 if (is_hash
&& S_ISDIR(inode
->i_mode
)) {
773 struct ceph_inode_frag frag
;
776 ceph_choose_frag(ci
, hash
, &frag
, &found
);
778 if (mode
== USE_ANY_MDS
&& frag
.ndist
> 0) {
781 /* choose a random replica */
782 get_random_bytes(&r
, 1);
785 dout("choose_mds %p %llx.%llx "
786 "frag %u mds%d (%d/%d)\n",
787 inode
, ceph_vinop(inode
),
790 if (ceph_mdsmap_get_state(mdsc
->mdsmap
, mds
) >=
791 CEPH_MDS_STATE_ACTIVE
)
795 /* since this file/dir wasn't known to be
796 * replicated, then we want to look for the
797 * authoritative mds. */
800 /* choose auth mds */
802 dout("choose_mds %p %llx.%llx "
803 "frag %u mds%d (auth)\n",
804 inode
, ceph_vinop(inode
), frag
.frag
, mds
);
805 if (ceph_mdsmap_get_state(mdsc
->mdsmap
, mds
) >=
806 CEPH_MDS_STATE_ACTIVE
)
812 spin_lock(&ci
->i_ceph_lock
);
814 if (mode
== USE_AUTH_MDS
)
815 cap
= ci
->i_auth_cap
;
816 if (!cap
&& !RB_EMPTY_ROOT(&ci
->i_caps
))
817 cap
= rb_entry(rb_first(&ci
->i_caps
), struct ceph_cap
, ci_node
);
819 spin_unlock(&ci
->i_ceph_lock
);
822 mds
= cap
->session
->s_mds
;
823 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
824 inode
, ceph_vinop(inode
), mds
,
825 cap
== ci
->i_auth_cap
? "auth " : "", cap
);
826 spin_unlock(&ci
->i_ceph_lock
);
830 mds
= ceph_mdsmap_get_random_mds(mdsc
->mdsmap
);
831 dout("choose_mds chose random mds%d\n", mds
);
839 static struct ceph_msg
*create_session_msg(u32 op
, u64 seq
)
841 struct ceph_msg
*msg
;
842 struct ceph_mds_session_head
*h
;
844 msg
= ceph_msg_new(CEPH_MSG_CLIENT_SESSION
, sizeof(*h
), GFP_NOFS
,
847 pr_err("create_session_msg ENOMEM creating msg\n");
850 h
= msg
->front
.iov_base
;
851 h
->op
= cpu_to_le32(op
);
852 h
->seq
= cpu_to_le64(seq
);
858 * session message, specialization for CEPH_SESSION_REQUEST_OPEN
859 * to include additional client metadata fields.
861 static struct ceph_msg
*create_session_open_msg(struct ceph_mds_client
*mdsc
, u64 seq
)
863 struct ceph_msg
*msg
;
864 struct ceph_mds_session_head
*h
;
866 int metadata_bytes
= 0;
867 int metadata_key_count
= 0;
868 struct ceph_options
*opt
= mdsc
->fsc
->client
->options
;
871 const char* metadata
[][2] = {
872 {"hostname", utsname()->nodename
},
873 {"kernel_version", utsname()->release
},
874 {"entity_id", opt
->name
? opt
->name
: ""},
878 /* Calculate serialized length of metadata */
879 metadata_bytes
= 4; /* map length */
880 for (i
= 0; metadata
[i
][0] != NULL
; ++i
) {
881 metadata_bytes
+= 8 + strlen(metadata
[i
][0]) +
882 strlen(metadata
[i
][1]);
883 metadata_key_count
++;
886 /* Allocate the message */
887 msg
= ceph_msg_new(CEPH_MSG_CLIENT_SESSION
, sizeof(*h
) + metadata_bytes
,
890 pr_err("create_session_msg ENOMEM creating msg\n");
893 h
= msg
->front
.iov_base
;
894 h
->op
= cpu_to_le32(CEPH_SESSION_REQUEST_OPEN
);
895 h
->seq
= cpu_to_le64(seq
);
898 * Serialize client metadata into waiting buffer space, using
899 * the format that userspace expects for map<string, string>
901 * ClientSession messages with metadata are v2
903 msg
->hdr
.version
= cpu_to_le16(2);
904 msg
->hdr
.compat_version
= cpu_to_le16(1);
906 /* The write pointer, following the session_head structure */
907 p
= msg
->front
.iov_base
+ sizeof(*h
);
909 /* Number of entries in the map */
910 ceph_encode_32(&p
, metadata_key_count
);
912 /* Two length-prefixed strings for each entry in the map */
913 for (i
= 0; metadata
[i
][0] != NULL
; ++i
) {
914 size_t const key_len
= strlen(metadata
[i
][0]);
915 size_t const val_len
= strlen(metadata
[i
][1]);
917 ceph_encode_32(&p
, key_len
);
918 memcpy(p
, metadata
[i
][0], key_len
);
920 ceph_encode_32(&p
, val_len
);
921 memcpy(p
, metadata
[i
][1], val_len
);
929 * send session open request.
931 * called under mdsc->mutex
933 static int __open_session(struct ceph_mds_client
*mdsc
,
934 struct ceph_mds_session
*session
)
936 struct ceph_msg
*msg
;
938 int mds
= session
->s_mds
;
940 /* wait for mds to go active? */
941 mstate
= ceph_mdsmap_get_state(mdsc
->mdsmap
, mds
);
942 dout("open_session to mds%d (%s)\n", mds
,
943 ceph_mds_state_name(mstate
));
944 session
->s_state
= CEPH_MDS_SESSION_OPENING
;
945 session
->s_renew_requested
= jiffies
;
947 /* send connect message */
948 msg
= create_session_open_msg(mdsc
, session
->s_seq
);
951 ceph_con_send(&session
->s_con
, msg
);
956 * open sessions for any export targets for the given mds
958 * called under mdsc->mutex
960 static struct ceph_mds_session
*
961 __open_export_target_session(struct ceph_mds_client
*mdsc
, int target
)
963 struct ceph_mds_session
*session
;
965 session
= __ceph_lookup_mds_session(mdsc
, target
);
967 session
= register_session(mdsc
, target
);
971 if (session
->s_state
== CEPH_MDS_SESSION_NEW
||
972 session
->s_state
== CEPH_MDS_SESSION_CLOSING
)
973 __open_session(mdsc
, session
);
978 struct ceph_mds_session
*
979 ceph_mdsc_open_export_target_session(struct ceph_mds_client
*mdsc
, int target
)
981 struct ceph_mds_session
*session
;
983 dout("open_export_target_session to mds%d\n", target
);
985 mutex_lock(&mdsc
->mutex
);
986 session
= __open_export_target_session(mdsc
, target
);
987 mutex_unlock(&mdsc
->mutex
);
992 static void __open_export_target_sessions(struct ceph_mds_client
*mdsc
,
993 struct ceph_mds_session
*session
)
995 struct ceph_mds_info
*mi
;
996 struct ceph_mds_session
*ts
;
997 int i
, mds
= session
->s_mds
;
999 if (mds
>= mdsc
->mdsmap
->m_max_mds
)
1002 mi
= &mdsc
->mdsmap
->m_info
[mds
];
1003 dout("open_export_target_sessions for mds%d (%d targets)\n",
1004 session
->s_mds
, mi
->num_export_targets
);
1006 for (i
= 0; i
< mi
->num_export_targets
; i
++) {
1007 ts
= __open_export_target_session(mdsc
, mi
->export_targets
[i
]);
1009 ceph_put_mds_session(ts
);
1013 void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client
*mdsc
,
1014 struct ceph_mds_session
*session
)
1016 mutex_lock(&mdsc
->mutex
);
1017 __open_export_target_sessions(mdsc
, session
);
1018 mutex_unlock(&mdsc
->mutex
);
1025 /* caller holds s_cap_lock, we drop it */
1026 static void cleanup_cap_releases(struct ceph_mds_client
*mdsc
,
1027 struct ceph_mds_session
*session
)
1028 __releases(session
->s_cap_lock
)
1030 LIST_HEAD(tmp_list
);
1031 list_splice_init(&session
->s_cap_releases
, &tmp_list
);
1032 session
->s_num_cap_releases
= 0;
1033 spin_unlock(&session
->s_cap_lock
);
1035 dout("cleanup_cap_releases mds%d\n", session
->s_mds
);
1036 while (!list_empty(&tmp_list
)) {
1037 struct ceph_cap
*cap
;
1038 /* zero out the in-progress message */
1039 cap
= list_first_entry(&tmp_list
,
1040 struct ceph_cap
, session_caps
);
1041 list_del(&cap
->session_caps
);
1042 ceph_put_cap(mdsc
, cap
);
1046 static void cleanup_session_requests(struct ceph_mds_client
*mdsc
,
1047 struct ceph_mds_session
*session
)
1049 struct ceph_mds_request
*req
;
1052 dout("cleanup_session_requests mds%d\n", session
->s_mds
);
1053 mutex_lock(&mdsc
->mutex
);
1054 while (!list_empty(&session
->s_unsafe
)) {
1055 req
= list_first_entry(&session
->s_unsafe
,
1056 struct ceph_mds_request
, r_unsafe_item
);
1057 pr_warn_ratelimited(" dropping unsafe request %llu\n",
1059 __unregister_request(mdsc
, req
);
1061 /* zero r_attempts, so kick_requests() will re-send requests */
1062 p
= rb_first(&mdsc
->request_tree
);
1064 req
= rb_entry(p
, struct ceph_mds_request
, r_node
);
1066 if (req
->r_session
&&
1067 req
->r_session
->s_mds
== session
->s_mds
)
1068 req
->r_attempts
= 0;
1070 mutex_unlock(&mdsc
->mutex
);
1074 * Helper to safely iterate over all caps associated with a session, with
1075 * special care taken to handle a racing __ceph_remove_cap().
1077 * Caller must hold session s_mutex.
1079 static int iterate_session_caps(struct ceph_mds_session
*session
,
1080 int (*cb
)(struct inode
*, struct ceph_cap
*,
1083 struct list_head
*p
;
1084 struct ceph_cap
*cap
;
1085 struct inode
*inode
, *last_inode
= NULL
;
1086 struct ceph_cap
*old_cap
= NULL
;
1089 dout("iterate_session_caps %p mds%d\n", session
, session
->s_mds
);
1090 spin_lock(&session
->s_cap_lock
);
1091 p
= session
->s_caps
.next
;
1092 while (p
!= &session
->s_caps
) {
1093 cap
= list_entry(p
, struct ceph_cap
, session_caps
);
1094 inode
= igrab(&cap
->ci
->vfs_inode
);
1099 session
->s_cap_iterator
= cap
;
1100 spin_unlock(&session
->s_cap_lock
);
1107 ceph_put_cap(session
->s_mdsc
, old_cap
);
1111 ret
= cb(inode
, cap
, arg
);
1114 spin_lock(&session
->s_cap_lock
);
1116 if (cap
->ci
== NULL
) {
1117 dout("iterate_session_caps finishing cap %p removal\n",
1119 BUG_ON(cap
->session
!= session
);
1120 cap
->session
= NULL
;
1121 list_del_init(&cap
->session_caps
);
1122 session
->s_nr_caps
--;
1123 if (cap
->queue_release
) {
1124 list_add_tail(&cap
->session_caps
,
1125 &session
->s_cap_releases
);
1126 session
->s_num_cap_releases
++;
1128 old_cap
= cap
; /* put_cap it w/o locks held */
1136 session
->s_cap_iterator
= NULL
;
1137 spin_unlock(&session
->s_cap_lock
);
1141 ceph_put_cap(session
->s_mdsc
, old_cap
);
1146 static int remove_session_caps_cb(struct inode
*inode
, struct ceph_cap
*cap
,
1149 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1150 LIST_HEAD(to_remove
);
1153 dout("removing cap %p, ci is %p, inode is %p\n",
1154 cap
, ci
, &ci
->vfs_inode
);
1155 spin_lock(&ci
->i_ceph_lock
);
1156 __ceph_remove_cap(cap
, false);
1157 if (!ci
->i_auth_cap
) {
1158 struct ceph_cap_flush
*cf
;
1159 struct ceph_mds_client
*mdsc
=
1160 ceph_sb_to_client(inode
->i_sb
)->mdsc
;
1163 struct rb_node
*n
= rb_first(&ci
->i_cap_flush_tree
);
1166 cf
= rb_entry(n
, struct ceph_cap_flush
, i_node
);
1167 rb_erase(&cf
->i_node
, &ci
->i_cap_flush_tree
);
1168 list_add(&cf
->list
, &to_remove
);
1171 spin_lock(&mdsc
->cap_dirty_lock
);
1173 list_for_each_entry(cf
, &to_remove
, list
)
1174 rb_erase(&cf
->g_node
, &mdsc
->cap_flush_tree
);
1176 if (!list_empty(&ci
->i_dirty_item
)) {
1177 pr_warn_ratelimited(
1178 " dropping dirty %s state for %p %lld\n",
1179 ceph_cap_string(ci
->i_dirty_caps
),
1180 inode
, ceph_ino(inode
));
1181 ci
->i_dirty_caps
= 0;
1182 list_del_init(&ci
->i_dirty_item
);
1185 if (!list_empty(&ci
->i_flushing_item
)) {
1186 pr_warn_ratelimited(
1187 " dropping dirty+flushing %s state for %p %lld\n",
1188 ceph_cap_string(ci
->i_flushing_caps
),
1189 inode
, ceph_ino(inode
));
1190 ci
->i_flushing_caps
= 0;
1191 list_del_init(&ci
->i_flushing_item
);
1192 mdsc
->num_cap_flushing
--;
1195 spin_unlock(&mdsc
->cap_dirty_lock
);
1197 if (!ci
->i_dirty_caps
&& ci
->i_prealloc_cap_flush
) {
1198 list_add(&ci
->i_prealloc_cap_flush
->list
, &to_remove
);
1199 ci
->i_prealloc_cap_flush
= NULL
;
1202 spin_unlock(&ci
->i_ceph_lock
);
1203 while (!list_empty(&to_remove
)) {
1204 struct ceph_cap_flush
*cf
;
1205 cf
= list_first_entry(&to_remove
,
1206 struct ceph_cap_flush
, list
);
1207 list_del(&cf
->list
);
1208 ceph_free_cap_flush(cf
);
1216 * caller must hold session s_mutex
1218 static void remove_session_caps(struct ceph_mds_session
*session
)
1220 dout("remove_session_caps on %p\n", session
);
1221 iterate_session_caps(session
, remove_session_caps_cb
, NULL
);
1223 spin_lock(&session
->s_cap_lock
);
1224 if (session
->s_nr_caps
> 0) {
1225 struct super_block
*sb
= session
->s_mdsc
->fsc
->sb
;
1226 struct inode
*inode
;
1227 struct ceph_cap
*cap
, *prev
= NULL
;
1228 struct ceph_vino vino
;
1230 * iterate_session_caps() skips inodes that are being
1231 * deleted, we need to wait until deletions are complete.
1232 * __wait_on_freeing_inode() is designed for the job,
1233 * but it is not exported, so use lookup inode function
1236 while (!list_empty(&session
->s_caps
)) {
1237 cap
= list_entry(session
->s_caps
.next
,
1238 struct ceph_cap
, session_caps
);
1242 vino
= cap
->ci
->i_vino
;
1243 spin_unlock(&session
->s_cap_lock
);
1245 inode
= ceph_find_inode(sb
, vino
);
1248 spin_lock(&session
->s_cap_lock
);
1252 // drop cap expires and unlock s_cap_lock
1253 cleanup_cap_releases(session
->s_mdsc
, session
);
1255 BUG_ON(session
->s_nr_caps
> 0);
1256 BUG_ON(!list_empty(&session
->s_cap_flushing
));
1260 * wake up any threads waiting on this session's caps. if the cap is
1261 * old (didn't get renewed on the client reconnect), remove it now.
1263 * caller must hold s_mutex.
1265 static int wake_up_session_cb(struct inode
*inode
, struct ceph_cap
*cap
,
1268 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1270 wake_up_all(&ci
->i_cap_wq
);
1272 spin_lock(&ci
->i_ceph_lock
);
1273 ci
->i_wanted_max_size
= 0;
1274 ci
->i_requested_max_size
= 0;
1275 spin_unlock(&ci
->i_ceph_lock
);
1280 static void wake_up_session_caps(struct ceph_mds_session
*session
,
1283 dout("wake_up_session_caps %p mds%d\n", session
, session
->s_mds
);
1284 iterate_session_caps(session
, wake_up_session_cb
,
1285 (void *)(unsigned long)reconnect
);
1289 * Send periodic message to MDS renewing all currently held caps. The
1290 * ack will reset the expiration for all caps from this session.
1292 * caller holds s_mutex
1294 static int send_renew_caps(struct ceph_mds_client
*mdsc
,
1295 struct ceph_mds_session
*session
)
1297 struct ceph_msg
*msg
;
1300 if (time_after_eq(jiffies
, session
->s_cap_ttl
) &&
1301 time_after_eq(session
->s_cap_ttl
, session
->s_renew_requested
))
1302 pr_info("mds%d caps stale\n", session
->s_mds
);
1303 session
->s_renew_requested
= jiffies
;
1305 /* do not try to renew caps until a recovering mds has reconnected
1306 * with its clients. */
1307 state
= ceph_mdsmap_get_state(mdsc
->mdsmap
, session
->s_mds
);
1308 if (state
< CEPH_MDS_STATE_RECONNECT
) {
1309 dout("send_renew_caps ignoring mds%d (%s)\n",
1310 session
->s_mds
, ceph_mds_state_name(state
));
1314 dout("send_renew_caps to mds%d (%s)\n", session
->s_mds
,
1315 ceph_mds_state_name(state
));
1316 msg
= create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS
,
1317 ++session
->s_renew_seq
);
1320 ceph_con_send(&session
->s_con
, msg
);
1324 static int send_flushmsg_ack(struct ceph_mds_client
*mdsc
,
1325 struct ceph_mds_session
*session
, u64 seq
)
1327 struct ceph_msg
*msg
;
1329 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
1330 session
->s_mds
, ceph_session_state_name(session
->s_state
), seq
);
1331 msg
= create_session_msg(CEPH_SESSION_FLUSHMSG_ACK
, seq
);
1334 ceph_con_send(&session
->s_con
, msg
);
1340 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
1342 * Called under session->s_mutex
1344 static void renewed_caps(struct ceph_mds_client
*mdsc
,
1345 struct ceph_mds_session
*session
, int is_renew
)
1350 spin_lock(&session
->s_cap_lock
);
1351 was_stale
= is_renew
&& time_after_eq(jiffies
, session
->s_cap_ttl
);
1353 session
->s_cap_ttl
= session
->s_renew_requested
+
1354 mdsc
->mdsmap
->m_session_timeout
*HZ
;
1357 if (time_before(jiffies
, session
->s_cap_ttl
)) {
1358 pr_info("mds%d caps renewed\n", session
->s_mds
);
1361 pr_info("mds%d caps still stale\n", session
->s_mds
);
1364 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1365 session
->s_mds
, session
->s_cap_ttl
, was_stale
? "stale" : "fresh",
1366 time_before(jiffies
, session
->s_cap_ttl
) ? "stale" : "fresh");
1367 spin_unlock(&session
->s_cap_lock
);
1370 wake_up_session_caps(session
, 0);
1374 * send a session close request
1376 static int request_close_session(struct ceph_mds_client
*mdsc
,
1377 struct ceph_mds_session
*session
)
1379 struct ceph_msg
*msg
;
1381 dout("request_close_session mds%d state %s seq %lld\n",
1382 session
->s_mds
, ceph_session_state_name(session
->s_state
),
1384 msg
= create_session_msg(CEPH_SESSION_REQUEST_CLOSE
, session
->s_seq
);
1387 ceph_con_send(&session
->s_con
, msg
);
1392 * Called with s_mutex held.
1394 static int __close_session(struct ceph_mds_client
*mdsc
,
1395 struct ceph_mds_session
*session
)
1397 if (session
->s_state
>= CEPH_MDS_SESSION_CLOSING
)
1399 session
->s_state
= CEPH_MDS_SESSION_CLOSING
;
1400 return request_close_session(mdsc
, session
);
1404 * Trim old(er) caps.
1406 * Because we can't cache an inode without one or more caps, we do
1407 * this indirectly: if a cap is unused, we prune its aliases, at which
1408 * point the inode will hopefully get dropped to.
1410 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1411 * memory pressure from the MDS, though, so it needn't be perfect.
1413 static int trim_caps_cb(struct inode
*inode
, struct ceph_cap
*cap
, void *arg
)
1415 struct ceph_mds_session
*session
= arg
;
1416 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1417 int used
, wanted
, oissued
, mine
;
1419 if (session
->s_trim_caps
<= 0)
1422 spin_lock(&ci
->i_ceph_lock
);
1423 mine
= cap
->issued
| cap
->implemented
;
1424 used
= __ceph_caps_used(ci
);
1425 wanted
= __ceph_caps_file_wanted(ci
);
1426 oissued
= __ceph_caps_issued_other(ci
, cap
);
1428 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
1429 inode
, cap
, ceph_cap_string(mine
), ceph_cap_string(oissued
),
1430 ceph_cap_string(used
), ceph_cap_string(wanted
));
1431 if (cap
== ci
->i_auth_cap
) {
1432 if (ci
->i_dirty_caps
|| ci
->i_flushing_caps
||
1433 !list_empty(&ci
->i_cap_snaps
))
1435 if ((used
| wanted
) & CEPH_CAP_ANY_WR
)
1438 /* The inode has cached pages, but it's no longer used.
1439 * we can safely drop it */
1440 if (wanted
== 0 && used
== CEPH_CAP_FILE_CACHE
&&
1441 !(oissued
& CEPH_CAP_FILE_CACHE
)) {
1445 if ((used
| wanted
) & ~oissued
& mine
)
1446 goto out
; /* we need these caps */
1448 session
->s_trim_caps
--;
1450 /* we aren't the only cap.. just remove us */
1451 __ceph_remove_cap(cap
, true);
1453 /* try dropping referring dentries */
1454 spin_unlock(&ci
->i_ceph_lock
);
1455 d_prune_aliases(inode
);
1456 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1457 inode
, cap
, atomic_read(&inode
->i_count
));
1462 spin_unlock(&ci
->i_ceph_lock
);
1467 * Trim session cap count down to some max number.
1469 static int trim_caps(struct ceph_mds_client
*mdsc
,
1470 struct ceph_mds_session
*session
,
1473 int trim_caps
= session
->s_nr_caps
- max_caps
;
1475 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1476 session
->s_mds
, session
->s_nr_caps
, max_caps
, trim_caps
);
1477 if (trim_caps
> 0) {
1478 session
->s_trim_caps
= trim_caps
;
1479 iterate_session_caps(session
, trim_caps_cb
, session
);
1480 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1481 session
->s_mds
, session
->s_nr_caps
, max_caps
,
1482 trim_caps
- session
->s_trim_caps
);
1483 session
->s_trim_caps
= 0;
1486 ceph_send_cap_releases(mdsc
, session
);
1490 static int check_capsnap_flush(struct ceph_inode_info
*ci
,
1494 spin_lock(&ci
->i_ceph_lock
);
1495 if (want_snap_seq
> 0 && !list_empty(&ci
->i_cap_snaps
)) {
1496 struct ceph_cap_snap
*capsnap
=
1497 list_first_entry(&ci
->i_cap_snaps
,
1498 struct ceph_cap_snap
, ci_item
);
1499 ret
= capsnap
->follows
>= want_snap_seq
;
1501 spin_unlock(&ci
->i_ceph_lock
);
1505 static int check_caps_flush(struct ceph_mds_client
*mdsc
,
1509 struct ceph_cap_flush
*cf
;
1512 spin_lock(&mdsc
->cap_dirty_lock
);
1513 n
= rb_first(&mdsc
->cap_flush_tree
);
1514 cf
= n
? rb_entry(n
, struct ceph_cap_flush
, g_node
) : NULL
;
1515 if (cf
&& cf
->tid
<= want_flush_tid
) {
1516 dout("check_caps_flush still flushing tid %llu <= %llu\n",
1517 cf
->tid
, want_flush_tid
);
1520 spin_unlock(&mdsc
->cap_dirty_lock
);
1525 * flush all dirty inode data to disk.
1527 * returns true if we've flushed through want_flush_tid
1529 static void wait_caps_flush(struct ceph_mds_client
*mdsc
,
1530 u64 want_flush_tid
, u64 want_snap_seq
)
1534 dout("check_caps_flush want %llu snap want %llu\n",
1535 want_flush_tid
, want_snap_seq
);
1536 mutex_lock(&mdsc
->mutex
);
1537 for (mds
= 0; mds
< mdsc
->max_sessions
; ) {
1538 struct ceph_mds_session
*session
= mdsc
->sessions
[mds
];
1539 struct inode
*inode
= NULL
;
1545 get_session(session
);
1546 mutex_unlock(&mdsc
->mutex
);
1548 mutex_lock(&session
->s_mutex
);
1549 if (!list_empty(&session
->s_cap_snaps_flushing
)) {
1550 struct ceph_cap_snap
*capsnap
=
1551 list_first_entry(&session
->s_cap_snaps_flushing
,
1552 struct ceph_cap_snap
,
1554 struct ceph_inode_info
*ci
= capsnap
->ci
;
1555 if (!check_capsnap_flush(ci
, want_snap_seq
)) {
1556 dout("check_cap_flush still flushing snap %p "
1557 "follows %lld <= %lld to mds%d\n",
1558 &ci
->vfs_inode
, capsnap
->follows
,
1559 want_snap_seq
, mds
);
1560 inode
= igrab(&ci
->vfs_inode
);
1563 mutex_unlock(&session
->s_mutex
);
1564 ceph_put_mds_session(session
);
1567 wait_event(mdsc
->cap_flushing_wq
,
1568 check_capsnap_flush(ceph_inode(inode
),
1575 mutex_lock(&mdsc
->mutex
);
1577 mutex_unlock(&mdsc
->mutex
);
1579 wait_event(mdsc
->cap_flushing_wq
,
1580 check_caps_flush(mdsc
, want_flush_tid
));
1582 dout("check_caps_flush ok, flushed thru %llu\n", want_flush_tid
);
1586 * called under s_mutex
1588 void ceph_send_cap_releases(struct ceph_mds_client
*mdsc
,
1589 struct ceph_mds_session
*session
)
1591 struct ceph_msg
*msg
= NULL
;
1592 struct ceph_mds_cap_release
*head
;
1593 struct ceph_mds_cap_item
*item
;
1594 struct ceph_cap
*cap
;
1595 LIST_HEAD(tmp_list
);
1596 int num_cap_releases
;
1598 spin_lock(&session
->s_cap_lock
);
1600 list_splice_init(&session
->s_cap_releases
, &tmp_list
);
1601 num_cap_releases
= session
->s_num_cap_releases
;
1602 session
->s_num_cap_releases
= 0;
1603 spin_unlock(&session
->s_cap_lock
);
1605 while (!list_empty(&tmp_list
)) {
1607 msg
= ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE
,
1608 PAGE_CACHE_SIZE
, GFP_NOFS
, false);
1611 head
= msg
->front
.iov_base
;
1612 head
->num
= cpu_to_le32(0);
1613 msg
->front
.iov_len
= sizeof(*head
);
1615 cap
= list_first_entry(&tmp_list
, struct ceph_cap
,
1617 list_del(&cap
->session_caps
);
1620 head
= msg
->front
.iov_base
;
1621 le32_add_cpu(&head
->num
, 1);
1622 item
= msg
->front
.iov_base
+ msg
->front
.iov_len
;
1623 item
->ino
= cpu_to_le64(cap
->cap_ino
);
1624 item
->cap_id
= cpu_to_le64(cap
->cap_id
);
1625 item
->migrate_seq
= cpu_to_le32(cap
->mseq
);
1626 item
->seq
= cpu_to_le32(cap
->issue_seq
);
1627 msg
->front
.iov_len
+= sizeof(*item
);
1629 ceph_put_cap(mdsc
, cap
);
1631 if (le32_to_cpu(head
->num
) == CEPH_CAPS_PER_RELEASE
) {
1632 msg
->hdr
.front_len
= cpu_to_le32(msg
->front
.iov_len
);
1633 dout("send_cap_releases mds%d %p\n", session
->s_mds
, msg
);
1634 ceph_con_send(&session
->s_con
, msg
);
1639 BUG_ON(num_cap_releases
!= 0);
1641 spin_lock(&session
->s_cap_lock
);
1642 if (!list_empty(&session
->s_cap_releases
))
1644 spin_unlock(&session
->s_cap_lock
);
1647 msg
->hdr
.front_len
= cpu_to_le32(msg
->front
.iov_len
);
1648 dout("send_cap_releases mds%d %p\n", session
->s_mds
, msg
);
1649 ceph_con_send(&session
->s_con
, msg
);
1653 pr_err("send_cap_releases mds%d, failed to allocate message\n",
1655 spin_lock(&session
->s_cap_lock
);
1656 list_splice(&tmp_list
, &session
->s_cap_releases
);
1657 session
->s_num_cap_releases
+= num_cap_releases
;
1658 spin_unlock(&session
->s_cap_lock
);
1665 int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request
*req
,
1668 struct ceph_inode_info
*ci
= ceph_inode(dir
);
1669 struct ceph_mds_reply_info_parsed
*rinfo
= &req
->r_reply_info
;
1670 struct ceph_mount_options
*opt
= req
->r_mdsc
->fsc
->mount_options
;
1671 size_t size
= sizeof(*rinfo
->dir_in
) + sizeof(*rinfo
->dir_dname_len
) +
1672 sizeof(*rinfo
->dir_dname
) + sizeof(*rinfo
->dir_dlease
);
1673 int order
, num_entries
;
1675 spin_lock(&ci
->i_ceph_lock
);
1676 num_entries
= ci
->i_files
+ ci
->i_subdirs
;
1677 spin_unlock(&ci
->i_ceph_lock
);
1678 num_entries
= max(num_entries
, 1);
1679 num_entries
= min(num_entries
, opt
->max_readdir
);
1681 order
= get_order(size
* num_entries
);
1682 while (order
>= 0) {
1683 rinfo
->dir_in
= (void*)__get_free_pages(GFP_KERNEL
|
1693 num_entries
= (PAGE_SIZE
<< order
) / size
;
1694 num_entries
= min(num_entries
, opt
->max_readdir
);
1696 rinfo
->dir_buf_size
= PAGE_SIZE
<< order
;
1697 req
->r_num_caps
= num_entries
+ 1;
1698 req
->r_args
.readdir
.max_entries
= cpu_to_le32(num_entries
);
1699 req
->r_args
.readdir
.max_bytes
= cpu_to_le32(opt
->max_readdir_bytes
);
1704 * Create an mds request.
1706 struct ceph_mds_request
*
1707 ceph_mdsc_create_request(struct ceph_mds_client
*mdsc
, int op
, int mode
)
1709 struct ceph_mds_request
*req
= kzalloc(sizeof(*req
), GFP_NOFS
);
1712 return ERR_PTR(-ENOMEM
);
1714 mutex_init(&req
->r_fill_mutex
);
1716 req
->r_started
= jiffies
;
1717 req
->r_resend_mds
= -1;
1718 INIT_LIST_HEAD(&req
->r_unsafe_dir_item
);
1719 INIT_LIST_HEAD(&req
->r_unsafe_target_item
);
1721 kref_init(&req
->r_kref
);
1722 INIT_LIST_HEAD(&req
->r_wait
);
1723 init_completion(&req
->r_completion
);
1724 init_completion(&req
->r_safe_completion
);
1725 INIT_LIST_HEAD(&req
->r_unsafe_item
);
1727 req
->r_stamp
= CURRENT_TIME
;
1730 req
->r_direct_mode
= mode
;
1735 * return oldest (lowest) request, tid in request tree, 0 if none.
1737 * called under mdsc->mutex.
1739 static struct ceph_mds_request
*__get_oldest_req(struct ceph_mds_client
*mdsc
)
1741 if (RB_EMPTY_ROOT(&mdsc
->request_tree
))
1743 return rb_entry(rb_first(&mdsc
->request_tree
),
1744 struct ceph_mds_request
, r_node
);
1747 static inline u64
__get_oldest_tid(struct ceph_mds_client
*mdsc
)
1749 return mdsc
->oldest_tid
;
1753 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1754 * on build_path_from_dentry in fs/cifs/dir.c.
1756 * If @stop_on_nosnap, generate path relative to the first non-snapped
1759 * Encode hidden .snap dirs as a double /, i.e.
1760 * foo/.snap/bar -> foo//bar
1762 char *ceph_mdsc_build_path(struct dentry
*dentry
, int *plen
, u64
*base
,
1765 struct dentry
*temp
;
1771 return ERR_PTR(-EINVAL
);
1775 seq
= read_seqbegin(&rename_lock
);
1777 for (temp
= dentry
; !IS_ROOT(temp
);) {
1778 struct inode
*inode
= d_inode(temp
);
1779 if (inode
&& ceph_snap(inode
) == CEPH_SNAPDIR
)
1780 len
++; /* slash only */
1781 else if (stop_on_nosnap
&& inode
&&
1782 ceph_snap(inode
) == CEPH_NOSNAP
)
1785 len
+= 1 + temp
->d_name
.len
;
1786 temp
= temp
->d_parent
;
1790 len
--; /* no leading '/' */
1792 path
= kmalloc(len
+1, GFP_NOFS
);
1794 return ERR_PTR(-ENOMEM
);
1796 path
[pos
] = 0; /* trailing null */
1798 for (temp
= dentry
; !IS_ROOT(temp
) && pos
!= 0; ) {
1799 struct inode
*inode
;
1801 spin_lock(&temp
->d_lock
);
1802 inode
= d_inode(temp
);
1803 if (inode
&& ceph_snap(inode
) == CEPH_SNAPDIR
) {
1804 dout("build_path path+%d: %p SNAPDIR\n",
1806 } else if (stop_on_nosnap
&& inode
&&
1807 ceph_snap(inode
) == CEPH_NOSNAP
) {
1808 spin_unlock(&temp
->d_lock
);
1811 pos
-= temp
->d_name
.len
;
1813 spin_unlock(&temp
->d_lock
);
1816 strncpy(path
+ pos
, temp
->d_name
.name
,
1819 spin_unlock(&temp
->d_lock
);
1822 temp
= temp
->d_parent
;
1825 if (pos
!= 0 || read_seqretry(&rename_lock
, seq
)) {
1826 pr_err("build_path did not end path lookup where "
1827 "expected, namelen is %d, pos is %d\n", len
, pos
);
1828 /* presumably this is only possible if racing with a
1829 rename of one of the parent directories (we can not
1830 lock the dentries above us to prevent this, but
1831 retrying should be harmless) */
1836 *base
= ceph_ino(d_inode(temp
));
1838 dout("build_path on %p %d built %llx '%.*s'\n",
1839 dentry
, d_count(dentry
), *base
, len
, path
);
1843 static int build_dentry_path(struct dentry
*dentry
,
1844 const char **ppath
, int *ppathlen
, u64
*pino
,
1849 if (ceph_snap(d_inode(dentry
->d_parent
)) == CEPH_NOSNAP
) {
1850 *pino
= ceph_ino(d_inode(dentry
->d_parent
));
1851 *ppath
= dentry
->d_name
.name
;
1852 *ppathlen
= dentry
->d_name
.len
;
1855 path
= ceph_mdsc_build_path(dentry
, ppathlen
, pino
, 1);
1857 return PTR_ERR(path
);
1863 static int build_inode_path(struct inode
*inode
,
1864 const char **ppath
, int *ppathlen
, u64
*pino
,
1867 struct dentry
*dentry
;
1870 if (ceph_snap(inode
) == CEPH_NOSNAP
) {
1871 *pino
= ceph_ino(inode
);
1875 dentry
= d_find_alias(inode
);
1876 path
= ceph_mdsc_build_path(dentry
, ppathlen
, pino
, 1);
1879 return PTR_ERR(path
);
1886 * request arguments may be specified via an inode *, a dentry *, or
1887 * an explicit ino+path.
1889 static int set_request_path_attr(struct inode
*rinode
, struct dentry
*rdentry
,
1890 const char *rpath
, u64 rino
,
1891 const char **ppath
, int *pathlen
,
1892 u64
*ino
, int *freepath
)
1897 r
= build_inode_path(rinode
, ppath
, pathlen
, ino
, freepath
);
1898 dout(" inode %p %llx.%llx\n", rinode
, ceph_ino(rinode
),
1900 } else if (rdentry
) {
1901 r
= build_dentry_path(rdentry
, ppath
, pathlen
, ino
, freepath
);
1902 dout(" dentry %p %llx/%.*s\n", rdentry
, *ino
, *pathlen
,
1904 } else if (rpath
|| rino
) {
1907 *pathlen
= rpath
? strlen(rpath
) : 0;
1908 dout(" path %.*s\n", *pathlen
, rpath
);
1915 * called under mdsc->mutex
1917 static struct ceph_msg
*create_request_message(struct ceph_mds_client
*mdsc
,
1918 struct ceph_mds_request
*req
,
1919 int mds
, bool drop_cap_releases
)
1921 struct ceph_msg
*msg
;
1922 struct ceph_mds_request_head
*head
;
1923 const char *path1
= NULL
;
1924 const char *path2
= NULL
;
1925 u64 ino1
= 0, ino2
= 0;
1926 int pathlen1
= 0, pathlen2
= 0;
1927 int freepath1
= 0, freepath2
= 0;
1933 ret
= set_request_path_attr(req
->r_inode
, req
->r_dentry
,
1934 req
->r_path1
, req
->r_ino1
.ino
,
1935 &path1
, &pathlen1
, &ino1
, &freepath1
);
1941 ret
= set_request_path_attr(NULL
, req
->r_old_dentry
,
1942 req
->r_path2
, req
->r_ino2
.ino
,
1943 &path2
, &pathlen2
, &ino2
, &freepath2
);
1949 len
= sizeof(*head
) +
1950 pathlen1
+ pathlen2
+ 2*(1 + sizeof(u32
) + sizeof(u64
)) +
1951 sizeof(struct ceph_timespec
);
1953 /* calculate (max) length for cap releases */
1954 len
+= sizeof(struct ceph_mds_request_release
) *
1955 (!!req
->r_inode_drop
+ !!req
->r_dentry_drop
+
1956 !!req
->r_old_inode_drop
+ !!req
->r_old_dentry_drop
);
1957 if (req
->r_dentry_drop
)
1958 len
+= req
->r_dentry
->d_name
.len
;
1959 if (req
->r_old_dentry_drop
)
1960 len
+= req
->r_old_dentry
->d_name
.len
;
1962 msg
= ceph_msg_new(CEPH_MSG_CLIENT_REQUEST
, len
, GFP_NOFS
, false);
1964 msg
= ERR_PTR(-ENOMEM
);
1968 msg
->hdr
.version
= cpu_to_le16(2);
1969 msg
->hdr
.tid
= cpu_to_le64(req
->r_tid
);
1971 head
= msg
->front
.iov_base
;
1972 p
= msg
->front
.iov_base
+ sizeof(*head
);
1973 end
= msg
->front
.iov_base
+ msg
->front
.iov_len
;
1975 head
->mdsmap_epoch
= cpu_to_le32(mdsc
->mdsmap
->m_epoch
);
1976 head
->op
= cpu_to_le32(req
->r_op
);
1977 head
->caller_uid
= cpu_to_le32(from_kuid(&init_user_ns
, req
->r_uid
));
1978 head
->caller_gid
= cpu_to_le32(from_kgid(&init_user_ns
, req
->r_gid
));
1979 head
->args
= req
->r_args
;
1981 ceph_encode_filepath(&p
, end
, ino1
, path1
);
1982 ceph_encode_filepath(&p
, end
, ino2
, path2
);
1984 /* make note of release offset, in case we need to replay */
1985 req
->r_request_release_offset
= p
- msg
->front
.iov_base
;
1989 if (req
->r_inode_drop
)
1990 releases
+= ceph_encode_inode_release(&p
,
1991 req
->r_inode
? req
->r_inode
: d_inode(req
->r_dentry
),
1992 mds
, req
->r_inode_drop
, req
->r_inode_unless
, 0);
1993 if (req
->r_dentry_drop
)
1994 releases
+= ceph_encode_dentry_release(&p
, req
->r_dentry
,
1995 mds
, req
->r_dentry_drop
, req
->r_dentry_unless
);
1996 if (req
->r_old_dentry_drop
)
1997 releases
+= ceph_encode_dentry_release(&p
, req
->r_old_dentry
,
1998 mds
, req
->r_old_dentry_drop
, req
->r_old_dentry_unless
);
1999 if (req
->r_old_inode_drop
)
2000 releases
+= ceph_encode_inode_release(&p
,
2001 d_inode(req
->r_old_dentry
),
2002 mds
, req
->r_old_inode_drop
, req
->r_old_inode_unless
, 0);
2004 if (drop_cap_releases
) {
2006 p
= msg
->front
.iov_base
+ req
->r_request_release_offset
;
2009 head
->num_releases
= cpu_to_le16(releases
);
2013 struct ceph_timespec ts
;
2014 ceph_encode_timespec(&ts
, &req
->r_stamp
);
2015 ceph_encode_copy(&p
, &ts
, sizeof(ts
));
2019 msg
->front
.iov_len
= p
- msg
->front
.iov_base
;
2020 msg
->hdr
.front_len
= cpu_to_le32(msg
->front
.iov_len
);
2022 if (req
->r_pagelist
) {
2023 struct ceph_pagelist
*pagelist
= req
->r_pagelist
;
2024 atomic_inc(&pagelist
->refcnt
);
2025 ceph_msg_data_add_pagelist(msg
, pagelist
);
2026 msg
->hdr
.data_len
= cpu_to_le32(pagelist
->length
);
2028 msg
->hdr
.data_len
= 0;
2031 msg
->hdr
.data_off
= cpu_to_le16(0);
2035 kfree((char *)path2
);
2038 kfree((char *)path1
);
2044 * called under mdsc->mutex if error, under no mutex if
2047 static void complete_request(struct ceph_mds_client
*mdsc
,
2048 struct ceph_mds_request
*req
)
2050 if (req
->r_callback
)
2051 req
->r_callback(mdsc
, req
);
2053 complete_all(&req
->r_completion
);
2057 * called under mdsc->mutex
2059 static int __prepare_send_request(struct ceph_mds_client
*mdsc
,
2060 struct ceph_mds_request
*req
,
2061 int mds
, bool drop_cap_releases
)
2063 struct ceph_mds_request_head
*rhead
;
2064 struct ceph_msg
*msg
;
2069 struct ceph_cap
*cap
=
2070 ceph_get_cap_for_mds(ceph_inode(req
->r_inode
), mds
);
2073 req
->r_sent_on_mseq
= cap
->mseq
;
2075 req
->r_sent_on_mseq
= -1;
2077 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req
,
2078 req
->r_tid
, ceph_mds_op_name(req
->r_op
), req
->r_attempts
);
2080 if (req
->r_got_unsafe
) {
2083 * Replay. Do not regenerate message (and rebuild
2084 * paths, etc.); just use the original message.
2085 * Rebuilding paths will break for renames because
2086 * d_move mangles the src name.
2088 msg
= req
->r_request
;
2089 rhead
= msg
->front
.iov_base
;
2091 flags
= le32_to_cpu(rhead
->flags
);
2092 flags
|= CEPH_MDS_FLAG_REPLAY
;
2093 rhead
->flags
= cpu_to_le32(flags
);
2095 if (req
->r_target_inode
)
2096 rhead
->ino
= cpu_to_le64(ceph_ino(req
->r_target_inode
));
2098 rhead
->num_retry
= req
->r_attempts
- 1;
2100 /* remove cap/dentry releases from message */
2101 rhead
->num_releases
= 0;
2104 p
= msg
->front
.iov_base
+ req
->r_request_release_offset
;
2106 struct ceph_timespec ts
;
2107 ceph_encode_timespec(&ts
, &req
->r_stamp
);
2108 ceph_encode_copy(&p
, &ts
, sizeof(ts
));
2111 msg
->front
.iov_len
= p
- msg
->front
.iov_base
;
2112 msg
->hdr
.front_len
= cpu_to_le32(msg
->front
.iov_len
);
2116 if (req
->r_request
) {
2117 ceph_msg_put(req
->r_request
);
2118 req
->r_request
= NULL
;
2120 msg
= create_request_message(mdsc
, req
, mds
, drop_cap_releases
);
2122 req
->r_err
= PTR_ERR(msg
);
2123 return PTR_ERR(msg
);
2125 req
->r_request
= msg
;
2127 rhead
= msg
->front
.iov_base
;
2128 rhead
->oldest_client_tid
= cpu_to_le64(__get_oldest_tid(mdsc
));
2129 if (req
->r_got_unsafe
)
2130 flags
|= CEPH_MDS_FLAG_REPLAY
;
2131 if (req
->r_locked_dir
)
2132 flags
|= CEPH_MDS_FLAG_WANT_DENTRY
;
2133 rhead
->flags
= cpu_to_le32(flags
);
2134 rhead
->num_fwd
= req
->r_num_fwd
;
2135 rhead
->num_retry
= req
->r_attempts
- 1;
2138 dout(" r_locked_dir = %p\n", req
->r_locked_dir
);
2143 * send request, or put it on the appropriate wait list.
2145 static int __do_request(struct ceph_mds_client
*mdsc
,
2146 struct ceph_mds_request
*req
)
2148 struct ceph_mds_session
*session
= NULL
;
2152 if (req
->r_err
|| req
->r_got_result
) {
2154 __unregister_request(mdsc
, req
);
2158 if (req
->r_timeout
&&
2159 time_after_eq(jiffies
, req
->r_started
+ req
->r_timeout
)) {
2160 dout("do_request timed out\n");
2164 if (ACCESS_ONCE(mdsc
->fsc
->mount_state
) == CEPH_MOUNT_SHUTDOWN
) {
2165 dout("do_request forced umount\n");
2170 put_request_session(req
);
2172 mds
= __choose_mds(mdsc
, req
);
2174 ceph_mdsmap_get_state(mdsc
->mdsmap
, mds
) < CEPH_MDS_STATE_ACTIVE
) {
2175 dout("do_request no mds or not active, waiting for map\n");
2176 list_add(&req
->r_wait
, &mdsc
->waiting_for_map
);
2180 /* get, open session */
2181 session
= __ceph_lookup_mds_session(mdsc
, mds
);
2183 session
= register_session(mdsc
, mds
);
2184 if (IS_ERR(session
)) {
2185 err
= PTR_ERR(session
);
2189 req
->r_session
= get_session(session
);
2191 dout("do_request mds%d session %p state %s\n", mds
, session
,
2192 ceph_session_state_name(session
->s_state
));
2193 if (session
->s_state
!= CEPH_MDS_SESSION_OPEN
&&
2194 session
->s_state
!= CEPH_MDS_SESSION_HUNG
) {
2195 if (session
->s_state
== CEPH_MDS_SESSION_NEW
||
2196 session
->s_state
== CEPH_MDS_SESSION_CLOSING
)
2197 __open_session(mdsc
, session
);
2198 list_add(&req
->r_wait
, &session
->s_waiting
);
2203 req
->r_resend_mds
= -1; /* forget any previous mds hint */
2205 if (req
->r_request_started
== 0) /* note request start time */
2206 req
->r_request_started
= jiffies
;
2208 err
= __prepare_send_request(mdsc
, req
, mds
, false);
2210 ceph_msg_get(req
->r_request
);
2211 ceph_con_send(&session
->s_con
, req
->r_request
);
2215 ceph_put_mds_session(session
);
2218 dout("__do_request early error %d\n", err
);
2220 complete_request(mdsc
, req
);
2221 __unregister_request(mdsc
, req
);
2228 * called under mdsc->mutex
2230 static void __wake_requests(struct ceph_mds_client
*mdsc
,
2231 struct list_head
*head
)
2233 struct ceph_mds_request
*req
;
2234 LIST_HEAD(tmp_list
);
2236 list_splice_init(head
, &tmp_list
);
2238 while (!list_empty(&tmp_list
)) {
2239 req
= list_entry(tmp_list
.next
,
2240 struct ceph_mds_request
, r_wait
);
2241 list_del_init(&req
->r_wait
);
2242 dout(" wake request %p tid %llu\n", req
, req
->r_tid
);
2243 __do_request(mdsc
, req
);
2248 * Wake up threads with requests pending for @mds, so that they can
2249 * resubmit their requests to a possibly different mds.
2251 static void kick_requests(struct ceph_mds_client
*mdsc
, int mds
)
2253 struct ceph_mds_request
*req
;
2254 struct rb_node
*p
= rb_first(&mdsc
->request_tree
);
2256 dout("kick_requests mds%d\n", mds
);
2258 req
= rb_entry(p
, struct ceph_mds_request
, r_node
);
2260 if (req
->r_got_unsafe
)
2262 if (req
->r_attempts
> 0)
2263 continue; /* only new requests */
2264 if (req
->r_session
&&
2265 req
->r_session
->s_mds
== mds
) {
2266 dout(" kicking tid %llu\n", req
->r_tid
);
2267 list_del_init(&req
->r_wait
);
2268 __do_request(mdsc
, req
);
2273 void ceph_mdsc_submit_request(struct ceph_mds_client
*mdsc
,
2274 struct ceph_mds_request
*req
)
2276 dout("submit_request on %p\n", req
);
2277 mutex_lock(&mdsc
->mutex
);
2278 __register_request(mdsc
, req
, NULL
);
2279 __do_request(mdsc
, req
);
2280 mutex_unlock(&mdsc
->mutex
);
2284 * Synchrously perform an mds request. Take care of all of the
2285 * session setup, forwarding, retry details.
2287 int ceph_mdsc_do_request(struct ceph_mds_client
*mdsc
,
2289 struct ceph_mds_request
*req
)
2293 dout("do_request on %p\n", req
);
2295 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
2297 ceph_get_cap_refs(ceph_inode(req
->r_inode
), CEPH_CAP_PIN
);
2298 if (req
->r_locked_dir
)
2299 ceph_get_cap_refs(ceph_inode(req
->r_locked_dir
), CEPH_CAP_PIN
);
2300 if (req
->r_old_dentry_dir
)
2301 ceph_get_cap_refs(ceph_inode(req
->r_old_dentry_dir
),
2305 mutex_lock(&mdsc
->mutex
);
2306 __register_request(mdsc
, req
, dir
);
2307 __do_request(mdsc
, req
);
2315 mutex_unlock(&mdsc
->mutex
);
2316 dout("do_request waiting\n");
2317 if (!req
->r_timeout
&& req
->r_wait_for_completion
) {
2318 err
= req
->r_wait_for_completion(mdsc
, req
);
2320 long timeleft
= wait_for_completion_killable_timeout(
2322 ceph_timeout_jiffies(req
->r_timeout
));
2326 err
= -EIO
; /* timed out */
2328 err
= timeleft
; /* killed */
2330 dout("do_request waited, got %d\n", err
);
2331 mutex_lock(&mdsc
->mutex
);
2333 /* only abort if we didn't race with a real reply */
2334 if (req
->r_got_result
) {
2335 err
= le32_to_cpu(req
->r_reply_info
.head
->result
);
2336 } else if (err
< 0) {
2337 dout("aborted request %lld with %d\n", req
->r_tid
, err
);
2340 * ensure we aren't running concurrently with
2341 * ceph_fill_trace or ceph_readdir_prepopulate, which
2342 * rely on locks (dir mutex) held by our caller.
2344 mutex_lock(&req
->r_fill_mutex
);
2346 req
->r_aborted
= true;
2347 mutex_unlock(&req
->r_fill_mutex
);
2349 if (req
->r_locked_dir
&&
2350 (req
->r_op
& CEPH_MDS_OP_WRITE
))
2351 ceph_invalidate_dir_request(req
);
2357 mutex_unlock(&mdsc
->mutex
);
2358 dout("do_request %p done, result %d\n", req
, err
);
2363 * Invalidate dir's completeness, dentry lease state on an aborted MDS
2364 * namespace request.
2366 void ceph_invalidate_dir_request(struct ceph_mds_request
*req
)
2368 struct inode
*inode
= req
->r_locked_dir
;
2370 dout("invalidate_dir_request %p (complete, lease(s))\n", inode
);
2372 ceph_dir_clear_complete(inode
);
2374 ceph_invalidate_dentry_lease(req
->r_dentry
);
2375 if (req
->r_old_dentry
)
2376 ceph_invalidate_dentry_lease(req
->r_old_dentry
);
2382 * We take the session mutex and parse and process the reply immediately.
2383 * This preserves the logical ordering of replies, capabilities, etc., sent
2384 * by the MDS as they are applied to our local cache.
2386 static void handle_reply(struct ceph_mds_session
*session
, struct ceph_msg
*msg
)
2388 struct ceph_mds_client
*mdsc
= session
->s_mdsc
;
2389 struct ceph_mds_request
*req
;
2390 struct ceph_mds_reply_head
*head
= msg
->front
.iov_base
;
2391 struct ceph_mds_reply_info_parsed
*rinfo
; /* parsed reply info */
2392 struct ceph_snap_realm
*realm
;
2395 int mds
= session
->s_mds
;
2397 if (msg
->front
.iov_len
< sizeof(*head
)) {
2398 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
2403 /* get request, session */
2404 tid
= le64_to_cpu(msg
->hdr
.tid
);
2405 mutex_lock(&mdsc
->mutex
);
2406 req
= __lookup_request(mdsc
, tid
);
2408 dout("handle_reply on unknown tid %llu\n", tid
);
2409 mutex_unlock(&mdsc
->mutex
);
2412 dout("handle_reply %p\n", req
);
2414 /* correct session? */
2415 if (req
->r_session
!= session
) {
2416 pr_err("mdsc_handle_reply got %llu on session mds%d"
2417 " not mds%d\n", tid
, session
->s_mds
,
2418 req
->r_session
? req
->r_session
->s_mds
: -1);
2419 mutex_unlock(&mdsc
->mutex
);
2424 if ((req
->r_got_unsafe
&& !head
->safe
) ||
2425 (req
->r_got_safe
&& head
->safe
)) {
2426 pr_warn("got a dup %s reply on %llu from mds%d\n",
2427 head
->safe
? "safe" : "unsafe", tid
, mds
);
2428 mutex_unlock(&mdsc
->mutex
);
2431 if (req
->r_got_safe
) {
2432 pr_warn("got unsafe after safe on %llu from mds%d\n",
2434 mutex_unlock(&mdsc
->mutex
);
2438 result
= le32_to_cpu(head
->result
);
2442 * if we're not talking to the authority, send to them
2443 * if the authority has changed while we weren't looking,
2444 * send to new authority
2445 * Otherwise we just have to return an ESTALE
2447 if (result
== -ESTALE
) {
2448 dout("got ESTALE on request %llu", req
->r_tid
);
2449 req
->r_resend_mds
= -1;
2450 if (req
->r_direct_mode
!= USE_AUTH_MDS
) {
2451 dout("not using auth, setting for that now");
2452 req
->r_direct_mode
= USE_AUTH_MDS
;
2453 __do_request(mdsc
, req
);
2454 mutex_unlock(&mdsc
->mutex
);
2457 int mds
= __choose_mds(mdsc
, req
);
2458 if (mds
>= 0 && mds
!= req
->r_session
->s_mds
) {
2459 dout("but auth changed, so resending");
2460 __do_request(mdsc
, req
);
2461 mutex_unlock(&mdsc
->mutex
);
2465 dout("have to return ESTALE on request %llu", req
->r_tid
);
2470 req
->r_got_safe
= true;
2471 __unregister_request(mdsc
, req
);
2473 if (req
->r_got_unsafe
) {
2475 * We already handled the unsafe response, now do the
2476 * cleanup. No need to examine the response; the MDS
2477 * doesn't include any result info in the safe
2478 * response. And even if it did, there is nothing
2479 * useful we could do with a revised return value.
2481 dout("got safe reply %llu, mds%d\n", tid
, mds
);
2483 /* last unsafe request during umount? */
2484 if (mdsc
->stopping
&& !__get_oldest_req(mdsc
))
2485 complete_all(&mdsc
->safe_umount_waiters
);
2486 mutex_unlock(&mdsc
->mutex
);
2490 req
->r_got_unsafe
= true;
2491 list_add_tail(&req
->r_unsafe_item
, &req
->r_session
->s_unsafe
);
2492 if (req
->r_unsafe_dir
) {
2493 struct ceph_inode_info
*ci
=
2494 ceph_inode(req
->r_unsafe_dir
);
2495 spin_lock(&ci
->i_unsafe_lock
);
2496 list_add_tail(&req
->r_unsafe_dir_item
,
2497 &ci
->i_unsafe_dirops
);
2498 spin_unlock(&ci
->i_unsafe_lock
);
2502 dout("handle_reply tid %lld result %d\n", tid
, result
);
2503 rinfo
= &req
->r_reply_info
;
2504 err
= parse_reply_info(msg
, rinfo
, session
->s_con
.peer_features
);
2505 mutex_unlock(&mdsc
->mutex
);
2507 mutex_lock(&session
->s_mutex
);
2509 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds
, tid
);
2516 if (rinfo
->snapblob_len
) {
2517 down_write(&mdsc
->snap_rwsem
);
2518 ceph_update_snap_trace(mdsc
, rinfo
->snapblob
,
2519 rinfo
->snapblob
+ rinfo
->snapblob_len
,
2520 le32_to_cpu(head
->op
) == CEPH_MDS_OP_RMSNAP
,
2522 downgrade_write(&mdsc
->snap_rwsem
);
2524 down_read(&mdsc
->snap_rwsem
);
2527 /* insert trace into our cache */
2528 mutex_lock(&req
->r_fill_mutex
);
2529 err
= ceph_fill_trace(mdsc
->fsc
->sb
, req
, req
->r_session
);
2531 if (result
== 0 && (req
->r_op
== CEPH_MDS_OP_READDIR
||
2532 req
->r_op
== CEPH_MDS_OP_LSSNAP
))
2533 ceph_readdir_prepopulate(req
, req
->r_session
);
2534 ceph_unreserve_caps(mdsc
, &req
->r_caps_reservation
);
2536 mutex_unlock(&req
->r_fill_mutex
);
2538 up_read(&mdsc
->snap_rwsem
);
2540 ceph_put_snap_realm(mdsc
, realm
);
2542 if (err
== 0 && req
->r_got_unsafe
&& req
->r_target_inode
) {
2543 struct ceph_inode_info
*ci
= ceph_inode(req
->r_target_inode
);
2544 spin_lock(&ci
->i_unsafe_lock
);
2545 list_add_tail(&req
->r_unsafe_target_item
, &ci
->i_unsafe_iops
);
2546 spin_unlock(&ci
->i_unsafe_lock
);
2549 mutex_lock(&mdsc
->mutex
);
2550 if (!req
->r_aborted
) {
2554 req
->r_reply
= ceph_msg_get(msg
);
2555 req
->r_got_result
= true;
2558 dout("reply arrived after request %lld was aborted\n", tid
);
2560 mutex_unlock(&mdsc
->mutex
);
2562 mutex_unlock(&session
->s_mutex
);
2564 /* kick calling process */
2565 complete_request(mdsc
, req
);
2567 ceph_mdsc_put_request(req
);
2574 * handle mds notification that our request has been forwarded.
2576 static void handle_forward(struct ceph_mds_client
*mdsc
,
2577 struct ceph_mds_session
*session
,
2578 struct ceph_msg
*msg
)
2580 struct ceph_mds_request
*req
;
2581 u64 tid
= le64_to_cpu(msg
->hdr
.tid
);
2585 void *p
= msg
->front
.iov_base
;
2586 void *end
= p
+ msg
->front
.iov_len
;
2588 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
2589 next_mds
= ceph_decode_32(&p
);
2590 fwd_seq
= ceph_decode_32(&p
);
2592 mutex_lock(&mdsc
->mutex
);
2593 req
= __lookup_request(mdsc
, tid
);
2595 dout("forward tid %llu to mds%d - req dne\n", tid
, next_mds
);
2596 goto out
; /* dup reply? */
2599 if (req
->r_aborted
) {
2600 dout("forward tid %llu aborted, unregistering\n", tid
);
2601 __unregister_request(mdsc
, req
);
2602 } else if (fwd_seq
<= req
->r_num_fwd
) {
2603 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
2604 tid
, next_mds
, req
->r_num_fwd
, fwd_seq
);
2606 /* resend. forward race not possible; mds would drop */
2607 dout("forward tid %llu to mds%d (we resend)\n", tid
, next_mds
);
2609 BUG_ON(req
->r_got_result
);
2610 req
->r_attempts
= 0;
2611 req
->r_num_fwd
= fwd_seq
;
2612 req
->r_resend_mds
= next_mds
;
2613 put_request_session(req
);
2614 __do_request(mdsc
, req
);
2616 ceph_mdsc_put_request(req
);
2618 mutex_unlock(&mdsc
->mutex
);
2622 pr_err("mdsc_handle_forward decode error err=%d\n", err
);
2626 * handle a mds session control message
2628 static void handle_session(struct ceph_mds_session
*session
,
2629 struct ceph_msg
*msg
)
2631 struct ceph_mds_client
*mdsc
= session
->s_mdsc
;
2634 int mds
= session
->s_mds
;
2635 struct ceph_mds_session_head
*h
= msg
->front
.iov_base
;
2639 if (msg
->front
.iov_len
!= sizeof(*h
))
2641 op
= le32_to_cpu(h
->op
);
2642 seq
= le64_to_cpu(h
->seq
);
2644 mutex_lock(&mdsc
->mutex
);
2645 if (op
== CEPH_SESSION_CLOSE
)
2646 __unregister_session(mdsc
, session
);
2647 /* FIXME: this ttl calculation is generous */
2648 session
->s_ttl
= jiffies
+ HZ
*mdsc
->mdsmap
->m_session_autoclose
;
2649 mutex_unlock(&mdsc
->mutex
);
2651 mutex_lock(&session
->s_mutex
);
2653 dout("handle_session mds%d %s %p state %s seq %llu\n",
2654 mds
, ceph_session_op_name(op
), session
,
2655 ceph_session_state_name(session
->s_state
), seq
);
2657 if (session
->s_state
== CEPH_MDS_SESSION_HUNG
) {
2658 session
->s_state
= CEPH_MDS_SESSION_OPEN
;
2659 pr_info("mds%d came back\n", session
->s_mds
);
2663 case CEPH_SESSION_OPEN
:
2664 if (session
->s_state
== CEPH_MDS_SESSION_RECONNECTING
)
2665 pr_info("mds%d reconnect success\n", session
->s_mds
);
2666 session
->s_state
= CEPH_MDS_SESSION_OPEN
;
2667 renewed_caps(mdsc
, session
, 0);
2670 __close_session(mdsc
, session
);
2673 case CEPH_SESSION_RENEWCAPS
:
2674 if (session
->s_renew_seq
== seq
)
2675 renewed_caps(mdsc
, session
, 1);
2678 case CEPH_SESSION_CLOSE
:
2679 if (session
->s_state
== CEPH_MDS_SESSION_RECONNECTING
)
2680 pr_info("mds%d reconnect denied\n", session
->s_mds
);
2681 cleanup_session_requests(mdsc
, session
);
2682 remove_session_caps(session
);
2683 wake
= 2; /* for good measure */
2684 wake_up_all(&mdsc
->session_close_wq
);
2687 case CEPH_SESSION_STALE
:
2688 pr_info("mds%d caps went stale, renewing\n",
2690 spin_lock(&session
->s_gen_ttl_lock
);
2691 session
->s_cap_gen
++;
2692 session
->s_cap_ttl
= jiffies
- 1;
2693 spin_unlock(&session
->s_gen_ttl_lock
);
2694 send_renew_caps(mdsc
, session
);
2697 case CEPH_SESSION_RECALL_STATE
:
2698 trim_caps(mdsc
, session
, le32_to_cpu(h
->max_caps
));
2701 case CEPH_SESSION_FLUSHMSG
:
2702 send_flushmsg_ack(mdsc
, session
, seq
);
2705 case CEPH_SESSION_FORCE_RO
:
2706 dout("force_session_readonly %p\n", session
);
2707 spin_lock(&session
->s_cap_lock
);
2708 session
->s_readonly
= true;
2709 spin_unlock(&session
->s_cap_lock
);
2710 wake_up_session_caps(session
, 0);
2714 pr_err("mdsc_handle_session bad op %d mds%d\n", op
, mds
);
2718 mutex_unlock(&session
->s_mutex
);
2720 mutex_lock(&mdsc
->mutex
);
2721 __wake_requests(mdsc
, &session
->s_waiting
);
2723 kick_requests(mdsc
, mds
);
2724 mutex_unlock(&mdsc
->mutex
);
2729 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds
,
2730 (int)msg
->front
.iov_len
);
2737 * called under session->mutex.
2739 static void replay_unsafe_requests(struct ceph_mds_client
*mdsc
,
2740 struct ceph_mds_session
*session
)
2742 struct ceph_mds_request
*req
, *nreq
;
2746 dout("replay_unsafe_requests mds%d\n", session
->s_mds
);
2748 mutex_lock(&mdsc
->mutex
);
2749 list_for_each_entry_safe(req
, nreq
, &session
->s_unsafe
, r_unsafe_item
) {
2750 err
= __prepare_send_request(mdsc
, req
, session
->s_mds
, true);
2752 ceph_msg_get(req
->r_request
);
2753 ceph_con_send(&session
->s_con
, req
->r_request
);
2758 * also re-send old requests when MDS enters reconnect stage. So that MDS
2759 * can process completed request in clientreplay stage.
2761 p
= rb_first(&mdsc
->request_tree
);
2763 req
= rb_entry(p
, struct ceph_mds_request
, r_node
);
2765 if (req
->r_got_unsafe
)
2767 if (req
->r_attempts
== 0)
2768 continue; /* only old requests */
2769 if (req
->r_session
&&
2770 req
->r_session
->s_mds
== session
->s_mds
) {
2771 err
= __prepare_send_request(mdsc
, req
,
2772 session
->s_mds
, true);
2774 ceph_msg_get(req
->r_request
);
2775 ceph_con_send(&session
->s_con
, req
->r_request
);
2779 mutex_unlock(&mdsc
->mutex
);
2783 * Encode information about a cap for a reconnect with the MDS.
2785 static int encode_caps_cb(struct inode
*inode
, struct ceph_cap
*cap
,
2789 struct ceph_mds_cap_reconnect v2
;
2790 struct ceph_mds_cap_reconnect_v1 v1
;
2793 struct ceph_inode_info
*ci
;
2794 struct ceph_reconnect_state
*recon_state
= arg
;
2795 struct ceph_pagelist
*pagelist
= recon_state
->pagelist
;
2799 struct dentry
*dentry
;
2803 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2804 inode
, ceph_vinop(inode
), cap
, cap
->cap_id
,
2805 ceph_cap_string(cap
->issued
));
2806 err
= ceph_pagelist_encode_64(pagelist
, ceph_ino(inode
));
2810 dentry
= d_find_alias(inode
);
2812 path
= ceph_mdsc_build_path(dentry
, &pathlen
, &pathbase
, 0);
2814 err
= PTR_ERR(path
);
2821 err
= ceph_pagelist_encode_string(pagelist
, path
, pathlen
);
2825 spin_lock(&ci
->i_ceph_lock
);
2826 cap
->seq
= 0; /* reset cap seq */
2827 cap
->issue_seq
= 0; /* and issue_seq */
2828 cap
->mseq
= 0; /* and migrate_seq */
2829 cap
->cap_gen
= cap
->session
->s_cap_gen
;
2831 if (recon_state
->flock
) {
2832 rec
.v2
.cap_id
= cpu_to_le64(cap
->cap_id
);
2833 rec
.v2
.wanted
= cpu_to_le32(__ceph_caps_wanted(ci
));
2834 rec
.v2
.issued
= cpu_to_le32(cap
->issued
);
2835 rec
.v2
.snaprealm
= cpu_to_le64(ci
->i_snap_realm
->ino
);
2836 rec
.v2
.pathbase
= cpu_to_le64(pathbase
);
2837 rec
.v2
.flock_len
= 0;
2838 reclen
= sizeof(rec
.v2
);
2840 rec
.v1
.cap_id
= cpu_to_le64(cap
->cap_id
);
2841 rec
.v1
.wanted
= cpu_to_le32(__ceph_caps_wanted(ci
));
2842 rec
.v1
.issued
= cpu_to_le32(cap
->issued
);
2843 rec
.v1
.size
= cpu_to_le64(inode
->i_size
);
2844 ceph_encode_timespec(&rec
.v1
.mtime
, &inode
->i_mtime
);
2845 ceph_encode_timespec(&rec
.v1
.atime
, &inode
->i_atime
);
2846 rec
.v1
.snaprealm
= cpu_to_le64(ci
->i_snap_realm
->ino
);
2847 rec
.v1
.pathbase
= cpu_to_le64(pathbase
);
2848 reclen
= sizeof(rec
.v1
);
2850 spin_unlock(&ci
->i_ceph_lock
);
2852 if (recon_state
->flock
) {
2853 int num_fcntl_locks
, num_flock_locks
;
2854 struct ceph_filelock
*flocks
;
2857 ceph_count_locks(inode
, &num_fcntl_locks
, &num_flock_locks
);
2858 flocks
= kmalloc((num_fcntl_locks
+num_flock_locks
) *
2859 sizeof(struct ceph_filelock
), GFP_NOFS
);
2864 err
= ceph_encode_locks_to_buffer(inode
, flocks
,
2874 * number of encoded locks is stable, so copy to pagelist
2876 rec
.v2
.flock_len
= cpu_to_le32(2*sizeof(u32
) +
2877 (num_fcntl_locks
+num_flock_locks
) *
2878 sizeof(struct ceph_filelock
));
2879 err
= ceph_pagelist_append(pagelist
, &rec
, reclen
);
2881 err
= ceph_locks_to_pagelist(flocks
, pagelist
,
2886 err
= ceph_pagelist_append(pagelist
, &rec
, reclen
);
2889 recon_state
->nr_caps
++;
2899 * If an MDS fails and recovers, clients need to reconnect in order to
2900 * reestablish shared state. This includes all caps issued through
2901 * this session _and_ the snap_realm hierarchy. Because it's not
2902 * clear which snap realms the mds cares about, we send everything we
2903 * know about.. that ensures we'll then get any new info the
2904 * recovering MDS might have.
2906 * This is a relatively heavyweight operation, but it's rare.
2908 * called with mdsc->mutex held.
2910 static void send_mds_reconnect(struct ceph_mds_client
*mdsc
,
2911 struct ceph_mds_session
*session
)
2913 struct ceph_msg
*reply
;
2915 int mds
= session
->s_mds
;
2918 struct ceph_pagelist
*pagelist
;
2919 struct ceph_reconnect_state recon_state
;
2921 pr_info("mds%d reconnect start\n", mds
);
2923 pagelist
= kmalloc(sizeof(*pagelist
), GFP_NOFS
);
2925 goto fail_nopagelist
;
2926 ceph_pagelist_init(pagelist
);
2928 reply
= ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT
, 0, GFP_NOFS
, false);
2932 mutex_lock(&session
->s_mutex
);
2933 session
->s_state
= CEPH_MDS_SESSION_RECONNECTING
;
2936 dout("session %p state %s\n", session
,
2937 ceph_session_state_name(session
->s_state
));
2939 spin_lock(&session
->s_gen_ttl_lock
);
2940 session
->s_cap_gen
++;
2941 spin_unlock(&session
->s_gen_ttl_lock
);
2943 spin_lock(&session
->s_cap_lock
);
2944 /* don't know if session is readonly */
2945 session
->s_readonly
= 0;
2947 * notify __ceph_remove_cap() that we are composing cap reconnect.
2948 * If a cap get released before being added to the cap reconnect,
2949 * __ceph_remove_cap() should skip queuing cap release.
2951 session
->s_cap_reconnect
= 1;
2952 /* drop old cap expires; we're about to reestablish that state */
2953 cleanup_cap_releases(mdsc
, session
);
2955 /* trim unused caps to reduce MDS's cache rejoin time */
2956 if (mdsc
->fsc
->sb
->s_root
)
2957 shrink_dcache_parent(mdsc
->fsc
->sb
->s_root
);
2959 ceph_con_close(&session
->s_con
);
2960 ceph_con_open(&session
->s_con
,
2961 CEPH_ENTITY_TYPE_MDS
, mds
,
2962 ceph_mdsmap_get_addr(mdsc
->mdsmap
, mds
));
2964 /* replay unsafe requests */
2965 replay_unsafe_requests(mdsc
, session
);
2967 down_read(&mdsc
->snap_rwsem
);
2969 /* traverse this session's caps */
2970 s_nr_caps
= session
->s_nr_caps
;
2971 err
= ceph_pagelist_encode_32(pagelist
, s_nr_caps
);
2975 recon_state
.nr_caps
= 0;
2976 recon_state
.pagelist
= pagelist
;
2977 recon_state
.flock
= session
->s_con
.peer_features
& CEPH_FEATURE_FLOCK
;
2978 err
= iterate_session_caps(session
, encode_caps_cb
, &recon_state
);
2982 spin_lock(&session
->s_cap_lock
);
2983 session
->s_cap_reconnect
= 0;
2984 spin_unlock(&session
->s_cap_lock
);
2987 * snaprealms. we provide mds with the ino, seq (version), and
2988 * parent for all of our realms. If the mds has any newer info,
2991 for (p
= rb_first(&mdsc
->snap_realms
); p
; p
= rb_next(p
)) {
2992 struct ceph_snap_realm
*realm
=
2993 rb_entry(p
, struct ceph_snap_realm
, node
);
2994 struct ceph_mds_snaprealm_reconnect sr_rec
;
2996 dout(" adding snap realm %llx seq %lld parent %llx\n",
2997 realm
->ino
, realm
->seq
, realm
->parent_ino
);
2998 sr_rec
.ino
= cpu_to_le64(realm
->ino
);
2999 sr_rec
.seq
= cpu_to_le64(realm
->seq
);
3000 sr_rec
.parent
= cpu_to_le64(realm
->parent_ino
);
3001 err
= ceph_pagelist_append(pagelist
, &sr_rec
, sizeof(sr_rec
));
3006 if (recon_state
.flock
)
3007 reply
->hdr
.version
= cpu_to_le16(2);
3009 /* raced with cap release? */
3010 if (s_nr_caps
!= recon_state
.nr_caps
) {
3011 struct page
*page
= list_first_entry(&pagelist
->head
,
3013 __le32
*addr
= kmap_atomic(page
);
3014 *addr
= cpu_to_le32(recon_state
.nr_caps
);
3015 kunmap_atomic(addr
);
3018 reply
->hdr
.data_len
= cpu_to_le32(pagelist
->length
);
3019 ceph_msg_data_add_pagelist(reply
, pagelist
);
3021 ceph_early_kick_flushing_caps(mdsc
, session
);
3023 ceph_con_send(&session
->s_con
, reply
);
3025 mutex_unlock(&session
->s_mutex
);
3027 mutex_lock(&mdsc
->mutex
);
3028 __wake_requests(mdsc
, &session
->s_waiting
);
3029 mutex_unlock(&mdsc
->mutex
);
3031 up_read(&mdsc
->snap_rwsem
);
3035 ceph_msg_put(reply
);
3036 up_read(&mdsc
->snap_rwsem
);
3037 mutex_unlock(&session
->s_mutex
);
3039 ceph_pagelist_release(pagelist
);
3041 pr_err("error %d preparing reconnect for mds%d\n", err
, mds
);
3047 * compare old and new mdsmaps, kicking requests
3048 * and closing out old connections as necessary
3050 * called under mdsc->mutex.
3052 static void check_new_map(struct ceph_mds_client
*mdsc
,
3053 struct ceph_mdsmap
*newmap
,
3054 struct ceph_mdsmap
*oldmap
)
3057 int oldstate
, newstate
;
3058 struct ceph_mds_session
*s
;
3060 dout("check_new_map new %u old %u\n",
3061 newmap
->m_epoch
, oldmap
->m_epoch
);
3063 for (i
= 0; i
< oldmap
->m_max_mds
&& i
< mdsc
->max_sessions
; i
++) {
3064 if (mdsc
->sessions
[i
] == NULL
)
3066 s
= mdsc
->sessions
[i
];
3067 oldstate
= ceph_mdsmap_get_state(oldmap
, i
);
3068 newstate
= ceph_mdsmap_get_state(newmap
, i
);
3070 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
3071 i
, ceph_mds_state_name(oldstate
),
3072 ceph_mdsmap_is_laggy(oldmap
, i
) ? " (laggy)" : "",
3073 ceph_mds_state_name(newstate
),
3074 ceph_mdsmap_is_laggy(newmap
, i
) ? " (laggy)" : "",
3075 ceph_session_state_name(s
->s_state
));
3077 if (i
>= newmap
->m_max_mds
||
3078 memcmp(ceph_mdsmap_get_addr(oldmap
, i
),
3079 ceph_mdsmap_get_addr(newmap
, i
),
3080 sizeof(struct ceph_entity_addr
))) {
3081 if (s
->s_state
== CEPH_MDS_SESSION_OPENING
) {
3082 /* the session never opened, just close it
3084 __wake_requests(mdsc
, &s
->s_waiting
);
3085 __unregister_session(mdsc
, s
);
3088 mutex_unlock(&mdsc
->mutex
);
3089 mutex_lock(&s
->s_mutex
);
3090 mutex_lock(&mdsc
->mutex
);
3091 ceph_con_close(&s
->s_con
);
3092 mutex_unlock(&s
->s_mutex
);
3093 s
->s_state
= CEPH_MDS_SESSION_RESTARTING
;
3095 } else if (oldstate
== newstate
) {
3096 continue; /* nothing new with this mds */
3102 if (s
->s_state
== CEPH_MDS_SESSION_RESTARTING
&&
3103 newstate
>= CEPH_MDS_STATE_RECONNECT
) {
3104 mutex_unlock(&mdsc
->mutex
);
3105 send_mds_reconnect(mdsc
, s
);
3106 mutex_lock(&mdsc
->mutex
);
3110 * kick request on any mds that has gone active.
3112 if (oldstate
< CEPH_MDS_STATE_ACTIVE
&&
3113 newstate
>= CEPH_MDS_STATE_ACTIVE
) {
3114 if (oldstate
!= CEPH_MDS_STATE_CREATING
&&
3115 oldstate
!= CEPH_MDS_STATE_STARTING
)
3116 pr_info("mds%d recovery completed\n", s
->s_mds
);
3117 kick_requests(mdsc
, i
);
3118 ceph_kick_flushing_caps(mdsc
, s
);
3119 wake_up_session_caps(s
, 1);
3123 for (i
= 0; i
< newmap
->m_max_mds
&& i
< mdsc
->max_sessions
; i
++) {
3124 s
= mdsc
->sessions
[i
];
3127 if (!ceph_mdsmap_is_laggy(newmap
, i
))
3129 if (s
->s_state
== CEPH_MDS_SESSION_OPEN
||
3130 s
->s_state
== CEPH_MDS_SESSION_HUNG
||
3131 s
->s_state
== CEPH_MDS_SESSION_CLOSING
) {
3132 dout(" connecting to export targets of laggy mds%d\n",
3134 __open_export_target_sessions(mdsc
, s
);
3146 * caller must hold session s_mutex, dentry->d_lock
3148 void __ceph_mdsc_drop_dentry_lease(struct dentry
*dentry
)
3150 struct ceph_dentry_info
*di
= ceph_dentry(dentry
);
3152 ceph_put_mds_session(di
->lease_session
);
3153 di
->lease_session
= NULL
;
3156 static void handle_lease(struct ceph_mds_client
*mdsc
,
3157 struct ceph_mds_session
*session
,
3158 struct ceph_msg
*msg
)
3160 struct super_block
*sb
= mdsc
->fsc
->sb
;
3161 struct inode
*inode
;
3162 struct dentry
*parent
, *dentry
;
3163 struct ceph_dentry_info
*di
;
3164 int mds
= session
->s_mds
;
3165 struct ceph_mds_lease
*h
= msg
->front
.iov_base
;
3167 struct ceph_vino vino
;
3171 dout("handle_lease from mds%d\n", mds
);
3174 if (msg
->front
.iov_len
< sizeof(*h
) + sizeof(u32
))
3176 vino
.ino
= le64_to_cpu(h
->ino
);
3177 vino
.snap
= CEPH_NOSNAP
;
3178 seq
= le32_to_cpu(h
->seq
);
3179 dname
.name
= (void *)h
+ sizeof(*h
) + sizeof(u32
);
3180 dname
.len
= msg
->front
.iov_len
- sizeof(*h
) - sizeof(u32
);
3181 if (dname
.len
!= get_unaligned_le32(h
+1))
3185 inode
= ceph_find_inode(sb
, vino
);
3186 dout("handle_lease %s, ino %llx %p %.*s\n",
3187 ceph_lease_op_name(h
->action
), vino
.ino
, inode
,
3188 dname
.len
, dname
.name
);
3190 mutex_lock(&session
->s_mutex
);
3193 if (inode
== NULL
) {
3194 dout("handle_lease no inode %llx\n", vino
.ino
);
3199 parent
= d_find_alias(inode
);
3201 dout("no parent dentry on inode %p\n", inode
);
3203 goto release
; /* hrm... */
3205 dname
.hash
= full_name_hash(dname
.name
, dname
.len
);
3206 dentry
= d_lookup(parent
, &dname
);
3211 spin_lock(&dentry
->d_lock
);
3212 di
= ceph_dentry(dentry
);
3213 switch (h
->action
) {
3214 case CEPH_MDS_LEASE_REVOKE
:
3215 if (di
->lease_session
== session
) {
3216 if (ceph_seq_cmp(di
->lease_seq
, seq
) > 0)
3217 h
->seq
= cpu_to_le32(di
->lease_seq
);
3218 __ceph_mdsc_drop_dentry_lease(dentry
);
3223 case CEPH_MDS_LEASE_RENEW
:
3224 if (di
->lease_session
== session
&&
3225 di
->lease_gen
== session
->s_cap_gen
&&
3226 di
->lease_renew_from
&&
3227 di
->lease_renew_after
== 0) {
3228 unsigned long duration
=
3229 msecs_to_jiffies(le32_to_cpu(h
->duration_ms
));
3231 di
->lease_seq
= seq
;
3232 dentry
->d_time
= di
->lease_renew_from
+ duration
;
3233 di
->lease_renew_after
= di
->lease_renew_from
+
3235 di
->lease_renew_from
= 0;
3239 spin_unlock(&dentry
->d_lock
);
3246 /* let's just reuse the same message */
3247 h
->action
= CEPH_MDS_LEASE_REVOKE_ACK
;
3249 ceph_con_send(&session
->s_con
, msg
);
3253 mutex_unlock(&session
->s_mutex
);
3257 pr_err("corrupt lease message\n");
3261 void ceph_mdsc_lease_send_msg(struct ceph_mds_session
*session
,
3262 struct inode
*inode
,
3263 struct dentry
*dentry
, char action
,
3266 struct ceph_msg
*msg
;
3267 struct ceph_mds_lease
*lease
;
3268 int len
= sizeof(*lease
) + sizeof(u32
);
3271 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
3272 inode
, dentry
, ceph_lease_op_name(action
), session
->s_mds
);
3273 dnamelen
= dentry
->d_name
.len
;
3276 msg
= ceph_msg_new(CEPH_MSG_CLIENT_LEASE
, len
, GFP_NOFS
, false);
3279 lease
= msg
->front
.iov_base
;
3280 lease
->action
= action
;
3281 lease
->ino
= cpu_to_le64(ceph_vino(inode
).ino
);
3282 lease
->first
= lease
->last
= cpu_to_le64(ceph_vino(inode
).snap
);
3283 lease
->seq
= cpu_to_le32(seq
);
3284 put_unaligned_le32(dnamelen
, lease
+ 1);
3285 memcpy((void *)(lease
+ 1) + 4, dentry
->d_name
.name
, dnamelen
);
3288 * if this is a preemptive lease RELEASE, no need to
3289 * flush request stream, since the actual request will
3292 msg
->more_to_follow
= (action
== CEPH_MDS_LEASE_RELEASE
);
3294 ceph_con_send(&session
->s_con
, msg
);
3298 * Preemptively release a lease we expect to invalidate anyway.
3299 * Pass @inode always, @dentry is optional.
3301 void ceph_mdsc_lease_release(struct ceph_mds_client
*mdsc
, struct inode
*inode
,
3302 struct dentry
*dentry
)
3304 struct ceph_dentry_info
*di
;
3305 struct ceph_mds_session
*session
;
3308 BUG_ON(inode
== NULL
);
3309 BUG_ON(dentry
== NULL
);
3311 /* is dentry lease valid? */
3312 spin_lock(&dentry
->d_lock
);
3313 di
= ceph_dentry(dentry
);
3314 if (!di
|| !di
->lease_session
||
3315 di
->lease_session
->s_mds
< 0 ||
3316 di
->lease_gen
!= di
->lease_session
->s_cap_gen
||
3317 !time_before(jiffies
, dentry
->d_time
)) {
3318 dout("lease_release inode %p dentry %p -- "
3321 spin_unlock(&dentry
->d_lock
);
3325 /* we do have a lease on this dentry; note mds and seq */
3326 session
= ceph_get_mds_session(di
->lease_session
);
3327 seq
= di
->lease_seq
;
3328 __ceph_mdsc_drop_dentry_lease(dentry
);
3329 spin_unlock(&dentry
->d_lock
);
3331 dout("lease_release inode %p dentry %p to mds%d\n",
3332 inode
, dentry
, session
->s_mds
);
3333 ceph_mdsc_lease_send_msg(session
, inode
, dentry
,
3334 CEPH_MDS_LEASE_RELEASE
, seq
);
3335 ceph_put_mds_session(session
);
3339 * drop all leases (and dentry refs) in preparation for umount
3341 static void drop_leases(struct ceph_mds_client
*mdsc
)
3345 dout("drop_leases\n");
3346 mutex_lock(&mdsc
->mutex
);
3347 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
3348 struct ceph_mds_session
*s
= __ceph_lookup_mds_session(mdsc
, i
);
3351 mutex_unlock(&mdsc
->mutex
);
3352 mutex_lock(&s
->s_mutex
);
3353 mutex_unlock(&s
->s_mutex
);
3354 ceph_put_mds_session(s
);
3355 mutex_lock(&mdsc
->mutex
);
3357 mutex_unlock(&mdsc
->mutex
);
3363 * delayed work -- periodically trim expired leases, renew caps with mds
3365 static void schedule_delayed(struct ceph_mds_client
*mdsc
)
3368 unsigned hz
= round_jiffies_relative(HZ
* delay
);
3369 schedule_delayed_work(&mdsc
->delayed_work
, hz
);
3372 static void delayed_work(struct work_struct
*work
)
3375 struct ceph_mds_client
*mdsc
=
3376 container_of(work
, struct ceph_mds_client
, delayed_work
.work
);
3380 dout("mdsc delayed_work\n");
3381 ceph_check_delayed_caps(mdsc
);
3383 mutex_lock(&mdsc
->mutex
);
3384 renew_interval
= mdsc
->mdsmap
->m_session_timeout
>> 2;
3385 renew_caps
= time_after_eq(jiffies
, HZ
*renew_interval
+
3386 mdsc
->last_renew_caps
);
3388 mdsc
->last_renew_caps
= jiffies
;
3390 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
3391 struct ceph_mds_session
*s
= __ceph_lookup_mds_session(mdsc
, i
);
3394 if (s
->s_state
== CEPH_MDS_SESSION_CLOSING
) {
3395 dout("resending session close request for mds%d\n",
3397 request_close_session(mdsc
, s
);
3398 ceph_put_mds_session(s
);
3401 if (s
->s_ttl
&& time_after(jiffies
, s
->s_ttl
)) {
3402 if (s
->s_state
== CEPH_MDS_SESSION_OPEN
) {
3403 s
->s_state
= CEPH_MDS_SESSION_HUNG
;
3404 pr_info("mds%d hung\n", s
->s_mds
);
3407 if (s
->s_state
< CEPH_MDS_SESSION_OPEN
) {
3408 /* this mds is failed or recovering, just wait */
3409 ceph_put_mds_session(s
);
3412 mutex_unlock(&mdsc
->mutex
);
3414 mutex_lock(&s
->s_mutex
);
3416 send_renew_caps(mdsc
, s
);
3418 ceph_con_keepalive(&s
->s_con
);
3419 if (s
->s_state
== CEPH_MDS_SESSION_OPEN
||
3420 s
->s_state
== CEPH_MDS_SESSION_HUNG
)
3421 ceph_send_cap_releases(mdsc
, s
);
3422 mutex_unlock(&s
->s_mutex
);
3423 ceph_put_mds_session(s
);
3425 mutex_lock(&mdsc
->mutex
);
3427 mutex_unlock(&mdsc
->mutex
);
3429 schedule_delayed(mdsc
);
3432 int ceph_mdsc_init(struct ceph_fs_client
*fsc
)
3435 struct ceph_mds_client
*mdsc
;
3437 mdsc
= kzalloc(sizeof(struct ceph_mds_client
), GFP_NOFS
);
3442 mutex_init(&mdsc
->mutex
);
3443 mdsc
->mdsmap
= kzalloc(sizeof(*mdsc
->mdsmap
), GFP_NOFS
);
3444 if (mdsc
->mdsmap
== NULL
) {
3449 init_completion(&mdsc
->safe_umount_waiters
);
3450 init_waitqueue_head(&mdsc
->session_close_wq
);
3451 INIT_LIST_HEAD(&mdsc
->waiting_for_map
);
3452 mdsc
->sessions
= NULL
;
3453 atomic_set(&mdsc
->num_sessions
, 0);
3454 mdsc
->max_sessions
= 0;
3456 mdsc
->last_snap_seq
= 0;
3457 init_rwsem(&mdsc
->snap_rwsem
);
3458 mdsc
->snap_realms
= RB_ROOT
;
3459 INIT_LIST_HEAD(&mdsc
->snap_empty
);
3460 spin_lock_init(&mdsc
->snap_empty_lock
);
3462 mdsc
->oldest_tid
= 0;
3463 mdsc
->request_tree
= RB_ROOT
;
3464 INIT_DELAYED_WORK(&mdsc
->delayed_work
, delayed_work
);
3465 mdsc
->last_renew_caps
= jiffies
;
3466 INIT_LIST_HEAD(&mdsc
->cap_delay_list
);
3467 spin_lock_init(&mdsc
->cap_delay_lock
);
3468 INIT_LIST_HEAD(&mdsc
->snap_flush_list
);
3469 spin_lock_init(&mdsc
->snap_flush_lock
);
3470 mdsc
->last_cap_flush_tid
= 1;
3471 mdsc
->cap_flush_tree
= RB_ROOT
;
3472 INIT_LIST_HEAD(&mdsc
->cap_dirty
);
3473 INIT_LIST_HEAD(&mdsc
->cap_dirty_migrating
);
3474 mdsc
->num_cap_flushing
= 0;
3475 spin_lock_init(&mdsc
->cap_dirty_lock
);
3476 init_waitqueue_head(&mdsc
->cap_flushing_wq
);
3477 spin_lock_init(&mdsc
->dentry_lru_lock
);
3478 INIT_LIST_HEAD(&mdsc
->dentry_lru
);
3480 ceph_caps_init(mdsc
);
3481 ceph_adjust_min_caps(mdsc
, fsc
->min_caps
);
3483 init_rwsem(&mdsc
->pool_perm_rwsem
);
3484 mdsc
->pool_perm_tree
= RB_ROOT
;
3490 * Wait for safe replies on open mds requests. If we time out, drop
3491 * all requests from the tree to avoid dangling dentry refs.
3493 static void wait_requests(struct ceph_mds_client
*mdsc
)
3495 struct ceph_options
*opts
= mdsc
->fsc
->client
->options
;
3496 struct ceph_mds_request
*req
;
3498 mutex_lock(&mdsc
->mutex
);
3499 if (__get_oldest_req(mdsc
)) {
3500 mutex_unlock(&mdsc
->mutex
);
3502 dout("wait_requests waiting for requests\n");
3503 wait_for_completion_timeout(&mdsc
->safe_umount_waiters
,
3504 ceph_timeout_jiffies(opts
->mount_timeout
));
3506 /* tear down remaining requests */
3507 mutex_lock(&mdsc
->mutex
);
3508 while ((req
= __get_oldest_req(mdsc
))) {
3509 dout("wait_requests timed out on tid %llu\n",
3511 __unregister_request(mdsc
, req
);
3514 mutex_unlock(&mdsc
->mutex
);
3515 dout("wait_requests done\n");
3519 * called before mount is ro, and before dentries are torn down.
3520 * (hmm, does this still race with new lookups?)
3522 void ceph_mdsc_pre_umount(struct ceph_mds_client
*mdsc
)
3524 dout("pre_umount\n");
3528 ceph_flush_dirty_caps(mdsc
);
3529 wait_requests(mdsc
);
3532 * wait for reply handlers to drop their request refs and
3533 * their inode/dcache refs
3539 * wait for all write mds requests to flush.
3541 static void wait_unsafe_requests(struct ceph_mds_client
*mdsc
, u64 want_tid
)
3543 struct ceph_mds_request
*req
= NULL
, *nextreq
;
3546 mutex_lock(&mdsc
->mutex
);
3547 dout("wait_unsafe_requests want %lld\n", want_tid
);
3549 req
= __get_oldest_req(mdsc
);
3550 while (req
&& req
->r_tid
<= want_tid
) {
3551 /* find next request */
3552 n
= rb_next(&req
->r_node
);
3554 nextreq
= rb_entry(n
, struct ceph_mds_request
, r_node
);
3557 if (req
->r_op
!= CEPH_MDS_OP_SETFILELOCK
&&
3558 (req
->r_op
& CEPH_MDS_OP_WRITE
)) {
3560 ceph_mdsc_get_request(req
);
3562 ceph_mdsc_get_request(nextreq
);
3563 mutex_unlock(&mdsc
->mutex
);
3564 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3565 req
->r_tid
, want_tid
);
3566 wait_for_completion(&req
->r_safe_completion
);
3567 mutex_lock(&mdsc
->mutex
);
3568 ceph_mdsc_put_request(req
);
3570 break; /* next dne before, so we're done! */
3571 if (RB_EMPTY_NODE(&nextreq
->r_node
)) {
3572 /* next request was removed from tree */
3573 ceph_mdsc_put_request(nextreq
);
3576 ceph_mdsc_put_request(nextreq
); /* won't go away */
3580 mutex_unlock(&mdsc
->mutex
);
3581 dout("wait_unsafe_requests done\n");
3584 void ceph_mdsc_sync(struct ceph_mds_client
*mdsc
)
3586 u64 want_tid
, want_flush
, want_snap
;
3588 if (ACCESS_ONCE(mdsc
->fsc
->mount_state
) == CEPH_MOUNT_SHUTDOWN
)
3592 mutex_lock(&mdsc
->mutex
);
3593 want_tid
= mdsc
->last_tid
;
3594 mutex_unlock(&mdsc
->mutex
);
3596 ceph_flush_dirty_caps(mdsc
);
3597 spin_lock(&mdsc
->cap_dirty_lock
);
3598 want_flush
= mdsc
->last_cap_flush_tid
;
3599 spin_unlock(&mdsc
->cap_dirty_lock
);
3601 down_read(&mdsc
->snap_rwsem
);
3602 want_snap
= mdsc
->last_snap_seq
;
3603 up_read(&mdsc
->snap_rwsem
);
3605 dout("sync want tid %lld flush_seq %lld snap_seq %lld\n",
3606 want_tid
, want_flush
, want_snap
);
3608 wait_unsafe_requests(mdsc
, want_tid
);
3609 wait_caps_flush(mdsc
, want_flush
, want_snap
);
3613 * true if all sessions are closed, or we force unmount
3615 static bool done_closing_sessions(struct ceph_mds_client
*mdsc
)
3617 if (ACCESS_ONCE(mdsc
->fsc
->mount_state
) == CEPH_MOUNT_SHUTDOWN
)
3619 return atomic_read(&mdsc
->num_sessions
) == 0;
3623 * called after sb is ro.
3625 void ceph_mdsc_close_sessions(struct ceph_mds_client
*mdsc
)
3627 struct ceph_options
*opts
= mdsc
->fsc
->client
->options
;
3628 struct ceph_mds_session
*session
;
3631 dout("close_sessions\n");
3633 /* close sessions */
3634 mutex_lock(&mdsc
->mutex
);
3635 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
3636 session
= __ceph_lookup_mds_session(mdsc
, i
);
3639 mutex_unlock(&mdsc
->mutex
);
3640 mutex_lock(&session
->s_mutex
);
3641 __close_session(mdsc
, session
);
3642 mutex_unlock(&session
->s_mutex
);
3643 ceph_put_mds_session(session
);
3644 mutex_lock(&mdsc
->mutex
);
3646 mutex_unlock(&mdsc
->mutex
);
3648 dout("waiting for sessions to close\n");
3649 wait_event_timeout(mdsc
->session_close_wq
, done_closing_sessions(mdsc
),
3650 ceph_timeout_jiffies(opts
->mount_timeout
));
3652 /* tear down remaining sessions */
3653 mutex_lock(&mdsc
->mutex
);
3654 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
3655 if (mdsc
->sessions
[i
]) {
3656 session
= get_session(mdsc
->sessions
[i
]);
3657 __unregister_session(mdsc
, session
);
3658 mutex_unlock(&mdsc
->mutex
);
3659 mutex_lock(&session
->s_mutex
);
3660 remove_session_caps(session
);
3661 mutex_unlock(&session
->s_mutex
);
3662 ceph_put_mds_session(session
);
3663 mutex_lock(&mdsc
->mutex
);
3666 WARN_ON(!list_empty(&mdsc
->cap_delay_list
));
3667 mutex_unlock(&mdsc
->mutex
);
3669 ceph_cleanup_empty_realms(mdsc
);
3671 cancel_delayed_work_sync(&mdsc
->delayed_work
); /* cancel timer */
3676 void ceph_mdsc_force_umount(struct ceph_mds_client
*mdsc
)
3678 struct ceph_mds_session
*session
;
3681 dout("force umount\n");
3683 mutex_lock(&mdsc
->mutex
);
3684 for (mds
= 0; mds
< mdsc
->max_sessions
; mds
++) {
3685 session
= __ceph_lookup_mds_session(mdsc
, mds
);
3688 mutex_unlock(&mdsc
->mutex
);
3689 mutex_lock(&session
->s_mutex
);
3690 __close_session(mdsc
, session
);
3691 if (session
->s_state
== CEPH_MDS_SESSION_CLOSING
) {
3692 cleanup_session_requests(mdsc
, session
);
3693 remove_session_caps(session
);
3695 mutex_unlock(&session
->s_mutex
);
3696 ceph_put_mds_session(session
);
3697 mutex_lock(&mdsc
->mutex
);
3698 kick_requests(mdsc
, mds
);
3700 __wake_requests(mdsc
, &mdsc
->waiting_for_map
);
3701 mutex_unlock(&mdsc
->mutex
);
3704 static void ceph_mdsc_stop(struct ceph_mds_client
*mdsc
)
3707 cancel_delayed_work_sync(&mdsc
->delayed_work
); /* cancel timer */
3709 ceph_mdsmap_destroy(mdsc
->mdsmap
);
3710 kfree(mdsc
->sessions
);
3711 ceph_caps_finalize(mdsc
);
3712 ceph_pool_perm_destroy(mdsc
);
3715 void ceph_mdsc_destroy(struct ceph_fs_client
*fsc
)
3717 struct ceph_mds_client
*mdsc
= fsc
->mdsc
;
3719 dout("mdsc_destroy %p\n", mdsc
);
3720 ceph_mdsc_stop(mdsc
);
3722 /* flush out any connection work with references to us */
3727 dout("mdsc_destroy %p done\n", mdsc
);
3732 * handle mds map update.
3734 void ceph_mdsc_handle_map(struct ceph_mds_client
*mdsc
, struct ceph_msg
*msg
)
3738 void *p
= msg
->front
.iov_base
;
3739 void *end
= p
+ msg
->front
.iov_len
;
3740 struct ceph_mdsmap
*newmap
, *oldmap
;
3741 struct ceph_fsid fsid
;
3744 ceph_decode_need(&p
, end
, sizeof(fsid
)+2*sizeof(u32
), bad
);
3745 ceph_decode_copy(&p
, &fsid
, sizeof(fsid
));
3746 if (ceph_check_fsid(mdsc
->fsc
->client
, &fsid
) < 0)
3748 epoch
= ceph_decode_32(&p
);
3749 maplen
= ceph_decode_32(&p
);
3750 dout("handle_map epoch %u len %d\n", epoch
, (int)maplen
);
3752 /* do we need it? */
3753 ceph_monc_got_mdsmap(&mdsc
->fsc
->client
->monc
, epoch
);
3754 mutex_lock(&mdsc
->mutex
);
3755 if (mdsc
->mdsmap
&& epoch
<= mdsc
->mdsmap
->m_epoch
) {
3756 dout("handle_map epoch %u <= our %u\n",
3757 epoch
, mdsc
->mdsmap
->m_epoch
);
3758 mutex_unlock(&mdsc
->mutex
);
3762 newmap
= ceph_mdsmap_decode(&p
, end
);
3763 if (IS_ERR(newmap
)) {
3764 err
= PTR_ERR(newmap
);
3768 /* swap into place */
3770 oldmap
= mdsc
->mdsmap
;
3771 mdsc
->mdsmap
= newmap
;
3772 check_new_map(mdsc
, newmap
, oldmap
);
3773 ceph_mdsmap_destroy(oldmap
);
3775 mdsc
->mdsmap
= newmap
; /* first mds map */
3777 mdsc
->fsc
->sb
->s_maxbytes
= mdsc
->mdsmap
->m_max_file_size
;
3779 __wake_requests(mdsc
, &mdsc
->waiting_for_map
);
3781 mutex_unlock(&mdsc
->mutex
);
3782 schedule_delayed(mdsc
);
3786 mutex_unlock(&mdsc
->mutex
);
3788 pr_err("error decoding mdsmap %d\n", err
);
3792 static struct ceph_connection
*con_get(struct ceph_connection
*con
)
3794 struct ceph_mds_session
*s
= con
->private;
3796 if (get_session(s
)) {
3797 dout("mdsc con_get %p ok (%d)\n", s
, atomic_read(&s
->s_ref
));
3800 dout("mdsc con_get %p FAIL\n", s
);
3804 static void con_put(struct ceph_connection
*con
)
3806 struct ceph_mds_session
*s
= con
->private;
3808 dout("mdsc con_put %p (%d)\n", s
, atomic_read(&s
->s_ref
) - 1);
3809 ceph_put_mds_session(s
);
3813 * if the client is unresponsive for long enough, the mds will kill
3814 * the session entirely.
3816 static void peer_reset(struct ceph_connection
*con
)
3818 struct ceph_mds_session
*s
= con
->private;
3819 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
3821 pr_warn("mds%d closed our session\n", s
->s_mds
);
3822 send_mds_reconnect(mdsc
, s
);
3825 static void dispatch(struct ceph_connection
*con
, struct ceph_msg
*msg
)
3827 struct ceph_mds_session
*s
= con
->private;
3828 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
3829 int type
= le16_to_cpu(msg
->hdr
.type
);
3831 mutex_lock(&mdsc
->mutex
);
3832 if (__verify_registered_session(mdsc
, s
) < 0) {
3833 mutex_unlock(&mdsc
->mutex
);
3836 mutex_unlock(&mdsc
->mutex
);
3839 case CEPH_MSG_MDS_MAP
:
3840 ceph_mdsc_handle_map(mdsc
, msg
);
3842 case CEPH_MSG_CLIENT_SESSION
:
3843 handle_session(s
, msg
);
3845 case CEPH_MSG_CLIENT_REPLY
:
3846 handle_reply(s
, msg
);
3848 case CEPH_MSG_CLIENT_REQUEST_FORWARD
:
3849 handle_forward(mdsc
, s
, msg
);
3851 case CEPH_MSG_CLIENT_CAPS
:
3852 ceph_handle_caps(s
, msg
);
3854 case CEPH_MSG_CLIENT_SNAP
:
3855 ceph_handle_snap(mdsc
, s
, msg
);
3857 case CEPH_MSG_CLIENT_LEASE
:
3858 handle_lease(mdsc
, s
, msg
);
3862 pr_err("received unknown message type %d %s\n", type
,
3863 ceph_msg_type_name(type
));
3874 * Note: returned pointer is the address of a structure that's
3875 * managed separately. Caller must *not* attempt to free it.
3877 static struct ceph_auth_handshake
*get_authorizer(struct ceph_connection
*con
,
3878 int *proto
, int force_new
)
3880 struct ceph_mds_session
*s
= con
->private;
3881 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
3882 struct ceph_auth_client
*ac
= mdsc
->fsc
->client
->monc
.auth
;
3883 struct ceph_auth_handshake
*auth
= &s
->s_auth
;
3885 if (force_new
&& auth
->authorizer
) {
3886 ceph_auth_destroy_authorizer(ac
, auth
->authorizer
);
3887 auth
->authorizer
= NULL
;
3889 if (!auth
->authorizer
) {
3890 int ret
= ceph_auth_create_authorizer(ac
, CEPH_ENTITY_TYPE_MDS
,
3893 return ERR_PTR(ret
);
3895 int ret
= ceph_auth_update_authorizer(ac
, CEPH_ENTITY_TYPE_MDS
,
3898 return ERR_PTR(ret
);
3900 *proto
= ac
->protocol
;
3906 static int verify_authorizer_reply(struct ceph_connection
*con
, int len
)
3908 struct ceph_mds_session
*s
= con
->private;
3909 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
3910 struct ceph_auth_client
*ac
= mdsc
->fsc
->client
->monc
.auth
;
3912 return ceph_auth_verify_authorizer_reply(ac
, s
->s_auth
.authorizer
, len
);
3915 static int invalidate_authorizer(struct ceph_connection
*con
)
3917 struct ceph_mds_session
*s
= con
->private;
3918 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
3919 struct ceph_auth_client
*ac
= mdsc
->fsc
->client
->monc
.auth
;
3921 ceph_auth_invalidate_authorizer(ac
, CEPH_ENTITY_TYPE_MDS
);
3923 return ceph_monc_validate_auth(&mdsc
->fsc
->client
->monc
);
3926 static struct ceph_msg
*mds_alloc_msg(struct ceph_connection
*con
,
3927 struct ceph_msg_header
*hdr
, int *skip
)
3929 struct ceph_msg
*msg
;
3930 int type
= (int) le16_to_cpu(hdr
->type
);
3931 int front_len
= (int) le32_to_cpu(hdr
->front_len
);
3937 msg
= ceph_msg_new(type
, front_len
, GFP_NOFS
, false);
3939 pr_err("unable to allocate msg type %d len %d\n",
3947 static int mds_sign_message(struct ceph_msg
*msg
)
3949 struct ceph_mds_session
*s
= msg
->con
->private;
3950 struct ceph_auth_handshake
*auth
= &s
->s_auth
;
3952 return ceph_auth_sign_message(auth
, msg
);
3955 static int mds_check_message_signature(struct ceph_msg
*msg
)
3957 struct ceph_mds_session
*s
= msg
->con
->private;
3958 struct ceph_auth_handshake
*auth
= &s
->s_auth
;
3960 return ceph_auth_check_message_signature(auth
, msg
);
3963 static const struct ceph_connection_operations mds_con_ops
= {
3966 .dispatch
= dispatch
,
3967 .get_authorizer
= get_authorizer
,
3968 .verify_authorizer_reply
= verify_authorizer_reply
,
3969 .invalidate_authorizer
= invalidate_authorizer
,
3970 .peer_reset
= peer_reset
,
3971 .alloc_msg
= mds_alloc_msg
,
3972 .sign_message
= mds_sign_message
,
3973 .check_message_signature
= mds_check_message_signature
,