1 #include <linux/ceph/ceph_debug.h>
4 #include <linux/wait.h>
5 #include <linux/slab.h>
7 #include <linux/sched.h>
8 #include <linux/debugfs.h>
9 #include <linux/seq_file.h>
10 #include <linux/utsname.h>
11 #include <linux/ratelimit.h>
14 #include "mds_client.h"
16 #include <linux/ceph/ceph_features.h>
17 #include <linux/ceph/messenger.h>
18 #include <linux/ceph/decode.h>
19 #include <linux/ceph/pagelist.h>
20 #include <linux/ceph/auth.h>
21 #include <linux/ceph/debugfs.h>
24 * A cluster of MDS (metadata server) daemons is responsible for
25 * managing the file system namespace (the directory hierarchy and
26 * inodes) and for coordinating shared access to storage. Metadata is
27 * partitioning hierarchically across a number of servers, and that
28 * partition varies over time as the cluster adjusts the distribution
29 * in order to balance load.
31 * The MDS client is primarily responsible to managing synchronous
32 * metadata requests for operations like open, unlink, and so forth.
33 * If there is a MDS failure, we find out about it when we (possibly
34 * request and) receive a new MDS map, and can resubmit affected
37 * For the most part, though, we take advantage of a lossless
38 * communications channel to the MDS, and do not need to worry about
39 * timing out or resubmitting requests.
41 * We maintain a stateful "session" with each MDS we interact with.
42 * Within each session, we sent periodic heartbeat messages to ensure
43 * any capabilities or leases we have been issues remain valid. If
44 * the session times out and goes stale, our leases and capabilities
45 * are no longer valid.
48 struct ceph_reconnect_state
{
50 struct ceph_pagelist
*pagelist
;
54 static void __wake_requests(struct ceph_mds_client
*mdsc
,
55 struct list_head
*head
);
57 static const struct ceph_connection_operations mds_con_ops
;
65 * parse individual inode info
67 static int parse_reply_info_in(void **p
, void *end
,
68 struct ceph_mds_reply_info_in
*info
,
74 *p
+= sizeof(struct ceph_mds_reply_inode
) +
75 sizeof(*info
->in
->fragtree
.splits
) *
76 le32_to_cpu(info
->in
->fragtree
.nsplits
);
78 ceph_decode_32_safe(p
, end
, info
->symlink_len
, bad
);
79 ceph_decode_need(p
, end
, info
->symlink_len
, bad
);
81 *p
+= info
->symlink_len
;
83 if (features
& CEPH_FEATURE_DIRLAYOUTHASH
)
84 ceph_decode_copy_safe(p
, end
, &info
->dir_layout
,
85 sizeof(info
->dir_layout
), bad
);
87 memset(&info
->dir_layout
, 0, sizeof(info
->dir_layout
));
89 ceph_decode_32_safe(p
, end
, info
->xattr_len
, bad
);
90 ceph_decode_need(p
, end
, info
->xattr_len
, bad
);
91 info
->xattr_data
= *p
;
92 *p
+= info
->xattr_len
;
94 if (features
& CEPH_FEATURE_MDS_INLINE_DATA
) {
95 ceph_decode_64_safe(p
, end
, info
->inline_version
, bad
);
96 ceph_decode_32_safe(p
, end
, info
->inline_len
, bad
);
97 ceph_decode_need(p
, end
, info
->inline_len
, bad
);
98 info
->inline_data
= *p
;
99 *p
+= info
->inline_len
;
101 info
->inline_version
= CEPH_INLINE_NONE
;
109 * parse a normal reply, which may contain a (dir+)dentry and/or a
112 static int parse_reply_info_trace(void **p
, void *end
,
113 struct ceph_mds_reply_info_parsed
*info
,
118 if (info
->head
->is_dentry
) {
119 err
= parse_reply_info_in(p
, end
, &info
->diri
, features
);
123 if (unlikely(*p
+ sizeof(*info
->dirfrag
) > end
))
126 *p
+= sizeof(*info
->dirfrag
) +
127 sizeof(u32
)*le32_to_cpu(info
->dirfrag
->ndist
);
128 if (unlikely(*p
> end
))
131 ceph_decode_32_safe(p
, end
, info
->dname_len
, bad
);
132 ceph_decode_need(p
, end
, info
->dname_len
, bad
);
134 *p
+= info
->dname_len
;
136 *p
+= sizeof(*info
->dlease
);
139 if (info
->head
->is_target
) {
140 err
= parse_reply_info_in(p
, end
, &info
->targeti
, features
);
145 if (unlikely(*p
!= end
))
152 pr_err("problem parsing mds trace %d\n", err
);
157 * parse readdir results
159 static int parse_reply_info_dir(void **p
, void *end
,
160 struct ceph_mds_reply_info_parsed
*info
,
167 if (*p
+ sizeof(*info
->dir_dir
) > end
)
169 *p
+= sizeof(*info
->dir_dir
) +
170 sizeof(u32
)*le32_to_cpu(info
->dir_dir
->ndist
);
174 ceph_decode_need(p
, end
, sizeof(num
) + 2, bad
);
175 num
= ceph_decode_32(p
);
176 info
->dir_end
= ceph_decode_8(p
);
177 info
->dir_complete
= ceph_decode_8(p
);
181 BUG_ON(!info
->dir_in
);
182 info
->dir_dname
= (void *)(info
->dir_in
+ num
);
183 info
->dir_dname_len
= (void *)(info
->dir_dname
+ num
);
184 info
->dir_dlease
= (void *)(info
->dir_dname_len
+ num
);
185 if ((unsigned long)(info
->dir_dlease
+ num
) >
186 (unsigned long)info
->dir_in
+ info
->dir_buf_size
) {
187 pr_err("dir contents are larger than expected\n");
195 ceph_decode_need(p
, end
, sizeof(u32
)*2, bad
);
196 info
->dir_dname_len
[i
] = ceph_decode_32(p
);
197 ceph_decode_need(p
, end
, info
->dir_dname_len
[i
], bad
);
198 info
->dir_dname
[i
] = *p
;
199 *p
+= info
->dir_dname_len
[i
];
200 dout("parsed dir dname '%.*s'\n", info
->dir_dname_len
[i
],
202 info
->dir_dlease
[i
] = *p
;
203 *p
+= sizeof(struct ceph_mds_reply_lease
);
206 err
= parse_reply_info_in(p
, end
, &info
->dir_in
[i
], features
);
221 pr_err("problem parsing dir contents %d\n", err
);
226 * parse fcntl F_GETLK results
228 static int parse_reply_info_filelock(void **p
, void *end
,
229 struct ceph_mds_reply_info_parsed
*info
,
232 if (*p
+ sizeof(*info
->filelock_reply
) > end
)
235 info
->filelock_reply
= *p
;
236 *p
+= sizeof(*info
->filelock_reply
);
238 if (unlikely(*p
!= end
))
247 * parse create results
249 static int parse_reply_info_create(void **p
, void *end
,
250 struct ceph_mds_reply_info_parsed
*info
,
253 if (features
& CEPH_FEATURE_REPLY_CREATE_INODE
) {
255 info
->has_create_ino
= false;
257 info
->has_create_ino
= true;
258 info
->ino
= ceph_decode_64(p
);
262 if (unlikely(*p
!= end
))
271 * parse extra results
273 static int parse_reply_info_extra(void **p
, void *end
,
274 struct ceph_mds_reply_info_parsed
*info
,
277 u32 op
= le32_to_cpu(info
->head
->op
);
279 if (op
== CEPH_MDS_OP_GETFILELOCK
)
280 return parse_reply_info_filelock(p
, end
, info
, features
);
281 else if (op
== CEPH_MDS_OP_READDIR
|| op
== CEPH_MDS_OP_LSSNAP
)
282 return parse_reply_info_dir(p
, end
, info
, features
);
283 else if (op
== CEPH_MDS_OP_CREATE
)
284 return parse_reply_info_create(p
, end
, info
, features
);
290 * parse entire mds reply
292 static int parse_reply_info(struct ceph_msg
*msg
,
293 struct ceph_mds_reply_info_parsed
*info
,
300 info
->head
= msg
->front
.iov_base
;
301 p
= msg
->front
.iov_base
+ sizeof(struct ceph_mds_reply_head
);
302 end
= p
+ msg
->front
.iov_len
- sizeof(struct ceph_mds_reply_head
);
305 ceph_decode_32_safe(&p
, end
, len
, bad
);
307 ceph_decode_need(&p
, end
, len
, bad
);
308 err
= parse_reply_info_trace(&p
, p
+len
, info
, features
);
314 ceph_decode_32_safe(&p
, end
, len
, bad
);
316 ceph_decode_need(&p
, end
, len
, bad
);
317 err
= parse_reply_info_extra(&p
, p
+len
, info
, features
);
323 ceph_decode_32_safe(&p
, end
, len
, bad
);
324 info
->snapblob_len
= len
;
335 pr_err("mds parse_reply err %d\n", err
);
339 static void destroy_reply_info(struct ceph_mds_reply_info_parsed
*info
)
343 free_pages((unsigned long)info
->dir_in
, get_order(info
->dir_buf_size
));
350 const char *ceph_session_state_name(int s
)
353 case CEPH_MDS_SESSION_NEW
: return "new";
354 case CEPH_MDS_SESSION_OPENING
: return "opening";
355 case CEPH_MDS_SESSION_OPEN
: return "open";
356 case CEPH_MDS_SESSION_HUNG
: return "hung";
357 case CEPH_MDS_SESSION_CLOSING
: return "closing";
358 case CEPH_MDS_SESSION_RESTARTING
: return "restarting";
359 case CEPH_MDS_SESSION_RECONNECTING
: return "reconnecting";
360 default: return "???";
364 static struct ceph_mds_session
*get_session(struct ceph_mds_session
*s
)
366 if (atomic_inc_not_zero(&s
->s_ref
)) {
367 dout("mdsc get_session %p %d -> %d\n", s
,
368 atomic_read(&s
->s_ref
)-1, atomic_read(&s
->s_ref
));
371 dout("mdsc get_session %p 0 -- FAIL", s
);
376 void ceph_put_mds_session(struct ceph_mds_session
*s
)
378 dout("mdsc put_session %p %d -> %d\n", s
,
379 atomic_read(&s
->s_ref
), atomic_read(&s
->s_ref
)-1);
380 if (atomic_dec_and_test(&s
->s_ref
)) {
381 if (s
->s_auth
.authorizer
)
382 ceph_auth_destroy_authorizer(
383 s
->s_mdsc
->fsc
->client
->monc
.auth
,
384 s
->s_auth
.authorizer
);
390 * called under mdsc->mutex
392 struct ceph_mds_session
*__ceph_lookup_mds_session(struct ceph_mds_client
*mdsc
,
395 struct ceph_mds_session
*session
;
397 if (mds
>= mdsc
->max_sessions
|| mdsc
->sessions
[mds
] == NULL
)
399 session
= mdsc
->sessions
[mds
];
400 dout("lookup_mds_session %p %d\n", session
,
401 atomic_read(&session
->s_ref
));
402 get_session(session
);
406 static bool __have_session(struct ceph_mds_client
*mdsc
, int mds
)
408 if (mds
>= mdsc
->max_sessions
)
410 return mdsc
->sessions
[mds
];
413 static int __verify_registered_session(struct ceph_mds_client
*mdsc
,
414 struct ceph_mds_session
*s
)
416 if (s
->s_mds
>= mdsc
->max_sessions
||
417 mdsc
->sessions
[s
->s_mds
] != s
)
423 * create+register a new session for given mds.
424 * called under mdsc->mutex.
426 static struct ceph_mds_session
*register_session(struct ceph_mds_client
*mdsc
,
429 struct ceph_mds_session
*s
;
431 if (mds
>= mdsc
->mdsmap
->m_max_mds
)
432 return ERR_PTR(-EINVAL
);
434 s
= kzalloc(sizeof(*s
), GFP_NOFS
);
436 return ERR_PTR(-ENOMEM
);
439 s
->s_state
= CEPH_MDS_SESSION_NEW
;
442 mutex_init(&s
->s_mutex
);
444 ceph_con_init(&s
->s_con
, s
, &mds_con_ops
, &mdsc
->fsc
->client
->msgr
);
446 spin_lock_init(&s
->s_gen_ttl_lock
);
448 s
->s_cap_ttl
= jiffies
- 1;
450 spin_lock_init(&s
->s_cap_lock
);
451 s
->s_renew_requested
= 0;
453 INIT_LIST_HEAD(&s
->s_caps
);
456 atomic_set(&s
->s_ref
, 1);
457 INIT_LIST_HEAD(&s
->s_waiting
);
458 INIT_LIST_HEAD(&s
->s_unsafe
);
459 s
->s_num_cap_releases
= 0;
460 s
->s_cap_reconnect
= 0;
461 s
->s_cap_iterator
= NULL
;
462 INIT_LIST_HEAD(&s
->s_cap_releases
);
463 INIT_LIST_HEAD(&s
->s_cap_flushing
);
464 INIT_LIST_HEAD(&s
->s_cap_snaps_flushing
);
466 dout("register_session mds%d\n", mds
);
467 if (mds
>= mdsc
->max_sessions
) {
468 int newmax
= 1 << get_count_order(mds
+1);
469 struct ceph_mds_session
**sa
;
471 dout("register_session realloc to %d\n", newmax
);
472 sa
= kcalloc(newmax
, sizeof(void *), GFP_NOFS
);
475 if (mdsc
->sessions
) {
476 memcpy(sa
, mdsc
->sessions
,
477 mdsc
->max_sessions
* sizeof(void *));
478 kfree(mdsc
->sessions
);
481 mdsc
->max_sessions
= newmax
;
483 mdsc
->sessions
[mds
] = s
;
484 atomic_inc(&mdsc
->num_sessions
);
485 atomic_inc(&s
->s_ref
); /* one ref to sessions[], one to caller */
487 ceph_con_open(&s
->s_con
, CEPH_ENTITY_TYPE_MDS
, mds
,
488 ceph_mdsmap_get_addr(mdsc
->mdsmap
, mds
));
494 return ERR_PTR(-ENOMEM
);
498 * called under mdsc->mutex
500 static void __unregister_session(struct ceph_mds_client
*mdsc
,
501 struct ceph_mds_session
*s
)
503 dout("__unregister_session mds%d %p\n", s
->s_mds
, s
);
504 BUG_ON(mdsc
->sessions
[s
->s_mds
] != s
);
505 mdsc
->sessions
[s
->s_mds
] = NULL
;
506 ceph_con_close(&s
->s_con
);
507 ceph_put_mds_session(s
);
508 atomic_dec(&mdsc
->num_sessions
);
512 * drop session refs in request.
514 * should be last request ref, or hold mdsc->mutex
516 static void put_request_session(struct ceph_mds_request
*req
)
518 if (req
->r_session
) {
519 ceph_put_mds_session(req
->r_session
);
520 req
->r_session
= NULL
;
524 void ceph_mdsc_release_request(struct kref
*kref
)
526 struct ceph_mds_request
*req
= container_of(kref
,
527 struct ceph_mds_request
,
529 destroy_reply_info(&req
->r_reply_info
);
531 ceph_msg_put(req
->r_request
);
533 ceph_msg_put(req
->r_reply
);
535 ceph_put_cap_refs(ceph_inode(req
->r_inode
), CEPH_CAP_PIN
);
538 if (req
->r_locked_dir
)
539 ceph_put_cap_refs(ceph_inode(req
->r_locked_dir
), CEPH_CAP_PIN
);
540 iput(req
->r_target_inode
);
543 if (req
->r_old_dentry
)
544 dput(req
->r_old_dentry
);
545 if (req
->r_old_dentry_dir
) {
547 * track (and drop pins for) r_old_dentry_dir
548 * separately, since r_old_dentry's d_parent may have
549 * changed between the dir mutex being dropped and
550 * this request being freed.
552 ceph_put_cap_refs(ceph_inode(req
->r_old_dentry_dir
),
554 iput(req
->r_old_dentry_dir
);
559 ceph_pagelist_release(req
->r_pagelist
);
560 put_request_session(req
);
561 ceph_unreserve_caps(req
->r_mdsc
, &req
->r_caps_reservation
);
566 * lookup session, bump ref if found.
568 * called under mdsc->mutex.
570 static struct ceph_mds_request
*__lookup_request(struct ceph_mds_client
*mdsc
,
573 struct ceph_mds_request
*req
;
574 struct rb_node
*n
= mdsc
->request_tree
.rb_node
;
577 req
= rb_entry(n
, struct ceph_mds_request
, r_node
);
578 if (tid
< req
->r_tid
)
580 else if (tid
> req
->r_tid
)
583 ceph_mdsc_get_request(req
);
590 static void __insert_request(struct ceph_mds_client
*mdsc
,
591 struct ceph_mds_request
*new)
593 struct rb_node
**p
= &mdsc
->request_tree
.rb_node
;
594 struct rb_node
*parent
= NULL
;
595 struct ceph_mds_request
*req
= NULL
;
599 req
= rb_entry(parent
, struct ceph_mds_request
, r_node
);
600 if (new->r_tid
< req
->r_tid
)
602 else if (new->r_tid
> req
->r_tid
)
608 rb_link_node(&new->r_node
, parent
, p
);
609 rb_insert_color(&new->r_node
, &mdsc
->request_tree
);
613 * Register an in-flight request, and assign a tid. Link to directory
614 * are modifying (if any).
616 * Called under mdsc->mutex.
618 static void __register_request(struct ceph_mds_client
*mdsc
,
619 struct ceph_mds_request
*req
,
622 req
->r_tid
= ++mdsc
->last_tid
;
624 ceph_reserve_caps(mdsc
, &req
->r_caps_reservation
,
626 dout("__register_request %p tid %lld\n", req
, req
->r_tid
);
627 ceph_mdsc_get_request(req
);
628 __insert_request(mdsc
, req
);
630 req
->r_uid
= current_fsuid();
631 req
->r_gid
= current_fsgid();
633 if (mdsc
->oldest_tid
== 0 && req
->r_op
!= CEPH_MDS_OP_SETFILELOCK
)
634 mdsc
->oldest_tid
= req
->r_tid
;
638 req
->r_unsafe_dir
= dir
;
642 static void __unregister_request(struct ceph_mds_client
*mdsc
,
643 struct ceph_mds_request
*req
)
645 dout("__unregister_request %p tid %lld\n", req
, req
->r_tid
);
647 /* Never leave an unregistered request on an unsafe list! */
648 list_del_init(&req
->r_unsafe_item
);
650 if (req
->r_tid
== mdsc
->oldest_tid
) {
651 struct rb_node
*p
= rb_next(&req
->r_node
);
652 mdsc
->oldest_tid
= 0;
654 struct ceph_mds_request
*next_req
=
655 rb_entry(p
, struct ceph_mds_request
, r_node
);
656 if (next_req
->r_op
!= CEPH_MDS_OP_SETFILELOCK
) {
657 mdsc
->oldest_tid
= next_req
->r_tid
;
664 rb_erase(&req
->r_node
, &mdsc
->request_tree
);
665 RB_CLEAR_NODE(&req
->r_node
);
667 if (req
->r_unsafe_dir
&& req
->r_got_unsafe
) {
668 struct ceph_inode_info
*ci
= ceph_inode(req
->r_unsafe_dir
);
669 spin_lock(&ci
->i_unsafe_lock
);
670 list_del_init(&req
->r_unsafe_dir_item
);
671 spin_unlock(&ci
->i_unsafe_lock
);
673 if (req
->r_target_inode
&& req
->r_got_unsafe
) {
674 struct ceph_inode_info
*ci
= ceph_inode(req
->r_target_inode
);
675 spin_lock(&ci
->i_unsafe_lock
);
676 list_del_init(&req
->r_unsafe_target_item
);
677 spin_unlock(&ci
->i_unsafe_lock
);
680 if (req
->r_unsafe_dir
) {
681 iput(req
->r_unsafe_dir
);
682 req
->r_unsafe_dir
= NULL
;
685 complete_all(&req
->r_safe_completion
);
687 ceph_mdsc_put_request(req
);
691 * Choose mds to send request to next. If there is a hint set in the
692 * request (e.g., due to a prior forward hint from the mds), use that.
693 * Otherwise, consult frag tree and/or caps to identify the
694 * appropriate mds. If all else fails, choose randomly.
696 * Called under mdsc->mutex.
698 static struct dentry
*get_nonsnap_parent(struct dentry
*dentry
)
701 * we don't need to worry about protecting the d_parent access
702 * here because we never renaming inside the snapped namespace
703 * except to resplice to another snapdir, and either the old or new
704 * result is a valid result.
706 while (!IS_ROOT(dentry
) && ceph_snap(d_inode(dentry
)) != CEPH_NOSNAP
)
707 dentry
= dentry
->d_parent
;
711 static int __choose_mds(struct ceph_mds_client
*mdsc
,
712 struct ceph_mds_request
*req
)
715 struct ceph_inode_info
*ci
;
716 struct ceph_cap
*cap
;
717 int mode
= req
->r_direct_mode
;
719 u32 hash
= req
->r_direct_hash
;
720 bool is_hash
= req
->r_direct_is_hash
;
723 * is there a specific mds we should try? ignore hint if we have
724 * no session and the mds is not up (active or recovering).
726 if (req
->r_resend_mds
>= 0 &&
727 (__have_session(mdsc
, req
->r_resend_mds
) ||
728 ceph_mdsmap_get_state(mdsc
->mdsmap
, req
->r_resend_mds
) > 0)) {
729 dout("choose_mds using resend_mds mds%d\n",
731 return req
->r_resend_mds
;
734 if (mode
== USE_RANDOM_MDS
)
739 inode
= req
->r_inode
;
740 } else if (req
->r_dentry
) {
741 /* ignore race with rename; old or new d_parent is okay */
742 struct dentry
*parent
= req
->r_dentry
->d_parent
;
743 struct inode
*dir
= d_inode(parent
);
745 if (dir
->i_sb
!= mdsc
->fsc
->sb
) {
747 inode
= d_inode(req
->r_dentry
);
748 } else if (ceph_snap(dir
) != CEPH_NOSNAP
) {
749 /* direct snapped/virtual snapdir requests
750 * based on parent dir inode */
751 struct dentry
*dn
= get_nonsnap_parent(parent
);
753 dout("__choose_mds using nonsnap parent %p\n", inode
);
756 inode
= d_inode(req
->r_dentry
);
757 if (!inode
|| mode
== USE_AUTH_MDS
) {
760 hash
= ceph_dentry_hash(dir
, req
->r_dentry
);
766 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode
, (int)is_hash
,
770 ci
= ceph_inode(inode
);
772 if (is_hash
&& S_ISDIR(inode
->i_mode
)) {
773 struct ceph_inode_frag frag
;
776 ceph_choose_frag(ci
, hash
, &frag
, &found
);
778 if (mode
== USE_ANY_MDS
&& frag
.ndist
> 0) {
781 /* choose a random replica */
782 get_random_bytes(&r
, 1);
785 dout("choose_mds %p %llx.%llx "
786 "frag %u mds%d (%d/%d)\n",
787 inode
, ceph_vinop(inode
),
790 if (ceph_mdsmap_get_state(mdsc
->mdsmap
, mds
) >=
791 CEPH_MDS_STATE_ACTIVE
)
795 /* since this file/dir wasn't known to be
796 * replicated, then we want to look for the
797 * authoritative mds. */
800 /* choose auth mds */
802 dout("choose_mds %p %llx.%llx "
803 "frag %u mds%d (auth)\n",
804 inode
, ceph_vinop(inode
), frag
.frag
, mds
);
805 if (ceph_mdsmap_get_state(mdsc
->mdsmap
, mds
) >=
806 CEPH_MDS_STATE_ACTIVE
)
812 spin_lock(&ci
->i_ceph_lock
);
814 if (mode
== USE_AUTH_MDS
)
815 cap
= ci
->i_auth_cap
;
816 if (!cap
&& !RB_EMPTY_ROOT(&ci
->i_caps
))
817 cap
= rb_entry(rb_first(&ci
->i_caps
), struct ceph_cap
, ci_node
);
819 spin_unlock(&ci
->i_ceph_lock
);
822 mds
= cap
->session
->s_mds
;
823 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
824 inode
, ceph_vinop(inode
), mds
,
825 cap
== ci
->i_auth_cap
? "auth " : "", cap
);
826 spin_unlock(&ci
->i_ceph_lock
);
830 mds
= ceph_mdsmap_get_random_mds(mdsc
->mdsmap
);
831 dout("choose_mds chose random mds%d\n", mds
);
839 static struct ceph_msg
*create_session_msg(u32 op
, u64 seq
)
841 struct ceph_msg
*msg
;
842 struct ceph_mds_session_head
*h
;
844 msg
= ceph_msg_new(CEPH_MSG_CLIENT_SESSION
, sizeof(*h
), GFP_NOFS
,
847 pr_err("create_session_msg ENOMEM creating msg\n");
850 h
= msg
->front
.iov_base
;
851 h
->op
= cpu_to_le32(op
);
852 h
->seq
= cpu_to_le64(seq
);
858 * session message, specialization for CEPH_SESSION_REQUEST_OPEN
859 * to include additional client metadata fields.
861 static struct ceph_msg
*create_session_open_msg(struct ceph_mds_client
*mdsc
, u64 seq
)
863 struct ceph_msg
*msg
;
864 struct ceph_mds_session_head
*h
;
866 int metadata_bytes
= 0;
867 int metadata_key_count
= 0;
868 struct ceph_options
*opt
= mdsc
->fsc
->client
->options
;
871 const char* metadata
[][2] = {
872 {"hostname", utsname()->nodename
},
873 {"kernel_version", utsname()->release
},
874 {"entity_id", opt
->name
? opt
->name
: ""},
878 /* Calculate serialized length of metadata */
879 metadata_bytes
= 4; /* map length */
880 for (i
= 0; metadata
[i
][0] != NULL
; ++i
) {
881 metadata_bytes
+= 8 + strlen(metadata
[i
][0]) +
882 strlen(metadata
[i
][1]);
883 metadata_key_count
++;
886 /* Allocate the message */
887 msg
= ceph_msg_new(CEPH_MSG_CLIENT_SESSION
, sizeof(*h
) + metadata_bytes
,
890 pr_err("create_session_msg ENOMEM creating msg\n");
893 h
= msg
->front
.iov_base
;
894 h
->op
= cpu_to_le32(CEPH_SESSION_REQUEST_OPEN
);
895 h
->seq
= cpu_to_le64(seq
);
898 * Serialize client metadata into waiting buffer space, using
899 * the format that userspace expects for map<string, string>
901 * ClientSession messages with metadata are v2
903 msg
->hdr
.version
= cpu_to_le16(2);
904 msg
->hdr
.compat_version
= cpu_to_le16(1);
906 /* The write pointer, following the session_head structure */
907 p
= msg
->front
.iov_base
+ sizeof(*h
);
909 /* Number of entries in the map */
910 ceph_encode_32(&p
, metadata_key_count
);
912 /* Two length-prefixed strings for each entry in the map */
913 for (i
= 0; metadata
[i
][0] != NULL
; ++i
) {
914 size_t const key_len
= strlen(metadata
[i
][0]);
915 size_t const val_len
= strlen(metadata
[i
][1]);
917 ceph_encode_32(&p
, key_len
);
918 memcpy(p
, metadata
[i
][0], key_len
);
920 ceph_encode_32(&p
, val_len
);
921 memcpy(p
, metadata
[i
][1], val_len
);
929 * send session open request.
931 * called under mdsc->mutex
933 static int __open_session(struct ceph_mds_client
*mdsc
,
934 struct ceph_mds_session
*session
)
936 struct ceph_msg
*msg
;
938 int mds
= session
->s_mds
;
940 /* wait for mds to go active? */
941 mstate
= ceph_mdsmap_get_state(mdsc
->mdsmap
, mds
);
942 dout("open_session to mds%d (%s)\n", mds
,
943 ceph_mds_state_name(mstate
));
944 session
->s_state
= CEPH_MDS_SESSION_OPENING
;
945 session
->s_renew_requested
= jiffies
;
947 /* send connect message */
948 msg
= create_session_open_msg(mdsc
, session
->s_seq
);
951 ceph_con_send(&session
->s_con
, msg
);
956 * open sessions for any export targets for the given mds
958 * called under mdsc->mutex
960 static struct ceph_mds_session
*
961 __open_export_target_session(struct ceph_mds_client
*mdsc
, int target
)
963 struct ceph_mds_session
*session
;
965 session
= __ceph_lookup_mds_session(mdsc
, target
);
967 session
= register_session(mdsc
, target
);
971 if (session
->s_state
== CEPH_MDS_SESSION_NEW
||
972 session
->s_state
== CEPH_MDS_SESSION_CLOSING
)
973 __open_session(mdsc
, session
);
978 struct ceph_mds_session
*
979 ceph_mdsc_open_export_target_session(struct ceph_mds_client
*mdsc
, int target
)
981 struct ceph_mds_session
*session
;
983 dout("open_export_target_session to mds%d\n", target
);
985 mutex_lock(&mdsc
->mutex
);
986 session
= __open_export_target_session(mdsc
, target
);
987 mutex_unlock(&mdsc
->mutex
);
992 static void __open_export_target_sessions(struct ceph_mds_client
*mdsc
,
993 struct ceph_mds_session
*session
)
995 struct ceph_mds_info
*mi
;
996 struct ceph_mds_session
*ts
;
997 int i
, mds
= session
->s_mds
;
999 if (mds
>= mdsc
->mdsmap
->m_max_mds
)
1002 mi
= &mdsc
->mdsmap
->m_info
[mds
];
1003 dout("open_export_target_sessions for mds%d (%d targets)\n",
1004 session
->s_mds
, mi
->num_export_targets
);
1006 for (i
= 0; i
< mi
->num_export_targets
; i
++) {
1007 ts
= __open_export_target_session(mdsc
, mi
->export_targets
[i
]);
1009 ceph_put_mds_session(ts
);
1013 void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client
*mdsc
,
1014 struct ceph_mds_session
*session
)
1016 mutex_lock(&mdsc
->mutex
);
1017 __open_export_target_sessions(mdsc
, session
);
1018 mutex_unlock(&mdsc
->mutex
);
1025 /* caller holds s_cap_lock, we drop it */
1026 static void cleanup_cap_releases(struct ceph_mds_client
*mdsc
,
1027 struct ceph_mds_session
*session
)
1028 __releases(session
->s_cap_lock
)
1030 LIST_HEAD(tmp_list
);
1031 list_splice_init(&session
->s_cap_releases
, &tmp_list
);
1032 session
->s_num_cap_releases
= 0;
1033 spin_unlock(&session
->s_cap_lock
);
1035 dout("cleanup_cap_releases mds%d\n", session
->s_mds
);
1036 while (!list_empty(&tmp_list
)) {
1037 struct ceph_cap
*cap
;
1038 /* zero out the in-progress message */
1039 cap
= list_first_entry(&tmp_list
,
1040 struct ceph_cap
, session_caps
);
1041 list_del(&cap
->session_caps
);
1042 ceph_put_cap(mdsc
, cap
);
1046 static void cleanup_session_requests(struct ceph_mds_client
*mdsc
,
1047 struct ceph_mds_session
*session
)
1049 struct ceph_mds_request
*req
;
1052 dout("cleanup_session_requests mds%d\n", session
->s_mds
);
1053 mutex_lock(&mdsc
->mutex
);
1054 while (!list_empty(&session
->s_unsafe
)) {
1055 req
= list_first_entry(&session
->s_unsafe
,
1056 struct ceph_mds_request
, r_unsafe_item
);
1057 pr_warn_ratelimited(" dropping unsafe request %llu\n",
1059 __unregister_request(mdsc
, req
);
1061 /* zero r_attempts, so kick_requests() will re-send requests */
1062 p
= rb_first(&mdsc
->request_tree
);
1064 req
= rb_entry(p
, struct ceph_mds_request
, r_node
);
1066 if (req
->r_session
&&
1067 req
->r_session
->s_mds
== session
->s_mds
)
1068 req
->r_attempts
= 0;
1070 mutex_unlock(&mdsc
->mutex
);
1074 * Helper to safely iterate over all caps associated with a session, with
1075 * special care taken to handle a racing __ceph_remove_cap().
1077 * Caller must hold session s_mutex.
1079 static int iterate_session_caps(struct ceph_mds_session
*session
,
1080 int (*cb
)(struct inode
*, struct ceph_cap
*,
1083 struct list_head
*p
;
1084 struct ceph_cap
*cap
;
1085 struct inode
*inode
, *last_inode
= NULL
;
1086 struct ceph_cap
*old_cap
= NULL
;
1089 dout("iterate_session_caps %p mds%d\n", session
, session
->s_mds
);
1090 spin_lock(&session
->s_cap_lock
);
1091 p
= session
->s_caps
.next
;
1092 while (p
!= &session
->s_caps
) {
1093 cap
= list_entry(p
, struct ceph_cap
, session_caps
);
1094 inode
= igrab(&cap
->ci
->vfs_inode
);
1099 session
->s_cap_iterator
= cap
;
1100 spin_unlock(&session
->s_cap_lock
);
1107 ceph_put_cap(session
->s_mdsc
, old_cap
);
1111 ret
= cb(inode
, cap
, arg
);
1114 spin_lock(&session
->s_cap_lock
);
1116 if (cap
->ci
== NULL
) {
1117 dout("iterate_session_caps finishing cap %p removal\n",
1119 BUG_ON(cap
->session
!= session
);
1120 cap
->session
= NULL
;
1121 list_del_init(&cap
->session_caps
);
1122 session
->s_nr_caps
--;
1123 if (cap
->queue_release
) {
1124 list_add_tail(&cap
->session_caps
,
1125 &session
->s_cap_releases
);
1126 session
->s_num_cap_releases
++;
1128 old_cap
= cap
; /* put_cap it w/o locks held */
1136 session
->s_cap_iterator
= NULL
;
1137 spin_unlock(&session
->s_cap_lock
);
1141 ceph_put_cap(session
->s_mdsc
, old_cap
);
1146 static int remove_session_caps_cb(struct inode
*inode
, struct ceph_cap
*cap
,
1149 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1150 LIST_HEAD(to_remove
);
1153 dout("removing cap %p, ci is %p, inode is %p\n",
1154 cap
, ci
, &ci
->vfs_inode
);
1155 spin_lock(&ci
->i_ceph_lock
);
1156 __ceph_remove_cap(cap
, false);
1157 if (!ci
->i_auth_cap
) {
1158 struct ceph_cap_flush
*cf
;
1159 struct ceph_mds_client
*mdsc
=
1160 ceph_sb_to_client(inode
->i_sb
)->mdsc
;
1163 struct rb_node
*n
= rb_first(&ci
->i_cap_flush_tree
);
1166 cf
= rb_entry(n
, struct ceph_cap_flush
, i_node
);
1167 rb_erase(&cf
->i_node
, &ci
->i_cap_flush_tree
);
1168 list_add(&cf
->list
, &to_remove
);
1171 spin_lock(&mdsc
->cap_dirty_lock
);
1173 list_for_each_entry(cf
, &to_remove
, list
)
1174 rb_erase(&cf
->g_node
, &mdsc
->cap_flush_tree
);
1176 if (!list_empty(&ci
->i_dirty_item
)) {
1177 pr_warn_ratelimited(
1178 " dropping dirty %s state for %p %lld\n",
1179 ceph_cap_string(ci
->i_dirty_caps
),
1180 inode
, ceph_ino(inode
));
1181 ci
->i_dirty_caps
= 0;
1182 list_del_init(&ci
->i_dirty_item
);
1185 if (!list_empty(&ci
->i_flushing_item
)) {
1186 pr_warn_ratelimited(
1187 " dropping dirty+flushing %s state for %p %lld\n",
1188 ceph_cap_string(ci
->i_flushing_caps
),
1189 inode
, ceph_ino(inode
));
1190 ci
->i_flushing_caps
= 0;
1191 list_del_init(&ci
->i_flushing_item
);
1192 mdsc
->num_cap_flushing
--;
1195 spin_unlock(&mdsc
->cap_dirty_lock
);
1197 if (!ci
->i_dirty_caps
&& ci
->i_prealloc_cap_flush
) {
1198 list_add(&ci
->i_prealloc_cap_flush
->list
, &to_remove
);
1199 ci
->i_prealloc_cap_flush
= NULL
;
1202 spin_unlock(&ci
->i_ceph_lock
);
1203 while (!list_empty(&to_remove
)) {
1204 struct ceph_cap_flush
*cf
;
1205 cf
= list_first_entry(&to_remove
,
1206 struct ceph_cap_flush
, list
);
1207 list_del(&cf
->list
);
1208 ceph_free_cap_flush(cf
);
1216 * caller must hold session s_mutex
1218 static void remove_session_caps(struct ceph_mds_session
*session
)
1220 dout("remove_session_caps on %p\n", session
);
1221 iterate_session_caps(session
, remove_session_caps_cb
, NULL
);
1223 spin_lock(&session
->s_cap_lock
);
1224 if (session
->s_nr_caps
> 0) {
1225 struct super_block
*sb
= session
->s_mdsc
->fsc
->sb
;
1226 struct inode
*inode
;
1227 struct ceph_cap
*cap
, *prev
= NULL
;
1228 struct ceph_vino vino
;
1230 * iterate_session_caps() skips inodes that are being
1231 * deleted, we need to wait until deletions are complete.
1232 * __wait_on_freeing_inode() is designed for the job,
1233 * but it is not exported, so use lookup inode function
1236 while (!list_empty(&session
->s_caps
)) {
1237 cap
= list_entry(session
->s_caps
.next
,
1238 struct ceph_cap
, session_caps
);
1242 vino
= cap
->ci
->i_vino
;
1243 spin_unlock(&session
->s_cap_lock
);
1245 inode
= ceph_find_inode(sb
, vino
);
1248 spin_lock(&session
->s_cap_lock
);
1252 // drop cap expires and unlock s_cap_lock
1253 cleanup_cap_releases(session
->s_mdsc
, session
);
1255 BUG_ON(session
->s_nr_caps
> 0);
1256 BUG_ON(!list_empty(&session
->s_cap_flushing
));
1260 * wake up any threads waiting on this session's caps. if the cap is
1261 * old (didn't get renewed on the client reconnect), remove it now.
1263 * caller must hold s_mutex.
1265 static int wake_up_session_cb(struct inode
*inode
, struct ceph_cap
*cap
,
1268 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1270 wake_up_all(&ci
->i_cap_wq
);
1272 spin_lock(&ci
->i_ceph_lock
);
1273 ci
->i_wanted_max_size
= 0;
1274 ci
->i_requested_max_size
= 0;
1275 spin_unlock(&ci
->i_ceph_lock
);
1280 static void wake_up_session_caps(struct ceph_mds_session
*session
,
1283 dout("wake_up_session_caps %p mds%d\n", session
, session
->s_mds
);
1284 iterate_session_caps(session
, wake_up_session_cb
,
1285 (void *)(unsigned long)reconnect
);
1289 * Send periodic message to MDS renewing all currently held caps. The
1290 * ack will reset the expiration for all caps from this session.
1292 * caller holds s_mutex
1294 static int send_renew_caps(struct ceph_mds_client
*mdsc
,
1295 struct ceph_mds_session
*session
)
1297 struct ceph_msg
*msg
;
1300 if (time_after_eq(jiffies
, session
->s_cap_ttl
) &&
1301 time_after_eq(session
->s_cap_ttl
, session
->s_renew_requested
))
1302 pr_info("mds%d caps stale\n", session
->s_mds
);
1303 session
->s_renew_requested
= jiffies
;
1305 /* do not try to renew caps until a recovering mds has reconnected
1306 * with its clients. */
1307 state
= ceph_mdsmap_get_state(mdsc
->mdsmap
, session
->s_mds
);
1308 if (state
< CEPH_MDS_STATE_RECONNECT
) {
1309 dout("send_renew_caps ignoring mds%d (%s)\n",
1310 session
->s_mds
, ceph_mds_state_name(state
));
1314 dout("send_renew_caps to mds%d (%s)\n", session
->s_mds
,
1315 ceph_mds_state_name(state
));
1316 msg
= create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS
,
1317 ++session
->s_renew_seq
);
1320 ceph_con_send(&session
->s_con
, msg
);
1324 static int send_flushmsg_ack(struct ceph_mds_client
*mdsc
,
1325 struct ceph_mds_session
*session
, u64 seq
)
1327 struct ceph_msg
*msg
;
1329 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
1330 session
->s_mds
, ceph_session_state_name(session
->s_state
), seq
);
1331 msg
= create_session_msg(CEPH_SESSION_FLUSHMSG_ACK
, seq
);
1334 ceph_con_send(&session
->s_con
, msg
);
1340 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
1342 * Called under session->s_mutex
1344 static void renewed_caps(struct ceph_mds_client
*mdsc
,
1345 struct ceph_mds_session
*session
, int is_renew
)
1350 spin_lock(&session
->s_cap_lock
);
1351 was_stale
= is_renew
&& time_after_eq(jiffies
, session
->s_cap_ttl
);
1353 session
->s_cap_ttl
= session
->s_renew_requested
+
1354 mdsc
->mdsmap
->m_session_timeout
*HZ
;
1357 if (time_before(jiffies
, session
->s_cap_ttl
)) {
1358 pr_info("mds%d caps renewed\n", session
->s_mds
);
1361 pr_info("mds%d caps still stale\n", session
->s_mds
);
1364 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1365 session
->s_mds
, session
->s_cap_ttl
, was_stale
? "stale" : "fresh",
1366 time_before(jiffies
, session
->s_cap_ttl
) ? "stale" : "fresh");
1367 spin_unlock(&session
->s_cap_lock
);
1370 wake_up_session_caps(session
, 0);
1374 * send a session close request
1376 static int request_close_session(struct ceph_mds_client
*mdsc
,
1377 struct ceph_mds_session
*session
)
1379 struct ceph_msg
*msg
;
1381 dout("request_close_session mds%d state %s seq %lld\n",
1382 session
->s_mds
, ceph_session_state_name(session
->s_state
),
1384 msg
= create_session_msg(CEPH_SESSION_REQUEST_CLOSE
, session
->s_seq
);
1387 ceph_con_send(&session
->s_con
, msg
);
1392 * Called with s_mutex held.
1394 static int __close_session(struct ceph_mds_client
*mdsc
,
1395 struct ceph_mds_session
*session
)
1397 if (session
->s_state
>= CEPH_MDS_SESSION_CLOSING
)
1399 session
->s_state
= CEPH_MDS_SESSION_CLOSING
;
1400 return request_close_session(mdsc
, session
);
1403 static bool drop_negative_children(struct dentry
*dentry
)
1405 struct dentry
*child
;
1406 bool all_negative
= true;
1408 if (!d_is_dir(dentry
))
1411 spin_lock(&dentry
->d_lock
);
1412 list_for_each_entry(child
, &dentry
->d_subdirs
, d_child
) {
1413 if (d_really_is_positive(child
)) {
1414 all_negative
= false;
1418 spin_unlock(&dentry
->d_lock
);
1421 shrink_dcache_parent(dentry
);
1423 return all_negative
;
1427 * Trim old(er) caps.
1429 * Because we can't cache an inode without one or more caps, we do
1430 * this indirectly: if a cap is unused, we prune its aliases, at which
1431 * point the inode will hopefully get dropped to.
1433 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1434 * memory pressure from the MDS, though, so it needn't be perfect.
1436 static int trim_caps_cb(struct inode
*inode
, struct ceph_cap
*cap
, void *arg
)
1438 struct ceph_mds_session
*session
= arg
;
1439 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1440 int used
, wanted
, oissued
, mine
;
1442 if (session
->s_trim_caps
<= 0)
1445 spin_lock(&ci
->i_ceph_lock
);
1446 mine
= cap
->issued
| cap
->implemented
;
1447 used
= __ceph_caps_used(ci
);
1448 wanted
= __ceph_caps_file_wanted(ci
);
1449 oissued
= __ceph_caps_issued_other(ci
, cap
);
1451 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
1452 inode
, cap
, ceph_cap_string(mine
), ceph_cap_string(oissued
),
1453 ceph_cap_string(used
), ceph_cap_string(wanted
));
1454 if (cap
== ci
->i_auth_cap
) {
1455 if (ci
->i_dirty_caps
|| ci
->i_flushing_caps
||
1456 !list_empty(&ci
->i_cap_snaps
))
1458 if ((used
| wanted
) & CEPH_CAP_ANY_WR
)
1461 /* The inode has cached pages, but it's no longer used.
1462 * we can safely drop it */
1463 if (wanted
== 0 && used
== CEPH_CAP_FILE_CACHE
&&
1464 !(oissued
& CEPH_CAP_FILE_CACHE
)) {
1468 if ((used
| wanted
) & ~oissued
& mine
)
1469 goto out
; /* we need these caps */
1472 /* we aren't the only cap.. just remove us */
1473 __ceph_remove_cap(cap
, true);
1474 session
->s_trim_caps
--;
1476 struct dentry
*dentry
;
1477 /* try dropping referring dentries */
1478 spin_unlock(&ci
->i_ceph_lock
);
1479 dentry
= d_find_any_alias(inode
);
1480 if (dentry
&& drop_negative_children(dentry
)) {
1483 d_prune_aliases(inode
);
1484 count
= atomic_read(&inode
->i_count
);
1486 session
->s_trim_caps
--;
1487 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1496 spin_unlock(&ci
->i_ceph_lock
);
1501 * Trim session cap count down to some max number.
1503 static int trim_caps(struct ceph_mds_client
*mdsc
,
1504 struct ceph_mds_session
*session
,
1507 int trim_caps
= session
->s_nr_caps
- max_caps
;
1509 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1510 session
->s_mds
, session
->s_nr_caps
, max_caps
, trim_caps
);
1511 if (trim_caps
> 0) {
1512 session
->s_trim_caps
= trim_caps
;
1513 iterate_session_caps(session
, trim_caps_cb
, session
);
1514 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1515 session
->s_mds
, session
->s_nr_caps
, max_caps
,
1516 trim_caps
- session
->s_trim_caps
);
1517 session
->s_trim_caps
= 0;
1520 ceph_send_cap_releases(mdsc
, session
);
1524 static int check_capsnap_flush(struct ceph_inode_info
*ci
,
1528 spin_lock(&ci
->i_ceph_lock
);
1529 if (want_snap_seq
> 0 && !list_empty(&ci
->i_cap_snaps
)) {
1530 struct ceph_cap_snap
*capsnap
=
1531 list_first_entry(&ci
->i_cap_snaps
,
1532 struct ceph_cap_snap
, ci_item
);
1533 ret
= capsnap
->follows
>= want_snap_seq
;
1535 spin_unlock(&ci
->i_ceph_lock
);
1539 static int check_caps_flush(struct ceph_mds_client
*mdsc
,
1543 struct ceph_cap_flush
*cf
;
1546 spin_lock(&mdsc
->cap_dirty_lock
);
1547 n
= rb_first(&mdsc
->cap_flush_tree
);
1548 cf
= n
? rb_entry(n
, struct ceph_cap_flush
, g_node
) : NULL
;
1549 if (cf
&& cf
->tid
<= want_flush_tid
) {
1550 dout("check_caps_flush still flushing tid %llu <= %llu\n",
1551 cf
->tid
, want_flush_tid
);
1554 spin_unlock(&mdsc
->cap_dirty_lock
);
1559 * flush all dirty inode data to disk.
1561 * returns true if we've flushed through want_flush_tid
1563 static void wait_caps_flush(struct ceph_mds_client
*mdsc
,
1564 u64 want_flush_tid
, u64 want_snap_seq
)
1568 dout("check_caps_flush want %llu snap want %llu\n",
1569 want_flush_tid
, want_snap_seq
);
1570 mutex_lock(&mdsc
->mutex
);
1571 for (mds
= 0; mds
< mdsc
->max_sessions
; ) {
1572 struct ceph_mds_session
*session
= mdsc
->sessions
[mds
];
1573 struct inode
*inode
= NULL
;
1579 get_session(session
);
1580 mutex_unlock(&mdsc
->mutex
);
1582 mutex_lock(&session
->s_mutex
);
1583 if (!list_empty(&session
->s_cap_snaps_flushing
)) {
1584 struct ceph_cap_snap
*capsnap
=
1585 list_first_entry(&session
->s_cap_snaps_flushing
,
1586 struct ceph_cap_snap
,
1588 struct ceph_inode_info
*ci
= capsnap
->ci
;
1589 if (!check_capsnap_flush(ci
, want_snap_seq
)) {
1590 dout("check_cap_flush still flushing snap %p "
1591 "follows %lld <= %lld to mds%d\n",
1592 &ci
->vfs_inode
, capsnap
->follows
,
1593 want_snap_seq
, mds
);
1594 inode
= igrab(&ci
->vfs_inode
);
1597 mutex_unlock(&session
->s_mutex
);
1598 ceph_put_mds_session(session
);
1601 wait_event(mdsc
->cap_flushing_wq
,
1602 check_capsnap_flush(ceph_inode(inode
),
1609 mutex_lock(&mdsc
->mutex
);
1611 mutex_unlock(&mdsc
->mutex
);
1613 wait_event(mdsc
->cap_flushing_wq
,
1614 check_caps_flush(mdsc
, want_flush_tid
));
1616 dout("check_caps_flush ok, flushed thru %llu\n", want_flush_tid
);
1620 * called under s_mutex
1622 void ceph_send_cap_releases(struct ceph_mds_client
*mdsc
,
1623 struct ceph_mds_session
*session
)
1625 struct ceph_msg
*msg
= NULL
;
1626 struct ceph_mds_cap_release
*head
;
1627 struct ceph_mds_cap_item
*item
;
1628 struct ceph_cap
*cap
;
1629 LIST_HEAD(tmp_list
);
1630 int num_cap_releases
;
1632 spin_lock(&session
->s_cap_lock
);
1634 list_splice_init(&session
->s_cap_releases
, &tmp_list
);
1635 num_cap_releases
= session
->s_num_cap_releases
;
1636 session
->s_num_cap_releases
= 0;
1637 spin_unlock(&session
->s_cap_lock
);
1639 while (!list_empty(&tmp_list
)) {
1641 msg
= ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE
,
1642 PAGE_CACHE_SIZE
, GFP_NOFS
, false);
1645 head
= msg
->front
.iov_base
;
1646 head
->num
= cpu_to_le32(0);
1647 msg
->front
.iov_len
= sizeof(*head
);
1649 cap
= list_first_entry(&tmp_list
, struct ceph_cap
,
1651 list_del(&cap
->session_caps
);
1654 head
= msg
->front
.iov_base
;
1655 le32_add_cpu(&head
->num
, 1);
1656 item
= msg
->front
.iov_base
+ msg
->front
.iov_len
;
1657 item
->ino
= cpu_to_le64(cap
->cap_ino
);
1658 item
->cap_id
= cpu_to_le64(cap
->cap_id
);
1659 item
->migrate_seq
= cpu_to_le32(cap
->mseq
);
1660 item
->seq
= cpu_to_le32(cap
->issue_seq
);
1661 msg
->front
.iov_len
+= sizeof(*item
);
1663 ceph_put_cap(mdsc
, cap
);
1665 if (le32_to_cpu(head
->num
) == CEPH_CAPS_PER_RELEASE
) {
1666 msg
->hdr
.front_len
= cpu_to_le32(msg
->front
.iov_len
);
1667 dout("send_cap_releases mds%d %p\n", session
->s_mds
, msg
);
1668 ceph_con_send(&session
->s_con
, msg
);
1673 BUG_ON(num_cap_releases
!= 0);
1675 spin_lock(&session
->s_cap_lock
);
1676 if (!list_empty(&session
->s_cap_releases
))
1678 spin_unlock(&session
->s_cap_lock
);
1681 msg
->hdr
.front_len
= cpu_to_le32(msg
->front
.iov_len
);
1682 dout("send_cap_releases mds%d %p\n", session
->s_mds
, msg
);
1683 ceph_con_send(&session
->s_con
, msg
);
1687 pr_err("send_cap_releases mds%d, failed to allocate message\n",
1689 spin_lock(&session
->s_cap_lock
);
1690 list_splice(&tmp_list
, &session
->s_cap_releases
);
1691 session
->s_num_cap_releases
+= num_cap_releases
;
1692 spin_unlock(&session
->s_cap_lock
);
1699 int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request
*req
,
1702 struct ceph_inode_info
*ci
= ceph_inode(dir
);
1703 struct ceph_mds_reply_info_parsed
*rinfo
= &req
->r_reply_info
;
1704 struct ceph_mount_options
*opt
= req
->r_mdsc
->fsc
->mount_options
;
1705 size_t size
= sizeof(*rinfo
->dir_in
) + sizeof(*rinfo
->dir_dname_len
) +
1706 sizeof(*rinfo
->dir_dname
) + sizeof(*rinfo
->dir_dlease
);
1707 int order
, num_entries
;
1709 spin_lock(&ci
->i_ceph_lock
);
1710 num_entries
= ci
->i_files
+ ci
->i_subdirs
;
1711 spin_unlock(&ci
->i_ceph_lock
);
1712 num_entries
= max(num_entries
, 1);
1713 num_entries
= min(num_entries
, opt
->max_readdir
);
1715 order
= get_order(size
* num_entries
);
1716 while (order
>= 0) {
1717 rinfo
->dir_in
= (void*)__get_free_pages(GFP_KERNEL
|
1727 num_entries
= (PAGE_SIZE
<< order
) / size
;
1728 num_entries
= min(num_entries
, opt
->max_readdir
);
1730 rinfo
->dir_buf_size
= PAGE_SIZE
<< order
;
1731 req
->r_num_caps
= num_entries
+ 1;
1732 req
->r_args
.readdir
.max_entries
= cpu_to_le32(num_entries
);
1733 req
->r_args
.readdir
.max_bytes
= cpu_to_le32(opt
->max_readdir_bytes
);
1738 * Create an mds request.
1740 struct ceph_mds_request
*
1741 ceph_mdsc_create_request(struct ceph_mds_client
*mdsc
, int op
, int mode
)
1743 struct ceph_mds_request
*req
= kzalloc(sizeof(*req
), GFP_NOFS
);
1746 return ERR_PTR(-ENOMEM
);
1748 mutex_init(&req
->r_fill_mutex
);
1750 req
->r_started
= jiffies
;
1751 req
->r_resend_mds
= -1;
1752 INIT_LIST_HEAD(&req
->r_unsafe_dir_item
);
1753 INIT_LIST_HEAD(&req
->r_unsafe_target_item
);
1755 kref_init(&req
->r_kref
);
1756 INIT_LIST_HEAD(&req
->r_wait
);
1757 init_completion(&req
->r_completion
);
1758 init_completion(&req
->r_safe_completion
);
1759 INIT_LIST_HEAD(&req
->r_unsafe_item
);
1761 req
->r_stamp
= CURRENT_TIME
;
1764 req
->r_direct_mode
= mode
;
1769 * return oldest (lowest) request, tid in request tree, 0 if none.
1771 * called under mdsc->mutex.
1773 static struct ceph_mds_request
*__get_oldest_req(struct ceph_mds_client
*mdsc
)
1775 if (RB_EMPTY_ROOT(&mdsc
->request_tree
))
1777 return rb_entry(rb_first(&mdsc
->request_tree
),
1778 struct ceph_mds_request
, r_node
);
1781 static inline u64
__get_oldest_tid(struct ceph_mds_client
*mdsc
)
1783 return mdsc
->oldest_tid
;
1787 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1788 * on build_path_from_dentry in fs/cifs/dir.c.
1790 * If @stop_on_nosnap, generate path relative to the first non-snapped
1793 * Encode hidden .snap dirs as a double /, i.e.
1794 * foo/.snap/bar -> foo//bar
1796 char *ceph_mdsc_build_path(struct dentry
*dentry
, int *plen
, u64
*base
,
1799 struct dentry
*temp
;
1805 return ERR_PTR(-EINVAL
);
1809 seq
= read_seqbegin(&rename_lock
);
1811 for (temp
= dentry
; !IS_ROOT(temp
);) {
1812 struct inode
*inode
= d_inode(temp
);
1813 if (inode
&& ceph_snap(inode
) == CEPH_SNAPDIR
)
1814 len
++; /* slash only */
1815 else if (stop_on_nosnap
&& inode
&&
1816 ceph_snap(inode
) == CEPH_NOSNAP
)
1819 len
+= 1 + temp
->d_name
.len
;
1820 temp
= temp
->d_parent
;
1824 len
--; /* no leading '/' */
1826 path
= kmalloc(len
+1, GFP_NOFS
);
1828 return ERR_PTR(-ENOMEM
);
1830 path
[pos
] = 0; /* trailing null */
1832 for (temp
= dentry
; !IS_ROOT(temp
) && pos
!= 0; ) {
1833 struct inode
*inode
;
1835 spin_lock(&temp
->d_lock
);
1836 inode
= d_inode(temp
);
1837 if (inode
&& ceph_snap(inode
) == CEPH_SNAPDIR
) {
1838 dout("build_path path+%d: %p SNAPDIR\n",
1840 } else if (stop_on_nosnap
&& inode
&&
1841 ceph_snap(inode
) == CEPH_NOSNAP
) {
1842 spin_unlock(&temp
->d_lock
);
1845 pos
-= temp
->d_name
.len
;
1847 spin_unlock(&temp
->d_lock
);
1850 strncpy(path
+ pos
, temp
->d_name
.name
,
1853 spin_unlock(&temp
->d_lock
);
1856 temp
= temp
->d_parent
;
1859 if (pos
!= 0 || read_seqretry(&rename_lock
, seq
)) {
1860 pr_err("build_path did not end path lookup where "
1861 "expected, namelen is %d, pos is %d\n", len
, pos
);
1862 /* presumably this is only possible if racing with a
1863 rename of one of the parent directories (we can not
1864 lock the dentries above us to prevent this, but
1865 retrying should be harmless) */
1870 *base
= ceph_ino(d_inode(temp
));
1872 dout("build_path on %p %d built %llx '%.*s'\n",
1873 dentry
, d_count(dentry
), *base
, len
, path
);
1877 static int build_dentry_path(struct dentry
*dentry
,
1878 const char **ppath
, int *ppathlen
, u64
*pino
,
1885 dir
= d_inode_rcu(dentry
->d_parent
);
1886 if (dir
&& ceph_snap(dir
) == CEPH_NOSNAP
) {
1887 *pino
= ceph_ino(dir
);
1889 *ppath
= dentry
->d_name
.name
;
1890 *ppathlen
= dentry
->d_name
.len
;
1894 path
= ceph_mdsc_build_path(dentry
, ppathlen
, pino
, 1);
1896 return PTR_ERR(path
);
1902 static int build_inode_path(struct inode
*inode
,
1903 const char **ppath
, int *ppathlen
, u64
*pino
,
1906 struct dentry
*dentry
;
1909 if (ceph_snap(inode
) == CEPH_NOSNAP
) {
1910 *pino
= ceph_ino(inode
);
1914 dentry
= d_find_alias(inode
);
1915 path
= ceph_mdsc_build_path(dentry
, ppathlen
, pino
, 1);
1918 return PTR_ERR(path
);
1925 * request arguments may be specified via an inode *, a dentry *, or
1926 * an explicit ino+path.
1928 static int set_request_path_attr(struct inode
*rinode
, struct dentry
*rdentry
,
1929 const char *rpath
, u64 rino
,
1930 const char **ppath
, int *pathlen
,
1931 u64
*ino
, int *freepath
)
1936 r
= build_inode_path(rinode
, ppath
, pathlen
, ino
, freepath
);
1937 dout(" inode %p %llx.%llx\n", rinode
, ceph_ino(rinode
),
1939 } else if (rdentry
) {
1940 r
= build_dentry_path(rdentry
, ppath
, pathlen
, ino
, freepath
);
1941 dout(" dentry %p %llx/%.*s\n", rdentry
, *ino
, *pathlen
,
1943 } else if (rpath
|| rino
) {
1946 *pathlen
= rpath
? strlen(rpath
) : 0;
1947 dout(" path %.*s\n", *pathlen
, rpath
);
1954 * called under mdsc->mutex
1956 static struct ceph_msg
*create_request_message(struct ceph_mds_client
*mdsc
,
1957 struct ceph_mds_request
*req
,
1958 int mds
, bool drop_cap_releases
)
1960 struct ceph_msg
*msg
;
1961 struct ceph_mds_request_head
*head
;
1962 const char *path1
= NULL
;
1963 const char *path2
= NULL
;
1964 u64 ino1
= 0, ino2
= 0;
1965 int pathlen1
= 0, pathlen2
= 0;
1966 int freepath1
= 0, freepath2
= 0;
1972 ret
= set_request_path_attr(req
->r_inode
, req
->r_dentry
,
1973 req
->r_path1
, req
->r_ino1
.ino
,
1974 &path1
, &pathlen1
, &ino1
, &freepath1
);
1980 ret
= set_request_path_attr(NULL
, req
->r_old_dentry
,
1981 req
->r_path2
, req
->r_ino2
.ino
,
1982 &path2
, &pathlen2
, &ino2
, &freepath2
);
1988 len
= sizeof(*head
) +
1989 pathlen1
+ pathlen2
+ 2*(1 + sizeof(u32
) + sizeof(u64
)) +
1990 sizeof(struct ceph_timespec
);
1992 /* calculate (max) length for cap releases */
1993 len
+= sizeof(struct ceph_mds_request_release
) *
1994 (!!req
->r_inode_drop
+ !!req
->r_dentry_drop
+
1995 !!req
->r_old_inode_drop
+ !!req
->r_old_dentry_drop
);
1996 if (req
->r_dentry_drop
)
1997 len
+= req
->r_dentry
->d_name
.len
;
1998 if (req
->r_old_dentry_drop
)
1999 len
+= req
->r_old_dentry
->d_name
.len
;
2001 msg
= ceph_msg_new(CEPH_MSG_CLIENT_REQUEST
, len
, GFP_NOFS
, false);
2003 msg
= ERR_PTR(-ENOMEM
);
2007 msg
->hdr
.version
= cpu_to_le16(2);
2008 msg
->hdr
.tid
= cpu_to_le64(req
->r_tid
);
2010 head
= msg
->front
.iov_base
;
2011 p
= msg
->front
.iov_base
+ sizeof(*head
);
2012 end
= msg
->front
.iov_base
+ msg
->front
.iov_len
;
2014 head
->mdsmap_epoch
= cpu_to_le32(mdsc
->mdsmap
->m_epoch
);
2015 head
->op
= cpu_to_le32(req
->r_op
);
2016 head
->caller_uid
= cpu_to_le32(from_kuid(&init_user_ns
, req
->r_uid
));
2017 head
->caller_gid
= cpu_to_le32(from_kgid(&init_user_ns
, req
->r_gid
));
2018 head
->args
= req
->r_args
;
2020 ceph_encode_filepath(&p
, end
, ino1
, path1
);
2021 ceph_encode_filepath(&p
, end
, ino2
, path2
);
2023 /* make note of release offset, in case we need to replay */
2024 req
->r_request_release_offset
= p
- msg
->front
.iov_base
;
2028 if (req
->r_inode_drop
)
2029 releases
+= ceph_encode_inode_release(&p
,
2030 req
->r_inode
? req
->r_inode
: d_inode(req
->r_dentry
),
2031 mds
, req
->r_inode_drop
, req
->r_inode_unless
, 0);
2032 if (req
->r_dentry_drop
)
2033 releases
+= ceph_encode_dentry_release(&p
, req
->r_dentry
,
2034 mds
, req
->r_dentry_drop
, req
->r_dentry_unless
);
2035 if (req
->r_old_dentry_drop
)
2036 releases
+= ceph_encode_dentry_release(&p
, req
->r_old_dentry
,
2037 mds
, req
->r_old_dentry_drop
, req
->r_old_dentry_unless
);
2038 if (req
->r_old_inode_drop
)
2039 releases
+= ceph_encode_inode_release(&p
,
2040 d_inode(req
->r_old_dentry
),
2041 mds
, req
->r_old_inode_drop
, req
->r_old_inode_unless
, 0);
2043 if (drop_cap_releases
) {
2045 p
= msg
->front
.iov_base
+ req
->r_request_release_offset
;
2048 head
->num_releases
= cpu_to_le16(releases
);
2052 struct ceph_timespec ts
;
2053 ceph_encode_timespec(&ts
, &req
->r_stamp
);
2054 ceph_encode_copy(&p
, &ts
, sizeof(ts
));
2058 msg
->front
.iov_len
= p
- msg
->front
.iov_base
;
2059 msg
->hdr
.front_len
= cpu_to_le32(msg
->front
.iov_len
);
2061 if (req
->r_pagelist
) {
2062 struct ceph_pagelist
*pagelist
= req
->r_pagelist
;
2063 atomic_inc(&pagelist
->refcnt
);
2064 ceph_msg_data_add_pagelist(msg
, pagelist
);
2065 msg
->hdr
.data_len
= cpu_to_le32(pagelist
->length
);
2067 msg
->hdr
.data_len
= 0;
2070 msg
->hdr
.data_off
= cpu_to_le16(0);
2074 kfree((char *)path2
);
2077 kfree((char *)path1
);
2083 * called under mdsc->mutex if error, under no mutex if
2086 static void complete_request(struct ceph_mds_client
*mdsc
,
2087 struct ceph_mds_request
*req
)
2089 if (req
->r_callback
)
2090 req
->r_callback(mdsc
, req
);
2092 complete_all(&req
->r_completion
);
2096 * called under mdsc->mutex
2098 static int __prepare_send_request(struct ceph_mds_client
*mdsc
,
2099 struct ceph_mds_request
*req
,
2100 int mds
, bool drop_cap_releases
)
2102 struct ceph_mds_request_head
*rhead
;
2103 struct ceph_msg
*msg
;
2108 struct ceph_cap
*cap
=
2109 ceph_get_cap_for_mds(ceph_inode(req
->r_inode
), mds
);
2112 req
->r_sent_on_mseq
= cap
->mseq
;
2114 req
->r_sent_on_mseq
= -1;
2116 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req
,
2117 req
->r_tid
, ceph_mds_op_name(req
->r_op
), req
->r_attempts
);
2119 if (req
->r_got_unsafe
) {
2122 * Replay. Do not regenerate message (and rebuild
2123 * paths, etc.); just use the original message.
2124 * Rebuilding paths will break for renames because
2125 * d_move mangles the src name.
2127 msg
= req
->r_request
;
2128 rhead
= msg
->front
.iov_base
;
2130 flags
= le32_to_cpu(rhead
->flags
);
2131 flags
|= CEPH_MDS_FLAG_REPLAY
;
2132 rhead
->flags
= cpu_to_le32(flags
);
2134 if (req
->r_target_inode
)
2135 rhead
->ino
= cpu_to_le64(ceph_ino(req
->r_target_inode
));
2137 rhead
->num_retry
= req
->r_attempts
- 1;
2139 /* remove cap/dentry releases from message */
2140 rhead
->num_releases
= 0;
2143 p
= msg
->front
.iov_base
+ req
->r_request_release_offset
;
2145 struct ceph_timespec ts
;
2146 ceph_encode_timespec(&ts
, &req
->r_stamp
);
2147 ceph_encode_copy(&p
, &ts
, sizeof(ts
));
2150 msg
->front
.iov_len
= p
- msg
->front
.iov_base
;
2151 msg
->hdr
.front_len
= cpu_to_le32(msg
->front
.iov_len
);
2155 if (req
->r_request
) {
2156 ceph_msg_put(req
->r_request
);
2157 req
->r_request
= NULL
;
2159 msg
= create_request_message(mdsc
, req
, mds
, drop_cap_releases
);
2161 req
->r_err
= PTR_ERR(msg
);
2162 return PTR_ERR(msg
);
2164 req
->r_request
= msg
;
2166 rhead
= msg
->front
.iov_base
;
2167 rhead
->oldest_client_tid
= cpu_to_le64(__get_oldest_tid(mdsc
));
2168 if (req
->r_got_unsafe
)
2169 flags
|= CEPH_MDS_FLAG_REPLAY
;
2170 if (req
->r_locked_dir
)
2171 flags
|= CEPH_MDS_FLAG_WANT_DENTRY
;
2172 rhead
->flags
= cpu_to_le32(flags
);
2173 rhead
->num_fwd
= req
->r_num_fwd
;
2174 rhead
->num_retry
= req
->r_attempts
- 1;
2177 dout(" r_locked_dir = %p\n", req
->r_locked_dir
);
2182 * send request, or put it on the appropriate wait list.
2184 static int __do_request(struct ceph_mds_client
*mdsc
,
2185 struct ceph_mds_request
*req
)
2187 struct ceph_mds_session
*session
= NULL
;
2191 if (req
->r_err
|| req
->r_got_result
) {
2193 __unregister_request(mdsc
, req
);
2197 if (req
->r_timeout
&&
2198 time_after_eq(jiffies
, req
->r_started
+ req
->r_timeout
)) {
2199 dout("do_request timed out\n");
2203 if (ACCESS_ONCE(mdsc
->fsc
->mount_state
) == CEPH_MOUNT_SHUTDOWN
) {
2204 dout("do_request forced umount\n");
2209 put_request_session(req
);
2211 mds
= __choose_mds(mdsc
, req
);
2213 ceph_mdsmap_get_state(mdsc
->mdsmap
, mds
) < CEPH_MDS_STATE_ACTIVE
) {
2214 dout("do_request no mds or not active, waiting for map\n");
2215 list_add(&req
->r_wait
, &mdsc
->waiting_for_map
);
2219 /* get, open session */
2220 session
= __ceph_lookup_mds_session(mdsc
, mds
);
2222 session
= register_session(mdsc
, mds
);
2223 if (IS_ERR(session
)) {
2224 err
= PTR_ERR(session
);
2228 req
->r_session
= get_session(session
);
2230 dout("do_request mds%d session %p state %s\n", mds
, session
,
2231 ceph_session_state_name(session
->s_state
));
2232 if (session
->s_state
!= CEPH_MDS_SESSION_OPEN
&&
2233 session
->s_state
!= CEPH_MDS_SESSION_HUNG
) {
2234 if (session
->s_state
== CEPH_MDS_SESSION_NEW
||
2235 session
->s_state
== CEPH_MDS_SESSION_CLOSING
)
2236 __open_session(mdsc
, session
);
2237 list_add(&req
->r_wait
, &session
->s_waiting
);
2242 req
->r_resend_mds
= -1; /* forget any previous mds hint */
2244 if (req
->r_request_started
== 0) /* note request start time */
2245 req
->r_request_started
= jiffies
;
2247 err
= __prepare_send_request(mdsc
, req
, mds
, false);
2249 ceph_msg_get(req
->r_request
);
2250 ceph_con_send(&session
->s_con
, req
->r_request
);
2254 ceph_put_mds_session(session
);
2257 dout("__do_request early error %d\n", err
);
2259 complete_request(mdsc
, req
);
2260 __unregister_request(mdsc
, req
);
2267 * called under mdsc->mutex
2269 static void __wake_requests(struct ceph_mds_client
*mdsc
,
2270 struct list_head
*head
)
2272 struct ceph_mds_request
*req
;
2273 LIST_HEAD(tmp_list
);
2275 list_splice_init(head
, &tmp_list
);
2277 while (!list_empty(&tmp_list
)) {
2278 req
= list_entry(tmp_list
.next
,
2279 struct ceph_mds_request
, r_wait
);
2280 list_del_init(&req
->r_wait
);
2281 dout(" wake request %p tid %llu\n", req
, req
->r_tid
);
2282 __do_request(mdsc
, req
);
2287 * Wake up threads with requests pending for @mds, so that they can
2288 * resubmit their requests to a possibly different mds.
2290 static void kick_requests(struct ceph_mds_client
*mdsc
, int mds
)
2292 struct ceph_mds_request
*req
;
2293 struct rb_node
*p
= rb_first(&mdsc
->request_tree
);
2295 dout("kick_requests mds%d\n", mds
);
2297 req
= rb_entry(p
, struct ceph_mds_request
, r_node
);
2299 if (req
->r_got_unsafe
)
2301 if (req
->r_attempts
> 0)
2302 continue; /* only new requests */
2303 if (req
->r_session
&&
2304 req
->r_session
->s_mds
== mds
) {
2305 dout(" kicking tid %llu\n", req
->r_tid
);
2306 list_del_init(&req
->r_wait
);
2307 __do_request(mdsc
, req
);
2312 void ceph_mdsc_submit_request(struct ceph_mds_client
*mdsc
,
2313 struct ceph_mds_request
*req
)
2315 dout("submit_request on %p\n", req
);
2316 mutex_lock(&mdsc
->mutex
);
2317 __register_request(mdsc
, req
, NULL
);
2318 __do_request(mdsc
, req
);
2319 mutex_unlock(&mdsc
->mutex
);
2323 * Synchrously perform an mds request. Take care of all of the
2324 * session setup, forwarding, retry details.
2326 int ceph_mdsc_do_request(struct ceph_mds_client
*mdsc
,
2328 struct ceph_mds_request
*req
)
2332 dout("do_request on %p\n", req
);
2334 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
2336 ceph_get_cap_refs(ceph_inode(req
->r_inode
), CEPH_CAP_PIN
);
2337 if (req
->r_locked_dir
)
2338 ceph_get_cap_refs(ceph_inode(req
->r_locked_dir
), CEPH_CAP_PIN
);
2339 if (req
->r_old_dentry_dir
)
2340 ceph_get_cap_refs(ceph_inode(req
->r_old_dentry_dir
),
2344 mutex_lock(&mdsc
->mutex
);
2345 __register_request(mdsc
, req
, dir
);
2346 __do_request(mdsc
, req
);
2354 mutex_unlock(&mdsc
->mutex
);
2355 dout("do_request waiting\n");
2356 if (!req
->r_timeout
&& req
->r_wait_for_completion
) {
2357 err
= req
->r_wait_for_completion(mdsc
, req
);
2359 long timeleft
= wait_for_completion_killable_timeout(
2361 ceph_timeout_jiffies(req
->r_timeout
));
2365 err
= -EIO
; /* timed out */
2367 err
= timeleft
; /* killed */
2369 dout("do_request waited, got %d\n", err
);
2370 mutex_lock(&mdsc
->mutex
);
2372 /* only abort if we didn't race with a real reply */
2373 if (req
->r_got_result
) {
2374 err
= le32_to_cpu(req
->r_reply_info
.head
->result
);
2375 } else if (err
< 0) {
2376 dout("aborted request %lld with %d\n", req
->r_tid
, err
);
2379 * ensure we aren't running concurrently with
2380 * ceph_fill_trace or ceph_readdir_prepopulate, which
2381 * rely on locks (dir mutex) held by our caller.
2383 mutex_lock(&req
->r_fill_mutex
);
2385 req
->r_aborted
= true;
2386 mutex_unlock(&req
->r_fill_mutex
);
2388 if (req
->r_locked_dir
&&
2389 (req
->r_op
& CEPH_MDS_OP_WRITE
))
2390 ceph_invalidate_dir_request(req
);
2396 mutex_unlock(&mdsc
->mutex
);
2397 dout("do_request %p done, result %d\n", req
, err
);
2402 * Invalidate dir's completeness, dentry lease state on an aborted MDS
2403 * namespace request.
2405 void ceph_invalidate_dir_request(struct ceph_mds_request
*req
)
2407 struct inode
*inode
= req
->r_locked_dir
;
2409 dout("invalidate_dir_request %p (complete, lease(s))\n", inode
);
2411 ceph_dir_clear_complete(inode
);
2413 ceph_invalidate_dentry_lease(req
->r_dentry
);
2414 if (req
->r_old_dentry
)
2415 ceph_invalidate_dentry_lease(req
->r_old_dentry
);
2421 * We take the session mutex and parse and process the reply immediately.
2422 * This preserves the logical ordering of replies, capabilities, etc., sent
2423 * by the MDS as they are applied to our local cache.
2425 static void handle_reply(struct ceph_mds_session
*session
, struct ceph_msg
*msg
)
2427 struct ceph_mds_client
*mdsc
= session
->s_mdsc
;
2428 struct ceph_mds_request
*req
;
2429 struct ceph_mds_reply_head
*head
= msg
->front
.iov_base
;
2430 struct ceph_mds_reply_info_parsed
*rinfo
; /* parsed reply info */
2431 struct ceph_snap_realm
*realm
;
2434 int mds
= session
->s_mds
;
2436 if (msg
->front
.iov_len
< sizeof(*head
)) {
2437 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
2442 /* get request, session */
2443 tid
= le64_to_cpu(msg
->hdr
.tid
);
2444 mutex_lock(&mdsc
->mutex
);
2445 req
= __lookup_request(mdsc
, tid
);
2447 dout("handle_reply on unknown tid %llu\n", tid
);
2448 mutex_unlock(&mdsc
->mutex
);
2451 dout("handle_reply %p\n", req
);
2453 /* correct session? */
2454 if (req
->r_session
!= session
) {
2455 pr_err("mdsc_handle_reply got %llu on session mds%d"
2456 " not mds%d\n", tid
, session
->s_mds
,
2457 req
->r_session
? req
->r_session
->s_mds
: -1);
2458 mutex_unlock(&mdsc
->mutex
);
2463 if ((req
->r_got_unsafe
&& !head
->safe
) ||
2464 (req
->r_got_safe
&& head
->safe
)) {
2465 pr_warn("got a dup %s reply on %llu from mds%d\n",
2466 head
->safe
? "safe" : "unsafe", tid
, mds
);
2467 mutex_unlock(&mdsc
->mutex
);
2470 if (req
->r_got_safe
) {
2471 pr_warn("got unsafe after safe on %llu from mds%d\n",
2473 mutex_unlock(&mdsc
->mutex
);
2477 result
= le32_to_cpu(head
->result
);
2481 * if we're not talking to the authority, send to them
2482 * if the authority has changed while we weren't looking,
2483 * send to new authority
2484 * Otherwise we just have to return an ESTALE
2486 if (result
== -ESTALE
) {
2487 dout("got ESTALE on request %llu", req
->r_tid
);
2488 req
->r_resend_mds
= -1;
2489 if (req
->r_direct_mode
!= USE_AUTH_MDS
) {
2490 dout("not using auth, setting for that now");
2491 req
->r_direct_mode
= USE_AUTH_MDS
;
2492 __do_request(mdsc
, req
);
2493 mutex_unlock(&mdsc
->mutex
);
2496 int mds
= __choose_mds(mdsc
, req
);
2497 if (mds
>= 0 && mds
!= req
->r_session
->s_mds
) {
2498 dout("but auth changed, so resending");
2499 __do_request(mdsc
, req
);
2500 mutex_unlock(&mdsc
->mutex
);
2504 dout("have to return ESTALE on request %llu", req
->r_tid
);
2509 req
->r_got_safe
= true;
2510 __unregister_request(mdsc
, req
);
2512 if (req
->r_got_unsafe
) {
2514 * We already handled the unsafe response, now do the
2515 * cleanup. No need to examine the response; the MDS
2516 * doesn't include any result info in the safe
2517 * response. And even if it did, there is nothing
2518 * useful we could do with a revised return value.
2520 dout("got safe reply %llu, mds%d\n", tid
, mds
);
2522 /* last unsafe request during umount? */
2523 if (mdsc
->stopping
&& !__get_oldest_req(mdsc
))
2524 complete_all(&mdsc
->safe_umount_waiters
);
2525 mutex_unlock(&mdsc
->mutex
);
2529 req
->r_got_unsafe
= true;
2530 list_add_tail(&req
->r_unsafe_item
, &req
->r_session
->s_unsafe
);
2531 if (req
->r_unsafe_dir
) {
2532 struct ceph_inode_info
*ci
=
2533 ceph_inode(req
->r_unsafe_dir
);
2534 spin_lock(&ci
->i_unsafe_lock
);
2535 list_add_tail(&req
->r_unsafe_dir_item
,
2536 &ci
->i_unsafe_dirops
);
2537 spin_unlock(&ci
->i_unsafe_lock
);
2541 dout("handle_reply tid %lld result %d\n", tid
, result
);
2542 rinfo
= &req
->r_reply_info
;
2543 err
= parse_reply_info(msg
, rinfo
, session
->s_con
.peer_features
);
2544 mutex_unlock(&mdsc
->mutex
);
2546 mutex_lock(&session
->s_mutex
);
2548 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds
, tid
);
2555 if (rinfo
->snapblob_len
) {
2556 down_write(&mdsc
->snap_rwsem
);
2557 ceph_update_snap_trace(mdsc
, rinfo
->snapblob
,
2558 rinfo
->snapblob
+ rinfo
->snapblob_len
,
2559 le32_to_cpu(head
->op
) == CEPH_MDS_OP_RMSNAP
,
2561 downgrade_write(&mdsc
->snap_rwsem
);
2563 down_read(&mdsc
->snap_rwsem
);
2566 /* insert trace into our cache */
2567 mutex_lock(&req
->r_fill_mutex
);
2568 err
= ceph_fill_trace(mdsc
->fsc
->sb
, req
, req
->r_session
);
2570 if (result
== 0 && (req
->r_op
== CEPH_MDS_OP_READDIR
||
2571 req
->r_op
== CEPH_MDS_OP_LSSNAP
))
2572 ceph_readdir_prepopulate(req
, req
->r_session
);
2573 ceph_unreserve_caps(mdsc
, &req
->r_caps_reservation
);
2575 mutex_unlock(&req
->r_fill_mutex
);
2577 up_read(&mdsc
->snap_rwsem
);
2579 ceph_put_snap_realm(mdsc
, realm
);
2581 if (err
== 0 && req
->r_got_unsafe
&& req
->r_target_inode
) {
2582 struct ceph_inode_info
*ci
= ceph_inode(req
->r_target_inode
);
2583 spin_lock(&ci
->i_unsafe_lock
);
2584 list_add_tail(&req
->r_unsafe_target_item
, &ci
->i_unsafe_iops
);
2585 spin_unlock(&ci
->i_unsafe_lock
);
2588 mutex_lock(&mdsc
->mutex
);
2589 if (!req
->r_aborted
) {
2593 req
->r_reply
= ceph_msg_get(msg
);
2594 req
->r_got_result
= true;
2597 dout("reply arrived after request %lld was aborted\n", tid
);
2599 mutex_unlock(&mdsc
->mutex
);
2601 mutex_unlock(&session
->s_mutex
);
2603 /* kick calling process */
2604 complete_request(mdsc
, req
);
2606 ceph_mdsc_put_request(req
);
2613 * handle mds notification that our request has been forwarded.
2615 static void handle_forward(struct ceph_mds_client
*mdsc
,
2616 struct ceph_mds_session
*session
,
2617 struct ceph_msg
*msg
)
2619 struct ceph_mds_request
*req
;
2620 u64 tid
= le64_to_cpu(msg
->hdr
.tid
);
2624 void *p
= msg
->front
.iov_base
;
2625 void *end
= p
+ msg
->front
.iov_len
;
2627 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
2628 next_mds
= ceph_decode_32(&p
);
2629 fwd_seq
= ceph_decode_32(&p
);
2631 mutex_lock(&mdsc
->mutex
);
2632 req
= __lookup_request(mdsc
, tid
);
2634 dout("forward tid %llu to mds%d - req dne\n", tid
, next_mds
);
2635 goto out
; /* dup reply? */
2638 if (req
->r_aborted
) {
2639 dout("forward tid %llu aborted, unregistering\n", tid
);
2640 __unregister_request(mdsc
, req
);
2641 } else if (fwd_seq
<= req
->r_num_fwd
) {
2642 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
2643 tid
, next_mds
, req
->r_num_fwd
, fwd_seq
);
2645 /* resend. forward race not possible; mds would drop */
2646 dout("forward tid %llu to mds%d (we resend)\n", tid
, next_mds
);
2648 BUG_ON(req
->r_got_result
);
2649 req
->r_attempts
= 0;
2650 req
->r_num_fwd
= fwd_seq
;
2651 req
->r_resend_mds
= next_mds
;
2652 put_request_session(req
);
2653 __do_request(mdsc
, req
);
2655 ceph_mdsc_put_request(req
);
2657 mutex_unlock(&mdsc
->mutex
);
2661 pr_err("mdsc_handle_forward decode error err=%d\n", err
);
2665 * handle a mds session control message
2667 static void handle_session(struct ceph_mds_session
*session
,
2668 struct ceph_msg
*msg
)
2670 struct ceph_mds_client
*mdsc
= session
->s_mdsc
;
2673 int mds
= session
->s_mds
;
2674 struct ceph_mds_session_head
*h
= msg
->front
.iov_base
;
2678 if (msg
->front
.iov_len
!= sizeof(*h
))
2680 op
= le32_to_cpu(h
->op
);
2681 seq
= le64_to_cpu(h
->seq
);
2683 mutex_lock(&mdsc
->mutex
);
2684 if (op
== CEPH_SESSION_CLOSE
)
2685 __unregister_session(mdsc
, session
);
2686 /* FIXME: this ttl calculation is generous */
2687 session
->s_ttl
= jiffies
+ HZ
*mdsc
->mdsmap
->m_session_autoclose
;
2688 mutex_unlock(&mdsc
->mutex
);
2690 mutex_lock(&session
->s_mutex
);
2692 dout("handle_session mds%d %s %p state %s seq %llu\n",
2693 mds
, ceph_session_op_name(op
), session
,
2694 ceph_session_state_name(session
->s_state
), seq
);
2696 if (session
->s_state
== CEPH_MDS_SESSION_HUNG
) {
2697 session
->s_state
= CEPH_MDS_SESSION_OPEN
;
2698 pr_info("mds%d came back\n", session
->s_mds
);
2702 case CEPH_SESSION_OPEN
:
2703 if (session
->s_state
== CEPH_MDS_SESSION_RECONNECTING
)
2704 pr_info("mds%d reconnect success\n", session
->s_mds
);
2705 session
->s_state
= CEPH_MDS_SESSION_OPEN
;
2706 renewed_caps(mdsc
, session
, 0);
2709 __close_session(mdsc
, session
);
2712 case CEPH_SESSION_RENEWCAPS
:
2713 if (session
->s_renew_seq
== seq
)
2714 renewed_caps(mdsc
, session
, 1);
2717 case CEPH_SESSION_CLOSE
:
2718 if (session
->s_state
== CEPH_MDS_SESSION_RECONNECTING
)
2719 pr_info("mds%d reconnect denied\n", session
->s_mds
);
2720 cleanup_session_requests(mdsc
, session
);
2721 remove_session_caps(session
);
2722 wake
= 2; /* for good measure */
2723 wake_up_all(&mdsc
->session_close_wq
);
2726 case CEPH_SESSION_STALE
:
2727 pr_info("mds%d caps went stale, renewing\n",
2729 spin_lock(&session
->s_gen_ttl_lock
);
2730 session
->s_cap_gen
++;
2731 session
->s_cap_ttl
= jiffies
- 1;
2732 spin_unlock(&session
->s_gen_ttl_lock
);
2733 send_renew_caps(mdsc
, session
);
2736 case CEPH_SESSION_RECALL_STATE
:
2737 trim_caps(mdsc
, session
, le32_to_cpu(h
->max_caps
));
2740 case CEPH_SESSION_FLUSHMSG
:
2741 send_flushmsg_ack(mdsc
, session
, seq
);
2744 case CEPH_SESSION_FORCE_RO
:
2745 dout("force_session_readonly %p\n", session
);
2746 spin_lock(&session
->s_cap_lock
);
2747 session
->s_readonly
= true;
2748 spin_unlock(&session
->s_cap_lock
);
2749 wake_up_session_caps(session
, 0);
2753 pr_err("mdsc_handle_session bad op %d mds%d\n", op
, mds
);
2757 mutex_unlock(&session
->s_mutex
);
2759 mutex_lock(&mdsc
->mutex
);
2760 __wake_requests(mdsc
, &session
->s_waiting
);
2762 kick_requests(mdsc
, mds
);
2763 mutex_unlock(&mdsc
->mutex
);
2768 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds
,
2769 (int)msg
->front
.iov_len
);
2776 * called under session->mutex.
2778 static void replay_unsafe_requests(struct ceph_mds_client
*mdsc
,
2779 struct ceph_mds_session
*session
)
2781 struct ceph_mds_request
*req
, *nreq
;
2785 dout("replay_unsafe_requests mds%d\n", session
->s_mds
);
2787 mutex_lock(&mdsc
->mutex
);
2788 list_for_each_entry_safe(req
, nreq
, &session
->s_unsafe
, r_unsafe_item
) {
2789 err
= __prepare_send_request(mdsc
, req
, session
->s_mds
, true);
2791 ceph_msg_get(req
->r_request
);
2792 ceph_con_send(&session
->s_con
, req
->r_request
);
2797 * also re-send old requests when MDS enters reconnect stage. So that MDS
2798 * can process completed request in clientreplay stage.
2800 p
= rb_first(&mdsc
->request_tree
);
2802 req
= rb_entry(p
, struct ceph_mds_request
, r_node
);
2804 if (req
->r_got_unsafe
)
2806 if (req
->r_attempts
== 0)
2807 continue; /* only old requests */
2808 if (req
->r_session
&&
2809 req
->r_session
->s_mds
== session
->s_mds
) {
2810 err
= __prepare_send_request(mdsc
, req
,
2811 session
->s_mds
, true);
2813 ceph_msg_get(req
->r_request
);
2814 ceph_con_send(&session
->s_con
, req
->r_request
);
2818 mutex_unlock(&mdsc
->mutex
);
2822 * Encode information about a cap for a reconnect with the MDS.
2824 static int encode_caps_cb(struct inode
*inode
, struct ceph_cap
*cap
,
2828 struct ceph_mds_cap_reconnect v2
;
2829 struct ceph_mds_cap_reconnect_v1 v1
;
2832 struct ceph_inode_info
*ci
;
2833 struct ceph_reconnect_state
*recon_state
= arg
;
2834 struct ceph_pagelist
*pagelist
= recon_state
->pagelist
;
2838 struct dentry
*dentry
;
2842 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2843 inode
, ceph_vinop(inode
), cap
, cap
->cap_id
,
2844 ceph_cap_string(cap
->issued
));
2845 err
= ceph_pagelist_encode_64(pagelist
, ceph_ino(inode
));
2849 dentry
= d_find_alias(inode
);
2851 path
= ceph_mdsc_build_path(dentry
, &pathlen
, &pathbase
, 0);
2853 err
= PTR_ERR(path
);
2860 err
= ceph_pagelist_encode_string(pagelist
, path
, pathlen
);
2864 spin_lock(&ci
->i_ceph_lock
);
2865 cap
->seq
= 0; /* reset cap seq */
2866 cap
->issue_seq
= 0; /* and issue_seq */
2867 cap
->mseq
= 0; /* and migrate_seq */
2868 cap
->cap_gen
= cap
->session
->s_cap_gen
;
2870 if (recon_state
->flock
) {
2871 rec
.v2
.cap_id
= cpu_to_le64(cap
->cap_id
);
2872 rec
.v2
.wanted
= cpu_to_le32(__ceph_caps_wanted(ci
));
2873 rec
.v2
.issued
= cpu_to_le32(cap
->issued
);
2874 rec
.v2
.snaprealm
= cpu_to_le64(ci
->i_snap_realm
->ino
);
2875 rec
.v2
.pathbase
= cpu_to_le64(pathbase
);
2876 rec
.v2
.flock_len
= 0;
2877 reclen
= sizeof(rec
.v2
);
2879 rec
.v1
.cap_id
= cpu_to_le64(cap
->cap_id
);
2880 rec
.v1
.wanted
= cpu_to_le32(__ceph_caps_wanted(ci
));
2881 rec
.v1
.issued
= cpu_to_le32(cap
->issued
);
2882 rec
.v1
.size
= cpu_to_le64(inode
->i_size
);
2883 ceph_encode_timespec(&rec
.v1
.mtime
, &inode
->i_mtime
);
2884 ceph_encode_timespec(&rec
.v1
.atime
, &inode
->i_atime
);
2885 rec
.v1
.snaprealm
= cpu_to_le64(ci
->i_snap_realm
->ino
);
2886 rec
.v1
.pathbase
= cpu_to_le64(pathbase
);
2887 reclen
= sizeof(rec
.v1
);
2889 spin_unlock(&ci
->i_ceph_lock
);
2891 if (recon_state
->flock
) {
2892 int num_fcntl_locks
, num_flock_locks
;
2893 struct ceph_filelock
*flocks
;
2896 ceph_count_locks(inode
, &num_fcntl_locks
, &num_flock_locks
);
2897 flocks
= kmalloc((num_fcntl_locks
+num_flock_locks
) *
2898 sizeof(struct ceph_filelock
), GFP_NOFS
);
2903 err
= ceph_encode_locks_to_buffer(inode
, flocks
,
2913 * number of encoded locks is stable, so copy to pagelist
2915 rec
.v2
.flock_len
= cpu_to_le32(2*sizeof(u32
) +
2916 (num_fcntl_locks
+num_flock_locks
) *
2917 sizeof(struct ceph_filelock
));
2918 err
= ceph_pagelist_append(pagelist
, &rec
, reclen
);
2920 err
= ceph_locks_to_pagelist(flocks
, pagelist
,
2925 err
= ceph_pagelist_append(pagelist
, &rec
, reclen
);
2928 recon_state
->nr_caps
++;
2938 * If an MDS fails and recovers, clients need to reconnect in order to
2939 * reestablish shared state. This includes all caps issued through
2940 * this session _and_ the snap_realm hierarchy. Because it's not
2941 * clear which snap realms the mds cares about, we send everything we
2942 * know about.. that ensures we'll then get any new info the
2943 * recovering MDS might have.
2945 * This is a relatively heavyweight operation, but it's rare.
2947 * called with mdsc->mutex held.
2949 static void send_mds_reconnect(struct ceph_mds_client
*mdsc
,
2950 struct ceph_mds_session
*session
)
2952 struct ceph_msg
*reply
;
2954 int mds
= session
->s_mds
;
2957 struct ceph_pagelist
*pagelist
;
2958 struct ceph_reconnect_state recon_state
;
2960 pr_info("mds%d reconnect start\n", mds
);
2962 pagelist
= kmalloc(sizeof(*pagelist
), GFP_NOFS
);
2964 goto fail_nopagelist
;
2965 ceph_pagelist_init(pagelist
);
2967 reply
= ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT
, 0, GFP_NOFS
, false);
2971 mutex_lock(&session
->s_mutex
);
2972 session
->s_state
= CEPH_MDS_SESSION_RECONNECTING
;
2975 dout("session %p state %s\n", session
,
2976 ceph_session_state_name(session
->s_state
));
2978 spin_lock(&session
->s_gen_ttl_lock
);
2979 session
->s_cap_gen
++;
2980 spin_unlock(&session
->s_gen_ttl_lock
);
2982 spin_lock(&session
->s_cap_lock
);
2983 /* don't know if session is readonly */
2984 session
->s_readonly
= 0;
2986 * notify __ceph_remove_cap() that we are composing cap reconnect.
2987 * If a cap get released before being added to the cap reconnect,
2988 * __ceph_remove_cap() should skip queuing cap release.
2990 session
->s_cap_reconnect
= 1;
2991 /* drop old cap expires; we're about to reestablish that state */
2992 cleanup_cap_releases(mdsc
, session
);
2994 /* trim unused caps to reduce MDS's cache rejoin time */
2995 if (mdsc
->fsc
->sb
->s_root
)
2996 shrink_dcache_parent(mdsc
->fsc
->sb
->s_root
);
2998 ceph_con_close(&session
->s_con
);
2999 ceph_con_open(&session
->s_con
,
3000 CEPH_ENTITY_TYPE_MDS
, mds
,
3001 ceph_mdsmap_get_addr(mdsc
->mdsmap
, mds
));
3003 /* replay unsafe requests */
3004 replay_unsafe_requests(mdsc
, session
);
3006 down_read(&mdsc
->snap_rwsem
);
3008 /* traverse this session's caps */
3009 s_nr_caps
= session
->s_nr_caps
;
3010 err
= ceph_pagelist_encode_32(pagelist
, s_nr_caps
);
3014 recon_state
.nr_caps
= 0;
3015 recon_state
.pagelist
= pagelist
;
3016 recon_state
.flock
= session
->s_con
.peer_features
& CEPH_FEATURE_FLOCK
;
3017 err
= iterate_session_caps(session
, encode_caps_cb
, &recon_state
);
3021 spin_lock(&session
->s_cap_lock
);
3022 session
->s_cap_reconnect
= 0;
3023 spin_unlock(&session
->s_cap_lock
);
3026 * snaprealms. we provide mds with the ino, seq (version), and
3027 * parent for all of our realms. If the mds has any newer info,
3030 for (p
= rb_first(&mdsc
->snap_realms
); p
; p
= rb_next(p
)) {
3031 struct ceph_snap_realm
*realm
=
3032 rb_entry(p
, struct ceph_snap_realm
, node
);
3033 struct ceph_mds_snaprealm_reconnect sr_rec
;
3035 dout(" adding snap realm %llx seq %lld parent %llx\n",
3036 realm
->ino
, realm
->seq
, realm
->parent_ino
);
3037 sr_rec
.ino
= cpu_to_le64(realm
->ino
);
3038 sr_rec
.seq
= cpu_to_le64(realm
->seq
);
3039 sr_rec
.parent
= cpu_to_le64(realm
->parent_ino
);
3040 err
= ceph_pagelist_append(pagelist
, &sr_rec
, sizeof(sr_rec
));
3045 if (recon_state
.flock
)
3046 reply
->hdr
.version
= cpu_to_le16(2);
3048 /* raced with cap release? */
3049 if (s_nr_caps
!= recon_state
.nr_caps
) {
3050 struct page
*page
= list_first_entry(&pagelist
->head
,
3052 __le32
*addr
= kmap_atomic(page
);
3053 *addr
= cpu_to_le32(recon_state
.nr_caps
);
3054 kunmap_atomic(addr
);
3057 reply
->hdr
.data_len
= cpu_to_le32(pagelist
->length
);
3058 ceph_msg_data_add_pagelist(reply
, pagelist
);
3060 ceph_early_kick_flushing_caps(mdsc
, session
);
3062 ceph_con_send(&session
->s_con
, reply
);
3064 mutex_unlock(&session
->s_mutex
);
3066 mutex_lock(&mdsc
->mutex
);
3067 __wake_requests(mdsc
, &session
->s_waiting
);
3068 mutex_unlock(&mdsc
->mutex
);
3070 up_read(&mdsc
->snap_rwsem
);
3074 ceph_msg_put(reply
);
3075 up_read(&mdsc
->snap_rwsem
);
3076 mutex_unlock(&session
->s_mutex
);
3078 ceph_pagelist_release(pagelist
);
3080 pr_err("error %d preparing reconnect for mds%d\n", err
, mds
);
3086 * compare old and new mdsmaps, kicking requests
3087 * and closing out old connections as necessary
3089 * called under mdsc->mutex.
3091 static void check_new_map(struct ceph_mds_client
*mdsc
,
3092 struct ceph_mdsmap
*newmap
,
3093 struct ceph_mdsmap
*oldmap
)
3096 int oldstate
, newstate
;
3097 struct ceph_mds_session
*s
;
3099 dout("check_new_map new %u old %u\n",
3100 newmap
->m_epoch
, oldmap
->m_epoch
);
3102 for (i
= 0; i
< oldmap
->m_max_mds
&& i
< mdsc
->max_sessions
; i
++) {
3103 if (mdsc
->sessions
[i
] == NULL
)
3105 s
= mdsc
->sessions
[i
];
3106 oldstate
= ceph_mdsmap_get_state(oldmap
, i
);
3107 newstate
= ceph_mdsmap_get_state(newmap
, i
);
3109 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
3110 i
, ceph_mds_state_name(oldstate
),
3111 ceph_mdsmap_is_laggy(oldmap
, i
) ? " (laggy)" : "",
3112 ceph_mds_state_name(newstate
),
3113 ceph_mdsmap_is_laggy(newmap
, i
) ? " (laggy)" : "",
3114 ceph_session_state_name(s
->s_state
));
3116 if (i
>= newmap
->m_max_mds
||
3117 memcmp(ceph_mdsmap_get_addr(oldmap
, i
),
3118 ceph_mdsmap_get_addr(newmap
, i
),
3119 sizeof(struct ceph_entity_addr
))) {
3120 if (s
->s_state
== CEPH_MDS_SESSION_OPENING
) {
3121 /* the session never opened, just close it
3123 __wake_requests(mdsc
, &s
->s_waiting
);
3124 __unregister_session(mdsc
, s
);
3127 mutex_unlock(&mdsc
->mutex
);
3128 mutex_lock(&s
->s_mutex
);
3129 mutex_lock(&mdsc
->mutex
);
3130 ceph_con_close(&s
->s_con
);
3131 mutex_unlock(&s
->s_mutex
);
3132 s
->s_state
= CEPH_MDS_SESSION_RESTARTING
;
3134 } else if (oldstate
== newstate
) {
3135 continue; /* nothing new with this mds */
3141 if (s
->s_state
== CEPH_MDS_SESSION_RESTARTING
&&
3142 newstate
>= CEPH_MDS_STATE_RECONNECT
) {
3143 mutex_unlock(&mdsc
->mutex
);
3144 send_mds_reconnect(mdsc
, s
);
3145 mutex_lock(&mdsc
->mutex
);
3149 * kick request on any mds that has gone active.
3151 if (oldstate
< CEPH_MDS_STATE_ACTIVE
&&
3152 newstate
>= CEPH_MDS_STATE_ACTIVE
) {
3153 if (oldstate
!= CEPH_MDS_STATE_CREATING
&&
3154 oldstate
!= CEPH_MDS_STATE_STARTING
)
3155 pr_info("mds%d recovery completed\n", s
->s_mds
);
3156 kick_requests(mdsc
, i
);
3157 ceph_kick_flushing_caps(mdsc
, s
);
3158 wake_up_session_caps(s
, 1);
3162 for (i
= 0; i
< newmap
->m_max_mds
&& i
< mdsc
->max_sessions
; i
++) {
3163 s
= mdsc
->sessions
[i
];
3166 if (!ceph_mdsmap_is_laggy(newmap
, i
))
3168 if (s
->s_state
== CEPH_MDS_SESSION_OPEN
||
3169 s
->s_state
== CEPH_MDS_SESSION_HUNG
||
3170 s
->s_state
== CEPH_MDS_SESSION_CLOSING
) {
3171 dout(" connecting to export targets of laggy mds%d\n",
3173 __open_export_target_sessions(mdsc
, s
);
3185 * caller must hold session s_mutex, dentry->d_lock
3187 void __ceph_mdsc_drop_dentry_lease(struct dentry
*dentry
)
3189 struct ceph_dentry_info
*di
= ceph_dentry(dentry
);
3191 ceph_put_mds_session(di
->lease_session
);
3192 di
->lease_session
= NULL
;
3195 static void handle_lease(struct ceph_mds_client
*mdsc
,
3196 struct ceph_mds_session
*session
,
3197 struct ceph_msg
*msg
)
3199 struct super_block
*sb
= mdsc
->fsc
->sb
;
3200 struct inode
*inode
;
3201 struct dentry
*parent
, *dentry
;
3202 struct ceph_dentry_info
*di
;
3203 int mds
= session
->s_mds
;
3204 struct ceph_mds_lease
*h
= msg
->front
.iov_base
;
3206 struct ceph_vino vino
;
3210 dout("handle_lease from mds%d\n", mds
);
3213 if (msg
->front
.iov_len
< sizeof(*h
) + sizeof(u32
))
3215 vino
.ino
= le64_to_cpu(h
->ino
);
3216 vino
.snap
= CEPH_NOSNAP
;
3217 seq
= le32_to_cpu(h
->seq
);
3218 dname
.name
= (void *)h
+ sizeof(*h
) + sizeof(u32
);
3219 dname
.len
= msg
->front
.iov_len
- sizeof(*h
) - sizeof(u32
);
3220 if (dname
.len
!= get_unaligned_le32(h
+1))
3224 inode
= ceph_find_inode(sb
, vino
);
3225 dout("handle_lease %s, ino %llx %p %.*s\n",
3226 ceph_lease_op_name(h
->action
), vino
.ino
, inode
,
3227 dname
.len
, dname
.name
);
3229 mutex_lock(&session
->s_mutex
);
3232 if (inode
== NULL
) {
3233 dout("handle_lease no inode %llx\n", vino
.ino
);
3238 parent
= d_find_alias(inode
);
3240 dout("no parent dentry on inode %p\n", inode
);
3242 goto release
; /* hrm... */
3244 dname
.hash
= full_name_hash(dname
.name
, dname
.len
);
3245 dentry
= d_lookup(parent
, &dname
);
3250 spin_lock(&dentry
->d_lock
);
3251 di
= ceph_dentry(dentry
);
3252 switch (h
->action
) {
3253 case CEPH_MDS_LEASE_REVOKE
:
3254 if (di
->lease_session
== session
) {
3255 if (ceph_seq_cmp(di
->lease_seq
, seq
) > 0)
3256 h
->seq
= cpu_to_le32(di
->lease_seq
);
3257 __ceph_mdsc_drop_dentry_lease(dentry
);
3262 case CEPH_MDS_LEASE_RENEW
:
3263 if (di
->lease_session
== session
&&
3264 di
->lease_gen
== session
->s_cap_gen
&&
3265 di
->lease_renew_from
&&
3266 di
->lease_renew_after
== 0) {
3267 unsigned long duration
=
3268 msecs_to_jiffies(le32_to_cpu(h
->duration_ms
));
3270 di
->lease_seq
= seq
;
3271 dentry
->d_time
= di
->lease_renew_from
+ duration
;
3272 di
->lease_renew_after
= di
->lease_renew_from
+
3274 di
->lease_renew_from
= 0;
3278 spin_unlock(&dentry
->d_lock
);
3285 /* let's just reuse the same message */
3286 h
->action
= CEPH_MDS_LEASE_REVOKE_ACK
;
3288 ceph_con_send(&session
->s_con
, msg
);
3292 mutex_unlock(&session
->s_mutex
);
3296 pr_err("corrupt lease message\n");
3300 void ceph_mdsc_lease_send_msg(struct ceph_mds_session
*session
,
3301 struct inode
*inode
,
3302 struct dentry
*dentry
, char action
,
3305 struct ceph_msg
*msg
;
3306 struct ceph_mds_lease
*lease
;
3307 int len
= sizeof(*lease
) + sizeof(u32
);
3310 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
3311 inode
, dentry
, ceph_lease_op_name(action
), session
->s_mds
);
3312 dnamelen
= dentry
->d_name
.len
;
3315 msg
= ceph_msg_new(CEPH_MSG_CLIENT_LEASE
, len
, GFP_NOFS
, false);
3318 lease
= msg
->front
.iov_base
;
3319 lease
->action
= action
;
3320 lease
->ino
= cpu_to_le64(ceph_vino(inode
).ino
);
3321 lease
->first
= lease
->last
= cpu_to_le64(ceph_vino(inode
).snap
);
3322 lease
->seq
= cpu_to_le32(seq
);
3323 put_unaligned_le32(dnamelen
, lease
+ 1);
3324 memcpy((void *)(lease
+ 1) + 4, dentry
->d_name
.name
, dnamelen
);
3327 * if this is a preemptive lease RELEASE, no need to
3328 * flush request stream, since the actual request will
3331 msg
->more_to_follow
= (action
== CEPH_MDS_LEASE_RELEASE
);
3333 ceph_con_send(&session
->s_con
, msg
);
3337 * Preemptively release a lease we expect to invalidate anyway.
3338 * Pass @inode always, @dentry is optional.
3340 void ceph_mdsc_lease_release(struct ceph_mds_client
*mdsc
, struct inode
*inode
,
3341 struct dentry
*dentry
)
3343 struct ceph_dentry_info
*di
;
3344 struct ceph_mds_session
*session
;
3347 BUG_ON(inode
== NULL
);
3348 BUG_ON(dentry
== NULL
);
3350 /* is dentry lease valid? */
3351 spin_lock(&dentry
->d_lock
);
3352 di
= ceph_dentry(dentry
);
3353 if (!di
|| !di
->lease_session
||
3354 di
->lease_session
->s_mds
< 0 ||
3355 di
->lease_gen
!= di
->lease_session
->s_cap_gen
||
3356 !time_before(jiffies
, dentry
->d_time
)) {
3357 dout("lease_release inode %p dentry %p -- "
3360 spin_unlock(&dentry
->d_lock
);
3364 /* we do have a lease on this dentry; note mds and seq */
3365 session
= ceph_get_mds_session(di
->lease_session
);
3366 seq
= di
->lease_seq
;
3367 __ceph_mdsc_drop_dentry_lease(dentry
);
3368 spin_unlock(&dentry
->d_lock
);
3370 dout("lease_release inode %p dentry %p to mds%d\n",
3371 inode
, dentry
, session
->s_mds
);
3372 ceph_mdsc_lease_send_msg(session
, inode
, dentry
,
3373 CEPH_MDS_LEASE_RELEASE
, seq
);
3374 ceph_put_mds_session(session
);
3378 * drop all leases (and dentry refs) in preparation for umount
3380 static void drop_leases(struct ceph_mds_client
*mdsc
)
3384 dout("drop_leases\n");
3385 mutex_lock(&mdsc
->mutex
);
3386 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
3387 struct ceph_mds_session
*s
= __ceph_lookup_mds_session(mdsc
, i
);
3390 mutex_unlock(&mdsc
->mutex
);
3391 mutex_lock(&s
->s_mutex
);
3392 mutex_unlock(&s
->s_mutex
);
3393 ceph_put_mds_session(s
);
3394 mutex_lock(&mdsc
->mutex
);
3396 mutex_unlock(&mdsc
->mutex
);
3402 * delayed work -- periodically trim expired leases, renew caps with mds
3404 static void schedule_delayed(struct ceph_mds_client
*mdsc
)
3407 unsigned hz
= round_jiffies_relative(HZ
* delay
);
3408 schedule_delayed_work(&mdsc
->delayed_work
, hz
);
3411 static void delayed_work(struct work_struct
*work
)
3414 struct ceph_mds_client
*mdsc
=
3415 container_of(work
, struct ceph_mds_client
, delayed_work
.work
);
3419 dout("mdsc delayed_work\n");
3420 ceph_check_delayed_caps(mdsc
);
3422 mutex_lock(&mdsc
->mutex
);
3423 renew_interval
= mdsc
->mdsmap
->m_session_timeout
>> 2;
3424 renew_caps
= time_after_eq(jiffies
, HZ
*renew_interval
+
3425 mdsc
->last_renew_caps
);
3427 mdsc
->last_renew_caps
= jiffies
;
3429 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
3430 struct ceph_mds_session
*s
= __ceph_lookup_mds_session(mdsc
, i
);
3433 if (s
->s_state
== CEPH_MDS_SESSION_CLOSING
) {
3434 dout("resending session close request for mds%d\n",
3436 request_close_session(mdsc
, s
);
3437 ceph_put_mds_session(s
);
3440 if (s
->s_ttl
&& time_after(jiffies
, s
->s_ttl
)) {
3441 if (s
->s_state
== CEPH_MDS_SESSION_OPEN
) {
3442 s
->s_state
= CEPH_MDS_SESSION_HUNG
;
3443 pr_info("mds%d hung\n", s
->s_mds
);
3446 if (s
->s_state
< CEPH_MDS_SESSION_OPEN
) {
3447 /* this mds is failed or recovering, just wait */
3448 ceph_put_mds_session(s
);
3451 mutex_unlock(&mdsc
->mutex
);
3453 mutex_lock(&s
->s_mutex
);
3455 send_renew_caps(mdsc
, s
);
3457 ceph_con_keepalive(&s
->s_con
);
3458 if (s
->s_state
== CEPH_MDS_SESSION_OPEN
||
3459 s
->s_state
== CEPH_MDS_SESSION_HUNG
)
3460 ceph_send_cap_releases(mdsc
, s
);
3461 mutex_unlock(&s
->s_mutex
);
3462 ceph_put_mds_session(s
);
3464 mutex_lock(&mdsc
->mutex
);
3466 mutex_unlock(&mdsc
->mutex
);
3468 schedule_delayed(mdsc
);
3471 int ceph_mdsc_init(struct ceph_fs_client
*fsc
)
3474 struct ceph_mds_client
*mdsc
;
3476 mdsc
= kzalloc(sizeof(struct ceph_mds_client
), GFP_NOFS
);
3481 mutex_init(&mdsc
->mutex
);
3482 mdsc
->mdsmap
= kzalloc(sizeof(*mdsc
->mdsmap
), GFP_NOFS
);
3483 if (mdsc
->mdsmap
== NULL
) {
3488 init_completion(&mdsc
->safe_umount_waiters
);
3489 init_waitqueue_head(&mdsc
->session_close_wq
);
3490 INIT_LIST_HEAD(&mdsc
->waiting_for_map
);
3491 mdsc
->sessions
= NULL
;
3492 atomic_set(&mdsc
->num_sessions
, 0);
3493 mdsc
->max_sessions
= 0;
3495 mdsc
->last_snap_seq
= 0;
3496 init_rwsem(&mdsc
->snap_rwsem
);
3497 mdsc
->snap_realms
= RB_ROOT
;
3498 INIT_LIST_HEAD(&mdsc
->snap_empty
);
3499 spin_lock_init(&mdsc
->snap_empty_lock
);
3501 mdsc
->oldest_tid
= 0;
3502 mdsc
->request_tree
= RB_ROOT
;
3503 INIT_DELAYED_WORK(&mdsc
->delayed_work
, delayed_work
);
3504 mdsc
->last_renew_caps
= jiffies
;
3505 INIT_LIST_HEAD(&mdsc
->cap_delay_list
);
3506 spin_lock_init(&mdsc
->cap_delay_lock
);
3507 INIT_LIST_HEAD(&mdsc
->snap_flush_list
);
3508 spin_lock_init(&mdsc
->snap_flush_lock
);
3509 mdsc
->last_cap_flush_tid
= 1;
3510 mdsc
->cap_flush_tree
= RB_ROOT
;
3511 INIT_LIST_HEAD(&mdsc
->cap_dirty
);
3512 INIT_LIST_HEAD(&mdsc
->cap_dirty_migrating
);
3513 mdsc
->num_cap_flushing
= 0;
3514 spin_lock_init(&mdsc
->cap_dirty_lock
);
3515 init_waitqueue_head(&mdsc
->cap_flushing_wq
);
3516 spin_lock_init(&mdsc
->dentry_lru_lock
);
3517 INIT_LIST_HEAD(&mdsc
->dentry_lru
);
3519 ceph_caps_init(mdsc
);
3520 ceph_adjust_min_caps(mdsc
, fsc
->min_caps
);
3522 init_rwsem(&mdsc
->pool_perm_rwsem
);
3523 mdsc
->pool_perm_tree
= RB_ROOT
;
3529 * Wait for safe replies on open mds requests. If we time out, drop
3530 * all requests from the tree to avoid dangling dentry refs.
3532 static void wait_requests(struct ceph_mds_client
*mdsc
)
3534 struct ceph_options
*opts
= mdsc
->fsc
->client
->options
;
3535 struct ceph_mds_request
*req
;
3537 mutex_lock(&mdsc
->mutex
);
3538 if (__get_oldest_req(mdsc
)) {
3539 mutex_unlock(&mdsc
->mutex
);
3541 dout("wait_requests waiting for requests\n");
3542 wait_for_completion_timeout(&mdsc
->safe_umount_waiters
,
3543 ceph_timeout_jiffies(opts
->mount_timeout
));
3545 /* tear down remaining requests */
3546 mutex_lock(&mdsc
->mutex
);
3547 while ((req
= __get_oldest_req(mdsc
))) {
3548 dout("wait_requests timed out on tid %llu\n",
3550 __unregister_request(mdsc
, req
);
3553 mutex_unlock(&mdsc
->mutex
);
3554 dout("wait_requests done\n");
3558 * called before mount is ro, and before dentries are torn down.
3559 * (hmm, does this still race with new lookups?)
3561 void ceph_mdsc_pre_umount(struct ceph_mds_client
*mdsc
)
3563 dout("pre_umount\n");
3567 ceph_flush_dirty_caps(mdsc
);
3568 wait_requests(mdsc
);
3571 * wait for reply handlers to drop their request refs and
3572 * their inode/dcache refs
3578 * wait for all write mds requests to flush.
3580 static void wait_unsafe_requests(struct ceph_mds_client
*mdsc
, u64 want_tid
)
3582 struct ceph_mds_request
*req
= NULL
, *nextreq
;
3585 mutex_lock(&mdsc
->mutex
);
3586 dout("wait_unsafe_requests want %lld\n", want_tid
);
3588 req
= __get_oldest_req(mdsc
);
3589 while (req
&& req
->r_tid
<= want_tid
) {
3590 /* find next request */
3591 n
= rb_next(&req
->r_node
);
3593 nextreq
= rb_entry(n
, struct ceph_mds_request
, r_node
);
3596 if (req
->r_op
!= CEPH_MDS_OP_SETFILELOCK
&&
3597 (req
->r_op
& CEPH_MDS_OP_WRITE
)) {
3599 ceph_mdsc_get_request(req
);
3601 ceph_mdsc_get_request(nextreq
);
3602 mutex_unlock(&mdsc
->mutex
);
3603 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3604 req
->r_tid
, want_tid
);
3605 wait_for_completion(&req
->r_safe_completion
);
3606 mutex_lock(&mdsc
->mutex
);
3607 ceph_mdsc_put_request(req
);
3609 break; /* next dne before, so we're done! */
3610 if (RB_EMPTY_NODE(&nextreq
->r_node
)) {
3611 /* next request was removed from tree */
3612 ceph_mdsc_put_request(nextreq
);
3615 ceph_mdsc_put_request(nextreq
); /* won't go away */
3619 mutex_unlock(&mdsc
->mutex
);
3620 dout("wait_unsafe_requests done\n");
3623 void ceph_mdsc_sync(struct ceph_mds_client
*mdsc
)
3625 u64 want_tid
, want_flush
, want_snap
;
3627 if (ACCESS_ONCE(mdsc
->fsc
->mount_state
) == CEPH_MOUNT_SHUTDOWN
)
3631 mutex_lock(&mdsc
->mutex
);
3632 want_tid
= mdsc
->last_tid
;
3633 mutex_unlock(&mdsc
->mutex
);
3635 ceph_flush_dirty_caps(mdsc
);
3636 spin_lock(&mdsc
->cap_dirty_lock
);
3637 want_flush
= mdsc
->last_cap_flush_tid
;
3638 spin_unlock(&mdsc
->cap_dirty_lock
);
3640 down_read(&mdsc
->snap_rwsem
);
3641 want_snap
= mdsc
->last_snap_seq
;
3642 up_read(&mdsc
->snap_rwsem
);
3644 dout("sync want tid %lld flush_seq %lld snap_seq %lld\n",
3645 want_tid
, want_flush
, want_snap
);
3647 wait_unsafe_requests(mdsc
, want_tid
);
3648 wait_caps_flush(mdsc
, want_flush
, want_snap
);
3652 * true if all sessions are closed, or we force unmount
3654 static bool done_closing_sessions(struct ceph_mds_client
*mdsc
)
3656 if (ACCESS_ONCE(mdsc
->fsc
->mount_state
) == CEPH_MOUNT_SHUTDOWN
)
3658 return atomic_read(&mdsc
->num_sessions
) == 0;
3662 * called after sb is ro.
3664 void ceph_mdsc_close_sessions(struct ceph_mds_client
*mdsc
)
3666 struct ceph_options
*opts
= mdsc
->fsc
->client
->options
;
3667 struct ceph_mds_session
*session
;
3670 dout("close_sessions\n");
3672 /* close sessions */
3673 mutex_lock(&mdsc
->mutex
);
3674 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
3675 session
= __ceph_lookup_mds_session(mdsc
, i
);
3678 mutex_unlock(&mdsc
->mutex
);
3679 mutex_lock(&session
->s_mutex
);
3680 __close_session(mdsc
, session
);
3681 mutex_unlock(&session
->s_mutex
);
3682 ceph_put_mds_session(session
);
3683 mutex_lock(&mdsc
->mutex
);
3685 mutex_unlock(&mdsc
->mutex
);
3687 dout("waiting for sessions to close\n");
3688 wait_event_timeout(mdsc
->session_close_wq
, done_closing_sessions(mdsc
),
3689 ceph_timeout_jiffies(opts
->mount_timeout
));
3691 /* tear down remaining sessions */
3692 mutex_lock(&mdsc
->mutex
);
3693 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
3694 if (mdsc
->sessions
[i
]) {
3695 session
= get_session(mdsc
->sessions
[i
]);
3696 __unregister_session(mdsc
, session
);
3697 mutex_unlock(&mdsc
->mutex
);
3698 mutex_lock(&session
->s_mutex
);
3699 remove_session_caps(session
);
3700 mutex_unlock(&session
->s_mutex
);
3701 ceph_put_mds_session(session
);
3702 mutex_lock(&mdsc
->mutex
);
3705 WARN_ON(!list_empty(&mdsc
->cap_delay_list
));
3706 mutex_unlock(&mdsc
->mutex
);
3708 ceph_cleanup_empty_realms(mdsc
);
3710 cancel_delayed_work_sync(&mdsc
->delayed_work
); /* cancel timer */
3715 void ceph_mdsc_force_umount(struct ceph_mds_client
*mdsc
)
3717 struct ceph_mds_session
*session
;
3720 dout("force umount\n");
3722 mutex_lock(&mdsc
->mutex
);
3723 for (mds
= 0; mds
< mdsc
->max_sessions
; mds
++) {
3724 session
= __ceph_lookup_mds_session(mdsc
, mds
);
3727 mutex_unlock(&mdsc
->mutex
);
3728 mutex_lock(&session
->s_mutex
);
3729 __close_session(mdsc
, session
);
3730 if (session
->s_state
== CEPH_MDS_SESSION_CLOSING
) {
3731 cleanup_session_requests(mdsc
, session
);
3732 remove_session_caps(session
);
3734 mutex_unlock(&session
->s_mutex
);
3735 ceph_put_mds_session(session
);
3736 mutex_lock(&mdsc
->mutex
);
3737 kick_requests(mdsc
, mds
);
3739 __wake_requests(mdsc
, &mdsc
->waiting_for_map
);
3740 mutex_unlock(&mdsc
->mutex
);
3743 static void ceph_mdsc_stop(struct ceph_mds_client
*mdsc
)
3746 cancel_delayed_work_sync(&mdsc
->delayed_work
); /* cancel timer */
3748 ceph_mdsmap_destroy(mdsc
->mdsmap
);
3749 kfree(mdsc
->sessions
);
3750 ceph_caps_finalize(mdsc
);
3751 ceph_pool_perm_destroy(mdsc
);
3754 void ceph_mdsc_destroy(struct ceph_fs_client
*fsc
)
3756 struct ceph_mds_client
*mdsc
= fsc
->mdsc
;
3758 dout("mdsc_destroy %p\n", mdsc
);
3759 ceph_mdsc_stop(mdsc
);
3761 /* flush out any connection work with references to us */
3766 dout("mdsc_destroy %p done\n", mdsc
);
3771 * handle mds map update.
3773 void ceph_mdsc_handle_map(struct ceph_mds_client
*mdsc
, struct ceph_msg
*msg
)
3777 void *p
= msg
->front
.iov_base
;
3778 void *end
= p
+ msg
->front
.iov_len
;
3779 struct ceph_mdsmap
*newmap
, *oldmap
;
3780 struct ceph_fsid fsid
;
3783 ceph_decode_need(&p
, end
, sizeof(fsid
)+2*sizeof(u32
), bad
);
3784 ceph_decode_copy(&p
, &fsid
, sizeof(fsid
));
3785 if (ceph_check_fsid(mdsc
->fsc
->client
, &fsid
) < 0)
3787 epoch
= ceph_decode_32(&p
);
3788 maplen
= ceph_decode_32(&p
);
3789 dout("handle_map epoch %u len %d\n", epoch
, (int)maplen
);
3791 /* do we need it? */
3792 ceph_monc_got_mdsmap(&mdsc
->fsc
->client
->monc
, epoch
);
3793 mutex_lock(&mdsc
->mutex
);
3794 if (mdsc
->mdsmap
&& epoch
<= mdsc
->mdsmap
->m_epoch
) {
3795 dout("handle_map epoch %u <= our %u\n",
3796 epoch
, mdsc
->mdsmap
->m_epoch
);
3797 mutex_unlock(&mdsc
->mutex
);
3801 newmap
= ceph_mdsmap_decode(&p
, end
);
3802 if (IS_ERR(newmap
)) {
3803 err
= PTR_ERR(newmap
);
3807 /* swap into place */
3809 oldmap
= mdsc
->mdsmap
;
3810 mdsc
->mdsmap
= newmap
;
3811 check_new_map(mdsc
, newmap
, oldmap
);
3812 ceph_mdsmap_destroy(oldmap
);
3814 mdsc
->mdsmap
= newmap
; /* first mds map */
3816 mdsc
->fsc
->sb
->s_maxbytes
= mdsc
->mdsmap
->m_max_file_size
;
3818 __wake_requests(mdsc
, &mdsc
->waiting_for_map
);
3820 mutex_unlock(&mdsc
->mutex
);
3821 schedule_delayed(mdsc
);
3825 mutex_unlock(&mdsc
->mutex
);
3827 pr_err("error decoding mdsmap %d\n", err
);
3831 static struct ceph_connection
*con_get(struct ceph_connection
*con
)
3833 struct ceph_mds_session
*s
= con
->private;
3835 if (get_session(s
)) {
3836 dout("mdsc con_get %p ok (%d)\n", s
, atomic_read(&s
->s_ref
));
3839 dout("mdsc con_get %p FAIL\n", s
);
3843 static void con_put(struct ceph_connection
*con
)
3845 struct ceph_mds_session
*s
= con
->private;
3847 dout("mdsc con_put %p (%d)\n", s
, atomic_read(&s
->s_ref
) - 1);
3848 ceph_put_mds_session(s
);
3852 * if the client is unresponsive for long enough, the mds will kill
3853 * the session entirely.
3855 static void peer_reset(struct ceph_connection
*con
)
3857 struct ceph_mds_session
*s
= con
->private;
3858 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
3860 pr_warn("mds%d closed our session\n", s
->s_mds
);
3861 send_mds_reconnect(mdsc
, s
);
3864 static void dispatch(struct ceph_connection
*con
, struct ceph_msg
*msg
)
3866 struct ceph_mds_session
*s
= con
->private;
3867 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
3868 int type
= le16_to_cpu(msg
->hdr
.type
);
3870 mutex_lock(&mdsc
->mutex
);
3871 if (__verify_registered_session(mdsc
, s
) < 0) {
3872 mutex_unlock(&mdsc
->mutex
);
3875 mutex_unlock(&mdsc
->mutex
);
3878 case CEPH_MSG_MDS_MAP
:
3879 ceph_mdsc_handle_map(mdsc
, msg
);
3881 case CEPH_MSG_CLIENT_SESSION
:
3882 handle_session(s
, msg
);
3884 case CEPH_MSG_CLIENT_REPLY
:
3885 handle_reply(s
, msg
);
3887 case CEPH_MSG_CLIENT_REQUEST_FORWARD
:
3888 handle_forward(mdsc
, s
, msg
);
3890 case CEPH_MSG_CLIENT_CAPS
:
3891 ceph_handle_caps(s
, msg
);
3893 case CEPH_MSG_CLIENT_SNAP
:
3894 ceph_handle_snap(mdsc
, s
, msg
);
3896 case CEPH_MSG_CLIENT_LEASE
:
3897 handle_lease(mdsc
, s
, msg
);
3901 pr_err("received unknown message type %d %s\n", type
,
3902 ceph_msg_type_name(type
));
3913 * Note: returned pointer is the address of a structure that's
3914 * managed separately. Caller must *not* attempt to free it.
3916 static struct ceph_auth_handshake
*get_authorizer(struct ceph_connection
*con
,
3917 int *proto
, int force_new
)
3919 struct ceph_mds_session
*s
= con
->private;
3920 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
3921 struct ceph_auth_client
*ac
= mdsc
->fsc
->client
->monc
.auth
;
3922 struct ceph_auth_handshake
*auth
= &s
->s_auth
;
3924 if (force_new
&& auth
->authorizer
) {
3925 ceph_auth_destroy_authorizer(ac
, auth
->authorizer
);
3926 auth
->authorizer
= NULL
;
3928 if (!auth
->authorizer
) {
3929 int ret
= ceph_auth_create_authorizer(ac
, CEPH_ENTITY_TYPE_MDS
,
3932 return ERR_PTR(ret
);
3934 int ret
= ceph_auth_update_authorizer(ac
, CEPH_ENTITY_TYPE_MDS
,
3937 return ERR_PTR(ret
);
3939 *proto
= ac
->protocol
;
3945 static int verify_authorizer_reply(struct ceph_connection
*con
, int len
)
3947 struct ceph_mds_session
*s
= con
->private;
3948 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
3949 struct ceph_auth_client
*ac
= mdsc
->fsc
->client
->monc
.auth
;
3951 return ceph_auth_verify_authorizer_reply(ac
, s
->s_auth
.authorizer
, len
);
3954 static int invalidate_authorizer(struct ceph_connection
*con
)
3956 struct ceph_mds_session
*s
= con
->private;
3957 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
3958 struct ceph_auth_client
*ac
= mdsc
->fsc
->client
->monc
.auth
;
3960 ceph_auth_invalidate_authorizer(ac
, CEPH_ENTITY_TYPE_MDS
);
3962 return ceph_monc_validate_auth(&mdsc
->fsc
->client
->monc
);
3965 static struct ceph_msg
*mds_alloc_msg(struct ceph_connection
*con
,
3966 struct ceph_msg_header
*hdr
, int *skip
)
3968 struct ceph_msg
*msg
;
3969 int type
= (int) le16_to_cpu(hdr
->type
);
3970 int front_len
= (int) le32_to_cpu(hdr
->front_len
);
3976 msg
= ceph_msg_new(type
, front_len
, GFP_NOFS
, false);
3978 pr_err("unable to allocate msg type %d len %d\n",
3986 static int mds_sign_message(struct ceph_msg
*msg
)
3988 struct ceph_mds_session
*s
= msg
->con
->private;
3989 struct ceph_auth_handshake
*auth
= &s
->s_auth
;
3991 return ceph_auth_sign_message(auth
, msg
);
3994 static int mds_check_message_signature(struct ceph_msg
*msg
)
3996 struct ceph_mds_session
*s
= msg
->con
->private;
3997 struct ceph_auth_handshake
*auth
= &s
->s_auth
;
3999 return ceph_auth_check_message_signature(auth
, msg
);
4002 static const struct ceph_connection_operations mds_con_ops
= {
4005 .dispatch
= dispatch
,
4006 .get_authorizer
= get_authorizer
,
4007 .verify_authorizer_reply
= verify_authorizer_reply
,
4008 .invalidate_authorizer
= invalidate_authorizer
,
4009 .peer_reset
= peer_reset
,
4010 .alloc_msg
= mds_alloc_msg
,
4011 .sign_message
= mds_sign_message
,
4012 .check_message_signature
= mds_check_message_signature
,