1 #include "ceph_debug.h"
3 #include <linux/wait.h>
4 #include <linux/slab.h>
5 #include <linux/sched.h>
7 #include "mds_client.h"
8 #include "mon_client.h"
10 #include "messenger.h"
16 * A cluster of MDS (metadata server) daemons is responsible for
17 * managing the file system namespace (the directory hierarchy and
18 * inodes) and for coordinating shared access to storage. Metadata is
19 * partitioning hierarchically across a number of servers, and that
20 * partition varies over time as the cluster adjusts the distribution
21 * in order to balance load.
23 * The MDS client is primarily responsible to managing synchronous
24 * metadata requests for operations like open, unlink, and so forth.
25 * If there is a MDS failure, we find out about it when we (possibly
26 * request and) receive a new MDS map, and can resubmit affected
29 * For the most part, though, we take advantage of a lossless
30 * communications channel to the MDS, and do not need to worry about
31 * timing out or resubmitting requests.
33 * We maintain a stateful "session" with each MDS we interact with.
34 * Within each session, we sent periodic heartbeat messages to ensure
35 * any capabilities or leases we have been issues remain valid. If
36 * the session times out and goes stale, our leases and capabilities
37 * are no longer valid.
40 static void __wake_requests(struct ceph_mds_client
*mdsc
,
41 struct list_head
*head
);
43 static const struct ceph_connection_operations mds_con_ops
;
51 * parse individual inode info
53 static int parse_reply_info_in(void **p
, void *end
,
54 struct ceph_mds_reply_info_in
*info
)
59 *p
+= sizeof(struct ceph_mds_reply_inode
) +
60 sizeof(*info
->in
->fragtree
.splits
) *
61 le32_to_cpu(info
->in
->fragtree
.nsplits
);
63 ceph_decode_32_safe(p
, end
, info
->symlink_len
, bad
);
64 ceph_decode_need(p
, end
, info
->symlink_len
, bad
);
66 *p
+= info
->symlink_len
;
68 ceph_decode_32_safe(p
, end
, info
->xattr_len
, bad
);
69 ceph_decode_need(p
, end
, info
->xattr_len
, bad
);
70 info
->xattr_data
= *p
;
71 *p
+= info
->xattr_len
;
78 * parse a normal reply, which may contain a (dir+)dentry and/or a
81 static int parse_reply_info_trace(void **p
, void *end
,
82 struct ceph_mds_reply_info_parsed
*info
)
86 if (info
->head
->is_dentry
) {
87 err
= parse_reply_info_in(p
, end
, &info
->diri
);
91 if (unlikely(*p
+ sizeof(*info
->dirfrag
) > end
))
94 *p
+= sizeof(*info
->dirfrag
) +
95 sizeof(u32
)*le32_to_cpu(info
->dirfrag
->ndist
);
96 if (unlikely(*p
> end
))
99 ceph_decode_32_safe(p
, end
, info
->dname_len
, bad
);
100 ceph_decode_need(p
, end
, info
->dname_len
, bad
);
102 *p
+= info
->dname_len
;
104 *p
+= sizeof(*info
->dlease
);
107 if (info
->head
->is_target
) {
108 err
= parse_reply_info_in(p
, end
, &info
->targeti
);
113 if (unlikely(*p
!= end
))
120 pr_err("problem parsing mds trace %d\n", err
);
125 * parse readdir results
127 static int parse_reply_info_dir(void **p
, void *end
,
128 struct ceph_mds_reply_info_parsed
*info
)
134 if (*p
+ sizeof(*info
->dir_dir
) > end
)
136 *p
+= sizeof(*info
->dir_dir
) +
137 sizeof(u32
)*le32_to_cpu(info
->dir_dir
->ndist
);
141 ceph_decode_need(p
, end
, sizeof(num
) + 2, bad
);
142 num
= ceph_decode_32(p
);
143 info
->dir_end
= ceph_decode_8(p
);
144 info
->dir_complete
= ceph_decode_8(p
);
148 /* alloc large array */
150 info
->dir_in
= kcalloc(num
, sizeof(*info
->dir_in
) +
151 sizeof(*info
->dir_dname
) +
152 sizeof(*info
->dir_dname_len
) +
153 sizeof(*info
->dir_dlease
),
155 if (info
->dir_in
== NULL
) {
159 info
->dir_dname
= (void *)(info
->dir_in
+ num
);
160 info
->dir_dname_len
= (void *)(info
->dir_dname
+ num
);
161 info
->dir_dlease
= (void *)(info
->dir_dname_len
+ num
);
165 ceph_decode_need(p
, end
, sizeof(u32
)*2, bad
);
166 info
->dir_dname_len
[i
] = ceph_decode_32(p
);
167 ceph_decode_need(p
, end
, info
->dir_dname_len
[i
], bad
);
168 info
->dir_dname
[i
] = *p
;
169 *p
+= info
->dir_dname_len
[i
];
170 dout("parsed dir dname '%.*s'\n", info
->dir_dname_len
[i
],
172 info
->dir_dlease
[i
] = *p
;
173 *p
+= sizeof(struct ceph_mds_reply_lease
);
176 err
= parse_reply_info_in(p
, end
, &info
->dir_in
[i
]);
191 pr_err("problem parsing dir contents %d\n", err
);
196 * parse entire mds reply
198 static int parse_reply_info(struct ceph_msg
*msg
,
199 struct ceph_mds_reply_info_parsed
*info
)
205 info
->head
= msg
->front
.iov_base
;
206 p
= msg
->front
.iov_base
+ sizeof(struct ceph_mds_reply_head
);
207 end
= p
+ msg
->front
.iov_len
- sizeof(struct ceph_mds_reply_head
);
210 ceph_decode_32_safe(&p
, end
, len
, bad
);
212 err
= parse_reply_info_trace(&p
, p
+len
, info
);
218 ceph_decode_32_safe(&p
, end
, len
, bad
);
220 err
= parse_reply_info_dir(&p
, p
+len
, info
);
226 ceph_decode_32_safe(&p
, end
, len
, bad
);
227 info
->snapblob_len
= len
;
238 pr_err("mds parse_reply err %d\n", err
);
242 static void destroy_reply_info(struct ceph_mds_reply_info_parsed
*info
)
251 static const char *session_state_name(int s
)
254 case CEPH_MDS_SESSION_NEW
: return "new";
255 case CEPH_MDS_SESSION_OPENING
: return "opening";
256 case CEPH_MDS_SESSION_OPEN
: return "open";
257 case CEPH_MDS_SESSION_HUNG
: return "hung";
258 case CEPH_MDS_SESSION_CLOSING
: return "closing";
259 case CEPH_MDS_SESSION_RESTARTING
: return "restarting";
260 case CEPH_MDS_SESSION_RECONNECTING
: return "reconnecting";
261 default: return "???";
265 static struct ceph_mds_session
*get_session(struct ceph_mds_session
*s
)
267 if (atomic_inc_not_zero(&s
->s_ref
)) {
268 dout("mdsc get_session %p %d -> %d\n", s
,
269 atomic_read(&s
->s_ref
)-1, atomic_read(&s
->s_ref
));
272 dout("mdsc get_session %p 0 -- FAIL", s
);
277 void ceph_put_mds_session(struct ceph_mds_session
*s
)
279 dout("mdsc put_session %p %d -> %d\n", s
,
280 atomic_read(&s
->s_ref
), atomic_read(&s
->s_ref
)-1);
281 if (atomic_dec_and_test(&s
->s_ref
)) {
283 s
->s_mdsc
->client
->monc
.auth
->ops
->destroy_authorizer(
284 s
->s_mdsc
->client
->monc
.auth
, s
->s_authorizer
);
290 * called under mdsc->mutex
292 struct ceph_mds_session
*__ceph_lookup_mds_session(struct ceph_mds_client
*mdsc
,
295 struct ceph_mds_session
*session
;
297 if (mds
>= mdsc
->max_sessions
|| mdsc
->sessions
[mds
] == NULL
)
299 session
= mdsc
->sessions
[mds
];
300 dout("lookup_mds_session %p %d\n", session
,
301 atomic_read(&session
->s_ref
));
302 get_session(session
);
306 static bool __have_session(struct ceph_mds_client
*mdsc
, int mds
)
308 if (mds
>= mdsc
->max_sessions
)
310 return mdsc
->sessions
[mds
];
313 static int __verify_registered_session(struct ceph_mds_client
*mdsc
,
314 struct ceph_mds_session
*s
)
316 if (s
->s_mds
>= mdsc
->max_sessions
||
317 mdsc
->sessions
[s
->s_mds
] != s
)
323 * create+register a new session for given mds.
324 * called under mdsc->mutex.
326 static struct ceph_mds_session
*register_session(struct ceph_mds_client
*mdsc
,
329 struct ceph_mds_session
*s
;
331 s
= kzalloc(sizeof(*s
), GFP_NOFS
);
333 return ERR_PTR(-ENOMEM
);
336 s
->s_state
= CEPH_MDS_SESSION_NEW
;
339 mutex_init(&s
->s_mutex
);
341 ceph_con_init(mdsc
->client
->msgr
, &s
->s_con
);
342 s
->s_con
.private = s
;
343 s
->s_con
.ops
= &mds_con_ops
;
344 s
->s_con
.peer_name
.type
= CEPH_ENTITY_TYPE_MDS
;
345 s
->s_con
.peer_name
.num
= cpu_to_le64(mds
);
347 spin_lock_init(&s
->s_cap_lock
);
350 s
->s_renew_requested
= 0;
352 INIT_LIST_HEAD(&s
->s_caps
);
355 atomic_set(&s
->s_ref
, 1);
356 INIT_LIST_HEAD(&s
->s_waiting
);
357 INIT_LIST_HEAD(&s
->s_unsafe
);
358 s
->s_num_cap_releases
= 0;
359 s
->s_cap_iterator
= NULL
;
360 INIT_LIST_HEAD(&s
->s_cap_releases
);
361 INIT_LIST_HEAD(&s
->s_cap_releases_done
);
362 INIT_LIST_HEAD(&s
->s_cap_flushing
);
363 INIT_LIST_HEAD(&s
->s_cap_snaps_flushing
);
365 dout("register_session mds%d\n", mds
);
366 if (mds
>= mdsc
->max_sessions
) {
367 int newmax
= 1 << get_count_order(mds
+1);
368 struct ceph_mds_session
**sa
;
370 dout("register_session realloc to %d\n", newmax
);
371 sa
= kcalloc(newmax
, sizeof(void *), GFP_NOFS
);
374 if (mdsc
->sessions
) {
375 memcpy(sa
, mdsc
->sessions
,
376 mdsc
->max_sessions
* sizeof(void *));
377 kfree(mdsc
->sessions
);
380 mdsc
->max_sessions
= newmax
;
382 mdsc
->sessions
[mds
] = s
;
383 atomic_inc(&s
->s_ref
); /* one ref to sessions[], one to caller */
385 ceph_con_open(&s
->s_con
, ceph_mdsmap_get_addr(mdsc
->mdsmap
, mds
));
391 return ERR_PTR(-ENOMEM
);
395 * called under mdsc->mutex
397 static void __unregister_session(struct ceph_mds_client
*mdsc
,
398 struct ceph_mds_session
*s
)
400 dout("__unregister_session mds%d %p\n", s
->s_mds
, s
);
401 BUG_ON(mdsc
->sessions
[s
->s_mds
] != s
);
402 mdsc
->sessions
[s
->s_mds
] = NULL
;
403 ceph_con_close(&s
->s_con
);
404 ceph_put_mds_session(s
);
408 * drop session refs in request.
410 * should be last request ref, or hold mdsc->mutex
412 static void put_request_session(struct ceph_mds_request
*req
)
414 if (req
->r_session
) {
415 ceph_put_mds_session(req
->r_session
);
416 req
->r_session
= NULL
;
420 void ceph_mdsc_release_request(struct kref
*kref
)
422 struct ceph_mds_request
*req
= container_of(kref
,
423 struct ceph_mds_request
,
426 ceph_msg_put(req
->r_request
);
428 ceph_msg_put(req
->r_reply
);
429 destroy_reply_info(&req
->r_reply_info
);
432 ceph_put_cap_refs(ceph_inode(req
->r_inode
),
436 if (req
->r_locked_dir
)
437 ceph_put_cap_refs(ceph_inode(req
->r_locked_dir
),
439 if (req
->r_target_inode
)
440 iput(req
->r_target_inode
);
443 if (req
->r_old_dentry
) {
445 ceph_inode(req
->r_old_dentry
->d_parent
->d_inode
),
447 dput(req
->r_old_dentry
);
451 put_request_session(req
);
452 ceph_unreserve_caps(req
->r_mdsc
, &req
->r_caps_reservation
);
457 * lookup session, bump ref if found.
459 * called under mdsc->mutex.
461 static struct ceph_mds_request
*__lookup_request(struct ceph_mds_client
*mdsc
,
464 struct ceph_mds_request
*req
;
465 struct rb_node
*n
= mdsc
->request_tree
.rb_node
;
468 req
= rb_entry(n
, struct ceph_mds_request
, r_node
);
469 if (tid
< req
->r_tid
)
471 else if (tid
> req
->r_tid
)
474 ceph_mdsc_get_request(req
);
481 static void __insert_request(struct ceph_mds_client
*mdsc
,
482 struct ceph_mds_request
*new)
484 struct rb_node
**p
= &mdsc
->request_tree
.rb_node
;
485 struct rb_node
*parent
= NULL
;
486 struct ceph_mds_request
*req
= NULL
;
490 req
= rb_entry(parent
, struct ceph_mds_request
, r_node
);
491 if (new->r_tid
< req
->r_tid
)
493 else if (new->r_tid
> req
->r_tid
)
499 rb_link_node(&new->r_node
, parent
, p
);
500 rb_insert_color(&new->r_node
, &mdsc
->request_tree
);
504 * Register an in-flight request, and assign a tid. Link to directory
505 * are modifying (if any).
507 * Called under mdsc->mutex.
509 static void __register_request(struct ceph_mds_client
*mdsc
,
510 struct ceph_mds_request
*req
,
513 req
->r_tid
= ++mdsc
->last_tid
;
515 ceph_reserve_caps(mdsc
, &req
->r_caps_reservation
,
517 dout("__register_request %p tid %lld\n", req
, req
->r_tid
);
518 ceph_mdsc_get_request(req
);
519 __insert_request(mdsc
, req
);
522 struct ceph_inode_info
*ci
= ceph_inode(dir
);
524 spin_lock(&ci
->i_unsafe_lock
);
525 req
->r_unsafe_dir
= dir
;
526 list_add_tail(&req
->r_unsafe_dir_item
, &ci
->i_unsafe_dirops
);
527 spin_unlock(&ci
->i_unsafe_lock
);
531 static void __unregister_request(struct ceph_mds_client
*mdsc
,
532 struct ceph_mds_request
*req
)
534 dout("__unregister_request %p tid %lld\n", req
, req
->r_tid
);
535 rb_erase(&req
->r_node
, &mdsc
->request_tree
);
536 RB_CLEAR_NODE(&req
->r_node
);
538 if (req
->r_unsafe_dir
) {
539 struct ceph_inode_info
*ci
= ceph_inode(req
->r_unsafe_dir
);
541 spin_lock(&ci
->i_unsafe_lock
);
542 list_del_init(&req
->r_unsafe_dir_item
);
543 spin_unlock(&ci
->i_unsafe_lock
);
546 ceph_mdsc_put_request(req
);
550 * Choose mds to send request to next. If there is a hint set in the
551 * request (e.g., due to a prior forward hint from the mds), use that.
552 * Otherwise, consult frag tree and/or caps to identify the
553 * appropriate mds. If all else fails, choose randomly.
555 * Called under mdsc->mutex.
557 static int __choose_mds(struct ceph_mds_client
*mdsc
,
558 struct ceph_mds_request
*req
)
561 struct ceph_inode_info
*ci
;
562 struct ceph_cap
*cap
;
563 int mode
= req
->r_direct_mode
;
565 u32 hash
= req
->r_direct_hash
;
566 bool is_hash
= req
->r_direct_is_hash
;
569 * is there a specific mds we should try? ignore hint if we have
570 * no session and the mds is not up (active or recovering).
572 if (req
->r_resend_mds
>= 0 &&
573 (__have_session(mdsc
, req
->r_resend_mds
) ||
574 ceph_mdsmap_get_state(mdsc
->mdsmap
, req
->r_resend_mds
) > 0)) {
575 dout("choose_mds using resend_mds mds%d\n",
577 return req
->r_resend_mds
;
580 if (mode
== USE_RANDOM_MDS
)
585 inode
= req
->r_inode
;
586 } else if (req
->r_dentry
) {
587 if (req
->r_dentry
->d_inode
) {
588 inode
= req
->r_dentry
->d_inode
;
590 inode
= req
->r_dentry
->d_parent
->d_inode
;
591 hash
= req
->r_dentry
->d_name
.hash
;
595 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode
, (int)is_hash
,
599 ci
= ceph_inode(inode
);
601 if (is_hash
&& S_ISDIR(inode
->i_mode
)) {
602 struct ceph_inode_frag frag
;
605 ceph_choose_frag(ci
, hash
, &frag
, &found
);
607 if (mode
== USE_ANY_MDS
&& frag
.ndist
> 0) {
610 /* choose a random replica */
611 get_random_bytes(&r
, 1);
614 dout("choose_mds %p %llx.%llx "
615 "frag %u mds%d (%d/%d)\n",
616 inode
, ceph_vinop(inode
),
622 /* since this file/dir wasn't known to be
623 * replicated, then we want to look for the
624 * authoritative mds. */
627 /* choose auth mds */
629 dout("choose_mds %p %llx.%llx "
630 "frag %u mds%d (auth)\n",
631 inode
, ceph_vinop(inode
), frag
.frag
, mds
);
637 spin_lock(&inode
->i_lock
);
639 if (mode
== USE_AUTH_MDS
)
640 cap
= ci
->i_auth_cap
;
641 if (!cap
&& !RB_EMPTY_ROOT(&ci
->i_caps
))
642 cap
= rb_entry(rb_first(&ci
->i_caps
), struct ceph_cap
, ci_node
);
644 spin_unlock(&inode
->i_lock
);
647 mds
= cap
->session
->s_mds
;
648 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
649 inode
, ceph_vinop(inode
), mds
,
650 cap
== ci
->i_auth_cap
? "auth " : "", cap
);
651 spin_unlock(&inode
->i_lock
);
655 mds
= ceph_mdsmap_get_random_mds(mdsc
->mdsmap
);
656 dout("choose_mds chose random mds%d\n", mds
);
664 static struct ceph_msg
*create_session_msg(u32 op
, u64 seq
)
666 struct ceph_msg
*msg
;
667 struct ceph_mds_session_head
*h
;
669 msg
= ceph_msg_new(CEPH_MSG_CLIENT_SESSION
, sizeof(*h
), GFP_NOFS
);
671 pr_err("create_session_msg ENOMEM creating msg\n");
674 h
= msg
->front
.iov_base
;
675 h
->op
= cpu_to_le32(op
);
676 h
->seq
= cpu_to_le64(seq
);
681 * send session open request.
683 * called under mdsc->mutex
685 static int __open_session(struct ceph_mds_client
*mdsc
,
686 struct ceph_mds_session
*session
)
688 struct ceph_msg
*msg
;
690 int mds
= session
->s_mds
;
692 /* wait for mds to go active? */
693 mstate
= ceph_mdsmap_get_state(mdsc
->mdsmap
, mds
);
694 dout("open_session to mds%d (%s)\n", mds
,
695 ceph_mds_state_name(mstate
));
696 session
->s_state
= CEPH_MDS_SESSION_OPENING
;
697 session
->s_renew_requested
= jiffies
;
699 /* send connect message */
700 msg
= create_session_msg(CEPH_SESSION_REQUEST_OPEN
, session
->s_seq
);
703 ceph_con_send(&session
->s_con
, msg
);
708 * open sessions for any export targets for the given mds
710 * called under mdsc->mutex
712 static void __open_export_target_sessions(struct ceph_mds_client
*mdsc
,
713 struct ceph_mds_session
*session
)
715 struct ceph_mds_info
*mi
;
716 struct ceph_mds_session
*ts
;
717 int i
, mds
= session
->s_mds
;
720 if (mds
>= mdsc
->mdsmap
->m_max_mds
)
722 mi
= &mdsc
->mdsmap
->m_info
[mds
];
723 dout("open_export_target_sessions for mds%d (%d targets)\n",
724 session
->s_mds
, mi
->num_export_targets
);
726 for (i
= 0; i
< mi
->num_export_targets
; i
++) {
727 target
= mi
->export_targets
[i
];
728 ts
= __ceph_lookup_mds_session(mdsc
, target
);
730 ts
= register_session(mdsc
, target
);
734 if (session
->s_state
== CEPH_MDS_SESSION_NEW
||
735 session
->s_state
== CEPH_MDS_SESSION_CLOSING
)
736 __open_session(mdsc
, session
);
738 dout(" mds%d target mds%d %p is %s\n", session
->s_mds
,
739 i
, ts
, session_state_name(ts
->s_state
));
740 ceph_put_mds_session(ts
);
749 * Free preallocated cap messages assigned to this session
751 static void cleanup_cap_releases(struct ceph_mds_session
*session
)
753 struct ceph_msg
*msg
;
755 spin_lock(&session
->s_cap_lock
);
756 while (!list_empty(&session
->s_cap_releases
)) {
757 msg
= list_first_entry(&session
->s_cap_releases
,
758 struct ceph_msg
, list_head
);
759 list_del_init(&msg
->list_head
);
762 while (!list_empty(&session
->s_cap_releases_done
)) {
763 msg
= list_first_entry(&session
->s_cap_releases_done
,
764 struct ceph_msg
, list_head
);
765 list_del_init(&msg
->list_head
);
768 spin_unlock(&session
->s_cap_lock
);
772 * Helper to safely iterate over all caps associated with a session, with
773 * special care taken to handle a racing __ceph_remove_cap().
775 * Caller must hold session s_mutex.
777 static int iterate_session_caps(struct ceph_mds_session
*session
,
778 int (*cb
)(struct inode
*, struct ceph_cap
*,
782 struct ceph_cap
*cap
;
783 struct inode
*inode
, *last_inode
= NULL
;
784 struct ceph_cap
*old_cap
= NULL
;
787 dout("iterate_session_caps %p mds%d\n", session
, session
->s_mds
);
788 spin_lock(&session
->s_cap_lock
);
789 p
= session
->s_caps
.next
;
790 while (p
!= &session
->s_caps
) {
791 cap
= list_entry(p
, struct ceph_cap
, session_caps
);
792 inode
= igrab(&cap
->ci
->vfs_inode
);
797 session
->s_cap_iterator
= cap
;
798 spin_unlock(&session
->s_cap_lock
);
805 ceph_put_cap(session
->s_mdsc
, old_cap
);
809 ret
= cb(inode
, cap
, arg
);
812 spin_lock(&session
->s_cap_lock
);
814 if (cap
->ci
== NULL
) {
815 dout("iterate_session_caps finishing cap %p removal\n",
817 BUG_ON(cap
->session
!= session
);
818 list_del_init(&cap
->session_caps
);
819 session
->s_nr_caps
--;
821 old_cap
= cap
; /* put_cap it w/o locks held */
828 session
->s_cap_iterator
= NULL
;
829 spin_unlock(&session
->s_cap_lock
);
834 ceph_put_cap(session
->s_mdsc
, old_cap
);
839 static int remove_session_caps_cb(struct inode
*inode
, struct ceph_cap
*cap
,
842 struct ceph_inode_info
*ci
= ceph_inode(inode
);
845 dout("removing cap %p, ci is %p, inode is %p\n",
846 cap
, ci
, &ci
->vfs_inode
);
847 spin_lock(&inode
->i_lock
);
848 __ceph_remove_cap(cap
);
849 if (!__ceph_is_any_real_caps(ci
)) {
850 struct ceph_mds_client
*mdsc
=
851 &ceph_sb_to_client(inode
->i_sb
)->mdsc
;
853 spin_lock(&mdsc
->cap_dirty_lock
);
854 if (!list_empty(&ci
->i_dirty_item
)) {
855 pr_info(" dropping dirty %s state for %p %lld\n",
856 ceph_cap_string(ci
->i_dirty_caps
),
857 inode
, ceph_ino(inode
));
858 ci
->i_dirty_caps
= 0;
859 list_del_init(&ci
->i_dirty_item
);
862 if (!list_empty(&ci
->i_flushing_item
)) {
863 pr_info(" dropping dirty+flushing %s state for %p %lld\n",
864 ceph_cap_string(ci
->i_flushing_caps
),
865 inode
, ceph_ino(inode
));
866 ci
->i_flushing_caps
= 0;
867 list_del_init(&ci
->i_flushing_item
);
868 mdsc
->num_cap_flushing
--;
871 if (drop
&& ci
->i_wrbuffer_ref
) {
872 pr_info(" dropping dirty data for %p %lld\n",
873 inode
, ceph_ino(inode
));
874 ci
->i_wrbuffer_ref
= 0;
875 ci
->i_wrbuffer_ref_head
= 0;
878 spin_unlock(&mdsc
->cap_dirty_lock
);
880 spin_unlock(&inode
->i_lock
);
887 * caller must hold session s_mutex
889 static void remove_session_caps(struct ceph_mds_session
*session
)
891 dout("remove_session_caps on %p\n", session
);
892 iterate_session_caps(session
, remove_session_caps_cb
, NULL
);
893 BUG_ON(session
->s_nr_caps
> 0);
894 BUG_ON(!list_empty(&session
->s_cap_flushing
));
895 cleanup_cap_releases(session
);
899 * wake up any threads waiting on this session's caps. if the cap is
900 * old (didn't get renewed on the client reconnect), remove it now.
902 * caller must hold s_mutex.
904 static int wake_up_session_cb(struct inode
*inode
, struct ceph_cap
*cap
,
907 struct ceph_inode_info
*ci
= ceph_inode(inode
);
909 wake_up_all(&ci
->i_cap_wq
);
911 spin_lock(&inode
->i_lock
);
912 ci
->i_wanted_max_size
= 0;
913 ci
->i_requested_max_size
= 0;
914 spin_unlock(&inode
->i_lock
);
919 static void wake_up_session_caps(struct ceph_mds_session
*session
,
922 dout("wake_up_session_caps %p mds%d\n", session
, session
->s_mds
);
923 iterate_session_caps(session
, wake_up_session_cb
,
924 (void *)(unsigned long)reconnect
);
928 * Send periodic message to MDS renewing all currently held caps. The
929 * ack will reset the expiration for all caps from this session.
931 * caller holds s_mutex
933 static int send_renew_caps(struct ceph_mds_client
*mdsc
,
934 struct ceph_mds_session
*session
)
936 struct ceph_msg
*msg
;
939 if (time_after_eq(jiffies
, session
->s_cap_ttl
) &&
940 time_after_eq(session
->s_cap_ttl
, session
->s_renew_requested
))
941 pr_info("mds%d caps stale\n", session
->s_mds
);
942 session
->s_renew_requested
= jiffies
;
944 /* do not try to renew caps until a recovering mds has reconnected
945 * with its clients. */
946 state
= ceph_mdsmap_get_state(mdsc
->mdsmap
, session
->s_mds
);
947 if (state
< CEPH_MDS_STATE_RECONNECT
) {
948 dout("send_renew_caps ignoring mds%d (%s)\n",
949 session
->s_mds
, ceph_mds_state_name(state
));
953 dout("send_renew_caps to mds%d (%s)\n", session
->s_mds
,
954 ceph_mds_state_name(state
));
955 msg
= create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS
,
956 ++session
->s_renew_seq
);
959 ceph_con_send(&session
->s_con
, msg
);
964 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
966 * Called under session->s_mutex
968 static void renewed_caps(struct ceph_mds_client
*mdsc
,
969 struct ceph_mds_session
*session
, int is_renew
)
974 spin_lock(&session
->s_cap_lock
);
975 was_stale
= is_renew
&& (session
->s_cap_ttl
== 0 ||
976 time_after_eq(jiffies
, session
->s_cap_ttl
));
978 session
->s_cap_ttl
= session
->s_renew_requested
+
979 mdsc
->mdsmap
->m_session_timeout
*HZ
;
982 if (time_before(jiffies
, session
->s_cap_ttl
)) {
983 pr_info("mds%d caps renewed\n", session
->s_mds
);
986 pr_info("mds%d caps still stale\n", session
->s_mds
);
989 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
990 session
->s_mds
, session
->s_cap_ttl
, was_stale
? "stale" : "fresh",
991 time_before(jiffies
, session
->s_cap_ttl
) ? "stale" : "fresh");
992 spin_unlock(&session
->s_cap_lock
);
995 wake_up_session_caps(session
, 0);
999 * send a session close request
1001 static int request_close_session(struct ceph_mds_client
*mdsc
,
1002 struct ceph_mds_session
*session
)
1004 struct ceph_msg
*msg
;
1006 dout("request_close_session mds%d state %s seq %lld\n",
1007 session
->s_mds
, session_state_name(session
->s_state
),
1009 msg
= create_session_msg(CEPH_SESSION_REQUEST_CLOSE
, session
->s_seq
);
1012 ceph_con_send(&session
->s_con
, msg
);
1017 * Called with s_mutex held.
1019 static int __close_session(struct ceph_mds_client
*mdsc
,
1020 struct ceph_mds_session
*session
)
1022 if (session
->s_state
>= CEPH_MDS_SESSION_CLOSING
)
1024 session
->s_state
= CEPH_MDS_SESSION_CLOSING
;
1025 return request_close_session(mdsc
, session
);
1029 * Trim old(er) caps.
1031 * Because we can't cache an inode without one or more caps, we do
1032 * this indirectly: if a cap is unused, we prune its aliases, at which
1033 * point the inode will hopefully get dropped to.
1035 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1036 * memory pressure from the MDS, though, so it needn't be perfect.
1038 static int trim_caps_cb(struct inode
*inode
, struct ceph_cap
*cap
, void *arg
)
1040 struct ceph_mds_session
*session
= arg
;
1041 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1042 int used
, oissued
, mine
;
1044 if (session
->s_trim_caps
<= 0)
1047 spin_lock(&inode
->i_lock
);
1048 mine
= cap
->issued
| cap
->implemented
;
1049 used
= __ceph_caps_used(ci
);
1050 oissued
= __ceph_caps_issued_other(ci
, cap
);
1052 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n",
1053 inode
, cap
, ceph_cap_string(mine
), ceph_cap_string(oissued
),
1054 ceph_cap_string(used
));
1055 if (ci
->i_dirty_caps
)
1056 goto out
; /* dirty caps */
1057 if ((used
& ~oissued
) & mine
)
1058 goto out
; /* we need these caps */
1060 session
->s_trim_caps
--;
1062 /* we aren't the only cap.. just remove us */
1063 __ceph_remove_cap(cap
);
1065 /* try to drop referring dentries */
1066 spin_unlock(&inode
->i_lock
);
1067 d_prune_aliases(inode
);
1068 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1069 inode
, cap
, atomic_read(&inode
->i_count
));
1074 spin_unlock(&inode
->i_lock
);
1079 * Trim session cap count down to some max number.
1081 static int trim_caps(struct ceph_mds_client
*mdsc
,
1082 struct ceph_mds_session
*session
,
1085 int trim_caps
= session
->s_nr_caps
- max_caps
;
1087 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1088 session
->s_mds
, session
->s_nr_caps
, max_caps
, trim_caps
);
1089 if (trim_caps
> 0) {
1090 session
->s_trim_caps
= trim_caps
;
1091 iterate_session_caps(session
, trim_caps_cb
, session
);
1092 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1093 session
->s_mds
, session
->s_nr_caps
, max_caps
,
1094 trim_caps
- session
->s_trim_caps
);
1095 session
->s_trim_caps
= 0;
1101 * Allocate cap_release messages. If there is a partially full message
1102 * in the queue, try to allocate enough to cover it's remainder, so that
1103 * we can send it immediately.
1105 * Called under s_mutex.
1107 int ceph_add_cap_releases(struct ceph_mds_client
*mdsc
,
1108 struct ceph_mds_session
*session
)
1110 struct ceph_msg
*msg
, *partial
= NULL
;
1111 struct ceph_mds_cap_release
*head
;
1113 int extra
= mdsc
->client
->mount_args
->cap_release_safety
;
1116 dout("add_cap_releases %p mds%d extra %d\n", session
, session
->s_mds
,
1119 spin_lock(&session
->s_cap_lock
);
1121 if (!list_empty(&session
->s_cap_releases
)) {
1122 msg
= list_first_entry(&session
->s_cap_releases
,
1125 head
= msg
->front
.iov_base
;
1126 num
= le32_to_cpu(head
->num
);
1128 dout(" partial %p with (%d/%d)\n", msg
, num
,
1129 (int)CEPH_CAPS_PER_RELEASE
);
1130 extra
+= CEPH_CAPS_PER_RELEASE
- num
;
1134 while (session
->s_num_cap_releases
< session
->s_nr_caps
+ extra
) {
1135 spin_unlock(&session
->s_cap_lock
);
1136 msg
= ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE
, PAGE_CACHE_SIZE
,
1140 dout("add_cap_releases %p msg %p now %d\n", session
, msg
,
1141 (int)msg
->front
.iov_len
);
1142 head
= msg
->front
.iov_base
;
1143 head
->num
= cpu_to_le32(0);
1144 msg
->front
.iov_len
= sizeof(*head
);
1145 spin_lock(&session
->s_cap_lock
);
1146 list_add(&msg
->list_head
, &session
->s_cap_releases
);
1147 session
->s_num_cap_releases
+= CEPH_CAPS_PER_RELEASE
;
1151 head
= partial
->front
.iov_base
;
1152 num
= le32_to_cpu(head
->num
);
1153 dout(" queueing partial %p with %d/%d\n", partial
, num
,
1154 (int)CEPH_CAPS_PER_RELEASE
);
1155 list_move_tail(&partial
->list_head
,
1156 &session
->s_cap_releases_done
);
1157 session
->s_num_cap_releases
-= CEPH_CAPS_PER_RELEASE
- num
;
1160 spin_unlock(&session
->s_cap_lock
);
1166 * flush all dirty inode data to disk.
1168 * returns true if we've flushed through want_flush_seq
1170 static int check_cap_flush(struct ceph_mds_client
*mdsc
, u64 want_flush_seq
)
1174 dout("check_cap_flush want %lld\n", want_flush_seq
);
1175 mutex_lock(&mdsc
->mutex
);
1176 for (mds
= 0; ret
&& mds
< mdsc
->max_sessions
; mds
++) {
1177 struct ceph_mds_session
*session
= mdsc
->sessions
[mds
];
1181 get_session(session
);
1182 mutex_unlock(&mdsc
->mutex
);
1184 mutex_lock(&session
->s_mutex
);
1185 if (!list_empty(&session
->s_cap_flushing
)) {
1186 struct ceph_inode_info
*ci
=
1187 list_entry(session
->s_cap_flushing
.next
,
1188 struct ceph_inode_info
,
1190 struct inode
*inode
= &ci
->vfs_inode
;
1192 spin_lock(&inode
->i_lock
);
1193 if (ci
->i_cap_flush_seq
<= want_flush_seq
) {
1194 dout("check_cap_flush still flushing %p "
1195 "seq %lld <= %lld to mds%d\n", inode
,
1196 ci
->i_cap_flush_seq
, want_flush_seq
,
1200 spin_unlock(&inode
->i_lock
);
1202 mutex_unlock(&session
->s_mutex
);
1203 ceph_put_mds_session(session
);
1207 mutex_lock(&mdsc
->mutex
);
1210 mutex_unlock(&mdsc
->mutex
);
1211 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq
);
1216 * called under s_mutex
1218 void ceph_send_cap_releases(struct ceph_mds_client
*mdsc
,
1219 struct ceph_mds_session
*session
)
1221 struct ceph_msg
*msg
;
1223 dout("send_cap_releases mds%d\n", session
->s_mds
);
1224 spin_lock(&session
->s_cap_lock
);
1225 while (!list_empty(&session
->s_cap_releases_done
)) {
1226 msg
= list_first_entry(&session
->s_cap_releases_done
,
1227 struct ceph_msg
, list_head
);
1228 list_del_init(&msg
->list_head
);
1229 spin_unlock(&session
->s_cap_lock
);
1230 msg
->hdr
.front_len
= cpu_to_le32(msg
->front
.iov_len
);
1231 dout("send_cap_releases mds%d %p\n", session
->s_mds
, msg
);
1232 ceph_con_send(&session
->s_con
, msg
);
1233 spin_lock(&session
->s_cap_lock
);
1235 spin_unlock(&session
->s_cap_lock
);
1238 static void discard_cap_releases(struct ceph_mds_client
*mdsc
,
1239 struct ceph_mds_session
*session
)
1241 struct ceph_msg
*msg
;
1242 struct ceph_mds_cap_release
*head
;
1245 dout("discard_cap_releases mds%d\n", session
->s_mds
);
1246 spin_lock(&session
->s_cap_lock
);
1248 /* zero out the in-progress message */
1249 msg
= list_first_entry(&session
->s_cap_releases
,
1250 struct ceph_msg
, list_head
);
1251 head
= msg
->front
.iov_base
;
1252 num
= le32_to_cpu(head
->num
);
1253 dout("discard_cap_releases mds%d %p %u\n", session
->s_mds
, msg
, num
);
1254 head
->num
= cpu_to_le32(0);
1255 session
->s_num_cap_releases
+= num
;
1257 /* requeue completed messages */
1258 while (!list_empty(&session
->s_cap_releases_done
)) {
1259 msg
= list_first_entry(&session
->s_cap_releases_done
,
1260 struct ceph_msg
, list_head
);
1261 list_del_init(&msg
->list_head
);
1263 head
= msg
->front
.iov_base
;
1264 num
= le32_to_cpu(head
->num
);
1265 dout("discard_cap_releases mds%d %p %u\n", session
->s_mds
, msg
,
1267 session
->s_num_cap_releases
+= num
;
1268 head
->num
= cpu_to_le32(0);
1269 msg
->front
.iov_len
= sizeof(*head
);
1270 list_add(&msg
->list_head
, &session
->s_cap_releases
);
1273 spin_unlock(&session
->s_cap_lock
);
1281 * Create an mds request.
1283 struct ceph_mds_request
*
1284 ceph_mdsc_create_request(struct ceph_mds_client
*mdsc
, int op
, int mode
)
1286 struct ceph_mds_request
*req
= kzalloc(sizeof(*req
), GFP_NOFS
);
1289 return ERR_PTR(-ENOMEM
);
1291 mutex_init(&req
->r_fill_mutex
);
1293 req
->r_started
= jiffies
;
1294 req
->r_resend_mds
= -1;
1295 INIT_LIST_HEAD(&req
->r_unsafe_dir_item
);
1297 kref_init(&req
->r_kref
);
1298 INIT_LIST_HEAD(&req
->r_wait
);
1299 init_completion(&req
->r_completion
);
1300 init_completion(&req
->r_safe_completion
);
1301 INIT_LIST_HEAD(&req
->r_unsafe_item
);
1304 req
->r_direct_mode
= mode
;
1309 * return oldest (lowest) request, tid in request tree, 0 if none.
1311 * called under mdsc->mutex.
1313 static struct ceph_mds_request
*__get_oldest_req(struct ceph_mds_client
*mdsc
)
1315 if (RB_EMPTY_ROOT(&mdsc
->request_tree
))
1317 return rb_entry(rb_first(&mdsc
->request_tree
),
1318 struct ceph_mds_request
, r_node
);
1321 static u64
__get_oldest_tid(struct ceph_mds_client
*mdsc
)
1323 struct ceph_mds_request
*req
= __get_oldest_req(mdsc
);
1331 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1332 * on build_path_from_dentry in fs/cifs/dir.c.
1334 * If @stop_on_nosnap, generate path relative to the first non-snapped
1337 * Encode hidden .snap dirs as a double /, i.e.
1338 * foo/.snap/bar -> foo//bar
1340 char *ceph_mdsc_build_path(struct dentry
*dentry
, int *plen
, u64
*base
,
1343 struct dentry
*temp
;
1348 return ERR_PTR(-EINVAL
);
1352 for (temp
= dentry
; !IS_ROOT(temp
);) {
1353 struct inode
*inode
= temp
->d_inode
;
1354 if (inode
&& ceph_snap(inode
) == CEPH_SNAPDIR
)
1355 len
++; /* slash only */
1356 else if (stop_on_nosnap
&& inode
&&
1357 ceph_snap(inode
) == CEPH_NOSNAP
)
1360 len
+= 1 + temp
->d_name
.len
;
1361 temp
= temp
->d_parent
;
1363 pr_err("build_path corrupt dentry %p\n", dentry
);
1364 return ERR_PTR(-EINVAL
);
1368 len
--; /* no leading '/' */
1370 path
= kmalloc(len
+1, GFP_NOFS
);
1372 return ERR_PTR(-ENOMEM
);
1374 path
[pos
] = 0; /* trailing null */
1375 for (temp
= dentry
; !IS_ROOT(temp
) && pos
!= 0; ) {
1376 struct inode
*inode
= temp
->d_inode
;
1378 if (inode
&& ceph_snap(inode
) == CEPH_SNAPDIR
) {
1379 dout("build_path path+%d: %p SNAPDIR\n",
1381 } else if (stop_on_nosnap
&& inode
&&
1382 ceph_snap(inode
) == CEPH_NOSNAP
) {
1385 pos
-= temp
->d_name
.len
;
1388 strncpy(path
+ pos
, temp
->d_name
.name
,
1393 temp
= temp
->d_parent
;
1395 pr_err("build_path corrupt dentry\n");
1397 return ERR_PTR(-EINVAL
);
1401 pr_err("build_path did not end path lookup where "
1402 "expected, namelen is %d, pos is %d\n", len
, pos
);
1403 /* presumably this is only possible if racing with a
1404 rename of one of the parent directories (we can not
1405 lock the dentries above us to prevent this, but
1406 retrying should be harmless) */
1411 *base
= ceph_ino(temp
->d_inode
);
1413 dout("build_path on %p %d built %llx '%.*s'\n",
1414 dentry
, atomic_read(&dentry
->d_count
), *base
, len
, path
);
1418 static int build_dentry_path(struct dentry
*dentry
,
1419 const char **ppath
, int *ppathlen
, u64
*pino
,
1424 if (ceph_snap(dentry
->d_parent
->d_inode
) == CEPH_NOSNAP
) {
1425 *pino
= ceph_ino(dentry
->d_parent
->d_inode
);
1426 *ppath
= dentry
->d_name
.name
;
1427 *ppathlen
= dentry
->d_name
.len
;
1430 path
= ceph_mdsc_build_path(dentry
, ppathlen
, pino
, 1);
1432 return PTR_ERR(path
);
1438 static int build_inode_path(struct inode
*inode
,
1439 const char **ppath
, int *ppathlen
, u64
*pino
,
1442 struct dentry
*dentry
;
1445 if (ceph_snap(inode
) == CEPH_NOSNAP
) {
1446 *pino
= ceph_ino(inode
);
1450 dentry
= d_find_alias(inode
);
1451 path
= ceph_mdsc_build_path(dentry
, ppathlen
, pino
, 1);
1454 return PTR_ERR(path
);
1461 * request arguments may be specified via an inode *, a dentry *, or
1462 * an explicit ino+path.
1464 static int set_request_path_attr(struct inode
*rinode
, struct dentry
*rdentry
,
1465 const char *rpath
, u64 rino
,
1466 const char **ppath
, int *pathlen
,
1467 u64
*ino
, int *freepath
)
1472 r
= build_inode_path(rinode
, ppath
, pathlen
, ino
, freepath
);
1473 dout(" inode %p %llx.%llx\n", rinode
, ceph_ino(rinode
),
1475 } else if (rdentry
) {
1476 r
= build_dentry_path(rdentry
, ppath
, pathlen
, ino
, freepath
);
1477 dout(" dentry %p %llx/%.*s\n", rdentry
, *ino
, *pathlen
,
1482 *pathlen
= strlen(rpath
);
1483 dout(" path %.*s\n", *pathlen
, rpath
);
1490 * called under mdsc->mutex
1492 static struct ceph_msg
*create_request_message(struct ceph_mds_client
*mdsc
,
1493 struct ceph_mds_request
*req
,
1496 struct ceph_msg
*msg
;
1497 struct ceph_mds_request_head
*head
;
1498 const char *path1
= NULL
;
1499 const char *path2
= NULL
;
1500 u64 ino1
= 0, ino2
= 0;
1501 int pathlen1
= 0, pathlen2
= 0;
1502 int freepath1
= 0, freepath2
= 0;
1508 ret
= set_request_path_attr(req
->r_inode
, req
->r_dentry
,
1509 req
->r_path1
, req
->r_ino1
.ino
,
1510 &path1
, &pathlen1
, &ino1
, &freepath1
);
1516 ret
= set_request_path_attr(NULL
, req
->r_old_dentry
,
1517 req
->r_path2
, req
->r_ino2
.ino
,
1518 &path2
, &pathlen2
, &ino2
, &freepath2
);
1524 len
= sizeof(*head
) +
1525 pathlen1
+ pathlen2
+ 2*(1 + sizeof(u32
) + sizeof(u64
));
1527 /* calculate (max) length for cap releases */
1528 len
+= sizeof(struct ceph_mds_request_release
) *
1529 (!!req
->r_inode_drop
+ !!req
->r_dentry_drop
+
1530 !!req
->r_old_inode_drop
+ !!req
->r_old_dentry_drop
);
1531 if (req
->r_dentry_drop
)
1532 len
+= req
->r_dentry
->d_name
.len
;
1533 if (req
->r_old_dentry_drop
)
1534 len
+= req
->r_old_dentry
->d_name
.len
;
1536 msg
= ceph_msg_new(CEPH_MSG_CLIENT_REQUEST
, len
, GFP_NOFS
);
1538 msg
= ERR_PTR(-ENOMEM
);
1542 msg
->hdr
.tid
= cpu_to_le64(req
->r_tid
);
1544 head
= msg
->front
.iov_base
;
1545 p
= msg
->front
.iov_base
+ sizeof(*head
);
1546 end
= msg
->front
.iov_base
+ msg
->front
.iov_len
;
1548 head
->mdsmap_epoch
= cpu_to_le32(mdsc
->mdsmap
->m_epoch
);
1549 head
->op
= cpu_to_le32(req
->r_op
);
1550 head
->caller_uid
= cpu_to_le32(current_fsuid());
1551 head
->caller_gid
= cpu_to_le32(current_fsgid());
1552 head
->args
= req
->r_args
;
1554 ceph_encode_filepath(&p
, end
, ino1
, path1
);
1555 ceph_encode_filepath(&p
, end
, ino2
, path2
);
1557 /* make note of release offset, in case we need to replay */
1558 req
->r_request_release_offset
= p
- msg
->front
.iov_base
;
1562 if (req
->r_inode_drop
)
1563 releases
+= ceph_encode_inode_release(&p
,
1564 req
->r_inode
? req
->r_inode
: req
->r_dentry
->d_inode
,
1565 mds
, req
->r_inode_drop
, req
->r_inode_unless
, 0);
1566 if (req
->r_dentry_drop
)
1567 releases
+= ceph_encode_dentry_release(&p
, req
->r_dentry
,
1568 mds
, req
->r_dentry_drop
, req
->r_dentry_unless
);
1569 if (req
->r_old_dentry_drop
)
1570 releases
+= ceph_encode_dentry_release(&p
, req
->r_old_dentry
,
1571 mds
, req
->r_old_dentry_drop
, req
->r_old_dentry_unless
);
1572 if (req
->r_old_inode_drop
)
1573 releases
+= ceph_encode_inode_release(&p
,
1574 req
->r_old_dentry
->d_inode
,
1575 mds
, req
->r_old_inode_drop
, req
->r_old_inode_unless
, 0);
1576 head
->num_releases
= cpu_to_le16(releases
);
1579 msg
->front
.iov_len
= p
- msg
->front
.iov_base
;
1580 msg
->hdr
.front_len
= cpu_to_le32(msg
->front
.iov_len
);
1582 msg
->pages
= req
->r_pages
;
1583 msg
->nr_pages
= req
->r_num_pages
;
1584 msg
->hdr
.data_len
= cpu_to_le32(req
->r_data_len
);
1585 msg
->hdr
.data_off
= cpu_to_le16(0);
1589 kfree((char *)path2
);
1592 kfree((char *)path1
);
1598 * called under mdsc->mutex if error, under no mutex if
1601 static void complete_request(struct ceph_mds_client
*mdsc
,
1602 struct ceph_mds_request
*req
)
1604 if (req
->r_callback
)
1605 req
->r_callback(mdsc
, req
);
1607 complete_all(&req
->r_completion
);
1611 * called under mdsc->mutex
1613 static int __prepare_send_request(struct ceph_mds_client
*mdsc
,
1614 struct ceph_mds_request
*req
,
1617 struct ceph_mds_request_head
*rhead
;
1618 struct ceph_msg
*msg
;
1623 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req
,
1624 req
->r_tid
, ceph_mds_op_name(req
->r_op
), req
->r_attempts
);
1626 if (req
->r_got_unsafe
) {
1628 * Replay. Do not regenerate message (and rebuild
1629 * paths, etc.); just use the original message.
1630 * Rebuilding paths will break for renames because
1631 * d_move mangles the src name.
1633 msg
= req
->r_request
;
1634 rhead
= msg
->front
.iov_base
;
1636 flags
= le32_to_cpu(rhead
->flags
);
1637 flags
|= CEPH_MDS_FLAG_REPLAY
;
1638 rhead
->flags
= cpu_to_le32(flags
);
1640 if (req
->r_target_inode
)
1641 rhead
->ino
= cpu_to_le64(ceph_ino(req
->r_target_inode
));
1643 rhead
->num_retry
= req
->r_attempts
- 1;
1645 /* remove cap/dentry releases from message */
1646 rhead
->num_releases
= 0;
1647 msg
->hdr
.front_len
= cpu_to_le32(req
->r_request_release_offset
);
1648 msg
->front
.iov_len
= req
->r_request_release_offset
;
1652 if (req
->r_request
) {
1653 ceph_msg_put(req
->r_request
);
1654 req
->r_request
= NULL
;
1656 msg
= create_request_message(mdsc
, req
, mds
);
1658 req
->r_err
= PTR_ERR(msg
);
1659 complete_request(mdsc
, req
);
1660 return PTR_ERR(msg
);
1662 req
->r_request
= msg
;
1664 rhead
= msg
->front
.iov_base
;
1665 rhead
->oldest_client_tid
= cpu_to_le64(__get_oldest_tid(mdsc
));
1666 if (req
->r_got_unsafe
)
1667 flags
|= CEPH_MDS_FLAG_REPLAY
;
1668 if (req
->r_locked_dir
)
1669 flags
|= CEPH_MDS_FLAG_WANT_DENTRY
;
1670 rhead
->flags
= cpu_to_le32(flags
);
1671 rhead
->num_fwd
= req
->r_num_fwd
;
1672 rhead
->num_retry
= req
->r_attempts
- 1;
1675 dout(" r_locked_dir = %p\n", req
->r_locked_dir
);
1680 * send request, or put it on the appropriate wait list.
1682 static int __do_request(struct ceph_mds_client
*mdsc
,
1683 struct ceph_mds_request
*req
)
1685 struct ceph_mds_session
*session
= NULL
;
1689 if (req
->r_err
|| req
->r_got_result
)
1692 if (req
->r_timeout
&&
1693 time_after_eq(jiffies
, req
->r_started
+ req
->r_timeout
)) {
1694 dout("do_request timed out\n");
1699 mds
= __choose_mds(mdsc
, req
);
1701 ceph_mdsmap_get_state(mdsc
->mdsmap
, mds
) < CEPH_MDS_STATE_ACTIVE
) {
1702 dout("do_request no mds or not active, waiting for map\n");
1703 list_add(&req
->r_wait
, &mdsc
->waiting_for_map
);
1707 /* get, open session */
1708 session
= __ceph_lookup_mds_session(mdsc
, mds
);
1710 session
= register_session(mdsc
, mds
);
1711 if (IS_ERR(session
)) {
1712 err
= PTR_ERR(session
);
1716 dout("do_request mds%d session %p state %s\n", mds
, session
,
1717 session_state_name(session
->s_state
));
1718 if (session
->s_state
!= CEPH_MDS_SESSION_OPEN
&&
1719 session
->s_state
!= CEPH_MDS_SESSION_HUNG
) {
1720 if (session
->s_state
== CEPH_MDS_SESSION_NEW
||
1721 session
->s_state
== CEPH_MDS_SESSION_CLOSING
)
1722 __open_session(mdsc
, session
);
1723 list_add(&req
->r_wait
, &session
->s_waiting
);
1728 req
->r_session
= get_session(session
);
1729 req
->r_resend_mds
= -1; /* forget any previous mds hint */
1731 if (req
->r_request_started
== 0) /* note request start time */
1732 req
->r_request_started
= jiffies
;
1734 err
= __prepare_send_request(mdsc
, req
, mds
);
1736 ceph_msg_get(req
->r_request
);
1737 ceph_con_send(&session
->s_con
, req
->r_request
);
1741 ceph_put_mds_session(session
);
1747 complete_request(mdsc
, req
);
1752 * called under mdsc->mutex
1754 static void __wake_requests(struct ceph_mds_client
*mdsc
,
1755 struct list_head
*head
)
1757 struct ceph_mds_request
*req
, *nreq
;
1759 list_for_each_entry_safe(req
, nreq
, head
, r_wait
) {
1760 list_del_init(&req
->r_wait
);
1761 __do_request(mdsc
, req
);
1766 * Wake up threads with requests pending for @mds, so that they can
1767 * resubmit their requests to a possibly different mds.
1769 static void kick_requests(struct ceph_mds_client
*mdsc
, int mds
)
1771 struct ceph_mds_request
*req
;
1774 dout("kick_requests mds%d\n", mds
);
1775 for (p
= rb_first(&mdsc
->request_tree
); p
; p
= rb_next(p
)) {
1776 req
= rb_entry(p
, struct ceph_mds_request
, r_node
);
1777 if (req
->r_got_unsafe
)
1779 if (req
->r_session
&&
1780 req
->r_session
->s_mds
== mds
) {
1781 dout(" kicking tid %llu\n", req
->r_tid
);
1782 put_request_session(req
);
1783 __do_request(mdsc
, req
);
1788 void ceph_mdsc_submit_request(struct ceph_mds_client
*mdsc
,
1789 struct ceph_mds_request
*req
)
1791 dout("submit_request on %p\n", req
);
1792 mutex_lock(&mdsc
->mutex
);
1793 __register_request(mdsc
, req
, NULL
);
1794 __do_request(mdsc
, req
);
1795 mutex_unlock(&mdsc
->mutex
);
1799 * Synchrously perform an mds request. Take care of all of the
1800 * session setup, forwarding, retry details.
1802 int ceph_mdsc_do_request(struct ceph_mds_client
*mdsc
,
1804 struct ceph_mds_request
*req
)
1808 dout("do_request on %p\n", req
);
1810 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
1812 ceph_get_cap_refs(ceph_inode(req
->r_inode
), CEPH_CAP_PIN
);
1813 if (req
->r_locked_dir
)
1814 ceph_get_cap_refs(ceph_inode(req
->r_locked_dir
), CEPH_CAP_PIN
);
1815 if (req
->r_old_dentry
)
1817 ceph_inode(req
->r_old_dentry
->d_parent
->d_inode
),
1821 mutex_lock(&mdsc
->mutex
);
1822 __register_request(mdsc
, req
, dir
);
1823 __do_request(mdsc
, req
);
1827 __unregister_request(mdsc
, req
);
1828 dout("do_request early error %d\n", err
);
1833 mutex_unlock(&mdsc
->mutex
);
1834 dout("do_request waiting\n");
1835 if (req
->r_timeout
) {
1836 err
= (long)wait_for_completion_killable_timeout(
1837 &req
->r_completion
, req
->r_timeout
);
1841 err
= wait_for_completion_killable(&req
->r_completion
);
1843 dout("do_request waited, got %d\n", err
);
1844 mutex_lock(&mdsc
->mutex
);
1846 /* only abort if we didn't race with a real reply */
1847 if (req
->r_got_result
) {
1848 err
= le32_to_cpu(req
->r_reply_info
.head
->result
);
1849 } else if (err
< 0) {
1850 dout("aborted request %lld with %d\n", req
->r_tid
, err
);
1853 * ensure we aren't running concurrently with
1854 * ceph_fill_trace or ceph_readdir_prepopulate, which
1855 * rely on locks (dir mutex) held by our caller.
1857 mutex_lock(&req
->r_fill_mutex
);
1859 req
->r_aborted
= true;
1860 mutex_unlock(&req
->r_fill_mutex
);
1862 if (req
->r_locked_dir
&&
1863 (req
->r_op
& CEPH_MDS_OP_WRITE
))
1864 ceph_invalidate_dir_request(req
);
1870 mutex_unlock(&mdsc
->mutex
);
1871 dout("do_request %p done, result %d\n", req
, err
);
1876 * Invalidate dir I_COMPLETE, dentry lease state on an aborted MDS
1877 * namespace request.
1879 void ceph_invalidate_dir_request(struct ceph_mds_request
*req
)
1881 struct inode
*inode
= req
->r_locked_dir
;
1882 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1884 dout("invalidate_dir_request %p (I_COMPLETE, lease(s))\n", inode
);
1885 spin_lock(&inode
->i_lock
);
1886 ci
->i_ceph_flags
&= ~CEPH_I_COMPLETE
;
1887 ci
->i_release_count
++;
1888 spin_unlock(&inode
->i_lock
);
1891 ceph_invalidate_dentry_lease(req
->r_dentry
);
1892 if (req
->r_old_dentry
)
1893 ceph_invalidate_dentry_lease(req
->r_old_dentry
);
1899 * We take the session mutex and parse and process the reply immediately.
1900 * This preserves the logical ordering of replies, capabilities, etc., sent
1901 * by the MDS as they are applied to our local cache.
1903 static void handle_reply(struct ceph_mds_session
*session
, struct ceph_msg
*msg
)
1905 struct ceph_mds_client
*mdsc
= session
->s_mdsc
;
1906 struct ceph_mds_request
*req
;
1907 struct ceph_mds_reply_head
*head
= msg
->front
.iov_base
;
1908 struct ceph_mds_reply_info_parsed
*rinfo
; /* parsed reply info */
1911 int mds
= session
->s_mds
;
1913 if (msg
->front
.iov_len
< sizeof(*head
)) {
1914 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
1919 /* get request, session */
1920 tid
= le64_to_cpu(msg
->hdr
.tid
);
1921 mutex_lock(&mdsc
->mutex
);
1922 req
= __lookup_request(mdsc
, tid
);
1924 dout("handle_reply on unknown tid %llu\n", tid
);
1925 mutex_unlock(&mdsc
->mutex
);
1928 dout("handle_reply %p\n", req
);
1930 /* correct session? */
1931 if (req
->r_session
!= session
) {
1932 pr_err("mdsc_handle_reply got %llu on session mds%d"
1933 " not mds%d\n", tid
, session
->s_mds
,
1934 req
->r_session
? req
->r_session
->s_mds
: -1);
1935 mutex_unlock(&mdsc
->mutex
);
1940 if ((req
->r_got_unsafe
&& !head
->safe
) ||
1941 (req
->r_got_safe
&& head
->safe
)) {
1942 pr_warning("got a dup %s reply on %llu from mds%d\n",
1943 head
->safe
? "safe" : "unsafe", tid
, mds
);
1944 mutex_unlock(&mdsc
->mutex
);
1947 if (req
->r_got_safe
&& !head
->safe
) {
1948 pr_warning("got unsafe after safe on %llu from mds%d\n",
1950 mutex_unlock(&mdsc
->mutex
);
1954 result
= le32_to_cpu(head
->result
);
1957 * Tolerate 2 consecutive ESTALEs from the same mds.
1958 * FIXME: we should be looking at the cap migrate_seq.
1960 if (result
== -ESTALE
) {
1961 req
->r_direct_mode
= USE_AUTH_MDS
;
1963 if (req
->r_num_stale
<= 2) {
1964 __do_request(mdsc
, req
);
1965 mutex_unlock(&mdsc
->mutex
);
1969 req
->r_num_stale
= 0;
1973 req
->r_got_safe
= true;
1974 __unregister_request(mdsc
, req
);
1975 complete_all(&req
->r_safe_completion
);
1977 if (req
->r_got_unsafe
) {
1979 * We already handled the unsafe response, now do the
1980 * cleanup. No need to examine the response; the MDS
1981 * doesn't include any result info in the safe
1982 * response. And even if it did, there is nothing
1983 * useful we could do with a revised return value.
1985 dout("got safe reply %llu, mds%d\n", tid
, mds
);
1986 list_del_init(&req
->r_unsafe_item
);
1988 /* last unsafe request during umount? */
1989 if (mdsc
->stopping
&& !__get_oldest_req(mdsc
))
1990 complete_all(&mdsc
->safe_umount_waiters
);
1991 mutex_unlock(&mdsc
->mutex
);
1995 req
->r_got_unsafe
= true;
1996 list_add_tail(&req
->r_unsafe_item
, &req
->r_session
->s_unsafe
);
1999 dout("handle_reply tid %lld result %d\n", tid
, result
);
2000 rinfo
= &req
->r_reply_info
;
2001 err
= parse_reply_info(msg
, rinfo
);
2002 mutex_unlock(&mdsc
->mutex
);
2004 mutex_lock(&session
->s_mutex
);
2006 pr_err("mdsc_handle_reply got corrupt reply mds%d\n", mds
);
2012 if (rinfo
->snapblob_len
) {
2013 down_write(&mdsc
->snap_rwsem
);
2014 ceph_update_snap_trace(mdsc
, rinfo
->snapblob
,
2015 rinfo
->snapblob
+ rinfo
->snapblob_len
,
2016 le32_to_cpu(head
->op
) == CEPH_MDS_OP_RMSNAP
);
2017 downgrade_write(&mdsc
->snap_rwsem
);
2019 down_read(&mdsc
->snap_rwsem
);
2022 /* insert trace into our cache */
2023 mutex_lock(&req
->r_fill_mutex
);
2024 err
= ceph_fill_trace(mdsc
->client
->sb
, req
, req
->r_session
);
2026 if (result
== 0 && rinfo
->dir_nr
)
2027 ceph_readdir_prepopulate(req
, req
->r_session
);
2028 ceph_unreserve_caps(mdsc
, &req
->r_caps_reservation
);
2030 mutex_unlock(&req
->r_fill_mutex
);
2032 up_read(&mdsc
->snap_rwsem
);
2034 mutex_lock(&mdsc
->mutex
);
2035 if (!req
->r_aborted
) {
2041 req
->r_got_result
= true;
2044 dout("reply arrived after request %lld was aborted\n", tid
);
2046 mutex_unlock(&mdsc
->mutex
);
2048 ceph_add_cap_releases(mdsc
, req
->r_session
);
2049 mutex_unlock(&session
->s_mutex
);
2051 /* kick calling process */
2052 complete_request(mdsc
, req
);
2054 ceph_mdsc_put_request(req
);
2061 * handle mds notification that our request has been forwarded.
2063 static void handle_forward(struct ceph_mds_client
*mdsc
,
2064 struct ceph_mds_session
*session
,
2065 struct ceph_msg
*msg
)
2067 struct ceph_mds_request
*req
;
2068 u64 tid
= le64_to_cpu(msg
->hdr
.tid
);
2072 void *p
= msg
->front
.iov_base
;
2073 void *end
= p
+ msg
->front
.iov_len
;
2075 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
2076 next_mds
= ceph_decode_32(&p
);
2077 fwd_seq
= ceph_decode_32(&p
);
2079 mutex_lock(&mdsc
->mutex
);
2080 req
= __lookup_request(mdsc
, tid
);
2082 dout("forward tid %llu to mds%d - req dne\n", tid
, next_mds
);
2083 goto out
; /* dup reply? */
2086 if (req
->r_aborted
) {
2087 dout("forward tid %llu aborted, unregistering\n", tid
);
2088 __unregister_request(mdsc
, req
);
2089 } else if (fwd_seq
<= req
->r_num_fwd
) {
2090 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
2091 tid
, next_mds
, req
->r_num_fwd
, fwd_seq
);
2093 /* resend. forward race not possible; mds would drop */
2094 dout("forward tid %llu to mds%d (we resend)\n", tid
, next_mds
);
2096 BUG_ON(req
->r_got_result
);
2097 req
->r_num_fwd
= fwd_seq
;
2098 req
->r_resend_mds
= next_mds
;
2099 put_request_session(req
);
2100 __do_request(mdsc
, req
);
2102 ceph_mdsc_put_request(req
);
2104 mutex_unlock(&mdsc
->mutex
);
2108 pr_err("mdsc_handle_forward decode error err=%d\n", err
);
2112 * handle a mds session control message
2114 static void handle_session(struct ceph_mds_session
*session
,
2115 struct ceph_msg
*msg
)
2117 struct ceph_mds_client
*mdsc
= session
->s_mdsc
;
2120 int mds
= session
->s_mds
;
2121 struct ceph_mds_session_head
*h
= msg
->front
.iov_base
;
2125 if (msg
->front
.iov_len
!= sizeof(*h
))
2127 op
= le32_to_cpu(h
->op
);
2128 seq
= le64_to_cpu(h
->seq
);
2130 mutex_lock(&mdsc
->mutex
);
2131 if (op
== CEPH_SESSION_CLOSE
)
2132 __unregister_session(mdsc
, session
);
2133 /* FIXME: this ttl calculation is generous */
2134 session
->s_ttl
= jiffies
+ HZ
*mdsc
->mdsmap
->m_session_autoclose
;
2135 mutex_unlock(&mdsc
->mutex
);
2137 mutex_lock(&session
->s_mutex
);
2139 dout("handle_session mds%d %s %p state %s seq %llu\n",
2140 mds
, ceph_session_op_name(op
), session
,
2141 session_state_name(session
->s_state
), seq
);
2143 if (session
->s_state
== CEPH_MDS_SESSION_HUNG
) {
2144 session
->s_state
= CEPH_MDS_SESSION_OPEN
;
2145 pr_info("mds%d came back\n", session
->s_mds
);
2149 case CEPH_SESSION_OPEN
:
2150 if (session
->s_state
== CEPH_MDS_SESSION_RECONNECTING
)
2151 pr_info("mds%d reconnect success\n", session
->s_mds
);
2152 session
->s_state
= CEPH_MDS_SESSION_OPEN
;
2153 renewed_caps(mdsc
, session
, 0);
2156 __close_session(mdsc
, session
);
2159 case CEPH_SESSION_RENEWCAPS
:
2160 if (session
->s_renew_seq
== seq
)
2161 renewed_caps(mdsc
, session
, 1);
2164 case CEPH_SESSION_CLOSE
:
2165 if (session
->s_state
== CEPH_MDS_SESSION_RECONNECTING
)
2166 pr_info("mds%d reconnect denied\n", session
->s_mds
);
2167 remove_session_caps(session
);
2168 wake
= 1; /* for good measure */
2169 complete_all(&mdsc
->session_close_waiters
);
2170 kick_requests(mdsc
, mds
);
2173 case CEPH_SESSION_STALE
:
2174 pr_info("mds%d caps went stale, renewing\n",
2176 spin_lock(&session
->s_cap_lock
);
2177 session
->s_cap_gen
++;
2178 session
->s_cap_ttl
= 0;
2179 spin_unlock(&session
->s_cap_lock
);
2180 send_renew_caps(mdsc
, session
);
2183 case CEPH_SESSION_RECALL_STATE
:
2184 trim_caps(mdsc
, session
, le32_to_cpu(h
->max_caps
));
2188 pr_err("mdsc_handle_session bad op %d mds%d\n", op
, mds
);
2192 mutex_unlock(&session
->s_mutex
);
2194 mutex_lock(&mdsc
->mutex
);
2195 __wake_requests(mdsc
, &session
->s_waiting
);
2196 mutex_unlock(&mdsc
->mutex
);
2201 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds
,
2202 (int)msg
->front
.iov_len
);
2209 * called under session->mutex.
2211 static void replay_unsafe_requests(struct ceph_mds_client
*mdsc
,
2212 struct ceph_mds_session
*session
)
2214 struct ceph_mds_request
*req
, *nreq
;
2217 dout("replay_unsafe_requests mds%d\n", session
->s_mds
);
2219 mutex_lock(&mdsc
->mutex
);
2220 list_for_each_entry_safe(req
, nreq
, &session
->s_unsafe
, r_unsafe_item
) {
2221 err
= __prepare_send_request(mdsc
, req
, session
->s_mds
);
2223 ceph_msg_get(req
->r_request
);
2224 ceph_con_send(&session
->s_con
, req
->r_request
);
2227 mutex_unlock(&mdsc
->mutex
);
2231 * Encode information about a cap for a reconnect with the MDS.
2233 static int encode_caps_cb(struct inode
*inode
, struct ceph_cap
*cap
,
2236 struct ceph_mds_cap_reconnect rec
;
2237 struct ceph_inode_info
*ci
;
2238 struct ceph_pagelist
*pagelist
= arg
;
2242 struct dentry
*dentry
;
2246 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2247 inode
, ceph_vinop(inode
), cap
, cap
->cap_id
,
2248 ceph_cap_string(cap
->issued
));
2249 err
= ceph_pagelist_encode_64(pagelist
, ceph_ino(inode
));
2253 dentry
= d_find_alias(inode
);
2255 path
= ceph_mdsc_build_path(dentry
, &pathlen
, &pathbase
, 0);
2257 err
= PTR_ERR(path
);
2264 err
= ceph_pagelist_encode_string(pagelist
, path
, pathlen
);
2268 spin_lock(&inode
->i_lock
);
2269 cap
->seq
= 0; /* reset cap seq */
2270 cap
->issue_seq
= 0; /* and issue_seq */
2271 rec
.cap_id
= cpu_to_le64(cap
->cap_id
);
2272 rec
.pathbase
= cpu_to_le64(pathbase
);
2273 rec
.wanted
= cpu_to_le32(__ceph_caps_wanted(ci
));
2274 rec
.issued
= cpu_to_le32(cap
->issued
);
2275 rec
.size
= cpu_to_le64(inode
->i_size
);
2276 ceph_encode_timespec(&rec
.mtime
, &inode
->i_mtime
);
2277 ceph_encode_timespec(&rec
.atime
, &inode
->i_atime
);
2278 rec
.snaprealm
= cpu_to_le64(ci
->i_snap_realm
->ino
);
2279 spin_unlock(&inode
->i_lock
);
2281 err
= ceph_pagelist_append(pagelist
, &rec
, sizeof(rec
));
2291 * If an MDS fails and recovers, clients need to reconnect in order to
2292 * reestablish shared state. This includes all caps issued through
2293 * this session _and_ the snap_realm hierarchy. Because it's not
2294 * clear which snap realms the mds cares about, we send everything we
2295 * know about.. that ensures we'll then get any new info the
2296 * recovering MDS might have.
2298 * This is a relatively heavyweight operation, but it's rare.
2300 * called with mdsc->mutex held.
2302 static void send_mds_reconnect(struct ceph_mds_client
*mdsc
,
2303 struct ceph_mds_session
*session
)
2305 struct ceph_msg
*reply
;
2307 int mds
= session
->s_mds
;
2309 struct ceph_pagelist
*pagelist
;
2311 pr_info("mds%d reconnect start\n", mds
);
2313 pagelist
= kmalloc(sizeof(*pagelist
), GFP_NOFS
);
2315 goto fail_nopagelist
;
2316 ceph_pagelist_init(pagelist
);
2318 reply
= ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT
, 0, GFP_NOFS
);
2322 mutex_lock(&session
->s_mutex
);
2323 session
->s_state
= CEPH_MDS_SESSION_RECONNECTING
;
2326 ceph_con_open(&session
->s_con
,
2327 ceph_mdsmap_get_addr(mdsc
->mdsmap
, mds
));
2329 /* replay unsafe requests */
2330 replay_unsafe_requests(mdsc
, session
);
2332 down_read(&mdsc
->snap_rwsem
);
2334 dout("session %p state %s\n", session
,
2335 session_state_name(session
->s_state
));
2337 /* drop old cap expires; we're about to reestablish that state */
2338 discard_cap_releases(mdsc
, session
);
2340 /* traverse this session's caps */
2341 err
= ceph_pagelist_encode_32(pagelist
, session
->s_nr_caps
);
2344 err
= iterate_session_caps(session
, encode_caps_cb
, pagelist
);
2349 * snaprealms. we provide mds with the ino, seq (version), and
2350 * parent for all of our realms. If the mds has any newer info,
2353 for (p
= rb_first(&mdsc
->snap_realms
); p
; p
= rb_next(p
)) {
2354 struct ceph_snap_realm
*realm
=
2355 rb_entry(p
, struct ceph_snap_realm
, node
);
2356 struct ceph_mds_snaprealm_reconnect sr_rec
;
2358 dout(" adding snap realm %llx seq %lld parent %llx\n",
2359 realm
->ino
, realm
->seq
, realm
->parent_ino
);
2360 sr_rec
.ino
= cpu_to_le64(realm
->ino
);
2361 sr_rec
.seq
= cpu_to_le64(realm
->seq
);
2362 sr_rec
.parent
= cpu_to_le64(realm
->parent_ino
);
2363 err
= ceph_pagelist_append(pagelist
, &sr_rec
, sizeof(sr_rec
));
2368 reply
->pagelist
= pagelist
;
2369 reply
->hdr
.data_len
= cpu_to_le32(pagelist
->length
);
2370 reply
->nr_pages
= calc_pages_for(0, pagelist
->length
);
2371 ceph_con_send(&session
->s_con
, reply
);
2373 mutex_unlock(&session
->s_mutex
);
2375 mutex_lock(&mdsc
->mutex
);
2376 __wake_requests(mdsc
, &session
->s_waiting
);
2377 mutex_unlock(&mdsc
->mutex
);
2379 up_read(&mdsc
->snap_rwsem
);
2383 ceph_msg_put(reply
);
2384 up_read(&mdsc
->snap_rwsem
);
2385 mutex_unlock(&session
->s_mutex
);
2387 ceph_pagelist_release(pagelist
);
2390 pr_err("error %d preparing reconnect for mds%d\n", err
, mds
);
2396 * compare old and new mdsmaps, kicking requests
2397 * and closing out old connections as necessary
2399 * called under mdsc->mutex.
2401 static void check_new_map(struct ceph_mds_client
*mdsc
,
2402 struct ceph_mdsmap
*newmap
,
2403 struct ceph_mdsmap
*oldmap
)
2406 int oldstate
, newstate
;
2407 struct ceph_mds_session
*s
;
2409 dout("check_new_map new %u old %u\n",
2410 newmap
->m_epoch
, oldmap
->m_epoch
);
2412 for (i
= 0; i
< oldmap
->m_max_mds
&& i
< mdsc
->max_sessions
; i
++) {
2413 if (mdsc
->sessions
[i
] == NULL
)
2415 s
= mdsc
->sessions
[i
];
2416 oldstate
= ceph_mdsmap_get_state(oldmap
, i
);
2417 newstate
= ceph_mdsmap_get_state(newmap
, i
);
2419 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
2420 i
, ceph_mds_state_name(oldstate
),
2421 ceph_mdsmap_is_laggy(oldmap
, i
) ? " (laggy)" : "",
2422 ceph_mds_state_name(newstate
),
2423 ceph_mdsmap_is_laggy(newmap
, i
) ? " (laggy)" : "",
2424 session_state_name(s
->s_state
));
2426 if (memcmp(ceph_mdsmap_get_addr(oldmap
, i
),
2427 ceph_mdsmap_get_addr(newmap
, i
),
2428 sizeof(struct ceph_entity_addr
))) {
2429 if (s
->s_state
== CEPH_MDS_SESSION_OPENING
) {
2430 /* the session never opened, just close it
2432 __wake_requests(mdsc
, &s
->s_waiting
);
2433 __unregister_session(mdsc
, s
);
2436 mutex_unlock(&mdsc
->mutex
);
2437 mutex_lock(&s
->s_mutex
);
2438 mutex_lock(&mdsc
->mutex
);
2439 ceph_con_close(&s
->s_con
);
2440 mutex_unlock(&s
->s_mutex
);
2441 s
->s_state
= CEPH_MDS_SESSION_RESTARTING
;
2444 /* kick any requests waiting on the recovering mds */
2445 kick_requests(mdsc
, i
);
2446 } else if (oldstate
== newstate
) {
2447 continue; /* nothing new with this mds */
2453 if (s
->s_state
== CEPH_MDS_SESSION_RESTARTING
&&
2454 newstate
>= CEPH_MDS_STATE_RECONNECT
) {
2455 mutex_unlock(&mdsc
->mutex
);
2456 send_mds_reconnect(mdsc
, s
);
2457 mutex_lock(&mdsc
->mutex
);
2461 * kick request on any mds that has gone active.
2463 if (oldstate
< CEPH_MDS_STATE_ACTIVE
&&
2464 newstate
>= CEPH_MDS_STATE_ACTIVE
) {
2465 if (oldstate
!= CEPH_MDS_STATE_CREATING
&&
2466 oldstate
!= CEPH_MDS_STATE_STARTING
)
2467 pr_info("mds%d recovery completed\n", s
->s_mds
);
2468 kick_requests(mdsc
, i
);
2469 ceph_kick_flushing_caps(mdsc
, s
);
2470 wake_up_session_caps(s
, 1);
2482 * caller must hold session s_mutex, dentry->d_lock
2484 void __ceph_mdsc_drop_dentry_lease(struct dentry
*dentry
)
2486 struct ceph_dentry_info
*di
= ceph_dentry(dentry
);
2488 ceph_put_mds_session(di
->lease_session
);
2489 di
->lease_session
= NULL
;
2492 static void handle_lease(struct ceph_mds_client
*mdsc
,
2493 struct ceph_mds_session
*session
,
2494 struct ceph_msg
*msg
)
2496 struct super_block
*sb
= mdsc
->client
->sb
;
2497 struct inode
*inode
;
2498 struct ceph_inode_info
*ci
;
2499 struct dentry
*parent
, *dentry
;
2500 struct ceph_dentry_info
*di
;
2501 int mds
= session
->s_mds
;
2502 struct ceph_mds_lease
*h
= msg
->front
.iov_base
;
2504 struct ceph_vino vino
;
2509 dout("handle_lease from mds%d\n", mds
);
2512 if (msg
->front
.iov_len
< sizeof(*h
) + sizeof(u32
))
2514 vino
.ino
= le64_to_cpu(h
->ino
);
2515 vino
.snap
= CEPH_NOSNAP
;
2516 mask
= le16_to_cpu(h
->mask
);
2517 seq
= le32_to_cpu(h
->seq
);
2518 dname
.name
= (void *)h
+ sizeof(*h
) + sizeof(u32
);
2519 dname
.len
= msg
->front
.iov_len
- sizeof(*h
) - sizeof(u32
);
2520 if (dname
.len
!= get_unaligned_le32(h
+1))
2523 mutex_lock(&session
->s_mutex
);
2527 inode
= ceph_find_inode(sb
, vino
);
2528 dout("handle_lease %s, mask %d, ino %llx %p %.*s\n",
2529 ceph_lease_op_name(h
->action
), mask
, vino
.ino
, inode
,
2530 dname
.len
, dname
.name
);
2531 if (inode
== NULL
) {
2532 dout("handle_lease no inode %llx\n", vino
.ino
);
2535 ci
= ceph_inode(inode
);
2538 parent
= d_find_alias(inode
);
2540 dout("no parent dentry on inode %p\n", inode
);
2542 goto release
; /* hrm... */
2544 dname
.hash
= full_name_hash(dname
.name
, dname
.len
);
2545 dentry
= d_lookup(parent
, &dname
);
2550 spin_lock(&dentry
->d_lock
);
2551 di
= ceph_dentry(dentry
);
2552 switch (h
->action
) {
2553 case CEPH_MDS_LEASE_REVOKE
:
2554 if (di
&& di
->lease_session
== session
) {
2555 if (ceph_seq_cmp(di
->lease_seq
, seq
) > 0)
2556 h
->seq
= cpu_to_le32(di
->lease_seq
);
2557 __ceph_mdsc_drop_dentry_lease(dentry
);
2562 case CEPH_MDS_LEASE_RENEW
:
2563 if (di
&& di
->lease_session
== session
&&
2564 di
->lease_gen
== session
->s_cap_gen
&&
2565 di
->lease_renew_from
&&
2566 di
->lease_renew_after
== 0) {
2567 unsigned long duration
=
2568 le32_to_cpu(h
->duration_ms
) * HZ
/ 1000;
2570 di
->lease_seq
= seq
;
2571 dentry
->d_time
= di
->lease_renew_from
+ duration
;
2572 di
->lease_renew_after
= di
->lease_renew_from
+
2574 di
->lease_renew_from
= 0;
2578 spin_unlock(&dentry
->d_lock
);
2585 /* let's just reuse the same message */
2586 h
->action
= CEPH_MDS_LEASE_REVOKE_ACK
;
2588 ceph_con_send(&session
->s_con
, msg
);
2592 mutex_unlock(&session
->s_mutex
);
2596 pr_err("corrupt lease message\n");
2600 void ceph_mdsc_lease_send_msg(struct ceph_mds_session
*session
,
2601 struct inode
*inode
,
2602 struct dentry
*dentry
, char action
,
2605 struct ceph_msg
*msg
;
2606 struct ceph_mds_lease
*lease
;
2607 int len
= sizeof(*lease
) + sizeof(u32
);
2610 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
2611 inode
, dentry
, ceph_lease_op_name(action
), session
->s_mds
);
2612 dnamelen
= dentry
->d_name
.len
;
2615 msg
= ceph_msg_new(CEPH_MSG_CLIENT_LEASE
, len
, GFP_NOFS
);
2618 lease
= msg
->front
.iov_base
;
2619 lease
->action
= action
;
2620 lease
->mask
= cpu_to_le16(1);
2621 lease
->ino
= cpu_to_le64(ceph_vino(inode
).ino
);
2622 lease
->first
= lease
->last
= cpu_to_le64(ceph_vino(inode
).snap
);
2623 lease
->seq
= cpu_to_le32(seq
);
2624 put_unaligned_le32(dnamelen
, lease
+ 1);
2625 memcpy((void *)(lease
+ 1) + 4, dentry
->d_name
.name
, dnamelen
);
2628 * if this is a preemptive lease RELEASE, no need to
2629 * flush request stream, since the actual request will
2632 msg
->more_to_follow
= (action
== CEPH_MDS_LEASE_RELEASE
);
2634 ceph_con_send(&session
->s_con
, msg
);
2638 * Preemptively release a lease we expect to invalidate anyway.
2639 * Pass @inode always, @dentry is optional.
2641 void ceph_mdsc_lease_release(struct ceph_mds_client
*mdsc
, struct inode
*inode
,
2642 struct dentry
*dentry
, int mask
)
2644 struct ceph_dentry_info
*di
;
2645 struct ceph_mds_session
*session
;
2648 BUG_ON(inode
== NULL
);
2649 BUG_ON(dentry
== NULL
);
2652 /* is dentry lease valid? */
2653 spin_lock(&dentry
->d_lock
);
2654 di
= ceph_dentry(dentry
);
2655 if (!di
|| !di
->lease_session
||
2656 di
->lease_session
->s_mds
< 0 ||
2657 di
->lease_gen
!= di
->lease_session
->s_cap_gen
||
2658 !time_before(jiffies
, dentry
->d_time
)) {
2659 dout("lease_release inode %p dentry %p -- "
2661 inode
, dentry
, mask
);
2662 spin_unlock(&dentry
->d_lock
);
2666 /* we do have a lease on this dentry; note mds and seq */
2667 session
= ceph_get_mds_session(di
->lease_session
);
2668 seq
= di
->lease_seq
;
2669 __ceph_mdsc_drop_dentry_lease(dentry
);
2670 spin_unlock(&dentry
->d_lock
);
2672 dout("lease_release inode %p dentry %p mask %d to mds%d\n",
2673 inode
, dentry
, mask
, session
->s_mds
);
2674 ceph_mdsc_lease_send_msg(session
, inode
, dentry
,
2675 CEPH_MDS_LEASE_RELEASE
, seq
);
2676 ceph_put_mds_session(session
);
2680 * drop all leases (and dentry refs) in preparation for umount
2682 static void drop_leases(struct ceph_mds_client
*mdsc
)
2686 dout("drop_leases\n");
2687 mutex_lock(&mdsc
->mutex
);
2688 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
2689 struct ceph_mds_session
*s
= __ceph_lookup_mds_session(mdsc
, i
);
2692 mutex_unlock(&mdsc
->mutex
);
2693 mutex_lock(&s
->s_mutex
);
2694 mutex_unlock(&s
->s_mutex
);
2695 ceph_put_mds_session(s
);
2696 mutex_lock(&mdsc
->mutex
);
2698 mutex_unlock(&mdsc
->mutex
);
2704 * delayed work -- periodically trim expired leases, renew caps with mds
2706 static void schedule_delayed(struct ceph_mds_client
*mdsc
)
2709 unsigned hz
= round_jiffies_relative(HZ
* delay
);
2710 schedule_delayed_work(&mdsc
->delayed_work
, hz
);
2713 static void delayed_work(struct work_struct
*work
)
2716 struct ceph_mds_client
*mdsc
=
2717 container_of(work
, struct ceph_mds_client
, delayed_work
.work
);
2721 dout("mdsc delayed_work\n");
2722 ceph_check_delayed_caps(mdsc
);
2724 mutex_lock(&mdsc
->mutex
);
2725 renew_interval
= mdsc
->mdsmap
->m_session_timeout
>> 2;
2726 renew_caps
= time_after_eq(jiffies
, HZ
*renew_interval
+
2727 mdsc
->last_renew_caps
);
2729 mdsc
->last_renew_caps
= jiffies
;
2731 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
2732 struct ceph_mds_session
*s
= __ceph_lookup_mds_session(mdsc
, i
);
2735 if (s
->s_state
== CEPH_MDS_SESSION_CLOSING
) {
2736 dout("resending session close request for mds%d\n",
2738 request_close_session(mdsc
, s
);
2739 ceph_put_mds_session(s
);
2742 if (s
->s_ttl
&& time_after(jiffies
, s
->s_ttl
)) {
2743 if (s
->s_state
== CEPH_MDS_SESSION_OPEN
) {
2744 s
->s_state
= CEPH_MDS_SESSION_HUNG
;
2745 pr_info("mds%d hung\n", s
->s_mds
);
2748 if (s
->s_state
< CEPH_MDS_SESSION_OPEN
) {
2749 /* this mds is failed or recovering, just wait */
2750 ceph_put_mds_session(s
);
2753 mutex_unlock(&mdsc
->mutex
);
2755 mutex_lock(&s
->s_mutex
);
2757 send_renew_caps(mdsc
, s
);
2759 ceph_con_keepalive(&s
->s_con
);
2760 ceph_add_cap_releases(mdsc
, s
);
2761 if (s
->s_state
== CEPH_MDS_SESSION_OPEN
||
2762 s
->s_state
== CEPH_MDS_SESSION_HUNG
)
2763 ceph_send_cap_releases(mdsc
, s
);
2764 mutex_unlock(&s
->s_mutex
);
2765 ceph_put_mds_session(s
);
2767 mutex_lock(&mdsc
->mutex
);
2769 mutex_unlock(&mdsc
->mutex
);
2771 schedule_delayed(mdsc
);
2775 int ceph_mdsc_init(struct ceph_mds_client
*mdsc
, struct ceph_client
*client
)
2777 mdsc
->client
= client
;
2778 mutex_init(&mdsc
->mutex
);
2779 mdsc
->mdsmap
= kzalloc(sizeof(*mdsc
->mdsmap
), GFP_NOFS
);
2780 if (mdsc
->mdsmap
== NULL
)
2783 init_completion(&mdsc
->safe_umount_waiters
);
2784 init_completion(&mdsc
->session_close_waiters
);
2785 INIT_LIST_HEAD(&mdsc
->waiting_for_map
);
2786 mdsc
->sessions
= NULL
;
2787 mdsc
->max_sessions
= 0;
2789 init_rwsem(&mdsc
->snap_rwsem
);
2790 mdsc
->snap_realms
= RB_ROOT
;
2791 INIT_LIST_HEAD(&mdsc
->snap_empty
);
2792 spin_lock_init(&mdsc
->snap_empty_lock
);
2794 mdsc
->request_tree
= RB_ROOT
;
2795 INIT_DELAYED_WORK(&mdsc
->delayed_work
, delayed_work
);
2796 mdsc
->last_renew_caps
= jiffies
;
2797 INIT_LIST_HEAD(&mdsc
->cap_delay_list
);
2798 spin_lock_init(&mdsc
->cap_delay_lock
);
2799 INIT_LIST_HEAD(&mdsc
->snap_flush_list
);
2800 spin_lock_init(&mdsc
->snap_flush_lock
);
2801 mdsc
->cap_flush_seq
= 0;
2802 INIT_LIST_HEAD(&mdsc
->cap_dirty
);
2803 mdsc
->num_cap_flushing
= 0;
2804 spin_lock_init(&mdsc
->cap_dirty_lock
);
2805 init_waitqueue_head(&mdsc
->cap_flushing_wq
);
2806 spin_lock_init(&mdsc
->dentry_lru_lock
);
2807 INIT_LIST_HEAD(&mdsc
->dentry_lru
);
2809 ceph_caps_init(mdsc
);
2810 ceph_adjust_min_caps(mdsc
, client
->min_caps
);
2816 * Wait for safe replies on open mds requests. If we time out, drop
2817 * all requests from the tree to avoid dangling dentry refs.
2819 static void wait_requests(struct ceph_mds_client
*mdsc
)
2821 struct ceph_mds_request
*req
;
2822 struct ceph_client
*client
= mdsc
->client
;
2824 mutex_lock(&mdsc
->mutex
);
2825 if (__get_oldest_req(mdsc
)) {
2826 mutex_unlock(&mdsc
->mutex
);
2828 dout("wait_requests waiting for requests\n");
2829 wait_for_completion_timeout(&mdsc
->safe_umount_waiters
,
2830 client
->mount_args
->mount_timeout
* HZ
);
2832 /* tear down remaining requests */
2833 mutex_lock(&mdsc
->mutex
);
2834 while ((req
= __get_oldest_req(mdsc
))) {
2835 dout("wait_requests timed out on tid %llu\n",
2837 __unregister_request(mdsc
, req
);
2840 mutex_unlock(&mdsc
->mutex
);
2841 dout("wait_requests done\n");
2845 * called before mount is ro, and before dentries are torn down.
2846 * (hmm, does this still race with new lookups?)
2848 void ceph_mdsc_pre_umount(struct ceph_mds_client
*mdsc
)
2850 dout("pre_umount\n");
2854 ceph_flush_dirty_caps(mdsc
);
2855 wait_requests(mdsc
);
2858 * wait for reply handlers to drop their request refs and
2859 * their inode/dcache refs
2865 * wait for all write mds requests to flush.
2867 static void wait_unsafe_requests(struct ceph_mds_client
*mdsc
, u64 want_tid
)
2869 struct ceph_mds_request
*req
= NULL
, *nextreq
;
2872 mutex_lock(&mdsc
->mutex
);
2873 dout("wait_unsafe_requests want %lld\n", want_tid
);
2875 req
= __get_oldest_req(mdsc
);
2876 while (req
&& req
->r_tid
<= want_tid
) {
2877 /* find next request */
2878 n
= rb_next(&req
->r_node
);
2880 nextreq
= rb_entry(n
, struct ceph_mds_request
, r_node
);
2883 if ((req
->r_op
& CEPH_MDS_OP_WRITE
)) {
2885 ceph_mdsc_get_request(req
);
2887 ceph_mdsc_get_request(nextreq
);
2888 mutex_unlock(&mdsc
->mutex
);
2889 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
2890 req
->r_tid
, want_tid
);
2891 wait_for_completion(&req
->r_safe_completion
);
2892 mutex_lock(&mdsc
->mutex
);
2893 ceph_mdsc_put_request(req
);
2895 break; /* next dne before, so we're done! */
2896 if (RB_EMPTY_NODE(&nextreq
->r_node
)) {
2897 /* next request was removed from tree */
2898 ceph_mdsc_put_request(nextreq
);
2901 ceph_mdsc_put_request(nextreq
); /* won't go away */
2905 mutex_unlock(&mdsc
->mutex
);
2906 dout("wait_unsafe_requests done\n");
2909 void ceph_mdsc_sync(struct ceph_mds_client
*mdsc
)
2911 u64 want_tid
, want_flush
;
2913 if (mdsc
->client
->mount_state
== CEPH_MOUNT_SHUTDOWN
)
2917 mutex_lock(&mdsc
->mutex
);
2918 want_tid
= mdsc
->last_tid
;
2919 want_flush
= mdsc
->cap_flush_seq
;
2920 mutex_unlock(&mdsc
->mutex
);
2921 dout("sync want tid %lld flush_seq %lld\n", want_tid
, want_flush
);
2923 ceph_flush_dirty_caps(mdsc
);
2925 wait_unsafe_requests(mdsc
, want_tid
);
2926 wait_event(mdsc
->cap_flushing_wq
, check_cap_flush(mdsc
, want_flush
));
2931 * called after sb is ro.
2933 void ceph_mdsc_close_sessions(struct ceph_mds_client
*mdsc
)
2935 struct ceph_mds_session
*session
;
2938 struct ceph_client
*client
= mdsc
->client
;
2939 unsigned long started
, timeout
= client
->mount_args
->mount_timeout
* HZ
;
2941 dout("close_sessions\n");
2943 mutex_lock(&mdsc
->mutex
);
2945 /* close sessions */
2947 while (time_before(jiffies
, started
+ timeout
)) {
2948 dout("closing sessions\n");
2950 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
2951 session
= __ceph_lookup_mds_session(mdsc
, i
);
2954 mutex_unlock(&mdsc
->mutex
);
2955 mutex_lock(&session
->s_mutex
);
2956 __close_session(mdsc
, session
);
2957 mutex_unlock(&session
->s_mutex
);
2958 ceph_put_mds_session(session
);
2959 mutex_lock(&mdsc
->mutex
);
2965 if (client
->mount_state
== CEPH_MOUNT_SHUTDOWN
)
2968 dout("waiting for sessions to close\n");
2969 mutex_unlock(&mdsc
->mutex
);
2970 wait_for_completion_timeout(&mdsc
->session_close_waiters
,
2972 mutex_lock(&mdsc
->mutex
);
2975 /* tear down remaining sessions */
2976 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
2977 if (mdsc
->sessions
[i
]) {
2978 session
= get_session(mdsc
->sessions
[i
]);
2979 __unregister_session(mdsc
, session
);
2980 mutex_unlock(&mdsc
->mutex
);
2981 mutex_lock(&session
->s_mutex
);
2982 remove_session_caps(session
);
2983 mutex_unlock(&session
->s_mutex
);
2984 ceph_put_mds_session(session
);
2985 mutex_lock(&mdsc
->mutex
);
2989 WARN_ON(!list_empty(&mdsc
->cap_delay_list
));
2991 mutex_unlock(&mdsc
->mutex
);
2993 ceph_cleanup_empty_realms(mdsc
);
2995 cancel_delayed_work_sync(&mdsc
->delayed_work
); /* cancel timer */
3000 void ceph_mdsc_stop(struct ceph_mds_client
*mdsc
)
3003 cancel_delayed_work_sync(&mdsc
->delayed_work
); /* cancel timer */
3005 ceph_mdsmap_destroy(mdsc
->mdsmap
);
3006 kfree(mdsc
->sessions
);
3007 ceph_caps_finalize(mdsc
);
3012 * handle mds map update.
3014 void ceph_mdsc_handle_map(struct ceph_mds_client
*mdsc
, struct ceph_msg
*msg
)
3018 void *p
= msg
->front
.iov_base
;
3019 void *end
= p
+ msg
->front
.iov_len
;
3020 struct ceph_mdsmap
*newmap
, *oldmap
;
3021 struct ceph_fsid fsid
;
3024 ceph_decode_need(&p
, end
, sizeof(fsid
)+2*sizeof(u32
), bad
);
3025 ceph_decode_copy(&p
, &fsid
, sizeof(fsid
));
3026 if (ceph_check_fsid(mdsc
->client
, &fsid
) < 0)
3028 epoch
= ceph_decode_32(&p
);
3029 maplen
= ceph_decode_32(&p
);
3030 dout("handle_map epoch %u len %d\n", epoch
, (int)maplen
);
3032 /* do we need it? */
3033 ceph_monc_got_mdsmap(&mdsc
->client
->monc
, epoch
);
3034 mutex_lock(&mdsc
->mutex
);
3035 if (mdsc
->mdsmap
&& epoch
<= mdsc
->mdsmap
->m_epoch
) {
3036 dout("handle_map epoch %u <= our %u\n",
3037 epoch
, mdsc
->mdsmap
->m_epoch
);
3038 mutex_unlock(&mdsc
->mutex
);
3042 newmap
= ceph_mdsmap_decode(&p
, end
);
3043 if (IS_ERR(newmap
)) {
3044 err
= PTR_ERR(newmap
);
3048 /* swap into place */
3050 oldmap
= mdsc
->mdsmap
;
3051 mdsc
->mdsmap
= newmap
;
3052 check_new_map(mdsc
, newmap
, oldmap
);
3053 ceph_mdsmap_destroy(oldmap
);
3055 mdsc
->mdsmap
= newmap
; /* first mds map */
3057 mdsc
->client
->sb
->s_maxbytes
= mdsc
->mdsmap
->m_max_file_size
;
3059 __wake_requests(mdsc
, &mdsc
->waiting_for_map
);
3061 mutex_unlock(&mdsc
->mutex
);
3062 schedule_delayed(mdsc
);
3066 mutex_unlock(&mdsc
->mutex
);
3068 pr_err("error decoding mdsmap %d\n", err
);
3072 static struct ceph_connection
*con_get(struct ceph_connection
*con
)
3074 struct ceph_mds_session
*s
= con
->private;
3076 if (get_session(s
)) {
3077 dout("mdsc con_get %p ok (%d)\n", s
, atomic_read(&s
->s_ref
));
3080 dout("mdsc con_get %p FAIL\n", s
);
3084 static void con_put(struct ceph_connection
*con
)
3086 struct ceph_mds_session
*s
= con
->private;
3088 ceph_put_mds_session(s
);
3089 dout("mdsc con_put %p (%d)\n", s
, atomic_read(&s
->s_ref
));
3093 * if the client is unresponsive for long enough, the mds will kill
3094 * the session entirely.
3096 static void peer_reset(struct ceph_connection
*con
)
3098 struct ceph_mds_session
*s
= con
->private;
3099 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
3101 pr_warning("mds%d closed our session\n", s
->s_mds
);
3102 send_mds_reconnect(mdsc
, s
);
3105 static void dispatch(struct ceph_connection
*con
, struct ceph_msg
*msg
)
3107 struct ceph_mds_session
*s
= con
->private;
3108 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
3109 int type
= le16_to_cpu(msg
->hdr
.type
);
3111 mutex_lock(&mdsc
->mutex
);
3112 if (__verify_registered_session(mdsc
, s
) < 0) {
3113 mutex_unlock(&mdsc
->mutex
);
3116 mutex_unlock(&mdsc
->mutex
);
3119 case CEPH_MSG_MDS_MAP
:
3120 ceph_mdsc_handle_map(mdsc
, msg
);
3122 case CEPH_MSG_CLIENT_SESSION
:
3123 handle_session(s
, msg
);
3125 case CEPH_MSG_CLIENT_REPLY
:
3126 handle_reply(s
, msg
);
3128 case CEPH_MSG_CLIENT_REQUEST_FORWARD
:
3129 handle_forward(mdsc
, s
, msg
);
3131 case CEPH_MSG_CLIENT_CAPS
:
3132 ceph_handle_caps(s
, msg
);
3134 case CEPH_MSG_CLIENT_SNAP
:
3135 ceph_handle_snap(mdsc
, s
, msg
);
3137 case CEPH_MSG_CLIENT_LEASE
:
3138 handle_lease(mdsc
, s
, msg
);
3142 pr_err("received unknown message type %d %s\n", type
,
3143 ceph_msg_type_name(type
));
3152 static int get_authorizer(struct ceph_connection
*con
,
3153 void **buf
, int *len
, int *proto
,
3154 void **reply_buf
, int *reply_len
, int force_new
)
3156 struct ceph_mds_session
*s
= con
->private;
3157 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
3158 struct ceph_auth_client
*ac
= mdsc
->client
->monc
.auth
;
3161 if (force_new
&& s
->s_authorizer
) {
3162 ac
->ops
->destroy_authorizer(ac
, s
->s_authorizer
);
3163 s
->s_authorizer
= NULL
;
3165 if (s
->s_authorizer
== NULL
) {
3166 if (ac
->ops
->create_authorizer
) {
3167 ret
= ac
->ops
->create_authorizer(
3168 ac
, CEPH_ENTITY_TYPE_MDS
,
3170 &s
->s_authorizer_buf
,
3171 &s
->s_authorizer_buf_len
,
3172 &s
->s_authorizer_reply_buf
,
3173 &s
->s_authorizer_reply_buf_len
);
3179 *proto
= ac
->protocol
;
3180 *buf
= s
->s_authorizer_buf
;
3181 *len
= s
->s_authorizer_buf_len
;
3182 *reply_buf
= s
->s_authorizer_reply_buf
;
3183 *reply_len
= s
->s_authorizer_reply_buf_len
;
3188 static int verify_authorizer_reply(struct ceph_connection
*con
, int len
)
3190 struct ceph_mds_session
*s
= con
->private;
3191 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
3192 struct ceph_auth_client
*ac
= mdsc
->client
->monc
.auth
;
3194 return ac
->ops
->verify_authorizer_reply(ac
, s
->s_authorizer
, len
);
3197 static int invalidate_authorizer(struct ceph_connection
*con
)
3199 struct ceph_mds_session
*s
= con
->private;
3200 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
3201 struct ceph_auth_client
*ac
= mdsc
->client
->monc
.auth
;
3203 if (ac
->ops
->invalidate_authorizer
)
3204 ac
->ops
->invalidate_authorizer(ac
, CEPH_ENTITY_TYPE_MDS
);
3206 return ceph_monc_validate_auth(&mdsc
->client
->monc
);
3209 static const struct ceph_connection_operations mds_con_ops
= {
3212 .dispatch
= dispatch
,
3213 .get_authorizer
= get_authorizer
,
3214 .verify_authorizer_reply
= verify_authorizer_reply
,
3215 .invalidate_authorizer
= invalidate_authorizer
,
3216 .peer_reset
= peer_reset
,