1 #include "ceph_debug.h"
3 #include <linux/wait.h>
4 #include <linux/sched.h>
6 #include "mds_client.h"
7 #include "mon_client.h"
15 * A cluster of MDS (metadata server) daemons is responsible for
16 * managing the file system namespace (the directory hierarchy and
17 * inodes) and for coordinating shared access to storage. Metadata is
18 * partitioning hierarchically across a number of servers, and that
19 * partition varies over time as the cluster adjusts the distribution
20 * in order to balance load.
22 * The MDS client is primarily responsible to managing synchronous
23 * metadata requests for operations like open, unlink, and so forth.
24 * If there is a MDS failure, we find out about it when we (possibly
25 * request and) receive a new MDS map, and can resubmit affected
28 * For the most part, though, we take advantage of a lossless
29 * communications channel to the MDS, and do not need to worry about
30 * timing out or resubmitting requests.
32 * We maintain a stateful "session" with each MDS we interact with.
33 * Within each session, we sent periodic heartbeat messages to ensure
34 * any capabilities or leases we have been issues remain valid. If
35 * the session times out and goes stale, our leases and capabilities
36 * are no longer valid.
39 static void __wake_requests(struct ceph_mds_client
*mdsc
,
40 struct list_head
*head
);
42 const static struct ceph_connection_operations mds_con_ops
;
50 * parse individual inode info
52 static int parse_reply_info_in(void **p
, void *end
,
53 struct ceph_mds_reply_info_in
*info
)
58 *p
+= sizeof(struct ceph_mds_reply_inode
) +
59 sizeof(*info
->in
->fragtree
.splits
) *
60 le32_to_cpu(info
->in
->fragtree
.nsplits
);
62 ceph_decode_32_safe(p
, end
, info
->symlink_len
, bad
);
63 ceph_decode_need(p
, end
, info
->symlink_len
, bad
);
65 *p
+= info
->symlink_len
;
67 ceph_decode_32_safe(p
, end
, info
->xattr_len
, bad
);
68 ceph_decode_need(p
, end
, info
->xattr_len
, bad
);
69 info
->xattr_data
= *p
;
70 *p
+= info
->xattr_len
;
77 * parse a normal reply, which may contain a (dir+)dentry and/or a
80 static int parse_reply_info_trace(void **p
, void *end
,
81 struct ceph_mds_reply_info_parsed
*info
)
85 if (info
->head
->is_dentry
) {
86 err
= parse_reply_info_in(p
, end
, &info
->diri
);
90 if (unlikely(*p
+ sizeof(*info
->dirfrag
) > end
))
93 *p
+= sizeof(*info
->dirfrag
) +
94 sizeof(u32
)*le32_to_cpu(info
->dirfrag
->ndist
);
95 if (unlikely(*p
> end
))
98 ceph_decode_32_safe(p
, end
, info
->dname_len
, bad
);
99 ceph_decode_need(p
, end
, info
->dname_len
, bad
);
101 *p
+= info
->dname_len
;
103 *p
+= sizeof(*info
->dlease
);
106 if (info
->head
->is_target
) {
107 err
= parse_reply_info_in(p
, end
, &info
->targeti
);
112 if (unlikely(*p
!= end
))
119 pr_err("problem parsing mds trace %d\n", err
);
124 * parse readdir results
126 static int parse_reply_info_dir(void **p
, void *end
,
127 struct ceph_mds_reply_info_parsed
*info
)
133 if (*p
+ sizeof(*info
->dir_dir
) > end
)
135 *p
+= sizeof(*info
->dir_dir
) +
136 sizeof(u32
)*le32_to_cpu(info
->dir_dir
->ndist
);
140 ceph_decode_need(p
, end
, sizeof(num
) + 2, bad
);
141 num
= ceph_decode_32(p
);
142 info
->dir_end
= ceph_decode_8(p
);
143 info
->dir_complete
= ceph_decode_8(p
);
147 /* alloc large array */
149 info
->dir_in
= kcalloc(num
, sizeof(*info
->dir_in
) +
150 sizeof(*info
->dir_dname
) +
151 sizeof(*info
->dir_dname_len
) +
152 sizeof(*info
->dir_dlease
),
154 if (info
->dir_in
== NULL
) {
158 info
->dir_dname
= (void *)(info
->dir_in
+ num
);
159 info
->dir_dname_len
= (void *)(info
->dir_dname
+ num
);
160 info
->dir_dlease
= (void *)(info
->dir_dname_len
+ num
);
164 ceph_decode_need(p
, end
, sizeof(u32
)*2, bad
);
165 info
->dir_dname_len
[i
] = ceph_decode_32(p
);
166 ceph_decode_need(p
, end
, info
->dir_dname_len
[i
], bad
);
167 info
->dir_dname
[i
] = *p
;
168 *p
+= info
->dir_dname_len
[i
];
169 dout("parsed dir dname '%.*s'\n", info
->dir_dname_len
[i
],
171 info
->dir_dlease
[i
] = *p
;
172 *p
+= sizeof(struct ceph_mds_reply_lease
);
175 err
= parse_reply_info_in(p
, end
, &info
->dir_in
[i
]);
190 pr_err("problem parsing dir contents %d\n", err
);
195 * parse entire mds reply
197 static int parse_reply_info(struct ceph_msg
*msg
,
198 struct ceph_mds_reply_info_parsed
*info
)
204 info
->head
= msg
->front
.iov_base
;
205 p
= msg
->front
.iov_base
+ sizeof(struct ceph_mds_reply_head
);
206 end
= p
+ msg
->front
.iov_len
- sizeof(struct ceph_mds_reply_head
);
209 ceph_decode_32_safe(&p
, end
, len
, bad
);
211 err
= parse_reply_info_trace(&p
, p
+len
, info
);
217 ceph_decode_32_safe(&p
, end
, len
, bad
);
219 err
= parse_reply_info_dir(&p
, p
+len
, info
);
225 ceph_decode_32_safe(&p
, end
, len
, bad
);
226 info
->snapblob_len
= len
;
237 pr_err("mds parse_reply err %d\n", err
);
241 static void destroy_reply_info(struct ceph_mds_reply_info_parsed
*info
)
250 static const char *session_state_name(int s
)
253 case CEPH_MDS_SESSION_NEW
: return "new";
254 case CEPH_MDS_SESSION_OPENING
: return "opening";
255 case CEPH_MDS_SESSION_OPEN
: return "open";
256 case CEPH_MDS_SESSION_HUNG
: return "hung";
257 case CEPH_MDS_SESSION_CLOSING
: return "closing";
258 case CEPH_MDS_SESSION_RESTARTING
: return "restarting";
259 case CEPH_MDS_SESSION_RECONNECTING
: return "reconnecting";
260 default: return "???";
264 static struct ceph_mds_session
*get_session(struct ceph_mds_session
*s
)
266 if (atomic_inc_not_zero(&s
->s_ref
)) {
267 dout("mdsc get_session %p %d -> %d\n", s
,
268 atomic_read(&s
->s_ref
)-1, atomic_read(&s
->s_ref
));
271 dout("mdsc get_session %p 0 -- FAIL", s
);
276 void ceph_put_mds_session(struct ceph_mds_session
*s
)
278 dout("mdsc put_session %p %d -> %d\n", s
,
279 atomic_read(&s
->s_ref
), atomic_read(&s
->s_ref
)-1);
280 if (atomic_dec_and_test(&s
->s_ref
)) {
282 s
->s_mdsc
->client
->monc
.auth
->ops
->destroy_authorizer(
283 s
->s_mdsc
->client
->monc
.auth
, s
->s_authorizer
);
289 * called under mdsc->mutex
291 struct ceph_mds_session
*__ceph_lookup_mds_session(struct ceph_mds_client
*mdsc
,
294 struct ceph_mds_session
*session
;
296 if (mds
>= mdsc
->max_sessions
|| mdsc
->sessions
[mds
] == NULL
)
298 session
= mdsc
->sessions
[mds
];
299 dout("lookup_mds_session %p %d\n", session
,
300 atomic_read(&session
->s_ref
));
301 get_session(session
);
305 static bool __have_session(struct ceph_mds_client
*mdsc
, int mds
)
307 if (mds
>= mdsc
->max_sessions
)
309 return mdsc
->sessions
[mds
];
313 * create+register a new session for given mds.
314 * called under mdsc->mutex.
316 static struct ceph_mds_session
*register_session(struct ceph_mds_client
*mdsc
,
319 struct ceph_mds_session
*s
;
321 s
= kzalloc(sizeof(*s
), GFP_NOFS
);
324 s
->s_state
= CEPH_MDS_SESSION_NEW
;
327 mutex_init(&s
->s_mutex
);
329 ceph_con_init(mdsc
->client
->msgr
, &s
->s_con
);
330 s
->s_con
.private = s
;
331 s
->s_con
.ops
= &mds_con_ops
;
332 s
->s_con
.peer_name
.type
= CEPH_ENTITY_TYPE_MDS
;
333 s
->s_con
.peer_name
.num
= cpu_to_le64(mds
);
335 spin_lock_init(&s
->s_cap_lock
);
338 s
->s_renew_requested
= 0;
340 INIT_LIST_HEAD(&s
->s_caps
);
343 atomic_set(&s
->s_ref
, 1);
344 INIT_LIST_HEAD(&s
->s_waiting
);
345 INIT_LIST_HEAD(&s
->s_unsafe
);
346 s
->s_num_cap_releases
= 0;
347 s
->s_iterating_caps
= false;
348 INIT_LIST_HEAD(&s
->s_cap_releases
);
349 INIT_LIST_HEAD(&s
->s_cap_releases_done
);
350 INIT_LIST_HEAD(&s
->s_cap_flushing
);
351 INIT_LIST_HEAD(&s
->s_cap_snaps_flushing
);
353 dout("register_session mds%d\n", mds
);
354 if (mds
>= mdsc
->max_sessions
) {
355 int newmax
= 1 << get_count_order(mds
+1);
356 struct ceph_mds_session
**sa
;
358 dout("register_session realloc to %d\n", newmax
);
359 sa
= kcalloc(newmax
, sizeof(void *), GFP_NOFS
);
362 if (mdsc
->sessions
) {
363 memcpy(sa
, mdsc
->sessions
,
364 mdsc
->max_sessions
* sizeof(void *));
365 kfree(mdsc
->sessions
);
368 mdsc
->max_sessions
= newmax
;
370 mdsc
->sessions
[mds
] = s
;
371 atomic_inc(&s
->s_ref
); /* one ref to sessions[], one to caller */
373 ceph_con_open(&s
->s_con
, ceph_mdsmap_get_addr(mdsc
->mdsmap
, mds
));
379 return ERR_PTR(-ENOMEM
);
383 * called under mdsc->mutex
385 static void unregister_session(struct ceph_mds_client
*mdsc
,
386 struct ceph_mds_session
*s
)
388 dout("unregister_session mds%d %p\n", s
->s_mds
, s
);
389 mdsc
->sessions
[s
->s_mds
] = NULL
;
390 ceph_con_close(&s
->s_con
);
391 ceph_put_mds_session(s
);
395 * drop session refs in request.
397 * should be last request ref, or hold mdsc->mutex
399 static void put_request_session(struct ceph_mds_request
*req
)
401 if (req
->r_session
) {
402 ceph_put_mds_session(req
->r_session
);
403 req
->r_session
= NULL
;
407 void ceph_mdsc_release_request(struct kref
*kref
)
409 struct ceph_mds_request
*req
= container_of(kref
,
410 struct ceph_mds_request
,
413 ceph_msg_put(req
->r_request
);
415 ceph_msg_put(req
->r_reply
);
416 destroy_reply_info(&req
->r_reply_info
);
419 ceph_put_cap_refs(ceph_inode(req
->r_inode
),
423 if (req
->r_locked_dir
)
424 ceph_put_cap_refs(ceph_inode(req
->r_locked_dir
),
426 if (req
->r_target_inode
)
427 iput(req
->r_target_inode
);
430 if (req
->r_old_dentry
) {
432 ceph_inode(req
->r_old_dentry
->d_parent
->d_inode
),
434 dput(req
->r_old_dentry
);
438 put_request_session(req
);
439 ceph_unreserve_caps(&req
->r_caps_reservation
);
444 * lookup session, bump ref if found.
446 * called under mdsc->mutex.
448 static struct ceph_mds_request
*__lookup_request(struct ceph_mds_client
*mdsc
,
451 struct ceph_mds_request
*req
;
452 struct rb_node
*n
= mdsc
->request_tree
.rb_node
;
455 req
= rb_entry(n
, struct ceph_mds_request
, r_node
);
456 if (tid
< req
->r_tid
)
458 else if (tid
> req
->r_tid
)
461 ceph_mdsc_get_request(req
);
468 static void __insert_request(struct ceph_mds_client
*mdsc
,
469 struct ceph_mds_request
*new)
471 struct rb_node
**p
= &mdsc
->request_tree
.rb_node
;
472 struct rb_node
*parent
= NULL
;
473 struct ceph_mds_request
*req
= NULL
;
477 req
= rb_entry(parent
, struct ceph_mds_request
, r_node
);
478 if (new->r_tid
< req
->r_tid
)
480 else if (new->r_tid
> req
->r_tid
)
486 rb_link_node(&new->r_node
, parent
, p
);
487 rb_insert_color(&new->r_node
, &mdsc
->request_tree
);
491 * Register an in-flight request, and assign a tid. Link to directory
492 * are modifying (if any).
494 * Called under mdsc->mutex.
496 static void __register_request(struct ceph_mds_client
*mdsc
,
497 struct ceph_mds_request
*req
,
500 req
->r_tid
= ++mdsc
->last_tid
;
502 ceph_reserve_caps(&req
->r_caps_reservation
, req
->r_num_caps
);
503 dout("__register_request %p tid %lld\n", req
, req
->r_tid
);
504 ceph_mdsc_get_request(req
);
505 __insert_request(mdsc
, req
);
508 struct ceph_inode_info
*ci
= ceph_inode(dir
);
510 spin_lock(&ci
->i_unsafe_lock
);
511 req
->r_unsafe_dir
= dir
;
512 list_add_tail(&req
->r_unsafe_dir_item
, &ci
->i_unsafe_dirops
);
513 spin_unlock(&ci
->i_unsafe_lock
);
517 static void __unregister_request(struct ceph_mds_client
*mdsc
,
518 struct ceph_mds_request
*req
)
520 dout("__unregister_request %p tid %lld\n", req
, req
->r_tid
);
521 rb_erase(&req
->r_node
, &mdsc
->request_tree
);
522 ceph_mdsc_put_request(req
);
524 if (req
->r_unsafe_dir
) {
525 struct ceph_inode_info
*ci
= ceph_inode(req
->r_unsafe_dir
);
527 spin_lock(&ci
->i_unsafe_lock
);
528 list_del_init(&req
->r_unsafe_dir_item
);
529 spin_unlock(&ci
->i_unsafe_lock
);
534 * Choose mds to send request to next. If there is a hint set in the
535 * request (e.g., due to a prior forward hint from the mds), use that.
536 * Otherwise, consult frag tree and/or caps to identify the
537 * appropriate mds. If all else fails, choose randomly.
539 * Called under mdsc->mutex.
541 static int __choose_mds(struct ceph_mds_client
*mdsc
,
542 struct ceph_mds_request
*req
)
545 struct ceph_inode_info
*ci
;
546 struct ceph_cap
*cap
;
547 int mode
= req
->r_direct_mode
;
549 u32 hash
= req
->r_direct_hash
;
550 bool is_hash
= req
->r_direct_is_hash
;
553 * is there a specific mds we should try? ignore hint if we have
554 * no session and the mds is not up (active or recovering).
556 if (req
->r_resend_mds
>= 0 &&
557 (__have_session(mdsc
, req
->r_resend_mds
) ||
558 ceph_mdsmap_get_state(mdsc
->mdsmap
, req
->r_resend_mds
) > 0)) {
559 dout("choose_mds using resend_mds mds%d\n",
561 return req
->r_resend_mds
;
564 if (mode
== USE_RANDOM_MDS
)
569 inode
= req
->r_inode
;
570 } else if (req
->r_dentry
) {
571 if (req
->r_dentry
->d_inode
) {
572 inode
= req
->r_dentry
->d_inode
;
574 inode
= req
->r_dentry
->d_parent
->d_inode
;
575 hash
= req
->r_dentry
->d_name
.hash
;
579 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode
, (int)is_hash
,
583 ci
= ceph_inode(inode
);
585 if (is_hash
&& S_ISDIR(inode
->i_mode
)) {
586 struct ceph_inode_frag frag
;
589 ceph_choose_frag(ci
, hash
, &frag
, &found
);
591 if (mode
== USE_ANY_MDS
&& frag
.ndist
> 0) {
594 /* choose a random replica */
595 get_random_bytes(&r
, 1);
598 dout("choose_mds %p %llx.%llx "
599 "frag %u mds%d (%d/%d)\n",
600 inode
, ceph_vinop(inode
),
606 /* since this file/dir wasn't known to be
607 * replicated, then we want to look for the
608 * authoritative mds. */
611 /* choose auth mds */
613 dout("choose_mds %p %llx.%llx "
614 "frag %u mds%d (auth)\n",
615 inode
, ceph_vinop(inode
), frag
.frag
, mds
);
621 spin_lock(&inode
->i_lock
);
623 if (mode
== USE_AUTH_MDS
)
624 cap
= ci
->i_auth_cap
;
625 if (!cap
&& !RB_EMPTY_ROOT(&ci
->i_caps
))
626 cap
= rb_entry(rb_first(&ci
->i_caps
), struct ceph_cap
, ci_node
);
628 spin_unlock(&inode
->i_lock
);
631 mds
= cap
->session
->s_mds
;
632 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
633 inode
, ceph_vinop(inode
), mds
,
634 cap
== ci
->i_auth_cap
? "auth " : "", cap
);
635 spin_unlock(&inode
->i_lock
);
639 mds
= ceph_mdsmap_get_random_mds(mdsc
->mdsmap
);
640 dout("choose_mds chose random mds%d\n", mds
);
648 static struct ceph_msg
*create_session_msg(u32 op
, u64 seq
)
650 struct ceph_msg
*msg
;
651 struct ceph_mds_session_head
*h
;
653 msg
= ceph_msg_new(CEPH_MSG_CLIENT_SESSION
, sizeof(*h
), 0, 0, NULL
);
655 pr_err("create_session_msg ENOMEM creating msg\n");
656 return ERR_PTR(PTR_ERR(msg
));
658 h
= msg
->front
.iov_base
;
659 h
->op
= cpu_to_le32(op
);
660 h
->seq
= cpu_to_le64(seq
);
665 * send session open request.
667 * called under mdsc->mutex
669 static int __open_session(struct ceph_mds_client
*mdsc
,
670 struct ceph_mds_session
*session
)
672 struct ceph_msg
*msg
;
674 int mds
= session
->s_mds
;
677 /* wait for mds to go active? */
678 mstate
= ceph_mdsmap_get_state(mdsc
->mdsmap
, mds
);
679 dout("open_session to mds%d (%s)\n", mds
,
680 ceph_mds_state_name(mstate
));
681 session
->s_state
= CEPH_MDS_SESSION_OPENING
;
682 session
->s_renew_requested
= jiffies
;
684 /* send connect message */
685 msg
= create_session_msg(CEPH_SESSION_REQUEST_OPEN
, session
->s_seq
);
690 ceph_con_send(&session
->s_con
, msg
);
701 * Free preallocated cap messages assigned to this session
703 static void cleanup_cap_releases(struct ceph_mds_session
*session
)
705 struct ceph_msg
*msg
;
707 spin_lock(&session
->s_cap_lock
);
708 while (!list_empty(&session
->s_cap_releases
)) {
709 msg
= list_first_entry(&session
->s_cap_releases
,
710 struct ceph_msg
, list_head
);
711 list_del_init(&msg
->list_head
);
714 while (!list_empty(&session
->s_cap_releases_done
)) {
715 msg
= list_first_entry(&session
->s_cap_releases_done
,
716 struct ceph_msg
, list_head
);
717 list_del_init(&msg
->list_head
);
720 spin_unlock(&session
->s_cap_lock
);
724 * Helper to safely iterate over all caps associated with a session.
726 * caller must hold session s_mutex
728 static int iterate_session_caps(struct ceph_mds_session
*session
,
729 int (*cb
)(struct inode
*, struct ceph_cap
*,
732 struct ceph_cap
*cap
, *ncap
;
736 dout("iterate_session_caps %p mds%d\n", session
, session
->s_mds
);
737 spin_lock(&session
->s_cap_lock
);
738 session
->s_iterating_caps
= true;
739 list_for_each_entry_safe(cap
, ncap
, &session
->s_caps
, session_caps
) {
740 inode
= igrab(&cap
->ci
->vfs_inode
);
743 spin_unlock(&session
->s_cap_lock
);
744 ret
= cb(inode
, cap
, arg
);
746 spin_lock(&session
->s_cap_lock
);
752 session
->s_iterating_caps
= false;
753 spin_unlock(&session
->s_cap_lock
);
757 static int remove_session_caps_cb(struct inode
*inode
, struct ceph_cap
*cap
,
760 struct ceph_inode_info
*ci
= ceph_inode(inode
);
761 dout("removing cap %p, ci is %p, inode is %p\n",
762 cap
, ci
, &ci
->vfs_inode
);
763 ceph_remove_cap(cap
);
768 * caller must hold session s_mutex
770 static void remove_session_caps(struct ceph_mds_session
*session
)
772 dout("remove_session_caps on %p\n", session
);
773 iterate_session_caps(session
, remove_session_caps_cb
, NULL
);
774 BUG_ON(session
->s_nr_caps
> 0);
775 cleanup_cap_releases(session
);
779 * wake up any threads waiting on this session's caps. if the cap is
780 * old (didn't get renewed on the client reconnect), remove it now.
782 * caller must hold s_mutex.
784 static int wake_up_session_cb(struct inode
*inode
, struct ceph_cap
*cap
,
787 struct ceph_inode_info
*ci
= ceph_inode(inode
);
789 wake_up(&ci
->i_cap_wq
);
791 spin_lock(&inode
->i_lock
);
792 ci
->i_wanted_max_size
= 0;
793 ci
->i_requested_max_size
= 0;
794 spin_unlock(&inode
->i_lock
);
799 static void wake_up_session_caps(struct ceph_mds_session
*session
,
802 dout("wake_up_session_caps %p mds%d\n", session
, session
->s_mds
);
803 iterate_session_caps(session
, wake_up_session_cb
,
804 (void *)(unsigned long)reconnect
);
808 * Send periodic message to MDS renewing all currently held caps. The
809 * ack will reset the expiration for all caps from this session.
811 * caller holds s_mutex
813 static int send_renew_caps(struct ceph_mds_client
*mdsc
,
814 struct ceph_mds_session
*session
)
816 struct ceph_msg
*msg
;
819 if (time_after_eq(jiffies
, session
->s_cap_ttl
) &&
820 time_after_eq(session
->s_cap_ttl
, session
->s_renew_requested
))
821 pr_info("mds%d caps stale\n", session
->s_mds
);
823 /* do not try to renew caps until a recovering mds has reconnected
824 * with its clients. */
825 state
= ceph_mdsmap_get_state(mdsc
->mdsmap
, session
->s_mds
);
826 if (state
< CEPH_MDS_STATE_RECONNECT
) {
827 dout("send_renew_caps ignoring mds%d (%s)\n",
828 session
->s_mds
, ceph_mds_state_name(state
));
832 dout("send_renew_caps to mds%d (%s)\n", session
->s_mds
,
833 ceph_mds_state_name(state
));
834 session
->s_renew_requested
= jiffies
;
835 msg
= create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS
,
836 ++session
->s_renew_seq
);
839 ceph_con_send(&session
->s_con
, msg
);
844 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
846 * Called under session->s_mutex
848 static void renewed_caps(struct ceph_mds_client
*mdsc
,
849 struct ceph_mds_session
*session
, int is_renew
)
854 spin_lock(&session
->s_cap_lock
);
855 was_stale
= is_renew
&& (session
->s_cap_ttl
== 0 ||
856 time_after_eq(jiffies
, session
->s_cap_ttl
));
858 session
->s_cap_ttl
= session
->s_renew_requested
+
859 mdsc
->mdsmap
->m_session_timeout
*HZ
;
862 if (time_before(jiffies
, session
->s_cap_ttl
)) {
863 pr_info("mds%d caps renewed\n", session
->s_mds
);
866 pr_info("mds%d caps still stale\n", session
->s_mds
);
869 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
870 session
->s_mds
, session
->s_cap_ttl
, was_stale
? "stale" : "fresh",
871 time_before(jiffies
, session
->s_cap_ttl
) ? "stale" : "fresh");
872 spin_unlock(&session
->s_cap_lock
);
875 wake_up_session_caps(session
, 0);
879 * send a session close request
881 static int request_close_session(struct ceph_mds_client
*mdsc
,
882 struct ceph_mds_session
*session
)
884 struct ceph_msg
*msg
;
887 dout("request_close_session mds%d state %s seq %lld\n",
888 session
->s_mds
, session_state_name(session
->s_state
),
890 msg
= create_session_msg(CEPH_SESSION_REQUEST_CLOSE
, session
->s_seq
);
894 ceph_con_send(&session
->s_con
, msg
);
899 * Called with s_mutex held.
901 static int __close_session(struct ceph_mds_client
*mdsc
,
902 struct ceph_mds_session
*session
)
904 if (session
->s_state
>= CEPH_MDS_SESSION_CLOSING
)
906 session
->s_state
= CEPH_MDS_SESSION_CLOSING
;
907 return request_close_session(mdsc
, session
);
913 * Because we can't cache an inode without one or more caps, we do
914 * this indirectly: if a cap is unused, we prune its aliases, at which
915 * point the inode will hopefully get dropped to.
917 * Yes, this is a bit sloppy. Our only real goal here is to respond to
918 * memory pressure from the MDS, though, so it needn't be perfect.
920 static int trim_caps_cb(struct inode
*inode
, struct ceph_cap
*cap
, void *arg
)
922 struct ceph_mds_session
*session
= arg
;
923 struct ceph_inode_info
*ci
= ceph_inode(inode
);
924 int used
, oissued
, mine
;
926 if (session
->s_trim_caps
<= 0)
929 spin_lock(&inode
->i_lock
);
930 mine
= cap
->issued
| cap
->implemented
;
931 used
= __ceph_caps_used(ci
);
932 oissued
= __ceph_caps_issued_other(ci
, cap
);
934 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n",
935 inode
, cap
, ceph_cap_string(mine
), ceph_cap_string(oissued
),
936 ceph_cap_string(used
));
937 if (ci
->i_dirty_caps
)
938 goto out
; /* dirty caps */
939 if ((used
& ~oissued
) & mine
)
940 goto out
; /* we need these caps */
942 session
->s_trim_caps
--;
944 /* we aren't the only cap.. just remove us */
945 __ceph_remove_cap(cap
, NULL
);
947 /* try to drop referring dentries */
948 spin_unlock(&inode
->i_lock
);
949 d_prune_aliases(inode
);
950 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
951 inode
, cap
, atomic_read(&inode
->i_count
));
956 spin_unlock(&inode
->i_lock
);
961 * Trim session cap count down to some max number.
963 static int trim_caps(struct ceph_mds_client
*mdsc
,
964 struct ceph_mds_session
*session
,
967 int trim_caps
= session
->s_nr_caps
- max_caps
;
969 dout("trim_caps mds%d start: %d / %d, trim %d\n",
970 session
->s_mds
, session
->s_nr_caps
, max_caps
, trim_caps
);
972 session
->s_trim_caps
= trim_caps
;
973 iterate_session_caps(session
, trim_caps_cb
, session
);
974 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
975 session
->s_mds
, session
->s_nr_caps
, max_caps
,
976 trim_caps
- session
->s_trim_caps
);
977 session
->s_trim_caps
= 0;
983 * Allocate cap_release messages. If there is a partially full message
984 * in the queue, try to allocate enough to cover it's remainder, so that
985 * we can send it immediately.
987 * Called under s_mutex.
989 static int add_cap_releases(struct ceph_mds_client
*mdsc
,
990 struct ceph_mds_session
*session
,
993 struct ceph_msg
*msg
;
994 struct ceph_mds_cap_release
*head
;
998 extra
= mdsc
->client
->mount_args
->cap_release_safety
;
1000 spin_lock(&session
->s_cap_lock
);
1002 if (!list_empty(&session
->s_cap_releases
)) {
1003 msg
= list_first_entry(&session
->s_cap_releases
,
1006 head
= msg
->front
.iov_base
;
1007 extra
+= CEPH_CAPS_PER_RELEASE
- le32_to_cpu(head
->num
);
1010 while (session
->s_num_cap_releases
< session
->s_nr_caps
+ extra
) {
1011 spin_unlock(&session
->s_cap_lock
);
1012 msg
= ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE
, PAGE_CACHE_SIZE
,
1016 dout("add_cap_releases %p msg %p now %d\n", session
, msg
,
1017 (int)msg
->front
.iov_len
);
1018 head
= msg
->front
.iov_base
;
1019 head
->num
= cpu_to_le32(0);
1020 msg
->front
.iov_len
= sizeof(*head
);
1021 spin_lock(&session
->s_cap_lock
);
1022 list_add(&msg
->list_head
, &session
->s_cap_releases
);
1023 session
->s_num_cap_releases
+= CEPH_CAPS_PER_RELEASE
;
1026 if (!list_empty(&session
->s_cap_releases
)) {
1027 msg
= list_first_entry(&session
->s_cap_releases
,
1030 head
= msg
->front
.iov_base
;
1032 dout(" queueing non-full %p (%d)\n", msg
,
1033 le32_to_cpu(head
->num
));
1034 list_move_tail(&msg
->list_head
,
1035 &session
->s_cap_releases_done
);
1036 session
->s_num_cap_releases
-=
1037 CEPH_CAPS_PER_RELEASE
- le32_to_cpu(head
->num
);
1041 spin_unlock(&session
->s_cap_lock
);
1047 * flush all dirty inode data to disk.
1049 * returns true if we've flushed through want_flush_seq
1051 static int check_cap_flush(struct ceph_mds_client
*mdsc
, u64 want_flush_seq
)
1055 dout("check_cap_flush want %lld\n", want_flush_seq
);
1056 mutex_lock(&mdsc
->mutex
);
1057 for (mds
= 0; ret
&& mds
< mdsc
->max_sessions
; mds
++) {
1058 struct ceph_mds_session
*session
= mdsc
->sessions
[mds
];
1062 get_session(session
);
1063 mutex_unlock(&mdsc
->mutex
);
1065 mutex_lock(&session
->s_mutex
);
1066 if (!list_empty(&session
->s_cap_flushing
)) {
1067 struct ceph_inode_info
*ci
=
1068 list_entry(session
->s_cap_flushing
.next
,
1069 struct ceph_inode_info
,
1071 struct inode
*inode
= &ci
->vfs_inode
;
1073 spin_lock(&inode
->i_lock
);
1074 if (ci
->i_cap_flush_seq
<= want_flush_seq
) {
1075 dout("check_cap_flush still flushing %p "
1076 "seq %lld <= %lld to mds%d\n", inode
,
1077 ci
->i_cap_flush_seq
, want_flush_seq
,
1081 spin_unlock(&inode
->i_lock
);
1083 mutex_unlock(&session
->s_mutex
);
1084 ceph_put_mds_session(session
);
1088 mutex_lock(&mdsc
->mutex
);
1091 mutex_unlock(&mdsc
->mutex
);
1092 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq
);
1097 * called under s_mutex
1099 static void send_cap_releases(struct ceph_mds_client
*mdsc
,
1100 struct ceph_mds_session
*session
)
1102 struct ceph_msg
*msg
;
1104 dout("send_cap_releases mds%d\n", session
->s_mds
);
1106 spin_lock(&session
->s_cap_lock
);
1107 if (list_empty(&session
->s_cap_releases_done
))
1109 msg
= list_first_entry(&session
->s_cap_releases_done
,
1110 struct ceph_msg
, list_head
);
1111 list_del_init(&msg
->list_head
);
1112 spin_unlock(&session
->s_cap_lock
);
1113 msg
->hdr
.front_len
= cpu_to_le32(msg
->front
.iov_len
);
1114 dout("send_cap_releases mds%d %p\n", session
->s_mds
, msg
);
1115 ceph_con_send(&session
->s_con
, msg
);
1117 spin_unlock(&session
->s_cap_lock
);
1125 * Create an mds request.
1127 struct ceph_mds_request
*
1128 ceph_mdsc_create_request(struct ceph_mds_client
*mdsc
, int op
, int mode
)
1130 struct ceph_mds_request
*req
= kzalloc(sizeof(*req
), GFP_NOFS
);
1133 return ERR_PTR(-ENOMEM
);
1135 req
->r_started
= jiffies
;
1136 req
->r_resend_mds
= -1;
1137 INIT_LIST_HEAD(&req
->r_unsafe_dir_item
);
1139 kref_init(&req
->r_kref
);
1140 INIT_LIST_HEAD(&req
->r_wait
);
1141 init_completion(&req
->r_completion
);
1142 init_completion(&req
->r_safe_completion
);
1143 INIT_LIST_HEAD(&req
->r_unsafe_item
);
1146 req
->r_direct_mode
= mode
;
1151 * return oldest (lowest) request, tid in request tree, 0 if none.
1153 * called under mdsc->mutex.
1155 static struct ceph_mds_request
*__get_oldest_req(struct ceph_mds_client
*mdsc
)
1157 if (RB_EMPTY_ROOT(&mdsc
->request_tree
))
1159 return rb_entry(rb_first(&mdsc
->request_tree
),
1160 struct ceph_mds_request
, r_node
);
1163 static u64
__get_oldest_tid(struct ceph_mds_client
*mdsc
)
1165 struct ceph_mds_request
*req
= __get_oldest_req(mdsc
);
1173 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1174 * on build_path_from_dentry in fs/cifs/dir.c.
1176 * If @stop_on_nosnap, generate path relative to the first non-snapped
1179 * Encode hidden .snap dirs as a double /, i.e.
1180 * foo/.snap/bar -> foo//bar
1182 char *ceph_mdsc_build_path(struct dentry
*dentry
, int *plen
, u64
*base
,
1185 struct dentry
*temp
;
1190 return ERR_PTR(-EINVAL
);
1194 for (temp
= dentry
; !IS_ROOT(temp
);) {
1195 struct inode
*inode
= temp
->d_inode
;
1196 if (inode
&& ceph_snap(inode
) == CEPH_SNAPDIR
)
1197 len
++; /* slash only */
1198 else if (stop_on_nosnap
&& inode
&&
1199 ceph_snap(inode
) == CEPH_NOSNAP
)
1202 len
+= 1 + temp
->d_name
.len
;
1203 temp
= temp
->d_parent
;
1205 pr_err("build_path_dentry corrupt dentry %p\n", dentry
);
1206 return ERR_PTR(-EINVAL
);
1210 len
--; /* no leading '/' */
1212 path
= kmalloc(len
+1, GFP_NOFS
);
1214 return ERR_PTR(-ENOMEM
);
1216 path
[pos
] = 0; /* trailing null */
1217 for (temp
= dentry
; !IS_ROOT(temp
) && pos
!= 0; ) {
1218 struct inode
*inode
= temp
->d_inode
;
1220 if (inode
&& ceph_snap(inode
) == CEPH_SNAPDIR
) {
1221 dout("build_path_dentry path+%d: %p SNAPDIR\n",
1223 } else if (stop_on_nosnap
&& inode
&&
1224 ceph_snap(inode
) == CEPH_NOSNAP
) {
1227 pos
-= temp
->d_name
.len
;
1230 strncpy(path
+ pos
, temp
->d_name
.name
,
1232 dout("build_path_dentry path+%d: %p '%.*s'\n",
1233 pos
, temp
, temp
->d_name
.len
, path
+ pos
);
1237 temp
= temp
->d_parent
;
1239 pr_err("build_path_dentry corrupt dentry\n");
1241 return ERR_PTR(-EINVAL
);
1245 pr_err("build_path_dentry did not end path lookup where "
1246 "expected, namelen is %d, pos is %d\n", len
, pos
);
1247 /* presumably this is only possible if racing with a
1248 rename of one of the parent directories (we can not
1249 lock the dentries above us to prevent this, but
1250 retrying should be harmless) */
1255 *base
= ceph_ino(temp
->d_inode
);
1257 dout("build_path_dentry on %p %d built %llx '%.*s'\n",
1258 dentry
, atomic_read(&dentry
->d_count
), *base
, len
, path
);
1262 static int build_dentry_path(struct dentry
*dentry
,
1263 const char **ppath
, int *ppathlen
, u64
*pino
,
1268 if (ceph_snap(dentry
->d_parent
->d_inode
) == CEPH_NOSNAP
) {
1269 *pino
= ceph_ino(dentry
->d_parent
->d_inode
);
1270 *ppath
= dentry
->d_name
.name
;
1271 *ppathlen
= dentry
->d_name
.len
;
1274 path
= ceph_mdsc_build_path(dentry
, ppathlen
, pino
, 1);
1276 return PTR_ERR(path
);
1282 static int build_inode_path(struct inode
*inode
,
1283 const char **ppath
, int *ppathlen
, u64
*pino
,
1286 struct dentry
*dentry
;
1289 if (ceph_snap(inode
) == CEPH_NOSNAP
) {
1290 *pino
= ceph_ino(inode
);
1294 dentry
= d_find_alias(inode
);
1295 path
= ceph_mdsc_build_path(dentry
, ppathlen
, pino
, 1);
1298 return PTR_ERR(path
);
1305 * request arguments may be specified via an inode *, a dentry *, or
1306 * an explicit ino+path.
1308 static int set_request_path_attr(struct inode
*rinode
, struct dentry
*rdentry
,
1309 const char *rpath
, u64 rino
,
1310 const char **ppath
, int *pathlen
,
1311 u64
*ino
, int *freepath
)
1316 r
= build_inode_path(rinode
, ppath
, pathlen
, ino
, freepath
);
1317 dout(" inode %p %llx.%llx\n", rinode
, ceph_ino(rinode
),
1319 } else if (rdentry
) {
1320 r
= build_dentry_path(rdentry
, ppath
, pathlen
, ino
, freepath
);
1321 dout(" dentry %p %llx/%.*s\n", rdentry
, *ino
, *pathlen
,
1326 *pathlen
= strlen(rpath
);
1327 dout(" path %.*s\n", *pathlen
, rpath
);
1334 * called under mdsc->mutex
1336 static struct ceph_msg
*create_request_message(struct ceph_mds_client
*mdsc
,
1337 struct ceph_mds_request
*req
,
1340 struct ceph_msg
*msg
;
1341 struct ceph_mds_request_head
*head
;
1342 const char *path1
= NULL
;
1343 const char *path2
= NULL
;
1344 u64 ino1
= 0, ino2
= 0;
1345 int pathlen1
= 0, pathlen2
= 0;
1346 int freepath1
= 0, freepath2
= 0;
1352 ret
= set_request_path_attr(req
->r_inode
, req
->r_dentry
,
1353 req
->r_path1
, req
->r_ino1
.ino
,
1354 &path1
, &pathlen1
, &ino1
, &freepath1
);
1360 ret
= set_request_path_attr(NULL
, req
->r_old_dentry
,
1361 req
->r_path2
, req
->r_ino2
.ino
,
1362 &path2
, &pathlen2
, &ino2
, &freepath2
);
1368 len
= sizeof(*head
) +
1369 pathlen1
+ pathlen2
+ 2*(1 + sizeof(u32
) + sizeof(u64
));
1371 /* calculate (max) length for cap releases */
1372 len
+= sizeof(struct ceph_mds_request_release
) *
1373 (!!req
->r_inode_drop
+ !!req
->r_dentry_drop
+
1374 !!req
->r_old_inode_drop
+ !!req
->r_old_dentry_drop
);
1375 if (req
->r_dentry_drop
)
1376 len
+= req
->r_dentry
->d_name
.len
;
1377 if (req
->r_old_dentry_drop
)
1378 len
+= req
->r_old_dentry
->d_name
.len
;
1380 msg
= ceph_msg_new(CEPH_MSG_CLIENT_REQUEST
, len
, 0, 0, NULL
);
1384 msg
->hdr
.tid
= cpu_to_le64(req
->r_tid
);
1386 head
= msg
->front
.iov_base
;
1387 p
= msg
->front
.iov_base
+ sizeof(*head
);
1388 end
= msg
->front
.iov_base
+ msg
->front
.iov_len
;
1390 head
->mdsmap_epoch
= cpu_to_le32(mdsc
->mdsmap
->m_epoch
);
1391 head
->op
= cpu_to_le32(req
->r_op
);
1392 head
->caller_uid
= cpu_to_le32(current_fsuid());
1393 head
->caller_gid
= cpu_to_le32(current_fsgid());
1394 head
->args
= req
->r_args
;
1396 ceph_encode_filepath(&p
, end
, ino1
, path1
);
1397 ceph_encode_filepath(&p
, end
, ino2
, path2
);
1401 if (req
->r_inode_drop
)
1402 releases
+= ceph_encode_inode_release(&p
,
1403 req
->r_inode
? req
->r_inode
: req
->r_dentry
->d_inode
,
1404 mds
, req
->r_inode_drop
, req
->r_inode_unless
, 0);
1405 if (req
->r_dentry_drop
)
1406 releases
+= ceph_encode_dentry_release(&p
, req
->r_dentry
,
1407 mds
, req
->r_dentry_drop
, req
->r_dentry_unless
);
1408 if (req
->r_old_dentry_drop
)
1409 releases
+= ceph_encode_dentry_release(&p
, req
->r_old_dentry
,
1410 mds
, req
->r_old_dentry_drop
, req
->r_old_dentry_unless
);
1411 if (req
->r_old_inode_drop
)
1412 releases
+= ceph_encode_inode_release(&p
,
1413 req
->r_old_dentry
->d_inode
,
1414 mds
, req
->r_old_inode_drop
, req
->r_old_inode_unless
, 0);
1415 head
->num_releases
= cpu_to_le16(releases
);
1418 msg
->front
.iov_len
= p
- msg
->front
.iov_base
;
1419 msg
->hdr
.front_len
= cpu_to_le32(msg
->front
.iov_len
);
1421 msg
->pages
= req
->r_pages
;
1422 msg
->nr_pages
= req
->r_num_pages
;
1423 msg
->hdr
.data_len
= cpu_to_le32(req
->r_data_len
);
1424 msg
->hdr
.data_off
= cpu_to_le16(0);
1428 kfree((char *)path2
);
1431 kfree((char *)path1
);
1437 * called under mdsc->mutex if error, under no mutex if
1440 static void complete_request(struct ceph_mds_client
*mdsc
,
1441 struct ceph_mds_request
*req
)
1443 if (req
->r_callback
)
1444 req
->r_callback(mdsc
, req
);
1446 complete(&req
->r_completion
);
1450 * called under mdsc->mutex
1452 static int __prepare_send_request(struct ceph_mds_client
*mdsc
,
1453 struct ceph_mds_request
*req
,
1456 struct ceph_mds_request_head
*rhead
;
1457 struct ceph_msg
*msg
;
1462 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req
,
1463 req
->r_tid
, ceph_mds_op_name(req
->r_op
), req
->r_attempts
);
1465 if (req
->r_request
) {
1466 ceph_msg_put(req
->r_request
);
1467 req
->r_request
= NULL
;
1469 msg
= create_request_message(mdsc
, req
, mds
);
1471 req
->r_reply
= ERR_PTR(PTR_ERR(msg
));
1472 complete_request(mdsc
, req
);
1473 return -PTR_ERR(msg
);
1475 req
->r_request
= msg
;
1477 rhead
= msg
->front
.iov_base
;
1478 rhead
->oldest_client_tid
= cpu_to_le64(__get_oldest_tid(mdsc
));
1479 if (req
->r_got_unsafe
)
1480 flags
|= CEPH_MDS_FLAG_REPLAY
;
1481 if (req
->r_locked_dir
)
1482 flags
|= CEPH_MDS_FLAG_WANT_DENTRY
;
1483 rhead
->flags
= cpu_to_le32(flags
);
1484 rhead
->num_fwd
= req
->r_num_fwd
;
1485 rhead
->num_retry
= req
->r_attempts
- 1;
1487 dout(" r_locked_dir = %p\n", req
->r_locked_dir
);
1489 if (req
->r_target_inode
&& req
->r_got_unsafe
)
1490 rhead
->ino
= cpu_to_le64(ceph_ino(req
->r_target_inode
));
1497 * send request, or put it on the appropriate wait list.
1499 static int __do_request(struct ceph_mds_client
*mdsc
,
1500 struct ceph_mds_request
*req
)
1502 struct ceph_mds_session
*session
= NULL
;
1509 if (req
->r_timeout
&&
1510 time_after_eq(jiffies
, req
->r_started
+ req
->r_timeout
)) {
1511 dout("do_request timed out\n");
1516 mds
= __choose_mds(mdsc
, req
);
1518 ceph_mdsmap_get_state(mdsc
->mdsmap
, mds
) < CEPH_MDS_STATE_ACTIVE
) {
1519 dout("do_request no mds or not active, waiting for map\n");
1520 list_add(&req
->r_wait
, &mdsc
->waiting_for_map
);
1524 /* get, open session */
1525 session
= __ceph_lookup_mds_session(mdsc
, mds
);
1527 session
= register_session(mdsc
, mds
);
1528 dout("do_request mds%d session %p state %s\n", mds
, session
,
1529 session_state_name(session
->s_state
));
1530 if (session
->s_state
!= CEPH_MDS_SESSION_OPEN
&&
1531 session
->s_state
!= CEPH_MDS_SESSION_HUNG
) {
1532 if (session
->s_state
== CEPH_MDS_SESSION_NEW
||
1533 session
->s_state
== CEPH_MDS_SESSION_CLOSING
)
1534 __open_session(mdsc
, session
);
1535 list_add(&req
->r_wait
, &session
->s_waiting
);
1540 req
->r_session
= get_session(session
);
1541 req
->r_resend_mds
= -1; /* forget any previous mds hint */
1543 if (req
->r_request_started
== 0) /* note request start time */
1544 req
->r_request_started
= jiffies
;
1546 err
= __prepare_send_request(mdsc
, req
, mds
);
1548 ceph_msg_get(req
->r_request
);
1549 ceph_con_send(&session
->s_con
, req
->r_request
);
1553 ceph_put_mds_session(session
);
1558 req
->r_reply
= ERR_PTR(err
);
1559 complete_request(mdsc
, req
);
1564 * called under mdsc->mutex
1566 static void __wake_requests(struct ceph_mds_client
*mdsc
,
1567 struct list_head
*head
)
1569 struct ceph_mds_request
*req
, *nreq
;
1571 list_for_each_entry_safe(req
, nreq
, head
, r_wait
) {
1572 list_del_init(&req
->r_wait
);
1573 __do_request(mdsc
, req
);
1578 * Wake up threads with requests pending for @mds, so that they can
1579 * resubmit their requests to a possibly different mds. If @all is set,
1580 * wake up if their requests has been forwarded to @mds, too.
1582 static void kick_requests(struct ceph_mds_client
*mdsc
, int mds
, int all
)
1584 struct ceph_mds_request
*req
;
1587 dout("kick_requests mds%d\n", mds
);
1588 for (p
= rb_first(&mdsc
->request_tree
); p
; p
= rb_next(p
)) {
1589 req
= rb_entry(p
, struct ceph_mds_request
, r_node
);
1590 if (req
->r_got_unsafe
)
1592 if (req
->r_session
&&
1593 req
->r_session
->s_mds
== mds
) {
1594 dout(" kicking tid %llu\n", req
->r_tid
);
1595 put_request_session(req
);
1596 __do_request(mdsc
, req
);
1601 void ceph_mdsc_submit_request(struct ceph_mds_client
*mdsc
,
1602 struct ceph_mds_request
*req
)
1604 dout("submit_request on %p\n", req
);
1605 mutex_lock(&mdsc
->mutex
);
1606 __register_request(mdsc
, req
, NULL
);
1607 __do_request(mdsc
, req
);
1608 mutex_unlock(&mdsc
->mutex
);
1612 * Synchrously perform an mds request. Take care of all of the
1613 * session setup, forwarding, retry details.
1615 int ceph_mdsc_do_request(struct ceph_mds_client
*mdsc
,
1617 struct ceph_mds_request
*req
)
1621 dout("do_request on %p\n", req
);
1623 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
1625 ceph_get_cap_refs(ceph_inode(req
->r_inode
), CEPH_CAP_PIN
);
1626 if (req
->r_locked_dir
)
1627 ceph_get_cap_refs(ceph_inode(req
->r_locked_dir
), CEPH_CAP_PIN
);
1628 if (req
->r_old_dentry
)
1630 ceph_inode(req
->r_old_dentry
->d_parent
->d_inode
),
1634 mutex_lock(&mdsc
->mutex
);
1635 __register_request(mdsc
, req
, dir
);
1636 __do_request(mdsc
, req
);
1639 if (!req
->r_reply
) {
1640 mutex_unlock(&mdsc
->mutex
);
1641 if (req
->r_timeout
) {
1642 err
= (long)wait_for_completion_interruptible_timeout(
1643 &req
->r_completion
, req
->r_timeout
);
1645 req
->r_reply
= ERR_PTR(-EIO
);
1647 req
->r_reply
= ERR_PTR(err
);
1649 err
= wait_for_completion_interruptible(
1650 &req
->r_completion
);
1652 req
->r_reply
= ERR_PTR(err
);
1654 mutex_lock(&mdsc
->mutex
);
1657 if (IS_ERR(req
->r_reply
)) {
1658 err
= PTR_ERR(req
->r_reply
);
1659 req
->r_reply
= NULL
;
1661 if (err
== -ERESTARTSYS
) {
1663 req
->r_aborted
= true;
1665 if (req
->r_locked_dir
&&
1666 (req
->r_op
& CEPH_MDS_OP_WRITE
)) {
1667 struct ceph_inode_info
*ci
=
1668 ceph_inode(req
->r_locked_dir
);
1670 dout("aborted, clearing I_COMPLETE on %p\n",
1672 spin_lock(&req
->r_locked_dir
->i_lock
);
1673 ci
->i_ceph_flags
&= ~CEPH_I_COMPLETE
;
1674 ci
->i_release_count
++;
1675 spin_unlock(&req
->r_locked_dir
->i_lock
);
1678 /* clean up this request */
1679 __unregister_request(mdsc
, req
);
1680 if (!list_empty(&req
->r_unsafe_item
))
1681 list_del_init(&req
->r_unsafe_item
);
1682 complete(&req
->r_safe_completion
);
1684 } else if (req
->r_err
) {
1687 err
= le32_to_cpu(req
->r_reply_info
.head
->result
);
1689 mutex_unlock(&mdsc
->mutex
);
1691 dout("do_request %p done, result %d\n", req
, err
);
1698 * We take the session mutex and parse and process the reply immediately.
1699 * This preserves the logical ordering of replies, capabilities, etc., sent
1700 * by the MDS as they are applied to our local cache.
1702 static void handle_reply(struct ceph_mds_session
*session
, struct ceph_msg
*msg
)
1704 struct ceph_mds_client
*mdsc
= session
->s_mdsc
;
1705 struct ceph_mds_request
*req
;
1706 struct ceph_mds_reply_head
*head
= msg
->front
.iov_base
;
1707 struct ceph_mds_reply_info_parsed
*rinfo
; /* parsed reply info */
1712 if (msg
->hdr
.src
.name
.type
!= CEPH_ENTITY_TYPE_MDS
)
1714 if (msg
->front
.iov_len
< sizeof(*head
)) {
1715 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
1720 /* get request, session */
1721 tid
= le64_to_cpu(msg
->hdr
.tid
);
1722 mutex_lock(&mdsc
->mutex
);
1723 req
= __lookup_request(mdsc
, tid
);
1725 dout("handle_reply on unknown tid %llu\n", tid
);
1726 mutex_unlock(&mdsc
->mutex
);
1729 dout("handle_reply %p\n", req
);
1730 mds
= le64_to_cpu(msg
->hdr
.src
.name
.num
);
1732 /* correct session? */
1733 if (!req
->r_session
&& req
->r_session
!= session
) {
1734 pr_err("mdsc_handle_reply got %llu on session mds%d"
1735 " not mds%d\n", tid
, session
->s_mds
,
1736 req
->r_session
? req
->r_session
->s_mds
: -1);
1737 mutex_unlock(&mdsc
->mutex
);
1742 if ((req
->r_got_unsafe
&& !head
->safe
) ||
1743 (req
->r_got_safe
&& head
->safe
)) {
1744 pr_warning("got a dup %s reply on %llu from mds%d\n",
1745 head
->safe
? "safe" : "unsafe", tid
, mds
);
1746 mutex_unlock(&mdsc
->mutex
);
1750 result
= le32_to_cpu(head
->result
);
1753 * Tolerate 2 consecutive ESTALEs from the same mds.
1754 * FIXME: we should be looking at the cap migrate_seq.
1756 if (result
== -ESTALE
) {
1757 req
->r_direct_mode
= USE_AUTH_MDS
;
1759 if (req
->r_num_stale
<= 2) {
1760 __do_request(mdsc
, req
);
1761 mutex_unlock(&mdsc
->mutex
);
1765 req
->r_num_stale
= 0;
1769 req
->r_got_safe
= true;
1770 __unregister_request(mdsc
, req
);
1771 complete(&req
->r_safe_completion
);
1773 if (req
->r_got_unsafe
) {
1775 * We already handled the unsafe response, now do the
1776 * cleanup. No need to examine the response; the MDS
1777 * doesn't include any result info in the safe
1778 * response. And even if it did, there is nothing
1779 * useful we could do with a revised return value.
1781 dout("got safe reply %llu, mds%d\n", tid
, mds
);
1782 list_del_init(&req
->r_unsafe_item
);
1784 /* last unsafe request during umount? */
1785 if (mdsc
->stopping
&& !__get_oldest_req(mdsc
))
1786 complete(&mdsc
->safe_umount_waiters
);
1787 mutex_unlock(&mdsc
->mutex
);
1792 BUG_ON(req
->r_reply
);
1795 req
->r_got_unsafe
= true;
1796 list_add_tail(&req
->r_unsafe_item
, &req
->r_session
->s_unsafe
);
1799 dout("handle_reply tid %lld result %d\n", tid
, result
);
1800 rinfo
= &req
->r_reply_info
;
1801 err
= parse_reply_info(msg
, rinfo
);
1802 mutex_unlock(&mdsc
->mutex
);
1804 mutex_lock(&session
->s_mutex
);
1806 pr_err("mdsc_handle_reply got corrupt reply mds%d\n", mds
);
1812 if (rinfo
->snapblob_len
) {
1813 down_write(&mdsc
->snap_rwsem
);
1814 ceph_update_snap_trace(mdsc
, rinfo
->snapblob
,
1815 rinfo
->snapblob
+ rinfo
->snapblob_len
,
1816 le32_to_cpu(head
->op
) == CEPH_MDS_OP_RMSNAP
);
1817 downgrade_write(&mdsc
->snap_rwsem
);
1819 down_read(&mdsc
->snap_rwsem
);
1822 /* insert trace into our cache */
1823 err
= ceph_fill_trace(mdsc
->client
->sb
, req
, req
->r_session
);
1825 if (result
== 0 && rinfo
->dir_nr
)
1826 ceph_readdir_prepopulate(req
, req
->r_session
);
1827 ceph_unreserve_caps(&req
->r_caps_reservation
);
1830 up_read(&mdsc
->snap_rwsem
);
1839 add_cap_releases(mdsc
, req
->r_session
, -1);
1840 mutex_unlock(&session
->s_mutex
);
1842 /* kick calling process */
1843 complete_request(mdsc
, req
);
1845 ceph_mdsc_put_request(req
);
1852 * handle mds notification that our request has been forwarded.
1854 static void handle_forward(struct ceph_mds_client
*mdsc
, struct ceph_msg
*msg
)
1856 struct ceph_mds_request
*req
;
1862 void *p
= msg
->front
.iov_base
;
1863 void *end
= p
+ msg
->front
.iov_len
;
1864 int from_mds
, state
;
1866 if (msg
->hdr
.src
.name
.type
!= CEPH_ENTITY_TYPE_MDS
)
1868 from_mds
= le64_to_cpu(msg
->hdr
.src
.name
.num
);
1870 ceph_decode_need(&p
, end
, sizeof(u64
)+2*sizeof(u32
), bad
);
1871 tid
= ceph_decode_64(&p
);
1872 next_mds
= ceph_decode_32(&p
);
1873 fwd_seq
= ceph_decode_32(&p
);
1874 must_resend
= ceph_decode_8(&p
);
1876 WARN_ON(must_resend
); /* shouldn't happen. */
1878 mutex_lock(&mdsc
->mutex
);
1879 req
= __lookup_request(mdsc
, tid
);
1881 dout("forward %llu dne\n", tid
);
1882 goto out
; /* dup reply? */
1885 state
= mdsc
->sessions
[next_mds
]->s_state
;
1886 if (fwd_seq
<= req
->r_num_fwd
) {
1887 dout("forward %llu to mds%d - old seq %d <= %d\n",
1888 tid
, next_mds
, req
->r_num_fwd
, fwd_seq
);
1890 /* resend. forward race not possible; mds would drop */
1891 dout("forward %llu to mds%d (we resend)\n", tid
, next_mds
);
1892 req
->r_num_fwd
= fwd_seq
;
1893 req
->r_resend_mds
= next_mds
;
1894 put_request_session(req
);
1895 __do_request(mdsc
, req
);
1897 ceph_mdsc_put_request(req
);
1899 mutex_unlock(&mdsc
->mutex
);
1903 pr_err("mdsc_handle_forward decode error err=%d\n", err
);
1907 * handle a mds session control message
1909 static void handle_session(struct ceph_mds_session
*session
,
1910 struct ceph_msg
*msg
)
1912 struct ceph_mds_client
*mdsc
= session
->s_mdsc
;
1916 struct ceph_mds_session_head
*h
= msg
->front
.iov_base
;
1919 if (msg
->hdr
.src
.name
.type
!= CEPH_ENTITY_TYPE_MDS
)
1921 mds
= le64_to_cpu(msg
->hdr
.src
.name
.num
);
1924 if (msg
->front
.iov_len
!= sizeof(*h
))
1926 op
= le32_to_cpu(h
->op
);
1927 seq
= le64_to_cpu(h
->seq
);
1929 mutex_lock(&mdsc
->mutex
);
1930 /* FIXME: this ttl calculation is generous */
1931 session
->s_ttl
= jiffies
+ HZ
*mdsc
->mdsmap
->m_session_autoclose
;
1932 mutex_unlock(&mdsc
->mutex
);
1934 mutex_lock(&session
->s_mutex
);
1936 dout("handle_session mds%d %s %p state %s seq %llu\n",
1937 mds
, ceph_session_op_name(op
), session
,
1938 session_state_name(session
->s_state
), seq
);
1940 if (session
->s_state
== CEPH_MDS_SESSION_HUNG
) {
1941 session
->s_state
= CEPH_MDS_SESSION_OPEN
;
1942 pr_info("mds%d came back\n", session
->s_mds
);
1946 case CEPH_SESSION_OPEN
:
1947 session
->s_state
= CEPH_MDS_SESSION_OPEN
;
1948 renewed_caps(mdsc
, session
, 0);
1951 __close_session(mdsc
, session
);
1954 case CEPH_SESSION_RENEWCAPS
:
1955 if (session
->s_renew_seq
== seq
)
1956 renewed_caps(mdsc
, session
, 1);
1959 case CEPH_SESSION_CLOSE
:
1960 unregister_session(mdsc
, session
);
1961 remove_session_caps(session
);
1962 wake
= 1; /* for good measure */
1963 complete(&mdsc
->session_close_waiters
);
1964 kick_requests(mdsc
, mds
, 0); /* cur only */
1967 case CEPH_SESSION_STALE
:
1968 pr_info("mds%d caps went stale, renewing\n",
1970 spin_lock(&session
->s_cap_lock
);
1971 session
->s_cap_gen
++;
1972 session
->s_cap_ttl
= 0;
1973 spin_unlock(&session
->s_cap_lock
);
1974 send_renew_caps(mdsc
, session
);
1977 case CEPH_SESSION_RECALL_STATE
:
1978 trim_caps(mdsc
, session
, le32_to_cpu(h
->max_caps
));
1982 pr_err("mdsc_handle_session bad op %d mds%d\n", op
, mds
);
1986 mutex_unlock(&session
->s_mutex
);
1988 mutex_lock(&mdsc
->mutex
);
1989 __wake_requests(mdsc
, &session
->s_waiting
);
1990 mutex_unlock(&mdsc
->mutex
);
1995 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds
,
1996 (int)msg
->front
.iov_len
);
2003 * called under session->mutex.
2005 static void replay_unsafe_requests(struct ceph_mds_client
*mdsc
,
2006 struct ceph_mds_session
*session
)
2008 struct ceph_mds_request
*req
, *nreq
;
2011 dout("replay_unsafe_requests mds%d\n", session
->s_mds
);
2013 mutex_lock(&mdsc
->mutex
);
2014 list_for_each_entry_safe(req
, nreq
, &session
->s_unsafe
, r_unsafe_item
) {
2015 err
= __prepare_send_request(mdsc
, req
, session
->s_mds
);
2017 ceph_msg_get(req
->r_request
);
2018 ceph_con_send(&session
->s_con
, req
->r_request
);
2021 mutex_unlock(&mdsc
->mutex
);
2025 * Encode information about a cap for a reconnect with the MDS.
2027 static int encode_caps_cb(struct inode
*inode
, struct ceph_cap
*cap
,
2030 struct ceph_mds_cap_reconnect rec
;
2031 struct ceph_inode_info
*ci
;
2032 struct ceph_pagelist
*pagelist
= arg
;
2036 struct dentry
*dentry
;
2040 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2041 inode
, ceph_vinop(inode
), cap
, cap
->cap_id
,
2042 ceph_cap_string(cap
->issued
));
2043 err
= ceph_pagelist_encode_64(pagelist
, ceph_ino(inode
));
2047 dentry
= d_find_alias(inode
);
2049 path
= ceph_mdsc_build_path(dentry
, &pathlen
, &pathbase
, 0);
2051 err
= PTR_ERR(path
);
2058 err
= ceph_pagelist_encode_string(pagelist
, path
, pathlen
);
2062 spin_lock(&inode
->i_lock
);
2063 cap
->seq
= 0; /* reset cap seq */
2064 cap
->issue_seq
= 0; /* and issue_seq */
2065 rec
.cap_id
= cpu_to_le64(cap
->cap_id
);
2066 rec
.pathbase
= cpu_to_le64(pathbase
);
2067 rec
.wanted
= cpu_to_le32(__ceph_caps_wanted(ci
));
2068 rec
.issued
= cpu_to_le32(cap
->issued
);
2069 rec
.size
= cpu_to_le64(inode
->i_size
);
2070 ceph_encode_timespec(&rec
.mtime
, &inode
->i_mtime
);
2071 ceph_encode_timespec(&rec
.atime
, &inode
->i_atime
);
2072 rec
.snaprealm
= cpu_to_le64(ci
->i_snap_realm
->ino
);
2073 spin_unlock(&inode
->i_lock
);
2075 err
= ceph_pagelist_append(pagelist
, &rec
, sizeof(rec
));
2085 * If an MDS fails and recovers, clients need to reconnect in order to
2086 * reestablish shared state. This includes all caps issued through
2087 * this session _and_ the snap_realm hierarchy. Because it's not
2088 * clear which snap realms the mds cares about, we send everything we
2089 * know about.. that ensures we'll then get any new info the
2090 * recovering MDS might have.
2092 * This is a relatively heavyweight operation, but it's rare.
2094 * called with mdsc->mutex held.
2096 static void send_mds_reconnect(struct ceph_mds_client
*mdsc
, int mds
)
2098 struct ceph_mds_session
*session
= NULL
;
2099 struct ceph_msg
*reply
;
2102 u64 next_snap_ino
= 0;
2103 struct ceph_pagelist
*pagelist
;
2105 pr_info("reconnect to recovering mds%d\n", mds
);
2107 pagelist
= kmalloc(sizeof(*pagelist
), GFP_NOFS
);
2109 goto fail_nopagelist
;
2110 ceph_pagelist_init(pagelist
);
2112 reply
= ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT
, 0, 0, 0, NULL
);
2113 if (IS_ERR(reply
)) {
2114 err
= PTR_ERR(reply
);
2119 session
= __ceph_lookup_mds_session(mdsc
, mds
);
2120 mutex_unlock(&mdsc
->mutex
); /* drop lock for duration */
2123 mutex_lock(&session
->s_mutex
);
2125 session
->s_state
= CEPH_MDS_SESSION_RECONNECTING
;
2128 ceph_con_open(&session
->s_con
,
2129 ceph_mdsmap_get_addr(mdsc
->mdsmap
, mds
));
2131 /* replay unsafe requests */
2132 replay_unsafe_requests(mdsc
, session
);
2134 dout("no session for mds%d, will send short reconnect\n",
2138 down_read(&mdsc
->snap_rwsem
);
2142 dout("session %p state %s\n", session
,
2143 session_state_name(session
->s_state
));
2145 /* traverse this session's caps */
2146 err
= ceph_pagelist_encode_32(pagelist
, session
->s_nr_caps
);
2149 err
= iterate_session_caps(session
, encode_caps_cb
, pagelist
);
2154 * snaprealms. we provide mds with the ino, seq (version), and
2155 * parent for all of our realms. If the mds has any newer info,
2160 struct ceph_snap_realm
*realm
;
2161 struct ceph_mds_snaprealm_reconnect sr_rec
;
2162 got
= radix_tree_gang_lookup(&mdsc
->snap_realms
,
2163 (void **)&realm
, next_snap_ino
, 1);
2167 dout(" adding snap realm %llx seq %lld parent %llx\n",
2168 realm
->ino
, realm
->seq
, realm
->parent_ino
);
2169 sr_rec
.ino
= cpu_to_le64(realm
->ino
);
2170 sr_rec
.seq
= cpu_to_le64(realm
->seq
);
2171 sr_rec
.parent
= cpu_to_le64(realm
->parent_ino
);
2172 err
= ceph_pagelist_append(pagelist
, &sr_rec
, sizeof(sr_rec
));
2175 next_snap_ino
= realm
->ino
+ 1;
2179 reply
->pagelist
= pagelist
;
2180 reply
->hdr
.data_len
= cpu_to_le32(pagelist
->length
);
2181 reply
->nr_pages
= calc_pages_for(0, pagelist
->length
);
2182 ceph_con_send(&session
->s_con
, reply
);
2185 session
->s_state
= CEPH_MDS_SESSION_OPEN
;
2186 __wake_requests(mdsc
, &session
->s_waiting
);
2190 up_read(&mdsc
->snap_rwsem
);
2192 mutex_unlock(&session
->s_mutex
);
2193 ceph_put_mds_session(session
);
2195 mutex_lock(&mdsc
->mutex
);
2199 ceph_msg_put(reply
);
2201 ceph_pagelist_release(pagelist
);
2204 pr_err("ENOMEM preparing reconnect for mds%d\n", mds
);
2210 * compare old and new mdsmaps, kicking requests
2211 * and closing out old connections as necessary
2213 * called under mdsc->mutex.
2215 static void check_new_map(struct ceph_mds_client
*mdsc
,
2216 struct ceph_mdsmap
*newmap
,
2217 struct ceph_mdsmap
*oldmap
)
2220 int oldstate
, newstate
;
2221 struct ceph_mds_session
*s
;
2223 dout("check_new_map new %u old %u\n",
2224 newmap
->m_epoch
, oldmap
->m_epoch
);
2226 for (i
= 0; i
< oldmap
->m_max_mds
&& i
< mdsc
->max_sessions
; i
++) {
2227 if (mdsc
->sessions
[i
] == NULL
)
2229 s
= mdsc
->sessions
[i
];
2230 oldstate
= ceph_mdsmap_get_state(oldmap
, i
);
2231 newstate
= ceph_mdsmap_get_state(newmap
, i
);
2233 dout("check_new_map mds%d state %s -> %s (session %s)\n",
2234 i
, ceph_mds_state_name(oldstate
),
2235 ceph_mds_state_name(newstate
),
2236 session_state_name(s
->s_state
));
2238 if (memcmp(ceph_mdsmap_get_addr(oldmap
, i
),
2239 ceph_mdsmap_get_addr(newmap
, i
),
2240 sizeof(struct ceph_entity_addr
))) {
2241 if (s
->s_state
== CEPH_MDS_SESSION_OPENING
) {
2242 /* the session never opened, just close it
2244 __wake_requests(mdsc
, &s
->s_waiting
);
2245 unregister_session(mdsc
, s
);
2248 mutex_unlock(&mdsc
->mutex
);
2249 mutex_lock(&s
->s_mutex
);
2250 mutex_lock(&mdsc
->mutex
);
2251 ceph_con_close(&s
->s_con
);
2252 mutex_unlock(&s
->s_mutex
);
2253 s
->s_state
= CEPH_MDS_SESSION_RESTARTING
;
2256 /* kick any requests waiting on the recovering mds */
2257 kick_requests(mdsc
, i
, 1);
2258 } else if (oldstate
== newstate
) {
2259 continue; /* nothing new with this mds */
2265 if (s
->s_state
== CEPH_MDS_SESSION_RESTARTING
&&
2266 newstate
>= CEPH_MDS_STATE_RECONNECT
)
2267 send_mds_reconnect(mdsc
, i
);
2270 * kick requests on any mds that has gone active.
2272 * kick requests on cur or forwarder: we may have sent
2273 * the request to mds1, mds1 told us it forwarded it
2274 * to mds2, but then we learn mds1 failed and can't be
2275 * sure it successfully forwarded our request before
2278 if (oldstate
< CEPH_MDS_STATE_ACTIVE
&&
2279 newstate
>= CEPH_MDS_STATE_ACTIVE
) {
2280 pr_info("mds%d reconnect completed\n", s
->s_mds
);
2281 kick_requests(mdsc
, i
, 1);
2282 ceph_kick_flushing_caps(mdsc
, s
);
2283 wake_up_session_caps(s
, 1);
2295 * caller must hold session s_mutex, dentry->d_lock
2297 void __ceph_mdsc_drop_dentry_lease(struct dentry
*dentry
)
2299 struct ceph_dentry_info
*di
= ceph_dentry(dentry
);
2301 ceph_put_mds_session(di
->lease_session
);
2302 di
->lease_session
= NULL
;
2305 static void handle_lease(struct ceph_mds_client
*mdsc
, struct ceph_msg
*msg
)
2307 struct super_block
*sb
= mdsc
->client
->sb
;
2308 struct inode
*inode
;
2309 struct ceph_mds_session
*session
;
2310 struct ceph_inode_info
*ci
;
2311 struct dentry
*parent
, *dentry
;
2312 struct ceph_dentry_info
*di
;
2314 struct ceph_mds_lease
*h
= msg
->front
.iov_base
;
2315 struct ceph_vino vino
;
2320 if (msg
->hdr
.src
.name
.type
!= CEPH_ENTITY_TYPE_MDS
)
2322 mds
= le64_to_cpu(msg
->hdr
.src
.name
.num
);
2323 dout("handle_lease from mds%d\n", mds
);
2326 if (msg
->front
.iov_len
< sizeof(*h
) + sizeof(u32
))
2328 vino
.ino
= le64_to_cpu(h
->ino
);
2329 vino
.snap
= CEPH_NOSNAP
;
2330 mask
= le16_to_cpu(h
->mask
);
2331 dname
.name
= (void *)h
+ sizeof(*h
) + sizeof(u32
);
2332 dname
.len
= msg
->front
.iov_len
- sizeof(*h
) - sizeof(u32
);
2333 if (dname
.len
!= get_unaligned_le32(h
+1))
2337 mutex_lock(&mdsc
->mutex
);
2338 session
= __ceph_lookup_mds_session(mdsc
, mds
);
2339 mutex_unlock(&mdsc
->mutex
);
2341 pr_err("handle_lease got lease but no session mds%d\n", mds
);
2345 mutex_lock(&session
->s_mutex
);
2349 inode
= ceph_find_inode(sb
, vino
);
2350 dout("handle_lease '%s', mask %d, ino %llx %p\n",
2351 ceph_lease_op_name(h
->action
), mask
, vino
.ino
, inode
);
2352 if (inode
== NULL
) {
2353 dout("handle_lease no inode %llx\n", vino
.ino
);
2356 ci
= ceph_inode(inode
);
2359 parent
= d_find_alias(inode
);
2361 dout("no parent dentry on inode %p\n", inode
);
2363 goto release
; /* hrm... */
2365 dname
.hash
= full_name_hash(dname
.name
, dname
.len
);
2366 dentry
= d_lookup(parent
, &dname
);
2371 spin_lock(&dentry
->d_lock
);
2372 di
= ceph_dentry(dentry
);
2373 switch (h
->action
) {
2374 case CEPH_MDS_LEASE_REVOKE
:
2375 if (di
&& di
->lease_session
== session
) {
2376 h
->seq
= cpu_to_le32(di
->lease_seq
);
2377 __ceph_mdsc_drop_dentry_lease(dentry
);
2382 case CEPH_MDS_LEASE_RENEW
:
2383 if (di
&& di
->lease_session
== session
&&
2384 di
->lease_gen
== session
->s_cap_gen
&&
2385 di
->lease_renew_from
&&
2386 di
->lease_renew_after
== 0) {
2387 unsigned long duration
=
2388 le32_to_cpu(h
->duration_ms
) * HZ
/ 1000;
2390 di
->lease_seq
= le32_to_cpu(h
->seq
);
2391 dentry
->d_time
= di
->lease_renew_from
+ duration
;
2392 di
->lease_renew_after
= di
->lease_renew_from
+
2394 di
->lease_renew_from
= 0;
2398 spin_unlock(&dentry
->d_lock
);
2405 /* let's just reuse the same message */
2406 h
->action
= CEPH_MDS_LEASE_REVOKE_ACK
;
2408 ceph_con_send(&session
->s_con
, msg
);
2412 mutex_unlock(&session
->s_mutex
);
2413 ceph_put_mds_session(session
);
2417 pr_err("corrupt lease message\n");
2421 void ceph_mdsc_lease_send_msg(struct ceph_mds_session
*session
,
2422 struct inode
*inode
,
2423 struct dentry
*dentry
, char action
,
2426 struct ceph_msg
*msg
;
2427 struct ceph_mds_lease
*lease
;
2428 int len
= sizeof(*lease
) + sizeof(u32
);
2431 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
2432 inode
, dentry
, ceph_lease_op_name(action
), session
->s_mds
);
2433 dnamelen
= dentry
->d_name
.len
;
2436 msg
= ceph_msg_new(CEPH_MSG_CLIENT_LEASE
, len
, 0, 0, NULL
);
2439 lease
= msg
->front
.iov_base
;
2440 lease
->action
= action
;
2441 lease
->mask
= cpu_to_le16(CEPH_LOCK_DN
);
2442 lease
->ino
= cpu_to_le64(ceph_vino(inode
).ino
);
2443 lease
->first
= lease
->last
= cpu_to_le64(ceph_vino(inode
).snap
);
2444 lease
->seq
= cpu_to_le32(seq
);
2445 put_unaligned_le32(dnamelen
, lease
+ 1);
2446 memcpy((void *)(lease
+ 1) + 4, dentry
->d_name
.name
, dnamelen
);
2449 * if this is a preemptive lease RELEASE, no need to
2450 * flush request stream, since the actual request will
2453 msg
->more_to_follow
= (action
== CEPH_MDS_LEASE_RELEASE
);
2455 ceph_con_send(&session
->s_con
, msg
);
2459 * Preemptively release a lease we expect to invalidate anyway.
2460 * Pass @inode always, @dentry is optional.
2462 void ceph_mdsc_lease_release(struct ceph_mds_client
*mdsc
, struct inode
*inode
,
2463 struct dentry
*dentry
, int mask
)
2465 struct ceph_dentry_info
*di
;
2466 struct ceph_mds_session
*session
;
2469 BUG_ON(inode
== NULL
);
2470 BUG_ON(dentry
== NULL
);
2471 BUG_ON(mask
!= CEPH_LOCK_DN
);
2473 /* is dentry lease valid? */
2474 spin_lock(&dentry
->d_lock
);
2475 di
= ceph_dentry(dentry
);
2476 if (!di
|| !di
->lease_session
||
2477 di
->lease_session
->s_mds
< 0 ||
2478 di
->lease_gen
!= di
->lease_session
->s_cap_gen
||
2479 !time_before(jiffies
, dentry
->d_time
)) {
2480 dout("lease_release inode %p dentry %p -- "
2482 inode
, dentry
, mask
);
2483 spin_unlock(&dentry
->d_lock
);
2487 /* we do have a lease on this dentry; note mds and seq */
2488 session
= ceph_get_mds_session(di
->lease_session
);
2489 seq
= di
->lease_seq
;
2490 __ceph_mdsc_drop_dentry_lease(dentry
);
2491 spin_unlock(&dentry
->d_lock
);
2493 dout("lease_release inode %p dentry %p mask %d to mds%d\n",
2494 inode
, dentry
, mask
, session
->s_mds
);
2495 ceph_mdsc_lease_send_msg(session
, inode
, dentry
,
2496 CEPH_MDS_LEASE_RELEASE
, seq
);
2497 ceph_put_mds_session(session
);
2501 * drop all leases (and dentry refs) in preparation for umount
2503 static void drop_leases(struct ceph_mds_client
*mdsc
)
2507 dout("drop_leases\n");
2508 mutex_lock(&mdsc
->mutex
);
2509 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
2510 struct ceph_mds_session
*s
= __ceph_lookup_mds_session(mdsc
, i
);
2513 mutex_unlock(&mdsc
->mutex
);
2514 mutex_lock(&s
->s_mutex
);
2515 mutex_unlock(&s
->s_mutex
);
2516 ceph_put_mds_session(s
);
2517 mutex_lock(&mdsc
->mutex
);
2519 mutex_unlock(&mdsc
->mutex
);
2525 * delayed work -- periodically trim expired leases, renew caps with mds
2527 static void schedule_delayed(struct ceph_mds_client
*mdsc
)
2530 unsigned hz
= round_jiffies_relative(HZ
* delay
);
2531 schedule_delayed_work(&mdsc
->delayed_work
, hz
);
2534 static void delayed_work(struct work_struct
*work
)
2537 struct ceph_mds_client
*mdsc
=
2538 container_of(work
, struct ceph_mds_client
, delayed_work
.work
);
2542 dout("mdsc delayed_work\n");
2543 ceph_check_delayed_caps(mdsc
);
2545 mutex_lock(&mdsc
->mutex
);
2546 renew_interval
= mdsc
->mdsmap
->m_session_timeout
>> 2;
2547 renew_caps
= time_after_eq(jiffies
, HZ
*renew_interval
+
2548 mdsc
->last_renew_caps
);
2550 mdsc
->last_renew_caps
= jiffies
;
2552 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
2553 struct ceph_mds_session
*s
= __ceph_lookup_mds_session(mdsc
, i
);
2556 if (s
->s_state
== CEPH_MDS_SESSION_CLOSING
) {
2557 dout("resending session close request for mds%d\n",
2559 request_close_session(mdsc
, s
);
2560 ceph_put_mds_session(s
);
2563 if (s
->s_ttl
&& time_after(jiffies
, s
->s_ttl
)) {
2564 if (s
->s_state
== CEPH_MDS_SESSION_OPEN
) {
2565 s
->s_state
= CEPH_MDS_SESSION_HUNG
;
2566 pr_info("mds%d hung\n", s
->s_mds
);
2569 if (s
->s_state
< CEPH_MDS_SESSION_OPEN
) {
2570 /* this mds is failed or recovering, just wait */
2571 ceph_put_mds_session(s
);
2574 mutex_unlock(&mdsc
->mutex
);
2576 mutex_lock(&s
->s_mutex
);
2578 send_renew_caps(mdsc
, s
);
2580 ceph_con_keepalive(&s
->s_con
);
2581 add_cap_releases(mdsc
, s
, -1);
2582 send_cap_releases(mdsc
, s
);
2583 mutex_unlock(&s
->s_mutex
);
2584 ceph_put_mds_session(s
);
2586 mutex_lock(&mdsc
->mutex
);
2588 mutex_unlock(&mdsc
->mutex
);
2590 schedule_delayed(mdsc
);
2594 int ceph_mdsc_init(struct ceph_mds_client
*mdsc
, struct ceph_client
*client
)
2596 mdsc
->client
= client
;
2597 mutex_init(&mdsc
->mutex
);
2598 mdsc
->mdsmap
= kzalloc(sizeof(*mdsc
->mdsmap
), GFP_NOFS
);
2599 init_completion(&mdsc
->safe_umount_waiters
);
2600 init_completion(&mdsc
->session_close_waiters
);
2601 INIT_LIST_HEAD(&mdsc
->waiting_for_map
);
2602 mdsc
->sessions
= NULL
;
2603 mdsc
->max_sessions
= 0;
2605 init_rwsem(&mdsc
->snap_rwsem
);
2606 INIT_RADIX_TREE(&mdsc
->snap_realms
, GFP_NOFS
);
2607 INIT_LIST_HEAD(&mdsc
->snap_empty
);
2608 spin_lock_init(&mdsc
->snap_empty_lock
);
2610 mdsc
->request_tree
= RB_ROOT
;
2611 INIT_DELAYED_WORK(&mdsc
->delayed_work
, delayed_work
);
2612 mdsc
->last_renew_caps
= jiffies
;
2613 INIT_LIST_HEAD(&mdsc
->cap_delay_list
);
2614 spin_lock_init(&mdsc
->cap_delay_lock
);
2615 INIT_LIST_HEAD(&mdsc
->snap_flush_list
);
2616 spin_lock_init(&mdsc
->snap_flush_lock
);
2617 mdsc
->cap_flush_seq
= 0;
2618 INIT_LIST_HEAD(&mdsc
->cap_dirty
);
2619 mdsc
->num_cap_flushing
= 0;
2620 spin_lock_init(&mdsc
->cap_dirty_lock
);
2621 init_waitqueue_head(&mdsc
->cap_flushing_wq
);
2622 spin_lock_init(&mdsc
->dentry_lru_lock
);
2623 INIT_LIST_HEAD(&mdsc
->dentry_lru
);
2628 * Wait for safe replies on open mds requests. If we time out, drop
2629 * all requests from the tree to avoid dangling dentry refs.
2631 static void wait_requests(struct ceph_mds_client
*mdsc
)
2633 struct ceph_mds_request
*req
;
2634 struct ceph_client
*client
= mdsc
->client
;
2636 mutex_lock(&mdsc
->mutex
);
2637 if (__get_oldest_req(mdsc
)) {
2638 mutex_unlock(&mdsc
->mutex
);
2640 dout("wait_requests waiting for requests\n");
2641 wait_for_completion_timeout(&mdsc
->safe_umount_waiters
,
2642 client
->mount_args
->mount_timeout
* HZ
);
2644 /* tear down remaining requests */
2645 mutex_lock(&mdsc
->mutex
);
2646 while ((req
= __get_oldest_req(mdsc
))) {
2647 dout("wait_requests timed out on tid %llu\n",
2649 __unregister_request(mdsc
, req
);
2652 mutex_unlock(&mdsc
->mutex
);
2653 dout("wait_requests done\n");
2657 * called before mount is ro, and before dentries are torn down.
2658 * (hmm, does this still race with new lookups?)
2660 void ceph_mdsc_pre_umount(struct ceph_mds_client
*mdsc
)
2662 dout("pre_umount\n");
2666 ceph_flush_dirty_caps(mdsc
);
2667 wait_requests(mdsc
);
2671 * wait for all write mds requests to flush.
2673 static void wait_unsafe_requests(struct ceph_mds_client
*mdsc
, u64 want_tid
)
2675 struct ceph_mds_request
*req
= NULL
;
2678 mutex_lock(&mdsc
->mutex
);
2679 dout("wait_unsafe_requests want %lld\n", want_tid
);
2680 req
= __get_oldest_req(mdsc
);
2681 while (req
&& req
->r_tid
<= want_tid
) {
2682 if ((req
->r_op
& CEPH_MDS_OP_WRITE
)) {
2684 ceph_mdsc_get_request(req
);
2685 mutex_unlock(&mdsc
->mutex
);
2686 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
2687 req
->r_tid
, want_tid
);
2688 wait_for_completion(&req
->r_safe_completion
);
2689 mutex_lock(&mdsc
->mutex
);
2690 n
= rb_next(&req
->r_node
);
2691 ceph_mdsc_put_request(req
);
2693 n
= rb_next(&req
->r_node
);
2697 req
= rb_entry(n
, struct ceph_mds_request
, r_node
);
2699 mutex_unlock(&mdsc
->mutex
);
2700 dout("wait_unsafe_requests done\n");
2703 void ceph_mdsc_sync(struct ceph_mds_client
*mdsc
)
2705 u64 want_tid
, want_flush
;
2708 mutex_lock(&mdsc
->mutex
);
2709 want_tid
= mdsc
->last_tid
;
2710 want_flush
= mdsc
->cap_flush_seq
;
2711 mutex_unlock(&mdsc
->mutex
);
2712 dout("sync want tid %lld flush_seq %lld\n", want_tid
, want_flush
);
2714 ceph_flush_dirty_caps(mdsc
);
2716 wait_unsafe_requests(mdsc
, want_tid
);
2717 wait_event(mdsc
->cap_flushing_wq
, check_cap_flush(mdsc
, want_flush
));
2722 * called after sb is ro.
2724 void ceph_mdsc_close_sessions(struct ceph_mds_client
*mdsc
)
2726 struct ceph_mds_session
*session
;
2729 struct ceph_client
*client
= mdsc
->client
;
2730 unsigned long started
, timeout
= client
->mount_args
->mount_timeout
* HZ
;
2732 dout("close_sessions\n");
2734 mutex_lock(&mdsc
->mutex
);
2736 /* close sessions */
2738 while (time_before(jiffies
, started
+ timeout
)) {
2739 dout("closing sessions\n");
2741 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
2742 session
= __ceph_lookup_mds_session(mdsc
, i
);
2745 mutex_unlock(&mdsc
->mutex
);
2746 mutex_lock(&session
->s_mutex
);
2747 __close_session(mdsc
, session
);
2748 mutex_unlock(&session
->s_mutex
);
2749 ceph_put_mds_session(session
);
2750 mutex_lock(&mdsc
->mutex
);
2756 if (client
->mount_state
== CEPH_MOUNT_SHUTDOWN
)
2759 dout("waiting for sessions to close\n");
2760 mutex_unlock(&mdsc
->mutex
);
2761 wait_for_completion_timeout(&mdsc
->session_close_waiters
,
2763 mutex_lock(&mdsc
->mutex
);
2766 /* tear down remaining sessions */
2767 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
2768 if (mdsc
->sessions
[i
]) {
2769 session
= get_session(mdsc
->sessions
[i
]);
2770 unregister_session(mdsc
, session
);
2771 mutex_unlock(&mdsc
->mutex
);
2772 mutex_lock(&session
->s_mutex
);
2773 remove_session_caps(session
);
2774 mutex_unlock(&session
->s_mutex
);
2775 ceph_put_mds_session(session
);
2776 mutex_lock(&mdsc
->mutex
);
2780 WARN_ON(!list_empty(&mdsc
->cap_delay_list
));
2782 mutex_unlock(&mdsc
->mutex
);
2784 ceph_cleanup_empty_realms(mdsc
);
2786 cancel_delayed_work_sync(&mdsc
->delayed_work
); /* cancel timer */
2791 void ceph_mdsc_stop(struct ceph_mds_client
*mdsc
)
2794 cancel_delayed_work_sync(&mdsc
->delayed_work
); /* cancel timer */
2796 ceph_mdsmap_destroy(mdsc
->mdsmap
);
2797 kfree(mdsc
->sessions
);
2802 * handle mds map update.
2804 void ceph_mdsc_handle_map(struct ceph_mds_client
*mdsc
, struct ceph_msg
*msg
)
2808 void *p
= msg
->front
.iov_base
;
2809 void *end
= p
+ msg
->front
.iov_len
;
2810 struct ceph_mdsmap
*newmap
, *oldmap
;
2811 struct ceph_fsid fsid
;
2814 ceph_decode_need(&p
, end
, sizeof(fsid
)+2*sizeof(u32
), bad
);
2815 ceph_decode_copy(&p
, &fsid
, sizeof(fsid
));
2816 if (ceph_check_fsid(mdsc
->client
, &fsid
) < 0)
2818 epoch
= ceph_decode_32(&p
);
2819 maplen
= ceph_decode_32(&p
);
2820 dout("handle_map epoch %u len %d\n", epoch
, (int)maplen
);
2822 /* do we need it? */
2823 ceph_monc_got_mdsmap(&mdsc
->client
->monc
, epoch
);
2824 mutex_lock(&mdsc
->mutex
);
2825 if (mdsc
->mdsmap
&& epoch
<= mdsc
->mdsmap
->m_epoch
) {
2826 dout("handle_map epoch %u <= our %u\n",
2827 epoch
, mdsc
->mdsmap
->m_epoch
);
2828 mutex_unlock(&mdsc
->mutex
);
2832 newmap
= ceph_mdsmap_decode(&p
, end
);
2833 if (IS_ERR(newmap
)) {
2834 err
= PTR_ERR(newmap
);
2838 /* swap into place */
2840 oldmap
= mdsc
->mdsmap
;
2841 mdsc
->mdsmap
= newmap
;
2842 check_new_map(mdsc
, newmap
, oldmap
);
2843 ceph_mdsmap_destroy(oldmap
);
2845 mdsc
->mdsmap
= newmap
; /* first mds map */
2847 mdsc
->client
->sb
->s_maxbytes
= mdsc
->mdsmap
->m_max_file_size
;
2849 __wake_requests(mdsc
, &mdsc
->waiting_for_map
);
2851 mutex_unlock(&mdsc
->mutex
);
2852 schedule_delayed(mdsc
);
2856 mutex_unlock(&mdsc
->mutex
);
2858 pr_err("error decoding mdsmap %d\n", err
);
2862 static struct ceph_connection
*con_get(struct ceph_connection
*con
)
2864 struct ceph_mds_session
*s
= con
->private;
2866 if (get_session(s
)) {
2867 dout("mdsc con_get %p %d -> %d\n", s
,
2868 atomic_read(&s
->s_ref
) - 1, atomic_read(&s
->s_ref
));
2871 dout("mdsc con_get %p FAIL\n", s
);
2875 static void con_put(struct ceph_connection
*con
)
2877 struct ceph_mds_session
*s
= con
->private;
2879 dout("mdsc con_put %p %d -> %d\n", s
, atomic_read(&s
->s_ref
),
2880 atomic_read(&s
->s_ref
) - 1);
2881 ceph_put_mds_session(s
);
2885 * if the client is unresponsive for long enough, the mds will kill
2886 * the session entirely.
2888 static void peer_reset(struct ceph_connection
*con
)
2890 struct ceph_mds_session
*s
= con
->private;
2892 pr_err("mds%d gave us the boot. IMPLEMENT RECONNECT.\n",
2896 static void dispatch(struct ceph_connection
*con
, struct ceph_msg
*msg
)
2898 struct ceph_mds_session
*s
= con
->private;
2899 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
2900 int type
= le16_to_cpu(msg
->hdr
.type
);
2903 case CEPH_MSG_MDS_MAP
:
2904 ceph_mdsc_handle_map(mdsc
, msg
);
2906 case CEPH_MSG_CLIENT_SESSION
:
2907 handle_session(s
, msg
);
2909 case CEPH_MSG_CLIENT_REPLY
:
2910 handle_reply(s
, msg
);
2912 case CEPH_MSG_CLIENT_REQUEST_FORWARD
:
2913 handle_forward(mdsc
, msg
);
2915 case CEPH_MSG_CLIENT_CAPS
:
2916 ceph_handle_caps(s
, msg
);
2918 case CEPH_MSG_CLIENT_SNAP
:
2919 ceph_handle_snap(mdsc
, msg
);
2921 case CEPH_MSG_CLIENT_LEASE
:
2922 handle_lease(mdsc
, msg
);
2926 pr_err("received unknown message type %d %s\n", type
,
2927 ceph_msg_type_name(type
));
2935 static int get_authorizer(struct ceph_connection
*con
,
2936 void **buf
, int *len
, int *proto
,
2937 void **reply_buf
, int *reply_len
, int force_new
)
2939 struct ceph_mds_session
*s
= con
->private;
2940 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
2941 struct ceph_auth_client
*ac
= mdsc
->client
->monc
.auth
;
2944 if (force_new
&& s
->s_authorizer
) {
2945 ac
->ops
->destroy_authorizer(ac
, s
->s_authorizer
);
2946 s
->s_authorizer
= NULL
;
2948 if (s
->s_authorizer
== NULL
) {
2949 if (ac
->ops
->create_authorizer
) {
2950 ret
= ac
->ops
->create_authorizer(
2951 ac
, CEPH_ENTITY_TYPE_MDS
,
2953 &s
->s_authorizer_buf
,
2954 &s
->s_authorizer_buf_len
,
2955 &s
->s_authorizer_reply_buf
,
2956 &s
->s_authorizer_reply_buf_len
);
2962 *proto
= ac
->protocol
;
2963 *buf
= s
->s_authorizer_buf
;
2964 *len
= s
->s_authorizer_buf_len
;
2965 *reply_buf
= s
->s_authorizer_reply_buf
;
2966 *reply_len
= s
->s_authorizer_reply_buf_len
;
2971 static int verify_authorizer_reply(struct ceph_connection
*con
, int len
)
2973 struct ceph_mds_session
*s
= con
->private;
2974 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
2975 struct ceph_auth_client
*ac
= mdsc
->client
->monc
.auth
;
2977 return ac
->ops
->verify_authorizer_reply(ac
, s
->s_authorizer
, len
);
2980 static int invalidate_authorizer(struct ceph_connection
*con
)
2982 struct ceph_mds_session
*s
= con
->private;
2983 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
2984 struct ceph_auth_client
*ac
= mdsc
->client
->monc
.auth
;
2986 if (ac
->ops
->invalidate_authorizer
)
2987 ac
->ops
->invalidate_authorizer(ac
, CEPH_ENTITY_TYPE_MDS
);
2989 return ceph_monc_validate_auth(&mdsc
->client
->monc
);
2992 const static struct ceph_connection_operations mds_con_ops
= {
2995 .dispatch
= dispatch
,
2996 .get_authorizer
= get_authorizer
,
2997 .verify_authorizer_reply
= verify_authorizer_reply
,
2998 .invalidate_authorizer
= invalidate_authorizer
,
2999 .peer_reset
= peer_reset
,