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
];
312 static int __verify_registered_session(struct ceph_mds_client
*mdsc
,
313 struct ceph_mds_session
*s
)
315 if (s
->s_mds
>= mdsc
->max_sessions
||
316 mdsc
->sessions
[s
->s_mds
] != s
)
322 * create+register a new session for given mds.
323 * called under mdsc->mutex.
325 static struct ceph_mds_session
*register_session(struct ceph_mds_client
*mdsc
,
328 struct ceph_mds_session
*s
;
330 s
= kzalloc(sizeof(*s
), GFP_NOFS
);
333 s
->s_state
= CEPH_MDS_SESSION_NEW
;
336 mutex_init(&s
->s_mutex
);
338 ceph_con_init(mdsc
->client
->msgr
, &s
->s_con
);
339 s
->s_con
.private = s
;
340 s
->s_con
.ops
= &mds_con_ops
;
341 s
->s_con
.peer_name
.type
= CEPH_ENTITY_TYPE_MDS
;
342 s
->s_con
.peer_name
.num
= cpu_to_le64(mds
);
344 spin_lock_init(&s
->s_cap_lock
);
347 s
->s_renew_requested
= 0;
349 INIT_LIST_HEAD(&s
->s_caps
);
352 atomic_set(&s
->s_ref
, 1);
353 INIT_LIST_HEAD(&s
->s_waiting
);
354 INIT_LIST_HEAD(&s
->s_unsafe
);
355 s
->s_num_cap_releases
= 0;
356 s
->s_cap_iterator
= NULL
;
357 INIT_LIST_HEAD(&s
->s_cap_releases
);
358 INIT_LIST_HEAD(&s
->s_cap_releases_done
);
359 INIT_LIST_HEAD(&s
->s_cap_flushing
);
360 INIT_LIST_HEAD(&s
->s_cap_snaps_flushing
);
362 dout("register_session mds%d\n", mds
);
363 if (mds
>= mdsc
->max_sessions
) {
364 int newmax
= 1 << get_count_order(mds
+1);
365 struct ceph_mds_session
**sa
;
367 dout("register_session realloc to %d\n", newmax
);
368 sa
= kcalloc(newmax
, sizeof(void *), GFP_NOFS
);
371 if (mdsc
->sessions
) {
372 memcpy(sa
, mdsc
->sessions
,
373 mdsc
->max_sessions
* sizeof(void *));
374 kfree(mdsc
->sessions
);
377 mdsc
->max_sessions
= newmax
;
379 mdsc
->sessions
[mds
] = s
;
380 atomic_inc(&s
->s_ref
); /* one ref to sessions[], one to caller */
382 ceph_con_open(&s
->s_con
, ceph_mdsmap_get_addr(mdsc
->mdsmap
, mds
));
388 return ERR_PTR(-ENOMEM
);
392 * called under mdsc->mutex
394 static void __unregister_session(struct ceph_mds_client
*mdsc
,
395 struct ceph_mds_session
*s
)
397 dout("__unregister_session mds%d %p\n", s
->s_mds
, s
);
398 BUG_ON(mdsc
->sessions
[s
->s_mds
] != s
);
399 mdsc
->sessions
[s
->s_mds
] = NULL
;
400 ceph_con_close(&s
->s_con
);
401 ceph_put_mds_session(s
);
405 * drop session refs in request.
407 * should be last request ref, or hold mdsc->mutex
409 static void put_request_session(struct ceph_mds_request
*req
)
411 if (req
->r_session
) {
412 ceph_put_mds_session(req
->r_session
);
413 req
->r_session
= NULL
;
417 void ceph_mdsc_release_request(struct kref
*kref
)
419 struct ceph_mds_request
*req
= container_of(kref
,
420 struct ceph_mds_request
,
423 ceph_msg_put(req
->r_request
);
425 ceph_msg_put(req
->r_reply
);
426 destroy_reply_info(&req
->r_reply_info
);
429 ceph_put_cap_refs(ceph_inode(req
->r_inode
),
433 if (req
->r_locked_dir
)
434 ceph_put_cap_refs(ceph_inode(req
->r_locked_dir
),
436 if (req
->r_target_inode
)
437 iput(req
->r_target_inode
);
440 if (req
->r_old_dentry
) {
442 ceph_inode(req
->r_old_dentry
->d_parent
->d_inode
),
444 dput(req
->r_old_dentry
);
448 put_request_session(req
);
449 ceph_unreserve_caps(&req
->r_caps_reservation
);
454 * lookup session, bump ref if found.
456 * called under mdsc->mutex.
458 static struct ceph_mds_request
*__lookup_request(struct ceph_mds_client
*mdsc
,
461 struct ceph_mds_request
*req
;
462 struct rb_node
*n
= mdsc
->request_tree
.rb_node
;
465 req
= rb_entry(n
, struct ceph_mds_request
, r_node
);
466 if (tid
< req
->r_tid
)
468 else if (tid
> req
->r_tid
)
471 ceph_mdsc_get_request(req
);
478 static void __insert_request(struct ceph_mds_client
*mdsc
,
479 struct ceph_mds_request
*new)
481 struct rb_node
**p
= &mdsc
->request_tree
.rb_node
;
482 struct rb_node
*parent
= NULL
;
483 struct ceph_mds_request
*req
= NULL
;
487 req
= rb_entry(parent
, struct ceph_mds_request
, r_node
);
488 if (new->r_tid
< req
->r_tid
)
490 else if (new->r_tid
> req
->r_tid
)
496 rb_link_node(&new->r_node
, parent
, p
);
497 rb_insert_color(&new->r_node
, &mdsc
->request_tree
);
501 * Register an in-flight request, and assign a tid. Link to directory
502 * are modifying (if any).
504 * Called under mdsc->mutex.
506 static void __register_request(struct ceph_mds_client
*mdsc
,
507 struct ceph_mds_request
*req
,
510 req
->r_tid
= ++mdsc
->last_tid
;
512 ceph_reserve_caps(&req
->r_caps_reservation
, req
->r_num_caps
);
513 dout("__register_request %p tid %lld\n", req
, req
->r_tid
);
514 ceph_mdsc_get_request(req
);
515 __insert_request(mdsc
, req
);
518 struct ceph_inode_info
*ci
= ceph_inode(dir
);
520 spin_lock(&ci
->i_unsafe_lock
);
521 req
->r_unsafe_dir
= dir
;
522 list_add_tail(&req
->r_unsafe_dir_item
, &ci
->i_unsafe_dirops
);
523 spin_unlock(&ci
->i_unsafe_lock
);
527 static void __unregister_request(struct ceph_mds_client
*mdsc
,
528 struct ceph_mds_request
*req
)
530 dout("__unregister_request %p tid %lld\n", req
, req
->r_tid
);
531 rb_erase(&req
->r_node
, &mdsc
->request_tree
);
532 ceph_mdsc_put_request(req
);
534 if (req
->r_unsafe_dir
) {
535 struct ceph_inode_info
*ci
= ceph_inode(req
->r_unsafe_dir
);
537 spin_lock(&ci
->i_unsafe_lock
);
538 list_del_init(&req
->r_unsafe_dir_item
);
539 spin_unlock(&ci
->i_unsafe_lock
);
544 * Choose mds to send request to next. If there is a hint set in the
545 * request (e.g., due to a prior forward hint from the mds), use that.
546 * Otherwise, consult frag tree and/or caps to identify the
547 * appropriate mds. If all else fails, choose randomly.
549 * Called under mdsc->mutex.
551 static int __choose_mds(struct ceph_mds_client
*mdsc
,
552 struct ceph_mds_request
*req
)
555 struct ceph_inode_info
*ci
;
556 struct ceph_cap
*cap
;
557 int mode
= req
->r_direct_mode
;
559 u32 hash
= req
->r_direct_hash
;
560 bool is_hash
= req
->r_direct_is_hash
;
563 * is there a specific mds we should try? ignore hint if we have
564 * no session and the mds is not up (active or recovering).
566 if (req
->r_resend_mds
>= 0 &&
567 (__have_session(mdsc
, req
->r_resend_mds
) ||
568 ceph_mdsmap_get_state(mdsc
->mdsmap
, req
->r_resend_mds
) > 0)) {
569 dout("choose_mds using resend_mds mds%d\n",
571 return req
->r_resend_mds
;
574 if (mode
== USE_RANDOM_MDS
)
579 inode
= req
->r_inode
;
580 } else if (req
->r_dentry
) {
581 if (req
->r_dentry
->d_inode
) {
582 inode
= req
->r_dentry
->d_inode
;
584 inode
= req
->r_dentry
->d_parent
->d_inode
;
585 hash
= req
->r_dentry
->d_name
.hash
;
589 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode
, (int)is_hash
,
593 ci
= ceph_inode(inode
);
595 if (is_hash
&& S_ISDIR(inode
->i_mode
)) {
596 struct ceph_inode_frag frag
;
599 ceph_choose_frag(ci
, hash
, &frag
, &found
);
601 if (mode
== USE_ANY_MDS
&& frag
.ndist
> 0) {
604 /* choose a random replica */
605 get_random_bytes(&r
, 1);
608 dout("choose_mds %p %llx.%llx "
609 "frag %u mds%d (%d/%d)\n",
610 inode
, ceph_vinop(inode
),
616 /* since this file/dir wasn't known to be
617 * replicated, then we want to look for the
618 * authoritative mds. */
621 /* choose auth mds */
623 dout("choose_mds %p %llx.%llx "
624 "frag %u mds%d (auth)\n",
625 inode
, ceph_vinop(inode
), frag
.frag
, mds
);
631 spin_lock(&inode
->i_lock
);
633 if (mode
== USE_AUTH_MDS
)
634 cap
= ci
->i_auth_cap
;
635 if (!cap
&& !RB_EMPTY_ROOT(&ci
->i_caps
))
636 cap
= rb_entry(rb_first(&ci
->i_caps
), struct ceph_cap
, ci_node
);
638 spin_unlock(&inode
->i_lock
);
641 mds
= cap
->session
->s_mds
;
642 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
643 inode
, ceph_vinop(inode
), mds
,
644 cap
== ci
->i_auth_cap
? "auth " : "", cap
);
645 spin_unlock(&inode
->i_lock
);
649 mds
= ceph_mdsmap_get_random_mds(mdsc
->mdsmap
);
650 dout("choose_mds chose random mds%d\n", mds
);
658 static struct ceph_msg
*create_session_msg(u32 op
, u64 seq
)
660 struct ceph_msg
*msg
;
661 struct ceph_mds_session_head
*h
;
663 msg
= ceph_msg_new(CEPH_MSG_CLIENT_SESSION
, sizeof(*h
), 0, 0, NULL
);
665 pr_err("create_session_msg ENOMEM creating msg\n");
666 return ERR_PTR(PTR_ERR(msg
));
668 h
= msg
->front
.iov_base
;
669 h
->op
= cpu_to_le32(op
);
670 h
->seq
= cpu_to_le64(seq
);
675 * send session open request.
677 * called under mdsc->mutex
679 static int __open_session(struct ceph_mds_client
*mdsc
,
680 struct ceph_mds_session
*session
)
682 struct ceph_msg
*msg
;
684 int mds
= session
->s_mds
;
687 /* wait for mds to go active? */
688 mstate
= ceph_mdsmap_get_state(mdsc
->mdsmap
, mds
);
689 dout("open_session to mds%d (%s)\n", mds
,
690 ceph_mds_state_name(mstate
));
691 session
->s_state
= CEPH_MDS_SESSION_OPENING
;
692 session
->s_renew_requested
= jiffies
;
694 /* send connect message */
695 msg
= create_session_msg(CEPH_SESSION_REQUEST_OPEN
, session
->s_seq
);
700 ceph_con_send(&session
->s_con
, msg
);
711 * Free preallocated cap messages assigned to this session
713 static void cleanup_cap_releases(struct ceph_mds_session
*session
)
715 struct ceph_msg
*msg
;
717 spin_lock(&session
->s_cap_lock
);
718 while (!list_empty(&session
->s_cap_releases
)) {
719 msg
= list_first_entry(&session
->s_cap_releases
,
720 struct ceph_msg
, list_head
);
721 list_del_init(&msg
->list_head
);
724 while (!list_empty(&session
->s_cap_releases_done
)) {
725 msg
= list_first_entry(&session
->s_cap_releases_done
,
726 struct ceph_msg
, list_head
);
727 list_del_init(&msg
->list_head
);
730 spin_unlock(&session
->s_cap_lock
);
734 * Helper to safely iterate over all caps associated with a session.
736 * caller must hold session s_mutex
738 static int iterate_session_caps(struct ceph_mds_session
*session
,
739 int (*cb
)(struct inode
*, struct ceph_cap
*,
743 struct ceph_cap
*cap
;
744 struct inode
*inode
, *last_inode
= NULL
;
745 struct ceph_cap
*old_cap
= NULL
;
748 dout("iterate_session_caps %p mds%d\n", session
, session
->s_mds
);
749 spin_lock(&session
->s_cap_lock
);
750 p
= session
->s_caps
.next
;
751 while (p
!= &session
->s_caps
) {
752 cap
= list_entry(p
, struct ceph_cap
, session_caps
);
753 inode
= igrab(&cap
->ci
->vfs_inode
);
758 session
->s_cap_iterator
= cap
;
759 spin_unlock(&session
->s_cap_lock
);
766 ceph_put_cap(old_cap
);
770 ret
= cb(inode
, cap
, arg
);
773 spin_lock(&session
->s_cap_lock
);
775 if (cap
->ci
== NULL
) {
776 dout("iterate_session_caps finishing cap %p removal\n",
778 BUG_ON(cap
->session
!= session
);
779 list_del_init(&cap
->session_caps
);
780 session
->s_nr_caps
--;
782 old_cap
= cap
; /* put_cap it w/o locks held */
789 session
->s_cap_iterator
= NULL
;
790 spin_unlock(&session
->s_cap_lock
);
795 ceph_put_cap(old_cap
);
800 static int remove_session_caps_cb(struct inode
*inode
, struct ceph_cap
*cap
,
803 struct ceph_inode_info
*ci
= ceph_inode(inode
);
804 dout("removing cap %p, ci is %p, inode is %p\n",
805 cap
, ci
, &ci
->vfs_inode
);
806 ceph_remove_cap(cap
);
811 * caller must hold session s_mutex
813 static void remove_session_caps(struct ceph_mds_session
*session
)
815 dout("remove_session_caps on %p\n", session
);
816 iterate_session_caps(session
, remove_session_caps_cb
, NULL
);
817 BUG_ON(session
->s_nr_caps
> 0);
818 cleanup_cap_releases(session
);
822 * wake up any threads waiting on this session's caps. if the cap is
823 * old (didn't get renewed on the client reconnect), remove it now.
825 * caller must hold s_mutex.
827 static int wake_up_session_cb(struct inode
*inode
, struct ceph_cap
*cap
,
830 struct ceph_inode_info
*ci
= ceph_inode(inode
);
832 wake_up(&ci
->i_cap_wq
);
834 spin_lock(&inode
->i_lock
);
835 ci
->i_wanted_max_size
= 0;
836 ci
->i_requested_max_size
= 0;
837 spin_unlock(&inode
->i_lock
);
842 static void wake_up_session_caps(struct ceph_mds_session
*session
,
845 dout("wake_up_session_caps %p mds%d\n", session
, session
->s_mds
);
846 iterate_session_caps(session
, wake_up_session_cb
,
847 (void *)(unsigned long)reconnect
);
851 * Send periodic message to MDS renewing all currently held caps. The
852 * ack will reset the expiration for all caps from this session.
854 * caller holds s_mutex
856 static int send_renew_caps(struct ceph_mds_client
*mdsc
,
857 struct ceph_mds_session
*session
)
859 struct ceph_msg
*msg
;
862 if (time_after_eq(jiffies
, session
->s_cap_ttl
) &&
863 time_after_eq(session
->s_cap_ttl
, session
->s_renew_requested
))
864 pr_info("mds%d caps stale\n", session
->s_mds
);
866 /* do not try to renew caps until a recovering mds has reconnected
867 * with its clients. */
868 state
= ceph_mdsmap_get_state(mdsc
->mdsmap
, session
->s_mds
);
869 if (state
< CEPH_MDS_STATE_RECONNECT
) {
870 dout("send_renew_caps ignoring mds%d (%s)\n",
871 session
->s_mds
, ceph_mds_state_name(state
));
875 dout("send_renew_caps to mds%d (%s)\n", session
->s_mds
,
876 ceph_mds_state_name(state
));
877 session
->s_renew_requested
= jiffies
;
878 msg
= create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS
,
879 ++session
->s_renew_seq
);
882 ceph_con_send(&session
->s_con
, msg
);
887 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
889 * Called under session->s_mutex
891 static void renewed_caps(struct ceph_mds_client
*mdsc
,
892 struct ceph_mds_session
*session
, int is_renew
)
897 spin_lock(&session
->s_cap_lock
);
898 was_stale
= is_renew
&& (session
->s_cap_ttl
== 0 ||
899 time_after_eq(jiffies
, session
->s_cap_ttl
));
901 session
->s_cap_ttl
= session
->s_renew_requested
+
902 mdsc
->mdsmap
->m_session_timeout
*HZ
;
905 if (time_before(jiffies
, session
->s_cap_ttl
)) {
906 pr_info("mds%d caps renewed\n", session
->s_mds
);
909 pr_info("mds%d caps still stale\n", session
->s_mds
);
912 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
913 session
->s_mds
, session
->s_cap_ttl
, was_stale
? "stale" : "fresh",
914 time_before(jiffies
, session
->s_cap_ttl
) ? "stale" : "fresh");
915 spin_unlock(&session
->s_cap_lock
);
918 wake_up_session_caps(session
, 0);
922 * send a session close request
924 static int request_close_session(struct ceph_mds_client
*mdsc
,
925 struct ceph_mds_session
*session
)
927 struct ceph_msg
*msg
;
930 dout("request_close_session mds%d state %s seq %lld\n",
931 session
->s_mds
, session_state_name(session
->s_state
),
933 msg
= create_session_msg(CEPH_SESSION_REQUEST_CLOSE
, session
->s_seq
);
937 ceph_con_send(&session
->s_con
, msg
);
942 * Called with s_mutex held.
944 static int __close_session(struct ceph_mds_client
*mdsc
,
945 struct ceph_mds_session
*session
)
947 if (session
->s_state
>= CEPH_MDS_SESSION_CLOSING
)
949 session
->s_state
= CEPH_MDS_SESSION_CLOSING
;
950 return request_close_session(mdsc
, session
);
956 * Because we can't cache an inode without one or more caps, we do
957 * this indirectly: if a cap is unused, we prune its aliases, at which
958 * point the inode will hopefully get dropped to.
960 * Yes, this is a bit sloppy. Our only real goal here is to respond to
961 * memory pressure from the MDS, though, so it needn't be perfect.
963 static int trim_caps_cb(struct inode
*inode
, struct ceph_cap
*cap
, void *arg
)
965 struct ceph_mds_session
*session
= arg
;
966 struct ceph_inode_info
*ci
= ceph_inode(inode
);
967 int used
, oissued
, mine
;
969 if (session
->s_trim_caps
<= 0)
972 spin_lock(&inode
->i_lock
);
973 mine
= cap
->issued
| cap
->implemented
;
974 used
= __ceph_caps_used(ci
);
975 oissued
= __ceph_caps_issued_other(ci
, cap
);
977 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n",
978 inode
, cap
, ceph_cap_string(mine
), ceph_cap_string(oissued
),
979 ceph_cap_string(used
));
980 if (ci
->i_dirty_caps
)
981 goto out
; /* dirty caps */
982 if ((used
& ~oissued
) & mine
)
983 goto out
; /* we need these caps */
985 session
->s_trim_caps
--;
987 /* we aren't the only cap.. just remove us */
988 __ceph_remove_cap(cap
);
990 /* try to drop referring dentries */
991 spin_unlock(&inode
->i_lock
);
992 d_prune_aliases(inode
);
993 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
994 inode
, cap
, atomic_read(&inode
->i_count
));
999 spin_unlock(&inode
->i_lock
);
1004 * Trim session cap count down to some max number.
1006 static int trim_caps(struct ceph_mds_client
*mdsc
,
1007 struct ceph_mds_session
*session
,
1010 int trim_caps
= session
->s_nr_caps
- max_caps
;
1012 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1013 session
->s_mds
, session
->s_nr_caps
, max_caps
, trim_caps
);
1014 if (trim_caps
> 0) {
1015 session
->s_trim_caps
= trim_caps
;
1016 iterate_session_caps(session
, trim_caps_cb
, session
);
1017 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1018 session
->s_mds
, session
->s_nr_caps
, max_caps
,
1019 trim_caps
- session
->s_trim_caps
);
1020 session
->s_trim_caps
= 0;
1026 * Allocate cap_release messages. If there is a partially full message
1027 * in the queue, try to allocate enough to cover it's remainder, so that
1028 * we can send it immediately.
1030 * Called under s_mutex.
1032 static int add_cap_releases(struct ceph_mds_client
*mdsc
,
1033 struct ceph_mds_session
*session
,
1036 struct ceph_msg
*msg
;
1037 struct ceph_mds_cap_release
*head
;
1041 extra
= mdsc
->client
->mount_args
->cap_release_safety
;
1043 spin_lock(&session
->s_cap_lock
);
1045 if (!list_empty(&session
->s_cap_releases
)) {
1046 msg
= list_first_entry(&session
->s_cap_releases
,
1049 head
= msg
->front
.iov_base
;
1050 extra
+= CEPH_CAPS_PER_RELEASE
- le32_to_cpu(head
->num
);
1053 while (session
->s_num_cap_releases
< session
->s_nr_caps
+ extra
) {
1054 spin_unlock(&session
->s_cap_lock
);
1055 msg
= ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE
, PAGE_CACHE_SIZE
,
1059 dout("add_cap_releases %p msg %p now %d\n", session
, msg
,
1060 (int)msg
->front
.iov_len
);
1061 head
= msg
->front
.iov_base
;
1062 head
->num
= cpu_to_le32(0);
1063 msg
->front
.iov_len
= sizeof(*head
);
1064 spin_lock(&session
->s_cap_lock
);
1065 list_add(&msg
->list_head
, &session
->s_cap_releases
);
1066 session
->s_num_cap_releases
+= CEPH_CAPS_PER_RELEASE
;
1069 if (!list_empty(&session
->s_cap_releases
)) {
1070 msg
= list_first_entry(&session
->s_cap_releases
,
1073 head
= msg
->front
.iov_base
;
1075 dout(" queueing non-full %p (%d)\n", msg
,
1076 le32_to_cpu(head
->num
));
1077 list_move_tail(&msg
->list_head
,
1078 &session
->s_cap_releases_done
);
1079 session
->s_num_cap_releases
-=
1080 CEPH_CAPS_PER_RELEASE
- le32_to_cpu(head
->num
);
1084 spin_unlock(&session
->s_cap_lock
);
1090 * flush all dirty inode data to disk.
1092 * returns true if we've flushed through want_flush_seq
1094 static int check_cap_flush(struct ceph_mds_client
*mdsc
, u64 want_flush_seq
)
1098 dout("check_cap_flush want %lld\n", want_flush_seq
);
1099 mutex_lock(&mdsc
->mutex
);
1100 for (mds
= 0; ret
&& mds
< mdsc
->max_sessions
; mds
++) {
1101 struct ceph_mds_session
*session
= mdsc
->sessions
[mds
];
1105 get_session(session
);
1106 mutex_unlock(&mdsc
->mutex
);
1108 mutex_lock(&session
->s_mutex
);
1109 if (!list_empty(&session
->s_cap_flushing
)) {
1110 struct ceph_inode_info
*ci
=
1111 list_entry(session
->s_cap_flushing
.next
,
1112 struct ceph_inode_info
,
1114 struct inode
*inode
= &ci
->vfs_inode
;
1116 spin_lock(&inode
->i_lock
);
1117 if (ci
->i_cap_flush_seq
<= want_flush_seq
) {
1118 dout("check_cap_flush still flushing %p "
1119 "seq %lld <= %lld to mds%d\n", inode
,
1120 ci
->i_cap_flush_seq
, want_flush_seq
,
1124 spin_unlock(&inode
->i_lock
);
1126 mutex_unlock(&session
->s_mutex
);
1127 ceph_put_mds_session(session
);
1131 mutex_lock(&mdsc
->mutex
);
1134 mutex_unlock(&mdsc
->mutex
);
1135 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq
);
1140 * called under s_mutex
1142 static void send_cap_releases(struct ceph_mds_client
*mdsc
,
1143 struct ceph_mds_session
*session
)
1145 struct ceph_msg
*msg
;
1147 dout("send_cap_releases mds%d\n", session
->s_mds
);
1149 spin_lock(&session
->s_cap_lock
);
1150 if (list_empty(&session
->s_cap_releases_done
))
1152 msg
= list_first_entry(&session
->s_cap_releases_done
,
1153 struct ceph_msg
, list_head
);
1154 list_del_init(&msg
->list_head
);
1155 spin_unlock(&session
->s_cap_lock
);
1156 msg
->hdr
.front_len
= cpu_to_le32(msg
->front
.iov_len
);
1157 dout("send_cap_releases mds%d %p\n", session
->s_mds
, msg
);
1158 ceph_con_send(&session
->s_con
, msg
);
1160 spin_unlock(&session
->s_cap_lock
);
1168 * Create an mds request.
1170 struct ceph_mds_request
*
1171 ceph_mdsc_create_request(struct ceph_mds_client
*mdsc
, int op
, int mode
)
1173 struct ceph_mds_request
*req
= kzalloc(sizeof(*req
), GFP_NOFS
);
1176 return ERR_PTR(-ENOMEM
);
1178 req
->r_started
= jiffies
;
1179 req
->r_resend_mds
= -1;
1180 INIT_LIST_HEAD(&req
->r_unsafe_dir_item
);
1182 kref_init(&req
->r_kref
);
1183 INIT_LIST_HEAD(&req
->r_wait
);
1184 init_completion(&req
->r_completion
);
1185 init_completion(&req
->r_safe_completion
);
1186 INIT_LIST_HEAD(&req
->r_unsafe_item
);
1189 req
->r_direct_mode
= mode
;
1194 * return oldest (lowest) request, tid in request tree, 0 if none.
1196 * called under mdsc->mutex.
1198 static struct ceph_mds_request
*__get_oldest_req(struct ceph_mds_client
*mdsc
)
1200 if (RB_EMPTY_ROOT(&mdsc
->request_tree
))
1202 return rb_entry(rb_first(&mdsc
->request_tree
),
1203 struct ceph_mds_request
, r_node
);
1206 static u64
__get_oldest_tid(struct ceph_mds_client
*mdsc
)
1208 struct ceph_mds_request
*req
= __get_oldest_req(mdsc
);
1216 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1217 * on build_path_from_dentry in fs/cifs/dir.c.
1219 * If @stop_on_nosnap, generate path relative to the first non-snapped
1222 * Encode hidden .snap dirs as a double /, i.e.
1223 * foo/.snap/bar -> foo//bar
1225 char *ceph_mdsc_build_path(struct dentry
*dentry
, int *plen
, u64
*base
,
1228 struct dentry
*temp
;
1233 return ERR_PTR(-EINVAL
);
1237 for (temp
= dentry
; !IS_ROOT(temp
);) {
1238 struct inode
*inode
= temp
->d_inode
;
1239 if (inode
&& ceph_snap(inode
) == CEPH_SNAPDIR
)
1240 len
++; /* slash only */
1241 else if (stop_on_nosnap
&& inode
&&
1242 ceph_snap(inode
) == CEPH_NOSNAP
)
1245 len
+= 1 + temp
->d_name
.len
;
1246 temp
= temp
->d_parent
;
1248 pr_err("build_path_dentry corrupt dentry %p\n", dentry
);
1249 return ERR_PTR(-EINVAL
);
1253 len
--; /* no leading '/' */
1255 path
= kmalloc(len
+1, GFP_NOFS
);
1257 return ERR_PTR(-ENOMEM
);
1259 path
[pos
] = 0; /* trailing null */
1260 for (temp
= dentry
; !IS_ROOT(temp
) && pos
!= 0; ) {
1261 struct inode
*inode
= temp
->d_inode
;
1263 if (inode
&& ceph_snap(inode
) == CEPH_SNAPDIR
) {
1264 dout("build_path_dentry path+%d: %p SNAPDIR\n",
1266 } else if (stop_on_nosnap
&& inode
&&
1267 ceph_snap(inode
) == CEPH_NOSNAP
) {
1270 pos
-= temp
->d_name
.len
;
1273 strncpy(path
+ pos
, temp
->d_name
.name
,
1275 dout("build_path_dentry path+%d: %p '%.*s'\n",
1276 pos
, temp
, temp
->d_name
.len
, path
+ pos
);
1280 temp
= temp
->d_parent
;
1282 pr_err("build_path_dentry corrupt dentry\n");
1284 return ERR_PTR(-EINVAL
);
1288 pr_err("build_path_dentry did not end path lookup where "
1289 "expected, namelen is %d, pos is %d\n", len
, pos
);
1290 /* presumably this is only possible if racing with a
1291 rename of one of the parent directories (we can not
1292 lock the dentries above us to prevent this, but
1293 retrying should be harmless) */
1298 *base
= ceph_ino(temp
->d_inode
);
1300 dout("build_path_dentry on %p %d built %llx '%.*s'\n",
1301 dentry
, atomic_read(&dentry
->d_count
), *base
, len
, path
);
1305 static int build_dentry_path(struct dentry
*dentry
,
1306 const char **ppath
, int *ppathlen
, u64
*pino
,
1311 if (ceph_snap(dentry
->d_parent
->d_inode
) == CEPH_NOSNAP
) {
1312 *pino
= ceph_ino(dentry
->d_parent
->d_inode
);
1313 *ppath
= dentry
->d_name
.name
;
1314 *ppathlen
= dentry
->d_name
.len
;
1317 path
= ceph_mdsc_build_path(dentry
, ppathlen
, pino
, 1);
1319 return PTR_ERR(path
);
1325 static int build_inode_path(struct inode
*inode
,
1326 const char **ppath
, int *ppathlen
, u64
*pino
,
1329 struct dentry
*dentry
;
1332 if (ceph_snap(inode
) == CEPH_NOSNAP
) {
1333 *pino
= ceph_ino(inode
);
1337 dentry
= d_find_alias(inode
);
1338 path
= ceph_mdsc_build_path(dentry
, ppathlen
, pino
, 1);
1341 return PTR_ERR(path
);
1348 * request arguments may be specified via an inode *, a dentry *, or
1349 * an explicit ino+path.
1351 static int set_request_path_attr(struct inode
*rinode
, struct dentry
*rdentry
,
1352 const char *rpath
, u64 rino
,
1353 const char **ppath
, int *pathlen
,
1354 u64
*ino
, int *freepath
)
1359 r
= build_inode_path(rinode
, ppath
, pathlen
, ino
, freepath
);
1360 dout(" inode %p %llx.%llx\n", rinode
, ceph_ino(rinode
),
1362 } else if (rdentry
) {
1363 r
= build_dentry_path(rdentry
, ppath
, pathlen
, ino
, freepath
);
1364 dout(" dentry %p %llx/%.*s\n", rdentry
, *ino
, *pathlen
,
1369 *pathlen
= strlen(rpath
);
1370 dout(" path %.*s\n", *pathlen
, rpath
);
1377 * called under mdsc->mutex
1379 static struct ceph_msg
*create_request_message(struct ceph_mds_client
*mdsc
,
1380 struct ceph_mds_request
*req
,
1383 struct ceph_msg
*msg
;
1384 struct ceph_mds_request_head
*head
;
1385 const char *path1
= NULL
;
1386 const char *path2
= NULL
;
1387 u64 ino1
= 0, ino2
= 0;
1388 int pathlen1
= 0, pathlen2
= 0;
1389 int freepath1
= 0, freepath2
= 0;
1395 ret
= set_request_path_attr(req
->r_inode
, req
->r_dentry
,
1396 req
->r_path1
, req
->r_ino1
.ino
,
1397 &path1
, &pathlen1
, &ino1
, &freepath1
);
1403 ret
= set_request_path_attr(NULL
, req
->r_old_dentry
,
1404 req
->r_path2
, req
->r_ino2
.ino
,
1405 &path2
, &pathlen2
, &ino2
, &freepath2
);
1411 len
= sizeof(*head
) +
1412 pathlen1
+ pathlen2
+ 2*(1 + sizeof(u32
) + sizeof(u64
));
1414 /* calculate (max) length for cap releases */
1415 len
+= sizeof(struct ceph_mds_request_release
) *
1416 (!!req
->r_inode_drop
+ !!req
->r_dentry_drop
+
1417 !!req
->r_old_inode_drop
+ !!req
->r_old_dentry_drop
);
1418 if (req
->r_dentry_drop
)
1419 len
+= req
->r_dentry
->d_name
.len
;
1420 if (req
->r_old_dentry_drop
)
1421 len
+= req
->r_old_dentry
->d_name
.len
;
1423 msg
= ceph_msg_new(CEPH_MSG_CLIENT_REQUEST
, len
, 0, 0, NULL
);
1427 msg
->hdr
.tid
= cpu_to_le64(req
->r_tid
);
1429 head
= msg
->front
.iov_base
;
1430 p
= msg
->front
.iov_base
+ sizeof(*head
);
1431 end
= msg
->front
.iov_base
+ msg
->front
.iov_len
;
1433 head
->mdsmap_epoch
= cpu_to_le32(mdsc
->mdsmap
->m_epoch
);
1434 head
->op
= cpu_to_le32(req
->r_op
);
1435 head
->caller_uid
= cpu_to_le32(current_fsuid());
1436 head
->caller_gid
= cpu_to_le32(current_fsgid());
1437 head
->args
= req
->r_args
;
1439 ceph_encode_filepath(&p
, end
, ino1
, path1
);
1440 ceph_encode_filepath(&p
, end
, ino2
, path2
);
1444 if (req
->r_inode_drop
)
1445 releases
+= ceph_encode_inode_release(&p
,
1446 req
->r_inode
? req
->r_inode
: req
->r_dentry
->d_inode
,
1447 mds
, req
->r_inode_drop
, req
->r_inode_unless
, 0);
1448 if (req
->r_dentry_drop
)
1449 releases
+= ceph_encode_dentry_release(&p
, req
->r_dentry
,
1450 mds
, req
->r_dentry_drop
, req
->r_dentry_unless
);
1451 if (req
->r_old_dentry_drop
)
1452 releases
+= ceph_encode_dentry_release(&p
, req
->r_old_dentry
,
1453 mds
, req
->r_old_dentry_drop
, req
->r_old_dentry_unless
);
1454 if (req
->r_old_inode_drop
)
1455 releases
+= ceph_encode_inode_release(&p
,
1456 req
->r_old_dentry
->d_inode
,
1457 mds
, req
->r_old_inode_drop
, req
->r_old_inode_unless
, 0);
1458 head
->num_releases
= cpu_to_le16(releases
);
1461 msg
->front
.iov_len
= p
- msg
->front
.iov_base
;
1462 msg
->hdr
.front_len
= cpu_to_le32(msg
->front
.iov_len
);
1464 msg
->pages
= req
->r_pages
;
1465 msg
->nr_pages
= req
->r_num_pages
;
1466 msg
->hdr
.data_len
= cpu_to_le32(req
->r_data_len
);
1467 msg
->hdr
.data_off
= cpu_to_le16(0);
1471 kfree((char *)path2
);
1474 kfree((char *)path1
);
1480 * called under mdsc->mutex if error, under no mutex if
1483 static void complete_request(struct ceph_mds_client
*mdsc
,
1484 struct ceph_mds_request
*req
)
1486 if (req
->r_callback
)
1487 req
->r_callback(mdsc
, req
);
1489 complete(&req
->r_completion
);
1493 * called under mdsc->mutex
1495 static int __prepare_send_request(struct ceph_mds_client
*mdsc
,
1496 struct ceph_mds_request
*req
,
1499 struct ceph_mds_request_head
*rhead
;
1500 struct ceph_msg
*msg
;
1505 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req
,
1506 req
->r_tid
, ceph_mds_op_name(req
->r_op
), req
->r_attempts
);
1508 if (req
->r_request
) {
1509 ceph_msg_put(req
->r_request
);
1510 req
->r_request
= NULL
;
1512 msg
= create_request_message(mdsc
, req
, mds
);
1514 req
->r_reply
= ERR_PTR(PTR_ERR(msg
));
1515 complete_request(mdsc
, req
);
1516 return -PTR_ERR(msg
);
1518 req
->r_request
= msg
;
1520 rhead
= msg
->front
.iov_base
;
1521 rhead
->oldest_client_tid
= cpu_to_le64(__get_oldest_tid(mdsc
));
1522 if (req
->r_got_unsafe
)
1523 flags
|= CEPH_MDS_FLAG_REPLAY
;
1524 if (req
->r_locked_dir
)
1525 flags
|= CEPH_MDS_FLAG_WANT_DENTRY
;
1526 rhead
->flags
= cpu_to_le32(flags
);
1527 rhead
->num_fwd
= req
->r_num_fwd
;
1528 rhead
->num_retry
= req
->r_attempts
- 1;
1530 dout(" r_locked_dir = %p\n", req
->r_locked_dir
);
1532 if (req
->r_target_inode
&& req
->r_got_unsafe
)
1533 rhead
->ino
= cpu_to_le64(ceph_ino(req
->r_target_inode
));
1540 * send request, or put it on the appropriate wait list.
1542 static int __do_request(struct ceph_mds_client
*mdsc
,
1543 struct ceph_mds_request
*req
)
1545 struct ceph_mds_session
*session
= NULL
;
1552 if (req
->r_timeout
&&
1553 time_after_eq(jiffies
, req
->r_started
+ req
->r_timeout
)) {
1554 dout("do_request timed out\n");
1559 mds
= __choose_mds(mdsc
, req
);
1561 ceph_mdsmap_get_state(mdsc
->mdsmap
, mds
) < CEPH_MDS_STATE_ACTIVE
) {
1562 dout("do_request no mds or not active, waiting for map\n");
1563 list_add(&req
->r_wait
, &mdsc
->waiting_for_map
);
1567 /* get, open session */
1568 session
= __ceph_lookup_mds_session(mdsc
, mds
);
1570 session
= register_session(mdsc
, mds
);
1571 dout("do_request mds%d session %p state %s\n", mds
, session
,
1572 session_state_name(session
->s_state
));
1573 if (session
->s_state
!= CEPH_MDS_SESSION_OPEN
&&
1574 session
->s_state
!= CEPH_MDS_SESSION_HUNG
) {
1575 if (session
->s_state
== CEPH_MDS_SESSION_NEW
||
1576 session
->s_state
== CEPH_MDS_SESSION_CLOSING
)
1577 __open_session(mdsc
, session
);
1578 list_add(&req
->r_wait
, &session
->s_waiting
);
1583 req
->r_session
= get_session(session
);
1584 req
->r_resend_mds
= -1; /* forget any previous mds hint */
1586 if (req
->r_request_started
== 0) /* note request start time */
1587 req
->r_request_started
= jiffies
;
1589 err
= __prepare_send_request(mdsc
, req
, mds
);
1591 ceph_msg_get(req
->r_request
);
1592 ceph_con_send(&session
->s_con
, req
->r_request
);
1596 ceph_put_mds_session(session
);
1601 req
->r_reply
= ERR_PTR(err
);
1602 complete_request(mdsc
, req
);
1607 * called under mdsc->mutex
1609 static void __wake_requests(struct ceph_mds_client
*mdsc
,
1610 struct list_head
*head
)
1612 struct ceph_mds_request
*req
, *nreq
;
1614 list_for_each_entry_safe(req
, nreq
, head
, r_wait
) {
1615 list_del_init(&req
->r_wait
);
1616 __do_request(mdsc
, req
);
1621 * Wake up threads with requests pending for @mds, so that they can
1622 * resubmit their requests to a possibly different mds. If @all is set,
1623 * wake up if their requests has been forwarded to @mds, too.
1625 static void kick_requests(struct ceph_mds_client
*mdsc
, int mds
, int all
)
1627 struct ceph_mds_request
*req
;
1630 dout("kick_requests mds%d\n", mds
);
1631 for (p
= rb_first(&mdsc
->request_tree
); p
; p
= rb_next(p
)) {
1632 req
= rb_entry(p
, struct ceph_mds_request
, r_node
);
1633 if (req
->r_got_unsafe
)
1635 if (req
->r_session
&&
1636 req
->r_session
->s_mds
== mds
) {
1637 dout(" kicking tid %llu\n", req
->r_tid
);
1638 put_request_session(req
);
1639 __do_request(mdsc
, req
);
1644 void ceph_mdsc_submit_request(struct ceph_mds_client
*mdsc
,
1645 struct ceph_mds_request
*req
)
1647 dout("submit_request on %p\n", req
);
1648 mutex_lock(&mdsc
->mutex
);
1649 __register_request(mdsc
, req
, NULL
);
1650 __do_request(mdsc
, req
);
1651 mutex_unlock(&mdsc
->mutex
);
1655 * Synchrously perform an mds request. Take care of all of the
1656 * session setup, forwarding, retry details.
1658 int ceph_mdsc_do_request(struct ceph_mds_client
*mdsc
,
1660 struct ceph_mds_request
*req
)
1664 dout("do_request on %p\n", req
);
1666 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
1668 ceph_get_cap_refs(ceph_inode(req
->r_inode
), CEPH_CAP_PIN
);
1669 if (req
->r_locked_dir
)
1670 ceph_get_cap_refs(ceph_inode(req
->r_locked_dir
), CEPH_CAP_PIN
);
1671 if (req
->r_old_dentry
)
1673 ceph_inode(req
->r_old_dentry
->d_parent
->d_inode
),
1677 mutex_lock(&mdsc
->mutex
);
1678 __register_request(mdsc
, req
, dir
);
1679 __do_request(mdsc
, req
);
1682 if (!req
->r_reply
) {
1683 mutex_unlock(&mdsc
->mutex
);
1684 if (req
->r_timeout
) {
1685 err
= (long)wait_for_completion_interruptible_timeout(
1686 &req
->r_completion
, req
->r_timeout
);
1688 req
->r_reply
= ERR_PTR(-EIO
);
1690 req
->r_reply
= ERR_PTR(err
);
1692 err
= wait_for_completion_interruptible(
1693 &req
->r_completion
);
1695 req
->r_reply
= ERR_PTR(err
);
1697 mutex_lock(&mdsc
->mutex
);
1700 if (IS_ERR(req
->r_reply
)) {
1701 err
= PTR_ERR(req
->r_reply
);
1702 req
->r_reply
= NULL
;
1704 if (err
== -ERESTARTSYS
) {
1706 req
->r_aborted
= true;
1708 if (req
->r_locked_dir
&&
1709 (req
->r_op
& CEPH_MDS_OP_WRITE
)) {
1710 struct ceph_inode_info
*ci
=
1711 ceph_inode(req
->r_locked_dir
);
1713 dout("aborted, clearing I_COMPLETE on %p\n",
1715 spin_lock(&req
->r_locked_dir
->i_lock
);
1716 ci
->i_ceph_flags
&= ~CEPH_I_COMPLETE
;
1717 ci
->i_release_count
++;
1718 spin_unlock(&req
->r_locked_dir
->i_lock
);
1721 /* clean up this request */
1722 __unregister_request(mdsc
, req
);
1723 if (!list_empty(&req
->r_unsafe_item
))
1724 list_del_init(&req
->r_unsafe_item
);
1725 complete(&req
->r_safe_completion
);
1727 } else if (req
->r_err
) {
1730 err
= le32_to_cpu(req
->r_reply_info
.head
->result
);
1732 mutex_unlock(&mdsc
->mutex
);
1734 dout("do_request %p done, result %d\n", req
, err
);
1741 * We take the session mutex and parse and process the reply immediately.
1742 * This preserves the logical ordering of replies, capabilities, etc., sent
1743 * by the MDS as they are applied to our local cache.
1745 static void handle_reply(struct ceph_mds_session
*session
, struct ceph_msg
*msg
)
1747 struct ceph_mds_client
*mdsc
= session
->s_mdsc
;
1748 struct ceph_mds_request
*req
;
1749 struct ceph_mds_reply_head
*head
= msg
->front
.iov_base
;
1750 struct ceph_mds_reply_info_parsed
*rinfo
; /* parsed reply info */
1753 int mds
= session
->s_mds
;
1755 if (msg
->front
.iov_len
< sizeof(*head
)) {
1756 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
1761 /* get request, session */
1762 tid
= le64_to_cpu(msg
->hdr
.tid
);
1763 mutex_lock(&mdsc
->mutex
);
1764 req
= __lookup_request(mdsc
, tid
);
1766 dout("handle_reply on unknown tid %llu\n", tid
);
1767 mutex_unlock(&mdsc
->mutex
);
1770 dout("handle_reply %p\n", req
);
1772 /* correct session? */
1773 if (!req
->r_session
&& req
->r_session
!= session
) {
1774 pr_err("mdsc_handle_reply got %llu on session mds%d"
1775 " not mds%d\n", tid
, session
->s_mds
,
1776 req
->r_session
? req
->r_session
->s_mds
: -1);
1777 mutex_unlock(&mdsc
->mutex
);
1782 if ((req
->r_got_unsafe
&& !head
->safe
) ||
1783 (req
->r_got_safe
&& head
->safe
)) {
1784 pr_warning("got a dup %s reply on %llu from mds%d\n",
1785 head
->safe
? "safe" : "unsafe", tid
, mds
);
1786 mutex_unlock(&mdsc
->mutex
);
1790 result
= le32_to_cpu(head
->result
);
1793 * Tolerate 2 consecutive ESTALEs from the same mds.
1794 * FIXME: we should be looking at the cap migrate_seq.
1796 if (result
== -ESTALE
) {
1797 req
->r_direct_mode
= USE_AUTH_MDS
;
1799 if (req
->r_num_stale
<= 2) {
1800 __do_request(mdsc
, req
);
1801 mutex_unlock(&mdsc
->mutex
);
1805 req
->r_num_stale
= 0;
1809 req
->r_got_safe
= true;
1810 __unregister_request(mdsc
, req
);
1811 complete(&req
->r_safe_completion
);
1813 if (req
->r_got_unsafe
) {
1815 * We already handled the unsafe response, now do the
1816 * cleanup. No need to examine the response; the MDS
1817 * doesn't include any result info in the safe
1818 * response. And even if it did, there is nothing
1819 * useful we could do with a revised return value.
1821 dout("got safe reply %llu, mds%d\n", tid
, mds
);
1822 list_del_init(&req
->r_unsafe_item
);
1824 /* last unsafe request during umount? */
1825 if (mdsc
->stopping
&& !__get_oldest_req(mdsc
))
1826 complete(&mdsc
->safe_umount_waiters
);
1827 mutex_unlock(&mdsc
->mutex
);
1832 BUG_ON(req
->r_reply
);
1835 req
->r_got_unsafe
= true;
1836 list_add_tail(&req
->r_unsafe_item
, &req
->r_session
->s_unsafe
);
1839 dout("handle_reply tid %lld result %d\n", tid
, result
);
1840 rinfo
= &req
->r_reply_info
;
1841 err
= parse_reply_info(msg
, rinfo
);
1842 mutex_unlock(&mdsc
->mutex
);
1844 mutex_lock(&session
->s_mutex
);
1846 pr_err("mdsc_handle_reply got corrupt reply mds%d\n", mds
);
1852 if (rinfo
->snapblob_len
) {
1853 down_write(&mdsc
->snap_rwsem
);
1854 ceph_update_snap_trace(mdsc
, rinfo
->snapblob
,
1855 rinfo
->snapblob
+ rinfo
->snapblob_len
,
1856 le32_to_cpu(head
->op
) == CEPH_MDS_OP_RMSNAP
);
1857 downgrade_write(&mdsc
->snap_rwsem
);
1859 down_read(&mdsc
->snap_rwsem
);
1862 /* insert trace into our cache */
1863 err
= ceph_fill_trace(mdsc
->client
->sb
, req
, req
->r_session
);
1865 if (result
== 0 && rinfo
->dir_nr
)
1866 ceph_readdir_prepopulate(req
, req
->r_session
);
1867 ceph_unreserve_caps(&req
->r_caps_reservation
);
1870 up_read(&mdsc
->snap_rwsem
);
1879 add_cap_releases(mdsc
, req
->r_session
, -1);
1880 mutex_unlock(&session
->s_mutex
);
1882 /* kick calling process */
1883 complete_request(mdsc
, req
);
1885 ceph_mdsc_put_request(req
);
1892 * handle mds notification that our request has been forwarded.
1894 static void handle_forward(struct ceph_mds_client
*mdsc
,
1895 struct ceph_mds_session
*session
,
1896 struct ceph_msg
*msg
)
1898 struct ceph_mds_request
*req
;
1899 u64 tid
= le64_to_cpu(msg
->hdr
.tid
);
1903 void *p
= msg
->front
.iov_base
;
1904 void *end
= p
+ msg
->front
.iov_len
;
1906 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
1907 next_mds
= ceph_decode_32(&p
);
1908 fwd_seq
= ceph_decode_32(&p
);
1910 mutex_lock(&mdsc
->mutex
);
1911 req
= __lookup_request(mdsc
, tid
);
1913 dout("forward %llu to mds%d - req dne\n", tid
, next_mds
);
1914 goto out
; /* dup reply? */
1917 if (fwd_seq
<= req
->r_num_fwd
) {
1918 dout("forward %llu to mds%d - old seq %d <= %d\n",
1919 tid
, next_mds
, req
->r_num_fwd
, fwd_seq
);
1921 /* resend. forward race not possible; mds would drop */
1922 dout("forward %llu to mds%d (we resend)\n", tid
, next_mds
);
1923 req
->r_num_fwd
= fwd_seq
;
1924 req
->r_resend_mds
= next_mds
;
1925 put_request_session(req
);
1926 __do_request(mdsc
, req
);
1928 ceph_mdsc_put_request(req
);
1930 mutex_unlock(&mdsc
->mutex
);
1934 pr_err("mdsc_handle_forward decode error err=%d\n", err
);
1938 * handle a mds session control message
1940 static void handle_session(struct ceph_mds_session
*session
,
1941 struct ceph_msg
*msg
)
1943 struct ceph_mds_client
*mdsc
= session
->s_mdsc
;
1946 int mds
= session
->s_mds
;
1947 struct ceph_mds_session_head
*h
= msg
->front
.iov_base
;
1951 if (msg
->front
.iov_len
!= sizeof(*h
))
1953 op
= le32_to_cpu(h
->op
);
1954 seq
= le64_to_cpu(h
->seq
);
1956 mutex_lock(&mdsc
->mutex
);
1957 if (op
== CEPH_SESSION_CLOSE
)
1958 __unregister_session(mdsc
, session
);
1959 /* FIXME: this ttl calculation is generous */
1960 session
->s_ttl
= jiffies
+ HZ
*mdsc
->mdsmap
->m_session_autoclose
;
1961 mutex_unlock(&mdsc
->mutex
);
1963 mutex_lock(&session
->s_mutex
);
1965 dout("handle_session mds%d %s %p state %s seq %llu\n",
1966 mds
, ceph_session_op_name(op
), session
,
1967 session_state_name(session
->s_state
), seq
);
1969 if (session
->s_state
== CEPH_MDS_SESSION_HUNG
) {
1970 session
->s_state
= CEPH_MDS_SESSION_OPEN
;
1971 pr_info("mds%d came back\n", session
->s_mds
);
1975 case CEPH_SESSION_OPEN
:
1976 session
->s_state
= CEPH_MDS_SESSION_OPEN
;
1977 renewed_caps(mdsc
, session
, 0);
1980 __close_session(mdsc
, session
);
1983 case CEPH_SESSION_RENEWCAPS
:
1984 if (session
->s_renew_seq
== seq
)
1985 renewed_caps(mdsc
, session
, 1);
1988 case CEPH_SESSION_CLOSE
:
1989 remove_session_caps(session
);
1990 wake
= 1; /* for good measure */
1991 complete(&mdsc
->session_close_waiters
);
1992 kick_requests(mdsc
, mds
, 0); /* cur only */
1995 case CEPH_SESSION_STALE
:
1996 pr_info("mds%d caps went stale, renewing\n",
1998 spin_lock(&session
->s_cap_lock
);
1999 session
->s_cap_gen
++;
2000 session
->s_cap_ttl
= 0;
2001 spin_unlock(&session
->s_cap_lock
);
2002 send_renew_caps(mdsc
, session
);
2005 case CEPH_SESSION_RECALL_STATE
:
2006 trim_caps(mdsc
, session
, le32_to_cpu(h
->max_caps
));
2010 pr_err("mdsc_handle_session bad op %d mds%d\n", op
, mds
);
2014 mutex_unlock(&session
->s_mutex
);
2016 mutex_lock(&mdsc
->mutex
);
2017 __wake_requests(mdsc
, &session
->s_waiting
);
2018 mutex_unlock(&mdsc
->mutex
);
2023 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds
,
2024 (int)msg
->front
.iov_len
);
2031 * called under session->mutex.
2033 static void replay_unsafe_requests(struct ceph_mds_client
*mdsc
,
2034 struct ceph_mds_session
*session
)
2036 struct ceph_mds_request
*req
, *nreq
;
2039 dout("replay_unsafe_requests mds%d\n", session
->s_mds
);
2041 mutex_lock(&mdsc
->mutex
);
2042 list_for_each_entry_safe(req
, nreq
, &session
->s_unsafe
, r_unsafe_item
) {
2043 err
= __prepare_send_request(mdsc
, req
, session
->s_mds
);
2045 ceph_msg_get(req
->r_request
);
2046 ceph_con_send(&session
->s_con
, req
->r_request
);
2049 mutex_unlock(&mdsc
->mutex
);
2053 * Encode information about a cap for a reconnect with the MDS.
2055 static int encode_caps_cb(struct inode
*inode
, struct ceph_cap
*cap
,
2058 struct ceph_mds_cap_reconnect rec
;
2059 struct ceph_inode_info
*ci
;
2060 struct ceph_pagelist
*pagelist
= arg
;
2064 struct dentry
*dentry
;
2068 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2069 inode
, ceph_vinop(inode
), cap
, cap
->cap_id
,
2070 ceph_cap_string(cap
->issued
));
2071 err
= ceph_pagelist_encode_64(pagelist
, ceph_ino(inode
));
2075 dentry
= d_find_alias(inode
);
2077 path
= ceph_mdsc_build_path(dentry
, &pathlen
, &pathbase
, 0);
2079 err
= PTR_ERR(path
);
2086 err
= ceph_pagelist_encode_string(pagelist
, path
, pathlen
);
2090 spin_lock(&inode
->i_lock
);
2091 cap
->seq
= 0; /* reset cap seq */
2092 cap
->issue_seq
= 0; /* and issue_seq */
2093 rec
.cap_id
= cpu_to_le64(cap
->cap_id
);
2094 rec
.pathbase
= cpu_to_le64(pathbase
);
2095 rec
.wanted
= cpu_to_le32(__ceph_caps_wanted(ci
));
2096 rec
.issued
= cpu_to_le32(cap
->issued
);
2097 rec
.size
= cpu_to_le64(inode
->i_size
);
2098 ceph_encode_timespec(&rec
.mtime
, &inode
->i_mtime
);
2099 ceph_encode_timespec(&rec
.atime
, &inode
->i_atime
);
2100 rec
.snaprealm
= cpu_to_le64(ci
->i_snap_realm
->ino
);
2101 spin_unlock(&inode
->i_lock
);
2103 err
= ceph_pagelist_append(pagelist
, &rec
, sizeof(rec
));
2113 * If an MDS fails and recovers, clients need to reconnect in order to
2114 * reestablish shared state. This includes all caps issued through
2115 * this session _and_ the snap_realm hierarchy. Because it's not
2116 * clear which snap realms the mds cares about, we send everything we
2117 * know about.. that ensures we'll then get any new info the
2118 * recovering MDS might have.
2120 * This is a relatively heavyweight operation, but it's rare.
2122 * called with mdsc->mutex held.
2124 static void send_mds_reconnect(struct ceph_mds_client
*mdsc
, int mds
)
2126 struct ceph_mds_session
*session
= NULL
;
2127 struct ceph_msg
*reply
;
2130 struct ceph_pagelist
*pagelist
;
2132 pr_info("reconnect to recovering mds%d\n", mds
);
2134 pagelist
= kmalloc(sizeof(*pagelist
), GFP_NOFS
);
2136 goto fail_nopagelist
;
2137 ceph_pagelist_init(pagelist
);
2139 reply
= ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT
, 0, 0, 0, NULL
);
2140 if (IS_ERR(reply
)) {
2141 err
= PTR_ERR(reply
);
2146 session
= __ceph_lookup_mds_session(mdsc
, mds
);
2147 mutex_unlock(&mdsc
->mutex
); /* drop lock for duration */
2150 mutex_lock(&session
->s_mutex
);
2152 session
->s_state
= CEPH_MDS_SESSION_RECONNECTING
;
2155 ceph_con_open(&session
->s_con
,
2156 ceph_mdsmap_get_addr(mdsc
->mdsmap
, mds
));
2158 /* replay unsafe requests */
2159 replay_unsafe_requests(mdsc
, session
);
2161 dout("no session for mds%d, will send short reconnect\n",
2165 down_read(&mdsc
->snap_rwsem
);
2169 dout("session %p state %s\n", session
,
2170 session_state_name(session
->s_state
));
2172 /* traverse this session's caps */
2173 err
= ceph_pagelist_encode_32(pagelist
, session
->s_nr_caps
);
2176 err
= iterate_session_caps(session
, encode_caps_cb
, pagelist
);
2181 * snaprealms. we provide mds with the ino, seq (version), and
2182 * parent for all of our realms. If the mds has any newer info,
2185 for (p
= rb_first(&mdsc
->snap_realms
); p
; p
= rb_next(p
)) {
2186 struct ceph_snap_realm
*realm
=
2187 rb_entry(p
, struct ceph_snap_realm
, node
);
2188 struct ceph_mds_snaprealm_reconnect sr_rec
;
2190 dout(" adding snap realm %llx seq %lld parent %llx\n",
2191 realm
->ino
, realm
->seq
, realm
->parent_ino
);
2192 sr_rec
.ino
= cpu_to_le64(realm
->ino
);
2193 sr_rec
.seq
= cpu_to_le64(realm
->seq
);
2194 sr_rec
.parent
= cpu_to_le64(realm
->parent_ino
);
2195 err
= ceph_pagelist_append(pagelist
, &sr_rec
, sizeof(sr_rec
));
2201 reply
->pagelist
= pagelist
;
2202 reply
->hdr
.data_len
= cpu_to_le32(pagelist
->length
);
2203 reply
->nr_pages
= calc_pages_for(0, pagelist
->length
);
2204 ceph_con_send(&session
->s_con
, reply
);
2207 session
->s_state
= CEPH_MDS_SESSION_OPEN
;
2208 __wake_requests(mdsc
, &session
->s_waiting
);
2212 up_read(&mdsc
->snap_rwsem
);
2214 mutex_unlock(&session
->s_mutex
);
2215 ceph_put_mds_session(session
);
2217 mutex_lock(&mdsc
->mutex
);
2221 ceph_msg_put(reply
);
2223 ceph_pagelist_release(pagelist
);
2226 pr_err("ENOMEM preparing reconnect for mds%d\n", mds
);
2232 * compare old and new mdsmaps, kicking requests
2233 * and closing out old connections as necessary
2235 * called under mdsc->mutex.
2237 static void check_new_map(struct ceph_mds_client
*mdsc
,
2238 struct ceph_mdsmap
*newmap
,
2239 struct ceph_mdsmap
*oldmap
)
2242 int oldstate
, newstate
;
2243 struct ceph_mds_session
*s
;
2245 dout("check_new_map new %u old %u\n",
2246 newmap
->m_epoch
, oldmap
->m_epoch
);
2248 for (i
= 0; i
< oldmap
->m_max_mds
&& i
< mdsc
->max_sessions
; i
++) {
2249 if (mdsc
->sessions
[i
] == NULL
)
2251 s
= mdsc
->sessions
[i
];
2252 oldstate
= ceph_mdsmap_get_state(oldmap
, i
);
2253 newstate
= ceph_mdsmap_get_state(newmap
, i
);
2255 dout("check_new_map mds%d state %s -> %s (session %s)\n",
2256 i
, ceph_mds_state_name(oldstate
),
2257 ceph_mds_state_name(newstate
),
2258 session_state_name(s
->s_state
));
2260 if (memcmp(ceph_mdsmap_get_addr(oldmap
, i
),
2261 ceph_mdsmap_get_addr(newmap
, i
),
2262 sizeof(struct ceph_entity_addr
))) {
2263 if (s
->s_state
== CEPH_MDS_SESSION_OPENING
) {
2264 /* the session never opened, just close it
2266 __wake_requests(mdsc
, &s
->s_waiting
);
2267 __unregister_session(mdsc
, s
);
2270 mutex_unlock(&mdsc
->mutex
);
2271 mutex_lock(&s
->s_mutex
);
2272 mutex_lock(&mdsc
->mutex
);
2273 ceph_con_close(&s
->s_con
);
2274 mutex_unlock(&s
->s_mutex
);
2275 s
->s_state
= CEPH_MDS_SESSION_RESTARTING
;
2278 /* kick any requests waiting on the recovering mds */
2279 kick_requests(mdsc
, i
, 1);
2280 } else if (oldstate
== newstate
) {
2281 continue; /* nothing new with this mds */
2287 if (s
->s_state
== CEPH_MDS_SESSION_RESTARTING
&&
2288 newstate
>= CEPH_MDS_STATE_RECONNECT
)
2289 send_mds_reconnect(mdsc
, i
);
2292 * kick requests on any mds that has gone active.
2294 * kick requests on cur or forwarder: we may have sent
2295 * the request to mds1, mds1 told us it forwarded it
2296 * to mds2, but then we learn mds1 failed and can't be
2297 * sure it successfully forwarded our request before
2300 if (oldstate
< CEPH_MDS_STATE_ACTIVE
&&
2301 newstate
>= CEPH_MDS_STATE_ACTIVE
) {
2302 pr_info("mds%d reconnect completed\n", s
->s_mds
);
2303 kick_requests(mdsc
, i
, 1);
2304 ceph_kick_flushing_caps(mdsc
, s
);
2305 wake_up_session_caps(s
, 1);
2317 * caller must hold session s_mutex, dentry->d_lock
2319 void __ceph_mdsc_drop_dentry_lease(struct dentry
*dentry
)
2321 struct ceph_dentry_info
*di
= ceph_dentry(dentry
);
2323 ceph_put_mds_session(di
->lease_session
);
2324 di
->lease_session
= NULL
;
2327 static void handle_lease(struct ceph_mds_client
*mdsc
,
2328 struct ceph_mds_session
*session
,
2329 struct ceph_msg
*msg
)
2331 struct super_block
*sb
= mdsc
->client
->sb
;
2332 struct inode
*inode
;
2333 struct ceph_inode_info
*ci
;
2334 struct dentry
*parent
, *dentry
;
2335 struct ceph_dentry_info
*di
;
2336 int mds
= session
->s_mds
;
2337 struct ceph_mds_lease
*h
= msg
->front
.iov_base
;
2338 struct ceph_vino vino
;
2343 dout("handle_lease from mds%d\n", mds
);
2346 if (msg
->front
.iov_len
< sizeof(*h
) + sizeof(u32
))
2348 vino
.ino
= le64_to_cpu(h
->ino
);
2349 vino
.snap
= CEPH_NOSNAP
;
2350 mask
= le16_to_cpu(h
->mask
);
2351 dname
.name
= (void *)h
+ sizeof(*h
) + sizeof(u32
);
2352 dname
.len
= msg
->front
.iov_len
- sizeof(*h
) - sizeof(u32
);
2353 if (dname
.len
!= get_unaligned_le32(h
+1))
2356 mutex_lock(&session
->s_mutex
);
2360 inode
= ceph_find_inode(sb
, vino
);
2361 dout("handle_lease '%s', mask %d, ino %llx %p\n",
2362 ceph_lease_op_name(h
->action
), mask
, vino
.ino
, inode
);
2363 if (inode
== NULL
) {
2364 dout("handle_lease no inode %llx\n", vino
.ino
);
2367 ci
= ceph_inode(inode
);
2370 parent
= d_find_alias(inode
);
2372 dout("no parent dentry on inode %p\n", inode
);
2374 goto release
; /* hrm... */
2376 dname
.hash
= full_name_hash(dname
.name
, dname
.len
);
2377 dentry
= d_lookup(parent
, &dname
);
2382 spin_lock(&dentry
->d_lock
);
2383 di
= ceph_dentry(dentry
);
2384 switch (h
->action
) {
2385 case CEPH_MDS_LEASE_REVOKE
:
2386 if (di
&& di
->lease_session
== session
) {
2387 h
->seq
= cpu_to_le32(di
->lease_seq
);
2388 __ceph_mdsc_drop_dentry_lease(dentry
);
2393 case CEPH_MDS_LEASE_RENEW
:
2394 if (di
&& di
->lease_session
== session
&&
2395 di
->lease_gen
== session
->s_cap_gen
&&
2396 di
->lease_renew_from
&&
2397 di
->lease_renew_after
== 0) {
2398 unsigned long duration
=
2399 le32_to_cpu(h
->duration_ms
) * HZ
/ 1000;
2401 di
->lease_seq
= le32_to_cpu(h
->seq
);
2402 dentry
->d_time
= di
->lease_renew_from
+ duration
;
2403 di
->lease_renew_after
= di
->lease_renew_from
+
2405 di
->lease_renew_from
= 0;
2409 spin_unlock(&dentry
->d_lock
);
2416 /* let's just reuse the same message */
2417 h
->action
= CEPH_MDS_LEASE_REVOKE_ACK
;
2419 ceph_con_send(&session
->s_con
, msg
);
2423 mutex_unlock(&session
->s_mutex
);
2427 pr_err("corrupt lease message\n");
2431 void ceph_mdsc_lease_send_msg(struct ceph_mds_session
*session
,
2432 struct inode
*inode
,
2433 struct dentry
*dentry
, char action
,
2436 struct ceph_msg
*msg
;
2437 struct ceph_mds_lease
*lease
;
2438 int len
= sizeof(*lease
) + sizeof(u32
);
2441 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
2442 inode
, dentry
, ceph_lease_op_name(action
), session
->s_mds
);
2443 dnamelen
= dentry
->d_name
.len
;
2446 msg
= ceph_msg_new(CEPH_MSG_CLIENT_LEASE
, len
, 0, 0, NULL
);
2449 lease
= msg
->front
.iov_base
;
2450 lease
->action
= action
;
2451 lease
->mask
= cpu_to_le16(CEPH_LOCK_DN
);
2452 lease
->ino
= cpu_to_le64(ceph_vino(inode
).ino
);
2453 lease
->first
= lease
->last
= cpu_to_le64(ceph_vino(inode
).snap
);
2454 lease
->seq
= cpu_to_le32(seq
);
2455 put_unaligned_le32(dnamelen
, lease
+ 1);
2456 memcpy((void *)(lease
+ 1) + 4, dentry
->d_name
.name
, dnamelen
);
2459 * if this is a preemptive lease RELEASE, no need to
2460 * flush request stream, since the actual request will
2463 msg
->more_to_follow
= (action
== CEPH_MDS_LEASE_RELEASE
);
2465 ceph_con_send(&session
->s_con
, msg
);
2469 * Preemptively release a lease we expect to invalidate anyway.
2470 * Pass @inode always, @dentry is optional.
2472 void ceph_mdsc_lease_release(struct ceph_mds_client
*mdsc
, struct inode
*inode
,
2473 struct dentry
*dentry
, int mask
)
2475 struct ceph_dentry_info
*di
;
2476 struct ceph_mds_session
*session
;
2479 BUG_ON(inode
== NULL
);
2480 BUG_ON(dentry
== NULL
);
2481 BUG_ON(mask
!= CEPH_LOCK_DN
);
2483 /* is dentry lease valid? */
2484 spin_lock(&dentry
->d_lock
);
2485 di
= ceph_dentry(dentry
);
2486 if (!di
|| !di
->lease_session
||
2487 di
->lease_session
->s_mds
< 0 ||
2488 di
->lease_gen
!= di
->lease_session
->s_cap_gen
||
2489 !time_before(jiffies
, dentry
->d_time
)) {
2490 dout("lease_release inode %p dentry %p -- "
2492 inode
, dentry
, mask
);
2493 spin_unlock(&dentry
->d_lock
);
2497 /* we do have a lease on this dentry; note mds and seq */
2498 session
= ceph_get_mds_session(di
->lease_session
);
2499 seq
= di
->lease_seq
;
2500 __ceph_mdsc_drop_dentry_lease(dentry
);
2501 spin_unlock(&dentry
->d_lock
);
2503 dout("lease_release inode %p dentry %p mask %d to mds%d\n",
2504 inode
, dentry
, mask
, session
->s_mds
);
2505 ceph_mdsc_lease_send_msg(session
, inode
, dentry
,
2506 CEPH_MDS_LEASE_RELEASE
, seq
);
2507 ceph_put_mds_session(session
);
2511 * drop all leases (and dentry refs) in preparation for umount
2513 static void drop_leases(struct ceph_mds_client
*mdsc
)
2517 dout("drop_leases\n");
2518 mutex_lock(&mdsc
->mutex
);
2519 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
2520 struct ceph_mds_session
*s
= __ceph_lookup_mds_session(mdsc
, i
);
2523 mutex_unlock(&mdsc
->mutex
);
2524 mutex_lock(&s
->s_mutex
);
2525 mutex_unlock(&s
->s_mutex
);
2526 ceph_put_mds_session(s
);
2527 mutex_lock(&mdsc
->mutex
);
2529 mutex_unlock(&mdsc
->mutex
);
2535 * delayed work -- periodically trim expired leases, renew caps with mds
2537 static void schedule_delayed(struct ceph_mds_client
*mdsc
)
2540 unsigned hz
= round_jiffies_relative(HZ
* delay
);
2541 schedule_delayed_work(&mdsc
->delayed_work
, hz
);
2544 static void delayed_work(struct work_struct
*work
)
2547 struct ceph_mds_client
*mdsc
=
2548 container_of(work
, struct ceph_mds_client
, delayed_work
.work
);
2552 dout("mdsc delayed_work\n");
2553 ceph_check_delayed_caps(mdsc
);
2555 mutex_lock(&mdsc
->mutex
);
2556 renew_interval
= mdsc
->mdsmap
->m_session_timeout
>> 2;
2557 renew_caps
= time_after_eq(jiffies
, HZ
*renew_interval
+
2558 mdsc
->last_renew_caps
);
2560 mdsc
->last_renew_caps
= jiffies
;
2562 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
2563 struct ceph_mds_session
*s
= __ceph_lookup_mds_session(mdsc
, i
);
2566 if (s
->s_state
== CEPH_MDS_SESSION_CLOSING
) {
2567 dout("resending session close request for mds%d\n",
2569 request_close_session(mdsc
, s
);
2570 ceph_put_mds_session(s
);
2573 if (s
->s_ttl
&& time_after(jiffies
, s
->s_ttl
)) {
2574 if (s
->s_state
== CEPH_MDS_SESSION_OPEN
) {
2575 s
->s_state
= CEPH_MDS_SESSION_HUNG
;
2576 pr_info("mds%d hung\n", s
->s_mds
);
2579 if (s
->s_state
< CEPH_MDS_SESSION_OPEN
) {
2580 /* this mds is failed or recovering, just wait */
2581 ceph_put_mds_session(s
);
2584 mutex_unlock(&mdsc
->mutex
);
2586 mutex_lock(&s
->s_mutex
);
2588 send_renew_caps(mdsc
, s
);
2590 ceph_con_keepalive(&s
->s_con
);
2591 add_cap_releases(mdsc
, s
, -1);
2592 send_cap_releases(mdsc
, s
);
2593 mutex_unlock(&s
->s_mutex
);
2594 ceph_put_mds_session(s
);
2596 mutex_lock(&mdsc
->mutex
);
2598 mutex_unlock(&mdsc
->mutex
);
2600 schedule_delayed(mdsc
);
2604 int ceph_mdsc_init(struct ceph_mds_client
*mdsc
, struct ceph_client
*client
)
2606 mdsc
->client
= client
;
2607 mutex_init(&mdsc
->mutex
);
2608 mdsc
->mdsmap
= kzalloc(sizeof(*mdsc
->mdsmap
), GFP_NOFS
);
2609 init_completion(&mdsc
->safe_umount_waiters
);
2610 init_completion(&mdsc
->session_close_waiters
);
2611 INIT_LIST_HEAD(&mdsc
->waiting_for_map
);
2612 mdsc
->sessions
= NULL
;
2613 mdsc
->max_sessions
= 0;
2615 init_rwsem(&mdsc
->snap_rwsem
);
2616 mdsc
->snap_realms
= RB_ROOT
;
2617 INIT_LIST_HEAD(&mdsc
->snap_empty
);
2618 spin_lock_init(&mdsc
->snap_empty_lock
);
2620 mdsc
->request_tree
= RB_ROOT
;
2621 INIT_DELAYED_WORK(&mdsc
->delayed_work
, delayed_work
);
2622 mdsc
->last_renew_caps
= jiffies
;
2623 INIT_LIST_HEAD(&mdsc
->cap_delay_list
);
2624 spin_lock_init(&mdsc
->cap_delay_lock
);
2625 INIT_LIST_HEAD(&mdsc
->snap_flush_list
);
2626 spin_lock_init(&mdsc
->snap_flush_lock
);
2627 mdsc
->cap_flush_seq
= 0;
2628 INIT_LIST_HEAD(&mdsc
->cap_dirty
);
2629 mdsc
->num_cap_flushing
= 0;
2630 spin_lock_init(&mdsc
->cap_dirty_lock
);
2631 init_waitqueue_head(&mdsc
->cap_flushing_wq
);
2632 spin_lock_init(&mdsc
->dentry_lru_lock
);
2633 INIT_LIST_HEAD(&mdsc
->dentry_lru
);
2638 * Wait for safe replies on open mds requests. If we time out, drop
2639 * all requests from the tree to avoid dangling dentry refs.
2641 static void wait_requests(struct ceph_mds_client
*mdsc
)
2643 struct ceph_mds_request
*req
;
2644 struct ceph_client
*client
= mdsc
->client
;
2646 mutex_lock(&mdsc
->mutex
);
2647 if (__get_oldest_req(mdsc
)) {
2648 mutex_unlock(&mdsc
->mutex
);
2650 dout("wait_requests waiting for requests\n");
2651 wait_for_completion_timeout(&mdsc
->safe_umount_waiters
,
2652 client
->mount_args
->mount_timeout
* HZ
);
2654 /* tear down remaining requests */
2655 mutex_lock(&mdsc
->mutex
);
2656 while ((req
= __get_oldest_req(mdsc
))) {
2657 dout("wait_requests timed out on tid %llu\n",
2659 __unregister_request(mdsc
, req
);
2662 mutex_unlock(&mdsc
->mutex
);
2663 dout("wait_requests done\n");
2667 * called before mount is ro, and before dentries are torn down.
2668 * (hmm, does this still race with new lookups?)
2670 void ceph_mdsc_pre_umount(struct ceph_mds_client
*mdsc
)
2672 dout("pre_umount\n");
2676 ceph_flush_dirty_caps(mdsc
);
2677 wait_requests(mdsc
);
2681 * wait for all write mds requests to flush.
2683 static void wait_unsafe_requests(struct ceph_mds_client
*mdsc
, u64 want_tid
)
2685 struct ceph_mds_request
*req
= NULL
;
2688 mutex_lock(&mdsc
->mutex
);
2689 dout("wait_unsafe_requests want %lld\n", want_tid
);
2690 req
= __get_oldest_req(mdsc
);
2691 while (req
&& req
->r_tid
<= want_tid
) {
2692 if ((req
->r_op
& CEPH_MDS_OP_WRITE
)) {
2694 ceph_mdsc_get_request(req
);
2695 mutex_unlock(&mdsc
->mutex
);
2696 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
2697 req
->r_tid
, want_tid
);
2698 wait_for_completion(&req
->r_safe_completion
);
2699 mutex_lock(&mdsc
->mutex
);
2700 n
= rb_next(&req
->r_node
);
2701 ceph_mdsc_put_request(req
);
2703 n
= rb_next(&req
->r_node
);
2707 req
= rb_entry(n
, struct ceph_mds_request
, r_node
);
2709 mutex_unlock(&mdsc
->mutex
);
2710 dout("wait_unsafe_requests done\n");
2713 void ceph_mdsc_sync(struct ceph_mds_client
*mdsc
)
2715 u64 want_tid
, want_flush
;
2718 mutex_lock(&mdsc
->mutex
);
2719 want_tid
= mdsc
->last_tid
;
2720 want_flush
= mdsc
->cap_flush_seq
;
2721 mutex_unlock(&mdsc
->mutex
);
2722 dout("sync want tid %lld flush_seq %lld\n", want_tid
, want_flush
);
2724 ceph_flush_dirty_caps(mdsc
);
2726 wait_unsafe_requests(mdsc
, want_tid
);
2727 wait_event(mdsc
->cap_flushing_wq
, check_cap_flush(mdsc
, want_flush
));
2732 * called after sb is ro.
2734 void ceph_mdsc_close_sessions(struct ceph_mds_client
*mdsc
)
2736 struct ceph_mds_session
*session
;
2739 struct ceph_client
*client
= mdsc
->client
;
2740 unsigned long started
, timeout
= client
->mount_args
->mount_timeout
* HZ
;
2742 dout("close_sessions\n");
2744 mutex_lock(&mdsc
->mutex
);
2746 /* close sessions */
2748 while (time_before(jiffies
, started
+ timeout
)) {
2749 dout("closing sessions\n");
2751 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
2752 session
= __ceph_lookup_mds_session(mdsc
, i
);
2755 mutex_unlock(&mdsc
->mutex
);
2756 mutex_lock(&session
->s_mutex
);
2757 __close_session(mdsc
, session
);
2758 mutex_unlock(&session
->s_mutex
);
2759 ceph_put_mds_session(session
);
2760 mutex_lock(&mdsc
->mutex
);
2766 if (client
->mount_state
== CEPH_MOUNT_SHUTDOWN
)
2769 dout("waiting for sessions to close\n");
2770 mutex_unlock(&mdsc
->mutex
);
2771 wait_for_completion_timeout(&mdsc
->session_close_waiters
,
2773 mutex_lock(&mdsc
->mutex
);
2776 /* tear down remaining sessions */
2777 for (i
= 0; i
< mdsc
->max_sessions
; i
++) {
2778 if (mdsc
->sessions
[i
]) {
2779 session
= get_session(mdsc
->sessions
[i
]);
2780 __unregister_session(mdsc
, session
);
2781 mutex_unlock(&mdsc
->mutex
);
2782 mutex_lock(&session
->s_mutex
);
2783 remove_session_caps(session
);
2784 mutex_unlock(&session
->s_mutex
);
2785 ceph_put_mds_session(session
);
2786 mutex_lock(&mdsc
->mutex
);
2790 WARN_ON(!list_empty(&mdsc
->cap_delay_list
));
2792 mutex_unlock(&mdsc
->mutex
);
2794 ceph_cleanup_empty_realms(mdsc
);
2796 cancel_delayed_work_sync(&mdsc
->delayed_work
); /* cancel timer */
2801 void ceph_mdsc_stop(struct ceph_mds_client
*mdsc
)
2804 cancel_delayed_work_sync(&mdsc
->delayed_work
); /* cancel timer */
2806 ceph_mdsmap_destroy(mdsc
->mdsmap
);
2807 kfree(mdsc
->sessions
);
2812 * handle mds map update.
2814 void ceph_mdsc_handle_map(struct ceph_mds_client
*mdsc
, struct ceph_msg
*msg
)
2818 void *p
= msg
->front
.iov_base
;
2819 void *end
= p
+ msg
->front
.iov_len
;
2820 struct ceph_mdsmap
*newmap
, *oldmap
;
2821 struct ceph_fsid fsid
;
2824 ceph_decode_need(&p
, end
, sizeof(fsid
)+2*sizeof(u32
), bad
);
2825 ceph_decode_copy(&p
, &fsid
, sizeof(fsid
));
2826 if (ceph_check_fsid(mdsc
->client
, &fsid
) < 0)
2828 epoch
= ceph_decode_32(&p
);
2829 maplen
= ceph_decode_32(&p
);
2830 dout("handle_map epoch %u len %d\n", epoch
, (int)maplen
);
2832 /* do we need it? */
2833 ceph_monc_got_mdsmap(&mdsc
->client
->monc
, epoch
);
2834 mutex_lock(&mdsc
->mutex
);
2835 if (mdsc
->mdsmap
&& epoch
<= mdsc
->mdsmap
->m_epoch
) {
2836 dout("handle_map epoch %u <= our %u\n",
2837 epoch
, mdsc
->mdsmap
->m_epoch
);
2838 mutex_unlock(&mdsc
->mutex
);
2842 newmap
= ceph_mdsmap_decode(&p
, end
);
2843 if (IS_ERR(newmap
)) {
2844 err
= PTR_ERR(newmap
);
2848 /* swap into place */
2850 oldmap
= mdsc
->mdsmap
;
2851 mdsc
->mdsmap
= newmap
;
2852 check_new_map(mdsc
, newmap
, oldmap
);
2853 ceph_mdsmap_destroy(oldmap
);
2855 mdsc
->mdsmap
= newmap
; /* first mds map */
2857 mdsc
->client
->sb
->s_maxbytes
= mdsc
->mdsmap
->m_max_file_size
;
2859 __wake_requests(mdsc
, &mdsc
->waiting_for_map
);
2861 mutex_unlock(&mdsc
->mutex
);
2862 schedule_delayed(mdsc
);
2866 mutex_unlock(&mdsc
->mutex
);
2868 pr_err("error decoding mdsmap %d\n", err
);
2872 static struct ceph_connection
*con_get(struct ceph_connection
*con
)
2874 struct ceph_mds_session
*s
= con
->private;
2876 if (get_session(s
)) {
2877 dout("mdsc con_get %p ok (%d)\n", s
, atomic_read(&s
->s_ref
));
2880 dout("mdsc con_get %p FAIL\n", s
);
2884 static void con_put(struct ceph_connection
*con
)
2886 struct ceph_mds_session
*s
= con
->private;
2888 ceph_put_mds_session(s
);
2889 dout("mdsc con_put %p (%d)\n", s
, atomic_read(&s
->s_ref
));
2893 * if the client is unresponsive for long enough, the mds will kill
2894 * the session entirely.
2896 static void peer_reset(struct ceph_connection
*con
)
2898 struct ceph_mds_session
*s
= con
->private;
2900 pr_err("mds%d gave us the boot. IMPLEMENT RECONNECT.\n",
2904 static void dispatch(struct ceph_connection
*con
, struct ceph_msg
*msg
)
2906 struct ceph_mds_session
*s
= con
->private;
2907 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
2908 int type
= le16_to_cpu(msg
->hdr
.type
);
2910 mutex_lock(&mdsc
->mutex
);
2911 if (__verify_registered_session(mdsc
, s
) < 0) {
2912 mutex_unlock(&mdsc
->mutex
);
2915 mutex_unlock(&mdsc
->mutex
);
2918 case CEPH_MSG_MDS_MAP
:
2919 ceph_mdsc_handle_map(mdsc
, msg
);
2921 case CEPH_MSG_CLIENT_SESSION
:
2922 handle_session(s
, msg
);
2924 case CEPH_MSG_CLIENT_REPLY
:
2925 handle_reply(s
, msg
);
2927 case CEPH_MSG_CLIENT_REQUEST_FORWARD
:
2928 handle_forward(mdsc
, s
, msg
);
2930 case CEPH_MSG_CLIENT_CAPS
:
2931 ceph_handle_caps(s
, msg
);
2933 case CEPH_MSG_CLIENT_SNAP
:
2934 ceph_handle_snap(mdsc
, s
, msg
);
2936 case CEPH_MSG_CLIENT_LEASE
:
2937 handle_lease(mdsc
, s
, msg
);
2941 pr_err("received unknown message type %d %s\n", type
,
2942 ceph_msg_type_name(type
));
2951 static int get_authorizer(struct ceph_connection
*con
,
2952 void **buf
, int *len
, int *proto
,
2953 void **reply_buf
, int *reply_len
, int force_new
)
2955 struct ceph_mds_session
*s
= con
->private;
2956 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
2957 struct ceph_auth_client
*ac
= mdsc
->client
->monc
.auth
;
2960 if (force_new
&& s
->s_authorizer
) {
2961 ac
->ops
->destroy_authorizer(ac
, s
->s_authorizer
);
2962 s
->s_authorizer
= NULL
;
2964 if (s
->s_authorizer
== NULL
) {
2965 if (ac
->ops
->create_authorizer
) {
2966 ret
= ac
->ops
->create_authorizer(
2967 ac
, CEPH_ENTITY_TYPE_MDS
,
2969 &s
->s_authorizer_buf
,
2970 &s
->s_authorizer_buf_len
,
2971 &s
->s_authorizer_reply_buf
,
2972 &s
->s_authorizer_reply_buf_len
);
2978 *proto
= ac
->protocol
;
2979 *buf
= s
->s_authorizer_buf
;
2980 *len
= s
->s_authorizer_buf_len
;
2981 *reply_buf
= s
->s_authorizer_reply_buf
;
2982 *reply_len
= s
->s_authorizer_reply_buf_len
;
2987 static int verify_authorizer_reply(struct ceph_connection
*con
, int len
)
2989 struct ceph_mds_session
*s
= con
->private;
2990 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
2991 struct ceph_auth_client
*ac
= mdsc
->client
->monc
.auth
;
2993 return ac
->ops
->verify_authorizer_reply(ac
, s
->s_authorizer
, len
);
2996 static int invalidate_authorizer(struct ceph_connection
*con
)
2998 struct ceph_mds_session
*s
= con
->private;
2999 struct ceph_mds_client
*mdsc
= s
->s_mdsc
;
3000 struct ceph_auth_client
*ac
= mdsc
->client
->monc
.auth
;
3002 if (ac
->ops
->invalidate_authorizer
)
3003 ac
->ops
->invalidate_authorizer(ac
, CEPH_ENTITY_TYPE_MDS
);
3005 return ceph_monc_validate_auth(&mdsc
->client
->monc
);
3008 const static struct ceph_connection_operations mds_con_ops
= {
3011 .dispatch
= dispatch
,
3012 .get_authorizer
= get_authorizer
,
3013 .verify_authorizer_reply
= verify_authorizer_reply
,
3014 .invalidate_authorizer
= invalidate_authorizer
,
3015 .peer_reset
= peer_reset
,