blob: a50fca1e03bea273cc515f316f1f1b6ace1d64f1 [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weil2f2dc052009-10-06 11:31:09 -07002
Sage Weil496e5952010-09-22 19:57:10 -07003#include <linux/fs.h>
Sage Weil2f2dc052009-10-06 11:31:09 -07004#include <linux/wait.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09005#include <linux/slab.h>
Sage Weil2f2dc052009-10-06 11:31:09 -07006#include <linux/sched.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07007#include <linux/debugfs.h>
8#include <linux/seq_file.h>
Sage Weil2f2dc052009-10-06 11:31:09 -07009
Sage Weil2f2dc052009-10-06 11:31:09 -070010#include "super.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070011#include "mds_client.h"
12
13#include <linux/ceph/messenger.h>
14#include <linux/ceph/decode.h>
15#include <linux/ceph/pagelist.h>
16#include <linux/ceph/auth.h>
17#include <linux/ceph/debugfs.h>
Sage Weil2f2dc052009-10-06 11:31:09 -070018
19/*
20 * A cluster of MDS (metadata server) daemons is responsible for
21 * managing the file system namespace (the directory hierarchy and
22 * inodes) and for coordinating shared access to storage. Metadata is
23 * partitioning hierarchically across a number of servers, and that
24 * partition varies over time as the cluster adjusts the distribution
25 * in order to balance load.
26 *
27 * The MDS client is primarily responsible to managing synchronous
28 * metadata requests for operations like open, unlink, and so forth.
29 * If there is a MDS failure, we find out about it when we (possibly
30 * request and) receive a new MDS map, and can resubmit affected
31 * requests.
32 *
33 * For the most part, though, we take advantage of a lossless
34 * communications channel to the MDS, and do not need to worry about
35 * timing out or resubmitting requests.
36 *
37 * We maintain a stateful "session" with each MDS we interact with.
38 * Within each session, we sent periodic heartbeat messages to ensure
39 * any capabilities or leases we have been issues remain valid. If
40 * the session times out and goes stale, our leases and capabilities
41 * are no longer valid.
42 */
43
Sage Weil20cb34a2010-05-12 15:21:32 -070044struct ceph_reconnect_state {
45 struct ceph_pagelist *pagelist;
46 bool flock;
47};
48
Sage Weil2f2dc052009-10-06 11:31:09 -070049static void __wake_requests(struct ceph_mds_client *mdsc,
50 struct list_head *head);
51
Tobias Klauser9e327892010-05-20 10:40:19 +020052static const struct ceph_connection_operations mds_con_ops;
Sage Weil2f2dc052009-10-06 11:31:09 -070053
54
55/*
56 * mds reply parsing
57 */
58
59/*
60 * parse individual inode info
61 */
62static int parse_reply_info_in(void **p, void *end,
63 struct ceph_mds_reply_info_in *info)
64{
65 int err = -EIO;
66
67 info->in = *p;
68 *p += sizeof(struct ceph_mds_reply_inode) +
69 sizeof(*info->in->fragtree.splits) *
70 le32_to_cpu(info->in->fragtree.nsplits);
71
72 ceph_decode_32_safe(p, end, info->symlink_len, bad);
73 ceph_decode_need(p, end, info->symlink_len, bad);
74 info->symlink = *p;
75 *p += info->symlink_len;
76
77 ceph_decode_32_safe(p, end, info->xattr_len, bad);
78 ceph_decode_need(p, end, info->xattr_len, bad);
79 info->xattr_data = *p;
80 *p += info->xattr_len;
81 return 0;
82bad:
83 return err;
84}
85
86/*
87 * parse a normal reply, which may contain a (dir+)dentry and/or a
88 * target inode.
89 */
90static int parse_reply_info_trace(void **p, void *end,
91 struct ceph_mds_reply_info_parsed *info)
92{
93 int err;
94
95 if (info->head->is_dentry) {
96 err = parse_reply_info_in(p, end, &info->diri);
97 if (err < 0)
98 goto out_bad;
99
100 if (unlikely(*p + sizeof(*info->dirfrag) > end))
101 goto bad;
102 info->dirfrag = *p;
103 *p += sizeof(*info->dirfrag) +
104 sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
105 if (unlikely(*p > end))
106 goto bad;
107
108 ceph_decode_32_safe(p, end, info->dname_len, bad);
109 ceph_decode_need(p, end, info->dname_len, bad);
110 info->dname = *p;
111 *p += info->dname_len;
112 info->dlease = *p;
113 *p += sizeof(*info->dlease);
114 }
115
116 if (info->head->is_target) {
117 err = parse_reply_info_in(p, end, &info->targeti);
118 if (err < 0)
119 goto out_bad;
120 }
121
122 if (unlikely(*p != end))
123 goto bad;
124 return 0;
125
126bad:
127 err = -EIO;
128out_bad:
129 pr_err("problem parsing mds trace %d\n", err);
130 return err;
131}
132
133/*
134 * parse readdir results
135 */
136static int parse_reply_info_dir(void **p, void *end,
137 struct ceph_mds_reply_info_parsed *info)
138{
139 u32 num, i = 0;
140 int err;
141
142 info->dir_dir = *p;
143 if (*p + sizeof(*info->dir_dir) > end)
144 goto bad;
145 *p += sizeof(*info->dir_dir) +
146 sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
147 if (*p > end)
148 goto bad;
149
150 ceph_decode_need(p, end, sizeof(num) + 2, bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700151 num = ceph_decode_32(p);
152 info->dir_end = ceph_decode_8(p);
153 info->dir_complete = ceph_decode_8(p);
Sage Weil2f2dc052009-10-06 11:31:09 -0700154 if (num == 0)
155 goto done;
156
157 /* alloc large array */
158 info->dir_nr = num;
159 info->dir_in = kcalloc(num, sizeof(*info->dir_in) +
160 sizeof(*info->dir_dname) +
161 sizeof(*info->dir_dname_len) +
162 sizeof(*info->dir_dlease),
163 GFP_NOFS);
164 if (info->dir_in == NULL) {
165 err = -ENOMEM;
166 goto out_bad;
167 }
168 info->dir_dname = (void *)(info->dir_in + num);
169 info->dir_dname_len = (void *)(info->dir_dname + num);
170 info->dir_dlease = (void *)(info->dir_dname_len + num);
171
172 while (num) {
173 /* dentry */
174 ceph_decode_need(p, end, sizeof(u32)*2, bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700175 info->dir_dname_len[i] = ceph_decode_32(p);
Sage Weil2f2dc052009-10-06 11:31:09 -0700176 ceph_decode_need(p, end, info->dir_dname_len[i], bad);
177 info->dir_dname[i] = *p;
178 *p += info->dir_dname_len[i];
179 dout("parsed dir dname '%.*s'\n", info->dir_dname_len[i],
180 info->dir_dname[i]);
181 info->dir_dlease[i] = *p;
182 *p += sizeof(struct ceph_mds_reply_lease);
183
184 /* inode */
185 err = parse_reply_info_in(p, end, &info->dir_in[i]);
186 if (err < 0)
187 goto out_bad;
188 i++;
189 num--;
190 }
191
192done:
193 if (*p != end)
194 goto bad;
195 return 0;
196
197bad:
198 err = -EIO;
199out_bad:
200 pr_err("problem parsing dir contents %d\n", err);
201 return err;
202}
203
204/*
Herb Shiu25933ab2010-12-01 14:14:38 -0800205 * parse fcntl F_GETLK results
206 */
207static int parse_reply_info_filelock(void **p, void *end,
208 struct ceph_mds_reply_info_parsed *info)
209{
210 if (*p + sizeof(*info->filelock_reply) > end)
211 goto bad;
212
213 info->filelock_reply = *p;
214 *p += sizeof(*info->filelock_reply);
215
216 if (unlikely(*p != end))
217 goto bad;
218 return 0;
219
220bad:
221 return -EIO;
222}
223
224/*
225 * parse extra results
226 */
227static int parse_reply_info_extra(void **p, void *end,
228 struct ceph_mds_reply_info_parsed *info)
229{
230 if (info->head->op == CEPH_MDS_OP_GETFILELOCK)
231 return parse_reply_info_filelock(p, end, info);
232 else
233 return parse_reply_info_dir(p, end, info);
234}
235
236/*
Sage Weil2f2dc052009-10-06 11:31:09 -0700237 * parse entire mds reply
238 */
239static int parse_reply_info(struct ceph_msg *msg,
240 struct ceph_mds_reply_info_parsed *info)
241{
242 void *p, *end;
243 u32 len;
244 int err;
245
246 info->head = msg->front.iov_base;
247 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
248 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
249
250 /* trace */
251 ceph_decode_32_safe(&p, end, len, bad);
252 if (len > 0) {
253 err = parse_reply_info_trace(&p, p+len, info);
254 if (err < 0)
255 goto out_bad;
256 }
257
Herb Shiu25933ab2010-12-01 14:14:38 -0800258 /* extra */
Sage Weil2f2dc052009-10-06 11:31:09 -0700259 ceph_decode_32_safe(&p, end, len, bad);
260 if (len > 0) {
Herb Shiu25933ab2010-12-01 14:14:38 -0800261 err = parse_reply_info_extra(&p, p+len, info);
Sage Weil2f2dc052009-10-06 11:31:09 -0700262 if (err < 0)
263 goto out_bad;
264 }
265
266 /* snap blob */
267 ceph_decode_32_safe(&p, end, len, bad);
268 info->snapblob_len = len;
269 info->snapblob = p;
270 p += len;
271
272 if (p != end)
273 goto bad;
274 return 0;
275
276bad:
277 err = -EIO;
278out_bad:
279 pr_err("mds parse_reply err %d\n", err);
280 return err;
281}
282
283static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
284{
285 kfree(info->dir_in);
286}
287
288
289/*
290 * sessions
291 */
292static const char *session_state_name(int s)
293{
294 switch (s) {
295 case CEPH_MDS_SESSION_NEW: return "new";
296 case CEPH_MDS_SESSION_OPENING: return "opening";
297 case CEPH_MDS_SESSION_OPEN: return "open";
298 case CEPH_MDS_SESSION_HUNG: return "hung";
299 case CEPH_MDS_SESSION_CLOSING: return "closing";
Sage Weil44ca18f2010-02-15 12:08:46 -0800300 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
Sage Weil2f2dc052009-10-06 11:31:09 -0700301 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
302 default: return "???";
303 }
304}
305
306static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
307{
308 if (atomic_inc_not_zero(&s->s_ref)) {
309 dout("mdsc get_session %p %d -> %d\n", s,
310 atomic_read(&s->s_ref)-1, atomic_read(&s->s_ref));
311 return s;
312 } else {
313 dout("mdsc get_session %p 0 -- FAIL", s);
314 return NULL;
315 }
316}
317
318void ceph_put_mds_session(struct ceph_mds_session *s)
319{
320 dout("mdsc put_session %p %d -> %d\n", s,
321 atomic_read(&s->s_ref), atomic_read(&s->s_ref)-1);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800322 if (atomic_dec_and_test(&s->s_ref)) {
323 if (s->s_authorizer)
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700324 s->s_mdsc->fsc->client->monc.auth->ops->destroy_authorizer(
325 s->s_mdsc->fsc->client->monc.auth,
326 s->s_authorizer);
Sage Weil2f2dc052009-10-06 11:31:09 -0700327 kfree(s);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800328 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700329}
330
331/*
332 * called under mdsc->mutex
333 */
334struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
335 int mds)
336{
337 struct ceph_mds_session *session;
338
339 if (mds >= mdsc->max_sessions || mdsc->sessions[mds] == NULL)
340 return NULL;
341 session = mdsc->sessions[mds];
342 dout("lookup_mds_session %p %d\n", session,
343 atomic_read(&session->s_ref));
344 get_session(session);
345 return session;
346}
347
348static bool __have_session(struct ceph_mds_client *mdsc, int mds)
349{
350 if (mds >= mdsc->max_sessions)
351 return false;
352 return mdsc->sessions[mds];
353}
354
Sage Weil2600d2d2010-02-22 15:12:16 -0800355static int __verify_registered_session(struct ceph_mds_client *mdsc,
356 struct ceph_mds_session *s)
357{
358 if (s->s_mds >= mdsc->max_sessions ||
359 mdsc->sessions[s->s_mds] != s)
360 return -ENOENT;
361 return 0;
362}
363
Sage Weil2f2dc052009-10-06 11:31:09 -0700364/*
365 * create+register a new session for given mds.
366 * called under mdsc->mutex.
367 */
368static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
369 int mds)
370{
371 struct ceph_mds_session *s;
372
373 s = kzalloc(sizeof(*s), GFP_NOFS);
Dan Carpenter4736b002010-03-20 15:30:16 +0300374 if (!s)
375 return ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -0700376 s->s_mdsc = mdsc;
377 s->s_mds = mds;
378 s->s_state = CEPH_MDS_SESSION_NEW;
379 s->s_ttl = 0;
380 s->s_seq = 0;
381 mutex_init(&s->s_mutex);
382
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700383 ceph_con_init(mdsc->fsc->client->msgr, &s->s_con);
Sage Weil2f2dc052009-10-06 11:31:09 -0700384 s->s_con.private = s;
385 s->s_con.ops = &mds_con_ops;
386 s->s_con.peer_name.type = CEPH_ENTITY_TYPE_MDS;
387 s->s_con.peer_name.num = cpu_to_le64(mds);
Sage Weil2f2dc052009-10-06 11:31:09 -0700388
389 spin_lock_init(&s->s_cap_lock);
390 s->s_cap_gen = 0;
391 s->s_cap_ttl = 0;
392 s->s_renew_requested = 0;
393 s->s_renew_seq = 0;
394 INIT_LIST_HEAD(&s->s_caps);
395 s->s_nr_caps = 0;
Sage Weil5dacf092009-12-21 20:40:34 -0800396 s->s_trim_caps = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -0700397 atomic_set(&s->s_ref, 1);
398 INIT_LIST_HEAD(&s->s_waiting);
399 INIT_LIST_HEAD(&s->s_unsafe);
400 s->s_num_cap_releases = 0;
Sage Weil7c1332b2010-02-16 11:39:45 -0800401 s->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700402 INIT_LIST_HEAD(&s->s_cap_releases);
403 INIT_LIST_HEAD(&s->s_cap_releases_done);
404 INIT_LIST_HEAD(&s->s_cap_flushing);
405 INIT_LIST_HEAD(&s->s_cap_snaps_flushing);
406
407 dout("register_session mds%d\n", mds);
408 if (mds >= mdsc->max_sessions) {
409 int newmax = 1 << get_count_order(mds+1);
410 struct ceph_mds_session **sa;
411
412 dout("register_session realloc to %d\n", newmax);
413 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
414 if (sa == NULL)
Sage Weil42ce56e2009-11-18 11:22:36 -0800415 goto fail_realloc;
Sage Weil2f2dc052009-10-06 11:31:09 -0700416 if (mdsc->sessions) {
417 memcpy(sa, mdsc->sessions,
418 mdsc->max_sessions * sizeof(void *));
419 kfree(mdsc->sessions);
420 }
421 mdsc->sessions = sa;
422 mdsc->max_sessions = newmax;
423 }
424 mdsc->sessions[mds] = s;
425 atomic_inc(&s->s_ref); /* one ref to sessions[], one to caller */
Sage Weil42ce56e2009-11-18 11:22:36 -0800426
427 ceph_con_open(&s->s_con, ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
428
Sage Weil2f2dc052009-10-06 11:31:09 -0700429 return s;
Sage Weil42ce56e2009-11-18 11:22:36 -0800430
431fail_realloc:
432 kfree(s);
433 return ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -0700434}
435
436/*
437 * called under mdsc->mutex
438 */
Sage Weil2600d2d2010-02-22 15:12:16 -0800439static void __unregister_session(struct ceph_mds_client *mdsc,
Sage Weil42ce56e2009-11-18 11:22:36 -0800440 struct ceph_mds_session *s)
Sage Weil2f2dc052009-10-06 11:31:09 -0700441{
Sage Weil2600d2d2010-02-22 15:12:16 -0800442 dout("__unregister_session mds%d %p\n", s->s_mds, s);
443 BUG_ON(mdsc->sessions[s->s_mds] != s);
Sage Weil42ce56e2009-11-18 11:22:36 -0800444 mdsc->sessions[s->s_mds] = NULL;
445 ceph_con_close(&s->s_con);
446 ceph_put_mds_session(s);
Sage Weil2f2dc052009-10-06 11:31:09 -0700447}
448
449/*
450 * drop session refs in request.
451 *
452 * should be last request ref, or hold mdsc->mutex
453 */
454static void put_request_session(struct ceph_mds_request *req)
455{
456 if (req->r_session) {
457 ceph_put_mds_session(req->r_session);
458 req->r_session = NULL;
459 }
460}
461
Sage Weil153c8e62009-12-07 12:31:09 -0800462void ceph_mdsc_release_request(struct kref *kref)
Sage Weil2f2dc052009-10-06 11:31:09 -0700463{
Sage Weil153c8e62009-12-07 12:31:09 -0800464 struct ceph_mds_request *req = container_of(kref,
465 struct ceph_mds_request,
466 r_kref);
467 if (req->r_request)
468 ceph_msg_put(req->r_request);
469 if (req->r_reply) {
470 ceph_msg_put(req->r_reply);
471 destroy_reply_info(&req->r_reply_info);
Sage Weil2f2dc052009-10-06 11:31:09 -0700472 }
Sage Weil153c8e62009-12-07 12:31:09 -0800473 if (req->r_inode) {
474 ceph_put_cap_refs(ceph_inode(req->r_inode),
475 CEPH_CAP_PIN);
476 iput(req->r_inode);
477 }
478 if (req->r_locked_dir)
479 ceph_put_cap_refs(ceph_inode(req->r_locked_dir),
480 CEPH_CAP_PIN);
481 if (req->r_target_inode)
482 iput(req->r_target_inode);
483 if (req->r_dentry)
484 dput(req->r_dentry);
485 if (req->r_old_dentry) {
486 ceph_put_cap_refs(
487 ceph_inode(req->r_old_dentry->d_parent->d_inode),
488 CEPH_CAP_PIN);
489 dput(req->r_old_dentry);
490 }
491 kfree(req->r_path1);
492 kfree(req->r_path2);
493 put_request_session(req);
Yehuda Sadeh37151662010-06-17 16:16:12 -0700494 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
Sage Weil153c8e62009-12-07 12:31:09 -0800495 kfree(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700496}
497
498/*
499 * lookup session, bump ref if found.
500 *
501 * called under mdsc->mutex.
502 */
503static struct ceph_mds_request *__lookup_request(struct ceph_mds_client *mdsc,
504 u64 tid)
505{
506 struct ceph_mds_request *req;
Sage Weil44ca18f2010-02-15 12:08:46 -0800507 struct rb_node *n = mdsc->request_tree.rb_node;
508
509 while (n) {
510 req = rb_entry(n, struct ceph_mds_request, r_node);
511 if (tid < req->r_tid)
512 n = n->rb_left;
513 else if (tid > req->r_tid)
514 n = n->rb_right;
515 else {
516 ceph_mdsc_get_request(req);
517 return req;
518 }
519 }
520 return NULL;
521}
522
523static void __insert_request(struct ceph_mds_client *mdsc,
524 struct ceph_mds_request *new)
525{
526 struct rb_node **p = &mdsc->request_tree.rb_node;
527 struct rb_node *parent = NULL;
528 struct ceph_mds_request *req = NULL;
529
530 while (*p) {
531 parent = *p;
532 req = rb_entry(parent, struct ceph_mds_request, r_node);
533 if (new->r_tid < req->r_tid)
534 p = &(*p)->rb_left;
535 else if (new->r_tid > req->r_tid)
536 p = &(*p)->rb_right;
537 else
538 BUG();
539 }
540
541 rb_link_node(&new->r_node, parent, p);
542 rb_insert_color(&new->r_node, &mdsc->request_tree);
Sage Weil2f2dc052009-10-06 11:31:09 -0700543}
544
545/*
546 * Register an in-flight request, and assign a tid. Link to directory
547 * are modifying (if any).
548 *
549 * Called under mdsc->mutex.
550 */
551static void __register_request(struct ceph_mds_client *mdsc,
552 struct ceph_mds_request *req,
553 struct inode *dir)
554{
555 req->r_tid = ++mdsc->last_tid;
556 if (req->r_num_caps)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700557 ceph_reserve_caps(mdsc, &req->r_caps_reservation,
558 req->r_num_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -0700559 dout("__register_request %p tid %lld\n", req, req->r_tid);
560 ceph_mdsc_get_request(req);
Sage Weil44ca18f2010-02-15 12:08:46 -0800561 __insert_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700562
Sage Weilcb4276c2010-11-08 07:28:52 -0800563 req->r_uid = current_fsuid();
564 req->r_gid = current_fsgid();
565
Sage Weil2f2dc052009-10-06 11:31:09 -0700566 if (dir) {
567 struct ceph_inode_info *ci = ceph_inode(dir);
568
569 spin_lock(&ci->i_unsafe_lock);
570 req->r_unsafe_dir = dir;
571 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
572 spin_unlock(&ci->i_unsafe_lock);
573 }
574}
575
576static void __unregister_request(struct ceph_mds_client *mdsc,
577 struct ceph_mds_request *req)
578{
579 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
Sage Weil44ca18f2010-02-15 12:08:46 -0800580 rb_erase(&req->r_node, &mdsc->request_tree);
Sage Weil80fc7312010-03-16 15:28:54 -0700581 RB_CLEAR_NODE(&req->r_node);
Sage Weil2f2dc052009-10-06 11:31:09 -0700582
583 if (req->r_unsafe_dir) {
584 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
585
586 spin_lock(&ci->i_unsafe_lock);
587 list_del_init(&req->r_unsafe_dir_item);
588 spin_unlock(&ci->i_unsafe_lock);
589 }
Sage Weil94aa8ae2010-03-28 21:22:50 -0700590
591 ceph_mdsc_put_request(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700592}
593
594/*
595 * Choose mds to send request to next. If there is a hint set in the
596 * request (e.g., due to a prior forward hint from the mds), use that.
597 * Otherwise, consult frag tree and/or caps to identify the
598 * appropriate mds. If all else fails, choose randomly.
599 *
600 * Called under mdsc->mutex.
601 */
Sage Weileb6bb1c2010-08-16 09:21:27 -0700602struct dentry *get_nonsnap_parent(struct dentry *dentry)
603{
604 while (!IS_ROOT(dentry) && ceph_snap(dentry->d_inode) != CEPH_NOSNAP)
605 dentry = dentry->d_parent;
606 return dentry;
607}
608
Sage Weil2f2dc052009-10-06 11:31:09 -0700609static int __choose_mds(struct ceph_mds_client *mdsc,
610 struct ceph_mds_request *req)
611{
612 struct inode *inode;
613 struct ceph_inode_info *ci;
614 struct ceph_cap *cap;
615 int mode = req->r_direct_mode;
616 int mds = -1;
617 u32 hash = req->r_direct_hash;
618 bool is_hash = req->r_direct_is_hash;
619
620 /*
621 * is there a specific mds we should try? ignore hint if we have
622 * no session and the mds is not up (active or recovering).
623 */
624 if (req->r_resend_mds >= 0 &&
625 (__have_session(mdsc, req->r_resend_mds) ||
626 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
627 dout("choose_mds using resend_mds mds%d\n",
628 req->r_resend_mds);
629 return req->r_resend_mds;
630 }
631
632 if (mode == USE_RANDOM_MDS)
633 goto random;
634
635 inode = NULL;
636 if (req->r_inode) {
637 inode = req->r_inode;
638 } else if (req->r_dentry) {
Sage Weileb6bb1c2010-08-16 09:21:27 -0700639 struct inode *dir = req->r_dentry->d_parent->d_inode;
640
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700641 if (dir->i_sb != mdsc->fsc->sb) {
Sage Weileb6bb1c2010-08-16 09:21:27 -0700642 /* not this fs! */
643 inode = req->r_dentry->d_inode;
644 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
645 /* direct snapped/virtual snapdir requests
646 * based on parent dir inode */
647 struct dentry *dn =
648 get_nonsnap_parent(req->r_dentry->d_parent);
649 inode = dn->d_inode;
650 dout("__choose_mds using nonsnap parent %p\n", inode);
651 } else if (req->r_dentry->d_inode) {
652 /* dentry target */
Sage Weil2f2dc052009-10-06 11:31:09 -0700653 inode = req->r_dentry->d_inode;
654 } else {
Sage Weileb6bb1c2010-08-16 09:21:27 -0700655 /* dir + name */
656 inode = dir;
Sage Weil2f2dc052009-10-06 11:31:09 -0700657 hash = req->r_dentry->d_name.hash;
658 is_hash = true;
659 }
660 }
Sage Weileb6bb1c2010-08-16 09:21:27 -0700661
Sage Weil2f2dc052009-10-06 11:31:09 -0700662 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
663 (int)hash, mode);
664 if (!inode)
665 goto random;
666 ci = ceph_inode(inode);
667
668 if (is_hash && S_ISDIR(inode->i_mode)) {
669 struct ceph_inode_frag frag;
670 int found;
671
672 ceph_choose_frag(ci, hash, &frag, &found);
673 if (found) {
674 if (mode == USE_ANY_MDS && frag.ndist > 0) {
675 u8 r;
676
677 /* choose a random replica */
678 get_random_bytes(&r, 1);
679 r %= frag.ndist;
680 mds = frag.dist[r];
681 dout("choose_mds %p %llx.%llx "
682 "frag %u mds%d (%d/%d)\n",
683 inode, ceph_vinop(inode),
684 frag.frag, frag.mds,
685 (int)r, frag.ndist);
686 return mds;
687 }
688
689 /* since this file/dir wasn't known to be
690 * replicated, then we want to look for the
691 * authoritative mds. */
692 mode = USE_AUTH_MDS;
693 if (frag.mds >= 0) {
694 /* choose auth mds */
695 mds = frag.mds;
696 dout("choose_mds %p %llx.%llx "
697 "frag %u mds%d (auth)\n",
698 inode, ceph_vinop(inode), frag.frag, mds);
699 return mds;
700 }
701 }
702 }
703
704 spin_lock(&inode->i_lock);
705 cap = NULL;
706 if (mode == USE_AUTH_MDS)
707 cap = ci->i_auth_cap;
708 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
709 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
710 if (!cap) {
711 spin_unlock(&inode->i_lock);
712 goto random;
713 }
714 mds = cap->session->s_mds;
715 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
716 inode, ceph_vinop(inode), mds,
717 cap == ci->i_auth_cap ? "auth " : "", cap);
718 spin_unlock(&inode->i_lock);
719 return mds;
720
721random:
722 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
723 dout("choose_mds chose random mds%d\n", mds);
724 return mds;
725}
726
727
728/*
729 * session messages
730 */
731static struct ceph_msg *create_session_msg(u32 op, u64 seq)
732{
733 struct ceph_msg *msg;
734 struct ceph_mds_session_head *h;
735
Yehuda Sadeh34d23762010-04-06 14:33:58 -0700736 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -0700737 if (!msg) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700738 pr_err("create_session_msg ENOMEM creating msg\n");
Sage Weila79832f2010-04-01 16:06:19 -0700739 return NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700740 }
741 h = msg->front.iov_base;
742 h->op = cpu_to_le32(op);
743 h->seq = cpu_to_le64(seq);
744 return msg;
745}
746
747/*
748 * send session open request.
749 *
750 * called under mdsc->mutex
751 */
752static int __open_session(struct ceph_mds_client *mdsc,
753 struct ceph_mds_session *session)
754{
755 struct ceph_msg *msg;
756 int mstate;
757 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -0700758
759 /* wait for mds to go active? */
760 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
761 dout("open_session to mds%d (%s)\n", mds,
762 ceph_mds_state_name(mstate));
763 session->s_state = CEPH_MDS_SESSION_OPENING;
764 session->s_renew_requested = jiffies;
765
766 /* send connect message */
767 msg = create_session_msg(CEPH_SESSION_REQUEST_OPEN, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -0700768 if (!msg)
769 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -0700770 ceph_con_send(&session->s_con, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -0700771 return 0;
772}
773
774/*
Sage Weiled0552a2010-06-21 13:38:25 -0700775 * open sessions for any export targets for the given mds
776 *
777 * called under mdsc->mutex
778 */
779static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
780 struct ceph_mds_session *session)
781{
782 struct ceph_mds_info *mi;
783 struct ceph_mds_session *ts;
784 int i, mds = session->s_mds;
785 int target;
786
787 if (mds >= mdsc->mdsmap->m_max_mds)
788 return;
789 mi = &mdsc->mdsmap->m_info[mds];
790 dout("open_export_target_sessions for mds%d (%d targets)\n",
791 session->s_mds, mi->num_export_targets);
792
793 for (i = 0; i < mi->num_export_targets; i++) {
794 target = mi->export_targets[i];
795 ts = __ceph_lookup_mds_session(mdsc, target);
796 if (!ts) {
797 ts = register_session(mdsc, target);
798 if (IS_ERR(ts))
799 return;
800 }
801 if (session->s_state == CEPH_MDS_SESSION_NEW ||
802 session->s_state == CEPH_MDS_SESSION_CLOSING)
803 __open_session(mdsc, session);
804 else
805 dout(" mds%d target mds%d %p is %s\n", session->s_mds,
806 i, ts, session_state_name(ts->s_state));
807 ceph_put_mds_session(ts);
808 }
809}
810
Sage Weil154f42c2010-06-21 13:45:04 -0700811void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
812 struct ceph_mds_session *session)
813{
814 mutex_lock(&mdsc->mutex);
815 __open_export_target_sessions(mdsc, session);
816 mutex_unlock(&mdsc->mutex);
817}
818
Sage Weiled0552a2010-06-21 13:38:25 -0700819/*
Sage Weil2f2dc052009-10-06 11:31:09 -0700820 * session caps
821 */
822
823/*
824 * Free preallocated cap messages assigned to this session
825 */
826static void cleanup_cap_releases(struct ceph_mds_session *session)
827{
828 struct ceph_msg *msg;
829
830 spin_lock(&session->s_cap_lock);
831 while (!list_empty(&session->s_cap_releases)) {
832 msg = list_first_entry(&session->s_cap_releases,
833 struct ceph_msg, list_head);
834 list_del_init(&msg->list_head);
835 ceph_msg_put(msg);
836 }
837 while (!list_empty(&session->s_cap_releases_done)) {
838 msg = list_first_entry(&session->s_cap_releases_done,
839 struct ceph_msg, list_head);
840 list_del_init(&msg->list_head);
841 ceph_msg_put(msg);
842 }
843 spin_unlock(&session->s_cap_lock);
844}
845
846/*
Sage Weilf818a732010-05-11 20:56:31 -0700847 * Helper to safely iterate over all caps associated with a session, with
848 * special care taken to handle a racing __ceph_remove_cap().
Sage Weil2f2dc052009-10-06 11:31:09 -0700849 *
Sage Weilf818a732010-05-11 20:56:31 -0700850 * Caller must hold session s_mutex.
Sage Weil2f2dc052009-10-06 11:31:09 -0700851 */
852static int iterate_session_caps(struct ceph_mds_session *session,
853 int (*cb)(struct inode *, struct ceph_cap *,
854 void *), void *arg)
855{
Sage Weil7c1332b2010-02-16 11:39:45 -0800856 struct list_head *p;
857 struct ceph_cap *cap;
858 struct inode *inode, *last_inode = NULL;
859 struct ceph_cap *old_cap = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700860 int ret;
861
862 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
863 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800864 p = session->s_caps.next;
865 while (p != &session->s_caps) {
866 cap = list_entry(p, struct ceph_cap, session_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -0700867 inode = igrab(&cap->ci->vfs_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -0800868 if (!inode) {
869 p = p->next;
Sage Weil2f2dc052009-10-06 11:31:09 -0700870 continue;
Sage Weil7c1332b2010-02-16 11:39:45 -0800871 }
872 session->s_cap_iterator = cap;
Sage Weil2f2dc052009-10-06 11:31:09 -0700873 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800874
875 if (last_inode) {
876 iput(last_inode);
877 last_inode = NULL;
878 }
879 if (old_cap) {
Yehuda Sadeh37151662010-06-17 16:16:12 -0700880 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -0800881 old_cap = NULL;
882 }
883
Sage Weil2f2dc052009-10-06 11:31:09 -0700884 ret = cb(inode, cap, arg);
Sage Weil7c1332b2010-02-16 11:39:45 -0800885 last_inode = inode;
886
Sage Weil2f2dc052009-10-06 11:31:09 -0700887 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800888 p = p->next;
889 if (cap->ci == NULL) {
890 dout("iterate_session_caps finishing cap %p removal\n",
891 cap);
892 BUG_ON(cap->session != session);
893 list_del_init(&cap->session_caps);
894 session->s_nr_caps--;
895 cap->session = NULL;
896 old_cap = cap; /* put_cap it w/o locks held */
897 }
Sage Weil5dacf092009-12-21 20:40:34 -0800898 if (ret < 0)
899 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -0700900 }
Sage Weil5dacf092009-12-21 20:40:34 -0800901 ret = 0;
902out:
Sage Weil7c1332b2010-02-16 11:39:45 -0800903 session->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700904 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800905
906 if (last_inode)
907 iput(last_inode);
908 if (old_cap)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700909 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -0800910
Sage Weil5dacf092009-12-21 20:40:34 -0800911 return ret;
Sage Weil2f2dc052009-10-06 11:31:09 -0700912}
913
914static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
Sage Weil6c99f252010-05-10 16:12:25 -0700915 void *arg)
Sage Weil2f2dc052009-10-06 11:31:09 -0700916{
917 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil6c99f252010-05-10 16:12:25 -0700918 int drop = 0;
919
Sage Weil2f2dc052009-10-06 11:31:09 -0700920 dout("removing cap %p, ci is %p, inode is %p\n",
921 cap, ci, &ci->vfs_inode);
Sage Weil6c99f252010-05-10 16:12:25 -0700922 spin_lock(&inode->i_lock);
923 __ceph_remove_cap(cap);
924 if (!__ceph_is_any_real_caps(ci)) {
925 struct ceph_mds_client *mdsc =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700926 ceph_sb_to_client(inode->i_sb)->mdsc;
Sage Weil6c99f252010-05-10 16:12:25 -0700927
928 spin_lock(&mdsc->cap_dirty_lock);
929 if (!list_empty(&ci->i_dirty_item)) {
930 pr_info(" dropping dirty %s state for %p %lld\n",
931 ceph_cap_string(ci->i_dirty_caps),
932 inode, ceph_ino(inode));
933 ci->i_dirty_caps = 0;
934 list_del_init(&ci->i_dirty_item);
935 drop = 1;
936 }
937 if (!list_empty(&ci->i_flushing_item)) {
938 pr_info(" dropping dirty+flushing %s state for %p %lld\n",
939 ceph_cap_string(ci->i_flushing_caps),
940 inode, ceph_ino(inode));
941 ci->i_flushing_caps = 0;
942 list_del_init(&ci->i_flushing_item);
943 mdsc->num_cap_flushing--;
944 drop = 1;
945 }
946 if (drop && ci->i_wrbuffer_ref) {
947 pr_info(" dropping dirty data for %p %lld\n",
948 inode, ceph_ino(inode));
949 ci->i_wrbuffer_ref = 0;
950 ci->i_wrbuffer_ref_head = 0;
951 drop++;
952 }
953 spin_unlock(&mdsc->cap_dirty_lock);
954 }
955 spin_unlock(&inode->i_lock);
956 while (drop--)
957 iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -0700958 return 0;
959}
960
961/*
962 * caller must hold session s_mutex
963 */
964static void remove_session_caps(struct ceph_mds_session *session)
965{
966 dout("remove_session_caps on %p\n", session);
967 iterate_session_caps(session, remove_session_caps_cb, NULL);
968 BUG_ON(session->s_nr_caps > 0);
Sage Weil6c99f252010-05-10 16:12:25 -0700969 BUG_ON(!list_empty(&session->s_cap_flushing));
Sage Weil2f2dc052009-10-06 11:31:09 -0700970 cleanup_cap_releases(session);
971}
972
973/*
974 * wake up any threads waiting on this session's caps. if the cap is
975 * old (didn't get renewed on the client reconnect), remove it now.
976 *
977 * caller must hold s_mutex.
978 */
979static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
980 void *arg)
981{
Sage Weil0dc25702009-11-20 13:43:45 -0800982 struct ceph_inode_info *ci = ceph_inode(inode);
983
Yehuda Sadeh03066f22010-07-27 13:11:08 -0700984 wake_up_all(&ci->i_cap_wq);
Sage Weil0dc25702009-11-20 13:43:45 -0800985 if (arg) {
986 spin_lock(&inode->i_lock);
987 ci->i_wanted_max_size = 0;
988 ci->i_requested_max_size = 0;
989 spin_unlock(&inode->i_lock);
990 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700991 return 0;
992}
993
Sage Weil0dc25702009-11-20 13:43:45 -0800994static void wake_up_session_caps(struct ceph_mds_session *session,
995 int reconnect)
Sage Weil2f2dc052009-10-06 11:31:09 -0700996{
997 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
Sage Weil0dc25702009-11-20 13:43:45 -0800998 iterate_session_caps(session, wake_up_session_cb,
999 (void *)(unsigned long)reconnect);
Sage Weil2f2dc052009-10-06 11:31:09 -07001000}
1001
1002/*
1003 * Send periodic message to MDS renewing all currently held caps. The
1004 * ack will reset the expiration for all caps from this session.
1005 *
1006 * caller holds s_mutex
1007 */
1008static int send_renew_caps(struct ceph_mds_client *mdsc,
1009 struct ceph_mds_session *session)
1010{
1011 struct ceph_msg *msg;
1012 int state;
1013
1014 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1015 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1016 pr_info("mds%d caps stale\n", session->s_mds);
Sage Weile4cb4cb2010-03-18 13:43:09 -07001017 session->s_renew_requested = jiffies;
Sage Weil2f2dc052009-10-06 11:31:09 -07001018
1019 /* do not try to renew caps until a recovering mds has reconnected
1020 * with its clients. */
1021 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1022 if (state < CEPH_MDS_STATE_RECONNECT) {
1023 dout("send_renew_caps ignoring mds%d (%s)\n",
1024 session->s_mds, ceph_mds_state_name(state));
1025 return 0;
1026 }
1027
1028 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1029 ceph_mds_state_name(state));
Sage Weil2f2dc052009-10-06 11:31:09 -07001030 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1031 ++session->s_renew_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001032 if (!msg)
1033 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -07001034 ceph_con_send(&session->s_con, msg);
1035 return 0;
1036}
1037
1038/*
1039 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
Sage Weil0dc25702009-11-20 13:43:45 -08001040 *
1041 * Called under session->s_mutex
Sage Weil2f2dc052009-10-06 11:31:09 -07001042 */
1043static void renewed_caps(struct ceph_mds_client *mdsc,
1044 struct ceph_mds_session *session, int is_renew)
1045{
1046 int was_stale;
1047 int wake = 0;
1048
1049 spin_lock(&session->s_cap_lock);
1050 was_stale = is_renew && (session->s_cap_ttl == 0 ||
1051 time_after_eq(jiffies, session->s_cap_ttl));
1052
1053 session->s_cap_ttl = session->s_renew_requested +
1054 mdsc->mdsmap->m_session_timeout*HZ;
1055
1056 if (was_stale) {
1057 if (time_before(jiffies, session->s_cap_ttl)) {
1058 pr_info("mds%d caps renewed\n", session->s_mds);
1059 wake = 1;
1060 } else {
1061 pr_info("mds%d caps still stale\n", session->s_mds);
1062 }
1063 }
1064 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1065 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1066 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1067 spin_unlock(&session->s_cap_lock);
1068
1069 if (wake)
Sage Weil0dc25702009-11-20 13:43:45 -08001070 wake_up_session_caps(session, 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07001071}
1072
1073/*
1074 * send a session close request
1075 */
1076static int request_close_session(struct ceph_mds_client *mdsc,
1077 struct ceph_mds_session *session)
1078{
1079 struct ceph_msg *msg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001080
1081 dout("request_close_session mds%d state %s seq %lld\n",
1082 session->s_mds, session_state_name(session->s_state),
1083 session->s_seq);
1084 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001085 if (!msg)
1086 return -ENOMEM;
1087 ceph_con_send(&session->s_con, msg);
1088 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001089}
1090
1091/*
1092 * Called with s_mutex held.
1093 */
1094static int __close_session(struct ceph_mds_client *mdsc,
1095 struct ceph_mds_session *session)
1096{
1097 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1098 return 0;
1099 session->s_state = CEPH_MDS_SESSION_CLOSING;
1100 return request_close_session(mdsc, session);
1101}
1102
1103/*
1104 * Trim old(er) caps.
1105 *
1106 * Because we can't cache an inode without one or more caps, we do
1107 * this indirectly: if a cap is unused, we prune its aliases, at which
1108 * point the inode will hopefully get dropped to.
1109 *
1110 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1111 * memory pressure from the MDS, though, so it needn't be perfect.
1112 */
1113static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1114{
1115 struct ceph_mds_session *session = arg;
1116 struct ceph_inode_info *ci = ceph_inode(inode);
1117 int used, oissued, mine;
1118
1119 if (session->s_trim_caps <= 0)
1120 return -1;
1121
1122 spin_lock(&inode->i_lock);
1123 mine = cap->issued | cap->implemented;
1124 used = __ceph_caps_used(ci);
1125 oissued = __ceph_caps_issued_other(ci, cap);
1126
1127 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n",
1128 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
1129 ceph_cap_string(used));
1130 if (ci->i_dirty_caps)
1131 goto out; /* dirty caps */
1132 if ((used & ~oissued) & mine)
1133 goto out; /* we need these caps */
1134
1135 session->s_trim_caps--;
1136 if (oissued) {
1137 /* we aren't the only cap.. just remove us */
Sage Weil7c1332b2010-02-16 11:39:45 -08001138 __ceph_remove_cap(cap);
Sage Weil2f2dc052009-10-06 11:31:09 -07001139 } else {
1140 /* try to drop referring dentries */
1141 spin_unlock(&inode->i_lock);
1142 d_prune_aliases(inode);
1143 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1144 inode, cap, atomic_read(&inode->i_count));
1145 return 0;
1146 }
1147
1148out:
1149 spin_unlock(&inode->i_lock);
1150 return 0;
1151}
1152
1153/*
1154 * Trim session cap count down to some max number.
1155 */
1156static int trim_caps(struct ceph_mds_client *mdsc,
1157 struct ceph_mds_session *session,
1158 int max_caps)
1159{
1160 int trim_caps = session->s_nr_caps - max_caps;
1161
1162 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1163 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1164 if (trim_caps > 0) {
1165 session->s_trim_caps = trim_caps;
1166 iterate_session_caps(session, trim_caps_cb, session);
1167 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1168 session->s_mds, session->s_nr_caps, max_caps,
1169 trim_caps - session->s_trim_caps);
Sage Weil5dacf092009-12-21 20:40:34 -08001170 session->s_trim_caps = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001171 }
1172 return 0;
1173}
1174
1175/*
1176 * Allocate cap_release messages. If there is a partially full message
1177 * in the queue, try to allocate enough to cover it's remainder, so that
1178 * we can send it immediately.
1179 *
1180 * Called under s_mutex.
1181 */
Sage Weil2b2300d2010-06-09 16:52:04 -07001182int ceph_add_cap_releases(struct ceph_mds_client *mdsc,
Sage Weilee6b2722010-06-10 12:55:52 -07001183 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001184{
Sage Weil38e8883e2010-06-10 12:57:11 -07001185 struct ceph_msg *msg, *partial = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001186 struct ceph_mds_cap_release *head;
1187 int err = -ENOMEM;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001188 int extra = mdsc->fsc->mount_options->cap_release_safety;
Sage Weil38e8883e2010-06-10 12:57:11 -07001189 int num;
Sage Weil2f2dc052009-10-06 11:31:09 -07001190
Sage Weil38e8883e2010-06-10 12:57:11 -07001191 dout("add_cap_releases %p mds%d extra %d\n", session, session->s_mds,
1192 extra);
Sage Weil2f2dc052009-10-06 11:31:09 -07001193
1194 spin_lock(&session->s_cap_lock);
1195
1196 if (!list_empty(&session->s_cap_releases)) {
1197 msg = list_first_entry(&session->s_cap_releases,
1198 struct ceph_msg,
1199 list_head);
1200 head = msg->front.iov_base;
Sage Weil38e8883e2010-06-10 12:57:11 -07001201 num = le32_to_cpu(head->num);
1202 if (num) {
1203 dout(" partial %p with (%d/%d)\n", msg, num,
1204 (int)CEPH_CAPS_PER_RELEASE);
1205 extra += CEPH_CAPS_PER_RELEASE - num;
1206 partial = msg;
1207 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001208 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001209 while (session->s_num_cap_releases < session->s_nr_caps + extra) {
1210 spin_unlock(&session->s_cap_lock);
Yehuda Sadeh34d23762010-04-06 14:33:58 -07001211 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE,
1212 GFP_NOFS);
Sage Weil2f2dc052009-10-06 11:31:09 -07001213 if (!msg)
1214 goto out_unlocked;
1215 dout("add_cap_releases %p msg %p now %d\n", session, msg,
1216 (int)msg->front.iov_len);
1217 head = msg->front.iov_base;
1218 head->num = cpu_to_le32(0);
1219 msg->front.iov_len = sizeof(*head);
1220 spin_lock(&session->s_cap_lock);
1221 list_add(&msg->list_head, &session->s_cap_releases);
1222 session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
1223 }
1224
Sage Weil38e8883e2010-06-10 12:57:11 -07001225 if (partial) {
1226 head = partial->front.iov_base;
1227 num = le32_to_cpu(head->num);
1228 dout(" queueing partial %p with %d/%d\n", partial, num,
1229 (int)CEPH_CAPS_PER_RELEASE);
1230 list_move_tail(&partial->list_head,
1231 &session->s_cap_releases_done);
1232 session->s_num_cap_releases -= CEPH_CAPS_PER_RELEASE - num;
Sage Weil2f2dc052009-10-06 11:31:09 -07001233 }
1234 err = 0;
1235 spin_unlock(&session->s_cap_lock);
1236out_unlocked:
1237 return err;
1238}
1239
1240/*
1241 * flush all dirty inode data to disk.
1242 *
1243 * returns true if we've flushed through want_flush_seq
1244 */
1245static int check_cap_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
1246{
1247 int mds, ret = 1;
1248
1249 dout("check_cap_flush want %lld\n", want_flush_seq);
1250 mutex_lock(&mdsc->mutex);
1251 for (mds = 0; ret && mds < mdsc->max_sessions; mds++) {
1252 struct ceph_mds_session *session = mdsc->sessions[mds];
1253
1254 if (!session)
1255 continue;
1256 get_session(session);
1257 mutex_unlock(&mdsc->mutex);
1258
1259 mutex_lock(&session->s_mutex);
1260 if (!list_empty(&session->s_cap_flushing)) {
1261 struct ceph_inode_info *ci =
1262 list_entry(session->s_cap_flushing.next,
1263 struct ceph_inode_info,
1264 i_flushing_item);
1265 struct inode *inode = &ci->vfs_inode;
1266
1267 spin_lock(&inode->i_lock);
1268 if (ci->i_cap_flush_seq <= want_flush_seq) {
1269 dout("check_cap_flush still flushing %p "
1270 "seq %lld <= %lld to mds%d\n", inode,
1271 ci->i_cap_flush_seq, want_flush_seq,
1272 session->s_mds);
1273 ret = 0;
1274 }
1275 spin_unlock(&inode->i_lock);
1276 }
1277 mutex_unlock(&session->s_mutex);
1278 ceph_put_mds_session(session);
1279
1280 if (!ret)
1281 return ret;
1282 mutex_lock(&mdsc->mutex);
1283 }
1284
1285 mutex_unlock(&mdsc->mutex);
1286 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
1287 return ret;
1288}
1289
1290/*
1291 * called under s_mutex
1292 */
Sage Weil3d7ded42010-06-09 16:47:10 -07001293void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1294 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001295{
1296 struct ceph_msg *msg;
1297
1298 dout("send_cap_releases mds%d\n", session->s_mds);
Sage Weil0f8605f2010-05-05 15:51:35 -07001299 spin_lock(&session->s_cap_lock);
1300 while (!list_empty(&session->s_cap_releases_done)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001301 msg = list_first_entry(&session->s_cap_releases_done,
1302 struct ceph_msg, list_head);
1303 list_del_init(&msg->list_head);
1304 spin_unlock(&session->s_cap_lock);
1305 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1306 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1307 ceph_con_send(&session->s_con, msg);
Sage Weil0f8605f2010-05-05 15:51:35 -07001308 spin_lock(&session->s_cap_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001309 }
1310 spin_unlock(&session->s_cap_lock);
1311}
1312
Sage Weile01a5942010-05-10 15:36:44 -07001313static void discard_cap_releases(struct ceph_mds_client *mdsc,
1314 struct ceph_mds_session *session)
1315{
1316 struct ceph_msg *msg;
1317 struct ceph_mds_cap_release *head;
1318 unsigned num;
1319
1320 dout("discard_cap_releases mds%d\n", session->s_mds);
1321 spin_lock(&session->s_cap_lock);
1322
1323 /* zero out the in-progress message */
1324 msg = list_first_entry(&session->s_cap_releases,
1325 struct ceph_msg, list_head);
1326 head = msg->front.iov_base;
1327 num = le32_to_cpu(head->num);
1328 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg, num);
1329 head->num = cpu_to_le32(0);
1330 session->s_num_cap_releases += num;
1331
1332 /* requeue completed messages */
1333 while (!list_empty(&session->s_cap_releases_done)) {
1334 msg = list_first_entry(&session->s_cap_releases_done,
1335 struct ceph_msg, list_head);
1336 list_del_init(&msg->list_head);
1337
1338 head = msg->front.iov_base;
1339 num = le32_to_cpu(head->num);
1340 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg,
1341 num);
1342 session->s_num_cap_releases += num;
1343 head->num = cpu_to_le32(0);
1344 msg->front.iov_len = sizeof(*head);
1345 list_add(&msg->list_head, &session->s_cap_releases);
1346 }
1347
1348 spin_unlock(&session->s_cap_lock);
1349}
1350
Sage Weil2f2dc052009-10-06 11:31:09 -07001351/*
1352 * requests
1353 */
1354
1355/*
1356 * Create an mds request.
1357 */
1358struct ceph_mds_request *
1359ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1360{
1361 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1362
1363 if (!req)
1364 return ERR_PTR(-ENOMEM);
1365
Sage Weilb4556392010-05-13 12:01:13 -07001366 mutex_init(&req->r_fill_mutex);
Yehuda Sadeh37151662010-06-17 16:16:12 -07001367 req->r_mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07001368 req->r_started = jiffies;
1369 req->r_resend_mds = -1;
1370 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1371 req->r_fmode = -1;
Sage Weil153c8e62009-12-07 12:31:09 -08001372 kref_init(&req->r_kref);
Sage Weil2f2dc052009-10-06 11:31:09 -07001373 INIT_LIST_HEAD(&req->r_wait);
1374 init_completion(&req->r_completion);
1375 init_completion(&req->r_safe_completion);
1376 INIT_LIST_HEAD(&req->r_unsafe_item);
1377
1378 req->r_op = op;
1379 req->r_direct_mode = mode;
1380 return req;
1381}
1382
1383/*
Sage Weil44ca18f2010-02-15 12:08:46 -08001384 * return oldest (lowest) request, tid in request tree, 0 if none.
Sage Weil2f2dc052009-10-06 11:31:09 -07001385 *
1386 * called under mdsc->mutex.
1387 */
Sage Weil44ca18f2010-02-15 12:08:46 -08001388static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1389{
1390 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1391 return NULL;
1392 return rb_entry(rb_first(&mdsc->request_tree),
1393 struct ceph_mds_request, r_node);
1394}
1395
Sage Weil2f2dc052009-10-06 11:31:09 -07001396static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1397{
Sage Weil44ca18f2010-02-15 12:08:46 -08001398 struct ceph_mds_request *req = __get_oldest_req(mdsc);
1399
1400 if (req)
1401 return req->r_tid;
1402 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001403}
1404
1405/*
1406 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1407 * on build_path_from_dentry in fs/cifs/dir.c.
1408 *
1409 * If @stop_on_nosnap, generate path relative to the first non-snapped
1410 * inode.
1411 *
1412 * Encode hidden .snap dirs as a double /, i.e.
1413 * foo/.snap/bar -> foo//bar
1414 */
1415char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1416 int stop_on_nosnap)
1417{
1418 struct dentry *temp;
1419 char *path;
1420 int len, pos;
1421
1422 if (dentry == NULL)
1423 return ERR_PTR(-EINVAL);
1424
1425retry:
1426 len = 0;
1427 for (temp = dentry; !IS_ROOT(temp);) {
1428 struct inode *inode = temp->d_inode;
1429 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1430 len++; /* slash only */
1431 else if (stop_on_nosnap && inode &&
1432 ceph_snap(inode) == CEPH_NOSNAP)
1433 break;
1434 else
1435 len += 1 + temp->d_name.len;
1436 temp = temp->d_parent;
1437 if (temp == NULL) {
Sage Weil6c99f252010-05-10 16:12:25 -07001438 pr_err("build_path corrupt dentry %p\n", dentry);
Sage Weil2f2dc052009-10-06 11:31:09 -07001439 return ERR_PTR(-EINVAL);
1440 }
1441 }
1442 if (len)
1443 len--; /* no leading '/' */
1444
1445 path = kmalloc(len+1, GFP_NOFS);
1446 if (path == NULL)
1447 return ERR_PTR(-ENOMEM);
1448 pos = len;
1449 path[pos] = 0; /* trailing null */
1450 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
1451 struct inode *inode = temp->d_inode;
1452
1453 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
Sage Weil104648a2010-03-18 10:14:30 -07001454 dout("build_path path+%d: %p SNAPDIR\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001455 pos, temp);
1456 } else if (stop_on_nosnap && inode &&
1457 ceph_snap(inode) == CEPH_NOSNAP) {
1458 break;
1459 } else {
1460 pos -= temp->d_name.len;
1461 if (pos < 0)
1462 break;
1463 strncpy(path + pos, temp->d_name.name,
1464 temp->d_name.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07001465 }
1466 if (pos)
1467 path[--pos] = '/';
1468 temp = temp->d_parent;
1469 if (temp == NULL) {
Sage Weil104648a2010-03-18 10:14:30 -07001470 pr_err("build_path corrupt dentry\n");
Sage Weil2f2dc052009-10-06 11:31:09 -07001471 kfree(path);
1472 return ERR_PTR(-EINVAL);
1473 }
1474 }
1475 if (pos != 0) {
Sage Weil104648a2010-03-18 10:14:30 -07001476 pr_err("build_path did not end path lookup where "
Sage Weil2f2dc052009-10-06 11:31:09 -07001477 "expected, namelen is %d, pos is %d\n", len, pos);
1478 /* presumably this is only possible if racing with a
1479 rename of one of the parent directories (we can not
1480 lock the dentries above us to prevent this, but
1481 retrying should be harmless) */
1482 kfree(path);
1483 goto retry;
1484 }
1485
1486 *base = ceph_ino(temp->d_inode);
1487 *plen = len;
Sage Weil104648a2010-03-18 10:14:30 -07001488 dout("build_path on %p %d built %llx '%.*s'\n",
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001489 dentry, dentry->d_count, *base, len, path);
Sage Weil2f2dc052009-10-06 11:31:09 -07001490 return path;
1491}
1492
1493static int build_dentry_path(struct dentry *dentry,
1494 const char **ppath, int *ppathlen, u64 *pino,
1495 int *pfreepath)
1496{
1497 char *path;
1498
1499 if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) {
1500 *pino = ceph_ino(dentry->d_parent->d_inode);
1501 *ppath = dentry->d_name.name;
1502 *ppathlen = dentry->d_name.len;
1503 return 0;
1504 }
1505 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1506 if (IS_ERR(path))
1507 return PTR_ERR(path);
1508 *ppath = path;
1509 *pfreepath = 1;
1510 return 0;
1511}
1512
1513static int build_inode_path(struct inode *inode,
1514 const char **ppath, int *ppathlen, u64 *pino,
1515 int *pfreepath)
1516{
1517 struct dentry *dentry;
1518 char *path;
1519
1520 if (ceph_snap(inode) == CEPH_NOSNAP) {
1521 *pino = ceph_ino(inode);
1522 *ppathlen = 0;
1523 return 0;
1524 }
1525 dentry = d_find_alias(inode);
1526 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1527 dput(dentry);
1528 if (IS_ERR(path))
1529 return PTR_ERR(path);
1530 *ppath = path;
1531 *pfreepath = 1;
1532 return 0;
1533}
1534
1535/*
1536 * request arguments may be specified via an inode *, a dentry *, or
1537 * an explicit ino+path.
1538 */
1539static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1540 const char *rpath, u64 rino,
1541 const char **ppath, int *pathlen,
1542 u64 *ino, int *freepath)
1543{
1544 int r = 0;
1545
1546 if (rinode) {
1547 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1548 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1549 ceph_snap(rinode));
1550 } else if (rdentry) {
1551 r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
1552 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1553 *ppath);
1554 } else if (rpath) {
1555 *ino = rino;
1556 *ppath = rpath;
1557 *pathlen = strlen(rpath);
1558 dout(" path %.*s\n", *pathlen, rpath);
1559 }
1560
1561 return r;
1562}
1563
1564/*
1565 * called under mdsc->mutex
1566 */
1567static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1568 struct ceph_mds_request *req,
1569 int mds)
1570{
1571 struct ceph_msg *msg;
1572 struct ceph_mds_request_head *head;
1573 const char *path1 = NULL;
1574 const char *path2 = NULL;
1575 u64 ino1 = 0, ino2 = 0;
1576 int pathlen1 = 0, pathlen2 = 0;
1577 int freepath1 = 0, freepath2 = 0;
1578 int len;
1579 u16 releases;
1580 void *p, *end;
1581 int ret;
1582
1583 ret = set_request_path_attr(req->r_inode, req->r_dentry,
1584 req->r_path1, req->r_ino1.ino,
1585 &path1, &pathlen1, &ino1, &freepath1);
1586 if (ret < 0) {
1587 msg = ERR_PTR(ret);
1588 goto out;
1589 }
1590
1591 ret = set_request_path_attr(NULL, req->r_old_dentry,
1592 req->r_path2, req->r_ino2.ino,
1593 &path2, &pathlen2, &ino2, &freepath2);
1594 if (ret < 0) {
1595 msg = ERR_PTR(ret);
1596 goto out_free1;
1597 }
1598
1599 len = sizeof(*head) +
Sage Weilac8839d2010-01-27 14:28:10 -08001600 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64));
Sage Weil2f2dc052009-10-06 11:31:09 -07001601
1602 /* calculate (max) length for cap releases */
1603 len += sizeof(struct ceph_mds_request_release) *
1604 (!!req->r_inode_drop + !!req->r_dentry_drop +
1605 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1606 if (req->r_dentry_drop)
1607 len += req->r_dentry->d_name.len;
1608 if (req->r_old_dentry_drop)
1609 len += req->r_old_dentry->d_name.len;
1610
Yehuda Sadeh34d23762010-04-06 14:33:58 -07001611 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -07001612 if (!msg) {
1613 msg = ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -07001614 goto out_free2;
Sage Weila79832f2010-04-01 16:06:19 -07001615 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001616
Sage Weil6df058c2009-12-22 11:24:33 -08001617 msg->hdr.tid = cpu_to_le64(req->r_tid);
1618
Sage Weil2f2dc052009-10-06 11:31:09 -07001619 head = msg->front.iov_base;
1620 p = msg->front.iov_base + sizeof(*head);
1621 end = msg->front.iov_base + msg->front.iov_len;
1622
1623 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1624 head->op = cpu_to_le32(req->r_op);
Sage Weilcb4276c2010-11-08 07:28:52 -08001625 head->caller_uid = cpu_to_le32(req->r_uid);
1626 head->caller_gid = cpu_to_le32(req->r_gid);
Sage Weil2f2dc052009-10-06 11:31:09 -07001627 head->args = req->r_args;
1628
1629 ceph_encode_filepath(&p, end, ino1, path1);
1630 ceph_encode_filepath(&p, end, ino2, path2);
1631
Sage Weile979cf52010-07-15 14:58:39 -07001632 /* make note of release offset, in case we need to replay */
1633 req->r_request_release_offset = p - msg->front.iov_base;
1634
Sage Weil2f2dc052009-10-06 11:31:09 -07001635 /* cap releases */
1636 releases = 0;
1637 if (req->r_inode_drop)
1638 releases += ceph_encode_inode_release(&p,
1639 req->r_inode ? req->r_inode : req->r_dentry->d_inode,
1640 mds, req->r_inode_drop, req->r_inode_unless, 0);
1641 if (req->r_dentry_drop)
1642 releases += ceph_encode_dentry_release(&p, req->r_dentry,
1643 mds, req->r_dentry_drop, req->r_dentry_unless);
1644 if (req->r_old_dentry_drop)
1645 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
1646 mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
1647 if (req->r_old_inode_drop)
1648 releases += ceph_encode_inode_release(&p,
1649 req->r_old_dentry->d_inode,
1650 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
1651 head->num_releases = cpu_to_le16(releases);
1652
1653 BUG_ON(p > end);
1654 msg->front.iov_len = p - msg->front.iov_base;
1655 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1656
1657 msg->pages = req->r_pages;
1658 msg->nr_pages = req->r_num_pages;
1659 msg->hdr.data_len = cpu_to_le32(req->r_data_len);
1660 msg->hdr.data_off = cpu_to_le16(0);
1661
1662out_free2:
1663 if (freepath2)
1664 kfree((char *)path2);
1665out_free1:
1666 if (freepath1)
1667 kfree((char *)path1);
1668out:
1669 return msg;
1670}
1671
1672/*
1673 * called under mdsc->mutex if error, under no mutex if
1674 * success.
1675 */
1676static void complete_request(struct ceph_mds_client *mdsc,
1677 struct ceph_mds_request *req)
1678{
1679 if (req->r_callback)
1680 req->r_callback(mdsc, req);
1681 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001682 complete_all(&req->r_completion);
Sage Weil2f2dc052009-10-06 11:31:09 -07001683}
1684
1685/*
1686 * called under mdsc->mutex
1687 */
1688static int __prepare_send_request(struct ceph_mds_client *mdsc,
1689 struct ceph_mds_request *req,
1690 int mds)
1691{
1692 struct ceph_mds_request_head *rhead;
1693 struct ceph_msg *msg;
1694 int flags = 0;
1695
1696 req->r_mds = mds;
1697 req->r_attempts++;
Greg Farnume55b71f2010-06-22 15:58:01 -07001698 if (req->r_inode) {
1699 struct ceph_cap *cap =
1700 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
1701
1702 if (cap)
1703 req->r_sent_on_mseq = cap->mseq;
1704 else
1705 req->r_sent_on_mseq = -1;
1706 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001707 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
1708 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
1709
Sage Weil01a92f12010-07-15 13:24:32 -07001710 if (req->r_got_unsafe) {
1711 /*
1712 * Replay. Do not regenerate message (and rebuild
1713 * paths, etc.); just use the original message.
1714 * Rebuilding paths will break for renames because
1715 * d_move mangles the src name.
1716 */
1717 msg = req->r_request;
1718 rhead = msg->front.iov_base;
1719
1720 flags = le32_to_cpu(rhead->flags);
1721 flags |= CEPH_MDS_FLAG_REPLAY;
1722 rhead->flags = cpu_to_le32(flags);
1723
1724 if (req->r_target_inode)
1725 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
1726
1727 rhead->num_retry = req->r_attempts - 1;
Sage Weile979cf52010-07-15 14:58:39 -07001728
1729 /* remove cap/dentry releases from message */
1730 rhead->num_releases = 0;
1731 msg->hdr.front_len = cpu_to_le32(req->r_request_release_offset);
1732 msg->front.iov_len = req->r_request_release_offset;
Sage Weil01a92f12010-07-15 13:24:32 -07001733 return 0;
1734 }
1735
Sage Weil2f2dc052009-10-06 11:31:09 -07001736 if (req->r_request) {
1737 ceph_msg_put(req->r_request);
1738 req->r_request = NULL;
1739 }
1740 msg = create_request_message(mdsc, req, mds);
1741 if (IS_ERR(msg)) {
Sage Weile1518c72010-05-13 11:19:06 -07001742 req->r_err = PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001743 complete_request(mdsc, req);
Sage Weila79832f2010-04-01 16:06:19 -07001744 return PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001745 }
1746 req->r_request = msg;
1747
1748 rhead = msg->front.iov_base;
Sage Weil2f2dc052009-10-06 11:31:09 -07001749 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
1750 if (req->r_got_unsafe)
1751 flags |= CEPH_MDS_FLAG_REPLAY;
1752 if (req->r_locked_dir)
1753 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
1754 rhead->flags = cpu_to_le32(flags);
1755 rhead->num_fwd = req->r_num_fwd;
1756 rhead->num_retry = req->r_attempts - 1;
Sage Weil01a92f12010-07-15 13:24:32 -07001757 rhead->ino = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001758
1759 dout(" r_locked_dir = %p\n", req->r_locked_dir);
Sage Weil2f2dc052009-10-06 11:31:09 -07001760 return 0;
1761}
1762
1763/*
1764 * send request, or put it on the appropriate wait list.
1765 */
1766static int __do_request(struct ceph_mds_client *mdsc,
1767 struct ceph_mds_request *req)
1768{
1769 struct ceph_mds_session *session = NULL;
1770 int mds = -1;
1771 int err = -EAGAIN;
1772
Sage Weile1518c72010-05-13 11:19:06 -07001773 if (req->r_err || req->r_got_result)
Sage Weil2f2dc052009-10-06 11:31:09 -07001774 goto out;
1775
1776 if (req->r_timeout &&
1777 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
1778 dout("do_request timed out\n");
1779 err = -EIO;
1780 goto finish;
1781 }
1782
1783 mds = __choose_mds(mdsc, req);
1784 if (mds < 0 ||
1785 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
1786 dout("do_request no mds or not active, waiting for map\n");
1787 list_add(&req->r_wait, &mdsc->waiting_for_map);
1788 goto out;
1789 }
1790
1791 /* get, open session */
1792 session = __ceph_lookup_mds_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07001793 if (!session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001794 session = register_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07001795 if (IS_ERR(session)) {
1796 err = PTR_ERR(session);
1797 goto finish;
1798 }
1799 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001800 dout("do_request mds%d session %p state %s\n", mds, session,
1801 session_state_name(session->s_state));
1802 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
1803 session->s_state != CEPH_MDS_SESSION_HUNG) {
1804 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1805 session->s_state == CEPH_MDS_SESSION_CLOSING)
1806 __open_session(mdsc, session);
1807 list_add(&req->r_wait, &session->s_waiting);
1808 goto out_session;
1809 }
1810
1811 /* send request */
1812 req->r_session = get_session(session);
1813 req->r_resend_mds = -1; /* forget any previous mds hint */
1814
1815 if (req->r_request_started == 0) /* note request start time */
1816 req->r_request_started = jiffies;
1817
1818 err = __prepare_send_request(mdsc, req, mds);
1819 if (!err) {
1820 ceph_msg_get(req->r_request);
1821 ceph_con_send(&session->s_con, req->r_request);
1822 }
1823
1824out_session:
1825 ceph_put_mds_session(session);
1826out:
1827 return err;
1828
1829finish:
Sage Weile1518c72010-05-13 11:19:06 -07001830 req->r_err = err;
Sage Weil2f2dc052009-10-06 11:31:09 -07001831 complete_request(mdsc, req);
1832 goto out;
1833}
1834
1835/*
1836 * called under mdsc->mutex
1837 */
1838static void __wake_requests(struct ceph_mds_client *mdsc,
1839 struct list_head *head)
1840{
1841 struct ceph_mds_request *req, *nreq;
1842
1843 list_for_each_entry_safe(req, nreq, head, r_wait) {
1844 list_del_init(&req->r_wait);
1845 __do_request(mdsc, req);
1846 }
1847}
1848
1849/*
1850 * Wake up threads with requests pending for @mds, so that they can
Sage Weil29790f22010-03-18 14:45:05 -07001851 * resubmit their requests to a possibly different mds.
Sage Weil2f2dc052009-10-06 11:31:09 -07001852 */
Sage Weil29790f22010-03-18 14:45:05 -07001853static void kick_requests(struct ceph_mds_client *mdsc, int mds)
Sage Weil2f2dc052009-10-06 11:31:09 -07001854{
Sage Weil44ca18f2010-02-15 12:08:46 -08001855 struct ceph_mds_request *req;
1856 struct rb_node *p;
Sage Weil2f2dc052009-10-06 11:31:09 -07001857
1858 dout("kick_requests mds%d\n", mds);
Sage Weil44ca18f2010-02-15 12:08:46 -08001859 for (p = rb_first(&mdsc->request_tree); p; p = rb_next(p)) {
1860 req = rb_entry(p, struct ceph_mds_request, r_node);
1861 if (req->r_got_unsafe)
1862 continue;
1863 if (req->r_session &&
1864 req->r_session->s_mds == mds) {
1865 dout(" kicking tid %llu\n", req->r_tid);
1866 put_request_session(req);
1867 __do_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07001868 }
1869 }
1870}
1871
1872void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
1873 struct ceph_mds_request *req)
1874{
1875 dout("submit_request on %p\n", req);
1876 mutex_lock(&mdsc->mutex);
1877 __register_request(mdsc, req, NULL);
1878 __do_request(mdsc, req);
1879 mutex_unlock(&mdsc->mutex);
1880}
1881
1882/*
1883 * Synchrously perform an mds request. Take care of all of the
1884 * session setup, forwarding, retry details.
1885 */
1886int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
1887 struct inode *dir,
1888 struct ceph_mds_request *req)
1889{
1890 int err;
1891
1892 dout("do_request on %p\n", req);
1893
1894 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
1895 if (req->r_inode)
1896 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
1897 if (req->r_locked_dir)
1898 ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
1899 if (req->r_old_dentry)
1900 ceph_get_cap_refs(
1901 ceph_inode(req->r_old_dentry->d_parent->d_inode),
1902 CEPH_CAP_PIN);
1903
1904 /* issue */
1905 mutex_lock(&mdsc->mutex);
1906 __register_request(mdsc, req, dir);
1907 __do_request(mdsc, req);
1908
Sage Weile1518c72010-05-13 11:19:06 -07001909 if (req->r_err) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001910 err = req->r_err;
Sage Weile1518c72010-05-13 11:19:06 -07001911 __unregister_request(mdsc, req);
1912 dout("do_request early error %d\n", err);
1913 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07001914 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001915
Sage Weile1518c72010-05-13 11:19:06 -07001916 /* wait */
1917 mutex_unlock(&mdsc->mutex);
1918 dout("do_request waiting\n");
1919 if (req->r_timeout) {
Sage Weilaa916472010-05-24 11:15:51 -07001920 err = (long)wait_for_completion_killable_timeout(
Sage Weile1518c72010-05-13 11:19:06 -07001921 &req->r_completion, req->r_timeout);
1922 if (err == 0)
1923 err = -EIO;
1924 } else {
Sage Weilaa916472010-05-24 11:15:51 -07001925 err = wait_for_completion_killable(&req->r_completion);
Sage Weile1518c72010-05-13 11:19:06 -07001926 }
1927 dout("do_request waited, got %d\n", err);
1928 mutex_lock(&mdsc->mutex);
1929
1930 /* only abort if we didn't race with a real reply */
1931 if (req->r_got_result) {
1932 err = le32_to_cpu(req->r_reply_info.head->result);
1933 } else if (err < 0) {
1934 dout("aborted request %lld with %d\n", req->r_tid, err);
Sage Weilb4556392010-05-13 12:01:13 -07001935
1936 /*
1937 * ensure we aren't running concurrently with
1938 * ceph_fill_trace or ceph_readdir_prepopulate, which
1939 * rely on locks (dir mutex) held by our caller.
1940 */
1941 mutex_lock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07001942 req->r_err = err;
1943 req->r_aborted = true;
Sage Weilb4556392010-05-13 12:01:13 -07001944 mutex_unlock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07001945
1946 if (req->r_locked_dir &&
Sage Weil167c9e32010-05-14 10:02:57 -07001947 (req->r_op & CEPH_MDS_OP_WRITE))
1948 ceph_invalidate_dir_request(req);
Sage Weile1518c72010-05-13 11:19:06 -07001949 } else {
1950 err = req->r_err;
1951 }
1952
1953out:
1954 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07001955 dout("do_request %p done, result %d\n", req, err);
1956 return err;
1957}
1958
1959/*
Sage Weil167c9e32010-05-14 10:02:57 -07001960 * Invalidate dir I_COMPLETE, dentry lease state on an aborted MDS
1961 * namespace request.
1962 */
1963void ceph_invalidate_dir_request(struct ceph_mds_request *req)
1964{
1965 struct inode *inode = req->r_locked_dir;
1966 struct ceph_inode_info *ci = ceph_inode(inode);
1967
1968 dout("invalidate_dir_request %p (I_COMPLETE, lease(s))\n", inode);
1969 spin_lock(&inode->i_lock);
1970 ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
1971 ci->i_release_count++;
1972 spin_unlock(&inode->i_lock);
1973
1974 if (req->r_dentry)
1975 ceph_invalidate_dentry_lease(req->r_dentry);
1976 if (req->r_old_dentry)
1977 ceph_invalidate_dentry_lease(req->r_old_dentry);
1978}
1979
1980/*
Sage Weil2f2dc052009-10-06 11:31:09 -07001981 * Handle mds reply.
1982 *
1983 * We take the session mutex and parse and process the reply immediately.
1984 * This preserves the logical ordering of replies, capabilities, etc., sent
1985 * by the MDS as they are applied to our local cache.
1986 */
1987static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
1988{
1989 struct ceph_mds_client *mdsc = session->s_mdsc;
1990 struct ceph_mds_request *req;
1991 struct ceph_mds_reply_head *head = msg->front.iov_base;
1992 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
1993 u64 tid;
1994 int err, result;
Sage Weil2600d2d2010-02-22 15:12:16 -08001995 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07001996
Sage Weil2f2dc052009-10-06 11:31:09 -07001997 if (msg->front.iov_len < sizeof(*head)) {
1998 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08001999 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002000 return;
2001 }
2002
2003 /* get request, session */
Sage Weil6df058c2009-12-22 11:24:33 -08002004 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002005 mutex_lock(&mdsc->mutex);
2006 req = __lookup_request(mdsc, tid);
2007 if (!req) {
2008 dout("handle_reply on unknown tid %llu\n", tid);
2009 mutex_unlock(&mdsc->mutex);
2010 return;
2011 }
2012 dout("handle_reply %p\n", req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002013
2014 /* correct session? */
Sage Weild96d6042010-03-20 20:50:58 -07002015 if (req->r_session != session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002016 pr_err("mdsc_handle_reply got %llu on session mds%d"
2017 " not mds%d\n", tid, session->s_mds,
2018 req->r_session ? req->r_session->s_mds : -1);
2019 mutex_unlock(&mdsc->mutex);
2020 goto out;
2021 }
2022
2023 /* dup? */
2024 if ((req->r_got_unsafe && !head->safe) ||
2025 (req->r_got_safe && head->safe)) {
2026 pr_warning("got a dup %s reply on %llu from mds%d\n",
2027 head->safe ? "safe" : "unsafe", tid, mds);
2028 mutex_unlock(&mdsc->mutex);
2029 goto out;
2030 }
Sage Weil85792d0d2010-05-13 09:06:02 -07002031 if (req->r_got_safe && !head->safe) {
2032 pr_warning("got unsafe after safe on %llu from mds%d\n",
2033 tid, mds);
2034 mutex_unlock(&mdsc->mutex);
2035 goto out;
2036 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002037
2038 result = le32_to_cpu(head->result);
2039
2040 /*
Greg Farnume55b71f2010-06-22 15:58:01 -07002041 * Handle an ESTALE
2042 * if we're not talking to the authority, send to them
2043 * if the authority has changed while we weren't looking,
2044 * send to new authority
2045 * Otherwise we just have to return an ESTALE
Sage Weil2f2dc052009-10-06 11:31:09 -07002046 */
2047 if (result == -ESTALE) {
Greg Farnume55b71f2010-06-22 15:58:01 -07002048 dout("got ESTALE on request %llu", req->r_tid);
Sage Weil213c99e2010-08-03 10:25:11 -07002049 if (!req->r_inode) {
2050 /* do nothing; not an authority problem */
2051 } else if (req->r_direct_mode != USE_AUTH_MDS) {
Greg Farnume55b71f2010-06-22 15:58:01 -07002052 dout("not using auth, setting for that now");
2053 req->r_direct_mode = USE_AUTH_MDS;
Sage Weil2f2dc052009-10-06 11:31:09 -07002054 __do_request(mdsc, req);
2055 mutex_unlock(&mdsc->mutex);
2056 goto out;
Greg Farnume55b71f2010-06-22 15:58:01 -07002057 } else {
2058 struct ceph_inode_info *ci = ceph_inode(req->r_inode);
2059 struct ceph_cap *cap =
2060 ceph_get_cap_for_mds(ci, req->r_mds);;
2061
2062 dout("already using auth");
2063 if ((!cap || cap != ci->i_auth_cap) ||
2064 (cap->mseq != req->r_sent_on_mseq)) {
2065 dout("but cap changed, so resending");
2066 __do_request(mdsc, req);
2067 mutex_unlock(&mdsc->mutex);
2068 goto out;
2069 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002070 }
Greg Farnume55b71f2010-06-22 15:58:01 -07002071 dout("have to return ESTALE on request %llu", req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002072 }
2073
Greg Farnume55b71f2010-06-22 15:58:01 -07002074
Sage Weil2f2dc052009-10-06 11:31:09 -07002075 if (head->safe) {
2076 req->r_got_safe = true;
2077 __unregister_request(mdsc, req);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002078 complete_all(&req->r_safe_completion);
Sage Weil2f2dc052009-10-06 11:31:09 -07002079
2080 if (req->r_got_unsafe) {
2081 /*
2082 * We already handled the unsafe response, now do the
2083 * cleanup. No need to examine the response; the MDS
2084 * doesn't include any result info in the safe
2085 * response. And even if it did, there is nothing
2086 * useful we could do with a revised return value.
2087 */
2088 dout("got safe reply %llu, mds%d\n", tid, mds);
2089 list_del_init(&req->r_unsafe_item);
2090
2091 /* last unsafe request during umount? */
Sage Weil44ca18f2010-02-15 12:08:46 -08002092 if (mdsc->stopping && !__get_oldest_req(mdsc))
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002093 complete_all(&mdsc->safe_umount_waiters);
Sage Weil2f2dc052009-10-06 11:31:09 -07002094 mutex_unlock(&mdsc->mutex);
2095 goto out;
2096 }
Sage Weile1518c72010-05-13 11:19:06 -07002097 } else {
Sage Weil2f2dc052009-10-06 11:31:09 -07002098 req->r_got_unsafe = true;
2099 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
2100 }
2101
2102 dout("handle_reply tid %lld result %d\n", tid, result);
2103 rinfo = &req->r_reply_info;
2104 err = parse_reply_info(msg, rinfo);
2105 mutex_unlock(&mdsc->mutex);
2106
2107 mutex_lock(&session->s_mutex);
2108 if (err < 0) {
Herb Shiu25933ab2010-12-01 14:14:38 -08002109 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002110 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002111 goto out_err;
2112 }
2113
2114 /* snap trace */
2115 if (rinfo->snapblob_len) {
2116 down_write(&mdsc->snap_rwsem);
2117 ceph_update_snap_trace(mdsc, rinfo->snapblob,
2118 rinfo->snapblob + rinfo->snapblob_len,
2119 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP);
2120 downgrade_write(&mdsc->snap_rwsem);
2121 } else {
2122 down_read(&mdsc->snap_rwsem);
2123 }
2124
2125 /* insert trace into our cache */
Sage Weilb4556392010-05-13 12:01:13 -07002126 mutex_lock(&req->r_fill_mutex);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002127 err = ceph_fill_trace(mdsc->fsc->sb, req, req->r_session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002128 if (err == 0) {
Herb Shiu25933ab2010-12-01 14:14:38 -08002129 if (result == 0 && req->r_op != CEPH_MDS_OP_GETFILELOCK &&
2130 rinfo->dir_nr)
Sage Weil2f2dc052009-10-06 11:31:09 -07002131 ceph_readdir_prepopulate(req, req->r_session);
Yehuda Sadeh37151662010-06-17 16:16:12 -07002132 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
Sage Weil2f2dc052009-10-06 11:31:09 -07002133 }
Sage Weilb4556392010-05-13 12:01:13 -07002134 mutex_unlock(&req->r_fill_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002135
2136 up_read(&mdsc->snap_rwsem);
2137out_err:
Sage Weile1518c72010-05-13 11:19:06 -07002138 mutex_lock(&mdsc->mutex);
2139 if (!req->r_aborted) {
2140 if (err) {
2141 req->r_err = err;
2142 } else {
2143 req->r_reply = msg;
2144 ceph_msg_get(msg);
2145 req->r_got_result = true;
2146 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002147 } else {
Sage Weile1518c72010-05-13 11:19:06 -07002148 dout("reply arrived after request %lld was aborted\n", tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002149 }
Sage Weile1518c72010-05-13 11:19:06 -07002150 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002151
Sage Weilee6b2722010-06-10 12:55:52 -07002152 ceph_add_cap_releases(mdsc, req->r_session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002153 mutex_unlock(&session->s_mutex);
2154
2155 /* kick calling process */
2156 complete_request(mdsc, req);
2157out:
2158 ceph_mdsc_put_request(req);
2159 return;
2160}
2161
2162
2163
2164/*
2165 * handle mds notification that our request has been forwarded.
2166 */
Sage Weil2600d2d2010-02-22 15:12:16 -08002167static void handle_forward(struct ceph_mds_client *mdsc,
2168 struct ceph_mds_session *session,
2169 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002170{
2171 struct ceph_mds_request *req;
Sage Weila1ea7872010-02-23 14:02:44 -08002172 u64 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002173 u32 next_mds;
2174 u32 fwd_seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002175 int err = -EINVAL;
2176 void *p = msg->front.iov_base;
2177 void *end = p + msg->front.iov_len;
Sage Weil2f2dc052009-10-06 11:31:09 -07002178
Sage Weila1ea7872010-02-23 14:02:44 -08002179 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002180 next_mds = ceph_decode_32(&p);
2181 fwd_seq = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07002182
2183 mutex_lock(&mdsc->mutex);
2184 req = __lookup_request(mdsc, tid);
2185 if (!req) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07002186 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002187 goto out; /* dup reply? */
2188 }
2189
Sage Weil2a8e5e32010-05-28 16:43:16 -07002190 if (req->r_aborted) {
2191 dout("forward tid %llu aborted, unregistering\n", tid);
2192 __unregister_request(mdsc, req);
2193 } else if (fwd_seq <= req->r_num_fwd) {
2194 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002195 tid, next_mds, req->r_num_fwd, fwd_seq);
2196 } else {
2197 /* resend. forward race not possible; mds would drop */
Sage Weil2a8e5e32010-05-28 16:43:16 -07002198 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2199 BUG_ON(req->r_err);
2200 BUG_ON(req->r_got_result);
Sage Weil2f2dc052009-10-06 11:31:09 -07002201 req->r_num_fwd = fwd_seq;
2202 req->r_resend_mds = next_mds;
2203 put_request_session(req);
2204 __do_request(mdsc, req);
2205 }
2206 ceph_mdsc_put_request(req);
2207out:
2208 mutex_unlock(&mdsc->mutex);
2209 return;
2210
2211bad:
2212 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2213}
2214
2215/*
2216 * handle a mds session control message
2217 */
2218static void handle_session(struct ceph_mds_session *session,
2219 struct ceph_msg *msg)
2220{
2221 struct ceph_mds_client *mdsc = session->s_mdsc;
2222 u32 op;
2223 u64 seq;
Sage Weil2600d2d2010-02-22 15:12:16 -08002224 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002225 struct ceph_mds_session_head *h = msg->front.iov_base;
2226 int wake = 0;
2227
Sage Weil2f2dc052009-10-06 11:31:09 -07002228 /* decode */
2229 if (msg->front.iov_len != sizeof(*h))
2230 goto bad;
2231 op = le32_to_cpu(h->op);
2232 seq = le64_to_cpu(h->seq);
2233
2234 mutex_lock(&mdsc->mutex);
Sage Weil2600d2d2010-02-22 15:12:16 -08002235 if (op == CEPH_SESSION_CLOSE)
2236 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002237 /* FIXME: this ttl calculation is generous */
2238 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2239 mutex_unlock(&mdsc->mutex);
2240
2241 mutex_lock(&session->s_mutex);
2242
2243 dout("handle_session mds%d %s %p state %s seq %llu\n",
2244 mds, ceph_session_op_name(op), session,
2245 session_state_name(session->s_state), seq);
2246
2247 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2248 session->s_state = CEPH_MDS_SESSION_OPEN;
2249 pr_info("mds%d came back\n", session->s_mds);
2250 }
2251
2252 switch (op) {
2253 case CEPH_SESSION_OPEN:
Sage Weil29790f22010-03-18 14:45:05 -07002254 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2255 pr_info("mds%d reconnect success\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002256 session->s_state = CEPH_MDS_SESSION_OPEN;
2257 renewed_caps(mdsc, session, 0);
2258 wake = 1;
2259 if (mdsc->stopping)
2260 __close_session(mdsc, session);
2261 break;
2262
2263 case CEPH_SESSION_RENEWCAPS:
2264 if (session->s_renew_seq == seq)
2265 renewed_caps(mdsc, session, 1);
2266 break;
2267
2268 case CEPH_SESSION_CLOSE:
Sage Weil29790f22010-03-18 14:45:05 -07002269 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2270 pr_info("mds%d reconnect denied\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002271 remove_session_caps(session);
2272 wake = 1; /* for good measure */
Sage Weilf3c60c52010-08-11 14:51:23 -07002273 wake_up_all(&mdsc->session_close_wq);
Sage Weil29790f22010-03-18 14:45:05 -07002274 kick_requests(mdsc, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002275 break;
2276
2277 case CEPH_SESSION_STALE:
2278 pr_info("mds%d caps went stale, renewing\n",
2279 session->s_mds);
2280 spin_lock(&session->s_cap_lock);
2281 session->s_cap_gen++;
2282 session->s_cap_ttl = 0;
2283 spin_unlock(&session->s_cap_lock);
2284 send_renew_caps(mdsc, session);
2285 break;
2286
2287 case CEPH_SESSION_RECALL_STATE:
2288 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2289 break;
2290
2291 default:
2292 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2293 WARN_ON(1);
2294 }
2295
2296 mutex_unlock(&session->s_mutex);
2297 if (wake) {
2298 mutex_lock(&mdsc->mutex);
2299 __wake_requests(mdsc, &session->s_waiting);
2300 mutex_unlock(&mdsc->mutex);
2301 }
2302 return;
2303
2304bad:
2305 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2306 (int)msg->front.iov_len);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002307 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002308 return;
2309}
2310
2311
2312/*
2313 * called under session->mutex.
2314 */
2315static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2316 struct ceph_mds_session *session)
2317{
2318 struct ceph_mds_request *req, *nreq;
2319 int err;
2320
2321 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2322
2323 mutex_lock(&mdsc->mutex);
2324 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
2325 err = __prepare_send_request(mdsc, req, session->s_mds);
2326 if (!err) {
2327 ceph_msg_get(req->r_request);
2328 ceph_con_send(&session->s_con, req->r_request);
2329 }
2330 }
2331 mutex_unlock(&mdsc->mutex);
2332}
2333
2334/*
2335 * Encode information about a cap for a reconnect with the MDS.
2336 */
Sage Weil2f2dc052009-10-06 11:31:09 -07002337static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2338 void *arg)
2339{
Sage Weil20cb34a2010-05-12 15:21:32 -07002340 union {
2341 struct ceph_mds_cap_reconnect v2;
2342 struct ceph_mds_cap_reconnect_v1 v1;
2343 } rec;
2344 size_t reclen;
Sage Weil2f2dc052009-10-06 11:31:09 -07002345 struct ceph_inode_info *ci;
Sage Weil20cb34a2010-05-12 15:21:32 -07002346 struct ceph_reconnect_state *recon_state = arg;
2347 struct ceph_pagelist *pagelist = recon_state->pagelist;
Sage Weil2f2dc052009-10-06 11:31:09 -07002348 char *path;
2349 int pathlen, err;
2350 u64 pathbase;
2351 struct dentry *dentry;
2352
2353 ci = cap->ci;
2354
2355 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2356 inode, ceph_vinop(inode), cap, cap->cap_id,
2357 ceph_cap_string(cap->issued));
Sage Weil93cea5b2009-12-23 12:21:51 -08002358 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2359 if (err)
2360 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002361
2362 dentry = d_find_alias(inode);
2363 if (dentry) {
2364 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2365 if (IS_ERR(path)) {
2366 err = PTR_ERR(path);
Sage Weile072f8a2010-08-26 09:26:37 -07002367 goto out_dput;
Sage Weil2f2dc052009-10-06 11:31:09 -07002368 }
2369 } else {
2370 path = NULL;
2371 pathlen = 0;
2372 }
Sage Weil93cea5b2009-12-23 12:21:51 -08002373 err = ceph_pagelist_encode_string(pagelist, path, pathlen);
2374 if (err)
Sage Weile072f8a2010-08-26 09:26:37 -07002375 goto out_free;
Sage Weil2f2dc052009-10-06 11:31:09 -07002376
Sage Weil2f2dc052009-10-06 11:31:09 -07002377 spin_lock(&inode->i_lock);
2378 cap->seq = 0; /* reset cap seq */
2379 cap->issue_seq = 0; /* and issue_seq */
Sage Weil20cb34a2010-05-12 15:21:32 -07002380
2381 if (recon_state->flock) {
2382 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
2383 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2384 rec.v2.issued = cpu_to_le32(cap->issued);
2385 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2386 rec.v2.pathbase = cpu_to_le64(pathbase);
2387 rec.v2.flock_len = 0;
2388 reclen = sizeof(rec.v2);
2389 } else {
2390 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
2391 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2392 rec.v1.issued = cpu_to_le32(cap->issued);
2393 rec.v1.size = cpu_to_le64(inode->i_size);
2394 ceph_encode_timespec(&rec.v1.mtime, &inode->i_mtime);
2395 ceph_encode_timespec(&rec.v1.atime, &inode->i_atime);
2396 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2397 rec.v1.pathbase = cpu_to_le64(pathbase);
2398 reclen = sizeof(rec.v1);
2399 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002400 spin_unlock(&inode->i_lock);
2401
Greg Farnum40819f62010-08-02 15:34:23 -07002402 if (recon_state->flock) {
2403 int num_fcntl_locks, num_flock_locks;
Greg Farnumfca44512010-09-17 10:24:02 -07002404 struct ceph_pagelist_cursor trunc_point;
Greg Farnum40819f62010-08-02 15:34:23 -07002405
Greg Farnumfca44512010-09-17 10:24:02 -07002406 ceph_pagelist_set_cursor(pagelist, &trunc_point);
2407 do {
Sage Weil496e5952010-09-22 19:57:10 -07002408 lock_flocks();
Greg Farnumfca44512010-09-17 10:24:02 -07002409 ceph_count_locks(inode, &num_fcntl_locks,
2410 &num_flock_locks);
2411 rec.v2.flock_len = (2*sizeof(u32) +
2412 (num_fcntl_locks+num_flock_locks) *
2413 sizeof(struct ceph_filelock));
Sage Weil496e5952010-09-22 19:57:10 -07002414 unlock_flocks();
Greg Farnum40819f62010-08-02 15:34:23 -07002415
Greg Farnumfca44512010-09-17 10:24:02 -07002416 /* pre-alloc pagelist */
2417 ceph_pagelist_truncate(pagelist, &trunc_point);
2418 err = ceph_pagelist_append(pagelist, &rec, reclen);
2419 if (!err)
2420 err = ceph_pagelist_reserve(pagelist,
2421 rec.v2.flock_len);
2422
2423 /* encode locks */
2424 if (!err) {
Sage Weil496e5952010-09-22 19:57:10 -07002425 lock_flocks();
Greg Farnumfca44512010-09-17 10:24:02 -07002426 err = ceph_encode_locks(inode,
2427 pagelist,
2428 num_fcntl_locks,
2429 num_flock_locks);
Sage Weil496e5952010-09-22 19:57:10 -07002430 unlock_flocks();
Greg Farnumfca44512010-09-17 10:24:02 -07002431 }
2432 } while (err == -ENOSPC);
Sage Weil3612abb2010-09-07 15:59:27 -07002433 } else {
2434 err = ceph_pagelist_append(pagelist, &rec, reclen);
Greg Farnum40819f62010-08-02 15:34:23 -07002435 }
Sage Weil93cea5b2009-12-23 12:21:51 -08002436
Sage Weile072f8a2010-08-26 09:26:37 -07002437out_free:
Sage Weil2f2dc052009-10-06 11:31:09 -07002438 kfree(path);
Sage Weile072f8a2010-08-26 09:26:37 -07002439out_dput:
Sage Weil2f2dc052009-10-06 11:31:09 -07002440 dput(dentry);
Sage Weil93cea5b2009-12-23 12:21:51 -08002441 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002442}
2443
2444
2445/*
2446 * If an MDS fails and recovers, clients need to reconnect in order to
2447 * reestablish shared state. This includes all caps issued through
2448 * this session _and_ the snap_realm hierarchy. Because it's not
2449 * clear which snap realms the mds cares about, we send everything we
2450 * know about.. that ensures we'll then get any new info the
2451 * recovering MDS might have.
2452 *
2453 * This is a relatively heavyweight operation, but it's rare.
2454 *
2455 * called with mdsc->mutex held.
2456 */
Sage Weil34b6c852010-05-10 16:31:25 -07002457static void send_mds_reconnect(struct ceph_mds_client *mdsc,
2458 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07002459{
Sage Weil2f2dc052009-10-06 11:31:09 -07002460 struct ceph_msg *reply;
Sage Weila105f002010-02-15 14:37:55 -08002461 struct rb_node *p;
Sage Weil34b6c852010-05-10 16:31:25 -07002462 int mds = session->s_mds;
Sage Weil9abf82b2010-05-10 21:58:38 -07002463 int err = -ENOMEM;
Sage Weil93cea5b2009-12-23 12:21:51 -08002464 struct ceph_pagelist *pagelist;
Sage Weil20cb34a2010-05-12 15:21:32 -07002465 struct ceph_reconnect_state recon_state;
Sage Weil2f2dc052009-10-06 11:31:09 -07002466
Sage Weil34b6c852010-05-10 16:31:25 -07002467 pr_info("mds%d reconnect start\n", mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002468
Sage Weil93cea5b2009-12-23 12:21:51 -08002469 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
2470 if (!pagelist)
2471 goto fail_nopagelist;
2472 ceph_pagelist_init(pagelist);
2473
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002474 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -07002475 if (!reply)
Sage Weil93cea5b2009-12-23 12:21:51 -08002476 goto fail_nomsg;
Sage Weil93cea5b2009-12-23 12:21:51 -08002477
Sage Weil34b6c852010-05-10 16:31:25 -07002478 mutex_lock(&session->s_mutex);
2479 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
2480 session->s_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002481
Sage Weil34b6c852010-05-10 16:31:25 -07002482 ceph_con_open(&session->s_con,
2483 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
Sage Weil2f2dc052009-10-06 11:31:09 -07002484
Sage Weil34b6c852010-05-10 16:31:25 -07002485 /* replay unsafe requests */
2486 replay_unsafe_requests(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002487
2488 down_read(&mdsc->snap_rwsem);
2489
Sage Weil2f2dc052009-10-06 11:31:09 -07002490 dout("session %p state %s\n", session,
2491 session_state_name(session->s_state));
2492
Sage Weile01a5942010-05-10 15:36:44 -07002493 /* drop old cap expires; we're about to reestablish that state */
2494 discard_cap_releases(mdsc, session);
2495
Sage Weil2f2dc052009-10-06 11:31:09 -07002496 /* traverse this session's caps */
Sage Weil93cea5b2009-12-23 12:21:51 -08002497 err = ceph_pagelist_encode_32(pagelist, session->s_nr_caps);
2498 if (err)
2499 goto fail;
Sage Weil20cb34a2010-05-12 15:21:32 -07002500
2501 recon_state.pagelist = pagelist;
2502 recon_state.flock = session->s_con.peer_features & CEPH_FEATURE_FLOCK;
2503 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
Sage Weil2f2dc052009-10-06 11:31:09 -07002504 if (err < 0)
Sage Weil9abf82b2010-05-10 21:58:38 -07002505 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07002506
2507 /*
2508 * snaprealms. we provide mds with the ino, seq (version), and
2509 * parent for all of our realms. If the mds has any newer info,
2510 * it will tell us.
2511 */
Sage Weila105f002010-02-15 14:37:55 -08002512 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
2513 struct ceph_snap_realm *realm =
2514 rb_entry(p, struct ceph_snap_realm, node);
Sage Weil93cea5b2009-12-23 12:21:51 -08002515 struct ceph_mds_snaprealm_reconnect sr_rec;
Sage Weil2f2dc052009-10-06 11:31:09 -07002516
2517 dout(" adding snap realm %llx seq %lld parent %llx\n",
2518 realm->ino, realm->seq, realm->parent_ino);
Sage Weil93cea5b2009-12-23 12:21:51 -08002519 sr_rec.ino = cpu_to_le64(realm->ino);
2520 sr_rec.seq = cpu_to_le64(realm->seq);
2521 sr_rec.parent = cpu_to_le64(realm->parent_ino);
2522 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
2523 if (err)
2524 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07002525 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002526
Sage Weil93cea5b2009-12-23 12:21:51 -08002527 reply->pagelist = pagelist;
Sage Weil20cb34a2010-05-12 15:21:32 -07002528 if (recon_state.flock)
2529 reply->hdr.version = cpu_to_le16(2);
Sage Weil93cea5b2009-12-23 12:21:51 -08002530 reply->hdr.data_len = cpu_to_le32(pagelist->length);
2531 reply->nr_pages = calc_pages_for(0, pagelist->length);
Sage Weil2f2dc052009-10-06 11:31:09 -07002532 ceph_con_send(&session->s_con, reply);
2533
Sage Weil9abf82b2010-05-10 21:58:38 -07002534 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002535
Sage Weil9abf82b2010-05-10 21:58:38 -07002536 mutex_lock(&mdsc->mutex);
2537 __wake_requests(mdsc, &session->s_waiting);
2538 mutex_unlock(&mdsc->mutex);
2539
Sage Weil2f2dc052009-10-06 11:31:09 -07002540 up_read(&mdsc->snap_rwsem);
Sage Weil2f2dc052009-10-06 11:31:09 -07002541 return;
2542
Sage Weil93cea5b2009-12-23 12:21:51 -08002543fail:
Sage Weil2f2dc052009-10-06 11:31:09 -07002544 ceph_msg_put(reply);
Sage Weil9abf82b2010-05-10 21:58:38 -07002545 up_read(&mdsc->snap_rwsem);
2546 mutex_unlock(&session->s_mutex);
Sage Weil93cea5b2009-12-23 12:21:51 -08002547fail_nomsg:
2548 ceph_pagelist_release(pagelist);
2549 kfree(pagelist);
2550fail_nopagelist:
Sage Weil9abf82b2010-05-10 21:58:38 -07002551 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
Sage Weil9abf82b2010-05-10 21:58:38 -07002552 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07002553}
2554
2555
2556/*
2557 * compare old and new mdsmaps, kicking requests
2558 * and closing out old connections as necessary
2559 *
2560 * called under mdsc->mutex.
2561 */
2562static void check_new_map(struct ceph_mds_client *mdsc,
2563 struct ceph_mdsmap *newmap,
2564 struct ceph_mdsmap *oldmap)
2565{
2566 int i;
2567 int oldstate, newstate;
2568 struct ceph_mds_session *s;
2569
2570 dout("check_new_map new %u old %u\n",
2571 newmap->m_epoch, oldmap->m_epoch);
2572
2573 for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
2574 if (mdsc->sessions[i] == NULL)
2575 continue;
2576 s = mdsc->sessions[i];
2577 oldstate = ceph_mdsmap_get_state(oldmap, i);
2578 newstate = ceph_mdsmap_get_state(newmap, i);
2579
Sage Weil0deb01c2010-06-17 14:19:01 -07002580 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002581 i, ceph_mds_state_name(oldstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07002582 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
Sage Weil2f2dc052009-10-06 11:31:09 -07002583 ceph_mds_state_name(newstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07002584 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
Sage Weil2f2dc052009-10-06 11:31:09 -07002585 session_state_name(s->s_state));
2586
2587 if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
2588 ceph_mdsmap_get_addr(newmap, i),
2589 sizeof(struct ceph_entity_addr))) {
2590 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
2591 /* the session never opened, just close it
2592 * out now */
2593 __wake_requests(mdsc, &s->s_waiting);
Sage Weil2600d2d2010-02-22 15:12:16 -08002594 __unregister_session(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07002595 } else {
2596 /* just close it */
2597 mutex_unlock(&mdsc->mutex);
2598 mutex_lock(&s->s_mutex);
2599 mutex_lock(&mdsc->mutex);
2600 ceph_con_close(&s->s_con);
2601 mutex_unlock(&s->s_mutex);
2602 s->s_state = CEPH_MDS_SESSION_RESTARTING;
2603 }
2604
2605 /* kick any requests waiting on the recovering mds */
Sage Weil29790f22010-03-18 14:45:05 -07002606 kick_requests(mdsc, i);
Sage Weil2f2dc052009-10-06 11:31:09 -07002607 } else if (oldstate == newstate) {
2608 continue; /* nothing new with this mds */
2609 }
2610
2611 /*
2612 * send reconnect?
2613 */
2614 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
Sage Weil34b6c852010-05-10 16:31:25 -07002615 newstate >= CEPH_MDS_STATE_RECONNECT) {
2616 mutex_unlock(&mdsc->mutex);
2617 send_mds_reconnect(mdsc, s);
2618 mutex_lock(&mdsc->mutex);
2619 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002620
2621 /*
Sage Weil29790f22010-03-18 14:45:05 -07002622 * kick request on any mds that has gone active.
Sage Weil2f2dc052009-10-06 11:31:09 -07002623 */
2624 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
2625 newstate >= CEPH_MDS_STATE_ACTIVE) {
Sage Weil29790f22010-03-18 14:45:05 -07002626 if (oldstate != CEPH_MDS_STATE_CREATING &&
2627 oldstate != CEPH_MDS_STATE_STARTING)
2628 pr_info("mds%d recovery completed\n", s->s_mds);
2629 kick_requests(mdsc, i);
Sage Weil2f2dc052009-10-06 11:31:09 -07002630 ceph_kick_flushing_caps(mdsc, s);
Sage Weil0dc25702009-11-20 13:43:45 -08002631 wake_up_session_caps(s, 1);
Sage Weil2f2dc052009-10-06 11:31:09 -07002632 }
2633 }
Sage Weilcb170a22010-06-21 13:38:35 -07002634
2635 for (i = 0; i < newmap->m_max_mds && i < mdsc->max_sessions; i++) {
2636 s = mdsc->sessions[i];
2637 if (!s)
2638 continue;
2639 if (!ceph_mdsmap_is_laggy(newmap, i))
2640 continue;
2641 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2642 s->s_state == CEPH_MDS_SESSION_HUNG ||
2643 s->s_state == CEPH_MDS_SESSION_CLOSING) {
2644 dout(" connecting to export targets of laggy mds%d\n",
2645 i);
2646 __open_export_target_sessions(mdsc, s);
2647 }
2648 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002649}
2650
2651
2652
2653/*
2654 * leases
2655 */
2656
2657/*
2658 * caller must hold session s_mutex, dentry->d_lock
2659 */
2660void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
2661{
2662 struct ceph_dentry_info *di = ceph_dentry(dentry);
2663
2664 ceph_put_mds_session(di->lease_session);
2665 di->lease_session = NULL;
2666}
2667
Sage Weil2600d2d2010-02-22 15:12:16 -08002668static void handle_lease(struct ceph_mds_client *mdsc,
2669 struct ceph_mds_session *session,
2670 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002671{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002672 struct super_block *sb = mdsc->fsc->sb;
Sage Weil2f2dc052009-10-06 11:31:09 -07002673 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07002674 struct ceph_inode_info *ci;
2675 struct dentry *parent, *dentry;
2676 struct ceph_dentry_info *di;
Sage Weil2600d2d2010-02-22 15:12:16 -08002677 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002678 struct ceph_mds_lease *h = msg->front.iov_base;
Sage Weil1e5ea232010-06-04 10:05:40 -07002679 u32 seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002680 struct ceph_vino vino;
2681 int mask;
2682 struct qstr dname;
2683 int release = 0;
2684
Sage Weil2f2dc052009-10-06 11:31:09 -07002685 dout("handle_lease from mds%d\n", mds);
2686
2687 /* decode */
2688 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
2689 goto bad;
2690 vino.ino = le64_to_cpu(h->ino);
2691 vino.snap = CEPH_NOSNAP;
2692 mask = le16_to_cpu(h->mask);
Sage Weil1e5ea232010-06-04 10:05:40 -07002693 seq = le32_to_cpu(h->seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002694 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
2695 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
2696 if (dname.len != get_unaligned_le32(h+1))
2697 goto bad;
2698
Sage Weil2f2dc052009-10-06 11:31:09 -07002699 mutex_lock(&session->s_mutex);
2700 session->s_seq++;
2701
2702 /* lookup inode */
2703 inode = ceph_find_inode(sb, vino);
Sage Weil1e5ea232010-06-04 10:05:40 -07002704 dout("handle_lease %s, mask %d, ino %llx %p %.*s\n",
2705 ceph_lease_op_name(h->action), mask, vino.ino, inode,
2706 dname.len, dname.name);
Sage Weil2f2dc052009-10-06 11:31:09 -07002707 if (inode == NULL) {
2708 dout("handle_lease no inode %llx\n", vino.ino);
2709 goto release;
2710 }
2711 ci = ceph_inode(inode);
2712
2713 /* dentry */
2714 parent = d_find_alias(inode);
2715 if (!parent) {
2716 dout("no parent dentry on inode %p\n", inode);
2717 WARN_ON(1);
2718 goto release; /* hrm... */
2719 }
2720 dname.hash = full_name_hash(dname.name, dname.len);
2721 dentry = d_lookup(parent, &dname);
2722 dput(parent);
2723 if (!dentry)
2724 goto release;
2725
2726 spin_lock(&dentry->d_lock);
2727 di = ceph_dentry(dentry);
2728 switch (h->action) {
2729 case CEPH_MDS_LEASE_REVOKE:
2730 if (di && di->lease_session == session) {
Sage Weil1e5ea232010-06-04 10:05:40 -07002731 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
2732 h->seq = cpu_to_le32(di->lease_seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002733 __ceph_mdsc_drop_dentry_lease(dentry);
2734 }
2735 release = 1;
2736 break;
2737
2738 case CEPH_MDS_LEASE_RENEW:
2739 if (di && di->lease_session == session &&
2740 di->lease_gen == session->s_cap_gen &&
2741 di->lease_renew_from &&
2742 di->lease_renew_after == 0) {
2743 unsigned long duration =
2744 le32_to_cpu(h->duration_ms) * HZ / 1000;
2745
Sage Weil1e5ea232010-06-04 10:05:40 -07002746 di->lease_seq = seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002747 dentry->d_time = di->lease_renew_from + duration;
2748 di->lease_renew_after = di->lease_renew_from +
2749 (duration >> 1);
2750 di->lease_renew_from = 0;
2751 }
2752 break;
2753 }
2754 spin_unlock(&dentry->d_lock);
2755 dput(dentry);
2756
2757 if (!release)
2758 goto out;
2759
2760release:
2761 /* let's just reuse the same message */
2762 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
2763 ceph_msg_get(msg);
2764 ceph_con_send(&session->s_con, msg);
2765
2766out:
2767 iput(inode);
2768 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002769 return;
2770
2771bad:
2772 pr_err("corrupt lease message\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002773 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002774}
2775
2776void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
2777 struct inode *inode,
2778 struct dentry *dentry, char action,
2779 u32 seq)
2780{
2781 struct ceph_msg *msg;
2782 struct ceph_mds_lease *lease;
2783 int len = sizeof(*lease) + sizeof(u32);
2784 int dnamelen = 0;
2785
2786 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
2787 inode, dentry, ceph_lease_op_name(action), session->s_mds);
2788 dnamelen = dentry->d_name.len;
2789 len += dnamelen;
2790
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002791 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -07002792 if (!msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002793 return;
2794 lease = msg->front.iov_base;
2795 lease->action = action;
Sage Weildd1c9052010-05-25 16:45:25 -07002796 lease->mask = cpu_to_le16(1);
Sage Weil2f2dc052009-10-06 11:31:09 -07002797 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
2798 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
2799 lease->seq = cpu_to_le32(seq);
2800 put_unaligned_le32(dnamelen, lease + 1);
2801 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
2802
2803 /*
2804 * if this is a preemptive lease RELEASE, no need to
2805 * flush request stream, since the actual request will
2806 * soon follow.
2807 */
2808 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
2809
2810 ceph_con_send(&session->s_con, msg);
2811}
2812
2813/*
2814 * Preemptively release a lease we expect to invalidate anyway.
2815 * Pass @inode always, @dentry is optional.
2816 */
2817void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
2818 struct dentry *dentry, int mask)
2819{
2820 struct ceph_dentry_info *di;
2821 struct ceph_mds_session *session;
2822 u32 seq;
2823
2824 BUG_ON(inode == NULL);
2825 BUG_ON(dentry == NULL);
Sage Weildd1c9052010-05-25 16:45:25 -07002826 BUG_ON(mask == 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07002827
2828 /* is dentry lease valid? */
2829 spin_lock(&dentry->d_lock);
2830 di = ceph_dentry(dentry);
2831 if (!di || !di->lease_session ||
2832 di->lease_session->s_mds < 0 ||
2833 di->lease_gen != di->lease_session->s_cap_gen ||
2834 !time_before(jiffies, dentry->d_time)) {
2835 dout("lease_release inode %p dentry %p -- "
2836 "no lease on %d\n",
2837 inode, dentry, mask);
2838 spin_unlock(&dentry->d_lock);
2839 return;
2840 }
2841
2842 /* we do have a lease on this dentry; note mds and seq */
2843 session = ceph_get_mds_session(di->lease_session);
2844 seq = di->lease_seq;
2845 __ceph_mdsc_drop_dentry_lease(dentry);
2846 spin_unlock(&dentry->d_lock);
2847
2848 dout("lease_release inode %p dentry %p mask %d to mds%d\n",
2849 inode, dentry, mask, session->s_mds);
2850 ceph_mdsc_lease_send_msg(session, inode, dentry,
2851 CEPH_MDS_LEASE_RELEASE, seq);
2852 ceph_put_mds_session(session);
2853}
2854
2855/*
2856 * drop all leases (and dentry refs) in preparation for umount
2857 */
2858static void drop_leases(struct ceph_mds_client *mdsc)
2859{
2860 int i;
2861
2862 dout("drop_leases\n");
2863 mutex_lock(&mdsc->mutex);
2864 for (i = 0; i < mdsc->max_sessions; i++) {
2865 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2866 if (!s)
2867 continue;
2868 mutex_unlock(&mdsc->mutex);
2869 mutex_lock(&s->s_mutex);
2870 mutex_unlock(&s->s_mutex);
2871 ceph_put_mds_session(s);
2872 mutex_lock(&mdsc->mutex);
2873 }
2874 mutex_unlock(&mdsc->mutex);
2875}
2876
2877
2878
2879/*
2880 * delayed work -- periodically trim expired leases, renew caps with mds
2881 */
2882static void schedule_delayed(struct ceph_mds_client *mdsc)
2883{
2884 int delay = 5;
2885 unsigned hz = round_jiffies_relative(HZ * delay);
2886 schedule_delayed_work(&mdsc->delayed_work, hz);
2887}
2888
2889static void delayed_work(struct work_struct *work)
2890{
2891 int i;
2892 struct ceph_mds_client *mdsc =
2893 container_of(work, struct ceph_mds_client, delayed_work.work);
2894 int renew_interval;
2895 int renew_caps;
2896
2897 dout("mdsc delayed_work\n");
Sage Weilafcdaea2009-10-14 14:27:38 -07002898 ceph_check_delayed_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07002899
2900 mutex_lock(&mdsc->mutex);
2901 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
2902 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
2903 mdsc->last_renew_caps);
2904 if (renew_caps)
2905 mdsc->last_renew_caps = jiffies;
2906
2907 for (i = 0; i < mdsc->max_sessions; i++) {
2908 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2909 if (s == NULL)
2910 continue;
2911 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
2912 dout("resending session close request for mds%d\n",
2913 s->s_mds);
2914 request_close_session(mdsc, s);
2915 ceph_put_mds_session(s);
2916 continue;
2917 }
2918 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
2919 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
2920 s->s_state = CEPH_MDS_SESSION_HUNG;
2921 pr_info("mds%d hung\n", s->s_mds);
2922 }
2923 }
2924 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
2925 /* this mds is failed or recovering, just wait */
2926 ceph_put_mds_session(s);
2927 continue;
2928 }
2929 mutex_unlock(&mdsc->mutex);
2930
2931 mutex_lock(&s->s_mutex);
2932 if (renew_caps)
2933 send_renew_caps(mdsc, s);
2934 else
2935 ceph_con_keepalive(&s->s_con);
Sage Weilee6b2722010-06-10 12:55:52 -07002936 ceph_add_cap_releases(mdsc, s);
Sage Weilaab53dd2010-03-17 16:30:21 -07002937 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2938 s->s_state == CEPH_MDS_SESSION_HUNG)
Sage Weil3d7ded42010-06-09 16:47:10 -07002939 ceph_send_cap_releases(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07002940 mutex_unlock(&s->s_mutex);
2941 ceph_put_mds_session(s);
2942
2943 mutex_lock(&mdsc->mutex);
2944 }
2945 mutex_unlock(&mdsc->mutex);
2946
2947 schedule_delayed(mdsc);
2948}
2949
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002950int ceph_mdsc_init(struct ceph_fs_client *fsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07002951
Sage Weil2f2dc052009-10-06 11:31:09 -07002952{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002953 struct ceph_mds_client *mdsc;
2954
2955 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
2956 if (!mdsc)
2957 return -ENOMEM;
2958 mdsc->fsc = fsc;
2959 fsc->mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07002960 mutex_init(&mdsc->mutex);
2961 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08002962 if (mdsc->mdsmap == NULL)
2963 return -ENOMEM;
2964
Sage Weil2f2dc052009-10-06 11:31:09 -07002965 init_completion(&mdsc->safe_umount_waiters);
Sage Weilf3c60c52010-08-11 14:51:23 -07002966 init_waitqueue_head(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002967 INIT_LIST_HEAD(&mdsc->waiting_for_map);
2968 mdsc->sessions = NULL;
2969 mdsc->max_sessions = 0;
2970 mdsc->stopping = 0;
2971 init_rwsem(&mdsc->snap_rwsem);
Sage Weila105f002010-02-15 14:37:55 -08002972 mdsc->snap_realms = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07002973 INIT_LIST_HEAD(&mdsc->snap_empty);
2974 spin_lock_init(&mdsc->snap_empty_lock);
2975 mdsc->last_tid = 0;
Sage Weil44ca18f2010-02-15 12:08:46 -08002976 mdsc->request_tree = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07002977 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
2978 mdsc->last_renew_caps = jiffies;
2979 INIT_LIST_HEAD(&mdsc->cap_delay_list);
2980 spin_lock_init(&mdsc->cap_delay_lock);
2981 INIT_LIST_HEAD(&mdsc->snap_flush_list);
2982 spin_lock_init(&mdsc->snap_flush_lock);
2983 mdsc->cap_flush_seq = 0;
2984 INIT_LIST_HEAD(&mdsc->cap_dirty);
2985 mdsc->num_cap_flushing = 0;
2986 spin_lock_init(&mdsc->cap_dirty_lock);
2987 init_waitqueue_head(&mdsc->cap_flushing_wq);
2988 spin_lock_init(&mdsc->dentry_lru_lock);
2989 INIT_LIST_HEAD(&mdsc->dentry_lru);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08002990
Yehuda Sadeh37151662010-06-17 16:16:12 -07002991 ceph_caps_init(mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002992 ceph_adjust_min_caps(mdsc, fsc->min_caps);
Yehuda Sadeh37151662010-06-17 16:16:12 -07002993
Sage Weil5f44f142009-11-18 14:52:18 -08002994 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002995}
2996
2997/*
2998 * Wait for safe replies on open mds requests. If we time out, drop
2999 * all requests from the tree to avoid dangling dentry refs.
3000 */
3001static void wait_requests(struct ceph_mds_client *mdsc)
3002{
3003 struct ceph_mds_request *req;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003004 struct ceph_fs_client *fsc = mdsc->fsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07003005
3006 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003007 if (__get_oldest_req(mdsc)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003008 mutex_unlock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003009
Sage Weil2f2dc052009-10-06 11:31:09 -07003010 dout("wait_requests waiting for requests\n");
3011 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003012 fsc->client->options->mount_timeout * HZ);
Sage Weil2f2dc052009-10-06 11:31:09 -07003013
3014 /* tear down remaining requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08003015 mutex_lock(&mdsc->mutex);
3016 while ((req = __get_oldest_req(mdsc))) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003017 dout("wait_requests timed out on tid %llu\n",
3018 req->r_tid);
Sage Weil44ca18f2010-02-15 12:08:46 -08003019 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07003020 }
3021 }
3022 mutex_unlock(&mdsc->mutex);
3023 dout("wait_requests done\n");
3024}
3025
3026/*
3027 * called before mount is ro, and before dentries are torn down.
3028 * (hmm, does this still race with new lookups?)
3029 */
3030void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
3031{
3032 dout("pre_umount\n");
3033 mdsc->stopping = 1;
3034
3035 drop_leases(mdsc);
Sage Weilafcdaea2009-10-14 14:27:38 -07003036 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003037 wait_requests(mdsc);
Sage Weil17c688c2010-06-21 16:12:26 -07003038
3039 /*
3040 * wait for reply handlers to drop their request refs and
3041 * their inode/dcache refs
3042 */
3043 ceph_msgr_flush();
Sage Weil2f2dc052009-10-06 11:31:09 -07003044}
3045
3046/*
3047 * wait for all write mds requests to flush.
3048 */
3049static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
3050{
Sage Weil80fc7312010-03-16 15:28:54 -07003051 struct ceph_mds_request *req = NULL, *nextreq;
Sage Weil44ca18f2010-02-15 12:08:46 -08003052 struct rb_node *n;
Sage Weil2f2dc052009-10-06 11:31:09 -07003053
3054 mutex_lock(&mdsc->mutex);
3055 dout("wait_unsafe_requests want %lld\n", want_tid);
Sage Weil80fc7312010-03-16 15:28:54 -07003056restart:
Sage Weil44ca18f2010-02-15 12:08:46 -08003057 req = __get_oldest_req(mdsc);
3058 while (req && req->r_tid <= want_tid) {
Sage Weil80fc7312010-03-16 15:28:54 -07003059 /* find next request */
3060 n = rb_next(&req->r_node);
3061 if (n)
3062 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
3063 else
3064 nextreq = NULL;
Sage Weil44ca18f2010-02-15 12:08:46 -08003065 if ((req->r_op & CEPH_MDS_OP_WRITE)) {
3066 /* write op */
3067 ceph_mdsc_get_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07003068 if (nextreq)
3069 ceph_mdsc_get_request(nextreq);
Sage Weil44ca18f2010-02-15 12:08:46 -08003070 mutex_unlock(&mdsc->mutex);
3071 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3072 req->r_tid, want_tid);
3073 wait_for_completion(&req->r_safe_completion);
3074 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003075 ceph_mdsc_put_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07003076 if (!nextreq)
3077 break; /* next dne before, so we're done! */
3078 if (RB_EMPTY_NODE(&nextreq->r_node)) {
3079 /* next request was removed from tree */
3080 ceph_mdsc_put_request(nextreq);
3081 goto restart;
3082 }
3083 ceph_mdsc_put_request(nextreq); /* won't go away */
Sage Weil44ca18f2010-02-15 12:08:46 -08003084 }
Sage Weil80fc7312010-03-16 15:28:54 -07003085 req = nextreq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003086 }
3087 mutex_unlock(&mdsc->mutex);
3088 dout("wait_unsafe_requests done\n");
3089}
3090
3091void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
3092{
3093 u64 want_tid, want_flush;
3094
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003095 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
Sage Weil56b7cf92010-05-03 15:22:00 -07003096 return;
3097
Sage Weil2f2dc052009-10-06 11:31:09 -07003098 dout("sync\n");
3099 mutex_lock(&mdsc->mutex);
3100 want_tid = mdsc->last_tid;
3101 want_flush = mdsc->cap_flush_seq;
3102 mutex_unlock(&mdsc->mutex);
3103 dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
3104
Sage Weilafcdaea2009-10-14 14:27:38 -07003105 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003106
3107 wait_unsafe_requests(mdsc, want_tid);
3108 wait_event(mdsc->cap_flushing_wq, check_cap_flush(mdsc, want_flush));
3109}
3110
Sage Weilf3c60c52010-08-11 14:51:23 -07003111/*
3112 * true if all sessions are closed, or we force unmount
3113 */
3114bool done_closing_sessions(struct ceph_mds_client *mdsc)
3115{
3116 int i, n = 0;
3117
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003118 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
Sage Weilf3c60c52010-08-11 14:51:23 -07003119 return true;
3120
3121 mutex_lock(&mdsc->mutex);
3122 for (i = 0; i < mdsc->max_sessions; i++)
3123 if (mdsc->sessions[i])
3124 n++;
3125 mutex_unlock(&mdsc->mutex);
3126 return n == 0;
3127}
Sage Weil2f2dc052009-10-06 11:31:09 -07003128
3129/*
3130 * called after sb is ro.
3131 */
3132void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
3133{
3134 struct ceph_mds_session *session;
3135 int i;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003136 struct ceph_fs_client *fsc = mdsc->fsc;
3137 unsigned long timeout = fsc->client->options->mount_timeout * HZ;
Sage Weil2f2dc052009-10-06 11:31:09 -07003138
3139 dout("close_sessions\n");
3140
Sage Weil2f2dc052009-10-06 11:31:09 -07003141 /* close sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07003142 mutex_lock(&mdsc->mutex);
3143 for (i = 0; i < mdsc->max_sessions; i++) {
3144 session = __ceph_lookup_mds_session(mdsc, i);
3145 if (!session)
3146 continue;
Sage Weil2f2dc052009-10-06 11:31:09 -07003147 mutex_unlock(&mdsc->mutex);
Sage Weilf3c60c52010-08-11 14:51:23 -07003148 mutex_lock(&session->s_mutex);
3149 __close_session(mdsc, session);
3150 mutex_unlock(&session->s_mutex);
3151 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003152 mutex_lock(&mdsc->mutex);
3153 }
Sage Weilf3c60c52010-08-11 14:51:23 -07003154 mutex_unlock(&mdsc->mutex);
3155
3156 dout("waiting for sessions to close\n");
3157 wait_event_timeout(mdsc->session_close_wq, done_closing_sessions(mdsc),
3158 timeout);
Sage Weil2f2dc052009-10-06 11:31:09 -07003159
3160 /* tear down remaining sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07003161 mutex_lock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003162 for (i = 0; i < mdsc->max_sessions; i++) {
3163 if (mdsc->sessions[i]) {
3164 session = get_session(mdsc->sessions[i]);
Sage Weil2600d2d2010-02-22 15:12:16 -08003165 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003166 mutex_unlock(&mdsc->mutex);
3167 mutex_lock(&session->s_mutex);
3168 remove_session_caps(session);
3169 mutex_unlock(&session->s_mutex);
3170 ceph_put_mds_session(session);
3171 mutex_lock(&mdsc->mutex);
3172 }
3173 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003174 WARN_ON(!list_empty(&mdsc->cap_delay_list));
Sage Weil2f2dc052009-10-06 11:31:09 -07003175 mutex_unlock(&mdsc->mutex);
3176
3177 ceph_cleanup_empty_realms(mdsc);
3178
3179 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3180
3181 dout("stopped\n");
3182}
3183
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003184static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07003185{
3186 dout("stop\n");
3187 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3188 if (mdsc->mdsmap)
3189 ceph_mdsmap_destroy(mdsc->mdsmap);
3190 kfree(mdsc->sessions);
Yehuda Sadeh37151662010-06-17 16:16:12 -07003191 ceph_caps_finalize(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003192}
3193
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003194void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
3195{
3196 struct ceph_mds_client *mdsc = fsc->mdsc;
3197
3198 ceph_mdsc_stop(mdsc);
3199 fsc->mdsc = NULL;
3200 kfree(mdsc);
3201}
3202
Sage Weil2f2dc052009-10-06 11:31:09 -07003203
3204/*
3205 * handle mds map update.
3206 */
3207void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3208{
3209 u32 epoch;
3210 u32 maplen;
3211 void *p = msg->front.iov_base;
3212 void *end = p + msg->front.iov_len;
3213 struct ceph_mdsmap *newmap, *oldmap;
3214 struct ceph_fsid fsid;
3215 int err = -EINVAL;
3216
3217 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
3218 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003219 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
Sage Weil07433042009-11-18 16:50:41 -08003220 return;
Sage Weilc89136e2009-10-14 09:59:09 -07003221 epoch = ceph_decode_32(&p);
3222 maplen = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07003223 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
3224
3225 /* do we need it? */
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003226 ceph_monc_got_mdsmap(&mdsc->fsc->client->monc, epoch);
Sage Weil2f2dc052009-10-06 11:31:09 -07003227 mutex_lock(&mdsc->mutex);
3228 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
3229 dout("handle_map epoch %u <= our %u\n",
3230 epoch, mdsc->mdsmap->m_epoch);
3231 mutex_unlock(&mdsc->mutex);
3232 return;
3233 }
3234
3235 newmap = ceph_mdsmap_decode(&p, end);
3236 if (IS_ERR(newmap)) {
3237 err = PTR_ERR(newmap);
3238 goto bad_unlock;
3239 }
3240
3241 /* swap into place */
3242 if (mdsc->mdsmap) {
3243 oldmap = mdsc->mdsmap;
3244 mdsc->mdsmap = newmap;
3245 check_new_map(mdsc, newmap, oldmap);
3246 ceph_mdsmap_destroy(oldmap);
3247 } else {
3248 mdsc->mdsmap = newmap; /* first mds map */
3249 }
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003250 mdsc->fsc->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
Sage Weil2f2dc052009-10-06 11:31:09 -07003251
3252 __wake_requests(mdsc, &mdsc->waiting_for_map);
3253
3254 mutex_unlock(&mdsc->mutex);
3255 schedule_delayed(mdsc);
3256 return;
3257
3258bad_unlock:
3259 mutex_unlock(&mdsc->mutex);
3260bad:
3261 pr_err("error decoding mdsmap %d\n", err);
3262 return;
3263}
3264
3265static struct ceph_connection *con_get(struct ceph_connection *con)
3266{
3267 struct ceph_mds_session *s = con->private;
3268
3269 if (get_session(s)) {
Sage Weil2600d2d2010-02-22 15:12:16 -08003270 dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -07003271 return con;
3272 }
3273 dout("mdsc con_get %p FAIL\n", s);
3274 return NULL;
3275}
3276
3277static void con_put(struct ceph_connection *con)
3278{
3279 struct ceph_mds_session *s = con->private;
3280
Sage Weil2f2dc052009-10-06 11:31:09 -07003281 ceph_put_mds_session(s);
Sage Weil2600d2d2010-02-22 15:12:16 -08003282 dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -07003283}
3284
3285/*
3286 * if the client is unresponsive for long enough, the mds will kill
3287 * the session entirely.
3288 */
3289static void peer_reset(struct ceph_connection *con)
3290{
3291 struct ceph_mds_session *s = con->private;
Sage Weil7e70f0e2010-03-18 13:59:12 -07003292 struct ceph_mds_client *mdsc = s->s_mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07003293
Sage Weil7e70f0e2010-03-18 13:59:12 -07003294 pr_warning("mds%d closed our session\n", s->s_mds);
3295 send_mds_reconnect(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07003296}
3297
3298static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
3299{
3300 struct ceph_mds_session *s = con->private;
3301 struct ceph_mds_client *mdsc = s->s_mdsc;
3302 int type = le16_to_cpu(msg->hdr.type);
3303
Sage Weil2600d2d2010-02-22 15:12:16 -08003304 mutex_lock(&mdsc->mutex);
3305 if (__verify_registered_session(mdsc, s) < 0) {
3306 mutex_unlock(&mdsc->mutex);
3307 goto out;
3308 }
3309 mutex_unlock(&mdsc->mutex);
3310
Sage Weil2f2dc052009-10-06 11:31:09 -07003311 switch (type) {
3312 case CEPH_MSG_MDS_MAP:
3313 ceph_mdsc_handle_map(mdsc, msg);
3314 break;
3315 case CEPH_MSG_CLIENT_SESSION:
3316 handle_session(s, msg);
3317 break;
3318 case CEPH_MSG_CLIENT_REPLY:
3319 handle_reply(s, msg);
3320 break;
3321 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
Sage Weil2600d2d2010-02-22 15:12:16 -08003322 handle_forward(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003323 break;
3324 case CEPH_MSG_CLIENT_CAPS:
3325 ceph_handle_caps(s, msg);
3326 break;
3327 case CEPH_MSG_CLIENT_SNAP:
Sage Weil2600d2d2010-02-22 15:12:16 -08003328 ceph_handle_snap(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003329 break;
3330 case CEPH_MSG_CLIENT_LEASE:
Sage Weil2600d2d2010-02-22 15:12:16 -08003331 handle_lease(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003332 break;
3333
3334 default:
3335 pr_err("received unknown message type %d %s\n", type,
3336 ceph_msg_type_name(type));
3337 }
Sage Weil2600d2d2010-02-22 15:12:16 -08003338out:
Sage Weil2f2dc052009-10-06 11:31:09 -07003339 ceph_msg_put(msg);
3340}
3341
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003342/*
3343 * authentication
3344 */
3345static int get_authorizer(struct ceph_connection *con,
3346 void **buf, int *len, int *proto,
3347 void **reply_buf, int *reply_len, int force_new)
3348{
3349 struct ceph_mds_session *s = con->private;
3350 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003351 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003352 int ret = 0;
3353
3354 if (force_new && s->s_authorizer) {
3355 ac->ops->destroy_authorizer(ac, s->s_authorizer);
3356 s->s_authorizer = NULL;
3357 }
3358 if (s->s_authorizer == NULL) {
3359 if (ac->ops->create_authorizer) {
3360 ret = ac->ops->create_authorizer(
3361 ac, CEPH_ENTITY_TYPE_MDS,
3362 &s->s_authorizer,
3363 &s->s_authorizer_buf,
3364 &s->s_authorizer_buf_len,
3365 &s->s_authorizer_reply_buf,
3366 &s->s_authorizer_reply_buf_len);
3367 if (ret)
3368 return ret;
3369 }
3370 }
3371
3372 *proto = ac->protocol;
3373 *buf = s->s_authorizer_buf;
3374 *len = s->s_authorizer_buf_len;
3375 *reply_buf = s->s_authorizer_reply_buf;
3376 *reply_len = s->s_authorizer_reply_buf_len;
3377 return 0;
3378}
3379
3380
3381static int verify_authorizer_reply(struct ceph_connection *con, int len)
3382{
3383 struct ceph_mds_session *s = con->private;
3384 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003385 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003386
3387 return ac->ops->verify_authorizer_reply(ac, s->s_authorizer, len);
3388}
3389
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003390static int invalidate_authorizer(struct ceph_connection *con)
3391{
3392 struct ceph_mds_session *s = con->private;
3393 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003394 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003395
3396 if (ac->ops->invalidate_authorizer)
3397 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
3398
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003399 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003400}
3401
Tobias Klauser9e327892010-05-20 10:40:19 +02003402static const struct ceph_connection_operations mds_con_ops = {
Sage Weil2f2dc052009-10-06 11:31:09 -07003403 .get = con_get,
3404 .put = con_put,
3405 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003406 .get_authorizer = get_authorizer,
3407 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003408 .invalidate_authorizer = invalidate_authorizer,
Sage Weil2f2dc052009-10-06 11:31:09 -07003409 .peer_reset = peer_reset,
Sage Weil2f2dc052009-10-06 11:31:09 -07003410};
3411
Sage Weil2f2dc052009-10-06 11:31:09 -07003412/* eof */