blob: 5c17ab44d02d673416f3033b81133e7a6371eb5d [file] [log] [blame]
Sage Weil2f2dc052009-10-06 11:31:09 -07001#include "ceph_debug.h"
2
3#include <linux/wait.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09004#include <linux/slab.h>
Sage Weil2f2dc052009-10-06 11:31:09 -07005#include <linux/sched.h>
6
7#include "mds_client.h"
8#include "mon_client.h"
9#include "super.h"
10#include "messenger.h"
11#include "decode.h"
Sage Weil4e7a5dc2009-11-18 16:19:57 -080012#include "auth.h"
Sage Weil93cea5b2009-12-23 12:21:51 -080013#include "pagelist.h"
Sage Weil2f2dc052009-10-06 11:31:09 -070014
15/*
16 * A cluster of MDS (metadata server) daemons is responsible for
17 * managing the file system namespace (the directory hierarchy and
18 * inodes) and for coordinating shared access to storage. Metadata is
19 * partitioning hierarchically across a number of servers, and that
20 * partition varies over time as the cluster adjusts the distribution
21 * in order to balance load.
22 *
23 * The MDS client is primarily responsible to managing synchronous
24 * metadata requests for operations like open, unlink, and so forth.
25 * If there is a MDS failure, we find out about it when we (possibly
26 * request and) receive a new MDS map, and can resubmit affected
27 * requests.
28 *
29 * For the most part, though, we take advantage of a lossless
30 * communications channel to the MDS, and do not need to worry about
31 * timing out or resubmitting requests.
32 *
33 * We maintain a stateful "session" with each MDS we interact with.
34 * Within each session, we sent periodic heartbeat messages to ensure
35 * any capabilities or leases we have been issues remain valid. If
36 * the session times out and goes stale, our leases and capabilities
37 * are no longer valid.
38 */
39
40static void __wake_requests(struct ceph_mds_client *mdsc,
41 struct list_head *head);
42
43const static struct ceph_connection_operations mds_con_ops;
44
45
46/*
47 * mds reply parsing
48 */
49
50/*
51 * parse individual inode info
52 */
53static int parse_reply_info_in(void **p, void *end,
54 struct ceph_mds_reply_info_in *info)
55{
56 int err = -EIO;
57
58 info->in = *p;
59 *p += sizeof(struct ceph_mds_reply_inode) +
60 sizeof(*info->in->fragtree.splits) *
61 le32_to_cpu(info->in->fragtree.nsplits);
62
63 ceph_decode_32_safe(p, end, info->symlink_len, bad);
64 ceph_decode_need(p, end, info->symlink_len, bad);
65 info->symlink = *p;
66 *p += info->symlink_len;
67
68 ceph_decode_32_safe(p, end, info->xattr_len, bad);
69 ceph_decode_need(p, end, info->xattr_len, bad);
70 info->xattr_data = *p;
71 *p += info->xattr_len;
72 return 0;
73bad:
74 return err;
75}
76
77/*
78 * parse a normal reply, which may contain a (dir+)dentry and/or a
79 * target inode.
80 */
81static int parse_reply_info_trace(void **p, void *end,
82 struct ceph_mds_reply_info_parsed *info)
83{
84 int err;
85
86 if (info->head->is_dentry) {
87 err = parse_reply_info_in(p, end, &info->diri);
88 if (err < 0)
89 goto out_bad;
90
91 if (unlikely(*p + sizeof(*info->dirfrag) > end))
92 goto bad;
93 info->dirfrag = *p;
94 *p += sizeof(*info->dirfrag) +
95 sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
96 if (unlikely(*p > end))
97 goto bad;
98
99 ceph_decode_32_safe(p, end, info->dname_len, bad);
100 ceph_decode_need(p, end, info->dname_len, bad);
101 info->dname = *p;
102 *p += info->dname_len;
103 info->dlease = *p;
104 *p += sizeof(*info->dlease);
105 }
106
107 if (info->head->is_target) {
108 err = parse_reply_info_in(p, end, &info->targeti);
109 if (err < 0)
110 goto out_bad;
111 }
112
113 if (unlikely(*p != end))
114 goto bad;
115 return 0;
116
117bad:
118 err = -EIO;
119out_bad:
120 pr_err("problem parsing mds trace %d\n", err);
121 return err;
122}
123
124/*
125 * parse readdir results
126 */
127static int parse_reply_info_dir(void **p, void *end,
128 struct ceph_mds_reply_info_parsed *info)
129{
130 u32 num, i = 0;
131 int err;
132
133 info->dir_dir = *p;
134 if (*p + sizeof(*info->dir_dir) > end)
135 goto bad;
136 *p += sizeof(*info->dir_dir) +
137 sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
138 if (*p > end)
139 goto bad;
140
141 ceph_decode_need(p, end, sizeof(num) + 2, bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700142 num = ceph_decode_32(p);
143 info->dir_end = ceph_decode_8(p);
144 info->dir_complete = ceph_decode_8(p);
Sage Weil2f2dc052009-10-06 11:31:09 -0700145 if (num == 0)
146 goto done;
147
148 /* alloc large array */
149 info->dir_nr = num;
150 info->dir_in = kcalloc(num, sizeof(*info->dir_in) +
151 sizeof(*info->dir_dname) +
152 sizeof(*info->dir_dname_len) +
153 sizeof(*info->dir_dlease),
154 GFP_NOFS);
155 if (info->dir_in == NULL) {
156 err = -ENOMEM;
157 goto out_bad;
158 }
159 info->dir_dname = (void *)(info->dir_in + num);
160 info->dir_dname_len = (void *)(info->dir_dname + num);
161 info->dir_dlease = (void *)(info->dir_dname_len + num);
162
163 while (num) {
164 /* dentry */
165 ceph_decode_need(p, end, sizeof(u32)*2, bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700166 info->dir_dname_len[i] = ceph_decode_32(p);
Sage Weil2f2dc052009-10-06 11:31:09 -0700167 ceph_decode_need(p, end, info->dir_dname_len[i], bad);
168 info->dir_dname[i] = *p;
169 *p += info->dir_dname_len[i];
170 dout("parsed dir dname '%.*s'\n", info->dir_dname_len[i],
171 info->dir_dname[i]);
172 info->dir_dlease[i] = *p;
173 *p += sizeof(struct ceph_mds_reply_lease);
174
175 /* inode */
176 err = parse_reply_info_in(p, end, &info->dir_in[i]);
177 if (err < 0)
178 goto out_bad;
179 i++;
180 num--;
181 }
182
183done:
184 if (*p != end)
185 goto bad;
186 return 0;
187
188bad:
189 err = -EIO;
190out_bad:
191 pr_err("problem parsing dir contents %d\n", err);
192 return err;
193}
194
195/*
196 * parse entire mds reply
197 */
198static int parse_reply_info(struct ceph_msg *msg,
199 struct ceph_mds_reply_info_parsed *info)
200{
201 void *p, *end;
202 u32 len;
203 int err;
204
205 info->head = msg->front.iov_base;
206 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
207 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
208
209 /* trace */
210 ceph_decode_32_safe(&p, end, len, bad);
211 if (len > 0) {
212 err = parse_reply_info_trace(&p, p+len, info);
213 if (err < 0)
214 goto out_bad;
215 }
216
217 /* dir content */
218 ceph_decode_32_safe(&p, end, len, bad);
219 if (len > 0) {
220 err = parse_reply_info_dir(&p, p+len, info);
221 if (err < 0)
222 goto out_bad;
223 }
224
225 /* snap blob */
226 ceph_decode_32_safe(&p, end, len, bad);
227 info->snapblob_len = len;
228 info->snapblob = p;
229 p += len;
230
231 if (p != end)
232 goto bad;
233 return 0;
234
235bad:
236 err = -EIO;
237out_bad:
238 pr_err("mds parse_reply err %d\n", err);
239 return err;
240}
241
242static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
243{
244 kfree(info->dir_in);
245}
246
247
248/*
249 * sessions
250 */
251static const char *session_state_name(int s)
252{
253 switch (s) {
254 case CEPH_MDS_SESSION_NEW: return "new";
255 case CEPH_MDS_SESSION_OPENING: return "opening";
256 case CEPH_MDS_SESSION_OPEN: return "open";
257 case CEPH_MDS_SESSION_HUNG: return "hung";
258 case CEPH_MDS_SESSION_CLOSING: return "closing";
Sage Weil44ca18f2010-02-15 12:08:46 -0800259 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
Sage Weil2f2dc052009-10-06 11:31:09 -0700260 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
261 default: return "???";
262 }
263}
264
265static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
266{
267 if (atomic_inc_not_zero(&s->s_ref)) {
268 dout("mdsc get_session %p %d -> %d\n", s,
269 atomic_read(&s->s_ref)-1, atomic_read(&s->s_ref));
270 return s;
271 } else {
272 dout("mdsc get_session %p 0 -- FAIL", s);
273 return NULL;
274 }
275}
276
277void ceph_put_mds_session(struct ceph_mds_session *s)
278{
279 dout("mdsc put_session %p %d -> %d\n", s,
280 atomic_read(&s->s_ref), atomic_read(&s->s_ref)-1);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800281 if (atomic_dec_and_test(&s->s_ref)) {
282 if (s->s_authorizer)
283 s->s_mdsc->client->monc.auth->ops->destroy_authorizer(
284 s->s_mdsc->client->monc.auth, s->s_authorizer);
Sage Weil2f2dc052009-10-06 11:31:09 -0700285 kfree(s);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800286 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700287}
288
289/*
290 * called under mdsc->mutex
291 */
292struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
293 int mds)
294{
295 struct ceph_mds_session *session;
296
297 if (mds >= mdsc->max_sessions || mdsc->sessions[mds] == NULL)
298 return NULL;
299 session = mdsc->sessions[mds];
300 dout("lookup_mds_session %p %d\n", session,
301 atomic_read(&session->s_ref));
302 get_session(session);
303 return session;
304}
305
306static bool __have_session(struct ceph_mds_client *mdsc, int mds)
307{
308 if (mds >= mdsc->max_sessions)
309 return false;
310 return mdsc->sessions[mds];
311}
312
Sage Weil2600d2d2010-02-22 15:12:16 -0800313static int __verify_registered_session(struct ceph_mds_client *mdsc,
314 struct ceph_mds_session *s)
315{
316 if (s->s_mds >= mdsc->max_sessions ||
317 mdsc->sessions[s->s_mds] != s)
318 return -ENOENT;
319 return 0;
320}
321
Sage Weil2f2dc052009-10-06 11:31:09 -0700322/*
323 * create+register a new session for given mds.
324 * called under mdsc->mutex.
325 */
326static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
327 int mds)
328{
329 struct ceph_mds_session *s;
330
331 s = kzalloc(sizeof(*s), GFP_NOFS);
Dan Carpenter4736b002010-03-20 15:30:16 +0300332 if (!s)
333 return ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -0700334 s->s_mdsc = mdsc;
335 s->s_mds = mds;
336 s->s_state = CEPH_MDS_SESSION_NEW;
337 s->s_ttl = 0;
338 s->s_seq = 0;
339 mutex_init(&s->s_mutex);
340
341 ceph_con_init(mdsc->client->msgr, &s->s_con);
342 s->s_con.private = s;
343 s->s_con.ops = &mds_con_ops;
344 s->s_con.peer_name.type = CEPH_ENTITY_TYPE_MDS;
345 s->s_con.peer_name.num = cpu_to_le64(mds);
Sage Weil2f2dc052009-10-06 11:31:09 -0700346
347 spin_lock_init(&s->s_cap_lock);
348 s->s_cap_gen = 0;
349 s->s_cap_ttl = 0;
350 s->s_renew_requested = 0;
351 s->s_renew_seq = 0;
352 INIT_LIST_HEAD(&s->s_caps);
353 s->s_nr_caps = 0;
Sage Weil5dacf092009-12-21 20:40:34 -0800354 s->s_trim_caps = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -0700355 atomic_set(&s->s_ref, 1);
356 INIT_LIST_HEAD(&s->s_waiting);
357 INIT_LIST_HEAD(&s->s_unsafe);
358 s->s_num_cap_releases = 0;
Sage Weil7c1332b2010-02-16 11:39:45 -0800359 s->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700360 INIT_LIST_HEAD(&s->s_cap_releases);
361 INIT_LIST_HEAD(&s->s_cap_releases_done);
362 INIT_LIST_HEAD(&s->s_cap_flushing);
363 INIT_LIST_HEAD(&s->s_cap_snaps_flushing);
364
365 dout("register_session mds%d\n", mds);
366 if (mds >= mdsc->max_sessions) {
367 int newmax = 1 << get_count_order(mds+1);
368 struct ceph_mds_session **sa;
369
370 dout("register_session realloc to %d\n", newmax);
371 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
372 if (sa == NULL)
Sage Weil42ce56e2009-11-18 11:22:36 -0800373 goto fail_realloc;
Sage Weil2f2dc052009-10-06 11:31:09 -0700374 if (mdsc->sessions) {
375 memcpy(sa, mdsc->sessions,
376 mdsc->max_sessions * sizeof(void *));
377 kfree(mdsc->sessions);
378 }
379 mdsc->sessions = sa;
380 mdsc->max_sessions = newmax;
381 }
382 mdsc->sessions[mds] = s;
383 atomic_inc(&s->s_ref); /* one ref to sessions[], one to caller */
Sage Weil42ce56e2009-11-18 11:22:36 -0800384
385 ceph_con_open(&s->s_con, ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
386
Sage Weil2f2dc052009-10-06 11:31:09 -0700387 return s;
Sage Weil42ce56e2009-11-18 11:22:36 -0800388
389fail_realloc:
390 kfree(s);
391 return ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -0700392}
393
394/*
395 * called under mdsc->mutex
396 */
Sage Weil2600d2d2010-02-22 15:12:16 -0800397static void __unregister_session(struct ceph_mds_client *mdsc,
Sage Weil42ce56e2009-11-18 11:22:36 -0800398 struct ceph_mds_session *s)
Sage Weil2f2dc052009-10-06 11:31:09 -0700399{
Sage Weil2600d2d2010-02-22 15:12:16 -0800400 dout("__unregister_session mds%d %p\n", s->s_mds, s);
401 BUG_ON(mdsc->sessions[s->s_mds] != s);
Sage Weil42ce56e2009-11-18 11:22:36 -0800402 mdsc->sessions[s->s_mds] = NULL;
403 ceph_con_close(&s->s_con);
404 ceph_put_mds_session(s);
Sage Weil2f2dc052009-10-06 11:31:09 -0700405}
406
407/*
408 * drop session refs in request.
409 *
410 * should be last request ref, or hold mdsc->mutex
411 */
412static void put_request_session(struct ceph_mds_request *req)
413{
414 if (req->r_session) {
415 ceph_put_mds_session(req->r_session);
416 req->r_session = NULL;
417 }
418}
419
Sage Weil153c8e62009-12-07 12:31:09 -0800420void ceph_mdsc_release_request(struct kref *kref)
Sage Weil2f2dc052009-10-06 11:31:09 -0700421{
Sage Weil153c8e62009-12-07 12:31:09 -0800422 struct ceph_mds_request *req = container_of(kref,
423 struct ceph_mds_request,
424 r_kref);
425 if (req->r_request)
426 ceph_msg_put(req->r_request);
427 if (req->r_reply) {
428 ceph_msg_put(req->r_reply);
429 destroy_reply_info(&req->r_reply_info);
Sage Weil2f2dc052009-10-06 11:31:09 -0700430 }
Sage Weil153c8e62009-12-07 12:31:09 -0800431 if (req->r_inode) {
432 ceph_put_cap_refs(ceph_inode(req->r_inode),
433 CEPH_CAP_PIN);
434 iput(req->r_inode);
435 }
436 if (req->r_locked_dir)
437 ceph_put_cap_refs(ceph_inode(req->r_locked_dir),
438 CEPH_CAP_PIN);
439 if (req->r_target_inode)
440 iput(req->r_target_inode);
441 if (req->r_dentry)
442 dput(req->r_dentry);
443 if (req->r_old_dentry) {
444 ceph_put_cap_refs(
445 ceph_inode(req->r_old_dentry->d_parent->d_inode),
446 CEPH_CAP_PIN);
447 dput(req->r_old_dentry);
448 }
449 kfree(req->r_path1);
450 kfree(req->r_path2);
451 put_request_session(req);
452 ceph_unreserve_caps(&req->r_caps_reservation);
453 kfree(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700454}
455
456/*
457 * lookup session, bump ref if found.
458 *
459 * called under mdsc->mutex.
460 */
461static struct ceph_mds_request *__lookup_request(struct ceph_mds_client *mdsc,
462 u64 tid)
463{
464 struct ceph_mds_request *req;
Sage Weil44ca18f2010-02-15 12:08:46 -0800465 struct rb_node *n = mdsc->request_tree.rb_node;
466
467 while (n) {
468 req = rb_entry(n, struct ceph_mds_request, r_node);
469 if (tid < req->r_tid)
470 n = n->rb_left;
471 else if (tid > req->r_tid)
472 n = n->rb_right;
473 else {
474 ceph_mdsc_get_request(req);
475 return req;
476 }
477 }
478 return NULL;
479}
480
481static void __insert_request(struct ceph_mds_client *mdsc,
482 struct ceph_mds_request *new)
483{
484 struct rb_node **p = &mdsc->request_tree.rb_node;
485 struct rb_node *parent = NULL;
486 struct ceph_mds_request *req = NULL;
487
488 while (*p) {
489 parent = *p;
490 req = rb_entry(parent, struct ceph_mds_request, r_node);
491 if (new->r_tid < req->r_tid)
492 p = &(*p)->rb_left;
493 else if (new->r_tid > req->r_tid)
494 p = &(*p)->rb_right;
495 else
496 BUG();
497 }
498
499 rb_link_node(&new->r_node, parent, p);
500 rb_insert_color(&new->r_node, &mdsc->request_tree);
Sage Weil2f2dc052009-10-06 11:31:09 -0700501}
502
503/*
504 * Register an in-flight request, and assign a tid. Link to directory
505 * are modifying (if any).
506 *
507 * Called under mdsc->mutex.
508 */
509static void __register_request(struct ceph_mds_client *mdsc,
510 struct ceph_mds_request *req,
511 struct inode *dir)
512{
513 req->r_tid = ++mdsc->last_tid;
514 if (req->r_num_caps)
515 ceph_reserve_caps(&req->r_caps_reservation, req->r_num_caps);
516 dout("__register_request %p tid %lld\n", req, req->r_tid);
517 ceph_mdsc_get_request(req);
Sage Weil44ca18f2010-02-15 12:08:46 -0800518 __insert_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700519
520 if (dir) {
521 struct ceph_inode_info *ci = ceph_inode(dir);
522
523 spin_lock(&ci->i_unsafe_lock);
524 req->r_unsafe_dir = dir;
525 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
526 spin_unlock(&ci->i_unsafe_lock);
527 }
528}
529
530static void __unregister_request(struct ceph_mds_client *mdsc,
531 struct ceph_mds_request *req)
532{
533 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
Sage Weil44ca18f2010-02-15 12:08:46 -0800534 rb_erase(&req->r_node, &mdsc->request_tree);
Sage Weil80fc7312010-03-16 15:28:54 -0700535 RB_CLEAR_NODE(&req->r_node);
Sage Weil2f2dc052009-10-06 11:31:09 -0700536
537 if (req->r_unsafe_dir) {
538 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
539
540 spin_lock(&ci->i_unsafe_lock);
541 list_del_init(&req->r_unsafe_dir_item);
542 spin_unlock(&ci->i_unsafe_lock);
543 }
Sage Weil94aa8ae2010-03-28 21:22:50 -0700544
545 ceph_mdsc_put_request(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700546}
547
548/*
549 * Choose mds to send request to next. If there is a hint set in the
550 * request (e.g., due to a prior forward hint from the mds), use that.
551 * Otherwise, consult frag tree and/or caps to identify the
552 * appropriate mds. If all else fails, choose randomly.
553 *
554 * Called under mdsc->mutex.
555 */
556static int __choose_mds(struct ceph_mds_client *mdsc,
557 struct ceph_mds_request *req)
558{
559 struct inode *inode;
560 struct ceph_inode_info *ci;
561 struct ceph_cap *cap;
562 int mode = req->r_direct_mode;
563 int mds = -1;
564 u32 hash = req->r_direct_hash;
565 bool is_hash = req->r_direct_is_hash;
566
567 /*
568 * is there a specific mds we should try? ignore hint if we have
569 * no session and the mds is not up (active or recovering).
570 */
571 if (req->r_resend_mds >= 0 &&
572 (__have_session(mdsc, req->r_resend_mds) ||
573 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
574 dout("choose_mds using resend_mds mds%d\n",
575 req->r_resend_mds);
576 return req->r_resend_mds;
577 }
578
579 if (mode == USE_RANDOM_MDS)
580 goto random;
581
582 inode = NULL;
583 if (req->r_inode) {
584 inode = req->r_inode;
585 } else if (req->r_dentry) {
586 if (req->r_dentry->d_inode) {
587 inode = req->r_dentry->d_inode;
588 } else {
589 inode = req->r_dentry->d_parent->d_inode;
590 hash = req->r_dentry->d_name.hash;
591 is_hash = true;
592 }
593 }
594 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
595 (int)hash, mode);
596 if (!inode)
597 goto random;
598 ci = ceph_inode(inode);
599
600 if (is_hash && S_ISDIR(inode->i_mode)) {
601 struct ceph_inode_frag frag;
602 int found;
603
604 ceph_choose_frag(ci, hash, &frag, &found);
605 if (found) {
606 if (mode == USE_ANY_MDS && frag.ndist > 0) {
607 u8 r;
608
609 /* choose a random replica */
610 get_random_bytes(&r, 1);
611 r %= frag.ndist;
612 mds = frag.dist[r];
613 dout("choose_mds %p %llx.%llx "
614 "frag %u mds%d (%d/%d)\n",
615 inode, ceph_vinop(inode),
616 frag.frag, frag.mds,
617 (int)r, frag.ndist);
618 return mds;
619 }
620
621 /* since this file/dir wasn't known to be
622 * replicated, then we want to look for the
623 * authoritative mds. */
624 mode = USE_AUTH_MDS;
625 if (frag.mds >= 0) {
626 /* choose auth mds */
627 mds = frag.mds;
628 dout("choose_mds %p %llx.%llx "
629 "frag %u mds%d (auth)\n",
630 inode, ceph_vinop(inode), frag.frag, mds);
631 return mds;
632 }
633 }
634 }
635
636 spin_lock(&inode->i_lock);
637 cap = NULL;
638 if (mode == USE_AUTH_MDS)
639 cap = ci->i_auth_cap;
640 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
641 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
642 if (!cap) {
643 spin_unlock(&inode->i_lock);
644 goto random;
645 }
646 mds = cap->session->s_mds;
647 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
648 inode, ceph_vinop(inode), mds,
649 cap == ci->i_auth_cap ? "auth " : "", cap);
650 spin_unlock(&inode->i_lock);
651 return mds;
652
653random:
654 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
655 dout("choose_mds chose random mds%d\n", mds);
656 return mds;
657}
658
659
660/*
661 * session messages
662 */
663static struct ceph_msg *create_session_msg(u32 op, u64 seq)
664{
665 struct ceph_msg *msg;
666 struct ceph_mds_session_head *h;
667
Sage Weilbb257662010-04-01 16:07:23 -0700668 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h));
Sage Weila79832f2010-04-01 16:06:19 -0700669 if (!msg) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700670 pr_err("create_session_msg ENOMEM creating msg\n");
Sage Weila79832f2010-04-01 16:06:19 -0700671 return NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700672 }
673 h = msg->front.iov_base;
674 h->op = cpu_to_le32(op);
675 h->seq = cpu_to_le64(seq);
676 return msg;
677}
678
679/*
680 * send session open request.
681 *
682 * called under mdsc->mutex
683 */
684static int __open_session(struct ceph_mds_client *mdsc,
685 struct ceph_mds_session *session)
686{
687 struct ceph_msg *msg;
688 int mstate;
689 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -0700690
691 /* wait for mds to go active? */
692 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
693 dout("open_session to mds%d (%s)\n", mds,
694 ceph_mds_state_name(mstate));
695 session->s_state = CEPH_MDS_SESSION_OPENING;
696 session->s_renew_requested = jiffies;
697
698 /* send connect message */
699 msg = create_session_msg(CEPH_SESSION_REQUEST_OPEN, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -0700700 if (!msg)
701 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -0700702 ceph_con_send(&session->s_con, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -0700703 return 0;
704}
705
706/*
707 * session caps
708 */
709
710/*
711 * Free preallocated cap messages assigned to this session
712 */
713static void cleanup_cap_releases(struct ceph_mds_session *session)
714{
715 struct ceph_msg *msg;
716
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);
722 ceph_msg_put(msg);
723 }
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);
728 ceph_msg_put(msg);
729 }
730 spin_unlock(&session->s_cap_lock);
731}
732
733/*
Sage Weilf818a732010-05-11 20:56:31 -0700734 * Helper to safely iterate over all caps associated with a session, with
735 * special care taken to handle a racing __ceph_remove_cap().
Sage Weil2f2dc052009-10-06 11:31:09 -0700736 *
Sage Weilf818a732010-05-11 20:56:31 -0700737 * Caller must hold session s_mutex.
Sage Weil2f2dc052009-10-06 11:31:09 -0700738 */
739static int iterate_session_caps(struct ceph_mds_session *session,
740 int (*cb)(struct inode *, struct ceph_cap *,
741 void *), void *arg)
742{
Sage Weil7c1332b2010-02-16 11:39:45 -0800743 struct list_head *p;
744 struct ceph_cap *cap;
745 struct inode *inode, *last_inode = NULL;
746 struct ceph_cap *old_cap = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700747 int ret;
748
749 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
750 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800751 p = session->s_caps.next;
752 while (p != &session->s_caps) {
753 cap = list_entry(p, struct ceph_cap, session_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -0700754 inode = igrab(&cap->ci->vfs_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -0800755 if (!inode) {
756 p = p->next;
Sage Weil2f2dc052009-10-06 11:31:09 -0700757 continue;
Sage Weil7c1332b2010-02-16 11:39:45 -0800758 }
759 session->s_cap_iterator = cap;
Sage Weil2f2dc052009-10-06 11:31:09 -0700760 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800761
762 if (last_inode) {
763 iput(last_inode);
764 last_inode = NULL;
765 }
766 if (old_cap) {
767 ceph_put_cap(old_cap);
768 old_cap = NULL;
769 }
770
Sage Weil2f2dc052009-10-06 11:31:09 -0700771 ret = cb(inode, cap, arg);
Sage Weil7c1332b2010-02-16 11:39:45 -0800772 last_inode = inode;
773
Sage Weil2f2dc052009-10-06 11:31:09 -0700774 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800775 p = p->next;
776 if (cap->ci == NULL) {
777 dout("iterate_session_caps finishing cap %p removal\n",
778 cap);
779 BUG_ON(cap->session != session);
780 list_del_init(&cap->session_caps);
781 session->s_nr_caps--;
782 cap->session = NULL;
783 old_cap = cap; /* put_cap it w/o locks held */
784 }
Sage Weil5dacf092009-12-21 20:40:34 -0800785 if (ret < 0)
786 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -0700787 }
Sage Weil5dacf092009-12-21 20:40:34 -0800788 ret = 0;
789out:
Sage Weil7c1332b2010-02-16 11:39:45 -0800790 session->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700791 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800792
793 if (last_inode)
794 iput(last_inode);
795 if (old_cap)
796 ceph_put_cap(old_cap);
797
Sage Weil5dacf092009-12-21 20:40:34 -0800798 return ret;
Sage Weil2f2dc052009-10-06 11:31:09 -0700799}
800
801static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
Sage Weil6c99f252010-05-10 16:12:25 -0700802 void *arg)
Sage Weil2f2dc052009-10-06 11:31:09 -0700803{
804 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil6c99f252010-05-10 16:12:25 -0700805 int drop = 0;
806
Sage Weil2f2dc052009-10-06 11:31:09 -0700807 dout("removing cap %p, ci is %p, inode is %p\n",
808 cap, ci, &ci->vfs_inode);
Sage Weil6c99f252010-05-10 16:12:25 -0700809 spin_lock(&inode->i_lock);
810 __ceph_remove_cap(cap);
811 if (!__ceph_is_any_real_caps(ci)) {
812 struct ceph_mds_client *mdsc =
813 &ceph_sb_to_client(inode->i_sb)->mdsc;
814
815 spin_lock(&mdsc->cap_dirty_lock);
816 if (!list_empty(&ci->i_dirty_item)) {
817 pr_info(" dropping dirty %s state for %p %lld\n",
818 ceph_cap_string(ci->i_dirty_caps),
819 inode, ceph_ino(inode));
820 ci->i_dirty_caps = 0;
821 list_del_init(&ci->i_dirty_item);
822 drop = 1;
823 }
824 if (!list_empty(&ci->i_flushing_item)) {
825 pr_info(" dropping dirty+flushing %s state for %p %lld\n",
826 ceph_cap_string(ci->i_flushing_caps),
827 inode, ceph_ino(inode));
828 ci->i_flushing_caps = 0;
829 list_del_init(&ci->i_flushing_item);
830 mdsc->num_cap_flushing--;
831 drop = 1;
832 }
833 if (drop && ci->i_wrbuffer_ref) {
834 pr_info(" dropping dirty data for %p %lld\n",
835 inode, ceph_ino(inode));
836 ci->i_wrbuffer_ref = 0;
837 ci->i_wrbuffer_ref_head = 0;
838 drop++;
839 }
840 spin_unlock(&mdsc->cap_dirty_lock);
841 }
842 spin_unlock(&inode->i_lock);
843 while (drop--)
844 iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -0700845 return 0;
846}
847
848/*
849 * caller must hold session s_mutex
850 */
851static void remove_session_caps(struct ceph_mds_session *session)
852{
853 dout("remove_session_caps on %p\n", session);
854 iterate_session_caps(session, remove_session_caps_cb, NULL);
855 BUG_ON(session->s_nr_caps > 0);
Sage Weil6c99f252010-05-10 16:12:25 -0700856 BUG_ON(!list_empty(&session->s_cap_flushing));
Sage Weil2f2dc052009-10-06 11:31:09 -0700857 cleanup_cap_releases(session);
858}
859
860/*
861 * wake up any threads waiting on this session's caps. if the cap is
862 * old (didn't get renewed on the client reconnect), remove it now.
863 *
864 * caller must hold s_mutex.
865 */
866static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
867 void *arg)
868{
Sage Weil0dc25702009-11-20 13:43:45 -0800869 struct ceph_inode_info *ci = ceph_inode(inode);
870
871 wake_up(&ci->i_cap_wq);
872 if (arg) {
873 spin_lock(&inode->i_lock);
874 ci->i_wanted_max_size = 0;
875 ci->i_requested_max_size = 0;
876 spin_unlock(&inode->i_lock);
877 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700878 return 0;
879}
880
Sage Weil0dc25702009-11-20 13:43:45 -0800881static void wake_up_session_caps(struct ceph_mds_session *session,
882 int reconnect)
Sage Weil2f2dc052009-10-06 11:31:09 -0700883{
884 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
Sage Weil0dc25702009-11-20 13:43:45 -0800885 iterate_session_caps(session, wake_up_session_cb,
886 (void *)(unsigned long)reconnect);
Sage Weil2f2dc052009-10-06 11:31:09 -0700887}
888
889/*
890 * Send periodic message to MDS renewing all currently held caps. The
891 * ack will reset the expiration for all caps from this session.
892 *
893 * caller holds s_mutex
894 */
895static int send_renew_caps(struct ceph_mds_client *mdsc,
896 struct ceph_mds_session *session)
897{
898 struct ceph_msg *msg;
899 int state;
900
901 if (time_after_eq(jiffies, session->s_cap_ttl) &&
902 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
903 pr_info("mds%d caps stale\n", session->s_mds);
Sage Weile4cb4cb2010-03-18 13:43:09 -0700904 session->s_renew_requested = jiffies;
Sage Weil2f2dc052009-10-06 11:31:09 -0700905
906 /* do not try to renew caps until a recovering mds has reconnected
907 * with its clients. */
908 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
909 if (state < CEPH_MDS_STATE_RECONNECT) {
910 dout("send_renew_caps ignoring mds%d (%s)\n",
911 session->s_mds, ceph_mds_state_name(state));
912 return 0;
913 }
914
915 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
916 ceph_mds_state_name(state));
Sage Weil2f2dc052009-10-06 11:31:09 -0700917 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
918 ++session->s_renew_seq);
Sage Weila79832f2010-04-01 16:06:19 -0700919 if (!msg)
920 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -0700921 ceph_con_send(&session->s_con, msg);
922 return 0;
923}
924
925/*
926 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
Sage Weil0dc25702009-11-20 13:43:45 -0800927 *
928 * Called under session->s_mutex
Sage Weil2f2dc052009-10-06 11:31:09 -0700929 */
930static void renewed_caps(struct ceph_mds_client *mdsc,
931 struct ceph_mds_session *session, int is_renew)
932{
933 int was_stale;
934 int wake = 0;
935
936 spin_lock(&session->s_cap_lock);
937 was_stale = is_renew && (session->s_cap_ttl == 0 ||
938 time_after_eq(jiffies, session->s_cap_ttl));
939
940 session->s_cap_ttl = session->s_renew_requested +
941 mdsc->mdsmap->m_session_timeout*HZ;
942
943 if (was_stale) {
944 if (time_before(jiffies, session->s_cap_ttl)) {
945 pr_info("mds%d caps renewed\n", session->s_mds);
946 wake = 1;
947 } else {
948 pr_info("mds%d caps still stale\n", session->s_mds);
949 }
950 }
951 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
952 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
953 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
954 spin_unlock(&session->s_cap_lock);
955
956 if (wake)
Sage Weil0dc25702009-11-20 13:43:45 -0800957 wake_up_session_caps(session, 0);
Sage Weil2f2dc052009-10-06 11:31:09 -0700958}
959
960/*
961 * send a session close request
962 */
963static int request_close_session(struct ceph_mds_client *mdsc,
964 struct ceph_mds_session *session)
965{
966 struct ceph_msg *msg;
Sage Weil2f2dc052009-10-06 11:31:09 -0700967
968 dout("request_close_session mds%d state %s seq %lld\n",
969 session->s_mds, session_state_name(session->s_state),
970 session->s_seq);
971 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -0700972 if (!msg)
973 return -ENOMEM;
974 ceph_con_send(&session->s_con, msg);
975 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -0700976}
977
978/*
979 * Called with s_mutex held.
980 */
981static int __close_session(struct ceph_mds_client *mdsc,
982 struct ceph_mds_session *session)
983{
984 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
985 return 0;
986 session->s_state = CEPH_MDS_SESSION_CLOSING;
987 return request_close_session(mdsc, session);
988}
989
990/*
991 * Trim old(er) caps.
992 *
993 * Because we can't cache an inode without one or more caps, we do
994 * this indirectly: if a cap is unused, we prune its aliases, at which
995 * point the inode will hopefully get dropped to.
996 *
997 * Yes, this is a bit sloppy. Our only real goal here is to respond to
998 * memory pressure from the MDS, though, so it needn't be perfect.
999 */
1000static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1001{
1002 struct ceph_mds_session *session = arg;
1003 struct ceph_inode_info *ci = ceph_inode(inode);
1004 int used, oissued, mine;
1005
1006 if (session->s_trim_caps <= 0)
1007 return -1;
1008
1009 spin_lock(&inode->i_lock);
1010 mine = cap->issued | cap->implemented;
1011 used = __ceph_caps_used(ci);
1012 oissued = __ceph_caps_issued_other(ci, cap);
1013
1014 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n",
1015 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
1016 ceph_cap_string(used));
1017 if (ci->i_dirty_caps)
1018 goto out; /* dirty caps */
1019 if ((used & ~oissued) & mine)
1020 goto out; /* we need these caps */
1021
1022 session->s_trim_caps--;
1023 if (oissued) {
1024 /* we aren't the only cap.. just remove us */
Sage Weil7c1332b2010-02-16 11:39:45 -08001025 __ceph_remove_cap(cap);
Sage Weil2f2dc052009-10-06 11:31:09 -07001026 } else {
1027 /* try to drop referring dentries */
1028 spin_unlock(&inode->i_lock);
1029 d_prune_aliases(inode);
1030 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1031 inode, cap, atomic_read(&inode->i_count));
1032 return 0;
1033 }
1034
1035out:
1036 spin_unlock(&inode->i_lock);
1037 return 0;
1038}
1039
1040/*
1041 * Trim session cap count down to some max number.
1042 */
1043static int trim_caps(struct ceph_mds_client *mdsc,
1044 struct ceph_mds_session *session,
1045 int max_caps)
1046{
1047 int trim_caps = session->s_nr_caps - max_caps;
1048
1049 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1050 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1051 if (trim_caps > 0) {
1052 session->s_trim_caps = trim_caps;
1053 iterate_session_caps(session, trim_caps_cb, session);
1054 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1055 session->s_mds, session->s_nr_caps, max_caps,
1056 trim_caps - session->s_trim_caps);
Sage Weil5dacf092009-12-21 20:40:34 -08001057 session->s_trim_caps = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001058 }
1059 return 0;
1060}
1061
1062/*
1063 * Allocate cap_release messages. If there is a partially full message
1064 * in the queue, try to allocate enough to cover it's remainder, so that
1065 * we can send it immediately.
1066 *
1067 * Called under s_mutex.
1068 */
1069static int add_cap_releases(struct ceph_mds_client *mdsc,
1070 struct ceph_mds_session *session,
1071 int extra)
1072{
1073 struct ceph_msg *msg;
1074 struct ceph_mds_cap_release *head;
1075 int err = -ENOMEM;
1076
1077 if (extra < 0)
Sage Weil6b805182009-10-27 11:50:50 -07001078 extra = mdsc->client->mount_args->cap_release_safety;
Sage Weil2f2dc052009-10-06 11:31:09 -07001079
1080 spin_lock(&session->s_cap_lock);
1081
1082 if (!list_empty(&session->s_cap_releases)) {
1083 msg = list_first_entry(&session->s_cap_releases,
1084 struct ceph_msg,
1085 list_head);
1086 head = msg->front.iov_base;
1087 extra += CEPH_CAPS_PER_RELEASE - le32_to_cpu(head->num);
1088 }
1089
1090 while (session->s_num_cap_releases < session->s_nr_caps + extra) {
1091 spin_unlock(&session->s_cap_lock);
Sage Weilbb257662010-04-01 16:07:23 -07001092 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE);
Sage Weil2f2dc052009-10-06 11:31:09 -07001093 if (!msg)
1094 goto out_unlocked;
1095 dout("add_cap_releases %p msg %p now %d\n", session, msg,
1096 (int)msg->front.iov_len);
1097 head = msg->front.iov_base;
1098 head->num = cpu_to_le32(0);
1099 msg->front.iov_len = sizeof(*head);
1100 spin_lock(&session->s_cap_lock);
1101 list_add(&msg->list_head, &session->s_cap_releases);
1102 session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
1103 }
1104
1105 if (!list_empty(&session->s_cap_releases)) {
1106 msg = list_first_entry(&session->s_cap_releases,
1107 struct ceph_msg,
1108 list_head);
1109 head = msg->front.iov_base;
1110 if (head->num) {
1111 dout(" queueing non-full %p (%d)\n", msg,
1112 le32_to_cpu(head->num));
1113 list_move_tail(&msg->list_head,
1114 &session->s_cap_releases_done);
1115 session->s_num_cap_releases -=
1116 CEPH_CAPS_PER_RELEASE - le32_to_cpu(head->num);
1117 }
1118 }
1119 err = 0;
1120 spin_unlock(&session->s_cap_lock);
1121out_unlocked:
1122 return err;
1123}
1124
1125/*
1126 * flush all dirty inode data to disk.
1127 *
1128 * returns true if we've flushed through want_flush_seq
1129 */
1130static int check_cap_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
1131{
1132 int mds, ret = 1;
1133
1134 dout("check_cap_flush want %lld\n", want_flush_seq);
1135 mutex_lock(&mdsc->mutex);
1136 for (mds = 0; ret && mds < mdsc->max_sessions; mds++) {
1137 struct ceph_mds_session *session = mdsc->sessions[mds];
1138
1139 if (!session)
1140 continue;
1141 get_session(session);
1142 mutex_unlock(&mdsc->mutex);
1143
1144 mutex_lock(&session->s_mutex);
1145 if (!list_empty(&session->s_cap_flushing)) {
1146 struct ceph_inode_info *ci =
1147 list_entry(session->s_cap_flushing.next,
1148 struct ceph_inode_info,
1149 i_flushing_item);
1150 struct inode *inode = &ci->vfs_inode;
1151
1152 spin_lock(&inode->i_lock);
1153 if (ci->i_cap_flush_seq <= want_flush_seq) {
1154 dout("check_cap_flush still flushing %p "
1155 "seq %lld <= %lld to mds%d\n", inode,
1156 ci->i_cap_flush_seq, want_flush_seq,
1157 session->s_mds);
1158 ret = 0;
1159 }
1160 spin_unlock(&inode->i_lock);
1161 }
1162 mutex_unlock(&session->s_mutex);
1163 ceph_put_mds_session(session);
1164
1165 if (!ret)
1166 return ret;
1167 mutex_lock(&mdsc->mutex);
1168 }
1169
1170 mutex_unlock(&mdsc->mutex);
1171 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
1172 return ret;
1173}
1174
1175/*
1176 * called under s_mutex
1177 */
1178static void send_cap_releases(struct ceph_mds_client *mdsc,
1179 struct ceph_mds_session *session)
1180{
1181 struct ceph_msg *msg;
1182
1183 dout("send_cap_releases mds%d\n", session->s_mds);
Sage Weil0f8605f2010-05-05 15:51:35 -07001184 spin_lock(&session->s_cap_lock);
1185 while (!list_empty(&session->s_cap_releases_done)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001186 msg = list_first_entry(&session->s_cap_releases_done,
1187 struct ceph_msg, list_head);
1188 list_del_init(&msg->list_head);
1189 spin_unlock(&session->s_cap_lock);
1190 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1191 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1192 ceph_con_send(&session->s_con, msg);
Sage Weil0f8605f2010-05-05 15:51:35 -07001193 spin_lock(&session->s_cap_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001194 }
1195 spin_unlock(&session->s_cap_lock);
1196}
1197
Sage Weile01a5942010-05-10 15:36:44 -07001198static void discard_cap_releases(struct ceph_mds_client *mdsc,
1199 struct ceph_mds_session *session)
1200{
1201 struct ceph_msg *msg;
1202 struct ceph_mds_cap_release *head;
1203 unsigned num;
1204
1205 dout("discard_cap_releases mds%d\n", session->s_mds);
1206 spin_lock(&session->s_cap_lock);
1207
1208 /* zero out the in-progress message */
1209 msg = list_first_entry(&session->s_cap_releases,
1210 struct ceph_msg, list_head);
1211 head = msg->front.iov_base;
1212 num = le32_to_cpu(head->num);
1213 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg, num);
1214 head->num = cpu_to_le32(0);
1215 session->s_num_cap_releases += num;
1216
1217 /* requeue completed messages */
1218 while (!list_empty(&session->s_cap_releases_done)) {
1219 msg = list_first_entry(&session->s_cap_releases_done,
1220 struct ceph_msg, list_head);
1221 list_del_init(&msg->list_head);
1222
1223 head = msg->front.iov_base;
1224 num = le32_to_cpu(head->num);
1225 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg,
1226 num);
1227 session->s_num_cap_releases += num;
1228 head->num = cpu_to_le32(0);
1229 msg->front.iov_len = sizeof(*head);
1230 list_add(&msg->list_head, &session->s_cap_releases);
1231 }
1232
1233 spin_unlock(&session->s_cap_lock);
1234}
1235
Sage Weil2f2dc052009-10-06 11:31:09 -07001236/*
1237 * requests
1238 */
1239
1240/*
1241 * Create an mds request.
1242 */
1243struct ceph_mds_request *
1244ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1245{
1246 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1247
1248 if (!req)
1249 return ERR_PTR(-ENOMEM);
1250
Sage Weilb4556392010-05-13 12:01:13 -07001251 mutex_init(&req->r_fill_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07001252 req->r_started = jiffies;
1253 req->r_resend_mds = -1;
1254 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1255 req->r_fmode = -1;
Sage Weil153c8e62009-12-07 12:31:09 -08001256 kref_init(&req->r_kref);
Sage Weil2f2dc052009-10-06 11:31:09 -07001257 INIT_LIST_HEAD(&req->r_wait);
1258 init_completion(&req->r_completion);
1259 init_completion(&req->r_safe_completion);
1260 INIT_LIST_HEAD(&req->r_unsafe_item);
1261
1262 req->r_op = op;
1263 req->r_direct_mode = mode;
1264 return req;
1265}
1266
1267/*
Sage Weil44ca18f2010-02-15 12:08:46 -08001268 * return oldest (lowest) request, tid in request tree, 0 if none.
Sage Weil2f2dc052009-10-06 11:31:09 -07001269 *
1270 * called under mdsc->mutex.
1271 */
Sage Weil44ca18f2010-02-15 12:08:46 -08001272static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1273{
1274 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1275 return NULL;
1276 return rb_entry(rb_first(&mdsc->request_tree),
1277 struct ceph_mds_request, r_node);
1278}
1279
Sage Weil2f2dc052009-10-06 11:31:09 -07001280static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1281{
Sage Weil44ca18f2010-02-15 12:08:46 -08001282 struct ceph_mds_request *req = __get_oldest_req(mdsc);
1283
1284 if (req)
1285 return req->r_tid;
1286 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001287}
1288
1289/*
1290 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1291 * on build_path_from_dentry in fs/cifs/dir.c.
1292 *
1293 * If @stop_on_nosnap, generate path relative to the first non-snapped
1294 * inode.
1295 *
1296 * Encode hidden .snap dirs as a double /, i.e.
1297 * foo/.snap/bar -> foo//bar
1298 */
1299char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1300 int stop_on_nosnap)
1301{
1302 struct dentry *temp;
1303 char *path;
1304 int len, pos;
1305
1306 if (dentry == NULL)
1307 return ERR_PTR(-EINVAL);
1308
1309retry:
1310 len = 0;
1311 for (temp = dentry; !IS_ROOT(temp);) {
1312 struct inode *inode = temp->d_inode;
1313 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1314 len++; /* slash only */
1315 else if (stop_on_nosnap && inode &&
1316 ceph_snap(inode) == CEPH_NOSNAP)
1317 break;
1318 else
1319 len += 1 + temp->d_name.len;
1320 temp = temp->d_parent;
1321 if (temp == NULL) {
Sage Weil6c99f252010-05-10 16:12:25 -07001322 pr_err("build_path corrupt dentry %p\n", dentry);
Sage Weil2f2dc052009-10-06 11:31:09 -07001323 return ERR_PTR(-EINVAL);
1324 }
1325 }
1326 if (len)
1327 len--; /* no leading '/' */
1328
1329 path = kmalloc(len+1, GFP_NOFS);
1330 if (path == NULL)
1331 return ERR_PTR(-ENOMEM);
1332 pos = len;
1333 path[pos] = 0; /* trailing null */
1334 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
1335 struct inode *inode = temp->d_inode;
1336
1337 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
Sage Weil104648a2010-03-18 10:14:30 -07001338 dout("build_path path+%d: %p SNAPDIR\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001339 pos, temp);
1340 } else if (stop_on_nosnap && inode &&
1341 ceph_snap(inode) == CEPH_NOSNAP) {
1342 break;
1343 } else {
1344 pos -= temp->d_name.len;
1345 if (pos < 0)
1346 break;
1347 strncpy(path + pos, temp->d_name.name,
1348 temp->d_name.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07001349 }
1350 if (pos)
1351 path[--pos] = '/';
1352 temp = temp->d_parent;
1353 if (temp == NULL) {
Sage Weil104648a2010-03-18 10:14:30 -07001354 pr_err("build_path corrupt dentry\n");
Sage Weil2f2dc052009-10-06 11:31:09 -07001355 kfree(path);
1356 return ERR_PTR(-EINVAL);
1357 }
1358 }
1359 if (pos != 0) {
Sage Weil104648a2010-03-18 10:14:30 -07001360 pr_err("build_path did not end path lookup where "
Sage Weil2f2dc052009-10-06 11:31:09 -07001361 "expected, namelen is %d, pos is %d\n", len, pos);
1362 /* presumably this is only possible if racing with a
1363 rename of one of the parent directories (we can not
1364 lock the dentries above us to prevent this, but
1365 retrying should be harmless) */
1366 kfree(path);
1367 goto retry;
1368 }
1369
1370 *base = ceph_ino(temp->d_inode);
1371 *plen = len;
Sage Weil104648a2010-03-18 10:14:30 -07001372 dout("build_path on %p %d built %llx '%.*s'\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001373 dentry, atomic_read(&dentry->d_count), *base, len, path);
1374 return path;
1375}
1376
1377static int build_dentry_path(struct dentry *dentry,
1378 const char **ppath, int *ppathlen, u64 *pino,
1379 int *pfreepath)
1380{
1381 char *path;
1382
1383 if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) {
1384 *pino = ceph_ino(dentry->d_parent->d_inode);
1385 *ppath = dentry->d_name.name;
1386 *ppathlen = dentry->d_name.len;
1387 return 0;
1388 }
1389 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1390 if (IS_ERR(path))
1391 return PTR_ERR(path);
1392 *ppath = path;
1393 *pfreepath = 1;
1394 return 0;
1395}
1396
1397static int build_inode_path(struct inode *inode,
1398 const char **ppath, int *ppathlen, u64 *pino,
1399 int *pfreepath)
1400{
1401 struct dentry *dentry;
1402 char *path;
1403
1404 if (ceph_snap(inode) == CEPH_NOSNAP) {
1405 *pino = ceph_ino(inode);
1406 *ppathlen = 0;
1407 return 0;
1408 }
1409 dentry = d_find_alias(inode);
1410 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1411 dput(dentry);
1412 if (IS_ERR(path))
1413 return PTR_ERR(path);
1414 *ppath = path;
1415 *pfreepath = 1;
1416 return 0;
1417}
1418
1419/*
1420 * request arguments may be specified via an inode *, a dentry *, or
1421 * an explicit ino+path.
1422 */
1423static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1424 const char *rpath, u64 rino,
1425 const char **ppath, int *pathlen,
1426 u64 *ino, int *freepath)
1427{
1428 int r = 0;
1429
1430 if (rinode) {
1431 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1432 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1433 ceph_snap(rinode));
1434 } else if (rdentry) {
1435 r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
1436 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1437 *ppath);
1438 } else if (rpath) {
1439 *ino = rino;
1440 *ppath = rpath;
1441 *pathlen = strlen(rpath);
1442 dout(" path %.*s\n", *pathlen, rpath);
1443 }
1444
1445 return r;
1446}
1447
1448/*
1449 * called under mdsc->mutex
1450 */
1451static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1452 struct ceph_mds_request *req,
1453 int mds)
1454{
1455 struct ceph_msg *msg;
1456 struct ceph_mds_request_head *head;
1457 const char *path1 = NULL;
1458 const char *path2 = NULL;
1459 u64 ino1 = 0, ino2 = 0;
1460 int pathlen1 = 0, pathlen2 = 0;
1461 int freepath1 = 0, freepath2 = 0;
1462 int len;
1463 u16 releases;
1464 void *p, *end;
1465 int ret;
1466
1467 ret = set_request_path_attr(req->r_inode, req->r_dentry,
1468 req->r_path1, req->r_ino1.ino,
1469 &path1, &pathlen1, &ino1, &freepath1);
1470 if (ret < 0) {
1471 msg = ERR_PTR(ret);
1472 goto out;
1473 }
1474
1475 ret = set_request_path_attr(NULL, req->r_old_dentry,
1476 req->r_path2, req->r_ino2.ino,
1477 &path2, &pathlen2, &ino2, &freepath2);
1478 if (ret < 0) {
1479 msg = ERR_PTR(ret);
1480 goto out_free1;
1481 }
1482
1483 len = sizeof(*head) +
Sage Weilac8839d2010-01-27 14:28:10 -08001484 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64));
Sage Weil2f2dc052009-10-06 11:31:09 -07001485
1486 /* calculate (max) length for cap releases */
1487 len += sizeof(struct ceph_mds_request_release) *
1488 (!!req->r_inode_drop + !!req->r_dentry_drop +
1489 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1490 if (req->r_dentry_drop)
1491 len += req->r_dentry->d_name.len;
1492 if (req->r_old_dentry_drop)
1493 len += req->r_old_dentry->d_name.len;
1494
Sage Weilbb257662010-04-01 16:07:23 -07001495 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len);
Sage Weila79832f2010-04-01 16:06:19 -07001496 if (!msg) {
1497 msg = ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -07001498 goto out_free2;
Sage Weila79832f2010-04-01 16:06:19 -07001499 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001500
Sage Weil6df058c2009-12-22 11:24:33 -08001501 msg->hdr.tid = cpu_to_le64(req->r_tid);
1502
Sage Weil2f2dc052009-10-06 11:31:09 -07001503 head = msg->front.iov_base;
1504 p = msg->front.iov_base + sizeof(*head);
1505 end = msg->front.iov_base + msg->front.iov_len;
1506
1507 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1508 head->op = cpu_to_le32(req->r_op);
1509 head->caller_uid = cpu_to_le32(current_fsuid());
1510 head->caller_gid = cpu_to_le32(current_fsgid());
1511 head->args = req->r_args;
1512
1513 ceph_encode_filepath(&p, end, ino1, path1);
1514 ceph_encode_filepath(&p, end, ino2, path2);
1515
1516 /* cap releases */
1517 releases = 0;
1518 if (req->r_inode_drop)
1519 releases += ceph_encode_inode_release(&p,
1520 req->r_inode ? req->r_inode : req->r_dentry->d_inode,
1521 mds, req->r_inode_drop, req->r_inode_unless, 0);
1522 if (req->r_dentry_drop)
1523 releases += ceph_encode_dentry_release(&p, req->r_dentry,
1524 mds, req->r_dentry_drop, req->r_dentry_unless);
1525 if (req->r_old_dentry_drop)
1526 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
1527 mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
1528 if (req->r_old_inode_drop)
1529 releases += ceph_encode_inode_release(&p,
1530 req->r_old_dentry->d_inode,
1531 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
1532 head->num_releases = cpu_to_le16(releases);
1533
1534 BUG_ON(p > end);
1535 msg->front.iov_len = p - msg->front.iov_base;
1536 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1537
1538 msg->pages = req->r_pages;
1539 msg->nr_pages = req->r_num_pages;
1540 msg->hdr.data_len = cpu_to_le32(req->r_data_len);
1541 msg->hdr.data_off = cpu_to_le16(0);
1542
1543out_free2:
1544 if (freepath2)
1545 kfree((char *)path2);
1546out_free1:
1547 if (freepath1)
1548 kfree((char *)path1);
1549out:
1550 return msg;
1551}
1552
1553/*
1554 * called under mdsc->mutex if error, under no mutex if
1555 * success.
1556 */
1557static void complete_request(struct ceph_mds_client *mdsc,
1558 struct ceph_mds_request *req)
1559{
1560 if (req->r_callback)
1561 req->r_callback(mdsc, req);
1562 else
1563 complete(&req->r_completion);
1564}
1565
1566/*
1567 * called under mdsc->mutex
1568 */
1569static int __prepare_send_request(struct ceph_mds_client *mdsc,
1570 struct ceph_mds_request *req,
1571 int mds)
1572{
1573 struct ceph_mds_request_head *rhead;
1574 struct ceph_msg *msg;
1575 int flags = 0;
1576
1577 req->r_mds = mds;
1578 req->r_attempts++;
1579 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
1580 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
1581
1582 if (req->r_request) {
1583 ceph_msg_put(req->r_request);
1584 req->r_request = NULL;
1585 }
1586 msg = create_request_message(mdsc, req, mds);
1587 if (IS_ERR(msg)) {
Sage Weile1518c72010-05-13 11:19:06 -07001588 req->r_err = PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001589 complete_request(mdsc, req);
Sage Weila79832f2010-04-01 16:06:19 -07001590 return PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001591 }
1592 req->r_request = msg;
1593
1594 rhead = msg->front.iov_base;
Sage Weil2f2dc052009-10-06 11:31:09 -07001595 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
1596 if (req->r_got_unsafe)
1597 flags |= CEPH_MDS_FLAG_REPLAY;
1598 if (req->r_locked_dir)
1599 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
1600 rhead->flags = cpu_to_le32(flags);
1601 rhead->num_fwd = req->r_num_fwd;
1602 rhead->num_retry = req->r_attempts - 1;
1603
1604 dout(" r_locked_dir = %p\n", req->r_locked_dir);
1605
1606 if (req->r_target_inode && req->r_got_unsafe)
1607 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
1608 else
1609 rhead->ino = 0;
1610 return 0;
1611}
1612
1613/*
1614 * send request, or put it on the appropriate wait list.
1615 */
1616static int __do_request(struct ceph_mds_client *mdsc,
1617 struct ceph_mds_request *req)
1618{
1619 struct ceph_mds_session *session = NULL;
1620 int mds = -1;
1621 int err = -EAGAIN;
1622
Sage Weile1518c72010-05-13 11:19:06 -07001623 if (req->r_err || req->r_got_result)
Sage Weil2f2dc052009-10-06 11:31:09 -07001624 goto out;
1625
1626 if (req->r_timeout &&
1627 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
1628 dout("do_request timed out\n");
1629 err = -EIO;
1630 goto finish;
1631 }
1632
1633 mds = __choose_mds(mdsc, req);
1634 if (mds < 0 ||
1635 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
1636 dout("do_request no mds or not active, waiting for map\n");
1637 list_add(&req->r_wait, &mdsc->waiting_for_map);
1638 goto out;
1639 }
1640
1641 /* get, open session */
1642 session = __ceph_lookup_mds_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07001643 if (!session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001644 session = register_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07001645 if (IS_ERR(session)) {
1646 err = PTR_ERR(session);
1647 goto finish;
1648 }
1649 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001650 dout("do_request mds%d session %p state %s\n", mds, session,
1651 session_state_name(session->s_state));
1652 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
1653 session->s_state != CEPH_MDS_SESSION_HUNG) {
1654 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1655 session->s_state == CEPH_MDS_SESSION_CLOSING)
1656 __open_session(mdsc, session);
1657 list_add(&req->r_wait, &session->s_waiting);
1658 goto out_session;
1659 }
1660
1661 /* send request */
1662 req->r_session = get_session(session);
1663 req->r_resend_mds = -1; /* forget any previous mds hint */
1664
1665 if (req->r_request_started == 0) /* note request start time */
1666 req->r_request_started = jiffies;
1667
1668 err = __prepare_send_request(mdsc, req, mds);
1669 if (!err) {
1670 ceph_msg_get(req->r_request);
1671 ceph_con_send(&session->s_con, req->r_request);
1672 }
1673
1674out_session:
1675 ceph_put_mds_session(session);
1676out:
1677 return err;
1678
1679finish:
Sage Weile1518c72010-05-13 11:19:06 -07001680 req->r_err = err;
Sage Weil2f2dc052009-10-06 11:31:09 -07001681 complete_request(mdsc, req);
1682 goto out;
1683}
1684
1685/*
1686 * called under mdsc->mutex
1687 */
1688static void __wake_requests(struct ceph_mds_client *mdsc,
1689 struct list_head *head)
1690{
1691 struct ceph_mds_request *req, *nreq;
1692
1693 list_for_each_entry_safe(req, nreq, head, r_wait) {
1694 list_del_init(&req->r_wait);
1695 __do_request(mdsc, req);
1696 }
1697}
1698
1699/*
1700 * Wake up threads with requests pending for @mds, so that they can
Sage Weil29790f22010-03-18 14:45:05 -07001701 * resubmit their requests to a possibly different mds.
Sage Weil2f2dc052009-10-06 11:31:09 -07001702 */
Sage Weil29790f22010-03-18 14:45:05 -07001703static void kick_requests(struct ceph_mds_client *mdsc, int mds)
Sage Weil2f2dc052009-10-06 11:31:09 -07001704{
Sage Weil44ca18f2010-02-15 12:08:46 -08001705 struct ceph_mds_request *req;
1706 struct rb_node *p;
Sage Weil2f2dc052009-10-06 11:31:09 -07001707
1708 dout("kick_requests mds%d\n", mds);
Sage Weil44ca18f2010-02-15 12:08:46 -08001709 for (p = rb_first(&mdsc->request_tree); p; p = rb_next(p)) {
1710 req = rb_entry(p, struct ceph_mds_request, r_node);
1711 if (req->r_got_unsafe)
1712 continue;
1713 if (req->r_session &&
1714 req->r_session->s_mds == mds) {
1715 dout(" kicking tid %llu\n", req->r_tid);
1716 put_request_session(req);
1717 __do_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07001718 }
1719 }
1720}
1721
1722void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
1723 struct ceph_mds_request *req)
1724{
1725 dout("submit_request on %p\n", req);
1726 mutex_lock(&mdsc->mutex);
1727 __register_request(mdsc, req, NULL);
1728 __do_request(mdsc, req);
1729 mutex_unlock(&mdsc->mutex);
1730}
1731
1732/*
1733 * Synchrously perform an mds request. Take care of all of the
1734 * session setup, forwarding, retry details.
1735 */
1736int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
1737 struct inode *dir,
1738 struct ceph_mds_request *req)
1739{
1740 int err;
1741
1742 dout("do_request on %p\n", req);
1743
1744 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
1745 if (req->r_inode)
1746 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
1747 if (req->r_locked_dir)
1748 ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
1749 if (req->r_old_dentry)
1750 ceph_get_cap_refs(
1751 ceph_inode(req->r_old_dentry->d_parent->d_inode),
1752 CEPH_CAP_PIN);
1753
1754 /* issue */
1755 mutex_lock(&mdsc->mutex);
1756 __register_request(mdsc, req, dir);
1757 __do_request(mdsc, req);
1758
Sage Weile1518c72010-05-13 11:19:06 -07001759 if (req->r_err) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001760 err = req->r_err;
Sage Weile1518c72010-05-13 11:19:06 -07001761 __unregister_request(mdsc, req);
1762 dout("do_request early error %d\n", err);
1763 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07001764 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001765
Sage Weile1518c72010-05-13 11:19:06 -07001766 /* wait */
1767 mutex_unlock(&mdsc->mutex);
1768 dout("do_request waiting\n");
1769 if (req->r_timeout) {
1770 err = (long)wait_for_completion_interruptible_timeout(
1771 &req->r_completion, req->r_timeout);
1772 if (err == 0)
1773 err = -EIO;
1774 } else {
1775 err = wait_for_completion_interruptible(&req->r_completion);
1776 }
1777 dout("do_request waited, got %d\n", err);
1778 mutex_lock(&mdsc->mutex);
1779
1780 /* only abort if we didn't race with a real reply */
1781 if (req->r_got_result) {
1782 err = le32_to_cpu(req->r_reply_info.head->result);
1783 } else if (err < 0) {
1784 dout("aborted request %lld with %d\n", req->r_tid, err);
Sage Weilb4556392010-05-13 12:01:13 -07001785
1786 /*
1787 * ensure we aren't running concurrently with
1788 * ceph_fill_trace or ceph_readdir_prepopulate, which
1789 * rely on locks (dir mutex) held by our caller.
1790 */
1791 mutex_lock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07001792 req->r_err = err;
1793 req->r_aborted = true;
Sage Weilb4556392010-05-13 12:01:13 -07001794 mutex_unlock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07001795
1796 if (req->r_locked_dir &&
Sage Weil167c9e32010-05-14 10:02:57 -07001797 (req->r_op & CEPH_MDS_OP_WRITE))
1798 ceph_invalidate_dir_request(req);
Sage Weile1518c72010-05-13 11:19:06 -07001799 } else {
1800 err = req->r_err;
1801 }
1802
1803out:
1804 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07001805 dout("do_request %p done, result %d\n", req, err);
1806 return err;
1807}
1808
1809/*
Sage Weil167c9e32010-05-14 10:02:57 -07001810 * Invalidate dir I_COMPLETE, dentry lease state on an aborted MDS
1811 * namespace request.
1812 */
1813void ceph_invalidate_dir_request(struct ceph_mds_request *req)
1814{
1815 struct inode *inode = req->r_locked_dir;
1816 struct ceph_inode_info *ci = ceph_inode(inode);
1817
1818 dout("invalidate_dir_request %p (I_COMPLETE, lease(s))\n", inode);
1819 spin_lock(&inode->i_lock);
1820 ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
1821 ci->i_release_count++;
1822 spin_unlock(&inode->i_lock);
1823
1824 if (req->r_dentry)
1825 ceph_invalidate_dentry_lease(req->r_dentry);
1826 if (req->r_old_dentry)
1827 ceph_invalidate_dentry_lease(req->r_old_dentry);
1828}
1829
1830/*
Sage Weil2f2dc052009-10-06 11:31:09 -07001831 * Handle mds reply.
1832 *
1833 * We take the session mutex and parse and process the reply immediately.
1834 * This preserves the logical ordering of replies, capabilities, etc., sent
1835 * by the MDS as they are applied to our local cache.
1836 */
1837static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
1838{
1839 struct ceph_mds_client *mdsc = session->s_mdsc;
1840 struct ceph_mds_request *req;
1841 struct ceph_mds_reply_head *head = msg->front.iov_base;
1842 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
1843 u64 tid;
1844 int err, result;
Sage Weil2600d2d2010-02-22 15:12:16 -08001845 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07001846
Sage Weil2f2dc052009-10-06 11:31:09 -07001847 if (msg->front.iov_len < sizeof(*head)) {
1848 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08001849 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001850 return;
1851 }
1852
1853 /* get request, session */
Sage Weil6df058c2009-12-22 11:24:33 -08001854 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07001855 mutex_lock(&mdsc->mutex);
1856 req = __lookup_request(mdsc, tid);
1857 if (!req) {
1858 dout("handle_reply on unknown tid %llu\n", tid);
1859 mutex_unlock(&mdsc->mutex);
1860 return;
1861 }
1862 dout("handle_reply %p\n", req);
Sage Weil2f2dc052009-10-06 11:31:09 -07001863
1864 /* correct session? */
Sage Weild96d6042010-03-20 20:50:58 -07001865 if (req->r_session != session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001866 pr_err("mdsc_handle_reply got %llu on session mds%d"
1867 " not mds%d\n", tid, session->s_mds,
1868 req->r_session ? req->r_session->s_mds : -1);
1869 mutex_unlock(&mdsc->mutex);
1870 goto out;
1871 }
1872
1873 /* dup? */
1874 if ((req->r_got_unsafe && !head->safe) ||
1875 (req->r_got_safe && head->safe)) {
1876 pr_warning("got a dup %s reply on %llu from mds%d\n",
1877 head->safe ? "safe" : "unsafe", tid, mds);
1878 mutex_unlock(&mdsc->mutex);
1879 goto out;
1880 }
Sage Weil85792d0d2010-05-13 09:06:02 -07001881 if (req->r_got_safe && !head->safe) {
1882 pr_warning("got unsafe after safe on %llu from mds%d\n",
1883 tid, mds);
1884 mutex_unlock(&mdsc->mutex);
1885 goto out;
1886 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001887
1888 result = le32_to_cpu(head->result);
1889
1890 /*
1891 * Tolerate 2 consecutive ESTALEs from the same mds.
1892 * FIXME: we should be looking at the cap migrate_seq.
1893 */
1894 if (result == -ESTALE) {
1895 req->r_direct_mode = USE_AUTH_MDS;
1896 req->r_num_stale++;
1897 if (req->r_num_stale <= 2) {
1898 __do_request(mdsc, req);
1899 mutex_unlock(&mdsc->mutex);
1900 goto out;
1901 }
1902 } else {
1903 req->r_num_stale = 0;
1904 }
1905
1906 if (head->safe) {
1907 req->r_got_safe = true;
1908 __unregister_request(mdsc, req);
1909 complete(&req->r_safe_completion);
1910
1911 if (req->r_got_unsafe) {
1912 /*
1913 * We already handled the unsafe response, now do the
1914 * cleanup. No need to examine the response; the MDS
1915 * doesn't include any result info in the safe
1916 * response. And even if it did, there is nothing
1917 * useful we could do with a revised return value.
1918 */
1919 dout("got safe reply %llu, mds%d\n", tid, mds);
1920 list_del_init(&req->r_unsafe_item);
1921
1922 /* last unsafe request during umount? */
Sage Weil44ca18f2010-02-15 12:08:46 -08001923 if (mdsc->stopping && !__get_oldest_req(mdsc))
Sage Weil2f2dc052009-10-06 11:31:09 -07001924 complete(&mdsc->safe_umount_waiters);
1925 mutex_unlock(&mdsc->mutex);
1926 goto out;
1927 }
Sage Weile1518c72010-05-13 11:19:06 -07001928 } else {
Sage Weil2f2dc052009-10-06 11:31:09 -07001929 req->r_got_unsafe = true;
1930 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
1931 }
1932
1933 dout("handle_reply tid %lld result %d\n", tid, result);
1934 rinfo = &req->r_reply_info;
1935 err = parse_reply_info(msg, rinfo);
1936 mutex_unlock(&mdsc->mutex);
1937
1938 mutex_lock(&session->s_mutex);
1939 if (err < 0) {
1940 pr_err("mdsc_handle_reply got corrupt reply mds%d\n", mds);
Sage Weil9ec7cab2009-12-14 15:13:47 -08001941 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001942 goto out_err;
1943 }
1944
1945 /* snap trace */
1946 if (rinfo->snapblob_len) {
1947 down_write(&mdsc->snap_rwsem);
1948 ceph_update_snap_trace(mdsc, rinfo->snapblob,
1949 rinfo->snapblob + rinfo->snapblob_len,
1950 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP);
1951 downgrade_write(&mdsc->snap_rwsem);
1952 } else {
1953 down_read(&mdsc->snap_rwsem);
1954 }
1955
1956 /* insert trace into our cache */
Sage Weilb4556392010-05-13 12:01:13 -07001957 mutex_lock(&req->r_fill_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07001958 err = ceph_fill_trace(mdsc->client->sb, req, req->r_session);
1959 if (err == 0) {
1960 if (result == 0 && rinfo->dir_nr)
1961 ceph_readdir_prepopulate(req, req->r_session);
1962 ceph_unreserve_caps(&req->r_caps_reservation);
1963 }
Sage Weilb4556392010-05-13 12:01:13 -07001964 mutex_unlock(&req->r_fill_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07001965
1966 up_read(&mdsc->snap_rwsem);
1967out_err:
Sage Weile1518c72010-05-13 11:19:06 -07001968 mutex_lock(&mdsc->mutex);
1969 if (!req->r_aborted) {
1970 if (err) {
1971 req->r_err = err;
1972 } else {
1973 req->r_reply = msg;
1974 ceph_msg_get(msg);
1975 req->r_got_result = true;
1976 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001977 } else {
Sage Weile1518c72010-05-13 11:19:06 -07001978 dout("reply arrived after request %lld was aborted\n", tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07001979 }
Sage Weile1518c72010-05-13 11:19:06 -07001980 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07001981
1982 add_cap_releases(mdsc, req->r_session, -1);
1983 mutex_unlock(&session->s_mutex);
1984
1985 /* kick calling process */
1986 complete_request(mdsc, req);
1987out:
1988 ceph_mdsc_put_request(req);
1989 return;
1990}
1991
1992
1993
1994/*
1995 * handle mds notification that our request has been forwarded.
1996 */
Sage Weil2600d2d2010-02-22 15:12:16 -08001997static void handle_forward(struct ceph_mds_client *mdsc,
1998 struct ceph_mds_session *session,
1999 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002000{
2001 struct ceph_mds_request *req;
Sage Weila1ea7872010-02-23 14:02:44 -08002002 u64 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002003 u32 next_mds;
2004 u32 fwd_seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002005 int err = -EINVAL;
2006 void *p = msg->front.iov_base;
2007 void *end = p + msg->front.iov_len;
Sage Weil2f2dc052009-10-06 11:31:09 -07002008
Sage Weila1ea7872010-02-23 14:02:44 -08002009 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002010 next_mds = ceph_decode_32(&p);
2011 fwd_seq = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07002012
2013 mutex_lock(&mdsc->mutex);
2014 req = __lookup_request(mdsc, tid);
2015 if (!req) {
Sage Weil080af172010-02-25 16:40:07 -08002016 dout("forward %llu to mds%d - req dne\n", tid, next_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002017 goto out; /* dup reply? */
2018 }
2019
Sage Weil2f2dc052009-10-06 11:31:09 -07002020 if (fwd_seq <= req->r_num_fwd) {
2021 dout("forward %llu to mds%d - old seq %d <= %d\n",
2022 tid, next_mds, req->r_num_fwd, fwd_seq);
2023 } else {
2024 /* resend. forward race not possible; mds would drop */
2025 dout("forward %llu to mds%d (we resend)\n", tid, next_mds);
2026 req->r_num_fwd = fwd_seq;
2027 req->r_resend_mds = next_mds;
2028 put_request_session(req);
2029 __do_request(mdsc, req);
2030 }
2031 ceph_mdsc_put_request(req);
2032out:
2033 mutex_unlock(&mdsc->mutex);
2034 return;
2035
2036bad:
2037 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2038}
2039
2040/*
2041 * handle a mds session control message
2042 */
2043static void handle_session(struct ceph_mds_session *session,
2044 struct ceph_msg *msg)
2045{
2046 struct ceph_mds_client *mdsc = session->s_mdsc;
2047 u32 op;
2048 u64 seq;
Sage Weil2600d2d2010-02-22 15:12:16 -08002049 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002050 struct ceph_mds_session_head *h = msg->front.iov_base;
2051 int wake = 0;
2052
Sage Weil2f2dc052009-10-06 11:31:09 -07002053 /* decode */
2054 if (msg->front.iov_len != sizeof(*h))
2055 goto bad;
2056 op = le32_to_cpu(h->op);
2057 seq = le64_to_cpu(h->seq);
2058
2059 mutex_lock(&mdsc->mutex);
Sage Weil2600d2d2010-02-22 15:12:16 -08002060 if (op == CEPH_SESSION_CLOSE)
2061 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002062 /* FIXME: this ttl calculation is generous */
2063 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2064 mutex_unlock(&mdsc->mutex);
2065
2066 mutex_lock(&session->s_mutex);
2067
2068 dout("handle_session mds%d %s %p state %s seq %llu\n",
2069 mds, ceph_session_op_name(op), session,
2070 session_state_name(session->s_state), seq);
2071
2072 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2073 session->s_state = CEPH_MDS_SESSION_OPEN;
2074 pr_info("mds%d came back\n", session->s_mds);
2075 }
2076
2077 switch (op) {
2078 case CEPH_SESSION_OPEN:
Sage Weil29790f22010-03-18 14:45:05 -07002079 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2080 pr_info("mds%d reconnect success\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002081 session->s_state = CEPH_MDS_SESSION_OPEN;
2082 renewed_caps(mdsc, session, 0);
2083 wake = 1;
2084 if (mdsc->stopping)
2085 __close_session(mdsc, session);
2086 break;
2087
2088 case CEPH_SESSION_RENEWCAPS:
2089 if (session->s_renew_seq == seq)
2090 renewed_caps(mdsc, session, 1);
2091 break;
2092
2093 case CEPH_SESSION_CLOSE:
Sage Weil29790f22010-03-18 14:45:05 -07002094 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2095 pr_info("mds%d reconnect denied\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002096 remove_session_caps(session);
2097 wake = 1; /* for good measure */
2098 complete(&mdsc->session_close_waiters);
Sage Weil29790f22010-03-18 14:45:05 -07002099 kick_requests(mdsc, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002100 break;
2101
2102 case CEPH_SESSION_STALE:
2103 pr_info("mds%d caps went stale, renewing\n",
2104 session->s_mds);
2105 spin_lock(&session->s_cap_lock);
2106 session->s_cap_gen++;
2107 session->s_cap_ttl = 0;
2108 spin_unlock(&session->s_cap_lock);
2109 send_renew_caps(mdsc, session);
2110 break;
2111
2112 case CEPH_SESSION_RECALL_STATE:
2113 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2114 break;
2115
2116 default:
2117 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2118 WARN_ON(1);
2119 }
2120
2121 mutex_unlock(&session->s_mutex);
2122 if (wake) {
2123 mutex_lock(&mdsc->mutex);
2124 __wake_requests(mdsc, &session->s_waiting);
2125 mutex_unlock(&mdsc->mutex);
2126 }
2127 return;
2128
2129bad:
2130 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2131 (int)msg->front.iov_len);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002132 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002133 return;
2134}
2135
2136
2137/*
2138 * called under session->mutex.
2139 */
2140static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2141 struct ceph_mds_session *session)
2142{
2143 struct ceph_mds_request *req, *nreq;
2144 int err;
2145
2146 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2147
2148 mutex_lock(&mdsc->mutex);
2149 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
2150 err = __prepare_send_request(mdsc, req, session->s_mds);
2151 if (!err) {
2152 ceph_msg_get(req->r_request);
2153 ceph_con_send(&session->s_con, req->r_request);
2154 }
2155 }
2156 mutex_unlock(&mdsc->mutex);
2157}
2158
2159/*
2160 * Encode information about a cap for a reconnect with the MDS.
2161 */
Sage Weil2f2dc052009-10-06 11:31:09 -07002162static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2163 void *arg)
2164{
Sage Weil93cea5b2009-12-23 12:21:51 -08002165 struct ceph_mds_cap_reconnect rec;
Sage Weil2f2dc052009-10-06 11:31:09 -07002166 struct ceph_inode_info *ci;
Sage Weil93cea5b2009-12-23 12:21:51 -08002167 struct ceph_pagelist *pagelist = arg;
Sage Weil2f2dc052009-10-06 11:31:09 -07002168 char *path;
2169 int pathlen, err;
2170 u64 pathbase;
2171 struct dentry *dentry;
2172
2173 ci = cap->ci;
2174
2175 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2176 inode, ceph_vinop(inode), cap, cap->cap_id,
2177 ceph_cap_string(cap->issued));
Sage Weil93cea5b2009-12-23 12:21:51 -08002178 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2179 if (err)
2180 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002181
2182 dentry = d_find_alias(inode);
2183 if (dentry) {
2184 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2185 if (IS_ERR(path)) {
2186 err = PTR_ERR(path);
2187 BUG_ON(err);
2188 }
2189 } else {
2190 path = NULL;
2191 pathlen = 0;
2192 }
Sage Weil93cea5b2009-12-23 12:21:51 -08002193 err = ceph_pagelist_encode_string(pagelist, path, pathlen);
2194 if (err)
2195 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07002196
Sage Weil2f2dc052009-10-06 11:31:09 -07002197 spin_lock(&inode->i_lock);
2198 cap->seq = 0; /* reset cap seq */
2199 cap->issue_seq = 0; /* and issue_seq */
Sage Weil93cea5b2009-12-23 12:21:51 -08002200 rec.cap_id = cpu_to_le64(cap->cap_id);
2201 rec.pathbase = cpu_to_le64(pathbase);
2202 rec.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2203 rec.issued = cpu_to_le32(cap->issued);
2204 rec.size = cpu_to_le64(inode->i_size);
2205 ceph_encode_timespec(&rec.mtime, &inode->i_mtime);
2206 ceph_encode_timespec(&rec.atime, &inode->i_atime);
2207 rec.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
Sage Weil2f2dc052009-10-06 11:31:09 -07002208 spin_unlock(&inode->i_lock);
2209
Sage Weil93cea5b2009-12-23 12:21:51 -08002210 err = ceph_pagelist_append(pagelist, &rec, sizeof(rec));
2211
2212out:
Sage Weil2f2dc052009-10-06 11:31:09 -07002213 kfree(path);
2214 dput(dentry);
Sage Weil93cea5b2009-12-23 12:21:51 -08002215 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002216}
2217
2218
2219/*
2220 * If an MDS fails and recovers, clients need to reconnect in order to
2221 * reestablish shared state. This includes all caps issued through
2222 * this session _and_ the snap_realm hierarchy. Because it's not
2223 * clear which snap realms the mds cares about, we send everything we
2224 * know about.. that ensures we'll then get any new info the
2225 * recovering MDS might have.
2226 *
2227 * This is a relatively heavyweight operation, but it's rare.
2228 *
2229 * called with mdsc->mutex held.
2230 */
Sage Weil34b6c852010-05-10 16:31:25 -07002231static void send_mds_reconnect(struct ceph_mds_client *mdsc,
2232 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07002233{
Sage Weil2f2dc052009-10-06 11:31:09 -07002234 struct ceph_msg *reply;
Sage Weila105f002010-02-15 14:37:55 -08002235 struct rb_node *p;
Sage Weil34b6c852010-05-10 16:31:25 -07002236 int mds = session->s_mds;
Sage Weil9abf82b2010-05-10 21:58:38 -07002237 int err = -ENOMEM;
Sage Weil93cea5b2009-12-23 12:21:51 -08002238 struct ceph_pagelist *pagelist;
Sage Weil2f2dc052009-10-06 11:31:09 -07002239
Sage Weil34b6c852010-05-10 16:31:25 -07002240 pr_info("mds%d reconnect start\n", mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002241
Sage Weil93cea5b2009-12-23 12:21:51 -08002242 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
2243 if (!pagelist)
2244 goto fail_nopagelist;
2245 ceph_pagelist_init(pagelist);
2246
Sage Weilbb257662010-04-01 16:07:23 -07002247 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0);
Sage Weila79832f2010-04-01 16:06:19 -07002248 if (!reply)
Sage Weil93cea5b2009-12-23 12:21:51 -08002249 goto fail_nomsg;
Sage Weil93cea5b2009-12-23 12:21:51 -08002250
Sage Weil34b6c852010-05-10 16:31:25 -07002251 mutex_lock(&session->s_mutex);
2252 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
2253 session->s_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002254
Sage Weil34b6c852010-05-10 16:31:25 -07002255 ceph_con_open(&session->s_con,
2256 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
Sage Weil2f2dc052009-10-06 11:31:09 -07002257
Sage Weil34b6c852010-05-10 16:31:25 -07002258 /* replay unsafe requests */
2259 replay_unsafe_requests(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002260
2261 down_read(&mdsc->snap_rwsem);
2262
Sage Weil2f2dc052009-10-06 11:31:09 -07002263 dout("session %p state %s\n", session,
2264 session_state_name(session->s_state));
2265
Sage Weile01a5942010-05-10 15:36:44 -07002266 /* drop old cap expires; we're about to reestablish that state */
2267 discard_cap_releases(mdsc, session);
2268
Sage Weil2f2dc052009-10-06 11:31:09 -07002269 /* traverse this session's caps */
Sage Weil93cea5b2009-12-23 12:21:51 -08002270 err = ceph_pagelist_encode_32(pagelist, session->s_nr_caps);
2271 if (err)
2272 goto fail;
2273 err = iterate_session_caps(session, encode_caps_cb, pagelist);
Sage Weil2f2dc052009-10-06 11:31:09 -07002274 if (err < 0)
Sage Weil9abf82b2010-05-10 21:58:38 -07002275 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07002276
2277 /*
2278 * snaprealms. we provide mds with the ino, seq (version), and
2279 * parent for all of our realms. If the mds has any newer info,
2280 * it will tell us.
2281 */
Sage Weila105f002010-02-15 14:37:55 -08002282 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
2283 struct ceph_snap_realm *realm =
2284 rb_entry(p, struct ceph_snap_realm, node);
Sage Weil93cea5b2009-12-23 12:21:51 -08002285 struct ceph_mds_snaprealm_reconnect sr_rec;
Sage Weil2f2dc052009-10-06 11:31:09 -07002286
2287 dout(" adding snap realm %llx seq %lld parent %llx\n",
2288 realm->ino, realm->seq, realm->parent_ino);
Sage Weil93cea5b2009-12-23 12:21:51 -08002289 sr_rec.ino = cpu_to_le64(realm->ino);
2290 sr_rec.seq = cpu_to_le64(realm->seq);
2291 sr_rec.parent = cpu_to_le64(realm->parent_ino);
2292 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
2293 if (err)
2294 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07002295 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002296
Sage Weil93cea5b2009-12-23 12:21:51 -08002297 reply->pagelist = pagelist;
2298 reply->hdr.data_len = cpu_to_le32(pagelist->length);
2299 reply->nr_pages = calc_pages_for(0, pagelist->length);
Sage Weil2f2dc052009-10-06 11:31:09 -07002300 ceph_con_send(&session->s_con, reply);
2301
Sage Weil9abf82b2010-05-10 21:58:38 -07002302 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002303
Sage Weil9abf82b2010-05-10 21:58:38 -07002304 mutex_lock(&mdsc->mutex);
2305 __wake_requests(mdsc, &session->s_waiting);
2306 mutex_unlock(&mdsc->mutex);
2307
Sage Weil2f2dc052009-10-06 11:31:09 -07002308 up_read(&mdsc->snap_rwsem);
Sage Weil2f2dc052009-10-06 11:31:09 -07002309 return;
2310
Sage Weil93cea5b2009-12-23 12:21:51 -08002311fail:
Sage Weil2f2dc052009-10-06 11:31:09 -07002312 ceph_msg_put(reply);
Sage Weil9abf82b2010-05-10 21:58:38 -07002313 up_read(&mdsc->snap_rwsem);
2314 mutex_unlock(&session->s_mutex);
Sage Weil93cea5b2009-12-23 12:21:51 -08002315fail_nomsg:
2316 ceph_pagelist_release(pagelist);
2317 kfree(pagelist);
2318fail_nopagelist:
Sage Weil9abf82b2010-05-10 21:58:38 -07002319 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
Sage Weil9abf82b2010-05-10 21:58:38 -07002320 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07002321}
2322
2323
2324/*
2325 * compare old and new mdsmaps, kicking requests
2326 * and closing out old connections as necessary
2327 *
2328 * called under mdsc->mutex.
2329 */
2330static void check_new_map(struct ceph_mds_client *mdsc,
2331 struct ceph_mdsmap *newmap,
2332 struct ceph_mdsmap *oldmap)
2333{
2334 int i;
2335 int oldstate, newstate;
2336 struct ceph_mds_session *s;
2337
2338 dout("check_new_map new %u old %u\n",
2339 newmap->m_epoch, oldmap->m_epoch);
2340
2341 for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
2342 if (mdsc->sessions[i] == NULL)
2343 continue;
2344 s = mdsc->sessions[i];
2345 oldstate = ceph_mdsmap_get_state(oldmap, i);
2346 newstate = ceph_mdsmap_get_state(newmap, i);
2347
2348 dout("check_new_map mds%d state %s -> %s (session %s)\n",
2349 i, ceph_mds_state_name(oldstate),
2350 ceph_mds_state_name(newstate),
2351 session_state_name(s->s_state));
2352
2353 if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
2354 ceph_mdsmap_get_addr(newmap, i),
2355 sizeof(struct ceph_entity_addr))) {
2356 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
2357 /* the session never opened, just close it
2358 * out now */
2359 __wake_requests(mdsc, &s->s_waiting);
Sage Weil2600d2d2010-02-22 15:12:16 -08002360 __unregister_session(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07002361 } else {
2362 /* just close it */
2363 mutex_unlock(&mdsc->mutex);
2364 mutex_lock(&s->s_mutex);
2365 mutex_lock(&mdsc->mutex);
2366 ceph_con_close(&s->s_con);
2367 mutex_unlock(&s->s_mutex);
2368 s->s_state = CEPH_MDS_SESSION_RESTARTING;
2369 }
2370
2371 /* kick any requests waiting on the recovering mds */
Sage Weil29790f22010-03-18 14:45:05 -07002372 kick_requests(mdsc, i);
Sage Weil2f2dc052009-10-06 11:31:09 -07002373 } else if (oldstate == newstate) {
2374 continue; /* nothing new with this mds */
2375 }
2376
2377 /*
2378 * send reconnect?
2379 */
2380 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
Sage Weil34b6c852010-05-10 16:31:25 -07002381 newstate >= CEPH_MDS_STATE_RECONNECT) {
2382 mutex_unlock(&mdsc->mutex);
2383 send_mds_reconnect(mdsc, s);
2384 mutex_lock(&mdsc->mutex);
2385 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002386
2387 /*
Sage Weil29790f22010-03-18 14:45:05 -07002388 * kick request on any mds that has gone active.
Sage Weil2f2dc052009-10-06 11:31:09 -07002389 */
2390 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
2391 newstate >= CEPH_MDS_STATE_ACTIVE) {
Sage Weil29790f22010-03-18 14:45:05 -07002392 if (oldstate != CEPH_MDS_STATE_CREATING &&
2393 oldstate != CEPH_MDS_STATE_STARTING)
2394 pr_info("mds%d recovery completed\n", s->s_mds);
2395 kick_requests(mdsc, i);
Sage Weil2f2dc052009-10-06 11:31:09 -07002396 ceph_kick_flushing_caps(mdsc, s);
Sage Weil0dc25702009-11-20 13:43:45 -08002397 wake_up_session_caps(s, 1);
Sage Weil2f2dc052009-10-06 11:31:09 -07002398 }
2399 }
2400}
2401
2402
2403
2404/*
2405 * leases
2406 */
2407
2408/*
2409 * caller must hold session s_mutex, dentry->d_lock
2410 */
2411void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
2412{
2413 struct ceph_dentry_info *di = ceph_dentry(dentry);
2414
2415 ceph_put_mds_session(di->lease_session);
2416 di->lease_session = NULL;
2417}
2418
Sage Weil2600d2d2010-02-22 15:12:16 -08002419static void handle_lease(struct ceph_mds_client *mdsc,
2420 struct ceph_mds_session *session,
2421 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002422{
2423 struct super_block *sb = mdsc->client->sb;
2424 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07002425 struct ceph_inode_info *ci;
2426 struct dentry *parent, *dentry;
2427 struct ceph_dentry_info *di;
Sage Weil2600d2d2010-02-22 15:12:16 -08002428 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002429 struct ceph_mds_lease *h = msg->front.iov_base;
2430 struct ceph_vino vino;
2431 int mask;
2432 struct qstr dname;
2433 int release = 0;
2434
Sage Weil2f2dc052009-10-06 11:31:09 -07002435 dout("handle_lease from mds%d\n", mds);
2436
2437 /* decode */
2438 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
2439 goto bad;
2440 vino.ino = le64_to_cpu(h->ino);
2441 vino.snap = CEPH_NOSNAP;
2442 mask = le16_to_cpu(h->mask);
2443 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
2444 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
2445 if (dname.len != get_unaligned_le32(h+1))
2446 goto bad;
2447
Sage Weil2f2dc052009-10-06 11:31:09 -07002448 mutex_lock(&session->s_mutex);
2449 session->s_seq++;
2450
2451 /* lookup inode */
2452 inode = ceph_find_inode(sb, vino);
2453 dout("handle_lease '%s', mask %d, ino %llx %p\n",
2454 ceph_lease_op_name(h->action), mask, vino.ino, inode);
2455 if (inode == NULL) {
2456 dout("handle_lease no inode %llx\n", vino.ino);
2457 goto release;
2458 }
2459 ci = ceph_inode(inode);
2460
2461 /* dentry */
2462 parent = d_find_alias(inode);
2463 if (!parent) {
2464 dout("no parent dentry on inode %p\n", inode);
2465 WARN_ON(1);
2466 goto release; /* hrm... */
2467 }
2468 dname.hash = full_name_hash(dname.name, dname.len);
2469 dentry = d_lookup(parent, &dname);
2470 dput(parent);
2471 if (!dentry)
2472 goto release;
2473
2474 spin_lock(&dentry->d_lock);
2475 di = ceph_dentry(dentry);
2476 switch (h->action) {
2477 case CEPH_MDS_LEASE_REVOKE:
2478 if (di && di->lease_session == session) {
2479 h->seq = cpu_to_le32(di->lease_seq);
2480 __ceph_mdsc_drop_dentry_lease(dentry);
2481 }
2482 release = 1;
2483 break;
2484
2485 case CEPH_MDS_LEASE_RENEW:
2486 if (di && di->lease_session == session &&
2487 di->lease_gen == session->s_cap_gen &&
2488 di->lease_renew_from &&
2489 di->lease_renew_after == 0) {
2490 unsigned long duration =
2491 le32_to_cpu(h->duration_ms) * HZ / 1000;
2492
2493 di->lease_seq = le32_to_cpu(h->seq);
2494 dentry->d_time = di->lease_renew_from + duration;
2495 di->lease_renew_after = di->lease_renew_from +
2496 (duration >> 1);
2497 di->lease_renew_from = 0;
2498 }
2499 break;
2500 }
2501 spin_unlock(&dentry->d_lock);
2502 dput(dentry);
2503
2504 if (!release)
2505 goto out;
2506
2507release:
2508 /* let's just reuse the same message */
2509 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
2510 ceph_msg_get(msg);
2511 ceph_con_send(&session->s_con, msg);
2512
2513out:
2514 iput(inode);
2515 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002516 return;
2517
2518bad:
2519 pr_err("corrupt lease message\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002520 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002521}
2522
2523void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
2524 struct inode *inode,
2525 struct dentry *dentry, char action,
2526 u32 seq)
2527{
2528 struct ceph_msg *msg;
2529 struct ceph_mds_lease *lease;
2530 int len = sizeof(*lease) + sizeof(u32);
2531 int dnamelen = 0;
2532
2533 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
2534 inode, dentry, ceph_lease_op_name(action), session->s_mds);
2535 dnamelen = dentry->d_name.len;
2536 len += dnamelen;
2537
Sage Weilbb257662010-04-01 16:07:23 -07002538 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len);
Sage Weila79832f2010-04-01 16:06:19 -07002539 if (!msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002540 return;
2541 lease = msg->front.iov_base;
2542 lease->action = action;
2543 lease->mask = cpu_to_le16(CEPH_LOCK_DN);
2544 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
2545 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
2546 lease->seq = cpu_to_le32(seq);
2547 put_unaligned_le32(dnamelen, lease + 1);
2548 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
2549
2550 /*
2551 * if this is a preemptive lease RELEASE, no need to
2552 * flush request stream, since the actual request will
2553 * soon follow.
2554 */
2555 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
2556
2557 ceph_con_send(&session->s_con, msg);
2558}
2559
2560/*
2561 * Preemptively release a lease we expect to invalidate anyway.
2562 * Pass @inode always, @dentry is optional.
2563 */
2564void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
2565 struct dentry *dentry, int mask)
2566{
2567 struct ceph_dentry_info *di;
2568 struct ceph_mds_session *session;
2569 u32 seq;
2570
2571 BUG_ON(inode == NULL);
2572 BUG_ON(dentry == NULL);
2573 BUG_ON(mask != CEPH_LOCK_DN);
2574
2575 /* is dentry lease valid? */
2576 spin_lock(&dentry->d_lock);
2577 di = ceph_dentry(dentry);
2578 if (!di || !di->lease_session ||
2579 di->lease_session->s_mds < 0 ||
2580 di->lease_gen != di->lease_session->s_cap_gen ||
2581 !time_before(jiffies, dentry->d_time)) {
2582 dout("lease_release inode %p dentry %p -- "
2583 "no lease on %d\n",
2584 inode, dentry, mask);
2585 spin_unlock(&dentry->d_lock);
2586 return;
2587 }
2588
2589 /* we do have a lease on this dentry; note mds and seq */
2590 session = ceph_get_mds_session(di->lease_session);
2591 seq = di->lease_seq;
2592 __ceph_mdsc_drop_dentry_lease(dentry);
2593 spin_unlock(&dentry->d_lock);
2594
2595 dout("lease_release inode %p dentry %p mask %d to mds%d\n",
2596 inode, dentry, mask, session->s_mds);
2597 ceph_mdsc_lease_send_msg(session, inode, dentry,
2598 CEPH_MDS_LEASE_RELEASE, seq);
2599 ceph_put_mds_session(session);
2600}
2601
2602/*
2603 * drop all leases (and dentry refs) in preparation for umount
2604 */
2605static void drop_leases(struct ceph_mds_client *mdsc)
2606{
2607 int i;
2608
2609 dout("drop_leases\n");
2610 mutex_lock(&mdsc->mutex);
2611 for (i = 0; i < mdsc->max_sessions; i++) {
2612 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2613 if (!s)
2614 continue;
2615 mutex_unlock(&mdsc->mutex);
2616 mutex_lock(&s->s_mutex);
2617 mutex_unlock(&s->s_mutex);
2618 ceph_put_mds_session(s);
2619 mutex_lock(&mdsc->mutex);
2620 }
2621 mutex_unlock(&mdsc->mutex);
2622}
2623
2624
2625
2626/*
2627 * delayed work -- periodically trim expired leases, renew caps with mds
2628 */
2629static void schedule_delayed(struct ceph_mds_client *mdsc)
2630{
2631 int delay = 5;
2632 unsigned hz = round_jiffies_relative(HZ * delay);
2633 schedule_delayed_work(&mdsc->delayed_work, hz);
2634}
2635
2636static void delayed_work(struct work_struct *work)
2637{
2638 int i;
2639 struct ceph_mds_client *mdsc =
2640 container_of(work, struct ceph_mds_client, delayed_work.work);
2641 int renew_interval;
2642 int renew_caps;
2643
2644 dout("mdsc delayed_work\n");
Sage Weilafcdaea2009-10-14 14:27:38 -07002645 ceph_check_delayed_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07002646
2647 mutex_lock(&mdsc->mutex);
2648 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
2649 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
2650 mdsc->last_renew_caps);
2651 if (renew_caps)
2652 mdsc->last_renew_caps = jiffies;
2653
2654 for (i = 0; i < mdsc->max_sessions; i++) {
2655 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2656 if (s == NULL)
2657 continue;
2658 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
2659 dout("resending session close request for mds%d\n",
2660 s->s_mds);
2661 request_close_session(mdsc, s);
2662 ceph_put_mds_session(s);
2663 continue;
2664 }
2665 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
2666 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
2667 s->s_state = CEPH_MDS_SESSION_HUNG;
2668 pr_info("mds%d hung\n", s->s_mds);
2669 }
2670 }
2671 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
2672 /* this mds is failed or recovering, just wait */
2673 ceph_put_mds_session(s);
2674 continue;
2675 }
2676 mutex_unlock(&mdsc->mutex);
2677
2678 mutex_lock(&s->s_mutex);
2679 if (renew_caps)
2680 send_renew_caps(mdsc, s);
2681 else
2682 ceph_con_keepalive(&s->s_con);
2683 add_cap_releases(mdsc, s, -1);
Sage Weilaab53dd2010-03-17 16:30:21 -07002684 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2685 s->s_state == CEPH_MDS_SESSION_HUNG)
2686 send_cap_releases(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07002687 mutex_unlock(&s->s_mutex);
2688 ceph_put_mds_session(s);
2689
2690 mutex_lock(&mdsc->mutex);
2691 }
2692 mutex_unlock(&mdsc->mutex);
2693
2694 schedule_delayed(mdsc);
2695}
2696
2697
Sage Weil5f44f142009-11-18 14:52:18 -08002698int ceph_mdsc_init(struct ceph_mds_client *mdsc, struct ceph_client *client)
Sage Weil2f2dc052009-10-06 11:31:09 -07002699{
2700 mdsc->client = client;
2701 mutex_init(&mdsc->mutex);
2702 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08002703 if (mdsc->mdsmap == NULL)
2704 return -ENOMEM;
2705
Sage Weil2f2dc052009-10-06 11:31:09 -07002706 init_completion(&mdsc->safe_umount_waiters);
2707 init_completion(&mdsc->session_close_waiters);
2708 INIT_LIST_HEAD(&mdsc->waiting_for_map);
2709 mdsc->sessions = NULL;
2710 mdsc->max_sessions = 0;
2711 mdsc->stopping = 0;
2712 init_rwsem(&mdsc->snap_rwsem);
Sage Weila105f002010-02-15 14:37:55 -08002713 mdsc->snap_realms = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07002714 INIT_LIST_HEAD(&mdsc->snap_empty);
2715 spin_lock_init(&mdsc->snap_empty_lock);
2716 mdsc->last_tid = 0;
Sage Weil44ca18f2010-02-15 12:08:46 -08002717 mdsc->request_tree = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07002718 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
2719 mdsc->last_renew_caps = jiffies;
2720 INIT_LIST_HEAD(&mdsc->cap_delay_list);
2721 spin_lock_init(&mdsc->cap_delay_lock);
2722 INIT_LIST_HEAD(&mdsc->snap_flush_list);
2723 spin_lock_init(&mdsc->snap_flush_lock);
2724 mdsc->cap_flush_seq = 0;
2725 INIT_LIST_HEAD(&mdsc->cap_dirty);
2726 mdsc->num_cap_flushing = 0;
2727 spin_lock_init(&mdsc->cap_dirty_lock);
2728 init_waitqueue_head(&mdsc->cap_flushing_wq);
2729 spin_lock_init(&mdsc->dentry_lru_lock);
2730 INIT_LIST_HEAD(&mdsc->dentry_lru);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08002731
Sage Weil5f44f142009-11-18 14:52:18 -08002732 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002733}
2734
2735/*
2736 * Wait for safe replies on open mds requests. If we time out, drop
2737 * all requests from the tree to avoid dangling dentry refs.
2738 */
2739static void wait_requests(struct ceph_mds_client *mdsc)
2740{
2741 struct ceph_mds_request *req;
2742 struct ceph_client *client = mdsc->client;
2743
2744 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08002745 if (__get_oldest_req(mdsc)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002746 mutex_unlock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08002747
Sage Weil2f2dc052009-10-06 11:31:09 -07002748 dout("wait_requests waiting for requests\n");
2749 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
Sage Weil6b805182009-10-27 11:50:50 -07002750 client->mount_args->mount_timeout * HZ);
Sage Weil2f2dc052009-10-06 11:31:09 -07002751
2752 /* tear down remaining requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08002753 mutex_lock(&mdsc->mutex);
2754 while ((req = __get_oldest_req(mdsc))) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002755 dout("wait_requests timed out on tid %llu\n",
2756 req->r_tid);
Sage Weil44ca18f2010-02-15 12:08:46 -08002757 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002758 }
2759 }
2760 mutex_unlock(&mdsc->mutex);
2761 dout("wait_requests done\n");
2762}
2763
2764/*
2765 * called before mount is ro, and before dentries are torn down.
2766 * (hmm, does this still race with new lookups?)
2767 */
2768void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
2769{
2770 dout("pre_umount\n");
2771 mdsc->stopping = 1;
2772
2773 drop_leases(mdsc);
Sage Weilafcdaea2009-10-14 14:27:38 -07002774 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07002775 wait_requests(mdsc);
2776}
2777
2778/*
2779 * wait for all write mds requests to flush.
2780 */
2781static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
2782{
Sage Weil80fc7312010-03-16 15:28:54 -07002783 struct ceph_mds_request *req = NULL, *nextreq;
Sage Weil44ca18f2010-02-15 12:08:46 -08002784 struct rb_node *n;
Sage Weil2f2dc052009-10-06 11:31:09 -07002785
2786 mutex_lock(&mdsc->mutex);
2787 dout("wait_unsafe_requests want %lld\n", want_tid);
Sage Weil80fc7312010-03-16 15:28:54 -07002788restart:
Sage Weil44ca18f2010-02-15 12:08:46 -08002789 req = __get_oldest_req(mdsc);
2790 while (req && req->r_tid <= want_tid) {
Sage Weil80fc7312010-03-16 15:28:54 -07002791 /* find next request */
2792 n = rb_next(&req->r_node);
2793 if (n)
2794 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
2795 else
2796 nextreq = NULL;
Sage Weil44ca18f2010-02-15 12:08:46 -08002797 if ((req->r_op & CEPH_MDS_OP_WRITE)) {
2798 /* write op */
2799 ceph_mdsc_get_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07002800 if (nextreq)
2801 ceph_mdsc_get_request(nextreq);
Sage Weil44ca18f2010-02-15 12:08:46 -08002802 mutex_unlock(&mdsc->mutex);
2803 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
2804 req->r_tid, want_tid);
2805 wait_for_completion(&req->r_safe_completion);
2806 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08002807 ceph_mdsc_put_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07002808 if (!nextreq)
2809 break; /* next dne before, so we're done! */
2810 if (RB_EMPTY_NODE(&nextreq->r_node)) {
2811 /* next request was removed from tree */
2812 ceph_mdsc_put_request(nextreq);
2813 goto restart;
2814 }
2815 ceph_mdsc_put_request(nextreq); /* won't go away */
Sage Weil44ca18f2010-02-15 12:08:46 -08002816 }
Sage Weil80fc7312010-03-16 15:28:54 -07002817 req = nextreq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002818 }
2819 mutex_unlock(&mdsc->mutex);
2820 dout("wait_unsafe_requests done\n");
2821}
2822
2823void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
2824{
2825 u64 want_tid, want_flush;
2826
Sage Weil56b7cf92010-05-03 15:22:00 -07002827 if (mdsc->client->mount_state == CEPH_MOUNT_SHUTDOWN)
2828 return;
2829
Sage Weil2f2dc052009-10-06 11:31:09 -07002830 dout("sync\n");
2831 mutex_lock(&mdsc->mutex);
2832 want_tid = mdsc->last_tid;
2833 want_flush = mdsc->cap_flush_seq;
2834 mutex_unlock(&mdsc->mutex);
2835 dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
2836
Sage Weilafcdaea2009-10-14 14:27:38 -07002837 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07002838
2839 wait_unsafe_requests(mdsc, want_tid);
2840 wait_event(mdsc->cap_flushing_wq, check_cap_flush(mdsc, want_flush));
2841}
2842
2843
2844/*
2845 * called after sb is ro.
2846 */
2847void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
2848{
2849 struct ceph_mds_session *session;
2850 int i;
2851 int n;
2852 struct ceph_client *client = mdsc->client;
Sage Weil6b805182009-10-27 11:50:50 -07002853 unsigned long started, timeout = client->mount_args->mount_timeout * HZ;
Sage Weil2f2dc052009-10-06 11:31:09 -07002854
2855 dout("close_sessions\n");
2856
2857 mutex_lock(&mdsc->mutex);
2858
2859 /* close sessions */
2860 started = jiffies;
2861 while (time_before(jiffies, started + timeout)) {
2862 dout("closing sessions\n");
2863 n = 0;
2864 for (i = 0; i < mdsc->max_sessions; i++) {
2865 session = __ceph_lookup_mds_session(mdsc, i);
2866 if (!session)
2867 continue;
2868 mutex_unlock(&mdsc->mutex);
2869 mutex_lock(&session->s_mutex);
2870 __close_session(mdsc, session);
2871 mutex_unlock(&session->s_mutex);
2872 ceph_put_mds_session(session);
2873 mutex_lock(&mdsc->mutex);
2874 n++;
2875 }
2876 if (n == 0)
2877 break;
2878
2879 if (client->mount_state == CEPH_MOUNT_SHUTDOWN)
2880 break;
2881
2882 dout("waiting for sessions to close\n");
2883 mutex_unlock(&mdsc->mutex);
2884 wait_for_completion_timeout(&mdsc->session_close_waiters,
2885 timeout);
2886 mutex_lock(&mdsc->mutex);
2887 }
2888
2889 /* tear down remaining sessions */
2890 for (i = 0; i < mdsc->max_sessions; i++) {
2891 if (mdsc->sessions[i]) {
2892 session = get_session(mdsc->sessions[i]);
Sage Weil2600d2d2010-02-22 15:12:16 -08002893 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002894 mutex_unlock(&mdsc->mutex);
2895 mutex_lock(&session->s_mutex);
2896 remove_session_caps(session);
2897 mutex_unlock(&session->s_mutex);
2898 ceph_put_mds_session(session);
2899 mutex_lock(&mdsc->mutex);
2900 }
2901 }
2902
2903 WARN_ON(!list_empty(&mdsc->cap_delay_list));
2904
2905 mutex_unlock(&mdsc->mutex);
2906
2907 ceph_cleanup_empty_realms(mdsc);
2908
2909 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
2910
2911 dout("stopped\n");
2912}
2913
2914void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
2915{
2916 dout("stop\n");
2917 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
2918 if (mdsc->mdsmap)
2919 ceph_mdsmap_destroy(mdsc->mdsmap);
2920 kfree(mdsc->sessions);
2921}
2922
2923
2924/*
2925 * handle mds map update.
2926 */
2927void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
2928{
2929 u32 epoch;
2930 u32 maplen;
2931 void *p = msg->front.iov_base;
2932 void *end = p + msg->front.iov_len;
2933 struct ceph_mdsmap *newmap, *oldmap;
2934 struct ceph_fsid fsid;
2935 int err = -EINVAL;
2936
2937 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
2938 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08002939 if (ceph_check_fsid(mdsc->client, &fsid) < 0)
2940 return;
Sage Weilc89136e2009-10-14 09:59:09 -07002941 epoch = ceph_decode_32(&p);
2942 maplen = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07002943 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
2944
2945 /* do we need it? */
2946 ceph_monc_got_mdsmap(&mdsc->client->monc, epoch);
2947 mutex_lock(&mdsc->mutex);
2948 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
2949 dout("handle_map epoch %u <= our %u\n",
2950 epoch, mdsc->mdsmap->m_epoch);
2951 mutex_unlock(&mdsc->mutex);
2952 return;
2953 }
2954
2955 newmap = ceph_mdsmap_decode(&p, end);
2956 if (IS_ERR(newmap)) {
2957 err = PTR_ERR(newmap);
2958 goto bad_unlock;
2959 }
2960
2961 /* swap into place */
2962 if (mdsc->mdsmap) {
2963 oldmap = mdsc->mdsmap;
2964 mdsc->mdsmap = newmap;
2965 check_new_map(mdsc, newmap, oldmap);
2966 ceph_mdsmap_destroy(oldmap);
2967 } else {
2968 mdsc->mdsmap = newmap; /* first mds map */
2969 }
2970 mdsc->client->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
2971
2972 __wake_requests(mdsc, &mdsc->waiting_for_map);
2973
2974 mutex_unlock(&mdsc->mutex);
2975 schedule_delayed(mdsc);
2976 return;
2977
2978bad_unlock:
2979 mutex_unlock(&mdsc->mutex);
2980bad:
2981 pr_err("error decoding mdsmap %d\n", err);
2982 return;
2983}
2984
2985static struct ceph_connection *con_get(struct ceph_connection *con)
2986{
2987 struct ceph_mds_session *s = con->private;
2988
2989 if (get_session(s)) {
Sage Weil2600d2d2010-02-22 15:12:16 -08002990 dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -07002991 return con;
2992 }
2993 dout("mdsc con_get %p FAIL\n", s);
2994 return NULL;
2995}
2996
2997static void con_put(struct ceph_connection *con)
2998{
2999 struct ceph_mds_session *s = con->private;
3000
Sage Weil2f2dc052009-10-06 11:31:09 -07003001 ceph_put_mds_session(s);
Sage Weil2600d2d2010-02-22 15:12:16 -08003002 dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -07003003}
3004
3005/*
3006 * if the client is unresponsive for long enough, the mds will kill
3007 * the session entirely.
3008 */
3009static void peer_reset(struct ceph_connection *con)
3010{
3011 struct ceph_mds_session *s = con->private;
Sage Weil7e70f0e2010-03-18 13:59:12 -07003012 struct ceph_mds_client *mdsc = s->s_mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07003013
Sage Weil7e70f0e2010-03-18 13:59:12 -07003014 pr_warning("mds%d closed our session\n", s->s_mds);
3015 send_mds_reconnect(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07003016}
3017
3018static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
3019{
3020 struct ceph_mds_session *s = con->private;
3021 struct ceph_mds_client *mdsc = s->s_mdsc;
3022 int type = le16_to_cpu(msg->hdr.type);
3023
Sage Weil2600d2d2010-02-22 15:12:16 -08003024 mutex_lock(&mdsc->mutex);
3025 if (__verify_registered_session(mdsc, s) < 0) {
3026 mutex_unlock(&mdsc->mutex);
3027 goto out;
3028 }
3029 mutex_unlock(&mdsc->mutex);
3030
Sage Weil2f2dc052009-10-06 11:31:09 -07003031 switch (type) {
3032 case CEPH_MSG_MDS_MAP:
3033 ceph_mdsc_handle_map(mdsc, msg);
3034 break;
3035 case CEPH_MSG_CLIENT_SESSION:
3036 handle_session(s, msg);
3037 break;
3038 case CEPH_MSG_CLIENT_REPLY:
3039 handle_reply(s, msg);
3040 break;
3041 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
Sage Weil2600d2d2010-02-22 15:12:16 -08003042 handle_forward(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003043 break;
3044 case CEPH_MSG_CLIENT_CAPS:
3045 ceph_handle_caps(s, msg);
3046 break;
3047 case CEPH_MSG_CLIENT_SNAP:
Sage Weil2600d2d2010-02-22 15:12:16 -08003048 ceph_handle_snap(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003049 break;
3050 case CEPH_MSG_CLIENT_LEASE:
Sage Weil2600d2d2010-02-22 15:12:16 -08003051 handle_lease(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003052 break;
3053
3054 default:
3055 pr_err("received unknown message type %d %s\n", type,
3056 ceph_msg_type_name(type));
3057 }
Sage Weil2600d2d2010-02-22 15:12:16 -08003058out:
Sage Weil2f2dc052009-10-06 11:31:09 -07003059 ceph_msg_put(msg);
3060}
3061
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003062/*
3063 * authentication
3064 */
3065static int get_authorizer(struct ceph_connection *con,
3066 void **buf, int *len, int *proto,
3067 void **reply_buf, int *reply_len, int force_new)
3068{
3069 struct ceph_mds_session *s = con->private;
3070 struct ceph_mds_client *mdsc = s->s_mdsc;
3071 struct ceph_auth_client *ac = mdsc->client->monc.auth;
3072 int ret = 0;
3073
3074 if (force_new && s->s_authorizer) {
3075 ac->ops->destroy_authorizer(ac, s->s_authorizer);
3076 s->s_authorizer = NULL;
3077 }
3078 if (s->s_authorizer == NULL) {
3079 if (ac->ops->create_authorizer) {
3080 ret = ac->ops->create_authorizer(
3081 ac, CEPH_ENTITY_TYPE_MDS,
3082 &s->s_authorizer,
3083 &s->s_authorizer_buf,
3084 &s->s_authorizer_buf_len,
3085 &s->s_authorizer_reply_buf,
3086 &s->s_authorizer_reply_buf_len);
3087 if (ret)
3088 return ret;
3089 }
3090 }
3091
3092 *proto = ac->protocol;
3093 *buf = s->s_authorizer_buf;
3094 *len = s->s_authorizer_buf_len;
3095 *reply_buf = s->s_authorizer_reply_buf;
3096 *reply_len = s->s_authorizer_reply_buf_len;
3097 return 0;
3098}
3099
3100
3101static int verify_authorizer_reply(struct ceph_connection *con, int len)
3102{
3103 struct ceph_mds_session *s = con->private;
3104 struct ceph_mds_client *mdsc = s->s_mdsc;
3105 struct ceph_auth_client *ac = mdsc->client->monc.auth;
3106
3107 return ac->ops->verify_authorizer_reply(ac, s->s_authorizer, len);
3108}
3109
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003110static int invalidate_authorizer(struct ceph_connection *con)
3111{
3112 struct ceph_mds_session *s = con->private;
3113 struct ceph_mds_client *mdsc = s->s_mdsc;
3114 struct ceph_auth_client *ac = mdsc->client->monc.auth;
3115
3116 if (ac->ops->invalidate_authorizer)
3117 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
3118
3119 return ceph_monc_validate_auth(&mdsc->client->monc);
3120}
3121
Sage Weil2f2dc052009-10-06 11:31:09 -07003122const static struct ceph_connection_operations mds_con_ops = {
3123 .get = con_get,
3124 .put = con_put,
3125 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003126 .get_authorizer = get_authorizer,
3127 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003128 .invalidate_authorizer = invalidate_authorizer,
Sage Weil2f2dc052009-10-06 11:31:09 -07003129 .peer_reset = peer_reset,
Sage Weil2f2dc052009-10-06 11:31:09 -07003130};
3131
3132
3133
3134
3135/* eof */