1 #include "ceph_debug.h"
3 #include <linux/wait.h>
4 #include <linux/slab.h>
5 #include <linux/sched.h>
6 #include <linux/smp_lock.h>
8 #include "mds_client.h"
9 #include "mon_client.h"
11 #include "messenger.h"
17 * A cluster of MDS (metadata server) daemons is responsible for
18 * managing the file system namespace (the directory hierarchy and
19 * inodes) and for coordinating shared access to storage. Metadata is
20 * partitioning hierarchically across a number of servers, and that
21 * partition varies over time as the cluster adjusts the distribution
22 * in order to balance load.
24 * The MDS client is primarily responsible to managing synchronous
25 * metadata requests for operations like open, unlink, and so forth.
26 * If there is a MDS failure, we find out about it when we (possibly
27 * request and) receive a new MDS map, and can resubmit affected
30 * For the most part, though, we take advantage of a lossless
31 * communications channel to the MDS, and do not need to worry about
32 * timing out or resubmitting requests.
34 * We maintain a stateful "session" with each MDS we interact with.
35 * Within each session, we sent periodic heartbeat messages to ensure
36 * any capabilities or leases we have been issues remain valid. If
37 * the session times out and goes stale, our leases and capabilities
38 * are no longer valid.
41 struct ceph_reconnect_state
{
42 struct ceph_pagelist
*pagelist
;
46 static void __wake_requests(struct ceph_mds_client
*mdsc
,
47 struct list_head
*head
);
49 static const struct ceph_connection_operations mds_con_ops
;
57 * parse individual inode info
59 static int parse_reply_info_in(void **p
, void *end
,
60 struct ceph_mds_reply_info_in
*info
)
65 *p
+= sizeof(struct ceph_mds_reply_inode
) +
66 sizeof(*info
->in
->fragtree
.splits
) *
67 le32_to_cpu(info
->in
->fragtree
.nsplits
);
69 ceph_decode_32_safe(p
, end
, info
->symlink_len
, bad
);
70 ceph_decode_need(p
, end
, info
->symlink_len
, bad
);
72 *p
+= info
->symlink_len
;
74 ceph_decode_32_safe(p
, end
, info
->xattr_len
, bad
);
75 ceph_decode_need(p
, end
, info
->xattr_len
, bad
);
76 info
->xattr_data
= *p
;
77 *p
+= info
->xattr_len
;
84 * parse a normal reply, which may contain a (dir+)dentry and/or a
87 static int parse_reply_info_trace(void **p
, void *end
,
88 struct ceph_mds_reply_info_parsed
*info
)
92 if (info
->head
->is_dentry
) {
93 err
= parse_reply_info_in(p
, end
, &info
->diri
);
97 if (unlikely(*p
+ sizeof(*info
->dirfrag
) > end
))
100 *p
+= sizeof(*info
->dirfrag
) +
101 sizeof(u32
)*le32_to_cpu(info
->dirfrag
->ndist
);
102 if (unlikely(*p
> end
))
105 ceph_decode_32_safe(p
, end
, info
->dname_len
, bad
);
106 ceph_decode_need(p
, end
, info
->dname_len
, bad
);
108 *p
+= info
->dname_len
;
110 *p
+= sizeof(*info
->dlease
);
113 if (info
->head
->is_target
) {
114 err
= parse_reply_info_in(p
, end
, &info
->targeti
);
119 if (unlikely(*p
!= end
))
126 pr_err("problem parsing mds trace %d\n", err
);
131 * parse readdir results
133 static int parse_reply_info_dir(void **p
, void *end
,
134 struct ceph_mds_reply_info_parsed
*info
)
140 if (*p
+ sizeof(*info
->dir_dir
) > end
)
142 *p
+= sizeof(*info
->dir_dir
) +
143 sizeof(u32
)*le32_to_cpu(info
->dir_dir
->ndist
);
147 ceph_decode_need(p
, end
, sizeof(num
) + 2, bad
);
148 num
= ceph_decode_32(p
);
149 info
->dir_end
= ceph_decode_8(p
);
150 info
->dir_complete
= ceph_decode_8(p
);
154 /* alloc large array */
156 info
->dir_in
= kcalloc(num
, sizeof(*info
->dir_in
) +
157 sizeof(*info
->dir_dname
) +
158 sizeof(*info
->dir_dname_len
) +
159 sizeof(*info
->dir_dlease
),
161 if (info
->dir_in
== NULL
) {
165 info
->dir_dname
= (void *)(info
->dir_in
+ num
);
166 info
->dir_dname_len
= (void *)(info
->dir_dname
+ num
);
167 info
->dir_dlease
= (void *)(info
->dir_dname_len
+ num
);
171 ceph_decode_need(p
, end
, sizeof(u32
)*2, bad
);
172 info
->dir_dname_len
[i
] = ceph_decode_32(p
);
173 ceph_decode_need(p
, end
, info
->dir_dname_len
[i
], bad
);
174 info
->dir_dname
[i
] = *p
;
175 *p
+= info
->dir_dname_len
[i
];
176 dout("parsed dir dname '%.*s'\n", info
->dir_dname_len
[i
],
178 info
->dir_dlease
[i
] = *p
;
179 *p
+= sizeof(struct ceph_mds_reply_lease
);
182 err
= parse_reply_info_in(p
, end
, &info
->dir_in
[i
]);
197 pr_err("problem parsing dir contents %d\n", err
);
202 * parse entire mds reply
204 static int parse_reply_info(struct ceph_msg
*msg
,
205 struct ceph_mds_reply_info_parsed
*info
)
211 info
->head
= msg
->front
.iov_base
;
212 p
= msg
->front
.iov_base
+ sizeof(struct ceph_mds_reply_head
);
213 end
= p
+ msg
->front
.iov_len
- sizeof(struct ceph_mds_reply_head
);
216 ceph_decode_32_safe(&p
, end
, len
, bad
);
218 err
= parse_reply_info_trace(&p
, p
+len
, info
);
224 ceph_decode_32_safe(&p
, end
, len
, bad
);
226 err
= parse_reply_info_dir(&p
, p
+len
, info
);
232 ceph_decode_32_safe(&p
, end
, len
, bad
);
233 info
->snapblob_len
= len
;
244 pr_err("mds parse_reply err %d\n", err
);
248 static void destroy_reply_info(struct ceph_mds_reply_info_parsed
*info
)
257 static const char *session_state_name(int s
)
260 case CEPH_MDS_SESSION_NEW
: return "new";
261 case CEPH_MDS_SESSION_OPENING
: return "opening";
262 case CEPH_MDS_SESSION_OPEN
: return "open";
263 case CEPH_MDS_SESSION_HUNG
: return "hung";
264 case CEPH_MDS_SESSION_CLOSING
: return "closing";
265 case CEPH_MDS_SESSION_RESTARTING
: return "restarting";
266 case CEPH_MDS_SESSION_RECONNECTING
: return "reconnecting";
267 default: return "???";
271 static struct ceph_mds_session
*get_session(struct ceph_mds_session
*s
)
273 if (atomic_inc_not_zero(&s
->s_ref
)) {
274 dout("mdsc get_session %p %d -> %d\n", s
,
275 atomic_read(&s
->s_ref
)-1, atomic_read(&s
->s_ref
));
278 dout("mdsc get_session %p 0 -- FAIL", s
);
283 void ceph_put_mds_session(struct ceph_mds_session
*s
)
285 dout("mdsc put_session %p %d -> %d\n", s
,
286 atomic_read(&s
->s_ref
), atomic_read(&s
->s_ref
)-1);
287 if (atomic_dec_and_test(&s
->s_ref
)) {
289 s
->s_mdsc
->client
->monc
.auth
->ops
->destroy_authorizer(
290 s
->s_mdsc
->client
->monc
.auth
, s
->s_authorizer
);
296 * called under mdsc->mutex
298 struct ceph_mds_session
*__ceph_lookup_mds_session(struct ceph_mds_client
*mdsc
,
301 struct ceph_mds_session
*session
;
303 if (mds
>= mdsc
->max_sessions
|| mdsc
->sessions
[mds
] == NULL
)
305 session
= mdsc
->sessions
[mds
];
306 dout("lookup_mds_session %p %d\n", session
,
307 atomic_read(&session
->s_ref
));
308 get_session(session
);
312 static bool __have_session(struct ceph_mds_client
*mdsc
, int mds
)
314 if (mds
>= mdsc
->max_sessions
)
316 return mdsc
->sessions
[mds
];
319 static int __verify_registered_session(struct ceph_mds_client
*mdsc
,
320 struct ceph_mds_session
*s
)
322 if (s
->s_mds
>= mdsc
->max_sessions
||
323 mdsc
->sessions
[s
->s_mds
] != s
)
329 * create+register a new session for given mds.
330 * called under mdsc->mutex.
332 static struct ceph_mds_session
*register_session(struct ceph_mds_client
*mdsc
,
335 struct ceph_mds_session
*s
;
337 s
= kzalloc(sizeof(*s
), GFP_NOFS
);
339 return ERR_PTR(-ENOMEM
);
342 s
->s_state
= CEPH_MDS_SESSION_NEW
;
345 mutex_init(&s
->s_mutex
);
347 ceph_con_init(mdsc
->client
->msgr
, &s
->s_con
);
348 s
->s_con
.private = s
;
349 s
->s_con
.ops
= &mds_con_ops
;
350 s
->s_con
.peer_name
.type
= CEPH_ENTITY_TYPE_MDS
;
351 s
->s_con
.peer_name
.num
= cpu_to_le64(mds
);
353 spin_lock_init(&s
->s_cap_lock
);
356 s
->s_renew_requested
= 0;
358 INIT_LIST_HEAD(&s
->s_caps
);
361 atomic_set(&s
->s_ref
, 1);
362 INIT_LIST_HEAD(&s
->s_waiting
);
363 INIT_LIST_HEAD(&s
->s_unsafe
);
364 s
->s_num_cap_releases
= 0;
365 s
->s_cap_iterator
= NULL
;
366 INIT_LIST_HEAD(&s
->s_cap_releases
);
367 INIT_LIST_HEAD(&s
->s_cap_releases_done
);
368 INIT_LIST_HEAD(&s
->s_cap_flushing
);
369 INIT_LIST_HEAD(&s
->s_cap_snaps_flushing
);
371 dout("register_session mds%d\n", mds
);
372 if (mds
>= mdsc
->max_sessions
) {
373 int newmax
= 1 << get_count_order(mds
+1);
374 struct ceph_mds_session
**sa
;
376 dout("register_session realloc to %d\n", newmax
);
377 sa
= kcalloc(newmax
, sizeof(void *), GFP_NOFS
);
380 if (mdsc
->sessions
) {
381 memcpy(sa
, mdsc
->sessions
,
382 mdsc
->max_sessions
* sizeof(void *));
383 kfree(mdsc
->sessions
);
386 mdsc
->max_sessions
= newmax
;
388 mdsc
->sessions
[mds
] = s
;
389 atomic_inc(&s
->s_ref
); /* one ref to sessions[], one to caller */
391 ceph_con_open(&s
->s_con
, ceph_mdsmap_get_addr(mdsc
->mdsmap
, mds
));
397 return ERR_PTR(-ENOMEM
);
401 * called under mdsc->mutex
403 static void __unregister_session(struct ceph_mds_client
*mdsc
,
404 struct ceph_mds_session
*s
)
406 dout("__unregister_session mds%d %p\n", s
->s_mds
, s
);
407 BUG_ON(mdsc
->sessions
[s
->s_mds
] != s
);
408 mdsc
->sessions
[s
->s_mds
] = NULL
;
409 ceph_con_close(&s
->s_con
);
410 ceph_put_mds_session(s
);
414 * drop session refs in request.
416 * should be last request ref, or hold mdsc->mutex
418 static void put_request_session(struct ceph_mds_request
*req
)
420 if (req
->r_session
) {
421 ceph_put_mds_session(req
->r_session
);
422 req
->r_session
= NULL
;
426 void ceph_mdsc_release_request(struct kref
*kref
)
428 struct ceph_mds_request
*req
= container_of(kref
,
429 struct ceph_mds_request
,
432 ceph_msg_put(req
->r_request
);
434 ceph_msg_put(req
->r_reply
);
435 destroy_reply_info(&req
->r_reply_info
);
438 ceph_put_cap_refs(ceph_inode(req
->r_inode
),
442 if (req
->r_locked_dir
)
443 ceph_put_cap_refs(ceph_inode(req
->r_locked_dir
),
445 if (req
->r_target_inode
)
446 iput(req
->r_target_inode
);
449 if (req
->r_old_dentry
) {
451 ceph_inode(req
->r_old_dentry
->d_parent
->d_inode
),
453 dput(req
->r_old_dentry
);
457 put_request_session(req
);
458 ceph_unreserve_caps(req
->r_mdsc
, &req
->r_caps_reservation
);
463 * lookup session, bump ref if found.
465 * called under mdsc->mutex.
467 static struct ceph_mds_request
*__lookup_request(struct ceph_mds_client
*mdsc
,
470 struct ceph_mds_request
*req
;
471 struct rb_node
*n
= mdsc
->request_tree
.rb_node
;
474 req
= rb_entry(n
, struct ceph_mds_request
, r_node
);
475 if (tid
< req
->r_tid
)
477 else if (tid
> req
->r_tid
)
480 ceph_mdsc_get_request(req
);
487 static void __insert_request(struct ceph_mds_client
*mdsc
,
488 struct ceph_mds_request
*new)
490 struct rb_node
**p
= &mdsc
->request_tree
.rb_node
;
491 struct rb_node
*parent
= NULL
;
492 struct ceph_mds_request
*req
= NULL
;
496 req
= rb_entry(parent
, struct ceph_mds_request
, r_node
);
497 if (new->r_tid
< req
->r_tid
)
499 else if (new->r_tid
> req
->r_tid
)
505 rb_link_node(&new->r_node
, parent
, p
);
506 rb_insert_color(&new->r_node
, &mdsc
->request_tree
);
510 * Register an in-flight request, and assign a tid. Link to directory
511 * are modifying (if any).
513 * Called under mdsc->mutex.
515 static void __register_request(struct ceph_mds_client
*mdsc
,
516 struct ceph_mds_request
*req
,
519 req
->r_tid
= ++mdsc
->last_tid
;
521 ceph_reserve_caps(mdsc
, &req
->r_caps_reservation
,
523 dout("__register_request %p tid %lld\n", req
, req
->r_tid
);
524 ceph_mdsc_get_request(req
);
525 __insert_request(mdsc
, req
);
528 struct ceph_inode_info
*ci
= ceph_inode(dir
);
530 spin_lock(&ci
->i_unsafe_lock
);
531 req
->r_unsafe_dir
= dir
;
532 list_add_tail(&req
->r_unsafe_dir_item
, &ci
->i_unsafe_dirops
);
533 spin_unlock(&ci
->i_unsafe_lock
);
537 static void __unregister_request(struct ceph_mds_client
*mdsc
,
538 struct ceph_mds_request
*req
)
540 dout("__unregister_request %p tid %lld\n", req
, req
->r_tid
);
541 rb_erase(&req
->r_node
, &mdsc
->request_tree
);
542 RB_CLEAR_NODE(&req
->r_node
);
544 if (req
->r_unsafe_dir
) {
545 struct ceph_inode_info
*ci
= ceph_inode(req
->r_unsafe_dir
);
547 spin_lock(&ci
->i_unsafe_lock
);
548 list_del_init(&req
->r_unsafe_dir_item
);
549 spin_unlock(&ci
->i_unsafe_lock
);
552 ceph_mdsc_put_request(req
);
556 * Choose mds to send request to next. If there is a hint set in the
557 * request (e.g., due to a prior forward hint from the mds), use that.
558 * Otherwise, consult frag tree and/or caps to identify the
559 * appropriate mds. If all else fails, choose randomly.
561 * Called under mdsc->mutex.
563 struct dentry
*get_nonsnap_parent(struct dentry
*dentry
)
565 while (!IS_ROOT(dentry
) && ceph_snap(dentry
->d_inode
) != CEPH_NOSNAP
)
566 dentry
= dentry
->d_parent
;
570 static int __choose_mds(struct ceph_mds_client
*mdsc
,
571 struct ceph_mds_request
*req
)
574 struct ceph_inode_info
*ci
;
575 struct ceph_cap
*cap
;
576 int mode
= req
->r_direct_mode
;
578 u32 hash
= req
->r_direct_hash
;
579 bool is_hash
= req
->r_direct_is_hash
;
582 * is there a specific mds we should try? ignore hint if we have
583 * no session and the mds is not up (active or recovering).
585 if (req
->r_resend_mds
>= 0 &&
586 (__have_session(mdsc
, req
->r_resend_mds
) ||
587 ceph_mdsmap_get_state(mdsc
->mdsmap
, req
->r_resend_mds
) > 0)) {
588 dout("choose_mds using resend_mds mds%d\n",
590 return req
->r_resend_mds
;
593 if (mode
== USE_RANDOM_MDS
)
598 inode
= req
->r_inode
;
599 } else if (req
->r_dentry
) {
600 struct inode
*dir
= req
->r_dentry
->d_parent
->d_inode
;
602 if (dir
->i_sb
!= mdsc
->client
->sb
) {
604 inode
= req
->r_dentry
->d_inode
;
605 } else if (ceph_snap(dir
) != CEPH_NOSNAP
) {
606 /* direct snapped/virtual snapdir requests
607 * based on parent dir inode */
609 get_nonsnap_parent(req
->r_dentry
->d_parent
);
611 dout("__choose_mds using nonsnap parent %p\n", inode
);
612 } else if (req
->r_dentry
->d_inode
) {
614 inode
= req
->r_dentry
->d_inode
;
618 hash
= req
->r_dentry
->d_name
.hash
;
623 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode
, (int)is_hash
,
627 ci
= ceph_inode(inode
);
629 if (is_hash
&& S_ISDIR(inode
->i_mode
)) {
630 struct ceph_inode_frag frag
;
633 ceph_choose_frag(ci
, hash
, &frag
, &found
);
635 if (mode
== USE_ANY_MDS
&& frag
.ndist
> 0) {
638 /* choose a random replica */
639 get_random_bytes(&r
, 1);
642 dout("choose_mds %p %llx.%llx "
643 "frag %u mds%d (%d/%d)\n",
644 inode
, ceph_vinop(inode
),
650 /* since this file/dir wasn't known to be
651 * replicated, then we want to look for the
652 * authoritative mds. */
655 /* choose auth mds */
657 dout("choose_mds %p %llx.%llx "
658 "frag %u mds%d (auth)\n",
659 inode
, ceph_vinop(inode
), frag
.frag
, mds
);
665 spin_lock(&inode
->i_lock
);
667 if (mode
== USE_AUTH_MDS
)
668 cap
= ci
->i_auth_cap
;
669 if (!cap
&& !RB_EMPTY_ROOT(&ci
->i_caps
))
670 cap
= rb_entry(rb_first(&ci
->i_caps
), struct ceph_cap
, ci_node
);
672 spin_unlock(&inode
->i_lock
);
675 mds
= cap
->session
->s_mds
;
676 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
677 inode
, ceph_vinop(inode
), mds
,
678 cap
== ci
->i_auth_cap
? "auth " : "", cap
);
679 spin_unlock(&inode
->i_lock
);
683 mds
= ceph_mdsmap_get_random_mds(mdsc
->mdsmap
);
684 dout("choose_mds chose random mds%d\n", mds
);
692 static struct ceph_msg
*create_session_msg(u32 op
, u64 seq
)
694 struct ceph_msg
*msg
;
695 struct ceph_mds_session_head
*h
;
697 msg
= ceph_msg_new(CEPH_MSG_CLIENT_SESSION
, sizeof(*h
), GFP_NOFS
);
699 pr_err("create_session_msg ENOMEM creating msg\n");
702 h
= msg
->front
.iov_base
;
703 h
->op
= cpu_to_le32(op
);
704 h
->seq
= cpu_to_le64(seq
);
709 * send session open request.
711 * called under mdsc->mutex
713 static int __open_session(struct ceph_mds_client
*mdsc
,
714 struct ceph_mds_session
*session
)
716 struct ceph_msg
*msg
;
718 int mds
= session
->s_mds
;
720 /* wait for mds to go active? */
721 mstate
= ceph_mdsmap_get_state(mdsc
->mdsmap
, mds
);
722 dout("open_session to mds%d (%s)\n", mds
,
723 ceph_mds_state_name(mstate
));
724 session
->s_state
= CEPH_MDS_SESSION_OPENING
;
725 session
->s_renew_requested
= jiffies
;
727 /* send connect message */
728 msg
= create_session_msg(CEPH_SESSION_REQUEST_OPEN
, session
->s_seq
);
731 ceph_con_send(&session
->s_con
, msg
);
736 * open sessions for any export targets for the given mds
738 * called under mdsc->mutex
740 static void __open_export_target_sessions(struct ceph_mds_client
*mdsc
,
741 struct ceph_mds_session
*session
)
743 struct ceph_mds_info
*mi
;
744 struct ceph_mds_session
*ts
;
745 int i
, mds
= session
->s_mds
;
748 if (mds
>= mdsc
->mdsmap
->m_max_mds
)
750 mi
= &mdsc
->mdsmap
->m_info
[mds
];
751 dout("open_export_target_sessions for mds%d (%d targets)\n",
752 session
->s_mds
, mi
->num_export_targets
);
754 for (i
= 0; i
< mi
->num_export_targets
; i
++) {
755 target
= mi
->export_targets
[i
];
756 ts
= __ceph_lookup_mds_session(mdsc
, target
);
758 ts
= register_session(mdsc
, target
);
762 if (session
->s_state
== CEPH_MDS_SESSION_NEW
||
763 session
->s_state
== CEPH_MDS_SESSION_CLOSING
)
764 __open_session(mdsc
, session
);
766 dout(" mds%d target mds%d %p is %s\n", session
->s_mds
,
767 i
, ts
, session_state_name(ts
->s_state
));
768 ceph_put_mds_session(ts
);
772 void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client
*mdsc
,
773 struct ceph_mds_session
*session
)
775 mutex_lock(&mdsc
->mutex
);
776 __open_export_target_sessions(mdsc
, session
);
777 mutex_unlock(&mdsc
->mutex
);
785 * Free preallocated cap messages assigned to this session
787 static void cleanup_cap_releases(struct ceph_mds_session
*session
)
789 struct ceph_msg
*msg
;
791 spin_lock(&session
->s_cap_lock
);
792 while (!list_empty(&session
->s_cap_releases
)) {
793 msg
= list_first_entry(&session
->s_cap_releases
,
794 struct ceph_msg
, list_head
);
795 list_del_init(&msg
->list_head
);
798 while (!list_empty(&session
->s_cap_releases_done
)) {
799 msg
= list_first_entry(&session
->s_cap_releases_done
,
800 struct ceph_msg
, list_head
);
801 list_del_init(&msg
->list_head
);
804 spin_unlock(&session
->s_cap_lock
);
808 * Helper to safely iterate over all caps associated with a session, with
809 * special care taken to handle a racing __ceph_remove_cap().
811 * Caller must hold session s_mutex.
813 static int iterate_session_caps(struct ceph_mds_session
*session
,
814 int (*cb
)(struct inode
*, struct ceph_cap
*,
818 struct ceph_cap
*cap
;
819 struct inode
*inode
, *last_inode
= NULL
;
820 struct ceph_cap
*old_cap
= NULL
;
823 dout("iterate_session_caps %p mds%d\n", session
, session
->s_mds
);
824 spin_lock(&session
->s_cap_lock
);
825 p
= session
->s_caps
.next
;
826 while (p
!= &session
->s_caps
) {
827 cap
= list_entry(p
, struct ceph_cap
, session_caps
);
828 inode
= igrab(&cap
->ci
->vfs_inode
);
833 session
->s_cap_iterator
= cap
;
834 spin_unlock(&session
->s_cap_lock
);
841 ceph_put_cap(session
->s_mdsc
, old_cap
);
845 ret
= cb(inode
, cap
, arg
);
848 spin_lock(&session
->s_cap_lock
);
850 if (cap
->ci
== NULL
) {
851 dout("iterate_session_caps finishing cap %p removal\n",
853 BUG_ON(cap
->session
!= session
);
854 list_del_init(&cap
->session_caps
);
855 session
->s_nr_caps
--;
857 old_cap
= cap
; /* put_cap it w/o locks held */
864 session
->s_cap_iterator
= NULL
;
865 spin_unlock(&session
->s_cap_lock
);
870 ceph_put_cap(session
->s_mdsc
, old_cap
);
875 static int remove_session_caps_cb(struct inode
*inode
, struct ceph_cap
*cap
,
878 struct ceph_inode_info
*ci
= ceph_inode(inode
);
881 dout("removing cap %p, ci is %p, inode is %p\n",
882 cap
, ci
, &ci
->vfs_inode
);
883 spin_lock(&inode
->i_lock
);
884 __ceph_remove_cap(cap
);
885 if (!__ceph_is_any_real_caps(ci
)) {
886 struct ceph_mds_client
*mdsc
=
887 &ceph_sb_to_client(inode
->i_sb
)->mdsc
;
889 spin_lock(&mdsc
->cap_dirty_lock
);
890 if (!list_empty(&ci
->i_dirty_item
)) {
891 pr_info(" dropping dirty %s state for %p %lld\n",
892 ceph_cap_string(ci
->i_dirty_caps
),
893 inode
, ceph_ino(inode
));
894 ci
->i_dirty_caps
= 0;
895 list_del_init(&ci
->i_dirty_item
);
898 if (!list_empty(&ci
->i_flushing_item
)) {
899 pr_info(" dropping dirty+flushing %s state for %p %lld\n",
900 ceph_cap_string(ci
->i_flushing_caps
),
901 inode
, ceph_ino(inode
));
902 ci
->i_flushing_caps
= 0;
903 list_del_init(&ci
->i_flushing_item
);
904 mdsc
->num_cap_flushing
--;
907 if (drop
&& ci
->i_wrbuffer_ref
) {
908 pr_info(" dropping dirty data for %p %lld\n",
909 inode
, ceph_ino(inode
));
910 ci
->i_wrbuffer_ref
= 0;
911 ci
->i_wrbuffer_ref_head
= 0;
914 spin_unlock(&mdsc
->cap_dirty_lock
);
916 spin_unlock(&inode
->i_lock
);
923 * caller must hold session s_mutex
925 static void remove_session_caps(struct ceph_mds_session
*session
)
927 dout("remove_session_caps on %p\n", session
);
928 iterate_session_caps(session
, remove_session_caps_cb
, NULL
);
929 BUG_ON(session
->s_nr_caps
> 0);
930 BUG_ON(!list_empty(&session
->s_cap_flushing
));
931 cleanup_cap_releases(session
);
935 * wake up any threads waiting on this session's caps. if the cap is
936 * old (didn't get renewed on the client reconnect), remove it now.
938 * caller must hold s_mutex.
940 static int wake_up_session_cb(struct inode
*inode
, struct ceph_cap
*cap
,
943 struct ceph_inode_info
*ci
= ceph_inode(inode
);
945 wake_up_all(&ci
->i_cap_wq
);
947 spin_lock(&inode
->i_lock
);
948 ci
->i_wanted_max_size
= 0;
949 ci
->i_requested_max_size
= 0;
950 spin_unlock(&inode
->i_lock
);
955 static void wake_up_session_caps(struct ceph_mds_session
*session
,
958 dout("wake_up_session_caps %p mds%d\n", session
, session
->s_mds
);
959 iterate_session_caps(session
, wake_up_session_cb
,
960 (void *)(unsigned long)reconnect
);
964 * Send periodic message to MDS renewing all currently held caps. The
965 * ack will reset the expiration for all caps from this session.
967 * caller holds s_mutex
969 static int send_renew_caps(struct ceph_mds_client
*mdsc
,
970 struct ceph_mds_session
*session
)
972 struct ceph_msg
*msg
;
975 if (time_after_eq(jiffies
, session
->s_cap_ttl
) &&
976 time_after_eq(session
->s_cap_ttl
, session
->s_renew_requested
))
977 pr_info("mds%d caps stale\n", session
->s_mds
);
978 session
->s_renew_requested
= jiffies
;
980 /* do not try to renew caps until a recovering mds has reconnected
981 * with its clients. */
982 state
= ceph_mdsmap_get_state(mdsc
->mdsmap
, session
->s_mds
);
983 if (state
< CEPH_MDS_STATE_RECONNECT
) {
984 dout("send_renew_caps ignoring mds%d (%s)\n",
985 session
->s_mds
, ceph_mds_state_name(state
));
989 dout("send_renew_caps to mds%d (%s)\n", session
->s_mds
,
990 ceph_mds_state_name(state
));
991 msg
= create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS
,
992 ++session
->s_renew_seq
);
995 ceph_con_send(&session
->s_con
, msg
);
1000 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
1002 * Called under session->s_mutex
1004 static void renewed_caps(struct ceph_mds_client
*mdsc
,
1005 struct ceph_mds_session
*session
, int is_renew
)
1010 spin_lock(&session
->s_cap_lock
);
1011 was_stale
= is_renew
&& (session
->s_cap_ttl
== 0 ||
1012 time_after_eq(jiffies
, session
->s_cap_ttl
));
1014 session
->s_cap_ttl
= session
->s_renew_requested
+
1015 mdsc
->mdsmap
->m_session_timeout
*HZ
;
1018 if (time_before(jiffies
, session
->s_cap_ttl
)) {
1019 pr_info("mds%d caps renewed\n", session
->s_mds
);
1022 pr_info("mds%d caps still stale\n", session
->s_mds
);
1025 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1026 session
->s_mds
, session
->s_cap_ttl
, was_stale
? "stale" : "fresh",
1027 time_before(jiffies
, session
->s_cap_ttl
) ? "stale" : "fresh");
1028 spin_unlock(&session
->s_cap_lock
);
1031 wake_up_session_caps(session
, 0);
1035 * send a session close request
1037 static int request_close_session(struct ceph_mds_client
*mdsc
,
1038 struct ceph_mds_session
*session
)
1040 struct ceph_msg
*msg
;
1042 dout("request_close_session mds%d state %s seq %lld\n",
1043 session
->s_mds
, session_state_name(session
->s_state
),
1045 msg
= create_session_msg(CEPH_SESSION_REQUEST_CLOSE
, session
->s_seq
);
1048 ceph_con_send(&session
->s_con
, msg
);
1053 * Called with s_mutex held.
1055 static int __close_session(struct ceph_mds_client
*mdsc
,
1056 struct ceph_mds_session
*session
)
1058 if (session
->s_state
>= CEPH_MDS_SESSION_CLOSING
)
1060 session
->s_state
= CEPH_MDS_SESSION_CLOSING
;
1061 return request_close_session(mdsc
, session
);
1065 * Trim old(er) caps.
1067 * Because we can't cache an inode without one or more caps, we do
1068 * this indirectly: if a cap is unused, we prune its aliases, at which
1069 * point the inode will hopefully get dropped to.
1071 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1072 * memory pressure from the MDS, though, so it needn't be perfect.
1074 static int trim_caps_cb(struct inode
*inode
, struct ceph_cap
*cap
, void *arg
)
1076 struct ceph_mds_session
*session
= arg
;
1077 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1078 int used
, oissued
, mine
;
1080 if (session
->s_trim_caps
<= 0)
1083 spin_lock(&inode
->i_lock
);
1084 mine
= cap
->issued
| cap
->implemented
;
1085 used
= __ceph_caps_used(ci
);
1086 oissued
= __ceph_caps_issued_other(ci
, cap
);
1088 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n",
1089 inode
, cap
, ceph_cap_string(mine
), ceph_cap_string(oissued
),
1090 ceph_cap_string(used
));
1091 if (ci
->i_dirty_caps
)
1092 goto out
; /* dirty caps */
1093 if ((used
& ~oissued
) & mine
)
1094 goto out
; /* we need these caps */
1096 session
->s_trim_caps
--;
1098 /* we aren't the only cap.. just remove us */
1099 __ceph_remove_cap(cap
);
1101 /* try to drop referring dentries */
1102 spin_unlock(&inode
->i_lock
);
1103 d_prune_aliases(inode
);
1104 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1105 inode
, cap
, atomic_read(&inode
->i_count
));
1110 spin_unlock(&inode
->i_lock
);
1115 * Trim session cap count down to some max number.
1117 static int trim_caps(struct ceph_mds_client
*mdsc
,
1118 struct ceph_mds_session
*session
,
1121 int trim_caps
= session
->s_nr_caps
- max_caps
;
1123 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1124 session
->s_mds
, session
->s_nr_caps
, max_caps
, trim_caps
);
1125 if (trim_caps
> 0) {
1126 session
->s_trim_caps
= trim_caps
;
1127 iterate_session_caps(session
, trim_caps_cb
, session
);
1128 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1129 session
->s_mds
, session
->s_nr_caps
, max_caps
,
1130 trim_caps
- session
->s_trim_caps
);
1131 session
->s_trim_caps
= 0;
1137 * Allocate cap_release messages. If there is a partially full message
1138 * in the queue, try to allocate enough to cover it's remainder, so that
1139 * we can send it immediately.
1141 * Called under s_mutex.
1143 int ceph_add_cap_releases(struct ceph_mds_client
*mdsc
,
1144 struct ceph_mds_session
*session
)
1146 struct ceph_msg
*msg
, *partial
= NULL
;
1147 struct ceph_mds_cap_release
*head
;
1149 int extra
= mdsc
->client
->mount_args
->cap_release_safety
;
1152 dout("add_cap_releases %p mds%d extra %d\n", session
, session
->s_mds
,
1155 spin_lock(&session
->s_cap_lock
);
1157 if (!list_empty(&session
->s_cap_releases
)) {
1158 msg
= list_first_entry(&session
->s_cap_releases
,
1161 head
= msg
->front
.iov_base
;
1162 num
= le32_to_cpu(head
->num
);
1164 dout(" partial %p with (%d/%d)\n", msg
, num
,
1165 (int)CEPH_CAPS_PER_RELEASE
);
1166 extra
+= CEPH_CAPS_PER_RELEASE
- num
;
1170 while (session
->s_num_cap_releases
< session
->s_nr_caps
+ extra
) {
1171 spin_unlock(&session
->s_cap_lock
);
1172 msg
= ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE
, PAGE_CACHE_SIZE
,
1176 dout("add_cap_releases %p msg %p now %d\n", session
, msg
,
1177 (int)msg
->front
.iov_len
);
1178 head
= msg
->front
.iov_base
;
1179 head
->num
= cpu_to_le32(0);
1180 msg
->front
.iov_len
= sizeof(*head
);
1181 spin_lock(&session
->s_cap_lock
);
1182 list_add(&msg
->list_head
, &session
->s_cap_releases
);
1183 session
->s_num_cap_releases
+= CEPH_CAPS_PER_RELEASE
;
1187 head
= partial
->front
.iov_base
;
1188 num
= le32_to_cpu(head
->num
);
1189 dout(" queueing partial %p with %d/%d\n", partial
, num
,
1190 (int)CEPH_CAPS_PER_RELEASE
);
1191 list_move_tail(&partial
->list_head
,
1192 &session
->s_cap_releases_done
);
1193 session
->s_num_cap_releases
-= CEPH_CAPS_PER_RELEASE
- num
;
1196 spin_unlock(&session
->s_cap_lock
);
1202 * flush all dirty inode data to disk.
1204 * returns true if we've flushed through want_flush_seq
1206 static int check_cap_flush(struct ceph_mds_client
*mdsc
, u64 want_flush_seq
)
1210 dout("check_cap_flush want %lld\n", want_flush_seq
);
1211 mutex_lock(&mdsc
->mutex
);
1212 for (mds
= 0; ret
&& mds
< mdsc
->max_sessions
; mds
++) {
1213 struct ceph_mds_session
*session
= mdsc
->sessions
[mds
];
1217 get_session(session
);
1218 mutex_unlock(&mdsc
->mutex
);
1220 mutex_lock(&session
->s_mutex
);
1221 if (!list_empty(&session
->s_cap_flushing
)) {
1222 struct ceph_inode_info
*ci
=
1223 list_entry(session
->s_cap_flushing
.next
,
1224 struct ceph_inode_info
,
1226 struct inode
*inode
= &ci
->vfs_inode
;
1228 spin_lock(&inode
->i_lock
);
1229 if (ci
->i_cap_flush_seq
<= want_flush_seq
) {
1230 dout("check_cap_flush still flushing %p "
1231 "seq %lld <= %lld to mds%d\n", inode
,
1232 ci
->i_cap_flush_seq
, want_flush_seq
,
1236 spin_unlock(&inode
->i_lock
);
1238 mutex_unlock(&session
->s_mutex
);
1239 ceph_put_mds_session(session
);
1243 mutex_lock(&mdsc
->mutex
);
1246 mutex_unlock(&mdsc
->mutex
);
1247 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq
);
1252 * called under s_mutex
1254 void ceph_send_cap_releases(struct ceph_mds_client
*mdsc
,
1255 struct ceph_mds_session
*session
)
1257 struct ceph_msg
*msg
;
1259 dout("send_cap_releases mds%d\n", session
->s_mds
);
1260 spin_lock(&session
->s_cap_lock
);
1261 while (!list_empty(&session
->s_cap_releases_done
)) {
1262 msg
= list_first_entry(&session
->s_cap_releases_done
,
1263 struct ceph_msg
, list_head
);
1264 list_del_init(&msg
->list_head
);
1265 spin_unlock(&session
->s_cap_lock
);
1266 msg
->hdr
.front_len
= cpu_to_le32(msg
->front
.iov_len
);
1267 dout("send_cap_releases mds%d %p\n", session
->s_mds
, msg
);
1268 ceph_con_send(&session
->s_con
, msg
);
1269 spin_lock(&session
->s_cap_lock
);
1271 spin_unlock(&session
->s_cap_lock
);
1274 static void discard_cap_releases(struct ceph_mds_client
*mdsc
,
1275 struct ceph_mds_session
*session
)
1277 struct ceph_msg
*msg
;
1278 struct ceph_mds_cap_release
*head
;
1281 dout("discard_cap_releases mds%d\n", session
->s_mds
);
1282 spin_lock(&session
->s_cap_lock
);
1284 /* zero out the in-progress message */
1285 msg
= list_first_entry(&session
->s_cap_releases
,
1286 struct ceph_msg
, list_head
);
1287 head
= msg
->front
.iov_base
;
1288 num
= le32_to_cpu(head
->num
);
1289 dout("discard_cap_releases mds%d %p %u\n", session
->s_mds
, msg
, num
);
1290 head
->num
= cpu_to_le32(0);
1291 session
->s_num_cap_releases
+= num
;
1293 /* requeue completed messages */
1294 while (!list_empty(&session
->s_cap_releases_done
)) {
1295 msg
= list_first_entry(&session
->s_cap_releases_done
,
1296 struct ceph_msg
, list_head
);
1297 list_del_init(&msg
->list_head
);
1299 head
= msg
->front
.iov_base
;
1300 num
= le32_to_cpu(head
->num
);
1301 dout("discard_cap_releases mds%d %p %u\n", session
->s_mds
, msg
,
1303 session
->s_num_cap_releases
+= num
;
1304 head
->num
= cpu_to_le32(0);
1305 msg
->front
.iov_len
= sizeof(*head
);
1306 list_add(&msg
->list_head
, &session
->s_cap_releases
);
1309 spin_unlock(&session
->s_cap_lock
);
1317 * Create an mds request.
1319 struct ceph_mds_request
*
1320 ceph_mdsc_create_request(struct ceph_mds_client
*mdsc
, int op
, int mode
)
1322 struct ceph_mds_request
*req
= kzalloc(sizeof(*req
), GFP_NOFS
);
1325 return ERR_PTR(-ENOMEM
);
1327 mutex_init(&req
->r_fill_mutex
);
1329 req
->r_started
= jiffies
;
1330 req
->r_resend_mds
= -1;
1331 INIT_LIST_HEAD(&req
->r_unsafe_dir_item
);
1333 kref_init(&req
->r_kref
);
1334 INIT_LIST_HEAD(&req
->r_wait
);
1335 init_completion(&req
->r_completion
);
1336 init_completion(&req
->r_safe_completion
);
1337 INIT_LIST_HEAD(&req
->r_unsafe_item
);
1340 req
->r_direct_mode
= mode
;
1345 * return oldest (lowest) request, tid in request tree, 0 if none.
1347 * called under mdsc->mutex.
1349 static struct ceph_mds_request
*__get_oldest_req(struct ceph_mds_client
*mdsc
)
1351 if (RB_EMPTY_ROOT(&mdsc
->request_tree
))
1353 return rb_entry(rb_first(&mdsc
->request_tree
),
1354 struct ceph_mds_request
, r_node
);
1357 static u64
__get_oldest_tid(struct ceph_mds_client
*mdsc
)
1359 struct ceph_mds_request
*req
= __get_oldest_req(mdsc
);
1367 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1368 * on build_path_from_dentry in fs/cifs/dir.c.
1370 * If @stop_on_nosnap, generate path relative to the first non-snapped
1373 * Encode hidden .snap dirs as a double /, i.e.
1374 * foo/.snap/bar -> foo//bar
1376 char *ceph_mdsc_build_path(struct dentry
*dentry
, int *plen
, u64
*base
,
1379 struct dentry
*temp
;
1384 return ERR_PTR(-EINVAL
);
1388 for (temp
= dentry
; !IS_ROOT(temp
);) {
1389 struct inode
*inode
= temp
->d_inode
;
1390 if (inode
&& ceph_snap(inode
) == CEPH_SNAPDIR
)
1391 len
++; /* slash only */
1392 else if (stop_on_nosnap
&& inode
&&
1393 ceph_snap(inode
) == CEPH_NOSNAP
)
1396 len
+= 1 + temp
->d_name
.len
;
1397 temp
= temp
->d_parent
;
1399 pr_err("build_path corrupt dentry %p\n", dentry
);
1400 return ERR_PTR(-EINVAL
);
1404 len
--; /* no leading '/' */
1406 path
= kmalloc(len
+1, GFP_NOFS
);
1408 return ERR_PTR(-ENOMEM
);
1410 path
[pos
] = 0; /* trailing null */
1411 for (temp
= dentry
; !IS_ROOT(temp
) && pos
!= 0; ) {
1412 struct inode
*inode
= temp
->d_inode
;
1414 if (inode
&& ceph_snap(inode
) == CEPH_SNAPDIR
) {
1415 dout("build_path path+%d: %p SNAPDIR\n",
1417 } else if (stop_on_nosnap
&& inode
&&
1418 ceph_snap(inode
) == CEPH_NOSNAP
) {
1421 pos
-= temp
->d_name
.len
;
1424 strncpy(path
+ pos
, temp
->d_name
.name
,
1429 temp
= temp
->d_parent
;
1431 pr_err("build_path corrupt dentry\n");
1433 return ERR_PTR(-EINVAL
);
1437 pr_err("build_path did not end path lookup where "
1438 "expected, namelen is %d, pos is %d\n", len
, pos
);
1439 /* presumably this is only possible if racing with a
1440 rename of one of the parent directories (we can not
1441 lock the dentries above us to prevent this, but
1442 retrying should be harmless) */
1447 *base
= ceph_ino(temp
->d_inode
);
1449 dout("build_path on %p %d built %llx '%.*s'\n",
1450 dentry
, atomic_read(&dentry
->d_count
), *base
, len
, path
);
1454 static int build_dentry_path(struct dentry
*dentry
,
1455 const char **ppath
, int *ppathlen
, u64
*pino
,
1460 if (ceph_snap(dentry
->d_parent
->d_inode
) == CEPH_NOSNAP
) {
1461 *pino
= ceph_ino(dentry
->d_parent
->d_inode
);
1462 *ppath
= dentry
->d_name
.name
;
1463 *ppathlen
= dentry
->d_name
.len
;
1466 path
= ceph_mdsc_build_path(dentry
, ppathlen
, pino
, 1);
1468 return PTR_ERR(path
);
1474 static int build_inode_path(struct inode
*inode
,
1475 const char **ppath
, int *ppathlen
, u64
*pino
,
1478 struct dentry
*dentry
;
1481 if (ceph_snap(inode
) == CEPH_NOSNAP
) {
1482 *pino
= ceph_ino(inode
);
1486 dentry
= d_find_alias(inode
);
1487 path
= ceph_mdsc_build_path(dentry
, ppathlen
, pino
, 1);
1490 return PTR_ERR(path
);
1497 * request arguments may be specified via an inode *, a dentry *, or
1498 * an explicit ino+path.
1500 static int set_request_path_attr(struct inode
*rinode
, struct dentry
*rdentry
,
1501 const char *rpath
, u64 rino
,
1502 const char **ppath
, int *pathlen
,
1503 u64
*ino
, int *freepath
)
1508 r
= build_inode_path(rinode
, ppath
, pathlen
, ino
, freepath
);
1509 dout(" inode %p %llx.%llx\n", rinode
, ceph_ino(rinode
),
1511 } else if (rdentry
) {
1512 r
= build_dentry_path(rdentry
, ppath
, pathlen
, ino
, freepath
);
1513 dout(" dentry %p %llx/%.*s\n", rdentry
, *ino
, *pathlen
,
1518 *pathlen
= strlen(rpath
);
1519 dout(" path %.*s\n", *pathlen
, rpath
);
1526 * called under mdsc->mutex
1528 static struct ceph_msg
*create_request_message(struct ceph_mds_client
*mdsc
,
1529 struct ceph_mds_request
*req
,
1532 struct ceph_msg
*msg
;
1533 struct ceph_mds_request_head
*head
;
1534 const char *path1
= NULL
;
1535 const char *path2
= NULL
;
1536 u64 ino1
= 0, ino2
= 0;
1537 int pathlen1
= 0, pathlen2
= 0;
1538 int freepath1
= 0, freepath2
= 0;
1544 ret
= set_request_path_attr(req
->r_inode
, req
->r_dentry
,
1545 req
->r_path1
, req
->r_ino1
.ino
,
1546 &path1
, &pathlen1
, &ino1
, &freepath1
);
1552 ret
= set_request_path_attr(NULL
, req
->r_old_dentry
,
1553 req
->r_path2
, req
->r_ino2
.ino
,
1554 &path2
, &pathlen2
, &ino2
, &freepath2
);
1560 len
= sizeof(*head
) +
1561 pathlen1
+ pathlen2
+ 2*(1 + sizeof(u32
) + sizeof(u64
));
1563 /* calculate (max) length for cap releases */
1564 len
+= sizeof(struct ceph_mds_request_release
) *
1565 (!!req
->r_inode_drop
+ !!req
->r_dentry_drop
+
1566 !!req
->r_old_inode_drop
+ !!req
->r_old_dentry_drop
);
1567 if (req
->r_dentry_drop
)
1568 len
+= req
->r_dentry
->d_name
.len
;
1569 if (req
->r_old_dentry_drop
)
1570 len
+= req
->r_old_dentry
->d_name
.len
;
1572 msg
= ceph_msg_new(CEPH_MSG_CLIENT_REQUEST
, len
, GFP_NOFS
);
1574 msg
= ERR_PTR(-ENOMEM
);
1578 msg
->hdr
.tid
= cpu_to_le64(req
->r_tid
);
1580 head
= msg
->front
.iov_base
;
1581 p
= msg
->front
.iov_base
+ sizeof(*head
);
1582 end
= msg
->front
.iov_base
+ msg
->front
.iov_len
;
1584 head
->mdsmap_epoch
= cpu_to_le32(mdsc
->mdsmap
->m_epoch
);
1585 head
->op
= cpu_to_le32(req
->r_op
);
1586 head
->caller_uid
= cpu_to_le32(current_fsuid());
1587 head
->caller_gid
= cpu_to_le32(current_fsgid());
1588 head
->args
= req
->r_args
;
1590 ceph_encode_filepath(&p
, end
, ino1
, path1
);
1591 ceph_encode_filepath(&p
, end
, ino2
, path2
);
1593 /* make note of release offset, in case we need to replay */
1594 req
->r_request_release_offset
= p
- msg
->front
.iov_base
;
1598 if (req
->r_inode_drop
)
1599 releases
+= ceph_encode_inode_release(&p
,
1600 req
->r_inode
? req
->r_inode
: req
->r_dentry
->d_inode
,
1601 mds
, req
->r_inode_drop
, req
->r_inode_unless
, 0);
1602 if (req
->r_dentry_drop
)
1603 releases
+= ceph_encode_dentry_release(&p
, req
->r_dentry
,
1604 mds
, req
->r_dentry_drop
, req
->r_dentry_unless
);
1605 if (req
->r_old_dentry_drop
)
1606 releases
+= ceph_encode_dentry_release(&p
, req
->r_old_dentry
,
1607 mds
, req
->r_old_dentry_drop
, req
->r_old_dentry_unless
);
1608 if (req
->r_old_inode_drop
)
1609 releases
+= ceph_encode_inode_release(&p
,
1610 req
->r_old_dentry
->d_inode
,
1611 mds
, req
->r_old_inode_drop
, req
->r_old_inode_unless
, 0);
1612 head
->num_releases
= cpu_to_le16(releases
);
1615 msg
->front
.iov_len
= p
- msg
->front
.iov_base
;
1616 msg
->hdr
.front_len
= cpu_to_le32(msg
->front
.iov_len
);
1618 msg
->pages
= req
->r_pages
;
1619 msg
->nr_pages
= req
->r_num_pages
;
1620 msg
->hdr
.data_len
= cpu_to_le32(req
->r_data_len
);
1621 msg
->hdr
.data_off
= cpu_to_le16(0);
1625 kfree((char *)path2
);
1628 kfree((char *)path1
);
1634 * called under mdsc->mutex if error, under no mutex if
1637 static void complete_request(struct ceph_mds_client
*mdsc
,
1638 struct ceph_mds_request
*req
)
1640 if (req
->r_callback
)
1641 req
->r_callback(mdsc
, req
);
1643 complete_all(&req
->r_completion
);
1647 * called under mdsc->mutex
1649 static int __prepare_send_request(struct ceph_mds_client
*mdsc
,
1650 struct ceph_mds_request
*req
,
1653 struct ceph_mds_request_head
*rhead
;
1654 struct ceph_msg
*msg
;
1660 struct ceph_cap
*cap
=
1661 ceph_get_cap_for_mds(ceph_inode(req
->r_inode
), mds
);
1664 req
->r_sent_on_mseq
= cap
->mseq
;
1666 req
->r_sent_on_mseq
= -1;
1668 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req
,
1669 req
->r_tid
, ceph_mds_op_name(req
->r_op
), req
->r_attempts
);
1671 if (req
->r_got_unsafe
) {
1673 * Replay. Do not regenerate message (and rebuild
1674 * paths, etc.); just use the original message.
1675 * Rebuilding paths will break for renames because
1676 * d_move mangles the src name.
1678 msg
= req
->r_request
;
1679 rhead
= msg
->front
.iov_base
;
1681 flags
= le32_to_cpu(rhead
->flags
);
1682 flags
|= CEPH_MDS_FLAG_REPLAY
;
1683 rhead
->flags
= cpu_to_le32(flags
);
1685 if (req
->r_target_inode
)
1686 rhead
->ino
= cpu_to_le64(ceph_ino(req
->r_target_inode
));
1688 rhead
->num_retry
= req
->r_attempts
- 1;
1690 /* remove cap/dentry releases from message */
1691 rhead
->num_releases
= 0;
1692 msg
->hdr
.front_len
= cpu_to_le32(req
->r_request_release_offset
);
1693 msg
->front
.iov_len
= req
->r_request_release_offset
;
1697 if (req
->r_request
) {
1698 ceph_msg_put(req
->r_request
);
1699 req
->r_request
= NULL
;
1701 msg
= create_request_message(mdsc
, req
, mds
);
1703 req
->r_err
= PTR_ERR(msg
);
1704 complete_request(mdsc
, req
);
1705 return PTR_ERR(msg
);
1707 req
->r_request
= msg
;
1709 rhead
= msg
->front
.iov_base
;
1710 rhead
->oldest_client_tid
= cpu_to_le64(__get_oldest_tid(mdsc
));
1711 if (req
->r_got_unsafe
)
1712 flags
|= CEPH_MDS_FLAG_REPLAY
;
1713 if (req
->r_locked_dir
)
1714 flags
|= CEPH_MDS_FLAG_WANT_DENTRY
;
1715 rhead
->flags
= cpu_to_le32(flags
);
1716 rhead
->num_fwd
= req
->r_num_fwd
;
1717 rhead
->num_retry
= req
->r_attempts
- 1;
1720 dout(" r_locked_dir = %p\n", req
->r_locked_dir
);
1725 * send request, or put it on the appropriate wait list.
1727 static int __do_request(struct ceph_mds_client
*mdsc
,
1728 struct ceph_mds_request
*req
)
1730 struct ceph_mds_session
*session
= NULL
;
1734 if (req
->r_err
|| req
->r_got_result
)
1737 if (req
->r_timeout
&&
1738 time_after_eq(jiffies
, req
->r_started
+ req
->r_timeout
)) {
1739 dout("do_request timed out\n");
1744 mds
= __choose_mds(mdsc
, req
);
1746 ceph_mdsmap_get_state(mdsc
->mdsmap
, mds
) < CEPH_MDS_STATE_ACTIVE
) {
1747 dout("do_request no mds or not active, waiting for map\n");
1748 list_add(&req
->r_wait
, &mdsc
->waiting_for_map
);
1752 /* get, open session */
1753 session
= __ceph_lookup_mds_session(mdsc
, mds
);
1755 session
= register_session(mdsc
, mds
);
1756 if (IS_ERR(session
)) {
1757 err
= PTR_ERR(session
);
1761 dout("do_request mds%d session %p state %s\n", mds
, session
,
1762 session_state_name(session
->s_state
));
1763 if (session
->s_state
!= CEPH_MDS_SESSION_OPEN
&&
1764 session
->s_state
!= CEPH_MDS_SESSION_HUNG
) {
1765 if (session
->s_state
== CEPH_MDS_SESSION_NEW
||
1766 session
->s_state
== CEPH_MDS_SESSION_CLOSING
)
1767 __open_session(mdsc
, session
);
1768 list_add(&req
->r_wait
, &session
->s_waiting
);
1773 req
->r_session
= get_session(session
);
1774 req
->r_resend_mds
= -1; /* forget any previous mds hint */
1776 if (req
->r_request_started
== 0) /* note request start time */
1777 req
->r_request_started
= jiffies
;
1779 err
= __prepare_send_request(mdsc
, req
, mds
);
1781 ceph_msg_get(req
->r_request
);
1782 ceph_con_send(&session
->s_con
, req
->r_request
);
1786 ceph_put_mds_session(session
);
1792 complete_request(mdsc
, req
);
1797 * called under mdsc->mutex
1799 static void __wake_requests(struct ceph_mds_client
*mdsc
,
1800 struct list_head
*head
)
1802 struct ceph_mds_request
*req
, *nreq
;
1804 list_for_each_entry_safe(req
, nreq
, head
, r_wait
) {
1805 list_del_init(&req
->r_wait
);
1806 __do_request(mdsc
, req
);
1811 * Wake up threads with requests pending for @mds, so that they can
1812 * resubmit their requests to a possibly different mds.
1814 static void kick_requests(struct ceph_mds_client
*mdsc
, int mds
)
1816 struct ceph_mds_request
*req
;
1819 dout("kick_requests mds%d\n", mds
);
1820 for (p
= rb_first(&mdsc
->request_tree
); p
; p
= rb_next(p
)) {
1821 req
= rb_entry(p
, struct ceph_mds_request
, r_node
);
1822 if (req
->r_got_unsafe
)
1824 if (req
->r_session
&&
1825 req
->r_session
->s_mds
== mds
) {
1826 dout(" kicking tid %llu\n", req
->r_tid
);
1827 put_request_session(req
);
1828 __do_request(mdsc
, req
);
1833 void ceph_mdsc_submit_request(struct ceph_mds_client
*mdsc
,
1834 struct ceph_mds_request
*req
)
1836 dout("submit_request on %p\n", req
);
1837 mutex_lock(&mdsc
->mutex
);
1838 __register_request(mdsc
, req
, NULL
);
1839 __do_request(mdsc
, req
);
1840 mutex_unlock(&mdsc
->mutex
);
1844 * Synchrously perform an mds request. Take care of all of the
1845 * session setup, forwarding, retry details.
1847 int ceph_mdsc_do_request(struct ceph_mds_client
*mdsc
,
1849 struct ceph_mds_request
*req
)
1853 dout("do_request on %p\n", req
);
1855 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
1857 ceph_get_cap_refs(ceph_inode(req
->r_inode
), CEPH_CAP_PIN
);
1858 if (req
->r_locked_dir
)
1859 ceph_get_cap_refs(ceph_inode(req
->r_locked_dir
), CEPH_CAP_PIN
);
1860 if (req
->r_old_dentry
)
1862 ceph_inode(req
->r_old_dentry
->d_parent
->d_inode
),
1866 mutex_lock(&mdsc
->mutex
);
1867 __register_request(mdsc
, req
, dir
);
1868 __do_request(mdsc
, req
);
1872 __unregister_request(mdsc
, req
);
1873 dout("do_request early error %d\n", err
);
1878 mutex_unlock(&mdsc
->mutex
);
1879 dout("do_request waiting\n");
1880 if (req
->r_timeout
) {
1881 err
= (long)wait_for_completion_killable_timeout(
1882 &req
->r_completion
, req
->r_timeout
);
1886 err
= wait_for_completion_killable(&req
->r_completion
);
1888 dout("do_request waited, got %d\n", err
);
1889 mutex_lock(&mdsc
->mutex
);
1891 /* only abort if we didn't race with a real reply */
1892 if (req
->r_got_result
) {
1893 err
= le32_to_cpu(req
->r_reply_info
.head
->result
);
1894 } else if (err
< 0) {
1895 dout("aborted request %lld with %d\n", req
->r_tid
, err
);
1898 * ensure we aren't running concurrently with
1899 * ceph_fill_trace or ceph_readdir_prepopulate, which
1900 * rely on locks (dir mutex) held by our caller.
1902 mutex_lock(&req
->r_fill_mutex
);
1904 req
->r_aborted
= true;
1905 mutex_unlock(&req
->r_fill_mutex
);
1907 if (req
->r_locked_dir
&&
1908 (req
->r_op
& CEPH_MDS_OP_WRITE
))
1909 ceph_invalidate_dir_request(req
);
1915 mutex_unlock(&mdsc
->mutex
);
1916 dout("do_request %p done, result %d\n", req
, err
);
1921 * Invalidate dir I_COMPLETE, dentry lease state on an aborted MDS
1922 * namespace request.
1924 void ceph_invalidate_dir_request(struct ceph_mds_request
*req
)
1926 struct inode
*inode
= req
->r_locked_dir
;
1927 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1929 dout("invalidate_dir_request %p (I_COMPLETE, lease(s))\n", inode
);
1930 spin_lock(&inode
->i_lock
);
1931 ci
->i_ceph_flags
&= ~CEPH_I_COMPLETE
;
1932 ci
->i_release_count
++;
1933 spin_unlock(&inode
->i_lock
);
1936 ceph_invalidate_dentry_lease(req
->r_dentry
);
1937 if (req
->r_old_dentry
)
1938 ceph_invalidate_dentry_lease(req
->r_old_dentry
);
1944 * We take the session mutex and parse and process the reply immediately.
1945 * This preserves the logical ordering of replies, capabilities, etc., sent
1946 * by the MDS as they are applied to our local cache.
1948 static void handle_reply(struct ceph_mds_session
*session
, struct ceph_msg
*msg
)
1950 struct ceph_mds_client
*mdsc
= session
->s_mdsc
;
1951 struct ceph_mds_request
*req
;
1952 struct ceph_mds_reply_head
*head
= msg
->front
.iov_base
;
1953 struct ceph_mds_reply_info_parsed
*rinfo
; /* parsed reply info */
1956 int mds
= session
->s_mds
;
1958 if (msg
->front
.iov_len
< sizeof(*head
)) {
1959 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
1964 /* get request, session */
1965 tid
= le64_to_cpu(msg
->hdr
.tid
);
1966 mutex_lock(&mdsc
->mutex
);
1967 req
= __lookup_request(mdsc
, tid
);
1969 dout("handle_reply on unknown tid %llu\n", tid
);
1970 mutex_unlock(&mdsc
->mutex
);
1973 dout("handle_reply %p\n", req
);
1975 /* correct session? */
1976 if (req
->r_session
!= session
) {
1977 pr_err("mdsc_handle_reply got %llu on session mds%d"
1978 " not mds%d\n", tid
, session
->s_mds
,
1979 req
->r_session
? req
->r_session
->s_mds
: -1);
1980 mutex_unlock(&mdsc
->mutex
);
1985 if ((req
->r_got_unsafe
&& !head
->safe
) ||
1986 (req
->r_got_safe
&& head
->safe
)) {
1987 pr_warning("got a dup %s reply on %llu from mds%d\n",
1988 head
->safe
? "safe" : "unsafe", tid
, mds
);
1989 mutex_unlock(&mdsc
->mutex
);
1992 if (req
->r_got_safe
&& !head
->safe
) {
1993 pr_warning("got unsafe after safe on %llu from mds%d\n",
1995 mutex_unlock(&mdsc
->mutex
);
1999 result
= le32_to_cpu(head
->result
);
2003 * if we're not talking to the authority, send to them
2004 * if the authority has changed while we weren't looking,
2005 * send to new authority
2006 * Otherwise we just have to return an ESTALE
2008 if (result
== -ESTALE
) {
2009 dout("got ESTALE on request %llu", req
->r_tid
);
2010 if (!req
->r_inode
) {
2011 /* do nothing; not an authority problem */
2012 } else if (req
->r_direct_mode
!= USE_AUTH_MDS
) {
2013 dout("not using auth, setting for that now");
2014 req
->r_direct_mode
= USE_AUTH_MDS
;
2015 __do_request(mdsc
, req
);
2016 mutex_unlock(&mdsc
->mutex
);
2019 struct ceph_inode_info
*ci
= ceph_inode(req
->r_inode
);
2020 struct ceph_cap
*cap
=
2021 ceph_get_cap_for_mds(ci
, req
->r_mds
);;
2023 dout("already using auth");
2024 if ((!cap
|| cap
!= ci
->i_auth_cap
) ||
2025 (cap
->mseq
!= req
->r_sent_on_mseq
)) {
2026 dout("but cap changed, so resending");
2027 __do_request(mdsc
, req
);
2028 mutex_unlock(&mdsc
->mutex
);
2032 dout("have to return ESTALE on request %llu", req
->r_tid
);
2037 req
->r_got_safe
= true;
2038 __unregister_request(mdsc
, req
);
2039 complete_all(&req
->r_safe_completion
);
2041 if (req
->r_got_unsafe
) {
2043 * We already handled the unsafe response, now do the
2044 * cleanup. No need to examine the response; the MDS
2045 * doesn't include any result info in the safe
2046 * response. And even if it did, there is nothing
2047 * useful we could do with a revised return value.
2049 dout("got safe reply %llu, mds%d\n", tid
, mds
);
2050 list_del_init(&req
->r_unsafe_item
);
2052 /* last unsafe request during umount? */
2053 if (mdsc
->stopping
&& !__get_oldest_req(mdsc
))
2054 complete_all(&mdsc
->safe_umount_waiters
);
2055 mutex_unlock(&mdsc
->mutex
);
2059 req
->r_got_unsafe
= true;
2060 list_add_tail(&req
->r_unsafe_item
, &req
->r_session
->s_unsafe
);
2063 dout("handle_reply tid %lld result %d\n", tid
, result
);
2064 rinfo
= &req
->r_reply_info
;
2065 err
= parse_reply_info(msg
, rinfo
);
2066 mutex_unlock(&mdsc
->mutex
);
2068 mutex_lock(&session
->s_mutex
);
2070 pr_err("mdsc_handle_reply got corrupt reply mds%d\n", mds
);
2076 if (rinfo
->snapblob_len
) {
2077 down_write(&mdsc
->snap_rwsem
);
2078 ceph_update_snap_trace(mdsc
, rinfo
->snapblob
,
2079 rinfo
->snapblob
+ rinfo
->snapblob_len
,
2080 le32_to_cpu(head
->op
) == CEPH_MDS_OP_RMSNAP
);
2081 downgrade_write(&mdsc
->snap_rwsem
);
2083 down_read(&mdsc
->snap_rwsem
);
2086 /* insert trace into our cache */
2087 mutex_lock(&req
->r_fill_mutex
);
2088 err
= ceph_fill_trace(mdsc
->client
->sb
, req
, req
->r_session
);
2090 if (result
== 0 && rinfo
->dir_nr
)
2091 ceph_readdir_prepopulate(req
, req
->r_session
);
2092 ceph_unreserve_caps(mdsc
, &req
->r_caps_reservation
);
2094 mutex_unlock(&req
->r_fill_mutex
);
2096 up_read(&mdsc
->snap_rwsem
);
2098 mutex_lock(&mdsc
->mutex
);
2099 if (!req
->r_aborted
) {
2105 req
->r_got_result
= true;
2108 dout("reply arrived after request %lld was aborted\n", tid
);
2110 mutex_unlock(&mdsc
->mutex
);
2112 ceph_add_cap_releases(mdsc
, req
->r_session
);
2113 mutex_unlock(&session
->s_mutex
);
2115 /* kick calling process */
2116 complete_request(mdsc
, req
);
2118 ceph_mdsc_put_request(req
);
2125 * handle mds notification that our request has been forwarded.
2127 static void handle_forward(struct ceph_mds_client
*mdsc
,
2128 struct ceph_mds_session
*session
,
2129 struct ceph_msg
*msg
)
2131 struct ceph_mds_request
*req
;
2132 u64 tid
= le64_to_cpu(msg
->hdr
.tid
);
2136 void *p
= msg
->front
.iov_base
;
2137 void *end
= p
+ msg
->front
.iov_len
;
2139 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
2140 next_mds
= ceph_decode_32(&p
);
2141 fwd_seq
= ceph_decode_32(&p
);
2143 mutex_lock(&mdsc
->mutex
);
2144 req
= __lookup_request(mdsc
, tid
);
2146 dout("forward tid %llu to mds%d - req dne\n", tid
, next_mds
);
2147 goto out
; /* dup reply? */
2150 if (req
->r_aborted
) {
2151 dout("forward tid %llu aborted, unregistering\n", tid
);
2152 __unregister_request(mdsc
, req
);
2153 } else if (fwd_seq
<= req
->r_num_fwd
) {
2154 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
2155 tid
, next_mds
, req
->r_num_fwd
, fwd_seq
);
2157 /* resend. forward race not possible; mds would drop */
2158 dout("forward tid %llu to mds%d (we resend)\n", tid
, next_mds
);
2160 BUG_ON(req
->r_got_result
);
2161 req
->r_num_fwd
= fwd_seq
;
2162 req
->r_resend_mds
= next_mds
;
2163 put_request_session(req
);
2164 __do_request(mdsc
, req
);
2166 ceph_mdsc_put_request(req
);
2168 mutex_unlock(&mdsc
->mutex
);
2172 pr_err("mdsc_handle_forward decode error err=%d\n", err
);
2176 * handle a mds session control message
2178 static void handle_session(struct ceph_mds_session
*session
,
2179 struct ceph_msg
*msg
)
2181 struct ceph_mds_client
*mdsc
= session
->s_mdsc
;
2184 int mds
= session
->s_mds
;
2185 struct ceph_mds_session_head
*h
= msg
->front
.iov_base
;
2189 if (msg
->front
.iov_len
!= sizeof(*h
))
2191 op
= le32_to_cpu(h
->op
);
2192 seq
= le64_to_cpu(h
->seq
);
2194 mutex_lock(&mdsc
->mutex
);
2195 if (op
== CEPH_SESSION_CLOSE
)
2196 __unregister_session(mdsc
, session
);
2197 /* FIXME: this ttl calculation is generous */
2198 session
->s_ttl
= jiffies
+ HZ
*mdsc
->mdsmap
->m_session_autoclose
;
2199 mutex_unlock(&mdsc
->mutex
);
2201 mutex_lock(&session
->s_mutex
);
2203 dout("handle_session mds%d %s %p state %s seq %llu\n",
2204 mds
, ceph_session_op_name(op
), session
,
2205 session_state_name(session
->s_state
), seq
);
2207 if (session
->s_state
== CEPH_MDS_SESSION_HUNG
) {
2208 session
->s_state
= CEPH_MDS_SESSION_OPEN
;
2209 pr_info("mds%d came back\n", session
->s_mds
);
2213 case CEPH_SESSION_OPEN
:
2214 if (session
->s_state
== CEPH_MDS_SESSION_RECONNECTING
)
2215 pr_info("mds%d reconnect success\n", session
->s_mds
);
2216 session
->s_state
= CEPH_MDS_SESSION_OPEN
;
2217 renewed_caps(mdsc
, session
, 0);
2220 __close_session(mdsc
, session
);
2223 case CEPH_SESSION_RENEWCAPS
:
2224 if (session
->s_renew_seq
== seq
)
2225 renewed_caps(mdsc
, session
, 1);
2228 case CEPH_SESSION_CLOSE
:
2229 if (session
->s_state
== CEPH_MDS_SESSION_RECONNECTING
)
2230 pr_info("mds%d reconnect denied\n", session
->s_mds
);
2231 remove_session_caps(session
);
2232 wake
= 1; /* for good measure */
2233 wake_up_all(&mdsc
->session_close_wq
);
2234 kick_requests(mdsc
, mds
);
2237 case CEPH_SESSION_STALE
:
2238 pr_info("mds%d caps went stale, renewing\n",
2240 spin_lock(&session
->s_cap_lock
);
2241 session
->s_cap_gen
++;
2242 session
->s_cap_ttl
= 0;
2243 spin_unlock(&session
->s_cap_lock
);
2244 send_renew_caps(mdsc
, session
);
2247 case CEPH_SESSION_RECALL_STATE
:
2248 trim_caps(mdsc
, session
, le32_to_cpu(h
->max_caps
));
2252 pr_err("mdsc_handle_session bad op %d mds%d\n", op
, mds
);
2256 mutex_unlock(&session
->s_mutex
);
2258 mutex_lock(&mdsc
->mutex
);
2259 __wake_requests(mdsc
, &session
->s_waiting
);
2260 mutex_unlock(&mdsc
->mutex
);
2265 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds
,
2266 (int)msg
->front
.iov_len
);
2273 * called under session->mutex.
2275 static void replay_unsafe_requests(struct ceph_mds_client
*mdsc
,
2276 struct ceph_mds_session
*session
)
2278 struct ceph_mds_request
*req
, *nreq
;
2281 dout("replay_unsafe_requests mds%d\n", session
->s_mds
);
2283 mutex_lock(&mdsc
->mutex
);
2284 list_for_each_entry_safe(req
, nreq
, &session
->s_unsafe
, r_unsafe_item
) {
2285 err
= __prepare_send_request(mdsc
, req
, session
->s_mds
);
2287 ceph_msg_get(req
->r_request
);
2288 ceph_con_send(&session
->s_con
, req
->r_request
);
2291 mutex_unlock(&mdsc
->mutex
);
2295 * Encode information about a cap for a reconnect with the MDS.
2297 static int encode_caps_cb(struct inode
*inode
, struct ceph_cap
*cap
,
2301 struct ceph_mds_cap_reconnect v2
;
2302 struct ceph_mds_cap_reconnect_v1 v1
;
2305 struct ceph_inode_info
*ci
;
2306 struct ceph_reconnect_state
*recon_state
= arg
;
2307 struct ceph_pagelist
*pagelist
= recon_state
->pagelist
;
2311 struct dentry
*dentry
;
2315 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2316 inode
, ceph_vinop(inode
), cap
, cap
->cap_id
,
2317 ceph_cap_string(cap
->issued
));
2318 err
= ceph_pagelist_encode_64(pagelist
, ceph_ino(inode
));
2322 dentry
= d_find_alias(inode
);
2324 path
= ceph_mdsc_build_path(dentry
, &pathlen
, &pathbase
, 0);
2326 err
= PTR_ERR(path
);
2333 err
= ceph_pagelist_encode_string(pagelist
, path
, pathlen
);
2337 spin_lock(&inode
->i_lock
);
2338 cap
->seq
= 0; /* reset cap seq */
2339 cap
->issue_seq
= 0; /* and issue_seq */
2341 if (recon_state
->flock
) {
2342 rec
.v2
.cap_id
= cpu_to_le64(cap
->cap_id
);
2343 rec
.v2
.wanted
= cpu_to_le32(__ceph_caps_wanted(ci
));
2344 rec
.v2
.issued
= cpu_to_le32(cap
->issued
);
2345 rec
.v2
.snaprealm
= cpu_to_le64(ci
->i_snap_realm
->ino
);
2346 rec
.v2
.pathbase
= cpu_to_le64(pathbase
);
2347 rec
.v2
.flock_len
= 0;
2348 reclen
= sizeof(rec
.v2
);
2350 rec
.v1
.cap_id
= cpu_to_le64(cap
->cap_id
);
2351 rec
.v1
.wanted
= cpu_to_le32(__ceph_caps_wanted(ci
));
2352 rec
.v1
.issued
= cpu_to_le32(cap
->issued
);
2353 rec
.v1
.size
= cpu_to_le64(inode
->i_size
);
2354 ceph_encode_timespec(&rec
.v1
.mtime
, &inode
->i_mtime
);
2355 ceph_encode_timespec(&rec
.v1
.atime
, &inode
->i_atime
);
2356 rec
.v1
.snaprealm
= cpu_to_le64(ci
->i_snap_realm
->ino
);
2357 rec
.v1
.pathbase
= cpu_to_le64(pathbase
);
2358 reclen
= sizeof(rec
.v1
);
2360 spin_unlock(&inode
->i_lock
);
2362 if (recon_state
->flock
) {
2363 int num_fcntl_locks
, num_flock_locks
;
2366 ceph_count_locks(inode
, &num_fcntl_locks
, &num_flock_locks
);
2367 rec
.v2
.flock_len
= (2*sizeof(u32
) +
2368 (num_fcntl_locks
+num_flock_locks
) *
2369 sizeof(struct ceph_filelock
));
2371 err
= ceph_pagelist_append(pagelist
, &rec
, reclen
);
2373 err
= ceph_encode_locks(inode
, pagelist
,
2388 * If an MDS fails and recovers, clients need to reconnect in order to
2389 * reestablish shared state. This includes all caps issued through
2390 * this session _and_ the snap_realm hierarchy. Because it's not
2391 * clear which snap realms the mds cares about, we send everything we
2392 * know about.. that ensures we'll then get any new info the
2393 * recovering MDS might have.
2395 * This is a relatively heavyweight operation, but it's rare.
2397 * called with mdsc->mutex held.
2399 static void send_mds_reconnect(struct ceph_mds_client
*mdsc
,
2400 struct ceph_mds_session
*session
)
2402 struct ceph_msg
*reply
;
2404 int mds
= session
->s_mds
;
2406 struct ceph_pagelist
*pagelist
;
2407 struct ceph_reconnect_state recon_state
;
2409 pr_info("mds%d reconnect start\n", mds
);
2411 pagelist
= kmalloc(sizeof(*pagelist
), GFP_NOFS
);
2413 goto fail_nopagelist
;
2414 ceph_pagelist_init(pagelist
);
2416 reply
= ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT
, 0, GFP_NOFS
);
2420 mutex_lock(&session
->s_mutex
);
2421 session
->s_state
= CEPH_MDS_SESSION_RECONNECTING
;
2424 ceph_con_open(&session
->s_con
,
2425 ceph_mdsmap_get_addr(mdsc
->mdsmap
, mds
));
2427 /* replay unsafe requests */
2428 replay_unsafe_requests(mdsc
, session
);
2430 down_read(&mdsc
->snap_rwsem
);
2432 dout("session %p state %s\n", session
,
2433 session_state_name(session
->s_state
));
2435 /* drop old cap expires; we're about to reestablish that state */
2436 discard_cap_releases(mdsc
, session
);
2438 /* traverse this session's caps */
2439 err
= ceph_pagelist_encode_32(pagelist
, session
->s_nr_caps
);
2443 recon_state
.pagelist
= pagelist
;
2444 recon_state
.flock
= session
->s_con
.peer_features
& CEPH_FEATURE_FLOCK
;
2445 err
= iterate_session_caps(session
, encode_caps_cb
, &recon_state
);
2450 * snaprealms. we provide mds with the ino, seq (version), and
2451 * parent for all of our realms. If the mds has any newer info,
2454 for (p
= rb_first(&mdsc
->snap_realms
); p
; p
= rb_next(p
)) {
2455 struct ceph_snap_realm
*realm
=
2456 rb_entry(p
, struct ceph_snap_realm
, node
);
2457 struct ceph_mds_snaprealm_reconnect sr_rec
;
2459 dout(" adding snap realm %llx seq %lld parent %llx\n",
2460 realm
->ino
, realm
->seq
, realm
->parent_ino
);
2461 sr_rec
.ino
= cpu_to_le64(realm
->ino
);
2462 sr_rec
.seq
= cpu_to_le64(realm
->seq
);
2463 sr_rec
.parent
= cpu_to_le64(realm
->parent_ino
);
2464 err
= ceph_pagelist_append(pagelist
, &sr_rec
, sizeof(sr_rec
));
2469 reply
->pagelist
= pagelist
;
2470 if (recon_state
.flock
)
2471 reply
->hdr
.version
= cpu_to_le16(2);
2472 reply
->hdr
.data_len
= cpu_to_le32(pagelist
->length
);
2473 reply
->nr_pages
= calc_pages_for(0, pagelist
->length
);
2474 ceph_con_send(&session
->s_con
, reply
);
2476 mutex_unlock(&session
->s_mutex
);
2478 mutex_lock(&mdsc
->mutex
);
2479 __wake_requests(mdsc
, &session
->s_waiting
);
2480 mutex_unlock(&mdsc
->mutex
);
2482 up_read(&mdsc
->snap_rwsem
);
2486 ceph_msg_put(reply
);
2487 up_read(&mdsc
->snap_rwsem
);
2488 mutex_unlock(&session
->s_mutex
);
2490 ceph_pagelist_release(pagelist
);
2493 pr_err("error %d preparing reconnect for mds%d\n", err
, mds
);
2499 * compare old and new mdsmaps, kicking requests
2500 * and closing out old connections as necessary
2502 * called under mdsc->mutex.
2504 static void check_new_map(struct ceph_mds_client
*mdsc
,
2505 struct ceph_mdsmap
*newmap
,
2506 struct ceph_mdsmap
*oldmap
)
2509 int oldstate
, newstate
;
2510 struct ceph_mds_session
*s
;
2512 dout("check_new_map new %u old %u\n",
2513 newmap
->m_epoch
, oldmap
->m_epoch
);
2515 for (i
= 0; i
< oldmap
->m_max_mds
&& i
< mdsc
->max_sessions
; i
++) {
2516 if (mdsc
->sessions
[i
] == NULL
)
2518 s
= mdsc
->sessions
[i
];
2519 oldstate
= ceph_mdsmap_get_state(oldmap
, i
);
2520 newstate
= ceph_mdsmap_get_state(newmap
, i
);
2522 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
2523 i
, ceph_mds_state_name(oldstate
),
2524 ceph_mdsmap_is_laggy(oldmap
, i
) ? " (laggy)" : "",
2525 ceph_mds_state_name(newstate
),
2526 ceph_mdsmap_is_laggy(newmap
, i
) ? " (laggy)" : "",
2527 session_state_name(s
->s_state
));
2529 if (memcmp(ceph_mdsmap_get_addr(oldmap
, i
),
2530 ceph_mdsmap_get_addr(newmap
, i
),
2531 sizeof(struct ceph_entity_addr
))) {
2532 if (s
->s_state
== CEPH_MDS_SESSION_OPENING
) {
2533 /* the session never opened, just close it
2535 __wake_requests(mdsc
, &s
->s_waiting
);
2536 __unregister_session(mdsc
, s
);
2539 mutex_unlock(&mdsc
->mutex
);
2540 mutex_lock(&s
->s_mutex
);
2541 mutex_lock(&mdsc
->mutex
);
2542 ceph_con_close(&s
->s_con
);
2543 mutex_unlock(&s
->s_mutex
);
2544 s
->s_state
= CEPH_MDS_SESSION_RESTARTING
;
2547 /* kick any requests waiting on the recovering mds */
2548 kick_requests(mdsc
, i
);
2549 } else if (oldstate
== newstate
) {
2550 continue; /* nothing new with this mds */
2556 if (s
->s_state
== CEPH_MDS_SESSION_RESTARTING
&&
2557 newstate
>= CEPH_MDS_STATE_RECONNECT
) {
2558 mutex_unlock(&mdsc
->mutex
);
2559 send_mds_reconnect(mdsc
, s
);
2560 mutex_lock(&mdsc
->mutex
);
2564 * kick request on any mds that has gone active.
2566 if (oldstate
< CEPH_MDS_STATE_ACTIVE
&&
2567 newstate
>= CEPH_MDS_STATE_ACTIVE
) {
2568 if (oldstate
!= CEPH_MDS_STATE_CREATING
&&
2569 oldstate
!= CEPH_MDS_STATE_STARTING
)
2570 pr_info("mds%d recovery completed\n", s
->s_mds
);
2571 kick_requests(mdsc
, i
);
2572 ceph_kick_flushing_caps(mdsc
, s
);
2573 wake_up_session_caps(s
, 1);
2577 for (i
= 0; i
< newmap
->m_max_mds
&& i
< mdsc
->max_sessions
; i
++) {
2578 s
= mdsc
->sessions
[i
];
2581 if (!ceph_mdsmap_is_laggy(newmap
, i
))
2583 if (s
->s_state
== CEPH_MDS_SESSION_OPEN
||
2584 s
->s_state
== CEPH_MDS_SESSION_HUNG
||
2585 s
->s_state
== CEPH_MDS_SESSION_CLOSING
) {
2586 dout(" connecting to export targets of laggy mds%d\n",
2588 __open_export_target_sessions(mdsc
, s
);
2600 * caller must hold session s_mutex, dentry->d_lock
2602 void __ceph_mdsc_drop_dentry_lease(struct dentry
*dentry
)
2604 struct ceph_dentry_info
*di
= ceph_dentry(dentry
);
2606 ceph_put_mds_session(di
->lease_session
);
2607 di
->lease_session
= NULL
;
2610 static void handle_lease(struct ceph_mds_client
*mdsc
,
2611 struct ceph_mds_session
*session
,
2612 struct ceph_msg
*msg
)
2614 struct super_block
*sb
= mdsc
->client
->sb
;
2615 struct inode
*inode
;
2616 struct ceph_inode_info
*ci
;
2617 struct dentry
*parent
, *dentry
;
2618 struct ceph_dentry_info
*di
;
2619 int mds
= session
->s_mds
;
2620 struct ceph_mds_lease
*h
= msg
->front
.iov_base
;
2622 struct ceph_vino vino
;
2627 dout("handle_lease from mds%d\n", mds
);
2630 if (msg
->front
.iov_len
< sizeof(*h
) + sizeof(u32
))
2632 vino
.ino
= le64_to_cpu(h
->ino
);
2633 vino
.snap
= CEPH_NOSNAP
;
2634 mask
= le16_to_cpu(h
->mask
);
2635 seq
= le32_to_cpu(h
->seq
);
2636 dname
.name
= (void *)h
+ sizeof(*h
) + sizeof(u32
);
2637 dname
.len
= msg
->front
.iov_len
- sizeof(*h
) - sizeof(u32
);
2638 if (dname
.len
!= get_unaligned_le32(h
+1))
2641 mutex_lock(&session
->s_mutex
);
2645 inode
= ceph_find_inode(sb
, vino
);
2646 dout("handle_lease %s, mask %d, ino %llx %p %.*s\n",
2647 ceph_lease_op_name(h
->action
), mask
, vino
.ino
, inode
,
2648 dname
.len
, dname
.name
);
2649 if (inode
== NULL
) {
2650 dout("handle_lease no inode %llx\n", vino
.ino
);
2653 ci
= ceph_inode(inode
);
2656 parent
= d_find_alias(inode
);
2658 dout("no parent dentry on inode %p\n", inode
);
2660 goto release
; /* hrm... */
2662 dname
.hash
= full_name_hash(dname
.name
, dname
.len
);
2663 dentry
= d_lookup(parent
, &dname
);
2668 spin_lock(&dentry
->d_lock
);
2669 di
= ceph_dentry(dentry
);
2670 switch (h
->action
) {
2671 case CEPH_MDS_LEASE_REVOKE
:
2672 if (di
&& di
->lease_session
== session
) {
2673 if (ceph_seq_cmp(di
->lease_seq
, seq
) > 0)
2674 h
->seq
= cpu_to_le32(di
->lease_seq
);
2675 __ceph_mdsc_drop_dentry_lease(dentry
);
2680 case CEPH_MDS_LEASE_RENEW
:
2681 if (di
&& di
->lease_session
== session
&&
2682 di
->lease_gen
== session
->s_cap_gen
&&
2683 di
->lease_renew_from
&&
2684 di
->lease_renew_after
== 0) {
2685 unsigned long duration
=
2686 le32_to_cpu(h
->duration_ms
) * HZ
/ 1000;
2688 di
->lease_seq
= seq
;
2689 dentry
->d_time
= di
->lease_renew_from
+ duration
;
2690 di
->lease_renew_after
= di
->lease_renew_from
+
2692 di
->lease_renew_from
= 0;
2696 spin_unlock(&dentry
->d_lock
);
2703 /* let's just reuse the same message */
2704 h
->action
= CEPH_MDS_LEASE_REVOKE_ACK
;
2706 ceph_con_send(&session
->s_con
, msg
);
2710 mutex_unlock(&session
->s_mutex
);
2714 pr_err("corrupt lease message\n");
2718 void ceph_mdsc_lease_send_msg(struct ceph_mds_session
*session
,
2719 struct inode
*inode
,
2720 struct dentry
*dentry
, char action
,
2723 struct ceph_msg
*msg
;
2724 struct ceph_mds_lease
*lease
;
2725 int len
= sizeof(*lease
) + sizeof(u32
);
2728 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
2729 inode
, dentry
, ceph_lease_op_name(action
), session
->s_mds
);
2730 dnamelen
= dentry
->d_name
.len
;
2733 msg
= ceph_msg_new(CEPH_MSG_CLIENT_LEASE
, len
, GFP_NOFS
);
2736 lease
= msg
->front
.iov_base
;
2737 lease
->action
= action
;
2738 lease
->mask
= cpu_to_le16(1);
2739 lease
->ino
= cpu_to_le64(ceph_vino(inode
).ino
);
2740 lease
->first
= lease
->last
= cpu_to_le64(ceph_vino(inode
).snap
);
2741 lease
->seq
= cpu_to_le32(seq
);
2742 put_unaligned_le32(dnamelen
, lease
+ 1);
2743 memcpy((void *)(lease
+ 1) + 4, dentry
->d_name
.name
, dnamelen
);
2746 * if this is a preemptive lease RELEASE, no need to
2747 * flush request stream, since the actual request will
2750 msg
->more_to_follow
= (action
== CEPH_MDS_LEASE_RELEASE
);
2752 ceph_con_send(&session
->s_con
, msg
);
2756 * Preemptively release a lease we expect to invalidate anyway.
2757 * Pass @inode always, @dentry is optional.
2759 void ceph_mdsc_lease_release(struct ceph_mds_client
*mdsc
, struct inode
*inode
,
2760 struct dentry
*dentry
, int mask
)
2762 struct ceph_dentry_info
*di
;
2763 struct ceph_mds_session
*session
;
2766 BUG_ON(inode
== NULL
);
2767 BUG_ON(dentry
== NULL
);
2770 /* is dentry lease valid? */
2771 spin_lock(&dentry
->d_lock
);
2772 di
= ceph_dentry(dentry
);
2773 if (!di
|| !di
->lease_session
||
2774 di
->lease_session
->s_mds
< 0 ||
2775 di
->lease_gen
!= di
->lease_session
->s_cap_gen
||
2776 !time_before(jiffies
, dentry
->d_time
)) {
2777 dout("lease_release inode %p dentry %p -- "
2779 inode
, dentry
, mask
);
2780 spin_unlock(&dentry
->d_lock
);
2784 /* we do have a lease on this dentry; note mds and seq */
2785 session
= ceph_get_mds_session(di
->lease_session
);
2786 seq
= di
->lease_seq
;
2787 __ceph_mdsc_drop_dentry_lease(dentry
);
2788 spin_unlock(&dentry
->d_lock
);
2790 dout("lease_release inode %p dentry %p mask %d to mds%d\n",
2791 inode
, dentry
, mask
, session
->s_mds
);
2792 ceph_mdsc_lease_send_msg(session
, inode
, dentry
,
2793 CEPH_MDS_LEASE_RELEASE
, seq
);
2794 ceph_put_mds_session(session
);
2798 * drop all leases (and dentry refs) in preparation for umount
2800 static void drop_leases(struct ceph_mds_client
*mdsc
)
2804 dout("drop_leases\n");
2805 mutex_lock(&mdsc
->mutex
);
2806 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
2807 struct ceph_mds_session
*s
= __ceph_lookup_mds_session(mdsc
, i
);
2810 mutex_unlock(&mdsc
->mutex
);
2811 mutex_lock(&s
->s_mutex
);
2812 mutex_unlock(&s
->s_mutex
);
2813 ceph_put_mds_session(s
);
2814 mutex_lock(&mdsc
->mutex
);
2816 mutex_unlock(&mdsc
->mutex
);
2822 * delayed work -- periodically trim expired leases, renew caps with mds
2824 static void schedule_delayed(struct ceph_mds_client
*mdsc
)
2827 unsigned hz
= round_jiffies_relative(HZ
* delay
);
2828 schedule_delayed_work(&mdsc
->delayed_work
, hz
);
2831 static void delayed_work(struct work_struct
*work
)
2834 struct ceph_mds_client
*mdsc
=
2835 container_of(work
, struct ceph_mds_client
, delayed_work
.work
);
2839 dout("mdsc delayed_work\n");
2840 ceph_check_delayed_caps(mdsc
);
2842 mutex_lock(&mdsc
->mutex
);
2843 renew_interval
= mdsc
->mdsmap
->m_session_timeout
>> 2;
2844 renew_caps
= time_after_eq(jiffies
, HZ
*renew_interval
+
2845 mdsc
->last_renew_caps
);
2847 mdsc
->last_renew_caps
= jiffies
;
2849 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
2850 struct ceph_mds_session
*s
= __ceph_lookup_mds_session(mdsc
, i
);
2853 if (s
->s_state
== CEPH_MDS_SESSION_CLOSING
) {
2854 dout("resending session close request for mds%d\n",
2856 request_close_session(mdsc
, s
);
2857 ceph_put_mds_session(s
);
2860 if (s
->s_ttl
&& time_after(jiffies
, s
->s_ttl
)) {
2861 if (s
->s_state
== CEPH_MDS_SESSION_OPEN
) {
2862 s
->s_state
= CEPH_MDS_SESSION_HUNG
;
2863 pr_info("mds%d hung\n", s
->s_mds
);
2866 if (s
->s_state
< CEPH_MDS_SESSION_OPEN
) {
2867 /* this mds is failed or recovering, just wait */
2868 ceph_put_mds_session(s
);
2871 mutex_unlock(&mdsc
->mutex
);
2873 mutex_lock(&s
->s_mutex
);
2875 send_renew_caps(mdsc
, s
);
2877 ceph_con_keepalive(&s
->s_con
);
2878 ceph_add_cap_releases(mdsc
, s
);
2879 if (s
->s_state
== CEPH_MDS_SESSION_OPEN
||
2880 s
->s_state
== CEPH_MDS_SESSION_HUNG
)
2881 ceph_send_cap_releases(mdsc
, s
);
2882 mutex_unlock(&s
->s_mutex
);
2883 ceph_put_mds_session(s
);
2885 mutex_lock(&mdsc
->mutex
);
2887 mutex_unlock(&mdsc
->mutex
);
2889 schedule_delayed(mdsc
);
2893 int ceph_mdsc_init(struct ceph_mds_client
*mdsc
, struct ceph_client
*client
)
2895 mdsc
->client
= client
;
2896 mutex_init(&mdsc
->mutex
);
2897 mdsc
->mdsmap
= kzalloc(sizeof(*mdsc
->mdsmap
), GFP_NOFS
);
2898 if (mdsc
->mdsmap
== NULL
)
2901 init_completion(&mdsc
->safe_umount_waiters
);
2902 init_waitqueue_head(&mdsc
->session_close_wq
);
2903 INIT_LIST_HEAD(&mdsc
->waiting_for_map
);
2904 mdsc
->sessions
= NULL
;
2905 mdsc
->max_sessions
= 0;
2907 init_rwsem(&mdsc
->snap_rwsem
);
2908 mdsc
->snap_realms
= RB_ROOT
;
2909 INIT_LIST_HEAD(&mdsc
->snap_empty
);
2910 spin_lock_init(&mdsc
->snap_empty_lock
);
2912 mdsc
->request_tree
= RB_ROOT
;
2913 INIT_DELAYED_WORK(&mdsc
->delayed_work
, delayed_work
);
2914 mdsc
->last_renew_caps
= jiffies
;
2915 INIT_LIST_HEAD(&mdsc
->cap_delay_list
);
2916 spin_lock_init(&mdsc
->cap_delay_lock
);
2917 INIT_LIST_HEAD(&mdsc
->snap_flush_list
);
2918 spin_lock_init(&mdsc
->snap_flush_lock
);
2919 mdsc
->cap_flush_seq
= 0;
2920 INIT_LIST_HEAD(&mdsc
->cap_dirty
);
2921 mdsc
->num_cap_flushing
= 0;
2922 spin_lock_init(&mdsc
->cap_dirty_lock
);
2923 init_waitqueue_head(&mdsc
->cap_flushing_wq
);
2924 spin_lock_init(&mdsc
->dentry_lru_lock
);
2925 INIT_LIST_HEAD(&mdsc
->dentry_lru
);
2927 ceph_caps_init(mdsc
);
2928 ceph_adjust_min_caps(mdsc
, client
->min_caps
);
2934 * Wait for safe replies on open mds requests. If we time out, drop
2935 * all requests from the tree to avoid dangling dentry refs.
2937 static void wait_requests(struct ceph_mds_client
*mdsc
)
2939 struct ceph_mds_request
*req
;
2940 struct ceph_client
*client
= mdsc
->client
;
2942 mutex_lock(&mdsc
->mutex
);
2943 if (__get_oldest_req(mdsc
)) {
2944 mutex_unlock(&mdsc
->mutex
);
2946 dout("wait_requests waiting for requests\n");
2947 wait_for_completion_timeout(&mdsc
->safe_umount_waiters
,
2948 client
->mount_args
->mount_timeout
* HZ
);
2950 /* tear down remaining requests */
2951 mutex_lock(&mdsc
->mutex
);
2952 while ((req
= __get_oldest_req(mdsc
))) {
2953 dout("wait_requests timed out on tid %llu\n",
2955 __unregister_request(mdsc
, req
);
2958 mutex_unlock(&mdsc
->mutex
);
2959 dout("wait_requests done\n");
2963 * called before mount is ro, and before dentries are torn down.
2964 * (hmm, does this still race with new lookups?)
2966 void ceph_mdsc_pre_umount(struct ceph_mds_client
*mdsc
)
2968 dout("pre_umount\n");
2972 ceph_flush_dirty_caps(mdsc
);
2973 wait_requests(mdsc
);
2976 * wait for reply handlers to drop their request refs and
2977 * their inode/dcache refs
2983 * wait for all write mds requests to flush.
2985 static void wait_unsafe_requests(struct ceph_mds_client
*mdsc
, u64 want_tid
)
2987 struct ceph_mds_request
*req
= NULL
, *nextreq
;
2990 mutex_lock(&mdsc
->mutex
);
2991 dout("wait_unsafe_requests want %lld\n", want_tid
);
2993 req
= __get_oldest_req(mdsc
);
2994 while (req
&& req
->r_tid
<= want_tid
) {
2995 /* find next request */
2996 n
= rb_next(&req
->r_node
);
2998 nextreq
= rb_entry(n
, struct ceph_mds_request
, r_node
);
3001 if ((req
->r_op
& CEPH_MDS_OP_WRITE
)) {
3003 ceph_mdsc_get_request(req
);
3005 ceph_mdsc_get_request(nextreq
);
3006 mutex_unlock(&mdsc
->mutex
);
3007 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3008 req
->r_tid
, want_tid
);
3009 wait_for_completion(&req
->r_safe_completion
);
3010 mutex_lock(&mdsc
->mutex
);
3011 ceph_mdsc_put_request(req
);
3013 break; /* next dne before, so we're done! */
3014 if (RB_EMPTY_NODE(&nextreq
->r_node
)) {
3015 /* next request was removed from tree */
3016 ceph_mdsc_put_request(nextreq
);
3019 ceph_mdsc_put_request(nextreq
); /* won't go away */
3023 mutex_unlock(&mdsc
->mutex
);
3024 dout("wait_unsafe_requests done\n");
3027 void ceph_mdsc_sync(struct ceph_mds_client
*mdsc
)
3029 u64 want_tid
, want_flush
;
3031 if (mdsc
->client
->mount_state
== CEPH_MOUNT_SHUTDOWN
)
3035 mutex_lock(&mdsc
->mutex
);
3036 want_tid
= mdsc
->last_tid
;
3037 want_flush
= mdsc
->cap_flush_seq
;
3038 mutex_unlock(&mdsc
->mutex
);
3039 dout("sync want tid %lld flush_seq %lld\n", want_tid
, want_flush
);
3041 ceph_flush_dirty_caps(mdsc
);
3043 wait_unsafe_requests(mdsc
, want_tid
);
3044 wait_event(mdsc
->cap_flushing_wq
, check_cap_flush(mdsc
, want_flush
));
3048 * true if all sessions are closed, or we force unmount
3050 bool done_closing_sessions(struct ceph_mds_client
*mdsc
)
3054 if (mdsc
->client
->mount_state
== CEPH_MOUNT_SHUTDOWN
)
3057 mutex_lock(&mdsc
->mutex
);
3058 for (i
= 0; i
< mdsc
->max_sessions
; i
++)
3059 if (mdsc
->sessions
[i
])
3061 mutex_unlock(&mdsc
->mutex
);
3066 * called after sb is ro.
3068 void ceph_mdsc_close_sessions(struct ceph_mds_client
*mdsc
)
3070 struct ceph_mds_session
*session
;
3072 struct ceph_client
*client
= mdsc
->client
;
3073 unsigned long timeout
= client
->mount_args
->mount_timeout
* HZ
;
3075 dout("close_sessions\n");
3077 /* close sessions */
3078 mutex_lock(&mdsc
->mutex
);
3079 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
3080 session
= __ceph_lookup_mds_session(mdsc
, i
);
3083 mutex_unlock(&mdsc
->mutex
);
3084 mutex_lock(&session
->s_mutex
);
3085 __close_session(mdsc
, session
);
3086 mutex_unlock(&session
->s_mutex
);
3087 ceph_put_mds_session(session
);
3088 mutex_lock(&mdsc
->mutex
);
3090 mutex_unlock(&mdsc
->mutex
);
3092 dout("waiting for sessions to close\n");
3093 wait_event_timeout(mdsc
->session_close_wq
, done_closing_sessions(mdsc
),
3096 /* tear down remaining sessions */
3097 mutex_lock(&mdsc
->mutex
);
3098 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
3099 if (mdsc
->sessions
[i
]) {
3100 session
= get_session(mdsc
->sessions
[i
]);
3101 __unregister_session(mdsc
, session
);
3102 mutex_unlock(&mdsc
->mutex
);
3103 mutex_lock(&session
->s_mutex
);
3104 remove_session_caps(session
);
3105 mutex_unlock(&session
->s_mutex
);
3106 ceph_put_mds_session(session
);
3107 mutex_lock(&mdsc
->mutex
);
3110 WARN_ON(!list_empty(&mdsc
->cap_delay_list
));
3111 mutex_unlock(&mdsc
->mutex
);
3113 ceph_cleanup_empty_realms(mdsc
);
3115 cancel_delayed_work_sync(&mdsc
->delayed_work
); /* cancel timer */
3120 void ceph_mdsc_stop(struct ceph_mds_client
*mdsc
)
3123 cancel_delayed_work_sync(&mdsc
->delayed_work
); /* cancel timer */
3125 ceph_mdsmap_destroy(mdsc
->mdsmap
);
3126 kfree(mdsc
->sessions
);
3127 ceph_caps_finalize(mdsc
);
3132 * handle mds map update.
3134 void ceph_mdsc_handle_map(struct ceph_mds_client
*mdsc
, struct ceph_msg
*msg
)
3138 void *p
= msg
->front
.iov_base
;
3139 void *end
= p
+ msg
->front
.iov_len
;
3140 struct ceph_mdsmap
*newmap
, *oldmap
;
3141 struct ceph_fsid fsid
;
3144 ceph_decode_need(&p
, end
, sizeof(fsid
)+2*sizeof(u32
), bad
);
3145 ceph_decode_copy(&p
, &fsid
, sizeof(fsid
));
3146 if (ceph_check_fsid(mdsc
->client
, &fsid
) < 0)
3148 epoch
= ceph_decode_32(&p
);
3149 maplen
= ceph_decode_32(&p
);
3150 dout("handle_map epoch %u len %d\n", epoch
, (int)maplen
);
3152 /* do we need it? */
3153 ceph_monc_got_mdsmap(&mdsc
->client
->monc
, epoch
);
3154 mutex_lock(&mdsc
->mutex
);
3155 if (mdsc
->mdsmap
&& epoch
<= mdsc
->mdsmap
->m_epoch
) {
3156 dout("handle_map epoch %u <= our %u\n",
3157 epoch
, mdsc
->mdsmap
->m_epoch
);
3158 mutex_unlock(&mdsc
->mutex
);
3162 newmap
= ceph_mdsmap_decode(&p
, end
);
3163 if (IS_ERR(newmap
)) {
3164 err
= PTR_ERR(newmap
);
3168 /* swap into place */
3170 oldmap
= mdsc
->mdsmap
;
3171 mdsc
->mdsmap
= newmap
;
3172 check_new_map(mdsc
, newmap
, oldmap
);
3173 ceph_mdsmap_destroy(oldmap
);
3175 mdsc
->mdsmap
= newmap
; /* first mds map */
3177 mdsc
->client
->sb
->s_maxbytes
= mdsc
->mdsmap
->m_max_file_size
;
3179 __wake_requests(mdsc
, &mdsc
->waiting_for_map
);
3181 mutex_unlock(&mdsc
->mutex
);
3182 schedule_delayed(mdsc
);
3186 mutex_unlock(&mdsc
->mutex
);
3188 pr_err("error decoding mdsmap %d\n", err
);
3192 static struct ceph_connection
*con_get(struct ceph_connection
*con
)
3194 struct ceph_mds_session
*s
= con
->private;
3196 if (get_session(s
)) {
3197 dout("mdsc con_get %p ok (%d)\n", s
, atomic_read(&s
->s_ref
));
3200 dout("mdsc con_get %p FAIL\n", s
);
3204 static void con_put(struct ceph_connection
*con
)
3206 struct ceph_mds_session
*s
= con
->private;
3208 ceph_put_mds_session(s
);
3209 dout("mdsc con_put %p (%d)\n", s
, atomic_read(&s
->s_ref
));
3213 * if the client is unresponsive for long enough, the mds will kill
3214 * the session entirely.
3216 static void peer_reset(struct ceph_connection
*con
)
3218 struct ceph_mds_session
*s
= con
->private;
3219 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
3221 pr_warning("mds%d closed our session\n", s
->s_mds
);
3222 send_mds_reconnect(mdsc
, s
);
3225 static void dispatch(struct ceph_connection
*con
, struct ceph_msg
*msg
)
3227 struct ceph_mds_session
*s
= con
->private;
3228 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
3229 int type
= le16_to_cpu(msg
->hdr
.type
);
3231 mutex_lock(&mdsc
->mutex
);
3232 if (__verify_registered_session(mdsc
, s
) < 0) {
3233 mutex_unlock(&mdsc
->mutex
);
3236 mutex_unlock(&mdsc
->mutex
);
3239 case CEPH_MSG_MDS_MAP
:
3240 ceph_mdsc_handle_map(mdsc
, msg
);
3242 case CEPH_MSG_CLIENT_SESSION
:
3243 handle_session(s
, msg
);
3245 case CEPH_MSG_CLIENT_REPLY
:
3246 handle_reply(s
, msg
);
3248 case CEPH_MSG_CLIENT_REQUEST_FORWARD
:
3249 handle_forward(mdsc
, s
, msg
);
3251 case CEPH_MSG_CLIENT_CAPS
:
3252 ceph_handle_caps(s
, msg
);
3254 case CEPH_MSG_CLIENT_SNAP
:
3255 ceph_handle_snap(mdsc
, s
, msg
);
3257 case CEPH_MSG_CLIENT_LEASE
:
3258 handle_lease(mdsc
, s
, msg
);
3262 pr_err("received unknown message type %d %s\n", type
,
3263 ceph_msg_type_name(type
));
3272 static int get_authorizer(struct ceph_connection
*con
,
3273 void **buf
, int *len
, int *proto
,
3274 void **reply_buf
, int *reply_len
, int force_new
)
3276 struct ceph_mds_session
*s
= con
->private;
3277 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
3278 struct ceph_auth_client
*ac
= mdsc
->client
->monc
.auth
;
3281 if (force_new
&& s
->s_authorizer
) {
3282 ac
->ops
->destroy_authorizer(ac
, s
->s_authorizer
);
3283 s
->s_authorizer
= NULL
;
3285 if (s
->s_authorizer
== NULL
) {
3286 if (ac
->ops
->create_authorizer
) {
3287 ret
= ac
->ops
->create_authorizer(
3288 ac
, CEPH_ENTITY_TYPE_MDS
,
3290 &s
->s_authorizer_buf
,
3291 &s
->s_authorizer_buf_len
,
3292 &s
->s_authorizer_reply_buf
,
3293 &s
->s_authorizer_reply_buf_len
);
3299 *proto
= ac
->protocol
;
3300 *buf
= s
->s_authorizer_buf
;
3301 *len
= s
->s_authorizer_buf_len
;
3302 *reply_buf
= s
->s_authorizer_reply_buf
;
3303 *reply_len
= s
->s_authorizer_reply_buf_len
;
3308 static int verify_authorizer_reply(struct ceph_connection
*con
, int len
)
3310 struct ceph_mds_session
*s
= con
->private;
3311 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
3312 struct ceph_auth_client
*ac
= mdsc
->client
->monc
.auth
;
3314 return ac
->ops
->verify_authorizer_reply(ac
, s
->s_authorizer
, len
);
3317 static int invalidate_authorizer(struct ceph_connection
*con
)
3319 struct ceph_mds_session
*s
= con
->private;
3320 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
3321 struct ceph_auth_client
*ac
= mdsc
->client
->monc
.auth
;
3323 if (ac
->ops
->invalidate_authorizer
)
3324 ac
->ops
->invalidate_authorizer(ac
, CEPH_ENTITY_TYPE_MDS
);
3326 return ceph_monc_validate_auth(&mdsc
->client
->monc
);
3329 static const struct ceph_connection_operations mds_con_ops
= {
3332 .dispatch
= dispatch
,
3333 .get_authorizer
= get_authorizer
,
3334 .verify_authorizer_reply
= verify_authorizer_reply
,
3335 .invalidate_authorizer
= invalidate_authorizer
,
3336 .peer_reset
= peer_reset
,