blob: 641a8a37e7b3f790304a398fd44f6e3d5b964053 [file] [log] [blame]
Sage Weil2f2dc052009-10-06 11:31:09 -07001#include "ceph_debug.h"
2
3#include <linux/wait.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09004#include <linux/slab.h>
Sage Weil2f2dc052009-10-06 11:31:09 -07005#include <linux/sched.h>
6
7#include "mds_client.h"
8#include "mon_client.h"
9#include "super.h"
10#include "messenger.h"
11#include "decode.h"
Sage Weil4e7a5dc2009-11-18 16:19:57 -080012#include "auth.h"
Sage Weil93cea5b2009-12-23 12:21:51 -080013#include "pagelist.h"
Sage Weil2f2dc052009-10-06 11:31:09 -070014
15/*
16 * A cluster of MDS (metadata server) daemons is responsible for
17 * managing the file system namespace (the directory hierarchy and
18 * inodes) and for coordinating shared access to storage. Metadata is
19 * partitioning hierarchically across a number of servers, and that
20 * partition varies over time as the cluster adjusts the distribution
21 * in order to balance load.
22 *
23 * The MDS client is primarily responsible to managing synchronous
24 * metadata requests for operations like open, unlink, and so forth.
25 * If there is a MDS failure, we find out about it when we (possibly
26 * request and) receive a new MDS map, and can resubmit affected
27 * requests.
28 *
29 * For the most part, though, we take advantage of a lossless
30 * communications channel to the MDS, and do not need to worry about
31 * timing out or resubmitting requests.
32 *
33 * We maintain a stateful "session" with each MDS we interact with.
34 * Within each session, we sent periodic heartbeat messages to ensure
35 * any capabilities or leases we have been issues remain valid. If
36 * the session times out and goes stale, our leases and capabilities
37 * are no longer valid.
38 */
39
40static void __wake_requests(struct ceph_mds_client *mdsc,
41 struct list_head *head);
42
Tobias Klauser9e327892010-05-20 10:40:19 +020043static const struct ceph_connection_operations mds_con_ops;
Sage Weil2f2dc052009-10-06 11:31:09 -070044
45
46/*
47 * mds reply parsing
48 */
49
50/*
51 * parse individual inode info
52 */
53static int parse_reply_info_in(void **p, void *end,
54 struct ceph_mds_reply_info_in *info)
55{
56 int err = -EIO;
57
58 info->in = *p;
59 *p += sizeof(struct ceph_mds_reply_inode) +
60 sizeof(*info->in->fragtree.splits) *
61 le32_to_cpu(info->in->fragtree.nsplits);
62
63 ceph_decode_32_safe(p, end, info->symlink_len, bad);
64 ceph_decode_need(p, end, info->symlink_len, bad);
65 info->symlink = *p;
66 *p += info->symlink_len;
67
68 ceph_decode_32_safe(p, end, info->xattr_len, bad);
69 ceph_decode_need(p, end, info->xattr_len, bad);
70 info->xattr_data = *p;
71 *p += info->xattr_len;
72 return 0;
73bad:
74 return err;
75}
76
77/*
78 * parse a normal reply, which may contain a (dir+)dentry and/or a
79 * target inode.
80 */
81static int parse_reply_info_trace(void **p, void *end,
82 struct ceph_mds_reply_info_parsed *info)
83{
84 int err;
85
86 if (info->head->is_dentry) {
87 err = parse_reply_info_in(p, end, &info->diri);
88 if (err < 0)
89 goto out_bad;
90
91 if (unlikely(*p + sizeof(*info->dirfrag) > end))
92 goto bad;
93 info->dirfrag = *p;
94 *p += sizeof(*info->dirfrag) +
95 sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
96 if (unlikely(*p > end))
97 goto bad;
98
99 ceph_decode_32_safe(p, end, info->dname_len, bad);
100 ceph_decode_need(p, end, info->dname_len, bad);
101 info->dname = *p;
102 *p += info->dname_len;
103 info->dlease = *p;
104 *p += sizeof(*info->dlease);
105 }
106
107 if (info->head->is_target) {
108 err = parse_reply_info_in(p, end, &info->targeti);
109 if (err < 0)
110 goto out_bad;
111 }
112
113 if (unlikely(*p != end))
114 goto bad;
115 return 0;
116
117bad:
118 err = -EIO;
119out_bad:
120 pr_err("problem parsing mds trace %d\n", err);
121 return err;
122}
123
124/*
125 * parse readdir results
126 */
127static int parse_reply_info_dir(void **p, void *end,
128 struct ceph_mds_reply_info_parsed *info)
129{
130 u32 num, i = 0;
131 int err;
132
133 info->dir_dir = *p;
134 if (*p + sizeof(*info->dir_dir) > end)
135 goto bad;
136 *p += sizeof(*info->dir_dir) +
137 sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
138 if (*p > end)
139 goto bad;
140
141 ceph_decode_need(p, end, sizeof(num) + 2, bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700142 num = ceph_decode_32(p);
143 info->dir_end = ceph_decode_8(p);
144 info->dir_complete = ceph_decode_8(p);
Sage Weil2f2dc052009-10-06 11:31:09 -0700145 if (num == 0)
146 goto done;
147
148 /* alloc large array */
149 info->dir_nr = num;
150 info->dir_in = kcalloc(num, sizeof(*info->dir_in) +
151 sizeof(*info->dir_dname) +
152 sizeof(*info->dir_dname_len) +
153 sizeof(*info->dir_dlease),
154 GFP_NOFS);
155 if (info->dir_in == NULL) {
156 err = -ENOMEM;
157 goto out_bad;
158 }
159 info->dir_dname = (void *)(info->dir_in + num);
160 info->dir_dname_len = (void *)(info->dir_dname + num);
161 info->dir_dlease = (void *)(info->dir_dname_len + num);
162
163 while (num) {
164 /* dentry */
165 ceph_decode_need(p, end, sizeof(u32)*2, bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700166 info->dir_dname_len[i] = ceph_decode_32(p);
Sage Weil2f2dc052009-10-06 11:31:09 -0700167 ceph_decode_need(p, end, info->dir_dname_len[i], bad);
168 info->dir_dname[i] = *p;
169 *p += info->dir_dname_len[i];
170 dout("parsed dir dname '%.*s'\n", info->dir_dname_len[i],
171 info->dir_dname[i]);
172 info->dir_dlease[i] = *p;
173 *p += sizeof(struct ceph_mds_reply_lease);
174
175 /* inode */
176 err = parse_reply_info_in(p, end, &info->dir_in[i]);
177 if (err < 0)
178 goto out_bad;
179 i++;
180 num--;
181 }
182
183done:
184 if (*p != end)
185 goto bad;
186 return 0;
187
188bad:
189 err = -EIO;
190out_bad:
191 pr_err("problem parsing dir contents %d\n", err);
192 return err;
193}
194
195/*
196 * parse entire mds reply
197 */
198static int parse_reply_info(struct ceph_msg *msg,
199 struct ceph_mds_reply_info_parsed *info)
200{
201 void *p, *end;
202 u32 len;
203 int err;
204
205 info->head = msg->front.iov_base;
206 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
207 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
208
209 /* trace */
210 ceph_decode_32_safe(&p, end, len, bad);
211 if (len > 0) {
212 err = parse_reply_info_trace(&p, p+len, info);
213 if (err < 0)
214 goto out_bad;
215 }
216
217 /* dir content */
218 ceph_decode_32_safe(&p, end, len, bad);
219 if (len > 0) {
220 err = parse_reply_info_dir(&p, p+len, info);
221 if (err < 0)
222 goto out_bad;
223 }
224
225 /* snap blob */
226 ceph_decode_32_safe(&p, end, len, bad);
227 info->snapblob_len = len;
228 info->snapblob = p;
229 p += len;
230
231 if (p != end)
232 goto bad;
233 return 0;
234
235bad:
236 err = -EIO;
237out_bad:
238 pr_err("mds parse_reply err %d\n", err);
239 return err;
240}
241
242static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
243{
244 kfree(info->dir_in);
245}
246
247
248/*
249 * sessions
250 */
251static const char *session_state_name(int s)
252{
253 switch (s) {
254 case CEPH_MDS_SESSION_NEW: return "new";
255 case CEPH_MDS_SESSION_OPENING: return "opening";
256 case CEPH_MDS_SESSION_OPEN: return "open";
257 case CEPH_MDS_SESSION_HUNG: return "hung";
258 case CEPH_MDS_SESSION_CLOSING: return "closing";
Sage Weil44ca18f2010-02-15 12:08:46 -0800259 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
Sage Weil2f2dc052009-10-06 11:31:09 -0700260 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
261 default: return "???";
262 }
263}
264
265static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
266{
267 if (atomic_inc_not_zero(&s->s_ref)) {
268 dout("mdsc get_session %p %d -> %d\n", s,
269 atomic_read(&s->s_ref)-1, atomic_read(&s->s_ref));
270 return s;
271 } else {
272 dout("mdsc get_session %p 0 -- FAIL", s);
273 return NULL;
274 }
275}
276
277void ceph_put_mds_session(struct ceph_mds_session *s)
278{
279 dout("mdsc put_session %p %d -> %d\n", s,
280 atomic_read(&s->s_ref), atomic_read(&s->s_ref)-1);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800281 if (atomic_dec_and_test(&s->s_ref)) {
282 if (s->s_authorizer)
283 s->s_mdsc->client->monc.auth->ops->destroy_authorizer(
284 s->s_mdsc->client->monc.auth, s->s_authorizer);
Sage Weil2f2dc052009-10-06 11:31:09 -0700285 kfree(s);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800286 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700287}
288
289/*
290 * called under mdsc->mutex
291 */
292struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
293 int mds)
294{
295 struct ceph_mds_session *session;
296
297 if (mds >= mdsc->max_sessions || mdsc->sessions[mds] == NULL)
298 return NULL;
299 session = mdsc->sessions[mds];
300 dout("lookup_mds_session %p %d\n", session,
301 atomic_read(&session->s_ref));
302 get_session(session);
303 return session;
304}
305
306static bool __have_session(struct ceph_mds_client *mdsc, int mds)
307{
308 if (mds >= mdsc->max_sessions)
309 return false;
310 return mdsc->sessions[mds];
311}
312
Sage Weil2600d2d2010-02-22 15:12:16 -0800313static int __verify_registered_session(struct ceph_mds_client *mdsc,
314 struct ceph_mds_session *s)
315{
316 if (s->s_mds >= mdsc->max_sessions ||
317 mdsc->sessions[s->s_mds] != s)
318 return -ENOENT;
319 return 0;
320}
321
Sage Weil2f2dc052009-10-06 11:31:09 -0700322/*
323 * create+register a new session for given mds.
324 * called under mdsc->mutex.
325 */
326static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
327 int mds)
328{
329 struct ceph_mds_session *s;
330
331 s = kzalloc(sizeof(*s), GFP_NOFS);
Dan Carpenter4736b002010-03-20 15:30:16 +0300332 if (!s)
333 return ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -0700334 s->s_mdsc = mdsc;
335 s->s_mds = mds;
336 s->s_state = CEPH_MDS_SESSION_NEW;
337 s->s_ttl = 0;
338 s->s_seq = 0;
339 mutex_init(&s->s_mutex);
340
341 ceph_con_init(mdsc->client->msgr, &s->s_con);
342 s->s_con.private = s;
343 s->s_con.ops = &mds_con_ops;
344 s->s_con.peer_name.type = CEPH_ENTITY_TYPE_MDS;
345 s->s_con.peer_name.num = cpu_to_le64(mds);
Sage Weil2f2dc052009-10-06 11:31:09 -0700346
347 spin_lock_init(&s->s_cap_lock);
348 s->s_cap_gen = 0;
349 s->s_cap_ttl = 0;
350 s->s_renew_requested = 0;
351 s->s_renew_seq = 0;
352 INIT_LIST_HEAD(&s->s_caps);
353 s->s_nr_caps = 0;
Sage Weil5dacf092009-12-21 20:40:34 -0800354 s->s_trim_caps = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -0700355 atomic_set(&s->s_ref, 1);
356 INIT_LIST_HEAD(&s->s_waiting);
357 INIT_LIST_HEAD(&s->s_unsafe);
358 s->s_num_cap_releases = 0;
Sage Weil7c1332b2010-02-16 11:39:45 -0800359 s->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700360 INIT_LIST_HEAD(&s->s_cap_releases);
361 INIT_LIST_HEAD(&s->s_cap_releases_done);
362 INIT_LIST_HEAD(&s->s_cap_flushing);
363 INIT_LIST_HEAD(&s->s_cap_snaps_flushing);
364
365 dout("register_session mds%d\n", mds);
366 if (mds >= mdsc->max_sessions) {
367 int newmax = 1 << get_count_order(mds+1);
368 struct ceph_mds_session **sa;
369
370 dout("register_session realloc to %d\n", newmax);
371 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
372 if (sa == NULL)
Sage Weil42ce56e2009-11-18 11:22:36 -0800373 goto fail_realloc;
Sage Weil2f2dc052009-10-06 11:31:09 -0700374 if (mdsc->sessions) {
375 memcpy(sa, mdsc->sessions,
376 mdsc->max_sessions * sizeof(void *));
377 kfree(mdsc->sessions);
378 }
379 mdsc->sessions = sa;
380 mdsc->max_sessions = newmax;
381 }
382 mdsc->sessions[mds] = s;
383 atomic_inc(&s->s_ref); /* one ref to sessions[], one to caller */
Sage Weil42ce56e2009-11-18 11:22:36 -0800384
385 ceph_con_open(&s->s_con, ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
386
Sage Weil2f2dc052009-10-06 11:31:09 -0700387 return s;
Sage Weil42ce56e2009-11-18 11:22:36 -0800388
389fail_realloc:
390 kfree(s);
391 return ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -0700392}
393
394/*
395 * called under mdsc->mutex
396 */
Sage Weil2600d2d2010-02-22 15:12:16 -0800397static void __unregister_session(struct ceph_mds_client *mdsc,
Sage Weil42ce56e2009-11-18 11:22:36 -0800398 struct ceph_mds_session *s)
Sage Weil2f2dc052009-10-06 11:31:09 -0700399{
Sage Weil2600d2d2010-02-22 15:12:16 -0800400 dout("__unregister_session mds%d %p\n", s->s_mds, s);
401 BUG_ON(mdsc->sessions[s->s_mds] != s);
Sage Weil42ce56e2009-11-18 11:22:36 -0800402 mdsc->sessions[s->s_mds] = NULL;
403 ceph_con_close(&s->s_con);
404 ceph_put_mds_session(s);
Sage Weil2f2dc052009-10-06 11:31:09 -0700405}
406
407/*
408 * drop session refs in request.
409 *
410 * should be last request ref, or hold mdsc->mutex
411 */
412static void put_request_session(struct ceph_mds_request *req)
413{
414 if (req->r_session) {
415 ceph_put_mds_session(req->r_session);
416 req->r_session = NULL;
417 }
418}
419
Sage Weil153c8e62009-12-07 12:31:09 -0800420void ceph_mdsc_release_request(struct kref *kref)
Sage Weil2f2dc052009-10-06 11:31:09 -0700421{
Sage Weil153c8e62009-12-07 12:31:09 -0800422 struct ceph_mds_request *req = container_of(kref,
423 struct ceph_mds_request,
424 r_kref);
425 if (req->r_request)
426 ceph_msg_put(req->r_request);
427 if (req->r_reply) {
428 ceph_msg_put(req->r_reply);
429 destroy_reply_info(&req->r_reply_info);
Sage Weil2f2dc052009-10-06 11:31:09 -0700430 }
Sage Weil153c8e62009-12-07 12:31:09 -0800431 if (req->r_inode) {
432 ceph_put_cap_refs(ceph_inode(req->r_inode),
433 CEPH_CAP_PIN);
434 iput(req->r_inode);
435 }
436 if (req->r_locked_dir)
437 ceph_put_cap_refs(ceph_inode(req->r_locked_dir),
438 CEPH_CAP_PIN);
439 if (req->r_target_inode)
440 iput(req->r_target_inode);
441 if (req->r_dentry)
442 dput(req->r_dentry);
443 if (req->r_old_dentry) {
444 ceph_put_cap_refs(
445 ceph_inode(req->r_old_dentry->d_parent->d_inode),
446 CEPH_CAP_PIN);
447 dput(req->r_old_dentry);
448 }
449 kfree(req->r_path1);
450 kfree(req->r_path2);
451 put_request_session(req);
Yehuda Sadeh37151662010-06-17 16:16:12 -0700452 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
Sage Weil153c8e62009-12-07 12:31:09 -0800453 kfree(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700454}
455
456/*
457 * lookup session, bump ref if found.
458 *
459 * called under mdsc->mutex.
460 */
461static struct ceph_mds_request *__lookup_request(struct ceph_mds_client *mdsc,
462 u64 tid)
463{
464 struct ceph_mds_request *req;
Sage Weil44ca18f2010-02-15 12:08:46 -0800465 struct rb_node *n = mdsc->request_tree.rb_node;
466
467 while (n) {
468 req = rb_entry(n, struct ceph_mds_request, r_node);
469 if (tid < req->r_tid)
470 n = n->rb_left;
471 else if (tid > req->r_tid)
472 n = n->rb_right;
473 else {
474 ceph_mdsc_get_request(req);
475 return req;
476 }
477 }
478 return NULL;
479}
480
481static void __insert_request(struct ceph_mds_client *mdsc,
482 struct ceph_mds_request *new)
483{
484 struct rb_node **p = &mdsc->request_tree.rb_node;
485 struct rb_node *parent = NULL;
486 struct ceph_mds_request *req = NULL;
487
488 while (*p) {
489 parent = *p;
490 req = rb_entry(parent, struct ceph_mds_request, r_node);
491 if (new->r_tid < req->r_tid)
492 p = &(*p)->rb_left;
493 else if (new->r_tid > req->r_tid)
494 p = &(*p)->rb_right;
495 else
496 BUG();
497 }
498
499 rb_link_node(&new->r_node, parent, p);
500 rb_insert_color(&new->r_node, &mdsc->request_tree);
Sage Weil2f2dc052009-10-06 11:31:09 -0700501}
502
503/*
504 * Register an in-flight request, and assign a tid. Link to directory
505 * are modifying (if any).
506 *
507 * Called under mdsc->mutex.
508 */
509static void __register_request(struct ceph_mds_client *mdsc,
510 struct ceph_mds_request *req,
511 struct inode *dir)
512{
513 req->r_tid = ++mdsc->last_tid;
514 if (req->r_num_caps)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700515 ceph_reserve_caps(mdsc, &req->r_caps_reservation,
516 req->r_num_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -0700517 dout("__register_request %p tid %lld\n", req, req->r_tid);
518 ceph_mdsc_get_request(req);
Sage Weil44ca18f2010-02-15 12:08:46 -0800519 __insert_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700520
521 if (dir) {
522 struct ceph_inode_info *ci = ceph_inode(dir);
523
524 spin_lock(&ci->i_unsafe_lock);
525 req->r_unsafe_dir = dir;
526 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
527 spin_unlock(&ci->i_unsafe_lock);
528 }
529}
530
531static void __unregister_request(struct ceph_mds_client *mdsc,
532 struct ceph_mds_request *req)
533{
534 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
Sage Weil44ca18f2010-02-15 12:08:46 -0800535 rb_erase(&req->r_node, &mdsc->request_tree);
Sage Weil80fc7312010-03-16 15:28:54 -0700536 RB_CLEAR_NODE(&req->r_node);
Sage Weil2f2dc052009-10-06 11:31:09 -0700537
538 if (req->r_unsafe_dir) {
539 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
540
541 spin_lock(&ci->i_unsafe_lock);
542 list_del_init(&req->r_unsafe_dir_item);
543 spin_unlock(&ci->i_unsafe_lock);
544 }
Sage Weil94aa8ae2010-03-28 21:22:50 -0700545
546 ceph_mdsc_put_request(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700547}
548
549/*
550 * Choose mds to send request to next. If there is a hint set in the
551 * request (e.g., due to a prior forward hint from the mds), use that.
552 * Otherwise, consult frag tree and/or caps to identify the
553 * appropriate mds. If all else fails, choose randomly.
554 *
555 * Called under mdsc->mutex.
556 */
557static int __choose_mds(struct ceph_mds_client *mdsc,
558 struct ceph_mds_request *req)
559{
560 struct inode *inode;
561 struct ceph_inode_info *ci;
562 struct ceph_cap *cap;
563 int mode = req->r_direct_mode;
564 int mds = -1;
565 u32 hash = req->r_direct_hash;
566 bool is_hash = req->r_direct_is_hash;
567
568 /*
569 * is there a specific mds we should try? ignore hint if we have
570 * no session and the mds is not up (active or recovering).
571 */
572 if (req->r_resend_mds >= 0 &&
573 (__have_session(mdsc, req->r_resend_mds) ||
574 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
575 dout("choose_mds using resend_mds mds%d\n",
576 req->r_resend_mds);
577 return req->r_resend_mds;
578 }
579
580 if (mode == USE_RANDOM_MDS)
581 goto random;
582
583 inode = NULL;
584 if (req->r_inode) {
585 inode = req->r_inode;
586 } else if (req->r_dentry) {
587 if (req->r_dentry->d_inode) {
588 inode = req->r_dentry->d_inode;
589 } else {
590 inode = req->r_dentry->d_parent->d_inode;
591 hash = req->r_dentry->d_name.hash;
592 is_hash = true;
593 }
594 }
595 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
596 (int)hash, mode);
597 if (!inode)
598 goto random;
599 ci = ceph_inode(inode);
600
601 if (is_hash && S_ISDIR(inode->i_mode)) {
602 struct ceph_inode_frag frag;
603 int found;
604
605 ceph_choose_frag(ci, hash, &frag, &found);
606 if (found) {
607 if (mode == USE_ANY_MDS && frag.ndist > 0) {
608 u8 r;
609
610 /* choose a random replica */
611 get_random_bytes(&r, 1);
612 r %= frag.ndist;
613 mds = frag.dist[r];
614 dout("choose_mds %p %llx.%llx "
615 "frag %u mds%d (%d/%d)\n",
616 inode, ceph_vinop(inode),
617 frag.frag, frag.mds,
618 (int)r, frag.ndist);
619 return mds;
620 }
621
622 /* since this file/dir wasn't known to be
623 * replicated, then we want to look for the
624 * authoritative mds. */
625 mode = USE_AUTH_MDS;
626 if (frag.mds >= 0) {
627 /* choose auth mds */
628 mds = frag.mds;
629 dout("choose_mds %p %llx.%llx "
630 "frag %u mds%d (auth)\n",
631 inode, ceph_vinop(inode), frag.frag, mds);
632 return mds;
633 }
634 }
635 }
636
637 spin_lock(&inode->i_lock);
638 cap = NULL;
639 if (mode == USE_AUTH_MDS)
640 cap = ci->i_auth_cap;
641 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
642 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
643 if (!cap) {
644 spin_unlock(&inode->i_lock);
645 goto random;
646 }
647 mds = cap->session->s_mds;
648 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
649 inode, ceph_vinop(inode), mds,
650 cap == ci->i_auth_cap ? "auth " : "", cap);
651 spin_unlock(&inode->i_lock);
652 return mds;
653
654random:
655 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
656 dout("choose_mds chose random mds%d\n", mds);
657 return mds;
658}
659
660
661/*
662 * session messages
663 */
664static struct ceph_msg *create_session_msg(u32 op, u64 seq)
665{
666 struct ceph_msg *msg;
667 struct ceph_mds_session_head *h;
668
Yehuda Sadeh34d23762010-04-06 14:33:58 -0700669 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -0700670 if (!msg) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700671 pr_err("create_session_msg ENOMEM creating msg\n");
Sage Weila79832f2010-04-01 16:06:19 -0700672 return NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700673 }
674 h = msg->front.iov_base;
675 h->op = cpu_to_le32(op);
676 h->seq = cpu_to_le64(seq);
677 return msg;
678}
679
680/*
681 * send session open request.
682 *
683 * called under mdsc->mutex
684 */
685static int __open_session(struct ceph_mds_client *mdsc,
686 struct ceph_mds_session *session)
687{
688 struct ceph_msg *msg;
689 int mstate;
690 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -0700691
692 /* wait for mds to go active? */
693 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
694 dout("open_session to mds%d (%s)\n", mds,
695 ceph_mds_state_name(mstate));
696 session->s_state = CEPH_MDS_SESSION_OPENING;
697 session->s_renew_requested = jiffies;
698
699 /* send connect message */
700 msg = create_session_msg(CEPH_SESSION_REQUEST_OPEN, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -0700701 if (!msg)
702 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -0700703 ceph_con_send(&session->s_con, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -0700704 return 0;
705}
706
707/*
708 * session caps
709 */
710
711/*
712 * Free preallocated cap messages assigned to this session
713 */
714static void cleanup_cap_releases(struct ceph_mds_session *session)
715{
716 struct ceph_msg *msg;
717
718 spin_lock(&session->s_cap_lock);
719 while (!list_empty(&session->s_cap_releases)) {
720 msg = list_first_entry(&session->s_cap_releases,
721 struct ceph_msg, list_head);
722 list_del_init(&msg->list_head);
723 ceph_msg_put(msg);
724 }
725 while (!list_empty(&session->s_cap_releases_done)) {
726 msg = list_first_entry(&session->s_cap_releases_done,
727 struct ceph_msg, list_head);
728 list_del_init(&msg->list_head);
729 ceph_msg_put(msg);
730 }
731 spin_unlock(&session->s_cap_lock);
732}
733
734/*
Sage Weilf818a732010-05-11 20:56:31 -0700735 * Helper to safely iterate over all caps associated with a session, with
736 * special care taken to handle a racing __ceph_remove_cap().
Sage Weil2f2dc052009-10-06 11:31:09 -0700737 *
Sage Weilf818a732010-05-11 20:56:31 -0700738 * Caller must hold session s_mutex.
Sage Weil2f2dc052009-10-06 11:31:09 -0700739 */
740static int iterate_session_caps(struct ceph_mds_session *session,
741 int (*cb)(struct inode *, struct ceph_cap *,
742 void *), void *arg)
743{
Sage Weil7c1332b2010-02-16 11:39:45 -0800744 struct list_head *p;
745 struct ceph_cap *cap;
746 struct inode *inode, *last_inode = NULL;
747 struct ceph_cap *old_cap = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700748 int ret;
749
750 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
751 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800752 p = session->s_caps.next;
753 while (p != &session->s_caps) {
754 cap = list_entry(p, struct ceph_cap, session_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -0700755 inode = igrab(&cap->ci->vfs_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -0800756 if (!inode) {
757 p = p->next;
Sage Weil2f2dc052009-10-06 11:31:09 -0700758 continue;
Sage Weil7c1332b2010-02-16 11:39:45 -0800759 }
760 session->s_cap_iterator = cap;
Sage Weil2f2dc052009-10-06 11:31:09 -0700761 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800762
763 if (last_inode) {
764 iput(last_inode);
765 last_inode = NULL;
766 }
767 if (old_cap) {
Yehuda Sadeh37151662010-06-17 16:16:12 -0700768 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -0800769 old_cap = NULL;
770 }
771
Sage Weil2f2dc052009-10-06 11:31:09 -0700772 ret = cb(inode, cap, arg);
Sage Weil7c1332b2010-02-16 11:39:45 -0800773 last_inode = inode;
774
Sage Weil2f2dc052009-10-06 11:31:09 -0700775 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800776 p = p->next;
777 if (cap->ci == NULL) {
778 dout("iterate_session_caps finishing cap %p removal\n",
779 cap);
780 BUG_ON(cap->session != session);
781 list_del_init(&cap->session_caps);
782 session->s_nr_caps--;
783 cap->session = NULL;
784 old_cap = cap; /* put_cap it w/o locks held */
785 }
Sage Weil5dacf092009-12-21 20:40:34 -0800786 if (ret < 0)
787 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -0700788 }
Sage Weil5dacf092009-12-21 20:40:34 -0800789 ret = 0;
790out:
Sage Weil7c1332b2010-02-16 11:39:45 -0800791 session->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700792 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800793
794 if (last_inode)
795 iput(last_inode);
796 if (old_cap)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700797 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -0800798
Sage Weil5dacf092009-12-21 20:40:34 -0800799 return ret;
Sage Weil2f2dc052009-10-06 11:31:09 -0700800}
801
802static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
Sage Weil6c99f252010-05-10 16:12:25 -0700803 void *arg)
Sage Weil2f2dc052009-10-06 11:31:09 -0700804{
805 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil6c99f252010-05-10 16:12:25 -0700806 int drop = 0;
807
Sage Weil2f2dc052009-10-06 11:31:09 -0700808 dout("removing cap %p, ci is %p, inode is %p\n",
809 cap, ci, &ci->vfs_inode);
Sage Weil6c99f252010-05-10 16:12:25 -0700810 spin_lock(&inode->i_lock);
811 __ceph_remove_cap(cap);
812 if (!__ceph_is_any_real_caps(ci)) {
813 struct ceph_mds_client *mdsc =
814 &ceph_sb_to_client(inode->i_sb)->mdsc;
815
816 spin_lock(&mdsc->cap_dirty_lock);
817 if (!list_empty(&ci->i_dirty_item)) {
818 pr_info(" dropping dirty %s state for %p %lld\n",
819 ceph_cap_string(ci->i_dirty_caps),
820 inode, ceph_ino(inode));
821 ci->i_dirty_caps = 0;
822 list_del_init(&ci->i_dirty_item);
823 drop = 1;
824 }
825 if (!list_empty(&ci->i_flushing_item)) {
826 pr_info(" dropping dirty+flushing %s state for %p %lld\n",
827 ceph_cap_string(ci->i_flushing_caps),
828 inode, ceph_ino(inode));
829 ci->i_flushing_caps = 0;
830 list_del_init(&ci->i_flushing_item);
831 mdsc->num_cap_flushing--;
832 drop = 1;
833 }
834 if (drop && ci->i_wrbuffer_ref) {
835 pr_info(" dropping dirty data for %p %lld\n",
836 inode, ceph_ino(inode));
837 ci->i_wrbuffer_ref = 0;
838 ci->i_wrbuffer_ref_head = 0;
839 drop++;
840 }
841 spin_unlock(&mdsc->cap_dirty_lock);
842 }
843 spin_unlock(&inode->i_lock);
844 while (drop--)
845 iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -0700846 return 0;
847}
848
849/*
850 * caller must hold session s_mutex
851 */
852static void remove_session_caps(struct ceph_mds_session *session)
853{
854 dout("remove_session_caps on %p\n", session);
855 iterate_session_caps(session, remove_session_caps_cb, NULL);
856 BUG_ON(session->s_nr_caps > 0);
Sage Weil6c99f252010-05-10 16:12:25 -0700857 BUG_ON(!list_empty(&session->s_cap_flushing));
Sage Weil2f2dc052009-10-06 11:31:09 -0700858 cleanup_cap_releases(session);
859}
860
861/*
862 * wake up any threads waiting on this session's caps. if the cap is
863 * old (didn't get renewed on the client reconnect), remove it now.
864 *
865 * caller must hold s_mutex.
866 */
867static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
868 void *arg)
869{
Sage Weil0dc25702009-11-20 13:43:45 -0800870 struct ceph_inode_info *ci = ceph_inode(inode);
871
Yehuda Sadeh03066f22010-07-27 13:11:08 -0700872 wake_up_all(&ci->i_cap_wq);
Sage Weil0dc25702009-11-20 13:43:45 -0800873 if (arg) {
874 spin_lock(&inode->i_lock);
875 ci->i_wanted_max_size = 0;
876 ci->i_requested_max_size = 0;
877 spin_unlock(&inode->i_lock);
878 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700879 return 0;
880}
881
Sage Weil0dc25702009-11-20 13:43:45 -0800882static void wake_up_session_caps(struct ceph_mds_session *session,
883 int reconnect)
Sage Weil2f2dc052009-10-06 11:31:09 -0700884{
885 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
Sage Weil0dc25702009-11-20 13:43:45 -0800886 iterate_session_caps(session, wake_up_session_cb,
887 (void *)(unsigned long)reconnect);
Sage Weil2f2dc052009-10-06 11:31:09 -0700888}
889
890/*
891 * Send periodic message to MDS renewing all currently held caps. The
892 * ack will reset the expiration for all caps from this session.
893 *
894 * caller holds s_mutex
895 */
896static int send_renew_caps(struct ceph_mds_client *mdsc,
897 struct ceph_mds_session *session)
898{
899 struct ceph_msg *msg;
900 int state;
901
902 if (time_after_eq(jiffies, session->s_cap_ttl) &&
903 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
904 pr_info("mds%d caps stale\n", session->s_mds);
Sage Weile4cb4cb2010-03-18 13:43:09 -0700905 session->s_renew_requested = jiffies;
Sage Weil2f2dc052009-10-06 11:31:09 -0700906
907 /* do not try to renew caps until a recovering mds has reconnected
908 * with its clients. */
909 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
910 if (state < CEPH_MDS_STATE_RECONNECT) {
911 dout("send_renew_caps ignoring mds%d (%s)\n",
912 session->s_mds, ceph_mds_state_name(state));
913 return 0;
914 }
915
916 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
917 ceph_mds_state_name(state));
Sage Weil2f2dc052009-10-06 11:31:09 -0700918 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
919 ++session->s_renew_seq);
Sage Weila79832f2010-04-01 16:06:19 -0700920 if (!msg)
921 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -0700922 ceph_con_send(&session->s_con, msg);
923 return 0;
924}
925
926/*
927 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
Sage Weil0dc25702009-11-20 13:43:45 -0800928 *
929 * Called under session->s_mutex
Sage Weil2f2dc052009-10-06 11:31:09 -0700930 */
931static void renewed_caps(struct ceph_mds_client *mdsc,
932 struct ceph_mds_session *session, int is_renew)
933{
934 int was_stale;
935 int wake = 0;
936
937 spin_lock(&session->s_cap_lock);
938 was_stale = is_renew && (session->s_cap_ttl == 0 ||
939 time_after_eq(jiffies, session->s_cap_ttl));
940
941 session->s_cap_ttl = session->s_renew_requested +
942 mdsc->mdsmap->m_session_timeout*HZ;
943
944 if (was_stale) {
945 if (time_before(jiffies, session->s_cap_ttl)) {
946 pr_info("mds%d caps renewed\n", session->s_mds);
947 wake = 1;
948 } else {
949 pr_info("mds%d caps still stale\n", session->s_mds);
950 }
951 }
952 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
953 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
954 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
955 spin_unlock(&session->s_cap_lock);
956
957 if (wake)
Sage Weil0dc25702009-11-20 13:43:45 -0800958 wake_up_session_caps(session, 0);
Sage Weil2f2dc052009-10-06 11:31:09 -0700959}
960
961/*
962 * send a session close request
963 */
964static int request_close_session(struct ceph_mds_client *mdsc,
965 struct ceph_mds_session *session)
966{
967 struct ceph_msg *msg;
Sage Weil2f2dc052009-10-06 11:31:09 -0700968
969 dout("request_close_session mds%d state %s seq %lld\n",
970 session->s_mds, session_state_name(session->s_state),
971 session->s_seq);
972 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -0700973 if (!msg)
974 return -ENOMEM;
975 ceph_con_send(&session->s_con, msg);
976 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -0700977}
978
979/*
980 * Called with s_mutex held.
981 */
982static int __close_session(struct ceph_mds_client *mdsc,
983 struct ceph_mds_session *session)
984{
985 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
986 return 0;
987 session->s_state = CEPH_MDS_SESSION_CLOSING;
988 return request_close_session(mdsc, session);
989}
990
991/*
992 * Trim old(er) caps.
993 *
994 * Because we can't cache an inode without one or more caps, we do
995 * this indirectly: if a cap is unused, we prune its aliases, at which
996 * point the inode will hopefully get dropped to.
997 *
998 * Yes, this is a bit sloppy. Our only real goal here is to respond to
999 * memory pressure from the MDS, though, so it needn't be perfect.
1000 */
1001static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1002{
1003 struct ceph_mds_session *session = arg;
1004 struct ceph_inode_info *ci = ceph_inode(inode);
1005 int used, oissued, mine;
1006
1007 if (session->s_trim_caps <= 0)
1008 return -1;
1009
1010 spin_lock(&inode->i_lock);
1011 mine = cap->issued | cap->implemented;
1012 used = __ceph_caps_used(ci);
1013 oissued = __ceph_caps_issued_other(ci, cap);
1014
1015 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n",
1016 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
1017 ceph_cap_string(used));
1018 if (ci->i_dirty_caps)
1019 goto out; /* dirty caps */
1020 if ((used & ~oissued) & mine)
1021 goto out; /* we need these caps */
1022
1023 session->s_trim_caps--;
1024 if (oissued) {
1025 /* we aren't the only cap.. just remove us */
Sage Weil7c1332b2010-02-16 11:39:45 -08001026 __ceph_remove_cap(cap);
Sage Weil2f2dc052009-10-06 11:31:09 -07001027 } else {
1028 /* try to drop referring dentries */
1029 spin_unlock(&inode->i_lock);
1030 d_prune_aliases(inode);
1031 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1032 inode, cap, atomic_read(&inode->i_count));
1033 return 0;
1034 }
1035
1036out:
1037 spin_unlock(&inode->i_lock);
1038 return 0;
1039}
1040
1041/*
1042 * Trim session cap count down to some max number.
1043 */
1044static int trim_caps(struct ceph_mds_client *mdsc,
1045 struct ceph_mds_session *session,
1046 int max_caps)
1047{
1048 int trim_caps = session->s_nr_caps - max_caps;
1049
1050 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1051 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1052 if (trim_caps > 0) {
1053 session->s_trim_caps = trim_caps;
1054 iterate_session_caps(session, trim_caps_cb, session);
1055 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1056 session->s_mds, session->s_nr_caps, max_caps,
1057 trim_caps - session->s_trim_caps);
Sage Weil5dacf092009-12-21 20:40:34 -08001058 session->s_trim_caps = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001059 }
1060 return 0;
1061}
1062
1063/*
1064 * Allocate cap_release messages. If there is a partially full message
1065 * in the queue, try to allocate enough to cover it's remainder, so that
1066 * we can send it immediately.
1067 *
1068 * Called under s_mutex.
1069 */
Sage Weil2b2300d2010-06-09 16:52:04 -07001070int ceph_add_cap_releases(struct ceph_mds_client *mdsc,
Sage Weilee6b2722010-06-10 12:55:52 -07001071 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001072{
Sage Weil38e8883e2010-06-10 12:57:11 -07001073 struct ceph_msg *msg, *partial = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001074 struct ceph_mds_cap_release *head;
1075 int err = -ENOMEM;
Sage Weilee6b2722010-06-10 12:55:52 -07001076 int extra = mdsc->client->mount_args->cap_release_safety;
Sage Weil38e8883e2010-06-10 12:57:11 -07001077 int num;
Sage Weil2f2dc052009-10-06 11:31:09 -07001078
Sage Weil38e8883e2010-06-10 12:57:11 -07001079 dout("add_cap_releases %p mds%d extra %d\n", session, session->s_mds,
1080 extra);
Sage Weil2f2dc052009-10-06 11:31:09 -07001081
1082 spin_lock(&session->s_cap_lock);
1083
1084 if (!list_empty(&session->s_cap_releases)) {
1085 msg = list_first_entry(&session->s_cap_releases,
1086 struct ceph_msg,
1087 list_head);
1088 head = msg->front.iov_base;
Sage Weil38e8883e2010-06-10 12:57:11 -07001089 num = le32_to_cpu(head->num);
1090 if (num) {
1091 dout(" partial %p with (%d/%d)\n", msg, num,
1092 (int)CEPH_CAPS_PER_RELEASE);
1093 extra += CEPH_CAPS_PER_RELEASE - num;
1094 partial = msg;
1095 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001096 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001097 while (session->s_num_cap_releases < session->s_nr_caps + extra) {
1098 spin_unlock(&session->s_cap_lock);
Yehuda Sadeh34d23762010-04-06 14:33:58 -07001099 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE,
1100 GFP_NOFS);
Sage Weil2f2dc052009-10-06 11:31:09 -07001101 if (!msg)
1102 goto out_unlocked;
1103 dout("add_cap_releases %p msg %p now %d\n", session, msg,
1104 (int)msg->front.iov_len);
1105 head = msg->front.iov_base;
1106 head->num = cpu_to_le32(0);
1107 msg->front.iov_len = sizeof(*head);
1108 spin_lock(&session->s_cap_lock);
1109 list_add(&msg->list_head, &session->s_cap_releases);
1110 session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
1111 }
1112
Sage Weil38e8883e2010-06-10 12:57:11 -07001113 if (partial) {
1114 head = partial->front.iov_base;
1115 num = le32_to_cpu(head->num);
1116 dout(" queueing partial %p with %d/%d\n", partial, num,
1117 (int)CEPH_CAPS_PER_RELEASE);
1118 list_move_tail(&partial->list_head,
1119 &session->s_cap_releases_done);
1120 session->s_num_cap_releases -= CEPH_CAPS_PER_RELEASE - num;
Sage Weil2f2dc052009-10-06 11:31:09 -07001121 }
1122 err = 0;
1123 spin_unlock(&session->s_cap_lock);
1124out_unlocked:
1125 return err;
1126}
1127
1128/*
1129 * flush all dirty inode data to disk.
1130 *
1131 * returns true if we've flushed through want_flush_seq
1132 */
1133static int check_cap_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
1134{
1135 int mds, ret = 1;
1136
1137 dout("check_cap_flush want %lld\n", want_flush_seq);
1138 mutex_lock(&mdsc->mutex);
1139 for (mds = 0; ret && mds < mdsc->max_sessions; mds++) {
1140 struct ceph_mds_session *session = mdsc->sessions[mds];
1141
1142 if (!session)
1143 continue;
1144 get_session(session);
1145 mutex_unlock(&mdsc->mutex);
1146
1147 mutex_lock(&session->s_mutex);
1148 if (!list_empty(&session->s_cap_flushing)) {
1149 struct ceph_inode_info *ci =
1150 list_entry(session->s_cap_flushing.next,
1151 struct ceph_inode_info,
1152 i_flushing_item);
1153 struct inode *inode = &ci->vfs_inode;
1154
1155 spin_lock(&inode->i_lock);
1156 if (ci->i_cap_flush_seq <= want_flush_seq) {
1157 dout("check_cap_flush still flushing %p "
1158 "seq %lld <= %lld to mds%d\n", inode,
1159 ci->i_cap_flush_seq, want_flush_seq,
1160 session->s_mds);
1161 ret = 0;
1162 }
1163 spin_unlock(&inode->i_lock);
1164 }
1165 mutex_unlock(&session->s_mutex);
1166 ceph_put_mds_session(session);
1167
1168 if (!ret)
1169 return ret;
1170 mutex_lock(&mdsc->mutex);
1171 }
1172
1173 mutex_unlock(&mdsc->mutex);
1174 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
1175 return ret;
1176}
1177
1178/*
1179 * called under s_mutex
1180 */
Sage Weil3d7ded42010-06-09 16:47:10 -07001181void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1182 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001183{
1184 struct ceph_msg *msg;
1185
1186 dout("send_cap_releases mds%d\n", session->s_mds);
Sage Weil0f8605f2010-05-05 15:51:35 -07001187 spin_lock(&session->s_cap_lock);
1188 while (!list_empty(&session->s_cap_releases_done)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001189 msg = list_first_entry(&session->s_cap_releases_done,
1190 struct ceph_msg, list_head);
1191 list_del_init(&msg->list_head);
1192 spin_unlock(&session->s_cap_lock);
1193 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1194 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1195 ceph_con_send(&session->s_con, msg);
Sage Weil0f8605f2010-05-05 15:51:35 -07001196 spin_lock(&session->s_cap_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001197 }
1198 spin_unlock(&session->s_cap_lock);
1199}
1200
Sage Weile01a5942010-05-10 15:36:44 -07001201static void discard_cap_releases(struct ceph_mds_client *mdsc,
1202 struct ceph_mds_session *session)
1203{
1204 struct ceph_msg *msg;
1205 struct ceph_mds_cap_release *head;
1206 unsigned num;
1207
1208 dout("discard_cap_releases mds%d\n", session->s_mds);
1209 spin_lock(&session->s_cap_lock);
1210
1211 /* zero out the in-progress message */
1212 msg = list_first_entry(&session->s_cap_releases,
1213 struct ceph_msg, list_head);
1214 head = msg->front.iov_base;
1215 num = le32_to_cpu(head->num);
1216 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg, num);
1217 head->num = cpu_to_le32(0);
1218 session->s_num_cap_releases += num;
1219
1220 /* requeue completed messages */
1221 while (!list_empty(&session->s_cap_releases_done)) {
1222 msg = list_first_entry(&session->s_cap_releases_done,
1223 struct ceph_msg, list_head);
1224 list_del_init(&msg->list_head);
1225
1226 head = msg->front.iov_base;
1227 num = le32_to_cpu(head->num);
1228 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg,
1229 num);
1230 session->s_num_cap_releases += num;
1231 head->num = cpu_to_le32(0);
1232 msg->front.iov_len = sizeof(*head);
1233 list_add(&msg->list_head, &session->s_cap_releases);
1234 }
1235
1236 spin_unlock(&session->s_cap_lock);
1237}
1238
Sage Weil2f2dc052009-10-06 11:31:09 -07001239/*
1240 * requests
1241 */
1242
1243/*
1244 * Create an mds request.
1245 */
1246struct ceph_mds_request *
1247ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1248{
1249 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1250
1251 if (!req)
1252 return ERR_PTR(-ENOMEM);
1253
Sage Weilb4556392010-05-13 12:01:13 -07001254 mutex_init(&req->r_fill_mutex);
Yehuda Sadeh37151662010-06-17 16:16:12 -07001255 req->r_mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07001256 req->r_started = jiffies;
1257 req->r_resend_mds = -1;
1258 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1259 req->r_fmode = -1;
Sage Weil153c8e62009-12-07 12:31:09 -08001260 kref_init(&req->r_kref);
Sage Weil2f2dc052009-10-06 11:31:09 -07001261 INIT_LIST_HEAD(&req->r_wait);
1262 init_completion(&req->r_completion);
1263 init_completion(&req->r_safe_completion);
1264 INIT_LIST_HEAD(&req->r_unsafe_item);
1265
1266 req->r_op = op;
1267 req->r_direct_mode = mode;
1268 return req;
1269}
1270
1271/*
Sage Weil44ca18f2010-02-15 12:08:46 -08001272 * return oldest (lowest) request, tid in request tree, 0 if none.
Sage Weil2f2dc052009-10-06 11:31:09 -07001273 *
1274 * called under mdsc->mutex.
1275 */
Sage Weil44ca18f2010-02-15 12:08:46 -08001276static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1277{
1278 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1279 return NULL;
1280 return rb_entry(rb_first(&mdsc->request_tree),
1281 struct ceph_mds_request, r_node);
1282}
1283
Sage Weil2f2dc052009-10-06 11:31:09 -07001284static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1285{
Sage Weil44ca18f2010-02-15 12:08:46 -08001286 struct ceph_mds_request *req = __get_oldest_req(mdsc);
1287
1288 if (req)
1289 return req->r_tid;
1290 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001291}
1292
1293/*
1294 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1295 * on build_path_from_dentry in fs/cifs/dir.c.
1296 *
1297 * If @stop_on_nosnap, generate path relative to the first non-snapped
1298 * inode.
1299 *
1300 * Encode hidden .snap dirs as a double /, i.e.
1301 * foo/.snap/bar -> foo//bar
1302 */
1303char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1304 int stop_on_nosnap)
1305{
1306 struct dentry *temp;
1307 char *path;
1308 int len, pos;
1309
1310 if (dentry == NULL)
1311 return ERR_PTR(-EINVAL);
1312
1313retry:
1314 len = 0;
1315 for (temp = dentry; !IS_ROOT(temp);) {
1316 struct inode *inode = temp->d_inode;
1317 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1318 len++; /* slash only */
1319 else if (stop_on_nosnap && inode &&
1320 ceph_snap(inode) == CEPH_NOSNAP)
1321 break;
1322 else
1323 len += 1 + temp->d_name.len;
1324 temp = temp->d_parent;
1325 if (temp == NULL) {
Sage Weil6c99f252010-05-10 16:12:25 -07001326 pr_err("build_path corrupt dentry %p\n", dentry);
Sage Weil2f2dc052009-10-06 11:31:09 -07001327 return ERR_PTR(-EINVAL);
1328 }
1329 }
1330 if (len)
1331 len--; /* no leading '/' */
1332
1333 path = kmalloc(len+1, GFP_NOFS);
1334 if (path == NULL)
1335 return ERR_PTR(-ENOMEM);
1336 pos = len;
1337 path[pos] = 0; /* trailing null */
1338 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
1339 struct inode *inode = temp->d_inode;
1340
1341 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
Sage Weil104648a2010-03-18 10:14:30 -07001342 dout("build_path path+%d: %p SNAPDIR\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001343 pos, temp);
1344 } else if (stop_on_nosnap && inode &&
1345 ceph_snap(inode) == CEPH_NOSNAP) {
1346 break;
1347 } else {
1348 pos -= temp->d_name.len;
1349 if (pos < 0)
1350 break;
1351 strncpy(path + pos, temp->d_name.name,
1352 temp->d_name.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07001353 }
1354 if (pos)
1355 path[--pos] = '/';
1356 temp = temp->d_parent;
1357 if (temp == NULL) {
Sage Weil104648a2010-03-18 10:14:30 -07001358 pr_err("build_path corrupt dentry\n");
Sage Weil2f2dc052009-10-06 11:31:09 -07001359 kfree(path);
1360 return ERR_PTR(-EINVAL);
1361 }
1362 }
1363 if (pos != 0) {
Sage Weil104648a2010-03-18 10:14:30 -07001364 pr_err("build_path did not end path lookup where "
Sage Weil2f2dc052009-10-06 11:31:09 -07001365 "expected, namelen is %d, pos is %d\n", len, pos);
1366 /* presumably this is only possible if racing with a
1367 rename of one of the parent directories (we can not
1368 lock the dentries above us to prevent this, but
1369 retrying should be harmless) */
1370 kfree(path);
1371 goto retry;
1372 }
1373
1374 *base = ceph_ino(temp->d_inode);
1375 *plen = len;
Sage Weil104648a2010-03-18 10:14:30 -07001376 dout("build_path on %p %d built %llx '%.*s'\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001377 dentry, atomic_read(&dentry->d_count), *base, len, path);
1378 return path;
1379}
1380
1381static int build_dentry_path(struct dentry *dentry,
1382 const char **ppath, int *ppathlen, u64 *pino,
1383 int *pfreepath)
1384{
1385 char *path;
1386
1387 if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) {
1388 *pino = ceph_ino(dentry->d_parent->d_inode);
1389 *ppath = dentry->d_name.name;
1390 *ppathlen = dentry->d_name.len;
1391 return 0;
1392 }
1393 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1394 if (IS_ERR(path))
1395 return PTR_ERR(path);
1396 *ppath = path;
1397 *pfreepath = 1;
1398 return 0;
1399}
1400
1401static int build_inode_path(struct inode *inode,
1402 const char **ppath, int *ppathlen, u64 *pino,
1403 int *pfreepath)
1404{
1405 struct dentry *dentry;
1406 char *path;
1407
1408 if (ceph_snap(inode) == CEPH_NOSNAP) {
1409 *pino = ceph_ino(inode);
1410 *ppathlen = 0;
1411 return 0;
1412 }
1413 dentry = d_find_alias(inode);
1414 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1415 dput(dentry);
1416 if (IS_ERR(path))
1417 return PTR_ERR(path);
1418 *ppath = path;
1419 *pfreepath = 1;
1420 return 0;
1421}
1422
1423/*
1424 * request arguments may be specified via an inode *, a dentry *, or
1425 * an explicit ino+path.
1426 */
1427static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1428 const char *rpath, u64 rino,
1429 const char **ppath, int *pathlen,
1430 u64 *ino, int *freepath)
1431{
1432 int r = 0;
1433
1434 if (rinode) {
1435 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1436 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1437 ceph_snap(rinode));
1438 } else if (rdentry) {
1439 r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
1440 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1441 *ppath);
1442 } else if (rpath) {
1443 *ino = rino;
1444 *ppath = rpath;
1445 *pathlen = strlen(rpath);
1446 dout(" path %.*s\n", *pathlen, rpath);
1447 }
1448
1449 return r;
1450}
1451
1452/*
1453 * called under mdsc->mutex
1454 */
1455static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1456 struct ceph_mds_request *req,
1457 int mds)
1458{
1459 struct ceph_msg *msg;
1460 struct ceph_mds_request_head *head;
1461 const char *path1 = NULL;
1462 const char *path2 = NULL;
1463 u64 ino1 = 0, ino2 = 0;
1464 int pathlen1 = 0, pathlen2 = 0;
1465 int freepath1 = 0, freepath2 = 0;
1466 int len;
1467 u16 releases;
1468 void *p, *end;
1469 int ret;
1470
1471 ret = set_request_path_attr(req->r_inode, req->r_dentry,
1472 req->r_path1, req->r_ino1.ino,
1473 &path1, &pathlen1, &ino1, &freepath1);
1474 if (ret < 0) {
1475 msg = ERR_PTR(ret);
1476 goto out;
1477 }
1478
1479 ret = set_request_path_attr(NULL, req->r_old_dentry,
1480 req->r_path2, req->r_ino2.ino,
1481 &path2, &pathlen2, &ino2, &freepath2);
1482 if (ret < 0) {
1483 msg = ERR_PTR(ret);
1484 goto out_free1;
1485 }
1486
1487 len = sizeof(*head) +
Sage Weilac8839d2010-01-27 14:28:10 -08001488 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64));
Sage Weil2f2dc052009-10-06 11:31:09 -07001489
1490 /* calculate (max) length for cap releases */
1491 len += sizeof(struct ceph_mds_request_release) *
1492 (!!req->r_inode_drop + !!req->r_dentry_drop +
1493 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1494 if (req->r_dentry_drop)
1495 len += req->r_dentry->d_name.len;
1496 if (req->r_old_dentry_drop)
1497 len += req->r_old_dentry->d_name.len;
1498
Yehuda Sadeh34d23762010-04-06 14:33:58 -07001499 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -07001500 if (!msg) {
1501 msg = ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -07001502 goto out_free2;
Sage Weila79832f2010-04-01 16:06:19 -07001503 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001504
Sage Weil6df058c2009-12-22 11:24:33 -08001505 msg->hdr.tid = cpu_to_le64(req->r_tid);
1506
Sage Weil2f2dc052009-10-06 11:31:09 -07001507 head = msg->front.iov_base;
1508 p = msg->front.iov_base + sizeof(*head);
1509 end = msg->front.iov_base + msg->front.iov_len;
1510
1511 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1512 head->op = cpu_to_le32(req->r_op);
1513 head->caller_uid = cpu_to_le32(current_fsuid());
1514 head->caller_gid = cpu_to_le32(current_fsgid());
1515 head->args = req->r_args;
1516
1517 ceph_encode_filepath(&p, end, ino1, path1);
1518 ceph_encode_filepath(&p, end, ino2, path2);
1519
Sage Weile979cf52010-07-15 14:58:39 -07001520 /* make note of release offset, in case we need to replay */
1521 req->r_request_release_offset = p - msg->front.iov_base;
1522
Sage Weil2f2dc052009-10-06 11:31:09 -07001523 /* cap releases */
1524 releases = 0;
1525 if (req->r_inode_drop)
1526 releases += ceph_encode_inode_release(&p,
1527 req->r_inode ? req->r_inode : req->r_dentry->d_inode,
1528 mds, req->r_inode_drop, req->r_inode_unless, 0);
1529 if (req->r_dentry_drop)
1530 releases += ceph_encode_dentry_release(&p, req->r_dentry,
1531 mds, req->r_dentry_drop, req->r_dentry_unless);
1532 if (req->r_old_dentry_drop)
1533 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
1534 mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
1535 if (req->r_old_inode_drop)
1536 releases += ceph_encode_inode_release(&p,
1537 req->r_old_dentry->d_inode,
1538 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
1539 head->num_releases = cpu_to_le16(releases);
1540
1541 BUG_ON(p > end);
1542 msg->front.iov_len = p - msg->front.iov_base;
1543 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1544
1545 msg->pages = req->r_pages;
1546 msg->nr_pages = req->r_num_pages;
1547 msg->hdr.data_len = cpu_to_le32(req->r_data_len);
1548 msg->hdr.data_off = cpu_to_le16(0);
1549
1550out_free2:
1551 if (freepath2)
1552 kfree((char *)path2);
1553out_free1:
1554 if (freepath1)
1555 kfree((char *)path1);
1556out:
1557 return msg;
1558}
1559
1560/*
1561 * called under mdsc->mutex if error, under no mutex if
1562 * success.
1563 */
1564static void complete_request(struct ceph_mds_client *mdsc,
1565 struct ceph_mds_request *req)
1566{
1567 if (req->r_callback)
1568 req->r_callback(mdsc, req);
1569 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001570 complete_all(&req->r_completion);
Sage Weil2f2dc052009-10-06 11:31:09 -07001571}
1572
1573/*
1574 * called under mdsc->mutex
1575 */
1576static int __prepare_send_request(struct ceph_mds_client *mdsc,
1577 struct ceph_mds_request *req,
1578 int mds)
1579{
1580 struct ceph_mds_request_head *rhead;
1581 struct ceph_msg *msg;
1582 int flags = 0;
1583
1584 req->r_mds = mds;
1585 req->r_attempts++;
1586 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
1587 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
1588
Sage Weil01a92f12010-07-15 13:24:32 -07001589 if (req->r_got_unsafe) {
1590 /*
1591 * Replay. Do not regenerate message (and rebuild
1592 * paths, etc.); just use the original message.
1593 * Rebuilding paths will break for renames because
1594 * d_move mangles the src name.
1595 */
1596 msg = req->r_request;
1597 rhead = msg->front.iov_base;
1598
1599 flags = le32_to_cpu(rhead->flags);
1600 flags |= CEPH_MDS_FLAG_REPLAY;
1601 rhead->flags = cpu_to_le32(flags);
1602
1603 if (req->r_target_inode)
1604 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
1605
1606 rhead->num_retry = req->r_attempts - 1;
Sage Weile979cf52010-07-15 14:58:39 -07001607
1608 /* remove cap/dentry releases from message */
1609 rhead->num_releases = 0;
1610 msg->hdr.front_len = cpu_to_le32(req->r_request_release_offset);
1611 msg->front.iov_len = req->r_request_release_offset;
Sage Weil01a92f12010-07-15 13:24:32 -07001612 return 0;
1613 }
1614
Sage Weil2f2dc052009-10-06 11:31:09 -07001615 if (req->r_request) {
1616 ceph_msg_put(req->r_request);
1617 req->r_request = NULL;
1618 }
1619 msg = create_request_message(mdsc, req, mds);
1620 if (IS_ERR(msg)) {
Sage Weile1518c72010-05-13 11:19:06 -07001621 req->r_err = PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001622 complete_request(mdsc, req);
Sage Weila79832f2010-04-01 16:06:19 -07001623 return PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001624 }
1625 req->r_request = msg;
1626
1627 rhead = msg->front.iov_base;
Sage Weil2f2dc052009-10-06 11:31:09 -07001628 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
1629 if (req->r_got_unsafe)
1630 flags |= CEPH_MDS_FLAG_REPLAY;
1631 if (req->r_locked_dir)
1632 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
1633 rhead->flags = cpu_to_le32(flags);
1634 rhead->num_fwd = req->r_num_fwd;
1635 rhead->num_retry = req->r_attempts - 1;
Sage Weil01a92f12010-07-15 13:24:32 -07001636 rhead->ino = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001637
1638 dout(" r_locked_dir = %p\n", req->r_locked_dir);
Sage Weil2f2dc052009-10-06 11:31:09 -07001639 return 0;
1640}
1641
1642/*
1643 * send request, or put it on the appropriate wait list.
1644 */
1645static int __do_request(struct ceph_mds_client *mdsc,
1646 struct ceph_mds_request *req)
1647{
1648 struct ceph_mds_session *session = NULL;
1649 int mds = -1;
1650 int err = -EAGAIN;
1651
Sage Weile1518c72010-05-13 11:19:06 -07001652 if (req->r_err || req->r_got_result)
Sage Weil2f2dc052009-10-06 11:31:09 -07001653 goto out;
1654
1655 if (req->r_timeout &&
1656 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
1657 dout("do_request timed out\n");
1658 err = -EIO;
1659 goto finish;
1660 }
1661
1662 mds = __choose_mds(mdsc, req);
1663 if (mds < 0 ||
1664 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
1665 dout("do_request no mds or not active, waiting for map\n");
1666 list_add(&req->r_wait, &mdsc->waiting_for_map);
1667 goto out;
1668 }
1669
1670 /* get, open session */
1671 session = __ceph_lookup_mds_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07001672 if (!session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001673 session = register_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07001674 if (IS_ERR(session)) {
1675 err = PTR_ERR(session);
1676 goto finish;
1677 }
1678 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001679 dout("do_request mds%d session %p state %s\n", mds, session,
1680 session_state_name(session->s_state));
1681 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
1682 session->s_state != CEPH_MDS_SESSION_HUNG) {
1683 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1684 session->s_state == CEPH_MDS_SESSION_CLOSING)
1685 __open_session(mdsc, session);
1686 list_add(&req->r_wait, &session->s_waiting);
1687 goto out_session;
1688 }
1689
1690 /* send request */
1691 req->r_session = get_session(session);
1692 req->r_resend_mds = -1; /* forget any previous mds hint */
1693
1694 if (req->r_request_started == 0) /* note request start time */
1695 req->r_request_started = jiffies;
1696
1697 err = __prepare_send_request(mdsc, req, mds);
1698 if (!err) {
1699 ceph_msg_get(req->r_request);
1700 ceph_con_send(&session->s_con, req->r_request);
1701 }
1702
1703out_session:
1704 ceph_put_mds_session(session);
1705out:
1706 return err;
1707
1708finish:
Sage Weile1518c72010-05-13 11:19:06 -07001709 req->r_err = err;
Sage Weil2f2dc052009-10-06 11:31:09 -07001710 complete_request(mdsc, req);
1711 goto out;
1712}
1713
1714/*
1715 * called under mdsc->mutex
1716 */
1717static void __wake_requests(struct ceph_mds_client *mdsc,
1718 struct list_head *head)
1719{
1720 struct ceph_mds_request *req, *nreq;
1721
1722 list_for_each_entry_safe(req, nreq, head, r_wait) {
1723 list_del_init(&req->r_wait);
1724 __do_request(mdsc, req);
1725 }
1726}
1727
1728/*
1729 * Wake up threads with requests pending for @mds, so that they can
Sage Weil29790f22010-03-18 14:45:05 -07001730 * resubmit their requests to a possibly different mds.
Sage Weil2f2dc052009-10-06 11:31:09 -07001731 */
Sage Weil29790f22010-03-18 14:45:05 -07001732static void kick_requests(struct ceph_mds_client *mdsc, int mds)
Sage Weil2f2dc052009-10-06 11:31:09 -07001733{
Sage Weil44ca18f2010-02-15 12:08:46 -08001734 struct ceph_mds_request *req;
1735 struct rb_node *p;
Sage Weil2f2dc052009-10-06 11:31:09 -07001736
1737 dout("kick_requests mds%d\n", mds);
Sage Weil44ca18f2010-02-15 12:08:46 -08001738 for (p = rb_first(&mdsc->request_tree); p; p = rb_next(p)) {
1739 req = rb_entry(p, struct ceph_mds_request, r_node);
1740 if (req->r_got_unsafe)
1741 continue;
1742 if (req->r_session &&
1743 req->r_session->s_mds == mds) {
1744 dout(" kicking tid %llu\n", req->r_tid);
1745 put_request_session(req);
1746 __do_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07001747 }
1748 }
1749}
1750
1751void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
1752 struct ceph_mds_request *req)
1753{
1754 dout("submit_request on %p\n", req);
1755 mutex_lock(&mdsc->mutex);
1756 __register_request(mdsc, req, NULL);
1757 __do_request(mdsc, req);
1758 mutex_unlock(&mdsc->mutex);
1759}
1760
1761/*
1762 * Synchrously perform an mds request. Take care of all of the
1763 * session setup, forwarding, retry details.
1764 */
1765int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
1766 struct inode *dir,
1767 struct ceph_mds_request *req)
1768{
1769 int err;
1770
1771 dout("do_request on %p\n", req);
1772
1773 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
1774 if (req->r_inode)
1775 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
1776 if (req->r_locked_dir)
1777 ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
1778 if (req->r_old_dentry)
1779 ceph_get_cap_refs(
1780 ceph_inode(req->r_old_dentry->d_parent->d_inode),
1781 CEPH_CAP_PIN);
1782
1783 /* issue */
1784 mutex_lock(&mdsc->mutex);
1785 __register_request(mdsc, req, dir);
1786 __do_request(mdsc, req);
1787
Sage Weile1518c72010-05-13 11:19:06 -07001788 if (req->r_err) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001789 err = req->r_err;
Sage Weile1518c72010-05-13 11:19:06 -07001790 __unregister_request(mdsc, req);
1791 dout("do_request early error %d\n", err);
1792 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07001793 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001794
Sage Weile1518c72010-05-13 11:19:06 -07001795 /* wait */
1796 mutex_unlock(&mdsc->mutex);
1797 dout("do_request waiting\n");
1798 if (req->r_timeout) {
Sage Weilaa916472010-05-24 11:15:51 -07001799 err = (long)wait_for_completion_killable_timeout(
Sage Weile1518c72010-05-13 11:19:06 -07001800 &req->r_completion, req->r_timeout);
1801 if (err == 0)
1802 err = -EIO;
1803 } else {
Sage Weilaa916472010-05-24 11:15:51 -07001804 err = wait_for_completion_killable(&req->r_completion);
Sage Weile1518c72010-05-13 11:19:06 -07001805 }
1806 dout("do_request waited, got %d\n", err);
1807 mutex_lock(&mdsc->mutex);
1808
1809 /* only abort if we didn't race with a real reply */
1810 if (req->r_got_result) {
1811 err = le32_to_cpu(req->r_reply_info.head->result);
1812 } else if (err < 0) {
1813 dout("aborted request %lld with %d\n", req->r_tid, err);
Sage Weilb4556392010-05-13 12:01:13 -07001814
1815 /*
1816 * ensure we aren't running concurrently with
1817 * ceph_fill_trace or ceph_readdir_prepopulate, which
1818 * rely on locks (dir mutex) held by our caller.
1819 */
1820 mutex_lock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07001821 req->r_err = err;
1822 req->r_aborted = true;
Sage Weilb4556392010-05-13 12:01:13 -07001823 mutex_unlock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07001824
1825 if (req->r_locked_dir &&
Sage Weil167c9e32010-05-14 10:02:57 -07001826 (req->r_op & CEPH_MDS_OP_WRITE))
1827 ceph_invalidate_dir_request(req);
Sage Weile1518c72010-05-13 11:19:06 -07001828 } else {
1829 err = req->r_err;
1830 }
1831
1832out:
1833 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07001834 dout("do_request %p done, result %d\n", req, err);
1835 return err;
1836}
1837
1838/*
Sage Weil167c9e32010-05-14 10:02:57 -07001839 * Invalidate dir I_COMPLETE, dentry lease state on an aborted MDS
1840 * namespace request.
1841 */
1842void ceph_invalidate_dir_request(struct ceph_mds_request *req)
1843{
1844 struct inode *inode = req->r_locked_dir;
1845 struct ceph_inode_info *ci = ceph_inode(inode);
1846
1847 dout("invalidate_dir_request %p (I_COMPLETE, lease(s))\n", inode);
1848 spin_lock(&inode->i_lock);
1849 ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
1850 ci->i_release_count++;
1851 spin_unlock(&inode->i_lock);
1852
1853 if (req->r_dentry)
1854 ceph_invalidate_dentry_lease(req->r_dentry);
1855 if (req->r_old_dentry)
1856 ceph_invalidate_dentry_lease(req->r_old_dentry);
1857}
1858
1859/*
Sage Weil2f2dc052009-10-06 11:31:09 -07001860 * Handle mds reply.
1861 *
1862 * We take the session mutex and parse and process the reply immediately.
1863 * This preserves the logical ordering of replies, capabilities, etc., sent
1864 * by the MDS as they are applied to our local cache.
1865 */
1866static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
1867{
1868 struct ceph_mds_client *mdsc = session->s_mdsc;
1869 struct ceph_mds_request *req;
1870 struct ceph_mds_reply_head *head = msg->front.iov_base;
1871 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
1872 u64 tid;
1873 int err, result;
Sage Weil2600d2d2010-02-22 15:12:16 -08001874 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07001875
Sage Weil2f2dc052009-10-06 11:31:09 -07001876 if (msg->front.iov_len < sizeof(*head)) {
1877 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08001878 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001879 return;
1880 }
1881
1882 /* get request, session */
Sage Weil6df058c2009-12-22 11:24:33 -08001883 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07001884 mutex_lock(&mdsc->mutex);
1885 req = __lookup_request(mdsc, tid);
1886 if (!req) {
1887 dout("handle_reply on unknown tid %llu\n", tid);
1888 mutex_unlock(&mdsc->mutex);
1889 return;
1890 }
1891 dout("handle_reply %p\n", req);
Sage Weil2f2dc052009-10-06 11:31:09 -07001892
1893 /* correct session? */
Sage Weild96d6042010-03-20 20:50:58 -07001894 if (req->r_session != session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001895 pr_err("mdsc_handle_reply got %llu on session mds%d"
1896 " not mds%d\n", tid, session->s_mds,
1897 req->r_session ? req->r_session->s_mds : -1);
1898 mutex_unlock(&mdsc->mutex);
1899 goto out;
1900 }
1901
1902 /* dup? */
1903 if ((req->r_got_unsafe && !head->safe) ||
1904 (req->r_got_safe && head->safe)) {
1905 pr_warning("got a dup %s reply on %llu from mds%d\n",
1906 head->safe ? "safe" : "unsafe", tid, mds);
1907 mutex_unlock(&mdsc->mutex);
1908 goto out;
1909 }
Sage Weil85792d0d2010-05-13 09:06:02 -07001910 if (req->r_got_safe && !head->safe) {
1911 pr_warning("got unsafe after safe on %llu from mds%d\n",
1912 tid, mds);
1913 mutex_unlock(&mdsc->mutex);
1914 goto out;
1915 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001916
1917 result = le32_to_cpu(head->result);
1918
1919 /*
1920 * Tolerate 2 consecutive ESTALEs from the same mds.
1921 * FIXME: we should be looking at the cap migrate_seq.
1922 */
1923 if (result == -ESTALE) {
1924 req->r_direct_mode = USE_AUTH_MDS;
1925 req->r_num_stale++;
1926 if (req->r_num_stale <= 2) {
1927 __do_request(mdsc, req);
1928 mutex_unlock(&mdsc->mutex);
1929 goto out;
1930 }
1931 } else {
1932 req->r_num_stale = 0;
1933 }
1934
1935 if (head->safe) {
1936 req->r_got_safe = true;
1937 __unregister_request(mdsc, req);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001938 complete_all(&req->r_safe_completion);
Sage Weil2f2dc052009-10-06 11:31:09 -07001939
1940 if (req->r_got_unsafe) {
1941 /*
1942 * We already handled the unsafe response, now do the
1943 * cleanup. No need to examine the response; the MDS
1944 * doesn't include any result info in the safe
1945 * response. And even if it did, there is nothing
1946 * useful we could do with a revised return value.
1947 */
1948 dout("got safe reply %llu, mds%d\n", tid, mds);
1949 list_del_init(&req->r_unsafe_item);
1950
1951 /* last unsafe request during umount? */
Sage Weil44ca18f2010-02-15 12:08:46 -08001952 if (mdsc->stopping && !__get_oldest_req(mdsc))
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001953 complete_all(&mdsc->safe_umount_waiters);
Sage Weil2f2dc052009-10-06 11:31:09 -07001954 mutex_unlock(&mdsc->mutex);
1955 goto out;
1956 }
Sage Weile1518c72010-05-13 11:19:06 -07001957 } else {
Sage Weil2f2dc052009-10-06 11:31:09 -07001958 req->r_got_unsafe = true;
1959 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
1960 }
1961
1962 dout("handle_reply tid %lld result %d\n", tid, result);
1963 rinfo = &req->r_reply_info;
1964 err = parse_reply_info(msg, rinfo);
1965 mutex_unlock(&mdsc->mutex);
1966
1967 mutex_lock(&session->s_mutex);
1968 if (err < 0) {
1969 pr_err("mdsc_handle_reply got corrupt reply mds%d\n", mds);
Sage Weil9ec7cab2009-12-14 15:13:47 -08001970 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001971 goto out_err;
1972 }
1973
1974 /* snap trace */
1975 if (rinfo->snapblob_len) {
1976 down_write(&mdsc->snap_rwsem);
1977 ceph_update_snap_trace(mdsc, rinfo->snapblob,
1978 rinfo->snapblob + rinfo->snapblob_len,
1979 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP);
1980 downgrade_write(&mdsc->snap_rwsem);
1981 } else {
1982 down_read(&mdsc->snap_rwsem);
1983 }
1984
1985 /* insert trace into our cache */
Sage Weilb4556392010-05-13 12:01:13 -07001986 mutex_lock(&req->r_fill_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07001987 err = ceph_fill_trace(mdsc->client->sb, req, req->r_session);
1988 if (err == 0) {
1989 if (result == 0 && rinfo->dir_nr)
1990 ceph_readdir_prepopulate(req, req->r_session);
Yehuda Sadeh37151662010-06-17 16:16:12 -07001991 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
Sage Weil2f2dc052009-10-06 11:31:09 -07001992 }
Sage Weilb4556392010-05-13 12:01:13 -07001993 mutex_unlock(&req->r_fill_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07001994
1995 up_read(&mdsc->snap_rwsem);
1996out_err:
Sage Weile1518c72010-05-13 11:19:06 -07001997 mutex_lock(&mdsc->mutex);
1998 if (!req->r_aborted) {
1999 if (err) {
2000 req->r_err = err;
2001 } else {
2002 req->r_reply = msg;
2003 ceph_msg_get(msg);
2004 req->r_got_result = true;
2005 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002006 } else {
Sage Weile1518c72010-05-13 11:19:06 -07002007 dout("reply arrived after request %lld was aborted\n", tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002008 }
Sage Weile1518c72010-05-13 11:19:06 -07002009 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002010
Sage Weilee6b2722010-06-10 12:55:52 -07002011 ceph_add_cap_releases(mdsc, req->r_session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002012 mutex_unlock(&session->s_mutex);
2013
2014 /* kick calling process */
2015 complete_request(mdsc, req);
2016out:
2017 ceph_mdsc_put_request(req);
2018 return;
2019}
2020
2021
2022
2023/*
2024 * handle mds notification that our request has been forwarded.
2025 */
Sage Weil2600d2d2010-02-22 15:12:16 -08002026static void handle_forward(struct ceph_mds_client *mdsc,
2027 struct ceph_mds_session *session,
2028 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002029{
2030 struct ceph_mds_request *req;
Sage Weila1ea7872010-02-23 14:02:44 -08002031 u64 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002032 u32 next_mds;
2033 u32 fwd_seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002034 int err = -EINVAL;
2035 void *p = msg->front.iov_base;
2036 void *end = p + msg->front.iov_len;
Sage Weil2f2dc052009-10-06 11:31:09 -07002037
Sage Weila1ea7872010-02-23 14:02:44 -08002038 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002039 next_mds = ceph_decode_32(&p);
2040 fwd_seq = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07002041
2042 mutex_lock(&mdsc->mutex);
2043 req = __lookup_request(mdsc, tid);
2044 if (!req) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07002045 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002046 goto out; /* dup reply? */
2047 }
2048
Sage Weil2a8e5e32010-05-28 16:43:16 -07002049 if (req->r_aborted) {
2050 dout("forward tid %llu aborted, unregistering\n", tid);
2051 __unregister_request(mdsc, req);
2052 } else if (fwd_seq <= req->r_num_fwd) {
2053 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002054 tid, next_mds, req->r_num_fwd, fwd_seq);
2055 } else {
2056 /* resend. forward race not possible; mds would drop */
Sage Weil2a8e5e32010-05-28 16:43:16 -07002057 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2058 BUG_ON(req->r_err);
2059 BUG_ON(req->r_got_result);
Sage Weil2f2dc052009-10-06 11:31:09 -07002060 req->r_num_fwd = fwd_seq;
2061 req->r_resend_mds = next_mds;
2062 put_request_session(req);
2063 __do_request(mdsc, req);
2064 }
2065 ceph_mdsc_put_request(req);
2066out:
2067 mutex_unlock(&mdsc->mutex);
2068 return;
2069
2070bad:
2071 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2072}
2073
2074/*
2075 * handle a mds session control message
2076 */
2077static void handle_session(struct ceph_mds_session *session,
2078 struct ceph_msg *msg)
2079{
2080 struct ceph_mds_client *mdsc = session->s_mdsc;
2081 u32 op;
2082 u64 seq;
Sage Weil2600d2d2010-02-22 15:12:16 -08002083 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002084 struct ceph_mds_session_head *h = msg->front.iov_base;
2085 int wake = 0;
2086
Sage Weil2f2dc052009-10-06 11:31:09 -07002087 /* decode */
2088 if (msg->front.iov_len != sizeof(*h))
2089 goto bad;
2090 op = le32_to_cpu(h->op);
2091 seq = le64_to_cpu(h->seq);
2092
2093 mutex_lock(&mdsc->mutex);
Sage Weil2600d2d2010-02-22 15:12:16 -08002094 if (op == CEPH_SESSION_CLOSE)
2095 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002096 /* FIXME: this ttl calculation is generous */
2097 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2098 mutex_unlock(&mdsc->mutex);
2099
2100 mutex_lock(&session->s_mutex);
2101
2102 dout("handle_session mds%d %s %p state %s seq %llu\n",
2103 mds, ceph_session_op_name(op), session,
2104 session_state_name(session->s_state), seq);
2105
2106 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2107 session->s_state = CEPH_MDS_SESSION_OPEN;
2108 pr_info("mds%d came back\n", session->s_mds);
2109 }
2110
2111 switch (op) {
2112 case CEPH_SESSION_OPEN:
Sage Weil29790f22010-03-18 14:45:05 -07002113 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2114 pr_info("mds%d reconnect success\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002115 session->s_state = CEPH_MDS_SESSION_OPEN;
2116 renewed_caps(mdsc, session, 0);
2117 wake = 1;
2118 if (mdsc->stopping)
2119 __close_session(mdsc, session);
2120 break;
2121
2122 case CEPH_SESSION_RENEWCAPS:
2123 if (session->s_renew_seq == seq)
2124 renewed_caps(mdsc, session, 1);
2125 break;
2126
2127 case CEPH_SESSION_CLOSE:
Sage Weil29790f22010-03-18 14:45:05 -07002128 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2129 pr_info("mds%d reconnect denied\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002130 remove_session_caps(session);
2131 wake = 1; /* for good measure */
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002132 complete_all(&mdsc->session_close_waiters);
Sage Weil29790f22010-03-18 14:45:05 -07002133 kick_requests(mdsc, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002134 break;
2135
2136 case CEPH_SESSION_STALE:
2137 pr_info("mds%d caps went stale, renewing\n",
2138 session->s_mds);
2139 spin_lock(&session->s_cap_lock);
2140 session->s_cap_gen++;
2141 session->s_cap_ttl = 0;
2142 spin_unlock(&session->s_cap_lock);
2143 send_renew_caps(mdsc, session);
2144 break;
2145
2146 case CEPH_SESSION_RECALL_STATE:
2147 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2148 break;
2149
2150 default:
2151 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2152 WARN_ON(1);
2153 }
2154
2155 mutex_unlock(&session->s_mutex);
2156 if (wake) {
2157 mutex_lock(&mdsc->mutex);
2158 __wake_requests(mdsc, &session->s_waiting);
2159 mutex_unlock(&mdsc->mutex);
2160 }
2161 return;
2162
2163bad:
2164 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2165 (int)msg->front.iov_len);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002166 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002167 return;
2168}
2169
2170
2171/*
2172 * called under session->mutex.
2173 */
2174static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2175 struct ceph_mds_session *session)
2176{
2177 struct ceph_mds_request *req, *nreq;
2178 int err;
2179
2180 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2181
2182 mutex_lock(&mdsc->mutex);
2183 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
2184 err = __prepare_send_request(mdsc, req, session->s_mds);
2185 if (!err) {
2186 ceph_msg_get(req->r_request);
2187 ceph_con_send(&session->s_con, req->r_request);
2188 }
2189 }
2190 mutex_unlock(&mdsc->mutex);
2191}
2192
2193/*
2194 * Encode information about a cap for a reconnect with the MDS.
2195 */
Sage Weil2f2dc052009-10-06 11:31:09 -07002196static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2197 void *arg)
2198{
Sage Weil93cea5b2009-12-23 12:21:51 -08002199 struct ceph_mds_cap_reconnect rec;
Sage Weil2f2dc052009-10-06 11:31:09 -07002200 struct ceph_inode_info *ci;
Sage Weil93cea5b2009-12-23 12:21:51 -08002201 struct ceph_pagelist *pagelist = arg;
Sage Weil2f2dc052009-10-06 11:31:09 -07002202 char *path;
2203 int pathlen, err;
2204 u64 pathbase;
2205 struct dentry *dentry;
2206
2207 ci = cap->ci;
2208
2209 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2210 inode, ceph_vinop(inode), cap, cap->cap_id,
2211 ceph_cap_string(cap->issued));
Sage Weil93cea5b2009-12-23 12:21:51 -08002212 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2213 if (err)
2214 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002215
2216 dentry = d_find_alias(inode);
2217 if (dentry) {
2218 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2219 if (IS_ERR(path)) {
2220 err = PTR_ERR(path);
2221 BUG_ON(err);
2222 }
2223 } else {
2224 path = NULL;
2225 pathlen = 0;
2226 }
Sage Weil93cea5b2009-12-23 12:21:51 -08002227 err = ceph_pagelist_encode_string(pagelist, path, pathlen);
2228 if (err)
2229 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07002230
Sage Weil2f2dc052009-10-06 11:31:09 -07002231 spin_lock(&inode->i_lock);
2232 cap->seq = 0; /* reset cap seq */
2233 cap->issue_seq = 0; /* and issue_seq */
Sage Weil93cea5b2009-12-23 12:21:51 -08002234 rec.cap_id = cpu_to_le64(cap->cap_id);
2235 rec.pathbase = cpu_to_le64(pathbase);
2236 rec.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2237 rec.issued = cpu_to_le32(cap->issued);
2238 rec.size = cpu_to_le64(inode->i_size);
2239 ceph_encode_timespec(&rec.mtime, &inode->i_mtime);
2240 ceph_encode_timespec(&rec.atime, &inode->i_atime);
2241 rec.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
Sage Weil2f2dc052009-10-06 11:31:09 -07002242 spin_unlock(&inode->i_lock);
2243
Sage Weil93cea5b2009-12-23 12:21:51 -08002244 err = ceph_pagelist_append(pagelist, &rec, sizeof(rec));
2245
2246out:
Sage Weil2f2dc052009-10-06 11:31:09 -07002247 kfree(path);
2248 dput(dentry);
Sage Weil93cea5b2009-12-23 12:21:51 -08002249 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002250}
2251
2252
2253/*
2254 * If an MDS fails and recovers, clients need to reconnect in order to
2255 * reestablish shared state. This includes all caps issued through
2256 * this session _and_ the snap_realm hierarchy. Because it's not
2257 * clear which snap realms the mds cares about, we send everything we
2258 * know about.. that ensures we'll then get any new info the
2259 * recovering MDS might have.
2260 *
2261 * This is a relatively heavyweight operation, but it's rare.
2262 *
2263 * called with mdsc->mutex held.
2264 */
Sage Weil34b6c852010-05-10 16:31:25 -07002265static void send_mds_reconnect(struct ceph_mds_client *mdsc,
2266 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07002267{
Sage Weil2f2dc052009-10-06 11:31:09 -07002268 struct ceph_msg *reply;
Sage Weila105f002010-02-15 14:37:55 -08002269 struct rb_node *p;
Sage Weil34b6c852010-05-10 16:31:25 -07002270 int mds = session->s_mds;
Sage Weil9abf82b2010-05-10 21:58:38 -07002271 int err = -ENOMEM;
Sage Weil93cea5b2009-12-23 12:21:51 -08002272 struct ceph_pagelist *pagelist;
Sage Weil2f2dc052009-10-06 11:31:09 -07002273
Sage Weil34b6c852010-05-10 16:31:25 -07002274 pr_info("mds%d reconnect start\n", mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002275
Sage Weil93cea5b2009-12-23 12:21:51 -08002276 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
2277 if (!pagelist)
2278 goto fail_nopagelist;
2279 ceph_pagelist_init(pagelist);
2280
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002281 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -07002282 if (!reply)
Sage Weil93cea5b2009-12-23 12:21:51 -08002283 goto fail_nomsg;
Sage Weil93cea5b2009-12-23 12:21:51 -08002284
Sage Weil34b6c852010-05-10 16:31:25 -07002285 mutex_lock(&session->s_mutex);
2286 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
2287 session->s_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002288
Sage Weil34b6c852010-05-10 16:31:25 -07002289 ceph_con_open(&session->s_con,
2290 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
Sage Weil2f2dc052009-10-06 11:31:09 -07002291
Sage Weil34b6c852010-05-10 16:31:25 -07002292 /* replay unsafe requests */
2293 replay_unsafe_requests(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002294
2295 down_read(&mdsc->snap_rwsem);
2296
Sage Weil2f2dc052009-10-06 11:31:09 -07002297 dout("session %p state %s\n", session,
2298 session_state_name(session->s_state));
2299
Sage Weile01a5942010-05-10 15:36:44 -07002300 /* drop old cap expires; we're about to reestablish that state */
2301 discard_cap_releases(mdsc, session);
2302
Sage Weil2f2dc052009-10-06 11:31:09 -07002303 /* traverse this session's caps */
Sage Weil93cea5b2009-12-23 12:21:51 -08002304 err = ceph_pagelist_encode_32(pagelist, session->s_nr_caps);
2305 if (err)
2306 goto fail;
2307 err = iterate_session_caps(session, encode_caps_cb, pagelist);
Sage Weil2f2dc052009-10-06 11:31:09 -07002308 if (err < 0)
Sage Weil9abf82b2010-05-10 21:58:38 -07002309 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07002310
2311 /*
2312 * snaprealms. we provide mds with the ino, seq (version), and
2313 * parent for all of our realms. If the mds has any newer info,
2314 * it will tell us.
2315 */
Sage Weila105f002010-02-15 14:37:55 -08002316 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
2317 struct ceph_snap_realm *realm =
2318 rb_entry(p, struct ceph_snap_realm, node);
Sage Weil93cea5b2009-12-23 12:21:51 -08002319 struct ceph_mds_snaprealm_reconnect sr_rec;
Sage Weil2f2dc052009-10-06 11:31:09 -07002320
2321 dout(" adding snap realm %llx seq %lld parent %llx\n",
2322 realm->ino, realm->seq, realm->parent_ino);
Sage Weil93cea5b2009-12-23 12:21:51 -08002323 sr_rec.ino = cpu_to_le64(realm->ino);
2324 sr_rec.seq = cpu_to_le64(realm->seq);
2325 sr_rec.parent = cpu_to_le64(realm->parent_ino);
2326 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
2327 if (err)
2328 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07002329 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002330
Sage Weil93cea5b2009-12-23 12:21:51 -08002331 reply->pagelist = pagelist;
2332 reply->hdr.data_len = cpu_to_le32(pagelist->length);
2333 reply->nr_pages = calc_pages_for(0, pagelist->length);
Sage Weil2f2dc052009-10-06 11:31:09 -07002334 ceph_con_send(&session->s_con, reply);
2335
Sage Weil9abf82b2010-05-10 21:58:38 -07002336 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002337
Sage Weil9abf82b2010-05-10 21:58:38 -07002338 mutex_lock(&mdsc->mutex);
2339 __wake_requests(mdsc, &session->s_waiting);
2340 mutex_unlock(&mdsc->mutex);
2341
Sage Weil2f2dc052009-10-06 11:31:09 -07002342 up_read(&mdsc->snap_rwsem);
Sage Weil2f2dc052009-10-06 11:31:09 -07002343 return;
2344
Sage Weil93cea5b2009-12-23 12:21:51 -08002345fail:
Sage Weil2f2dc052009-10-06 11:31:09 -07002346 ceph_msg_put(reply);
Sage Weil9abf82b2010-05-10 21:58:38 -07002347 up_read(&mdsc->snap_rwsem);
2348 mutex_unlock(&session->s_mutex);
Sage Weil93cea5b2009-12-23 12:21:51 -08002349fail_nomsg:
2350 ceph_pagelist_release(pagelist);
2351 kfree(pagelist);
2352fail_nopagelist:
Sage Weil9abf82b2010-05-10 21:58:38 -07002353 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
Sage Weil9abf82b2010-05-10 21:58:38 -07002354 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07002355}
2356
2357
2358/*
2359 * compare old and new mdsmaps, kicking requests
2360 * and closing out old connections as necessary
2361 *
2362 * called under mdsc->mutex.
2363 */
2364static void check_new_map(struct ceph_mds_client *mdsc,
2365 struct ceph_mdsmap *newmap,
2366 struct ceph_mdsmap *oldmap)
2367{
2368 int i;
2369 int oldstate, newstate;
2370 struct ceph_mds_session *s;
2371
2372 dout("check_new_map new %u old %u\n",
2373 newmap->m_epoch, oldmap->m_epoch);
2374
2375 for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
2376 if (mdsc->sessions[i] == NULL)
2377 continue;
2378 s = mdsc->sessions[i];
2379 oldstate = ceph_mdsmap_get_state(oldmap, i);
2380 newstate = ceph_mdsmap_get_state(newmap, i);
2381
Sage Weil0deb01c2010-06-17 14:19:01 -07002382 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002383 i, ceph_mds_state_name(oldstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07002384 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
Sage Weil2f2dc052009-10-06 11:31:09 -07002385 ceph_mds_state_name(newstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07002386 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
Sage Weil2f2dc052009-10-06 11:31:09 -07002387 session_state_name(s->s_state));
2388
2389 if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
2390 ceph_mdsmap_get_addr(newmap, i),
2391 sizeof(struct ceph_entity_addr))) {
2392 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
2393 /* the session never opened, just close it
2394 * out now */
2395 __wake_requests(mdsc, &s->s_waiting);
Sage Weil2600d2d2010-02-22 15:12:16 -08002396 __unregister_session(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07002397 } else {
2398 /* just close it */
2399 mutex_unlock(&mdsc->mutex);
2400 mutex_lock(&s->s_mutex);
2401 mutex_lock(&mdsc->mutex);
2402 ceph_con_close(&s->s_con);
2403 mutex_unlock(&s->s_mutex);
2404 s->s_state = CEPH_MDS_SESSION_RESTARTING;
2405 }
2406
2407 /* kick any requests waiting on the recovering mds */
Sage Weil29790f22010-03-18 14:45:05 -07002408 kick_requests(mdsc, i);
Sage Weil2f2dc052009-10-06 11:31:09 -07002409 } else if (oldstate == newstate) {
2410 continue; /* nothing new with this mds */
2411 }
2412
2413 /*
2414 * send reconnect?
2415 */
2416 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
Sage Weil34b6c852010-05-10 16:31:25 -07002417 newstate >= CEPH_MDS_STATE_RECONNECT) {
2418 mutex_unlock(&mdsc->mutex);
2419 send_mds_reconnect(mdsc, s);
2420 mutex_lock(&mdsc->mutex);
2421 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002422
2423 /*
Sage Weil29790f22010-03-18 14:45:05 -07002424 * kick request on any mds that has gone active.
Sage Weil2f2dc052009-10-06 11:31:09 -07002425 */
2426 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
2427 newstate >= CEPH_MDS_STATE_ACTIVE) {
Sage Weil29790f22010-03-18 14:45:05 -07002428 if (oldstate != CEPH_MDS_STATE_CREATING &&
2429 oldstate != CEPH_MDS_STATE_STARTING)
2430 pr_info("mds%d recovery completed\n", s->s_mds);
2431 kick_requests(mdsc, i);
Sage Weil2f2dc052009-10-06 11:31:09 -07002432 ceph_kick_flushing_caps(mdsc, s);
Sage Weil0dc25702009-11-20 13:43:45 -08002433 wake_up_session_caps(s, 1);
Sage Weil2f2dc052009-10-06 11:31:09 -07002434 }
2435 }
2436}
2437
2438
2439
2440/*
2441 * leases
2442 */
2443
2444/*
2445 * caller must hold session s_mutex, dentry->d_lock
2446 */
2447void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
2448{
2449 struct ceph_dentry_info *di = ceph_dentry(dentry);
2450
2451 ceph_put_mds_session(di->lease_session);
2452 di->lease_session = NULL;
2453}
2454
Sage Weil2600d2d2010-02-22 15:12:16 -08002455static void handle_lease(struct ceph_mds_client *mdsc,
2456 struct ceph_mds_session *session,
2457 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002458{
2459 struct super_block *sb = mdsc->client->sb;
2460 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07002461 struct ceph_inode_info *ci;
2462 struct dentry *parent, *dentry;
2463 struct ceph_dentry_info *di;
Sage Weil2600d2d2010-02-22 15:12:16 -08002464 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002465 struct ceph_mds_lease *h = msg->front.iov_base;
Sage Weil1e5ea232010-06-04 10:05:40 -07002466 u32 seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002467 struct ceph_vino vino;
2468 int mask;
2469 struct qstr dname;
2470 int release = 0;
2471
Sage Weil2f2dc052009-10-06 11:31:09 -07002472 dout("handle_lease from mds%d\n", mds);
2473
2474 /* decode */
2475 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
2476 goto bad;
2477 vino.ino = le64_to_cpu(h->ino);
2478 vino.snap = CEPH_NOSNAP;
2479 mask = le16_to_cpu(h->mask);
Sage Weil1e5ea232010-06-04 10:05:40 -07002480 seq = le32_to_cpu(h->seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002481 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
2482 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
2483 if (dname.len != get_unaligned_le32(h+1))
2484 goto bad;
2485
Sage Weil2f2dc052009-10-06 11:31:09 -07002486 mutex_lock(&session->s_mutex);
2487 session->s_seq++;
2488
2489 /* lookup inode */
2490 inode = ceph_find_inode(sb, vino);
Sage Weil1e5ea232010-06-04 10:05:40 -07002491 dout("handle_lease %s, mask %d, ino %llx %p %.*s\n",
2492 ceph_lease_op_name(h->action), mask, vino.ino, inode,
2493 dname.len, dname.name);
Sage Weil2f2dc052009-10-06 11:31:09 -07002494 if (inode == NULL) {
2495 dout("handle_lease no inode %llx\n", vino.ino);
2496 goto release;
2497 }
2498 ci = ceph_inode(inode);
2499
2500 /* dentry */
2501 parent = d_find_alias(inode);
2502 if (!parent) {
2503 dout("no parent dentry on inode %p\n", inode);
2504 WARN_ON(1);
2505 goto release; /* hrm... */
2506 }
2507 dname.hash = full_name_hash(dname.name, dname.len);
2508 dentry = d_lookup(parent, &dname);
2509 dput(parent);
2510 if (!dentry)
2511 goto release;
2512
2513 spin_lock(&dentry->d_lock);
2514 di = ceph_dentry(dentry);
2515 switch (h->action) {
2516 case CEPH_MDS_LEASE_REVOKE:
2517 if (di && di->lease_session == session) {
Sage Weil1e5ea232010-06-04 10:05:40 -07002518 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
2519 h->seq = cpu_to_le32(di->lease_seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002520 __ceph_mdsc_drop_dentry_lease(dentry);
2521 }
2522 release = 1;
2523 break;
2524
2525 case CEPH_MDS_LEASE_RENEW:
2526 if (di && di->lease_session == session &&
2527 di->lease_gen == session->s_cap_gen &&
2528 di->lease_renew_from &&
2529 di->lease_renew_after == 0) {
2530 unsigned long duration =
2531 le32_to_cpu(h->duration_ms) * HZ / 1000;
2532
Sage Weil1e5ea232010-06-04 10:05:40 -07002533 di->lease_seq = seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002534 dentry->d_time = di->lease_renew_from + duration;
2535 di->lease_renew_after = di->lease_renew_from +
2536 (duration >> 1);
2537 di->lease_renew_from = 0;
2538 }
2539 break;
2540 }
2541 spin_unlock(&dentry->d_lock);
2542 dput(dentry);
2543
2544 if (!release)
2545 goto out;
2546
2547release:
2548 /* let's just reuse the same message */
2549 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
2550 ceph_msg_get(msg);
2551 ceph_con_send(&session->s_con, msg);
2552
2553out:
2554 iput(inode);
2555 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002556 return;
2557
2558bad:
2559 pr_err("corrupt lease message\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002560 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002561}
2562
2563void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
2564 struct inode *inode,
2565 struct dentry *dentry, char action,
2566 u32 seq)
2567{
2568 struct ceph_msg *msg;
2569 struct ceph_mds_lease *lease;
2570 int len = sizeof(*lease) + sizeof(u32);
2571 int dnamelen = 0;
2572
2573 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
2574 inode, dentry, ceph_lease_op_name(action), session->s_mds);
2575 dnamelen = dentry->d_name.len;
2576 len += dnamelen;
2577
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002578 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -07002579 if (!msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002580 return;
2581 lease = msg->front.iov_base;
2582 lease->action = action;
Sage Weildd1c9052010-05-25 16:45:25 -07002583 lease->mask = cpu_to_le16(1);
Sage Weil2f2dc052009-10-06 11:31:09 -07002584 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
2585 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
2586 lease->seq = cpu_to_le32(seq);
2587 put_unaligned_le32(dnamelen, lease + 1);
2588 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
2589
2590 /*
2591 * if this is a preemptive lease RELEASE, no need to
2592 * flush request stream, since the actual request will
2593 * soon follow.
2594 */
2595 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
2596
2597 ceph_con_send(&session->s_con, msg);
2598}
2599
2600/*
2601 * Preemptively release a lease we expect to invalidate anyway.
2602 * Pass @inode always, @dentry is optional.
2603 */
2604void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
2605 struct dentry *dentry, int mask)
2606{
2607 struct ceph_dentry_info *di;
2608 struct ceph_mds_session *session;
2609 u32 seq;
2610
2611 BUG_ON(inode == NULL);
2612 BUG_ON(dentry == NULL);
Sage Weildd1c9052010-05-25 16:45:25 -07002613 BUG_ON(mask == 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07002614
2615 /* is dentry lease valid? */
2616 spin_lock(&dentry->d_lock);
2617 di = ceph_dentry(dentry);
2618 if (!di || !di->lease_session ||
2619 di->lease_session->s_mds < 0 ||
2620 di->lease_gen != di->lease_session->s_cap_gen ||
2621 !time_before(jiffies, dentry->d_time)) {
2622 dout("lease_release inode %p dentry %p -- "
2623 "no lease on %d\n",
2624 inode, dentry, mask);
2625 spin_unlock(&dentry->d_lock);
2626 return;
2627 }
2628
2629 /* we do have a lease on this dentry; note mds and seq */
2630 session = ceph_get_mds_session(di->lease_session);
2631 seq = di->lease_seq;
2632 __ceph_mdsc_drop_dentry_lease(dentry);
2633 spin_unlock(&dentry->d_lock);
2634
2635 dout("lease_release inode %p dentry %p mask %d to mds%d\n",
2636 inode, dentry, mask, session->s_mds);
2637 ceph_mdsc_lease_send_msg(session, inode, dentry,
2638 CEPH_MDS_LEASE_RELEASE, seq);
2639 ceph_put_mds_session(session);
2640}
2641
2642/*
2643 * drop all leases (and dentry refs) in preparation for umount
2644 */
2645static void drop_leases(struct ceph_mds_client *mdsc)
2646{
2647 int i;
2648
2649 dout("drop_leases\n");
2650 mutex_lock(&mdsc->mutex);
2651 for (i = 0; i < mdsc->max_sessions; i++) {
2652 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2653 if (!s)
2654 continue;
2655 mutex_unlock(&mdsc->mutex);
2656 mutex_lock(&s->s_mutex);
2657 mutex_unlock(&s->s_mutex);
2658 ceph_put_mds_session(s);
2659 mutex_lock(&mdsc->mutex);
2660 }
2661 mutex_unlock(&mdsc->mutex);
2662}
2663
2664
2665
2666/*
2667 * delayed work -- periodically trim expired leases, renew caps with mds
2668 */
2669static void schedule_delayed(struct ceph_mds_client *mdsc)
2670{
2671 int delay = 5;
2672 unsigned hz = round_jiffies_relative(HZ * delay);
2673 schedule_delayed_work(&mdsc->delayed_work, hz);
2674}
2675
2676static void delayed_work(struct work_struct *work)
2677{
2678 int i;
2679 struct ceph_mds_client *mdsc =
2680 container_of(work, struct ceph_mds_client, delayed_work.work);
2681 int renew_interval;
2682 int renew_caps;
2683
2684 dout("mdsc delayed_work\n");
Sage Weilafcdaea2009-10-14 14:27:38 -07002685 ceph_check_delayed_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07002686
2687 mutex_lock(&mdsc->mutex);
2688 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
2689 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
2690 mdsc->last_renew_caps);
2691 if (renew_caps)
2692 mdsc->last_renew_caps = jiffies;
2693
2694 for (i = 0; i < mdsc->max_sessions; i++) {
2695 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2696 if (s == NULL)
2697 continue;
2698 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
2699 dout("resending session close request for mds%d\n",
2700 s->s_mds);
2701 request_close_session(mdsc, s);
2702 ceph_put_mds_session(s);
2703 continue;
2704 }
2705 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
2706 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
2707 s->s_state = CEPH_MDS_SESSION_HUNG;
2708 pr_info("mds%d hung\n", s->s_mds);
2709 }
2710 }
2711 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
2712 /* this mds is failed or recovering, just wait */
2713 ceph_put_mds_session(s);
2714 continue;
2715 }
2716 mutex_unlock(&mdsc->mutex);
2717
2718 mutex_lock(&s->s_mutex);
2719 if (renew_caps)
2720 send_renew_caps(mdsc, s);
2721 else
2722 ceph_con_keepalive(&s->s_con);
Sage Weilee6b2722010-06-10 12:55:52 -07002723 ceph_add_cap_releases(mdsc, s);
Sage Weilaab53dd2010-03-17 16:30:21 -07002724 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2725 s->s_state == CEPH_MDS_SESSION_HUNG)
Sage Weil3d7ded42010-06-09 16:47:10 -07002726 ceph_send_cap_releases(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07002727 mutex_unlock(&s->s_mutex);
2728 ceph_put_mds_session(s);
2729
2730 mutex_lock(&mdsc->mutex);
2731 }
2732 mutex_unlock(&mdsc->mutex);
2733
2734 schedule_delayed(mdsc);
2735}
2736
2737
Sage Weil5f44f142009-11-18 14:52:18 -08002738int ceph_mdsc_init(struct ceph_mds_client *mdsc, struct ceph_client *client)
Sage Weil2f2dc052009-10-06 11:31:09 -07002739{
2740 mdsc->client = client;
2741 mutex_init(&mdsc->mutex);
2742 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08002743 if (mdsc->mdsmap == NULL)
2744 return -ENOMEM;
2745
Sage Weil2f2dc052009-10-06 11:31:09 -07002746 init_completion(&mdsc->safe_umount_waiters);
2747 init_completion(&mdsc->session_close_waiters);
2748 INIT_LIST_HEAD(&mdsc->waiting_for_map);
2749 mdsc->sessions = NULL;
2750 mdsc->max_sessions = 0;
2751 mdsc->stopping = 0;
2752 init_rwsem(&mdsc->snap_rwsem);
Sage Weila105f002010-02-15 14:37:55 -08002753 mdsc->snap_realms = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07002754 INIT_LIST_HEAD(&mdsc->snap_empty);
2755 spin_lock_init(&mdsc->snap_empty_lock);
2756 mdsc->last_tid = 0;
Sage Weil44ca18f2010-02-15 12:08:46 -08002757 mdsc->request_tree = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07002758 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
2759 mdsc->last_renew_caps = jiffies;
2760 INIT_LIST_HEAD(&mdsc->cap_delay_list);
2761 spin_lock_init(&mdsc->cap_delay_lock);
2762 INIT_LIST_HEAD(&mdsc->snap_flush_list);
2763 spin_lock_init(&mdsc->snap_flush_lock);
2764 mdsc->cap_flush_seq = 0;
2765 INIT_LIST_HEAD(&mdsc->cap_dirty);
2766 mdsc->num_cap_flushing = 0;
2767 spin_lock_init(&mdsc->cap_dirty_lock);
2768 init_waitqueue_head(&mdsc->cap_flushing_wq);
2769 spin_lock_init(&mdsc->dentry_lru_lock);
2770 INIT_LIST_HEAD(&mdsc->dentry_lru);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08002771
Yehuda Sadeh37151662010-06-17 16:16:12 -07002772 ceph_caps_init(mdsc);
2773 ceph_adjust_min_caps(mdsc, client->min_caps);
2774
Sage Weil5f44f142009-11-18 14:52:18 -08002775 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002776}
2777
2778/*
2779 * Wait for safe replies on open mds requests. If we time out, drop
2780 * all requests from the tree to avoid dangling dentry refs.
2781 */
2782static void wait_requests(struct ceph_mds_client *mdsc)
2783{
2784 struct ceph_mds_request *req;
2785 struct ceph_client *client = mdsc->client;
2786
2787 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08002788 if (__get_oldest_req(mdsc)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002789 mutex_unlock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08002790
Sage Weil2f2dc052009-10-06 11:31:09 -07002791 dout("wait_requests waiting for requests\n");
2792 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
Sage Weil6b805182009-10-27 11:50:50 -07002793 client->mount_args->mount_timeout * HZ);
Sage Weil2f2dc052009-10-06 11:31:09 -07002794
2795 /* tear down remaining requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08002796 mutex_lock(&mdsc->mutex);
2797 while ((req = __get_oldest_req(mdsc))) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002798 dout("wait_requests timed out on tid %llu\n",
2799 req->r_tid);
Sage Weil44ca18f2010-02-15 12:08:46 -08002800 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002801 }
2802 }
2803 mutex_unlock(&mdsc->mutex);
2804 dout("wait_requests done\n");
2805}
2806
2807/*
2808 * called before mount is ro, and before dentries are torn down.
2809 * (hmm, does this still race with new lookups?)
2810 */
2811void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
2812{
2813 dout("pre_umount\n");
2814 mdsc->stopping = 1;
2815
2816 drop_leases(mdsc);
Sage Weilafcdaea2009-10-14 14:27:38 -07002817 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07002818 wait_requests(mdsc);
Sage Weil17c688c2010-06-21 16:12:26 -07002819
2820 /*
2821 * wait for reply handlers to drop their request refs and
2822 * their inode/dcache refs
2823 */
2824 ceph_msgr_flush();
Sage Weil2f2dc052009-10-06 11:31:09 -07002825}
2826
2827/*
2828 * wait for all write mds requests to flush.
2829 */
2830static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
2831{
Sage Weil80fc7312010-03-16 15:28:54 -07002832 struct ceph_mds_request *req = NULL, *nextreq;
Sage Weil44ca18f2010-02-15 12:08:46 -08002833 struct rb_node *n;
Sage Weil2f2dc052009-10-06 11:31:09 -07002834
2835 mutex_lock(&mdsc->mutex);
2836 dout("wait_unsafe_requests want %lld\n", want_tid);
Sage Weil80fc7312010-03-16 15:28:54 -07002837restart:
Sage Weil44ca18f2010-02-15 12:08:46 -08002838 req = __get_oldest_req(mdsc);
2839 while (req && req->r_tid <= want_tid) {
Sage Weil80fc7312010-03-16 15:28:54 -07002840 /* find next request */
2841 n = rb_next(&req->r_node);
2842 if (n)
2843 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
2844 else
2845 nextreq = NULL;
Sage Weil44ca18f2010-02-15 12:08:46 -08002846 if ((req->r_op & CEPH_MDS_OP_WRITE)) {
2847 /* write op */
2848 ceph_mdsc_get_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07002849 if (nextreq)
2850 ceph_mdsc_get_request(nextreq);
Sage Weil44ca18f2010-02-15 12:08:46 -08002851 mutex_unlock(&mdsc->mutex);
2852 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
2853 req->r_tid, want_tid);
2854 wait_for_completion(&req->r_safe_completion);
2855 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08002856 ceph_mdsc_put_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07002857 if (!nextreq)
2858 break; /* next dne before, so we're done! */
2859 if (RB_EMPTY_NODE(&nextreq->r_node)) {
2860 /* next request was removed from tree */
2861 ceph_mdsc_put_request(nextreq);
2862 goto restart;
2863 }
2864 ceph_mdsc_put_request(nextreq); /* won't go away */
Sage Weil44ca18f2010-02-15 12:08:46 -08002865 }
Sage Weil80fc7312010-03-16 15:28:54 -07002866 req = nextreq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002867 }
2868 mutex_unlock(&mdsc->mutex);
2869 dout("wait_unsafe_requests done\n");
2870}
2871
2872void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
2873{
2874 u64 want_tid, want_flush;
2875
Sage Weil56b7cf92010-05-03 15:22:00 -07002876 if (mdsc->client->mount_state == CEPH_MOUNT_SHUTDOWN)
2877 return;
2878
Sage Weil2f2dc052009-10-06 11:31:09 -07002879 dout("sync\n");
2880 mutex_lock(&mdsc->mutex);
2881 want_tid = mdsc->last_tid;
2882 want_flush = mdsc->cap_flush_seq;
2883 mutex_unlock(&mdsc->mutex);
2884 dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
2885
Sage Weilafcdaea2009-10-14 14:27:38 -07002886 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07002887
2888 wait_unsafe_requests(mdsc, want_tid);
2889 wait_event(mdsc->cap_flushing_wq, check_cap_flush(mdsc, want_flush));
2890}
2891
2892
2893/*
2894 * called after sb is ro.
2895 */
2896void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
2897{
2898 struct ceph_mds_session *session;
2899 int i;
2900 int n;
2901 struct ceph_client *client = mdsc->client;
Sage Weil6b805182009-10-27 11:50:50 -07002902 unsigned long started, timeout = client->mount_args->mount_timeout * HZ;
Sage Weil2f2dc052009-10-06 11:31:09 -07002903
2904 dout("close_sessions\n");
2905
2906 mutex_lock(&mdsc->mutex);
2907
2908 /* close sessions */
2909 started = jiffies;
2910 while (time_before(jiffies, started + timeout)) {
2911 dout("closing sessions\n");
2912 n = 0;
2913 for (i = 0; i < mdsc->max_sessions; i++) {
2914 session = __ceph_lookup_mds_session(mdsc, i);
2915 if (!session)
2916 continue;
2917 mutex_unlock(&mdsc->mutex);
2918 mutex_lock(&session->s_mutex);
2919 __close_session(mdsc, session);
2920 mutex_unlock(&session->s_mutex);
2921 ceph_put_mds_session(session);
2922 mutex_lock(&mdsc->mutex);
2923 n++;
2924 }
2925 if (n == 0)
2926 break;
2927
2928 if (client->mount_state == CEPH_MOUNT_SHUTDOWN)
2929 break;
2930
2931 dout("waiting for sessions to close\n");
2932 mutex_unlock(&mdsc->mutex);
2933 wait_for_completion_timeout(&mdsc->session_close_waiters,
2934 timeout);
2935 mutex_lock(&mdsc->mutex);
2936 }
2937
2938 /* tear down remaining sessions */
2939 for (i = 0; i < mdsc->max_sessions; i++) {
2940 if (mdsc->sessions[i]) {
2941 session = get_session(mdsc->sessions[i]);
Sage Weil2600d2d2010-02-22 15:12:16 -08002942 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002943 mutex_unlock(&mdsc->mutex);
2944 mutex_lock(&session->s_mutex);
2945 remove_session_caps(session);
2946 mutex_unlock(&session->s_mutex);
2947 ceph_put_mds_session(session);
2948 mutex_lock(&mdsc->mutex);
2949 }
2950 }
2951
2952 WARN_ON(!list_empty(&mdsc->cap_delay_list));
2953
2954 mutex_unlock(&mdsc->mutex);
2955
2956 ceph_cleanup_empty_realms(mdsc);
2957
2958 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
2959
2960 dout("stopped\n");
2961}
2962
2963void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
2964{
2965 dout("stop\n");
2966 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
2967 if (mdsc->mdsmap)
2968 ceph_mdsmap_destroy(mdsc->mdsmap);
2969 kfree(mdsc->sessions);
Yehuda Sadeh37151662010-06-17 16:16:12 -07002970 ceph_caps_finalize(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07002971}
2972
2973
2974/*
2975 * handle mds map update.
2976 */
2977void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
2978{
2979 u32 epoch;
2980 u32 maplen;
2981 void *p = msg->front.iov_base;
2982 void *end = p + msg->front.iov_len;
2983 struct ceph_mdsmap *newmap, *oldmap;
2984 struct ceph_fsid fsid;
2985 int err = -EINVAL;
2986
2987 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
2988 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08002989 if (ceph_check_fsid(mdsc->client, &fsid) < 0)
2990 return;
Sage Weilc89136e2009-10-14 09:59:09 -07002991 epoch = ceph_decode_32(&p);
2992 maplen = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07002993 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
2994
2995 /* do we need it? */
2996 ceph_monc_got_mdsmap(&mdsc->client->monc, epoch);
2997 mutex_lock(&mdsc->mutex);
2998 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
2999 dout("handle_map epoch %u <= our %u\n",
3000 epoch, mdsc->mdsmap->m_epoch);
3001 mutex_unlock(&mdsc->mutex);
3002 return;
3003 }
3004
3005 newmap = ceph_mdsmap_decode(&p, end);
3006 if (IS_ERR(newmap)) {
3007 err = PTR_ERR(newmap);
3008 goto bad_unlock;
3009 }
3010
3011 /* swap into place */
3012 if (mdsc->mdsmap) {
3013 oldmap = mdsc->mdsmap;
3014 mdsc->mdsmap = newmap;
3015 check_new_map(mdsc, newmap, oldmap);
3016 ceph_mdsmap_destroy(oldmap);
3017 } else {
3018 mdsc->mdsmap = newmap; /* first mds map */
3019 }
3020 mdsc->client->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
3021
3022 __wake_requests(mdsc, &mdsc->waiting_for_map);
3023
3024 mutex_unlock(&mdsc->mutex);
3025 schedule_delayed(mdsc);
3026 return;
3027
3028bad_unlock:
3029 mutex_unlock(&mdsc->mutex);
3030bad:
3031 pr_err("error decoding mdsmap %d\n", err);
3032 return;
3033}
3034
3035static struct ceph_connection *con_get(struct ceph_connection *con)
3036{
3037 struct ceph_mds_session *s = con->private;
3038
3039 if (get_session(s)) {
Sage Weil2600d2d2010-02-22 15:12:16 -08003040 dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -07003041 return con;
3042 }
3043 dout("mdsc con_get %p FAIL\n", s);
3044 return NULL;
3045}
3046
3047static void con_put(struct ceph_connection *con)
3048{
3049 struct ceph_mds_session *s = con->private;
3050
Sage Weil2f2dc052009-10-06 11:31:09 -07003051 ceph_put_mds_session(s);
Sage Weil2600d2d2010-02-22 15:12:16 -08003052 dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -07003053}
3054
3055/*
3056 * if the client is unresponsive for long enough, the mds will kill
3057 * the session entirely.
3058 */
3059static void peer_reset(struct ceph_connection *con)
3060{
3061 struct ceph_mds_session *s = con->private;
Sage Weil7e70f0e2010-03-18 13:59:12 -07003062 struct ceph_mds_client *mdsc = s->s_mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07003063
Sage Weil7e70f0e2010-03-18 13:59:12 -07003064 pr_warning("mds%d closed our session\n", s->s_mds);
3065 send_mds_reconnect(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07003066}
3067
3068static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
3069{
3070 struct ceph_mds_session *s = con->private;
3071 struct ceph_mds_client *mdsc = s->s_mdsc;
3072 int type = le16_to_cpu(msg->hdr.type);
3073
Sage Weil2600d2d2010-02-22 15:12:16 -08003074 mutex_lock(&mdsc->mutex);
3075 if (__verify_registered_session(mdsc, s) < 0) {
3076 mutex_unlock(&mdsc->mutex);
3077 goto out;
3078 }
3079 mutex_unlock(&mdsc->mutex);
3080
Sage Weil2f2dc052009-10-06 11:31:09 -07003081 switch (type) {
3082 case CEPH_MSG_MDS_MAP:
3083 ceph_mdsc_handle_map(mdsc, msg);
3084 break;
3085 case CEPH_MSG_CLIENT_SESSION:
3086 handle_session(s, msg);
3087 break;
3088 case CEPH_MSG_CLIENT_REPLY:
3089 handle_reply(s, msg);
3090 break;
3091 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
Sage Weil2600d2d2010-02-22 15:12:16 -08003092 handle_forward(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003093 break;
3094 case CEPH_MSG_CLIENT_CAPS:
3095 ceph_handle_caps(s, msg);
3096 break;
3097 case CEPH_MSG_CLIENT_SNAP:
Sage Weil2600d2d2010-02-22 15:12:16 -08003098 ceph_handle_snap(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003099 break;
3100 case CEPH_MSG_CLIENT_LEASE:
Sage Weil2600d2d2010-02-22 15:12:16 -08003101 handle_lease(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003102 break;
3103
3104 default:
3105 pr_err("received unknown message type %d %s\n", type,
3106 ceph_msg_type_name(type));
3107 }
Sage Weil2600d2d2010-02-22 15:12:16 -08003108out:
Sage Weil2f2dc052009-10-06 11:31:09 -07003109 ceph_msg_put(msg);
3110}
3111
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003112/*
3113 * authentication
3114 */
3115static int get_authorizer(struct ceph_connection *con,
3116 void **buf, int *len, int *proto,
3117 void **reply_buf, int *reply_len, int force_new)
3118{
3119 struct ceph_mds_session *s = con->private;
3120 struct ceph_mds_client *mdsc = s->s_mdsc;
3121 struct ceph_auth_client *ac = mdsc->client->monc.auth;
3122 int ret = 0;
3123
3124 if (force_new && s->s_authorizer) {
3125 ac->ops->destroy_authorizer(ac, s->s_authorizer);
3126 s->s_authorizer = NULL;
3127 }
3128 if (s->s_authorizer == NULL) {
3129 if (ac->ops->create_authorizer) {
3130 ret = ac->ops->create_authorizer(
3131 ac, CEPH_ENTITY_TYPE_MDS,
3132 &s->s_authorizer,
3133 &s->s_authorizer_buf,
3134 &s->s_authorizer_buf_len,
3135 &s->s_authorizer_reply_buf,
3136 &s->s_authorizer_reply_buf_len);
3137 if (ret)
3138 return ret;
3139 }
3140 }
3141
3142 *proto = ac->protocol;
3143 *buf = s->s_authorizer_buf;
3144 *len = s->s_authorizer_buf_len;
3145 *reply_buf = s->s_authorizer_reply_buf;
3146 *reply_len = s->s_authorizer_reply_buf_len;
3147 return 0;
3148}
3149
3150
3151static int verify_authorizer_reply(struct ceph_connection *con, int len)
3152{
3153 struct ceph_mds_session *s = con->private;
3154 struct ceph_mds_client *mdsc = s->s_mdsc;
3155 struct ceph_auth_client *ac = mdsc->client->monc.auth;
3156
3157 return ac->ops->verify_authorizer_reply(ac, s->s_authorizer, len);
3158}
3159
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003160static int invalidate_authorizer(struct ceph_connection *con)
3161{
3162 struct ceph_mds_session *s = con->private;
3163 struct ceph_mds_client *mdsc = s->s_mdsc;
3164 struct ceph_auth_client *ac = mdsc->client->monc.auth;
3165
3166 if (ac->ops->invalidate_authorizer)
3167 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
3168
3169 return ceph_monc_validate_auth(&mdsc->client->monc);
3170}
3171
Tobias Klauser9e327892010-05-20 10:40:19 +02003172static const struct ceph_connection_operations mds_con_ops = {
Sage Weil2f2dc052009-10-06 11:31:09 -07003173 .get = con_get,
3174 .put = con_put,
3175 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003176 .get_authorizer = get_authorizer,
3177 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003178 .invalidate_authorizer = invalidate_authorizer,
Sage Weil2f2dc052009-10-06 11:31:09 -07003179 .peer_reset = peer_reset,
Sage Weil2f2dc052009-10-06 11:31:09 -07003180};
3181
3182
3183
3184
3185/* eof */