1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
5 #include <linux/wait.h>
6 #include <linux/slab.h>
8 #include <linux/sched.h>
9 #include <linux/debugfs.h>
10 #include <linux/seq_file.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
;
103 info
->pool_ns_len
= 0;
104 info
->pool_ns_data
= NULL
;
105 if (features
& CEPH_FEATURE_FS_FILE_LAYOUT_V2
) {
106 ceph_decode_32_safe(p
, end
, info
->pool_ns_len
, bad
);
107 if (info
->pool_ns_len
> 0) {
108 ceph_decode_need(p
, end
, info
->pool_ns_len
, bad
);
109 info
->pool_ns_data
= *p
;
110 *p
+= info
->pool_ns_len
;
120 * parse a normal reply, which may contain a (dir+)dentry and/or a
123 static int parse_reply_info_trace(void **p
, void *end
,
124 struct ceph_mds_reply_info_parsed
*info
,
129 if (info
->head
->is_dentry
) {
130 err
= parse_reply_info_in(p
, end
, &info
->diri
, features
);
134 if (unlikely(*p
+ sizeof(*info
->dirfrag
) > end
))
137 *p
+= sizeof(*info
->dirfrag
) +
138 sizeof(u32
)*le32_to_cpu(info
->dirfrag
->ndist
);
139 if (unlikely(*p
> end
))
142 ceph_decode_32_safe(p
, end
, info
->dname_len
, bad
);
143 ceph_decode_need(p
, end
, info
->dname_len
, bad
);
145 *p
+= info
->dname_len
;
147 *p
+= sizeof(*info
->dlease
);
150 if (info
->head
->is_target
) {
151 err
= parse_reply_info_in(p
, end
, &info
->targeti
, features
);
156 if (unlikely(*p
!= end
))
163 pr_err("problem parsing mds trace %d\n", err
);
168 * parse readdir results
170 static int parse_reply_info_dir(void **p
, void *end
,
171 struct ceph_mds_reply_info_parsed
*info
,
178 if (*p
+ sizeof(*info
->dir_dir
) > end
)
180 *p
+= sizeof(*info
->dir_dir
) +
181 sizeof(u32
)*le32_to_cpu(info
->dir_dir
->ndist
);
185 ceph_decode_need(p
, end
, sizeof(num
) + 2, bad
);
186 num
= ceph_decode_32(p
);
188 u16 flags
= ceph_decode_16(p
);
189 info
->dir_end
= !!(flags
& CEPH_READDIR_FRAG_END
);
190 info
->dir_complete
= !!(flags
& CEPH_READDIR_FRAG_COMPLETE
);
191 info
->hash_order
= !!(flags
& CEPH_READDIR_HASH_ORDER
);
192 info
->offset_hash
= !!(flags
& CEPH_READDIR_OFFSET_HASH
);
197 BUG_ON(!info
->dir_entries
);
198 if ((unsigned long)(info
->dir_entries
+ num
) >
199 (unsigned long)info
->dir_entries
+ info
->dir_buf_size
) {
200 pr_err("dir contents are larger than expected\n");
207 struct ceph_mds_reply_dir_entry
*rde
= info
->dir_entries
+ i
;
209 ceph_decode_need(p
, end
, sizeof(u32
)*2, bad
);
210 rde
->name_len
= ceph_decode_32(p
);
211 ceph_decode_need(p
, end
, rde
->name_len
, bad
);
214 dout("parsed dir dname '%.*s'\n", rde
->name_len
, rde
->name
);
216 *p
+= sizeof(struct ceph_mds_reply_lease
);
219 err
= parse_reply_info_in(p
, end
, &rde
->inode
, features
);
222 /* ceph_readdir_prepopulate() will update it */
236 pr_err("problem parsing dir contents %d\n", err
);
241 * parse fcntl F_GETLK results
243 static int parse_reply_info_filelock(void **p
, void *end
,
244 struct ceph_mds_reply_info_parsed
*info
,
247 if (*p
+ sizeof(*info
->filelock_reply
) > end
)
250 info
->filelock_reply
= *p
;
251 *p
+= sizeof(*info
->filelock_reply
);
253 if (unlikely(*p
!= end
))
262 * parse create results
264 static int parse_reply_info_create(void **p
, void *end
,
265 struct ceph_mds_reply_info_parsed
*info
,
268 if (features
& CEPH_FEATURE_REPLY_CREATE_INODE
) {
270 info
->has_create_ino
= false;
272 info
->has_create_ino
= true;
273 info
->ino
= ceph_decode_64(p
);
277 if (unlikely(*p
!= end
))
286 * parse extra results
288 static int parse_reply_info_extra(void **p
, void *end
,
289 struct ceph_mds_reply_info_parsed
*info
,
292 u32 op
= le32_to_cpu(info
->head
->op
);
294 if (op
== CEPH_MDS_OP_GETFILELOCK
)
295 return parse_reply_info_filelock(p
, end
, info
, features
);
296 else if (op
== CEPH_MDS_OP_READDIR
|| op
== CEPH_MDS_OP_LSSNAP
)
297 return parse_reply_info_dir(p
, end
, info
, features
);
298 else if (op
== CEPH_MDS_OP_CREATE
)
299 return parse_reply_info_create(p
, end
, info
, features
);
305 * parse entire mds reply
307 static int parse_reply_info(struct ceph_msg
*msg
,
308 struct ceph_mds_reply_info_parsed
*info
,
315 info
->head
= msg
->front
.iov_base
;
316 p
= msg
->front
.iov_base
+ sizeof(struct ceph_mds_reply_head
);
317 end
= p
+ msg
->front
.iov_len
- sizeof(struct ceph_mds_reply_head
);
320 ceph_decode_32_safe(&p
, end
, len
, bad
);
322 ceph_decode_need(&p
, end
, len
, bad
);
323 err
= parse_reply_info_trace(&p
, p
+len
, info
, features
);
329 ceph_decode_32_safe(&p
, end
, len
, bad
);
331 ceph_decode_need(&p
, end
, len
, bad
);
332 err
= parse_reply_info_extra(&p
, p
+len
, info
, features
);
338 ceph_decode_32_safe(&p
, end
, len
, bad
);
339 info
->snapblob_len
= len
;
350 pr_err("mds parse_reply err %d\n", err
);
354 static void destroy_reply_info(struct ceph_mds_reply_info_parsed
*info
)
356 if (!info
->dir_entries
)
358 free_pages((unsigned long)info
->dir_entries
, get_order(info
->dir_buf_size
));
365 const char *ceph_session_state_name(int s
)
368 case CEPH_MDS_SESSION_NEW
: return "new";
369 case CEPH_MDS_SESSION_OPENING
: return "opening";
370 case CEPH_MDS_SESSION_OPEN
: return "open";
371 case CEPH_MDS_SESSION_HUNG
: return "hung";
372 case CEPH_MDS_SESSION_CLOSING
: return "closing";
373 case CEPH_MDS_SESSION_RESTARTING
: return "restarting";
374 case CEPH_MDS_SESSION_RECONNECTING
: return "reconnecting";
375 case CEPH_MDS_SESSION_REJECTED
: return "rejected";
376 default: return "???";
380 static struct ceph_mds_session
*get_session(struct ceph_mds_session
*s
)
382 if (refcount_inc_not_zero(&s
->s_ref
)) {
383 dout("mdsc get_session %p %d -> %d\n", s
,
384 refcount_read(&s
->s_ref
)-1, refcount_read(&s
->s_ref
));
387 dout("mdsc get_session %p 0 -- FAIL", s
);
392 void ceph_put_mds_session(struct ceph_mds_session
*s
)
394 dout("mdsc put_session %p %d -> %d\n", s
,
395 refcount_read(&s
->s_ref
), refcount_read(&s
->s_ref
)-1);
396 if (refcount_dec_and_test(&s
->s_ref
)) {
397 if (s
->s_auth
.authorizer
)
398 ceph_auth_destroy_authorizer(s
->s_auth
.authorizer
);
404 * called under mdsc->mutex
406 struct ceph_mds_session
*__ceph_lookup_mds_session(struct ceph_mds_client
*mdsc
,
409 struct ceph_mds_session
*session
;
411 if (mds
>= mdsc
->max_sessions
|| !mdsc
->sessions
[mds
])
413 session
= mdsc
->sessions
[mds
];
414 dout("lookup_mds_session %p %d\n", session
,
415 refcount_read(&session
->s_ref
));
416 get_session(session
);
420 static bool __have_session(struct ceph_mds_client
*mdsc
, int mds
)
422 if (mds
>= mdsc
->max_sessions
)
424 return mdsc
->sessions
[mds
];
427 static int __verify_registered_session(struct ceph_mds_client
*mdsc
,
428 struct ceph_mds_session
*s
)
430 if (s
->s_mds
>= mdsc
->max_sessions
||
431 mdsc
->sessions
[s
->s_mds
] != s
)
437 * create+register a new session for given mds.
438 * called under mdsc->mutex.
440 static struct ceph_mds_session
*register_session(struct ceph_mds_client
*mdsc
,
443 struct ceph_mds_session
*s
;
445 if (mds
>= mdsc
->mdsmap
->m_num_mds
)
446 return ERR_PTR(-EINVAL
);
448 s
= kzalloc(sizeof(*s
), GFP_NOFS
);
450 return ERR_PTR(-ENOMEM
);
453 s
->s_state
= CEPH_MDS_SESSION_NEW
;
456 mutex_init(&s
->s_mutex
);
458 ceph_con_init(&s
->s_con
, s
, &mds_con_ops
, &mdsc
->fsc
->client
->msgr
);
460 spin_lock_init(&s
->s_gen_ttl_lock
);
462 s
->s_cap_ttl
= jiffies
- 1;
464 spin_lock_init(&s
->s_cap_lock
);
465 s
->s_renew_requested
= 0;
467 INIT_LIST_HEAD(&s
->s_caps
);
470 refcount_set(&s
->s_ref
, 1);
471 INIT_LIST_HEAD(&s
->s_waiting
);
472 INIT_LIST_HEAD(&s
->s_unsafe
);
473 s
->s_num_cap_releases
= 0;
474 s
->s_cap_reconnect
= 0;
475 s
->s_cap_iterator
= NULL
;
476 INIT_LIST_HEAD(&s
->s_cap_releases
);
477 INIT_LIST_HEAD(&s
->s_cap_flushing
);
479 dout("register_session mds%d\n", mds
);
480 if (mds
>= mdsc
->max_sessions
) {
481 int newmax
= 1 << get_count_order(mds
+1);
482 struct ceph_mds_session
**sa
;
484 dout("register_session realloc to %d\n", newmax
);
485 sa
= kcalloc(newmax
, sizeof(void *), GFP_NOFS
);
488 if (mdsc
->sessions
) {
489 memcpy(sa
, mdsc
->sessions
,
490 mdsc
->max_sessions
* sizeof(void *));
491 kfree(mdsc
->sessions
);
494 mdsc
->max_sessions
= newmax
;
496 mdsc
->sessions
[mds
] = s
;
497 atomic_inc(&mdsc
->num_sessions
);
498 refcount_inc(&s
->s_ref
); /* one ref to sessions[], one to caller */
500 ceph_con_open(&s
->s_con
, CEPH_ENTITY_TYPE_MDS
, mds
,
501 ceph_mdsmap_get_addr(mdsc
->mdsmap
, mds
));
507 return ERR_PTR(-ENOMEM
);
511 * called under mdsc->mutex
513 static void __unregister_session(struct ceph_mds_client
*mdsc
,
514 struct ceph_mds_session
*s
)
516 dout("__unregister_session mds%d %p\n", s
->s_mds
, s
);
517 BUG_ON(mdsc
->sessions
[s
->s_mds
] != s
);
518 mdsc
->sessions
[s
->s_mds
] = NULL
;
519 ceph_con_close(&s
->s_con
);
520 ceph_put_mds_session(s
);
521 atomic_dec(&mdsc
->num_sessions
);
525 * drop session refs in request.
527 * should be last request ref, or hold mdsc->mutex
529 static void put_request_session(struct ceph_mds_request
*req
)
531 if (req
->r_session
) {
532 ceph_put_mds_session(req
->r_session
);
533 req
->r_session
= NULL
;
537 void ceph_mdsc_release_request(struct kref
*kref
)
539 struct ceph_mds_request
*req
= container_of(kref
,
540 struct ceph_mds_request
,
542 destroy_reply_info(&req
->r_reply_info
);
544 ceph_msg_put(req
->r_request
);
546 ceph_msg_put(req
->r_reply
);
548 ceph_put_cap_refs(ceph_inode(req
->r_inode
), CEPH_CAP_PIN
);
552 ceph_put_cap_refs(ceph_inode(req
->r_parent
), CEPH_CAP_PIN
);
553 iput(req
->r_target_inode
);
556 if (req
->r_old_dentry
)
557 dput(req
->r_old_dentry
);
558 if (req
->r_old_dentry_dir
) {
560 * track (and drop pins for) r_old_dentry_dir
561 * separately, since r_old_dentry's d_parent may have
562 * changed between the dir mutex being dropped and
563 * this request being freed.
565 ceph_put_cap_refs(ceph_inode(req
->r_old_dentry_dir
),
567 iput(req
->r_old_dentry_dir
);
572 ceph_pagelist_release(req
->r_pagelist
);
573 put_request_session(req
);
574 ceph_unreserve_caps(req
->r_mdsc
, &req
->r_caps_reservation
);
578 DEFINE_RB_FUNCS(request
, struct ceph_mds_request
, r_tid
, r_node
)
581 * lookup session, bump ref if found.
583 * called under mdsc->mutex.
585 static struct ceph_mds_request
*
586 lookup_get_request(struct ceph_mds_client
*mdsc
, u64 tid
)
588 struct ceph_mds_request
*req
;
590 req
= lookup_request(&mdsc
->request_tree
, tid
);
592 ceph_mdsc_get_request(req
);
598 * Register an in-flight request, and assign a tid. Link to directory
599 * are modifying (if any).
601 * Called under mdsc->mutex.
603 static void __register_request(struct ceph_mds_client
*mdsc
,
604 struct ceph_mds_request
*req
,
609 req
->r_tid
= ++mdsc
->last_tid
;
610 if (req
->r_num_caps
) {
611 ret
= ceph_reserve_caps(mdsc
, &req
->r_caps_reservation
,
614 pr_err("__register_request %p "
615 "failed to reserve caps: %d\n", req
, ret
);
616 /* set req->r_err to fail early from __do_request */
621 dout("__register_request %p tid %lld\n", req
, req
->r_tid
);
622 ceph_mdsc_get_request(req
);
623 insert_request(&mdsc
->request_tree
, req
);
625 req
->r_uid
= current_fsuid();
626 req
->r_gid
= current_fsgid();
628 if (mdsc
->oldest_tid
== 0 && req
->r_op
!= CEPH_MDS_OP_SETFILELOCK
)
629 mdsc
->oldest_tid
= req
->r_tid
;
633 req
->r_unsafe_dir
= dir
;
637 static void __unregister_request(struct ceph_mds_client
*mdsc
,
638 struct ceph_mds_request
*req
)
640 dout("__unregister_request %p tid %lld\n", req
, req
->r_tid
);
642 /* Never leave an unregistered request on an unsafe list! */
643 list_del_init(&req
->r_unsafe_item
);
645 if (req
->r_tid
== mdsc
->oldest_tid
) {
646 struct rb_node
*p
= rb_next(&req
->r_node
);
647 mdsc
->oldest_tid
= 0;
649 struct ceph_mds_request
*next_req
=
650 rb_entry(p
, struct ceph_mds_request
, r_node
);
651 if (next_req
->r_op
!= CEPH_MDS_OP_SETFILELOCK
) {
652 mdsc
->oldest_tid
= next_req
->r_tid
;
659 erase_request(&mdsc
->request_tree
, req
);
661 if (req
->r_unsafe_dir
&&
662 test_bit(CEPH_MDS_R_GOT_UNSAFE
, &req
->r_req_flags
)) {
663 struct ceph_inode_info
*ci
= ceph_inode(req
->r_unsafe_dir
);
664 spin_lock(&ci
->i_unsafe_lock
);
665 list_del_init(&req
->r_unsafe_dir_item
);
666 spin_unlock(&ci
->i_unsafe_lock
);
668 if (req
->r_target_inode
&&
669 test_bit(CEPH_MDS_R_GOT_UNSAFE
, &req
->r_req_flags
)) {
670 struct ceph_inode_info
*ci
= ceph_inode(req
->r_target_inode
);
671 spin_lock(&ci
->i_unsafe_lock
);
672 list_del_init(&req
->r_unsafe_target_item
);
673 spin_unlock(&ci
->i_unsafe_lock
);
676 if (req
->r_unsafe_dir
) {
677 iput(req
->r_unsafe_dir
);
678 req
->r_unsafe_dir
= NULL
;
681 complete_all(&req
->r_safe_completion
);
683 ceph_mdsc_put_request(req
);
687 * Walk back up the dentry tree until we hit a dentry representing a
688 * non-snapshot inode. We do this using the rcu_read_lock (which must be held
689 * when calling this) to ensure that the objects won't disappear while we're
690 * working with them. Once we hit a candidate dentry, we attempt to take a
691 * reference to it, and return that as the result.
693 static struct inode
*get_nonsnap_parent(struct dentry
*dentry
)
695 struct inode
*inode
= NULL
;
697 while (dentry
&& !IS_ROOT(dentry
)) {
698 inode
= d_inode_rcu(dentry
);
699 if (!inode
|| ceph_snap(inode
) == CEPH_NOSNAP
)
701 dentry
= dentry
->d_parent
;
704 inode
= igrab(inode
);
709 * Choose mds to send request to next. If there is a hint set in the
710 * request (e.g., due to a prior forward hint from the mds), use that.
711 * Otherwise, consult frag tree and/or caps to identify the
712 * appropriate mds. If all else fails, choose randomly.
714 * Called under mdsc->mutex.
716 static int __choose_mds(struct ceph_mds_client
*mdsc
,
717 struct ceph_mds_request
*req
)
720 struct ceph_inode_info
*ci
;
721 struct ceph_cap
*cap
;
722 int mode
= req
->r_direct_mode
;
724 u32 hash
= req
->r_direct_hash
;
725 bool is_hash
= test_bit(CEPH_MDS_R_DIRECT_IS_HASH
, &req
->r_req_flags
);
728 * is there a specific mds we should try? ignore hint if we have
729 * no session and the mds is not up (active or recovering).
731 if (req
->r_resend_mds
>= 0 &&
732 (__have_session(mdsc
, req
->r_resend_mds
) ||
733 ceph_mdsmap_get_state(mdsc
->mdsmap
, req
->r_resend_mds
) > 0)) {
734 dout("choose_mds using resend_mds mds%d\n",
736 return req
->r_resend_mds
;
739 if (mode
== USE_RANDOM_MDS
)
744 if (ceph_snap(req
->r_inode
) != CEPH_SNAPDIR
) {
745 inode
= req
->r_inode
;
748 /* req->r_dentry is non-null for LSSNAP request */
750 inode
= get_nonsnap_parent(req
->r_dentry
);
752 dout("__choose_mds using snapdir's parent %p\n", inode
);
754 } else if (req
->r_dentry
) {
755 /* ignore race with rename; old or new d_parent is okay */
756 struct dentry
*parent
;
760 parent
= req
->r_dentry
->d_parent
;
761 dir
= req
->r_parent
? : d_inode_rcu(parent
);
763 if (!dir
|| dir
->i_sb
!= mdsc
->fsc
->sb
) {
764 /* not this fs or parent went negative */
765 inode
= d_inode(req
->r_dentry
);
768 } else if (ceph_snap(dir
) != CEPH_NOSNAP
) {
769 /* direct snapped/virtual snapdir requests
770 * based on parent dir inode */
771 inode
= get_nonsnap_parent(parent
);
772 dout("__choose_mds using nonsnap parent %p\n", inode
);
775 inode
= d_inode(req
->r_dentry
);
776 if (!inode
|| mode
== USE_AUTH_MDS
) {
779 hash
= ceph_dentry_hash(dir
, req
->r_dentry
);
788 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode
, (int)is_hash
,
792 ci
= ceph_inode(inode
);
794 if (is_hash
&& S_ISDIR(inode
->i_mode
)) {
795 struct ceph_inode_frag frag
;
798 ceph_choose_frag(ci
, hash
, &frag
, &found
);
800 if (mode
== USE_ANY_MDS
&& frag
.ndist
> 0) {
803 /* choose a random replica */
804 get_random_bytes(&r
, 1);
807 dout("choose_mds %p %llx.%llx "
808 "frag %u mds%d (%d/%d)\n",
809 inode
, ceph_vinop(inode
),
812 if (ceph_mdsmap_get_state(mdsc
->mdsmap
, mds
) >=
813 CEPH_MDS_STATE_ACTIVE
)
817 /* since this file/dir wasn't known to be
818 * replicated, then we want to look for the
819 * authoritative mds. */
822 /* choose auth mds */
824 dout("choose_mds %p %llx.%llx "
825 "frag %u mds%d (auth)\n",
826 inode
, ceph_vinop(inode
), frag
.frag
, mds
);
827 if (ceph_mdsmap_get_state(mdsc
->mdsmap
, mds
) >=
828 CEPH_MDS_STATE_ACTIVE
)
834 spin_lock(&ci
->i_ceph_lock
);
836 if (mode
== USE_AUTH_MDS
)
837 cap
= ci
->i_auth_cap
;
838 if (!cap
&& !RB_EMPTY_ROOT(&ci
->i_caps
))
839 cap
= rb_entry(rb_first(&ci
->i_caps
), struct ceph_cap
, ci_node
);
841 spin_unlock(&ci
->i_ceph_lock
);
845 mds
= cap
->session
->s_mds
;
846 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
847 inode
, ceph_vinop(inode
), mds
,
848 cap
== ci
->i_auth_cap
? "auth " : "", cap
);
849 spin_unlock(&ci
->i_ceph_lock
);
855 mds
= ceph_mdsmap_get_random_mds(mdsc
->mdsmap
);
856 dout("choose_mds chose random mds%d\n", mds
);
864 static struct ceph_msg
*create_session_msg(u32 op
, u64 seq
)
866 struct ceph_msg
*msg
;
867 struct ceph_mds_session_head
*h
;
869 msg
= ceph_msg_new(CEPH_MSG_CLIENT_SESSION
, sizeof(*h
), GFP_NOFS
,
872 pr_err("create_session_msg ENOMEM creating msg\n");
875 h
= msg
->front
.iov_base
;
876 h
->op
= cpu_to_le32(op
);
877 h
->seq
= cpu_to_le64(seq
);
883 * session message, specialization for CEPH_SESSION_REQUEST_OPEN
884 * to include additional client metadata fields.
886 static struct ceph_msg
*create_session_open_msg(struct ceph_mds_client
*mdsc
, u64 seq
)
888 struct ceph_msg
*msg
;
889 struct ceph_mds_session_head
*h
;
891 int metadata_bytes
= 0;
892 int metadata_key_count
= 0;
893 struct ceph_options
*opt
= mdsc
->fsc
->client
->options
;
894 struct ceph_mount_options
*fsopt
= mdsc
->fsc
->mount_options
;
897 const char* metadata
[][2] = {
898 {"hostname", mdsc
->nodename
},
899 {"kernel_version", init_utsname()->release
},
900 {"entity_id", opt
->name
? : ""},
901 {"root", fsopt
->server_path
? : "/"},
905 /* Calculate serialized length of metadata */
906 metadata_bytes
= 4; /* map length */
907 for (i
= 0; metadata
[i
][0]; ++i
) {
908 metadata_bytes
+= 8 + strlen(metadata
[i
][0]) +
909 strlen(metadata
[i
][1]);
910 metadata_key_count
++;
913 /* Allocate the message */
914 msg
= ceph_msg_new(CEPH_MSG_CLIENT_SESSION
, sizeof(*h
) + metadata_bytes
,
917 pr_err("create_session_msg ENOMEM creating msg\n");
920 h
= msg
->front
.iov_base
;
921 h
->op
= cpu_to_le32(CEPH_SESSION_REQUEST_OPEN
);
922 h
->seq
= cpu_to_le64(seq
);
925 * Serialize client metadata into waiting buffer space, using
926 * the format that userspace expects for map<string, string>
928 * ClientSession messages with metadata are v2
930 msg
->hdr
.version
= cpu_to_le16(2);
931 msg
->hdr
.compat_version
= cpu_to_le16(1);
933 /* The write pointer, following the session_head structure */
934 p
= msg
->front
.iov_base
+ sizeof(*h
);
936 /* Number of entries in the map */
937 ceph_encode_32(&p
, metadata_key_count
);
939 /* Two length-prefixed strings for each entry in the map */
940 for (i
= 0; metadata
[i
][0]; ++i
) {
941 size_t const key_len
= strlen(metadata
[i
][0]);
942 size_t const val_len
= strlen(metadata
[i
][1]);
944 ceph_encode_32(&p
, key_len
);
945 memcpy(p
, metadata
[i
][0], key_len
);
947 ceph_encode_32(&p
, val_len
);
948 memcpy(p
, metadata
[i
][1], val_len
);
956 * send session open request.
958 * called under mdsc->mutex
960 static int __open_session(struct ceph_mds_client
*mdsc
,
961 struct ceph_mds_session
*session
)
963 struct ceph_msg
*msg
;
965 int mds
= session
->s_mds
;
967 /* wait for mds to go active? */
968 mstate
= ceph_mdsmap_get_state(mdsc
->mdsmap
, mds
);
969 dout("open_session to mds%d (%s)\n", mds
,
970 ceph_mds_state_name(mstate
));
971 session
->s_state
= CEPH_MDS_SESSION_OPENING
;
972 session
->s_renew_requested
= jiffies
;
974 /* send connect message */
975 msg
= create_session_open_msg(mdsc
, session
->s_seq
);
978 ceph_con_send(&session
->s_con
, msg
);
983 * open sessions for any export targets for the given mds
985 * called under mdsc->mutex
987 static struct ceph_mds_session
*
988 __open_export_target_session(struct ceph_mds_client
*mdsc
, int target
)
990 struct ceph_mds_session
*session
;
992 session
= __ceph_lookup_mds_session(mdsc
, target
);
994 session
= register_session(mdsc
, target
);
998 if (session
->s_state
== CEPH_MDS_SESSION_NEW
||
999 session
->s_state
== CEPH_MDS_SESSION_CLOSING
)
1000 __open_session(mdsc
, session
);
1005 struct ceph_mds_session
*
1006 ceph_mdsc_open_export_target_session(struct ceph_mds_client
*mdsc
, int target
)
1008 struct ceph_mds_session
*session
;
1010 dout("open_export_target_session to mds%d\n", target
);
1012 mutex_lock(&mdsc
->mutex
);
1013 session
= __open_export_target_session(mdsc
, target
);
1014 mutex_unlock(&mdsc
->mutex
);
1019 static void __open_export_target_sessions(struct ceph_mds_client
*mdsc
,
1020 struct ceph_mds_session
*session
)
1022 struct ceph_mds_info
*mi
;
1023 struct ceph_mds_session
*ts
;
1024 int i
, mds
= session
->s_mds
;
1026 if (mds
>= mdsc
->mdsmap
->m_num_mds
)
1029 mi
= &mdsc
->mdsmap
->m_info
[mds
];
1030 dout("open_export_target_sessions for mds%d (%d targets)\n",
1031 session
->s_mds
, mi
->num_export_targets
);
1033 for (i
= 0; i
< mi
->num_export_targets
; i
++) {
1034 ts
= __open_export_target_session(mdsc
, mi
->export_targets
[i
]);
1036 ceph_put_mds_session(ts
);
1040 void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client
*mdsc
,
1041 struct ceph_mds_session
*session
)
1043 mutex_lock(&mdsc
->mutex
);
1044 __open_export_target_sessions(mdsc
, session
);
1045 mutex_unlock(&mdsc
->mutex
);
1052 static void detach_cap_releases(struct ceph_mds_session
*session
,
1053 struct list_head
*target
)
1055 lockdep_assert_held(&session
->s_cap_lock
);
1057 list_splice_init(&session
->s_cap_releases
, target
);
1058 session
->s_num_cap_releases
= 0;
1059 dout("dispose_cap_releases mds%d\n", session
->s_mds
);
1062 static void dispose_cap_releases(struct ceph_mds_client
*mdsc
,
1063 struct list_head
*dispose
)
1065 while (!list_empty(dispose
)) {
1066 struct ceph_cap
*cap
;
1067 /* zero out the in-progress message */
1068 cap
= list_first_entry(dispose
, struct ceph_cap
, session_caps
);
1069 list_del(&cap
->session_caps
);
1070 ceph_put_cap(mdsc
, cap
);
1074 static void cleanup_session_requests(struct ceph_mds_client
*mdsc
,
1075 struct ceph_mds_session
*session
)
1077 struct ceph_mds_request
*req
;
1080 dout("cleanup_session_requests mds%d\n", session
->s_mds
);
1081 mutex_lock(&mdsc
->mutex
);
1082 while (!list_empty(&session
->s_unsafe
)) {
1083 req
= list_first_entry(&session
->s_unsafe
,
1084 struct ceph_mds_request
, r_unsafe_item
);
1085 pr_warn_ratelimited(" dropping unsafe request %llu\n",
1087 __unregister_request(mdsc
, req
);
1089 /* zero r_attempts, so kick_requests() will re-send requests */
1090 p
= rb_first(&mdsc
->request_tree
);
1092 req
= rb_entry(p
, struct ceph_mds_request
, r_node
);
1094 if (req
->r_session
&&
1095 req
->r_session
->s_mds
== session
->s_mds
)
1096 req
->r_attempts
= 0;
1098 mutex_unlock(&mdsc
->mutex
);
1102 * Helper to safely iterate over all caps associated with a session, with
1103 * special care taken to handle a racing __ceph_remove_cap().
1105 * Caller must hold session s_mutex.
1107 static int iterate_session_caps(struct ceph_mds_session
*session
,
1108 int (*cb
)(struct inode
*, struct ceph_cap
*,
1111 struct list_head
*p
;
1112 struct ceph_cap
*cap
;
1113 struct inode
*inode
, *last_inode
= NULL
;
1114 struct ceph_cap
*old_cap
= NULL
;
1117 dout("iterate_session_caps %p mds%d\n", session
, session
->s_mds
);
1118 spin_lock(&session
->s_cap_lock
);
1119 p
= session
->s_caps
.next
;
1120 while (p
!= &session
->s_caps
) {
1121 cap
= list_entry(p
, struct ceph_cap
, session_caps
);
1122 inode
= igrab(&cap
->ci
->vfs_inode
);
1127 session
->s_cap_iterator
= cap
;
1128 spin_unlock(&session
->s_cap_lock
);
1135 ceph_put_cap(session
->s_mdsc
, old_cap
);
1139 ret
= cb(inode
, cap
, arg
);
1142 spin_lock(&session
->s_cap_lock
);
1145 dout("iterate_session_caps finishing cap %p removal\n",
1147 BUG_ON(cap
->session
!= session
);
1148 cap
->session
= NULL
;
1149 list_del_init(&cap
->session_caps
);
1150 session
->s_nr_caps
--;
1151 if (cap
->queue_release
) {
1152 list_add_tail(&cap
->session_caps
,
1153 &session
->s_cap_releases
);
1154 session
->s_num_cap_releases
++;
1156 old_cap
= cap
; /* put_cap it w/o locks held */
1164 session
->s_cap_iterator
= NULL
;
1165 spin_unlock(&session
->s_cap_lock
);
1169 ceph_put_cap(session
->s_mdsc
, old_cap
);
1174 static int remove_session_caps_cb(struct inode
*inode
, struct ceph_cap
*cap
,
1177 struct ceph_fs_client
*fsc
= (struct ceph_fs_client
*)arg
;
1178 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1179 LIST_HEAD(to_remove
);
1181 bool invalidate
= false;
1183 dout("removing cap %p, ci is %p, inode is %p\n",
1184 cap
, ci
, &ci
->vfs_inode
);
1185 spin_lock(&ci
->i_ceph_lock
);
1186 __ceph_remove_cap(cap
, false);
1187 if (!ci
->i_auth_cap
) {
1188 struct ceph_cap_flush
*cf
;
1189 struct ceph_mds_client
*mdsc
= fsc
->mdsc
;
1191 ci
->i_ceph_flags
|= CEPH_I_CAP_DROPPED
;
1193 if (ci
->i_wrbuffer_ref
> 0 &&
1194 READ_ONCE(fsc
->mount_state
) == CEPH_MOUNT_SHUTDOWN
)
1197 while (!list_empty(&ci
->i_cap_flush_list
)) {
1198 cf
= list_first_entry(&ci
->i_cap_flush_list
,
1199 struct ceph_cap_flush
, i_list
);
1200 list_move(&cf
->i_list
, &to_remove
);
1203 spin_lock(&mdsc
->cap_dirty_lock
);
1205 list_for_each_entry(cf
, &to_remove
, i_list
)
1206 list_del(&cf
->g_list
);
1208 if (!list_empty(&ci
->i_dirty_item
)) {
1209 pr_warn_ratelimited(
1210 " dropping dirty %s state for %p %lld\n",
1211 ceph_cap_string(ci
->i_dirty_caps
),
1212 inode
, ceph_ino(inode
));
1213 ci
->i_dirty_caps
= 0;
1214 list_del_init(&ci
->i_dirty_item
);
1217 if (!list_empty(&ci
->i_flushing_item
)) {
1218 pr_warn_ratelimited(
1219 " dropping dirty+flushing %s state for %p %lld\n",
1220 ceph_cap_string(ci
->i_flushing_caps
),
1221 inode
, ceph_ino(inode
));
1222 ci
->i_flushing_caps
= 0;
1223 list_del_init(&ci
->i_flushing_item
);
1224 mdsc
->num_cap_flushing
--;
1227 spin_unlock(&mdsc
->cap_dirty_lock
);
1229 if (atomic_read(&ci
->i_filelock_ref
) > 0) {
1230 /* make further file lock syscall return -EIO */
1231 ci
->i_ceph_flags
|= CEPH_I_ERROR_FILELOCK
;
1232 pr_warn_ratelimited(" dropping file locks for %p %lld\n",
1233 inode
, ceph_ino(inode
));
1236 if (!ci
->i_dirty_caps
&& ci
->i_prealloc_cap_flush
) {
1237 list_add(&ci
->i_prealloc_cap_flush
->i_list
, &to_remove
);
1238 ci
->i_prealloc_cap_flush
= NULL
;
1241 spin_unlock(&ci
->i_ceph_lock
);
1242 while (!list_empty(&to_remove
)) {
1243 struct ceph_cap_flush
*cf
;
1244 cf
= list_first_entry(&to_remove
,
1245 struct ceph_cap_flush
, i_list
);
1246 list_del(&cf
->i_list
);
1247 ceph_free_cap_flush(cf
);
1250 wake_up_all(&ci
->i_cap_wq
);
1252 ceph_queue_invalidate(inode
);
1259 * caller must hold session s_mutex
1261 static void remove_session_caps(struct ceph_mds_session
*session
)
1263 struct ceph_fs_client
*fsc
= session
->s_mdsc
->fsc
;
1264 struct super_block
*sb
= fsc
->sb
;
1267 dout("remove_session_caps on %p\n", session
);
1268 iterate_session_caps(session
, remove_session_caps_cb
, fsc
);
1270 wake_up_all(&fsc
->mdsc
->cap_flushing_wq
);
1272 spin_lock(&session
->s_cap_lock
);
1273 if (session
->s_nr_caps
> 0) {
1274 struct inode
*inode
;
1275 struct ceph_cap
*cap
, *prev
= NULL
;
1276 struct ceph_vino vino
;
1278 * iterate_session_caps() skips inodes that are being
1279 * deleted, we need to wait until deletions are complete.
1280 * __wait_on_freeing_inode() is designed for the job,
1281 * but it is not exported, so use lookup inode function
1284 while (!list_empty(&session
->s_caps
)) {
1285 cap
= list_entry(session
->s_caps
.next
,
1286 struct ceph_cap
, session_caps
);
1290 vino
= cap
->ci
->i_vino
;
1291 spin_unlock(&session
->s_cap_lock
);
1293 inode
= ceph_find_inode(sb
, vino
);
1296 spin_lock(&session
->s_cap_lock
);
1300 // drop cap expires and unlock s_cap_lock
1301 detach_cap_releases(session
, &dispose
);
1303 BUG_ON(session
->s_nr_caps
> 0);
1304 BUG_ON(!list_empty(&session
->s_cap_flushing
));
1305 spin_unlock(&session
->s_cap_lock
);
1306 dispose_cap_releases(session
->s_mdsc
, &dispose
);
1310 * wake up any threads waiting on this session's caps. if the cap is
1311 * old (didn't get renewed on the client reconnect), remove it now.
1313 * caller must hold s_mutex.
1315 static int wake_up_session_cb(struct inode
*inode
, struct ceph_cap
*cap
,
1318 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1321 spin_lock(&ci
->i_ceph_lock
);
1322 ci
->i_wanted_max_size
= 0;
1323 ci
->i_requested_max_size
= 0;
1324 spin_unlock(&ci
->i_ceph_lock
);
1326 wake_up_all(&ci
->i_cap_wq
);
1330 static void wake_up_session_caps(struct ceph_mds_session
*session
,
1333 dout("wake_up_session_caps %p mds%d\n", session
, session
->s_mds
);
1334 iterate_session_caps(session
, wake_up_session_cb
,
1335 (void *)(unsigned long)reconnect
);
1339 * Send periodic message to MDS renewing all currently held caps. The
1340 * ack will reset the expiration for all caps from this session.
1342 * caller holds s_mutex
1344 static int send_renew_caps(struct ceph_mds_client
*mdsc
,
1345 struct ceph_mds_session
*session
)
1347 struct ceph_msg
*msg
;
1350 if (time_after_eq(jiffies
, session
->s_cap_ttl
) &&
1351 time_after_eq(session
->s_cap_ttl
, session
->s_renew_requested
))
1352 pr_info("mds%d caps stale\n", session
->s_mds
);
1353 session
->s_renew_requested
= jiffies
;
1355 /* do not try to renew caps until a recovering mds has reconnected
1356 * with its clients. */
1357 state
= ceph_mdsmap_get_state(mdsc
->mdsmap
, session
->s_mds
);
1358 if (state
< CEPH_MDS_STATE_RECONNECT
) {
1359 dout("send_renew_caps ignoring mds%d (%s)\n",
1360 session
->s_mds
, ceph_mds_state_name(state
));
1364 dout("send_renew_caps to mds%d (%s)\n", session
->s_mds
,
1365 ceph_mds_state_name(state
));
1366 msg
= create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS
,
1367 ++session
->s_renew_seq
);
1370 ceph_con_send(&session
->s_con
, msg
);
1374 static int send_flushmsg_ack(struct ceph_mds_client
*mdsc
,
1375 struct ceph_mds_session
*session
, u64 seq
)
1377 struct ceph_msg
*msg
;
1379 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
1380 session
->s_mds
, ceph_session_state_name(session
->s_state
), seq
);
1381 msg
= create_session_msg(CEPH_SESSION_FLUSHMSG_ACK
, seq
);
1384 ceph_con_send(&session
->s_con
, msg
);
1390 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
1392 * Called under session->s_mutex
1394 static void renewed_caps(struct ceph_mds_client
*mdsc
,
1395 struct ceph_mds_session
*session
, int is_renew
)
1400 spin_lock(&session
->s_cap_lock
);
1401 was_stale
= is_renew
&& time_after_eq(jiffies
, session
->s_cap_ttl
);
1403 session
->s_cap_ttl
= session
->s_renew_requested
+
1404 mdsc
->mdsmap
->m_session_timeout
*HZ
;
1407 if (time_before(jiffies
, session
->s_cap_ttl
)) {
1408 pr_info("mds%d caps renewed\n", session
->s_mds
);
1411 pr_info("mds%d caps still stale\n", session
->s_mds
);
1414 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1415 session
->s_mds
, session
->s_cap_ttl
, was_stale
? "stale" : "fresh",
1416 time_before(jiffies
, session
->s_cap_ttl
) ? "stale" : "fresh");
1417 spin_unlock(&session
->s_cap_lock
);
1420 wake_up_session_caps(session
, 0);
1424 * send a session close request
1426 static int request_close_session(struct ceph_mds_client
*mdsc
,
1427 struct ceph_mds_session
*session
)
1429 struct ceph_msg
*msg
;
1431 dout("request_close_session mds%d state %s seq %lld\n",
1432 session
->s_mds
, ceph_session_state_name(session
->s_state
),
1434 msg
= create_session_msg(CEPH_SESSION_REQUEST_CLOSE
, session
->s_seq
);
1437 ceph_con_send(&session
->s_con
, msg
);
1442 * Called with s_mutex held.
1444 static int __close_session(struct ceph_mds_client
*mdsc
,
1445 struct ceph_mds_session
*session
)
1447 if (session
->s_state
>= CEPH_MDS_SESSION_CLOSING
)
1449 session
->s_state
= CEPH_MDS_SESSION_CLOSING
;
1450 return request_close_session(mdsc
, session
);
1453 static bool drop_negative_children(struct dentry
*dentry
)
1455 struct dentry
*child
;
1456 bool all_negative
= true;
1458 if (!d_is_dir(dentry
))
1461 spin_lock(&dentry
->d_lock
);
1462 list_for_each_entry(child
, &dentry
->d_subdirs
, d_child
) {
1463 if (d_really_is_positive(child
)) {
1464 all_negative
= false;
1468 spin_unlock(&dentry
->d_lock
);
1471 shrink_dcache_parent(dentry
);
1473 return all_negative
;
1477 * Trim old(er) caps.
1479 * Because we can't cache an inode without one or more caps, we do
1480 * this indirectly: if a cap is unused, we prune its aliases, at which
1481 * point the inode will hopefully get dropped to.
1483 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1484 * memory pressure from the MDS, though, so it needn't be perfect.
1486 static int trim_caps_cb(struct inode
*inode
, struct ceph_cap
*cap
, void *arg
)
1488 struct ceph_mds_session
*session
= arg
;
1489 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1490 int used
, wanted
, oissued
, mine
;
1492 if (session
->s_trim_caps
<= 0)
1495 spin_lock(&ci
->i_ceph_lock
);
1496 mine
= cap
->issued
| cap
->implemented
;
1497 used
= __ceph_caps_used(ci
);
1498 wanted
= __ceph_caps_file_wanted(ci
);
1499 oissued
= __ceph_caps_issued_other(ci
, cap
);
1501 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
1502 inode
, cap
, ceph_cap_string(mine
), ceph_cap_string(oissued
),
1503 ceph_cap_string(used
), ceph_cap_string(wanted
));
1504 if (cap
== ci
->i_auth_cap
) {
1505 if (ci
->i_dirty_caps
|| ci
->i_flushing_caps
||
1506 !list_empty(&ci
->i_cap_snaps
))
1508 if ((used
| wanted
) & CEPH_CAP_ANY_WR
)
1510 /* Note: it's possible that i_filelock_ref becomes non-zero
1511 * after dropping auth caps. It doesn't hurt because reply
1512 * of lock mds request will re-add auth caps. */
1513 if (atomic_read(&ci
->i_filelock_ref
) > 0)
1516 /* The inode has cached pages, but it's no longer used.
1517 * we can safely drop it */
1518 if (wanted
== 0 && used
== CEPH_CAP_FILE_CACHE
&&
1519 !(oissued
& CEPH_CAP_FILE_CACHE
)) {
1523 if ((used
| wanted
) & ~oissued
& mine
)
1524 goto out
; /* we need these caps */
1527 /* we aren't the only cap.. just remove us */
1528 __ceph_remove_cap(cap
, true);
1529 session
->s_trim_caps
--;
1531 struct dentry
*dentry
;
1532 /* try dropping referring dentries */
1533 spin_unlock(&ci
->i_ceph_lock
);
1534 dentry
= d_find_any_alias(inode
);
1535 if (dentry
&& drop_negative_children(dentry
)) {
1538 d_prune_aliases(inode
);
1539 count
= atomic_read(&inode
->i_count
);
1541 session
->s_trim_caps
--;
1542 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1551 spin_unlock(&ci
->i_ceph_lock
);
1556 * Trim session cap count down to some max number.
1558 int ceph_trim_caps(struct ceph_mds_client
*mdsc
,
1559 struct ceph_mds_session
*session
,
1562 int trim_caps
= session
->s_nr_caps
- max_caps
;
1564 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1565 session
->s_mds
, session
->s_nr_caps
, max_caps
, trim_caps
);
1566 if (trim_caps
> 0) {
1567 session
->s_trim_caps
= trim_caps
;
1568 iterate_session_caps(session
, trim_caps_cb
, session
);
1569 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1570 session
->s_mds
, session
->s_nr_caps
, max_caps
,
1571 trim_caps
- session
->s_trim_caps
);
1572 session
->s_trim_caps
= 0;
1575 ceph_send_cap_releases(mdsc
, session
);
1579 static int check_caps_flush(struct ceph_mds_client
*mdsc
,
1584 spin_lock(&mdsc
->cap_dirty_lock
);
1585 if (!list_empty(&mdsc
->cap_flush_list
)) {
1586 struct ceph_cap_flush
*cf
=
1587 list_first_entry(&mdsc
->cap_flush_list
,
1588 struct ceph_cap_flush
, g_list
);
1589 if (cf
->tid
<= want_flush_tid
) {
1590 dout("check_caps_flush still flushing tid "
1591 "%llu <= %llu\n", cf
->tid
, want_flush_tid
);
1595 spin_unlock(&mdsc
->cap_dirty_lock
);
1600 * flush all dirty inode data to disk.
1602 * returns true if we've flushed through want_flush_tid
1604 static void wait_caps_flush(struct ceph_mds_client
*mdsc
,
1607 dout("check_caps_flush want %llu\n", want_flush_tid
);
1609 wait_event(mdsc
->cap_flushing_wq
,
1610 check_caps_flush(mdsc
, want_flush_tid
));
1612 dout("check_caps_flush ok, flushed thru %llu\n", want_flush_tid
);
1616 * called under s_mutex
1618 void ceph_send_cap_releases(struct ceph_mds_client
*mdsc
,
1619 struct ceph_mds_session
*session
)
1621 struct ceph_msg
*msg
= NULL
;
1622 struct ceph_mds_cap_release
*head
;
1623 struct ceph_mds_cap_item
*item
;
1624 struct ceph_osd_client
*osdc
= &mdsc
->fsc
->client
->osdc
;
1625 struct ceph_cap
*cap
;
1626 LIST_HEAD(tmp_list
);
1627 int num_cap_releases
;
1628 __le32 barrier
, *cap_barrier
;
1630 down_read(&osdc
->lock
);
1631 barrier
= cpu_to_le32(osdc
->epoch_barrier
);
1632 up_read(&osdc
->lock
);
1634 spin_lock(&session
->s_cap_lock
);
1636 list_splice_init(&session
->s_cap_releases
, &tmp_list
);
1637 num_cap_releases
= session
->s_num_cap_releases
;
1638 session
->s_num_cap_releases
= 0;
1639 spin_unlock(&session
->s_cap_lock
);
1641 while (!list_empty(&tmp_list
)) {
1643 msg
= ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE
,
1644 PAGE_SIZE
, GFP_NOFS
, false);
1647 head
= msg
->front
.iov_base
;
1648 head
->num
= cpu_to_le32(0);
1649 msg
->front
.iov_len
= sizeof(*head
);
1651 msg
->hdr
.version
= cpu_to_le16(2);
1652 msg
->hdr
.compat_version
= cpu_to_le16(1);
1655 cap
= list_first_entry(&tmp_list
, struct ceph_cap
,
1657 list_del(&cap
->session_caps
);
1660 head
= msg
->front
.iov_base
;
1661 le32_add_cpu(&head
->num
, 1);
1662 item
= msg
->front
.iov_base
+ msg
->front
.iov_len
;
1663 item
->ino
= cpu_to_le64(cap
->cap_ino
);
1664 item
->cap_id
= cpu_to_le64(cap
->cap_id
);
1665 item
->migrate_seq
= cpu_to_le32(cap
->mseq
);
1666 item
->seq
= cpu_to_le32(cap
->issue_seq
);
1667 msg
->front
.iov_len
+= sizeof(*item
);
1669 ceph_put_cap(mdsc
, cap
);
1671 if (le32_to_cpu(head
->num
) == CEPH_CAPS_PER_RELEASE
) {
1672 // Append cap_barrier field
1673 cap_barrier
= msg
->front
.iov_base
+ msg
->front
.iov_len
;
1674 *cap_barrier
= barrier
;
1675 msg
->front
.iov_len
+= sizeof(*cap_barrier
);
1677 msg
->hdr
.front_len
= cpu_to_le32(msg
->front
.iov_len
);
1678 dout("send_cap_releases mds%d %p\n", session
->s_mds
, msg
);
1679 ceph_con_send(&session
->s_con
, msg
);
1684 BUG_ON(num_cap_releases
!= 0);
1686 spin_lock(&session
->s_cap_lock
);
1687 if (!list_empty(&session
->s_cap_releases
))
1689 spin_unlock(&session
->s_cap_lock
);
1692 // Append cap_barrier field
1693 cap_barrier
= msg
->front
.iov_base
+ msg
->front
.iov_len
;
1694 *cap_barrier
= barrier
;
1695 msg
->front
.iov_len
+= sizeof(*cap_barrier
);
1697 msg
->hdr
.front_len
= cpu_to_le32(msg
->front
.iov_len
);
1698 dout("send_cap_releases mds%d %p\n", session
->s_mds
, msg
);
1699 ceph_con_send(&session
->s_con
, msg
);
1703 pr_err("send_cap_releases mds%d, failed to allocate message\n",
1705 spin_lock(&session
->s_cap_lock
);
1706 list_splice(&tmp_list
, &session
->s_cap_releases
);
1707 session
->s_num_cap_releases
+= num_cap_releases
;
1708 spin_unlock(&session
->s_cap_lock
);
1715 int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request
*req
,
1718 struct ceph_inode_info
*ci
= ceph_inode(dir
);
1719 struct ceph_mds_reply_info_parsed
*rinfo
= &req
->r_reply_info
;
1720 struct ceph_mount_options
*opt
= req
->r_mdsc
->fsc
->mount_options
;
1721 size_t size
= sizeof(struct ceph_mds_reply_dir_entry
);
1722 int order
, num_entries
;
1724 spin_lock(&ci
->i_ceph_lock
);
1725 num_entries
= ci
->i_files
+ ci
->i_subdirs
;
1726 spin_unlock(&ci
->i_ceph_lock
);
1727 num_entries
= max(num_entries
, 1);
1728 num_entries
= min(num_entries
, opt
->max_readdir
);
1730 order
= get_order(size
* num_entries
);
1731 while (order
>= 0) {
1732 rinfo
->dir_entries
= (void*)__get_free_pages(GFP_KERNEL
|
1735 if (rinfo
->dir_entries
)
1739 if (!rinfo
->dir_entries
)
1742 num_entries
= (PAGE_SIZE
<< order
) / size
;
1743 num_entries
= min(num_entries
, opt
->max_readdir
);
1745 rinfo
->dir_buf_size
= PAGE_SIZE
<< order
;
1746 req
->r_num_caps
= num_entries
+ 1;
1747 req
->r_args
.readdir
.max_entries
= cpu_to_le32(num_entries
);
1748 req
->r_args
.readdir
.max_bytes
= cpu_to_le32(opt
->max_readdir_bytes
);
1753 * Create an mds request.
1755 struct ceph_mds_request
*
1756 ceph_mdsc_create_request(struct ceph_mds_client
*mdsc
, int op
, int mode
)
1758 struct ceph_mds_request
*req
= kzalloc(sizeof(*req
), GFP_NOFS
);
1761 return ERR_PTR(-ENOMEM
);
1763 mutex_init(&req
->r_fill_mutex
);
1765 req
->r_started
= jiffies
;
1766 req
->r_resend_mds
= -1;
1767 INIT_LIST_HEAD(&req
->r_unsafe_dir_item
);
1768 INIT_LIST_HEAD(&req
->r_unsafe_target_item
);
1770 kref_init(&req
->r_kref
);
1771 RB_CLEAR_NODE(&req
->r_node
);
1772 INIT_LIST_HEAD(&req
->r_wait
);
1773 init_completion(&req
->r_completion
);
1774 init_completion(&req
->r_safe_completion
);
1775 INIT_LIST_HEAD(&req
->r_unsafe_item
);
1777 req
->r_stamp
= timespec_trunc(current_kernel_time(), mdsc
->fsc
->sb
->s_time_gran
);
1780 req
->r_direct_mode
= mode
;
1785 * return oldest (lowest) request, tid in request tree, 0 if none.
1787 * called under mdsc->mutex.
1789 static struct ceph_mds_request
*__get_oldest_req(struct ceph_mds_client
*mdsc
)
1791 if (RB_EMPTY_ROOT(&mdsc
->request_tree
))
1793 return rb_entry(rb_first(&mdsc
->request_tree
),
1794 struct ceph_mds_request
, r_node
);
1797 static inline u64
__get_oldest_tid(struct ceph_mds_client
*mdsc
)
1799 return mdsc
->oldest_tid
;
1803 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1804 * on build_path_from_dentry in fs/cifs/dir.c.
1806 * If @stop_on_nosnap, generate path relative to the first non-snapped
1809 * Encode hidden .snap dirs as a double /, i.e.
1810 * foo/.snap/bar -> foo//bar
1812 char *ceph_mdsc_build_path(struct dentry
*dentry
, int *plen
, u64
*base
,
1815 struct dentry
*temp
;
1821 return ERR_PTR(-EINVAL
);
1825 seq
= read_seqbegin(&rename_lock
);
1827 for (temp
= dentry
; !IS_ROOT(temp
);) {
1828 struct inode
*inode
= d_inode(temp
);
1829 if (inode
&& ceph_snap(inode
) == CEPH_SNAPDIR
)
1830 len
++; /* slash only */
1831 else if (stop_on_nosnap
&& inode
&&
1832 ceph_snap(inode
) == CEPH_NOSNAP
)
1835 len
+= 1 + temp
->d_name
.len
;
1836 temp
= temp
->d_parent
;
1840 len
--; /* no leading '/' */
1842 path
= kmalloc(len
+1, GFP_NOFS
);
1844 return ERR_PTR(-ENOMEM
);
1846 path
[pos
] = 0; /* trailing null */
1848 for (temp
= dentry
; !IS_ROOT(temp
) && pos
!= 0; ) {
1849 struct inode
*inode
;
1851 spin_lock(&temp
->d_lock
);
1852 inode
= d_inode(temp
);
1853 if (inode
&& ceph_snap(inode
) == CEPH_SNAPDIR
) {
1854 dout("build_path path+%d: %p SNAPDIR\n",
1856 } else if (stop_on_nosnap
&& inode
&&
1857 ceph_snap(inode
) == CEPH_NOSNAP
) {
1858 spin_unlock(&temp
->d_lock
);
1861 pos
-= temp
->d_name
.len
;
1863 spin_unlock(&temp
->d_lock
);
1866 strncpy(path
+ pos
, temp
->d_name
.name
,
1869 spin_unlock(&temp
->d_lock
);
1872 temp
= temp
->d_parent
;
1875 if (pos
!= 0 || read_seqretry(&rename_lock
, seq
)) {
1876 pr_err("build_path did not end path lookup where "
1877 "expected, namelen is %d, pos is %d\n", len
, pos
);
1878 /* presumably this is only possible if racing with a
1879 rename of one of the parent directories (we can not
1880 lock the dentries above us to prevent this, but
1881 retrying should be harmless) */
1886 *base
= ceph_ino(d_inode(temp
));
1888 dout("build_path on %p %d built %llx '%.*s'\n",
1889 dentry
, d_count(dentry
), *base
, len
, path
);
1893 static int build_dentry_path(struct dentry
*dentry
, struct inode
*dir
,
1894 const char **ppath
, int *ppathlen
, u64
*pino
,
1901 dir
= d_inode_rcu(dentry
->d_parent
);
1902 if (dir
&& ceph_snap(dir
) == CEPH_NOSNAP
) {
1903 *pino
= ceph_ino(dir
);
1905 *ppath
= dentry
->d_name
.name
;
1906 *ppathlen
= dentry
->d_name
.len
;
1910 path
= ceph_mdsc_build_path(dentry
, ppathlen
, pino
, 1);
1912 return PTR_ERR(path
);
1918 static int build_inode_path(struct inode
*inode
,
1919 const char **ppath
, int *ppathlen
, u64
*pino
,
1922 struct dentry
*dentry
;
1925 if (ceph_snap(inode
) == CEPH_NOSNAP
) {
1926 *pino
= ceph_ino(inode
);
1930 dentry
= d_find_alias(inode
);
1931 path
= ceph_mdsc_build_path(dentry
, ppathlen
, pino
, 1);
1934 return PTR_ERR(path
);
1941 * request arguments may be specified via an inode *, a dentry *, or
1942 * an explicit ino+path.
1944 static int set_request_path_attr(struct inode
*rinode
, struct dentry
*rdentry
,
1945 struct inode
*rdiri
, const char *rpath
,
1946 u64 rino
, const char **ppath
, int *pathlen
,
1947 u64
*ino
, int *freepath
)
1952 r
= build_inode_path(rinode
, ppath
, pathlen
, ino
, freepath
);
1953 dout(" inode %p %llx.%llx\n", rinode
, ceph_ino(rinode
),
1955 } else if (rdentry
) {
1956 r
= build_dentry_path(rdentry
, rdiri
, ppath
, pathlen
, ino
,
1958 dout(" dentry %p %llx/%.*s\n", rdentry
, *ino
, *pathlen
,
1960 } else if (rpath
|| rino
) {
1963 *pathlen
= rpath
? strlen(rpath
) : 0;
1964 dout(" path %.*s\n", *pathlen
, rpath
);
1971 * called under mdsc->mutex
1973 static struct ceph_msg
*create_request_message(struct ceph_mds_client
*mdsc
,
1974 struct ceph_mds_request
*req
,
1975 int mds
, bool drop_cap_releases
)
1977 struct ceph_msg
*msg
;
1978 struct ceph_mds_request_head
*head
;
1979 const char *path1
= NULL
;
1980 const char *path2
= NULL
;
1981 u64 ino1
= 0, ino2
= 0;
1982 int pathlen1
= 0, pathlen2
= 0;
1983 int freepath1
= 0, freepath2
= 0;
1989 ret
= set_request_path_attr(req
->r_inode
, req
->r_dentry
,
1990 req
->r_parent
, req
->r_path1
, req
->r_ino1
.ino
,
1991 &path1
, &pathlen1
, &ino1
, &freepath1
);
1997 ret
= set_request_path_attr(NULL
, req
->r_old_dentry
,
1998 req
->r_old_dentry_dir
,
1999 req
->r_path2
, req
->r_ino2
.ino
,
2000 &path2
, &pathlen2
, &ino2
, &freepath2
);
2006 len
= sizeof(*head
) +
2007 pathlen1
+ pathlen2
+ 2*(1 + sizeof(u32
) + sizeof(u64
)) +
2008 sizeof(struct ceph_timespec
);
2010 /* calculate (max) length for cap releases */
2011 len
+= sizeof(struct ceph_mds_request_release
) *
2012 (!!req
->r_inode_drop
+ !!req
->r_dentry_drop
+
2013 !!req
->r_old_inode_drop
+ !!req
->r_old_dentry_drop
);
2014 if (req
->r_dentry_drop
)
2015 len
+= req
->r_dentry
->d_name
.len
;
2016 if (req
->r_old_dentry_drop
)
2017 len
+= req
->r_old_dentry
->d_name
.len
;
2019 msg
= ceph_msg_new(CEPH_MSG_CLIENT_REQUEST
, len
, GFP_NOFS
, false);
2021 msg
= ERR_PTR(-ENOMEM
);
2025 msg
->hdr
.version
= cpu_to_le16(2);
2026 msg
->hdr
.tid
= cpu_to_le64(req
->r_tid
);
2028 head
= msg
->front
.iov_base
;
2029 p
= msg
->front
.iov_base
+ sizeof(*head
);
2030 end
= msg
->front
.iov_base
+ msg
->front
.iov_len
;
2032 head
->mdsmap_epoch
= cpu_to_le32(mdsc
->mdsmap
->m_epoch
);
2033 head
->op
= cpu_to_le32(req
->r_op
);
2034 head
->caller_uid
= cpu_to_le32(from_kuid(&init_user_ns
, req
->r_uid
));
2035 head
->caller_gid
= cpu_to_le32(from_kgid(&init_user_ns
, req
->r_gid
));
2036 head
->args
= req
->r_args
;
2038 ceph_encode_filepath(&p
, end
, ino1
, path1
);
2039 ceph_encode_filepath(&p
, end
, ino2
, path2
);
2041 /* make note of release offset, in case we need to replay */
2042 req
->r_request_release_offset
= p
- msg
->front
.iov_base
;
2046 if (req
->r_inode_drop
)
2047 releases
+= ceph_encode_inode_release(&p
,
2048 req
->r_inode
? req
->r_inode
: d_inode(req
->r_dentry
),
2049 mds
, req
->r_inode_drop
, req
->r_inode_unless
, 0);
2050 if (req
->r_dentry_drop
)
2051 releases
+= ceph_encode_dentry_release(&p
, req
->r_dentry
,
2052 req
->r_parent
, mds
, req
->r_dentry_drop
,
2053 req
->r_dentry_unless
);
2054 if (req
->r_old_dentry_drop
)
2055 releases
+= ceph_encode_dentry_release(&p
, req
->r_old_dentry
,
2056 req
->r_old_dentry_dir
, mds
,
2057 req
->r_old_dentry_drop
,
2058 req
->r_old_dentry_unless
);
2059 if (req
->r_old_inode_drop
)
2060 releases
+= ceph_encode_inode_release(&p
,
2061 d_inode(req
->r_old_dentry
),
2062 mds
, req
->r_old_inode_drop
, req
->r_old_inode_unless
, 0);
2064 if (drop_cap_releases
) {
2066 p
= msg
->front
.iov_base
+ req
->r_request_release_offset
;
2069 head
->num_releases
= cpu_to_le16(releases
);
2073 struct ceph_timespec ts
;
2074 ceph_encode_timespec(&ts
, &req
->r_stamp
);
2075 ceph_encode_copy(&p
, &ts
, sizeof(ts
));
2079 msg
->front
.iov_len
= p
- msg
->front
.iov_base
;
2080 msg
->hdr
.front_len
= cpu_to_le32(msg
->front
.iov_len
);
2082 if (req
->r_pagelist
) {
2083 struct ceph_pagelist
*pagelist
= req
->r_pagelist
;
2084 refcount_inc(&pagelist
->refcnt
);
2085 ceph_msg_data_add_pagelist(msg
, pagelist
);
2086 msg
->hdr
.data_len
= cpu_to_le32(pagelist
->length
);
2088 msg
->hdr
.data_len
= 0;
2091 msg
->hdr
.data_off
= cpu_to_le16(0);
2095 kfree((char *)path2
);
2098 kfree((char *)path1
);
2104 * called under mdsc->mutex if error, under no mutex if
2107 static void complete_request(struct ceph_mds_client
*mdsc
,
2108 struct ceph_mds_request
*req
)
2110 if (req
->r_callback
)
2111 req
->r_callback(mdsc
, req
);
2113 complete_all(&req
->r_completion
);
2117 * called under mdsc->mutex
2119 static int __prepare_send_request(struct ceph_mds_client
*mdsc
,
2120 struct ceph_mds_request
*req
,
2121 int mds
, bool drop_cap_releases
)
2123 struct ceph_mds_request_head
*rhead
;
2124 struct ceph_msg
*msg
;
2129 struct ceph_cap
*cap
=
2130 ceph_get_cap_for_mds(ceph_inode(req
->r_inode
), mds
);
2133 req
->r_sent_on_mseq
= cap
->mseq
;
2135 req
->r_sent_on_mseq
= -1;
2137 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req
,
2138 req
->r_tid
, ceph_mds_op_name(req
->r_op
), req
->r_attempts
);
2140 if (test_bit(CEPH_MDS_R_GOT_UNSAFE
, &req
->r_req_flags
)) {
2143 * Replay. Do not regenerate message (and rebuild
2144 * paths, etc.); just use the original message.
2145 * Rebuilding paths will break for renames because
2146 * d_move mangles the src name.
2148 msg
= req
->r_request
;
2149 rhead
= msg
->front
.iov_base
;
2151 flags
= le32_to_cpu(rhead
->flags
);
2152 flags
|= CEPH_MDS_FLAG_REPLAY
;
2153 rhead
->flags
= cpu_to_le32(flags
);
2155 if (req
->r_target_inode
)
2156 rhead
->ino
= cpu_to_le64(ceph_ino(req
->r_target_inode
));
2158 rhead
->num_retry
= req
->r_attempts
- 1;
2160 /* remove cap/dentry releases from message */
2161 rhead
->num_releases
= 0;
2164 p
= msg
->front
.iov_base
+ req
->r_request_release_offset
;
2166 struct ceph_timespec ts
;
2167 ceph_encode_timespec(&ts
, &req
->r_stamp
);
2168 ceph_encode_copy(&p
, &ts
, sizeof(ts
));
2171 msg
->front
.iov_len
= p
- msg
->front
.iov_base
;
2172 msg
->hdr
.front_len
= cpu_to_le32(msg
->front
.iov_len
);
2176 if (req
->r_request
) {
2177 ceph_msg_put(req
->r_request
);
2178 req
->r_request
= NULL
;
2180 msg
= create_request_message(mdsc
, req
, mds
, drop_cap_releases
);
2182 req
->r_err
= PTR_ERR(msg
);
2183 return PTR_ERR(msg
);
2185 req
->r_request
= msg
;
2187 rhead
= msg
->front
.iov_base
;
2188 rhead
->oldest_client_tid
= cpu_to_le64(__get_oldest_tid(mdsc
));
2189 if (test_bit(CEPH_MDS_R_GOT_UNSAFE
, &req
->r_req_flags
))
2190 flags
|= CEPH_MDS_FLAG_REPLAY
;
2192 flags
|= CEPH_MDS_FLAG_WANT_DENTRY
;
2193 rhead
->flags
= cpu_to_le32(flags
);
2194 rhead
->num_fwd
= req
->r_num_fwd
;
2195 rhead
->num_retry
= req
->r_attempts
- 1;
2198 dout(" r_parent = %p\n", req
->r_parent
);
2203 * send request, or put it on the appropriate wait list.
2205 static int __do_request(struct ceph_mds_client
*mdsc
,
2206 struct ceph_mds_request
*req
)
2208 struct ceph_mds_session
*session
= NULL
;
2212 if (req
->r_err
|| test_bit(CEPH_MDS_R_GOT_RESULT
, &req
->r_req_flags
)) {
2213 if (test_bit(CEPH_MDS_R_ABORTED
, &req
->r_req_flags
))
2214 __unregister_request(mdsc
, req
);
2218 if (req
->r_timeout
&&
2219 time_after_eq(jiffies
, req
->r_started
+ req
->r_timeout
)) {
2220 dout("do_request timed out\n");
2224 if (READ_ONCE(mdsc
->fsc
->mount_state
) == CEPH_MOUNT_SHUTDOWN
) {
2225 dout("do_request forced umount\n");
2229 if (READ_ONCE(mdsc
->fsc
->mount_state
) == CEPH_MOUNT_MOUNTING
) {
2230 if (mdsc
->mdsmap_err
) {
2231 err
= mdsc
->mdsmap_err
;
2232 dout("do_request mdsmap err %d\n", err
);
2235 if (mdsc
->mdsmap
->m_epoch
== 0) {
2236 dout("do_request no mdsmap, waiting for map\n");
2237 list_add(&req
->r_wait
, &mdsc
->waiting_for_map
);
2240 if (!(mdsc
->fsc
->mount_options
->flags
&
2241 CEPH_MOUNT_OPT_MOUNTWAIT
) &&
2242 !ceph_mdsmap_is_cluster_available(mdsc
->mdsmap
)) {
2244 pr_info("probably no mds server is up\n");
2249 put_request_session(req
);
2251 mds
= __choose_mds(mdsc
, req
);
2253 ceph_mdsmap_get_state(mdsc
->mdsmap
, mds
) < CEPH_MDS_STATE_ACTIVE
) {
2254 dout("do_request no mds or not active, waiting for map\n");
2255 list_add(&req
->r_wait
, &mdsc
->waiting_for_map
);
2259 /* get, open session */
2260 session
= __ceph_lookup_mds_session(mdsc
, mds
);
2262 session
= register_session(mdsc
, mds
);
2263 if (IS_ERR(session
)) {
2264 err
= PTR_ERR(session
);
2268 req
->r_session
= get_session(session
);
2270 dout("do_request mds%d session %p state %s\n", mds
, session
,
2271 ceph_session_state_name(session
->s_state
));
2272 if (session
->s_state
!= CEPH_MDS_SESSION_OPEN
&&
2273 session
->s_state
!= CEPH_MDS_SESSION_HUNG
) {
2274 if (session
->s_state
== CEPH_MDS_SESSION_REJECTED
) {
2278 if (session
->s_state
== CEPH_MDS_SESSION_NEW
||
2279 session
->s_state
== CEPH_MDS_SESSION_CLOSING
)
2280 __open_session(mdsc
, session
);
2281 list_add(&req
->r_wait
, &session
->s_waiting
);
2286 req
->r_resend_mds
= -1; /* forget any previous mds hint */
2288 if (req
->r_request_started
== 0) /* note request start time */
2289 req
->r_request_started
= jiffies
;
2291 err
= __prepare_send_request(mdsc
, req
, mds
, false);
2293 ceph_msg_get(req
->r_request
);
2294 ceph_con_send(&session
->s_con
, req
->r_request
);
2298 ceph_put_mds_session(session
);
2301 dout("__do_request early error %d\n", err
);
2303 complete_request(mdsc
, req
);
2304 __unregister_request(mdsc
, req
);
2311 * called under mdsc->mutex
2313 static void __wake_requests(struct ceph_mds_client
*mdsc
,
2314 struct list_head
*head
)
2316 struct ceph_mds_request
*req
;
2317 LIST_HEAD(tmp_list
);
2319 list_splice_init(head
, &tmp_list
);
2321 while (!list_empty(&tmp_list
)) {
2322 req
= list_entry(tmp_list
.next
,
2323 struct ceph_mds_request
, r_wait
);
2324 list_del_init(&req
->r_wait
);
2325 dout(" wake request %p tid %llu\n", req
, req
->r_tid
);
2326 __do_request(mdsc
, req
);
2331 * Wake up threads with requests pending for @mds, so that they can
2332 * resubmit their requests to a possibly different mds.
2334 static void kick_requests(struct ceph_mds_client
*mdsc
, int mds
)
2336 struct ceph_mds_request
*req
;
2337 struct rb_node
*p
= rb_first(&mdsc
->request_tree
);
2339 dout("kick_requests mds%d\n", mds
);
2341 req
= rb_entry(p
, struct ceph_mds_request
, r_node
);
2343 if (test_bit(CEPH_MDS_R_GOT_UNSAFE
, &req
->r_req_flags
))
2345 if (req
->r_attempts
> 0)
2346 continue; /* only new requests */
2347 if (req
->r_session
&&
2348 req
->r_session
->s_mds
== mds
) {
2349 dout(" kicking tid %llu\n", req
->r_tid
);
2350 list_del_init(&req
->r_wait
);
2351 __do_request(mdsc
, req
);
2356 void ceph_mdsc_submit_request(struct ceph_mds_client
*mdsc
,
2357 struct ceph_mds_request
*req
)
2359 dout("submit_request on %p\n", req
);
2360 mutex_lock(&mdsc
->mutex
);
2361 __register_request(mdsc
, req
, NULL
);
2362 __do_request(mdsc
, req
);
2363 mutex_unlock(&mdsc
->mutex
);
2367 * Synchrously perform an mds request. Take care of all of the
2368 * session setup, forwarding, retry details.
2370 int ceph_mdsc_do_request(struct ceph_mds_client
*mdsc
,
2372 struct ceph_mds_request
*req
)
2376 dout("do_request on %p\n", req
);
2378 /* take CAP_PIN refs for r_inode, r_parent, r_old_dentry */
2380 ceph_get_cap_refs(ceph_inode(req
->r_inode
), CEPH_CAP_PIN
);
2382 ceph_get_cap_refs(ceph_inode(req
->r_parent
), CEPH_CAP_PIN
);
2383 if (req
->r_old_dentry_dir
)
2384 ceph_get_cap_refs(ceph_inode(req
->r_old_dentry_dir
),
2388 mutex_lock(&mdsc
->mutex
);
2389 __register_request(mdsc
, req
, dir
);
2390 __do_request(mdsc
, req
);
2398 mutex_unlock(&mdsc
->mutex
);
2399 dout("do_request waiting\n");
2400 if (!req
->r_timeout
&& req
->r_wait_for_completion
) {
2401 err
= req
->r_wait_for_completion(mdsc
, req
);
2403 long timeleft
= wait_for_completion_killable_timeout(
2405 ceph_timeout_jiffies(req
->r_timeout
));
2409 err
= -EIO
; /* timed out */
2411 err
= timeleft
; /* killed */
2413 dout("do_request waited, got %d\n", err
);
2414 mutex_lock(&mdsc
->mutex
);
2416 /* only abort if we didn't race with a real reply */
2417 if (test_bit(CEPH_MDS_R_GOT_RESULT
, &req
->r_req_flags
)) {
2418 err
= le32_to_cpu(req
->r_reply_info
.head
->result
);
2419 } else if (err
< 0) {
2420 dout("aborted request %lld with %d\n", req
->r_tid
, err
);
2423 * ensure we aren't running concurrently with
2424 * ceph_fill_trace or ceph_readdir_prepopulate, which
2425 * rely on locks (dir mutex) held by our caller.
2427 mutex_lock(&req
->r_fill_mutex
);
2429 set_bit(CEPH_MDS_R_ABORTED
, &req
->r_req_flags
);
2430 mutex_unlock(&req
->r_fill_mutex
);
2432 if (req
->r_parent
&&
2433 (req
->r_op
& CEPH_MDS_OP_WRITE
))
2434 ceph_invalidate_dir_request(req
);
2440 mutex_unlock(&mdsc
->mutex
);
2441 dout("do_request %p done, result %d\n", req
, err
);
2446 * Invalidate dir's completeness, dentry lease state on an aborted MDS
2447 * namespace request.
2449 void ceph_invalidate_dir_request(struct ceph_mds_request
*req
)
2451 struct inode
*dir
= req
->r_parent
;
2452 struct inode
*old_dir
= req
->r_old_dentry_dir
;
2454 dout("invalidate_dir_request %p %p (complete, lease(s))\n", dir
, old_dir
);
2456 ceph_dir_clear_complete(dir
);
2458 ceph_dir_clear_complete(old_dir
);
2460 ceph_invalidate_dentry_lease(req
->r_dentry
);
2461 if (req
->r_old_dentry
)
2462 ceph_invalidate_dentry_lease(req
->r_old_dentry
);
2468 * We take the session mutex and parse and process the reply immediately.
2469 * This preserves the logical ordering of replies, capabilities, etc., sent
2470 * by the MDS as they are applied to our local cache.
2472 static void handle_reply(struct ceph_mds_session
*session
, struct ceph_msg
*msg
)
2474 struct ceph_mds_client
*mdsc
= session
->s_mdsc
;
2475 struct ceph_mds_request
*req
;
2476 struct ceph_mds_reply_head
*head
= msg
->front
.iov_base
;
2477 struct ceph_mds_reply_info_parsed
*rinfo
; /* parsed reply info */
2478 struct ceph_snap_realm
*realm
;
2481 int mds
= session
->s_mds
;
2483 if (msg
->front
.iov_len
< sizeof(*head
)) {
2484 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
2489 /* get request, session */
2490 tid
= le64_to_cpu(msg
->hdr
.tid
);
2491 mutex_lock(&mdsc
->mutex
);
2492 req
= lookup_get_request(mdsc
, tid
);
2494 dout("handle_reply on unknown tid %llu\n", tid
);
2495 mutex_unlock(&mdsc
->mutex
);
2498 dout("handle_reply %p\n", req
);
2500 /* correct session? */
2501 if (req
->r_session
!= session
) {
2502 pr_err("mdsc_handle_reply got %llu on session mds%d"
2503 " not mds%d\n", tid
, session
->s_mds
,
2504 req
->r_session
? req
->r_session
->s_mds
: -1);
2505 mutex_unlock(&mdsc
->mutex
);
2510 if ((test_bit(CEPH_MDS_R_GOT_UNSAFE
, &req
->r_req_flags
) && !head
->safe
) ||
2511 (test_bit(CEPH_MDS_R_GOT_SAFE
, &req
->r_req_flags
) && head
->safe
)) {
2512 pr_warn("got a dup %s reply on %llu from mds%d\n",
2513 head
->safe
? "safe" : "unsafe", tid
, mds
);
2514 mutex_unlock(&mdsc
->mutex
);
2517 if (test_bit(CEPH_MDS_R_GOT_SAFE
, &req
->r_req_flags
)) {
2518 pr_warn("got unsafe after safe on %llu from mds%d\n",
2520 mutex_unlock(&mdsc
->mutex
);
2524 result
= le32_to_cpu(head
->result
);
2528 * if we're not talking to the authority, send to them
2529 * if the authority has changed while we weren't looking,
2530 * send to new authority
2531 * Otherwise we just have to return an ESTALE
2533 if (result
== -ESTALE
) {
2534 dout("got ESTALE on request %llu", req
->r_tid
);
2535 req
->r_resend_mds
= -1;
2536 if (req
->r_direct_mode
!= USE_AUTH_MDS
) {
2537 dout("not using auth, setting for that now");
2538 req
->r_direct_mode
= USE_AUTH_MDS
;
2539 __do_request(mdsc
, req
);
2540 mutex_unlock(&mdsc
->mutex
);
2543 int mds
= __choose_mds(mdsc
, req
);
2544 if (mds
>= 0 && mds
!= req
->r_session
->s_mds
) {
2545 dout("but auth changed, so resending");
2546 __do_request(mdsc
, req
);
2547 mutex_unlock(&mdsc
->mutex
);
2551 dout("have to return ESTALE on request %llu", req
->r_tid
);
2556 set_bit(CEPH_MDS_R_GOT_SAFE
, &req
->r_req_flags
);
2557 __unregister_request(mdsc
, req
);
2559 if (test_bit(CEPH_MDS_R_GOT_UNSAFE
, &req
->r_req_flags
)) {
2561 * We already handled the unsafe response, now do the
2562 * cleanup. No need to examine the response; the MDS
2563 * doesn't include any result info in the safe
2564 * response. And even if it did, there is nothing
2565 * useful we could do with a revised return value.
2567 dout("got safe reply %llu, mds%d\n", tid
, mds
);
2569 /* last unsafe request during umount? */
2570 if (mdsc
->stopping
&& !__get_oldest_req(mdsc
))
2571 complete_all(&mdsc
->safe_umount_waiters
);
2572 mutex_unlock(&mdsc
->mutex
);
2576 set_bit(CEPH_MDS_R_GOT_UNSAFE
, &req
->r_req_flags
);
2577 list_add_tail(&req
->r_unsafe_item
, &req
->r_session
->s_unsafe
);
2578 if (req
->r_unsafe_dir
) {
2579 struct ceph_inode_info
*ci
=
2580 ceph_inode(req
->r_unsafe_dir
);
2581 spin_lock(&ci
->i_unsafe_lock
);
2582 list_add_tail(&req
->r_unsafe_dir_item
,
2583 &ci
->i_unsafe_dirops
);
2584 spin_unlock(&ci
->i_unsafe_lock
);
2588 dout("handle_reply tid %lld result %d\n", tid
, result
);
2589 rinfo
= &req
->r_reply_info
;
2590 err
= parse_reply_info(msg
, rinfo
, session
->s_con
.peer_features
);
2591 mutex_unlock(&mdsc
->mutex
);
2593 mutex_lock(&session
->s_mutex
);
2595 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds
, tid
);
2602 if (rinfo
->snapblob_len
) {
2603 down_write(&mdsc
->snap_rwsem
);
2604 ceph_update_snap_trace(mdsc
, rinfo
->snapblob
,
2605 rinfo
->snapblob
+ rinfo
->snapblob_len
,
2606 le32_to_cpu(head
->op
) == CEPH_MDS_OP_RMSNAP
,
2608 downgrade_write(&mdsc
->snap_rwsem
);
2610 down_read(&mdsc
->snap_rwsem
);
2613 /* insert trace into our cache */
2614 mutex_lock(&req
->r_fill_mutex
);
2615 current
->journal_info
= req
;
2616 err
= ceph_fill_trace(mdsc
->fsc
->sb
, req
);
2618 if (result
== 0 && (req
->r_op
== CEPH_MDS_OP_READDIR
||
2619 req
->r_op
== CEPH_MDS_OP_LSSNAP
))
2620 ceph_readdir_prepopulate(req
, req
->r_session
);
2621 ceph_unreserve_caps(mdsc
, &req
->r_caps_reservation
);
2623 current
->journal_info
= NULL
;
2624 mutex_unlock(&req
->r_fill_mutex
);
2626 up_read(&mdsc
->snap_rwsem
);
2628 ceph_put_snap_realm(mdsc
, realm
);
2630 if (err
== 0 && req
->r_target_inode
&&
2631 test_bit(CEPH_MDS_R_GOT_UNSAFE
, &req
->r_req_flags
)) {
2632 struct ceph_inode_info
*ci
= ceph_inode(req
->r_target_inode
);
2633 spin_lock(&ci
->i_unsafe_lock
);
2634 list_add_tail(&req
->r_unsafe_target_item
, &ci
->i_unsafe_iops
);
2635 spin_unlock(&ci
->i_unsafe_lock
);
2638 mutex_lock(&mdsc
->mutex
);
2639 if (!test_bit(CEPH_MDS_R_ABORTED
, &req
->r_req_flags
)) {
2643 req
->r_reply
= ceph_msg_get(msg
);
2644 set_bit(CEPH_MDS_R_GOT_RESULT
, &req
->r_req_flags
);
2647 dout("reply arrived after request %lld was aborted\n", tid
);
2649 mutex_unlock(&mdsc
->mutex
);
2651 mutex_unlock(&session
->s_mutex
);
2653 /* kick calling process */
2654 complete_request(mdsc
, req
);
2656 ceph_mdsc_put_request(req
);
2663 * handle mds notification that our request has been forwarded.
2665 static void handle_forward(struct ceph_mds_client
*mdsc
,
2666 struct ceph_mds_session
*session
,
2667 struct ceph_msg
*msg
)
2669 struct ceph_mds_request
*req
;
2670 u64 tid
= le64_to_cpu(msg
->hdr
.tid
);
2674 void *p
= msg
->front
.iov_base
;
2675 void *end
= p
+ msg
->front
.iov_len
;
2677 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
2678 next_mds
= ceph_decode_32(&p
);
2679 fwd_seq
= ceph_decode_32(&p
);
2681 mutex_lock(&mdsc
->mutex
);
2682 req
= lookup_get_request(mdsc
, tid
);
2684 dout("forward tid %llu to mds%d - req dne\n", tid
, next_mds
);
2685 goto out
; /* dup reply? */
2688 if (test_bit(CEPH_MDS_R_ABORTED
, &req
->r_req_flags
)) {
2689 dout("forward tid %llu aborted, unregistering\n", tid
);
2690 __unregister_request(mdsc
, req
);
2691 } else if (fwd_seq
<= req
->r_num_fwd
) {
2692 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
2693 tid
, next_mds
, req
->r_num_fwd
, fwd_seq
);
2695 /* resend. forward race not possible; mds would drop */
2696 dout("forward tid %llu to mds%d (we resend)\n", tid
, next_mds
);
2698 BUG_ON(test_bit(CEPH_MDS_R_GOT_RESULT
, &req
->r_req_flags
));
2699 req
->r_attempts
= 0;
2700 req
->r_num_fwd
= fwd_seq
;
2701 req
->r_resend_mds
= next_mds
;
2702 put_request_session(req
);
2703 __do_request(mdsc
, req
);
2705 ceph_mdsc_put_request(req
);
2707 mutex_unlock(&mdsc
->mutex
);
2711 pr_err("mdsc_handle_forward decode error err=%d\n", err
);
2715 * handle a mds session control message
2717 static void handle_session(struct ceph_mds_session
*session
,
2718 struct ceph_msg
*msg
)
2720 struct ceph_mds_client
*mdsc
= session
->s_mdsc
;
2723 int mds
= session
->s_mds
;
2724 struct ceph_mds_session_head
*h
= msg
->front
.iov_base
;
2728 if (msg
->front
.iov_len
!= sizeof(*h
))
2730 op
= le32_to_cpu(h
->op
);
2731 seq
= le64_to_cpu(h
->seq
);
2733 mutex_lock(&mdsc
->mutex
);
2734 if (op
== CEPH_SESSION_CLOSE
) {
2735 get_session(session
);
2736 __unregister_session(mdsc
, session
);
2738 /* FIXME: this ttl calculation is generous */
2739 session
->s_ttl
= jiffies
+ HZ
*mdsc
->mdsmap
->m_session_autoclose
;
2740 mutex_unlock(&mdsc
->mutex
);
2742 mutex_lock(&session
->s_mutex
);
2744 dout("handle_session mds%d %s %p state %s seq %llu\n",
2745 mds
, ceph_session_op_name(op
), session
,
2746 ceph_session_state_name(session
->s_state
), seq
);
2748 if (session
->s_state
== CEPH_MDS_SESSION_HUNG
) {
2749 session
->s_state
= CEPH_MDS_SESSION_OPEN
;
2750 pr_info("mds%d came back\n", session
->s_mds
);
2754 case CEPH_SESSION_OPEN
:
2755 if (session
->s_state
== CEPH_MDS_SESSION_RECONNECTING
)
2756 pr_info("mds%d reconnect success\n", session
->s_mds
);
2757 session
->s_state
= CEPH_MDS_SESSION_OPEN
;
2758 renewed_caps(mdsc
, session
, 0);
2761 __close_session(mdsc
, session
);
2764 case CEPH_SESSION_RENEWCAPS
:
2765 if (session
->s_renew_seq
== seq
)
2766 renewed_caps(mdsc
, session
, 1);
2769 case CEPH_SESSION_CLOSE
:
2770 if (session
->s_state
== CEPH_MDS_SESSION_RECONNECTING
)
2771 pr_info("mds%d reconnect denied\n", session
->s_mds
);
2772 cleanup_session_requests(mdsc
, session
);
2773 remove_session_caps(session
);
2774 wake
= 2; /* for good measure */
2775 wake_up_all(&mdsc
->session_close_wq
);
2778 case CEPH_SESSION_STALE
:
2779 pr_info("mds%d caps went stale, renewing\n",
2781 spin_lock(&session
->s_gen_ttl_lock
);
2782 session
->s_cap_gen
++;
2783 session
->s_cap_ttl
= jiffies
- 1;
2784 spin_unlock(&session
->s_gen_ttl_lock
);
2785 send_renew_caps(mdsc
, session
);
2788 case CEPH_SESSION_RECALL_STATE
:
2789 ceph_trim_caps(mdsc
, session
, le32_to_cpu(h
->max_caps
));
2792 case CEPH_SESSION_FLUSHMSG
:
2793 send_flushmsg_ack(mdsc
, session
, seq
);
2796 case CEPH_SESSION_FORCE_RO
:
2797 dout("force_session_readonly %p\n", session
);
2798 spin_lock(&session
->s_cap_lock
);
2799 session
->s_readonly
= true;
2800 spin_unlock(&session
->s_cap_lock
);
2801 wake_up_session_caps(session
, 0);
2804 case CEPH_SESSION_REJECT
:
2805 WARN_ON(session
->s_state
!= CEPH_MDS_SESSION_OPENING
);
2806 pr_info("mds%d rejected session\n", session
->s_mds
);
2807 session
->s_state
= CEPH_MDS_SESSION_REJECTED
;
2808 cleanup_session_requests(mdsc
, session
);
2809 remove_session_caps(session
);
2810 wake
= 2; /* for good measure */
2814 pr_err("mdsc_handle_session bad op %d mds%d\n", op
, mds
);
2818 mutex_unlock(&session
->s_mutex
);
2820 mutex_lock(&mdsc
->mutex
);
2821 __wake_requests(mdsc
, &session
->s_waiting
);
2823 kick_requests(mdsc
, mds
);
2824 mutex_unlock(&mdsc
->mutex
);
2826 if (op
== CEPH_SESSION_CLOSE
)
2827 ceph_put_mds_session(session
);
2831 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds
,
2832 (int)msg
->front
.iov_len
);
2839 * called under session->mutex.
2841 static void replay_unsafe_requests(struct ceph_mds_client
*mdsc
,
2842 struct ceph_mds_session
*session
)
2844 struct ceph_mds_request
*req
, *nreq
;
2848 dout("replay_unsafe_requests mds%d\n", session
->s_mds
);
2850 mutex_lock(&mdsc
->mutex
);
2851 list_for_each_entry_safe(req
, nreq
, &session
->s_unsafe
, r_unsafe_item
) {
2852 err
= __prepare_send_request(mdsc
, req
, session
->s_mds
, true);
2854 ceph_msg_get(req
->r_request
);
2855 ceph_con_send(&session
->s_con
, req
->r_request
);
2860 * also re-send old requests when MDS enters reconnect stage. So that MDS
2861 * can process completed request in clientreplay stage.
2863 p
= rb_first(&mdsc
->request_tree
);
2865 req
= rb_entry(p
, struct ceph_mds_request
, r_node
);
2867 if (test_bit(CEPH_MDS_R_GOT_UNSAFE
, &req
->r_req_flags
))
2869 if (req
->r_attempts
== 0)
2870 continue; /* only old requests */
2871 if (req
->r_session
&&
2872 req
->r_session
->s_mds
== session
->s_mds
) {
2873 err
= __prepare_send_request(mdsc
, req
,
2874 session
->s_mds
, true);
2876 ceph_msg_get(req
->r_request
);
2877 ceph_con_send(&session
->s_con
, req
->r_request
);
2881 mutex_unlock(&mdsc
->mutex
);
2885 * Encode information about a cap for a reconnect with the MDS.
2887 static int encode_caps_cb(struct inode
*inode
, struct ceph_cap
*cap
,
2891 struct ceph_mds_cap_reconnect v2
;
2892 struct ceph_mds_cap_reconnect_v1 v1
;
2894 struct ceph_inode_info
*ci
= cap
->ci
;
2895 struct ceph_reconnect_state
*recon_state
= arg
;
2896 struct ceph_pagelist
*pagelist
= recon_state
->pagelist
;
2901 struct dentry
*dentry
;
2903 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2904 inode
, ceph_vinop(inode
), cap
, cap
->cap_id
,
2905 ceph_cap_string(cap
->issued
));
2906 err
= ceph_pagelist_encode_64(pagelist
, ceph_ino(inode
));
2910 dentry
= d_find_alias(inode
);
2912 path
= ceph_mdsc_build_path(dentry
, &pathlen
, &pathbase
, 0);
2914 err
= PTR_ERR(path
);
2923 spin_lock(&ci
->i_ceph_lock
);
2924 cap
->seq
= 0; /* reset cap seq */
2925 cap
->issue_seq
= 0; /* and issue_seq */
2926 cap
->mseq
= 0; /* and migrate_seq */
2927 cap
->cap_gen
= cap
->session
->s_cap_gen
;
2929 if (recon_state
->msg_version
>= 2) {
2930 rec
.v2
.cap_id
= cpu_to_le64(cap
->cap_id
);
2931 rec
.v2
.wanted
= cpu_to_le32(__ceph_caps_wanted(ci
));
2932 rec
.v2
.issued
= cpu_to_le32(cap
->issued
);
2933 rec
.v2
.snaprealm
= cpu_to_le64(ci
->i_snap_realm
->ino
);
2934 rec
.v2
.pathbase
= cpu_to_le64(pathbase
);
2935 rec
.v2
.flock_len
= (__force __le32
)
2936 ((ci
->i_ceph_flags
& CEPH_I_ERROR_FILELOCK
) ? 0 : 1);
2938 rec
.v1
.cap_id
= cpu_to_le64(cap
->cap_id
);
2939 rec
.v1
.wanted
= cpu_to_le32(__ceph_caps_wanted(ci
));
2940 rec
.v1
.issued
= cpu_to_le32(cap
->issued
);
2941 rec
.v1
.size
= cpu_to_le64(inode
->i_size
);
2942 ceph_encode_timespec(&rec
.v1
.mtime
, &inode
->i_mtime
);
2943 ceph_encode_timespec(&rec
.v1
.atime
, &inode
->i_atime
);
2944 rec
.v1
.snaprealm
= cpu_to_le64(ci
->i_snap_realm
->ino
);
2945 rec
.v1
.pathbase
= cpu_to_le64(pathbase
);
2948 if (list_empty(&ci
->i_cap_snaps
)) {
2949 snap_follows
= ci
->i_head_snapc
? ci
->i_head_snapc
->seq
: 0;
2951 struct ceph_cap_snap
*capsnap
=
2952 list_first_entry(&ci
->i_cap_snaps
,
2953 struct ceph_cap_snap
, ci_item
);
2954 snap_follows
= capsnap
->follows
;
2956 spin_unlock(&ci
->i_ceph_lock
);
2958 if (recon_state
->msg_version
>= 2) {
2959 int num_fcntl_locks
, num_flock_locks
;
2960 struct ceph_filelock
*flocks
= NULL
;
2961 size_t struct_len
, total_len
= 0;
2965 if (rec
.v2
.flock_len
) {
2966 ceph_count_locks(inode
, &num_fcntl_locks
, &num_flock_locks
);
2968 num_fcntl_locks
= 0;
2969 num_flock_locks
= 0;
2971 if (num_fcntl_locks
+ num_flock_locks
> 0) {
2972 flocks
= kmalloc((num_fcntl_locks
+ num_flock_locks
) *
2973 sizeof(struct ceph_filelock
), GFP_NOFS
);
2978 err
= ceph_encode_locks_to_buffer(inode
, flocks
,
2993 if (recon_state
->msg_version
>= 3) {
2994 /* version, compat_version and struct_len */
2995 total_len
= 2 * sizeof(u8
) + sizeof(u32
);
2999 * number of encoded locks is stable, so copy to pagelist
3001 struct_len
= 2 * sizeof(u32
) +
3002 (num_fcntl_locks
+ num_flock_locks
) *
3003 sizeof(struct ceph_filelock
);
3004 rec
.v2
.flock_len
= cpu_to_le32(struct_len
);
3006 struct_len
+= sizeof(rec
.v2
);
3007 struct_len
+= sizeof(u32
) + pathlen
;
3010 struct_len
+= sizeof(u64
); /* snap_follows */
3012 total_len
+= struct_len
;
3013 err
= ceph_pagelist_reserve(pagelist
, total_len
);
3016 if (recon_state
->msg_version
>= 3) {
3017 ceph_pagelist_encode_8(pagelist
, struct_v
);
3018 ceph_pagelist_encode_8(pagelist
, 1);
3019 ceph_pagelist_encode_32(pagelist
, struct_len
);
3021 ceph_pagelist_encode_string(pagelist
, path
, pathlen
);
3022 ceph_pagelist_append(pagelist
, &rec
, sizeof(rec
.v2
));
3023 ceph_locks_to_pagelist(flocks
, pagelist
,
3027 ceph_pagelist_encode_64(pagelist
, snap_follows
);
3031 size_t size
= sizeof(u32
) + pathlen
+ sizeof(rec
.v1
);
3032 err
= ceph_pagelist_reserve(pagelist
, size
);
3034 ceph_pagelist_encode_string(pagelist
, path
, pathlen
);
3035 ceph_pagelist_append(pagelist
, &rec
, sizeof(rec
.v1
));
3039 recon_state
->nr_caps
++;
3049 * If an MDS fails and recovers, clients need to reconnect in order to
3050 * reestablish shared state. This includes all caps issued through
3051 * this session _and_ the snap_realm hierarchy. Because it's not
3052 * clear which snap realms the mds cares about, we send everything we
3053 * know about.. that ensures we'll then get any new info the
3054 * recovering MDS might have.
3056 * This is a relatively heavyweight operation, but it's rare.
3058 * called with mdsc->mutex held.
3060 static void send_mds_reconnect(struct ceph_mds_client
*mdsc
,
3061 struct ceph_mds_session
*session
)
3063 struct ceph_msg
*reply
;
3065 int mds
= session
->s_mds
;
3068 struct ceph_pagelist
*pagelist
;
3069 struct ceph_reconnect_state recon_state
;
3072 pr_info("mds%d reconnect start\n", mds
);
3074 pagelist
= kmalloc(sizeof(*pagelist
), GFP_NOFS
);
3076 goto fail_nopagelist
;
3077 ceph_pagelist_init(pagelist
);
3079 reply
= ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT
, 0, GFP_NOFS
, false);
3083 mutex_lock(&session
->s_mutex
);
3084 session
->s_state
= CEPH_MDS_SESSION_RECONNECTING
;
3087 dout("session %p state %s\n", session
,
3088 ceph_session_state_name(session
->s_state
));
3090 spin_lock(&session
->s_gen_ttl_lock
);
3091 session
->s_cap_gen
++;
3092 spin_unlock(&session
->s_gen_ttl_lock
);
3094 spin_lock(&session
->s_cap_lock
);
3095 /* don't know if session is readonly */
3096 session
->s_readonly
= 0;
3098 * notify __ceph_remove_cap() that we are composing cap reconnect.
3099 * If a cap get released before being added to the cap reconnect,
3100 * __ceph_remove_cap() should skip queuing cap release.
3102 session
->s_cap_reconnect
= 1;
3103 /* drop old cap expires; we're about to reestablish that state */
3104 detach_cap_releases(session
, &dispose
);
3105 spin_unlock(&session
->s_cap_lock
);
3106 dispose_cap_releases(mdsc
, &dispose
);
3108 /* trim unused caps to reduce MDS's cache rejoin time */
3109 if (mdsc
->fsc
->sb
->s_root
)
3110 shrink_dcache_parent(mdsc
->fsc
->sb
->s_root
);
3112 ceph_con_close(&session
->s_con
);
3113 ceph_con_open(&session
->s_con
,
3114 CEPH_ENTITY_TYPE_MDS
, mds
,
3115 ceph_mdsmap_get_addr(mdsc
->mdsmap
, mds
));
3117 /* replay unsafe requests */
3118 replay_unsafe_requests(mdsc
, session
);
3120 down_read(&mdsc
->snap_rwsem
);
3122 /* traverse this session's caps */
3123 s_nr_caps
= session
->s_nr_caps
;
3124 err
= ceph_pagelist_encode_32(pagelist
, s_nr_caps
);
3128 recon_state
.nr_caps
= 0;
3129 recon_state
.pagelist
= pagelist
;
3130 if (session
->s_con
.peer_features
& CEPH_FEATURE_MDSENC
)
3131 recon_state
.msg_version
= 3;
3132 else if (session
->s_con
.peer_features
& CEPH_FEATURE_FLOCK
)
3133 recon_state
.msg_version
= 2;
3135 recon_state
.msg_version
= 1;
3136 err
= iterate_session_caps(session
, encode_caps_cb
, &recon_state
);
3140 spin_lock(&session
->s_cap_lock
);
3141 session
->s_cap_reconnect
= 0;
3142 spin_unlock(&session
->s_cap_lock
);
3145 * snaprealms. we provide mds with the ino, seq (version), and
3146 * parent for all of our realms. If the mds has any newer info,
3149 for (p
= rb_first(&mdsc
->snap_realms
); p
; p
= rb_next(p
)) {
3150 struct ceph_snap_realm
*realm
=
3151 rb_entry(p
, struct ceph_snap_realm
, node
);
3152 struct ceph_mds_snaprealm_reconnect sr_rec
;
3154 dout(" adding snap realm %llx seq %lld parent %llx\n",
3155 realm
->ino
, realm
->seq
, realm
->parent_ino
);
3156 sr_rec
.ino
= cpu_to_le64(realm
->ino
);
3157 sr_rec
.seq
= cpu_to_le64(realm
->seq
);
3158 sr_rec
.parent
= cpu_to_le64(realm
->parent_ino
);
3159 err
= ceph_pagelist_append(pagelist
, &sr_rec
, sizeof(sr_rec
));
3164 reply
->hdr
.version
= cpu_to_le16(recon_state
.msg_version
);
3166 /* raced with cap release? */
3167 if (s_nr_caps
!= recon_state
.nr_caps
) {
3168 struct page
*page
= list_first_entry(&pagelist
->head
,
3170 __le32
*addr
= kmap_atomic(page
);
3171 *addr
= cpu_to_le32(recon_state
.nr_caps
);
3172 kunmap_atomic(addr
);
3175 reply
->hdr
.data_len
= cpu_to_le32(pagelist
->length
);
3176 ceph_msg_data_add_pagelist(reply
, pagelist
);
3178 ceph_early_kick_flushing_caps(mdsc
, session
);
3180 ceph_con_send(&session
->s_con
, reply
);
3182 mutex_unlock(&session
->s_mutex
);
3184 mutex_lock(&mdsc
->mutex
);
3185 __wake_requests(mdsc
, &session
->s_waiting
);
3186 mutex_unlock(&mdsc
->mutex
);
3188 up_read(&mdsc
->snap_rwsem
);
3192 ceph_msg_put(reply
);
3193 up_read(&mdsc
->snap_rwsem
);
3194 mutex_unlock(&session
->s_mutex
);
3196 ceph_pagelist_release(pagelist
);
3198 pr_err("error %d preparing reconnect for mds%d\n", err
, mds
);
3204 * compare old and new mdsmaps, kicking requests
3205 * and closing out old connections as necessary
3207 * called under mdsc->mutex.
3209 static void check_new_map(struct ceph_mds_client
*mdsc
,
3210 struct ceph_mdsmap
*newmap
,
3211 struct ceph_mdsmap
*oldmap
)
3214 int oldstate
, newstate
;
3215 struct ceph_mds_session
*s
;
3217 dout("check_new_map new %u old %u\n",
3218 newmap
->m_epoch
, oldmap
->m_epoch
);
3220 for (i
= 0; i
< oldmap
->m_num_mds
&& i
< mdsc
->max_sessions
; i
++) {
3221 if (!mdsc
->sessions
[i
])
3223 s
= mdsc
->sessions
[i
];
3224 oldstate
= ceph_mdsmap_get_state(oldmap
, i
);
3225 newstate
= ceph_mdsmap_get_state(newmap
, i
);
3227 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
3228 i
, ceph_mds_state_name(oldstate
),
3229 ceph_mdsmap_is_laggy(oldmap
, i
) ? " (laggy)" : "",
3230 ceph_mds_state_name(newstate
),
3231 ceph_mdsmap_is_laggy(newmap
, i
) ? " (laggy)" : "",
3232 ceph_session_state_name(s
->s_state
));
3234 if (i
>= newmap
->m_num_mds
||
3235 memcmp(ceph_mdsmap_get_addr(oldmap
, i
),
3236 ceph_mdsmap_get_addr(newmap
, i
),
3237 sizeof(struct ceph_entity_addr
))) {
3238 if (s
->s_state
== CEPH_MDS_SESSION_OPENING
) {
3239 /* the session never opened, just close it
3242 __unregister_session(mdsc
, s
);
3243 __wake_requests(mdsc
, &s
->s_waiting
);
3244 ceph_put_mds_session(s
);
3245 } else if (i
>= newmap
->m_num_mds
) {
3246 /* force close session for stopped mds */
3248 __unregister_session(mdsc
, s
);
3249 __wake_requests(mdsc
, &s
->s_waiting
);
3250 kick_requests(mdsc
, i
);
3251 mutex_unlock(&mdsc
->mutex
);
3253 mutex_lock(&s
->s_mutex
);
3254 cleanup_session_requests(mdsc
, s
);
3255 remove_session_caps(s
);
3256 mutex_unlock(&s
->s_mutex
);
3258 ceph_put_mds_session(s
);
3260 mutex_lock(&mdsc
->mutex
);
3263 mutex_unlock(&mdsc
->mutex
);
3264 mutex_lock(&s
->s_mutex
);
3265 mutex_lock(&mdsc
->mutex
);
3266 ceph_con_close(&s
->s_con
);
3267 mutex_unlock(&s
->s_mutex
);
3268 s
->s_state
= CEPH_MDS_SESSION_RESTARTING
;
3270 } else if (oldstate
== newstate
) {
3271 continue; /* nothing new with this mds */
3277 if (s
->s_state
== CEPH_MDS_SESSION_RESTARTING
&&
3278 newstate
>= CEPH_MDS_STATE_RECONNECT
) {
3279 mutex_unlock(&mdsc
->mutex
);
3280 send_mds_reconnect(mdsc
, s
);
3281 mutex_lock(&mdsc
->mutex
);
3285 * kick request on any mds that has gone active.
3287 if (oldstate
< CEPH_MDS_STATE_ACTIVE
&&
3288 newstate
>= CEPH_MDS_STATE_ACTIVE
) {
3289 if (oldstate
!= CEPH_MDS_STATE_CREATING
&&
3290 oldstate
!= CEPH_MDS_STATE_STARTING
)
3291 pr_info("mds%d recovery completed\n", s
->s_mds
);
3292 kick_requests(mdsc
, i
);
3293 ceph_kick_flushing_caps(mdsc
, s
);
3294 wake_up_session_caps(s
, 1);
3298 for (i
= 0; i
< newmap
->m_num_mds
&& i
< mdsc
->max_sessions
; i
++) {
3299 s
= mdsc
->sessions
[i
];
3302 if (!ceph_mdsmap_is_laggy(newmap
, i
))
3304 if (s
->s_state
== CEPH_MDS_SESSION_OPEN
||
3305 s
->s_state
== CEPH_MDS_SESSION_HUNG
||
3306 s
->s_state
== CEPH_MDS_SESSION_CLOSING
) {
3307 dout(" connecting to export targets of laggy mds%d\n",
3309 __open_export_target_sessions(mdsc
, s
);
3321 * caller must hold session s_mutex, dentry->d_lock
3323 void __ceph_mdsc_drop_dentry_lease(struct dentry
*dentry
)
3325 struct ceph_dentry_info
*di
= ceph_dentry(dentry
);
3327 ceph_put_mds_session(di
->lease_session
);
3328 di
->lease_session
= NULL
;
3331 static void handle_lease(struct ceph_mds_client
*mdsc
,
3332 struct ceph_mds_session
*session
,
3333 struct ceph_msg
*msg
)
3335 struct super_block
*sb
= mdsc
->fsc
->sb
;
3336 struct inode
*inode
;
3337 struct dentry
*parent
, *dentry
;
3338 struct ceph_dentry_info
*di
;
3339 int mds
= session
->s_mds
;
3340 struct ceph_mds_lease
*h
= msg
->front
.iov_base
;
3342 struct ceph_vino vino
;
3346 dout("handle_lease from mds%d\n", mds
);
3349 if (msg
->front
.iov_len
< sizeof(*h
) + sizeof(u32
))
3351 vino
.ino
= le64_to_cpu(h
->ino
);
3352 vino
.snap
= CEPH_NOSNAP
;
3353 seq
= le32_to_cpu(h
->seq
);
3354 dname
.name
= (void *)h
+ sizeof(*h
) + sizeof(u32
);
3355 dname
.len
= msg
->front
.iov_len
- sizeof(*h
) - sizeof(u32
);
3356 if (dname
.len
!= get_unaligned_le32(h
+1))
3360 inode
= ceph_find_inode(sb
, vino
);
3361 dout("handle_lease %s, ino %llx %p %.*s\n",
3362 ceph_lease_op_name(h
->action
), vino
.ino
, inode
,
3363 dname
.len
, dname
.name
);
3365 mutex_lock(&session
->s_mutex
);
3369 dout("handle_lease no inode %llx\n", vino
.ino
);
3374 parent
= d_find_alias(inode
);
3376 dout("no parent dentry on inode %p\n", inode
);
3378 goto release
; /* hrm... */
3380 dname
.hash
= full_name_hash(parent
, dname
.name
, dname
.len
);
3381 dentry
= d_lookup(parent
, &dname
);
3386 spin_lock(&dentry
->d_lock
);
3387 di
= ceph_dentry(dentry
);
3388 switch (h
->action
) {
3389 case CEPH_MDS_LEASE_REVOKE
:
3390 if (di
->lease_session
== session
) {
3391 if (ceph_seq_cmp(di
->lease_seq
, seq
) > 0)
3392 h
->seq
= cpu_to_le32(di
->lease_seq
);
3393 __ceph_mdsc_drop_dentry_lease(dentry
);
3398 case CEPH_MDS_LEASE_RENEW
:
3399 if (di
->lease_session
== session
&&
3400 di
->lease_gen
== session
->s_cap_gen
&&
3401 di
->lease_renew_from
&&
3402 di
->lease_renew_after
== 0) {
3403 unsigned long duration
=
3404 msecs_to_jiffies(le32_to_cpu(h
->duration_ms
));
3406 di
->lease_seq
= seq
;
3407 di
->time
= di
->lease_renew_from
+ duration
;
3408 di
->lease_renew_after
= di
->lease_renew_from
+
3410 di
->lease_renew_from
= 0;
3414 spin_unlock(&dentry
->d_lock
);
3421 /* let's just reuse the same message */
3422 h
->action
= CEPH_MDS_LEASE_REVOKE_ACK
;
3424 ceph_con_send(&session
->s_con
, msg
);
3428 mutex_unlock(&session
->s_mutex
);
3432 pr_err("corrupt lease message\n");
3436 void ceph_mdsc_lease_send_msg(struct ceph_mds_session
*session
,
3437 struct inode
*inode
,
3438 struct dentry
*dentry
, char action
,
3441 struct ceph_msg
*msg
;
3442 struct ceph_mds_lease
*lease
;
3443 int len
= sizeof(*lease
) + sizeof(u32
);
3446 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
3447 inode
, dentry
, ceph_lease_op_name(action
), session
->s_mds
);
3448 dnamelen
= dentry
->d_name
.len
;
3451 msg
= ceph_msg_new(CEPH_MSG_CLIENT_LEASE
, len
, GFP_NOFS
, false);
3454 lease
= msg
->front
.iov_base
;
3455 lease
->action
= action
;
3456 lease
->ino
= cpu_to_le64(ceph_vino(inode
).ino
);
3457 lease
->first
= lease
->last
= cpu_to_le64(ceph_vino(inode
).snap
);
3458 lease
->seq
= cpu_to_le32(seq
);
3459 put_unaligned_le32(dnamelen
, lease
+ 1);
3460 memcpy((void *)(lease
+ 1) + 4, dentry
->d_name
.name
, dnamelen
);
3463 * if this is a preemptive lease RELEASE, no need to
3464 * flush request stream, since the actual request will
3467 msg
->more_to_follow
= (action
== CEPH_MDS_LEASE_RELEASE
);
3469 ceph_con_send(&session
->s_con
, msg
);
3473 * drop all leases (and dentry refs) in preparation for umount
3475 static void drop_leases(struct ceph_mds_client
*mdsc
)
3479 dout("drop_leases\n");
3480 mutex_lock(&mdsc
->mutex
);
3481 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
3482 struct ceph_mds_session
*s
= __ceph_lookup_mds_session(mdsc
, i
);
3485 mutex_unlock(&mdsc
->mutex
);
3486 mutex_lock(&s
->s_mutex
);
3487 mutex_unlock(&s
->s_mutex
);
3488 ceph_put_mds_session(s
);
3489 mutex_lock(&mdsc
->mutex
);
3491 mutex_unlock(&mdsc
->mutex
);
3497 * delayed work -- periodically trim expired leases, renew caps with mds
3499 static void schedule_delayed(struct ceph_mds_client
*mdsc
)
3502 unsigned hz
= round_jiffies_relative(HZ
* delay
);
3503 schedule_delayed_work(&mdsc
->delayed_work
, hz
);
3506 static void delayed_work(struct work_struct
*work
)
3509 struct ceph_mds_client
*mdsc
=
3510 container_of(work
, struct ceph_mds_client
, delayed_work
.work
);
3514 dout("mdsc delayed_work\n");
3515 ceph_check_delayed_caps(mdsc
);
3517 mutex_lock(&mdsc
->mutex
);
3518 renew_interval
= mdsc
->mdsmap
->m_session_timeout
>> 2;
3519 renew_caps
= time_after_eq(jiffies
, HZ
*renew_interval
+
3520 mdsc
->last_renew_caps
);
3522 mdsc
->last_renew_caps
= jiffies
;
3524 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
3525 struct ceph_mds_session
*s
= __ceph_lookup_mds_session(mdsc
, i
);
3528 if (s
->s_state
== CEPH_MDS_SESSION_CLOSING
) {
3529 dout("resending session close request for mds%d\n",
3531 request_close_session(mdsc
, s
);
3532 ceph_put_mds_session(s
);
3535 if (s
->s_ttl
&& time_after(jiffies
, s
->s_ttl
)) {
3536 if (s
->s_state
== CEPH_MDS_SESSION_OPEN
) {
3537 s
->s_state
= CEPH_MDS_SESSION_HUNG
;
3538 pr_info("mds%d hung\n", s
->s_mds
);
3541 if (s
->s_state
< CEPH_MDS_SESSION_OPEN
) {
3542 /* this mds is failed or recovering, just wait */
3543 ceph_put_mds_session(s
);
3546 mutex_unlock(&mdsc
->mutex
);
3548 mutex_lock(&s
->s_mutex
);
3550 send_renew_caps(mdsc
, s
);
3552 ceph_con_keepalive(&s
->s_con
);
3553 if (s
->s_state
== CEPH_MDS_SESSION_OPEN
||
3554 s
->s_state
== CEPH_MDS_SESSION_HUNG
)
3555 ceph_send_cap_releases(mdsc
, s
);
3556 mutex_unlock(&s
->s_mutex
);
3557 ceph_put_mds_session(s
);
3559 mutex_lock(&mdsc
->mutex
);
3561 mutex_unlock(&mdsc
->mutex
);
3563 schedule_delayed(mdsc
);
3566 int ceph_mdsc_init(struct ceph_fs_client
*fsc
)
3569 struct ceph_mds_client
*mdsc
;
3571 mdsc
= kzalloc(sizeof(struct ceph_mds_client
), GFP_NOFS
);
3576 mutex_init(&mdsc
->mutex
);
3577 mdsc
->mdsmap
= kzalloc(sizeof(*mdsc
->mdsmap
), GFP_NOFS
);
3578 if (!mdsc
->mdsmap
) {
3583 init_completion(&mdsc
->safe_umount_waiters
);
3584 init_waitqueue_head(&mdsc
->session_close_wq
);
3585 INIT_LIST_HEAD(&mdsc
->waiting_for_map
);
3586 mdsc
->sessions
= NULL
;
3587 atomic_set(&mdsc
->num_sessions
, 0);
3588 mdsc
->max_sessions
= 0;
3590 mdsc
->last_snap_seq
= 0;
3591 init_rwsem(&mdsc
->snap_rwsem
);
3592 mdsc
->snap_realms
= RB_ROOT
;
3593 INIT_LIST_HEAD(&mdsc
->snap_empty
);
3594 spin_lock_init(&mdsc
->snap_empty_lock
);
3596 mdsc
->oldest_tid
= 0;
3597 mdsc
->request_tree
= RB_ROOT
;
3598 INIT_DELAYED_WORK(&mdsc
->delayed_work
, delayed_work
);
3599 mdsc
->last_renew_caps
= jiffies
;
3600 INIT_LIST_HEAD(&mdsc
->cap_delay_list
);
3601 spin_lock_init(&mdsc
->cap_delay_lock
);
3602 INIT_LIST_HEAD(&mdsc
->snap_flush_list
);
3603 spin_lock_init(&mdsc
->snap_flush_lock
);
3604 mdsc
->last_cap_flush_tid
= 1;
3605 INIT_LIST_HEAD(&mdsc
->cap_flush_list
);
3606 INIT_LIST_HEAD(&mdsc
->cap_dirty
);
3607 INIT_LIST_HEAD(&mdsc
->cap_dirty_migrating
);
3608 mdsc
->num_cap_flushing
= 0;
3609 spin_lock_init(&mdsc
->cap_dirty_lock
);
3610 init_waitqueue_head(&mdsc
->cap_flushing_wq
);
3611 spin_lock_init(&mdsc
->dentry_lru_lock
);
3612 INIT_LIST_HEAD(&mdsc
->dentry_lru
);
3614 ceph_caps_init(mdsc
);
3615 ceph_adjust_min_caps(mdsc
, fsc
->min_caps
);
3617 init_rwsem(&mdsc
->pool_perm_rwsem
);
3618 mdsc
->pool_perm_tree
= RB_ROOT
;
3620 strncpy(mdsc
->nodename
, utsname()->nodename
,
3621 sizeof(mdsc
->nodename
) - 1);
3626 * Wait for safe replies on open mds requests. If we time out, drop
3627 * all requests from the tree to avoid dangling dentry refs.
3629 static void wait_requests(struct ceph_mds_client
*mdsc
)
3631 struct ceph_options
*opts
= mdsc
->fsc
->client
->options
;
3632 struct ceph_mds_request
*req
;
3634 mutex_lock(&mdsc
->mutex
);
3635 if (__get_oldest_req(mdsc
)) {
3636 mutex_unlock(&mdsc
->mutex
);
3638 dout("wait_requests waiting for requests\n");
3639 wait_for_completion_timeout(&mdsc
->safe_umount_waiters
,
3640 ceph_timeout_jiffies(opts
->mount_timeout
));
3642 /* tear down remaining requests */
3643 mutex_lock(&mdsc
->mutex
);
3644 while ((req
= __get_oldest_req(mdsc
))) {
3645 dout("wait_requests timed out on tid %llu\n",
3647 __unregister_request(mdsc
, req
);
3650 mutex_unlock(&mdsc
->mutex
);
3651 dout("wait_requests done\n");
3655 * called before mount is ro, and before dentries are torn down.
3656 * (hmm, does this still race with new lookups?)
3658 void ceph_mdsc_pre_umount(struct ceph_mds_client
*mdsc
)
3660 dout("pre_umount\n");
3664 ceph_flush_dirty_caps(mdsc
);
3665 wait_requests(mdsc
);
3668 * wait for reply handlers to drop their request refs and
3669 * their inode/dcache refs
3675 * wait for all write mds requests to flush.
3677 static void wait_unsafe_requests(struct ceph_mds_client
*mdsc
, u64 want_tid
)
3679 struct ceph_mds_request
*req
= NULL
, *nextreq
;
3682 mutex_lock(&mdsc
->mutex
);
3683 dout("wait_unsafe_requests want %lld\n", want_tid
);
3685 req
= __get_oldest_req(mdsc
);
3686 while (req
&& req
->r_tid
<= want_tid
) {
3687 /* find next request */
3688 n
= rb_next(&req
->r_node
);
3690 nextreq
= rb_entry(n
, struct ceph_mds_request
, r_node
);
3693 if (req
->r_op
!= CEPH_MDS_OP_SETFILELOCK
&&
3694 (req
->r_op
& CEPH_MDS_OP_WRITE
)) {
3696 ceph_mdsc_get_request(req
);
3698 ceph_mdsc_get_request(nextreq
);
3699 mutex_unlock(&mdsc
->mutex
);
3700 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3701 req
->r_tid
, want_tid
);
3702 wait_for_completion(&req
->r_safe_completion
);
3703 mutex_lock(&mdsc
->mutex
);
3704 ceph_mdsc_put_request(req
);
3706 break; /* next dne before, so we're done! */
3707 if (RB_EMPTY_NODE(&nextreq
->r_node
)) {
3708 /* next request was removed from tree */
3709 ceph_mdsc_put_request(nextreq
);
3712 ceph_mdsc_put_request(nextreq
); /* won't go away */
3716 mutex_unlock(&mdsc
->mutex
);
3717 dout("wait_unsafe_requests done\n");
3720 void ceph_mdsc_sync(struct ceph_mds_client
*mdsc
)
3722 u64 want_tid
, want_flush
;
3724 if (READ_ONCE(mdsc
->fsc
->mount_state
) == CEPH_MOUNT_SHUTDOWN
)
3728 mutex_lock(&mdsc
->mutex
);
3729 want_tid
= mdsc
->last_tid
;
3730 mutex_unlock(&mdsc
->mutex
);
3732 ceph_flush_dirty_caps(mdsc
);
3733 spin_lock(&mdsc
->cap_dirty_lock
);
3734 want_flush
= mdsc
->last_cap_flush_tid
;
3735 if (!list_empty(&mdsc
->cap_flush_list
)) {
3736 struct ceph_cap_flush
*cf
=
3737 list_last_entry(&mdsc
->cap_flush_list
,
3738 struct ceph_cap_flush
, g_list
);
3741 spin_unlock(&mdsc
->cap_dirty_lock
);
3743 dout("sync want tid %lld flush_seq %lld\n",
3744 want_tid
, want_flush
);
3746 wait_unsafe_requests(mdsc
, want_tid
);
3747 wait_caps_flush(mdsc
, want_flush
);
3751 * true if all sessions are closed, or we force unmount
3753 static bool done_closing_sessions(struct ceph_mds_client
*mdsc
, int skipped
)
3755 if (READ_ONCE(mdsc
->fsc
->mount_state
) == CEPH_MOUNT_SHUTDOWN
)
3757 return atomic_read(&mdsc
->num_sessions
) <= skipped
;
3761 * called after sb is ro.
3763 void ceph_mdsc_close_sessions(struct ceph_mds_client
*mdsc
)
3765 struct ceph_options
*opts
= mdsc
->fsc
->client
->options
;
3766 struct ceph_mds_session
*session
;
3770 dout("close_sessions\n");
3772 /* close sessions */
3773 mutex_lock(&mdsc
->mutex
);
3774 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
3775 session
= __ceph_lookup_mds_session(mdsc
, i
);
3778 mutex_unlock(&mdsc
->mutex
);
3779 mutex_lock(&session
->s_mutex
);
3780 if (__close_session(mdsc
, session
) <= 0)
3782 mutex_unlock(&session
->s_mutex
);
3783 ceph_put_mds_session(session
);
3784 mutex_lock(&mdsc
->mutex
);
3786 mutex_unlock(&mdsc
->mutex
);
3788 dout("waiting for sessions to close\n");
3789 wait_event_timeout(mdsc
->session_close_wq
,
3790 done_closing_sessions(mdsc
, skipped
),
3791 ceph_timeout_jiffies(opts
->mount_timeout
));
3793 /* tear down remaining sessions */
3794 mutex_lock(&mdsc
->mutex
);
3795 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
3796 if (mdsc
->sessions
[i
]) {
3797 session
= get_session(mdsc
->sessions
[i
]);
3798 __unregister_session(mdsc
, session
);
3799 mutex_unlock(&mdsc
->mutex
);
3800 mutex_lock(&session
->s_mutex
);
3801 remove_session_caps(session
);
3802 mutex_unlock(&session
->s_mutex
);
3803 ceph_put_mds_session(session
);
3804 mutex_lock(&mdsc
->mutex
);
3807 WARN_ON(!list_empty(&mdsc
->cap_delay_list
));
3808 mutex_unlock(&mdsc
->mutex
);
3810 ceph_cleanup_empty_realms(mdsc
);
3812 cancel_delayed_work_sync(&mdsc
->delayed_work
); /* cancel timer */
3817 void ceph_mdsc_force_umount(struct ceph_mds_client
*mdsc
)
3819 struct ceph_mds_session
*session
;
3822 dout("force umount\n");
3824 mutex_lock(&mdsc
->mutex
);
3825 for (mds
= 0; mds
< mdsc
->max_sessions
; mds
++) {
3826 session
= __ceph_lookup_mds_session(mdsc
, mds
);
3829 mutex_unlock(&mdsc
->mutex
);
3830 mutex_lock(&session
->s_mutex
);
3831 __close_session(mdsc
, session
);
3832 if (session
->s_state
== CEPH_MDS_SESSION_CLOSING
) {
3833 cleanup_session_requests(mdsc
, session
);
3834 remove_session_caps(session
);
3836 mutex_unlock(&session
->s_mutex
);
3837 ceph_put_mds_session(session
);
3838 mutex_lock(&mdsc
->mutex
);
3839 kick_requests(mdsc
, mds
);
3841 __wake_requests(mdsc
, &mdsc
->waiting_for_map
);
3842 mutex_unlock(&mdsc
->mutex
);
3845 static void ceph_mdsc_stop(struct ceph_mds_client
*mdsc
)
3848 cancel_delayed_work_sync(&mdsc
->delayed_work
); /* cancel timer */
3850 ceph_mdsmap_destroy(mdsc
->mdsmap
);
3851 kfree(mdsc
->sessions
);
3852 ceph_caps_finalize(mdsc
);
3853 ceph_pool_perm_destroy(mdsc
);
3856 void ceph_mdsc_destroy(struct ceph_fs_client
*fsc
)
3858 struct ceph_mds_client
*mdsc
= fsc
->mdsc
;
3859 dout("mdsc_destroy %p\n", mdsc
);
3861 /* flush out any connection work with references to us */
3864 ceph_mdsc_stop(mdsc
);
3868 dout("mdsc_destroy %p done\n", mdsc
);
3871 void ceph_mdsc_handle_fsmap(struct ceph_mds_client
*mdsc
, struct ceph_msg
*msg
)
3873 struct ceph_fs_client
*fsc
= mdsc
->fsc
;
3874 const char *mds_namespace
= fsc
->mount_options
->mds_namespace
;
3875 void *p
= msg
->front
.iov_base
;
3876 void *end
= p
+ msg
->front
.iov_len
;
3880 u32 mount_fscid
= (u32
)-1;
3881 u8 struct_v
, struct_cv
;
3884 ceph_decode_need(&p
, end
, sizeof(u32
), bad
);
3885 epoch
= ceph_decode_32(&p
);
3887 dout("handle_fsmap epoch %u\n", epoch
);
3889 ceph_decode_need(&p
, end
, 2 + sizeof(u32
), bad
);
3890 struct_v
= ceph_decode_8(&p
);
3891 struct_cv
= ceph_decode_8(&p
);
3892 map_len
= ceph_decode_32(&p
);
3894 ceph_decode_need(&p
, end
, sizeof(u32
) * 3, bad
);
3895 p
+= sizeof(u32
) * 2; /* skip epoch and legacy_client_fscid */
3897 num_fs
= ceph_decode_32(&p
);
3898 while (num_fs
-- > 0) {
3899 void *info_p
, *info_end
;
3904 ceph_decode_need(&p
, end
, 2 + sizeof(u32
), bad
);
3905 info_v
= ceph_decode_8(&p
);
3906 info_cv
= ceph_decode_8(&p
);
3907 info_len
= ceph_decode_32(&p
);
3908 ceph_decode_need(&p
, end
, info_len
, bad
);
3910 info_end
= p
+ info_len
;
3913 ceph_decode_need(&info_p
, info_end
, sizeof(u32
) * 2, bad
);
3914 fscid
= ceph_decode_32(&info_p
);
3915 namelen
= ceph_decode_32(&info_p
);
3916 ceph_decode_need(&info_p
, info_end
, namelen
, bad
);
3918 if (mds_namespace
&&
3919 strlen(mds_namespace
) == namelen
&&
3920 !strncmp(mds_namespace
, (char *)info_p
, namelen
)) {
3921 mount_fscid
= fscid
;
3926 ceph_monc_got_map(&fsc
->client
->monc
, CEPH_SUB_FSMAP
, epoch
);
3927 if (mount_fscid
!= (u32
)-1) {
3928 fsc
->client
->monc
.fs_cluster_id
= mount_fscid
;
3929 ceph_monc_want_map(&fsc
->client
->monc
, CEPH_SUB_MDSMAP
,
3931 ceph_monc_renew_subs(&fsc
->client
->monc
);
3939 pr_err("error decoding fsmap\n");
3941 mutex_lock(&mdsc
->mutex
);
3942 mdsc
->mdsmap_err
= err
;
3943 __wake_requests(mdsc
, &mdsc
->waiting_for_map
);
3944 mutex_unlock(&mdsc
->mutex
);
3948 * handle mds map update.
3950 void ceph_mdsc_handle_mdsmap(struct ceph_mds_client
*mdsc
, struct ceph_msg
*msg
)
3954 void *p
= msg
->front
.iov_base
;
3955 void *end
= p
+ msg
->front
.iov_len
;
3956 struct ceph_mdsmap
*newmap
, *oldmap
;
3957 struct ceph_fsid fsid
;
3960 ceph_decode_need(&p
, end
, sizeof(fsid
)+2*sizeof(u32
), bad
);
3961 ceph_decode_copy(&p
, &fsid
, sizeof(fsid
));
3962 if (ceph_check_fsid(mdsc
->fsc
->client
, &fsid
) < 0)
3964 epoch
= ceph_decode_32(&p
);
3965 maplen
= ceph_decode_32(&p
);
3966 dout("handle_map epoch %u len %d\n", epoch
, (int)maplen
);
3968 /* do we need it? */
3969 mutex_lock(&mdsc
->mutex
);
3970 if (mdsc
->mdsmap
&& epoch
<= mdsc
->mdsmap
->m_epoch
) {
3971 dout("handle_map epoch %u <= our %u\n",
3972 epoch
, mdsc
->mdsmap
->m_epoch
);
3973 mutex_unlock(&mdsc
->mutex
);
3977 newmap
= ceph_mdsmap_decode(&p
, end
);
3978 if (IS_ERR(newmap
)) {
3979 err
= PTR_ERR(newmap
);
3983 /* swap into place */
3985 oldmap
= mdsc
->mdsmap
;
3986 mdsc
->mdsmap
= newmap
;
3987 check_new_map(mdsc
, newmap
, oldmap
);
3988 ceph_mdsmap_destroy(oldmap
);
3990 mdsc
->mdsmap
= newmap
; /* first mds map */
3992 mdsc
->fsc
->sb
->s_maxbytes
= mdsc
->mdsmap
->m_max_file_size
;
3994 __wake_requests(mdsc
, &mdsc
->waiting_for_map
);
3995 ceph_monc_got_map(&mdsc
->fsc
->client
->monc
, CEPH_SUB_MDSMAP
,
3996 mdsc
->mdsmap
->m_epoch
);
3998 mutex_unlock(&mdsc
->mutex
);
3999 schedule_delayed(mdsc
);
4003 mutex_unlock(&mdsc
->mutex
);
4005 pr_err("error decoding mdsmap %d\n", err
);
4009 static struct ceph_connection
*con_get(struct ceph_connection
*con
)
4011 struct ceph_mds_session
*s
= con
->private;
4013 if (get_session(s
)) {
4014 dout("mdsc con_get %p ok (%d)\n", s
, refcount_read(&s
->s_ref
));
4017 dout("mdsc con_get %p FAIL\n", s
);
4021 static void con_put(struct ceph_connection
*con
)
4023 struct ceph_mds_session
*s
= con
->private;
4025 dout("mdsc con_put %p (%d)\n", s
, refcount_read(&s
->s_ref
) - 1);
4026 ceph_put_mds_session(s
);
4030 * if the client is unresponsive for long enough, the mds will kill
4031 * the session entirely.
4033 static void peer_reset(struct ceph_connection
*con
)
4035 struct ceph_mds_session
*s
= con
->private;
4036 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
4038 pr_warn("mds%d closed our session\n", s
->s_mds
);
4039 send_mds_reconnect(mdsc
, s
);
4042 static void dispatch(struct ceph_connection
*con
, struct ceph_msg
*msg
)
4044 struct ceph_mds_session
*s
= con
->private;
4045 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
4046 int type
= le16_to_cpu(msg
->hdr
.type
);
4048 mutex_lock(&mdsc
->mutex
);
4049 if (__verify_registered_session(mdsc
, s
) < 0) {
4050 mutex_unlock(&mdsc
->mutex
);
4053 mutex_unlock(&mdsc
->mutex
);
4056 case CEPH_MSG_MDS_MAP
:
4057 ceph_mdsc_handle_mdsmap(mdsc
, msg
);
4059 case CEPH_MSG_FS_MAP_USER
:
4060 ceph_mdsc_handle_fsmap(mdsc
, msg
);
4062 case CEPH_MSG_CLIENT_SESSION
:
4063 handle_session(s
, msg
);
4065 case CEPH_MSG_CLIENT_REPLY
:
4066 handle_reply(s
, msg
);
4068 case CEPH_MSG_CLIENT_REQUEST_FORWARD
:
4069 handle_forward(mdsc
, s
, msg
);
4071 case CEPH_MSG_CLIENT_CAPS
:
4072 ceph_handle_caps(s
, msg
);
4074 case CEPH_MSG_CLIENT_SNAP
:
4075 ceph_handle_snap(mdsc
, s
, msg
);
4077 case CEPH_MSG_CLIENT_LEASE
:
4078 handle_lease(mdsc
, s
, msg
);
4082 pr_err("received unknown message type %d %s\n", type
,
4083 ceph_msg_type_name(type
));
4094 * Note: returned pointer is the address of a structure that's
4095 * managed separately. Caller must *not* attempt to free it.
4097 static struct ceph_auth_handshake
*get_authorizer(struct ceph_connection
*con
,
4098 int *proto
, int force_new
)
4100 struct ceph_mds_session
*s
= con
->private;
4101 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
4102 struct ceph_auth_client
*ac
= mdsc
->fsc
->client
->monc
.auth
;
4103 struct ceph_auth_handshake
*auth
= &s
->s_auth
;
4105 if (force_new
&& auth
->authorizer
) {
4106 ceph_auth_destroy_authorizer(auth
->authorizer
);
4107 auth
->authorizer
= NULL
;
4109 if (!auth
->authorizer
) {
4110 int ret
= ceph_auth_create_authorizer(ac
, CEPH_ENTITY_TYPE_MDS
,
4113 return ERR_PTR(ret
);
4115 int ret
= ceph_auth_update_authorizer(ac
, CEPH_ENTITY_TYPE_MDS
,
4118 return ERR_PTR(ret
);
4120 *proto
= ac
->protocol
;
4126 static int verify_authorizer_reply(struct ceph_connection
*con
)
4128 struct ceph_mds_session
*s
= con
->private;
4129 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
4130 struct ceph_auth_client
*ac
= mdsc
->fsc
->client
->monc
.auth
;
4132 return ceph_auth_verify_authorizer_reply(ac
, s
->s_auth
.authorizer
);
4135 static int invalidate_authorizer(struct ceph_connection
*con
)
4137 struct ceph_mds_session
*s
= con
->private;
4138 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
4139 struct ceph_auth_client
*ac
= mdsc
->fsc
->client
->monc
.auth
;
4141 ceph_auth_invalidate_authorizer(ac
, CEPH_ENTITY_TYPE_MDS
);
4143 return ceph_monc_validate_auth(&mdsc
->fsc
->client
->monc
);
4146 static struct ceph_msg
*mds_alloc_msg(struct ceph_connection
*con
,
4147 struct ceph_msg_header
*hdr
, int *skip
)
4149 struct ceph_msg
*msg
;
4150 int type
= (int) le16_to_cpu(hdr
->type
);
4151 int front_len
= (int) le32_to_cpu(hdr
->front_len
);
4157 msg
= ceph_msg_new(type
, front_len
, GFP_NOFS
, false);
4159 pr_err("unable to allocate msg type %d len %d\n",
4167 static int mds_sign_message(struct ceph_msg
*msg
)
4169 struct ceph_mds_session
*s
= msg
->con
->private;
4170 struct ceph_auth_handshake
*auth
= &s
->s_auth
;
4172 return ceph_auth_sign_message(auth
, msg
);
4175 static int mds_check_message_signature(struct ceph_msg
*msg
)
4177 struct ceph_mds_session
*s
= msg
->con
->private;
4178 struct ceph_auth_handshake
*auth
= &s
->s_auth
;
4180 return ceph_auth_check_message_signature(auth
, msg
);
4183 static const struct ceph_connection_operations mds_con_ops
= {
4186 .dispatch
= dispatch
,
4187 .get_authorizer
= get_authorizer
,
4188 .verify_authorizer_reply
= verify_authorizer_reply
,
4189 .invalidate_authorizer
= invalidate_authorizer
,
4190 .peer_reset
= peer_reset
,
4191 .alloc_msg
= mds_alloc_msg
,
4192 .sign_message
= mds_sign_message
,
4193 .check_message_signature
= mds_check_message_signature
,