blob: f091b1351786368de18757d8cb262a19d1006bf1 [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>
Greg Farnum40819f62010-08-02 15:34:23 -07006#include <linux/smp_lock.h>
Sage Weil2f2dc052009-10-06 11:31:09 -07007
8#include "mds_client.h"
9#include "mon_client.h"
10#include "super.h"
11#include "messenger.h"
12#include "decode.h"
Sage Weil4e7a5dc2009-11-18 16:19:57 -080013#include "auth.h"
Sage Weil93cea5b2009-12-23 12:21:51 -080014#include "pagelist.h"
Sage Weil2f2dc052009-10-06 11:31:09 -070015
16/*
17 * A cluster of MDS (metadata server) daemons is responsible for
18 * managing the file system namespace (the directory hierarchy and
19 * inodes) and for coordinating shared access to storage. Metadata is
20 * partitioning hierarchically across a number of servers, and that
21 * partition varies over time as the cluster adjusts the distribution
22 * in order to balance load.
23 *
24 * The MDS client is primarily responsible to managing synchronous
25 * metadata requests for operations like open, unlink, and so forth.
26 * If there is a MDS failure, we find out about it when we (possibly
27 * request and) receive a new MDS map, and can resubmit affected
28 * requests.
29 *
30 * For the most part, though, we take advantage of a lossless
31 * communications channel to the MDS, and do not need to worry about
32 * timing out or resubmitting requests.
33 *
34 * We maintain a stateful "session" with each MDS we interact with.
35 * Within each session, we sent periodic heartbeat messages to ensure
36 * any capabilities or leases we have been issues remain valid. If
37 * the session times out and goes stale, our leases and capabilities
38 * are no longer valid.
39 */
40
Sage Weil20cb34a2010-05-12 15:21:32 -070041struct ceph_reconnect_state {
42 struct ceph_pagelist *pagelist;
43 bool flock;
44};
45
Sage Weil2f2dc052009-10-06 11:31:09 -070046static void __wake_requests(struct ceph_mds_client *mdsc,
47 struct list_head *head);
48
Tobias Klauser9e327892010-05-20 10:40:19 +020049static const struct ceph_connection_operations mds_con_ops;
Sage Weil2f2dc052009-10-06 11:31:09 -070050
51
52/*
53 * mds reply parsing
54 */
55
56/*
57 * parse individual inode info
58 */
59static int parse_reply_info_in(void **p, void *end,
60 struct ceph_mds_reply_info_in *info)
61{
62 int err = -EIO;
63
64 info->in = *p;
65 *p += sizeof(struct ceph_mds_reply_inode) +
66 sizeof(*info->in->fragtree.splits) *
67 le32_to_cpu(info->in->fragtree.nsplits);
68
69 ceph_decode_32_safe(p, end, info->symlink_len, bad);
70 ceph_decode_need(p, end, info->symlink_len, bad);
71 info->symlink = *p;
72 *p += info->symlink_len;
73
74 ceph_decode_32_safe(p, end, info->xattr_len, bad);
75 ceph_decode_need(p, end, info->xattr_len, bad);
76 info->xattr_data = *p;
77 *p += info->xattr_len;
78 return 0;
79bad:
80 return err;
81}
82
83/*
84 * parse a normal reply, which may contain a (dir+)dentry and/or a
85 * target inode.
86 */
87static int parse_reply_info_trace(void **p, void *end,
88 struct ceph_mds_reply_info_parsed *info)
89{
90 int err;
91
92 if (info->head->is_dentry) {
93 err = parse_reply_info_in(p, end, &info->diri);
94 if (err < 0)
95 goto out_bad;
96
97 if (unlikely(*p + sizeof(*info->dirfrag) > end))
98 goto bad;
99 info->dirfrag = *p;
100 *p += sizeof(*info->dirfrag) +
101 sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
102 if (unlikely(*p > end))
103 goto bad;
104
105 ceph_decode_32_safe(p, end, info->dname_len, bad);
106 ceph_decode_need(p, end, info->dname_len, bad);
107 info->dname = *p;
108 *p += info->dname_len;
109 info->dlease = *p;
110 *p += sizeof(*info->dlease);
111 }
112
113 if (info->head->is_target) {
114 err = parse_reply_info_in(p, end, &info->targeti);
115 if (err < 0)
116 goto out_bad;
117 }
118
119 if (unlikely(*p != end))
120 goto bad;
121 return 0;
122
123bad:
124 err = -EIO;
125out_bad:
126 pr_err("problem parsing mds trace %d\n", err);
127 return err;
128}
129
130/*
131 * parse readdir results
132 */
133static int parse_reply_info_dir(void **p, void *end,
134 struct ceph_mds_reply_info_parsed *info)
135{
136 u32 num, i = 0;
137 int err;
138
139 info->dir_dir = *p;
140 if (*p + sizeof(*info->dir_dir) > end)
141 goto bad;
142 *p += sizeof(*info->dir_dir) +
143 sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
144 if (*p > end)
145 goto bad;
146
147 ceph_decode_need(p, end, sizeof(num) + 2, bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700148 num = ceph_decode_32(p);
149 info->dir_end = ceph_decode_8(p);
150 info->dir_complete = ceph_decode_8(p);
Sage Weil2f2dc052009-10-06 11:31:09 -0700151 if (num == 0)
152 goto done;
153
154 /* alloc large array */
155 info->dir_nr = num;
156 info->dir_in = kcalloc(num, sizeof(*info->dir_in) +
157 sizeof(*info->dir_dname) +
158 sizeof(*info->dir_dname_len) +
159 sizeof(*info->dir_dlease),
160 GFP_NOFS);
161 if (info->dir_in == NULL) {
162 err = -ENOMEM;
163 goto out_bad;
164 }
165 info->dir_dname = (void *)(info->dir_in + num);
166 info->dir_dname_len = (void *)(info->dir_dname + num);
167 info->dir_dlease = (void *)(info->dir_dname_len + num);
168
169 while (num) {
170 /* dentry */
171 ceph_decode_need(p, end, sizeof(u32)*2, bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700172 info->dir_dname_len[i] = ceph_decode_32(p);
Sage Weil2f2dc052009-10-06 11:31:09 -0700173 ceph_decode_need(p, end, info->dir_dname_len[i], bad);
174 info->dir_dname[i] = *p;
175 *p += info->dir_dname_len[i];
176 dout("parsed dir dname '%.*s'\n", info->dir_dname_len[i],
177 info->dir_dname[i]);
178 info->dir_dlease[i] = *p;
179 *p += sizeof(struct ceph_mds_reply_lease);
180
181 /* inode */
182 err = parse_reply_info_in(p, end, &info->dir_in[i]);
183 if (err < 0)
184 goto out_bad;
185 i++;
186 num--;
187 }
188
189done:
190 if (*p != end)
191 goto bad;
192 return 0;
193
194bad:
195 err = -EIO;
196out_bad:
197 pr_err("problem parsing dir contents %d\n", err);
198 return err;
199}
200
201/*
202 * parse entire mds reply
203 */
204static int parse_reply_info(struct ceph_msg *msg,
205 struct ceph_mds_reply_info_parsed *info)
206{
207 void *p, *end;
208 u32 len;
209 int err;
210
211 info->head = msg->front.iov_base;
212 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
213 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
214
215 /* trace */
216 ceph_decode_32_safe(&p, end, len, bad);
217 if (len > 0) {
218 err = parse_reply_info_trace(&p, p+len, info);
219 if (err < 0)
220 goto out_bad;
221 }
222
223 /* dir content */
224 ceph_decode_32_safe(&p, end, len, bad);
225 if (len > 0) {
226 err = parse_reply_info_dir(&p, p+len, info);
227 if (err < 0)
228 goto out_bad;
229 }
230
231 /* snap blob */
232 ceph_decode_32_safe(&p, end, len, bad);
233 info->snapblob_len = len;
234 info->snapblob = p;
235 p += len;
236
237 if (p != end)
238 goto bad;
239 return 0;
240
241bad:
242 err = -EIO;
243out_bad:
244 pr_err("mds parse_reply err %d\n", err);
245 return err;
246}
247
248static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
249{
250 kfree(info->dir_in);
251}
252
253
254/*
255 * sessions
256 */
257static const char *session_state_name(int s)
258{
259 switch (s) {
260 case CEPH_MDS_SESSION_NEW: return "new";
261 case CEPH_MDS_SESSION_OPENING: return "opening";
262 case CEPH_MDS_SESSION_OPEN: return "open";
263 case CEPH_MDS_SESSION_HUNG: return "hung";
264 case CEPH_MDS_SESSION_CLOSING: return "closing";
Sage Weil44ca18f2010-02-15 12:08:46 -0800265 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
Sage Weil2f2dc052009-10-06 11:31:09 -0700266 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
267 default: return "???";
268 }
269}
270
271static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
272{
273 if (atomic_inc_not_zero(&s->s_ref)) {
274 dout("mdsc get_session %p %d -> %d\n", s,
275 atomic_read(&s->s_ref)-1, atomic_read(&s->s_ref));
276 return s;
277 } else {
278 dout("mdsc get_session %p 0 -- FAIL", s);
279 return NULL;
280 }
281}
282
283void ceph_put_mds_session(struct ceph_mds_session *s)
284{
285 dout("mdsc put_session %p %d -> %d\n", s,
286 atomic_read(&s->s_ref), atomic_read(&s->s_ref)-1);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800287 if (atomic_dec_and_test(&s->s_ref)) {
288 if (s->s_authorizer)
289 s->s_mdsc->client->monc.auth->ops->destroy_authorizer(
290 s->s_mdsc->client->monc.auth, s->s_authorizer);
Sage Weil2f2dc052009-10-06 11:31:09 -0700291 kfree(s);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800292 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700293}
294
295/*
296 * called under mdsc->mutex
297 */
298struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
299 int mds)
300{
301 struct ceph_mds_session *session;
302
303 if (mds >= mdsc->max_sessions || mdsc->sessions[mds] == NULL)
304 return NULL;
305 session = mdsc->sessions[mds];
306 dout("lookup_mds_session %p %d\n", session,
307 atomic_read(&session->s_ref));
308 get_session(session);
309 return session;
310}
311
312static bool __have_session(struct ceph_mds_client *mdsc, int mds)
313{
314 if (mds >= mdsc->max_sessions)
315 return false;
316 return mdsc->sessions[mds];
317}
318
Sage Weil2600d2d2010-02-22 15:12:16 -0800319static int __verify_registered_session(struct ceph_mds_client *mdsc,
320 struct ceph_mds_session *s)
321{
322 if (s->s_mds >= mdsc->max_sessions ||
323 mdsc->sessions[s->s_mds] != s)
324 return -ENOENT;
325 return 0;
326}
327
Sage Weil2f2dc052009-10-06 11:31:09 -0700328/*
329 * create+register a new session for given mds.
330 * called under mdsc->mutex.
331 */
332static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
333 int mds)
334{
335 struct ceph_mds_session *s;
336
337 s = kzalloc(sizeof(*s), GFP_NOFS);
Dan Carpenter4736b002010-03-20 15:30:16 +0300338 if (!s)
339 return ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -0700340 s->s_mdsc = mdsc;
341 s->s_mds = mds;
342 s->s_state = CEPH_MDS_SESSION_NEW;
343 s->s_ttl = 0;
344 s->s_seq = 0;
345 mutex_init(&s->s_mutex);
346
347 ceph_con_init(mdsc->client->msgr, &s->s_con);
348 s->s_con.private = s;
349 s->s_con.ops = &mds_con_ops;
350 s->s_con.peer_name.type = CEPH_ENTITY_TYPE_MDS;
351 s->s_con.peer_name.num = cpu_to_le64(mds);
Sage Weil2f2dc052009-10-06 11:31:09 -0700352
353 spin_lock_init(&s->s_cap_lock);
354 s->s_cap_gen = 0;
355 s->s_cap_ttl = 0;
356 s->s_renew_requested = 0;
357 s->s_renew_seq = 0;
358 INIT_LIST_HEAD(&s->s_caps);
359 s->s_nr_caps = 0;
Sage Weil5dacf092009-12-21 20:40:34 -0800360 s->s_trim_caps = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -0700361 atomic_set(&s->s_ref, 1);
362 INIT_LIST_HEAD(&s->s_waiting);
363 INIT_LIST_HEAD(&s->s_unsafe);
364 s->s_num_cap_releases = 0;
Sage Weil7c1332b2010-02-16 11:39:45 -0800365 s->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700366 INIT_LIST_HEAD(&s->s_cap_releases);
367 INIT_LIST_HEAD(&s->s_cap_releases_done);
368 INIT_LIST_HEAD(&s->s_cap_flushing);
369 INIT_LIST_HEAD(&s->s_cap_snaps_flushing);
370
371 dout("register_session mds%d\n", mds);
372 if (mds >= mdsc->max_sessions) {
373 int newmax = 1 << get_count_order(mds+1);
374 struct ceph_mds_session **sa;
375
376 dout("register_session realloc to %d\n", newmax);
377 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
378 if (sa == NULL)
Sage Weil42ce56e2009-11-18 11:22:36 -0800379 goto fail_realloc;
Sage Weil2f2dc052009-10-06 11:31:09 -0700380 if (mdsc->sessions) {
381 memcpy(sa, mdsc->sessions,
382 mdsc->max_sessions * sizeof(void *));
383 kfree(mdsc->sessions);
384 }
385 mdsc->sessions = sa;
386 mdsc->max_sessions = newmax;
387 }
388 mdsc->sessions[mds] = s;
389 atomic_inc(&s->s_ref); /* one ref to sessions[], one to caller */
Sage Weil42ce56e2009-11-18 11:22:36 -0800390
391 ceph_con_open(&s->s_con, ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
392
Sage Weil2f2dc052009-10-06 11:31:09 -0700393 return s;
Sage Weil42ce56e2009-11-18 11:22:36 -0800394
395fail_realloc:
396 kfree(s);
397 return ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -0700398}
399
400/*
401 * called under mdsc->mutex
402 */
Sage Weil2600d2d2010-02-22 15:12:16 -0800403static void __unregister_session(struct ceph_mds_client *mdsc,
Sage Weil42ce56e2009-11-18 11:22:36 -0800404 struct ceph_mds_session *s)
Sage Weil2f2dc052009-10-06 11:31:09 -0700405{
Sage Weil2600d2d2010-02-22 15:12:16 -0800406 dout("__unregister_session mds%d %p\n", s->s_mds, s);
407 BUG_ON(mdsc->sessions[s->s_mds] != s);
Sage Weil42ce56e2009-11-18 11:22:36 -0800408 mdsc->sessions[s->s_mds] = NULL;
409 ceph_con_close(&s->s_con);
410 ceph_put_mds_session(s);
Sage Weil2f2dc052009-10-06 11:31:09 -0700411}
412
413/*
414 * drop session refs in request.
415 *
416 * should be last request ref, or hold mdsc->mutex
417 */
418static void put_request_session(struct ceph_mds_request *req)
419{
420 if (req->r_session) {
421 ceph_put_mds_session(req->r_session);
422 req->r_session = NULL;
423 }
424}
425
Sage Weil153c8e62009-12-07 12:31:09 -0800426void ceph_mdsc_release_request(struct kref *kref)
Sage Weil2f2dc052009-10-06 11:31:09 -0700427{
Sage Weil153c8e62009-12-07 12:31:09 -0800428 struct ceph_mds_request *req = container_of(kref,
429 struct ceph_mds_request,
430 r_kref);
431 if (req->r_request)
432 ceph_msg_put(req->r_request);
433 if (req->r_reply) {
434 ceph_msg_put(req->r_reply);
435 destroy_reply_info(&req->r_reply_info);
Sage Weil2f2dc052009-10-06 11:31:09 -0700436 }
Sage Weil153c8e62009-12-07 12:31:09 -0800437 if (req->r_inode) {
438 ceph_put_cap_refs(ceph_inode(req->r_inode),
439 CEPH_CAP_PIN);
440 iput(req->r_inode);
441 }
442 if (req->r_locked_dir)
443 ceph_put_cap_refs(ceph_inode(req->r_locked_dir),
444 CEPH_CAP_PIN);
445 if (req->r_target_inode)
446 iput(req->r_target_inode);
447 if (req->r_dentry)
448 dput(req->r_dentry);
449 if (req->r_old_dentry) {
450 ceph_put_cap_refs(
451 ceph_inode(req->r_old_dentry->d_parent->d_inode),
452 CEPH_CAP_PIN);
453 dput(req->r_old_dentry);
454 }
455 kfree(req->r_path1);
456 kfree(req->r_path2);
457 put_request_session(req);
Yehuda Sadeh37151662010-06-17 16:16:12 -0700458 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
Sage Weil153c8e62009-12-07 12:31:09 -0800459 kfree(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700460}
461
462/*
463 * lookup session, bump ref if found.
464 *
465 * called under mdsc->mutex.
466 */
467static struct ceph_mds_request *__lookup_request(struct ceph_mds_client *mdsc,
468 u64 tid)
469{
470 struct ceph_mds_request *req;
Sage Weil44ca18f2010-02-15 12:08:46 -0800471 struct rb_node *n = mdsc->request_tree.rb_node;
472
473 while (n) {
474 req = rb_entry(n, struct ceph_mds_request, r_node);
475 if (tid < req->r_tid)
476 n = n->rb_left;
477 else if (tid > req->r_tid)
478 n = n->rb_right;
479 else {
480 ceph_mdsc_get_request(req);
481 return req;
482 }
483 }
484 return NULL;
485}
486
487static void __insert_request(struct ceph_mds_client *mdsc,
488 struct ceph_mds_request *new)
489{
490 struct rb_node **p = &mdsc->request_tree.rb_node;
491 struct rb_node *parent = NULL;
492 struct ceph_mds_request *req = NULL;
493
494 while (*p) {
495 parent = *p;
496 req = rb_entry(parent, struct ceph_mds_request, r_node);
497 if (new->r_tid < req->r_tid)
498 p = &(*p)->rb_left;
499 else if (new->r_tid > req->r_tid)
500 p = &(*p)->rb_right;
501 else
502 BUG();
503 }
504
505 rb_link_node(&new->r_node, parent, p);
506 rb_insert_color(&new->r_node, &mdsc->request_tree);
Sage Weil2f2dc052009-10-06 11:31:09 -0700507}
508
509/*
510 * Register an in-flight request, and assign a tid. Link to directory
511 * are modifying (if any).
512 *
513 * Called under mdsc->mutex.
514 */
515static void __register_request(struct ceph_mds_client *mdsc,
516 struct ceph_mds_request *req,
517 struct inode *dir)
518{
519 req->r_tid = ++mdsc->last_tid;
520 if (req->r_num_caps)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700521 ceph_reserve_caps(mdsc, &req->r_caps_reservation,
522 req->r_num_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -0700523 dout("__register_request %p tid %lld\n", req, req->r_tid);
524 ceph_mdsc_get_request(req);
Sage Weil44ca18f2010-02-15 12:08:46 -0800525 __insert_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700526
527 if (dir) {
528 struct ceph_inode_info *ci = ceph_inode(dir);
529
530 spin_lock(&ci->i_unsafe_lock);
531 req->r_unsafe_dir = dir;
532 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
533 spin_unlock(&ci->i_unsafe_lock);
534 }
535}
536
537static void __unregister_request(struct ceph_mds_client *mdsc,
538 struct ceph_mds_request *req)
539{
540 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
Sage Weil44ca18f2010-02-15 12:08:46 -0800541 rb_erase(&req->r_node, &mdsc->request_tree);
Sage Weil80fc7312010-03-16 15:28:54 -0700542 RB_CLEAR_NODE(&req->r_node);
Sage Weil2f2dc052009-10-06 11:31:09 -0700543
544 if (req->r_unsafe_dir) {
545 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
546
547 spin_lock(&ci->i_unsafe_lock);
548 list_del_init(&req->r_unsafe_dir_item);
549 spin_unlock(&ci->i_unsafe_lock);
550 }
Sage Weil94aa8ae2010-03-28 21:22:50 -0700551
552 ceph_mdsc_put_request(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700553}
554
555/*
556 * Choose mds to send request to next. If there is a hint set in the
557 * request (e.g., due to a prior forward hint from the mds), use that.
558 * Otherwise, consult frag tree and/or caps to identify the
559 * appropriate mds. If all else fails, choose randomly.
560 *
561 * Called under mdsc->mutex.
562 */
Sage Weileb6bb1c2010-08-16 09:21:27 -0700563struct dentry *get_nonsnap_parent(struct dentry *dentry)
564{
565 while (!IS_ROOT(dentry) && ceph_snap(dentry->d_inode) != CEPH_NOSNAP)
566 dentry = dentry->d_parent;
567 return dentry;
568}
569
Sage Weil2f2dc052009-10-06 11:31:09 -0700570static int __choose_mds(struct ceph_mds_client *mdsc,
571 struct ceph_mds_request *req)
572{
573 struct inode *inode;
574 struct ceph_inode_info *ci;
575 struct ceph_cap *cap;
576 int mode = req->r_direct_mode;
577 int mds = -1;
578 u32 hash = req->r_direct_hash;
579 bool is_hash = req->r_direct_is_hash;
580
581 /*
582 * is there a specific mds we should try? ignore hint if we have
583 * no session and the mds is not up (active or recovering).
584 */
585 if (req->r_resend_mds >= 0 &&
586 (__have_session(mdsc, req->r_resend_mds) ||
587 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
588 dout("choose_mds using resend_mds mds%d\n",
589 req->r_resend_mds);
590 return req->r_resend_mds;
591 }
592
593 if (mode == USE_RANDOM_MDS)
594 goto random;
595
596 inode = NULL;
597 if (req->r_inode) {
598 inode = req->r_inode;
599 } else if (req->r_dentry) {
Sage Weileb6bb1c2010-08-16 09:21:27 -0700600 struct inode *dir = req->r_dentry->d_parent->d_inode;
601
602 if (dir->i_sb != mdsc->client->sb) {
603 /* not this fs! */
604 inode = req->r_dentry->d_inode;
605 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
606 /* direct snapped/virtual snapdir requests
607 * based on parent dir inode */
608 struct dentry *dn =
609 get_nonsnap_parent(req->r_dentry->d_parent);
610 inode = dn->d_inode;
611 dout("__choose_mds using nonsnap parent %p\n", inode);
612 } else if (req->r_dentry->d_inode) {
613 /* dentry target */
Sage Weil2f2dc052009-10-06 11:31:09 -0700614 inode = req->r_dentry->d_inode;
615 } else {
Sage Weileb6bb1c2010-08-16 09:21:27 -0700616 /* dir + name */
617 inode = dir;
Sage Weil2f2dc052009-10-06 11:31:09 -0700618 hash = req->r_dentry->d_name.hash;
619 is_hash = true;
620 }
621 }
Sage Weileb6bb1c2010-08-16 09:21:27 -0700622
Sage Weil2f2dc052009-10-06 11:31:09 -0700623 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
624 (int)hash, mode);
625 if (!inode)
626 goto random;
627 ci = ceph_inode(inode);
628
629 if (is_hash && S_ISDIR(inode->i_mode)) {
630 struct ceph_inode_frag frag;
631 int found;
632
633 ceph_choose_frag(ci, hash, &frag, &found);
634 if (found) {
635 if (mode == USE_ANY_MDS && frag.ndist > 0) {
636 u8 r;
637
638 /* choose a random replica */
639 get_random_bytes(&r, 1);
640 r %= frag.ndist;
641 mds = frag.dist[r];
642 dout("choose_mds %p %llx.%llx "
643 "frag %u mds%d (%d/%d)\n",
644 inode, ceph_vinop(inode),
645 frag.frag, frag.mds,
646 (int)r, frag.ndist);
647 return mds;
648 }
649
650 /* since this file/dir wasn't known to be
651 * replicated, then we want to look for the
652 * authoritative mds. */
653 mode = USE_AUTH_MDS;
654 if (frag.mds >= 0) {
655 /* choose auth mds */
656 mds = frag.mds;
657 dout("choose_mds %p %llx.%llx "
658 "frag %u mds%d (auth)\n",
659 inode, ceph_vinop(inode), frag.frag, mds);
660 return mds;
661 }
662 }
663 }
664
665 spin_lock(&inode->i_lock);
666 cap = NULL;
667 if (mode == USE_AUTH_MDS)
668 cap = ci->i_auth_cap;
669 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
670 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
671 if (!cap) {
672 spin_unlock(&inode->i_lock);
673 goto random;
674 }
675 mds = cap->session->s_mds;
676 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
677 inode, ceph_vinop(inode), mds,
678 cap == ci->i_auth_cap ? "auth " : "", cap);
679 spin_unlock(&inode->i_lock);
680 return mds;
681
682random:
683 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
684 dout("choose_mds chose random mds%d\n", mds);
685 return mds;
686}
687
688
689/*
690 * session messages
691 */
692static struct ceph_msg *create_session_msg(u32 op, u64 seq)
693{
694 struct ceph_msg *msg;
695 struct ceph_mds_session_head *h;
696
Yehuda Sadeh34d23762010-04-06 14:33:58 -0700697 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -0700698 if (!msg) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700699 pr_err("create_session_msg ENOMEM creating msg\n");
Sage Weila79832f2010-04-01 16:06:19 -0700700 return NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700701 }
702 h = msg->front.iov_base;
703 h->op = cpu_to_le32(op);
704 h->seq = cpu_to_le64(seq);
705 return msg;
706}
707
708/*
709 * send session open request.
710 *
711 * called under mdsc->mutex
712 */
713static int __open_session(struct ceph_mds_client *mdsc,
714 struct ceph_mds_session *session)
715{
716 struct ceph_msg *msg;
717 int mstate;
718 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -0700719
720 /* wait for mds to go active? */
721 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
722 dout("open_session to mds%d (%s)\n", mds,
723 ceph_mds_state_name(mstate));
724 session->s_state = CEPH_MDS_SESSION_OPENING;
725 session->s_renew_requested = jiffies;
726
727 /* send connect message */
728 msg = create_session_msg(CEPH_SESSION_REQUEST_OPEN, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -0700729 if (!msg)
730 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -0700731 ceph_con_send(&session->s_con, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -0700732 return 0;
733}
734
735/*
Sage Weiled0552a2010-06-21 13:38:25 -0700736 * open sessions for any export targets for the given mds
737 *
738 * called under mdsc->mutex
739 */
740static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
741 struct ceph_mds_session *session)
742{
743 struct ceph_mds_info *mi;
744 struct ceph_mds_session *ts;
745 int i, mds = session->s_mds;
746 int target;
747
748 if (mds >= mdsc->mdsmap->m_max_mds)
749 return;
750 mi = &mdsc->mdsmap->m_info[mds];
751 dout("open_export_target_sessions for mds%d (%d targets)\n",
752 session->s_mds, mi->num_export_targets);
753
754 for (i = 0; i < mi->num_export_targets; i++) {
755 target = mi->export_targets[i];
756 ts = __ceph_lookup_mds_session(mdsc, target);
757 if (!ts) {
758 ts = register_session(mdsc, target);
759 if (IS_ERR(ts))
760 return;
761 }
762 if (session->s_state == CEPH_MDS_SESSION_NEW ||
763 session->s_state == CEPH_MDS_SESSION_CLOSING)
764 __open_session(mdsc, session);
765 else
766 dout(" mds%d target mds%d %p is %s\n", session->s_mds,
767 i, ts, session_state_name(ts->s_state));
768 ceph_put_mds_session(ts);
769 }
770}
771
Sage Weil154f42c2010-06-21 13:45:04 -0700772void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
773 struct ceph_mds_session *session)
774{
775 mutex_lock(&mdsc->mutex);
776 __open_export_target_sessions(mdsc, session);
777 mutex_unlock(&mdsc->mutex);
778}
779
Sage Weiled0552a2010-06-21 13:38:25 -0700780/*
Sage Weil2f2dc052009-10-06 11:31:09 -0700781 * session caps
782 */
783
784/*
785 * Free preallocated cap messages assigned to this session
786 */
787static void cleanup_cap_releases(struct ceph_mds_session *session)
788{
789 struct ceph_msg *msg;
790
791 spin_lock(&session->s_cap_lock);
792 while (!list_empty(&session->s_cap_releases)) {
793 msg = list_first_entry(&session->s_cap_releases,
794 struct ceph_msg, list_head);
795 list_del_init(&msg->list_head);
796 ceph_msg_put(msg);
797 }
798 while (!list_empty(&session->s_cap_releases_done)) {
799 msg = list_first_entry(&session->s_cap_releases_done,
800 struct ceph_msg, list_head);
801 list_del_init(&msg->list_head);
802 ceph_msg_put(msg);
803 }
804 spin_unlock(&session->s_cap_lock);
805}
806
807/*
Sage Weilf818a732010-05-11 20:56:31 -0700808 * Helper to safely iterate over all caps associated with a session, with
809 * special care taken to handle a racing __ceph_remove_cap().
Sage Weil2f2dc052009-10-06 11:31:09 -0700810 *
Sage Weilf818a732010-05-11 20:56:31 -0700811 * Caller must hold session s_mutex.
Sage Weil2f2dc052009-10-06 11:31:09 -0700812 */
813static int iterate_session_caps(struct ceph_mds_session *session,
814 int (*cb)(struct inode *, struct ceph_cap *,
815 void *), void *arg)
816{
Sage Weil7c1332b2010-02-16 11:39:45 -0800817 struct list_head *p;
818 struct ceph_cap *cap;
819 struct inode *inode, *last_inode = NULL;
820 struct ceph_cap *old_cap = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700821 int ret;
822
823 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
824 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800825 p = session->s_caps.next;
826 while (p != &session->s_caps) {
827 cap = list_entry(p, struct ceph_cap, session_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -0700828 inode = igrab(&cap->ci->vfs_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -0800829 if (!inode) {
830 p = p->next;
Sage Weil2f2dc052009-10-06 11:31:09 -0700831 continue;
Sage Weil7c1332b2010-02-16 11:39:45 -0800832 }
833 session->s_cap_iterator = cap;
Sage Weil2f2dc052009-10-06 11:31:09 -0700834 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800835
836 if (last_inode) {
837 iput(last_inode);
838 last_inode = NULL;
839 }
840 if (old_cap) {
Yehuda Sadeh37151662010-06-17 16:16:12 -0700841 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -0800842 old_cap = NULL;
843 }
844
Sage Weil2f2dc052009-10-06 11:31:09 -0700845 ret = cb(inode, cap, arg);
Sage Weil7c1332b2010-02-16 11:39:45 -0800846 last_inode = inode;
847
Sage Weil2f2dc052009-10-06 11:31:09 -0700848 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800849 p = p->next;
850 if (cap->ci == NULL) {
851 dout("iterate_session_caps finishing cap %p removal\n",
852 cap);
853 BUG_ON(cap->session != session);
854 list_del_init(&cap->session_caps);
855 session->s_nr_caps--;
856 cap->session = NULL;
857 old_cap = cap; /* put_cap it w/o locks held */
858 }
Sage Weil5dacf092009-12-21 20:40:34 -0800859 if (ret < 0)
860 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -0700861 }
Sage Weil5dacf092009-12-21 20:40:34 -0800862 ret = 0;
863out:
Sage Weil7c1332b2010-02-16 11:39:45 -0800864 session->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700865 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800866
867 if (last_inode)
868 iput(last_inode);
869 if (old_cap)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700870 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -0800871
Sage Weil5dacf092009-12-21 20:40:34 -0800872 return ret;
Sage Weil2f2dc052009-10-06 11:31:09 -0700873}
874
875static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
Sage Weil6c99f252010-05-10 16:12:25 -0700876 void *arg)
Sage Weil2f2dc052009-10-06 11:31:09 -0700877{
878 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil6c99f252010-05-10 16:12:25 -0700879 int drop = 0;
880
Sage Weil2f2dc052009-10-06 11:31:09 -0700881 dout("removing cap %p, ci is %p, inode is %p\n",
882 cap, ci, &ci->vfs_inode);
Sage Weil6c99f252010-05-10 16:12:25 -0700883 spin_lock(&inode->i_lock);
884 __ceph_remove_cap(cap);
885 if (!__ceph_is_any_real_caps(ci)) {
886 struct ceph_mds_client *mdsc =
887 &ceph_sb_to_client(inode->i_sb)->mdsc;
888
889 spin_lock(&mdsc->cap_dirty_lock);
890 if (!list_empty(&ci->i_dirty_item)) {
891 pr_info(" dropping dirty %s state for %p %lld\n",
892 ceph_cap_string(ci->i_dirty_caps),
893 inode, ceph_ino(inode));
894 ci->i_dirty_caps = 0;
895 list_del_init(&ci->i_dirty_item);
896 drop = 1;
897 }
898 if (!list_empty(&ci->i_flushing_item)) {
899 pr_info(" dropping dirty+flushing %s state for %p %lld\n",
900 ceph_cap_string(ci->i_flushing_caps),
901 inode, ceph_ino(inode));
902 ci->i_flushing_caps = 0;
903 list_del_init(&ci->i_flushing_item);
904 mdsc->num_cap_flushing--;
905 drop = 1;
906 }
907 if (drop && ci->i_wrbuffer_ref) {
908 pr_info(" dropping dirty data for %p %lld\n",
909 inode, ceph_ino(inode));
910 ci->i_wrbuffer_ref = 0;
911 ci->i_wrbuffer_ref_head = 0;
912 drop++;
913 }
914 spin_unlock(&mdsc->cap_dirty_lock);
915 }
916 spin_unlock(&inode->i_lock);
917 while (drop--)
918 iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -0700919 return 0;
920}
921
922/*
923 * caller must hold session s_mutex
924 */
925static void remove_session_caps(struct ceph_mds_session *session)
926{
927 dout("remove_session_caps on %p\n", session);
928 iterate_session_caps(session, remove_session_caps_cb, NULL);
929 BUG_ON(session->s_nr_caps > 0);
Sage Weil6c99f252010-05-10 16:12:25 -0700930 BUG_ON(!list_empty(&session->s_cap_flushing));
Sage Weil2f2dc052009-10-06 11:31:09 -0700931 cleanup_cap_releases(session);
932}
933
934/*
935 * wake up any threads waiting on this session's caps. if the cap is
936 * old (didn't get renewed on the client reconnect), remove it now.
937 *
938 * caller must hold s_mutex.
939 */
940static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
941 void *arg)
942{
Sage Weil0dc25702009-11-20 13:43:45 -0800943 struct ceph_inode_info *ci = ceph_inode(inode);
944
Yehuda Sadeh03066f22010-07-27 13:11:08 -0700945 wake_up_all(&ci->i_cap_wq);
Sage Weil0dc25702009-11-20 13:43:45 -0800946 if (arg) {
947 spin_lock(&inode->i_lock);
948 ci->i_wanted_max_size = 0;
949 ci->i_requested_max_size = 0;
950 spin_unlock(&inode->i_lock);
951 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700952 return 0;
953}
954
Sage Weil0dc25702009-11-20 13:43:45 -0800955static void wake_up_session_caps(struct ceph_mds_session *session,
956 int reconnect)
Sage Weil2f2dc052009-10-06 11:31:09 -0700957{
958 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
Sage Weil0dc25702009-11-20 13:43:45 -0800959 iterate_session_caps(session, wake_up_session_cb,
960 (void *)(unsigned long)reconnect);
Sage Weil2f2dc052009-10-06 11:31:09 -0700961}
962
963/*
964 * Send periodic message to MDS renewing all currently held caps. The
965 * ack will reset the expiration for all caps from this session.
966 *
967 * caller holds s_mutex
968 */
969static int send_renew_caps(struct ceph_mds_client *mdsc,
970 struct ceph_mds_session *session)
971{
972 struct ceph_msg *msg;
973 int state;
974
975 if (time_after_eq(jiffies, session->s_cap_ttl) &&
976 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
977 pr_info("mds%d caps stale\n", session->s_mds);
Sage Weile4cb4cb2010-03-18 13:43:09 -0700978 session->s_renew_requested = jiffies;
Sage Weil2f2dc052009-10-06 11:31:09 -0700979
980 /* do not try to renew caps until a recovering mds has reconnected
981 * with its clients. */
982 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
983 if (state < CEPH_MDS_STATE_RECONNECT) {
984 dout("send_renew_caps ignoring mds%d (%s)\n",
985 session->s_mds, ceph_mds_state_name(state));
986 return 0;
987 }
988
989 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
990 ceph_mds_state_name(state));
Sage Weil2f2dc052009-10-06 11:31:09 -0700991 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
992 ++session->s_renew_seq);
Sage Weila79832f2010-04-01 16:06:19 -0700993 if (!msg)
994 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -0700995 ceph_con_send(&session->s_con, msg);
996 return 0;
997}
998
999/*
1000 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
Sage Weil0dc25702009-11-20 13:43:45 -08001001 *
1002 * Called under session->s_mutex
Sage Weil2f2dc052009-10-06 11:31:09 -07001003 */
1004static void renewed_caps(struct ceph_mds_client *mdsc,
1005 struct ceph_mds_session *session, int is_renew)
1006{
1007 int was_stale;
1008 int wake = 0;
1009
1010 spin_lock(&session->s_cap_lock);
1011 was_stale = is_renew && (session->s_cap_ttl == 0 ||
1012 time_after_eq(jiffies, session->s_cap_ttl));
1013
1014 session->s_cap_ttl = session->s_renew_requested +
1015 mdsc->mdsmap->m_session_timeout*HZ;
1016
1017 if (was_stale) {
1018 if (time_before(jiffies, session->s_cap_ttl)) {
1019 pr_info("mds%d caps renewed\n", session->s_mds);
1020 wake = 1;
1021 } else {
1022 pr_info("mds%d caps still stale\n", session->s_mds);
1023 }
1024 }
1025 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1026 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1027 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1028 spin_unlock(&session->s_cap_lock);
1029
1030 if (wake)
Sage Weil0dc25702009-11-20 13:43:45 -08001031 wake_up_session_caps(session, 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07001032}
1033
1034/*
1035 * send a session close request
1036 */
1037static int request_close_session(struct ceph_mds_client *mdsc,
1038 struct ceph_mds_session *session)
1039{
1040 struct ceph_msg *msg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001041
1042 dout("request_close_session mds%d state %s seq %lld\n",
1043 session->s_mds, session_state_name(session->s_state),
1044 session->s_seq);
1045 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001046 if (!msg)
1047 return -ENOMEM;
1048 ceph_con_send(&session->s_con, msg);
1049 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001050}
1051
1052/*
1053 * Called with s_mutex held.
1054 */
1055static int __close_session(struct ceph_mds_client *mdsc,
1056 struct ceph_mds_session *session)
1057{
1058 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1059 return 0;
1060 session->s_state = CEPH_MDS_SESSION_CLOSING;
1061 return request_close_session(mdsc, session);
1062}
1063
1064/*
1065 * Trim old(er) caps.
1066 *
1067 * Because we can't cache an inode without one or more caps, we do
1068 * this indirectly: if a cap is unused, we prune its aliases, at which
1069 * point the inode will hopefully get dropped to.
1070 *
1071 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1072 * memory pressure from the MDS, though, so it needn't be perfect.
1073 */
1074static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1075{
1076 struct ceph_mds_session *session = arg;
1077 struct ceph_inode_info *ci = ceph_inode(inode);
1078 int used, oissued, mine;
1079
1080 if (session->s_trim_caps <= 0)
1081 return -1;
1082
1083 spin_lock(&inode->i_lock);
1084 mine = cap->issued | cap->implemented;
1085 used = __ceph_caps_used(ci);
1086 oissued = __ceph_caps_issued_other(ci, cap);
1087
1088 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n",
1089 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
1090 ceph_cap_string(used));
1091 if (ci->i_dirty_caps)
1092 goto out; /* dirty caps */
1093 if ((used & ~oissued) & mine)
1094 goto out; /* we need these caps */
1095
1096 session->s_trim_caps--;
1097 if (oissued) {
1098 /* we aren't the only cap.. just remove us */
Sage Weil7c1332b2010-02-16 11:39:45 -08001099 __ceph_remove_cap(cap);
Sage Weil2f2dc052009-10-06 11:31:09 -07001100 } else {
1101 /* try to drop referring dentries */
1102 spin_unlock(&inode->i_lock);
1103 d_prune_aliases(inode);
1104 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1105 inode, cap, atomic_read(&inode->i_count));
1106 return 0;
1107 }
1108
1109out:
1110 spin_unlock(&inode->i_lock);
1111 return 0;
1112}
1113
1114/*
1115 * Trim session cap count down to some max number.
1116 */
1117static int trim_caps(struct ceph_mds_client *mdsc,
1118 struct ceph_mds_session *session,
1119 int max_caps)
1120{
1121 int trim_caps = session->s_nr_caps - max_caps;
1122
1123 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1124 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1125 if (trim_caps > 0) {
1126 session->s_trim_caps = trim_caps;
1127 iterate_session_caps(session, trim_caps_cb, session);
1128 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1129 session->s_mds, session->s_nr_caps, max_caps,
1130 trim_caps - session->s_trim_caps);
Sage Weil5dacf092009-12-21 20:40:34 -08001131 session->s_trim_caps = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001132 }
1133 return 0;
1134}
1135
1136/*
1137 * Allocate cap_release messages. If there is a partially full message
1138 * in the queue, try to allocate enough to cover it's remainder, so that
1139 * we can send it immediately.
1140 *
1141 * Called under s_mutex.
1142 */
Sage Weil2b2300d2010-06-09 16:52:04 -07001143int ceph_add_cap_releases(struct ceph_mds_client *mdsc,
Sage Weilee6b2722010-06-10 12:55:52 -07001144 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001145{
Sage Weil38e8883e2010-06-10 12:57:11 -07001146 struct ceph_msg *msg, *partial = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001147 struct ceph_mds_cap_release *head;
1148 int err = -ENOMEM;
Sage Weilee6b2722010-06-10 12:55:52 -07001149 int extra = mdsc->client->mount_args->cap_release_safety;
Sage Weil38e8883e2010-06-10 12:57:11 -07001150 int num;
Sage Weil2f2dc052009-10-06 11:31:09 -07001151
Sage Weil38e8883e2010-06-10 12:57:11 -07001152 dout("add_cap_releases %p mds%d extra %d\n", session, session->s_mds,
1153 extra);
Sage Weil2f2dc052009-10-06 11:31:09 -07001154
1155 spin_lock(&session->s_cap_lock);
1156
1157 if (!list_empty(&session->s_cap_releases)) {
1158 msg = list_first_entry(&session->s_cap_releases,
1159 struct ceph_msg,
1160 list_head);
1161 head = msg->front.iov_base;
Sage Weil38e8883e2010-06-10 12:57:11 -07001162 num = le32_to_cpu(head->num);
1163 if (num) {
1164 dout(" partial %p with (%d/%d)\n", msg, num,
1165 (int)CEPH_CAPS_PER_RELEASE);
1166 extra += CEPH_CAPS_PER_RELEASE - num;
1167 partial = msg;
1168 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001169 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001170 while (session->s_num_cap_releases < session->s_nr_caps + extra) {
1171 spin_unlock(&session->s_cap_lock);
Yehuda Sadeh34d23762010-04-06 14:33:58 -07001172 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE,
1173 GFP_NOFS);
Sage Weil2f2dc052009-10-06 11:31:09 -07001174 if (!msg)
1175 goto out_unlocked;
1176 dout("add_cap_releases %p msg %p now %d\n", session, msg,
1177 (int)msg->front.iov_len);
1178 head = msg->front.iov_base;
1179 head->num = cpu_to_le32(0);
1180 msg->front.iov_len = sizeof(*head);
1181 spin_lock(&session->s_cap_lock);
1182 list_add(&msg->list_head, &session->s_cap_releases);
1183 session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
1184 }
1185
Sage Weil38e8883e2010-06-10 12:57:11 -07001186 if (partial) {
1187 head = partial->front.iov_base;
1188 num = le32_to_cpu(head->num);
1189 dout(" queueing partial %p with %d/%d\n", partial, num,
1190 (int)CEPH_CAPS_PER_RELEASE);
1191 list_move_tail(&partial->list_head,
1192 &session->s_cap_releases_done);
1193 session->s_num_cap_releases -= CEPH_CAPS_PER_RELEASE - num;
Sage Weil2f2dc052009-10-06 11:31:09 -07001194 }
1195 err = 0;
1196 spin_unlock(&session->s_cap_lock);
1197out_unlocked:
1198 return err;
1199}
1200
1201/*
1202 * flush all dirty inode data to disk.
1203 *
1204 * returns true if we've flushed through want_flush_seq
1205 */
1206static int check_cap_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
1207{
1208 int mds, ret = 1;
1209
1210 dout("check_cap_flush want %lld\n", want_flush_seq);
1211 mutex_lock(&mdsc->mutex);
1212 for (mds = 0; ret && mds < mdsc->max_sessions; mds++) {
1213 struct ceph_mds_session *session = mdsc->sessions[mds];
1214
1215 if (!session)
1216 continue;
1217 get_session(session);
1218 mutex_unlock(&mdsc->mutex);
1219
1220 mutex_lock(&session->s_mutex);
1221 if (!list_empty(&session->s_cap_flushing)) {
1222 struct ceph_inode_info *ci =
1223 list_entry(session->s_cap_flushing.next,
1224 struct ceph_inode_info,
1225 i_flushing_item);
1226 struct inode *inode = &ci->vfs_inode;
1227
1228 spin_lock(&inode->i_lock);
1229 if (ci->i_cap_flush_seq <= want_flush_seq) {
1230 dout("check_cap_flush still flushing %p "
1231 "seq %lld <= %lld to mds%d\n", inode,
1232 ci->i_cap_flush_seq, want_flush_seq,
1233 session->s_mds);
1234 ret = 0;
1235 }
1236 spin_unlock(&inode->i_lock);
1237 }
1238 mutex_unlock(&session->s_mutex);
1239 ceph_put_mds_session(session);
1240
1241 if (!ret)
1242 return ret;
1243 mutex_lock(&mdsc->mutex);
1244 }
1245
1246 mutex_unlock(&mdsc->mutex);
1247 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
1248 return ret;
1249}
1250
1251/*
1252 * called under s_mutex
1253 */
Sage Weil3d7ded42010-06-09 16:47:10 -07001254void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1255 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001256{
1257 struct ceph_msg *msg;
1258
1259 dout("send_cap_releases mds%d\n", session->s_mds);
Sage Weil0f8605f2010-05-05 15:51:35 -07001260 spin_lock(&session->s_cap_lock);
1261 while (!list_empty(&session->s_cap_releases_done)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001262 msg = list_first_entry(&session->s_cap_releases_done,
1263 struct ceph_msg, list_head);
1264 list_del_init(&msg->list_head);
1265 spin_unlock(&session->s_cap_lock);
1266 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1267 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1268 ceph_con_send(&session->s_con, msg);
Sage Weil0f8605f2010-05-05 15:51:35 -07001269 spin_lock(&session->s_cap_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001270 }
1271 spin_unlock(&session->s_cap_lock);
1272}
1273
Sage Weile01a5942010-05-10 15:36:44 -07001274static void discard_cap_releases(struct ceph_mds_client *mdsc,
1275 struct ceph_mds_session *session)
1276{
1277 struct ceph_msg *msg;
1278 struct ceph_mds_cap_release *head;
1279 unsigned num;
1280
1281 dout("discard_cap_releases mds%d\n", session->s_mds);
1282 spin_lock(&session->s_cap_lock);
1283
1284 /* zero out the in-progress message */
1285 msg = list_first_entry(&session->s_cap_releases,
1286 struct ceph_msg, list_head);
1287 head = msg->front.iov_base;
1288 num = le32_to_cpu(head->num);
1289 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg, num);
1290 head->num = cpu_to_le32(0);
1291 session->s_num_cap_releases += num;
1292
1293 /* requeue completed messages */
1294 while (!list_empty(&session->s_cap_releases_done)) {
1295 msg = list_first_entry(&session->s_cap_releases_done,
1296 struct ceph_msg, list_head);
1297 list_del_init(&msg->list_head);
1298
1299 head = msg->front.iov_base;
1300 num = le32_to_cpu(head->num);
1301 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg,
1302 num);
1303 session->s_num_cap_releases += num;
1304 head->num = cpu_to_le32(0);
1305 msg->front.iov_len = sizeof(*head);
1306 list_add(&msg->list_head, &session->s_cap_releases);
1307 }
1308
1309 spin_unlock(&session->s_cap_lock);
1310}
1311
Sage Weil2f2dc052009-10-06 11:31:09 -07001312/*
1313 * requests
1314 */
1315
1316/*
1317 * Create an mds request.
1318 */
1319struct ceph_mds_request *
1320ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1321{
1322 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1323
1324 if (!req)
1325 return ERR_PTR(-ENOMEM);
1326
Sage Weilb4556392010-05-13 12:01:13 -07001327 mutex_init(&req->r_fill_mutex);
Yehuda Sadeh37151662010-06-17 16:16:12 -07001328 req->r_mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07001329 req->r_started = jiffies;
1330 req->r_resend_mds = -1;
1331 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1332 req->r_fmode = -1;
Sage Weil153c8e62009-12-07 12:31:09 -08001333 kref_init(&req->r_kref);
Sage Weil2f2dc052009-10-06 11:31:09 -07001334 INIT_LIST_HEAD(&req->r_wait);
1335 init_completion(&req->r_completion);
1336 init_completion(&req->r_safe_completion);
1337 INIT_LIST_HEAD(&req->r_unsafe_item);
1338
1339 req->r_op = op;
1340 req->r_direct_mode = mode;
1341 return req;
1342}
1343
1344/*
Sage Weil44ca18f2010-02-15 12:08:46 -08001345 * return oldest (lowest) request, tid in request tree, 0 if none.
Sage Weil2f2dc052009-10-06 11:31:09 -07001346 *
1347 * called under mdsc->mutex.
1348 */
Sage Weil44ca18f2010-02-15 12:08:46 -08001349static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1350{
1351 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1352 return NULL;
1353 return rb_entry(rb_first(&mdsc->request_tree),
1354 struct ceph_mds_request, r_node);
1355}
1356
Sage Weil2f2dc052009-10-06 11:31:09 -07001357static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1358{
Sage Weil44ca18f2010-02-15 12:08:46 -08001359 struct ceph_mds_request *req = __get_oldest_req(mdsc);
1360
1361 if (req)
1362 return req->r_tid;
1363 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001364}
1365
1366/*
1367 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1368 * on build_path_from_dentry in fs/cifs/dir.c.
1369 *
1370 * If @stop_on_nosnap, generate path relative to the first non-snapped
1371 * inode.
1372 *
1373 * Encode hidden .snap dirs as a double /, i.e.
1374 * foo/.snap/bar -> foo//bar
1375 */
1376char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1377 int stop_on_nosnap)
1378{
1379 struct dentry *temp;
1380 char *path;
1381 int len, pos;
1382
1383 if (dentry == NULL)
1384 return ERR_PTR(-EINVAL);
1385
1386retry:
1387 len = 0;
1388 for (temp = dentry; !IS_ROOT(temp);) {
1389 struct inode *inode = temp->d_inode;
1390 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1391 len++; /* slash only */
1392 else if (stop_on_nosnap && inode &&
1393 ceph_snap(inode) == CEPH_NOSNAP)
1394 break;
1395 else
1396 len += 1 + temp->d_name.len;
1397 temp = temp->d_parent;
1398 if (temp == NULL) {
Sage Weil6c99f252010-05-10 16:12:25 -07001399 pr_err("build_path corrupt dentry %p\n", dentry);
Sage Weil2f2dc052009-10-06 11:31:09 -07001400 return ERR_PTR(-EINVAL);
1401 }
1402 }
1403 if (len)
1404 len--; /* no leading '/' */
1405
1406 path = kmalloc(len+1, GFP_NOFS);
1407 if (path == NULL)
1408 return ERR_PTR(-ENOMEM);
1409 pos = len;
1410 path[pos] = 0; /* trailing null */
1411 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
1412 struct inode *inode = temp->d_inode;
1413
1414 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
Sage Weil104648a2010-03-18 10:14:30 -07001415 dout("build_path path+%d: %p SNAPDIR\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001416 pos, temp);
1417 } else if (stop_on_nosnap && inode &&
1418 ceph_snap(inode) == CEPH_NOSNAP) {
1419 break;
1420 } else {
1421 pos -= temp->d_name.len;
1422 if (pos < 0)
1423 break;
1424 strncpy(path + pos, temp->d_name.name,
1425 temp->d_name.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07001426 }
1427 if (pos)
1428 path[--pos] = '/';
1429 temp = temp->d_parent;
1430 if (temp == NULL) {
Sage Weil104648a2010-03-18 10:14:30 -07001431 pr_err("build_path corrupt dentry\n");
Sage Weil2f2dc052009-10-06 11:31:09 -07001432 kfree(path);
1433 return ERR_PTR(-EINVAL);
1434 }
1435 }
1436 if (pos != 0) {
Sage Weil104648a2010-03-18 10:14:30 -07001437 pr_err("build_path did not end path lookup where "
Sage Weil2f2dc052009-10-06 11:31:09 -07001438 "expected, namelen is %d, pos is %d\n", len, pos);
1439 /* presumably this is only possible if racing with a
1440 rename of one of the parent directories (we can not
1441 lock the dentries above us to prevent this, but
1442 retrying should be harmless) */
1443 kfree(path);
1444 goto retry;
1445 }
1446
1447 *base = ceph_ino(temp->d_inode);
1448 *plen = len;
Sage Weil104648a2010-03-18 10:14:30 -07001449 dout("build_path on %p %d built %llx '%.*s'\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001450 dentry, atomic_read(&dentry->d_count), *base, len, path);
1451 return path;
1452}
1453
1454static int build_dentry_path(struct dentry *dentry,
1455 const char **ppath, int *ppathlen, u64 *pino,
1456 int *pfreepath)
1457{
1458 char *path;
1459
1460 if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) {
1461 *pino = ceph_ino(dentry->d_parent->d_inode);
1462 *ppath = dentry->d_name.name;
1463 *ppathlen = dentry->d_name.len;
1464 return 0;
1465 }
1466 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1467 if (IS_ERR(path))
1468 return PTR_ERR(path);
1469 *ppath = path;
1470 *pfreepath = 1;
1471 return 0;
1472}
1473
1474static int build_inode_path(struct inode *inode,
1475 const char **ppath, int *ppathlen, u64 *pino,
1476 int *pfreepath)
1477{
1478 struct dentry *dentry;
1479 char *path;
1480
1481 if (ceph_snap(inode) == CEPH_NOSNAP) {
1482 *pino = ceph_ino(inode);
1483 *ppathlen = 0;
1484 return 0;
1485 }
1486 dentry = d_find_alias(inode);
1487 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1488 dput(dentry);
1489 if (IS_ERR(path))
1490 return PTR_ERR(path);
1491 *ppath = path;
1492 *pfreepath = 1;
1493 return 0;
1494}
1495
1496/*
1497 * request arguments may be specified via an inode *, a dentry *, or
1498 * an explicit ino+path.
1499 */
1500static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1501 const char *rpath, u64 rino,
1502 const char **ppath, int *pathlen,
1503 u64 *ino, int *freepath)
1504{
1505 int r = 0;
1506
1507 if (rinode) {
1508 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1509 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1510 ceph_snap(rinode));
1511 } else if (rdentry) {
1512 r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
1513 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1514 *ppath);
1515 } else if (rpath) {
1516 *ino = rino;
1517 *ppath = rpath;
1518 *pathlen = strlen(rpath);
1519 dout(" path %.*s\n", *pathlen, rpath);
1520 }
1521
1522 return r;
1523}
1524
1525/*
1526 * called under mdsc->mutex
1527 */
1528static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1529 struct ceph_mds_request *req,
1530 int mds)
1531{
1532 struct ceph_msg *msg;
1533 struct ceph_mds_request_head *head;
1534 const char *path1 = NULL;
1535 const char *path2 = NULL;
1536 u64 ino1 = 0, ino2 = 0;
1537 int pathlen1 = 0, pathlen2 = 0;
1538 int freepath1 = 0, freepath2 = 0;
1539 int len;
1540 u16 releases;
1541 void *p, *end;
1542 int ret;
1543
1544 ret = set_request_path_attr(req->r_inode, req->r_dentry,
1545 req->r_path1, req->r_ino1.ino,
1546 &path1, &pathlen1, &ino1, &freepath1);
1547 if (ret < 0) {
1548 msg = ERR_PTR(ret);
1549 goto out;
1550 }
1551
1552 ret = set_request_path_attr(NULL, req->r_old_dentry,
1553 req->r_path2, req->r_ino2.ino,
1554 &path2, &pathlen2, &ino2, &freepath2);
1555 if (ret < 0) {
1556 msg = ERR_PTR(ret);
1557 goto out_free1;
1558 }
1559
1560 len = sizeof(*head) +
Sage Weilac8839d2010-01-27 14:28:10 -08001561 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64));
Sage Weil2f2dc052009-10-06 11:31:09 -07001562
1563 /* calculate (max) length for cap releases */
1564 len += sizeof(struct ceph_mds_request_release) *
1565 (!!req->r_inode_drop + !!req->r_dentry_drop +
1566 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1567 if (req->r_dentry_drop)
1568 len += req->r_dentry->d_name.len;
1569 if (req->r_old_dentry_drop)
1570 len += req->r_old_dentry->d_name.len;
1571
Yehuda Sadeh34d23762010-04-06 14:33:58 -07001572 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -07001573 if (!msg) {
1574 msg = ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -07001575 goto out_free2;
Sage Weila79832f2010-04-01 16:06:19 -07001576 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001577
Sage Weil6df058c2009-12-22 11:24:33 -08001578 msg->hdr.tid = cpu_to_le64(req->r_tid);
1579
Sage Weil2f2dc052009-10-06 11:31:09 -07001580 head = msg->front.iov_base;
1581 p = msg->front.iov_base + sizeof(*head);
1582 end = msg->front.iov_base + msg->front.iov_len;
1583
1584 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1585 head->op = cpu_to_le32(req->r_op);
1586 head->caller_uid = cpu_to_le32(current_fsuid());
1587 head->caller_gid = cpu_to_le32(current_fsgid());
1588 head->args = req->r_args;
1589
1590 ceph_encode_filepath(&p, end, ino1, path1);
1591 ceph_encode_filepath(&p, end, ino2, path2);
1592
Sage Weile979cf52010-07-15 14:58:39 -07001593 /* make note of release offset, in case we need to replay */
1594 req->r_request_release_offset = p - msg->front.iov_base;
1595
Sage Weil2f2dc052009-10-06 11:31:09 -07001596 /* cap releases */
1597 releases = 0;
1598 if (req->r_inode_drop)
1599 releases += ceph_encode_inode_release(&p,
1600 req->r_inode ? req->r_inode : req->r_dentry->d_inode,
1601 mds, req->r_inode_drop, req->r_inode_unless, 0);
1602 if (req->r_dentry_drop)
1603 releases += ceph_encode_dentry_release(&p, req->r_dentry,
1604 mds, req->r_dentry_drop, req->r_dentry_unless);
1605 if (req->r_old_dentry_drop)
1606 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
1607 mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
1608 if (req->r_old_inode_drop)
1609 releases += ceph_encode_inode_release(&p,
1610 req->r_old_dentry->d_inode,
1611 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
1612 head->num_releases = cpu_to_le16(releases);
1613
1614 BUG_ON(p > end);
1615 msg->front.iov_len = p - msg->front.iov_base;
1616 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1617
1618 msg->pages = req->r_pages;
1619 msg->nr_pages = req->r_num_pages;
1620 msg->hdr.data_len = cpu_to_le32(req->r_data_len);
1621 msg->hdr.data_off = cpu_to_le16(0);
1622
1623out_free2:
1624 if (freepath2)
1625 kfree((char *)path2);
1626out_free1:
1627 if (freepath1)
1628 kfree((char *)path1);
1629out:
1630 return msg;
1631}
1632
1633/*
1634 * called under mdsc->mutex if error, under no mutex if
1635 * success.
1636 */
1637static void complete_request(struct ceph_mds_client *mdsc,
1638 struct ceph_mds_request *req)
1639{
1640 if (req->r_callback)
1641 req->r_callback(mdsc, req);
1642 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001643 complete_all(&req->r_completion);
Sage Weil2f2dc052009-10-06 11:31:09 -07001644}
1645
1646/*
1647 * called under mdsc->mutex
1648 */
1649static int __prepare_send_request(struct ceph_mds_client *mdsc,
1650 struct ceph_mds_request *req,
1651 int mds)
1652{
1653 struct ceph_mds_request_head *rhead;
1654 struct ceph_msg *msg;
1655 int flags = 0;
1656
1657 req->r_mds = mds;
1658 req->r_attempts++;
Greg Farnume55b71f2010-06-22 15:58:01 -07001659 if (req->r_inode) {
1660 struct ceph_cap *cap =
1661 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
1662
1663 if (cap)
1664 req->r_sent_on_mseq = cap->mseq;
1665 else
1666 req->r_sent_on_mseq = -1;
1667 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001668 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
1669 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
1670
Sage Weil01a92f12010-07-15 13:24:32 -07001671 if (req->r_got_unsafe) {
1672 /*
1673 * Replay. Do not regenerate message (and rebuild
1674 * paths, etc.); just use the original message.
1675 * Rebuilding paths will break for renames because
1676 * d_move mangles the src name.
1677 */
1678 msg = req->r_request;
1679 rhead = msg->front.iov_base;
1680
1681 flags = le32_to_cpu(rhead->flags);
1682 flags |= CEPH_MDS_FLAG_REPLAY;
1683 rhead->flags = cpu_to_le32(flags);
1684
1685 if (req->r_target_inode)
1686 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
1687
1688 rhead->num_retry = req->r_attempts - 1;
Sage Weile979cf52010-07-15 14:58:39 -07001689
1690 /* remove cap/dentry releases from message */
1691 rhead->num_releases = 0;
1692 msg->hdr.front_len = cpu_to_le32(req->r_request_release_offset);
1693 msg->front.iov_len = req->r_request_release_offset;
Sage Weil01a92f12010-07-15 13:24:32 -07001694 return 0;
1695 }
1696
Sage Weil2f2dc052009-10-06 11:31:09 -07001697 if (req->r_request) {
1698 ceph_msg_put(req->r_request);
1699 req->r_request = NULL;
1700 }
1701 msg = create_request_message(mdsc, req, mds);
1702 if (IS_ERR(msg)) {
Sage Weile1518c72010-05-13 11:19:06 -07001703 req->r_err = PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001704 complete_request(mdsc, req);
Sage Weila79832f2010-04-01 16:06:19 -07001705 return PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001706 }
1707 req->r_request = msg;
1708
1709 rhead = msg->front.iov_base;
Sage Weil2f2dc052009-10-06 11:31:09 -07001710 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
1711 if (req->r_got_unsafe)
1712 flags |= CEPH_MDS_FLAG_REPLAY;
1713 if (req->r_locked_dir)
1714 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
1715 rhead->flags = cpu_to_le32(flags);
1716 rhead->num_fwd = req->r_num_fwd;
1717 rhead->num_retry = req->r_attempts - 1;
Sage Weil01a92f12010-07-15 13:24:32 -07001718 rhead->ino = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001719
1720 dout(" r_locked_dir = %p\n", req->r_locked_dir);
Sage Weil2f2dc052009-10-06 11:31:09 -07001721 return 0;
1722}
1723
1724/*
1725 * send request, or put it on the appropriate wait list.
1726 */
1727static int __do_request(struct ceph_mds_client *mdsc,
1728 struct ceph_mds_request *req)
1729{
1730 struct ceph_mds_session *session = NULL;
1731 int mds = -1;
1732 int err = -EAGAIN;
1733
Sage Weile1518c72010-05-13 11:19:06 -07001734 if (req->r_err || req->r_got_result)
Sage Weil2f2dc052009-10-06 11:31:09 -07001735 goto out;
1736
1737 if (req->r_timeout &&
1738 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
1739 dout("do_request timed out\n");
1740 err = -EIO;
1741 goto finish;
1742 }
1743
1744 mds = __choose_mds(mdsc, req);
1745 if (mds < 0 ||
1746 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
1747 dout("do_request no mds or not active, waiting for map\n");
1748 list_add(&req->r_wait, &mdsc->waiting_for_map);
1749 goto out;
1750 }
1751
1752 /* get, open session */
1753 session = __ceph_lookup_mds_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07001754 if (!session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001755 session = register_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07001756 if (IS_ERR(session)) {
1757 err = PTR_ERR(session);
1758 goto finish;
1759 }
1760 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001761 dout("do_request mds%d session %p state %s\n", mds, session,
1762 session_state_name(session->s_state));
1763 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
1764 session->s_state != CEPH_MDS_SESSION_HUNG) {
1765 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1766 session->s_state == CEPH_MDS_SESSION_CLOSING)
1767 __open_session(mdsc, session);
1768 list_add(&req->r_wait, &session->s_waiting);
1769 goto out_session;
1770 }
1771
1772 /* send request */
1773 req->r_session = get_session(session);
1774 req->r_resend_mds = -1; /* forget any previous mds hint */
1775
1776 if (req->r_request_started == 0) /* note request start time */
1777 req->r_request_started = jiffies;
1778
1779 err = __prepare_send_request(mdsc, req, mds);
1780 if (!err) {
1781 ceph_msg_get(req->r_request);
1782 ceph_con_send(&session->s_con, req->r_request);
1783 }
1784
1785out_session:
1786 ceph_put_mds_session(session);
1787out:
1788 return err;
1789
1790finish:
Sage Weile1518c72010-05-13 11:19:06 -07001791 req->r_err = err;
Sage Weil2f2dc052009-10-06 11:31:09 -07001792 complete_request(mdsc, req);
1793 goto out;
1794}
1795
1796/*
1797 * called under mdsc->mutex
1798 */
1799static void __wake_requests(struct ceph_mds_client *mdsc,
1800 struct list_head *head)
1801{
1802 struct ceph_mds_request *req, *nreq;
1803
1804 list_for_each_entry_safe(req, nreq, head, r_wait) {
1805 list_del_init(&req->r_wait);
1806 __do_request(mdsc, req);
1807 }
1808}
1809
1810/*
1811 * Wake up threads with requests pending for @mds, so that they can
Sage Weil29790f22010-03-18 14:45:05 -07001812 * resubmit their requests to a possibly different mds.
Sage Weil2f2dc052009-10-06 11:31:09 -07001813 */
Sage Weil29790f22010-03-18 14:45:05 -07001814static void kick_requests(struct ceph_mds_client *mdsc, int mds)
Sage Weil2f2dc052009-10-06 11:31:09 -07001815{
Sage Weil44ca18f2010-02-15 12:08:46 -08001816 struct ceph_mds_request *req;
1817 struct rb_node *p;
Sage Weil2f2dc052009-10-06 11:31:09 -07001818
1819 dout("kick_requests mds%d\n", mds);
Sage Weil44ca18f2010-02-15 12:08:46 -08001820 for (p = rb_first(&mdsc->request_tree); p; p = rb_next(p)) {
1821 req = rb_entry(p, struct ceph_mds_request, r_node);
1822 if (req->r_got_unsafe)
1823 continue;
1824 if (req->r_session &&
1825 req->r_session->s_mds == mds) {
1826 dout(" kicking tid %llu\n", req->r_tid);
1827 put_request_session(req);
1828 __do_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07001829 }
1830 }
1831}
1832
1833void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
1834 struct ceph_mds_request *req)
1835{
1836 dout("submit_request on %p\n", req);
1837 mutex_lock(&mdsc->mutex);
1838 __register_request(mdsc, req, NULL);
1839 __do_request(mdsc, req);
1840 mutex_unlock(&mdsc->mutex);
1841}
1842
1843/*
1844 * Synchrously perform an mds request. Take care of all of the
1845 * session setup, forwarding, retry details.
1846 */
1847int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
1848 struct inode *dir,
1849 struct ceph_mds_request *req)
1850{
1851 int err;
1852
1853 dout("do_request on %p\n", req);
1854
1855 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
1856 if (req->r_inode)
1857 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
1858 if (req->r_locked_dir)
1859 ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
1860 if (req->r_old_dentry)
1861 ceph_get_cap_refs(
1862 ceph_inode(req->r_old_dentry->d_parent->d_inode),
1863 CEPH_CAP_PIN);
1864
1865 /* issue */
1866 mutex_lock(&mdsc->mutex);
1867 __register_request(mdsc, req, dir);
1868 __do_request(mdsc, req);
1869
Sage Weile1518c72010-05-13 11:19:06 -07001870 if (req->r_err) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001871 err = req->r_err;
Sage Weile1518c72010-05-13 11:19:06 -07001872 __unregister_request(mdsc, req);
1873 dout("do_request early error %d\n", err);
1874 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07001875 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001876
Sage Weile1518c72010-05-13 11:19:06 -07001877 /* wait */
1878 mutex_unlock(&mdsc->mutex);
1879 dout("do_request waiting\n");
1880 if (req->r_timeout) {
Sage Weilaa916472010-05-24 11:15:51 -07001881 err = (long)wait_for_completion_killable_timeout(
Sage Weile1518c72010-05-13 11:19:06 -07001882 &req->r_completion, req->r_timeout);
1883 if (err == 0)
1884 err = -EIO;
1885 } else {
Sage Weilaa916472010-05-24 11:15:51 -07001886 err = wait_for_completion_killable(&req->r_completion);
Sage Weile1518c72010-05-13 11:19:06 -07001887 }
1888 dout("do_request waited, got %d\n", err);
1889 mutex_lock(&mdsc->mutex);
1890
1891 /* only abort if we didn't race with a real reply */
1892 if (req->r_got_result) {
1893 err = le32_to_cpu(req->r_reply_info.head->result);
1894 } else if (err < 0) {
1895 dout("aborted request %lld with %d\n", req->r_tid, err);
Sage Weilb4556392010-05-13 12:01:13 -07001896
1897 /*
1898 * ensure we aren't running concurrently with
1899 * ceph_fill_trace or ceph_readdir_prepopulate, which
1900 * rely on locks (dir mutex) held by our caller.
1901 */
1902 mutex_lock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07001903 req->r_err = err;
1904 req->r_aborted = true;
Sage Weilb4556392010-05-13 12:01:13 -07001905 mutex_unlock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07001906
1907 if (req->r_locked_dir &&
Sage Weil167c9e32010-05-14 10:02:57 -07001908 (req->r_op & CEPH_MDS_OP_WRITE))
1909 ceph_invalidate_dir_request(req);
Sage Weile1518c72010-05-13 11:19:06 -07001910 } else {
1911 err = req->r_err;
1912 }
1913
1914out:
1915 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07001916 dout("do_request %p done, result %d\n", req, err);
1917 return err;
1918}
1919
1920/*
Sage Weil167c9e32010-05-14 10:02:57 -07001921 * Invalidate dir I_COMPLETE, dentry lease state on an aborted MDS
1922 * namespace request.
1923 */
1924void ceph_invalidate_dir_request(struct ceph_mds_request *req)
1925{
1926 struct inode *inode = req->r_locked_dir;
1927 struct ceph_inode_info *ci = ceph_inode(inode);
1928
1929 dout("invalidate_dir_request %p (I_COMPLETE, lease(s))\n", inode);
1930 spin_lock(&inode->i_lock);
1931 ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
1932 ci->i_release_count++;
1933 spin_unlock(&inode->i_lock);
1934
1935 if (req->r_dentry)
1936 ceph_invalidate_dentry_lease(req->r_dentry);
1937 if (req->r_old_dentry)
1938 ceph_invalidate_dentry_lease(req->r_old_dentry);
1939}
1940
1941/*
Sage Weil2f2dc052009-10-06 11:31:09 -07001942 * Handle mds reply.
1943 *
1944 * We take the session mutex and parse and process the reply immediately.
1945 * This preserves the logical ordering of replies, capabilities, etc., sent
1946 * by the MDS as they are applied to our local cache.
1947 */
1948static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
1949{
1950 struct ceph_mds_client *mdsc = session->s_mdsc;
1951 struct ceph_mds_request *req;
1952 struct ceph_mds_reply_head *head = msg->front.iov_base;
1953 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
1954 u64 tid;
1955 int err, result;
Sage Weil2600d2d2010-02-22 15:12:16 -08001956 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07001957
Sage Weil2f2dc052009-10-06 11:31:09 -07001958 if (msg->front.iov_len < sizeof(*head)) {
1959 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08001960 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001961 return;
1962 }
1963
1964 /* get request, session */
Sage Weil6df058c2009-12-22 11:24:33 -08001965 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07001966 mutex_lock(&mdsc->mutex);
1967 req = __lookup_request(mdsc, tid);
1968 if (!req) {
1969 dout("handle_reply on unknown tid %llu\n", tid);
1970 mutex_unlock(&mdsc->mutex);
1971 return;
1972 }
1973 dout("handle_reply %p\n", req);
Sage Weil2f2dc052009-10-06 11:31:09 -07001974
1975 /* correct session? */
Sage Weild96d6042010-03-20 20:50:58 -07001976 if (req->r_session != session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001977 pr_err("mdsc_handle_reply got %llu on session mds%d"
1978 " not mds%d\n", tid, session->s_mds,
1979 req->r_session ? req->r_session->s_mds : -1);
1980 mutex_unlock(&mdsc->mutex);
1981 goto out;
1982 }
1983
1984 /* dup? */
1985 if ((req->r_got_unsafe && !head->safe) ||
1986 (req->r_got_safe && head->safe)) {
1987 pr_warning("got a dup %s reply on %llu from mds%d\n",
1988 head->safe ? "safe" : "unsafe", tid, mds);
1989 mutex_unlock(&mdsc->mutex);
1990 goto out;
1991 }
Sage Weil85792d0d2010-05-13 09:06:02 -07001992 if (req->r_got_safe && !head->safe) {
1993 pr_warning("got unsafe after safe on %llu from mds%d\n",
1994 tid, mds);
1995 mutex_unlock(&mdsc->mutex);
1996 goto out;
1997 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001998
1999 result = le32_to_cpu(head->result);
2000
2001 /*
Greg Farnume55b71f2010-06-22 15:58:01 -07002002 * Handle an ESTALE
2003 * if we're not talking to the authority, send to them
2004 * if the authority has changed while we weren't looking,
2005 * send to new authority
2006 * Otherwise we just have to return an ESTALE
Sage Weil2f2dc052009-10-06 11:31:09 -07002007 */
2008 if (result == -ESTALE) {
Greg Farnume55b71f2010-06-22 15:58:01 -07002009 dout("got ESTALE on request %llu", req->r_tid);
Sage Weil213c99e2010-08-03 10:25:11 -07002010 if (!req->r_inode) {
2011 /* do nothing; not an authority problem */
2012 } else if (req->r_direct_mode != USE_AUTH_MDS) {
Greg Farnume55b71f2010-06-22 15:58:01 -07002013 dout("not using auth, setting for that now");
2014 req->r_direct_mode = USE_AUTH_MDS;
Sage Weil2f2dc052009-10-06 11:31:09 -07002015 __do_request(mdsc, req);
2016 mutex_unlock(&mdsc->mutex);
2017 goto out;
Greg Farnume55b71f2010-06-22 15:58:01 -07002018 } else {
2019 struct ceph_inode_info *ci = ceph_inode(req->r_inode);
2020 struct ceph_cap *cap =
2021 ceph_get_cap_for_mds(ci, req->r_mds);;
2022
2023 dout("already using auth");
2024 if ((!cap || cap != ci->i_auth_cap) ||
2025 (cap->mseq != req->r_sent_on_mseq)) {
2026 dout("but cap changed, so resending");
2027 __do_request(mdsc, req);
2028 mutex_unlock(&mdsc->mutex);
2029 goto out;
2030 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002031 }
Greg Farnume55b71f2010-06-22 15:58:01 -07002032 dout("have to return ESTALE on request %llu", req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002033 }
2034
Greg Farnume55b71f2010-06-22 15:58:01 -07002035
Sage Weil2f2dc052009-10-06 11:31:09 -07002036 if (head->safe) {
2037 req->r_got_safe = true;
2038 __unregister_request(mdsc, req);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002039 complete_all(&req->r_safe_completion);
Sage Weil2f2dc052009-10-06 11:31:09 -07002040
2041 if (req->r_got_unsafe) {
2042 /*
2043 * We already handled the unsafe response, now do the
2044 * cleanup. No need to examine the response; the MDS
2045 * doesn't include any result info in the safe
2046 * response. And even if it did, there is nothing
2047 * useful we could do with a revised return value.
2048 */
2049 dout("got safe reply %llu, mds%d\n", tid, mds);
2050 list_del_init(&req->r_unsafe_item);
2051
2052 /* last unsafe request during umount? */
Sage Weil44ca18f2010-02-15 12:08:46 -08002053 if (mdsc->stopping && !__get_oldest_req(mdsc))
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002054 complete_all(&mdsc->safe_umount_waiters);
Sage Weil2f2dc052009-10-06 11:31:09 -07002055 mutex_unlock(&mdsc->mutex);
2056 goto out;
2057 }
Sage Weile1518c72010-05-13 11:19:06 -07002058 } else {
Sage Weil2f2dc052009-10-06 11:31:09 -07002059 req->r_got_unsafe = true;
2060 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
2061 }
2062
2063 dout("handle_reply tid %lld result %d\n", tid, result);
2064 rinfo = &req->r_reply_info;
2065 err = parse_reply_info(msg, rinfo);
2066 mutex_unlock(&mdsc->mutex);
2067
2068 mutex_lock(&session->s_mutex);
2069 if (err < 0) {
2070 pr_err("mdsc_handle_reply got corrupt reply mds%d\n", mds);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002071 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002072 goto out_err;
2073 }
2074
2075 /* snap trace */
2076 if (rinfo->snapblob_len) {
2077 down_write(&mdsc->snap_rwsem);
2078 ceph_update_snap_trace(mdsc, rinfo->snapblob,
2079 rinfo->snapblob + rinfo->snapblob_len,
2080 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP);
2081 downgrade_write(&mdsc->snap_rwsem);
2082 } else {
2083 down_read(&mdsc->snap_rwsem);
2084 }
2085
2086 /* insert trace into our cache */
Sage Weilb4556392010-05-13 12:01:13 -07002087 mutex_lock(&req->r_fill_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002088 err = ceph_fill_trace(mdsc->client->sb, req, req->r_session);
2089 if (err == 0) {
2090 if (result == 0 && rinfo->dir_nr)
2091 ceph_readdir_prepopulate(req, req->r_session);
Yehuda Sadeh37151662010-06-17 16:16:12 -07002092 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
Sage Weil2f2dc052009-10-06 11:31:09 -07002093 }
Sage Weilb4556392010-05-13 12:01:13 -07002094 mutex_unlock(&req->r_fill_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002095
2096 up_read(&mdsc->snap_rwsem);
2097out_err:
Sage Weile1518c72010-05-13 11:19:06 -07002098 mutex_lock(&mdsc->mutex);
2099 if (!req->r_aborted) {
2100 if (err) {
2101 req->r_err = err;
2102 } else {
2103 req->r_reply = msg;
2104 ceph_msg_get(msg);
2105 req->r_got_result = true;
2106 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002107 } else {
Sage Weile1518c72010-05-13 11:19:06 -07002108 dout("reply arrived after request %lld was aborted\n", tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002109 }
Sage Weile1518c72010-05-13 11:19:06 -07002110 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002111
Sage Weilee6b2722010-06-10 12:55:52 -07002112 ceph_add_cap_releases(mdsc, req->r_session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002113 mutex_unlock(&session->s_mutex);
2114
2115 /* kick calling process */
2116 complete_request(mdsc, req);
2117out:
2118 ceph_mdsc_put_request(req);
2119 return;
2120}
2121
2122
2123
2124/*
2125 * handle mds notification that our request has been forwarded.
2126 */
Sage Weil2600d2d2010-02-22 15:12:16 -08002127static void handle_forward(struct ceph_mds_client *mdsc,
2128 struct ceph_mds_session *session,
2129 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002130{
2131 struct ceph_mds_request *req;
Sage Weila1ea7872010-02-23 14:02:44 -08002132 u64 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002133 u32 next_mds;
2134 u32 fwd_seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002135 int err = -EINVAL;
2136 void *p = msg->front.iov_base;
2137 void *end = p + msg->front.iov_len;
Sage Weil2f2dc052009-10-06 11:31:09 -07002138
Sage Weila1ea7872010-02-23 14:02:44 -08002139 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002140 next_mds = ceph_decode_32(&p);
2141 fwd_seq = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07002142
2143 mutex_lock(&mdsc->mutex);
2144 req = __lookup_request(mdsc, tid);
2145 if (!req) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07002146 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002147 goto out; /* dup reply? */
2148 }
2149
Sage Weil2a8e5e32010-05-28 16:43:16 -07002150 if (req->r_aborted) {
2151 dout("forward tid %llu aborted, unregistering\n", tid);
2152 __unregister_request(mdsc, req);
2153 } else if (fwd_seq <= req->r_num_fwd) {
2154 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002155 tid, next_mds, req->r_num_fwd, fwd_seq);
2156 } else {
2157 /* resend. forward race not possible; mds would drop */
Sage Weil2a8e5e32010-05-28 16:43:16 -07002158 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2159 BUG_ON(req->r_err);
2160 BUG_ON(req->r_got_result);
Sage Weil2f2dc052009-10-06 11:31:09 -07002161 req->r_num_fwd = fwd_seq;
2162 req->r_resend_mds = next_mds;
2163 put_request_session(req);
2164 __do_request(mdsc, req);
2165 }
2166 ceph_mdsc_put_request(req);
2167out:
2168 mutex_unlock(&mdsc->mutex);
2169 return;
2170
2171bad:
2172 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2173}
2174
2175/*
2176 * handle a mds session control message
2177 */
2178static void handle_session(struct ceph_mds_session *session,
2179 struct ceph_msg *msg)
2180{
2181 struct ceph_mds_client *mdsc = session->s_mdsc;
2182 u32 op;
2183 u64 seq;
Sage Weil2600d2d2010-02-22 15:12:16 -08002184 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002185 struct ceph_mds_session_head *h = msg->front.iov_base;
2186 int wake = 0;
2187
Sage Weil2f2dc052009-10-06 11:31:09 -07002188 /* decode */
2189 if (msg->front.iov_len != sizeof(*h))
2190 goto bad;
2191 op = le32_to_cpu(h->op);
2192 seq = le64_to_cpu(h->seq);
2193
2194 mutex_lock(&mdsc->mutex);
Sage Weil2600d2d2010-02-22 15:12:16 -08002195 if (op == CEPH_SESSION_CLOSE)
2196 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002197 /* FIXME: this ttl calculation is generous */
2198 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2199 mutex_unlock(&mdsc->mutex);
2200
2201 mutex_lock(&session->s_mutex);
2202
2203 dout("handle_session mds%d %s %p state %s seq %llu\n",
2204 mds, ceph_session_op_name(op), session,
2205 session_state_name(session->s_state), seq);
2206
2207 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2208 session->s_state = CEPH_MDS_SESSION_OPEN;
2209 pr_info("mds%d came back\n", session->s_mds);
2210 }
2211
2212 switch (op) {
2213 case CEPH_SESSION_OPEN:
Sage Weil29790f22010-03-18 14:45:05 -07002214 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2215 pr_info("mds%d reconnect success\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002216 session->s_state = CEPH_MDS_SESSION_OPEN;
2217 renewed_caps(mdsc, session, 0);
2218 wake = 1;
2219 if (mdsc->stopping)
2220 __close_session(mdsc, session);
2221 break;
2222
2223 case CEPH_SESSION_RENEWCAPS:
2224 if (session->s_renew_seq == seq)
2225 renewed_caps(mdsc, session, 1);
2226 break;
2227
2228 case CEPH_SESSION_CLOSE:
Sage Weil29790f22010-03-18 14:45:05 -07002229 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2230 pr_info("mds%d reconnect denied\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002231 remove_session_caps(session);
2232 wake = 1; /* for good measure */
Sage Weilf3c60c52010-08-11 14:51:23 -07002233 wake_up_all(&mdsc->session_close_wq);
Sage Weil29790f22010-03-18 14:45:05 -07002234 kick_requests(mdsc, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002235 break;
2236
2237 case CEPH_SESSION_STALE:
2238 pr_info("mds%d caps went stale, renewing\n",
2239 session->s_mds);
2240 spin_lock(&session->s_cap_lock);
2241 session->s_cap_gen++;
2242 session->s_cap_ttl = 0;
2243 spin_unlock(&session->s_cap_lock);
2244 send_renew_caps(mdsc, session);
2245 break;
2246
2247 case CEPH_SESSION_RECALL_STATE:
2248 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2249 break;
2250
2251 default:
2252 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2253 WARN_ON(1);
2254 }
2255
2256 mutex_unlock(&session->s_mutex);
2257 if (wake) {
2258 mutex_lock(&mdsc->mutex);
2259 __wake_requests(mdsc, &session->s_waiting);
2260 mutex_unlock(&mdsc->mutex);
2261 }
2262 return;
2263
2264bad:
2265 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2266 (int)msg->front.iov_len);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002267 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002268 return;
2269}
2270
2271
2272/*
2273 * called under session->mutex.
2274 */
2275static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2276 struct ceph_mds_session *session)
2277{
2278 struct ceph_mds_request *req, *nreq;
2279 int err;
2280
2281 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2282
2283 mutex_lock(&mdsc->mutex);
2284 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
2285 err = __prepare_send_request(mdsc, req, session->s_mds);
2286 if (!err) {
2287 ceph_msg_get(req->r_request);
2288 ceph_con_send(&session->s_con, req->r_request);
2289 }
2290 }
2291 mutex_unlock(&mdsc->mutex);
2292}
2293
2294/*
2295 * Encode information about a cap for a reconnect with the MDS.
2296 */
Sage Weil2f2dc052009-10-06 11:31:09 -07002297static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2298 void *arg)
2299{
Sage Weil20cb34a2010-05-12 15:21:32 -07002300 union {
2301 struct ceph_mds_cap_reconnect v2;
2302 struct ceph_mds_cap_reconnect_v1 v1;
2303 } rec;
2304 size_t reclen;
Sage Weil2f2dc052009-10-06 11:31:09 -07002305 struct ceph_inode_info *ci;
Sage Weil20cb34a2010-05-12 15:21:32 -07002306 struct ceph_reconnect_state *recon_state = arg;
2307 struct ceph_pagelist *pagelist = recon_state->pagelist;
Sage Weil2f2dc052009-10-06 11:31:09 -07002308 char *path;
2309 int pathlen, err;
2310 u64 pathbase;
2311 struct dentry *dentry;
2312
2313 ci = cap->ci;
2314
2315 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2316 inode, ceph_vinop(inode), cap, cap->cap_id,
2317 ceph_cap_string(cap->issued));
Sage Weil93cea5b2009-12-23 12:21:51 -08002318 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2319 if (err)
2320 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002321
2322 dentry = d_find_alias(inode);
2323 if (dentry) {
2324 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2325 if (IS_ERR(path)) {
2326 err = PTR_ERR(path);
Sage Weile072f8a2010-08-26 09:26:37 -07002327 goto out_dput;
Sage Weil2f2dc052009-10-06 11:31:09 -07002328 }
2329 } else {
2330 path = NULL;
2331 pathlen = 0;
2332 }
Sage Weil93cea5b2009-12-23 12:21:51 -08002333 err = ceph_pagelist_encode_string(pagelist, path, pathlen);
2334 if (err)
Sage Weile072f8a2010-08-26 09:26:37 -07002335 goto out_free;
Sage Weil2f2dc052009-10-06 11:31:09 -07002336
Sage Weil2f2dc052009-10-06 11:31:09 -07002337 spin_lock(&inode->i_lock);
2338 cap->seq = 0; /* reset cap seq */
2339 cap->issue_seq = 0; /* and issue_seq */
Sage Weil20cb34a2010-05-12 15:21:32 -07002340
2341 if (recon_state->flock) {
2342 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
2343 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2344 rec.v2.issued = cpu_to_le32(cap->issued);
2345 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2346 rec.v2.pathbase = cpu_to_le64(pathbase);
2347 rec.v2.flock_len = 0;
2348 reclen = sizeof(rec.v2);
2349 } else {
2350 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
2351 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2352 rec.v1.issued = cpu_to_le32(cap->issued);
2353 rec.v1.size = cpu_to_le64(inode->i_size);
2354 ceph_encode_timespec(&rec.v1.mtime, &inode->i_mtime);
2355 ceph_encode_timespec(&rec.v1.atime, &inode->i_atime);
2356 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2357 rec.v1.pathbase = cpu_to_le64(pathbase);
2358 reclen = sizeof(rec.v1);
2359 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002360 spin_unlock(&inode->i_lock);
2361
Greg Farnum40819f62010-08-02 15:34:23 -07002362 if (recon_state->flock) {
2363 int num_fcntl_locks, num_flock_locks;
2364
2365 lock_kernel();
2366 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
2367 rec.v2.flock_len = (2*sizeof(u32) +
2368 (num_fcntl_locks+num_flock_locks) *
2369 sizeof(struct ceph_filelock));
2370
2371 err = ceph_pagelist_append(pagelist, &rec, reclen);
2372 if (!err)
2373 err = ceph_encode_locks(inode, pagelist,
2374 num_fcntl_locks,
2375 num_flock_locks);
2376 unlock_kernel();
2377 }
Sage Weil93cea5b2009-12-23 12:21:51 -08002378
Sage Weile072f8a2010-08-26 09:26:37 -07002379out_free:
Sage Weil2f2dc052009-10-06 11:31:09 -07002380 kfree(path);
Sage Weile072f8a2010-08-26 09:26:37 -07002381out_dput:
Sage Weil2f2dc052009-10-06 11:31:09 -07002382 dput(dentry);
Sage Weil93cea5b2009-12-23 12:21:51 -08002383 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002384}
2385
2386
2387/*
2388 * If an MDS fails and recovers, clients need to reconnect in order to
2389 * reestablish shared state. This includes all caps issued through
2390 * this session _and_ the snap_realm hierarchy. Because it's not
2391 * clear which snap realms the mds cares about, we send everything we
2392 * know about.. that ensures we'll then get any new info the
2393 * recovering MDS might have.
2394 *
2395 * This is a relatively heavyweight operation, but it's rare.
2396 *
2397 * called with mdsc->mutex held.
2398 */
Sage Weil34b6c852010-05-10 16:31:25 -07002399static void send_mds_reconnect(struct ceph_mds_client *mdsc,
2400 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07002401{
Sage Weil2f2dc052009-10-06 11:31:09 -07002402 struct ceph_msg *reply;
Sage Weila105f002010-02-15 14:37:55 -08002403 struct rb_node *p;
Sage Weil34b6c852010-05-10 16:31:25 -07002404 int mds = session->s_mds;
Sage Weil9abf82b2010-05-10 21:58:38 -07002405 int err = -ENOMEM;
Sage Weil93cea5b2009-12-23 12:21:51 -08002406 struct ceph_pagelist *pagelist;
Sage Weil20cb34a2010-05-12 15:21:32 -07002407 struct ceph_reconnect_state recon_state;
Sage Weil2f2dc052009-10-06 11:31:09 -07002408
Sage Weil34b6c852010-05-10 16:31:25 -07002409 pr_info("mds%d reconnect start\n", mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002410
Sage Weil93cea5b2009-12-23 12:21:51 -08002411 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
2412 if (!pagelist)
2413 goto fail_nopagelist;
2414 ceph_pagelist_init(pagelist);
2415
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002416 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -07002417 if (!reply)
Sage Weil93cea5b2009-12-23 12:21:51 -08002418 goto fail_nomsg;
Sage Weil93cea5b2009-12-23 12:21:51 -08002419
Sage Weil34b6c852010-05-10 16:31:25 -07002420 mutex_lock(&session->s_mutex);
2421 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
2422 session->s_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002423
Sage Weil34b6c852010-05-10 16:31:25 -07002424 ceph_con_open(&session->s_con,
2425 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
Sage Weil2f2dc052009-10-06 11:31:09 -07002426
Sage Weil34b6c852010-05-10 16:31:25 -07002427 /* replay unsafe requests */
2428 replay_unsafe_requests(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002429
2430 down_read(&mdsc->snap_rwsem);
2431
Sage Weil2f2dc052009-10-06 11:31:09 -07002432 dout("session %p state %s\n", session,
2433 session_state_name(session->s_state));
2434
Sage Weile01a5942010-05-10 15:36:44 -07002435 /* drop old cap expires; we're about to reestablish that state */
2436 discard_cap_releases(mdsc, session);
2437
Sage Weil2f2dc052009-10-06 11:31:09 -07002438 /* traverse this session's caps */
Sage Weil93cea5b2009-12-23 12:21:51 -08002439 err = ceph_pagelist_encode_32(pagelist, session->s_nr_caps);
2440 if (err)
2441 goto fail;
Sage Weil20cb34a2010-05-12 15:21:32 -07002442
2443 recon_state.pagelist = pagelist;
2444 recon_state.flock = session->s_con.peer_features & CEPH_FEATURE_FLOCK;
2445 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
Sage Weil2f2dc052009-10-06 11:31:09 -07002446 if (err < 0)
Sage Weil9abf82b2010-05-10 21:58:38 -07002447 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07002448
2449 /*
2450 * snaprealms. we provide mds with the ino, seq (version), and
2451 * parent for all of our realms. If the mds has any newer info,
2452 * it will tell us.
2453 */
Sage Weila105f002010-02-15 14:37:55 -08002454 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
2455 struct ceph_snap_realm *realm =
2456 rb_entry(p, struct ceph_snap_realm, node);
Sage Weil93cea5b2009-12-23 12:21:51 -08002457 struct ceph_mds_snaprealm_reconnect sr_rec;
Sage Weil2f2dc052009-10-06 11:31:09 -07002458
2459 dout(" adding snap realm %llx seq %lld parent %llx\n",
2460 realm->ino, realm->seq, realm->parent_ino);
Sage Weil93cea5b2009-12-23 12:21:51 -08002461 sr_rec.ino = cpu_to_le64(realm->ino);
2462 sr_rec.seq = cpu_to_le64(realm->seq);
2463 sr_rec.parent = cpu_to_le64(realm->parent_ino);
2464 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
2465 if (err)
2466 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07002467 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002468
Sage Weil93cea5b2009-12-23 12:21:51 -08002469 reply->pagelist = pagelist;
Sage Weil20cb34a2010-05-12 15:21:32 -07002470 if (recon_state.flock)
2471 reply->hdr.version = cpu_to_le16(2);
Sage Weil93cea5b2009-12-23 12:21:51 -08002472 reply->hdr.data_len = cpu_to_le32(pagelist->length);
2473 reply->nr_pages = calc_pages_for(0, pagelist->length);
Sage Weil2f2dc052009-10-06 11:31:09 -07002474 ceph_con_send(&session->s_con, reply);
2475
Sage Weil9abf82b2010-05-10 21:58:38 -07002476 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002477
Sage Weil9abf82b2010-05-10 21:58:38 -07002478 mutex_lock(&mdsc->mutex);
2479 __wake_requests(mdsc, &session->s_waiting);
2480 mutex_unlock(&mdsc->mutex);
2481
Sage Weil2f2dc052009-10-06 11:31:09 -07002482 up_read(&mdsc->snap_rwsem);
Sage Weil2f2dc052009-10-06 11:31:09 -07002483 return;
2484
Sage Weil93cea5b2009-12-23 12:21:51 -08002485fail:
Sage Weil2f2dc052009-10-06 11:31:09 -07002486 ceph_msg_put(reply);
Sage Weil9abf82b2010-05-10 21:58:38 -07002487 up_read(&mdsc->snap_rwsem);
2488 mutex_unlock(&session->s_mutex);
Sage Weil93cea5b2009-12-23 12:21:51 -08002489fail_nomsg:
2490 ceph_pagelist_release(pagelist);
2491 kfree(pagelist);
2492fail_nopagelist:
Sage Weil9abf82b2010-05-10 21:58:38 -07002493 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
Sage Weil9abf82b2010-05-10 21:58:38 -07002494 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07002495}
2496
2497
2498/*
2499 * compare old and new mdsmaps, kicking requests
2500 * and closing out old connections as necessary
2501 *
2502 * called under mdsc->mutex.
2503 */
2504static void check_new_map(struct ceph_mds_client *mdsc,
2505 struct ceph_mdsmap *newmap,
2506 struct ceph_mdsmap *oldmap)
2507{
2508 int i;
2509 int oldstate, newstate;
2510 struct ceph_mds_session *s;
2511
2512 dout("check_new_map new %u old %u\n",
2513 newmap->m_epoch, oldmap->m_epoch);
2514
2515 for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
2516 if (mdsc->sessions[i] == NULL)
2517 continue;
2518 s = mdsc->sessions[i];
2519 oldstate = ceph_mdsmap_get_state(oldmap, i);
2520 newstate = ceph_mdsmap_get_state(newmap, i);
2521
Sage Weil0deb01c2010-06-17 14:19:01 -07002522 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002523 i, ceph_mds_state_name(oldstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07002524 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
Sage Weil2f2dc052009-10-06 11:31:09 -07002525 ceph_mds_state_name(newstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07002526 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
Sage Weil2f2dc052009-10-06 11:31:09 -07002527 session_state_name(s->s_state));
2528
2529 if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
2530 ceph_mdsmap_get_addr(newmap, i),
2531 sizeof(struct ceph_entity_addr))) {
2532 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
2533 /* the session never opened, just close it
2534 * out now */
2535 __wake_requests(mdsc, &s->s_waiting);
Sage Weil2600d2d2010-02-22 15:12:16 -08002536 __unregister_session(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07002537 } else {
2538 /* just close it */
2539 mutex_unlock(&mdsc->mutex);
2540 mutex_lock(&s->s_mutex);
2541 mutex_lock(&mdsc->mutex);
2542 ceph_con_close(&s->s_con);
2543 mutex_unlock(&s->s_mutex);
2544 s->s_state = CEPH_MDS_SESSION_RESTARTING;
2545 }
2546
2547 /* kick any requests waiting on the recovering mds */
Sage Weil29790f22010-03-18 14:45:05 -07002548 kick_requests(mdsc, i);
Sage Weil2f2dc052009-10-06 11:31:09 -07002549 } else if (oldstate == newstate) {
2550 continue; /* nothing new with this mds */
2551 }
2552
2553 /*
2554 * send reconnect?
2555 */
2556 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
Sage Weil34b6c852010-05-10 16:31:25 -07002557 newstate >= CEPH_MDS_STATE_RECONNECT) {
2558 mutex_unlock(&mdsc->mutex);
2559 send_mds_reconnect(mdsc, s);
2560 mutex_lock(&mdsc->mutex);
2561 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002562
2563 /*
Sage Weil29790f22010-03-18 14:45:05 -07002564 * kick request on any mds that has gone active.
Sage Weil2f2dc052009-10-06 11:31:09 -07002565 */
2566 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
2567 newstate >= CEPH_MDS_STATE_ACTIVE) {
Sage Weil29790f22010-03-18 14:45:05 -07002568 if (oldstate != CEPH_MDS_STATE_CREATING &&
2569 oldstate != CEPH_MDS_STATE_STARTING)
2570 pr_info("mds%d recovery completed\n", s->s_mds);
2571 kick_requests(mdsc, i);
Sage Weil2f2dc052009-10-06 11:31:09 -07002572 ceph_kick_flushing_caps(mdsc, s);
Sage Weil0dc25702009-11-20 13:43:45 -08002573 wake_up_session_caps(s, 1);
Sage Weil2f2dc052009-10-06 11:31:09 -07002574 }
2575 }
Sage Weilcb170a22010-06-21 13:38:35 -07002576
2577 for (i = 0; i < newmap->m_max_mds && i < mdsc->max_sessions; i++) {
2578 s = mdsc->sessions[i];
2579 if (!s)
2580 continue;
2581 if (!ceph_mdsmap_is_laggy(newmap, i))
2582 continue;
2583 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2584 s->s_state == CEPH_MDS_SESSION_HUNG ||
2585 s->s_state == CEPH_MDS_SESSION_CLOSING) {
2586 dout(" connecting to export targets of laggy mds%d\n",
2587 i);
2588 __open_export_target_sessions(mdsc, s);
2589 }
2590 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002591}
2592
2593
2594
2595/*
2596 * leases
2597 */
2598
2599/*
2600 * caller must hold session s_mutex, dentry->d_lock
2601 */
2602void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
2603{
2604 struct ceph_dentry_info *di = ceph_dentry(dentry);
2605
2606 ceph_put_mds_session(di->lease_session);
2607 di->lease_session = NULL;
2608}
2609
Sage Weil2600d2d2010-02-22 15:12:16 -08002610static void handle_lease(struct ceph_mds_client *mdsc,
2611 struct ceph_mds_session *session,
2612 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002613{
2614 struct super_block *sb = mdsc->client->sb;
2615 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07002616 struct ceph_inode_info *ci;
2617 struct dentry *parent, *dentry;
2618 struct ceph_dentry_info *di;
Sage Weil2600d2d2010-02-22 15:12:16 -08002619 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002620 struct ceph_mds_lease *h = msg->front.iov_base;
Sage Weil1e5ea232010-06-04 10:05:40 -07002621 u32 seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002622 struct ceph_vino vino;
2623 int mask;
2624 struct qstr dname;
2625 int release = 0;
2626
Sage Weil2f2dc052009-10-06 11:31:09 -07002627 dout("handle_lease from mds%d\n", mds);
2628
2629 /* decode */
2630 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
2631 goto bad;
2632 vino.ino = le64_to_cpu(h->ino);
2633 vino.snap = CEPH_NOSNAP;
2634 mask = le16_to_cpu(h->mask);
Sage Weil1e5ea232010-06-04 10:05:40 -07002635 seq = le32_to_cpu(h->seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002636 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
2637 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
2638 if (dname.len != get_unaligned_le32(h+1))
2639 goto bad;
2640
Sage Weil2f2dc052009-10-06 11:31:09 -07002641 mutex_lock(&session->s_mutex);
2642 session->s_seq++;
2643
2644 /* lookup inode */
2645 inode = ceph_find_inode(sb, vino);
Sage Weil1e5ea232010-06-04 10:05:40 -07002646 dout("handle_lease %s, mask %d, ino %llx %p %.*s\n",
2647 ceph_lease_op_name(h->action), mask, vino.ino, inode,
2648 dname.len, dname.name);
Sage Weil2f2dc052009-10-06 11:31:09 -07002649 if (inode == NULL) {
2650 dout("handle_lease no inode %llx\n", vino.ino);
2651 goto release;
2652 }
2653 ci = ceph_inode(inode);
2654
2655 /* dentry */
2656 parent = d_find_alias(inode);
2657 if (!parent) {
2658 dout("no parent dentry on inode %p\n", inode);
2659 WARN_ON(1);
2660 goto release; /* hrm... */
2661 }
2662 dname.hash = full_name_hash(dname.name, dname.len);
2663 dentry = d_lookup(parent, &dname);
2664 dput(parent);
2665 if (!dentry)
2666 goto release;
2667
2668 spin_lock(&dentry->d_lock);
2669 di = ceph_dentry(dentry);
2670 switch (h->action) {
2671 case CEPH_MDS_LEASE_REVOKE:
2672 if (di && di->lease_session == session) {
Sage Weil1e5ea232010-06-04 10:05:40 -07002673 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
2674 h->seq = cpu_to_le32(di->lease_seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002675 __ceph_mdsc_drop_dentry_lease(dentry);
2676 }
2677 release = 1;
2678 break;
2679
2680 case CEPH_MDS_LEASE_RENEW:
2681 if (di && di->lease_session == session &&
2682 di->lease_gen == session->s_cap_gen &&
2683 di->lease_renew_from &&
2684 di->lease_renew_after == 0) {
2685 unsigned long duration =
2686 le32_to_cpu(h->duration_ms) * HZ / 1000;
2687
Sage Weil1e5ea232010-06-04 10:05:40 -07002688 di->lease_seq = seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002689 dentry->d_time = di->lease_renew_from + duration;
2690 di->lease_renew_after = di->lease_renew_from +
2691 (duration >> 1);
2692 di->lease_renew_from = 0;
2693 }
2694 break;
2695 }
2696 spin_unlock(&dentry->d_lock);
2697 dput(dentry);
2698
2699 if (!release)
2700 goto out;
2701
2702release:
2703 /* let's just reuse the same message */
2704 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
2705 ceph_msg_get(msg);
2706 ceph_con_send(&session->s_con, msg);
2707
2708out:
2709 iput(inode);
2710 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002711 return;
2712
2713bad:
2714 pr_err("corrupt lease message\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002715 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002716}
2717
2718void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
2719 struct inode *inode,
2720 struct dentry *dentry, char action,
2721 u32 seq)
2722{
2723 struct ceph_msg *msg;
2724 struct ceph_mds_lease *lease;
2725 int len = sizeof(*lease) + sizeof(u32);
2726 int dnamelen = 0;
2727
2728 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
2729 inode, dentry, ceph_lease_op_name(action), session->s_mds);
2730 dnamelen = dentry->d_name.len;
2731 len += dnamelen;
2732
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002733 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -07002734 if (!msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002735 return;
2736 lease = msg->front.iov_base;
2737 lease->action = action;
Sage Weildd1c9052010-05-25 16:45:25 -07002738 lease->mask = cpu_to_le16(1);
Sage Weil2f2dc052009-10-06 11:31:09 -07002739 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
2740 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
2741 lease->seq = cpu_to_le32(seq);
2742 put_unaligned_le32(dnamelen, lease + 1);
2743 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
2744
2745 /*
2746 * if this is a preemptive lease RELEASE, no need to
2747 * flush request stream, since the actual request will
2748 * soon follow.
2749 */
2750 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
2751
2752 ceph_con_send(&session->s_con, msg);
2753}
2754
2755/*
2756 * Preemptively release a lease we expect to invalidate anyway.
2757 * Pass @inode always, @dentry is optional.
2758 */
2759void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
2760 struct dentry *dentry, int mask)
2761{
2762 struct ceph_dentry_info *di;
2763 struct ceph_mds_session *session;
2764 u32 seq;
2765
2766 BUG_ON(inode == NULL);
2767 BUG_ON(dentry == NULL);
Sage Weildd1c9052010-05-25 16:45:25 -07002768 BUG_ON(mask == 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07002769
2770 /* is dentry lease valid? */
2771 spin_lock(&dentry->d_lock);
2772 di = ceph_dentry(dentry);
2773 if (!di || !di->lease_session ||
2774 di->lease_session->s_mds < 0 ||
2775 di->lease_gen != di->lease_session->s_cap_gen ||
2776 !time_before(jiffies, dentry->d_time)) {
2777 dout("lease_release inode %p dentry %p -- "
2778 "no lease on %d\n",
2779 inode, dentry, mask);
2780 spin_unlock(&dentry->d_lock);
2781 return;
2782 }
2783
2784 /* we do have a lease on this dentry; note mds and seq */
2785 session = ceph_get_mds_session(di->lease_session);
2786 seq = di->lease_seq;
2787 __ceph_mdsc_drop_dentry_lease(dentry);
2788 spin_unlock(&dentry->d_lock);
2789
2790 dout("lease_release inode %p dentry %p mask %d to mds%d\n",
2791 inode, dentry, mask, session->s_mds);
2792 ceph_mdsc_lease_send_msg(session, inode, dentry,
2793 CEPH_MDS_LEASE_RELEASE, seq);
2794 ceph_put_mds_session(session);
2795}
2796
2797/*
2798 * drop all leases (and dentry refs) in preparation for umount
2799 */
2800static void drop_leases(struct ceph_mds_client *mdsc)
2801{
2802 int i;
2803
2804 dout("drop_leases\n");
2805 mutex_lock(&mdsc->mutex);
2806 for (i = 0; i < mdsc->max_sessions; i++) {
2807 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2808 if (!s)
2809 continue;
2810 mutex_unlock(&mdsc->mutex);
2811 mutex_lock(&s->s_mutex);
2812 mutex_unlock(&s->s_mutex);
2813 ceph_put_mds_session(s);
2814 mutex_lock(&mdsc->mutex);
2815 }
2816 mutex_unlock(&mdsc->mutex);
2817}
2818
2819
2820
2821/*
2822 * delayed work -- periodically trim expired leases, renew caps with mds
2823 */
2824static void schedule_delayed(struct ceph_mds_client *mdsc)
2825{
2826 int delay = 5;
2827 unsigned hz = round_jiffies_relative(HZ * delay);
2828 schedule_delayed_work(&mdsc->delayed_work, hz);
2829}
2830
2831static void delayed_work(struct work_struct *work)
2832{
2833 int i;
2834 struct ceph_mds_client *mdsc =
2835 container_of(work, struct ceph_mds_client, delayed_work.work);
2836 int renew_interval;
2837 int renew_caps;
2838
2839 dout("mdsc delayed_work\n");
Sage Weilafcdaea2009-10-14 14:27:38 -07002840 ceph_check_delayed_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07002841
2842 mutex_lock(&mdsc->mutex);
2843 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
2844 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
2845 mdsc->last_renew_caps);
2846 if (renew_caps)
2847 mdsc->last_renew_caps = jiffies;
2848
2849 for (i = 0; i < mdsc->max_sessions; i++) {
2850 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2851 if (s == NULL)
2852 continue;
2853 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
2854 dout("resending session close request for mds%d\n",
2855 s->s_mds);
2856 request_close_session(mdsc, s);
2857 ceph_put_mds_session(s);
2858 continue;
2859 }
2860 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
2861 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
2862 s->s_state = CEPH_MDS_SESSION_HUNG;
2863 pr_info("mds%d hung\n", s->s_mds);
2864 }
2865 }
2866 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
2867 /* this mds is failed or recovering, just wait */
2868 ceph_put_mds_session(s);
2869 continue;
2870 }
2871 mutex_unlock(&mdsc->mutex);
2872
2873 mutex_lock(&s->s_mutex);
2874 if (renew_caps)
2875 send_renew_caps(mdsc, s);
2876 else
2877 ceph_con_keepalive(&s->s_con);
Sage Weilee6b2722010-06-10 12:55:52 -07002878 ceph_add_cap_releases(mdsc, s);
Sage Weilaab53dd2010-03-17 16:30:21 -07002879 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2880 s->s_state == CEPH_MDS_SESSION_HUNG)
Sage Weil3d7ded42010-06-09 16:47:10 -07002881 ceph_send_cap_releases(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07002882 mutex_unlock(&s->s_mutex);
2883 ceph_put_mds_session(s);
2884
2885 mutex_lock(&mdsc->mutex);
2886 }
2887 mutex_unlock(&mdsc->mutex);
2888
2889 schedule_delayed(mdsc);
2890}
2891
2892
Sage Weil5f44f142009-11-18 14:52:18 -08002893int ceph_mdsc_init(struct ceph_mds_client *mdsc, struct ceph_client *client)
Sage Weil2f2dc052009-10-06 11:31:09 -07002894{
2895 mdsc->client = client;
2896 mutex_init(&mdsc->mutex);
2897 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08002898 if (mdsc->mdsmap == NULL)
2899 return -ENOMEM;
2900
Sage Weil2f2dc052009-10-06 11:31:09 -07002901 init_completion(&mdsc->safe_umount_waiters);
Sage Weilf3c60c52010-08-11 14:51:23 -07002902 init_waitqueue_head(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002903 INIT_LIST_HEAD(&mdsc->waiting_for_map);
2904 mdsc->sessions = NULL;
2905 mdsc->max_sessions = 0;
2906 mdsc->stopping = 0;
2907 init_rwsem(&mdsc->snap_rwsem);
Sage Weila105f002010-02-15 14:37:55 -08002908 mdsc->snap_realms = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07002909 INIT_LIST_HEAD(&mdsc->snap_empty);
2910 spin_lock_init(&mdsc->snap_empty_lock);
2911 mdsc->last_tid = 0;
Sage Weil44ca18f2010-02-15 12:08:46 -08002912 mdsc->request_tree = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07002913 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
2914 mdsc->last_renew_caps = jiffies;
2915 INIT_LIST_HEAD(&mdsc->cap_delay_list);
2916 spin_lock_init(&mdsc->cap_delay_lock);
2917 INIT_LIST_HEAD(&mdsc->snap_flush_list);
2918 spin_lock_init(&mdsc->snap_flush_lock);
2919 mdsc->cap_flush_seq = 0;
2920 INIT_LIST_HEAD(&mdsc->cap_dirty);
2921 mdsc->num_cap_flushing = 0;
2922 spin_lock_init(&mdsc->cap_dirty_lock);
2923 init_waitqueue_head(&mdsc->cap_flushing_wq);
2924 spin_lock_init(&mdsc->dentry_lru_lock);
2925 INIT_LIST_HEAD(&mdsc->dentry_lru);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08002926
Yehuda Sadeh37151662010-06-17 16:16:12 -07002927 ceph_caps_init(mdsc);
2928 ceph_adjust_min_caps(mdsc, client->min_caps);
2929
Sage Weil5f44f142009-11-18 14:52:18 -08002930 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002931}
2932
2933/*
2934 * Wait for safe replies on open mds requests. If we time out, drop
2935 * all requests from the tree to avoid dangling dentry refs.
2936 */
2937static void wait_requests(struct ceph_mds_client *mdsc)
2938{
2939 struct ceph_mds_request *req;
2940 struct ceph_client *client = mdsc->client;
2941
2942 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08002943 if (__get_oldest_req(mdsc)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002944 mutex_unlock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08002945
Sage Weil2f2dc052009-10-06 11:31:09 -07002946 dout("wait_requests waiting for requests\n");
2947 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
Sage Weil6b805182009-10-27 11:50:50 -07002948 client->mount_args->mount_timeout * HZ);
Sage Weil2f2dc052009-10-06 11:31:09 -07002949
2950 /* tear down remaining requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08002951 mutex_lock(&mdsc->mutex);
2952 while ((req = __get_oldest_req(mdsc))) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002953 dout("wait_requests timed out on tid %llu\n",
2954 req->r_tid);
Sage Weil44ca18f2010-02-15 12:08:46 -08002955 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002956 }
2957 }
2958 mutex_unlock(&mdsc->mutex);
2959 dout("wait_requests done\n");
2960}
2961
2962/*
2963 * called before mount is ro, and before dentries are torn down.
2964 * (hmm, does this still race with new lookups?)
2965 */
2966void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
2967{
2968 dout("pre_umount\n");
2969 mdsc->stopping = 1;
2970
2971 drop_leases(mdsc);
Sage Weilafcdaea2009-10-14 14:27:38 -07002972 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07002973 wait_requests(mdsc);
Sage Weil17c688c2010-06-21 16:12:26 -07002974
2975 /*
2976 * wait for reply handlers to drop their request refs and
2977 * their inode/dcache refs
2978 */
2979 ceph_msgr_flush();
Sage Weil2f2dc052009-10-06 11:31:09 -07002980}
2981
2982/*
2983 * wait for all write mds requests to flush.
2984 */
2985static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
2986{
Sage Weil80fc7312010-03-16 15:28:54 -07002987 struct ceph_mds_request *req = NULL, *nextreq;
Sage Weil44ca18f2010-02-15 12:08:46 -08002988 struct rb_node *n;
Sage Weil2f2dc052009-10-06 11:31:09 -07002989
2990 mutex_lock(&mdsc->mutex);
2991 dout("wait_unsafe_requests want %lld\n", want_tid);
Sage Weil80fc7312010-03-16 15:28:54 -07002992restart:
Sage Weil44ca18f2010-02-15 12:08:46 -08002993 req = __get_oldest_req(mdsc);
2994 while (req && req->r_tid <= want_tid) {
Sage Weil80fc7312010-03-16 15:28:54 -07002995 /* find next request */
2996 n = rb_next(&req->r_node);
2997 if (n)
2998 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
2999 else
3000 nextreq = NULL;
Sage Weil44ca18f2010-02-15 12:08:46 -08003001 if ((req->r_op & CEPH_MDS_OP_WRITE)) {
3002 /* write op */
3003 ceph_mdsc_get_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07003004 if (nextreq)
3005 ceph_mdsc_get_request(nextreq);
Sage Weil44ca18f2010-02-15 12:08:46 -08003006 mutex_unlock(&mdsc->mutex);
3007 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3008 req->r_tid, want_tid);
3009 wait_for_completion(&req->r_safe_completion);
3010 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003011 ceph_mdsc_put_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07003012 if (!nextreq)
3013 break; /* next dne before, so we're done! */
3014 if (RB_EMPTY_NODE(&nextreq->r_node)) {
3015 /* next request was removed from tree */
3016 ceph_mdsc_put_request(nextreq);
3017 goto restart;
3018 }
3019 ceph_mdsc_put_request(nextreq); /* won't go away */
Sage Weil44ca18f2010-02-15 12:08:46 -08003020 }
Sage Weil80fc7312010-03-16 15:28:54 -07003021 req = nextreq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003022 }
3023 mutex_unlock(&mdsc->mutex);
3024 dout("wait_unsafe_requests done\n");
3025}
3026
3027void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
3028{
3029 u64 want_tid, want_flush;
3030
Sage Weil56b7cf92010-05-03 15:22:00 -07003031 if (mdsc->client->mount_state == CEPH_MOUNT_SHUTDOWN)
3032 return;
3033
Sage Weil2f2dc052009-10-06 11:31:09 -07003034 dout("sync\n");
3035 mutex_lock(&mdsc->mutex);
3036 want_tid = mdsc->last_tid;
3037 want_flush = mdsc->cap_flush_seq;
3038 mutex_unlock(&mdsc->mutex);
3039 dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
3040
Sage Weilafcdaea2009-10-14 14:27:38 -07003041 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003042
3043 wait_unsafe_requests(mdsc, want_tid);
3044 wait_event(mdsc->cap_flushing_wq, check_cap_flush(mdsc, want_flush));
3045}
3046
Sage Weilf3c60c52010-08-11 14:51:23 -07003047/*
3048 * true if all sessions are closed, or we force unmount
3049 */
3050bool done_closing_sessions(struct ceph_mds_client *mdsc)
3051{
3052 int i, n = 0;
3053
3054 if (mdsc->client->mount_state == CEPH_MOUNT_SHUTDOWN)
3055 return true;
3056
3057 mutex_lock(&mdsc->mutex);
3058 for (i = 0; i < mdsc->max_sessions; i++)
3059 if (mdsc->sessions[i])
3060 n++;
3061 mutex_unlock(&mdsc->mutex);
3062 return n == 0;
3063}
Sage Weil2f2dc052009-10-06 11:31:09 -07003064
3065/*
3066 * called after sb is ro.
3067 */
3068void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
3069{
3070 struct ceph_mds_session *session;
3071 int i;
Sage Weil2f2dc052009-10-06 11:31:09 -07003072 struct ceph_client *client = mdsc->client;
Sage Weilf3c60c52010-08-11 14:51:23 -07003073 unsigned long timeout = client->mount_args->mount_timeout * HZ;
Sage Weil2f2dc052009-10-06 11:31:09 -07003074
3075 dout("close_sessions\n");
3076
Sage Weil2f2dc052009-10-06 11:31:09 -07003077 /* close sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07003078 mutex_lock(&mdsc->mutex);
3079 for (i = 0; i < mdsc->max_sessions; i++) {
3080 session = __ceph_lookup_mds_session(mdsc, i);
3081 if (!session)
3082 continue;
Sage Weil2f2dc052009-10-06 11:31:09 -07003083 mutex_unlock(&mdsc->mutex);
Sage Weilf3c60c52010-08-11 14:51:23 -07003084 mutex_lock(&session->s_mutex);
3085 __close_session(mdsc, session);
3086 mutex_unlock(&session->s_mutex);
3087 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003088 mutex_lock(&mdsc->mutex);
3089 }
Sage Weilf3c60c52010-08-11 14:51:23 -07003090 mutex_unlock(&mdsc->mutex);
3091
3092 dout("waiting for sessions to close\n");
3093 wait_event_timeout(mdsc->session_close_wq, done_closing_sessions(mdsc),
3094 timeout);
Sage Weil2f2dc052009-10-06 11:31:09 -07003095
3096 /* tear down remaining sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07003097 mutex_lock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003098 for (i = 0; i < mdsc->max_sessions; i++) {
3099 if (mdsc->sessions[i]) {
3100 session = get_session(mdsc->sessions[i]);
Sage Weil2600d2d2010-02-22 15:12:16 -08003101 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003102 mutex_unlock(&mdsc->mutex);
3103 mutex_lock(&session->s_mutex);
3104 remove_session_caps(session);
3105 mutex_unlock(&session->s_mutex);
3106 ceph_put_mds_session(session);
3107 mutex_lock(&mdsc->mutex);
3108 }
3109 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003110 WARN_ON(!list_empty(&mdsc->cap_delay_list));
Sage Weil2f2dc052009-10-06 11:31:09 -07003111 mutex_unlock(&mdsc->mutex);
3112
3113 ceph_cleanup_empty_realms(mdsc);
3114
3115 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3116
3117 dout("stopped\n");
3118}
3119
3120void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
3121{
3122 dout("stop\n");
3123 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3124 if (mdsc->mdsmap)
3125 ceph_mdsmap_destroy(mdsc->mdsmap);
3126 kfree(mdsc->sessions);
Yehuda Sadeh37151662010-06-17 16:16:12 -07003127 ceph_caps_finalize(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003128}
3129
3130
3131/*
3132 * handle mds map update.
3133 */
3134void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3135{
3136 u32 epoch;
3137 u32 maplen;
3138 void *p = msg->front.iov_base;
3139 void *end = p + msg->front.iov_len;
3140 struct ceph_mdsmap *newmap, *oldmap;
3141 struct ceph_fsid fsid;
3142 int err = -EINVAL;
3143
3144 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
3145 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08003146 if (ceph_check_fsid(mdsc->client, &fsid) < 0)
3147 return;
Sage Weilc89136e2009-10-14 09:59:09 -07003148 epoch = ceph_decode_32(&p);
3149 maplen = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07003150 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
3151
3152 /* do we need it? */
3153 ceph_monc_got_mdsmap(&mdsc->client->monc, epoch);
3154 mutex_lock(&mdsc->mutex);
3155 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
3156 dout("handle_map epoch %u <= our %u\n",
3157 epoch, mdsc->mdsmap->m_epoch);
3158 mutex_unlock(&mdsc->mutex);
3159 return;
3160 }
3161
3162 newmap = ceph_mdsmap_decode(&p, end);
3163 if (IS_ERR(newmap)) {
3164 err = PTR_ERR(newmap);
3165 goto bad_unlock;
3166 }
3167
3168 /* swap into place */
3169 if (mdsc->mdsmap) {
3170 oldmap = mdsc->mdsmap;
3171 mdsc->mdsmap = newmap;
3172 check_new_map(mdsc, newmap, oldmap);
3173 ceph_mdsmap_destroy(oldmap);
3174 } else {
3175 mdsc->mdsmap = newmap; /* first mds map */
3176 }
3177 mdsc->client->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
3178
3179 __wake_requests(mdsc, &mdsc->waiting_for_map);
3180
3181 mutex_unlock(&mdsc->mutex);
3182 schedule_delayed(mdsc);
3183 return;
3184
3185bad_unlock:
3186 mutex_unlock(&mdsc->mutex);
3187bad:
3188 pr_err("error decoding mdsmap %d\n", err);
3189 return;
3190}
3191
3192static struct ceph_connection *con_get(struct ceph_connection *con)
3193{
3194 struct ceph_mds_session *s = con->private;
3195
3196 if (get_session(s)) {
Sage Weil2600d2d2010-02-22 15:12:16 -08003197 dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -07003198 return con;
3199 }
3200 dout("mdsc con_get %p FAIL\n", s);
3201 return NULL;
3202}
3203
3204static void con_put(struct ceph_connection *con)
3205{
3206 struct ceph_mds_session *s = con->private;
3207
Sage Weil2f2dc052009-10-06 11:31:09 -07003208 ceph_put_mds_session(s);
Sage Weil2600d2d2010-02-22 15:12:16 -08003209 dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -07003210}
3211
3212/*
3213 * if the client is unresponsive for long enough, the mds will kill
3214 * the session entirely.
3215 */
3216static void peer_reset(struct ceph_connection *con)
3217{
3218 struct ceph_mds_session *s = con->private;
Sage Weil7e70f0e2010-03-18 13:59:12 -07003219 struct ceph_mds_client *mdsc = s->s_mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07003220
Sage Weil7e70f0e2010-03-18 13:59:12 -07003221 pr_warning("mds%d closed our session\n", s->s_mds);
3222 send_mds_reconnect(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07003223}
3224
3225static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
3226{
3227 struct ceph_mds_session *s = con->private;
3228 struct ceph_mds_client *mdsc = s->s_mdsc;
3229 int type = le16_to_cpu(msg->hdr.type);
3230
Sage Weil2600d2d2010-02-22 15:12:16 -08003231 mutex_lock(&mdsc->mutex);
3232 if (__verify_registered_session(mdsc, s) < 0) {
3233 mutex_unlock(&mdsc->mutex);
3234 goto out;
3235 }
3236 mutex_unlock(&mdsc->mutex);
3237
Sage Weil2f2dc052009-10-06 11:31:09 -07003238 switch (type) {
3239 case CEPH_MSG_MDS_MAP:
3240 ceph_mdsc_handle_map(mdsc, msg);
3241 break;
3242 case CEPH_MSG_CLIENT_SESSION:
3243 handle_session(s, msg);
3244 break;
3245 case CEPH_MSG_CLIENT_REPLY:
3246 handle_reply(s, msg);
3247 break;
3248 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
Sage Weil2600d2d2010-02-22 15:12:16 -08003249 handle_forward(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003250 break;
3251 case CEPH_MSG_CLIENT_CAPS:
3252 ceph_handle_caps(s, msg);
3253 break;
3254 case CEPH_MSG_CLIENT_SNAP:
Sage Weil2600d2d2010-02-22 15:12:16 -08003255 ceph_handle_snap(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003256 break;
3257 case CEPH_MSG_CLIENT_LEASE:
Sage Weil2600d2d2010-02-22 15:12:16 -08003258 handle_lease(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003259 break;
3260
3261 default:
3262 pr_err("received unknown message type %d %s\n", type,
3263 ceph_msg_type_name(type));
3264 }
Sage Weil2600d2d2010-02-22 15:12:16 -08003265out:
Sage Weil2f2dc052009-10-06 11:31:09 -07003266 ceph_msg_put(msg);
3267}
3268
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003269/*
3270 * authentication
3271 */
3272static int get_authorizer(struct ceph_connection *con,
3273 void **buf, int *len, int *proto,
3274 void **reply_buf, int *reply_len, int force_new)
3275{
3276 struct ceph_mds_session *s = con->private;
3277 struct ceph_mds_client *mdsc = s->s_mdsc;
3278 struct ceph_auth_client *ac = mdsc->client->monc.auth;
3279 int ret = 0;
3280
3281 if (force_new && s->s_authorizer) {
3282 ac->ops->destroy_authorizer(ac, s->s_authorizer);
3283 s->s_authorizer = NULL;
3284 }
3285 if (s->s_authorizer == NULL) {
3286 if (ac->ops->create_authorizer) {
3287 ret = ac->ops->create_authorizer(
3288 ac, CEPH_ENTITY_TYPE_MDS,
3289 &s->s_authorizer,
3290 &s->s_authorizer_buf,
3291 &s->s_authorizer_buf_len,
3292 &s->s_authorizer_reply_buf,
3293 &s->s_authorizer_reply_buf_len);
3294 if (ret)
3295 return ret;
3296 }
3297 }
3298
3299 *proto = ac->protocol;
3300 *buf = s->s_authorizer_buf;
3301 *len = s->s_authorizer_buf_len;
3302 *reply_buf = s->s_authorizer_reply_buf;
3303 *reply_len = s->s_authorizer_reply_buf_len;
3304 return 0;
3305}
3306
3307
3308static int verify_authorizer_reply(struct ceph_connection *con, int len)
3309{
3310 struct ceph_mds_session *s = con->private;
3311 struct ceph_mds_client *mdsc = s->s_mdsc;
3312 struct ceph_auth_client *ac = mdsc->client->monc.auth;
3313
3314 return ac->ops->verify_authorizer_reply(ac, s->s_authorizer, len);
3315}
3316
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003317static int invalidate_authorizer(struct ceph_connection *con)
3318{
3319 struct ceph_mds_session *s = con->private;
3320 struct ceph_mds_client *mdsc = s->s_mdsc;
3321 struct ceph_auth_client *ac = mdsc->client->monc.auth;
3322
3323 if (ac->ops->invalidate_authorizer)
3324 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
3325
3326 return ceph_monc_validate_auth(&mdsc->client->monc);
3327}
3328
Tobias Klauser9e327892010-05-20 10:40:19 +02003329static const struct ceph_connection_operations mds_con_ops = {
Sage Weil2f2dc052009-10-06 11:31:09 -07003330 .get = con_get,
3331 .put = con_put,
3332 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003333 .get_authorizer = get_authorizer,
3334 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003335 .invalidate_authorizer = invalidate_authorizer,
Sage Weil2f2dc052009-10-06 11:31:09 -07003336 .peer_reset = peer_reset,
Sage Weil2f2dc052009-10-06 11:31:09 -07003337};
3338
3339
3340
3341
3342/* eof */