blob: c0568fe3c0badb6874b27a0b6aba2deab9219702 [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
668 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), 0, 0, NULL);
669 if (IS_ERR(msg)) {
670 pr_err("create_session_msg ENOMEM creating msg\n");
671 return ERR_PTR(PTR_ERR(msg));
672 }
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;
690 int err = 0;
691
692 /* wait for mds to go active? */
693 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
694 dout("open_session to mds%d (%s)\n", mds,
695 ceph_mds_state_name(mstate));
696 session->s_state = CEPH_MDS_SESSION_OPENING;
697 session->s_renew_requested = jiffies;
698
699 /* send connect message */
700 msg = create_session_msg(CEPH_SESSION_REQUEST_OPEN, session->s_seq);
701 if (IS_ERR(msg)) {
702 err = PTR_ERR(msg);
703 goto out;
704 }
705 ceph_con_send(&session->s_con, msg);
706
707out:
708 return 0;
709}
710
711/*
712 * session caps
713 */
714
715/*
716 * Free preallocated cap messages assigned to this session
717 */
718static void cleanup_cap_releases(struct ceph_mds_session *session)
719{
720 struct ceph_msg *msg;
721
722 spin_lock(&session->s_cap_lock);
723 while (!list_empty(&session->s_cap_releases)) {
724 msg = list_first_entry(&session->s_cap_releases,
725 struct ceph_msg, list_head);
726 list_del_init(&msg->list_head);
727 ceph_msg_put(msg);
728 }
729 while (!list_empty(&session->s_cap_releases_done)) {
730 msg = list_first_entry(&session->s_cap_releases_done,
731 struct ceph_msg, list_head);
732 list_del_init(&msg->list_head);
733 ceph_msg_put(msg);
734 }
735 spin_unlock(&session->s_cap_lock);
736}
737
738/*
Sage Weilf818a732010-05-11 20:56:31 -0700739 * Helper to safely iterate over all caps associated with a session, with
740 * special care taken to handle a racing __ceph_remove_cap().
Sage Weil2f2dc052009-10-06 11:31:09 -0700741 *
Sage Weilf818a732010-05-11 20:56:31 -0700742 * Caller must hold session s_mutex.
Sage Weil2f2dc052009-10-06 11:31:09 -0700743 */
744static int iterate_session_caps(struct ceph_mds_session *session,
745 int (*cb)(struct inode *, struct ceph_cap *,
746 void *), void *arg)
747{
Sage Weil7c1332b2010-02-16 11:39:45 -0800748 struct list_head *p;
749 struct ceph_cap *cap;
750 struct inode *inode, *last_inode = NULL;
751 struct ceph_cap *old_cap = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700752 int ret;
753
754 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
755 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800756 p = session->s_caps.next;
757 while (p != &session->s_caps) {
758 cap = list_entry(p, struct ceph_cap, session_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -0700759 inode = igrab(&cap->ci->vfs_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -0800760 if (!inode) {
761 p = p->next;
Sage Weil2f2dc052009-10-06 11:31:09 -0700762 continue;
Sage Weil7c1332b2010-02-16 11:39:45 -0800763 }
764 session->s_cap_iterator = cap;
Sage Weil2f2dc052009-10-06 11:31:09 -0700765 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800766
767 if (last_inode) {
768 iput(last_inode);
769 last_inode = NULL;
770 }
771 if (old_cap) {
772 ceph_put_cap(old_cap);
773 old_cap = NULL;
774 }
775
Sage Weil2f2dc052009-10-06 11:31:09 -0700776 ret = cb(inode, cap, arg);
Sage Weil7c1332b2010-02-16 11:39:45 -0800777 last_inode = inode;
778
Sage Weil2f2dc052009-10-06 11:31:09 -0700779 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800780 p = p->next;
781 if (cap->ci == NULL) {
782 dout("iterate_session_caps finishing cap %p removal\n",
783 cap);
784 BUG_ON(cap->session != session);
785 list_del_init(&cap->session_caps);
786 session->s_nr_caps--;
787 cap->session = NULL;
788 old_cap = cap; /* put_cap it w/o locks held */
789 }
Sage Weil5dacf092009-12-21 20:40:34 -0800790 if (ret < 0)
791 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -0700792 }
Sage Weil5dacf092009-12-21 20:40:34 -0800793 ret = 0;
794out:
Sage Weil7c1332b2010-02-16 11:39:45 -0800795 session->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700796 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800797
798 if (last_inode)
799 iput(last_inode);
800 if (old_cap)
801 ceph_put_cap(old_cap);
802
Sage Weil5dacf092009-12-21 20:40:34 -0800803 return ret;
Sage Weil2f2dc052009-10-06 11:31:09 -0700804}
805
806static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
807 void *arg)
808{
809 struct ceph_inode_info *ci = ceph_inode(inode);
810 dout("removing cap %p, ci is %p, inode is %p\n",
811 cap, ci, &ci->vfs_inode);
812 ceph_remove_cap(cap);
813 return 0;
814}
815
816/*
817 * caller must hold session s_mutex
818 */
819static void remove_session_caps(struct ceph_mds_session *session)
820{
821 dout("remove_session_caps on %p\n", session);
822 iterate_session_caps(session, remove_session_caps_cb, NULL);
823 BUG_ON(session->s_nr_caps > 0);
824 cleanup_cap_releases(session);
825}
826
827/*
828 * wake up any threads waiting on this session's caps. if the cap is
829 * old (didn't get renewed on the client reconnect), remove it now.
830 *
831 * caller must hold s_mutex.
832 */
833static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
834 void *arg)
835{
Sage Weil0dc25702009-11-20 13:43:45 -0800836 struct ceph_inode_info *ci = ceph_inode(inode);
837
838 wake_up(&ci->i_cap_wq);
839 if (arg) {
840 spin_lock(&inode->i_lock);
841 ci->i_wanted_max_size = 0;
842 ci->i_requested_max_size = 0;
843 spin_unlock(&inode->i_lock);
844 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700845 return 0;
846}
847
Sage Weil0dc25702009-11-20 13:43:45 -0800848static void wake_up_session_caps(struct ceph_mds_session *session,
849 int reconnect)
Sage Weil2f2dc052009-10-06 11:31:09 -0700850{
851 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
Sage Weil0dc25702009-11-20 13:43:45 -0800852 iterate_session_caps(session, wake_up_session_cb,
853 (void *)(unsigned long)reconnect);
Sage Weil2f2dc052009-10-06 11:31:09 -0700854}
855
856/*
857 * Send periodic message to MDS renewing all currently held caps. The
858 * ack will reset the expiration for all caps from this session.
859 *
860 * caller holds s_mutex
861 */
862static int send_renew_caps(struct ceph_mds_client *mdsc,
863 struct ceph_mds_session *session)
864{
865 struct ceph_msg *msg;
866 int state;
867
868 if (time_after_eq(jiffies, session->s_cap_ttl) &&
869 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
870 pr_info("mds%d caps stale\n", session->s_mds);
Sage Weile4cb4cb2010-03-18 13:43:09 -0700871 session->s_renew_requested = jiffies;
Sage Weil2f2dc052009-10-06 11:31:09 -0700872
873 /* do not try to renew caps until a recovering mds has reconnected
874 * with its clients. */
875 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
876 if (state < CEPH_MDS_STATE_RECONNECT) {
877 dout("send_renew_caps ignoring mds%d (%s)\n",
878 session->s_mds, ceph_mds_state_name(state));
879 return 0;
880 }
881
882 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
883 ceph_mds_state_name(state));
Sage Weil2f2dc052009-10-06 11:31:09 -0700884 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
885 ++session->s_renew_seq);
886 if (IS_ERR(msg))
887 return PTR_ERR(msg);
888 ceph_con_send(&session->s_con, msg);
889 return 0;
890}
891
892/*
893 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
Sage Weil0dc25702009-11-20 13:43:45 -0800894 *
895 * Called under session->s_mutex
Sage Weil2f2dc052009-10-06 11:31:09 -0700896 */
897static void renewed_caps(struct ceph_mds_client *mdsc,
898 struct ceph_mds_session *session, int is_renew)
899{
900 int was_stale;
901 int wake = 0;
902
903 spin_lock(&session->s_cap_lock);
904 was_stale = is_renew && (session->s_cap_ttl == 0 ||
905 time_after_eq(jiffies, session->s_cap_ttl));
906
907 session->s_cap_ttl = session->s_renew_requested +
908 mdsc->mdsmap->m_session_timeout*HZ;
909
910 if (was_stale) {
911 if (time_before(jiffies, session->s_cap_ttl)) {
912 pr_info("mds%d caps renewed\n", session->s_mds);
913 wake = 1;
914 } else {
915 pr_info("mds%d caps still stale\n", session->s_mds);
916 }
917 }
918 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
919 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
920 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
921 spin_unlock(&session->s_cap_lock);
922
923 if (wake)
Sage Weil0dc25702009-11-20 13:43:45 -0800924 wake_up_session_caps(session, 0);
Sage Weil2f2dc052009-10-06 11:31:09 -0700925}
926
927/*
928 * send a session close request
929 */
930static int request_close_session(struct ceph_mds_client *mdsc,
931 struct ceph_mds_session *session)
932{
933 struct ceph_msg *msg;
934 int err = 0;
935
936 dout("request_close_session mds%d state %s seq %lld\n",
937 session->s_mds, session_state_name(session->s_state),
938 session->s_seq);
939 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
940 if (IS_ERR(msg))
941 err = PTR_ERR(msg);
942 else
943 ceph_con_send(&session->s_con, msg);
944 return err;
945}
946
947/*
948 * Called with s_mutex held.
949 */
950static int __close_session(struct ceph_mds_client *mdsc,
951 struct ceph_mds_session *session)
952{
953 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
954 return 0;
955 session->s_state = CEPH_MDS_SESSION_CLOSING;
956 return request_close_session(mdsc, session);
957}
958
959/*
960 * Trim old(er) caps.
961 *
962 * Because we can't cache an inode without one or more caps, we do
963 * this indirectly: if a cap is unused, we prune its aliases, at which
964 * point the inode will hopefully get dropped to.
965 *
966 * Yes, this is a bit sloppy. Our only real goal here is to respond to
967 * memory pressure from the MDS, though, so it needn't be perfect.
968 */
969static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
970{
971 struct ceph_mds_session *session = arg;
972 struct ceph_inode_info *ci = ceph_inode(inode);
973 int used, oissued, mine;
974
975 if (session->s_trim_caps <= 0)
976 return -1;
977
978 spin_lock(&inode->i_lock);
979 mine = cap->issued | cap->implemented;
980 used = __ceph_caps_used(ci);
981 oissued = __ceph_caps_issued_other(ci, cap);
982
983 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n",
984 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
985 ceph_cap_string(used));
986 if (ci->i_dirty_caps)
987 goto out; /* dirty caps */
988 if ((used & ~oissued) & mine)
989 goto out; /* we need these caps */
990
991 session->s_trim_caps--;
992 if (oissued) {
993 /* we aren't the only cap.. just remove us */
Sage Weil7c1332b2010-02-16 11:39:45 -0800994 __ceph_remove_cap(cap);
Sage Weil2f2dc052009-10-06 11:31:09 -0700995 } else {
996 /* try to drop referring dentries */
997 spin_unlock(&inode->i_lock);
998 d_prune_aliases(inode);
999 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1000 inode, cap, atomic_read(&inode->i_count));
1001 return 0;
1002 }
1003
1004out:
1005 spin_unlock(&inode->i_lock);
1006 return 0;
1007}
1008
1009/*
1010 * Trim session cap count down to some max number.
1011 */
1012static int trim_caps(struct ceph_mds_client *mdsc,
1013 struct ceph_mds_session *session,
1014 int max_caps)
1015{
1016 int trim_caps = session->s_nr_caps - max_caps;
1017
1018 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1019 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1020 if (trim_caps > 0) {
1021 session->s_trim_caps = trim_caps;
1022 iterate_session_caps(session, trim_caps_cb, session);
1023 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1024 session->s_mds, session->s_nr_caps, max_caps,
1025 trim_caps - session->s_trim_caps);
Sage Weil5dacf092009-12-21 20:40:34 -08001026 session->s_trim_caps = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001027 }
1028 return 0;
1029}
1030
1031/*
1032 * Allocate cap_release messages. If there is a partially full message
1033 * in the queue, try to allocate enough to cover it's remainder, so that
1034 * we can send it immediately.
1035 *
1036 * Called under s_mutex.
1037 */
1038static int add_cap_releases(struct ceph_mds_client *mdsc,
1039 struct ceph_mds_session *session,
1040 int extra)
1041{
1042 struct ceph_msg *msg;
1043 struct ceph_mds_cap_release *head;
1044 int err = -ENOMEM;
1045
1046 if (extra < 0)
Sage Weil6b805182009-10-27 11:50:50 -07001047 extra = mdsc->client->mount_args->cap_release_safety;
Sage Weil2f2dc052009-10-06 11:31:09 -07001048
1049 spin_lock(&session->s_cap_lock);
1050
1051 if (!list_empty(&session->s_cap_releases)) {
1052 msg = list_first_entry(&session->s_cap_releases,
1053 struct ceph_msg,
1054 list_head);
1055 head = msg->front.iov_base;
1056 extra += CEPH_CAPS_PER_RELEASE - le32_to_cpu(head->num);
1057 }
1058
1059 while (session->s_num_cap_releases < session->s_nr_caps + extra) {
1060 spin_unlock(&session->s_cap_lock);
1061 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE,
1062 0, 0, NULL);
1063 if (!msg)
1064 goto out_unlocked;
1065 dout("add_cap_releases %p msg %p now %d\n", session, msg,
1066 (int)msg->front.iov_len);
1067 head = msg->front.iov_base;
1068 head->num = cpu_to_le32(0);
1069 msg->front.iov_len = sizeof(*head);
1070 spin_lock(&session->s_cap_lock);
1071 list_add(&msg->list_head, &session->s_cap_releases);
1072 session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
1073 }
1074
1075 if (!list_empty(&session->s_cap_releases)) {
1076 msg = list_first_entry(&session->s_cap_releases,
1077 struct ceph_msg,
1078 list_head);
1079 head = msg->front.iov_base;
1080 if (head->num) {
1081 dout(" queueing non-full %p (%d)\n", msg,
1082 le32_to_cpu(head->num));
1083 list_move_tail(&msg->list_head,
1084 &session->s_cap_releases_done);
1085 session->s_num_cap_releases -=
1086 CEPH_CAPS_PER_RELEASE - le32_to_cpu(head->num);
1087 }
1088 }
1089 err = 0;
1090 spin_unlock(&session->s_cap_lock);
1091out_unlocked:
1092 return err;
1093}
1094
1095/*
1096 * flush all dirty inode data to disk.
1097 *
1098 * returns true if we've flushed through want_flush_seq
1099 */
1100static int check_cap_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
1101{
1102 int mds, ret = 1;
1103
1104 dout("check_cap_flush want %lld\n", want_flush_seq);
1105 mutex_lock(&mdsc->mutex);
1106 for (mds = 0; ret && mds < mdsc->max_sessions; mds++) {
1107 struct ceph_mds_session *session = mdsc->sessions[mds];
1108
1109 if (!session)
1110 continue;
1111 get_session(session);
1112 mutex_unlock(&mdsc->mutex);
1113
1114 mutex_lock(&session->s_mutex);
1115 if (!list_empty(&session->s_cap_flushing)) {
1116 struct ceph_inode_info *ci =
1117 list_entry(session->s_cap_flushing.next,
1118 struct ceph_inode_info,
1119 i_flushing_item);
1120 struct inode *inode = &ci->vfs_inode;
1121
1122 spin_lock(&inode->i_lock);
1123 if (ci->i_cap_flush_seq <= want_flush_seq) {
1124 dout("check_cap_flush still flushing %p "
1125 "seq %lld <= %lld to mds%d\n", inode,
1126 ci->i_cap_flush_seq, want_flush_seq,
1127 session->s_mds);
1128 ret = 0;
1129 }
1130 spin_unlock(&inode->i_lock);
1131 }
1132 mutex_unlock(&session->s_mutex);
1133 ceph_put_mds_session(session);
1134
1135 if (!ret)
1136 return ret;
1137 mutex_lock(&mdsc->mutex);
1138 }
1139
1140 mutex_unlock(&mdsc->mutex);
1141 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
1142 return ret;
1143}
1144
1145/*
1146 * called under s_mutex
1147 */
1148static void send_cap_releases(struct ceph_mds_client *mdsc,
1149 struct ceph_mds_session *session)
1150{
1151 struct ceph_msg *msg;
1152
1153 dout("send_cap_releases mds%d\n", session->s_mds);
1154 while (1) {
1155 spin_lock(&session->s_cap_lock);
1156 if (list_empty(&session->s_cap_releases_done))
1157 break;
1158 msg = list_first_entry(&session->s_cap_releases_done,
1159 struct ceph_msg, list_head);
1160 list_del_init(&msg->list_head);
1161 spin_unlock(&session->s_cap_lock);
1162 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1163 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1164 ceph_con_send(&session->s_con, msg);
1165 }
1166 spin_unlock(&session->s_cap_lock);
1167}
1168
1169/*
1170 * requests
1171 */
1172
1173/*
1174 * Create an mds request.
1175 */
1176struct ceph_mds_request *
1177ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1178{
1179 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1180
1181 if (!req)
1182 return ERR_PTR(-ENOMEM);
1183
Sage Weilb4556392010-05-13 12:01:13 -07001184 mutex_init(&req->r_fill_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07001185 req->r_started = jiffies;
1186 req->r_resend_mds = -1;
1187 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1188 req->r_fmode = -1;
Sage Weil153c8e62009-12-07 12:31:09 -08001189 kref_init(&req->r_kref);
Sage Weil2f2dc052009-10-06 11:31:09 -07001190 INIT_LIST_HEAD(&req->r_wait);
1191 init_completion(&req->r_completion);
1192 init_completion(&req->r_safe_completion);
1193 INIT_LIST_HEAD(&req->r_unsafe_item);
1194
1195 req->r_op = op;
1196 req->r_direct_mode = mode;
1197 return req;
1198}
1199
1200/*
Sage Weil44ca18f2010-02-15 12:08:46 -08001201 * return oldest (lowest) request, tid in request tree, 0 if none.
Sage Weil2f2dc052009-10-06 11:31:09 -07001202 *
1203 * called under mdsc->mutex.
1204 */
Sage Weil44ca18f2010-02-15 12:08:46 -08001205static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1206{
1207 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1208 return NULL;
1209 return rb_entry(rb_first(&mdsc->request_tree),
1210 struct ceph_mds_request, r_node);
1211}
1212
Sage Weil2f2dc052009-10-06 11:31:09 -07001213static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1214{
Sage Weil44ca18f2010-02-15 12:08:46 -08001215 struct ceph_mds_request *req = __get_oldest_req(mdsc);
1216
1217 if (req)
1218 return req->r_tid;
1219 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001220}
1221
1222/*
1223 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1224 * on build_path_from_dentry in fs/cifs/dir.c.
1225 *
1226 * If @stop_on_nosnap, generate path relative to the first non-snapped
1227 * inode.
1228 *
1229 * Encode hidden .snap dirs as a double /, i.e.
1230 * foo/.snap/bar -> foo//bar
1231 */
1232char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1233 int stop_on_nosnap)
1234{
1235 struct dentry *temp;
1236 char *path;
1237 int len, pos;
1238
1239 if (dentry == NULL)
1240 return ERR_PTR(-EINVAL);
1241
1242retry:
1243 len = 0;
1244 for (temp = dentry; !IS_ROOT(temp);) {
1245 struct inode *inode = temp->d_inode;
1246 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1247 len++; /* slash only */
1248 else if (stop_on_nosnap && inode &&
1249 ceph_snap(inode) == CEPH_NOSNAP)
1250 break;
1251 else
1252 len += 1 + temp->d_name.len;
1253 temp = temp->d_parent;
1254 if (temp == NULL) {
1255 pr_err("build_path_dentry corrupt dentry %p\n", dentry);
1256 return ERR_PTR(-EINVAL);
1257 }
1258 }
1259 if (len)
1260 len--; /* no leading '/' */
1261
1262 path = kmalloc(len+1, GFP_NOFS);
1263 if (path == NULL)
1264 return ERR_PTR(-ENOMEM);
1265 pos = len;
1266 path[pos] = 0; /* trailing null */
1267 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
1268 struct inode *inode = temp->d_inode;
1269
1270 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
1271 dout("build_path_dentry path+%d: %p SNAPDIR\n",
1272 pos, temp);
1273 } else if (stop_on_nosnap && inode &&
1274 ceph_snap(inode) == CEPH_NOSNAP) {
1275 break;
1276 } else {
1277 pos -= temp->d_name.len;
1278 if (pos < 0)
1279 break;
1280 strncpy(path + pos, temp->d_name.name,
1281 temp->d_name.len);
1282 dout("build_path_dentry path+%d: %p '%.*s'\n",
1283 pos, temp, temp->d_name.len, path + pos);
1284 }
1285 if (pos)
1286 path[--pos] = '/';
1287 temp = temp->d_parent;
1288 if (temp == NULL) {
1289 pr_err("build_path_dentry corrupt dentry\n");
1290 kfree(path);
1291 return ERR_PTR(-EINVAL);
1292 }
1293 }
1294 if (pos != 0) {
1295 pr_err("build_path_dentry did not end path lookup where "
1296 "expected, namelen is %d, pos is %d\n", len, pos);
1297 /* presumably this is only possible if racing with a
1298 rename of one of the parent directories (we can not
1299 lock the dentries above us to prevent this, but
1300 retrying should be harmless) */
1301 kfree(path);
1302 goto retry;
1303 }
1304
1305 *base = ceph_ino(temp->d_inode);
1306 *plen = len;
1307 dout("build_path_dentry on %p %d built %llx '%.*s'\n",
1308 dentry, atomic_read(&dentry->d_count), *base, len, path);
1309 return path;
1310}
1311
1312static int build_dentry_path(struct dentry *dentry,
1313 const char **ppath, int *ppathlen, u64 *pino,
1314 int *pfreepath)
1315{
1316 char *path;
1317
1318 if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) {
1319 *pino = ceph_ino(dentry->d_parent->d_inode);
1320 *ppath = dentry->d_name.name;
1321 *ppathlen = dentry->d_name.len;
1322 return 0;
1323 }
1324 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1325 if (IS_ERR(path))
1326 return PTR_ERR(path);
1327 *ppath = path;
1328 *pfreepath = 1;
1329 return 0;
1330}
1331
1332static int build_inode_path(struct inode *inode,
1333 const char **ppath, int *ppathlen, u64 *pino,
1334 int *pfreepath)
1335{
1336 struct dentry *dentry;
1337 char *path;
1338
1339 if (ceph_snap(inode) == CEPH_NOSNAP) {
1340 *pino = ceph_ino(inode);
1341 *ppathlen = 0;
1342 return 0;
1343 }
1344 dentry = d_find_alias(inode);
1345 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1346 dput(dentry);
1347 if (IS_ERR(path))
1348 return PTR_ERR(path);
1349 *ppath = path;
1350 *pfreepath = 1;
1351 return 0;
1352}
1353
1354/*
1355 * request arguments may be specified via an inode *, a dentry *, or
1356 * an explicit ino+path.
1357 */
1358static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1359 const char *rpath, u64 rino,
1360 const char **ppath, int *pathlen,
1361 u64 *ino, int *freepath)
1362{
1363 int r = 0;
1364
1365 if (rinode) {
1366 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1367 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1368 ceph_snap(rinode));
1369 } else if (rdentry) {
1370 r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
1371 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1372 *ppath);
1373 } else if (rpath) {
1374 *ino = rino;
1375 *ppath = rpath;
1376 *pathlen = strlen(rpath);
1377 dout(" path %.*s\n", *pathlen, rpath);
1378 }
1379
1380 return r;
1381}
1382
1383/*
1384 * called under mdsc->mutex
1385 */
1386static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1387 struct ceph_mds_request *req,
1388 int mds)
1389{
1390 struct ceph_msg *msg;
1391 struct ceph_mds_request_head *head;
1392 const char *path1 = NULL;
1393 const char *path2 = NULL;
1394 u64 ino1 = 0, ino2 = 0;
1395 int pathlen1 = 0, pathlen2 = 0;
1396 int freepath1 = 0, freepath2 = 0;
1397 int len;
1398 u16 releases;
1399 void *p, *end;
1400 int ret;
1401
1402 ret = set_request_path_attr(req->r_inode, req->r_dentry,
1403 req->r_path1, req->r_ino1.ino,
1404 &path1, &pathlen1, &ino1, &freepath1);
1405 if (ret < 0) {
1406 msg = ERR_PTR(ret);
1407 goto out;
1408 }
1409
1410 ret = set_request_path_attr(NULL, req->r_old_dentry,
1411 req->r_path2, req->r_ino2.ino,
1412 &path2, &pathlen2, &ino2, &freepath2);
1413 if (ret < 0) {
1414 msg = ERR_PTR(ret);
1415 goto out_free1;
1416 }
1417
1418 len = sizeof(*head) +
Sage Weilac8839d2010-01-27 14:28:10 -08001419 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64));
Sage Weil2f2dc052009-10-06 11:31:09 -07001420
1421 /* calculate (max) length for cap releases */
1422 len += sizeof(struct ceph_mds_request_release) *
1423 (!!req->r_inode_drop + !!req->r_dentry_drop +
1424 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1425 if (req->r_dentry_drop)
1426 len += req->r_dentry->d_name.len;
1427 if (req->r_old_dentry_drop)
1428 len += req->r_old_dentry->d_name.len;
1429
1430 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, 0, 0, NULL);
1431 if (IS_ERR(msg))
1432 goto out_free2;
1433
Sage Weil6df058c2009-12-22 11:24:33 -08001434 msg->hdr.tid = cpu_to_le64(req->r_tid);
1435
Sage Weil2f2dc052009-10-06 11:31:09 -07001436 head = msg->front.iov_base;
1437 p = msg->front.iov_base + sizeof(*head);
1438 end = msg->front.iov_base + msg->front.iov_len;
1439
1440 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1441 head->op = cpu_to_le32(req->r_op);
1442 head->caller_uid = cpu_to_le32(current_fsuid());
1443 head->caller_gid = cpu_to_le32(current_fsgid());
1444 head->args = req->r_args;
1445
1446 ceph_encode_filepath(&p, end, ino1, path1);
1447 ceph_encode_filepath(&p, end, ino2, path2);
1448
1449 /* cap releases */
1450 releases = 0;
1451 if (req->r_inode_drop)
1452 releases += ceph_encode_inode_release(&p,
1453 req->r_inode ? req->r_inode : req->r_dentry->d_inode,
1454 mds, req->r_inode_drop, req->r_inode_unless, 0);
1455 if (req->r_dentry_drop)
1456 releases += ceph_encode_dentry_release(&p, req->r_dentry,
1457 mds, req->r_dentry_drop, req->r_dentry_unless);
1458 if (req->r_old_dentry_drop)
1459 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
1460 mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
1461 if (req->r_old_inode_drop)
1462 releases += ceph_encode_inode_release(&p,
1463 req->r_old_dentry->d_inode,
1464 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
1465 head->num_releases = cpu_to_le16(releases);
1466
1467 BUG_ON(p > end);
1468 msg->front.iov_len = p - msg->front.iov_base;
1469 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1470
1471 msg->pages = req->r_pages;
1472 msg->nr_pages = req->r_num_pages;
1473 msg->hdr.data_len = cpu_to_le32(req->r_data_len);
1474 msg->hdr.data_off = cpu_to_le16(0);
1475
1476out_free2:
1477 if (freepath2)
1478 kfree((char *)path2);
1479out_free1:
1480 if (freepath1)
1481 kfree((char *)path1);
1482out:
1483 return msg;
1484}
1485
1486/*
1487 * called under mdsc->mutex if error, under no mutex if
1488 * success.
1489 */
1490static void complete_request(struct ceph_mds_client *mdsc,
1491 struct ceph_mds_request *req)
1492{
1493 if (req->r_callback)
1494 req->r_callback(mdsc, req);
1495 else
1496 complete(&req->r_completion);
1497}
1498
1499/*
1500 * called under mdsc->mutex
1501 */
1502static int __prepare_send_request(struct ceph_mds_client *mdsc,
1503 struct ceph_mds_request *req,
1504 int mds)
1505{
1506 struct ceph_mds_request_head *rhead;
1507 struct ceph_msg *msg;
1508 int flags = 0;
1509
1510 req->r_mds = mds;
1511 req->r_attempts++;
1512 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
1513 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
1514
1515 if (req->r_request) {
1516 ceph_msg_put(req->r_request);
1517 req->r_request = NULL;
1518 }
1519 msg = create_request_message(mdsc, req, mds);
1520 if (IS_ERR(msg)) {
Sage Weile1518c72010-05-13 11:19:06 -07001521 req->r_err = PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001522 complete_request(mdsc, req);
1523 return -PTR_ERR(msg);
1524 }
1525 req->r_request = msg;
1526
1527 rhead = msg->front.iov_base;
Sage Weil2f2dc052009-10-06 11:31:09 -07001528 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
1529 if (req->r_got_unsafe)
1530 flags |= CEPH_MDS_FLAG_REPLAY;
1531 if (req->r_locked_dir)
1532 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
1533 rhead->flags = cpu_to_le32(flags);
1534 rhead->num_fwd = req->r_num_fwd;
1535 rhead->num_retry = req->r_attempts - 1;
1536
1537 dout(" r_locked_dir = %p\n", req->r_locked_dir);
1538
1539 if (req->r_target_inode && req->r_got_unsafe)
1540 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
1541 else
1542 rhead->ino = 0;
1543 return 0;
1544}
1545
1546/*
1547 * send request, or put it on the appropriate wait list.
1548 */
1549static int __do_request(struct ceph_mds_client *mdsc,
1550 struct ceph_mds_request *req)
1551{
1552 struct ceph_mds_session *session = NULL;
1553 int mds = -1;
1554 int err = -EAGAIN;
1555
Sage Weile1518c72010-05-13 11:19:06 -07001556 if (req->r_err || req->r_got_result)
Sage Weil2f2dc052009-10-06 11:31:09 -07001557 goto out;
1558
1559 if (req->r_timeout &&
1560 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
1561 dout("do_request timed out\n");
1562 err = -EIO;
1563 goto finish;
1564 }
1565
1566 mds = __choose_mds(mdsc, req);
1567 if (mds < 0 ||
1568 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
1569 dout("do_request no mds or not active, waiting for map\n");
1570 list_add(&req->r_wait, &mdsc->waiting_for_map);
1571 goto out;
1572 }
1573
1574 /* get, open session */
1575 session = __ceph_lookup_mds_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07001576 if (!session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001577 session = register_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07001578 if (IS_ERR(session)) {
1579 err = PTR_ERR(session);
1580 goto finish;
1581 }
1582 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001583 dout("do_request mds%d session %p state %s\n", mds, session,
1584 session_state_name(session->s_state));
1585 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
1586 session->s_state != CEPH_MDS_SESSION_HUNG) {
1587 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1588 session->s_state == CEPH_MDS_SESSION_CLOSING)
1589 __open_session(mdsc, session);
1590 list_add(&req->r_wait, &session->s_waiting);
1591 goto out_session;
1592 }
1593
1594 /* send request */
1595 req->r_session = get_session(session);
1596 req->r_resend_mds = -1; /* forget any previous mds hint */
1597
1598 if (req->r_request_started == 0) /* note request start time */
1599 req->r_request_started = jiffies;
1600
1601 err = __prepare_send_request(mdsc, req, mds);
1602 if (!err) {
1603 ceph_msg_get(req->r_request);
1604 ceph_con_send(&session->s_con, req->r_request);
1605 }
1606
1607out_session:
1608 ceph_put_mds_session(session);
1609out:
1610 return err;
1611
1612finish:
Sage Weile1518c72010-05-13 11:19:06 -07001613 req->r_err = err;
Sage Weil2f2dc052009-10-06 11:31:09 -07001614 complete_request(mdsc, req);
1615 goto out;
1616}
1617
1618/*
1619 * called under mdsc->mutex
1620 */
1621static void __wake_requests(struct ceph_mds_client *mdsc,
1622 struct list_head *head)
1623{
1624 struct ceph_mds_request *req, *nreq;
1625
1626 list_for_each_entry_safe(req, nreq, head, r_wait) {
1627 list_del_init(&req->r_wait);
1628 __do_request(mdsc, req);
1629 }
1630}
1631
1632/*
1633 * Wake up threads with requests pending for @mds, so that they can
1634 * resubmit their requests to a possibly different mds. If @all is set,
1635 * wake up if their requests has been forwarded to @mds, too.
1636 */
1637static void kick_requests(struct ceph_mds_client *mdsc, int mds, int all)
1638{
Sage Weil44ca18f2010-02-15 12:08:46 -08001639 struct ceph_mds_request *req;
1640 struct rb_node *p;
Sage Weil2f2dc052009-10-06 11:31:09 -07001641
1642 dout("kick_requests mds%d\n", mds);
Sage Weil44ca18f2010-02-15 12:08:46 -08001643 for (p = rb_first(&mdsc->request_tree); p; p = rb_next(p)) {
1644 req = rb_entry(p, struct ceph_mds_request, r_node);
1645 if (req->r_got_unsafe)
1646 continue;
1647 if (req->r_session &&
1648 req->r_session->s_mds == mds) {
1649 dout(" kicking tid %llu\n", req->r_tid);
1650 put_request_session(req);
1651 __do_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07001652 }
1653 }
1654}
1655
1656void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
1657 struct ceph_mds_request *req)
1658{
1659 dout("submit_request on %p\n", req);
1660 mutex_lock(&mdsc->mutex);
1661 __register_request(mdsc, req, NULL);
1662 __do_request(mdsc, req);
1663 mutex_unlock(&mdsc->mutex);
1664}
1665
1666/*
1667 * Synchrously perform an mds request. Take care of all of the
1668 * session setup, forwarding, retry details.
1669 */
1670int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
1671 struct inode *dir,
1672 struct ceph_mds_request *req)
1673{
1674 int err;
1675
1676 dout("do_request on %p\n", req);
1677
1678 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
1679 if (req->r_inode)
1680 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
1681 if (req->r_locked_dir)
1682 ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
1683 if (req->r_old_dentry)
1684 ceph_get_cap_refs(
1685 ceph_inode(req->r_old_dentry->d_parent->d_inode),
1686 CEPH_CAP_PIN);
1687
1688 /* issue */
1689 mutex_lock(&mdsc->mutex);
1690 __register_request(mdsc, req, dir);
1691 __do_request(mdsc, req);
1692
Sage Weile1518c72010-05-13 11:19:06 -07001693 if (req->r_err) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001694 err = req->r_err;
Sage Weile1518c72010-05-13 11:19:06 -07001695 __unregister_request(mdsc, req);
1696 dout("do_request early error %d\n", err);
1697 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07001698 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001699
Sage Weile1518c72010-05-13 11:19:06 -07001700 /* wait */
1701 mutex_unlock(&mdsc->mutex);
1702 dout("do_request waiting\n");
1703 if (req->r_timeout) {
1704 err = (long)wait_for_completion_interruptible_timeout(
1705 &req->r_completion, req->r_timeout);
1706 if (err == 0)
1707 err = -EIO;
1708 } else {
1709 err = wait_for_completion_interruptible(&req->r_completion);
1710 }
1711 dout("do_request waited, got %d\n", err);
1712 mutex_lock(&mdsc->mutex);
1713
1714 /* only abort if we didn't race with a real reply */
1715 if (req->r_got_result) {
1716 err = le32_to_cpu(req->r_reply_info.head->result);
1717 } else if (err < 0) {
1718 dout("aborted request %lld with %d\n", req->r_tid, err);
Sage Weilb4556392010-05-13 12:01:13 -07001719
1720 /*
1721 * ensure we aren't running concurrently with
1722 * ceph_fill_trace or ceph_readdir_prepopulate, which
1723 * rely on locks (dir mutex) held by our caller.
1724 */
1725 mutex_lock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07001726 req->r_err = err;
1727 req->r_aborted = true;
Sage Weilb4556392010-05-13 12:01:13 -07001728 mutex_unlock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07001729
1730 if (req->r_locked_dir &&
1731 (req->r_op & CEPH_MDS_OP_WRITE)) {
1732 struct ceph_inode_info *ci =
1733 ceph_inode(req->r_locked_dir);
1734
1735 dout("aborted, clearing I_COMPLETE on %p\n",
1736 req->r_locked_dir);
1737 spin_lock(&req->r_locked_dir->i_lock);
1738 ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
1739 ci->i_release_count++;
1740 spin_unlock(&req->r_locked_dir->i_lock);
1741 }
1742 } else {
1743 err = req->r_err;
1744 }
1745
1746out:
1747 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07001748 dout("do_request %p done, result %d\n", req, err);
1749 return err;
1750}
1751
1752/*
1753 * Handle mds reply.
1754 *
1755 * We take the session mutex and parse and process the reply immediately.
1756 * This preserves the logical ordering of replies, capabilities, etc., sent
1757 * by the MDS as they are applied to our local cache.
1758 */
1759static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
1760{
1761 struct ceph_mds_client *mdsc = session->s_mdsc;
1762 struct ceph_mds_request *req;
1763 struct ceph_mds_reply_head *head = msg->front.iov_base;
1764 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
1765 u64 tid;
1766 int err, result;
Sage Weil2600d2d2010-02-22 15:12:16 -08001767 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07001768
Sage Weil2f2dc052009-10-06 11:31:09 -07001769 if (msg->front.iov_len < sizeof(*head)) {
1770 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08001771 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001772 return;
1773 }
1774
1775 /* get request, session */
Sage Weil6df058c2009-12-22 11:24:33 -08001776 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07001777 mutex_lock(&mdsc->mutex);
1778 req = __lookup_request(mdsc, tid);
1779 if (!req) {
1780 dout("handle_reply on unknown tid %llu\n", tid);
1781 mutex_unlock(&mdsc->mutex);
1782 return;
1783 }
1784 dout("handle_reply %p\n", req);
Sage Weil2f2dc052009-10-06 11:31:09 -07001785
1786 /* correct session? */
Sage Weild96d6042010-03-20 20:50:58 -07001787 if (req->r_session != session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001788 pr_err("mdsc_handle_reply got %llu on session mds%d"
1789 " not mds%d\n", tid, session->s_mds,
1790 req->r_session ? req->r_session->s_mds : -1);
1791 mutex_unlock(&mdsc->mutex);
1792 goto out;
1793 }
1794
1795 /* dup? */
1796 if ((req->r_got_unsafe && !head->safe) ||
1797 (req->r_got_safe && head->safe)) {
1798 pr_warning("got a dup %s reply on %llu from mds%d\n",
1799 head->safe ? "safe" : "unsafe", tid, mds);
1800 mutex_unlock(&mdsc->mutex);
1801 goto out;
1802 }
1803
1804 result = le32_to_cpu(head->result);
1805
1806 /*
1807 * Tolerate 2 consecutive ESTALEs from the same mds.
1808 * FIXME: we should be looking at the cap migrate_seq.
1809 */
1810 if (result == -ESTALE) {
1811 req->r_direct_mode = USE_AUTH_MDS;
1812 req->r_num_stale++;
1813 if (req->r_num_stale <= 2) {
1814 __do_request(mdsc, req);
1815 mutex_unlock(&mdsc->mutex);
1816 goto out;
1817 }
1818 } else {
1819 req->r_num_stale = 0;
1820 }
1821
1822 if (head->safe) {
1823 req->r_got_safe = true;
1824 __unregister_request(mdsc, req);
1825 complete(&req->r_safe_completion);
1826
1827 if (req->r_got_unsafe) {
1828 /*
1829 * We already handled the unsafe response, now do the
1830 * cleanup. No need to examine the response; the MDS
1831 * doesn't include any result info in the safe
1832 * response. And even if it did, there is nothing
1833 * useful we could do with a revised return value.
1834 */
1835 dout("got safe reply %llu, mds%d\n", tid, mds);
1836 list_del_init(&req->r_unsafe_item);
1837
1838 /* last unsafe request during umount? */
Sage Weil44ca18f2010-02-15 12:08:46 -08001839 if (mdsc->stopping && !__get_oldest_req(mdsc))
Sage Weil2f2dc052009-10-06 11:31:09 -07001840 complete(&mdsc->safe_umount_waiters);
1841 mutex_unlock(&mdsc->mutex);
1842 goto out;
1843 }
Sage Weile1518c72010-05-13 11:19:06 -07001844 } else {
Sage Weil2f2dc052009-10-06 11:31:09 -07001845 req->r_got_unsafe = true;
1846 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
1847 }
1848
1849 dout("handle_reply tid %lld result %d\n", tid, result);
1850 rinfo = &req->r_reply_info;
1851 err = parse_reply_info(msg, rinfo);
1852 mutex_unlock(&mdsc->mutex);
1853
1854 mutex_lock(&session->s_mutex);
1855 if (err < 0) {
1856 pr_err("mdsc_handle_reply got corrupt reply mds%d\n", mds);
Sage Weil9ec7cab2009-12-14 15:13:47 -08001857 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001858 goto out_err;
1859 }
1860
1861 /* snap trace */
1862 if (rinfo->snapblob_len) {
1863 down_write(&mdsc->snap_rwsem);
1864 ceph_update_snap_trace(mdsc, rinfo->snapblob,
1865 rinfo->snapblob + rinfo->snapblob_len,
1866 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP);
1867 downgrade_write(&mdsc->snap_rwsem);
1868 } else {
1869 down_read(&mdsc->snap_rwsem);
1870 }
1871
1872 /* insert trace into our cache */
Sage Weilb4556392010-05-13 12:01:13 -07001873 mutex_lock(&req->r_fill_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07001874 err = ceph_fill_trace(mdsc->client->sb, req, req->r_session);
1875 if (err == 0) {
1876 if (result == 0 && rinfo->dir_nr)
1877 ceph_readdir_prepopulate(req, req->r_session);
1878 ceph_unreserve_caps(&req->r_caps_reservation);
1879 }
Sage Weilb4556392010-05-13 12:01:13 -07001880 mutex_unlock(&req->r_fill_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07001881
1882 up_read(&mdsc->snap_rwsem);
1883out_err:
Sage Weile1518c72010-05-13 11:19:06 -07001884 mutex_lock(&mdsc->mutex);
1885 if (!req->r_aborted) {
1886 if (err) {
1887 req->r_err = err;
1888 } else {
1889 req->r_reply = msg;
1890 ceph_msg_get(msg);
1891 req->r_got_result = true;
1892 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001893 } else {
Sage Weile1518c72010-05-13 11:19:06 -07001894 dout("reply arrived after request %lld was aborted\n", tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07001895 }
Sage Weile1518c72010-05-13 11:19:06 -07001896 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07001897
1898 add_cap_releases(mdsc, req->r_session, -1);
1899 mutex_unlock(&session->s_mutex);
1900
1901 /* kick calling process */
1902 complete_request(mdsc, req);
1903out:
1904 ceph_mdsc_put_request(req);
1905 return;
1906}
1907
1908
1909
1910/*
1911 * handle mds notification that our request has been forwarded.
1912 */
Sage Weil2600d2d2010-02-22 15:12:16 -08001913static void handle_forward(struct ceph_mds_client *mdsc,
1914 struct ceph_mds_session *session,
1915 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07001916{
1917 struct ceph_mds_request *req;
Sage Weila1ea7872010-02-23 14:02:44 -08001918 u64 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07001919 u32 next_mds;
1920 u32 fwd_seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07001921 int err = -EINVAL;
1922 void *p = msg->front.iov_base;
1923 void *end = p + msg->front.iov_len;
Sage Weil2f2dc052009-10-06 11:31:09 -07001924
Sage Weila1ea7872010-02-23 14:02:44 -08001925 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001926 next_mds = ceph_decode_32(&p);
1927 fwd_seq = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07001928
1929 mutex_lock(&mdsc->mutex);
1930 req = __lookup_request(mdsc, tid);
1931 if (!req) {
Sage Weil080af172010-02-25 16:40:07 -08001932 dout("forward %llu to mds%d - req dne\n", tid, next_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07001933 goto out; /* dup reply? */
1934 }
1935
Sage Weil2f2dc052009-10-06 11:31:09 -07001936 if (fwd_seq <= req->r_num_fwd) {
1937 dout("forward %llu to mds%d - old seq %d <= %d\n",
1938 tid, next_mds, req->r_num_fwd, fwd_seq);
1939 } else {
1940 /* resend. forward race not possible; mds would drop */
1941 dout("forward %llu to mds%d (we resend)\n", tid, next_mds);
1942 req->r_num_fwd = fwd_seq;
1943 req->r_resend_mds = next_mds;
1944 put_request_session(req);
1945 __do_request(mdsc, req);
1946 }
1947 ceph_mdsc_put_request(req);
1948out:
1949 mutex_unlock(&mdsc->mutex);
1950 return;
1951
1952bad:
1953 pr_err("mdsc_handle_forward decode error err=%d\n", err);
1954}
1955
1956/*
1957 * handle a mds session control message
1958 */
1959static void handle_session(struct ceph_mds_session *session,
1960 struct ceph_msg *msg)
1961{
1962 struct ceph_mds_client *mdsc = session->s_mdsc;
1963 u32 op;
1964 u64 seq;
Sage Weil2600d2d2010-02-22 15:12:16 -08001965 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07001966 struct ceph_mds_session_head *h = msg->front.iov_base;
1967 int wake = 0;
1968
Sage Weil2f2dc052009-10-06 11:31:09 -07001969 /* decode */
1970 if (msg->front.iov_len != sizeof(*h))
1971 goto bad;
1972 op = le32_to_cpu(h->op);
1973 seq = le64_to_cpu(h->seq);
1974
1975 mutex_lock(&mdsc->mutex);
Sage Weil2600d2d2010-02-22 15:12:16 -08001976 if (op == CEPH_SESSION_CLOSE)
1977 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07001978 /* FIXME: this ttl calculation is generous */
1979 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
1980 mutex_unlock(&mdsc->mutex);
1981
1982 mutex_lock(&session->s_mutex);
1983
1984 dout("handle_session mds%d %s %p state %s seq %llu\n",
1985 mds, ceph_session_op_name(op), session,
1986 session_state_name(session->s_state), seq);
1987
1988 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
1989 session->s_state = CEPH_MDS_SESSION_OPEN;
1990 pr_info("mds%d came back\n", session->s_mds);
1991 }
1992
1993 switch (op) {
1994 case CEPH_SESSION_OPEN:
1995 session->s_state = CEPH_MDS_SESSION_OPEN;
1996 renewed_caps(mdsc, session, 0);
1997 wake = 1;
1998 if (mdsc->stopping)
1999 __close_session(mdsc, session);
2000 break;
2001
2002 case CEPH_SESSION_RENEWCAPS:
2003 if (session->s_renew_seq == seq)
2004 renewed_caps(mdsc, session, 1);
2005 break;
2006
2007 case CEPH_SESSION_CLOSE:
Sage Weil2f2dc052009-10-06 11:31:09 -07002008 remove_session_caps(session);
2009 wake = 1; /* for good measure */
2010 complete(&mdsc->session_close_waiters);
2011 kick_requests(mdsc, mds, 0); /* cur only */
2012 break;
2013
2014 case CEPH_SESSION_STALE:
2015 pr_info("mds%d caps went stale, renewing\n",
2016 session->s_mds);
2017 spin_lock(&session->s_cap_lock);
2018 session->s_cap_gen++;
2019 session->s_cap_ttl = 0;
2020 spin_unlock(&session->s_cap_lock);
2021 send_renew_caps(mdsc, session);
2022 break;
2023
2024 case CEPH_SESSION_RECALL_STATE:
2025 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2026 break;
2027
2028 default:
2029 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2030 WARN_ON(1);
2031 }
2032
2033 mutex_unlock(&session->s_mutex);
2034 if (wake) {
2035 mutex_lock(&mdsc->mutex);
2036 __wake_requests(mdsc, &session->s_waiting);
2037 mutex_unlock(&mdsc->mutex);
2038 }
2039 return;
2040
2041bad:
2042 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2043 (int)msg->front.iov_len);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002044 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002045 return;
2046}
2047
2048
2049/*
2050 * called under session->mutex.
2051 */
2052static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2053 struct ceph_mds_session *session)
2054{
2055 struct ceph_mds_request *req, *nreq;
2056 int err;
2057
2058 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2059
2060 mutex_lock(&mdsc->mutex);
2061 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
2062 err = __prepare_send_request(mdsc, req, session->s_mds);
2063 if (!err) {
2064 ceph_msg_get(req->r_request);
2065 ceph_con_send(&session->s_con, req->r_request);
2066 }
2067 }
2068 mutex_unlock(&mdsc->mutex);
2069}
2070
2071/*
2072 * Encode information about a cap for a reconnect with the MDS.
2073 */
Sage Weil2f2dc052009-10-06 11:31:09 -07002074static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2075 void *arg)
2076{
Sage Weil93cea5b2009-12-23 12:21:51 -08002077 struct ceph_mds_cap_reconnect rec;
Sage Weil2f2dc052009-10-06 11:31:09 -07002078 struct ceph_inode_info *ci;
Sage Weil93cea5b2009-12-23 12:21:51 -08002079 struct ceph_pagelist *pagelist = arg;
Sage Weil2f2dc052009-10-06 11:31:09 -07002080 char *path;
2081 int pathlen, err;
2082 u64 pathbase;
2083 struct dentry *dentry;
2084
2085 ci = cap->ci;
2086
2087 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2088 inode, ceph_vinop(inode), cap, cap->cap_id,
2089 ceph_cap_string(cap->issued));
Sage Weil93cea5b2009-12-23 12:21:51 -08002090 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2091 if (err)
2092 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002093
2094 dentry = d_find_alias(inode);
2095 if (dentry) {
2096 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2097 if (IS_ERR(path)) {
2098 err = PTR_ERR(path);
2099 BUG_ON(err);
2100 }
2101 } else {
2102 path = NULL;
2103 pathlen = 0;
2104 }
Sage Weil93cea5b2009-12-23 12:21:51 -08002105 err = ceph_pagelist_encode_string(pagelist, path, pathlen);
2106 if (err)
2107 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07002108
Sage Weil2f2dc052009-10-06 11:31:09 -07002109 spin_lock(&inode->i_lock);
2110 cap->seq = 0; /* reset cap seq */
2111 cap->issue_seq = 0; /* and issue_seq */
Sage Weil93cea5b2009-12-23 12:21:51 -08002112 rec.cap_id = cpu_to_le64(cap->cap_id);
2113 rec.pathbase = cpu_to_le64(pathbase);
2114 rec.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2115 rec.issued = cpu_to_le32(cap->issued);
2116 rec.size = cpu_to_le64(inode->i_size);
2117 ceph_encode_timespec(&rec.mtime, &inode->i_mtime);
2118 ceph_encode_timespec(&rec.atime, &inode->i_atime);
2119 rec.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
Sage Weil2f2dc052009-10-06 11:31:09 -07002120 spin_unlock(&inode->i_lock);
2121
Sage Weil93cea5b2009-12-23 12:21:51 -08002122 err = ceph_pagelist_append(pagelist, &rec, sizeof(rec));
2123
2124out:
Sage Weil2f2dc052009-10-06 11:31:09 -07002125 kfree(path);
2126 dput(dentry);
Sage Weil93cea5b2009-12-23 12:21:51 -08002127 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002128}
2129
2130
2131/*
2132 * If an MDS fails and recovers, clients need to reconnect in order to
2133 * reestablish shared state. This includes all caps issued through
2134 * this session _and_ the snap_realm hierarchy. Because it's not
2135 * clear which snap realms the mds cares about, we send everything we
2136 * know about.. that ensures we'll then get any new info the
2137 * recovering MDS might have.
2138 *
2139 * This is a relatively heavyweight operation, but it's rare.
2140 *
2141 * called with mdsc->mutex held.
2142 */
2143static void send_mds_reconnect(struct ceph_mds_client *mdsc, int mds)
2144{
Sage Weil93cea5b2009-12-23 12:21:51 -08002145 struct ceph_mds_session *session = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07002146 struct ceph_msg *reply;
Sage Weila105f002010-02-15 14:37:55 -08002147 struct rb_node *p;
Sage Weil9abf82b2010-05-10 21:58:38 -07002148 int err = -ENOMEM;
Sage Weil93cea5b2009-12-23 12:21:51 -08002149 struct ceph_pagelist *pagelist;
Sage Weil2f2dc052009-10-06 11:31:09 -07002150
2151 pr_info("reconnect to recovering mds%d\n", mds);
2152
Sage Weil93cea5b2009-12-23 12:21:51 -08002153 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
2154 if (!pagelist)
2155 goto fail_nopagelist;
2156 ceph_pagelist_init(pagelist);
2157
2158 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, 0, 0, NULL);
2159 if (IS_ERR(reply)) {
2160 err = PTR_ERR(reply);
2161 goto fail_nomsg;
2162 }
2163
Sage Weil2f2dc052009-10-06 11:31:09 -07002164 /* find session */
2165 session = __ceph_lookup_mds_session(mdsc, mds);
2166 mutex_unlock(&mdsc->mutex); /* drop lock for duration */
2167
2168 if (session) {
2169 mutex_lock(&session->s_mutex);
2170
2171 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
2172 session->s_seq = 0;
2173
2174 ceph_con_open(&session->s_con,
2175 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
2176
2177 /* replay unsafe requests */
2178 replay_unsafe_requests(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002179 } else {
2180 dout("no session for mds%d, will send short reconnect\n",
2181 mds);
2182 }
2183
2184 down_read(&mdsc->snap_rwsem);
2185
Sage Weil93cea5b2009-12-23 12:21:51 -08002186 if (!session)
Sage Weil2f2dc052009-10-06 11:31:09 -07002187 goto send;
Sage Weil2f2dc052009-10-06 11:31:09 -07002188 dout("session %p state %s\n", session,
2189 session_state_name(session->s_state));
2190
2191 /* traverse this session's caps */
Sage Weil93cea5b2009-12-23 12:21:51 -08002192 err = ceph_pagelist_encode_32(pagelist, session->s_nr_caps);
2193 if (err)
2194 goto fail;
2195 err = iterate_session_caps(session, encode_caps_cb, pagelist);
Sage Weil2f2dc052009-10-06 11:31:09 -07002196 if (err < 0)
Sage Weil9abf82b2010-05-10 21:58:38 -07002197 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07002198
2199 /*
2200 * snaprealms. we provide mds with the ino, seq (version), and
2201 * parent for all of our realms. If the mds has any newer info,
2202 * it will tell us.
2203 */
Sage Weila105f002010-02-15 14:37:55 -08002204 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
2205 struct ceph_snap_realm *realm =
2206 rb_entry(p, struct ceph_snap_realm, node);
Sage Weil93cea5b2009-12-23 12:21:51 -08002207 struct ceph_mds_snaprealm_reconnect sr_rec;
Sage Weil2f2dc052009-10-06 11:31:09 -07002208
2209 dout(" adding snap realm %llx seq %lld parent %llx\n",
2210 realm->ino, realm->seq, realm->parent_ino);
Sage Weil93cea5b2009-12-23 12:21:51 -08002211 sr_rec.ino = cpu_to_le64(realm->ino);
2212 sr_rec.seq = cpu_to_le64(realm->seq);
2213 sr_rec.parent = cpu_to_le64(realm->parent_ino);
2214 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
2215 if (err)
2216 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07002217 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002218
2219send:
Sage Weil93cea5b2009-12-23 12:21:51 -08002220 reply->pagelist = pagelist;
2221 reply->hdr.data_len = cpu_to_le32(pagelist->length);
2222 reply->nr_pages = calc_pages_for(0, pagelist->length);
Sage Weil2f2dc052009-10-06 11:31:09 -07002223 ceph_con_send(&session->s_con, reply);
2224
Sage Weil9abf82b2010-05-10 21:58:38 -07002225 session->s_state = CEPH_MDS_SESSION_OPEN;
2226 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002227
Sage Weil9abf82b2010-05-10 21:58:38 -07002228 mutex_lock(&mdsc->mutex);
2229 __wake_requests(mdsc, &session->s_waiting);
2230 mutex_unlock(&mdsc->mutex);
2231
2232 ceph_put_mds_session(session);
2233
Sage Weil2f2dc052009-10-06 11:31:09 -07002234 up_read(&mdsc->snap_rwsem);
Sage Weil2f2dc052009-10-06 11:31:09 -07002235 mutex_lock(&mdsc->mutex);
2236 return;
2237
Sage Weil93cea5b2009-12-23 12:21:51 -08002238fail:
Sage Weil2f2dc052009-10-06 11:31:09 -07002239 ceph_msg_put(reply);
Sage Weil9abf82b2010-05-10 21:58:38 -07002240 up_read(&mdsc->snap_rwsem);
2241 mutex_unlock(&session->s_mutex);
2242 ceph_put_mds_session(session);
Sage Weil93cea5b2009-12-23 12:21:51 -08002243fail_nomsg:
2244 ceph_pagelist_release(pagelist);
2245 kfree(pagelist);
2246fail_nopagelist:
Sage Weil9abf82b2010-05-10 21:58:38 -07002247 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
2248 mutex_lock(&mdsc->mutex);
2249 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07002250}
2251
2252
2253/*
2254 * compare old and new mdsmaps, kicking requests
2255 * and closing out old connections as necessary
2256 *
2257 * called under mdsc->mutex.
2258 */
2259static void check_new_map(struct ceph_mds_client *mdsc,
2260 struct ceph_mdsmap *newmap,
2261 struct ceph_mdsmap *oldmap)
2262{
2263 int i;
2264 int oldstate, newstate;
2265 struct ceph_mds_session *s;
2266
2267 dout("check_new_map new %u old %u\n",
2268 newmap->m_epoch, oldmap->m_epoch);
2269
2270 for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
2271 if (mdsc->sessions[i] == NULL)
2272 continue;
2273 s = mdsc->sessions[i];
2274 oldstate = ceph_mdsmap_get_state(oldmap, i);
2275 newstate = ceph_mdsmap_get_state(newmap, i);
2276
2277 dout("check_new_map mds%d state %s -> %s (session %s)\n",
2278 i, ceph_mds_state_name(oldstate),
2279 ceph_mds_state_name(newstate),
2280 session_state_name(s->s_state));
2281
2282 if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
2283 ceph_mdsmap_get_addr(newmap, i),
2284 sizeof(struct ceph_entity_addr))) {
2285 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
2286 /* the session never opened, just close it
2287 * out now */
2288 __wake_requests(mdsc, &s->s_waiting);
Sage Weil2600d2d2010-02-22 15:12:16 -08002289 __unregister_session(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07002290 } else {
2291 /* just close it */
2292 mutex_unlock(&mdsc->mutex);
2293 mutex_lock(&s->s_mutex);
2294 mutex_lock(&mdsc->mutex);
2295 ceph_con_close(&s->s_con);
2296 mutex_unlock(&s->s_mutex);
2297 s->s_state = CEPH_MDS_SESSION_RESTARTING;
2298 }
2299
2300 /* kick any requests waiting on the recovering mds */
2301 kick_requests(mdsc, i, 1);
2302 } else if (oldstate == newstate) {
2303 continue; /* nothing new with this mds */
2304 }
2305
2306 /*
2307 * send reconnect?
2308 */
2309 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
2310 newstate >= CEPH_MDS_STATE_RECONNECT)
2311 send_mds_reconnect(mdsc, i);
2312
2313 /*
2314 * kick requests on any mds that has gone active.
2315 *
2316 * kick requests on cur or forwarder: we may have sent
2317 * the request to mds1, mds1 told us it forwarded it
2318 * to mds2, but then we learn mds1 failed and can't be
2319 * sure it successfully forwarded our request before
2320 * it died.
2321 */
2322 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
2323 newstate >= CEPH_MDS_STATE_ACTIVE) {
Sage Weilfef320f2009-11-11 15:50:12 -08002324 pr_info("mds%d reconnect completed\n", s->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002325 kick_requests(mdsc, i, 1);
2326 ceph_kick_flushing_caps(mdsc, s);
Sage Weil0dc25702009-11-20 13:43:45 -08002327 wake_up_session_caps(s, 1);
Sage Weil2f2dc052009-10-06 11:31:09 -07002328 }
2329 }
2330}
2331
2332
2333
2334/*
2335 * leases
2336 */
2337
2338/*
2339 * caller must hold session s_mutex, dentry->d_lock
2340 */
2341void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
2342{
2343 struct ceph_dentry_info *di = ceph_dentry(dentry);
2344
2345 ceph_put_mds_session(di->lease_session);
2346 di->lease_session = NULL;
2347}
2348
Sage Weil2600d2d2010-02-22 15:12:16 -08002349static void handle_lease(struct ceph_mds_client *mdsc,
2350 struct ceph_mds_session *session,
2351 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002352{
2353 struct super_block *sb = mdsc->client->sb;
2354 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07002355 struct ceph_inode_info *ci;
2356 struct dentry *parent, *dentry;
2357 struct ceph_dentry_info *di;
Sage Weil2600d2d2010-02-22 15:12:16 -08002358 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002359 struct ceph_mds_lease *h = msg->front.iov_base;
2360 struct ceph_vino vino;
2361 int mask;
2362 struct qstr dname;
2363 int release = 0;
2364
Sage Weil2f2dc052009-10-06 11:31:09 -07002365 dout("handle_lease from mds%d\n", mds);
2366
2367 /* decode */
2368 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
2369 goto bad;
2370 vino.ino = le64_to_cpu(h->ino);
2371 vino.snap = CEPH_NOSNAP;
2372 mask = le16_to_cpu(h->mask);
2373 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
2374 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
2375 if (dname.len != get_unaligned_le32(h+1))
2376 goto bad;
2377
Sage Weil2f2dc052009-10-06 11:31:09 -07002378 mutex_lock(&session->s_mutex);
2379 session->s_seq++;
2380
2381 /* lookup inode */
2382 inode = ceph_find_inode(sb, vino);
2383 dout("handle_lease '%s', mask %d, ino %llx %p\n",
2384 ceph_lease_op_name(h->action), mask, vino.ino, inode);
2385 if (inode == NULL) {
2386 dout("handle_lease no inode %llx\n", vino.ino);
2387 goto release;
2388 }
2389 ci = ceph_inode(inode);
2390
2391 /* dentry */
2392 parent = d_find_alias(inode);
2393 if (!parent) {
2394 dout("no parent dentry on inode %p\n", inode);
2395 WARN_ON(1);
2396 goto release; /* hrm... */
2397 }
2398 dname.hash = full_name_hash(dname.name, dname.len);
2399 dentry = d_lookup(parent, &dname);
2400 dput(parent);
2401 if (!dentry)
2402 goto release;
2403
2404 spin_lock(&dentry->d_lock);
2405 di = ceph_dentry(dentry);
2406 switch (h->action) {
2407 case CEPH_MDS_LEASE_REVOKE:
2408 if (di && di->lease_session == session) {
2409 h->seq = cpu_to_le32(di->lease_seq);
2410 __ceph_mdsc_drop_dentry_lease(dentry);
2411 }
2412 release = 1;
2413 break;
2414
2415 case CEPH_MDS_LEASE_RENEW:
2416 if (di && di->lease_session == session &&
2417 di->lease_gen == session->s_cap_gen &&
2418 di->lease_renew_from &&
2419 di->lease_renew_after == 0) {
2420 unsigned long duration =
2421 le32_to_cpu(h->duration_ms) * HZ / 1000;
2422
2423 di->lease_seq = le32_to_cpu(h->seq);
2424 dentry->d_time = di->lease_renew_from + duration;
2425 di->lease_renew_after = di->lease_renew_from +
2426 (duration >> 1);
2427 di->lease_renew_from = 0;
2428 }
2429 break;
2430 }
2431 spin_unlock(&dentry->d_lock);
2432 dput(dentry);
2433
2434 if (!release)
2435 goto out;
2436
2437release:
2438 /* let's just reuse the same message */
2439 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
2440 ceph_msg_get(msg);
2441 ceph_con_send(&session->s_con, msg);
2442
2443out:
2444 iput(inode);
2445 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002446 return;
2447
2448bad:
2449 pr_err("corrupt lease message\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002450 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002451}
2452
2453void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
2454 struct inode *inode,
2455 struct dentry *dentry, char action,
2456 u32 seq)
2457{
2458 struct ceph_msg *msg;
2459 struct ceph_mds_lease *lease;
2460 int len = sizeof(*lease) + sizeof(u32);
2461 int dnamelen = 0;
2462
2463 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
2464 inode, dentry, ceph_lease_op_name(action), session->s_mds);
2465 dnamelen = dentry->d_name.len;
2466 len += dnamelen;
2467
2468 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, 0, 0, NULL);
2469 if (IS_ERR(msg))
2470 return;
2471 lease = msg->front.iov_base;
2472 lease->action = action;
2473 lease->mask = cpu_to_le16(CEPH_LOCK_DN);
2474 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
2475 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
2476 lease->seq = cpu_to_le32(seq);
2477 put_unaligned_le32(dnamelen, lease + 1);
2478 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
2479
2480 /*
2481 * if this is a preemptive lease RELEASE, no need to
2482 * flush request stream, since the actual request will
2483 * soon follow.
2484 */
2485 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
2486
2487 ceph_con_send(&session->s_con, msg);
2488}
2489
2490/*
2491 * Preemptively release a lease we expect to invalidate anyway.
2492 * Pass @inode always, @dentry is optional.
2493 */
2494void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
2495 struct dentry *dentry, int mask)
2496{
2497 struct ceph_dentry_info *di;
2498 struct ceph_mds_session *session;
2499 u32 seq;
2500
2501 BUG_ON(inode == NULL);
2502 BUG_ON(dentry == NULL);
2503 BUG_ON(mask != CEPH_LOCK_DN);
2504
2505 /* is dentry lease valid? */
2506 spin_lock(&dentry->d_lock);
2507 di = ceph_dentry(dentry);
2508 if (!di || !di->lease_session ||
2509 di->lease_session->s_mds < 0 ||
2510 di->lease_gen != di->lease_session->s_cap_gen ||
2511 !time_before(jiffies, dentry->d_time)) {
2512 dout("lease_release inode %p dentry %p -- "
2513 "no lease on %d\n",
2514 inode, dentry, mask);
2515 spin_unlock(&dentry->d_lock);
2516 return;
2517 }
2518
2519 /* we do have a lease on this dentry; note mds and seq */
2520 session = ceph_get_mds_session(di->lease_session);
2521 seq = di->lease_seq;
2522 __ceph_mdsc_drop_dentry_lease(dentry);
2523 spin_unlock(&dentry->d_lock);
2524
2525 dout("lease_release inode %p dentry %p mask %d to mds%d\n",
2526 inode, dentry, mask, session->s_mds);
2527 ceph_mdsc_lease_send_msg(session, inode, dentry,
2528 CEPH_MDS_LEASE_RELEASE, seq);
2529 ceph_put_mds_session(session);
2530}
2531
2532/*
2533 * drop all leases (and dentry refs) in preparation for umount
2534 */
2535static void drop_leases(struct ceph_mds_client *mdsc)
2536{
2537 int i;
2538
2539 dout("drop_leases\n");
2540 mutex_lock(&mdsc->mutex);
2541 for (i = 0; i < mdsc->max_sessions; i++) {
2542 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2543 if (!s)
2544 continue;
2545 mutex_unlock(&mdsc->mutex);
2546 mutex_lock(&s->s_mutex);
2547 mutex_unlock(&s->s_mutex);
2548 ceph_put_mds_session(s);
2549 mutex_lock(&mdsc->mutex);
2550 }
2551 mutex_unlock(&mdsc->mutex);
2552}
2553
2554
2555
2556/*
2557 * delayed work -- periodically trim expired leases, renew caps with mds
2558 */
2559static void schedule_delayed(struct ceph_mds_client *mdsc)
2560{
2561 int delay = 5;
2562 unsigned hz = round_jiffies_relative(HZ * delay);
2563 schedule_delayed_work(&mdsc->delayed_work, hz);
2564}
2565
2566static void delayed_work(struct work_struct *work)
2567{
2568 int i;
2569 struct ceph_mds_client *mdsc =
2570 container_of(work, struct ceph_mds_client, delayed_work.work);
2571 int renew_interval;
2572 int renew_caps;
2573
2574 dout("mdsc delayed_work\n");
Sage Weilafcdaea2009-10-14 14:27:38 -07002575 ceph_check_delayed_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07002576
2577 mutex_lock(&mdsc->mutex);
2578 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
2579 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
2580 mdsc->last_renew_caps);
2581 if (renew_caps)
2582 mdsc->last_renew_caps = jiffies;
2583
2584 for (i = 0; i < mdsc->max_sessions; i++) {
2585 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2586 if (s == NULL)
2587 continue;
2588 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
2589 dout("resending session close request for mds%d\n",
2590 s->s_mds);
2591 request_close_session(mdsc, s);
2592 ceph_put_mds_session(s);
2593 continue;
2594 }
2595 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
2596 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
2597 s->s_state = CEPH_MDS_SESSION_HUNG;
2598 pr_info("mds%d hung\n", s->s_mds);
2599 }
2600 }
2601 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
2602 /* this mds is failed or recovering, just wait */
2603 ceph_put_mds_session(s);
2604 continue;
2605 }
2606 mutex_unlock(&mdsc->mutex);
2607
2608 mutex_lock(&s->s_mutex);
2609 if (renew_caps)
2610 send_renew_caps(mdsc, s);
2611 else
2612 ceph_con_keepalive(&s->s_con);
2613 add_cap_releases(mdsc, s, -1);
2614 send_cap_releases(mdsc, s);
2615 mutex_unlock(&s->s_mutex);
2616 ceph_put_mds_session(s);
2617
2618 mutex_lock(&mdsc->mutex);
2619 }
2620 mutex_unlock(&mdsc->mutex);
2621
2622 schedule_delayed(mdsc);
2623}
2624
2625
Sage Weil5f44f142009-11-18 14:52:18 -08002626int ceph_mdsc_init(struct ceph_mds_client *mdsc, struct ceph_client *client)
Sage Weil2f2dc052009-10-06 11:31:09 -07002627{
2628 mdsc->client = client;
2629 mutex_init(&mdsc->mutex);
2630 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
2631 init_completion(&mdsc->safe_umount_waiters);
2632 init_completion(&mdsc->session_close_waiters);
2633 INIT_LIST_HEAD(&mdsc->waiting_for_map);
2634 mdsc->sessions = NULL;
2635 mdsc->max_sessions = 0;
2636 mdsc->stopping = 0;
2637 init_rwsem(&mdsc->snap_rwsem);
Sage Weila105f002010-02-15 14:37:55 -08002638 mdsc->snap_realms = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07002639 INIT_LIST_HEAD(&mdsc->snap_empty);
2640 spin_lock_init(&mdsc->snap_empty_lock);
2641 mdsc->last_tid = 0;
Sage Weil44ca18f2010-02-15 12:08:46 -08002642 mdsc->request_tree = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07002643 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
2644 mdsc->last_renew_caps = jiffies;
2645 INIT_LIST_HEAD(&mdsc->cap_delay_list);
2646 spin_lock_init(&mdsc->cap_delay_lock);
2647 INIT_LIST_HEAD(&mdsc->snap_flush_list);
2648 spin_lock_init(&mdsc->snap_flush_lock);
2649 mdsc->cap_flush_seq = 0;
2650 INIT_LIST_HEAD(&mdsc->cap_dirty);
2651 mdsc->num_cap_flushing = 0;
2652 spin_lock_init(&mdsc->cap_dirty_lock);
2653 init_waitqueue_head(&mdsc->cap_flushing_wq);
2654 spin_lock_init(&mdsc->dentry_lru_lock);
2655 INIT_LIST_HEAD(&mdsc->dentry_lru);
Sage Weil5f44f142009-11-18 14:52:18 -08002656 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002657}
2658
2659/*
2660 * Wait for safe replies on open mds requests. If we time out, drop
2661 * all requests from the tree to avoid dangling dentry refs.
2662 */
2663static void wait_requests(struct ceph_mds_client *mdsc)
2664{
2665 struct ceph_mds_request *req;
2666 struct ceph_client *client = mdsc->client;
2667
2668 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08002669 if (__get_oldest_req(mdsc)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002670 mutex_unlock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08002671
Sage Weil2f2dc052009-10-06 11:31:09 -07002672 dout("wait_requests waiting for requests\n");
2673 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
Sage Weil6b805182009-10-27 11:50:50 -07002674 client->mount_args->mount_timeout * HZ);
Sage Weil2f2dc052009-10-06 11:31:09 -07002675
2676 /* tear down remaining requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08002677 mutex_lock(&mdsc->mutex);
2678 while ((req = __get_oldest_req(mdsc))) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002679 dout("wait_requests timed out on tid %llu\n",
2680 req->r_tid);
Sage Weil44ca18f2010-02-15 12:08:46 -08002681 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002682 }
2683 }
2684 mutex_unlock(&mdsc->mutex);
2685 dout("wait_requests done\n");
2686}
2687
2688/*
2689 * called before mount is ro, and before dentries are torn down.
2690 * (hmm, does this still race with new lookups?)
2691 */
2692void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
2693{
2694 dout("pre_umount\n");
2695 mdsc->stopping = 1;
2696
2697 drop_leases(mdsc);
Sage Weilafcdaea2009-10-14 14:27:38 -07002698 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07002699 wait_requests(mdsc);
2700}
2701
2702/*
2703 * wait for all write mds requests to flush.
2704 */
2705static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
2706{
Sage Weil80fc7312010-03-16 15:28:54 -07002707 struct ceph_mds_request *req = NULL, *nextreq;
Sage Weil44ca18f2010-02-15 12:08:46 -08002708 struct rb_node *n;
Sage Weil2f2dc052009-10-06 11:31:09 -07002709
2710 mutex_lock(&mdsc->mutex);
2711 dout("wait_unsafe_requests want %lld\n", want_tid);
Sage Weil80fc7312010-03-16 15:28:54 -07002712restart:
Sage Weil44ca18f2010-02-15 12:08:46 -08002713 req = __get_oldest_req(mdsc);
2714 while (req && req->r_tid <= want_tid) {
Sage Weil80fc7312010-03-16 15:28:54 -07002715 /* find next request */
2716 n = rb_next(&req->r_node);
2717 if (n)
2718 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
2719 else
2720 nextreq = NULL;
Sage Weil44ca18f2010-02-15 12:08:46 -08002721 if ((req->r_op & CEPH_MDS_OP_WRITE)) {
2722 /* write op */
2723 ceph_mdsc_get_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07002724 if (nextreq)
2725 ceph_mdsc_get_request(nextreq);
Sage Weil44ca18f2010-02-15 12:08:46 -08002726 mutex_unlock(&mdsc->mutex);
2727 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
2728 req->r_tid, want_tid);
2729 wait_for_completion(&req->r_safe_completion);
2730 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08002731 ceph_mdsc_put_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07002732 if (!nextreq)
2733 break; /* next dne before, so we're done! */
2734 if (RB_EMPTY_NODE(&nextreq->r_node)) {
2735 /* next request was removed from tree */
2736 ceph_mdsc_put_request(nextreq);
2737 goto restart;
2738 }
2739 ceph_mdsc_put_request(nextreq); /* won't go away */
Sage Weil44ca18f2010-02-15 12:08:46 -08002740 }
Sage Weil80fc7312010-03-16 15:28:54 -07002741 req = nextreq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002742 }
2743 mutex_unlock(&mdsc->mutex);
2744 dout("wait_unsafe_requests done\n");
2745}
2746
2747void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
2748{
2749 u64 want_tid, want_flush;
2750
2751 dout("sync\n");
2752 mutex_lock(&mdsc->mutex);
2753 want_tid = mdsc->last_tid;
2754 want_flush = mdsc->cap_flush_seq;
2755 mutex_unlock(&mdsc->mutex);
2756 dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
2757
Sage Weilafcdaea2009-10-14 14:27:38 -07002758 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07002759
2760 wait_unsafe_requests(mdsc, want_tid);
2761 wait_event(mdsc->cap_flushing_wq, check_cap_flush(mdsc, want_flush));
2762}
2763
2764
2765/*
2766 * called after sb is ro.
2767 */
2768void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
2769{
2770 struct ceph_mds_session *session;
2771 int i;
2772 int n;
2773 struct ceph_client *client = mdsc->client;
Sage Weil6b805182009-10-27 11:50:50 -07002774 unsigned long started, timeout = client->mount_args->mount_timeout * HZ;
Sage Weil2f2dc052009-10-06 11:31:09 -07002775
2776 dout("close_sessions\n");
2777
2778 mutex_lock(&mdsc->mutex);
2779
2780 /* close sessions */
2781 started = jiffies;
2782 while (time_before(jiffies, started + timeout)) {
2783 dout("closing sessions\n");
2784 n = 0;
2785 for (i = 0; i < mdsc->max_sessions; i++) {
2786 session = __ceph_lookup_mds_session(mdsc, i);
2787 if (!session)
2788 continue;
2789 mutex_unlock(&mdsc->mutex);
2790 mutex_lock(&session->s_mutex);
2791 __close_session(mdsc, session);
2792 mutex_unlock(&session->s_mutex);
2793 ceph_put_mds_session(session);
2794 mutex_lock(&mdsc->mutex);
2795 n++;
2796 }
2797 if (n == 0)
2798 break;
2799
2800 if (client->mount_state == CEPH_MOUNT_SHUTDOWN)
2801 break;
2802
2803 dout("waiting for sessions to close\n");
2804 mutex_unlock(&mdsc->mutex);
2805 wait_for_completion_timeout(&mdsc->session_close_waiters,
2806 timeout);
2807 mutex_lock(&mdsc->mutex);
2808 }
2809
2810 /* tear down remaining sessions */
2811 for (i = 0; i < mdsc->max_sessions; i++) {
2812 if (mdsc->sessions[i]) {
2813 session = get_session(mdsc->sessions[i]);
Sage Weil2600d2d2010-02-22 15:12:16 -08002814 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002815 mutex_unlock(&mdsc->mutex);
2816 mutex_lock(&session->s_mutex);
2817 remove_session_caps(session);
2818 mutex_unlock(&session->s_mutex);
2819 ceph_put_mds_session(session);
2820 mutex_lock(&mdsc->mutex);
2821 }
2822 }
2823
2824 WARN_ON(!list_empty(&mdsc->cap_delay_list));
2825
2826 mutex_unlock(&mdsc->mutex);
2827
2828 ceph_cleanup_empty_realms(mdsc);
2829
2830 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
2831
2832 dout("stopped\n");
2833}
2834
2835void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
2836{
2837 dout("stop\n");
2838 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
2839 if (mdsc->mdsmap)
2840 ceph_mdsmap_destroy(mdsc->mdsmap);
2841 kfree(mdsc->sessions);
2842}
2843
2844
2845/*
2846 * handle mds map update.
2847 */
2848void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
2849{
2850 u32 epoch;
2851 u32 maplen;
2852 void *p = msg->front.iov_base;
2853 void *end = p + msg->front.iov_len;
2854 struct ceph_mdsmap *newmap, *oldmap;
2855 struct ceph_fsid fsid;
2856 int err = -EINVAL;
2857
2858 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
2859 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08002860 if (ceph_check_fsid(mdsc->client, &fsid) < 0)
2861 return;
Sage Weilc89136e2009-10-14 09:59:09 -07002862 epoch = ceph_decode_32(&p);
2863 maplen = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07002864 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
2865
2866 /* do we need it? */
2867 ceph_monc_got_mdsmap(&mdsc->client->monc, epoch);
2868 mutex_lock(&mdsc->mutex);
2869 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
2870 dout("handle_map epoch %u <= our %u\n",
2871 epoch, mdsc->mdsmap->m_epoch);
2872 mutex_unlock(&mdsc->mutex);
2873 return;
2874 }
2875
2876 newmap = ceph_mdsmap_decode(&p, end);
2877 if (IS_ERR(newmap)) {
2878 err = PTR_ERR(newmap);
2879 goto bad_unlock;
2880 }
2881
2882 /* swap into place */
2883 if (mdsc->mdsmap) {
2884 oldmap = mdsc->mdsmap;
2885 mdsc->mdsmap = newmap;
2886 check_new_map(mdsc, newmap, oldmap);
2887 ceph_mdsmap_destroy(oldmap);
2888 } else {
2889 mdsc->mdsmap = newmap; /* first mds map */
2890 }
2891 mdsc->client->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
2892
2893 __wake_requests(mdsc, &mdsc->waiting_for_map);
2894
2895 mutex_unlock(&mdsc->mutex);
2896 schedule_delayed(mdsc);
2897 return;
2898
2899bad_unlock:
2900 mutex_unlock(&mdsc->mutex);
2901bad:
2902 pr_err("error decoding mdsmap %d\n", err);
2903 return;
2904}
2905
2906static struct ceph_connection *con_get(struct ceph_connection *con)
2907{
2908 struct ceph_mds_session *s = con->private;
2909
2910 if (get_session(s)) {
Sage Weil2600d2d2010-02-22 15:12:16 -08002911 dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -07002912 return con;
2913 }
2914 dout("mdsc con_get %p FAIL\n", s);
2915 return NULL;
2916}
2917
2918static void con_put(struct ceph_connection *con)
2919{
2920 struct ceph_mds_session *s = con->private;
2921
Sage Weil2f2dc052009-10-06 11:31:09 -07002922 ceph_put_mds_session(s);
Sage Weil2600d2d2010-02-22 15:12:16 -08002923 dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -07002924}
2925
2926/*
2927 * if the client is unresponsive for long enough, the mds will kill
2928 * the session entirely.
2929 */
2930static void peer_reset(struct ceph_connection *con)
2931{
2932 struct ceph_mds_session *s = con->private;
2933
2934 pr_err("mds%d gave us the boot. IMPLEMENT RECONNECT.\n",
2935 s->s_mds);
2936}
2937
2938static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2939{
2940 struct ceph_mds_session *s = con->private;
2941 struct ceph_mds_client *mdsc = s->s_mdsc;
2942 int type = le16_to_cpu(msg->hdr.type);
2943
Sage Weil2600d2d2010-02-22 15:12:16 -08002944 mutex_lock(&mdsc->mutex);
2945 if (__verify_registered_session(mdsc, s) < 0) {
2946 mutex_unlock(&mdsc->mutex);
2947 goto out;
2948 }
2949 mutex_unlock(&mdsc->mutex);
2950
Sage Weil2f2dc052009-10-06 11:31:09 -07002951 switch (type) {
2952 case CEPH_MSG_MDS_MAP:
2953 ceph_mdsc_handle_map(mdsc, msg);
2954 break;
2955 case CEPH_MSG_CLIENT_SESSION:
2956 handle_session(s, msg);
2957 break;
2958 case CEPH_MSG_CLIENT_REPLY:
2959 handle_reply(s, msg);
2960 break;
2961 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
Sage Weil2600d2d2010-02-22 15:12:16 -08002962 handle_forward(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002963 break;
2964 case CEPH_MSG_CLIENT_CAPS:
2965 ceph_handle_caps(s, msg);
2966 break;
2967 case CEPH_MSG_CLIENT_SNAP:
Sage Weil2600d2d2010-02-22 15:12:16 -08002968 ceph_handle_snap(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002969 break;
2970 case CEPH_MSG_CLIENT_LEASE:
Sage Weil2600d2d2010-02-22 15:12:16 -08002971 handle_lease(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002972 break;
2973
2974 default:
2975 pr_err("received unknown message type %d %s\n", type,
2976 ceph_msg_type_name(type));
2977 }
Sage Weil2600d2d2010-02-22 15:12:16 -08002978out:
Sage Weil2f2dc052009-10-06 11:31:09 -07002979 ceph_msg_put(msg);
2980}
2981
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002982/*
2983 * authentication
2984 */
2985static int get_authorizer(struct ceph_connection *con,
2986 void **buf, int *len, int *proto,
2987 void **reply_buf, int *reply_len, int force_new)
2988{
2989 struct ceph_mds_session *s = con->private;
2990 struct ceph_mds_client *mdsc = s->s_mdsc;
2991 struct ceph_auth_client *ac = mdsc->client->monc.auth;
2992 int ret = 0;
2993
2994 if (force_new && s->s_authorizer) {
2995 ac->ops->destroy_authorizer(ac, s->s_authorizer);
2996 s->s_authorizer = NULL;
2997 }
2998 if (s->s_authorizer == NULL) {
2999 if (ac->ops->create_authorizer) {
3000 ret = ac->ops->create_authorizer(
3001 ac, CEPH_ENTITY_TYPE_MDS,
3002 &s->s_authorizer,
3003 &s->s_authorizer_buf,
3004 &s->s_authorizer_buf_len,
3005 &s->s_authorizer_reply_buf,
3006 &s->s_authorizer_reply_buf_len);
3007 if (ret)
3008 return ret;
3009 }
3010 }
3011
3012 *proto = ac->protocol;
3013 *buf = s->s_authorizer_buf;
3014 *len = s->s_authorizer_buf_len;
3015 *reply_buf = s->s_authorizer_reply_buf;
3016 *reply_len = s->s_authorizer_reply_buf_len;
3017 return 0;
3018}
3019
3020
3021static int verify_authorizer_reply(struct ceph_connection *con, int len)
3022{
3023 struct ceph_mds_session *s = con->private;
3024 struct ceph_mds_client *mdsc = s->s_mdsc;
3025 struct ceph_auth_client *ac = mdsc->client->monc.auth;
3026
3027 return ac->ops->verify_authorizer_reply(ac, s->s_authorizer, len);
3028}
3029
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003030static int invalidate_authorizer(struct ceph_connection *con)
3031{
3032 struct ceph_mds_session *s = con->private;
3033 struct ceph_mds_client *mdsc = s->s_mdsc;
3034 struct ceph_auth_client *ac = mdsc->client->monc.auth;
3035
3036 if (ac->ops->invalidate_authorizer)
3037 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
3038
3039 return ceph_monc_validate_auth(&mdsc->client->monc);
3040}
3041
Sage Weil2f2dc052009-10-06 11:31:09 -07003042const static struct ceph_connection_operations mds_con_ops = {
3043 .get = con_get,
3044 .put = con_put,
3045 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003046 .get_authorizer = get_authorizer,
3047 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003048 .invalidate_authorizer = invalidate_authorizer,
Sage Weil2f2dc052009-10-06 11:31:09 -07003049 .peer_reset = peer_reset,
Sage Weil2f2dc052009-10-06 11:31:09 -07003050};
3051
3052
3053
3054
3055/* eof */