blob: 33568239a08e3c71cd2ec9376af15c1a4cb14bab [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weil2f2dc052009-10-06 11:31:09 -07002
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>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07007#include <linux/debugfs.h>
8#include <linux/seq_file.h>
Sage Weil2f2dc052009-10-06 11:31:09 -07009
Sage Weil2f2dc052009-10-06 11:31:09 -070010#include "super.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070011#include "mds_client.h"
12
13#include <linux/ceph/messenger.h>
14#include <linux/ceph/decode.h>
15#include <linux/ceph/pagelist.h>
16#include <linux/ceph/auth.h>
17#include <linux/ceph/debugfs.h>
Sage Weil2f2dc052009-10-06 11:31:09 -070018
19/*
20 * A cluster of MDS (metadata server) daemons is responsible for
21 * managing the file system namespace (the directory hierarchy and
22 * inodes) and for coordinating shared access to storage. Metadata is
23 * partitioning hierarchically across a number of servers, and that
24 * partition varies over time as the cluster adjusts the distribution
25 * in order to balance load.
26 *
27 * The MDS client is primarily responsible to managing synchronous
28 * metadata requests for operations like open, unlink, and so forth.
29 * If there is a MDS failure, we find out about it when we (possibly
30 * request and) receive a new MDS map, and can resubmit affected
31 * requests.
32 *
33 * For the most part, though, we take advantage of a lossless
34 * communications channel to the MDS, and do not need to worry about
35 * timing out or resubmitting requests.
36 *
37 * We maintain a stateful "session" with each MDS we interact with.
38 * Within each session, we sent periodic heartbeat messages to ensure
39 * any capabilities or leases we have been issues remain valid. If
40 * the session times out and goes stale, our leases and capabilities
41 * are no longer valid.
42 */
43
Sage Weil20cb34a2010-05-12 15:21:32 -070044struct ceph_reconnect_state {
45 struct ceph_pagelist *pagelist;
46 bool flock;
47};
48
Sage Weil2f2dc052009-10-06 11:31:09 -070049static void __wake_requests(struct ceph_mds_client *mdsc,
50 struct list_head *head);
51
Tobias Klauser9e327892010-05-20 10:40:19 +020052static const struct ceph_connection_operations mds_con_ops;
Sage Weil2f2dc052009-10-06 11:31:09 -070053
54
55/*
56 * mds reply parsing
57 */
58
59/*
60 * parse individual inode info
61 */
62static int parse_reply_info_in(void **p, void *end,
63 struct ceph_mds_reply_info_in *info)
64{
65 int err = -EIO;
66
67 info->in = *p;
68 *p += sizeof(struct ceph_mds_reply_inode) +
69 sizeof(*info->in->fragtree.splits) *
70 le32_to_cpu(info->in->fragtree.nsplits);
71
72 ceph_decode_32_safe(p, end, info->symlink_len, bad);
73 ceph_decode_need(p, end, info->symlink_len, bad);
74 info->symlink = *p;
75 *p += info->symlink_len;
76
77 ceph_decode_32_safe(p, end, info->xattr_len, bad);
78 ceph_decode_need(p, end, info->xattr_len, bad);
79 info->xattr_data = *p;
80 *p += info->xattr_len;
81 return 0;
82bad:
83 return err;
84}
85
86/*
87 * parse a normal reply, which may contain a (dir+)dentry and/or a
88 * target inode.
89 */
90static int parse_reply_info_trace(void **p, void *end,
91 struct ceph_mds_reply_info_parsed *info)
92{
93 int err;
94
95 if (info->head->is_dentry) {
96 err = parse_reply_info_in(p, end, &info->diri);
97 if (err < 0)
98 goto out_bad;
99
100 if (unlikely(*p + sizeof(*info->dirfrag) > end))
101 goto bad;
102 info->dirfrag = *p;
103 *p += sizeof(*info->dirfrag) +
104 sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
105 if (unlikely(*p > end))
106 goto bad;
107
108 ceph_decode_32_safe(p, end, info->dname_len, bad);
109 ceph_decode_need(p, end, info->dname_len, bad);
110 info->dname = *p;
111 *p += info->dname_len;
112 info->dlease = *p;
113 *p += sizeof(*info->dlease);
114 }
115
116 if (info->head->is_target) {
117 err = parse_reply_info_in(p, end, &info->targeti);
118 if (err < 0)
119 goto out_bad;
120 }
121
122 if (unlikely(*p != end))
123 goto bad;
124 return 0;
125
126bad:
127 err = -EIO;
128out_bad:
129 pr_err("problem parsing mds trace %d\n", err);
130 return err;
131}
132
133/*
134 * parse readdir results
135 */
136static int parse_reply_info_dir(void **p, void *end,
137 struct ceph_mds_reply_info_parsed *info)
138{
139 u32 num, i = 0;
140 int err;
141
142 info->dir_dir = *p;
143 if (*p + sizeof(*info->dir_dir) > end)
144 goto bad;
145 *p += sizeof(*info->dir_dir) +
146 sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
147 if (*p > end)
148 goto bad;
149
150 ceph_decode_need(p, end, sizeof(num) + 2, bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700151 num = ceph_decode_32(p);
152 info->dir_end = ceph_decode_8(p);
153 info->dir_complete = ceph_decode_8(p);
Sage Weil2f2dc052009-10-06 11:31:09 -0700154 if (num == 0)
155 goto done;
156
157 /* alloc large array */
158 info->dir_nr = num;
159 info->dir_in = kcalloc(num, sizeof(*info->dir_in) +
160 sizeof(*info->dir_dname) +
161 sizeof(*info->dir_dname_len) +
162 sizeof(*info->dir_dlease),
163 GFP_NOFS);
164 if (info->dir_in == NULL) {
165 err = -ENOMEM;
166 goto out_bad;
167 }
168 info->dir_dname = (void *)(info->dir_in + num);
169 info->dir_dname_len = (void *)(info->dir_dname + num);
170 info->dir_dlease = (void *)(info->dir_dname_len + num);
171
172 while (num) {
173 /* dentry */
174 ceph_decode_need(p, end, sizeof(u32)*2, bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700175 info->dir_dname_len[i] = ceph_decode_32(p);
Sage Weil2f2dc052009-10-06 11:31:09 -0700176 ceph_decode_need(p, end, info->dir_dname_len[i], bad);
177 info->dir_dname[i] = *p;
178 *p += info->dir_dname_len[i];
179 dout("parsed dir dname '%.*s'\n", info->dir_dname_len[i],
180 info->dir_dname[i]);
181 info->dir_dlease[i] = *p;
182 *p += sizeof(struct ceph_mds_reply_lease);
183
184 /* inode */
185 err = parse_reply_info_in(p, end, &info->dir_in[i]);
186 if (err < 0)
187 goto out_bad;
188 i++;
189 num--;
190 }
191
192done:
193 if (*p != end)
194 goto bad;
195 return 0;
196
197bad:
198 err = -EIO;
199out_bad:
200 pr_err("problem parsing dir contents %d\n", err);
201 return err;
202}
203
204/*
205 * parse entire mds reply
206 */
207static int parse_reply_info(struct ceph_msg *msg,
208 struct ceph_mds_reply_info_parsed *info)
209{
210 void *p, *end;
211 u32 len;
212 int err;
213
214 info->head = msg->front.iov_base;
215 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
216 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
217
218 /* trace */
219 ceph_decode_32_safe(&p, end, len, bad);
220 if (len > 0) {
221 err = parse_reply_info_trace(&p, p+len, info);
222 if (err < 0)
223 goto out_bad;
224 }
225
226 /* dir content */
227 ceph_decode_32_safe(&p, end, len, bad);
228 if (len > 0) {
229 err = parse_reply_info_dir(&p, p+len, info);
230 if (err < 0)
231 goto out_bad;
232 }
233
234 /* snap blob */
235 ceph_decode_32_safe(&p, end, len, bad);
236 info->snapblob_len = len;
237 info->snapblob = p;
238 p += len;
239
240 if (p != end)
241 goto bad;
242 return 0;
243
244bad:
245 err = -EIO;
246out_bad:
247 pr_err("mds parse_reply err %d\n", err);
248 return err;
249}
250
251static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
252{
253 kfree(info->dir_in);
254}
255
256
257/*
258 * sessions
259 */
260static const char *session_state_name(int s)
261{
262 switch (s) {
263 case CEPH_MDS_SESSION_NEW: return "new";
264 case CEPH_MDS_SESSION_OPENING: return "opening";
265 case CEPH_MDS_SESSION_OPEN: return "open";
266 case CEPH_MDS_SESSION_HUNG: return "hung";
267 case CEPH_MDS_SESSION_CLOSING: return "closing";
Sage Weil44ca18f2010-02-15 12:08:46 -0800268 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
Sage Weil2f2dc052009-10-06 11:31:09 -0700269 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
270 default: return "???";
271 }
272}
273
274static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
275{
276 if (atomic_inc_not_zero(&s->s_ref)) {
277 dout("mdsc get_session %p %d -> %d\n", s,
278 atomic_read(&s->s_ref)-1, atomic_read(&s->s_ref));
279 return s;
280 } else {
281 dout("mdsc get_session %p 0 -- FAIL", s);
282 return NULL;
283 }
284}
285
286void ceph_put_mds_session(struct ceph_mds_session *s)
287{
288 dout("mdsc put_session %p %d -> %d\n", s,
289 atomic_read(&s->s_ref), atomic_read(&s->s_ref)-1);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800290 if (atomic_dec_and_test(&s->s_ref)) {
291 if (s->s_authorizer)
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700292 s->s_mdsc->fsc->client->monc.auth->ops->destroy_authorizer(
293 s->s_mdsc->fsc->client->monc.auth,
294 s->s_authorizer);
Sage Weil2f2dc052009-10-06 11:31:09 -0700295 kfree(s);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800296 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700297}
298
299/*
300 * called under mdsc->mutex
301 */
302struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
303 int mds)
304{
305 struct ceph_mds_session *session;
306
307 if (mds >= mdsc->max_sessions || mdsc->sessions[mds] == NULL)
308 return NULL;
309 session = mdsc->sessions[mds];
310 dout("lookup_mds_session %p %d\n", session,
311 atomic_read(&session->s_ref));
312 get_session(session);
313 return session;
314}
315
316static bool __have_session(struct ceph_mds_client *mdsc, int mds)
317{
318 if (mds >= mdsc->max_sessions)
319 return false;
320 return mdsc->sessions[mds];
321}
322
Sage Weil2600d2d2010-02-22 15:12:16 -0800323static int __verify_registered_session(struct ceph_mds_client *mdsc,
324 struct ceph_mds_session *s)
325{
326 if (s->s_mds >= mdsc->max_sessions ||
327 mdsc->sessions[s->s_mds] != s)
328 return -ENOENT;
329 return 0;
330}
331
Sage Weil2f2dc052009-10-06 11:31:09 -0700332/*
333 * create+register a new session for given mds.
334 * called under mdsc->mutex.
335 */
336static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
337 int mds)
338{
339 struct ceph_mds_session *s;
340
341 s = kzalloc(sizeof(*s), GFP_NOFS);
Dan Carpenter4736b002010-03-20 15:30:16 +0300342 if (!s)
343 return ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -0700344 s->s_mdsc = mdsc;
345 s->s_mds = mds;
346 s->s_state = CEPH_MDS_SESSION_NEW;
347 s->s_ttl = 0;
348 s->s_seq = 0;
349 mutex_init(&s->s_mutex);
350
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700351 ceph_con_init(mdsc->fsc->client->msgr, &s->s_con);
Sage Weil2f2dc052009-10-06 11:31:09 -0700352 s->s_con.private = s;
353 s->s_con.ops = &mds_con_ops;
354 s->s_con.peer_name.type = CEPH_ENTITY_TYPE_MDS;
355 s->s_con.peer_name.num = cpu_to_le64(mds);
Sage Weil2f2dc052009-10-06 11:31:09 -0700356
357 spin_lock_init(&s->s_cap_lock);
358 s->s_cap_gen = 0;
359 s->s_cap_ttl = 0;
360 s->s_renew_requested = 0;
361 s->s_renew_seq = 0;
362 INIT_LIST_HEAD(&s->s_caps);
363 s->s_nr_caps = 0;
Sage Weil5dacf092009-12-21 20:40:34 -0800364 s->s_trim_caps = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -0700365 atomic_set(&s->s_ref, 1);
366 INIT_LIST_HEAD(&s->s_waiting);
367 INIT_LIST_HEAD(&s->s_unsafe);
368 s->s_num_cap_releases = 0;
Sage Weil7c1332b2010-02-16 11:39:45 -0800369 s->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700370 INIT_LIST_HEAD(&s->s_cap_releases);
371 INIT_LIST_HEAD(&s->s_cap_releases_done);
372 INIT_LIST_HEAD(&s->s_cap_flushing);
373 INIT_LIST_HEAD(&s->s_cap_snaps_flushing);
374
375 dout("register_session mds%d\n", mds);
376 if (mds >= mdsc->max_sessions) {
377 int newmax = 1 << get_count_order(mds+1);
378 struct ceph_mds_session **sa;
379
380 dout("register_session realloc to %d\n", newmax);
381 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
382 if (sa == NULL)
Sage Weil42ce56e2009-11-18 11:22:36 -0800383 goto fail_realloc;
Sage Weil2f2dc052009-10-06 11:31:09 -0700384 if (mdsc->sessions) {
385 memcpy(sa, mdsc->sessions,
386 mdsc->max_sessions * sizeof(void *));
387 kfree(mdsc->sessions);
388 }
389 mdsc->sessions = sa;
390 mdsc->max_sessions = newmax;
391 }
392 mdsc->sessions[mds] = s;
393 atomic_inc(&s->s_ref); /* one ref to sessions[], one to caller */
Sage Weil42ce56e2009-11-18 11:22:36 -0800394
395 ceph_con_open(&s->s_con, ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
396
Sage Weil2f2dc052009-10-06 11:31:09 -0700397 return s;
Sage Weil42ce56e2009-11-18 11:22:36 -0800398
399fail_realloc:
400 kfree(s);
401 return ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -0700402}
403
404/*
405 * called under mdsc->mutex
406 */
Sage Weil2600d2d2010-02-22 15:12:16 -0800407static void __unregister_session(struct ceph_mds_client *mdsc,
Sage Weil42ce56e2009-11-18 11:22:36 -0800408 struct ceph_mds_session *s)
Sage Weil2f2dc052009-10-06 11:31:09 -0700409{
Sage Weil2600d2d2010-02-22 15:12:16 -0800410 dout("__unregister_session mds%d %p\n", s->s_mds, s);
411 BUG_ON(mdsc->sessions[s->s_mds] != s);
Sage Weil42ce56e2009-11-18 11:22:36 -0800412 mdsc->sessions[s->s_mds] = NULL;
413 ceph_con_close(&s->s_con);
414 ceph_put_mds_session(s);
Sage Weil2f2dc052009-10-06 11:31:09 -0700415}
416
417/*
418 * drop session refs in request.
419 *
420 * should be last request ref, or hold mdsc->mutex
421 */
422static void put_request_session(struct ceph_mds_request *req)
423{
424 if (req->r_session) {
425 ceph_put_mds_session(req->r_session);
426 req->r_session = NULL;
427 }
428}
429
Sage Weil153c8e62009-12-07 12:31:09 -0800430void ceph_mdsc_release_request(struct kref *kref)
Sage Weil2f2dc052009-10-06 11:31:09 -0700431{
Sage Weil153c8e62009-12-07 12:31:09 -0800432 struct ceph_mds_request *req = container_of(kref,
433 struct ceph_mds_request,
434 r_kref);
435 if (req->r_request)
436 ceph_msg_put(req->r_request);
437 if (req->r_reply) {
438 ceph_msg_put(req->r_reply);
439 destroy_reply_info(&req->r_reply_info);
Sage Weil2f2dc052009-10-06 11:31:09 -0700440 }
Sage Weil153c8e62009-12-07 12:31:09 -0800441 if (req->r_inode) {
442 ceph_put_cap_refs(ceph_inode(req->r_inode),
443 CEPH_CAP_PIN);
444 iput(req->r_inode);
445 }
446 if (req->r_locked_dir)
447 ceph_put_cap_refs(ceph_inode(req->r_locked_dir),
448 CEPH_CAP_PIN);
449 if (req->r_target_inode)
450 iput(req->r_target_inode);
451 if (req->r_dentry)
452 dput(req->r_dentry);
453 if (req->r_old_dentry) {
454 ceph_put_cap_refs(
455 ceph_inode(req->r_old_dentry->d_parent->d_inode),
456 CEPH_CAP_PIN);
457 dput(req->r_old_dentry);
458 }
459 kfree(req->r_path1);
460 kfree(req->r_path2);
461 put_request_session(req);
Yehuda Sadeh37151662010-06-17 16:16:12 -0700462 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
Sage Weil153c8e62009-12-07 12:31:09 -0800463 kfree(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700464}
465
466/*
467 * lookup session, bump ref if found.
468 *
469 * called under mdsc->mutex.
470 */
471static struct ceph_mds_request *__lookup_request(struct ceph_mds_client *mdsc,
472 u64 tid)
473{
474 struct ceph_mds_request *req;
Sage Weil44ca18f2010-02-15 12:08:46 -0800475 struct rb_node *n = mdsc->request_tree.rb_node;
476
477 while (n) {
478 req = rb_entry(n, struct ceph_mds_request, r_node);
479 if (tid < req->r_tid)
480 n = n->rb_left;
481 else if (tid > req->r_tid)
482 n = n->rb_right;
483 else {
484 ceph_mdsc_get_request(req);
485 return req;
486 }
487 }
488 return NULL;
489}
490
491static void __insert_request(struct ceph_mds_client *mdsc,
492 struct ceph_mds_request *new)
493{
494 struct rb_node **p = &mdsc->request_tree.rb_node;
495 struct rb_node *parent = NULL;
496 struct ceph_mds_request *req = NULL;
497
498 while (*p) {
499 parent = *p;
500 req = rb_entry(parent, struct ceph_mds_request, r_node);
501 if (new->r_tid < req->r_tid)
502 p = &(*p)->rb_left;
503 else if (new->r_tid > req->r_tid)
504 p = &(*p)->rb_right;
505 else
506 BUG();
507 }
508
509 rb_link_node(&new->r_node, parent, p);
510 rb_insert_color(&new->r_node, &mdsc->request_tree);
Sage Weil2f2dc052009-10-06 11:31:09 -0700511}
512
513/*
514 * Register an in-flight request, and assign a tid. Link to directory
515 * are modifying (if any).
516 *
517 * Called under mdsc->mutex.
518 */
519static void __register_request(struct ceph_mds_client *mdsc,
520 struct ceph_mds_request *req,
521 struct inode *dir)
522{
523 req->r_tid = ++mdsc->last_tid;
524 if (req->r_num_caps)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700525 ceph_reserve_caps(mdsc, &req->r_caps_reservation,
526 req->r_num_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -0700527 dout("__register_request %p tid %lld\n", req, req->r_tid);
528 ceph_mdsc_get_request(req);
Sage Weil44ca18f2010-02-15 12:08:46 -0800529 __insert_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700530
531 if (dir) {
532 struct ceph_inode_info *ci = ceph_inode(dir);
533
534 spin_lock(&ci->i_unsafe_lock);
535 req->r_unsafe_dir = dir;
536 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
537 spin_unlock(&ci->i_unsafe_lock);
538 }
539}
540
541static void __unregister_request(struct ceph_mds_client *mdsc,
542 struct ceph_mds_request *req)
543{
544 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
Sage Weil44ca18f2010-02-15 12:08:46 -0800545 rb_erase(&req->r_node, &mdsc->request_tree);
Sage Weil80fc7312010-03-16 15:28:54 -0700546 RB_CLEAR_NODE(&req->r_node);
Sage Weil2f2dc052009-10-06 11:31:09 -0700547
548 if (req->r_unsafe_dir) {
549 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
550
551 spin_lock(&ci->i_unsafe_lock);
552 list_del_init(&req->r_unsafe_dir_item);
553 spin_unlock(&ci->i_unsafe_lock);
554 }
Sage Weil94aa8ae2010-03-28 21:22:50 -0700555
556 ceph_mdsc_put_request(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700557}
558
559/*
560 * Choose mds to send request to next. If there is a hint set in the
561 * request (e.g., due to a prior forward hint from the mds), use that.
562 * Otherwise, consult frag tree and/or caps to identify the
563 * appropriate mds. If all else fails, choose randomly.
564 *
565 * Called under mdsc->mutex.
566 */
Sage Weileb6bb1c2010-08-16 09:21:27 -0700567struct dentry *get_nonsnap_parent(struct dentry *dentry)
568{
569 while (!IS_ROOT(dentry) && ceph_snap(dentry->d_inode) != CEPH_NOSNAP)
570 dentry = dentry->d_parent;
571 return dentry;
572}
573
Sage Weil2f2dc052009-10-06 11:31:09 -0700574static int __choose_mds(struct ceph_mds_client *mdsc,
575 struct ceph_mds_request *req)
576{
577 struct inode *inode;
578 struct ceph_inode_info *ci;
579 struct ceph_cap *cap;
580 int mode = req->r_direct_mode;
581 int mds = -1;
582 u32 hash = req->r_direct_hash;
583 bool is_hash = req->r_direct_is_hash;
584
585 /*
586 * is there a specific mds we should try? ignore hint if we have
587 * no session and the mds is not up (active or recovering).
588 */
589 if (req->r_resend_mds >= 0 &&
590 (__have_session(mdsc, req->r_resend_mds) ||
591 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
592 dout("choose_mds using resend_mds mds%d\n",
593 req->r_resend_mds);
594 return req->r_resend_mds;
595 }
596
597 if (mode == USE_RANDOM_MDS)
598 goto random;
599
600 inode = NULL;
601 if (req->r_inode) {
602 inode = req->r_inode;
603 } else if (req->r_dentry) {
Sage Weileb6bb1c2010-08-16 09:21:27 -0700604 struct inode *dir = req->r_dentry->d_parent->d_inode;
605
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700606 if (dir->i_sb != mdsc->fsc->sb) {
Sage Weileb6bb1c2010-08-16 09:21:27 -0700607 /* not this fs! */
608 inode = req->r_dentry->d_inode;
609 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
610 /* direct snapped/virtual snapdir requests
611 * based on parent dir inode */
612 struct dentry *dn =
613 get_nonsnap_parent(req->r_dentry->d_parent);
614 inode = dn->d_inode;
615 dout("__choose_mds using nonsnap parent %p\n", inode);
616 } else if (req->r_dentry->d_inode) {
617 /* dentry target */
Sage Weil2f2dc052009-10-06 11:31:09 -0700618 inode = req->r_dentry->d_inode;
619 } else {
Sage Weileb6bb1c2010-08-16 09:21:27 -0700620 /* dir + name */
621 inode = dir;
Sage Weil2f2dc052009-10-06 11:31:09 -0700622 hash = req->r_dentry->d_name.hash;
623 is_hash = true;
624 }
625 }
Sage Weileb6bb1c2010-08-16 09:21:27 -0700626
Sage Weil2f2dc052009-10-06 11:31:09 -0700627 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
628 (int)hash, mode);
629 if (!inode)
630 goto random;
631 ci = ceph_inode(inode);
632
633 if (is_hash && S_ISDIR(inode->i_mode)) {
634 struct ceph_inode_frag frag;
635 int found;
636
637 ceph_choose_frag(ci, hash, &frag, &found);
638 if (found) {
639 if (mode == USE_ANY_MDS && frag.ndist > 0) {
640 u8 r;
641
642 /* choose a random replica */
643 get_random_bytes(&r, 1);
644 r %= frag.ndist;
645 mds = frag.dist[r];
646 dout("choose_mds %p %llx.%llx "
647 "frag %u mds%d (%d/%d)\n",
648 inode, ceph_vinop(inode),
649 frag.frag, frag.mds,
650 (int)r, frag.ndist);
651 return mds;
652 }
653
654 /* since this file/dir wasn't known to be
655 * replicated, then we want to look for the
656 * authoritative mds. */
657 mode = USE_AUTH_MDS;
658 if (frag.mds >= 0) {
659 /* choose auth mds */
660 mds = frag.mds;
661 dout("choose_mds %p %llx.%llx "
662 "frag %u mds%d (auth)\n",
663 inode, ceph_vinop(inode), frag.frag, mds);
664 return mds;
665 }
666 }
667 }
668
669 spin_lock(&inode->i_lock);
670 cap = NULL;
671 if (mode == USE_AUTH_MDS)
672 cap = ci->i_auth_cap;
673 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
674 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
675 if (!cap) {
676 spin_unlock(&inode->i_lock);
677 goto random;
678 }
679 mds = cap->session->s_mds;
680 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
681 inode, ceph_vinop(inode), mds,
682 cap == ci->i_auth_cap ? "auth " : "", cap);
683 spin_unlock(&inode->i_lock);
684 return mds;
685
686random:
687 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
688 dout("choose_mds chose random mds%d\n", mds);
689 return mds;
690}
691
692
693/*
694 * session messages
695 */
696static struct ceph_msg *create_session_msg(u32 op, u64 seq)
697{
698 struct ceph_msg *msg;
699 struct ceph_mds_session_head *h;
700
Yehuda Sadeh34d23762010-04-06 14:33:58 -0700701 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -0700702 if (!msg) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700703 pr_err("create_session_msg ENOMEM creating msg\n");
Sage Weila79832f2010-04-01 16:06:19 -0700704 return NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700705 }
706 h = msg->front.iov_base;
707 h->op = cpu_to_le32(op);
708 h->seq = cpu_to_le64(seq);
709 return msg;
710}
711
712/*
713 * send session open request.
714 *
715 * called under mdsc->mutex
716 */
717static int __open_session(struct ceph_mds_client *mdsc,
718 struct ceph_mds_session *session)
719{
720 struct ceph_msg *msg;
721 int mstate;
722 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -0700723
724 /* wait for mds to go active? */
725 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
726 dout("open_session to mds%d (%s)\n", mds,
727 ceph_mds_state_name(mstate));
728 session->s_state = CEPH_MDS_SESSION_OPENING;
729 session->s_renew_requested = jiffies;
730
731 /* send connect message */
732 msg = create_session_msg(CEPH_SESSION_REQUEST_OPEN, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -0700733 if (!msg)
734 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -0700735 ceph_con_send(&session->s_con, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -0700736 return 0;
737}
738
739/*
Sage Weiled0552a2010-06-21 13:38:25 -0700740 * open sessions for any export targets for the given mds
741 *
742 * called under mdsc->mutex
743 */
744static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
745 struct ceph_mds_session *session)
746{
747 struct ceph_mds_info *mi;
748 struct ceph_mds_session *ts;
749 int i, mds = session->s_mds;
750 int target;
751
752 if (mds >= mdsc->mdsmap->m_max_mds)
753 return;
754 mi = &mdsc->mdsmap->m_info[mds];
755 dout("open_export_target_sessions for mds%d (%d targets)\n",
756 session->s_mds, mi->num_export_targets);
757
758 for (i = 0; i < mi->num_export_targets; i++) {
759 target = mi->export_targets[i];
760 ts = __ceph_lookup_mds_session(mdsc, target);
761 if (!ts) {
762 ts = register_session(mdsc, target);
763 if (IS_ERR(ts))
764 return;
765 }
766 if (session->s_state == CEPH_MDS_SESSION_NEW ||
767 session->s_state == CEPH_MDS_SESSION_CLOSING)
768 __open_session(mdsc, session);
769 else
770 dout(" mds%d target mds%d %p is %s\n", session->s_mds,
771 i, ts, session_state_name(ts->s_state));
772 ceph_put_mds_session(ts);
773 }
774}
775
Sage Weil154f42c2010-06-21 13:45:04 -0700776void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
777 struct ceph_mds_session *session)
778{
779 mutex_lock(&mdsc->mutex);
780 __open_export_target_sessions(mdsc, session);
781 mutex_unlock(&mdsc->mutex);
782}
783
Sage Weiled0552a2010-06-21 13:38:25 -0700784/*
Sage Weil2f2dc052009-10-06 11:31:09 -0700785 * session caps
786 */
787
788/*
789 * Free preallocated cap messages assigned to this session
790 */
791static void cleanup_cap_releases(struct ceph_mds_session *session)
792{
793 struct ceph_msg *msg;
794
795 spin_lock(&session->s_cap_lock);
796 while (!list_empty(&session->s_cap_releases)) {
797 msg = list_first_entry(&session->s_cap_releases,
798 struct ceph_msg, list_head);
799 list_del_init(&msg->list_head);
800 ceph_msg_put(msg);
801 }
802 while (!list_empty(&session->s_cap_releases_done)) {
803 msg = list_first_entry(&session->s_cap_releases_done,
804 struct ceph_msg, list_head);
805 list_del_init(&msg->list_head);
806 ceph_msg_put(msg);
807 }
808 spin_unlock(&session->s_cap_lock);
809}
810
811/*
Sage Weilf818a732010-05-11 20:56:31 -0700812 * Helper to safely iterate over all caps associated with a session, with
813 * special care taken to handle a racing __ceph_remove_cap().
Sage Weil2f2dc052009-10-06 11:31:09 -0700814 *
Sage Weilf818a732010-05-11 20:56:31 -0700815 * Caller must hold session s_mutex.
Sage Weil2f2dc052009-10-06 11:31:09 -0700816 */
817static int iterate_session_caps(struct ceph_mds_session *session,
818 int (*cb)(struct inode *, struct ceph_cap *,
819 void *), void *arg)
820{
Sage Weil7c1332b2010-02-16 11:39:45 -0800821 struct list_head *p;
822 struct ceph_cap *cap;
823 struct inode *inode, *last_inode = NULL;
824 struct ceph_cap *old_cap = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700825 int ret;
826
827 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
828 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800829 p = session->s_caps.next;
830 while (p != &session->s_caps) {
831 cap = list_entry(p, struct ceph_cap, session_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -0700832 inode = igrab(&cap->ci->vfs_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -0800833 if (!inode) {
834 p = p->next;
Sage Weil2f2dc052009-10-06 11:31:09 -0700835 continue;
Sage Weil7c1332b2010-02-16 11:39:45 -0800836 }
837 session->s_cap_iterator = cap;
Sage Weil2f2dc052009-10-06 11:31:09 -0700838 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800839
840 if (last_inode) {
841 iput(last_inode);
842 last_inode = NULL;
843 }
844 if (old_cap) {
Yehuda Sadeh37151662010-06-17 16:16:12 -0700845 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -0800846 old_cap = NULL;
847 }
848
Sage Weil2f2dc052009-10-06 11:31:09 -0700849 ret = cb(inode, cap, arg);
Sage Weil7c1332b2010-02-16 11:39:45 -0800850 last_inode = inode;
851
Sage Weil2f2dc052009-10-06 11:31:09 -0700852 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800853 p = p->next;
854 if (cap->ci == NULL) {
855 dout("iterate_session_caps finishing cap %p removal\n",
856 cap);
857 BUG_ON(cap->session != session);
858 list_del_init(&cap->session_caps);
859 session->s_nr_caps--;
860 cap->session = NULL;
861 old_cap = cap; /* put_cap it w/o locks held */
862 }
Sage Weil5dacf092009-12-21 20:40:34 -0800863 if (ret < 0)
864 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -0700865 }
Sage Weil5dacf092009-12-21 20:40:34 -0800866 ret = 0;
867out:
Sage Weil7c1332b2010-02-16 11:39:45 -0800868 session->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700869 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800870
871 if (last_inode)
872 iput(last_inode);
873 if (old_cap)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700874 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -0800875
Sage Weil5dacf092009-12-21 20:40:34 -0800876 return ret;
Sage Weil2f2dc052009-10-06 11:31:09 -0700877}
878
879static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
Sage Weil6c99f252010-05-10 16:12:25 -0700880 void *arg)
Sage Weil2f2dc052009-10-06 11:31:09 -0700881{
882 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil6c99f252010-05-10 16:12:25 -0700883 int drop = 0;
884
Sage Weil2f2dc052009-10-06 11:31:09 -0700885 dout("removing cap %p, ci is %p, inode is %p\n",
886 cap, ci, &ci->vfs_inode);
Sage Weil6c99f252010-05-10 16:12:25 -0700887 spin_lock(&inode->i_lock);
888 __ceph_remove_cap(cap);
889 if (!__ceph_is_any_real_caps(ci)) {
890 struct ceph_mds_client *mdsc =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700891 ceph_sb_to_client(inode->i_sb)->mdsc;
Sage Weil6c99f252010-05-10 16:12:25 -0700892
893 spin_lock(&mdsc->cap_dirty_lock);
894 if (!list_empty(&ci->i_dirty_item)) {
895 pr_info(" dropping dirty %s state for %p %lld\n",
896 ceph_cap_string(ci->i_dirty_caps),
897 inode, ceph_ino(inode));
898 ci->i_dirty_caps = 0;
899 list_del_init(&ci->i_dirty_item);
900 drop = 1;
901 }
902 if (!list_empty(&ci->i_flushing_item)) {
903 pr_info(" dropping dirty+flushing %s state for %p %lld\n",
904 ceph_cap_string(ci->i_flushing_caps),
905 inode, ceph_ino(inode));
906 ci->i_flushing_caps = 0;
907 list_del_init(&ci->i_flushing_item);
908 mdsc->num_cap_flushing--;
909 drop = 1;
910 }
911 if (drop && ci->i_wrbuffer_ref) {
912 pr_info(" dropping dirty data for %p %lld\n",
913 inode, ceph_ino(inode));
914 ci->i_wrbuffer_ref = 0;
915 ci->i_wrbuffer_ref_head = 0;
916 drop++;
917 }
918 spin_unlock(&mdsc->cap_dirty_lock);
919 }
920 spin_unlock(&inode->i_lock);
921 while (drop--)
922 iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -0700923 return 0;
924}
925
926/*
927 * caller must hold session s_mutex
928 */
929static void remove_session_caps(struct ceph_mds_session *session)
930{
931 dout("remove_session_caps on %p\n", session);
932 iterate_session_caps(session, remove_session_caps_cb, NULL);
933 BUG_ON(session->s_nr_caps > 0);
Sage Weil6c99f252010-05-10 16:12:25 -0700934 BUG_ON(!list_empty(&session->s_cap_flushing));
Sage Weil2f2dc052009-10-06 11:31:09 -0700935 cleanup_cap_releases(session);
936}
937
938/*
939 * wake up any threads waiting on this session's caps. if the cap is
940 * old (didn't get renewed on the client reconnect), remove it now.
941 *
942 * caller must hold s_mutex.
943 */
944static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
945 void *arg)
946{
Sage Weil0dc25702009-11-20 13:43:45 -0800947 struct ceph_inode_info *ci = ceph_inode(inode);
948
Yehuda Sadeh03066f22010-07-27 13:11:08 -0700949 wake_up_all(&ci->i_cap_wq);
Sage Weil0dc25702009-11-20 13:43:45 -0800950 if (arg) {
951 spin_lock(&inode->i_lock);
952 ci->i_wanted_max_size = 0;
953 ci->i_requested_max_size = 0;
954 spin_unlock(&inode->i_lock);
955 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700956 return 0;
957}
958
Sage Weil0dc25702009-11-20 13:43:45 -0800959static void wake_up_session_caps(struct ceph_mds_session *session,
960 int reconnect)
Sage Weil2f2dc052009-10-06 11:31:09 -0700961{
962 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
Sage Weil0dc25702009-11-20 13:43:45 -0800963 iterate_session_caps(session, wake_up_session_cb,
964 (void *)(unsigned long)reconnect);
Sage Weil2f2dc052009-10-06 11:31:09 -0700965}
966
967/*
968 * Send periodic message to MDS renewing all currently held caps. The
969 * ack will reset the expiration for all caps from this session.
970 *
971 * caller holds s_mutex
972 */
973static int send_renew_caps(struct ceph_mds_client *mdsc,
974 struct ceph_mds_session *session)
975{
976 struct ceph_msg *msg;
977 int state;
978
979 if (time_after_eq(jiffies, session->s_cap_ttl) &&
980 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
981 pr_info("mds%d caps stale\n", session->s_mds);
Sage Weile4cb4cb2010-03-18 13:43:09 -0700982 session->s_renew_requested = jiffies;
Sage Weil2f2dc052009-10-06 11:31:09 -0700983
984 /* do not try to renew caps until a recovering mds has reconnected
985 * with its clients. */
986 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
987 if (state < CEPH_MDS_STATE_RECONNECT) {
988 dout("send_renew_caps ignoring mds%d (%s)\n",
989 session->s_mds, ceph_mds_state_name(state));
990 return 0;
991 }
992
993 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
994 ceph_mds_state_name(state));
Sage Weil2f2dc052009-10-06 11:31:09 -0700995 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
996 ++session->s_renew_seq);
Sage Weila79832f2010-04-01 16:06:19 -0700997 if (!msg)
998 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -0700999 ceph_con_send(&session->s_con, msg);
1000 return 0;
1001}
1002
1003/*
1004 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
Sage Weil0dc25702009-11-20 13:43:45 -08001005 *
1006 * Called under session->s_mutex
Sage Weil2f2dc052009-10-06 11:31:09 -07001007 */
1008static void renewed_caps(struct ceph_mds_client *mdsc,
1009 struct ceph_mds_session *session, int is_renew)
1010{
1011 int was_stale;
1012 int wake = 0;
1013
1014 spin_lock(&session->s_cap_lock);
1015 was_stale = is_renew && (session->s_cap_ttl == 0 ||
1016 time_after_eq(jiffies, session->s_cap_ttl));
1017
1018 session->s_cap_ttl = session->s_renew_requested +
1019 mdsc->mdsmap->m_session_timeout*HZ;
1020
1021 if (was_stale) {
1022 if (time_before(jiffies, session->s_cap_ttl)) {
1023 pr_info("mds%d caps renewed\n", session->s_mds);
1024 wake = 1;
1025 } else {
1026 pr_info("mds%d caps still stale\n", session->s_mds);
1027 }
1028 }
1029 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1030 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1031 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1032 spin_unlock(&session->s_cap_lock);
1033
1034 if (wake)
Sage Weil0dc25702009-11-20 13:43:45 -08001035 wake_up_session_caps(session, 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07001036}
1037
1038/*
1039 * send a session close request
1040 */
1041static int request_close_session(struct ceph_mds_client *mdsc,
1042 struct ceph_mds_session *session)
1043{
1044 struct ceph_msg *msg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001045
1046 dout("request_close_session mds%d state %s seq %lld\n",
1047 session->s_mds, session_state_name(session->s_state),
1048 session->s_seq);
1049 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001050 if (!msg)
1051 return -ENOMEM;
1052 ceph_con_send(&session->s_con, msg);
1053 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001054}
1055
1056/*
1057 * Called with s_mutex held.
1058 */
1059static int __close_session(struct ceph_mds_client *mdsc,
1060 struct ceph_mds_session *session)
1061{
1062 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1063 return 0;
1064 session->s_state = CEPH_MDS_SESSION_CLOSING;
1065 return request_close_session(mdsc, session);
1066}
1067
1068/*
1069 * Trim old(er) caps.
1070 *
1071 * Because we can't cache an inode without one or more caps, we do
1072 * this indirectly: if a cap is unused, we prune its aliases, at which
1073 * point the inode will hopefully get dropped to.
1074 *
1075 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1076 * memory pressure from the MDS, though, so it needn't be perfect.
1077 */
1078static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1079{
1080 struct ceph_mds_session *session = arg;
1081 struct ceph_inode_info *ci = ceph_inode(inode);
1082 int used, oissued, mine;
1083
1084 if (session->s_trim_caps <= 0)
1085 return -1;
1086
1087 spin_lock(&inode->i_lock);
1088 mine = cap->issued | cap->implemented;
1089 used = __ceph_caps_used(ci);
1090 oissued = __ceph_caps_issued_other(ci, cap);
1091
1092 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n",
1093 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
1094 ceph_cap_string(used));
1095 if (ci->i_dirty_caps)
1096 goto out; /* dirty caps */
1097 if ((used & ~oissued) & mine)
1098 goto out; /* we need these caps */
1099
1100 session->s_trim_caps--;
1101 if (oissued) {
1102 /* we aren't the only cap.. just remove us */
Sage Weil7c1332b2010-02-16 11:39:45 -08001103 __ceph_remove_cap(cap);
Sage Weil2f2dc052009-10-06 11:31:09 -07001104 } else {
1105 /* try to drop referring dentries */
1106 spin_unlock(&inode->i_lock);
1107 d_prune_aliases(inode);
1108 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1109 inode, cap, atomic_read(&inode->i_count));
1110 return 0;
1111 }
1112
1113out:
1114 spin_unlock(&inode->i_lock);
1115 return 0;
1116}
1117
1118/*
1119 * Trim session cap count down to some max number.
1120 */
1121static int trim_caps(struct ceph_mds_client *mdsc,
1122 struct ceph_mds_session *session,
1123 int max_caps)
1124{
1125 int trim_caps = session->s_nr_caps - max_caps;
1126
1127 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1128 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1129 if (trim_caps > 0) {
1130 session->s_trim_caps = trim_caps;
1131 iterate_session_caps(session, trim_caps_cb, session);
1132 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1133 session->s_mds, session->s_nr_caps, max_caps,
1134 trim_caps - session->s_trim_caps);
Sage Weil5dacf092009-12-21 20:40:34 -08001135 session->s_trim_caps = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001136 }
1137 return 0;
1138}
1139
1140/*
1141 * Allocate cap_release messages. If there is a partially full message
1142 * in the queue, try to allocate enough to cover it's remainder, so that
1143 * we can send it immediately.
1144 *
1145 * Called under s_mutex.
1146 */
Sage Weil2b2300d2010-06-09 16:52:04 -07001147int ceph_add_cap_releases(struct ceph_mds_client *mdsc,
Sage Weilee6b2722010-06-10 12:55:52 -07001148 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001149{
Sage Weil38e8883e2010-06-10 12:57:11 -07001150 struct ceph_msg *msg, *partial = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001151 struct ceph_mds_cap_release *head;
1152 int err = -ENOMEM;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001153 int extra = mdsc->fsc->mount_options->cap_release_safety;
Sage Weil38e8883e2010-06-10 12:57:11 -07001154 int num;
Sage Weil2f2dc052009-10-06 11:31:09 -07001155
Sage Weil38e8883e2010-06-10 12:57:11 -07001156 dout("add_cap_releases %p mds%d extra %d\n", session, session->s_mds,
1157 extra);
Sage Weil2f2dc052009-10-06 11:31:09 -07001158
1159 spin_lock(&session->s_cap_lock);
1160
1161 if (!list_empty(&session->s_cap_releases)) {
1162 msg = list_first_entry(&session->s_cap_releases,
1163 struct ceph_msg,
1164 list_head);
1165 head = msg->front.iov_base;
Sage Weil38e8883e2010-06-10 12:57:11 -07001166 num = le32_to_cpu(head->num);
1167 if (num) {
1168 dout(" partial %p with (%d/%d)\n", msg, num,
1169 (int)CEPH_CAPS_PER_RELEASE);
1170 extra += CEPH_CAPS_PER_RELEASE - num;
1171 partial = msg;
1172 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001173 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001174 while (session->s_num_cap_releases < session->s_nr_caps + extra) {
1175 spin_unlock(&session->s_cap_lock);
Yehuda Sadeh34d23762010-04-06 14:33:58 -07001176 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE,
1177 GFP_NOFS);
Sage Weil2f2dc052009-10-06 11:31:09 -07001178 if (!msg)
1179 goto out_unlocked;
1180 dout("add_cap_releases %p msg %p now %d\n", session, msg,
1181 (int)msg->front.iov_len);
1182 head = msg->front.iov_base;
1183 head->num = cpu_to_le32(0);
1184 msg->front.iov_len = sizeof(*head);
1185 spin_lock(&session->s_cap_lock);
1186 list_add(&msg->list_head, &session->s_cap_releases);
1187 session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
1188 }
1189
Sage Weil38e8883e2010-06-10 12:57:11 -07001190 if (partial) {
1191 head = partial->front.iov_base;
1192 num = le32_to_cpu(head->num);
1193 dout(" queueing partial %p with %d/%d\n", partial, num,
1194 (int)CEPH_CAPS_PER_RELEASE);
1195 list_move_tail(&partial->list_head,
1196 &session->s_cap_releases_done);
1197 session->s_num_cap_releases -= CEPH_CAPS_PER_RELEASE - num;
Sage Weil2f2dc052009-10-06 11:31:09 -07001198 }
1199 err = 0;
1200 spin_unlock(&session->s_cap_lock);
1201out_unlocked:
1202 return err;
1203}
1204
1205/*
1206 * flush all dirty inode data to disk.
1207 *
1208 * returns true if we've flushed through want_flush_seq
1209 */
1210static int check_cap_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
1211{
1212 int mds, ret = 1;
1213
1214 dout("check_cap_flush want %lld\n", want_flush_seq);
1215 mutex_lock(&mdsc->mutex);
1216 for (mds = 0; ret && mds < mdsc->max_sessions; mds++) {
1217 struct ceph_mds_session *session = mdsc->sessions[mds];
1218
1219 if (!session)
1220 continue;
1221 get_session(session);
1222 mutex_unlock(&mdsc->mutex);
1223
1224 mutex_lock(&session->s_mutex);
1225 if (!list_empty(&session->s_cap_flushing)) {
1226 struct ceph_inode_info *ci =
1227 list_entry(session->s_cap_flushing.next,
1228 struct ceph_inode_info,
1229 i_flushing_item);
1230 struct inode *inode = &ci->vfs_inode;
1231
1232 spin_lock(&inode->i_lock);
1233 if (ci->i_cap_flush_seq <= want_flush_seq) {
1234 dout("check_cap_flush still flushing %p "
1235 "seq %lld <= %lld to mds%d\n", inode,
1236 ci->i_cap_flush_seq, want_flush_seq,
1237 session->s_mds);
1238 ret = 0;
1239 }
1240 spin_unlock(&inode->i_lock);
1241 }
1242 mutex_unlock(&session->s_mutex);
1243 ceph_put_mds_session(session);
1244
1245 if (!ret)
1246 return ret;
1247 mutex_lock(&mdsc->mutex);
1248 }
1249
1250 mutex_unlock(&mdsc->mutex);
1251 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
1252 return ret;
1253}
1254
1255/*
1256 * called under s_mutex
1257 */
Sage Weil3d7ded42010-06-09 16:47:10 -07001258void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1259 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001260{
1261 struct ceph_msg *msg;
1262
1263 dout("send_cap_releases mds%d\n", session->s_mds);
Sage Weil0f8605f2010-05-05 15:51:35 -07001264 spin_lock(&session->s_cap_lock);
1265 while (!list_empty(&session->s_cap_releases_done)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001266 msg = list_first_entry(&session->s_cap_releases_done,
1267 struct ceph_msg, list_head);
1268 list_del_init(&msg->list_head);
1269 spin_unlock(&session->s_cap_lock);
1270 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1271 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1272 ceph_con_send(&session->s_con, msg);
Sage Weil0f8605f2010-05-05 15:51:35 -07001273 spin_lock(&session->s_cap_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001274 }
1275 spin_unlock(&session->s_cap_lock);
1276}
1277
Sage Weile01a5942010-05-10 15:36:44 -07001278static void discard_cap_releases(struct ceph_mds_client *mdsc,
1279 struct ceph_mds_session *session)
1280{
1281 struct ceph_msg *msg;
1282 struct ceph_mds_cap_release *head;
1283 unsigned num;
1284
1285 dout("discard_cap_releases mds%d\n", session->s_mds);
1286 spin_lock(&session->s_cap_lock);
1287
1288 /* zero out the in-progress message */
1289 msg = list_first_entry(&session->s_cap_releases,
1290 struct ceph_msg, list_head);
1291 head = msg->front.iov_base;
1292 num = le32_to_cpu(head->num);
1293 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg, num);
1294 head->num = cpu_to_le32(0);
1295 session->s_num_cap_releases += num;
1296
1297 /* requeue completed messages */
1298 while (!list_empty(&session->s_cap_releases_done)) {
1299 msg = list_first_entry(&session->s_cap_releases_done,
1300 struct ceph_msg, list_head);
1301 list_del_init(&msg->list_head);
1302
1303 head = msg->front.iov_base;
1304 num = le32_to_cpu(head->num);
1305 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg,
1306 num);
1307 session->s_num_cap_releases += num;
1308 head->num = cpu_to_le32(0);
1309 msg->front.iov_len = sizeof(*head);
1310 list_add(&msg->list_head, &session->s_cap_releases);
1311 }
1312
1313 spin_unlock(&session->s_cap_lock);
1314}
1315
Sage Weil2f2dc052009-10-06 11:31:09 -07001316/*
1317 * requests
1318 */
1319
1320/*
1321 * Create an mds request.
1322 */
1323struct ceph_mds_request *
1324ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1325{
1326 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1327
1328 if (!req)
1329 return ERR_PTR(-ENOMEM);
1330
Sage Weilb4556392010-05-13 12:01:13 -07001331 mutex_init(&req->r_fill_mutex);
Yehuda Sadeh37151662010-06-17 16:16:12 -07001332 req->r_mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07001333 req->r_started = jiffies;
1334 req->r_resend_mds = -1;
1335 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1336 req->r_fmode = -1;
Sage Weil153c8e62009-12-07 12:31:09 -08001337 kref_init(&req->r_kref);
Sage Weil2f2dc052009-10-06 11:31:09 -07001338 INIT_LIST_HEAD(&req->r_wait);
1339 init_completion(&req->r_completion);
1340 init_completion(&req->r_safe_completion);
1341 INIT_LIST_HEAD(&req->r_unsafe_item);
1342
1343 req->r_op = op;
1344 req->r_direct_mode = mode;
1345 return req;
1346}
1347
1348/*
Sage Weil44ca18f2010-02-15 12:08:46 -08001349 * return oldest (lowest) request, tid in request tree, 0 if none.
Sage Weil2f2dc052009-10-06 11:31:09 -07001350 *
1351 * called under mdsc->mutex.
1352 */
Sage Weil44ca18f2010-02-15 12:08:46 -08001353static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1354{
1355 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1356 return NULL;
1357 return rb_entry(rb_first(&mdsc->request_tree),
1358 struct ceph_mds_request, r_node);
1359}
1360
Sage Weil2f2dc052009-10-06 11:31:09 -07001361static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1362{
Sage Weil44ca18f2010-02-15 12:08:46 -08001363 struct ceph_mds_request *req = __get_oldest_req(mdsc);
1364
1365 if (req)
1366 return req->r_tid;
1367 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001368}
1369
1370/*
1371 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1372 * on build_path_from_dentry in fs/cifs/dir.c.
1373 *
1374 * If @stop_on_nosnap, generate path relative to the first non-snapped
1375 * inode.
1376 *
1377 * Encode hidden .snap dirs as a double /, i.e.
1378 * foo/.snap/bar -> foo//bar
1379 */
1380char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1381 int stop_on_nosnap)
1382{
1383 struct dentry *temp;
1384 char *path;
1385 int len, pos;
1386
1387 if (dentry == NULL)
1388 return ERR_PTR(-EINVAL);
1389
1390retry:
1391 len = 0;
1392 for (temp = dentry; !IS_ROOT(temp);) {
1393 struct inode *inode = temp->d_inode;
1394 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1395 len++; /* slash only */
1396 else if (stop_on_nosnap && inode &&
1397 ceph_snap(inode) == CEPH_NOSNAP)
1398 break;
1399 else
1400 len += 1 + temp->d_name.len;
1401 temp = temp->d_parent;
1402 if (temp == NULL) {
Sage Weil6c99f252010-05-10 16:12:25 -07001403 pr_err("build_path corrupt dentry %p\n", dentry);
Sage Weil2f2dc052009-10-06 11:31:09 -07001404 return ERR_PTR(-EINVAL);
1405 }
1406 }
1407 if (len)
1408 len--; /* no leading '/' */
1409
1410 path = kmalloc(len+1, GFP_NOFS);
1411 if (path == NULL)
1412 return ERR_PTR(-ENOMEM);
1413 pos = len;
1414 path[pos] = 0; /* trailing null */
1415 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
1416 struct inode *inode = temp->d_inode;
1417
1418 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
Sage Weil104648a2010-03-18 10:14:30 -07001419 dout("build_path path+%d: %p SNAPDIR\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001420 pos, temp);
1421 } else if (stop_on_nosnap && inode &&
1422 ceph_snap(inode) == CEPH_NOSNAP) {
1423 break;
1424 } else {
1425 pos -= temp->d_name.len;
1426 if (pos < 0)
1427 break;
1428 strncpy(path + pos, temp->d_name.name,
1429 temp->d_name.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07001430 }
1431 if (pos)
1432 path[--pos] = '/';
1433 temp = temp->d_parent;
1434 if (temp == NULL) {
Sage Weil104648a2010-03-18 10:14:30 -07001435 pr_err("build_path corrupt dentry\n");
Sage Weil2f2dc052009-10-06 11:31:09 -07001436 kfree(path);
1437 return ERR_PTR(-EINVAL);
1438 }
1439 }
1440 if (pos != 0) {
Sage Weil104648a2010-03-18 10:14:30 -07001441 pr_err("build_path did not end path lookup where "
Sage Weil2f2dc052009-10-06 11:31:09 -07001442 "expected, namelen is %d, pos is %d\n", len, pos);
1443 /* presumably this is only possible if racing with a
1444 rename of one of the parent directories (we can not
1445 lock the dentries above us to prevent this, but
1446 retrying should be harmless) */
1447 kfree(path);
1448 goto retry;
1449 }
1450
1451 *base = ceph_ino(temp->d_inode);
1452 *plen = len;
Sage Weil104648a2010-03-18 10:14:30 -07001453 dout("build_path on %p %d built %llx '%.*s'\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001454 dentry, atomic_read(&dentry->d_count), *base, len, path);
1455 return path;
1456}
1457
1458static int build_dentry_path(struct dentry *dentry,
1459 const char **ppath, int *ppathlen, u64 *pino,
1460 int *pfreepath)
1461{
1462 char *path;
1463
1464 if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) {
1465 *pino = ceph_ino(dentry->d_parent->d_inode);
1466 *ppath = dentry->d_name.name;
1467 *ppathlen = dentry->d_name.len;
1468 return 0;
1469 }
1470 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1471 if (IS_ERR(path))
1472 return PTR_ERR(path);
1473 *ppath = path;
1474 *pfreepath = 1;
1475 return 0;
1476}
1477
1478static int build_inode_path(struct inode *inode,
1479 const char **ppath, int *ppathlen, u64 *pino,
1480 int *pfreepath)
1481{
1482 struct dentry *dentry;
1483 char *path;
1484
1485 if (ceph_snap(inode) == CEPH_NOSNAP) {
1486 *pino = ceph_ino(inode);
1487 *ppathlen = 0;
1488 return 0;
1489 }
1490 dentry = d_find_alias(inode);
1491 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1492 dput(dentry);
1493 if (IS_ERR(path))
1494 return PTR_ERR(path);
1495 *ppath = path;
1496 *pfreepath = 1;
1497 return 0;
1498}
1499
1500/*
1501 * request arguments may be specified via an inode *, a dentry *, or
1502 * an explicit ino+path.
1503 */
1504static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1505 const char *rpath, u64 rino,
1506 const char **ppath, int *pathlen,
1507 u64 *ino, int *freepath)
1508{
1509 int r = 0;
1510
1511 if (rinode) {
1512 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1513 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1514 ceph_snap(rinode));
1515 } else if (rdentry) {
1516 r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
1517 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1518 *ppath);
1519 } else if (rpath) {
1520 *ino = rino;
1521 *ppath = rpath;
1522 *pathlen = strlen(rpath);
1523 dout(" path %.*s\n", *pathlen, rpath);
1524 }
1525
1526 return r;
1527}
1528
1529/*
1530 * called under mdsc->mutex
1531 */
1532static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1533 struct ceph_mds_request *req,
1534 int mds)
1535{
1536 struct ceph_msg *msg;
1537 struct ceph_mds_request_head *head;
1538 const char *path1 = NULL;
1539 const char *path2 = NULL;
1540 u64 ino1 = 0, ino2 = 0;
1541 int pathlen1 = 0, pathlen2 = 0;
1542 int freepath1 = 0, freepath2 = 0;
1543 int len;
1544 u16 releases;
1545 void *p, *end;
1546 int ret;
1547
1548 ret = set_request_path_attr(req->r_inode, req->r_dentry,
1549 req->r_path1, req->r_ino1.ino,
1550 &path1, &pathlen1, &ino1, &freepath1);
1551 if (ret < 0) {
1552 msg = ERR_PTR(ret);
1553 goto out;
1554 }
1555
1556 ret = set_request_path_attr(NULL, req->r_old_dentry,
1557 req->r_path2, req->r_ino2.ino,
1558 &path2, &pathlen2, &ino2, &freepath2);
1559 if (ret < 0) {
1560 msg = ERR_PTR(ret);
1561 goto out_free1;
1562 }
1563
1564 len = sizeof(*head) +
Sage Weilac8839d2010-01-27 14:28:10 -08001565 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64));
Sage Weil2f2dc052009-10-06 11:31:09 -07001566
1567 /* calculate (max) length for cap releases */
1568 len += sizeof(struct ceph_mds_request_release) *
1569 (!!req->r_inode_drop + !!req->r_dentry_drop +
1570 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1571 if (req->r_dentry_drop)
1572 len += req->r_dentry->d_name.len;
1573 if (req->r_old_dentry_drop)
1574 len += req->r_old_dentry->d_name.len;
1575
Yehuda Sadeh34d23762010-04-06 14:33:58 -07001576 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -07001577 if (!msg) {
1578 msg = ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -07001579 goto out_free2;
Sage Weila79832f2010-04-01 16:06:19 -07001580 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001581
Sage Weil6df058c2009-12-22 11:24:33 -08001582 msg->hdr.tid = cpu_to_le64(req->r_tid);
1583
Sage Weil2f2dc052009-10-06 11:31:09 -07001584 head = msg->front.iov_base;
1585 p = msg->front.iov_base + sizeof(*head);
1586 end = msg->front.iov_base + msg->front.iov_len;
1587
1588 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1589 head->op = cpu_to_le32(req->r_op);
1590 head->caller_uid = cpu_to_le32(current_fsuid());
1591 head->caller_gid = cpu_to_le32(current_fsgid());
1592 head->args = req->r_args;
1593
1594 ceph_encode_filepath(&p, end, ino1, path1);
1595 ceph_encode_filepath(&p, end, ino2, path2);
1596
Sage Weile979cf52010-07-15 14:58:39 -07001597 /* make note of release offset, in case we need to replay */
1598 req->r_request_release_offset = p - msg->front.iov_base;
1599
Sage Weil2f2dc052009-10-06 11:31:09 -07001600 /* cap releases */
1601 releases = 0;
1602 if (req->r_inode_drop)
1603 releases += ceph_encode_inode_release(&p,
1604 req->r_inode ? req->r_inode : req->r_dentry->d_inode,
1605 mds, req->r_inode_drop, req->r_inode_unless, 0);
1606 if (req->r_dentry_drop)
1607 releases += ceph_encode_dentry_release(&p, req->r_dentry,
1608 mds, req->r_dentry_drop, req->r_dentry_unless);
1609 if (req->r_old_dentry_drop)
1610 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
1611 mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
1612 if (req->r_old_inode_drop)
1613 releases += ceph_encode_inode_release(&p,
1614 req->r_old_dentry->d_inode,
1615 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
1616 head->num_releases = cpu_to_le16(releases);
1617
1618 BUG_ON(p > end);
1619 msg->front.iov_len = p - msg->front.iov_base;
1620 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1621
1622 msg->pages = req->r_pages;
1623 msg->nr_pages = req->r_num_pages;
1624 msg->hdr.data_len = cpu_to_le32(req->r_data_len);
1625 msg->hdr.data_off = cpu_to_le16(0);
1626
1627out_free2:
1628 if (freepath2)
1629 kfree((char *)path2);
1630out_free1:
1631 if (freepath1)
1632 kfree((char *)path1);
1633out:
1634 return msg;
1635}
1636
1637/*
1638 * called under mdsc->mutex if error, under no mutex if
1639 * success.
1640 */
1641static void complete_request(struct ceph_mds_client *mdsc,
1642 struct ceph_mds_request *req)
1643{
1644 if (req->r_callback)
1645 req->r_callback(mdsc, req);
1646 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001647 complete_all(&req->r_completion);
Sage Weil2f2dc052009-10-06 11:31:09 -07001648}
1649
1650/*
1651 * called under mdsc->mutex
1652 */
1653static int __prepare_send_request(struct ceph_mds_client *mdsc,
1654 struct ceph_mds_request *req,
1655 int mds)
1656{
1657 struct ceph_mds_request_head *rhead;
1658 struct ceph_msg *msg;
1659 int flags = 0;
1660
1661 req->r_mds = mds;
1662 req->r_attempts++;
Greg Farnume55b71f2010-06-22 15:58:01 -07001663 if (req->r_inode) {
1664 struct ceph_cap *cap =
1665 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
1666
1667 if (cap)
1668 req->r_sent_on_mseq = cap->mseq;
1669 else
1670 req->r_sent_on_mseq = -1;
1671 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001672 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
1673 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
1674
Sage Weil01a92f12010-07-15 13:24:32 -07001675 if (req->r_got_unsafe) {
1676 /*
1677 * Replay. Do not regenerate message (and rebuild
1678 * paths, etc.); just use the original message.
1679 * Rebuilding paths will break for renames because
1680 * d_move mangles the src name.
1681 */
1682 msg = req->r_request;
1683 rhead = msg->front.iov_base;
1684
1685 flags = le32_to_cpu(rhead->flags);
1686 flags |= CEPH_MDS_FLAG_REPLAY;
1687 rhead->flags = cpu_to_le32(flags);
1688
1689 if (req->r_target_inode)
1690 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
1691
1692 rhead->num_retry = req->r_attempts - 1;
Sage Weile979cf52010-07-15 14:58:39 -07001693
1694 /* remove cap/dentry releases from message */
1695 rhead->num_releases = 0;
1696 msg->hdr.front_len = cpu_to_le32(req->r_request_release_offset);
1697 msg->front.iov_len = req->r_request_release_offset;
Sage Weil01a92f12010-07-15 13:24:32 -07001698 return 0;
1699 }
1700
Sage Weil2f2dc052009-10-06 11:31:09 -07001701 if (req->r_request) {
1702 ceph_msg_put(req->r_request);
1703 req->r_request = NULL;
1704 }
1705 msg = create_request_message(mdsc, req, mds);
1706 if (IS_ERR(msg)) {
Sage Weile1518c72010-05-13 11:19:06 -07001707 req->r_err = PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001708 complete_request(mdsc, req);
Sage Weila79832f2010-04-01 16:06:19 -07001709 return PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001710 }
1711 req->r_request = msg;
1712
1713 rhead = msg->front.iov_base;
Sage Weil2f2dc052009-10-06 11:31:09 -07001714 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
1715 if (req->r_got_unsafe)
1716 flags |= CEPH_MDS_FLAG_REPLAY;
1717 if (req->r_locked_dir)
1718 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
1719 rhead->flags = cpu_to_le32(flags);
1720 rhead->num_fwd = req->r_num_fwd;
1721 rhead->num_retry = req->r_attempts - 1;
Sage Weil01a92f12010-07-15 13:24:32 -07001722 rhead->ino = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001723
1724 dout(" r_locked_dir = %p\n", req->r_locked_dir);
Sage Weil2f2dc052009-10-06 11:31:09 -07001725 return 0;
1726}
1727
1728/*
1729 * send request, or put it on the appropriate wait list.
1730 */
1731static int __do_request(struct ceph_mds_client *mdsc,
1732 struct ceph_mds_request *req)
1733{
1734 struct ceph_mds_session *session = NULL;
1735 int mds = -1;
1736 int err = -EAGAIN;
1737
Sage Weile1518c72010-05-13 11:19:06 -07001738 if (req->r_err || req->r_got_result)
Sage Weil2f2dc052009-10-06 11:31:09 -07001739 goto out;
1740
1741 if (req->r_timeout &&
1742 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
1743 dout("do_request timed out\n");
1744 err = -EIO;
1745 goto finish;
1746 }
1747
1748 mds = __choose_mds(mdsc, req);
1749 if (mds < 0 ||
1750 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
1751 dout("do_request no mds or not active, waiting for map\n");
1752 list_add(&req->r_wait, &mdsc->waiting_for_map);
1753 goto out;
1754 }
1755
1756 /* get, open session */
1757 session = __ceph_lookup_mds_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07001758 if (!session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001759 session = register_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07001760 if (IS_ERR(session)) {
1761 err = PTR_ERR(session);
1762 goto finish;
1763 }
1764 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001765 dout("do_request mds%d session %p state %s\n", mds, session,
1766 session_state_name(session->s_state));
1767 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
1768 session->s_state != CEPH_MDS_SESSION_HUNG) {
1769 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1770 session->s_state == CEPH_MDS_SESSION_CLOSING)
1771 __open_session(mdsc, session);
1772 list_add(&req->r_wait, &session->s_waiting);
1773 goto out_session;
1774 }
1775
1776 /* send request */
1777 req->r_session = get_session(session);
1778 req->r_resend_mds = -1; /* forget any previous mds hint */
1779
1780 if (req->r_request_started == 0) /* note request start time */
1781 req->r_request_started = jiffies;
1782
1783 err = __prepare_send_request(mdsc, req, mds);
1784 if (!err) {
1785 ceph_msg_get(req->r_request);
1786 ceph_con_send(&session->s_con, req->r_request);
1787 }
1788
1789out_session:
1790 ceph_put_mds_session(session);
1791out:
1792 return err;
1793
1794finish:
Sage Weile1518c72010-05-13 11:19:06 -07001795 req->r_err = err;
Sage Weil2f2dc052009-10-06 11:31:09 -07001796 complete_request(mdsc, req);
1797 goto out;
1798}
1799
1800/*
1801 * called under mdsc->mutex
1802 */
1803static void __wake_requests(struct ceph_mds_client *mdsc,
1804 struct list_head *head)
1805{
1806 struct ceph_mds_request *req, *nreq;
1807
1808 list_for_each_entry_safe(req, nreq, head, r_wait) {
1809 list_del_init(&req->r_wait);
1810 __do_request(mdsc, req);
1811 }
1812}
1813
1814/*
1815 * Wake up threads with requests pending for @mds, so that they can
Sage Weil29790f22010-03-18 14:45:05 -07001816 * resubmit their requests to a possibly different mds.
Sage Weil2f2dc052009-10-06 11:31:09 -07001817 */
Sage Weil29790f22010-03-18 14:45:05 -07001818static void kick_requests(struct ceph_mds_client *mdsc, int mds)
Sage Weil2f2dc052009-10-06 11:31:09 -07001819{
Sage Weil44ca18f2010-02-15 12:08:46 -08001820 struct ceph_mds_request *req;
1821 struct rb_node *p;
Sage Weil2f2dc052009-10-06 11:31:09 -07001822
1823 dout("kick_requests mds%d\n", mds);
Sage Weil44ca18f2010-02-15 12:08:46 -08001824 for (p = rb_first(&mdsc->request_tree); p; p = rb_next(p)) {
1825 req = rb_entry(p, struct ceph_mds_request, r_node);
1826 if (req->r_got_unsafe)
1827 continue;
1828 if (req->r_session &&
1829 req->r_session->s_mds == mds) {
1830 dout(" kicking tid %llu\n", req->r_tid);
1831 put_request_session(req);
1832 __do_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07001833 }
1834 }
1835}
1836
1837void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
1838 struct ceph_mds_request *req)
1839{
1840 dout("submit_request on %p\n", req);
1841 mutex_lock(&mdsc->mutex);
1842 __register_request(mdsc, req, NULL);
1843 __do_request(mdsc, req);
1844 mutex_unlock(&mdsc->mutex);
1845}
1846
1847/*
1848 * Synchrously perform an mds request. Take care of all of the
1849 * session setup, forwarding, retry details.
1850 */
1851int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
1852 struct inode *dir,
1853 struct ceph_mds_request *req)
1854{
1855 int err;
1856
1857 dout("do_request on %p\n", req);
1858
1859 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
1860 if (req->r_inode)
1861 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
1862 if (req->r_locked_dir)
1863 ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
1864 if (req->r_old_dentry)
1865 ceph_get_cap_refs(
1866 ceph_inode(req->r_old_dentry->d_parent->d_inode),
1867 CEPH_CAP_PIN);
1868
1869 /* issue */
1870 mutex_lock(&mdsc->mutex);
1871 __register_request(mdsc, req, dir);
1872 __do_request(mdsc, req);
1873
Sage Weile1518c72010-05-13 11:19:06 -07001874 if (req->r_err) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001875 err = req->r_err;
Sage Weile1518c72010-05-13 11:19:06 -07001876 __unregister_request(mdsc, req);
1877 dout("do_request early error %d\n", err);
1878 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07001879 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001880
Sage Weile1518c72010-05-13 11:19:06 -07001881 /* wait */
1882 mutex_unlock(&mdsc->mutex);
1883 dout("do_request waiting\n");
1884 if (req->r_timeout) {
Sage Weilaa916472010-05-24 11:15:51 -07001885 err = (long)wait_for_completion_killable_timeout(
Sage Weile1518c72010-05-13 11:19:06 -07001886 &req->r_completion, req->r_timeout);
1887 if (err == 0)
1888 err = -EIO;
1889 } else {
Sage Weilaa916472010-05-24 11:15:51 -07001890 err = wait_for_completion_killable(&req->r_completion);
Sage Weile1518c72010-05-13 11:19:06 -07001891 }
1892 dout("do_request waited, got %d\n", err);
1893 mutex_lock(&mdsc->mutex);
1894
1895 /* only abort if we didn't race with a real reply */
1896 if (req->r_got_result) {
1897 err = le32_to_cpu(req->r_reply_info.head->result);
1898 } else if (err < 0) {
1899 dout("aborted request %lld with %d\n", req->r_tid, err);
Sage Weilb4556392010-05-13 12:01:13 -07001900
1901 /*
1902 * ensure we aren't running concurrently with
1903 * ceph_fill_trace or ceph_readdir_prepopulate, which
1904 * rely on locks (dir mutex) held by our caller.
1905 */
1906 mutex_lock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07001907 req->r_err = err;
1908 req->r_aborted = true;
Sage Weilb4556392010-05-13 12:01:13 -07001909 mutex_unlock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07001910
1911 if (req->r_locked_dir &&
Sage Weil167c9e32010-05-14 10:02:57 -07001912 (req->r_op & CEPH_MDS_OP_WRITE))
1913 ceph_invalidate_dir_request(req);
Sage Weile1518c72010-05-13 11:19:06 -07001914 } else {
1915 err = req->r_err;
1916 }
1917
1918out:
1919 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07001920 dout("do_request %p done, result %d\n", req, err);
1921 return err;
1922}
1923
1924/*
Sage Weil167c9e32010-05-14 10:02:57 -07001925 * Invalidate dir I_COMPLETE, dentry lease state on an aborted MDS
1926 * namespace request.
1927 */
1928void ceph_invalidate_dir_request(struct ceph_mds_request *req)
1929{
1930 struct inode *inode = req->r_locked_dir;
1931 struct ceph_inode_info *ci = ceph_inode(inode);
1932
1933 dout("invalidate_dir_request %p (I_COMPLETE, lease(s))\n", inode);
1934 spin_lock(&inode->i_lock);
1935 ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
1936 ci->i_release_count++;
1937 spin_unlock(&inode->i_lock);
1938
1939 if (req->r_dentry)
1940 ceph_invalidate_dentry_lease(req->r_dentry);
1941 if (req->r_old_dentry)
1942 ceph_invalidate_dentry_lease(req->r_old_dentry);
1943}
1944
1945/*
Sage Weil2f2dc052009-10-06 11:31:09 -07001946 * Handle mds reply.
1947 *
1948 * We take the session mutex and parse and process the reply immediately.
1949 * This preserves the logical ordering of replies, capabilities, etc., sent
1950 * by the MDS as they are applied to our local cache.
1951 */
1952static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
1953{
1954 struct ceph_mds_client *mdsc = session->s_mdsc;
1955 struct ceph_mds_request *req;
1956 struct ceph_mds_reply_head *head = msg->front.iov_base;
1957 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
1958 u64 tid;
1959 int err, result;
Sage Weil2600d2d2010-02-22 15:12:16 -08001960 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07001961
Sage Weil2f2dc052009-10-06 11:31:09 -07001962 if (msg->front.iov_len < sizeof(*head)) {
1963 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08001964 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001965 return;
1966 }
1967
1968 /* get request, session */
Sage Weil6df058c2009-12-22 11:24:33 -08001969 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07001970 mutex_lock(&mdsc->mutex);
1971 req = __lookup_request(mdsc, tid);
1972 if (!req) {
1973 dout("handle_reply on unknown tid %llu\n", tid);
1974 mutex_unlock(&mdsc->mutex);
1975 return;
1976 }
1977 dout("handle_reply %p\n", req);
Sage Weil2f2dc052009-10-06 11:31:09 -07001978
1979 /* correct session? */
Sage Weild96d6042010-03-20 20:50:58 -07001980 if (req->r_session != session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001981 pr_err("mdsc_handle_reply got %llu on session mds%d"
1982 " not mds%d\n", tid, session->s_mds,
1983 req->r_session ? req->r_session->s_mds : -1);
1984 mutex_unlock(&mdsc->mutex);
1985 goto out;
1986 }
1987
1988 /* dup? */
1989 if ((req->r_got_unsafe && !head->safe) ||
1990 (req->r_got_safe && head->safe)) {
1991 pr_warning("got a dup %s reply on %llu from mds%d\n",
1992 head->safe ? "safe" : "unsafe", tid, mds);
1993 mutex_unlock(&mdsc->mutex);
1994 goto out;
1995 }
Sage Weil85792d0d2010-05-13 09:06:02 -07001996 if (req->r_got_safe && !head->safe) {
1997 pr_warning("got unsafe after safe on %llu from mds%d\n",
1998 tid, mds);
1999 mutex_unlock(&mdsc->mutex);
2000 goto out;
2001 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002002
2003 result = le32_to_cpu(head->result);
2004
2005 /*
Greg Farnume55b71f2010-06-22 15:58:01 -07002006 * Handle an ESTALE
2007 * if we're not talking to the authority, send to them
2008 * if the authority has changed while we weren't looking,
2009 * send to new authority
2010 * Otherwise we just have to return an ESTALE
Sage Weil2f2dc052009-10-06 11:31:09 -07002011 */
2012 if (result == -ESTALE) {
Greg Farnume55b71f2010-06-22 15:58:01 -07002013 dout("got ESTALE on request %llu", req->r_tid);
Sage Weil213c99e2010-08-03 10:25:11 -07002014 if (!req->r_inode) {
2015 /* do nothing; not an authority problem */
2016 } else if (req->r_direct_mode != USE_AUTH_MDS) {
Greg Farnume55b71f2010-06-22 15:58:01 -07002017 dout("not using auth, setting for that now");
2018 req->r_direct_mode = USE_AUTH_MDS;
Sage Weil2f2dc052009-10-06 11:31:09 -07002019 __do_request(mdsc, req);
2020 mutex_unlock(&mdsc->mutex);
2021 goto out;
Greg Farnume55b71f2010-06-22 15:58:01 -07002022 } else {
2023 struct ceph_inode_info *ci = ceph_inode(req->r_inode);
2024 struct ceph_cap *cap =
2025 ceph_get_cap_for_mds(ci, req->r_mds);;
2026
2027 dout("already using auth");
2028 if ((!cap || cap != ci->i_auth_cap) ||
2029 (cap->mseq != req->r_sent_on_mseq)) {
2030 dout("but cap changed, so resending");
2031 __do_request(mdsc, req);
2032 mutex_unlock(&mdsc->mutex);
2033 goto out;
2034 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002035 }
Greg Farnume55b71f2010-06-22 15:58:01 -07002036 dout("have to return ESTALE on request %llu", req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002037 }
2038
Greg Farnume55b71f2010-06-22 15:58:01 -07002039
Sage Weil2f2dc052009-10-06 11:31:09 -07002040 if (head->safe) {
2041 req->r_got_safe = true;
2042 __unregister_request(mdsc, req);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002043 complete_all(&req->r_safe_completion);
Sage Weil2f2dc052009-10-06 11:31:09 -07002044
2045 if (req->r_got_unsafe) {
2046 /*
2047 * We already handled the unsafe response, now do the
2048 * cleanup. No need to examine the response; the MDS
2049 * doesn't include any result info in the safe
2050 * response. And even if it did, there is nothing
2051 * useful we could do with a revised return value.
2052 */
2053 dout("got safe reply %llu, mds%d\n", tid, mds);
2054 list_del_init(&req->r_unsafe_item);
2055
2056 /* last unsafe request during umount? */
Sage Weil44ca18f2010-02-15 12:08:46 -08002057 if (mdsc->stopping && !__get_oldest_req(mdsc))
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002058 complete_all(&mdsc->safe_umount_waiters);
Sage Weil2f2dc052009-10-06 11:31:09 -07002059 mutex_unlock(&mdsc->mutex);
2060 goto out;
2061 }
Sage Weile1518c72010-05-13 11:19:06 -07002062 } else {
Sage Weil2f2dc052009-10-06 11:31:09 -07002063 req->r_got_unsafe = true;
2064 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
2065 }
2066
2067 dout("handle_reply tid %lld result %d\n", tid, result);
2068 rinfo = &req->r_reply_info;
2069 err = parse_reply_info(msg, rinfo);
2070 mutex_unlock(&mdsc->mutex);
2071
2072 mutex_lock(&session->s_mutex);
2073 if (err < 0) {
2074 pr_err("mdsc_handle_reply got corrupt reply mds%d\n", mds);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002075 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002076 goto out_err;
2077 }
2078
2079 /* snap trace */
2080 if (rinfo->snapblob_len) {
2081 down_write(&mdsc->snap_rwsem);
2082 ceph_update_snap_trace(mdsc, rinfo->snapblob,
2083 rinfo->snapblob + rinfo->snapblob_len,
2084 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP);
2085 downgrade_write(&mdsc->snap_rwsem);
2086 } else {
2087 down_read(&mdsc->snap_rwsem);
2088 }
2089
2090 /* insert trace into our cache */
Sage Weilb4556392010-05-13 12:01:13 -07002091 mutex_lock(&req->r_fill_mutex);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002092 err = ceph_fill_trace(mdsc->fsc->sb, req, req->r_session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002093 if (err == 0) {
2094 if (result == 0 && rinfo->dir_nr)
2095 ceph_readdir_prepopulate(req, req->r_session);
Yehuda Sadeh37151662010-06-17 16:16:12 -07002096 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
Sage Weil2f2dc052009-10-06 11:31:09 -07002097 }
Sage Weilb4556392010-05-13 12:01:13 -07002098 mutex_unlock(&req->r_fill_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002099
2100 up_read(&mdsc->snap_rwsem);
2101out_err:
Sage Weile1518c72010-05-13 11:19:06 -07002102 mutex_lock(&mdsc->mutex);
2103 if (!req->r_aborted) {
2104 if (err) {
2105 req->r_err = err;
2106 } else {
2107 req->r_reply = msg;
2108 ceph_msg_get(msg);
2109 req->r_got_result = true;
2110 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002111 } else {
Sage Weile1518c72010-05-13 11:19:06 -07002112 dout("reply arrived after request %lld was aborted\n", tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002113 }
Sage Weile1518c72010-05-13 11:19:06 -07002114 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002115
Sage Weilee6b2722010-06-10 12:55:52 -07002116 ceph_add_cap_releases(mdsc, req->r_session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002117 mutex_unlock(&session->s_mutex);
2118
2119 /* kick calling process */
2120 complete_request(mdsc, req);
2121out:
2122 ceph_mdsc_put_request(req);
2123 return;
2124}
2125
2126
2127
2128/*
2129 * handle mds notification that our request has been forwarded.
2130 */
Sage Weil2600d2d2010-02-22 15:12:16 -08002131static void handle_forward(struct ceph_mds_client *mdsc,
2132 struct ceph_mds_session *session,
2133 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002134{
2135 struct ceph_mds_request *req;
Sage Weila1ea7872010-02-23 14:02:44 -08002136 u64 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002137 u32 next_mds;
2138 u32 fwd_seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002139 int err = -EINVAL;
2140 void *p = msg->front.iov_base;
2141 void *end = p + msg->front.iov_len;
Sage Weil2f2dc052009-10-06 11:31:09 -07002142
Sage Weila1ea7872010-02-23 14:02:44 -08002143 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002144 next_mds = ceph_decode_32(&p);
2145 fwd_seq = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07002146
2147 mutex_lock(&mdsc->mutex);
2148 req = __lookup_request(mdsc, tid);
2149 if (!req) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07002150 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002151 goto out; /* dup reply? */
2152 }
2153
Sage Weil2a8e5e32010-05-28 16:43:16 -07002154 if (req->r_aborted) {
2155 dout("forward tid %llu aborted, unregistering\n", tid);
2156 __unregister_request(mdsc, req);
2157 } else if (fwd_seq <= req->r_num_fwd) {
2158 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002159 tid, next_mds, req->r_num_fwd, fwd_seq);
2160 } else {
2161 /* resend. forward race not possible; mds would drop */
Sage Weil2a8e5e32010-05-28 16:43:16 -07002162 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2163 BUG_ON(req->r_err);
2164 BUG_ON(req->r_got_result);
Sage Weil2f2dc052009-10-06 11:31:09 -07002165 req->r_num_fwd = fwd_seq;
2166 req->r_resend_mds = next_mds;
2167 put_request_session(req);
2168 __do_request(mdsc, req);
2169 }
2170 ceph_mdsc_put_request(req);
2171out:
2172 mutex_unlock(&mdsc->mutex);
2173 return;
2174
2175bad:
2176 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2177}
2178
2179/*
2180 * handle a mds session control message
2181 */
2182static void handle_session(struct ceph_mds_session *session,
2183 struct ceph_msg *msg)
2184{
2185 struct ceph_mds_client *mdsc = session->s_mdsc;
2186 u32 op;
2187 u64 seq;
Sage Weil2600d2d2010-02-22 15:12:16 -08002188 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002189 struct ceph_mds_session_head *h = msg->front.iov_base;
2190 int wake = 0;
2191
Sage Weil2f2dc052009-10-06 11:31:09 -07002192 /* decode */
2193 if (msg->front.iov_len != sizeof(*h))
2194 goto bad;
2195 op = le32_to_cpu(h->op);
2196 seq = le64_to_cpu(h->seq);
2197
2198 mutex_lock(&mdsc->mutex);
Sage Weil2600d2d2010-02-22 15:12:16 -08002199 if (op == CEPH_SESSION_CLOSE)
2200 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002201 /* FIXME: this ttl calculation is generous */
2202 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2203 mutex_unlock(&mdsc->mutex);
2204
2205 mutex_lock(&session->s_mutex);
2206
2207 dout("handle_session mds%d %s %p state %s seq %llu\n",
2208 mds, ceph_session_op_name(op), session,
2209 session_state_name(session->s_state), seq);
2210
2211 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2212 session->s_state = CEPH_MDS_SESSION_OPEN;
2213 pr_info("mds%d came back\n", session->s_mds);
2214 }
2215
2216 switch (op) {
2217 case CEPH_SESSION_OPEN:
Sage Weil29790f22010-03-18 14:45:05 -07002218 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2219 pr_info("mds%d reconnect success\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002220 session->s_state = CEPH_MDS_SESSION_OPEN;
2221 renewed_caps(mdsc, session, 0);
2222 wake = 1;
2223 if (mdsc->stopping)
2224 __close_session(mdsc, session);
2225 break;
2226
2227 case CEPH_SESSION_RENEWCAPS:
2228 if (session->s_renew_seq == seq)
2229 renewed_caps(mdsc, session, 1);
2230 break;
2231
2232 case CEPH_SESSION_CLOSE:
Sage Weil29790f22010-03-18 14:45:05 -07002233 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2234 pr_info("mds%d reconnect denied\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002235 remove_session_caps(session);
2236 wake = 1; /* for good measure */
Sage Weilf3c60c52010-08-11 14:51:23 -07002237 wake_up_all(&mdsc->session_close_wq);
Sage Weil29790f22010-03-18 14:45:05 -07002238 kick_requests(mdsc, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002239 break;
2240
2241 case CEPH_SESSION_STALE:
2242 pr_info("mds%d caps went stale, renewing\n",
2243 session->s_mds);
2244 spin_lock(&session->s_cap_lock);
2245 session->s_cap_gen++;
2246 session->s_cap_ttl = 0;
2247 spin_unlock(&session->s_cap_lock);
2248 send_renew_caps(mdsc, session);
2249 break;
2250
2251 case CEPH_SESSION_RECALL_STATE:
2252 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2253 break;
2254
2255 default:
2256 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2257 WARN_ON(1);
2258 }
2259
2260 mutex_unlock(&session->s_mutex);
2261 if (wake) {
2262 mutex_lock(&mdsc->mutex);
2263 __wake_requests(mdsc, &session->s_waiting);
2264 mutex_unlock(&mdsc->mutex);
2265 }
2266 return;
2267
2268bad:
2269 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2270 (int)msg->front.iov_len);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002271 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002272 return;
2273}
2274
2275
2276/*
2277 * called under session->mutex.
2278 */
2279static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2280 struct ceph_mds_session *session)
2281{
2282 struct ceph_mds_request *req, *nreq;
2283 int err;
2284
2285 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2286
2287 mutex_lock(&mdsc->mutex);
2288 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
2289 err = __prepare_send_request(mdsc, req, session->s_mds);
2290 if (!err) {
2291 ceph_msg_get(req->r_request);
2292 ceph_con_send(&session->s_con, req->r_request);
2293 }
2294 }
2295 mutex_unlock(&mdsc->mutex);
2296}
2297
2298/*
2299 * Encode information about a cap for a reconnect with the MDS.
2300 */
Sage Weil2f2dc052009-10-06 11:31:09 -07002301static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2302 void *arg)
2303{
Sage Weil20cb34a2010-05-12 15:21:32 -07002304 union {
2305 struct ceph_mds_cap_reconnect v2;
2306 struct ceph_mds_cap_reconnect_v1 v1;
2307 } rec;
2308 size_t reclen;
Sage Weil2f2dc052009-10-06 11:31:09 -07002309 struct ceph_inode_info *ci;
Sage Weil20cb34a2010-05-12 15:21:32 -07002310 struct ceph_reconnect_state *recon_state = arg;
2311 struct ceph_pagelist *pagelist = recon_state->pagelist;
Sage Weil2f2dc052009-10-06 11:31:09 -07002312 char *path;
2313 int pathlen, err;
2314 u64 pathbase;
2315 struct dentry *dentry;
2316
2317 ci = cap->ci;
2318
2319 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2320 inode, ceph_vinop(inode), cap, cap->cap_id,
2321 ceph_cap_string(cap->issued));
Sage Weil93cea5b2009-12-23 12:21:51 -08002322 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2323 if (err)
2324 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002325
2326 dentry = d_find_alias(inode);
2327 if (dentry) {
2328 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2329 if (IS_ERR(path)) {
2330 err = PTR_ERR(path);
Sage Weile072f8a2010-08-26 09:26:37 -07002331 goto out_dput;
Sage Weil2f2dc052009-10-06 11:31:09 -07002332 }
2333 } else {
2334 path = NULL;
2335 pathlen = 0;
2336 }
Sage Weil93cea5b2009-12-23 12:21:51 -08002337 err = ceph_pagelist_encode_string(pagelist, path, pathlen);
2338 if (err)
Sage Weile072f8a2010-08-26 09:26:37 -07002339 goto out_free;
Sage Weil2f2dc052009-10-06 11:31:09 -07002340
Sage Weil2f2dc052009-10-06 11:31:09 -07002341 spin_lock(&inode->i_lock);
2342 cap->seq = 0; /* reset cap seq */
2343 cap->issue_seq = 0; /* and issue_seq */
Sage Weil20cb34a2010-05-12 15:21:32 -07002344
2345 if (recon_state->flock) {
2346 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
2347 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2348 rec.v2.issued = cpu_to_le32(cap->issued);
2349 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2350 rec.v2.pathbase = cpu_to_le64(pathbase);
2351 rec.v2.flock_len = 0;
2352 reclen = sizeof(rec.v2);
2353 } else {
2354 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
2355 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2356 rec.v1.issued = cpu_to_le32(cap->issued);
2357 rec.v1.size = cpu_to_le64(inode->i_size);
2358 ceph_encode_timespec(&rec.v1.mtime, &inode->i_mtime);
2359 ceph_encode_timespec(&rec.v1.atime, &inode->i_atime);
2360 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2361 rec.v1.pathbase = cpu_to_le64(pathbase);
2362 reclen = sizeof(rec.v1);
2363 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002364 spin_unlock(&inode->i_lock);
2365
Greg Farnum40819f62010-08-02 15:34:23 -07002366 if (recon_state->flock) {
2367 int num_fcntl_locks, num_flock_locks;
2368
2369 lock_kernel();
2370 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
2371 rec.v2.flock_len = (2*sizeof(u32) +
2372 (num_fcntl_locks+num_flock_locks) *
2373 sizeof(struct ceph_filelock));
2374
2375 err = ceph_pagelist_append(pagelist, &rec, reclen);
2376 if (!err)
2377 err = ceph_encode_locks(inode, pagelist,
2378 num_fcntl_locks,
2379 num_flock_locks);
2380 unlock_kernel();
Sage Weil3612abb2010-09-07 15:59:27 -07002381 } else {
2382 err = ceph_pagelist_append(pagelist, &rec, reclen);
Greg Farnum40819f62010-08-02 15:34:23 -07002383 }
Sage Weil93cea5b2009-12-23 12:21:51 -08002384
Sage Weile072f8a2010-08-26 09:26:37 -07002385out_free:
Sage Weil2f2dc052009-10-06 11:31:09 -07002386 kfree(path);
Sage Weile072f8a2010-08-26 09:26:37 -07002387out_dput:
Sage Weil2f2dc052009-10-06 11:31:09 -07002388 dput(dentry);
Sage Weil93cea5b2009-12-23 12:21:51 -08002389 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002390}
2391
2392
2393/*
2394 * If an MDS fails and recovers, clients need to reconnect in order to
2395 * reestablish shared state. This includes all caps issued through
2396 * this session _and_ the snap_realm hierarchy. Because it's not
2397 * clear which snap realms the mds cares about, we send everything we
2398 * know about.. that ensures we'll then get any new info the
2399 * recovering MDS might have.
2400 *
2401 * This is a relatively heavyweight operation, but it's rare.
2402 *
2403 * called with mdsc->mutex held.
2404 */
Sage Weil34b6c852010-05-10 16:31:25 -07002405static void send_mds_reconnect(struct ceph_mds_client *mdsc,
2406 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07002407{
Sage Weil2f2dc052009-10-06 11:31:09 -07002408 struct ceph_msg *reply;
Sage Weila105f002010-02-15 14:37:55 -08002409 struct rb_node *p;
Sage Weil34b6c852010-05-10 16:31:25 -07002410 int mds = session->s_mds;
Sage Weil9abf82b2010-05-10 21:58:38 -07002411 int err = -ENOMEM;
Sage Weil93cea5b2009-12-23 12:21:51 -08002412 struct ceph_pagelist *pagelist;
Sage Weil20cb34a2010-05-12 15:21:32 -07002413 struct ceph_reconnect_state recon_state;
Sage Weil2f2dc052009-10-06 11:31:09 -07002414
Sage Weil34b6c852010-05-10 16:31:25 -07002415 pr_info("mds%d reconnect start\n", mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002416
Sage Weil93cea5b2009-12-23 12:21:51 -08002417 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
2418 if (!pagelist)
2419 goto fail_nopagelist;
2420 ceph_pagelist_init(pagelist);
2421
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002422 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -07002423 if (!reply)
Sage Weil93cea5b2009-12-23 12:21:51 -08002424 goto fail_nomsg;
Sage Weil93cea5b2009-12-23 12:21:51 -08002425
Sage Weil34b6c852010-05-10 16:31:25 -07002426 mutex_lock(&session->s_mutex);
2427 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
2428 session->s_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002429
Sage Weil34b6c852010-05-10 16:31:25 -07002430 ceph_con_open(&session->s_con,
2431 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
Sage Weil2f2dc052009-10-06 11:31:09 -07002432
Sage Weil34b6c852010-05-10 16:31:25 -07002433 /* replay unsafe requests */
2434 replay_unsafe_requests(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002435
2436 down_read(&mdsc->snap_rwsem);
2437
Sage Weil2f2dc052009-10-06 11:31:09 -07002438 dout("session %p state %s\n", session,
2439 session_state_name(session->s_state));
2440
Sage Weile01a5942010-05-10 15:36:44 -07002441 /* drop old cap expires; we're about to reestablish that state */
2442 discard_cap_releases(mdsc, session);
2443
Sage Weil2f2dc052009-10-06 11:31:09 -07002444 /* traverse this session's caps */
Sage Weil93cea5b2009-12-23 12:21:51 -08002445 err = ceph_pagelist_encode_32(pagelist, session->s_nr_caps);
2446 if (err)
2447 goto fail;
Sage Weil20cb34a2010-05-12 15:21:32 -07002448
2449 recon_state.pagelist = pagelist;
2450 recon_state.flock = session->s_con.peer_features & CEPH_FEATURE_FLOCK;
2451 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
Sage Weil2f2dc052009-10-06 11:31:09 -07002452 if (err < 0)
Sage Weil9abf82b2010-05-10 21:58:38 -07002453 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07002454
2455 /*
2456 * snaprealms. we provide mds with the ino, seq (version), and
2457 * parent for all of our realms. If the mds has any newer info,
2458 * it will tell us.
2459 */
Sage Weila105f002010-02-15 14:37:55 -08002460 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
2461 struct ceph_snap_realm *realm =
2462 rb_entry(p, struct ceph_snap_realm, node);
Sage Weil93cea5b2009-12-23 12:21:51 -08002463 struct ceph_mds_snaprealm_reconnect sr_rec;
Sage Weil2f2dc052009-10-06 11:31:09 -07002464
2465 dout(" adding snap realm %llx seq %lld parent %llx\n",
2466 realm->ino, realm->seq, realm->parent_ino);
Sage Weil93cea5b2009-12-23 12:21:51 -08002467 sr_rec.ino = cpu_to_le64(realm->ino);
2468 sr_rec.seq = cpu_to_le64(realm->seq);
2469 sr_rec.parent = cpu_to_le64(realm->parent_ino);
2470 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
2471 if (err)
2472 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07002473 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002474
Sage Weil93cea5b2009-12-23 12:21:51 -08002475 reply->pagelist = pagelist;
Sage Weil20cb34a2010-05-12 15:21:32 -07002476 if (recon_state.flock)
2477 reply->hdr.version = cpu_to_le16(2);
Sage Weil93cea5b2009-12-23 12:21:51 -08002478 reply->hdr.data_len = cpu_to_le32(pagelist->length);
2479 reply->nr_pages = calc_pages_for(0, pagelist->length);
Sage Weil2f2dc052009-10-06 11:31:09 -07002480 ceph_con_send(&session->s_con, reply);
2481
Sage Weil9abf82b2010-05-10 21:58:38 -07002482 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002483
Sage Weil9abf82b2010-05-10 21:58:38 -07002484 mutex_lock(&mdsc->mutex);
2485 __wake_requests(mdsc, &session->s_waiting);
2486 mutex_unlock(&mdsc->mutex);
2487
Sage Weil2f2dc052009-10-06 11:31:09 -07002488 up_read(&mdsc->snap_rwsem);
Sage Weil2f2dc052009-10-06 11:31:09 -07002489 return;
2490
Sage Weil93cea5b2009-12-23 12:21:51 -08002491fail:
Sage Weil2f2dc052009-10-06 11:31:09 -07002492 ceph_msg_put(reply);
Sage Weil9abf82b2010-05-10 21:58:38 -07002493 up_read(&mdsc->snap_rwsem);
2494 mutex_unlock(&session->s_mutex);
Sage Weil93cea5b2009-12-23 12:21:51 -08002495fail_nomsg:
2496 ceph_pagelist_release(pagelist);
2497 kfree(pagelist);
2498fail_nopagelist:
Sage Weil9abf82b2010-05-10 21:58:38 -07002499 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
Sage Weil9abf82b2010-05-10 21:58:38 -07002500 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07002501}
2502
2503
2504/*
2505 * compare old and new mdsmaps, kicking requests
2506 * and closing out old connections as necessary
2507 *
2508 * called under mdsc->mutex.
2509 */
2510static void check_new_map(struct ceph_mds_client *mdsc,
2511 struct ceph_mdsmap *newmap,
2512 struct ceph_mdsmap *oldmap)
2513{
2514 int i;
2515 int oldstate, newstate;
2516 struct ceph_mds_session *s;
2517
2518 dout("check_new_map new %u old %u\n",
2519 newmap->m_epoch, oldmap->m_epoch);
2520
2521 for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
2522 if (mdsc->sessions[i] == NULL)
2523 continue;
2524 s = mdsc->sessions[i];
2525 oldstate = ceph_mdsmap_get_state(oldmap, i);
2526 newstate = ceph_mdsmap_get_state(newmap, i);
2527
Sage Weil0deb01c2010-06-17 14:19:01 -07002528 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002529 i, ceph_mds_state_name(oldstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07002530 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
Sage Weil2f2dc052009-10-06 11:31:09 -07002531 ceph_mds_state_name(newstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07002532 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
Sage Weil2f2dc052009-10-06 11:31:09 -07002533 session_state_name(s->s_state));
2534
2535 if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
2536 ceph_mdsmap_get_addr(newmap, i),
2537 sizeof(struct ceph_entity_addr))) {
2538 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
2539 /* the session never opened, just close it
2540 * out now */
2541 __wake_requests(mdsc, &s->s_waiting);
Sage Weil2600d2d2010-02-22 15:12:16 -08002542 __unregister_session(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07002543 } else {
2544 /* just close it */
2545 mutex_unlock(&mdsc->mutex);
2546 mutex_lock(&s->s_mutex);
2547 mutex_lock(&mdsc->mutex);
2548 ceph_con_close(&s->s_con);
2549 mutex_unlock(&s->s_mutex);
2550 s->s_state = CEPH_MDS_SESSION_RESTARTING;
2551 }
2552
2553 /* kick any requests waiting on the recovering mds */
Sage Weil29790f22010-03-18 14:45:05 -07002554 kick_requests(mdsc, i);
Sage Weil2f2dc052009-10-06 11:31:09 -07002555 } else if (oldstate == newstate) {
2556 continue; /* nothing new with this mds */
2557 }
2558
2559 /*
2560 * send reconnect?
2561 */
2562 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
Sage Weil34b6c852010-05-10 16:31:25 -07002563 newstate >= CEPH_MDS_STATE_RECONNECT) {
2564 mutex_unlock(&mdsc->mutex);
2565 send_mds_reconnect(mdsc, s);
2566 mutex_lock(&mdsc->mutex);
2567 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002568
2569 /*
Sage Weil29790f22010-03-18 14:45:05 -07002570 * kick request on any mds that has gone active.
Sage Weil2f2dc052009-10-06 11:31:09 -07002571 */
2572 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
2573 newstate >= CEPH_MDS_STATE_ACTIVE) {
Sage Weil29790f22010-03-18 14:45:05 -07002574 if (oldstate != CEPH_MDS_STATE_CREATING &&
2575 oldstate != CEPH_MDS_STATE_STARTING)
2576 pr_info("mds%d recovery completed\n", s->s_mds);
2577 kick_requests(mdsc, i);
Sage Weil2f2dc052009-10-06 11:31:09 -07002578 ceph_kick_flushing_caps(mdsc, s);
Sage Weil0dc25702009-11-20 13:43:45 -08002579 wake_up_session_caps(s, 1);
Sage Weil2f2dc052009-10-06 11:31:09 -07002580 }
2581 }
Sage Weilcb170a22010-06-21 13:38:35 -07002582
2583 for (i = 0; i < newmap->m_max_mds && i < mdsc->max_sessions; i++) {
2584 s = mdsc->sessions[i];
2585 if (!s)
2586 continue;
2587 if (!ceph_mdsmap_is_laggy(newmap, i))
2588 continue;
2589 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2590 s->s_state == CEPH_MDS_SESSION_HUNG ||
2591 s->s_state == CEPH_MDS_SESSION_CLOSING) {
2592 dout(" connecting to export targets of laggy mds%d\n",
2593 i);
2594 __open_export_target_sessions(mdsc, s);
2595 }
2596 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002597}
2598
2599
2600
2601/*
2602 * leases
2603 */
2604
2605/*
2606 * caller must hold session s_mutex, dentry->d_lock
2607 */
2608void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
2609{
2610 struct ceph_dentry_info *di = ceph_dentry(dentry);
2611
2612 ceph_put_mds_session(di->lease_session);
2613 di->lease_session = NULL;
2614}
2615
Sage Weil2600d2d2010-02-22 15:12:16 -08002616static void handle_lease(struct ceph_mds_client *mdsc,
2617 struct ceph_mds_session *session,
2618 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002619{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002620 struct super_block *sb = mdsc->fsc->sb;
Sage Weil2f2dc052009-10-06 11:31:09 -07002621 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07002622 struct ceph_inode_info *ci;
2623 struct dentry *parent, *dentry;
2624 struct ceph_dentry_info *di;
Sage Weil2600d2d2010-02-22 15:12:16 -08002625 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002626 struct ceph_mds_lease *h = msg->front.iov_base;
Sage Weil1e5ea232010-06-04 10:05:40 -07002627 u32 seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002628 struct ceph_vino vino;
2629 int mask;
2630 struct qstr dname;
2631 int release = 0;
2632
Sage Weil2f2dc052009-10-06 11:31:09 -07002633 dout("handle_lease from mds%d\n", mds);
2634
2635 /* decode */
2636 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
2637 goto bad;
2638 vino.ino = le64_to_cpu(h->ino);
2639 vino.snap = CEPH_NOSNAP;
2640 mask = le16_to_cpu(h->mask);
Sage Weil1e5ea232010-06-04 10:05:40 -07002641 seq = le32_to_cpu(h->seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002642 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
2643 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
2644 if (dname.len != get_unaligned_le32(h+1))
2645 goto bad;
2646
Sage Weil2f2dc052009-10-06 11:31:09 -07002647 mutex_lock(&session->s_mutex);
2648 session->s_seq++;
2649
2650 /* lookup inode */
2651 inode = ceph_find_inode(sb, vino);
Sage Weil1e5ea232010-06-04 10:05:40 -07002652 dout("handle_lease %s, mask %d, ino %llx %p %.*s\n",
2653 ceph_lease_op_name(h->action), mask, vino.ino, inode,
2654 dname.len, dname.name);
Sage Weil2f2dc052009-10-06 11:31:09 -07002655 if (inode == NULL) {
2656 dout("handle_lease no inode %llx\n", vino.ino);
2657 goto release;
2658 }
2659 ci = ceph_inode(inode);
2660
2661 /* dentry */
2662 parent = d_find_alias(inode);
2663 if (!parent) {
2664 dout("no parent dentry on inode %p\n", inode);
2665 WARN_ON(1);
2666 goto release; /* hrm... */
2667 }
2668 dname.hash = full_name_hash(dname.name, dname.len);
2669 dentry = d_lookup(parent, &dname);
2670 dput(parent);
2671 if (!dentry)
2672 goto release;
2673
2674 spin_lock(&dentry->d_lock);
2675 di = ceph_dentry(dentry);
2676 switch (h->action) {
2677 case CEPH_MDS_LEASE_REVOKE:
2678 if (di && di->lease_session == session) {
Sage Weil1e5ea232010-06-04 10:05:40 -07002679 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
2680 h->seq = cpu_to_le32(di->lease_seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002681 __ceph_mdsc_drop_dentry_lease(dentry);
2682 }
2683 release = 1;
2684 break;
2685
2686 case CEPH_MDS_LEASE_RENEW:
2687 if (di && di->lease_session == session &&
2688 di->lease_gen == session->s_cap_gen &&
2689 di->lease_renew_from &&
2690 di->lease_renew_after == 0) {
2691 unsigned long duration =
2692 le32_to_cpu(h->duration_ms) * HZ / 1000;
2693
Sage Weil1e5ea232010-06-04 10:05:40 -07002694 di->lease_seq = seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002695 dentry->d_time = di->lease_renew_from + duration;
2696 di->lease_renew_after = di->lease_renew_from +
2697 (duration >> 1);
2698 di->lease_renew_from = 0;
2699 }
2700 break;
2701 }
2702 spin_unlock(&dentry->d_lock);
2703 dput(dentry);
2704
2705 if (!release)
2706 goto out;
2707
2708release:
2709 /* let's just reuse the same message */
2710 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
2711 ceph_msg_get(msg);
2712 ceph_con_send(&session->s_con, msg);
2713
2714out:
2715 iput(inode);
2716 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002717 return;
2718
2719bad:
2720 pr_err("corrupt lease message\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002721 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002722}
2723
2724void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
2725 struct inode *inode,
2726 struct dentry *dentry, char action,
2727 u32 seq)
2728{
2729 struct ceph_msg *msg;
2730 struct ceph_mds_lease *lease;
2731 int len = sizeof(*lease) + sizeof(u32);
2732 int dnamelen = 0;
2733
2734 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
2735 inode, dentry, ceph_lease_op_name(action), session->s_mds);
2736 dnamelen = dentry->d_name.len;
2737 len += dnamelen;
2738
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002739 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -07002740 if (!msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002741 return;
2742 lease = msg->front.iov_base;
2743 lease->action = action;
Sage Weildd1c9052010-05-25 16:45:25 -07002744 lease->mask = cpu_to_le16(1);
Sage Weil2f2dc052009-10-06 11:31:09 -07002745 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
2746 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
2747 lease->seq = cpu_to_le32(seq);
2748 put_unaligned_le32(dnamelen, lease + 1);
2749 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
2750
2751 /*
2752 * if this is a preemptive lease RELEASE, no need to
2753 * flush request stream, since the actual request will
2754 * soon follow.
2755 */
2756 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
2757
2758 ceph_con_send(&session->s_con, msg);
2759}
2760
2761/*
2762 * Preemptively release a lease we expect to invalidate anyway.
2763 * Pass @inode always, @dentry is optional.
2764 */
2765void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
2766 struct dentry *dentry, int mask)
2767{
2768 struct ceph_dentry_info *di;
2769 struct ceph_mds_session *session;
2770 u32 seq;
2771
2772 BUG_ON(inode == NULL);
2773 BUG_ON(dentry == NULL);
Sage Weildd1c9052010-05-25 16:45:25 -07002774 BUG_ON(mask == 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07002775
2776 /* is dentry lease valid? */
2777 spin_lock(&dentry->d_lock);
2778 di = ceph_dentry(dentry);
2779 if (!di || !di->lease_session ||
2780 di->lease_session->s_mds < 0 ||
2781 di->lease_gen != di->lease_session->s_cap_gen ||
2782 !time_before(jiffies, dentry->d_time)) {
2783 dout("lease_release inode %p dentry %p -- "
2784 "no lease on %d\n",
2785 inode, dentry, mask);
2786 spin_unlock(&dentry->d_lock);
2787 return;
2788 }
2789
2790 /* we do have a lease on this dentry; note mds and seq */
2791 session = ceph_get_mds_session(di->lease_session);
2792 seq = di->lease_seq;
2793 __ceph_mdsc_drop_dentry_lease(dentry);
2794 spin_unlock(&dentry->d_lock);
2795
2796 dout("lease_release inode %p dentry %p mask %d to mds%d\n",
2797 inode, dentry, mask, session->s_mds);
2798 ceph_mdsc_lease_send_msg(session, inode, dentry,
2799 CEPH_MDS_LEASE_RELEASE, seq);
2800 ceph_put_mds_session(session);
2801}
2802
2803/*
2804 * drop all leases (and dentry refs) in preparation for umount
2805 */
2806static void drop_leases(struct ceph_mds_client *mdsc)
2807{
2808 int i;
2809
2810 dout("drop_leases\n");
2811 mutex_lock(&mdsc->mutex);
2812 for (i = 0; i < mdsc->max_sessions; i++) {
2813 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2814 if (!s)
2815 continue;
2816 mutex_unlock(&mdsc->mutex);
2817 mutex_lock(&s->s_mutex);
2818 mutex_unlock(&s->s_mutex);
2819 ceph_put_mds_session(s);
2820 mutex_lock(&mdsc->mutex);
2821 }
2822 mutex_unlock(&mdsc->mutex);
2823}
2824
2825
2826
2827/*
2828 * delayed work -- periodically trim expired leases, renew caps with mds
2829 */
2830static void schedule_delayed(struct ceph_mds_client *mdsc)
2831{
2832 int delay = 5;
2833 unsigned hz = round_jiffies_relative(HZ * delay);
2834 schedule_delayed_work(&mdsc->delayed_work, hz);
2835}
2836
2837static void delayed_work(struct work_struct *work)
2838{
2839 int i;
2840 struct ceph_mds_client *mdsc =
2841 container_of(work, struct ceph_mds_client, delayed_work.work);
2842 int renew_interval;
2843 int renew_caps;
2844
2845 dout("mdsc delayed_work\n");
Sage Weilafcdaea2009-10-14 14:27:38 -07002846 ceph_check_delayed_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07002847
2848 mutex_lock(&mdsc->mutex);
2849 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
2850 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
2851 mdsc->last_renew_caps);
2852 if (renew_caps)
2853 mdsc->last_renew_caps = jiffies;
2854
2855 for (i = 0; i < mdsc->max_sessions; i++) {
2856 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2857 if (s == NULL)
2858 continue;
2859 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
2860 dout("resending session close request for mds%d\n",
2861 s->s_mds);
2862 request_close_session(mdsc, s);
2863 ceph_put_mds_session(s);
2864 continue;
2865 }
2866 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
2867 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
2868 s->s_state = CEPH_MDS_SESSION_HUNG;
2869 pr_info("mds%d hung\n", s->s_mds);
2870 }
2871 }
2872 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
2873 /* this mds is failed or recovering, just wait */
2874 ceph_put_mds_session(s);
2875 continue;
2876 }
2877 mutex_unlock(&mdsc->mutex);
2878
2879 mutex_lock(&s->s_mutex);
2880 if (renew_caps)
2881 send_renew_caps(mdsc, s);
2882 else
2883 ceph_con_keepalive(&s->s_con);
Sage Weilee6b2722010-06-10 12:55:52 -07002884 ceph_add_cap_releases(mdsc, s);
Sage Weilaab53dd2010-03-17 16:30:21 -07002885 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2886 s->s_state == CEPH_MDS_SESSION_HUNG)
Sage Weil3d7ded42010-06-09 16:47:10 -07002887 ceph_send_cap_releases(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07002888 mutex_unlock(&s->s_mutex);
2889 ceph_put_mds_session(s);
2890
2891 mutex_lock(&mdsc->mutex);
2892 }
2893 mutex_unlock(&mdsc->mutex);
2894
2895 schedule_delayed(mdsc);
2896}
2897
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002898int ceph_mdsc_init(struct ceph_fs_client *fsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07002899
Sage Weil2f2dc052009-10-06 11:31:09 -07002900{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002901 struct ceph_mds_client *mdsc;
2902
2903 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
2904 if (!mdsc)
2905 return -ENOMEM;
2906 mdsc->fsc = fsc;
2907 fsc->mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07002908 mutex_init(&mdsc->mutex);
2909 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08002910 if (mdsc->mdsmap == NULL)
2911 return -ENOMEM;
2912
Sage Weil2f2dc052009-10-06 11:31:09 -07002913 init_completion(&mdsc->safe_umount_waiters);
Sage Weilf3c60c52010-08-11 14:51:23 -07002914 init_waitqueue_head(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002915 INIT_LIST_HEAD(&mdsc->waiting_for_map);
2916 mdsc->sessions = NULL;
2917 mdsc->max_sessions = 0;
2918 mdsc->stopping = 0;
2919 init_rwsem(&mdsc->snap_rwsem);
Sage Weila105f002010-02-15 14:37:55 -08002920 mdsc->snap_realms = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07002921 INIT_LIST_HEAD(&mdsc->snap_empty);
2922 spin_lock_init(&mdsc->snap_empty_lock);
2923 mdsc->last_tid = 0;
Sage Weil44ca18f2010-02-15 12:08:46 -08002924 mdsc->request_tree = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07002925 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
2926 mdsc->last_renew_caps = jiffies;
2927 INIT_LIST_HEAD(&mdsc->cap_delay_list);
2928 spin_lock_init(&mdsc->cap_delay_lock);
2929 INIT_LIST_HEAD(&mdsc->snap_flush_list);
2930 spin_lock_init(&mdsc->snap_flush_lock);
2931 mdsc->cap_flush_seq = 0;
2932 INIT_LIST_HEAD(&mdsc->cap_dirty);
2933 mdsc->num_cap_flushing = 0;
2934 spin_lock_init(&mdsc->cap_dirty_lock);
2935 init_waitqueue_head(&mdsc->cap_flushing_wq);
2936 spin_lock_init(&mdsc->dentry_lru_lock);
2937 INIT_LIST_HEAD(&mdsc->dentry_lru);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08002938
Yehuda Sadeh37151662010-06-17 16:16:12 -07002939 ceph_caps_init(mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002940 ceph_adjust_min_caps(mdsc, fsc->min_caps);
Yehuda Sadeh37151662010-06-17 16:16:12 -07002941
Sage Weil5f44f142009-11-18 14:52:18 -08002942 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002943}
2944
2945/*
2946 * Wait for safe replies on open mds requests. If we time out, drop
2947 * all requests from the tree to avoid dangling dentry refs.
2948 */
2949static void wait_requests(struct ceph_mds_client *mdsc)
2950{
2951 struct ceph_mds_request *req;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002952 struct ceph_fs_client *fsc = mdsc->fsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07002953
2954 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08002955 if (__get_oldest_req(mdsc)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002956 mutex_unlock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08002957
Sage Weil2f2dc052009-10-06 11:31:09 -07002958 dout("wait_requests waiting for requests\n");
2959 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002960 fsc->client->options->mount_timeout * HZ);
Sage Weil2f2dc052009-10-06 11:31:09 -07002961
2962 /* tear down remaining requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08002963 mutex_lock(&mdsc->mutex);
2964 while ((req = __get_oldest_req(mdsc))) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002965 dout("wait_requests timed out on tid %llu\n",
2966 req->r_tid);
Sage Weil44ca18f2010-02-15 12:08:46 -08002967 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002968 }
2969 }
2970 mutex_unlock(&mdsc->mutex);
2971 dout("wait_requests done\n");
2972}
2973
2974/*
2975 * called before mount is ro, and before dentries are torn down.
2976 * (hmm, does this still race with new lookups?)
2977 */
2978void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
2979{
2980 dout("pre_umount\n");
2981 mdsc->stopping = 1;
2982
2983 drop_leases(mdsc);
Sage Weilafcdaea2009-10-14 14:27:38 -07002984 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07002985 wait_requests(mdsc);
Sage Weil17c688c2010-06-21 16:12:26 -07002986
2987 /*
2988 * wait for reply handlers to drop their request refs and
2989 * their inode/dcache refs
2990 */
2991 ceph_msgr_flush();
Sage Weil2f2dc052009-10-06 11:31:09 -07002992}
2993
2994/*
2995 * wait for all write mds requests to flush.
2996 */
2997static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
2998{
Sage Weil80fc7312010-03-16 15:28:54 -07002999 struct ceph_mds_request *req = NULL, *nextreq;
Sage Weil44ca18f2010-02-15 12:08:46 -08003000 struct rb_node *n;
Sage Weil2f2dc052009-10-06 11:31:09 -07003001
3002 mutex_lock(&mdsc->mutex);
3003 dout("wait_unsafe_requests want %lld\n", want_tid);
Sage Weil80fc7312010-03-16 15:28:54 -07003004restart:
Sage Weil44ca18f2010-02-15 12:08:46 -08003005 req = __get_oldest_req(mdsc);
3006 while (req && req->r_tid <= want_tid) {
Sage Weil80fc7312010-03-16 15:28:54 -07003007 /* find next request */
3008 n = rb_next(&req->r_node);
3009 if (n)
3010 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
3011 else
3012 nextreq = NULL;
Sage Weil44ca18f2010-02-15 12:08:46 -08003013 if ((req->r_op & CEPH_MDS_OP_WRITE)) {
3014 /* write op */
3015 ceph_mdsc_get_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07003016 if (nextreq)
3017 ceph_mdsc_get_request(nextreq);
Sage Weil44ca18f2010-02-15 12:08:46 -08003018 mutex_unlock(&mdsc->mutex);
3019 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3020 req->r_tid, want_tid);
3021 wait_for_completion(&req->r_safe_completion);
3022 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003023 ceph_mdsc_put_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07003024 if (!nextreq)
3025 break; /* next dne before, so we're done! */
3026 if (RB_EMPTY_NODE(&nextreq->r_node)) {
3027 /* next request was removed from tree */
3028 ceph_mdsc_put_request(nextreq);
3029 goto restart;
3030 }
3031 ceph_mdsc_put_request(nextreq); /* won't go away */
Sage Weil44ca18f2010-02-15 12:08:46 -08003032 }
Sage Weil80fc7312010-03-16 15:28:54 -07003033 req = nextreq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003034 }
3035 mutex_unlock(&mdsc->mutex);
3036 dout("wait_unsafe_requests done\n");
3037}
3038
3039void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
3040{
3041 u64 want_tid, want_flush;
3042
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003043 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
Sage Weil56b7cf92010-05-03 15:22:00 -07003044 return;
3045
Sage Weil2f2dc052009-10-06 11:31:09 -07003046 dout("sync\n");
3047 mutex_lock(&mdsc->mutex);
3048 want_tid = mdsc->last_tid;
3049 want_flush = mdsc->cap_flush_seq;
3050 mutex_unlock(&mdsc->mutex);
3051 dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
3052
Sage Weilafcdaea2009-10-14 14:27:38 -07003053 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003054
3055 wait_unsafe_requests(mdsc, want_tid);
3056 wait_event(mdsc->cap_flushing_wq, check_cap_flush(mdsc, want_flush));
3057}
3058
Sage Weilf3c60c52010-08-11 14:51:23 -07003059/*
3060 * true if all sessions are closed, or we force unmount
3061 */
3062bool done_closing_sessions(struct ceph_mds_client *mdsc)
3063{
3064 int i, n = 0;
3065
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003066 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
Sage Weilf3c60c52010-08-11 14:51:23 -07003067 return true;
3068
3069 mutex_lock(&mdsc->mutex);
3070 for (i = 0; i < mdsc->max_sessions; i++)
3071 if (mdsc->sessions[i])
3072 n++;
3073 mutex_unlock(&mdsc->mutex);
3074 return n == 0;
3075}
Sage Weil2f2dc052009-10-06 11:31:09 -07003076
3077/*
3078 * called after sb is ro.
3079 */
3080void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
3081{
3082 struct ceph_mds_session *session;
3083 int i;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003084 struct ceph_fs_client *fsc = mdsc->fsc;
3085 unsigned long timeout = fsc->client->options->mount_timeout * HZ;
Sage Weil2f2dc052009-10-06 11:31:09 -07003086
3087 dout("close_sessions\n");
3088
Sage Weil2f2dc052009-10-06 11:31:09 -07003089 /* close sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07003090 mutex_lock(&mdsc->mutex);
3091 for (i = 0; i < mdsc->max_sessions; i++) {
3092 session = __ceph_lookup_mds_session(mdsc, i);
3093 if (!session)
3094 continue;
Sage Weil2f2dc052009-10-06 11:31:09 -07003095 mutex_unlock(&mdsc->mutex);
Sage Weilf3c60c52010-08-11 14:51:23 -07003096 mutex_lock(&session->s_mutex);
3097 __close_session(mdsc, session);
3098 mutex_unlock(&session->s_mutex);
3099 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003100 mutex_lock(&mdsc->mutex);
3101 }
Sage Weilf3c60c52010-08-11 14:51:23 -07003102 mutex_unlock(&mdsc->mutex);
3103
3104 dout("waiting for sessions to close\n");
3105 wait_event_timeout(mdsc->session_close_wq, done_closing_sessions(mdsc),
3106 timeout);
Sage Weil2f2dc052009-10-06 11:31:09 -07003107
3108 /* tear down remaining sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07003109 mutex_lock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003110 for (i = 0; i < mdsc->max_sessions; i++) {
3111 if (mdsc->sessions[i]) {
3112 session = get_session(mdsc->sessions[i]);
Sage Weil2600d2d2010-02-22 15:12:16 -08003113 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003114 mutex_unlock(&mdsc->mutex);
3115 mutex_lock(&session->s_mutex);
3116 remove_session_caps(session);
3117 mutex_unlock(&session->s_mutex);
3118 ceph_put_mds_session(session);
3119 mutex_lock(&mdsc->mutex);
3120 }
3121 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003122 WARN_ON(!list_empty(&mdsc->cap_delay_list));
Sage Weil2f2dc052009-10-06 11:31:09 -07003123 mutex_unlock(&mdsc->mutex);
3124
3125 ceph_cleanup_empty_realms(mdsc);
3126
3127 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3128
3129 dout("stopped\n");
3130}
3131
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003132static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07003133{
3134 dout("stop\n");
3135 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3136 if (mdsc->mdsmap)
3137 ceph_mdsmap_destroy(mdsc->mdsmap);
3138 kfree(mdsc->sessions);
Yehuda Sadeh37151662010-06-17 16:16:12 -07003139 ceph_caps_finalize(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003140}
3141
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003142void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
3143{
3144 struct ceph_mds_client *mdsc = fsc->mdsc;
3145
3146 ceph_mdsc_stop(mdsc);
3147 fsc->mdsc = NULL;
3148 kfree(mdsc);
3149}
3150
Sage Weil2f2dc052009-10-06 11:31:09 -07003151
3152/*
3153 * handle mds map update.
3154 */
3155void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3156{
3157 u32 epoch;
3158 u32 maplen;
3159 void *p = msg->front.iov_base;
3160 void *end = p + msg->front.iov_len;
3161 struct ceph_mdsmap *newmap, *oldmap;
3162 struct ceph_fsid fsid;
3163 int err = -EINVAL;
3164
3165 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
3166 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003167 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
Sage Weil07433042009-11-18 16:50:41 -08003168 return;
Sage Weilc89136e2009-10-14 09:59:09 -07003169 epoch = ceph_decode_32(&p);
3170 maplen = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07003171 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
3172
3173 /* do we need it? */
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003174 ceph_monc_got_mdsmap(&mdsc->fsc->client->monc, epoch);
Sage Weil2f2dc052009-10-06 11:31:09 -07003175 mutex_lock(&mdsc->mutex);
3176 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
3177 dout("handle_map epoch %u <= our %u\n",
3178 epoch, mdsc->mdsmap->m_epoch);
3179 mutex_unlock(&mdsc->mutex);
3180 return;
3181 }
3182
3183 newmap = ceph_mdsmap_decode(&p, end);
3184 if (IS_ERR(newmap)) {
3185 err = PTR_ERR(newmap);
3186 goto bad_unlock;
3187 }
3188
3189 /* swap into place */
3190 if (mdsc->mdsmap) {
3191 oldmap = mdsc->mdsmap;
3192 mdsc->mdsmap = newmap;
3193 check_new_map(mdsc, newmap, oldmap);
3194 ceph_mdsmap_destroy(oldmap);
3195 } else {
3196 mdsc->mdsmap = newmap; /* first mds map */
3197 }
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003198 mdsc->fsc->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
Sage Weil2f2dc052009-10-06 11:31:09 -07003199
3200 __wake_requests(mdsc, &mdsc->waiting_for_map);
3201
3202 mutex_unlock(&mdsc->mutex);
3203 schedule_delayed(mdsc);
3204 return;
3205
3206bad_unlock:
3207 mutex_unlock(&mdsc->mutex);
3208bad:
3209 pr_err("error decoding mdsmap %d\n", err);
3210 return;
3211}
3212
3213static struct ceph_connection *con_get(struct ceph_connection *con)
3214{
3215 struct ceph_mds_session *s = con->private;
3216
3217 if (get_session(s)) {
Sage Weil2600d2d2010-02-22 15:12:16 -08003218 dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -07003219 return con;
3220 }
3221 dout("mdsc con_get %p FAIL\n", s);
3222 return NULL;
3223}
3224
3225static void con_put(struct ceph_connection *con)
3226{
3227 struct ceph_mds_session *s = con->private;
3228
Sage Weil2f2dc052009-10-06 11:31:09 -07003229 ceph_put_mds_session(s);
Sage Weil2600d2d2010-02-22 15:12:16 -08003230 dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -07003231}
3232
3233/*
3234 * if the client is unresponsive for long enough, the mds will kill
3235 * the session entirely.
3236 */
3237static void peer_reset(struct ceph_connection *con)
3238{
3239 struct ceph_mds_session *s = con->private;
Sage Weil7e70f0e2010-03-18 13:59:12 -07003240 struct ceph_mds_client *mdsc = s->s_mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07003241
Sage Weil7e70f0e2010-03-18 13:59:12 -07003242 pr_warning("mds%d closed our session\n", s->s_mds);
3243 send_mds_reconnect(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07003244}
3245
3246static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
3247{
3248 struct ceph_mds_session *s = con->private;
3249 struct ceph_mds_client *mdsc = s->s_mdsc;
3250 int type = le16_to_cpu(msg->hdr.type);
3251
Sage Weil2600d2d2010-02-22 15:12:16 -08003252 mutex_lock(&mdsc->mutex);
3253 if (__verify_registered_session(mdsc, s) < 0) {
3254 mutex_unlock(&mdsc->mutex);
3255 goto out;
3256 }
3257 mutex_unlock(&mdsc->mutex);
3258
Sage Weil2f2dc052009-10-06 11:31:09 -07003259 switch (type) {
3260 case CEPH_MSG_MDS_MAP:
3261 ceph_mdsc_handle_map(mdsc, msg);
3262 break;
3263 case CEPH_MSG_CLIENT_SESSION:
3264 handle_session(s, msg);
3265 break;
3266 case CEPH_MSG_CLIENT_REPLY:
3267 handle_reply(s, msg);
3268 break;
3269 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
Sage Weil2600d2d2010-02-22 15:12:16 -08003270 handle_forward(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003271 break;
3272 case CEPH_MSG_CLIENT_CAPS:
3273 ceph_handle_caps(s, msg);
3274 break;
3275 case CEPH_MSG_CLIENT_SNAP:
Sage Weil2600d2d2010-02-22 15:12:16 -08003276 ceph_handle_snap(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003277 break;
3278 case CEPH_MSG_CLIENT_LEASE:
Sage Weil2600d2d2010-02-22 15:12:16 -08003279 handle_lease(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003280 break;
3281
3282 default:
3283 pr_err("received unknown message type %d %s\n", type,
3284 ceph_msg_type_name(type));
3285 }
Sage Weil2600d2d2010-02-22 15:12:16 -08003286out:
Sage Weil2f2dc052009-10-06 11:31:09 -07003287 ceph_msg_put(msg);
3288}
3289
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003290/*
3291 * authentication
3292 */
3293static int get_authorizer(struct ceph_connection *con,
3294 void **buf, int *len, int *proto,
3295 void **reply_buf, int *reply_len, int force_new)
3296{
3297 struct ceph_mds_session *s = con->private;
3298 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003299 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003300 int ret = 0;
3301
3302 if (force_new && s->s_authorizer) {
3303 ac->ops->destroy_authorizer(ac, s->s_authorizer);
3304 s->s_authorizer = NULL;
3305 }
3306 if (s->s_authorizer == NULL) {
3307 if (ac->ops->create_authorizer) {
3308 ret = ac->ops->create_authorizer(
3309 ac, CEPH_ENTITY_TYPE_MDS,
3310 &s->s_authorizer,
3311 &s->s_authorizer_buf,
3312 &s->s_authorizer_buf_len,
3313 &s->s_authorizer_reply_buf,
3314 &s->s_authorizer_reply_buf_len);
3315 if (ret)
3316 return ret;
3317 }
3318 }
3319
3320 *proto = ac->protocol;
3321 *buf = s->s_authorizer_buf;
3322 *len = s->s_authorizer_buf_len;
3323 *reply_buf = s->s_authorizer_reply_buf;
3324 *reply_len = s->s_authorizer_reply_buf_len;
3325 return 0;
3326}
3327
3328
3329static int verify_authorizer_reply(struct ceph_connection *con, int len)
3330{
3331 struct ceph_mds_session *s = con->private;
3332 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003333 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003334
3335 return ac->ops->verify_authorizer_reply(ac, s->s_authorizer, len);
3336}
3337
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003338static int invalidate_authorizer(struct ceph_connection *con)
3339{
3340 struct ceph_mds_session *s = con->private;
3341 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003342 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003343
3344 if (ac->ops->invalidate_authorizer)
3345 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
3346
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003347 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003348}
3349
Tobias Klauser9e327892010-05-20 10:40:19 +02003350static const struct ceph_connection_operations mds_con_ops = {
Sage Weil2f2dc052009-10-06 11:31:09 -07003351 .get = con_get,
3352 .put = con_put,
3353 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003354 .get_authorizer = get_authorizer,
3355 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003356 .invalidate_authorizer = invalidate_authorizer,
Sage Weil2f2dc052009-10-06 11:31:09 -07003357 .peer_reset = peer_reset,
Sage Weil2f2dc052009-10-06 11:31:09 -07003358};
3359
Sage Weil2f2dc052009-10-06 11:31:09 -07003360/* eof */