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_RECONNECTING
: return "reconnecting";
259 default: return "???";
263 static struct ceph_mds_session
*get_session(struct ceph_mds_session
*s
)
265 if (atomic_inc_not_zero(&s
->s_ref
)) {
266 dout("mdsc get_session %p %d -> %d\n", s
,
267 atomic_read(&s
->s_ref
)-1, atomic_read(&s
->s_ref
));
270 dout("mdsc get_session %p 0 -- FAIL", s
);
275 void ceph_put_mds_session(struct ceph_mds_session
*s
)
277 dout("mdsc put_session %p %d -> %d\n", s
,
278 atomic_read(&s
->s_ref
), atomic_read(&s
->s_ref
)-1);
279 if (atomic_dec_and_test(&s
->s_ref
)) {
281 s
->s_mdsc
->client
->monc
.auth
->ops
->destroy_authorizer(
282 s
->s_mdsc
->client
->monc
.auth
, s
->s_authorizer
);
288 * called under mdsc->mutex
290 struct ceph_mds_session
*__ceph_lookup_mds_session(struct ceph_mds_client
*mdsc
,
293 struct ceph_mds_session
*session
;
295 if (mds
>= mdsc
->max_sessions
|| mdsc
->sessions
[mds
] == NULL
)
297 session
= mdsc
->sessions
[mds
];
298 dout("lookup_mds_session %p %d\n", session
,
299 atomic_read(&session
->s_ref
));
300 get_session(session
);
304 static bool __have_session(struct ceph_mds_client
*mdsc
, int mds
)
306 if (mds
>= mdsc
->max_sessions
)
308 return mdsc
->sessions
[mds
];
312 * create+register a new session for given mds.
313 * called under mdsc->mutex.
315 static struct ceph_mds_session
*register_session(struct ceph_mds_client
*mdsc
,
318 struct ceph_mds_session
*s
;
320 s
= kzalloc(sizeof(*s
), GFP_NOFS
);
323 s
->s_state
= CEPH_MDS_SESSION_NEW
;
326 mutex_init(&s
->s_mutex
);
328 ceph_con_init(mdsc
->client
->msgr
, &s
->s_con
);
329 s
->s_con
.private = s
;
330 s
->s_con
.ops
= &mds_con_ops
;
331 s
->s_con
.peer_name
.type
= CEPH_ENTITY_TYPE_MDS
;
332 s
->s_con
.peer_name
.num
= cpu_to_le64(mds
);
334 spin_lock_init(&s
->s_cap_lock
);
337 s
->s_renew_requested
= 0;
339 INIT_LIST_HEAD(&s
->s_caps
);
342 atomic_set(&s
->s_ref
, 1);
343 INIT_LIST_HEAD(&s
->s_waiting
);
344 INIT_LIST_HEAD(&s
->s_unsafe
);
345 s
->s_num_cap_releases
= 0;
346 s
->s_iterating_caps
= false;
347 INIT_LIST_HEAD(&s
->s_cap_releases
);
348 INIT_LIST_HEAD(&s
->s_cap_releases_done
);
349 INIT_LIST_HEAD(&s
->s_cap_flushing
);
350 INIT_LIST_HEAD(&s
->s_cap_snaps_flushing
);
352 dout("register_session mds%d\n", mds
);
353 if (mds
>= mdsc
->max_sessions
) {
354 int newmax
= 1 << get_count_order(mds
+1);
355 struct ceph_mds_session
**sa
;
357 dout("register_session realloc to %d\n", newmax
);
358 sa
= kcalloc(newmax
, sizeof(void *), GFP_NOFS
);
361 if (mdsc
->sessions
) {
362 memcpy(sa
, mdsc
->sessions
,
363 mdsc
->max_sessions
* sizeof(void *));
364 kfree(mdsc
->sessions
);
367 mdsc
->max_sessions
= newmax
;
369 mdsc
->sessions
[mds
] = s
;
370 atomic_inc(&s
->s_ref
); /* one ref to sessions[], one to caller */
372 ceph_con_open(&s
->s_con
, ceph_mdsmap_get_addr(mdsc
->mdsmap
, mds
));
378 return ERR_PTR(-ENOMEM
);
382 * called under mdsc->mutex
384 static void unregister_session(struct ceph_mds_client
*mdsc
,
385 struct ceph_mds_session
*s
)
387 dout("unregister_session mds%d %p\n", s
->s_mds
, s
);
388 mdsc
->sessions
[s
->s_mds
] = NULL
;
389 ceph_con_close(&s
->s_con
);
390 ceph_put_mds_session(s
);
394 * drop session refs in request.
396 * should be last request ref, or hold mdsc->mutex
398 static void put_request_session(struct ceph_mds_request
*req
)
400 if (req
->r_session
) {
401 ceph_put_mds_session(req
->r_session
);
402 req
->r_session
= NULL
;
406 void ceph_mdsc_release_request(struct kref
*kref
)
408 struct ceph_mds_request
*req
= container_of(kref
,
409 struct ceph_mds_request
,
412 ceph_msg_put(req
->r_request
);
414 ceph_msg_put(req
->r_reply
);
415 destroy_reply_info(&req
->r_reply_info
);
418 ceph_put_cap_refs(ceph_inode(req
->r_inode
),
422 if (req
->r_locked_dir
)
423 ceph_put_cap_refs(ceph_inode(req
->r_locked_dir
),
425 if (req
->r_target_inode
)
426 iput(req
->r_target_inode
);
429 if (req
->r_old_dentry
) {
431 ceph_inode(req
->r_old_dentry
->d_parent
->d_inode
),
433 dput(req
->r_old_dentry
);
437 put_request_session(req
);
438 ceph_unreserve_caps(&req
->r_caps_reservation
);
443 * lookup session, bump ref if found.
445 * called under mdsc->mutex.
447 static struct ceph_mds_request
*__lookup_request(struct ceph_mds_client
*mdsc
,
450 struct ceph_mds_request
*req
;
451 req
= radix_tree_lookup(&mdsc
->request_tree
, tid
);
453 ceph_mdsc_get_request(req
);
458 * Register an in-flight request, and assign a tid. Link to directory
459 * are modifying (if any).
461 * Called under mdsc->mutex.
463 static void __register_request(struct ceph_mds_client
*mdsc
,
464 struct ceph_mds_request
*req
,
467 req
->r_tid
= ++mdsc
->last_tid
;
469 ceph_reserve_caps(&req
->r_caps_reservation
, req
->r_num_caps
);
470 dout("__register_request %p tid %lld\n", req
, req
->r_tid
);
471 ceph_mdsc_get_request(req
);
472 radix_tree_insert(&mdsc
->request_tree
, req
->r_tid
, (void *)req
);
475 struct ceph_inode_info
*ci
= ceph_inode(dir
);
477 spin_lock(&ci
->i_unsafe_lock
);
478 req
->r_unsafe_dir
= dir
;
479 list_add_tail(&req
->r_unsafe_dir_item
, &ci
->i_unsafe_dirops
);
480 spin_unlock(&ci
->i_unsafe_lock
);
484 static void __unregister_request(struct ceph_mds_client
*mdsc
,
485 struct ceph_mds_request
*req
)
487 dout("__unregister_request %p tid %lld\n", req
, req
->r_tid
);
488 radix_tree_delete(&mdsc
->request_tree
, req
->r_tid
);
489 ceph_mdsc_put_request(req
);
491 if (req
->r_unsafe_dir
) {
492 struct ceph_inode_info
*ci
= ceph_inode(req
->r_unsafe_dir
);
494 spin_lock(&ci
->i_unsafe_lock
);
495 list_del_init(&req
->r_unsafe_dir_item
);
496 spin_unlock(&ci
->i_unsafe_lock
);
501 * Choose mds to send request to next. If there is a hint set in the
502 * request (e.g., due to a prior forward hint from the mds), use that.
503 * Otherwise, consult frag tree and/or caps to identify the
504 * appropriate mds. If all else fails, choose randomly.
506 * Called under mdsc->mutex.
508 static int __choose_mds(struct ceph_mds_client
*mdsc
,
509 struct ceph_mds_request
*req
)
512 struct ceph_inode_info
*ci
;
513 struct ceph_cap
*cap
;
514 int mode
= req
->r_direct_mode
;
516 u32 hash
= req
->r_direct_hash
;
517 bool is_hash
= req
->r_direct_is_hash
;
520 * is there a specific mds we should try? ignore hint if we have
521 * no session and the mds is not up (active or recovering).
523 if (req
->r_resend_mds
>= 0 &&
524 (__have_session(mdsc
, req
->r_resend_mds
) ||
525 ceph_mdsmap_get_state(mdsc
->mdsmap
, req
->r_resend_mds
) > 0)) {
526 dout("choose_mds using resend_mds mds%d\n",
528 return req
->r_resend_mds
;
531 if (mode
== USE_RANDOM_MDS
)
536 inode
= req
->r_inode
;
537 } else if (req
->r_dentry
) {
538 if (req
->r_dentry
->d_inode
) {
539 inode
= req
->r_dentry
->d_inode
;
541 inode
= req
->r_dentry
->d_parent
->d_inode
;
542 hash
= req
->r_dentry
->d_name
.hash
;
546 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode
, (int)is_hash
,
550 ci
= ceph_inode(inode
);
552 if (is_hash
&& S_ISDIR(inode
->i_mode
)) {
553 struct ceph_inode_frag frag
;
556 ceph_choose_frag(ci
, hash
, &frag
, &found
);
558 if (mode
== USE_ANY_MDS
&& frag
.ndist
> 0) {
561 /* choose a random replica */
562 get_random_bytes(&r
, 1);
565 dout("choose_mds %p %llx.%llx "
566 "frag %u mds%d (%d/%d)\n",
567 inode
, ceph_vinop(inode
),
573 /* since this file/dir wasn't known to be
574 * replicated, then we want to look for the
575 * authoritative mds. */
578 /* choose auth mds */
580 dout("choose_mds %p %llx.%llx "
581 "frag %u mds%d (auth)\n",
582 inode
, ceph_vinop(inode
), frag
.frag
, mds
);
588 spin_lock(&inode
->i_lock
);
590 if (mode
== USE_AUTH_MDS
)
591 cap
= ci
->i_auth_cap
;
592 if (!cap
&& !RB_EMPTY_ROOT(&ci
->i_caps
))
593 cap
= rb_entry(rb_first(&ci
->i_caps
), struct ceph_cap
, ci_node
);
595 spin_unlock(&inode
->i_lock
);
598 mds
= cap
->session
->s_mds
;
599 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
600 inode
, ceph_vinop(inode
), mds
,
601 cap
== ci
->i_auth_cap
? "auth " : "", cap
);
602 spin_unlock(&inode
->i_lock
);
606 mds
= ceph_mdsmap_get_random_mds(mdsc
->mdsmap
);
607 dout("choose_mds chose random mds%d\n", mds
);
615 static struct ceph_msg
*create_session_msg(u32 op
, u64 seq
)
617 struct ceph_msg
*msg
;
618 struct ceph_mds_session_head
*h
;
620 msg
= ceph_msg_new(CEPH_MSG_CLIENT_SESSION
, sizeof(*h
), 0, 0, NULL
);
622 pr_err("create_session_msg ENOMEM creating msg\n");
623 return ERR_PTR(PTR_ERR(msg
));
625 h
= msg
->front
.iov_base
;
626 h
->op
= cpu_to_le32(op
);
627 h
->seq
= cpu_to_le64(seq
);
632 * send session open request.
634 * called under mdsc->mutex
636 static int __open_session(struct ceph_mds_client
*mdsc
,
637 struct ceph_mds_session
*session
)
639 struct ceph_msg
*msg
;
641 int mds
= session
->s_mds
;
644 /* wait for mds to go active? */
645 mstate
= ceph_mdsmap_get_state(mdsc
->mdsmap
, mds
);
646 dout("open_session to mds%d (%s)\n", mds
,
647 ceph_mds_state_name(mstate
));
648 session
->s_state
= CEPH_MDS_SESSION_OPENING
;
649 session
->s_renew_requested
= jiffies
;
651 /* send connect message */
652 msg
= create_session_msg(CEPH_SESSION_REQUEST_OPEN
, session
->s_seq
);
657 ceph_con_send(&session
->s_con
, msg
);
668 * Free preallocated cap messages assigned to this session
670 static void cleanup_cap_releases(struct ceph_mds_session
*session
)
672 struct ceph_msg
*msg
;
674 spin_lock(&session
->s_cap_lock
);
675 while (!list_empty(&session
->s_cap_releases
)) {
676 msg
= list_first_entry(&session
->s_cap_releases
,
677 struct ceph_msg
, list_head
);
678 list_del_init(&msg
->list_head
);
681 while (!list_empty(&session
->s_cap_releases_done
)) {
682 msg
= list_first_entry(&session
->s_cap_releases_done
,
683 struct ceph_msg
, list_head
);
684 list_del_init(&msg
->list_head
);
687 spin_unlock(&session
->s_cap_lock
);
691 * Helper to safely iterate over all caps associated with a session.
693 * caller must hold session s_mutex
695 static int iterate_session_caps(struct ceph_mds_session
*session
,
696 int (*cb
)(struct inode
*, struct ceph_cap
*,
699 struct ceph_cap
*cap
, *ncap
;
703 dout("iterate_session_caps %p mds%d\n", session
, session
->s_mds
);
704 spin_lock(&session
->s_cap_lock
);
705 session
->s_iterating_caps
= true;
706 list_for_each_entry_safe(cap
, ncap
, &session
->s_caps
, session_caps
) {
707 inode
= igrab(&cap
->ci
->vfs_inode
);
710 spin_unlock(&session
->s_cap_lock
);
711 ret
= cb(inode
, cap
, arg
);
713 spin_lock(&session
->s_cap_lock
);
719 session
->s_iterating_caps
= false;
720 spin_unlock(&session
->s_cap_lock
);
724 static int remove_session_caps_cb(struct inode
*inode
, struct ceph_cap
*cap
,
727 struct ceph_inode_info
*ci
= ceph_inode(inode
);
728 dout("removing cap %p, ci is %p, inode is %p\n",
729 cap
, ci
, &ci
->vfs_inode
);
730 ceph_remove_cap(cap
);
735 * caller must hold session s_mutex
737 static void remove_session_caps(struct ceph_mds_session
*session
)
739 dout("remove_session_caps on %p\n", session
);
740 iterate_session_caps(session
, remove_session_caps_cb
, NULL
);
741 BUG_ON(session
->s_nr_caps
> 0);
742 cleanup_cap_releases(session
);
746 * wake up any threads waiting on this session's caps. if the cap is
747 * old (didn't get renewed on the client reconnect), remove it now.
749 * caller must hold s_mutex.
751 static int wake_up_session_cb(struct inode
*inode
, struct ceph_cap
*cap
,
754 struct ceph_inode_info
*ci
= ceph_inode(inode
);
756 wake_up(&ci
->i_cap_wq
);
758 spin_lock(&inode
->i_lock
);
759 ci
->i_wanted_max_size
= 0;
760 ci
->i_requested_max_size
= 0;
761 spin_unlock(&inode
->i_lock
);
766 static void wake_up_session_caps(struct ceph_mds_session
*session
,
769 dout("wake_up_session_caps %p mds%d\n", session
, session
->s_mds
);
770 iterate_session_caps(session
, wake_up_session_cb
,
771 (void *)(unsigned long)reconnect
);
775 * Send periodic message to MDS renewing all currently held caps. The
776 * ack will reset the expiration for all caps from this session.
778 * caller holds s_mutex
780 static int send_renew_caps(struct ceph_mds_client
*mdsc
,
781 struct ceph_mds_session
*session
)
783 struct ceph_msg
*msg
;
786 if (time_after_eq(jiffies
, session
->s_cap_ttl
) &&
787 time_after_eq(session
->s_cap_ttl
, session
->s_renew_requested
))
788 pr_info("mds%d caps stale\n", session
->s_mds
);
790 /* do not try to renew caps until a recovering mds has reconnected
791 * with its clients. */
792 state
= ceph_mdsmap_get_state(mdsc
->mdsmap
, session
->s_mds
);
793 if (state
< CEPH_MDS_STATE_RECONNECT
) {
794 dout("send_renew_caps ignoring mds%d (%s)\n",
795 session
->s_mds
, ceph_mds_state_name(state
));
799 dout("send_renew_caps to mds%d (%s)\n", session
->s_mds
,
800 ceph_mds_state_name(state
));
801 session
->s_renew_requested
= jiffies
;
802 msg
= create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS
,
803 ++session
->s_renew_seq
);
806 ceph_con_send(&session
->s_con
, msg
);
811 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
813 * Called under session->s_mutex
815 static void renewed_caps(struct ceph_mds_client
*mdsc
,
816 struct ceph_mds_session
*session
, int is_renew
)
821 spin_lock(&session
->s_cap_lock
);
822 was_stale
= is_renew
&& (session
->s_cap_ttl
== 0 ||
823 time_after_eq(jiffies
, session
->s_cap_ttl
));
825 session
->s_cap_ttl
= session
->s_renew_requested
+
826 mdsc
->mdsmap
->m_session_timeout
*HZ
;
829 if (time_before(jiffies
, session
->s_cap_ttl
)) {
830 pr_info("mds%d caps renewed\n", session
->s_mds
);
833 pr_info("mds%d caps still stale\n", session
->s_mds
);
836 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
837 session
->s_mds
, session
->s_cap_ttl
, was_stale
? "stale" : "fresh",
838 time_before(jiffies
, session
->s_cap_ttl
) ? "stale" : "fresh");
839 spin_unlock(&session
->s_cap_lock
);
842 wake_up_session_caps(session
, 0);
846 * send a session close request
848 static int request_close_session(struct ceph_mds_client
*mdsc
,
849 struct ceph_mds_session
*session
)
851 struct ceph_msg
*msg
;
854 dout("request_close_session mds%d state %s seq %lld\n",
855 session
->s_mds
, session_state_name(session
->s_state
),
857 msg
= create_session_msg(CEPH_SESSION_REQUEST_CLOSE
, session
->s_seq
);
861 ceph_con_send(&session
->s_con
, msg
);
866 * Called with s_mutex held.
868 static int __close_session(struct ceph_mds_client
*mdsc
,
869 struct ceph_mds_session
*session
)
871 if (session
->s_state
>= CEPH_MDS_SESSION_CLOSING
)
873 session
->s_state
= CEPH_MDS_SESSION_CLOSING
;
874 return request_close_session(mdsc
, session
);
880 * Because we can't cache an inode without one or more caps, we do
881 * this indirectly: if a cap is unused, we prune its aliases, at which
882 * point the inode will hopefully get dropped to.
884 * Yes, this is a bit sloppy. Our only real goal here is to respond to
885 * memory pressure from the MDS, though, so it needn't be perfect.
887 static int trim_caps_cb(struct inode
*inode
, struct ceph_cap
*cap
, void *arg
)
889 struct ceph_mds_session
*session
= arg
;
890 struct ceph_inode_info
*ci
= ceph_inode(inode
);
891 int used
, oissued
, mine
;
893 if (session
->s_trim_caps
<= 0)
896 spin_lock(&inode
->i_lock
);
897 mine
= cap
->issued
| cap
->implemented
;
898 used
= __ceph_caps_used(ci
);
899 oissued
= __ceph_caps_issued_other(ci
, cap
);
901 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n",
902 inode
, cap
, ceph_cap_string(mine
), ceph_cap_string(oissued
),
903 ceph_cap_string(used
));
904 if (ci
->i_dirty_caps
)
905 goto out
; /* dirty caps */
906 if ((used
& ~oissued
) & mine
)
907 goto out
; /* we need these caps */
909 session
->s_trim_caps
--;
911 /* we aren't the only cap.. just remove us */
912 __ceph_remove_cap(cap
, NULL
);
914 /* try to drop referring dentries */
915 spin_unlock(&inode
->i_lock
);
916 d_prune_aliases(inode
);
917 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
918 inode
, cap
, atomic_read(&inode
->i_count
));
923 spin_unlock(&inode
->i_lock
);
928 * Trim session cap count down to some max number.
930 static int trim_caps(struct ceph_mds_client
*mdsc
,
931 struct ceph_mds_session
*session
,
934 int trim_caps
= session
->s_nr_caps
- max_caps
;
936 dout("trim_caps mds%d start: %d / %d, trim %d\n",
937 session
->s_mds
, session
->s_nr_caps
, max_caps
, trim_caps
);
939 session
->s_trim_caps
= trim_caps
;
940 iterate_session_caps(session
, trim_caps_cb
, session
);
941 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
942 session
->s_mds
, session
->s_nr_caps
, max_caps
,
943 trim_caps
- session
->s_trim_caps
);
944 session
->s_trim_caps
= 0;
950 * Allocate cap_release messages. If there is a partially full message
951 * in the queue, try to allocate enough to cover it's remainder, so that
952 * we can send it immediately.
954 * Called under s_mutex.
956 static int add_cap_releases(struct ceph_mds_client
*mdsc
,
957 struct ceph_mds_session
*session
,
960 struct ceph_msg
*msg
;
961 struct ceph_mds_cap_release
*head
;
965 extra
= mdsc
->client
->mount_args
->cap_release_safety
;
967 spin_lock(&session
->s_cap_lock
);
969 if (!list_empty(&session
->s_cap_releases
)) {
970 msg
= list_first_entry(&session
->s_cap_releases
,
973 head
= msg
->front
.iov_base
;
974 extra
+= CEPH_CAPS_PER_RELEASE
- le32_to_cpu(head
->num
);
977 while (session
->s_num_cap_releases
< session
->s_nr_caps
+ extra
) {
978 spin_unlock(&session
->s_cap_lock
);
979 msg
= ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE
, PAGE_CACHE_SIZE
,
983 dout("add_cap_releases %p msg %p now %d\n", session
, msg
,
984 (int)msg
->front
.iov_len
);
985 head
= msg
->front
.iov_base
;
986 head
->num
= cpu_to_le32(0);
987 msg
->front
.iov_len
= sizeof(*head
);
988 spin_lock(&session
->s_cap_lock
);
989 list_add(&msg
->list_head
, &session
->s_cap_releases
);
990 session
->s_num_cap_releases
+= CEPH_CAPS_PER_RELEASE
;
993 if (!list_empty(&session
->s_cap_releases
)) {
994 msg
= list_first_entry(&session
->s_cap_releases
,
997 head
= msg
->front
.iov_base
;
999 dout(" queueing non-full %p (%d)\n", msg
,
1000 le32_to_cpu(head
->num
));
1001 list_move_tail(&msg
->list_head
,
1002 &session
->s_cap_releases_done
);
1003 session
->s_num_cap_releases
-=
1004 CEPH_CAPS_PER_RELEASE
- le32_to_cpu(head
->num
);
1008 spin_unlock(&session
->s_cap_lock
);
1014 * flush all dirty inode data to disk.
1016 * returns true if we've flushed through want_flush_seq
1018 static int check_cap_flush(struct ceph_mds_client
*mdsc
, u64 want_flush_seq
)
1022 dout("check_cap_flush want %lld\n", want_flush_seq
);
1023 mutex_lock(&mdsc
->mutex
);
1024 for (mds
= 0; ret
&& mds
< mdsc
->max_sessions
; mds
++) {
1025 struct ceph_mds_session
*session
= mdsc
->sessions
[mds
];
1029 get_session(session
);
1030 mutex_unlock(&mdsc
->mutex
);
1032 mutex_lock(&session
->s_mutex
);
1033 if (!list_empty(&session
->s_cap_flushing
)) {
1034 struct ceph_inode_info
*ci
=
1035 list_entry(session
->s_cap_flushing
.next
,
1036 struct ceph_inode_info
,
1038 struct inode
*inode
= &ci
->vfs_inode
;
1040 spin_lock(&inode
->i_lock
);
1041 if (ci
->i_cap_flush_seq
<= want_flush_seq
) {
1042 dout("check_cap_flush still flushing %p "
1043 "seq %lld <= %lld to mds%d\n", inode
,
1044 ci
->i_cap_flush_seq
, want_flush_seq
,
1048 spin_unlock(&inode
->i_lock
);
1050 mutex_unlock(&session
->s_mutex
);
1051 ceph_put_mds_session(session
);
1055 mutex_lock(&mdsc
->mutex
);
1058 mutex_unlock(&mdsc
->mutex
);
1059 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq
);
1064 * called under s_mutex
1066 static void send_cap_releases(struct ceph_mds_client
*mdsc
,
1067 struct ceph_mds_session
*session
)
1069 struct ceph_msg
*msg
;
1071 dout("send_cap_releases mds%d\n", session
->s_mds
);
1073 spin_lock(&session
->s_cap_lock
);
1074 if (list_empty(&session
->s_cap_releases_done
))
1076 msg
= list_first_entry(&session
->s_cap_releases_done
,
1077 struct ceph_msg
, list_head
);
1078 list_del_init(&msg
->list_head
);
1079 spin_unlock(&session
->s_cap_lock
);
1080 msg
->hdr
.front_len
= cpu_to_le32(msg
->front
.iov_len
);
1081 dout("send_cap_releases mds%d %p\n", session
->s_mds
, msg
);
1082 ceph_con_send(&session
->s_con
, msg
);
1084 spin_unlock(&session
->s_cap_lock
);
1092 * Create an mds request.
1094 struct ceph_mds_request
*
1095 ceph_mdsc_create_request(struct ceph_mds_client
*mdsc
, int op
, int mode
)
1097 struct ceph_mds_request
*req
= kzalloc(sizeof(*req
), GFP_NOFS
);
1100 return ERR_PTR(-ENOMEM
);
1102 req
->r_started
= jiffies
;
1103 req
->r_resend_mds
= -1;
1104 INIT_LIST_HEAD(&req
->r_unsafe_dir_item
);
1106 kref_init(&req
->r_kref
);
1107 INIT_LIST_HEAD(&req
->r_wait
);
1108 init_completion(&req
->r_completion
);
1109 init_completion(&req
->r_safe_completion
);
1110 INIT_LIST_HEAD(&req
->r_unsafe_item
);
1113 req
->r_direct_mode
= mode
;
1118 * return oldest (lowest) tid in request tree, 0 if none.
1120 * called under mdsc->mutex.
1122 static u64
__get_oldest_tid(struct ceph_mds_client
*mdsc
)
1124 struct ceph_mds_request
*first
;
1125 if (radix_tree_gang_lookup(&mdsc
->request_tree
,
1126 (void **)&first
, 0, 1) <= 0)
1128 return first
->r_tid
;
1132 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1133 * on build_path_from_dentry in fs/cifs/dir.c.
1135 * If @stop_on_nosnap, generate path relative to the first non-snapped
1138 * Encode hidden .snap dirs as a double /, i.e.
1139 * foo/.snap/bar -> foo//bar
1141 char *ceph_mdsc_build_path(struct dentry
*dentry
, int *plen
, u64
*base
,
1144 struct dentry
*temp
;
1149 return ERR_PTR(-EINVAL
);
1153 for (temp
= dentry
; !IS_ROOT(temp
);) {
1154 struct inode
*inode
= temp
->d_inode
;
1155 if (inode
&& ceph_snap(inode
) == CEPH_SNAPDIR
)
1156 len
++; /* slash only */
1157 else if (stop_on_nosnap
&& inode
&&
1158 ceph_snap(inode
) == CEPH_NOSNAP
)
1161 len
+= 1 + temp
->d_name
.len
;
1162 temp
= temp
->d_parent
;
1164 pr_err("build_path_dentry corrupt dentry %p\n", dentry
);
1165 return ERR_PTR(-EINVAL
);
1169 len
--; /* no leading '/' */
1171 path
= kmalloc(len
+1, GFP_NOFS
);
1173 return ERR_PTR(-ENOMEM
);
1175 path
[pos
] = 0; /* trailing null */
1176 for (temp
= dentry
; !IS_ROOT(temp
) && pos
!= 0; ) {
1177 struct inode
*inode
= temp
->d_inode
;
1179 if (inode
&& ceph_snap(inode
) == CEPH_SNAPDIR
) {
1180 dout("build_path_dentry path+%d: %p SNAPDIR\n",
1182 } else if (stop_on_nosnap
&& inode
&&
1183 ceph_snap(inode
) == CEPH_NOSNAP
) {
1186 pos
-= temp
->d_name
.len
;
1189 strncpy(path
+ pos
, temp
->d_name
.name
,
1191 dout("build_path_dentry path+%d: %p '%.*s'\n",
1192 pos
, temp
, temp
->d_name
.len
, path
+ pos
);
1196 temp
= temp
->d_parent
;
1198 pr_err("build_path_dentry corrupt dentry\n");
1200 return ERR_PTR(-EINVAL
);
1204 pr_err("build_path_dentry did not end path lookup where "
1205 "expected, namelen is %d, pos is %d\n", len
, pos
);
1206 /* presumably this is only possible if racing with a
1207 rename of one of the parent directories (we can not
1208 lock the dentries above us to prevent this, but
1209 retrying should be harmless) */
1214 *base
= ceph_ino(temp
->d_inode
);
1216 dout("build_path_dentry on %p %d built %llx '%.*s'\n",
1217 dentry
, atomic_read(&dentry
->d_count
), *base
, len
, path
);
1221 static int build_dentry_path(struct dentry
*dentry
,
1222 const char **ppath
, int *ppathlen
, u64
*pino
,
1227 if (ceph_snap(dentry
->d_parent
->d_inode
) == CEPH_NOSNAP
) {
1228 *pino
= ceph_ino(dentry
->d_parent
->d_inode
);
1229 *ppath
= dentry
->d_name
.name
;
1230 *ppathlen
= dentry
->d_name
.len
;
1233 path
= ceph_mdsc_build_path(dentry
, ppathlen
, pino
, 1);
1235 return PTR_ERR(path
);
1241 static int build_inode_path(struct inode
*inode
,
1242 const char **ppath
, int *ppathlen
, u64
*pino
,
1245 struct dentry
*dentry
;
1248 if (ceph_snap(inode
) == CEPH_NOSNAP
) {
1249 *pino
= ceph_ino(inode
);
1253 dentry
= d_find_alias(inode
);
1254 path
= ceph_mdsc_build_path(dentry
, ppathlen
, pino
, 1);
1257 return PTR_ERR(path
);
1264 * request arguments may be specified via an inode *, a dentry *, or
1265 * an explicit ino+path.
1267 static int set_request_path_attr(struct inode
*rinode
, struct dentry
*rdentry
,
1268 const char *rpath
, u64 rino
,
1269 const char **ppath
, int *pathlen
,
1270 u64
*ino
, int *freepath
)
1275 r
= build_inode_path(rinode
, ppath
, pathlen
, ino
, freepath
);
1276 dout(" inode %p %llx.%llx\n", rinode
, ceph_ino(rinode
),
1278 } else if (rdentry
) {
1279 r
= build_dentry_path(rdentry
, ppath
, pathlen
, ino
, freepath
);
1280 dout(" dentry %p %llx/%.*s\n", rdentry
, *ino
, *pathlen
,
1285 *pathlen
= strlen(rpath
);
1286 dout(" path %.*s\n", *pathlen
, rpath
);
1293 * called under mdsc->mutex
1295 static struct ceph_msg
*create_request_message(struct ceph_mds_client
*mdsc
,
1296 struct ceph_mds_request
*req
,
1299 struct ceph_msg
*msg
;
1300 struct ceph_mds_request_head
*head
;
1301 const char *path1
= NULL
;
1302 const char *path2
= NULL
;
1303 u64 ino1
= 0, ino2
= 0;
1304 int pathlen1
= 0, pathlen2
= 0;
1305 int freepath1
= 0, freepath2
= 0;
1311 ret
= set_request_path_attr(req
->r_inode
, req
->r_dentry
,
1312 req
->r_path1
, req
->r_ino1
.ino
,
1313 &path1
, &pathlen1
, &ino1
, &freepath1
);
1319 ret
= set_request_path_attr(NULL
, req
->r_old_dentry
,
1320 req
->r_path2
, req
->r_ino2
.ino
,
1321 &path2
, &pathlen2
, &ino2
, &freepath2
);
1327 len
= sizeof(*head
) +
1328 pathlen1
+ pathlen2
+ 2*(sizeof(u32
) + sizeof(u64
));
1330 /* calculate (max) length for cap releases */
1331 len
+= sizeof(struct ceph_mds_request_release
) *
1332 (!!req
->r_inode_drop
+ !!req
->r_dentry_drop
+
1333 !!req
->r_old_inode_drop
+ !!req
->r_old_dentry_drop
);
1334 if (req
->r_dentry_drop
)
1335 len
+= req
->r_dentry
->d_name
.len
;
1336 if (req
->r_old_dentry_drop
)
1337 len
+= req
->r_old_dentry
->d_name
.len
;
1339 msg
= ceph_msg_new(CEPH_MSG_CLIENT_REQUEST
, len
, 0, 0, NULL
);
1343 msg
->hdr
.tid
= cpu_to_le64(req
->r_tid
);
1345 head
= msg
->front
.iov_base
;
1346 p
= msg
->front
.iov_base
+ sizeof(*head
);
1347 end
= msg
->front
.iov_base
+ msg
->front
.iov_len
;
1349 head
->mdsmap_epoch
= cpu_to_le32(mdsc
->mdsmap
->m_epoch
);
1350 head
->op
= cpu_to_le32(req
->r_op
);
1351 head
->caller_uid
= cpu_to_le32(current_fsuid());
1352 head
->caller_gid
= cpu_to_le32(current_fsgid());
1353 head
->args
= req
->r_args
;
1355 ceph_encode_filepath(&p
, end
, ino1
, path1
);
1356 ceph_encode_filepath(&p
, end
, ino2
, path2
);
1360 if (req
->r_inode_drop
)
1361 releases
+= ceph_encode_inode_release(&p
,
1362 req
->r_inode
? req
->r_inode
: req
->r_dentry
->d_inode
,
1363 mds
, req
->r_inode_drop
, req
->r_inode_unless
, 0);
1364 if (req
->r_dentry_drop
)
1365 releases
+= ceph_encode_dentry_release(&p
, req
->r_dentry
,
1366 mds
, req
->r_dentry_drop
, req
->r_dentry_unless
);
1367 if (req
->r_old_dentry_drop
)
1368 releases
+= ceph_encode_dentry_release(&p
, req
->r_old_dentry
,
1369 mds
, req
->r_old_dentry_drop
, req
->r_old_dentry_unless
);
1370 if (req
->r_old_inode_drop
)
1371 releases
+= ceph_encode_inode_release(&p
,
1372 req
->r_old_dentry
->d_inode
,
1373 mds
, req
->r_old_inode_drop
, req
->r_old_inode_unless
, 0);
1374 head
->num_releases
= cpu_to_le16(releases
);
1377 msg
->front
.iov_len
= p
- msg
->front
.iov_base
;
1378 msg
->hdr
.front_len
= cpu_to_le32(msg
->front
.iov_len
);
1380 msg
->pages
= req
->r_pages
;
1381 msg
->nr_pages
= req
->r_num_pages
;
1382 msg
->hdr
.data_len
= cpu_to_le32(req
->r_data_len
);
1383 msg
->hdr
.data_off
= cpu_to_le16(0);
1387 kfree((char *)path2
);
1390 kfree((char *)path1
);
1396 * called under mdsc->mutex if error, under no mutex if
1399 static void complete_request(struct ceph_mds_client
*mdsc
,
1400 struct ceph_mds_request
*req
)
1402 if (req
->r_callback
)
1403 req
->r_callback(mdsc
, req
);
1405 complete(&req
->r_completion
);
1409 * called under mdsc->mutex
1411 static int __prepare_send_request(struct ceph_mds_client
*mdsc
,
1412 struct ceph_mds_request
*req
,
1415 struct ceph_mds_request_head
*rhead
;
1416 struct ceph_msg
*msg
;
1421 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req
,
1422 req
->r_tid
, ceph_mds_op_name(req
->r_op
), req
->r_attempts
);
1424 if (req
->r_request
) {
1425 ceph_msg_put(req
->r_request
);
1426 req
->r_request
= NULL
;
1428 msg
= create_request_message(mdsc
, req
, mds
);
1430 req
->r_reply
= ERR_PTR(PTR_ERR(msg
));
1431 complete_request(mdsc
, req
);
1432 return -PTR_ERR(msg
);
1434 req
->r_request
= msg
;
1436 rhead
= msg
->front
.iov_base
;
1437 rhead
->oldest_client_tid
= cpu_to_le64(__get_oldest_tid(mdsc
));
1438 if (req
->r_got_unsafe
)
1439 flags
|= CEPH_MDS_FLAG_REPLAY
;
1440 if (req
->r_locked_dir
)
1441 flags
|= CEPH_MDS_FLAG_WANT_DENTRY
;
1442 rhead
->flags
= cpu_to_le32(flags
);
1443 rhead
->num_fwd
= req
->r_num_fwd
;
1444 rhead
->num_retry
= req
->r_attempts
- 1;
1446 dout(" r_locked_dir = %p\n", req
->r_locked_dir
);
1448 if (req
->r_target_inode
&& req
->r_got_unsafe
)
1449 rhead
->ino
= cpu_to_le64(ceph_ino(req
->r_target_inode
));
1456 * send request, or put it on the appropriate wait list.
1458 static int __do_request(struct ceph_mds_client
*mdsc
,
1459 struct ceph_mds_request
*req
)
1461 struct ceph_mds_session
*session
= NULL
;
1468 if (req
->r_timeout
&&
1469 time_after_eq(jiffies
, req
->r_started
+ req
->r_timeout
)) {
1470 dout("do_request timed out\n");
1475 mds
= __choose_mds(mdsc
, req
);
1477 ceph_mdsmap_get_state(mdsc
->mdsmap
, mds
) < CEPH_MDS_STATE_ACTIVE
) {
1478 dout("do_request no mds or not active, waiting for map\n");
1479 list_add(&req
->r_wait
, &mdsc
->waiting_for_map
);
1483 /* get, open session */
1484 session
= __ceph_lookup_mds_session(mdsc
, mds
);
1486 session
= register_session(mdsc
, mds
);
1487 dout("do_request mds%d session %p state %s\n", mds
, session
,
1488 session_state_name(session
->s_state
));
1489 if (session
->s_state
!= CEPH_MDS_SESSION_OPEN
&&
1490 session
->s_state
!= CEPH_MDS_SESSION_HUNG
) {
1491 if (session
->s_state
== CEPH_MDS_SESSION_NEW
||
1492 session
->s_state
== CEPH_MDS_SESSION_CLOSING
)
1493 __open_session(mdsc
, session
);
1494 list_add(&req
->r_wait
, &session
->s_waiting
);
1499 req
->r_session
= get_session(session
);
1500 req
->r_resend_mds
= -1; /* forget any previous mds hint */
1502 if (req
->r_request_started
== 0) /* note request start time */
1503 req
->r_request_started
= jiffies
;
1505 err
= __prepare_send_request(mdsc
, req
, mds
);
1507 ceph_msg_get(req
->r_request
);
1508 ceph_con_send(&session
->s_con
, req
->r_request
);
1512 ceph_put_mds_session(session
);
1517 req
->r_reply
= ERR_PTR(err
);
1518 complete_request(mdsc
, req
);
1523 * called under mdsc->mutex
1525 static void __wake_requests(struct ceph_mds_client
*mdsc
,
1526 struct list_head
*head
)
1528 struct ceph_mds_request
*req
, *nreq
;
1530 list_for_each_entry_safe(req
, nreq
, head
, r_wait
) {
1531 list_del_init(&req
->r_wait
);
1532 __do_request(mdsc
, req
);
1537 * Wake up threads with requests pending for @mds, so that they can
1538 * resubmit their requests to a possibly different mds. If @all is set,
1539 * wake up if their requests has been forwarded to @mds, too.
1541 static void kick_requests(struct ceph_mds_client
*mdsc
, int mds
, int all
)
1543 struct ceph_mds_request
*reqs
[10];
1547 dout("kick_requests mds%d\n", mds
);
1548 while (nexttid
<= mdsc
->last_tid
) {
1549 got
= radix_tree_gang_lookup(&mdsc
->request_tree
,
1550 (void **)&reqs
, nexttid
, 10);
1553 nexttid
= reqs
[got
-1]->r_tid
+ 1;
1554 for (i
= 0; i
< got
; i
++) {
1555 if (reqs
[i
]->r_got_unsafe
)
1557 if (reqs
[i
]->r_session
&&
1558 reqs
[i
]->r_session
->s_mds
== mds
) {
1559 dout(" kicking tid %llu\n", reqs
[i
]->r_tid
);
1560 put_request_session(reqs
[i
]);
1561 __do_request(mdsc
, reqs
[i
]);
1567 void ceph_mdsc_submit_request(struct ceph_mds_client
*mdsc
,
1568 struct ceph_mds_request
*req
)
1570 dout("submit_request on %p\n", req
);
1571 mutex_lock(&mdsc
->mutex
);
1572 __register_request(mdsc
, req
, NULL
);
1573 __do_request(mdsc
, req
);
1574 mutex_unlock(&mdsc
->mutex
);
1578 * Synchrously perform an mds request. Take care of all of the
1579 * session setup, forwarding, retry details.
1581 int ceph_mdsc_do_request(struct ceph_mds_client
*mdsc
,
1583 struct ceph_mds_request
*req
)
1587 dout("do_request on %p\n", req
);
1589 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
1591 ceph_get_cap_refs(ceph_inode(req
->r_inode
), CEPH_CAP_PIN
);
1592 if (req
->r_locked_dir
)
1593 ceph_get_cap_refs(ceph_inode(req
->r_locked_dir
), CEPH_CAP_PIN
);
1594 if (req
->r_old_dentry
)
1596 ceph_inode(req
->r_old_dentry
->d_parent
->d_inode
),
1600 mutex_lock(&mdsc
->mutex
);
1601 __register_request(mdsc
, req
, dir
);
1602 __do_request(mdsc
, req
);
1605 if (!req
->r_reply
) {
1606 mutex_unlock(&mdsc
->mutex
);
1607 if (req
->r_timeout
) {
1608 err
= (long)wait_for_completion_interruptible_timeout(
1609 &req
->r_completion
, req
->r_timeout
);
1611 req
->r_reply
= ERR_PTR(-EIO
);
1613 req
->r_reply
= ERR_PTR(err
);
1615 err
= wait_for_completion_interruptible(
1616 &req
->r_completion
);
1618 req
->r_reply
= ERR_PTR(err
);
1620 mutex_lock(&mdsc
->mutex
);
1623 if (IS_ERR(req
->r_reply
)) {
1624 err
= PTR_ERR(req
->r_reply
);
1625 req
->r_reply
= NULL
;
1627 if (err
== -ERESTARTSYS
) {
1629 req
->r_aborted
= true;
1631 if (req
->r_locked_dir
&&
1632 (req
->r_op
& CEPH_MDS_OP_WRITE
)) {
1633 struct ceph_inode_info
*ci
=
1634 ceph_inode(req
->r_locked_dir
);
1636 dout("aborted, clearing I_COMPLETE on %p\n",
1638 spin_lock(&req
->r_locked_dir
->i_lock
);
1639 ci
->i_ceph_flags
&= ~CEPH_I_COMPLETE
;
1640 ci
->i_release_count
++;
1641 spin_unlock(&req
->r_locked_dir
->i_lock
);
1644 /* clean up this request */
1645 __unregister_request(mdsc
, req
);
1646 if (!list_empty(&req
->r_unsafe_item
))
1647 list_del_init(&req
->r_unsafe_item
);
1648 complete(&req
->r_safe_completion
);
1650 } else if (req
->r_err
) {
1653 err
= le32_to_cpu(req
->r_reply_info
.head
->result
);
1655 mutex_unlock(&mdsc
->mutex
);
1657 dout("do_request %p done, result %d\n", req
, err
);
1664 * We take the session mutex and parse and process the reply immediately.
1665 * This preserves the logical ordering of replies, capabilities, etc., sent
1666 * by the MDS as they are applied to our local cache.
1668 static void handle_reply(struct ceph_mds_session
*session
, struct ceph_msg
*msg
)
1670 struct ceph_mds_client
*mdsc
= session
->s_mdsc
;
1671 struct ceph_mds_request
*req
;
1672 struct ceph_mds_reply_head
*head
= msg
->front
.iov_base
;
1673 struct ceph_mds_reply_info_parsed
*rinfo
; /* parsed reply info */
1678 if (msg
->hdr
.src
.name
.type
!= CEPH_ENTITY_TYPE_MDS
)
1680 if (msg
->front
.iov_len
< sizeof(*head
)) {
1681 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
1686 /* get request, session */
1687 tid
= le64_to_cpu(msg
->hdr
.tid
);
1688 mutex_lock(&mdsc
->mutex
);
1689 req
= __lookup_request(mdsc
, tid
);
1691 dout("handle_reply on unknown tid %llu\n", tid
);
1692 mutex_unlock(&mdsc
->mutex
);
1695 dout("handle_reply %p\n", req
);
1696 mds
= le64_to_cpu(msg
->hdr
.src
.name
.num
);
1698 /* correct session? */
1699 if (!req
->r_session
&& req
->r_session
!= session
) {
1700 pr_err("mdsc_handle_reply got %llu on session mds%d"
1701 " not mds%d\n", tid
, session
->s_mds
,
1702 req
->r_session
? req
->r_session
->s_mds
: -1);
1703 mutex_unlock(&mdsc
->mutex
);
1708 if ((req
->r_got_unsafe
&& !head
->safe
) ||
1709 (req
->r_got_safe
&& head
->safe
)) {
1710 pr_warning("got a dup %s reply on %llu from mds%d\n",
1711 head
->safe
? "safe" : "unsafe", tid
, mds
);
1712 mutex_unlock(&mdsc
->mutex
);
1716 result
= le32_to_cpu(head
->result
);
1719 * Tolerate 2 consecutive ESTALEs from the same mds.
1720 * FIXME: we should be looking at the cap migrate_seq.
1722 if (result
== -ESTALE
) {
1723 req
->r_direct_mode
= USE_AUTH_MDS
;
1725 if (req
->r_num_stale
<= 2) {
1726 __do_request(mdsc
, req
);
1727 mutex_unlock(&mdsc
->mutex
);
1731 req
->r_num_stale
= 0;
1735 req
->r_got_safe
= true;
1736 __unregister_request(mdsc
, req
);
1737 complete(&req
->r_safe_completion
);
1739 if (req
->r_got_unsafe
) {
1741 * We already handled the unsafe response, now do the
1742 * cleanup. No need to examine the response; the MDS
1743 * doesn't include any result info in the safe
1744 * response. And even if it did, there is nothing
1745 * useful we could do with a revised return value.
1747 dout("got safe reply %llu, mds%d\n", tid
, mds
);
1748 list_del_init(&req
->r_unsafe_item
);
1750 /* last unsafe request during umount? */
1751 if (mdsc
->stopping
&& !__get_oldest_tid(mdsc
))
1752 complete(&mdsc
->safe_umount_waiters
);
1753 mutex_unlock(&mdsc
->mutex
);
1758 BUG_ON(req
->r_reply
);
1761 req
->r_got_unsafe
= true;
1762 list_add_tail(&req
->r_unsafe_item
, &req
->r_session
->s_unsafe
);
1765 dout("handle_reply tid %lld result %d\n", tid
, result
);
1766 rinfo
= &req
->r_reply_info
;
1767 err
= parse_reply_info(msg
, rinfo
);
1768 mutex_unlock(&mdsc
->mutex
);
1770 mutex_lock(&session
->s_mutex
);
1772 pr_err("mdsc_handle_reply got corrupt reply mds%d\n", mds
);
1778 if (rinfo
->snapblob_len
) {
1779 down_write(&mdsc
->snap_rwsem
);
1780 ceph_update_snap_trace(mdsc
, rinfo
->snapblob
,
1781 rinfo
->snapblob
+ rinfo
->snapblob_len
,
1782 le32_to_cpu(head
->op
) == CEPH_MDS_OP_RMSNAP
);
1783 downgrade_write(&mdsc
->snap_rwsem
);
1785 down_read(&mdsc
->snap_rwsem
);
1788 /* insert trace into our cache */
1789 err
= ceph_fill_trace(mdsc
->client
->sb
, req
, req
->r_session
);
1791 if (result
== 0 && rinfo
->dir_nr
)
1792 ceph_readdir_prepopulate(req
, req
->r_session
);
1793 ceph_unreserve_caps(&req
->r_caps_reservation
);
1796 up_read(&mdsc
->snap_rwsem
);
1805 add_cap_releases(mdsc
, req
->r_session
, -1);
1806 mutex_unlock(&session
->s_mutex
);
1808 /* kick calling process */
1809 complete_request(mdsc
, req
);
1811 ceph_mdsc_put_request(req
);
1818 * handle mds notification that our request has been forwarded.
1820 static void handle_forward(struct ceph_mds_client
*mdsc
, struct ceph_msg
*msg
)
1822 struct ceph_mds_request
*req
;
1828 void *p
= msg
->front
.iov_base
;
1829 void *end
= p
+ msg
->front
.iov_len
;
1830 int from_mds
, state
;
1832 if (msg
->hdr
.src
.name
.type
!= CEPH_ENTITY_TYPE_MDS
)
1834 from_mds
= le64_to_cpu(msg
->hdr
.src
.name
.num
);
1836 ceph_decode_need(&p
, end
, sizeof(u64
)+2*sizeof(u32
), bad
);
1837 tid
= ceph_decode_64(&p
);
1838 next_mds
= ceph_decode_32(&p
);
1839 fwd_seq
= ceph_decode_32(&p
);
1840 must_resend
= ceph_decode_8(&p
);
1842 WARN_ON(must_resend
); /* shouldn't happen. */
1844 mutex_lock(&mdsc
->mutex
);
1845 req
= __lookup_request(mdsc
, tid
);
1847 dout("forward %llu dne\n", tid
);
1848 goto out
; /* dup reply? */
1851 state
= mdsc
->sessions
[next_mds
]->s_state
;
1852 if (fwd_seq
<= req
->r_num_fwd
) {
1853 dout("forward %llu to mds%d - old seq %d <= %d\n",
1854 tid
, next_mds
, req
->r_num_fwd
, fwd_seq
);
1856 /* resend. forward race not possible; mds would drop */
1857 dout("forward %llu to mds%d (we resend)\n", tid
, next_mds
);
1858 req
->r_num_fwd
= fwd_seq
;
1859 req
->r_resend_mds
= next_mds
;
1860 put_request_session(req
);
1861 __do_request(mdsc
, req
);
1863 ceph_mdsc_put_request(req
);
1865 mutex_unlock(&mdsc
->mutex
);
1869 pr_err("mdsc_handle_forward decode error err=%d\n", err
);
1873 * handle a mds session control message
1875 static void handle_session(struct ceph_mds_session
*session
,
1876 struct ceph_msg
*msg
)
1878 struct ceph_mds_client
*mdsc
= session
->s_mdsc
;
1882 struct ceph_mds_session_head
*h
= msg
->front
.iov_base
;
1885 if (msg
->hdr
.src
.name
.type
!= CEPH_ENTITY_TYPE_MDS
)
1887 mds
= le64_to_cpu(msg
->hdr
.src
.name
.num
);
1890 if (msg
->front
.iov_len
!= sizeof(*h
))
1892 op
= le32_to_cpu(h
->op
);
1893 seq
= le64_to_cpu(h
->seq
);
1895 mutex_lock(&mdsc
->mutex
);
1896 /* FIXME: this ttl calculation is generous */
1897 session
->s_ttl
= jiffies
+ HZ
*mdsc
->mdsmap
->m_session_autoclose
;
1898 mutex_unlock(&mdsc
->mutex
);
1900 mutex_lock(&session
->s_mutex
);
1902 dout("handle_session mds%d %s %p state %s seq %llu\n",
1903 mds
, ceph_session_op_name(op
), session
,
1904 session_state_name(session
->s_state
), seq
);
1906 if (session
->s_state
== CEPH_MDS_SESSION_HUNG
) {
1907 session
->s_state
= CEPH_MDS_SESSION_OPEN
;
1908 pr_info("mds%d came back\n", session
->s_mds
);
1912 case CEPH_SESSION_OPEN
:
1913 session
->s_state
= CEPH_MDS_SESSION_OPEN
;
1914 renewed_caps(mdsc
, session
, 0);
1917 __close_session(mdsc
, session
);
1920 case CEPH_SESSION_RENEWCAPS
:
1921 if (session
->s_renew_seq
== seq
)
1922 renewed_caps(mdsc
, session
, 1);
1925 case CEPH_SESSION_CLOSE
:
1926 unregister_session(mdsc
, session
);
1927 remove_session_caps(session
);
1928 wake
= 1; /* for good measure */
1929 complete(&mdsc
->session_close_waiters
);
1930 kick_requests(mdsc
, mds
, 0); /* cur only */
1933 case CEPH_SESSION_STALE
:
1934 pr_info("mds%d caps went stale, renewing\n",
1936 spin_lock(&session
->s_cap_lock
);
1937 session
->s_cap_gen
++;
1938 session
->s_cap_ttl
= 0;
1939 spin_unlock(&session
->s_cap_lock
);
1940 send_renew_caps(mdsc
, session
);
1943 case CEPH_SESSION_RECALL_STATE
:
1944 trim_caps(mdsc
, session
, le32_to_cpu(h
->max_caps
));
1948 pr_err("mdsc_handle_session bad op %d mds%d\n", op
, mds
);
1952 mutex_unlock(&session
->s_mutex
);
1954 mutex_lock(&mdsc
->mutex
);
1955 __wake_requests(mdsc
, &session
->s_waiting
);
1956 mutex_unlock(&mdsc
->mutex
);
1961 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds
,
1962 (int)msg
->front
.iov_len
);
1969 * called under session->mutex.
1971 static void replay_unsafe_requests(struct ceph_mds_client
*mdsc
,
1972 struct ceph_mds_session
*session
)
1974 struct ceph_mds_request
*req
, *nreq
;
1977 dout("replay_unsafe_requests mds%d\n", session
->s_mds
);
1979 mutex_lock(&mdsc
->mutex
);
1980 list_for_each_entry_safe(req
, nreq
, &session
->s_unsafe
, r_unsafe_item
) {
1981 err
= __prepare_send_request(mdsc
, req
, session
->s_mds
);
1983 ceph_msg_get(req
->r_request
);
1984 ceph_con_send(&session
->s_con
, req
->r_request
);
1987 mutex_unlock(&mdsc
->mutex
);
1991 * Encode information about a cap for a reconnect with the MDS.
1993 static int encode_caps_cb(struct inode
*inode
, struct ceph_cap
*cap
,
1996 struct ceph_mds_cap_reconnect rec
;
1997 struct ceph_inode_info
*ci
;
1998 struct ceph_pagelist
*pagelist
= arg
;
2002 struct dentry
*dentry
;
2006 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2007 inode
, ceph_vinop(inode
), cap
, cap
->cap_id
,
2008 ceph_cap_string(cap
->issued
));
2009 err
= ceph_pagelist_encode_64(pagelist
, ceph_ino(inode
));
2013 dentry
= d_find_alias(inode
);
2015 path
= ceph_mdsc_build_path(dentry
, &pathlen
, &pathbase
, 0);
2017 err
= PTR_ERR(path
);
2024 err
= ceph_pagelist_encode_string(pagelist
, path
, pathlen
);
2028 spin_lock(&inode
->i_lock
);
2029 cap
->seq
= 0; /* reset cap seq */
2030 cap
->issue_seq
= 0; /* and issue_seq */
2031 rec
.cap_id
= cpu_to_le64(cap
->cap_id
);
2032 rec
.pathbase
= cpu_to_le64(pathbase
);
2033 rec
.wanted
= cpu_to_le32(__ceph_caps_wanted(ci
));
2034 rec
.issued
= cpu_to_le32(cap
->issued
);
2035 rec
.size
= cpu_to_le64(inode
->i_size
);
2036 ceph_encode_timespec(&rec
.mtime
, &inode
->i_mtime
);
2037 ceph_encode_timespec(&rec
.atime
, &inode
->i_atime
);
2038 rec
.snaprealm
= cpu_to_le64(ci
->i_snap_realm
->ino
);
2039 spin_unlock(&inode
->i_lock
);
2041 err
= ceph_pagelist_append(pagelist
, &rec
, sizeof(rec
));
2051 * If an MDS fails and recovers, clients need to reconnect in order to
2052 * reestablish shared state. This includes all caps issued through
2053 * this session _and_ the snap_realm hierarchy. Because it's not
2054 * clear which snap realms the mds cares about, we send everything we
2055 * know about.. that ensures we'll then get any new info the
2056 * recovering MDS might have.
2058 * This is a relatively heavyweight operation, but it's rare.
2060 * called with mdsc->mutex held.
2062 static void send_mds_reconnect(struct ceph_mds_client
*mdsc
, int mds
)
2064 struct ceph_mds_session
*session
= NULL
;
2065 struct ceph_msg
*reply
;
2068 u64 next_snap_ino
= 0;
2069 struct ceph_pagelist
*pagelist
;
2071 pr_info("reconnect to recovering mds%d\n", mds
);
2073 pagelist
= kmalloc(sizeof(*pagelist
), GFP_NOFS
);
2075 goto fail_nopagelist
;
2076 ceph_pagelist_init(pagelist
);
2078 reply
= ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT
, 0, 0, 0, NULL
);
2079 if (IS_ERR(reply
)) {
2080 err
= PTR_ERR(reply
);
2085 session
= __ceph_lookup_mds_session(mdsc
, mds
);
2086 mutex_unlock(&mdsc
->mutex
); /* drop lock for duration */
2089 mutex_lock(&session
->s_mutex
);
2091 session
->s_state
= CEPH_MDS_SESSION_RECONNECTING
;
2094 ceph_con_open(&session
->s_con
,
2095 ceph_mdsmap_get_addr(mdsc
->mdsmap
, mds
));
2097 /* replay unsafe requests */
2098 replay_unsafe_requests(mdsc
, session
);
2100 dout("no session for mds%d, will send short reconnect\n",
2104 down_read(&mdsc
->snap_rwsem
);
2108 dout("session %p state %s\n", session
,
2109 session_state_name(session
->s_state
));
2111 /* traverse this session's caps */
2112 err
= ceph_pagelist_encode_32(pagelist
, session
->s_nr_caps
);
2115 err
= iterate_session_caps(session
, encode_caps_cb
, pagelist
);
2120 * snaprealms. we provide mds with the ino, seq (version), and
2121 * parent for all of our realms. If the mds has any newer info,
2126 struct ceph_snap_realm
*realm
;
2127 struct ceph_mds_snaprealm_reconnect sr_rec
;
2128 got
= radix_tree_gang_lookup(&mdsc
->snap_realms
,
2129 (void **)&realm
, next_snap_ino
, 1);
2133 dout(" adding snap realm %llx seq %lld parent %llx\n",
2134 realm
->ino
, realm
->seq
, realm
->parent_ino
);
2135 sr_rec
.ino
= cpu_to_le64(realm
->ino
);
2136 sr_rec
.seq
= cpu_to_le64(realm
->seq
);
2137 sr_rec
.parent
= cpu_to_le64(realm
->parent_ino
);
2138 err
= ceph_pagelist_append(pagelist
, &sr_rec
, sizeof(sr_rec
));
2141 next_snap_ino
= realm
->ino
+ 1;
2145 reply
->pagelist
= pagelist
;
2146 reply
->hdr
.data_len
= cpu_to_le32(pagelist
->length
);
2147 reply
->nr_pages
= calc_pages_for(0, pagelist
->length
);
2148 ceph_con_send(&session
->s_con
, reply
);
2151 session
->s_state
= CEPH_MDS_SESSION_OPEN
;
2152 __wake_requests(mdsc
, &session
->s_waiting
);
2156 up_read(&mdsc
->snap_rwsem
);
2158 mutex_unlock(&session
->s_mutex
);
2159 ceph_put_mds_session(session
);
2161 mutex_lock(&mdsc
->mutex
);
2165 ceph_msg_put(reply
);
2167 ceph_pagelist_release(pagelist
);
2170 pr_err("ENOMEM preparing reconnect for mds%d\n", mds
);
2176 * compare old and new mdsmaps, kicking requests
2177 * and closing out old connections as necessary
2179 * called under mdsc->mutex.
2181 static void check_new_map(struct ceph_mds_client
*mdsc
,
2182 struct ceph_mdsmap
*newmap
,
2183 struct ceph_mdsmap
*oldmap
)
2186 int oldstate
, newstate
;
2187 struct ceph_mds_session
*s
;
2189 dout("check_new_map new %u old %u\n",
2190 newmap
->m_epoch
, oldmap
->m_epoch
);
2192 for (i
= 0; i
< oldmap
->m_max_mds
&& i
< mdsc
->max_sessions
; i
++) {
2193 if (mdsc
->sessions
[i
] == NULL
)
2195 s
= mdsc
->sessions
[i
];
2196 oldstate
= ceph_mdsmap_get_state(oldmap
, i
);
2197 newstate
= ceph_mdsmap_get_state(newmap
, i
);
2199 dout("check_new_map mds%d state %s -> %s (session %s)\n",
2200 i
, ceph_mds_state_name(oldstate
),
2201 ceph_mds_state_name(newstate
),
2202 session_state_name(s
->s_state
));
2204 if (memcmp(ceph_mdsmap_get_addr(oldmap
, i
),
2205 ceph_mdsmap_get_addr(newmap
, i
),
2206 sizeof(struct ceph_entity_addr
))) {
2207 if (s
->s_state
== CEPH_MDS_SESSION_OPENING
) {
2208 /* the session never opened, just close it
2210 __wake_requests(mdsc
, &s
->s_waiting
);
2211 unregister_session(mdsc
, s
);
2214 mutex_unlock(&mdsc
->mutex
);
2215 mutex_lock(&s
->s_mutex
);
2216 mutex_lock(&mdsc
->mutex
);
2217 ceph_con_close(&s
->s_con
);
2218 mutex_unlock(&s
->s_mutex
);
2219 s
->s_state
= CEPH_MDS_SESSION_RESTARTING
;
2222 /* kick any requests waiting on the recovering mds */
2223 kick_requests(mdsc
, i
, 1);
2224 } else if (oldstate
== newstate
) {
2225 continue; /* nothing new with this mds */
2231 if (s
->s_state
== CEPH_MDS_SESSION_RESTARTING
&&
2232 newstate
>= CEPH_MDS_STATE_RECONNECT
)
2233 send_mds_reconnect(mdsc
, i
);
2236 * kick requests on any mds that has gone active.
2238 * kick requests on cur or forwarder: we may have sent
2239 * the request to mds1, mds1 told us it forwarded it
2240 * to mds2, but then we learn mds1 failed and can't be
2241 * sure it successfully forwarded our request before
2244 if (oldstate
< CEPH_MDS_STATE_ACTIVE
&&
2245 newstate
>= CEPH_MDS_STATE_ACTIVE
) {
2246 pr_info("mds%d reconnect completed\n", s
->s_mds
);
2247 kick_requests(mdsc
, i
, 1);
2248 ceph_kick_flushing_caps(mdsc
, s
);
2249 wake_up_session_caps(s
, 1);
2261 * caller must hold session s_mutex, dentry->d_lock
2263 void __ceph_mdsc_drop_dentry_lease(struct dentry
*dentry
)
2265 struct ceph_dentry_info
*di
= ceph_dentry(dentry
);
2267 ceph_put_mds_session(di
->lease_session
);
2268 di
->lease_session
= NULL
;
2271 static void handle_lease(struct ceph_mds_client
*mdsc
, struct ceph_msg
*msg
)
2273 struct super_block
*sb
= mdsc
->client
->sb
;
2274 struct inode
*inode
;
2275 struct ceph_mds_session
*session
;
2276 struct ceph_inode_info
*ci
;
2277 struct dentry
*parent
, *dentry
;
2278 struct ceph_dentry_info
*di
;
2280 struct ceph_mds_lease
*h
= msg
->front
.iov_base
;
2281 struct ceph_vino vino
;
2286 if (msg
->hdr
.src
.name
.type
!= CEPH_ENTITY_TYPE_MDS
)
2288 mds
= le64_to_cpu(msg
->hdr
.src
.name
.num
);
2289 dout("handle_lease from mds%d\n", mds
);
2292 if (msg
->front
.iov_len
< sizeof(*h
) + sizeof(u32
))
2294 vino
.ino
= le64_to_cpu(h
->ino
);
2295 vino
.snap
= CEPH_NOSNAP
;
2296 mask
= le16_to_cpu(h
->mask
);
2297 dname
.name
= (void *)h
+ sizeof(*h
) + sizeof(u32
);
2298 dname
.len
= msg
->front
.iov_len
- sizeof(*h
) - sizeof(u32
);
2299 if (dname
.len
!= get_unaligned_le32(h
+1))
2303 mutex_lock(&mdsc
->mutex
);
2304 session
= __ceph_lookup_mds_session(mdsc
, mds
);
2305 mutex_unlock(&mdsc
->mutex
);
2307 pr_err("handle_lease got lease but no session mds%d\n", mds
);
2311 mutex_lock(&session
->s_mutex
);
2315 inode
= ceph_find_inode(sb
, vino
);
2316 dout("handle_lease '%s', mask %d, ino %llx %p\n",
2317 ceph_lease_op_name(h
->action
), mask
, vino
.ino
, inode
);
2318 if (inode
== NULL
) {
2319 dout("handle_lease no inode %llx\n", vino
.ino
);
2322 ci
= ceph_inode(inode
);
2325 parent
= d_find_alias(inode
);
2327 dout("no parent dentry on inode %p\n", inode
);
2329 goto release
; /* hrm... */
2331 dname
.hash
= full_name_hash(dname
.name
, dname
.len
);
2332 dentry
= d_lookup(parent
, &dname
);
2337 spin_lock(&dentry
->d_lock
);
2338 di
= ceph_dentry(dentry
);
2339 switch (h
->action
) {
2340 case CEPH_MDS_LEASE_REVOKE
:
2341 if (di
&& di
->lease_session
== session
) {
2342 h
->seq
= cpu_to_le32(di
->lease_seq
);
2343 __ceph_mdsc_drop_dentry_lease(dentry
);
2348 case CEPH_MDS_LEASE_RENEW
:
2349 if (di
&& di
->lease_session
== session
&&
2350 di
->lease_gen
== session
->s_cap_gen
&&
2351 di
->lease_renew_from
&&
2352 di
->lease_renew_after
== 0) {
2353 unsigned long duration
=
2354 le32_to_cpu(h
->duration_ms
) * HZ
/ 1000;
2356 di
->lease_seq
= le32_to_cpu(h
->seq
);
2357 dentry
->d_time
= di
->lease_renew_from
+ duration
;
2358 di
->lease_renew_after
= di
->lease_renew_from
+
2360 di
->lease_renew_from
= 0;
2364 spin_unlock(&dentry
->d_lock
);
2371 /* let's just reuse the same message */
2372 h
->action
= CEPH_MDS_LEASE_REVOKE_ACK
;
2374 ceph_con_send(&session
->s_con
, msg
);
2378 mutex_unlock(&session
->s_mutex
);
2379 ceph_put_mds_session(session
);
2383 pr_err("corrupt lease message\n");
2387 void ceph_mdsc_lease_send_msg(struct ceph_mds_session
*session
,
2388 struct inode
*inode
,
2389 struct dentry
*dentry
, char action
,
2392 struct ceph_msg
*msg
;
2393 struct ceph_mds_lease
*lease
;
2394 int len
= sizeof(*lease
) + sizeof(u32
);
2397 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
2398 inode
, dentry
, ceph_lease_op_name(action
), session
->s_mds
);
2399 dnamelen
= dentry
->d_name
.len
;
2402 msg
= ceph_msg_new(CEPH_MSG_CLIENT_LEASE
, len
, 0, 0, NULL
);
2405 lease
= msg
->front
.iov_base
;
2406 lease
->action
= action
;
2407 lease
->mask
= cpu_to_le16(CEPH_LOCK_DN
);
2408 lease
->ino
= cpu_to_le64(ceph_vino(inode
).ino
);
2409 lease
->first
= lease
->last
= cpu_to_le64(ceph_vino(inode
).snap
);
2410 lease
->seq
= cpu_to_le32(seq
);
2411 put_unaligned_le32(dnamelen
, lease
+ 1);
2412 memcpy((void *)(lease
+ 1) + 4, dentry
->d_name
.name
, dnamelen
);
2415 * if this is a preemptive lease RELEASE, no need to
2416 * flush request stream, since the actual request will
2419 msg
->more_to_follow
= (action
== CEPH_MDS_LEASE_RELEASE
);
2421 ceph_con_send(&session
->s_con
, msg
);
2425 * Preemptively release a lease we expect to invalidate anyway.
2426 * Pass @inode always, @dentry is optional.
2428 void ceph_mdsc_lease_release(struct ceph_mds_client
*mdsc
, struct inode
*inode
,
2429 struct dentry
*dentry
, int mask
)
2431 struct ceph_dentry_info
*di
;
2432 struct ceph_mds_session
*session
;
2435 BUG_ON(inode
== NULL
);
2436 BUG_ON(dentry
== NULL
);
2437 BUG_ON(mask
!= CEPH_LOCK_DN
);
2439 /* is dentry lease valid? */
2440 spin_lock(&dentry
->d_lock
);
2441 di
= ceph_dentry(dentry
);
2442 if (!di
|| !di
->lease_session
||
2443 di
->lease_session
->s_mds
< 0 ||
2444 di
->lease_gen
!= di
->lease_session
->s_cap_gen
||
2445 !time_before(jiffies
, dentry
->d_time
)) {
2446 dout("lease_release inode %p dentry %p -- "
2448 inode
, dentry
, mask
);
2449 spin_unlock(&dentry
->d_lock
);
2453 /* we do have a lease on this dentry; note mds and seq */
2454 session
= ceph_get_mds_session(di
->lease_session
);
2455 seq
= di
->lease_seq
;
2456 __ceph_mdsc_drop_dentry_lease(dentry
);
2457 spin_unlock(&dentry
->d_lock
);
2459 dout("lease_release inode %p dentry %p mask %d to mds%d\n",
2460 inode
, dentry
, mask
, session
->s_mds
);
2461 ceph_mdsc_lease_send_msg(session
, inode
, dentry
,
2462 CEPH_MDS_LEASE_RELEASE
, seq
);
2463 ceph_put_mds_session(session
);
2467 * drop all leases (and dentry refs) in preparation for umount
2469 static void drop_leases(struct ceph_mds_client
*mdsc
)
2473 dout("drop_leases\n");
2474 mutex_lock(&mdsc
->mutex
);
2475 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
2476 struct ceph_mds_session
*s
= __ceph_lookup_mds_session(mdsc
, i
);
2479 mutex_unlock(&mdsc
->mutex
);
2480 mutex_lock(&s
->s_mutex
);
2481 mutex_unlock(&s
->s_mutex
);
2482 ceph_put_mds_session(s
);
2483 mutex_lock(&mdsc
->mutex
);
2485 mutex_unlock(&mdsc
->mutex
);
2491 * delayed work -- periodically trim expired leases, renew caps with mds
2493 static void schedule_delayed(struct ceph_mds_client
*mdsc
)
2496 unsigned hz
= round_jiffies_relative(HZ
* delay
);
2497 schedule_delayed_work(&mdsc
->delayed_work
, hz
);
2500 static void delayed_work(struct work_struct
*work
)
2503 struct ceph_mds_client
*mdsc
=
2504 container_of(work
, struct ceph_mds_client
, delayed_work
.work
);
2508 dout("mdsc delayed_work\n");
2509 ceph_check_delayed_caps(mdsc
);
2511 mutex_lock(&mdsc
->mutex
);
2512 renew_interval
= mdsc
->mdsmap
->m_session_timeout
>> 2;
2513 renew_caps
= time_after_eq(jiffies
, HZ
*renew_interval
+
2514 mdsc
->last_renew_caps
);
2516 mdsc
->last_renew_caps
= jiffies
;
2518 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
2519 struct ceph_mds_session
*s
= __ceph_lookup_mds_session(mdsc
, i
);
2522 if (s
->s_state
== CEPH_MDS_SESSION_CLOSING
) {
2523 dout("resending session close request for mds%d\n",
2525 request_close_session(mdsc
, s
);
2526 ceph_put_mds_session(s
);
2529 if (s
->s_ttl
&& time_after(jiffies
, s
->s_ttl
)) {
2530 if (s
->s_state
== CEPH_MDS_SESSION_OPEN
) {
2531 s
->s_state
= CEPH_MDS_SESSION_HUNG
;
2532 pr_info("mds%d hung\n", s
->s_mds
);
2535 if (s
->s_state
< CEPH_MDS_SESSION_OPEN
) {
2536 /* this mds is failed or recovering, just wait */
2537 ceph_put_mds_session(s
);
2540 mutex_unlock(&mdsc
->mutex
);
2542 mutex_lock(&s
->s_mutex
);
2544 send_renew_caps(mdsc
, s
);
2546 ceph_con_keepalive(&s
->s_con
);
2547 add_cap_releases(mdsc
, s
, -1);
2548 send_cap_releases(mdsc
, s
);
2549 mutex_unlock(&s
->s_mutex
);
2550 ceph_put_mds_session(s
);
2552 mutex_lock(&mdsc
->mutex
);
2554 mutex_unlock(&mdsc
->mutex
);
2556 schedule_delayed(mdsc
);
2560 int ceph_mdsc_init(struct ceph_mds_client
*mdsc
, struct ceph_client
*client
)
2562 mdsc
->client
= client
;
2563 mutex_init(&mdsc
->mutex
);
2564 mdsc
->mdsmap
= kzalloc(sizeof(*mdsc
->mdsmap
), GFP_NOFS
);
2565 init_completion(&mdsc
->safe_umount_waiters
);
2566 init_completion(&mdsc
->session_close_waiters
);
2567 INIT_LIST_HEAD(&mdsc
->waiting_for_map
);
2568 mdsc
->sessions
= NULL
;
2569 mdsc
->max_sessions
= 0;
2571 init_rwsem(&mdsc
->snap_rwsem
);
2572 INIT_RADIX_TREE(&mdsc
->snap_realms
, GFP_NOFS
);
2573 INIT_LIST_HEAD(&mdsc
->snap_empty
);
2574 spin_lock_init(&mdsc
->snap_empty_lock
);
2576 INIT_RADIX_TREE(&mdsc
->request_tree
, GFP_NOFS
);
2577 INIT_DELAYED_WORK(&mdsc
->delayed_work
, delayed_work
);
2578 mdsc
->last_renew_caps
= jiffies
;
2579 INIT_LIST_HEAD(&mdsc
->cap_delay_list
);
2580 spin_lock_init(&mdsc
->cap_delay_lock
);
2581 INIT_LIST_HEAD(&mdsc
->snap_flush_list
);
2582 spin_lock_init(&mdsc
->snap_flush_lock
);
2583 mdsc
->cap_flush_seq
= 0;
2584 INIT_LIST_HEAD(&mdsc
->cap_dirty
);
2585 mdsc
->num_cap_flushing
= 0;
2586 spin_lock_init(&mdsc
->cap_dirty_lock
);
2587 init_waitqueue_head(&mdsc
->cap_flushing_wq
);
2588 spin_lock_init(&mdsc
->dentry_lru_lock
);
2589 INIT_LIST_HEAD(&mdsc
->dentry_lru
);
2594 * Wait for safe replies on open mds requests. If we time out, drop
2595 * all requests from the tree to avoid dangling dentry refs.
2597 static void wait_requests(struct ceph_mds_client
*mdsc
)
2599 struct ceph_mds_request
*req
;
2600 struct ceph_client
*client
= mdsc
->client
;
2602 mutex_lock(&mdsc
->mutex
);
2603 if (__get_oldest_tid(mdsc
)) {
2604 mutex_unlock(&mdsc
->mutex
);
2605 dout("wait_requests waiting for requests\n");
2606 wait_for_completion_timeout(&mdsc
->safe_umount_waiters
,
2607 client
->mount_args
->mount_timeout
* HZ
);
2608 mutex_lock(&mdsc
->mutex
);
2610 /* tear down remaining requests */
2611 while (radix_tree_gang_lookup(&mdsc
->request_tree
,
2612 (void **)&req
, 0, 1)) {
2613 dout("wait_requests timed out on tid %llu\n",
2615 radix_tree_delete(&mdsc
->request_tree
, req
->r_tid
);
2616 ceph_mdsc_put_request(req
);
2619 mutex_unlock(&mdsc
->mutex
);
2620 dout("wait_requests done\n");
2624 * called before mount is ro, and before dentries are torn down.
2625 * (hmm, does this still race with new lookups?)
2627 void ceph_mdsc_pre_umount(struct ceph_mds_client
*mdsc
)
2629 dout("pre_umount\n");
2633 ceph_flush_dirty_caps(mdsc
);
2634 wait_requests(mdsc
);
2638 * wait for all write mds requests to flush.
2640 static void wait_unsafe_requests(struct ceph_mds_client
*mdsc
, u64 want_tid
)
2642 struct ceph_mds_request
*req
;
2646 mutex_lock(&mdsc
->mutex
);
2647 dout("wait_unsafe_requests want %lld\n", want_tid
);
2649 got
= radix_tree_gang_lookup(&mdsc
->request_tree
, (void **)&req
,
2653 if (req
->r_tid
> want_tid
)
2656 next_tid
= req
->r_tid
+ 1;
2657 if ((req
->r_op
& CEPH_MDS_OP_WRITE
) == 0)
2658 continue; /* not a write op */
2660 ceph_mdsc_get_request(req
);
2661 mutex_unlock(&mdsc
->mutex
);
2662 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
2663 req
->r_tid
, want_tid
);
2664 wait_for_completion(&req
->r_safe_completion
);
2665 mutex_lock(&mdsc
->mutex
);
2666 ceph_mdsc_put_request(req
);
2668 mutex_unlock(&mdsc
->mutex
);
2669 dout("wait_unsafe_requests done\n");
2672 void ceph_mdsc_sync(struct ceph_mds_client
*mdsc
)
2674 u64 want_tid
, want_flush
;
2677 mutex_lock(&mdsc
->mutex
);
2678 want_tid
= mdsc
->last_tid
;
2679 want_flush
= mdsc
->cap_flush_seq
;
2680 mutex_unlock(&mdsc
->mutex
);
2681 dout("sync want tid %lld flush_seq %lld\n", want_tid
, want_flush
);
2683 ceph_flush_dirty_caps(mdsc
);
2685 wait_unsafe_requests(mdsc
, want_tid
);
2686 wait_event(mdsc
->cap_flushing_wq
, check_cap_flush(mdsc
, want_flush
));
2691 * called after sb is ro.
2693 void ceph_mdsc_close_sessions(struct ceph_mds_client
*mdsc
)
2695 struct ceph_mds_session
*session
;
2698 struct ceph_client
*client
= mdsc
->client
;
2699 unsigned long started
, timeout
= client
->mount_args
->mount_timeout
* HZ
;
2701 dout("close_sessions\n");
2703 mutex_lock(&mdsc
->mutex
);
2705 /* close sessions */
2707 while (time_before(jiffies
, started
+ timeout
)) {
2708 dout("closing sessions\n");
2710 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
2711 session
= __ceph_lookup_mds_session(mdsc
, i
);
2714 mutex_unlock(&mdsc
->mutex
);
2715 mutex_lock(&session
->s_mutex
);
2716 __close_session(mdsc
, session
);
2717 mutex_unlock(&session
->s_mutex
);
2718 ceph_put_mds_session(session
);
2719 mutex_lock(&mdsc
->mutex
);
2725 if (client
->mount_state
== CEPH_MOUNT_SHUTDOWN
)
2728 dout("waiting for sessions to close\n");
2729 mutex_unlock(&mdsc
->mutex
);
2730 wait_for_completion_timeout(&mdsc
->session_close_waiters
,
2732 mutex_lock(&mdsc
->mutex
);
2735 /* tear down remaining sessions */
2736 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
2737 if (mdsc
->sessions
[i
]) {
2738 session
= get_session(mdsc
->sessions
[i
]);
2739 unregister_session(mdsc
, session
);
2740 mutex_unlock(&mdsc
->mutex
);
2741 mutex_lock(&session
->s_mutex
);
2742 remove_session_caps(session
);
2743 mutex_unlock(&session
->s_mutex
);
2744 ceph_put_mds_session(session
);
2745 mutex_lock(&mdsc
->mutex
);
2749 WARN_ON(!list_empty(&mdsc
->cap_delay_list
));
2751 mutex_unlock(&mdsc
->mutex
);
2753 ceph_cleanup_empty_realms(mdsc
);
2755 cancel_delayed_work_sync(&mdsc
->delayed_work
); /* cancel timer */
2760 void ceph_mdsc_stop(struct ceph_mds_client
*mdsc
)
2763 cancel_delayed_work_sync(&mdsc
->delayed_work
); /* cancel timer */
2765 ceph_mdsmap_destroy(mdsc
->mdsmap
);
2766 kfree(mdsc
->sessions
);
2771 * handle mds map update.
2773 void ceph_mdsc_handle_map(struct ceph_mds_client
*mdsc
, struct ceph_msg
*msg
)
2777 void *p
= msg
->front
.iov_base
;
2778 void *end
= p
+ msg
->front
.iov_len
;
2779 struct ceph_mdsmap
*newmap
, *oldmap
;
2780 struct ceph_fsid fsid
;
2783 ceph_decode_need(&p
, end
, sizeof(fsid
)+2*sizeof(u32
), bad
);
2784 ceph_decode_copy(&p
, &fsid
, sizeof(fsid
));
2785 if (ceph_check_fsid(mdsc
->client
, &fsid
) < 0)
2787 epoch
= ceph_decode_32(&p
);
2788 maplen
= ceph_decode_32(&p
);
2789 dout("handle_map epoch %u len %d\n", epoch
, (int)maplen
);
2791 /* do we need it? */
2792 ceph_monc_got_mdsmap(&mdsc
->client
->monc
, epoch
);
2793 mutex_lock(&mdsc
->mutex
);
2794 if (mdsc
->mdsmap
&& epoch
<= mdsc
->mdsmap
->m_epoch
) {
2795 dout("handle_map epoch %u <= our %u\n",
2796 epoch
, mdsc
->mdsmap
->m_epoch
);
2797 mutex_unlock(&mdsc
->mutex
);
2801 newmap
= ceph_mdsmap_decode(&p
, end
);
2802 if (IS_ERR(newmap
)) {
2803 err
= PTR_ERR(newmap
);
2807 /* swap into place */
2809 oldmap
= mdsc
->mdsmap
;
2810 mdsc
->mdsmap
= newmap
;
2811 check_new_map(mdsc
, newmap
, oldmap
);
2812 ceph_mdsmap_destroy(oldmap
);
2814 mdsc
->mdsmap
= newmap
; /* first mds map */
2816 mdsc
->client
->sb
->s_maxbytes
= mdsc
->mdsmap
->m_max_file_size
;
2818 __wake_requests(mdsc
, &mdsc
->waiting_for_map
);
2820 mutex_unlock(&mdsc
->mutex
);
2821 schedule_delayed(mdsc
);
2825 mutex_unlock(&mdsc
->mutex
);
2827 pr_err("error decoding mdsmap %d\n", err
);
2831 static struct ceph_connection
*con_get(struct ceph_connection
*con
)
2833 struct ceph_mds_session
*s
= con
->private;
2835 if (get_session(s
)) {
2836 dout("mdsc con_get %p %d -> %d\n", s
,
2837 atomic_read(&s
->s_ref
) - 1, atomic_read(&s
->s_ref
));
2840 dout("mdsc con_get %p FAIL\n", s
);
2844 static void con_put(struct ceph_connection
*con
)
2846 struct ceph_mds_session
*s
= con
->private;
2848 dout("mdsc con_put %p %d -> %d\n", s
, atomic_read(&s
->s_ref
),
2849 atomic_read(&s
->s_ref
) - 1);
2850 ceph_put_mds_session(s
);
2854 * if the client is unresponsive for long enough, the mds will kill
2855 * the session entirely.
2857 static void peer_reset(struct ceph_connection
*con
)
2859 struct ceph_mds_session
*s
= con
->private;
2861 pr_err("mds%d gave us the boot. IMPLEMENT RECONNECT.\n",
2865 static void dispatch(struct ceph_connection
*con
, struct ceph_msg
*msg
)
2867 struct ceph_mds_session
*s
= con
->private;
2868 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
2869 int type
= le16_to_cpu(msg
->hdr
.type
);
2872 case CEPH_MSG_MDS_MAP
:
2873 ceph_mdsc_handle_map(mdsc
, msg
);
2875 case CEPH_MSG_CLIENT_SESSION
:
2876 handle_session(s
, msg
);
2878 case CEPH_MSG_CLIENT_REPLY
:
2879 handle_reply(s
, msg
);
2881 case CEPH_MSG_CLIENT_REQUEST_FORWARD
:
2882 handle_forward(mdsc
, msg
);
2884 case CEPH_MSG_CLIENT_CAPS
:
2885 ceph_handle_caps(s
, msg
);
2887 case CEPH_MSG_CLIENT_SNAP
:
2888 ceph_handle_snap(mdsc
, msg
);
2890 case CEPH_MSG_CLIENT_LEASE
:
2891 handle_lease(mdsc
, msg
);
2895 pr_err("received unknown message type %d %s\n", type
,
2896 ceph_msg_type_name(type
));
2904 static int get_authorizer(struct ceph_connection
*con
,
2905 void **buf
, int *len
, int *proto
,
2906 void **reply_buf
, int *reply_len
, int force_new
)
2908 struct ceph_mds_session
*s
= con
->private;
2909 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
2910 struct ceph_auth_client
*ac
= mdsc
->client
->monc
.auth
;
2913 if (force_new
&& s
->s_authorizer
) {
2914 ac
->ops
->destroy_authorizer(ac
, s
->s_authorizer
);
2915 s
->s_authorizer
= NULL
;
2917 if (s
->s_authorizer
== NULL
) {
2918 if (ac
->ops
->create_authorizer
) {
2919 ret
= ac
->ops
->create_authorizer(
2920 ac
, CEPH_ENTITY_TYPE_MDS
,
2922 &s
->s_authorizer_buf
,
2923 &s
->s_authorizer_buf_len
,
2924 &s
->s_authorizer_reply_buf
,
2925 &s
->s_authorizer_reply_buf_len
);
2931 *proto
= ac
->protocol
;
2932 *buf
= s
->s_authorizer_buf
;
2933 *len
= s
->s_authorizer_buf_len
;
2934 *reply_buf
= s
->s_authorizer_reply_buf
;
2935 *reply_len
= s
->s_authorizer_reply_buf_len
;
2940 static int verify_authorizer_reply(struct ceph_connection
*con
, int len
)
2942 struct ceph_mds_session
*s
= con
->private;
2943 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
2944 struct ceph_auth_client
*ac
= mdsc
->client
->monc
.auth
;
2946 return ac
->ops
->verify_authorizer_reply(ac
, s
->s_authorizer
, len
);
2949 const static struct ceph_connection_operations mds_con_ops
= {
2952 .dispatch
= dispatch
,
2953 .get_authorizer
= get_authorizer
,
2954 .verify_authorizer_reply
= verify_authorizer_reply
,
2955 .peer_reset
= peer_reset
,