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 static int __choose_mds(struct ceph_mds_client
*mdsc
,
564 struct ceph_mds_request
*req
)
567 struct ceph_inode_info
*ci
;
568 struct ceph_cap
*cap
;
569 int mode
= req
->r_direct_mode
;
571 u32 hash
= req
->r_direct_hash
;
572 bool is_hash
= req
->r_direct_is_hash
;
575 * is there a specific mds we should try? ignore hint if we have
576 * no session and the mds is not up (active or recovering).
578 if (req
->r_resend_mds
>= 0 &&
579 (__have_session(mdsc
, req
->r_resend_mds
) ||
580 ceph_mdsmap_get_state(mdsc
->mdsmap
, req
->r_resend_mds
) > 0)) {
581 dout("choose_mds using resend_mds mds%d\n",
583 return req
->r_resend_mds
;
586 if (mode
== USE_RANDOM_MDS
)
591 inode
= req
->r_inode
;
592 } else if (req
->r_dentry
) {
593 if (req
->r_dentry
->d_inode
) {
594 inode
= req
->r_dentry
->d_inode
;
596 inode
= req
->r_dentry
->d_parent
->d_inode
;
597 hash
= req
->r_dentry
->d_name
.hash
;
601 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode
, (int)is_hash
,
605 ci
= ceph_inode(inode
);
607 if (is_hash
&& S_ISDIR(inode
->i_mode
)) {
608 struct ceph_inode_frag frag
;
611 ceph_choose_frag(ci
, hash
, &frag
, &found
);
613 if (mode
== USE_ANY_MDS
&& frag
.ndist
> 0) {
616 /* choose a random replica */
617 get_random_bytes(&r
, 1);
620 dout("choose_mds %p %llx.%llx "
621 "frag %u mds%d (%d/%d)\n",
622 inode
, ceph_vinop(inode
),
628 /* since this file/dir wasn't known to be
629 * replicated, then we want to look for the
630 * authoritative mds. */
633 /* choose auth mds */
635 dout("choose_mds %p %llx.%llx "
636 "frag %u mds%d (auth)\n",
637 inode
, ceph_vinop(inode
), frag
.frag
, mds
);
643 spin_lock(&inode
->i_lock
);
645 if (mode
== USE_AUTH_MDS
)
646 cap
= ci
->i_auth_cap
;
647 if (!cap
&& !RB_EMPTY_ROOT(&ci
->i_caps
))
648 cap
= rb_entry(rb_first(&ci
->i_caps
), struct ceph_cap
, ci_node
);
650 spin_unlock(&inode
->i_lock
);
653 mds
= cap
->session
->s_mds
;
654 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
655 inode
, ceph_vinop(inode
), mds
,
656 cap
== ci
->i_auth_cap
? "auth " : "", cap
);
657 spin_unlock(&inode
->i_lock
);
661 mds
= ceph_mdsmap_get_random_mds(mdsc
->mdsmap
);
662 dout("choose_mds chose random mds%d\n", mds
);
670 static struct ceph_msg
*create_session_msg(u32 op
, u64 seq
)
672 struct ceph_msg
*msg
;
673 struct ceph_mds_session_head
*h
;
675 msg
= ceph_msg_new(CEPH_MSG_CLIENT_SESSION
, sizeof(*h
), GFP_NOFS
);
677 pr_err("create_session_msg ENOMEM creating msg\n");
680 h
= msg
->front
.iov_base
;
681 h
->op
= cpu_to_le32(op
);
682 h
->seq
= cpu_to_le64(seq
);
687 * send session open request.
689 * called under mdsc->mutex
691 static int __open_session(struct ceph_mds_client
*mdsc
,
692 struct ceph_mds_session
*session
)
694 struct ceph_msg
*msg
;
696 int mds
= session
->s_mds
;
698 /* wait for mds to go active? */
699 mstate
= ceph_mdsmap_get_state(mdsc
->mdsmap
, mds
);
700 dout("open_session to mds%d (%s)\n", mds
,
701 ceph_mds_state_name(mstate
));
702 session
->s_state
= CEPH_MDS_SESSION_OPENING
;
703 session
->s_renew_requested
= jiffies
;
705 /* send connect message */
706 msg
= create_session_msg(CEPH_SESSION_REQUEST_OPEN
, session
->s_seq
);
709 ceph_con_send(&session
->s_con
, msg
);
714 * open sessions for any export targets for the given mds
716 * called under mdsc->mutex
718 static void __open_export_target_sessions(struct ceph_mds_client
*mdsc
,
719 struct ceph_mds_session
*session
)
721 struct ceph_mds_info
*mi
;
722 struct ceph_mds_session
*ts
;
723 int i
, mds
= session
->s_mds
;
726 if (mds
>= mdsc
->mdsmap
->m_max_mds
)
728 mi
= &mdsc
->mdsmap
->m_info
[mds
];
729 dout("open_export_target_sessions for mds%d (%d targets)\n",
730 session
->s_mds
, mi
->num_export_targets
);
732 for (i
= 0; i
< mi
->num_export_targets
; i
++) {
733 target
= mi
->export_targets
[i
];
734 ts
= __ceph_lookup_mds_session(mdsc
, target
);
736 ts
= register_session(mdsc
, target
);
740 if (session
->s_state
== CEPH_MDS_SESSION_NEW
||
741 session
->s_state
== CEPH_MDS_SESSION_CLOSING
)
742 __open_session(mdsc
, session
);
744 dout(" mds%d target mds%d %p is %s\n", session
->s_mds
,
745 i
, ts
, session_state_name(ts
->s_state
));
746 ceph_put_mds_session(ts
);
750 void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client
*mdsc
,
751 struct ceph_mds_session
*session
)
753 mutex_lock(&mdsc
->mutex
);
754 __open_export_target_sessions(mdsc
, session
);
755 mutex_unlock(&mdsc
->mutex
);
763 * Free preallocated cap messages assigned to this session
765 static void cleanup_cap_releases(struct ceph_mds_session
*session
)
767 struct ceph_msg
*msg
;
769 spin_lock(&session
->s_cap_lock
);
770 while (!list_empty(&session
->s_cap_releases
)) {
771 msg
= list_first_entry(&session
->s_cap_releases
,
772 struct ceph_msg
, list_head
);
773 list_del_init(&msg
->list_head
);
776 while (!list_empty(&session
->s_cap_releases_done
)) {
777 msg
= list_first_entry(&session
->s_cap_releases_done
,
778 struct ceph_msg
, list_head
);
779 list_del_init(&msg
->list_head
);
782 spin_unlock(&session
->s_cap_lock
);
786 * Helper to safely iterate over all caps associated with a session, with
787 * special care taken to handle a racing __ceph_remove_cap().
789 * Caller must hold session s_mutex.
791 static int iterate_session_caps(struct ceph_mds_session
*session
,
792 int (*cb
)(struct inode
*, struct ceph_cap
*,
796 struct ceph_cap
*cap
;
797 struct inode
*inode
, *last_inode
= NULL
;
798 struct ceph_cap
*old_cap
= NULL
;
801 dout("iterate_session_caps %p mds%d\n", session
, session
->s_mds
);
802 spin_lock(&session
->s_cap_lock
);
803 p
= session
->s_caps
.next
;
804 while (p
!= &session
->s_caps
) {
805 cap
= list_entry(p
, struct ceph_cap
, session_caps
);
806 inode
= igrab(&cap
->ci
->vfs_inode
);
811 session
->s_cap_iterator
= cap
;
812 spin_unlock(&session
->s_cap_lock
);
819 ceph_put_cap(session
->s_mdsc
, old_cap
);
823 ret
= cb(inode
, cap
, arg
);
826 spin_lock(&session
->s_cap_lock
);
828 if (cap
->ci
== NULL
) {
829 dout("iterate_session_caps finishing cap %p removal\n",
831 BUG_ON(cap
->session
!= session
);
832 list_del_init(&cap
->session_caps
);
833 session
->s_nr_caps
--;
835 old_cap
= cap
; /* put_cap it w/o locks held */
842 session
->s_cap_iterator
= NULL
;
843 spin_unlock(&session
->s_cap_lock
);
848 ceph_put_cap(session
->s_mdsc
, old_cap
);
853 static int remove_session_caps_cb(struct inode
*inode
, struct ceph_cap
*cap
,
856 struct ceph_inode_info
*ci
= ceph_inode(inode
);
859 dout("removing cap %p, ci is %p, inode is %p\n",
860 cap
, ci
, &ci
->vfs_inode
);
861 spin_lock(&inode
->i_lock
);
862 __ceph_remove_cap(cap
);
863 if (!__ceph_is_any_real_caps(ci
)) {
864 struct ceph_mds_client
*mdsc
=
865 &ceph_sb_to_client(inode
->i_sb
)->mdsc
;
867 spin_lock(&mdsc
->cap_dirty_lock
);
868 if (!list_empty(&ci
->i_dirty_item
)) {
869 pr_info(" dropping dirty %s state for %p %lld\n",
870 ceph_cap_string(ci
->i_dirty_caps
),
871 inode
, ceph_ino(inode
));
872 ci
->i_dirty_caps
= 0;
873 list_del_init(&ci
->i_dirty_item
);
876 if (!list_empty(&ci
->i_flushing_item
)) {
877 pr_info(" dropping dirty+flushing %s state for %p %lld\n",
878 ceph_cap_string(ci
->i_flushing_caps
),
879 inode
, ceph_ino(inode
));
880 ci
->i_flushing_caps
= 0;
881 list_del_init(&ci
->i_flushing_item
);
882 mdsc
->num_cap_flushing
--;
885 if (drop
&& ci
->i_wrbuffer_ref
) {
886 pr_info(" dropping dirty data for %p %lld\n",
887 inode
, ceph_ino(inode
));
888 ci
->i_wrbuffer_ref
= 0;
889 ci
->i_wrbuffer_ref_head
= 0;
892 spin_unlock(&mdsc
->cap_dirty_lock
);
894 spin_unlock(&inode
->i_lock
);
901 * caller must hold session s_mutex
903 static void remove_session_caps(struct ceph_mds_session
*session
)
905 dout("remove_session_caps on %p\n", session
);
906 iterate_session_caps(session
, remove_session_caps_cb
, NULL
);
907 BUG_ON(session
->s_nr_caps
> 0);
908 BUG_ON(!list_empty(&session
->s_cap_flushing
));
909 cleanup_cap_releases(session
);
913 * wake up any threads waiting on this session's caps. if the cap is
914 * old (didn't get renewed on the client reconnect), remove it now.
916 * caller must hold s_mutex.
918 static int wake_up_session_cb(struct inode
*inode
, struct ceph_cap
*cap
,
921 struct ceph_inode_info
*ci
= ceph_inode(inode
);
923 wake_up_all(&ci
->i_cap_wq
);
925 spin_lock(&inode
->i_lock
);
926 ci
->i_wanted_max_size
= 0;
927 ci
->i_requested_max_size
= 0;
928 spin_unlock(&inode
->i_lock
);
933 static void wake_up_session_caps(struct ceph_mds_session
*session
,
936 dout("wake_up_session_caps %p mds%d\n", session
, session
->s_mds
);
937 iterate_session_caps(session
, wake_up_session_cb
,
938 (void *)(unsigned long)reconnect
);
942 * Send periodic message to MDS renewing all currently held caps. The
943 * ack will reset the expiration for all caps from this session.
945 * caller holds s_mutex
947 static int send_renew_caps(struct ceph_mds_client
*mdsc
,
948 struct ceph_mds_session
*session
)
950 struct ceph_msg
*msg
;
953 if (time_after_eq(jiffies
, session
->s_cap_ttl
) &&
954 time_after_eq(session
->s_cap_ttl
, session
->s_renew_requested
))
955 pr_info("mds%d caps stale\n", session
->s_mds
);
956 session
->s_renew_requested
= jiffies
;
958 /* do not try to renew caps until a recovering mds has reconnected
959 * with its clients. */
960 state
= ceph_mdsmap_get_state(mdsc
->mdsmap
, session
->s_mds
);
961 if (state
< CEPH_MDS_STATE_RECONNECT
) {
962 dout("send_renew_caps ignoring mds%d (%s)\n",
963 session
->s_mds
, ceph_mds_state_name(state
));
967 dout("send_renew_caps to mds%d (%s)\n", session
->s_mds
,
968 ceph_mds_state_name(state
));
969 msg
= create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS
,
970 ++session
->s_renew_seq
);
973 ceph_con_send(&session
->s_con
, msg
);
978 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
980 * Called under session->s_mutex
982 static void renewed_caps(struct ceph_mds_client
*mdsc
,
983 struct ceph_mds_session
*session
, int is_renew
)
988 spin_lock(&session
->s_cap_lock
);
989 was_stale
= is_renew
&& (session
->s_cap_ttl
== 0 ||
990 time_after_eq(jiffies
, session
->s_cap_ttl
));
992 session
->s_cap_ttl
= session
->s_renew_requested
+
993 mdsc
->mdsmap
->m_session_timeout
*HZ
;
996 if (time_before(jiffies
, session
->s_cap_ttl
)) {
997 pr_info("mds%d caps renewed\n", session
->s_mds
);
1000 pr_info("mds%d caps still stale\n", session
->s_mds
);
1003 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1004 session
->s_mds
, session
->s_cap_ttl
, was_stale
? "stale" : "fresh",
1005 time_before(jiffies
, session
->s_cap_ttl
) ? "stale" : "fresh");
1006 spin_unlock(&session
->s_cap_lock
);
1009 wake_up_session_caps(session
, 0);
1013 * send a session close request
1015 static int request_close_session(struct ceph_mds_client
*mdsc
,
1016 struct ceph_mds_session
*session
)
1018 struct ceph_msg
*msg
;
1020 dout("request_close_session mds%d state %s seq %lld\n",
1021 session
->s_mds
, session_state_name(session
->s_state
),
1023 msg
= create_session_msg(CEPH_SESSION_REQUEST_CLOSE
, session
->s_seq
);
1026 ceph_con_send(&session
->s_con
, msg
);
1031 * Called with s_mutex held.
1033 static int __close_session(struct ceph_mds_client
*mdsc
,
1034 struct ceph_mds_session
*session
)
1036 if (session
->s_state
>= CEPH_MDS_SESSION_CLOSING
)
1038 session
->s_state
= CEPH_MDS_SESSION_CLOSING
;
1039 return request_close_session(mdsc
, session
);
1043 * Trim old(er) caps.
1045 * Because we can't cache an inode without one or more caps, we do
1046 * this indirectly: if a cap is unused, we prune its aliases, at which
1047 * point the inode will hopefully get dropped to.
1049 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1050 * memory pressure from the MDS, though, so it needn't be perfect.
1052 static int trim_caps_cb(struct inode
*inode
, struct ceph_cap
*cap
, void *arg
)
1054 struct ceph_mds_session
*session
= arg
;
1055 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1056 int used
, oissued
, mine
;
1058 if (session
->s_trim_caps
<= 0)
1061 spin_lock(&inode
->i_lock
);
1062 mine
= cap
->issued
| cap
->implemented
;
1063 used
= __ceph_caps_used(ci
);
1064 oissued
= __ceph_caps_issued_other(ci
, cap
);
1066 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n",
1067 inode
, cap
, ceph_cap_string(mine
), ceph_cap_string(oissued
),
1068 ceph_cap_string(used
));
1069 if (ci
->i_dirty_caps
)
1070 goto out
; /* dirty caps */
1071 if ((used
& ~oissued
) & mine
)
1072 goto out
; /* we need these caps */
1074 session
->s_trim_caps
--;
1076 /* we aren't the only cap.. just remove us */
1077 __ceph_remove_cap(cap
);
1079 /* try to drop referring dentries */
1080 spin_unlock(&inode
->i_lock
);
1081 d_prune_aliases(inode
);
1082 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1083 inode
, cap
, atomic_read(&inode
->i_count
));
1088 spin_unlock(&inode
->i_lock
);
1093 * Trim session cap count down to some max number.
1095 static int trim_caps(struct ceph_mds_client
*mdsc
,
1096 struct ceph_mds_session
*session
,
1099 int trim_caps
= session
->s_nr_caps
- max_caps
;
1101 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1102 session
->s_mds
, session
->s_nr_caps
, max_caps
, trim_caps
);
1103 if (trim_caps
> 0) {
1104 session
->s_trim_caps
= trim_caps
;
1105 iterate_session_caps(session
, trim_caps_cb
, session
);
1106 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1107 session
->s_mds
, session
->s_nr_caps
, max_caps
,
1108 trim_caps
- session
->s_trim_caps
);
1109 session
->s_trim_caps
= 0;
1115 * Allocate cap_release messages. If there is a partially full message
1116 * in the queue, try to allocate enough to cover it's remainder, so that
1117 * we can send it immediately.
1119 * Called under s_mutex.
1121 int ceph_add_cap_releases(struct ceph_mds_client
*mdsc
,
1122 struct ceph_mds_session
*session
)
1124 struct ceph_msg
*msg
, *partial
= NULL
;
1125 struct ceph_mds_cap_release
*head
;
1127 int extra
= mdsc
->client
->mount_args
->cap_release_safety
;
1130 dout("add_cap_releases %p mds%d extra %d\n", session
, session
->s_mds
,
1133 spin_lock(&session
->s_cap_lock
);
1135 if (!list_empty(&session
->s_cap_releases
)) {
1136 msg
= list_first_entry(&session
->s_cap_releases
,
1139 head
= msg
->front
.iov_base
;
1140 num
= le32_to_cpu(head
->num
);
1142 dout(" partial %p with (%d/%d)\n", msg
, num
,
1143 (int)CEPH_CAPS_PER_RELEASE
);
1144 extra
+= CEPH_CAPS_PER_RELEASE
- num
;
1148 while (session
->s_num_cap_releases
< session
->s_nr_caps
+ extra
) {
1149 spin_unlock(&session
->s_cap_lock
);
1150 msg
= ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE
, PAGE_CACHE_SIZE
,
1154 dout("add_cap_releases %p msg %p now %d\n", session
, msg
,
1155 (int)msg
->front
.iov_len
);
1156 head
= msg
->front
.iov_base
;
1157 head
->num
= cpu_to_le32(0);
1158 msg
->front
.iov_len
= sizeof(*head
);
1159 spin_lock(&session
->s_cap_lock
);
1160 list_add(&msg
->list_head
, &session
->s_cap_releases
);
1161 session
->s_num_cap_releases
+= CEPH_CAPS_PER_RELEASE
;
1165 head
= partial
->front
.iov_base
;
1166 num
= le32_to_cpu(head
->num
);
1167 dout(" queueing partial %p with %d/%d\n", partial
, num
,
1168 (int)CEPH_CAPS_PER_RELEASE
);
1169 list_move_tail(&partial
->list_head
,
1170 &session
->s_cap_releases_done
);
1171 session
->s_num_cap_releases
-= CEPH_CAPS_PER_RELEASE
- num
;
1174 spin_unlock(&session
->s_cap_lock
);
1180 * flush all dirty inode data to disk.
1182 * returns true if we've flushed through want_flush_seq
1184 static int check_cap_flush(struct ceph_mds_client
*mdsc
, u64 want_flush_seq
)
1188 dout("check_cap_flush want %lld\n", want_flush_seq
);
1189 mutex_lock(&mdsc
->mutex
);
1190 for (mds
= 0; ret
&& mds
< mdsc
->max_sessions
; mds
++) {
1191 struct ceph_mds_session
*session
= mdsc
->sessions
[mds
];
1195 get_session(session
);
1196 mutex_unlock(&mdsc
->mutex
);
1198 mutex_lock(&session
->s_mutex
);
1199 if (!list_empty(&session
->s_cap_flushing
)) {
1200 struct ceph_inode_info
*ci
=
1201 list_entry(session
->s_cap_flushing
.next
,
1202 struct ceph_inode_info
,
1204 struct inode
*inode
= &ci
->vfs_inode
;
1206 spin_lock(&inode
->i_lock
);
1207 if (ci
->i_cap_flush_seq
<= want_flush_seq
) {
1208 dout("check_cap_flush still flushing %p "
1209 "seq %lld <= %lld to mds%d\n", inode
,
1210 ci
->i_cap_flush_seq
, want_flush_seq
,
1214 spin_unlock(&inode
->i_lock
);
1216 mutex_unlock(&session
->s_mutex
);
1217 ceph_put_mds_session(session
);
1221 mutex_lock(&mdsc
->mutex
);
1224 mutex_unlock(&mdsc
->mutex
);
1225 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq
);
1230 * called under s_mutex
1232 void ceph_send_cap_releases(struct ceph_mds_client
*mdsc
,
1233 struct ceph_mds_session
*session
)
1235 struct ceph_msg
*msg
;
1237 dout("send_cap_releases mds%d\n", session
->s_mds
);
1238 spin_lock(&session
->s_cap_lock
);
1239 while (!list_empty(&session
->s_cap_releases_done
)) {
1240 msg
= list_first_entry(&session
->s_cap_releases_done
,
1241 struct ceph_msg
, list_head
);
1242 list_del_init(&msg
->list_head
);
1243 spin_unlock(&session
->s_cap_lock
);
1244 msg
->hdr
.front_len
= cpu_to_le32(msg
->front
.iov_len
);
1245 dout("send_cap_releases mds%d %p\n", session
->s_mds
, msg
);
1246 ceph_con_send(&session
->s_con
, msg
);
1247 spin_lock(&session
->s_cap_lock
);
1249 spin_unlock(&session
->s_cap_lock
);
1252 static void discard_cap_releases(struct ceph_mds_client
*mdsc
,
1253 struct ceph_mds_session
*session
)
1255 struct ceph_msg
*msg
;
1256 struct ceph_mds_cap_release
*head
;
1259 dout("discard_cap_releases mds%d\n", session
->s_mds
);
1260 spin_lock(&session
->s_cap_lock
);
1262 /* zero out the in-progress message */
1263 msg
= list_first_entry(&session
->s_cap_releases
,
1264 struct ceph_msg
, list_head
);
1265 head
= msg
->front
.iov_base
;
1266 num
= le32_to_cpu(head
->num
);
1267 dout("discard_cap_releases mds%d %p %u\n", session
->s_mds
, msg
, num
);
1268 head
->num
= cpu_to_le32(0);
1269 session
->s_num_cap_releases
+= num
;
1271 /* requeue completed messages */
1272 while (!list_empty(&session
->s_cap_releases_done
)) {
1273 msg
= list_first_entry(&session
->s_cap_releases_done
,
1274 struct ceph_msg
, list_head
);
1275 list_del_init(&msg
->list_head
);
1277 head
= msg
->front
.iov_base
;
1278 num
= le32_to_cpu(head
->num
);
1279 dout("discard_cap_releases mds%d %p %u\n", session
->s_mds
, msg
,
1281 session
->s_num_cap_releases
+= num
;
1282 head
->num
= cpu_to_le32(0);
1283 msg
->front
.iov_len
= sizeof(*head
);
1284 list_add(&msg
->list_head
, &session
->s_cap_releases
);
1287 spin_unlock(&session
->s_cap_lock
);
1295 * Create an mds request.
1297 struct ceph_mds_request
*
1298 ceph_mdsc_create_request(struct ceph_mds_client
*mdsc
, int op
, int mode
)
1300 struct ceph_mds_request
*req
= kzalloc(sizeof(*req
), GFP_NOFS
);
1303 return ERR_PTR(-ENOMEM
);
1305 mutex_init(&req
->r_fill_mutex
);
1307 req
->r_started
= jiffies
;
1308 req
->r_resend_mds
= -1;
1309 INIT_LIST_HEAD(&req
->r_unsafe_dir_item
);
1311 kref_init(&req
->r_kref
);
1312 INIT_LIST_HEAD(&req
->r_wait
);
1313 init_completion(&req
->r_completion
);
1314 init_completion(&req
->r_safe_completion
);
1315 INIT_LIST_HEAD(&req
->r_unsafe_item
);
1318 req
->r_direct_mode
= mode
;
1323 * return oldest (lowest) request, tid in request tree, 0 if none.
1325 * called under mdsc->mutex.
1327 static struct ceph_mds_request
*__get_oldest_req(struct ceph_mds_client
*mdsc
)
1329 if (RB_EMPTY_ROOT(&mdsc
->request_tree
))
1331 return rb_entry(rb_first(&mdsc
->request_tree
),
1332 struct ceph_mds_request
, r_node
);
1335 static u64
__get_oldest_tid(struct ceph_mds_client
*mdsc
)
1337 struct ceph_mds_request
*req
= __get_oldest_req(mdsc
);
1345 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1346 * on build_path_from_dentry in fs/cifs/dir.c.
1348 * If @stop_on_nosnap, generate path relative to the first non-snapped
1351 * Encode hidden .snap dirs as a double /, i.e.
1352 * foo/.snap/bar -> foo//bar
1354 char *ceph_mdsc_build_path(struct dentry
*dentry
, int *plen
, u64
*base
,
1357 struct dentry
*temp
;
1362 return ERR_PTR(-EINVAL
);
1366 for (temp
= dentry
; !IS_ROOT(temp
);) {
1367 struct inode
*inode
= temp
->d_inode
;
1368 if (inode
&& ceph_snap(inode
) == CEPH_SNAPDIR
)
1369 len
++; /* slash only */
1370 else if (stop_on_nosnap
&& inode
&&
1371 ceph_snap(inode
) == CEPH_NOSNAP
)
1374 len
+= 1 + temp
->d_name
.len
;
1375 temp
= temp
->d_parent
;
1377 pr_err("build_path corrupt dentry %p\n", dentry
);
1378 return ERR_PTR(-EINVAL
);
1382 len
--; /* no leading '/' */
1384 path
= kmalloc(len
+1, GFP_NOFS
);
1386 return ERR_PTR(-ENOMEM
);
1388 path
[pos
] = 0; /* trailing null */
1389 for (temp
= dentry
; !IS_ROOT(temp
) && pos
!= 0; ) {
1390 struct inode
*inode
= temp
->d_inode
;
1392 if (inode
&& ceph_snap(inode
) == CEPH_SNAPDIR
) {
1393 dout("build_path path+%d: %p SNAPDIR\n",
1395 } else if (stop_on_nosnap
&& inode
&&
1396 ceph_snap(inode
) == CEPH_NOSNAP
) {
1399 pos
-= temp
->d_name
.len
;
1402 strncpy(path
+ pos
, temp
->d_name
.name
,
1407 temp
= temp
->d_parent
;
1409 pr_err("build_path corrupt dentry\n");
1411 return ERR_PTR(-EINVAL
);
1415 pr_err("build_path did not end path lookup where "
1416 "expected, namelen is %d, pos is %d\n", len
, pos
);
1417 /* presumably this is only possible if racing with a
1418 rename of one of the parent directories (we can not
1419 lock the dentries above us to prevent this, but
1420 retrying should be harmless) */
1425 *base
= ceph_ino(temp
->d_inode
);
1427 dout("build_path on %p %d built %llx '%.*s'\n",
1428 dentry
, atomic_read(&dentry
->d_count
), *base
, len
, path
);
1432 static int build_dentry_path(struct dentry
*dentry
,
1433 const char **ppath
, int *ppathlen
, u64
*pino
,
1438 if (ceph_snap(dentry
->d_parent
->d_inode
) == CEPH_NOSNAP
) {
1439 *pino
= ceph_ino(dentry
->d_parent
->d_inode
);
1440 *ppath
= dentry
->d_name
.name
;
1441 *ppathlen
= dentry
->d_name
.len
;
1444 path
= ceph_mdsc_build_path(dentry
, ppathlen
, pino
, 1);
1446 return PTR_ERR(path
);
1452 static int build_inode_path(struct inode
*inode
,
1453 const char **ppath
, int *ppathlen
, u64
*pino
,
1456 struct dentry
*dentry
;
1459 if (ceph_snap(inode
) == CEPH_NOSNAP
) {
1460 *pino
= ceph_ino(inode
);
1464 dentry
= d_find_alias(inode
);
1465 path
= ceph_mdsc_build_path(dentry
, ppathlen
, pino
, 1);
1468 return PTR_ERR(path
);
1475 * request arguments may be specified via an inode *, a dentry *, or
1476 * an explicit ino+path.
1478 static int set_request_path_attr(struct inode
*rinode
, struct dentry
*rdentry
,
1479 const char *rpath
, u64 rino
,
1480 const char **ppath
, int *pathlen
,
1481 u64
*ino
, int *freepath
)
1486 r
= build_inode_path(rinode
, ppath
, pathlen
, ino
, freepath
);
1487 dout(" inode %p %llx.%llx\n", rinode
, ceph_ino(rinode
),
1489 } else if (rdentry
) {
1490 r
= build_dentry_path(rdentry
, ppath
, pathlen
, ino
, freepath
);
1491 dout(" dentry %p %llx/%.*s\n", rdentry
, *ino
, *pathlen
,
1496 *pathlen
= strlen(rpath
);
1497 dout(" path %.*s\n", *pathlen
, rpath
);
1504 * called under mdsc->mutex
1506 static struct ceph_msg
*create_request_message(struct ceph_mds_client
*mdsc
,
1507 struct ceph_mds_request
*req
,
1510 struct ceph_msg
*msg
;
1511 struct ceph_mds_request_head
*head
;
1512 const char *path1
= NULL
;
1513 const char *path2
= NULL
;
1514 u64 ino1
= 0, ino2
= 0;
1515 int pathlen1
= 0, pathlen2
= 0;
1516 int freepath1
= 0, freepath2
= 0;
1522 ret
= set_request_path_attr(req
->r_inode
, req
->r_dentry
,
1523 req
->r_path1
, req
->r_ino1
.ino
,
1524 &path1
, &pathlen1
, &ino1
, &freepath1
);
1530 ret
= set_request_path_attr(NULL
, req
->r_old_dentry
,
1531 req
->r_path2
, req
->r_ino2
.ino
,
1532 &path2
, &pathlen2
, &ino2
, &freepath2
);
1538 len
= sizeof(*head
) +
1539 pathlen1
+ pathlen2
+ 2*(1 + sizeof(u32
) + sizeof(u64
));
1541 /* calculate (max) length for cap releases */
1542 len
+= sizeof(struct ceph_mds_request_release
) *
1543 (!!req
->r_inode_drop
+ !!req
->r_dentry_drop
+
1544 !!req
->r_old_inode_drop
+ !!req
->r_old_dentry_drop
);
1545 if (req
->r_dentry_drop
)
1546 len
+= req
->r_dentry
->d_name
.len
;
1547 if (req
->r_old_dentry_drop
)
1548 len
+= req
->r_old_dentry
->d_name
.len
;
1550 msg
= ceph_msg_new(CEPH_MSG_CLIENT_REQUEST
, len
, GFP_NOFS
);
1552 msg
= ERR_PTR(-ENOMEM
);
1556 msg
->hdr
.tid
= cpu_to_le64(req
->r_tid
);
1558 head
= msg
->front
.iov_base
;
1559 p
= msg
->front
.iov_base
+ sizeof(*head
);
1560 end
= msg
->front
.iov_base
+ msg
->front
.iov_len
;
1562 head
->mdsmap_epoch
= cpu_to_le32(mdsc
->mdsmap
->m_epoch
);
1563 head
->op
= cpu_to_le32(req
->r_op
);
1564 head
->caller_uid
= cpu_to_le32(current_fsuid());
1565 head
->caller_gid
= cpu_to_le32(current_fsgid());
1566 head
->args
= req
->r_args
;
1568 ceph_encode_filepath(&p
, end
, ino1
, path1
);
1569 ceph_encode_filepath(&p
, end
, ino2
, path2
);
1571 /* make note of release offset, in case we need to replay */
1572 req
->r_request_release_offset
= p
- msg
->front
.iov_base
;
1576 if (req
->r_inode_drop
)
1577 releases
+= ceph_encode_inode_release(&p
,
1578 req
->r_inode
? req
->r_inode
: req
->r_dentry
->d_inode
,
1579 mds
, req
->r_inode_drop
, req
->r_inode_unless
, 0);
1580 if (req
->r_dentry_drop
)
1581 releases
+= ceph_encode_dentry_release(&p
, req
->r_dentry
,
1582 mds
, req
->r_dentry_drop
, req
->r_dentry_unless
);
1583 if (req
->r_old_dentry_drop
)
1584 releases
+= ceph_encode_dentry_release(&p
, req
->r_old_dentry
,
1585 mds
, req
->r_old_dentry_drop
, req
->r_old_dentry_unless
);
1586 if (req
->r_old_inode_drop
)
1587 releases
+= ceph_encode_inode_release(&p
,
1588 req
->r_old_dentry
->d_inode
,
1589 mds
, req
->r_old_inode_drop
, req
->r_old_inode_unless
, 0);
1590 head
->num_releases
= cpu_to_le16(releases
);
1593 msg
->front
.iov_len
= p
- msg
->front
.iov_base
;
1594 msg
->hdr
.front_len
= cpu_to_le32(msg
->front
.iov_len
);
1596 msg
->pages
= req
->r_pages
;
1597 msg
->nr_pages
= req
->r_num_pages
;
1598 msg
->hdr
.data_len
= cpu_to_le32(req
->r_data_len
);
1599 msg
->hdr
.data_off
= cpu_to_le16(0);
1603 kfree((char *)path2
);
1606 kfree((char *)path1
);
1612 * called under mdsc->mutex if error, under no mutex if
1615 static void complete_request(struct ceph_mds_client
*mdsc
,
1616 struct ceph_mds_request
*req
)
1618 if (req
->r_callback
)
1619 req
->r_callback(mdsc
, req
);
1621 complete_all(&req
->r_completion
);
1625 * called under mdsc->mutex
1627 static int __prepare_send_request(struct ceph_mds_client
*mdsc
,
1628 struct ceph_mds_request
*req
,
1631 struct ceph_mds_request_head
*rhead
;
1632 struct ceph_msg
*msg
;
1638 struct ceph_cap
*cap
=
1639 ceph_get_cap_for_mds(ceph_inode(req
->r_inode
), mds
);
1642 req
->r_sent_on_mseq
= cap
->mseq
;
1644 req
->r_sent_on_mseq
= -1;
1646 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req
,
1647 req
->r_tid
, ceph_mds_op_name(req
->r_op
), req
->r_attempts
);
1649 if (req
->r_got_unsafe
) {
1651 * Replay. Do not regenerate message (and rebuild
1652 * paths, etc.); just use the original message.
1653 * Rebuilding paths will break for renames because
1654 * d_move mangles the src name.
1656 msg
= req
->r_request
;
1657 rhead
= msg
->front
.iov_base
;
1659 flags
= le32_to_cpu(rhead
->flags
);
1660 flags
|= CEPH_MDS_FLAG_REPLAY
;
1661 rhead
->flags
= cpu_to_le32(flags
);
1663 if (req
->r_target_inode
)
1664 rhead
->ino
= cpu_to_le64(ceph_ino(req
->r_target_inode
));
1666 rhead
->num_retry
= req
->r_attempts
- 1;
1668 /* remove cap/dentry releases from message */
1669 rhead
->num_releases
= 0;
1670 msg
->hdr
.front_len
= cpu_to_le32(req
->r_request_release_offset
);
1671 msg
->front
.iov_len
= req
->r_request_release_offset
;
1675 if (req
->r_request
) {
1676 ceph_msg_put(req
->r_request
);
1677 req
->r_request
= NULL
;
1679 msg
= create_request_message(mdsc
, req
, mds
);
1681 req
->r_err
= PTR_ERR(msg
);
1682 complete_request(mdsc
, req
);
1683 return PTR_ERR(msg
);
1685 req
->r_request
= msg
;
1687 rhead
= msg
->front
.iov_base
;
1688 rhead
->oldest_client_tid
= cpu_to_le64(__get_oldest_tid(mdsc
));
1689 if (req
->r_got_unsafe
)
1690 flags
|= CEPH_MDS_FLAG_REPLAY
;
1691 if (req
->r_locked_dir
)
1692 flags
|= CEPH_MDS_FLAG_WANT_DENTRY
;
1693 rhead
->flags
= cpu_to_le32(flags
);
1694 rhead
->num_fwd
= req
->r_num_fwd
;
1695 rhead
->num_retry
= req
->r_attempts
- 1;
1698 dout(" r_locked_dir = %p\n", req
->r_locked_dir
);
1703 * send request, or put it on the appropriate wait list.
1705 static int __do_request(struct ceph_mds_client
*mdsc
,
1706 struct ceph_mds_request
*req
)
1708 struct ceph_mds_session
*session
= NULL
;
1712 if (req
->r_err
|| req
->r_got_result
)
1715 if (req
->r_timeout
&&
1716 time_after_eq(jiffies
, req
->r_started
+ req
->r_timeout
)) {
1717 dout("do_request timed out\n");
1722 mds
= __choose_mds(mdsc
, req
);
1724 ceph_mdsmap_get_state(mdsc
->mdsmap
, mds
) < CEPH_MDS_STATE_ACTIVE
) {
1725 dout("do_request no mds or not active, waiting for map\n");
1726 list_add(&req
->r_wait
, &mdsc
->waiting_for_map
);
1730 /* get, open session */
1731 session
= __ceph_lookup_mds_session(mdsc
, mds
);
1733 session
= register_session(mdsc
, mds
);
1734 if (IS_ERR(session
)) {
1735 err
= PTR_ERR(session
);
1739 dout("do_request mds%d session %p state %s\n", mds
, session
,
1740 session_state_name(session
->s_state
));
1741 if (session
->s_state
!= CEPH_MDS_SESSION_OPEN
&&
1742 session
->s_state
!= CEPH_MDS_SESSION_HUNG
) {
1743 if (session
->s_state
== CEPH_MDS_SESSION_NEW
||
1744 session
->s_state
== CEPH_MDS_SESSION_CLOSING
)
1745 __open_session(mdsc
, session
);
1746 list_add(&req
->r_wait
, &session
->s_waiting
);
1751 req
->r_session
= get_session(session
);
1752 req
->r_resend_mds
= -1; /* forget any previous mds hint */
1754 if (req
->r_request_started
== 0) /* note request start time */
1755 req
->r_request_started
= jiffies
;
1757 err
= __prepare_send_request(mdsc
, req
, mds
);
1759 ceph_msg_get(req
->r_request
);
1760 ceph_con_send(&session
->s_con
, req
->r_request
);
1764 ceph_put_mds_session(session
);
1770 complete_request(mdsc
, req
);
1775 * called under mdsc->mutex
1777 static void __wake_requests(struct ceph_mds_client
*mdsc
,
1778 struct list_head
*head
)
1780 struct ceph_mds_request
*req
, *nreq
;
1782 list_for_each_entry_safe(req
, nreq
, head
, r_wait
) {
1783 list_del_init(&req
->r_wait
);
1784 __do_request(mdsc
, req
);
1789 * Wake up threads with requests pending for @mds, so that they can
1790 * resubmit their requests to a possibly different mds.
1792 static void kick_requests(struct ceph_mds_client
*mdsc
, int mds
)
1794 struct ceph_mds_request
*req
;
1797 dout("kick_requests mds%d\n", mds
);
1798 for (p
= rb_first(&mdsc
->request_tree
); p
; p
= rb_next(p
)) {
1799 req
= rb_entry(p
, struct ceph_mds_request
, r_node
);
1800 if (req
->r_got_unsafe
)
1802 if (req
->r_session
&&
1803 req
->r_session
->s_mds
== mds
) {
1804 dout(" kicking tid %llu\n", req
->r_tid
);
1805 put_request_session(req
);
1806 __do_request(mdsc
, req
);
1811 void ceph_mdsc_submit_request(struct ceph_mds_client
*mdsc
,
1812 struct ceph_mds_request
*req
)
1814 dout("submit_request on %p\n", req
);
1815 mutex_lock(&mdsc
->mutex
);
1816 __register_request(mdsc
, req
, NULL
);
1817 __do_request(mdsc
, req
);
1818 mutex_unlock(&mdsc
->mutex
);
1822 * Synchrously perform an mds request. Take care of all of the
1823 * session setup, forwarding, retry details.
1825 int ceph_mdsc_do_request(struct ceph_mds_client
*mdsc
,
1827 struct ceph_mds_request
*req
)
1831 dout("do_request on %p\n", req
);
1833 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
1835 ceph_get_cap_refs(ceph_inode(req
->r_inode
), CEPH_CAP_PIN
);
1836 if (req
->r_locked_dir
)
1837 ceph_get_cap_refs(ceph_inode(req
->r_locked_dir
), CEPH_CAP_PIN
);
1838 if (req
->r_old_dentry
)
1840 ceph_inode(req
->r_old_dentry
->d_parent
->d_inode
),
1844 mutex_lock(&mdsc
->mutex
);
1845 __register_request(mdsc
, req
, dir
);
1846 __do_request(mdsc
, req
);
1850 __unregister_request(mdsc
, req
);
1851 dout("do_request early error %d\n", err
);
1856 mutex_unlock(&mdsc
->mutex
);
1857 dout("do_request waiting\n");
1858 if (req
->r_timeout
) {
1859 err
= (long)wait_for_completion_killable_timeout(
1860 &req
->r_completion
, req
->r_timeout
);
1864 err
= wait_for_completion_killable(&req
->r_completion
);
1866 dout("do_request waited, got %d\n", err
);
1867 mutex_lock(&mdsc
->mutex
);
1869 /* only abort if we didn't race with a real reply */
1870 if (req
->r_got_result
) {
1871 err
= le32_to_cpu(req
->r_reply_info
.head
->result
);
1872 } else if (err
< 0) {
1873 dout("aborted request %lld with %d\n", req
->r_tid
, err
);
1876 * ensure we aren't running concurrently with
1877 * ceph_fill_trace or ceph_readdir_prepopulate, which
1878 * rely on locks (dir mutex) held by our caller.
1880 mutex_lock(&req
->r_fill_mutex
);
1882 req
->r_aborted
= true;
1883 mutex_unlock(&req
->r_fill_mutex
);
1885 if (req
->r_locked_dir
&&
1886 (req
->r_op
& CEPH_MDS_OP_WRITE
))
1887 ceph_invalidate_dir_request(req
);
1893 mutex_unlock(&mdsc
->mutex
);
1894 dout("do_request %p done, result %d\n", req
, err
);
1899 * Invalidate dir I_COMPLETE, dentry lease state on an aborted MDS
1900 * namespace request.
1902 void ceph_invalidate_dir_request(struct ceph_mds_request
*req
)
1904 struct inode
*inode
= req
->r_locked_dir
;
1905 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1907 dout("invalidate_dir_request %p (I_COMPLETE, lease(s))\n", inode
);
1908 spin_lock(&inode
->i_lock
);
1909 ci
->i_ceph_flags
&= ~CEPH_I_COMPLETE
;
1910 ci
->i_release_count
++;
1911 spin_unlock(&inode
->i_lock
);
1914 ceph_invalidate_dentry_lease(req
->r_dentry
);
1915 if (req
->r_old_dentry
)
1916 ceph_invalidate_dentry_lease(req
->r_old_dentry
);
1922 * We take the session mutex and parse and process the reply immediately.
1923 * This preserves the logical ordering of replies, capabilities, etc., sent
1924 * by the MDS as they are applied to our local cache.
1926 static void handle_reply(struct ceph_mds_session
*session
, struct ceph_msg
*msg
)
1928 struct ceph_mds_client
*mdsc
= session
->s_mdsc
;
1929 struct ceph_mds_request
*req
;
1930 struct ceph_mds_reply_head
*head
= msg
->front
.iov_base
;
1931 struct ceph_mds_reply_info_parsed
*rinfo
; /* parsed reply info */
1934 int mds
= session
->s_mds
;
1936 if (msg
->front
.iov_len
< sizeof(*head
)) {
1937 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
1942 /* get request, session */
1943 tid
= le64_to_cpu(msg
->hdr
.tid
);
1944 mutex_lock(&mdsc
->mutex
);
1945 req
= __lookup_request(mdsc
, tid
);
1947 dout("handle_reply on unknown tid %llu\n", tid
);
1948 mutex_unlock(&mdsc
->mutex
);
1951 dout("handle_reply %p\n", req
);
1953 /* correct session? */
1954 if (req
->r_session
!= session
) {
1955 pr_err("mdsc_handle_reply got %llu on session mds%d"
1956 " not mds%d\n", tid
, session
->s_mds
,
1957 req
->r_session
? req
->r_session
->s_mds
: -1);
1958 mutex_unlock(&mdsc
->mutex
);
1963 if ((req
->r_got_unsafe
&& !head
->safe
) ||
1964 (req
->r_got_safe
&& head
->safe
)) {
1965 pr_warning("got a dup %s reply on %llu from mds%d\n",
1966 head
->safe
? "safe" : "unsafe", tid
, mds
);
1967 mutex_unlock(&mdsc
->mutex
);
1970 if (req
->r_got_safe
&& !head
->safe
) {
1971 pr_warning("got unsafe after safe on %llu from mds%d\n",
1973 mutex_unlock(&mdsc
->mutex
);
1977 result
= le32_to_cpu(head
->result
);
1981 * if we're not talking to the authority, send to them
1982 * if the authority has changed while we weren't looking,
1983 * send to new authority
1984 * Otherwise we just have to return an ESTALE
1986 if (result
== -ESTALE
) {
1987 dout("got ESTALE on request %llu", req
->r_tid
);
1988 if (!req
->r_inode
) ; //do nothing; not an authority problem
1989 else if (req
->r_direct_mode
!= USE_AUTH_MDS
) {
1990 dout("not using auth, setting for that now");
1991 req
->r_direct_mode
= USE_AUTH_MDS
;
1992 __do_request(mdsc
, req
);
1993 mutex_unlock(&mdsc
->mutex
);
1996 struct ceph_inode_info
*ci
= ceph_inode(req
->r_inode
);
1997 struct ceph_cap
*cap
=
1998 ceph_get_cap_for_mds(ci
, req
->r_mds
);;
2000 dout("already using auth");
2001 if ((!cap
|| cap
!= ci
->i_auth_cap
) ||
2002 (cap
->mseq
!= req
->r_sent_on_mseq
)) {
2003 dout("but cap changed, so resending");
2004 __do_request(mdsc
, req
);
2005 mutex_unlock(&mdsc
->mutex
);
2009 dout("have to return ESTALE on request %llu", req
->r_tid
);
2014 req
->r_got_safe
= true;
2015 __unregister_request(mdsc
, req
);
2016 complete_all(&req
->r_safe_completion
);
2018 if (req
->r_got_unsafe
) {
2020 * We already handled the unsafe response, now do the
2021 * cleanup. No need to examine the response; the MDS
2022 * doesn't include any result info in the safe
2023 * response. And even if it did, there is nothing
2024 * useful we could do with a revised return value.
2026 dout("got safe reply %llu, mds%d\n", tid
, mds
);
2027 list_del_init(&req
->r_unsafe_item
);
2029 /* last unsafe request during umount? */
2030 if (mdsc
->stopping
&& !__get_oldest_req(mdsc
))
2031 complete_all(&mdsc
->safe_umount_waiters
);
2032 mutex_unlock(&mdsc
->mutex
);
2036 req
->r_got_unsafe
= true;
2037 list_add_tail(&req
->r_unsafe_item
, &req
->r_session
->s_unsafe
);
2040 dout("handle_reply tid %lld result %d\n", tid
, result
);
2041 rinfo
= &req
->r_reply_info
;
2042 err
= parse_reply_info(msg
, rinfo
);
2043 mutex_unlock(&mdsc
->mutex
);
2045 mutex_lock(&session
->s_mutex
);
2047 pr_err("mdsc_handle_reply got corrupt reply mds%d\n", mds
);
2053 if (rinfo
->snapblob_len
) {
2054 down_write(&mdsc
->snap_rwsem
);
2055 ceph_update_snap_trace(mdsc
, rinfo
->snapblob
,
2056 rinfo
->snapblob
+ rinfo
->snapblob_len
,
2057 le32_to_cpu(head
->op
) == CEPH_MDS_OP_RMSNAP
);
2058 downgrade_write(&mdsc
->snap_rwsem
);
2060 down_read(&mdsc
->snap_rwsem
);
2063 /* insert trace into our cache */
2064 mutex_lock(&req
->r_fill_mutex
);
2065 err
= ceph_fill_trace(mdsc
->client
->sb
, req
, req
->r_session
);
2067 if (result
== 0 && rinfo
->dir_nr
)
2068 ceph_readdir_prepopulate(req
, req
->r_session
);
2069 ceph_unreserve_caps(mdsc
, &req
->r_caps_reservation
);
2071 mutex_unlock(&req
->r_fill_mutex
);
2073 up_read(&mdsc
->snap_rwsem
);
2075 mutex_lock(&mdsc
->mutex
);
2076 if (!req
->r_aborted
) {
2082 req
->r_got_result
= true;
2085 dout("reply arrived after request %lld was aborted\n", tid
);
2087 mutex_unlock(&mdsc
->mutex
);
2089 ceph_add_cap_releases(mdsc
, req
->r_session
);
2090 mutex_unlock(&session
->s_mutex
);
2092 /* kick calling process */
2093 complete_request(mdsc
, req
);
2095 ceph_mdsc_put_request(req
);
2102 * handle mds notification that our request has been forwarded.
2104 static void handle_forward(struct ceph_mds_client
*mdsc
,
2105 struct ceph_mds_session
*session
,
2106 struct ceph_msg
*msg
)
2108 struct ceph_mds_request
*req
;
2109 u64 tid
= le64_to_cpu(msg
->hdr
.tid
);
2113 void *p
= msg
->front
.iov_base
;
2114 void *end
= p
+ msg
->front
.iov_len
;
2116 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
2117 next_mds
= ceph_decode_32(&p
);
2118 fwd_seq
= ceph_decode_32(&p
);
2120 mutex_lock(&mdsc
->mutex
);
2121 req
= __lookup_request(mdsc
, tid
);
2123 dout("forward tid %llu to mds%d - req dne\n", tid
, next_mds
);
2124 goto out
; /* dup reply? */
2127 if (req
->r_aborted
) {
2128 dout("forward tid %llu aborted, unregistering\n", tid
);
2129 __unregister_request(mdsc
, req
);
2130 } else if (fwd_seq
<= req
->r_num_fwd
) {
2131 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
2132 tid
, next_mds
, req
->r_num_fwd
, fwd_seq
);
2134 /* resend. forward race not possible; mds would drop */
2135 dout("forward tid %llu to mds%d (we resend)\n", tid
, next_mds
);
2137 BUG_ON(req
->r_got_result
);
2138 req
->r_num_fwd
= fwd_seq
;
2139 req
->r_resend_mds
= next_mds
;
2140 put_request_session(req
);
2141 __do_request(mdsc
, req
);
2143 ceph_mdsc_put_request(req
);
2145 mutex_unlock(&mdsc
->mutex
);
2149 pr_err("mdsc_handle_forward decode error err=%d\n", err
);
2153 * handle a mds session control message
2155 static void handle_session(struct ceph_mds_session
*session
,
2156 struct ceph_msg
*msg
)
2158 struct ceph_mds_client
*mdsc
= session
->s_mdsc
;
2161 int mds
= session
->s_mds
;
2162 struct ceph_mds_session_head
*h
= msg
->front
.iov_base
;
2166 if (msg
->front
.iov_len
!= sizeof(*h
))
2168 op
= le32_to_cpu(h
->op
);
2169 seq
= le64_to_cpu(h
->seq
);
2171 mutex_lock(&mdsc
->mutex
);
2172 if (op
== CEPH_SESSION_CLOSE
)
2173 __unregister_session(mdsc
, session
);
2174 /* FIXME: this ttl calculation is generous */
2175 session
->s_ttl
= jiffies
+ HZ
*mdsc
->mdsmap
->m_session_autoclose
;
2176 mutex_unlock(&mdsc
->mutex
);
2178 mutex_lock(&session
->s_mutex
);
2180 dout("handle_session mds%d %s %p state %s seq %llu\n",
2181 mds
, ceph_session_op_name(op
), session
,
2182 session_state_name(session
->s_state
), seq
);
2184 if (session
->s_state
== CEPH_MDS_SESSION_HUNG
) {
2185 session
->s_state
= CEPH_MDS_SESSION_OPEN
;
2186 pr_info("mds%d came back\n", session
->s_mds
);
2190 case CEPH_SESSION_OPEN
:
2191 if (session
->s_state
== CEPH_MDS_SESSION_RECONNECTING
)
2192 pr_info("mds%d reconnect success\n", session
->s_mds
);
2193 session
->s_state
= CEPH_MDS_SESSION_OPEN
;
2194 renewed_caps(mdsc
, session
, 0);
2197 __close_session(mdsc
, session
);
2200 case CEPH_SESSION_RENEWCAPS
:
2201 if (session
->s_renew_seq
== seq
)
2202 renewed_caps(mdsc
, session
, 1);
2205 case CEPH_SESSION_CLOSE
:
2206 if (session
->s_state
== CEPH_MDS_SESSION_RECONNECTING
)
2207 pr_info("mds%d reconnect denied\n", session
->s_mds
);
2208 remove_session_caps(session
);
2209 wake
= 1; /* for good measure */
2210 complete_all(&mdsc
->session_close_waiters
);
2211 kick_requests(mdsc
, mds
);
2214 case CEPH_SESSION_STALE
:
2215 pr_info("mds%d caps went stale, renewing\n",
2217 spin_lock(&session
->s_cap_lock
);
2218 session
->s_cap_gen
++;
2219 session
->s_cap_ttl
= 0;
2220 spin_unlock(&session
->s_cap_lock
);
2221 send_renew_caps(mdsc
, session
);
2224 case CEPH_SESSION_RECALL_STATE
:
2225 trim_caps(mdsc
, session
, le32_to_cpu(h
->max_caps
));
2229 pr_err("mdsc_handle_session bad op %d mds%d\n", op
, mds
);
2233 mutex_unlock(&session
->s_mutex
);
2235 mutex_lock(&mdsc
->mutex
);
2236 __wake_requests(mdsc
, &session
->s_waiting
);
2237 mutex_unlock(&mdsc
->mutex
);
2242 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds
,
2243 (int)msg
->front
.iov_len
);
2250 * called under session->mutex.
2252 static void replay_unsafe_requests(struct ceph_mds_client
*mdsc
,
2253 struct ceph_mds_session
*session
)
2255 struct ceph_mds_request
*req
, *nreq
;
2258 dout("replay_unsafe_requests mds%d\n", session
->s_mds
);
2260 mutex_lock(&mdsc
->mutex
);
2261 list_for_each_entry_safe(req
, nreq
, &session
->s_unsafe
, r_unsafe_item
) {
2262 err
= __prepare_send_request(mdsc
, req
, session
->s_mds
);
2264 ceph_msg_get(req
->r_request
);
2265 ceph_con_send(&session
->s_con
, req
->r_request
);
2268 mutex_unlock(&mdsc
->mutex
);
2272 * Encode information about a cap for a reconnect with the MDS.
2274 static int encode_caps_cb(struct inode
*inode
, struct ceph_cap
*cap
,
2278 struct ceph_mds_cap_reconnect v2
;
2279 struct ceph_mds_cap_reconnect_v1 v1
;
2282 struct ceph_inode_info
*ci
;
2283 struct ceph_reconnect_state
*recon_state
= arg
;
2284 struct ceph_pagelist
*pagelist
= recon_state
->pagelist
;
2288 struct dentry
*dentry
;
2292 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2293 inode
, ceph_vinop(inode
), cap
, cap
->cap_id
,
2294 ceph_cap_string(cap
->issued
));
2295 err
= ceph_pagelist_encode_64(pagelist
, ceph_ino(inode
));
2299 dentry
= d_find_alias(inode
);
2301 path
= ceph_mdsc_build_path(dentry
, &pathlen
, &pathbase
, 0);
2303 err
= PTR_ERR(path
);
2310 err
= ceph_pagelist_encode_string(pagelist
, path
, pathlen
);
2314 spin_lock(&inode
->i_lock
);
2315 cap
->seq
= 0; /* reset cap seq */
2316 cap
->issue_seq
= 0; /* and issue_seq */
2318 if (recon_state
->flock
) {
2319 rec
.v2
.cap_id
= cpu_to_le64(cap
->cap_id
);
2320 rec
.v2
.wanted
= cpu_to_le32(__ceph_caps_wanted(ci
));
2321 rec
.v2
.issued
= cpu_to_le32(cap
->issued
);
2322 rec
.v2
.snaprealm
= cpu_to_le64(ci
->i_snap_realm
->ino
);
2323 rec
.v2
.pathbase
= cpu_to_le64(pathbase
);
2324 rec
.v2
.flock_len
= 0;
2325 reclen
= sizeof(rec
.v2
);
2327 rec
.v1
.cap_id
= cpu_to_le64(cap
->cap_id
);
2328 rec
.v1
.wanted
= cpu_to_le32(__ceph_caps_wanted(ci
));
2329 rec
.v1
.issued
= cpu_to_le32(cap
->issued
);
2330 rec
.v1
.size
= cpu_to_le64(inode
->i_size
);
2331 ceph_encode_timespec(&rec
.v1
.mtime
, &inode
->i_mtime
);
2332 ceph_encode_timespec(&rec
.v1
.atime
, &inode
->i_atime
);
2333 rec
.v1
.snaprealm
= cpu_to_le64(ci
->i_snap_realm
->ino
);
2334 rec
.v1
.pathbase
= cpu_to_le64(pathbase
);
2335 reclen
= sizeof(rec
.v1
);
2337 spin_unlock(&inode
->i_lock
);
2339 if (recon_state
->flock
) {
2340 int num_fcntl_locks
, num_flock_locks
;
2343 ceph_count_locks(inode
, &num_fcntl_locks
, &num_flock_locks
);
2344 rec
.v2
.flock_len
= (2*sizeof(u32
) +
2345 (num_fcntl_locks
+num_flock_locks
) *
2346 sizeof(struct ceph_filelock
));
2348 err
= ceph_pagelist_append(pagelist
, &rec
, reclen
);
2350 err
= ceph_encode_locks(inode
, pagelist
,
2364 * If an MDS fails and recovers, clients need to reconnect in order to
2365 * reestablish shared state. This includes all caps issued through
2366 * this session _and_ the snap_realm hierarchy. Because it's not
2367 * clear which snap realms the mds cares about, we send everything we
2368 * know about.. that ensures we'll then get any new info the
2369 * recovering MDS might have.
2371 * This is a relatively heavyweight operation, but it's rare.
2373 * called with mdsc->mutex held.
2375 static void send_mds_reconnect(struct ceph_mds_client
*mdsc
,
2376 struct ceph_mds_session
*session
)
2378 struct ceph_msg
*reply
;
2380 int mds
= session
->s_mds
;
2382 struct ceph_pagelist
*pagelist
;
2383 struct ceph_reconnect_state recon_state
;
2385 pr_info("mds%d reconnect start\n", mds
);
2387 pagelist
= kmalloc(sizeof(*pagelist
), GFP_NOFS
);
2389 goto fail_nopagelist
;
2390 ceph_pagelist_init(pagelist
);
2392 reply
= ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT
, 0, GFP_NOFS
);
2396 mutex_lock(&session
->s_mutex
);
2397 session
->s_state
= CEPH_MDS_SESSION_RECONNECTING
;
2400 ceph_con_open(&session
->s_con
,
2401 ceph_mdsmap_get_addr(mdsc
->mdsmap
, mds
));
2403 /* replay unsafe requests */
2404 replay_unsafe_requests(mdsc
, session
);
2406 down_read(&mdsc
->snap_rwsem
);
2408 dout("session %p state %s\n", session
,
2409 session_state_name(session
->s_state
));
2411 /* drop old cap expires; we're about to reestablish that state */
2412 discard_cap_releases(mdsc
, session
);
2414 /* traverse this session's caps */
2415 err
= ceph_pagelist_encode_32(pagelist
, session
->s_nr_caps
);
2419 recon_state
.pagelist
= pagelist
;
2420 recon_state
.flock
= session
->s_con
.peer_features
& CEPH_FEATURE_FLOCK
;
2421 err
= iterate_session_caps(session
, encode_caps_cb
, &recon_state
);
2426 * snaprealms. we provide mds with the ino, seq (version), and
2427 * parent for all of our realms. If the mds has any newer info,
2430 for (p
= rb_first(&mdsc
->snap_realms
); p
; p
= rb_next(p
)) {
2431 struct ceph_snap_realm
*realm
=
2432 rb_entry(p
, struct ceph_snap_realm
, node
);
2433 struct ceph_mds_snaprealm_reconnect sr_rec
;
2435 dout(" adding snap realm %llx seq %lld parent %llx\n",
2436 realm
->ino
, realm
->seq
, realm
->parent_ino
);
2437 sr_rec
.ino
= cpu_to_le64(realm
->ino
);
2438 sr_rec
.seq
= cpu_to_le64(realm
->seq
);
2439 sr_rec
.parent
= cpu_to_le64(realm
->parent_ino
);
2440 err
= ceph_pagelist_append(pagelist
, &sr_rec
, sizeof(sr_rec
));
2445 reply
->pagelist
= pagelist
;
2446 if (recon_state
.flock
)
2447 reply
->hdr
.version
= cpu_to_le16(2);
2448 reply
->hdr
.data_len
= cpu_to_le32(pagelist
->length
);
2449 reply
->nr_pages
= calc_pages_for(0, pagelist
->length
);
2450 ceph_con_send(&session
->s_con
, reply
);
2452 mutex_unlock(&session
->s_mutex
);
2454 mutex_lock(&mdsc
->mutex
);
2455 __wake_requests(mdsc
, &session
->s_waiting
);
2456 mutex_unlock(&mdsc
->mutex
);
2458 up_read(&mdsc
->snap_rwsem
);
2462 ceph_msg_put(reply
);
2463 up_read(&mdsc
->snap_rwsem
);
2464 mutex_unlock(&session
->s_mutex
);
2466 ceph_pagelist_release(pagelist
);
2469 pr_err("error %d preparing reconnect for mds%d\n", err
, mds
);
2475 * compare old and new mdsmaps, kicking requests
2476 * and closing out old connections as necessary
2478 * called under mdsc->mutex.
2480 static void check_new_map(struct ceph_mds_client
*mdsc
,
2481 struct ceph_mdsmap
*newmap
,
2482 struct ceph_mdsmap
*oldmap
)
2485 int oldstate
, newstate
;
2486 struct ceph_mds_session
*s
;
2488 dout("check_new_map new %u old %u\n",
2489 newmap
->m_epoch
, oldmap
->m_epoch
);
2491 for (i
= 0; i
< oldmap
->m_max_mds
&& i
< mdsc
->max_sessions
; i
++) {
2492 if (mdsc
->sessions
[i
] == NULL
)
2494 s
= mdsc
->sessions
[i
];
2495 oldstate
= ceph_mdsmap_get_state(oldmap
, i
);
2496 newstate
= ceph_mdsmap_get_state(newmap
, i
);
2498 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
2499 i
, ceph_mds_state_name(oldstate
),
2500 ceph_mdsmap_is_laggy(oldmap
, i
) ? " (laggy)" : "",
2501 ceph_mds_state_name(newstate
),
2502 ceph_mdsmap_is_laggy(newmap
, i
) ? " (laggy)" : "",
2503 session_state_name(s
->s_state
));
2505 if (memcmp(ceph_mdsmap_get_addr(oldmap
, i
),
2506 ceph_mdsmap_get_addr(newmap
, i
),
2507 sizeof(struct ceph_entity_addr
))) {
2508 if (s
->s_state
== CEPH_MDS_SESSION_OPENING
) {
2509 /* the session never opened, just close it
2511 __wake_requests(mdsc
, &s
->s_waiting
);
2512 __unregister_session(mdsc
, s
);
2515 mutex_unlock(&mdsc
->mutex
);
2516 mutex_lock(&s
->s_mutex
);
2517 mutex_lock(&mdsc
->mutex
);
2518 ceph_con_close(&s
->s_con
);
2519 mutex_unlock(&s
->s_mutex
);
2520 s
->s_state
= CEPH_MDS_SESSION_RESTARTING
;
2523 /* kick any requests waiting on the recovering mds */
2524 kick_requests(mdsc
, i
);
2525 } else if (oldstate
== newstate
) {
2526 continue; /* nothing new with this mds */
2532 if (s
->s_state
== CEPH_MDS_SESSION_RESTARTING
&&
2533 newstate
>= CEPH_MDS_STATE_RECONNECT
) {
2534 mutex_unlock(&mdsc
->mutex
);
2535 send_mds_reconnect(mdsc
, s
);
2536 mutex_lock(&mdsc
->mutex
);
2540 * kick request on any mds that has gone active.
2542 if (oldstate
< CEPH_MDS_STATE_ACTIVE
&&
2543 newstate
>= CEPH_MDS_STATE_ACTIVE
) {
2544 if (oldstate
!= CEPH_MDS_STATE_CREATING
&&
2545 oldstate
!= CEPH_MDS_STATE_STARTING
)
2546 pr_info("mds%d recovery completed\n", s
->s_mds
);
2547 kick_requests(mdsc
, i
);
2548 ceph_kick_flushing_caps(mdsc
, s
);
2549 wake_up_session_caps(s
, 1);
2553 for (i
= 0; i
< newmap
->m_max_mds
&& i
< mdsc
->max_sessions
; i
++) {
2554 s
= mdsc
->sessions
[i
];
2557 if (!ceph_mdsmap_is_laggy(newmap
, i
))
2559 if (s
->s_state
== CEPH_MDS_SESSION_OPEN
||
2560 s
->s_state
== CEPH_MDS_SESSION_HUNG
||
2561 s
->s_state
== CEPH_MDS_SESSION_CLOSING
) {
2562 dout(" connecting to export targets of laggy mds%d\n",
2564 __open_export_target_sessions(mdsc
, s
);
2576 * caller must hold session s_mutex, dentry->d_lock
2578 void __ceph_mdsc_drop_dentry_lease(struct dentry
*dentry
)
2580 struct ceph_dentry_info
*di
= ceph_dentry(dentry
);
2582 ceph_put_mds_session(di
->lease_session
);
2583 di
->lease_session
= NULL
;
2586 static void handle_lease(struct ceph_mds_client
*mdsc
,
2587 struct ceph_mds_session
*session
,
2588 struct ceph_msg
*msg
)
2590 struct super_block
*sb
= mdsc
->client
->sb
;
2591 struct inode
*inode
;
2592 struct ceph_inode_info
*ci
;
2593 struct dentry
*parent
, *dentry
;
2594 struct ceph_dentry_info
*di
;
2595 int mds
= session
->s_mds
;
2596 struct ceph_mds_lease
*h
= msg
->front
.iov_base
;
2598 struct ceph_vino vino
;
2603 dout("handle_lease from mds%d\n", mds
);
2606 if (msg
->front
.iov_len
< sizeof(*h
) + sizeof(u32
))
2608 vino
.ino
= le64_to_cpu(h
->ino
);
2609 vino
.snap
= CEPH_NOSNAP
;
2610 mask
= le16_to_cpu(h
->mask
);
2611 seq
= le32_to_cpu(h
->seq
);
2612 dname
.name
= (void *)h
+ sizeof(*h
) + sizeof(u32
);
2613 dname
.len
= msg
->front
.iov_len
- sizeof(*h
) - sizeof(u32
);
2614 if (dname
.len
!= get_unaligned_le32(h
+1))
2617 mutex_lock(&session
->s_mutex
);
2621 inode
= ceph_find_inode(sb
, vino
);
2622 dout("handle_lease %s, mask %d, ino %llx %p %.*s\n",
2623 ceph_lease_op_name(h
->action
), mask
, vino
.ino
, inode
,
2624 dname
.len
, dname
.name
);
2625 if (inode
== NULL
) {
2626 dout("handle_lease no inode %llx\n", vino
.ino
);
2629 ci
= ceph_inode(inode
);
2632 parent
= d_find_alias(inode
);
2634 dout("no parent dentry on inode %p\n", inode
);
2636 goto release
; /* hrm... */
2638 dname
.hash
= full_name_hash(dname
.name
, dname
.len
);
2639 dentry
= d_lookup(parent
, &dname
);
2644 spin_lock(&dentry
->d_lock
);
2645 di
= ceph_dentry(dentry
);
2646 switch (h
->action
) {
2647 case CEPH_MDS_LEASE_REVOKE
:
2648 if (di
&& di
->lease_session
== session
) {
2649 if (ceph_seq_cmp(di
->lease_seq
, seq
) > 0)
2650 h
->seq
= cpu_to_le32(di
->lease_seq
);
2651 __ceph_mdsc_drop_dentry_lease(dentry
);
2656 case CEPH_MDS_LEASE_RENEW
:
2657 if (di
&& di
->lease_session
== session
&&
2658 di
->lease_gen
== session
->s_cap_gen
&&
2659 di
->lease_renew_from
&&
2660 di
->lease_renew_after
== 0) {
2661 unsigned long duration
=
2662 le32_to_cpu(h
->duration_ms
) * HZ
/ 1000;
2664 di
->lease_seq
= seq
;
2665 dentry
->d_time
= di
->lease_renew_from
+ duration
;
2666 di
->lease_renew_after
= di
->lease_renew_from
+
2668 di
->lease_renew_from
= 0;
2672 spin_unlock(&dentry
->d_lock
);
2679 /* let's just reuse the same message */
2680 h
->action
= CEPH_MDS_LEASE_REVOKE_ACK
;
2682 ceph_con_send(&session
->s_con
, msg
);
2686 mutex_unlock(&session
->s_mutex
);
2690 pr_err("corrupt lease message\n");
2694 void ceph_mdsc_lease_send_msg(struct ceph_mds_session
*session
,
2695 struct inode
*inode
,
2696 struct dentry
*dentry
, char action
,
2699 struct ceph_msg
*msg
;
2700 struct ceph_mds_lease
*lease
;
2701 int len
= sizeof(*lease
) + sizeof(u32
);
2704 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
2705 inode
, dentry
, ceph_lease_op_name(action
), session
->s_mds
);
2706 dnamelen
= dentry
->d_name
.len
;
2709 msg
= ceph_msg_new(CEPH_MSG_CLIENT_LEASE
, len
, GFP_NOFS
);
2712 lease
= msg
->front
.iov_base
;
2713 lease
->action
= action
;
2714 lease
->mask
= cpu_to_le16(1);
2715 lease
->ino
= cpu_to_le64(ceph_vino(inode
).ino
);
2716 lease
->first
= lease
->last
= cpu_to_le64(ceph_vino(inode
).snap
);
2717 lease
->seq
= cpu_to_le32(seq
);
2718 put_unaligned_le32(dnamelen
, lease
+ 1);
2719 memcpy((void *)(lease
+ 1) + 4, dentry
->d_name
.name
, dnamelen
);
2722 * if this is a preemptive lease RELEASE, no need to
2723 * flush request stream, since the actual request will
2726 msg
->more_to_follow
= (action
== CEPH_MDS_LEASE_RELEASE
);
2728 ceph_con_send(&session
->s_con
, msg
);
2732 * Preemptively release a lease we expect to invalidate anyway.
2733 * Pass @inode always, @dentry is optional.
2735 void ceph_mdsc_lease_release(struct ceph_mds_client
*mdsc
, struct inode
*inode
,
2736 struct dentry
*dentry
, int mask
)
2738 struct ceph_dentry_info
*di
;
2739 struct ceph_mds_session
*session
;
2742 BUG_ON(inode
== NULL
);
2743 BUG_ON(dentry
== NULL
);
2746 /* is dentry lease valid? */
2747 spin_lock(&dentry
->d_lock
);
2748 di
= ceph_dentry(dentry
);
2749 if (!di
|| !di
->lease_session
||
2750 di
->lease_session
->s_mds
< 0 ||
2751 di
->lease_gen
!= di
->lease_session
->s_cap_gen
||
2752 !time_before(jiffies
, dentry
->d_time
)) {
2753 dout("lease_release inode %p dentry %p -- "
2755 inode
, dentry
, mask
);
2756 spin_unlock(&dentry
->d_lock
);
2760 /* we do have a lease on this dentry; note mds and seq */
2761 session
= ceph_get_mds_session(di
->lease_session
);
2762 seq
= di
->lease_seq
;
2763 __ceph_mdsc_drop_dentry_lease(dentry
);
2764 spin_unlock(&dentry
->d_lock
);
2766 dout("lease_release inode %p dentry %p mask %d to mds%d\n",
2767 inode
, dentry
, mask
, session
->s_mds
);
2768 ceph_mdsc_lease_send_msg(session
, inode
, dentry
,
2769 CEPH_MDS_LEASE_RELEASE
, seq
);
2770 ceph_put_mds_session(session
);
2774 * drop all leases (and dentry refs) in preparation for umount
2776 static void drop_leases(struct ceph_mds_client
*mdsc
)
2780 dout("drop_leases\n");
2781 mutex_lock(&mdsc
->mutex
);
2782 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
2783 struct ceph_mds_session
*s
= __ceph_lookup_mds_session(mdsc
, i
);
2786 mutex_unlock(&mdsc
->mutex
);
2787 mutex_lock(&s
->s_mutex
);
2788 mutex_unlock(&s
->s_mutex
);
2789 ceph_put_mds_session(s
);
2790 mutex_lock(&mdsc
->mutex
);
2792 mutex_unlock(&mdsc
->mutex
);
2798 * delayed work -- periodically trim expired leases, renew caps with mds
2800 static void schedule_delayed(struct ceph_mds_client
*mdsc
)
2803 unsigned hz
= round_jiffies_relative(HZ
* delay
);
2804 schedule_delayed_work(&mdsc
->delayed_work
, hz
);
2807 static void delayed_work(struct work_struct
*work
)
2810 struct ceph_mds_client
*mdsc
=
2811 container_of(work
, struct ceph_mds_client
, delayed_work
.work
);
2815 dout("mdsc delayed_work\n");
2816 ceph_check_delayed_caps(mdsc
);
2818 mutex_lock(&mdsc
->mutex
);
2819 renew_interval
= mdsc
->mdsmap
->m_session_timeout
>> 2;
2820 renew_caps
= time_after_eq(jiffies
, HZ
*renew_interval
+
2821 mdsc
->last_renew_caps
);
2823 mdsc
->last_renew_caps
= jiffies
;
2825 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
2826 struct ceph_mds_session
*s
= __ceph_lookup_mds_session(mdsc
, i
);
2829 if (s
->s_state
== CEPH_MDS_SESSION_CLOSING
) {
2830 dout("resending session close request for mds%d\n",
2832 request_close_session(mdsc
, s
);
2833 ceph_put_mds_session(s
);
2836 if (s
->s_ttl
&& time_after(jiffies
, s
->s_ttl
)) {
2837 if (s
->s_state
== CEPH_MDS_SESSION_OPEN
) {
2838 s
->s_state
= CEPH_MDS_SESSION_HUNG
;
2839 pr_info("mds%d hung\n", s
->s_mds
);
2842 if (s
->s_state
< CEPH_MDS_SESSION_OPEN
) {
2843 /* this mds is failed or recovering, just wait */
2844 ceph_put_mds_session(s
);
2847 mutex_unlock(&mdsc
->mutex
);
2849 mutex_lock(&s
->s_mutex
);
2851 send_renew_caps(mdsc
, s
);
2853 ceph_con_keepalive(&s
->s_con
);
2854 ceph_add_cap_releases(mdsc
, s
);
2855 if (s
->s_state
== CEPH_MDS_SESSION_OPEN
||
2856 s
->s_state
== CEPH_MDS_SESSION_HUNG
)
2857 ceph_send_cap_releases(mdsc
, s
);
2858 mutex_unlock(&s
->s_mutex
);
2859 ceph_put_mds_session(s
);
2861 mutex_lock(&mdsc
->mutex
);
2863 mutex_unlock(&mdsc
->mutex
);
2865 schedule_delayed(mdsc
);
2869 int ceph_mdsc_init(struct ceph_mds_client
*mdsc
, struct ceph_client
*client
)
2871 mdsc
->client
= client
;
2872 mutex_init(&mdsc
->mutex
);
2873 mdsc
->mdsmap
= kzalloc(sizeof(*mdsc
->mdsmap
), GFP_NOFS
);
2874 if (mdsc
->mdsmap
== NULL
)
2877 init_completion(&mdsc
->safe_umount_waiters
);
2878 init_completion(&mdsc
->session_close_waiters
);
2879 INIT_LIST_HEAD(&mdsc
->waiting_for_map
);
2880 mdsc
->sessions
= NULL
;
2881 mdsc
->max_sessions
= 0;
2883 init_rwsem(&mdsc
->snap_rwsem
);
2884 mdsc
->snap_realms
= RB_ROOT
;
2885 INIT_LIST_HEAD(&mdsc
->snap_empty
);
2886 spin_lock_init(&mdsc
->snap_empty_lock
);
2888 mdsc
->request_tree
= RB_ROOT
;
2889 INIT_DELAYED_WORK(&mdsc
->delayed_work
, delayed_work
);
2890 mdsc
->last_renew_caps
= jiffies
;
2891 INIT_LIST_HEAD(&mdsc
->cap_delay_list
);
2892 spin_lock_init(&mdsc
->cap_delay_lock
);
2893 INIT_LIST_HEAD(&mdsc
->snap_flush_list
);
2894 spin_lock_init(&mdsc
->snap_flush_lock
);
2895 mdsc
->cap_flush_seq
= 0;
2896 INIT_LIST_HEAD(&mdsc
->cap_dirty
);
2897 mdsc
->num_cap_flushing
= 0;
2898 spin_lock_init(&mdsc
->cap_dirty_lock
);
2899 init_waitqueue_head(&mdsc
->cap_flushing_wq
);
2900 spin_lock_init(&mdsc
->dentry_lru_lock
);
2901 INIT_LIST_HEAD(&mdsc
->dentry_lru
);
2903 ceph_caps_init(mdsc
);
2904 ceph_adjust_min_caps(mdsc
, client
->min_caps
);
2910 * Wait for safe replies on open mds requests. If we time out, drop
2911 * all requests from the tree to avoid dangling dentry refs.
2913 static void wait_requests(struct ceph_mds_client
*mdsc
)
2915 struct ceph_mds_request
*req
;
2916 struct ceph_client
*client
= mdsc
->client
;
2918 mutex_lock(&mdsc
->mutex
);
2919 if (__get_oldest_req(mdsc
)) {
2920 mutex_unlock(&mdsc
->mutex
);
2922 dout("wait_requests waiting for requests\n");
2923 wait_for_completion_timeout(&mdsc
->safe_umount_waiters
,
2924 client
->mount_args
->mount_timeout
* HZ
);
2926 /* tear down remaining requests */
2927 mutex_lock(&mdsc
->mutex
);
2928 while ((req
= __get_oldest_req(mdsc
))) {
2929 dout("wait_requests timed out on tid %llu\n",
2931 __unregister_request(mdsc
, req
);
2934 mutex_unlock(&mdsc
->mutex
);
2935 dout("wait_requests done\n");
2939 * called before mount is ro, and before dentries are torn down.
2940 * (hmm, does this still race with new lookups?)
2942 void ceph_mdsc_pre_umount(struct ceph_mds_client
*mdsc
)
2944 dout("pre_umount\n");
2948 ceph_flush_dirty_caps(mdsc
);
2949 wait_requests(mdsc
);
2952 * wait for reply handlers to drop their request refs and
2953 * their inode/dcache refs
2959 * wait for all write mds requests to flush.
2961 static void wait_unsafe_requests(struct ceph_mds_client
*mdsc
, u64 want_tid
)
2963 struct ceph_mds_request
*req
= NULL
, *nextreq
;
2966 mutex_lock(&mdsc
->mutex
);
2967 dout("wait_unsafe_requests want %lld\n", want_tid
);
2969 req
= __get_oldest_req(mdsc
);
2970 while (req
&& req
->r_tid
<= want_tid
) {
2971 /* find next request */
2972 n
= rb_next(&req
->r_node
);
2974 nextreq
= rb_entry(n
, struct ceph_mds_request
, r_node
);
2977 if ((req
->r_op
& CEPH_MDS_OP_WRITE
)) {
2979 ceph_mdsc_get_request(req
);
2981 ceph_mdsc_get_request(nextreq
);
2982 mutex_unlock(&mdsc
->mutex
);
2983 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
2984 req
->r_tid
, want_tid
);
2985 wait_for_completion(&req
->r_safe_completion
);
2986 mutex_lock(&mdsc
->mutex
);
2987 ceph_mdsc_put_request(req
);
2989 break; /* next dne before, so we're done! */
2990 if (RB_EMPTY_NODE(&nextreq
->r_node
)) {
2991 /* next request was removed from tree */
2992 ceph_mdsc_put_request(nextreq
);
2995 ceph_mdsc_put_request(nextreq
); /* won't go away */
2999 mutex_unlock(&mdsc
->mutex
);
3000 dout("wait_unsafe_requests done\n");
3003 void ceph_mdsc_sync(struct ceph_mds_client
*mdsc
)
3005 u64 want_tid
, want_flush
;
3007 if (mdsc
->client
->mount_state
== CEPH_MOUNT_SHUTDOWN
)
3011 mutex_lock(&mdsc
->mutex
);
3012 want_tid
= mdsc
->last_tid
;
3013 want_flush
= mdsc
->cap_flush_seq
;
3014 mutex_unlock(&mdsc
->mutex
);
3015 dout("sync want tid %lld flush_seq %lld\n", want_tid
, want_flush
);
3017 ceph_flush_dirty_caps(mdsc
);
3019 wait_unsafe_requests(mdsc
, want_tid
);
3020 wait_event(mdsc
->cap_flushing_wq
, check_cap_flush(mdsc
, want_flush
));
3025 * called after sb is ro.
3027 void ceph_mdsc_close_sessions(struct ceph_mds_client
*mdsc
)
3029 struct ceph_mds_session
*session
;
3032 struct ceph_client
*client
= mdsc
->client
;
3033 unsigned long started
, timeout
= client
->mount_args
->mount_timeout
* HZ
;
3035 dout("close_sessions\n");
3037 mutex_lock(&mdsc
->mutex
);
3039 /* close sessions */
3041 while (time_before(jiffies
, started
+ timeout
)) {
3042 dout("closing sessions\n");
3044 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
3045 session
= __ceph_lookup_mds_session(mdsc
, i
);
3048 mutex_unlock(&mdsc
->mutex
);
3049 mutex_lock(&session
->s_mutex
);
3050 __close_session(mdsc
, session
);
3051 mutex_unlock(&session
->s_mutex
);
3052 ceph_put_mds_session(session
);
3053 mutex_lock(&mdsc
->mutex
);
3059 if (client
->mount_state
== CEPH_MOUNT_SHUTDOWN
)
3062 dout("waiting for sessions to close\n");
3063 mutex_unlock(&mdsc
->mutex
);
3064 wait_for_completion_timeout(&mdsc
->session_close_waiters
,
3066 mutex_lock(&mdsc
->mutex
);
3069 /* tear down remaining sessions */
3070 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
3071 if (mdsc
->sessions
[i
]) {
3072 session
= get_session(mdsc
->sessions
[i
]);
3073 __unregister_session(mdsc
, session
);
3074 mutex_unlock(&mdsc
->mutex
);
3075 mutex_lock(&session
->s_mutex
);
3076 remove_session_caps(session
);
3077 mutex_unlock(&session
->s_mutex
);
3078 ceph_put_mds_session(session
);
3079 mutex_lock(&mdsc
->mutex
);
3083 WARN_ON(!list_empty(&mdsc
->cap_delay_list
));
3085 mutex_unlock(&mdsc
->mutex
);
3087 ceph_cleanup_empty_realms(mdsc
);
3089 cancel_delayed_work_sync(&mdsc
->delayed_work
); /* cancel timer */
3094 void ceph_mdsc_stop(struct ceph_mds_client
*mdsc
)
3097 cancel_delayed_work_sync(&mdsc
->delayed_work
); /* cancel timer */
3099 ceph_mdsmap_destroy(mdsc
->mdsmap
);
3100 kfree(mdsc
->sessions
);
3101 ceph_caps_finalize(mdsc
);
3106 * handle mds map update.
3108 void ceph_mdsc_handle_map(struct ceph_mds_client
*mdsc
, struct ceph_msg
*msg
)
3112 void *p
= msg
->front
.iov_base
;
3113 void *end
= p
+ msg
->front
.iov_len
;
3114 struct ceph_mdsmap
*newmap
, *oldmap
;
3115 struct ceph_fsid fsid
;
3118 ceph_decode_need(&p
, end
, sizeof(fsid
)+2*sizeof(u32
), bad
);
3119 ceph_decode_copy(&p
, &fsid
, sizeof(fsid
));
3120 if (ceph_check_fsid(mdsc
->client
, &fsid
) < 0)
3122 epoch
= ceph_decode_32(&p
);
3123 maplen
= ceph_decode_32(&p
);
3124 dout("handle_map epoch %u len %d\n", epoch
, (int)maplen
);
3126 /* do we need it? */
3127 ceph_monc_got_mdsmap(&mdsc
->client
->monc
, epoch
);
3128 mutex_lock(&mdsc
->mutex
);
3129 if (mdsc
->mdsmap
&& epoch
<= mdsc
->mdsmap
->m_epoch
) {
3130 dout("handle_map epoch %u <= our %u\n",
3131 epoch
, mdsc
->mdsmap
->m_epoch
);
3132 mutex_unlock(&mdsc
->mutex
);
3136 newmap
= ceph_mdsmap_decode(&p
, end
);
3137 if (IS_ERR(newmap
)) {
3138 err
= PTR_ERR(newmap
);
3142 /* swap into place */
3144 oldmap
= mdsc
->mdsmap
;
3145 mdsc
->mdsmap
= newmap
;
3146 check_new_map(mdsc
, newmap
, oldmap
);
3147 ceph_mdsmap_destroy(oldmap
);
3149 mdsc
->mdsmap
= newmap
; /* first mds map */
3151 mdsc
->client
->sb
->s_maxbytes
= mdsc
->mdsmap
->m_max_file_size
;
3153 __wake_requests(mdsc
, &mdsc
->waiting_for_map
);
3155 mutex_unlock(&mdsc
->mutex
);
3156 schedule_delayed(mdsc
);
3160 mutex_unlock(&mdsc
->mutex
);
3162 pr_err("error decoding mdsmap %d\n", err
);
3166 static struct ceph_connection
*con_get(struct ceph_connection
*con
)
3168 struct ceph_mds_session
*s
= con
->private;
3170 if (get_session(s
)) {
3171 dout("mdsc con_get %p ok (%d)\n", s
, atomic_read(&s
->s_ref
));
3174 dout("mdsc con_get %p FAIL\n", s
);
3178 static void con_put(struct ceph_connection
*con
)
3180 struct ceph_mds_session
*s
= con
->private;
3182 ceph_put_mds_session(s
);
3183 dout("mdsc con_put %p (%d)\n", s
, atomic_read(&s
->s_ref
));
3187 * if the client is unresponsive for long enough, the mds will kill
3188 * the session entirely.
3190 static void peer_reset(struct ceph_connection
*con
)
3192 struct ceph_mds_session
*s
= con
->private;
3193 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
3195 pr_warning("mds%d closed our session\n", s
->s_mds
);
3196 send_mds_reconnect(mdsc
, s
);
3199 static void dispatch(struct ceph_connection
*con
, struct ceph_msg
*msg
)
3201 struct ceph_mds_session
*s
= con
->private;
3202 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
3203 int type
= le16_to_cpu(msg
->hdr
.type
);
3205 mutex_lock(&mdsc
->mutex
);
3206 if (__verify_registered_session(mdsc
, s
) < 0) {
3207 mutex_unlock(&mdsc
->mutex
);
3210 mutex_unlock(&mdsc
->mutex
);
3213 case CEPH_MSG_MDS_MAP
:
3214 ceph_mdsc_handle_map(mdsc
, msg
);
3216 case CEPH_MSG_CLIENT_SESSION
:
3217 handle_session(s
, msg
);
3219 case CEPH_MSG_CLIENT_REPLY
:
3220 handle_reply(s
, msg
);
3222 case CEPH_MSG_CLIENT_REQUEST_FORWARD
:
3223 handle_forward(mdsc
, s
, msg
);
3225 case CEPH_MSG_CLIENT_CAPS
:
3226 ceph_handle_caps(s
, msg
);
3228 case CEPH_MSG_CLIENT_SNAP
:
3229 ceph_handle_snap(mdsc
, s
, msg
);
3231 case CEPH_MSG_CLIENT_LEASE
:
3232 handle_lease(mdsc
, s
, msg
);
3236 pr_err("received unknown message type %d %s\n", type
,
3237 ceph_msg_type_name(type
));
3246 static int get_authorizer(struct ceph_connection
*con
,
3247 void **buf
, int *len
, int *proto
,
3248 void **reply_buf
, int *reply_len
, int force_new
)
3250 struct ceph_mds_session
*s
= con
->private;
3251 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
3252 struct ceph_auth_client
*ac
= mdsc
->client
->monc
.auth
;
3255 if (force_new
&& s
->s_authorizer
) {
3256 ac
->ops
->destroy_authorizer(ac
, s
->s_authorizer
);
3257 s
->s_authorizer
= NULL
;
3259 if (s
->s_authorizer
== NULL
) {
3260 if (ac
->ops
->create_authorizer
) {
3261 ret
= ac
->ops
->create_authorizer(
3262 ac
, CEPH_ENTITY_TYPE_MDS
,
3264 &s
->s_authorizer_buf
,
3265 &s
->s_authorizer_buf_len
,
3266 &s
->s_authorizer_reply_buf
,
3267 &s
->s_authorizer_reply_buf_len
);
3273 *proto
= ac
->protocol
;
3274 *buf
= s
->s_authorizer_buf
;
3275 *len
= s
->s_authorizer_buf_len
;
3276 *reply_buf
= s
->s_authorizer_reply_buf
;
3277 *reply_len
= s
->s_authorizer_reply_buf_len
;
3282 static int verify_authorizer_reply(struct ceph_connection
*con
, int len
)
3284 struct ceph_mds_session
*s
= con
->private;
3285 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
3286 struct ceph_auth_client
*ac
= mdsc
->client
->monc
.auth
;
3288 return ac
->ops
->verify_authorizer_reply(ac
, s
->s_authorizer
, len
);
3291 static int invalidate_authorizer(struct ceph_connection
*con
)
3293 struct ceph_mds_session
*s
= con
->private;
3294 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
3295 struct ceph_auth_client
*ac
= mdsc
->client
->monc
.auth
;
3297 if (ac
->ops
->invalidate_authorizer
)
3298 ac
->ops
->invalidate_authorizer(ac
, CEPH_ENTITY_TYPE_MDS
);
3300 return ceph_monc_validate_auth(&mdsc
->client
->monc
);
3303 static const struct ceph_connection_operations mds_con_ops
= {
3306 .dispatch
= dispatch
,
3307 .get_authorizer
= get_authorizer
,
3308 .verify_authorizer_reply
= verify_authorizer_reply
,
3309 .invalidate_authorizer
= invalidate_authorizer
,
3310 .peer_reset
= peer_reset
,