blob: e22e8b41d5726d4e6f7ea577efbb7a010360984b [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weil2f2dc052009-10-06 11:31:09 -07002
Sage Weil496e5952010-09-22 19:57:10 -07003#include <linux/fs.h>
Sage Weil2f2dc052009-10-06 11:31:09 -07004#include <linux/wait.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09005#include <linux/slab.h>
Sage Weil2f2dc052009-10-06 11:31:09 -07006#include <linux/sched.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07007#include <linux/debugfs.h>
8#include <linux/seq_file.h>
Sage Weil2f2dc052009-10-06 11:31:09 -07009
Sage Weil2f2dc052009-10-06 11:31:09 -070010#include "super.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070011#include "mds_client.h"
12
13#include <linux/ceph/messenger.h>
14#include <linux/ceph/decode.h>
15#include <linux/ceph/pagelist.h>
16#include <linux/ceph/auth.h>
17#include <linux/ceph/debugfs.h>
Sage Weil2f2dc052009-10-06 11:31:09 -070018
19/*
20 * A cluster of MDS (metadata server) daemons is responsible for
21 * managing the file system namespace (the directory hierarchy and
22 * inodes) and for coordinating shared access to storage. Metadata is
23 * partitioning hierarchically across a number of servers, and that
24 * partition varies over time as the cluster adjusts the distribution
25 * in order to balance load.
26 *
27 * The MDS client is primarily responsible to managing synchronous
28 * metadata requests for operations like open, unlink, and so forth.
29 * If there is a MDS failure, we find out about it when we (possibly
30 * request and) receive a new MDS map, and can resubmit affected
31 * requests.
32 *
33 * For the most part, though, we take advantage of a lossless
34 * communications channel to the MDS, and do not need to worry about
35 * timing out or resubmitting requests.
36 *
37 * We maintain a stateful "session" with each MDS we interact with.
38 * Within each session, we sent periodic heartbeat messages to ensure
39 * any capabilities or leases we have been issues remain valid. If
40 * the session times out and goes stale, our leases and capabilities
41 * are no longer valid.
42 */
43
Sage Weil20cb34a2010-05-12 15:21:32 -070044struct ceph_reconnect_state {
45 struct ceph_pagelist *pagelist;
46 bool flock;
47};
48
Sage Weil2f2dc052009-10-06 11:31:09 -070049static void __wake_requests(struct ceph_mds_client *mdsc,
50 struct list_head *head);
51
Tobias Klauser9e327892010-05-20 10:40:19 +020052static const struct ceph_connection_operations mds_con_ops;
Sage Weil2f2dc052009-10-06 11:31:09 -070053
54
55/*
56 * mds reply parsing
57 */
58
59/*
60 * parse individual inode info
61 */
62static int parse_reply_info_in(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -080063 struct ceph_mds_reply_info_in *info,
64 int features)
Sage Weil2f2dc052009-10-06 11:31:09 -070065{
66 int err = -EIO;
67
68 info->in = *p;
69 *p += sizeof(struct ceph_mds_reply_inode) +
70 sizeof(*info->in->fragtree.splits) *
71 le32_to_cpu(info->in->fragtree.nsplits);
72
73 ceph_decode_32_safe(p, end, info->symlink_len, bad);
74 ceph_decode_need(p, end, info->symlink_len, bad);
75 info->symlink = *p;
76 *p += info->symlink_len;
77
Sage Weil14303d22010-12-14 17:37:52 -080078 if (features & CEPH_FEATURE_DIRLAYOUTHASH)
79 ceph_decode_copy_safe(p, end, &info->dir_layout,
80 sizeof(info->dir_layout), bad);
81 else
82 memset(&info->dir_layout, 0, sizeof(info->dir_layout));
83
Sage Weil2f2dc052009-10-06 11:31:09 -070084 ceph_decode_32_safe(p, end, info->xattr_len, bad);
85 ceph_decode_need(p, end, info->xattr_len, bad);
86 info->xattr_data = *p;
87 *p += info->xattr_len;
88 return 0;
89bad:
90 return err;
91}
92
93/*
94 * parse a normal reply, which may contain a (dir+)dentry and/or a
95 * target inode.
96 */
97static int parse_reply_info_trace(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -080098 struct ceph_mds_reply_info_parsed *info,
99 int features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700100{
101 int err;
102
103 if (info->head->is_dentry) {
Sage Weil14303d22010-12-14 17:37:52 -0800104 err = parse_reply_info_in(p, end, &info->diri, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700105 if (err < 0)
106 goto out_bad;
107
108 if (unlikely(*p + sizeof(*info->dirfrag) > end))
109 goto bad;
110 info->dirfrag = *p;
111 *p += sizeof(*info->dirfrag) +
112 sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
113 if (unlikely(*p > end))
114 goto bad;
115
116 ceph_decode_32_safe(p, end, info->dname_len, bad);
117 ceph_decode_need(p, end, info->dname_len, bad);
118 info->dname = *p;
119 *p += info->dname_len;
120 info->dlease = *p;
121 *p += sizeof(*info->dlease);
122 }
123
124 if (info->head->is_target) {
Sage Weil14303d22010-12-14 17:37:52 -0800125 err = parse_reply_info_in(p, end, &info->targeti, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700126 if (err < 0)
127 goto out_bad;
128 }
129
130 if (unlikely(*p != end))
131 goto bad;
132 return 0;
133
134bad:
135 err = -EIO;
136out_bad:
137 pr_err("problem parsing mds trace %d\n", err);
138 return err;
139}
140
141/*
142 * parse readdir results
143 */
144static int parse_reply_info_dir(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800145 struct ceph_mds_reply_info_parsed *info,
146 int features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700147{
148 u32 num, i = 0;
149 int err;
150
151 info->dir_dir = *p;
152 if (*p + sizeof(*info->dir_dir) > end)
153 goto bad;
154 *p += sizeof(*info->dir_dir) +
155 sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
156 if (*p > end)
157 goto bad;
158
159 ceph_decode_need(p, end, sizeof(num) + 2, bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700160 num = ceph_decode_32(p);
161 info->dir_end = ceph_decode_8(p);
162 info->dir_complete = ceph_decode_8(p);
Sage Weil2f2dc052009-10-06 11:31:09 -0700163 if (num == 0)
164 goto done;
165
166 /* alloc large array */
167 info->dir_nr = num;
168 info->dir_in = kcalloc(num, sizeof(*info->dir_in) +
169 sizeof(*info->dir_dname) +
170 sizeof(*info->dir_dname_len) +
171 sizeof(*info->dir_dlease),
172 GFP_NOFS);
173 if (info->dir_in == NULL) {
174 err = -ENOMEM;
175 goto out_bad;
176 }
177 info->dir_dname = (void *)(info->dir_in + num);
178 info->dir_dname_len = (void *)(info->dir_dname + num);
179 info->dir_dlease = (void *)(info->dir_dname_len + num);
180
181 while (num) {
182 /* dentry */
183 ceph_decode_need(p, end, sizeof(u32)*2, bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700184 info->dir_dname_len[i] = ceph_decode_32(p);
Sage Weil2f2dc052009-10-06 11:31:09 -0700185 ceph_decode_need(p, end, info->dir_dname_len[i], bad);
186 info->dir_dname[i] = *p;
187 *p += info->dir_dname_len[i];
188 dout("parsed dir dname '%.*s'\n", info->dir_dname_len[i],
189 info->dir_dname[i]);
190 info->dir_dlease[i] = *p;
191 *p += sizeof(struct ceph_mds_reply_lease);
192
193 /* inode */
Sage Weil14303d22010-12-14 17:37:52 -0800194 err = parse_reply_info_in(p, end, &info->dir_in[i], features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700195 if (err < 0)
196 goto out_bad;
197 i++;
198 num--;
199 }
200
201done:
202 if (*p != end)
203 goto bad;
204 return 0;
205
206bad:
207 err = -EIO;
208out_bad:
209 pr_err("problem parsing dir contents %d\n", err);
210 return err;
211}
212
213/*
Herb Shiu25933ab2010-12-01 14:14:38 -0800214 * parse fcntl F_GETLK results
215 */
216static int parse_reply_info_filelock(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800217 struct ceph_mds_reply_info_parsed *info,
218 int features)
Herb Shiu25933ab2010-12-01 14:14:38 -0800219{
220 if (*p + sizeof(*info->filelock_reply) > end)
221 goto bad;
222
223 info->filelock_reply = *p;
224 *p += sizeof(*info->filelock_reply);
225
226 if (unlikely(*p != end))
227 goto bad;
228 return 0;
229
230bad:
231 return -EIO;
232}
233
234/*
235 * parse extra results
236 */
237static int parse_reply_info_extra(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800238 struct ceph_mds_reply_info_parsed *info,
239 int features)
Herb Shiu25933ab2010-12-01 14:14:38 -0800240{
241 if (info->head->op == CEPH_MDS_OP_GETFILELOCK)
Sage Weil14303d22010-12-14 17:37:52 -0800242 return parse_reply_info_filelock(p, end, info, features);
Herb Shiu25933ab2010-12-01 14:14:38 -0800243 else
Sage Weil14303d22010-12-14 17:37:52 -0800244 return parse_reply_info_dir(p, end, info, features);
Herb Shiu25933ab2010-12-01 14:14:38 -0800245}
246
247/*
Sage Weil2f2dc052009-10-06 11:31:09 -0700248 * parse entire mds reply
249 */
250static int parse_reply_info(struct ceph_msg *msg,
Sage Weil14303d22010-12-14 17:37:52 -0800251 struct ceph_mds_reply_info_parsed *info,
252 int features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700253{
254 void *p, *end;
255 u32 len;
256 int err;
257
258 info->head = msg->front.iov_base;
259 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
260 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
261
262 /* trace */
263 ceph_decode_32_safe(&p, end, len, bad);
264 if (len > 0) {
Sage Weil14303d22010-12-14 17:37:52 -0800265 err = parse_reply_info_trace(&p, p+len, info, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700266 if (err < 0)
267 goto out_bad;
268 }
269
Herb Shiu25933ab2010-12-01 14:14:38 -0800270 /* extra */
Sage Weil2f2dc052009-10-06 11:31:09 -0700271 ceph_decode_32_safe(&p, end, len, bad);
272 if (len > 0) {
Sage Weil14303d22010-12-14 17:37:52 -0800273 err = parse_reply_info_extra(&p, p+len, info, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700274 if (err < 0)
275 goto out_bad;
276 }
277
278 /* snap blob */
279 ceph_decode_32_safe(&p, end, len, bad);
280 info->snapblob_len = len;
281 info->snapblob = p;
282 p += len;
283
284 if (p != end)
285 goto bad;
286 return 0;
287
288bad:
289 err = -EIO;
290out_bad:
291 pr_err("mds parse_reply err %d\n", err);
292 return err;
293}
294
295static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
296{
297 kfree(info->dir_in);
298}
299
300
301/*
302 * sessions
303 */
304static const char *session_state_name(int s)
305{
306 switch (s) {
307 case CEPH_MDS_SESSION_NEW: return "new";
308 case CEPH_MDS_SESSION_OPENING: return "opening";
309 case CEPH_MDS_SESSION_OPEN: return "open";
310 case CEPH_MDS_SESSION_HUNG: return "hung";
311 case CEPH_MDS_SESSION_CLOSING: return "closing";
Sage Weil44ca18f2010-02-15 12:08:46 -0800312 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
Sage Weil2f2dc052009-10-06 11:31:09 -0700313 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
314 default: return "???";
315 }
316}
317
318static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
319{
320 if (atomic_inc_not_zero(&s->s_ref)) {
321 dout("mdsc get_session %p %d -> %d\n", s,
322 atomic_read(&s->s_ref)-1, atomic_read(&s->s_ref));
323 return s;
324 } else {
325 dout("mdsc get_session %p 0 -- FAIL", s);
326 return NULL;
327 }
328}
329
330void ceph_put_mds_session(struct ceph_mds_session *s)
331{
332 dout("mdsc put_session %p %d -> %d\n", s,
333 atomic_read(&s->s_ref), atomic_read(&s->s_ref)-1);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800334 if (atomic_dec_and_test(&s->s_ref)) {
335 if (s->s_authorizer)
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700336 s->s_mdsc->fsc->client->monc.auth->ops->destroy_authorizer(
337 s->s_mdsc->fsc->client->monc.auth,
338 s->s_authorizer);
Sage Weil2f2dc052009-10-06 11:31:09 -0700339 kfree(s);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800340 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700341}
342
343/*
344 * called under mdsc->mutex
345 */
346struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
347 int mds)
348{
349 struct ceph_mds_session *session;
350
351 if (mds >= mdsc->max_sessions || mdsc->sessions[mds] == NULL)
352 return NULL;
353 session = mdsc->sessions[mds];
354 dout("lookup_mds_session %p %d\n", session,
355 atomic_read(&session->s_ref));
356 get_session(session);
357 return session;
358}
359
360static bool __have_session(struct ceph_mds_client *mdsc, int mds)
361{
362 if (mds >= mdsc->max_sessions)
363 return false;
364 return mdsc->sessions[mds];
365}
366
Sage Weil2600d2d2010-02-22 15:12:16 -0800367static int __verify_registered_session(struct ceph_mds_client *mdsc,
368 struct ceph_mds_session *s)
369{
370 if (s->s_mds >= mdsc->max_sessions ||
371 mdsc->sessions[s->s_mds] != s)
372 return -ENOENT;
373 return 0;
374}
375
Sage Weil2f2dc052009-10-06 11:31:09 -0700376/*
377 * create+register a new session for given mds.
378 * called under mdsc->mutex.
379 */
380static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
381 int mds)
382{
383 struct ceph_mds_session *s;
384
385 s = kzalloc(sizeof(*s), GFP_NOFS);
Dan Carpenter4736b002010-03-20 15:30:16 +0300386 if (!s)
387 return ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -0700388 s->s_mdsc = mdsc;
389 s->s_mds = mds;
390 s->s_state = CEPH_MDS_SESSION_NEW;
391 s->s_ttl = 0;
392 s->s_seq = 0;
393 mutex_init(&s->s_mutex);
394
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700395 ceph_con_init(mdsc->fsc->client->msgr, &s->s_con);
Sage Weil2f2dc052009-10-06 11:31:09 -0700396 s->s_con.private = s;
397 s->s_con.ops = &mds_con_ops;
398 s->s_con.peer_name.type = CEPH_ENTITY_TYPE_MDS;
399 s->s_con.peer_name.num = cpu_to_le64(mds);
Sage Weil2f2dc052009-10-06 11:31:09 -0700400
401 spin_lock_init(&s->s_cap_lock);
402 s->s_cap_gen = 0;
403 s->s_cap_ttl = 0;
404 s->s_renew_requested = 0;
405 s->s_renew_seq = 0;
406 INIT_LIST_HEAD(&s->s_caps);
407 s->s_nr_caps = 0;
Sage Weil5dacf092009-12-21 20:40:34 -0800408 s->s_trim_caps = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -0700409 atomic_set(&s->s_ref, 1);
410 INIT_LIST_HEAD(&s->s_waiting);
411 INIT_LIST_HEAD(&s->s_unsafe);
412 s->s_num_cap_releases = 0;
Sage Weil7c1332b2010-02-16 11:39:45 -0800413 s->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700414 INIT_LIST_HEAD(&s->s_cap_releases);
415 INIT_LIST_HEAD(&s->s_cap_releases_done);
416 INIT_LIST_HEAD(&s->s_cap_flushing);
417 INIT_LIST_HEAD(&s->s_cap_snaps_flushing);
418
419 dout("register_session mds%d\n", mds);
420 if (mds >= mdsc->max_sessions) {
421 int newmax = 1 << get_count_order(mds+1);
422 struct ceph_mds_session **sa;
423
424 dout("register_session realloc to %d\n", newmax);
425 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
426 if (sa == NULL)
Sage Weil42ce56e2009-11-18 11:22:36 -0800427 goto fail_realloc;
Sage Weil2f2dc052009-10-06 11:31:09 -0700428 if (mdsc->sessions) {
429 memcpy(sa, mdsc->sessions,
430 mdsc->max_sessions * sizeof(void *));
431 kfree(mdsc->sessions);
432 }
433 mdsc->sessions = sa;
434 mdsc->max_sessions = newmax;
435 }
436 mdsc->sessions[mds] = s;
437 atomic_inc(&s->s_ref); /* one ref to sessions[], one to caller */
Sage Weil42ce56e2009-11-18 11:22:36 -0800438
439 ceph_con_open(&s->s_con, ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
440
Sage Weil2f2dc052009-10-06 11:31:09 -0700441 return s;
Sage Weil42ce56e2009-11-18 11:22:36 -0800442
443fail_realloc:
444 kfree(s);
445 return ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -0700446}
447
448/*
449 * called under mdsc->mutex
450 */
Sage Weil2600d2d2010-02-22 15:12:16 -0800451static void __unregister_session(struct ceph_mds_client *mdsc,
Sage Weil42ce56e2009-11-18 11:22:36 -0800452 struct ceph_mds_session *s)
Sage Weil2f2dc052009-10-06 11:31:09 -0700453{
Sage Weil2600d2d2010-02-22 15:12:16 -0800454 dout("__unregister_session mds%d %p\n", s->s_mds, s);
455 BUG_ON(mdsc->sessions[s->s_mds] != s);
Sage Weil42ce56e2009-11-18 11:22:36 -0800456 mdsc->sessions[s->s_mds] = NULL;
457 ceph_con_close(&s->s_con);
458 ceph_put_mds_session(s);
Sage Weil2f2dc052009-10-06 11:31:09 -0700459}
460
461/*
462 * drop session refs in request.
463 *
464 * should be last request ref, or hold mdsc->mutex
465 */
466static void put_request_session(struct ceph_mds_request *req)
467{
468 if (req->r_session) {
469 ceph_put_mds_session(req->r_session);
470 req->r_session = NULL;
471 }
472}
473
Sage Weil153c8e62009-12-07 12:31:09 -0800474void ceph_mdsc_release_request(struct kref *kref)
Sage Weil2f2dc052009-10-06 11:31:09 -0700475{
Sage Weil153c8e62009-12-07 12:31:09 -0800476 struct ceph_mds_request *req = container_of(kref,
477 struct ceph_mds_request,
478 r_kref);
479 if (req->r_request)
480 ceph_msg_put(req->r_request);
481 if (req->r_reply) {
482 ceph_msg_put(req->r_reply);
483 destroy_reply_info(&req->r_reply_info);
Sage Weil2f2dc052009-10-06 11:31:09 -0700484 }
Sage Weil153c8e62009-12-07 12:31:09 -0800485 if (req->r_inode) {
486 ceph_put_cap_refs(ceph_inode(req->r_inode),
487 CEPH_CAP_PIN);
488 iput(req->r_inode);
489 }
490 if (req->r_locked_dir)
491 ceph_put_cap_refs(ceph_inode(req->r_locked_dir),
492 CEPH_CAP_PIN);
493 if (req->r_target_inode)
494 iput(req->r_target_inode);
495 if (req->r_dentry)
496 dput(req->r_dentry);
497 if (req->r_old_dentry) {
498 ceph_put_cap_refs(
499 ceph_inode(req->r_old_dentry->d_parent->d_inode),
500 CEPH_CAP_PIN);
501 dput(req->r_old_dentry);
502 }
503 kfree(req->r_path1);
504 kfree(req->r_path2);
505 put_request_session(req);
Yehuda Sadeh37151662010-06-17 16:16:12 -0700506 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
Sage Weil153c8e62009-12-07 12:31:09 -0800507 kfree(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700508}
509
510/*
511 * lookup session, bump ref if found.
512 *
513 * called under mdsc->mutex.
514 */
515static struct ceph_mds_request *__lookup_request(struct ceph_mds_client *mdsc,
516 u64 tid)
517{
518 struct ceph_mds_request *req;
Sage Weil44ca18f2010-02-15 12:08:46 -0800519 struct rb_node *n = mdsc->request_tree.rb_node;
520
521 while (n) {
522 req = rb_entry(n, struct ceph_mds_request, r_node);
523 if (tid < req->r_tid)
524 n = n->rb_left;
525 else if (tid > req->r_tid)
526 n = n->rb_right;
527 else {
528 ceph_mdsc_get_request(req);
529 return req;
530 }
531 }
532 return NULL;
533}
534
535static void __insert_request(struct ceph_mds_client *mdsc,
536 struct ceph_mds_request *new)
537{
538 struct rb_node **p = &mdsc->request_tree.rb_node;
539 struct rb_node *parent = NULL;
540 struct ceph_mds_request *req = NULL;
541
542 while (*p) {
543 parent = *p;
544 req = rb_entry(parent, struct ceph_mds_request, r_node);
545 if (new->r_tid < req->r_tid)
546 p = &(*p)->rb_left;
547 else if (new->r_tid > req->r_tid)
548 p = &(*p)->rb_right;
549 else
550 BUG();
551 }
552
553 rb_link_node(&new->r_node, parent, p);
554 rb_insert_color(&new->r_node, &mdsc->request_tree);
Sage Weil2f2dc052009-10-06 11:31:09 -0700555}
556
557/*
558 * Register an in-flight request, and assign a tid. Link to directory
559 * are modifying (if any).
560 *
561 * Called under mdsc->mutex.
562 */
563static void __register_request(struct ceph_mds_client *mdsc,
564 struct ceph_mds_request *req,
565 struct inode *dir)
566{
567 req->r_tid = ++mdsc->last_tid;
568 if (req->r_num_caps)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700569 ceph_reserve_caps(mdsc, &req->r_caps_reservation,
570 req->r_num_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -0700571 dout("__register_request %p tid %lld\n", req, req->r_tid);
572 ceph_mdsc_get_request(req);
Sage Weil44ca18f2010-02-15 12:08:46 -0800573 __insert_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700574
Sage Weilcb4276c2010-11-08 07:28:52 -0800575 req->r_uid = current_fsuid();
576 req->r_gid = current_fsgid();
577
Sage Weil2f2dc052009-10-06 11:31:09 -0700578 if (dir) {
579 struct ceph_inode_info *ci = ceph_inode(dir);
580
581 spin_lock(&ci->i_unsafe_lock);
582 req->r_unsafe_dir = dir;
583 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
584 spin_unlock(&ci->i_unsafe_lock);
585 }
586}
587
588static void __unregister_request(struct ceph_mds_client *mdsc,
589 struct ceph_mds_request *req)
590{
591 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
Sage Weil44ca18f2010-02-15 12:08:46 -0800592 rb_erase(&req->r_node, &mdsc->request_tree);
Sage Weil80fc7312010-03-16 15:28:54 -0700593 RB_CLEAR_NODE(&req->r_node);
Sage Weil2f2dc052009-10-06 11:31:09 -0700594
595 if (req->r_unsafe_dir) {
596 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
597
598 spin_lock(&ci->i_unsafe_lock);
599 list_del_init(&req->r_unsafe_dir_item);
600 spin_unlock(&ci->i_unsafe_lock);
601 }
Sage Weil94aa8ae2010-03-28 21:22:50 -0700602
603 ceph_mdsc_put_request(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700604}
605
606/*
607 * Choose mds to send request to next. If there is a hint set in the
608 * request (e.g., due to a prior forward hint from the mds), use that.
609 * Otherwise, consult frag tree and/or caps to identify the
610 * appropriate mds. If all else fails, choose randomly.
611 *
612 * Called under mdsc->mutex.
613 */
Sage Weileb6bb1c2010-08-16 09:21:27 -0700614struct dentry *get_nonsnap_parent(struct dentry *dentry)
615{
616 while (!IS_ROOT(dentry) && ceph_snap(dentry->d_inode) != CEPH_NOSNAP)
617 dentry = dentry->d_parent;
618 return dentry;
619}
620
Sage Weil2f2dc052009-10-06 11:31:09 -0700621static int __choose_mds(struct ceph_mds_client *mdsc,
622 struct ceph_mds_request *req)
623{
624 struct inode *inode;
625 struct ceph_inode_info *ci;
626 struct ceph_cap *cap;
627 int mode = req->r_direct_mode;
628 int mds = -1;
629 u32 hash = req->r_direct_hash;
630 bool is_hash = req->r_direct_is_hash;
631
632 /*
633 * is there a specific mds we should try? ignore hint if we have
634 * no session and the mds is not up (active or recovering).
635 */
636 if (req->r_resend_mds >= 0 &&
637 (__have_session(mdsc, req->r_resend_mds) ||
638 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
639 dout("choose_mds using resend_mds mds%d\n",
640 req->r_resend_mds);
641 return req->r_resend_mds;
642 }
643
644 if (mode == USE_RANDOM_MDS)
645 goto random;
646
647 inode = NULL;
648 if (req->r_inode) {
649 inode = req->r_inode;
650 } else if (req->r_dentry) {
Sage Weileb6bb1c2010-08-16 09:21:27 -0700651 struct inode *dir = req->r_dentry->d_parent->d_inode;
652
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700653 if (dir->i_sb != mdsc->fsc->sb) {
Sage Weileb6bb1c2010-08-16 09:21:27 -0700654 /* not this fs! */
655 inode = req->r_dentry->d_inode;
656 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
657 /* direct snapped/virtual snapdir requests
658 * based on parent dir inode */
659 struct dentry *dn =
660 get_nonsnap_parent(req->r_dentry->d_parent);
661 inode = dn->d_inode;
662 dout("__choose_mds using nonsnap parent %p\n", inode);
663 } else if (req->r_dentry->d_inode) {
664 /* dentry target */
Sage Weil2f2dc052009-10-06 11:31:09 -0700665 inode = req->r_dentry->d_inode;
666 } else {
Sage Weileb6bb1c2010-08-16 09:21:27 -0700667 /* dir + name */
668 inode = dir;
Sage Weil14303d22010-12-14 17:37:52 -0800669 hash = ceph_dentry_hash(req->r_dentry);
Sage Weil2f2dc052009-10-06 11:31:09 -0700670 is_hash = true;
671 }
672 }
Sage Weileb6bb1c2010-08-16 09:21:27 -0700673
Sage Weil2f2dc052009-10-06 11:31:09 -0700674 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
675 (int)hash, mode);
676 if (!inode)
677 goto random;
678 ci = ceph_inode(inode);
679
680 if (is_hash && S_ISDIR(inode->i_mode)) {
681 struct ceph_inode_frag frag;
682 int found;
683
684 ceph_choose_frag(ci, hash, &frag, &found);
685 if (found) {
686 if (mode == USE_ANY_MDS && frag.ndist > 0) {
687 u8 r;
688
689 /* choose a random replica */
690 get_random_bytes(&r, 1);
691 r %= frag.ndist;
692 mds = frag.dist[r];
693 dout("choose_mds %p %llx.%llx "
694 "frag %u mds%d (%d/%d)\n",
695 inode, ceph_vinop(inode),
696 frag.frag, frag.mds,
697 (int)r, frag.ndist);
698 return mds;
699 }
700
701 /* since this file/dir wasn't known to be
702 * replicated, then we want to look for the
703 * authoritative mds. */
704 mode = USE_AUTH_MDS;
705 if (frag.mds >= 0) {
706 /* choose auth mds */
707 mds = frag.mds;
708 dout("choose_mds %p %llx.%llx "
709 "frag %u mds%d (auth)\n",
710 inode, ceph_vinop(inode), frag.frag, mds);
711 return mds;
712 }
713 }
714 }
715
716 spin_lock(&inode->i_lock);
717 cap = NULL;
718 if (mode == USE_AUTH_MDS)
719 cap = ci->i_auth_cap;
720 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
721 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
722 if (!cap) {
723 spin_unlock(&inode->i_lock);
724 goto random;
725 }
726 mds = cap->session->s_mds;
727 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
728 inode, ceph_vinop(inode), mds,
729 cap == ci->i_auth_cap ? "auth " : "", cap);
730 spin_unlock(&inode->i_lock);
731 return mds;
732
733random:
734 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
735 dout("choose_mds chose random mds%d\n", mds);
736 return mds;
737}
738
739
740/*
741 * session messages
742 */
743static struct ceph_msg *create_session_msg(u32 op, u64 seq)
744{
745 struct ceph_msg *msg;
746 struct ceph_mds_session_head *h;
747
Yehuda Sadeh34d23762010-04-06 14:33:58 -0700748 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -0700749 if (!msg) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700750 pr_err("create_session_msg ENOMEM creating msg\n");
Sage Weila79832f2010-04-01 16:06:19 -0700751 return NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700752 }
753 h = msg->front.iov_base;
754 h->op = cpu_to_le32(op);
755 h->seq = cpu_to_le64(seq);
756 return msg;
757}
758
759/*
760 * send session open request.
761 *
762 * called under mdsc->mutex
763 */
764static int __open_session(struct ceph_mds_client *mdsc,
765 struct ceph_mds_session *session)
766{
767 struct ceph_msg *msg;
768 int mstate;
769 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -0700770
771 /* wait for mds to go active? */
772 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
773 dout("open_session to mds%d (%s)\n", mds,
774 ceph_mds_state_name(mstate));
775 session->s_state = CEPH_MDS_SESSION_OPENING;
776 session->s_renew_requested = jiffies;
777
778 /* send connect message */
779 msg = create_session_msg(CEPH_SESSION_REQUEST_OPEN, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -0700780 if (!msg)
781 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -0700782 ceph_con_send(&session->s_con, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -0700783 return 0;
784}
785
786/*
Sage Weiled0552a2010-06-21 13:38:25 -0700787 * open sessions for any export targets for the given mds
788 *
789 * called under mdsc->mutex
790 */
791static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
792 struct ceph_mds_session *session)
793{
794 struct ceph_mds_info *mi;
795 struct ceph_mds_session *ts;
796 int i, mds = session->s_mds;
797 int target;
798
799 if (mds >= mdsc->mdsmap->m_max_mds)
800 return;
801 mi = &mdsc->mdsmap->m_info[mds];
802 dout("open_export_target_sessions for mds%d (%d targets)\n",
803 session->s_mds, mi->num_export_targets);
804
805 for (i = 0; i < mi->num_export_targets; i++) {
806 target = mi->export_targets[i];
807 ts = __ceph_lookup_mds_session(mdsc, target);
808 if (!ts) {
809 ts = register_session(mdsc, target);
810 if (IS_ERR(ts))
811 return;
812 }
813 if (session->s_state == CEPH_MDS_SESSION_NEW ||
814 session->s_state == CEPH_MDS_SESSION_CLOSING)
815 __open_session(mdsc, session);
816 else
817 dout(" mds%d target mds%d %p is %s\n", session->s_mds,
818 i, ts, session_state_name(ts->s_state));
819 ceph_put_mds_session(ts);
820 }
821}
822
Sage Weil154f42c2010-06-21 13:45:04 -0700823void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
824 struct ceph_mds_session *session)
825{
826 mutex_lock(&mdsc->mutex);
827 __open_export_target_sessions(mdsc, session);
828 mutex_unlock(&mdsc->mutex);
829}
830
Sage Weiled0552a2010-06-21 13:38:25 -0700831/*
Sage Weil2f2dc052009-10-06 11:31:09 -0700832 * session caps
833 */
834
835/*
836 * Free preallocated cap messages assigned to this session
837 */
838static void cleanup_cap_releases(struct ceph_mds_session *session)
839{
840 struct ceph_msg *msg;
841
842 spin_lock(&session->s_cap_lock);
843 while (!list_empty(&session->s_cap_releases)) {
844 msg = list_first_entry(&session->s_cap_releases,
845 struct ceph_msg, list_head);
846 list_del_init(&msg->list_head);
847 ceph_msg_put(msg);
848 }
849 while (!list_empty(&session->s_cap_releases_done)) {
850 msg = list_first_entry(&session->s_cap_releases_done,
851 struct ceph_msg, list_head);
852 list_del_init(&msg->list_head);
853 ceph_msg_put(msg);
854 }
855 spin_unlock(&session->s_cap_lock);
856}
857
858/*
Sage Weilf818a732010-05-11 20:56:31 -0700859 * Helper to safely iterate over all caps associated with a session, with
860 * special care taken to handle a racing __ceph_remove_cap().
Sage Weil2f2dc052009-10-06 11:31:09 -0700861 *
Sage Weilf818a732010-05-11 20:56:31 -0700862 * Caller must hold session s_mutex.
Sage Weil2f2dc052009-10-06 11:31:09 -0700863 */
864static int iterate_session_caps(struct ceph_mds_session *session,
865 int (*cb)(struct inode *, struct ceph_cap *,
866 void *), void *arg)
867{
Sage Weil7c1332b2010-02-16 11:39:45 -0800868 struct list_head *p;
869 struct ceph_cap *cap;
870 struct inode *inode, *last_inode = NULL;
871 struct ceph_cap *old_cap = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700872 int ret;
873
874 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
875 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800876 p = session->s_caps.next;
877 while (p != &session->s_caps) {
878 cap = list_entry(p, struct ceph_cap, session_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -0700879 inode = igrab(&cap->ci->vfs_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -0800880 if (!inode) {
881 p = p->next;
Sage Weil2f2dc052009-10-06 11:31:09 -0700882 continue;
Sage Weil7c1332b2010-02-16 11:39:45 -0800883 }
884 session->s_cap_iterator = cap;
Sage Weil2f2dc052009-10-06 11:31:09 -0700885 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800886
887 if (last_inode) {
888 iput(last_inode);
889 last_inode = NULL;
890 }
891 if (old_cap) {
Yehuda Sadeh37151662010-06-17 16:16:12 -0700892 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -0800893 old_cap = NULL;
894 }
895
Sage Weil2f2dc052009-10-06 11:31:09 -0700896 ret = cb(inode, cap, arg);
Sage Weil7c1332b2010-02-16 11:39:45 -0800897 last_inode = inode;
898
Sage Weil2f2dc052009-10-06 11:31:09 -0700899 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800900 p = p->next;
901 if (cap->ci == NULL) {
902 dout("iterate_session_caps finishing cap %p removal\n",
903 cap);
904 BUG_ON(cap->session != session);
905 list_del_init(&cap->session_caps);
906 session->s_nr_caps--;
907 cap->session = NULL;
908 old_cap = cap; /* put_cap it w/o locks held */
909 }
Sage Weil5dacf092009-12-21 20:40:34 -0800910 if (ret < 0)
911 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -0700912 }
Sage Weil5dacf092009-12-21 20:40:34 -0800913 ret = 0;
914out:
Sage Weil7c1332b2010-02-16 11:39:45 -0800915 session->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700916 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800917
918 if (last_inode)
919 iput(last_inode);
920 if (old_cap)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700921 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -0800922
Sage Weil5dacf092009-12-21 20:40:34 -0800923 return ret;
Sage Weil2f2dc052009-10-06 11:31:09 -0700924}
925
926static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
Sage Weil6c99f252010-05-10 16:12:25 -0700927 void *arg)
Sage Weil2f2dc052009-10-06 11:31:09 -0700928{
929 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil6c99f252010-05-10 16:12:25 -0700930 int drop = 0;
931
Sage Weil2f2dc052009-10-06 11:31:09 -0700932 dout("removing cap %p, ci is %p, inode is %p\n",
933 cap, ci, &ci->vfs_inode);
Sage Weil6c99f252010-05-10 16:12:25 -0700934 spin_lock(&inode->i_lock);
935 __ceph_remove_cap(cap);
936 if (!__ceph_is_any_real_caps(ci)) {
937 struct ceph_mds_client *mdsc =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700938 ceph_sb_to_client(inode->i_sb)->mdsc;
Sage Weil6c99f252010-05-10 16:12:25 -0700939
940 spin_lock(&mdsc->cap_dirty_lock);
941 if (!list_empty(&ci->i_dirty_item)) {
942 pr_info(" dropping dirty %s state for %p %lld\n",
943 ceph_cap_string(ci->i_dirty_caps),
944 inode, ceph_ino(inode));
945 ci->i_dirty_caps = 0;
946 list_del_init(&ci->i_dirty_item);
947 drop = 1;
948 }
949 if (!list_empty(&ci->i_flushing_item)) {
950 pr_info(" dropping dirty+flushing %s state for %p %lld\n",
951 ceph_cap_string(ci->i_flushing_caps),
952 inode, ceph_ino(inode));
953 ci->i_flushing_caps = 0;
954 list_del_init(&ci->i_flushing_item);
955 mdsc->num_cap_flushing--;
956 drop = 1;
957 }
958 if (drop && ci->i_wrbuffer_ref) {
959 pr_info(" dropping dirty data for %p %lld\n",
960 inode, ceph_ino(inode));
961 ci->i_wrbuffer_ref = 0;
962 ci->i_wrbuffer_ref_head = 0;
963 drop++;
964 }
965 spin_unlock(&mdsc->cap_dirty_lock);
966 }
967 spin_unlock(&inode->i_lock);
968 while (drop--)
969 iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -0700970 return 0;
971}
972
973/*
974 * caller must hold session s_mutex
975 */
976static void remove_session_caps(struct ceph_mds_session *session)
977{
978 dout("remove_session_caps on %p\n", session);
979 iterate_session_caps(session, remove_session_caps_cb, NULL);
980 BUG_ON(session->s_nr_caps > 0);
Sage Weil6c99f252010-05-10 16:12:25 -0700981 BUG_ON(!list_empty(&session->s_cap_flushing));
Sage Weil2f2dc052009-10-06 11:31:09 -0700982 cleanup_cap_releases(session);
983}
984
985/*
986 * wake up any threads waiting on this session's caps. if the cap is
987 * old (didn't get renewed on the client reconnect), remove it now.
988 *
989 * caller must hold s_mutex.
990 */
991static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
992 void *arg)
993{
Sage Weil0dc25702009-11-20 13:43:45 -0800994 struct ceph_inode_info *ci = ceph_inode(inode);
995
Yehuda Sadeh03066f22010-07-27 13:11:08 -0700996 wake_up_all(&ci->i_cap_wq);
Sage Weil0dc25702009-11-20 13:43:45 -0800997 if (arg) {
998 spin_lock(&inode->i_lock);
999 ci->i_wanted_max_size = 0;
1000 ci->i_requested_max_size = 0;
1001 spin_unlock(&inode->i_lock);
1002 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001003 return 0;
1004}
1005
Sage Weil0dc25702009-11-20 13:43:45 -08001006static void wake_up_session_caps(struct ceph_mds_session *session,
1007 int reconnect)
Sage Weil2f2dc052009-10-06 11:31:09 -07001008{
1009 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
Sage Weil0dc25702009-11-20 13:43:45 -08001010 iterate_session_caps(session, wake_up_session_cb,
1011 (void *)(unsigned long)reconnect);
Sage Weil2f2dc052009-10-06 11:31:09 -07001012}
1013
1014/*
1015 * Send periodic message to MDS renewing all currently held caps. The
1016 * ack will reset the expiration for all caps from this session.
1017 *
1018 * caller holds s_mutex
1019 */
1020static int send_renew_caps(struct ceph_mds_client *mdsc,
1021 struct ceph_mds_session *session)
1022{
1023 struct ceph_msg *msg;
1024 int state;
1025
1026 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1027 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1028 pr_info("mds%d caps stale\n", session->s_mds);
Sage Weile4cb4cb2010-03-18 13:43:09 -07001029 session->s_renew_requested = jiffies;
Sage Weil2f2dc052009-10-06 11:31:09 -07001030
1031 /* do not try to renew caps until a recovering mds has reconnected
1032 * with its clients. */
1033 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1034 if (state < CEPH_MDS_STATE_RECONNECT) {
1035 dout("send_renew_caps ignoring mds%d (%s)\n",
1036 session->s_mds, ceph_mds_state_name(state));
1037 return 0;
1038 }
1039
1040 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1041 ceph_mds_state_name(state));
Sage Weil2f2dc052009-10-06 11:31:09 -07001042 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1043 ++session->s_renew_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001044 if (!msg)
1045 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -07001046 ceph_con_send(&session->s_con, msg);
1047 return 0;
1048}
1049
1050/*
1051 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
Sage Weil0dc25702009-11-20 13:43:45 -08001052 *
1053 * Called under session->s_mutex
Sage Weil2f2dc052009-10-06 11:31:09 -07001054 */
1055static void renewed_caps(struct ceph_mds_client *mdsc,
1056 struct ceph_mds_session *session, int is_renew)
1057{
1058 int was_stale;
1059 int wake = 0;
1060
1061 spin_lock(&session->s_cap_lock);
1062 was_stale = is_renew && (session->s_cap_ttl == 0 ||
1063 time_after_eq(jiffies, session->s_cap_ttl));
1064
1065 session->s_cap_ttl = session->s_renew_requested +
1066 mdsc->mdsmap->m_session_timeout*HZ;
1067
1068 if (was_stale) {
1069 if (time_before(jiffies, session->s_cap_ttl)) {
1070 pr_info("mds%d caps renewed\n", session->s_mds);
1071 wake = 1;
1072 } else {
1073 pr_info("mds%d caps still stale\n", session->s_mds);
1074 }
1075 }
1076 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1077 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1078 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1079 spin_unlock(&session->s_cap_lock);
1080
1081 if (wake)
Sage Weil0dc25702009-11-20 13:43:45 -08001082 wake_up_session_caps(session, 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07001083}
1084
1085/*
1086 * send a session close request
1087 */
1088static int request_close_session(struct ceph_mds_client *mdsc,
1089 struct ceph_mds_session *session)
1090{
1091 struct ceph_msg *msg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001092
1093 dout("request_close_session mds%d state %s seq %lld\n",
1094 session->s_mds, session_state_name(session->s_state),
1095 session->s_seq);
1096 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001097 if (!msg)
1098 return -ENOMEM;
1099 ceph_con_send(&session->s_con, msg);
1100 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001101}
1102
1103/*
1104 * Called with s_mutex held.
1105 */
1106static int __close_session(struct ceph_mds_client *mdsc,
1107 struct ceph_mds_session *session)
1108{
1109 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1110 return 0;
1111 session->s_state = CEPH_MDS_SESSION_CLOSING;
1112 return request_close_session(mdsc, session);
1113}
1114
1115/*
1116 * Trim old(er) caps.
1117 *
1118 * Because we can't cache an inode without one or more caps, we do
1119 * this indirectly: if a cap is unused, we prune its aliases, at which
1120 * point the inode will hopefully get dropped to.
1121 *
1122 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1123 * memory pressure from the MDS, though, so it needn't be perfect.
1124 */
1125static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1126{
1127 struct ceph_mds_session *session = arg;
1128 struct ceph_inode_info *ci = ceph_inode(inode);
1129 int used, oissued, mine;
1130
1131 if (session->s_trim_caps <= 0)
1132 return -1;
1133
1134 spin_lock(&inode->i_lock);
1135 mine = cap->issued | cap->implemented;
1136 used = __ceph_caps_used(ci);
1137 oissued = __ceph_caps_issued_other(ci, cap);
1138
1139 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n",
1140 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
1141 ceph_cap_string(used));
1142 if (ci->i_dirty_caps)
1143 goto out; /* dirty caps */
1144 if ((used & ~oissued) & mine)
1145 goto out; /* we need these caps */
1146
1147 session->s_trim_caps--;
1148 if (oissued) {
1149 /* we aren't the only cap.. just remove us */
Sage Weil7c1332b2010-02-16 11:39:45 -08001150 __ceph_remove_cap(cap);
Sage Weil2f2dc052009-10-06 11:31:09 -07001151 } else {
1152 /* try to drop referring dentries */
1153 spin_unlock(&inode->i_lock);
1154 d_prune_aliases(inode);
1155 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1156 inode, cap, atomic_read(&inode->i_count));
1157 return 0;
1158 }
1159
1160out:
1161 spin_unlock(&inode->i_lock);
1162 return 0;
1163}
1164
1165/*
1166 * Trim session cap count down to some max number.
1167 */
1168static int trim_caps(struct ceph_mds_client *mdsc,
1169 struct ceph_mds_session *session,
1170 int max_caps)
1171{
1172 int trim_caps = session->s_nr_caps - max_caps;
1173
1174 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1175 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1176 if (trim_caps > 0) {
1177 session->s_trim_caps = trim_caps;
1178 iterate_session_caps(session, trim_caps_cb, session);
1179 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1180 session->s_mds, session->s_nr_caps, max_caps,
1181 trim_caps - session->s_trim_caps);
Sage Weil5dacf092009-12-21 20:40:34 -08001182 session->s_trim_caps = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001183 }
1184 return 0;
1185}
1186
1187/*
1188 * Allocate cap_release messages. If there is a partially full message
1189 * in the queue, try to allocate enough to cover it's remainder, so that
1190 * we can send it immediately.
1191 *
1192 * Called under s_mutex.
1193 */
Sage Weil2b2300d2010-06-09 16:52:04 -07001194int ceph_add_cap_releases(struct ceph_mds_client *mdsc,
Sage Weilee6b2722010-06-10 12:55:52 -07001195 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001196{
Sage Weil38e8883e2010-06-10 12:57:11 -07001197 struct ceph_msg *msg, *partial = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001198 struct ceph_mds_cap_release *head;
1199 int err = -ENOMEM;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001200 int extra = mdsc->fsc->mount_options->cap_release_safety;
Sage Weil38e8883e2010-06-10 12:57:11 -07001201 int num;
Sage Weil2f2dc052009-10-06 11:31:09 -07001202
Sage Weil38e8883e2010-06-10 12:57:11 -07001203 dout("add_cap_releases %p mds%d extra %d\n", session, session->s_mds,
1204 extra);
Sage Weil2f2dc052009-10-06 11:31:09 -07001205
1206 spin_lock(&session->s_cap_lock);
1207
1208 if (!list_empty(&session->s_cap_releases)) {
1209 msg = list_first_entry(&session->s_cap_releases,
1210 struct ceph_msg,
1211 list_head);
1212 head = msg->front.iov_base;
Sage Weil38e8883e2010-06-10 12:57:11 -07001213 num = le32_to_cpu(head->num);
1214 if (num) {
1215 dout(" partial %p with (%d/%d)\n", msg, num,
1216 (int)CEPH_CAPS_PER_RELEASE);
1217 extra += CEPH_CAPS_PER_RELEASE - num;
1218 partial = msg;
1219 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001220 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001221 while (session->s_num_cap_releases < session->s_nr_caps + extra) {
1222 spin_unlock(&session->s_cap_lock);
Yehuda Sadeh34d23762010-04-06 14:33:58 -07001223 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE,
1224 GFP_NOFS);
Sage Weil2f2dc052009-10-06 11:31:09 -07001225 if (!msg)
1226 goto out_unlocked;
1227 dout("add_cap_releases %p msg %p now %d\n", session, msg,
1228 (int)msg->front.iov_len);
1229 head = msg->front.iov_base;
1230 head->num = cpu_to_le32(0);
1231 msg->front.iov_len = sizeof(*head);
1232 spin_lock(&session->s_cap_lock);
1233 list_add(&msg->list_head, &session->s_cap_releases);
1234 session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
1235 }
1236
Sage Weil38e8883e2010-06-10 12:57:11 -07001237 if (partial) {
1238 head = partial->front.iov_base;
1239 num = le32_to_cpu(head->num);
1240 dout(" queueing partial %p with %d/%d\n", partial, num,
1241 (int)CEPH_CAPS_PER_RELEASE);
1242 list_move_tail(&partial->list_head,
1243 &session->s_cap_releases_done);
1244 session->s_num_cap_releases -= CEPH_CAPS_PER_RELEASE - num;
Sage Weil2f2dc052009-10-06 11:31:09 -07001245 }
1246 err = 0;
1247 spin_unlock(&session->s_cap_lock);
1248out_unlocked:
1249 return err;
1250}
1251
1252/*
1253 * flush all dirty inode data to disk.
1254 *
1255 * returns true if we've flushed through want_flush_seq
1256 */
1257static int check_cap_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
1258{
1259 int mds, ret = 1;
1260
1261 dout("check_cap_flush want %lld\n", want_flush_seq);
1262 mutex_lock(&mdsc->mutex);
1263 for (mds = 0; ret && mds < mdsc->max_sessions; mds++) {
1264 struct ceph_mds_session *session = mdsc->sessions[mds];
1265
1266 if (!session)
1267 continue;
1268 get_session(session);
1269 mutex_unlock(&mdsc->mutex);
1270
1271 mutex_lock(&session->s_mutex);
1272 if (!list_empty(&session->s_cap_flushing)) {
1273 struct ceph_inode_info *ci =
1274 list_entry(session->s_cap_flushing.next,
1275 struct ceph_inode_info,
1276 i_flushing_item);
1277 struct inode *inode = &ci->vfs_inode;
1278
1279 spin_lock(&inode->i_lock);
1280 if (ci->i_cap_flush_seq <= want_flush_seq) {
1281 dout("check_cap_flush still flushing %p "
1282 "seq %lld <= %lld to mds%d\n", inode,
1283 ci->i_cap_flush_seq, want_flush_seq,
1284 session->s_mds);
1285 ret = 0;
1286 }
1287 spin_unlock(&inode->i_lock);
1288 }
1289 mutex_unlock(&session->s_mutex);
1290 ceph_put_mds_session(session);
1291
1292 if (!ret)
1293 return ret;
1294 mutex_lock(&mdsc->mutex);
1295 }
1296
1297 mutex_unlock(&mdsc->mutex);
1298 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
1299 return ret;
1300}
1301
1302/*
1303 * called under s_mutex
1304 */
Sage Weil3d7ded42010-06-09 16:47:10 -07001305void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1306 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001307{
1308 struct ceph_msg *msg;
1309
1310 dout("send_cap_releases mds%d\n", session->s_mds);
Sage Weil0f8605f2010-05-05 15:51:35 -07001311 spin_lock(&session->s_cap_lock);
1312 while (!list_empty(&session->s_cap_releases_done)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001313 msg = list_first_entry(&session->s_cap_releases_done,
1314 struct ceph_msg, list_head);
1315 list_del_init(&msg->list_head);
1316 spin_unlock(&session->s_cap_lock);
1317 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1318 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1319 ceph_con_send(&session->s_con, msg);
Sage Weil0f8605f2010-05-05 15:51:35 -07001320 spin_lock(&session->s_cap_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001321 }
1322 spin_unlock(&session->s_cap_lock);
1323}
1324
Sage Weile01a5942010-05-10 15:36:44 -07001325static void discard_cap_releases(struct ceph_mds_client *mdsc,
1326 struct ceph_mds_session *session)
1327{
1328 struct ceph_msg *msg;
1329 struct ceph_mds_cap_release *head;
1330 unsigned num;
1331
1332 dout("discard_cap_releases mds%d\n", session->s_mds);
1333 spin_lock(&session->s_cap_lock);
1334
1335 /* zero out the in-progress message */
1336 msg = list_first_entry(&session->s_cap_releases,
1337 struct ceph_msg, list_head);
1338 head = msg->front.iov_base;
1339 num = le32_to_cpu(head->num);
1340 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg, num);
1341 head->num = cpu_to_le32(0);
1342 session->s_num_cap_releases += num;
1343
1344 /* requeue completed messages */
1345 while (!list_empty(&session->s_cap_releases_done)) {
1346 msg = list_first_entry(&session->s_cap_releases_done,
1347 struct ceph_msg, list_head);
1348 list_del_init(&msg->list_head);
1349
1350 head = msg->front.iov_base;
1351 num = le32_to_cpu(head->num);
1352 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg,
1353 num);
1354 session->s_num_cap_releases += num;
1355 head->num = cpu_to_le32(0);
1356 msg->front.iov_len = sizeof(*head);
1357 list_add(&msg->list_head, &session->s_cap_releases);
1358 }
1359
1360 spin_unlock(&session->s_cap_lock);
1361}
1362
Sage Weil2f2dc052009-10-06 11:31:09 -07001363/*
1364 * requests
1365 */
1366
1367/*
1368 * Create an mds request.
1369 */
1370struct ceph_mds_request *
1371ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1372{
1373 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1374
1375 if (!req)
1376 return ERR_PTR(-ENOMEM);
1377
Sage Weilb4556392010-05-13 12:01:13 -07001378 mutex_init(&req->r_fill_mutex);
Yehuda Sadeh37151662010-06-17 16:16:12 -07001379 req->r_mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07001380 req->r_started = jiffies;
1381 req->r_resend_mds = -1;
1382 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1383 req->r_fmode = -1;
Sage Weil153c8e62009-12-07 12:31:09 -08001384 kref_init(&req->r_kref);
Sage Weil2f2dc052009-10-06 11:31:09 -07001385 INIT_LIST_HEAD(&req->r_wait);
1386 init_completion(&req->r_completion);
1387 init_completion(&req->r_safe_completion);
1388 INIT_LIST_HEAD(&req->r_unsafe_item);
1389
1390 req->r_op = op;
1391 req->r_direct_mode = mode;
1392 return req;
1393}
1394
1395/*
Sage Weil44ca18f2010-02-15 12:08:46 -08001396 * return oldest (lowest) request, tid in request tree, 0 if none.
Sage Weil2f2dc052009-10-06 11:31:09 -07001397 *
1398 * called under mdsc->mutex.
1399 */
Sage Weil44ca18f2010-02-15 12:08:46 -08001400static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1401{
1402 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1403 return NULL;
1404 return rb_entry(rb_first(&mdsc->request_tree),
1405 struct ceph_mds_request, r_node);
1406}
1407
Sage Weil2f2dc052009-10-06 11:31:09 -07001408static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1409{
Sage Weil44ca18f2010-02-15 12:08:46 -08001410 struct ceph_mds_request *req = __get_oldest_req(mdsc);
1411
1412 if (req)
1413 return req->r_tid;
1414 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001415}
1416
1417/*
1418 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1419 * on build_path_from_dentry in fs/cifs/dir.c.
1420 *
1421 * If @stop_on_nosnap, generate path relative to the first non-snapped
1422 * inode.
1423 *
1424 * Encode hidden .snap dirs as a double /, i.e.
1425 * foo/.snap/bar -> foo//bar
1426 */
1427char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1428 int stop_on_nosnap)
1429{
1430 struct dentry *temp;
1431 char *path;
1432 int len, pos;
1433
1434 if (dentry == NULL)
1435 return ERR_PTR(-EINVAL);
1436
1437retry:
1438 len = 0;
1439 for (temp = dentry; !IS_ROOT(temp);) {
1440 struct inode *inode = temp->d_inode;
1441 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1442 len++; /* slash only */
1443 else if (stop_on_nosnap && inode &&
1444 ceph_snap(inode) == CEPH_NOSNAP)
1445 break;
1446 else
1447 len += 1 + temp->d_name.len;
1448 temp = temp->d_parent;
1449 if (temp == NULL) {
Sage Weil6c99f252010-05-10 16:12:25 -07001450 pr_err("build_path corrupt dentry %p\n", dentry);
Sage Weil2f2dc052009-10-06 11:31:09 -07001451 return ERR_PTR(-EINVAL);
1452 }
1453 }
1454 if (len)
1455 len--; /* no leading '/' */
1456
1457 path = kmalloc(len+1, GFP_NOFS);
1458 if (path == NULL)
1459 return ERR_PTR(-ENOMEM);
1460 pos = len;
1461 path[pos] = 0; /* trailing null */
1462 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
1463 struct inode *inode = temp->d_inode;
1464
1465 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
Sage Weil104648a2010-03-18 10:14:30 -07001466 dout("build_path path+%d: %p SNAPDIR\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001467 pos, temp);
1468 } else if (stop_on_nosnap && inode &&
1469 ceph_snap(inode) == CEPH_NOSNAP) {
1470 break;
1471 } else {
1472 pos -= temp->d_name.len;
1473 if (pos < 0)
1474 break;
1475 strncpy(path + pos, temp->d_name.name,
1476 temp->d_name.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07001477 }
1478 if (pos)
1479 path[--pos] = '/';
1480 temp = temp->d_parent;
1481 if (temp == NULL) {
Sage Weil104648a2010-03-18 10:14:30 -07001482 pr_err("build_path corrupt dentry\n");
Sage Weil2f2dc052009-10-06 11:31:09 -07001483 kfree(path);
1484 return ERR_PTR(-EINVAL);
1485 }
1486 }
1487 if (pos != 0) {
Sage Weil104648a2010-03-18 10:14:30 -07001488 pr_err("build_path did not end path lookup where "
Sage Weil2f2dc052009-10-06 11:31:09 -07001489 "expected, namelen is %d, pos is %d\n", len, pos);
1490 /* presumably this is only possible if racing with a
1491 rename of one of the parent directories (we can not
1492 lock the dentries above us to prevent this, but
1493 retrying should be harmless) */
1494 kfree(path);
1495 goto retry;
1496 }
1497
1498 *base = ceph_ino(temp->d_inode);
1499 *plen = len;
Sage Weil104648a2010-03-18 10:14:30 -07001500 dout("build_path on %p %d built %llx '%.*s'\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001501 dentry, atomic_read(&dentry->d_count), *base, len, path);
1502 return path;
1503}
1504
1505static int build_dentry_path(struct dentry *dentry,
1506 const char **ppath, int *ppathlen, u64 *pino,
1507 int *pfreepath)
1508{
1509 char *path;
1510
1511 if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) {
1512 *pino = ceph_ino(dentry->d_parent->d_inode);
1513 *ppath = dentry->d_name.name;
1514 *ppathlen = dentry->d_name.len;
1515 return 0;
1516 }
1517 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1518 if (IS_ERR(path))
1519 return PTR_ERR(path);
1520 *ppath = path;
1521 *pfreepath = 1;
1522 return 0;
1523}
1524
1525static int build_inode_path(struct inode *inode,
1526 const char **ppath, int *ppathlen, u64 *pino,
1527 int *pfreepath)
1528{
1529 struct dentry *dentry;
1530 char *path;
1531
1532 if (ceph_snap(inode) == CEPH_NOSNAP) {
1533 *pino = ceph_ino(inode);
1534 *ppathlen = 0;
1535 return 0;
1536 }
1537 dentry = d_find_alias(inode);
1538 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1539 dput(dentry);
1540 if (IS_ERR(path))
1541 return PTR_ERR(path);
1542 *ppath = path;
1543 *pfreepath = 1;
1544 return 0;
1545}
1546
1547/*
1548 * request arguments may be specified via an inode *, a dentry *, or
1549 * an explicit ino+path.
1550 */
1551static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1552 const char *rpath, u64 rino,
1553 const char **ppath, int *pathlen,
1554 u64 *ino, int *freepath)
1555{
1556 int r = 0;
1557
1558 if (rinode) {
1559 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1560 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1561 ceph_snap(rinode));
1562 } else if (rdentry) {
1563 r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
1564 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1565 *ppath);
1566 } else if (rpath) {
1567 *ino = rino;
1568 *ppath = rpath;
1569 *pathlen = strlen(rpath);
1570 dout(" path %.*s\n", *pathlen, rpath);
1571 }
1572
1573 return r;
1574}
1575
1576/*
1577 * called under mdsc->mutex
1578 */
1579static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1580 struct ceph_mds_request *req,
1581 int mds)
1582{
1583 struct ceph_msg *msg;
1584 struct ceph_mds_request_head *head;
1585 const char *path1 = NULL;
1586 const char *path2 = NULL;
1587 u64 ino1 = 0, ino2 = 0;
1588 int pathlen1 = 0, pathlen2 = 0;
1589 int freepath1 = 0, freepath2 = 0;
1590 int len;
1591 u16 releases;
1592 void *p, *end;
1593 int ret;
1594
1595 ret = set_request_path_attr(req->r_inode, req->r_dentry,
1596 req->r_path1, req->r_ino1.ino,
1597 &path1, &pathlen1, &ino1, &freepath1);
1598 if (ret < 0) {
1599 msg = ERR_PTR(ret);
1600 goto out;
1601 }
1602
1603 ret = set_request_path_attr(NULL, req->r_old_dentry,
1604 req->r_path2, req->r_ino2.ino,
1605 &path2, &pathlen2, &ino2, &freepath2);
1606 if (ret < 0) {
1607 msg = ERR_PTR(ret);
1608 goto out_free1;
1609 }
1610
1611 len = sizeof(*head) +
Sage Weilac8839d2010-01-27 14:28:10 -08001612 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64));
Sage Weil2f2dc052009-10-06 11:31:09 -07001613
1614 /* calculate (max) length for cap releases */
1615 len += sizeof(struct ceph_mds_request_release) *
1616 (!!req->r_inode_drop + !!req->r_dentry_drop +
1617 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1618 if (req->r_dentry_drop)
1619 len += req->r_dentry->d_name.len;
1620 if (req->r_old_dentry_drop)
1621 len += req->r_old_dentry->d_name.len;
1622
Yehuda Sadeh34d23762010-04-06 14:33:58 -07001623 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -07001624 if (!msg) {
1625 msg = ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -07001626 goto out_free2;
Sage Weila79832f2010-04-01 16:06:19 -07001627 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001628
Sage Weil6df058c2009-12-22 11:24:33 -08001629 msg->hdr.tid = cpu_to_le64(req->r_tid);
1630
Sage Weil2f2dc052009-10-06 11:31:09 -07001631 head = msg->front.iov_base;
1632 p = msg->front.iov_base + sizeof(*head);
1633 end = msg->front.iov_base + msg->front.iov_len;
1634
1635 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1636 head->op = cpu_to_le32(req->r_op);
Sage Weilcb4276c2010-11-08 07:28:52 -08001637 head->caller_uid = cpu_to_le32(req->r_uid);
1638 head->caller_gid = cpu_to_le32(req->r_gid);
Sage Weil2f2dc052009-10-06 11:31:09 -07001639 head->args = req->r_args;
1640
1641 ceph_encode_filepath(&p, end, ino1, path1);
1642 ceph_encode_filepath(&p, end, ino2, path2);
1643
Sage Weile979cf52010-07-15 14:58:39 -07001644 /* make note of release offset, in case we need to replay */
1645 req->r_request_release_offset = p - msg->front.iov_base;
1646
Sage Weil2f2dc052009-10-06 11:31:09 -07001647 /* cap releases */
1648 releases = 0;
1649 if (req->r_inode_drop)
1650 releases += ceph_encode_inode_release(&p,
1651 req->r_inode ? req->r_inode : req->r_dentry->d_inode,
1652 mds, req->r_inode_drop, req->r_inode_unless, 0);
1653 if (req->r_dentry_drop)
1654 releases += ceph_encode_dentry_release(&p, req->r_dentry,
1655 mds, req->r_dentry_drop, req->r_dentry_unless);
1656 if (req->r_old_dentry_drop)
1657 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
1658 mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
1659 if (req->r_old_inode_drop)
1660 releases += ceph_encode_inode_release(&p,
1661 req->r_old_dentry->d_inode,
1662 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
1663 head->num_releases = cpu_to_le16(releases);
1664
1665 BUG_ON(p > end);
1666 msg->front.iov_len = p - msg->front.iov_base;
1667 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1668
1669 msg->pages = req->r_pages;
1670 msg->nr_pages = req->r_num_pages;
1671 msg->hdr.data_len = cpu_to_le32(req->r_data_len);
1672 msg->hdr.data_off = cpu_to_le16(0);
1673
1674out_free2:
1675 if (freepath2)
1676 kfree((char *)path2);
1677out_free1:
1678 if (freepath1)
1679 kfree((char *)path1);
1680out:
1681 return msg;
1682}
1683
1684/*
1685 * called under mdsc->mutex if error, under no mutex if
1686 * success.
1687 */
1688static void complete_request(struct ceph_mds_client *mdsc,
1689 struct ceph_mds_request *req)
1690{
1691 if (req->r_callback)
1692 req->r_callback(mdsc, req);
1693 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001694 complete_all(&req->r_completion);
Sage Weil2f2dc052009-10-06 11:31:09 -07001695}
1696
1697/*
1698 * called under mdsc->mutex
1699 */
1700static int __prepare_send_request(struct ceph_mds_client *mdsc,
1701 struct ceph_mds_request *req,
1702 int mds)
1703{
1704 struct ceph_mds_request_head *rhead;
1705 struct ceph_msg *msg;
1706 int flags = 0;
1707
Sage Weil2f2dc052009-10-06 11:31:09 -07001708 req->r_attempts++;
Greg Farnume55b71f2010-06-22 15:58:01 -07001709 if (req->r_inode) {
1710 struct ceph_cap *cap =
1711 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
1712
1713 if (cap)
1714 req->r_sent_on_mseq = cap->mseq;
1715 else
1716 req->r_sent_on_mseq = -1;
1717 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001718 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
1719 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
1720
Sage Weil01a92f12010-07-15 13:24:32 -07001721 if (req->r_got_unsafe) {
1722 /*
1723 * Replay. Do not regenerate message (and rebuild
1724 * paths, etc.); just use the original message.
1725 * Rebuilding paths will break for renames because
1726 * d_move mangles the src name.
1727 */
1728 msg = req->r_request;
1729 rhead = msg->front.iov_base;
1730
1731 flags = le32_to_cpu(rhead->flags);
1732 flags |= CEPH_MDS_FLAG_REPLAY;
1733 rhead->flags = cpu_to_le32(flags);
1734
1735 if (req->r_target_inode)
1736 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
1737
1738 rhead->num_retry = req->r_attempts - 1;
Sage Weile979cf52010-07-15 14:58:39 -07001739
1740 /* remove cap/dentry releases from message */
1741 rhead->num_releases = 0;
1742 msg->hdr.front_len = cpu_to_le32(req->r_request_release_offset);
1743 msg->front.iov_len = req->r_request_release_offset;
Sage Weil01a92f12010-07-15 13:24:32 -07001744 return 0;
1745 }
1746
Sage Weil2f2dc052009-10-06 11:31:09 -07001747 if (req->r_request) {
1748 ceph_msg_put(req->r_request);
1749 req->r_request = NULL;
1750 }
1751 msg = create_request_message(mdsc, req, mds);
1752 if (IS_ERR(msg)) {
Sage Weile1518c72010-05-13 11:19:06 -07001753 req->r_err = PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001754 complete_request(mdsc, req);
Sage Weila79832f2010-04-01 16:06:19 -07001755 return PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001756 }
1757 req->r_request = msg;
1758
1759 rhead = msg->front.iov_base;
Sage Weil2f2dc052009-10-06 11:31:09 -07001760 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
1761 if (req->r_got_unsafe)
1762 flags |= CEPH_MDS_FLAG_REPLAY;
1763 if (req->r_locked_dir)
1764 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
1765 rhead->flags = cpu_to_le32(flags);
1766 rhead->num_fwd = req->r_num_fwd;
1767 rhead->num_retry = req->r_attempts - 1;
Sage Weil01a92f12010-07-15 13:24:32 -07001768 rhead->ino = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001769
1770 dout(" r_locked_dir = %p\n", req->r_locked_dir);
Sage Weil2f2dc052009-10-06 11:31:09 -07001771 return 0;
1772}
1773
1774/*
1775 * send request, or put it on the appropriate wait list.
1776 */
1777static int __do_request(struct ceph_mds_client *mdsc,
1778 struct ceph_mds_request *req)
1779{
1780 struct ceph_mds_session *session = NULL;
1781 int mds = -1;
1782 int err = -EAGAIN;
1783
Sage Weile1518c72010-05-13 11:19:06 -07001784 if (req->r_err || req->r_got_result)
Sage Weil2f2dc052009-10-06 11:31:09 -07001785 goto out;
1786
1787 if (req->r_timeout &&
1788 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
1789 dout("do_request timed out\n");
1790 err = -EIO;
1791 goto finish;
1792 }
1793
1794 mds = __choose_mds(mdsc, req);
1795 if (mds < 0 ||
1796 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
1797 dout("do_request no mds or not active, waiting for map\n");
1798 list_add(&req->r_wait, &mdsc->waiting_for_map);
1799 goto out;
1800 }
1801
1802 /* get, open session */
1803 session = __ceph_lookup_mds_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07001804 if (!session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001805 session = register_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07001806 if (IS_ERR(session)) {
1807 err = PTR_ERR(session);
1808 goto finish;
1809 }
1810 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001811 dout("do_request mds%d session %p state %s\n", mds, session,
1812 session_state_name(session->s_state));
1813 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
1814 session->s_state != CEPH_MDS_SESSION_HUNG) {
1815 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1816 session->s_state == CEPH_MDS_SESSION_CLOSING)
1817 __open_session(mdsc, session);
1818 list_add(&req->r_wait, &session->s_waiting);
1819 goto out_session;
1820 }
1821
1822 /* send request */
1823 req->r_session = get_session(session);
1824 req->r_resend_mds = -1; /* forget any previous mds hint */
1825
1826 if (req->r_request_started == 0) /* note request start time */
1827 req->r_request_started = jiffies;
1828
1829 err = __prepare_send_request(mdsc, req, mds);
1830 if (!err) {
1831 ceph_msg_get(req->r_request);
1832 ceph_con_send(&session->s_con, req->r_request);
1833 }
1834
1835out_session:
1836 ceph_put_mds_session(session);
1837out:
1838 return err;
1839
1840finish:
Sage Weile1518c72010-05-13 11:19:06 -07001841 req->r_err = err;
Sage Weil2f2dc052009-10-06 11:31:09 -07001842 complete_request(mdsc, req);
1843 goto out;
1844}
1845
1846/*
1847 * called under mdsc->mutex
1848 */
1849static void __wake_requests(struct ceph_mds_client *mdsc,
1850 struct list_head *head)
1851{
1852 struct ceph_mds_request *req, *nreq;
1853
1854 list_for_each_entry_safe(req, nreq, head, r_wait) {
1855 list_del_init(&req->r_wait);
1856 __do_request(mdsc, req);
1857 }
1858}
1859
1860/*
1861 * Wake up threads with requests pending for @mds, so that they can
Sage Weil29790f22010-03-18 14:45:05 -07001862 * resubmit their requests to a possibly different mds.
Sage Weil2f2dc052009-10-06 11:31:09 -07001863 */
Sage Weil29790f22010-03-18 14:45:05 -07001864static void kick_requests(struct ceph_mds_client *mdsc, int mds)
Sage Weil2f2dc052009-10-06 11:31:09 -07001865{
Sage Weil44ca18f2010-02-15 12:08:46 -08001866 struct ceph_mds_request *req;
1867 struct rb_node *p;
Sage Weil2f2dc052009-10-06 11:31:09 -07001868
1869 dout("kick_requests mds%d\n", mds);
Sage Weil44ca18f2010-02-15 12:08:46 -08001870 for (p = rb_first(&mdsc->request_tree); p; p = rb_next(p)) {
1871 req = rb_entry(p, struct ceph_mds_request, r_node);
1872 if (req->r_got_unsafe)
1873 continue;
1874 if (req->r_session &&
1875 req->r_session->s_mds == mds) {
1876 dout(" kicking tid %llu\n", req->r_tid);
1877 put_request_session(req);
1878 __do_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07001879 }
1880 }
1881}
1882
1883void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
1884 struct ceph_mds_request *req)
1885{
1886 dout("submit_request on %p\n", req);
1887 mutex_lock(&mdsc->mutex);
1888 __register_request(mdsc, req, NULL);
1889 __do_request(mdsc, req);
1890 mutex_unlock(&mdsc->mutex);
1891}
1892
1893/*
1894 * Synchrously perform an mds request. Take care of all of the
1895 * session setup, forwarding, retry details.
1896 */
1897int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
1898 struct inode *dir,
1899 struct ceph_mds_request *req)
1900{
1901 int err;
1902
1903 dout("do_request on %p\n", req);
1904
1905 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
1906 if (req->r_inode)
1907 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
1908 if (req->r_locked_dir)
1909 ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
1910 if (req->r_old_dentry)
1911 ceph_get_cap_refs(
1912 ceph_inode(req->r_old_dentry->d_parent->d_inode),
1913 CEPH_CAP_PIN);
1914
1915 /* issue */
1916 mutex_lock(&mdsc->mutex);
1917 __register_request(mdsc, req, dir);
1918 __do_request(mdsc, req);
1919
Sage Weile1518c72010-05-13 11:19:06 -07001920 if (req->r_err) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001921 err = req->r_err;
Sage Weile1518c72010-05-13 11:19:06 -07001922 __unregister_request(mdsc, req);
1923 dout("do_request early error %d\n", err);
1924 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07001925 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001926
Sage Weile1518c72010-05-13 11:19:06 -07001927 /* wait */
1928 mutex_unlock(&mdsc->mutex);
1929 dout("do_request waiting\n");
1930 if (req->r_timeout) {
Sage Weilaa916472010-05-24 11:15:51 -07001931 err = (long)wait_for_completion_killable_timeout(
Sage Weile1518c72010-05-13 11:19:06 -07001932 &req->r_completion, req->r_timeout);
1933 if (err == 0)
1934 err = -EIO;
1935 } else {
Sage Weilaa916472010-05-24 11:15:51 -07001936 err = wait_for_completion_killable(&req->r_completion);
Sage Weile1518c72010-05-13 11:19:06 -07001937 }
1938 dout("do_request waited, got %d\n", err);
1939 mutex_lock(&mdsc->mutex);
1940
1941 /* only abort if we didn't race with a real reply */
1942 if (req->r_got_result) {
1943 err = le32_to_cpu(req->r_reply_info.head->result);
1944 } else if (err < 0) {
1945 dout("aborted request %lld with %d\n", req->r_tid, err);
Sage Weilb4556392010-05-13 12:01:13 -07001946
1947 /*
1948 * ensure we aren't running concurrently with
1949 * ceph_fill_trace or ceph_readdir_prepopulate, which
1950 * rely on locks (dir mutex) held by our caller.
1951 */
1952 mutex_lock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07001953 req->r_err = err;
1954 req->r_aborted = true;
Sage Weilb4556392010-05-13 12:01:13 -07001955 mutex_unlock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07001956
1957 if (req->r_locked_dir &&
Sage Weil167c9e32010-05-14 10:02:57 -07001958 (req->r_op & CEPH_MDS_OP_WRITE))
1959 ceph_invalidate_dir_request(req);
Sage Weile1518c72010-05-13 11:19:06 -07001960 } else {
1961 err = req->r_err;
1962 }
1963
1964out:
1965 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07001966 dout("do_request %p done, result %d\n", req, err);
1967 return err;
1968}
1969
1970/*
Sage Weil167c9e32010-05-14 10:02:57 -07001971 * Invalidate dir I_COMPLETE, dentry lease state on an aborted MDS
1972 * namespace request.
1973 */
1974void ceph_invalidate_dir_request(struct ceph_mds_request *req)
1975{
1976 struct inode *inode = req->r_locked_dir;
1977 struct ceph_inode_info *ci = ceph_inode(inode);
1978
1979 dout("invalidate_dir_request %p (I_COMPLETE, lease(s))\n", inode);
1980 spin_lock(&inode->i_lock);
1981 ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
1982 ci->i_release_count++;
1983 spin_unlock(&inode->i_lock);
1984
1985 if (req->r_dentry)
1986 ceph_invalidate_dentry_lease(req->r_dentry);
1987 if (req->r_old_dentry)
1988 ceph_invalidate_dentry_lease(req->r_old_dentry);
1989}
1990
1991/*
Sage Weil2f2dc052009-10-06 11:31:09 -07001992 * Handle mds reply.
1993 *
1994 * We take the session mutex and parse and process the reply immediately.
1995 * This preserves the logical ordering of replies, capabilities, etc., sent
1996 * by the MDS as they are applied to our local cache.
1997 */
1998static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
1999{
2000 struct ceph_mds_client *mdsc = session->s_mdsc;
2001 struct ceph_mds_request *req;
2002 struct ceph_mds_reply_head *head = msg->front.iov_base;
2003 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
2004 u64 tid;
2005 int err, result;
Sage Weil2600d2d2010-02-22 15:12:16 -08002006 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002007
Sage Weil2f2dc052009-10-06 11:31:09 -07002008 if (msg->front.iov_len < sizeof(*head)) {
2009 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002010 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002011 return;
2012 }
2013
2014 /* get request, session */
Sage Weil6df058c2009-12-22 11:24:33 -08002015 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002016 mutex_lock(&mdsc->mutex);
2017 req = __lookup_request(mdsc, tid);
2018 if (!req) {
2019 dout("handle_reply on unknown tid %llu\n", tid);
2020 mutex_unlock(&mdsc->mutex);
2021 return;
2022 }
2023 dout("handle_reply %p\n", req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002024
2025 /* correct session? */
Sage Weild96d6042010-03-20 20:50:58 -07002026 if (req->r_session != session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002027 pr_err("mdsc_handle_reply got %llu on session mds%d"
2028 " not mds%d\n", tid, session->s_mds,
2029 req->r_session ? req->r_session->s_mds : -1);
2030 mutex_unlock(&mdsc->mutex);
2031 goto out;
2032 }
2033
2034 /* dup? */
2035 if ((req->r_got_unsafe && !head->safe) ||
2036 (req->r_got_safe && head->safe)) {
2037 pr_warning("got a dup %s reply on %llu from mds%d\n",
2038 head->safe ? "safe" : "unsafe", tid, mds);
2039 mutex_unlock(&mdsc->mutex);
2040 goto out;
2041 }
Sage Weil85792d0d2010-05-13 09:06:02 -07002042 if (req->r_got_safe && !head->safe) {
2043 pr_warning("got unsafe after safe on %llu from mds%d\n",
2044 tid, mds);
2045 mutex_unlock(&mdsc->mutex);
2046 goto out;
2047 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002048
2049 result = le32_to_cpu(head->result);
2050
2051 /*
Greg Farnume55b71f2010-06-22 15:58:01 -07002052 * Handle an ESTALE
2053 * if we're not talking to the authority, send to them
2054 * if the authority has changed while we weren't looking,
2055 * send to new authority
2056 * Otherwise we just have to return an ESTALE
Sage Weil2f2dc052009-10-06 11:31:09 -07002057 */
2058 if (result == -ESTALE) {
Greg Farnume55b71f2010-06-22 15:58:01 -07002059 dout("got ESTALE on request %llu", req->r_tid);
Sage Weil213c99e2010-08-03 10:25:11 -07002060 if (!req->r_inode) {
2061 /* do nothing; not an authority problem */
2062 } else if (req->r_direct_mode != USE_AUTH_MDS) {
Greg Farnume55b71f2010-06-22 15:58:01 -07002063 dout("not using auth, setting for that now");
2064 req->r_direct_mode = USE_AUTH_MDS;
Sage Weil2f2dc052009-10-06 11:31:09 -07002065 __do_request(mdsc, req);
2066 mutex_unlock(&mdsc->mutex);
2067 goto out;
Greg Farnume55b71f2010-06-22 15:58:01 -07002068 } else {
2069 struct ceph_inode_info *ci = ceph_inode(req->r_inode);
Sage Weil4af25fd2010-11-02 13:41:47 -07002070 struct ceph_cap *cap = NULL;
2071
2072 if (req->r_session)
2073 cap = ceph_get_cap_for_mds(ci,
2074 req->r_session->s_mds);
Greg Farnume55b71f2010-06-22 15:58:01 -07002075
2076 dout("already using auth");
2077 if ((!cap || cap != ci->i_auth_cap) ||
2078 (cap->mseq != req->r_sent_on_mseq)) {
2079 dout("but cap changed, so resending");
2080 __do_request(mdsc, req);
2081 mutex_unlock(&mdsc->mutex);
2082 goto out;
2083 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002084 }
Greg Farnume55b71f2010-06-22 15:58:01 -07002085 dout("have to return ESTALE on request %llu", req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002086 }
2087
Greg Farnume55b71f2010-06-22 15:58:01 -07002088
Sage Weil2f2dc052009-10-06 11:31:09 -07002089 if (head->safe) {
2090 req->r_got_safe = true;
2091 __unregister_request(mdsc, req);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002092 complete_all(&req->r_safe_completion);
Sage Weil2f2dc052009-10-06 11:31:09 -07002093
2094 if (req->r_got_unsafe) {
2095 /*
2096 * We already handled the unsafe response, now do the
2097 * cleanup. No need to examine the response; the MDS
2098 * doesn't include any result info in the safe
2099 * response. And even if it did, there is nothing
2100 * useful we could do with a revised return value.
2101 */
2102 dout("got safe reply %llu, mds%d\n", tid, mds);
2103 list_del_init(&req->r_unsafe_item);
2104
2105 /* last unsafe request during umount? */
Sage Weil44ca18f2010-02-15 12:08:46 -08002106 if (mdsc->stopping && !__get_oldest_req(mdsc))
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002107 complete_all(&mdsc->safe_umount_waiters);
Sage Weil2f2dc052009-10-06 11:31:09 -07002108 mutex_unlock(&mdsc->mutex);
2109 goto out;
2110 }
Sage Weile1518c72010-05-13 11:19:06 -07002111 } else {
Sage Weil2f2dc052009-10-06 11:31:09 -07002112 req->r_got_unsafe = true;
2113 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
2114 }
2115
2116 dout("handle_reply tid %lld result %d\n", tid, result);
2117 rinfo = &req->r_reply_info;
Sage Weil14303d22010-12-14 17:37:52 -08002118 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
Sage Weil2f2dc052009-10-06 11:31:09 -07002119 mutex_unlock(&mdsc->mutex);
2120
2121 mutex_lock(&session->s_mutex);
2122 if (err < 0) {
Herb Shiu25933ab2010-12-01 14:14:38 -08002123 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002124 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002125 goto out_err;
2126 }
2127
2128 /* snap trace */
2129 if (rinfo->snapblob_len) {
2130 down_write(&mdsc->snap_rwsem);
2131 ceph_update_snap_trace(mdsc, rinfo->snapblob,
2132 rinfo->snapblob + rinfo->snapblob_len,
2133 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP);
2134 downgrade_write(&mdsc->snap_rwsem);
2135 } else {
2136 down_read(&mdsc->snap_rwsem);
2137 }
2138
2139 /* insert trace into our cache */
Sage Weilb4556392010-05-13 12:01:13 -07002140 mutex_lock(&req->r_fill_mutex);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002141 err = ceph_fill_trace(mdsc->fsc->sb, req, req->r_session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002142 if (err == 0) {
Herb Shiu25933ab2010-12-01 14:14:38 -08002143 if (result == 0 && req->r_op != CEPH_MDS_OP_GETFILELOCK &&
2144 rinfo->dir_nr)
Sage Weil2f2dc052009-10-06 11:31:09 -07002145 ceph_readdir_prepopulate(req, req->r_session);
Yehuda Sadeh37151662010-06-17 16:16:12 -07002146 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
Sage Weil2f2dc052009-10-06 11:31:09 -07002147 }
Sage Weilb4556392010-05-13 12:01:13 -07002148 mutex_unlock(&req->r_fill_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002149
2150 up_read(&mdsc->snap_rwsem);
2151out_err:
Sage Weile1518c72010-05-13 11:19:06 -07002152 mutex_lock(&mdsc->mutex);
2153 if (!req->r_aborted) {
2154 if (err) {
2155 req->r_err = err;
2156 } else {
2157 req->r_reply = msg;
2158 ceph_msg_get(msg);
2159 req->r_got_result = true;
2160 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002161 } else {
Sage Weile1518c72010-05-13 11:19:06 -07002162 dout("reply arrived after request %lld was aborted\n", tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002163 }
Sage Weile1518c72010-05-13 11:19:06 -07002164 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002165
Sage Weilee6b2722010-06-10 12:55:52 -07002166 ceph_add_cap_releases(mdsc, req->r_session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002167 mutex_unlock(&session->s_mutex);
2168
2169 /* kick calling process */
2170 complete_request(mdsc, req);
2171out:
2172 ceph_mdsc_put_request(req);
2173 return;
2174}
2175
2176
2177
2178/*
2179 * handle mds notification that our request has been forwarded.
2180 */
Sage Weil2600d2d2010-02-22 15:12:16 -08002181static void handle_forward(struct ceph_mds_client *mdsc,
2182 struct ceph_mds_session *session,
2183 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002184{
2185 struct ceph_mds_request *req;
Sage Weila1ea7872010-02-23 14:02:44 -08002186 u64 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002187 u32 next_mds;
2188 u32 fwd_seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002189 int err = -EINVAL;
2190 void *p = msg->front.iov_base;
2191 void *end = p + msg->front.iov_len;
Sage Weil2f2dc052009-10-06 11:31:09 -07002192
Sage Weila1ea7872010-02-23 14:02:44 -08002193 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002194 next_mds = ceph_decode_32(&p);
2195 fwd_seq = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07002196
2197 mutex_lock(&mdsc->mutex);
2198 req = __lookup_request(mdsc, tid);
2199 if (!req) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07002200 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002201 goto out; /* dup reply? */
2202 }
2203
Sage Weil2a8e5e32010-05-28 16:43:16 -07002204 if (req->r_aborted) {
2205 dout("forward tid %llu aborted, unregistering\n", tid);
2206 __unregister_request(mdsc, req);
2207 } else if (fwd_seq <= req->r_num_fwd) {
2208 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002209 tid, next_mds, req->r_num_fwd, fwd_seq);
2210 } else {
2211 /* resend. forward race not possible; mds would drop */
Sage Weil2a8e5e32010-05-28 16:43:16 -07002212 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2213 BUG_ON(req->r_err);
2214 BUG_ON(req->r_got_result);
Sage Weil2f2dc052009-10-06 11:31:09 -07002215 req->r_num_fwd = fwd_seq;
2216 req->r_resend_mds = next_mds;
2217 put_request_session(req);
2218 __do_request(mdsc, req);
2219 }
2220 ceph_mdsc_put_request(req);
2221out:
2222 mutex_unlock(&mdsc->mutex);
2223 return;
2224
2225bad:
2226 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2227}
2228
2229/*
2230 * handle a mds session control message
2231 */
2232static void handle_session(struct ceph_mds_session *session,
2233 struct ceph_msg *msg)
2234{
2235 struct ceph_mds_client *mdsc = session->s_mdsc;
2236 u32 op;
2237 u64 seq;
Sage Weil2600d2d2010-02-22 15:12:16 -08002238 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002239 struct ceph_mds_session_head *h = msg->front.iov_base;
2240 int wake = 0;
2241
Sage Weil2f2dc052009-10-06 11:31:09 -07002242 /* decode */
2243 if (msg->front.iov_len != sizeof(*h))
2244 goto bad;
2245 op = le32_to_cpu(h->op);
2246 seq = le64_to_cpu(h->seq);
2247
2248 mutex_lock(&mdsc->mutex);
Sage Weil2600d2d2010-02-22 15:12:16 -08002249 if (op == CEPH_SESSION_CLOSE)
2250 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002251 /* FIXME: this ttl calculation is generous */
2252 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2253 mutex_unlock(&mdsc->mutex);
2254
2255 mutex_lock(&session->s_mutex);
2256
2257 dout("handle_session mds%d %s %p state %s seq %llu\n",
2258 mds, ceph_session_op_name(op), session,
2259 session_state_name(session->s_state), seq);
2260
2261 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2262 session->s_state = CEPH_MDS_SESSION_OPEN;
2263 pr_info("mds%d came back\n", session->s_mds);
2264 }
2265
2266 switch (op) {
2267 case CEPH_SESSION_OPEN:
Sage Weil29790f22010-03-18 14:45:05 -07002268 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2269 pr_info("mds%d reconnect success\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002270 session->s_state = CEPH_MDS_SESSION_OPEN;
2271 renewed_caps(mdsc, session, 0);
2272 wake = 1;
2273 if (mdsc->stopping)
2274 __close_session(mdsc, session);
2275 break;
2276
2277 case CEPH_SESSION_RENEWCAPS:
2278 if (session->s_renew_seq == seq)
2279 renewed_caps(mdsc, session, 1);
2280 break;
2281
2282 case CEPH_SESSION_CLOSE:
Sage Weil29790f22010-03-18 14:45:05 -07002283 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2284 pr_info("mds%d reconnect denied\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002285 remove_session_caps(session);
2286 wake = 1; /* for good measure */
Sage Weilf3c60c52010-08-11 14:51:23 -07002287 wake_up_all(&mdsc->session_close_wq);
Sage Weil29790f22010-03-18 14:45:05 -07002288 kick_requests(mdsc, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002289 break;
2290
2291 case CEPH_SESSION_STALE:
2292 pr_info("mds%d caps went stale, renewing\n",
2293 session->s_mds);
2294 spin_lock(&session->s_cap_lock);
2295 session->s_cap_gen++;
2296 session->s_cap_ttl = 0;
2297 spin_unlock(&session->s_cap_lock);
2298 send_renew_caps(mdsc, session);
2299 break;
2300
2301 case CEPH_SESSION_RECALL_STATE:
2302 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2303 break;
2304
2305 default:
2306 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2307 WARN_ON(1);
2308 }
2309
2310 mutex_unlock(&session->s_mutex);
2311 if (wake) {
2312 mutex_lock(&mdsc->mutex);
2313 __wake_requests(mdsc, &session->s_waiting);
2314 mutex_unlock(&mdsc->mutex);
2315 }
2316 return;
2317
2318bad:
2319 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2320 (int)msg->front.iov_len);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002321 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002322 return;
2323}
2324
2325
2326/*
2327 * called under session->mutex.
2328 */
2329static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2330 struct ceph_mds_session *session)
2331{
2332 struct ceph_mds_request *req, *nreq;
2333 int err;
2334
2335 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2336
2337 mutex_lock(&mdsc->mutex);
2338 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
2339 err = __prepare_send_request(mdsc, req, session->s_mds);
2340 if (!err) {
2341 ceph_msg_get(req->r_request);
2342 ceph_con_send(&session->s_con, req->r_request);
2343 }
2344 }
2345 mutex_unlock(&mdsc->mutex);
2346}
2347
2348/*
2349 * Encode information about a cap for a reconnect with the MDS.
2350 */
Sage Weil2f2dc052009-10-06 11:31:09 -07002351static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2352 void *arg)
2353{
Sage Weil20cb34a2010-05-12 15:21:32 -07002354 union {
2355 struct ceph_mds_cap_reconnect v2;
2356 struct ceph_mds_cap_reconnect_v1 v1;
2357 } rec;
2358 size_t reclen;
Sage Weil2f2dc052009-10-06 11:31:09 -07002359 struct ceph_inode_info *ci;
Sage Weil20cb34a2010-05-12 15:21:32 -07002360 struct ceph_reconnect_state *recon_state = arg;
2361 struct ceph_pagelist *pagelist = recon_state->pagelist;
Sage Weil2f2dc052009-10-06 11:31:09 -07002362 char *path;
2363 int pathlen, err;
2364 u64 pathbase;
2365 struct dentry *dentry;
2366
2367 ci = cap->ci;
2368
2369 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2370 inode, ceph_vinop(inode), cap, cap->cap_id,
2371 ceph_cap_string(cap->issued));
Sage Weil93cea5b2009-12-23 12:21:51 -08002372 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2373 if (err)
2374 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002375
2376 dentry = d_find_alias(inode);
2377 if (dentry) {
2378 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2379 if (IS_ERR(path)) {
2380 err = PTR_ERR(path);
Sage Weile072f8a2010-08-26 09:26:37 -07002381 goto out_dput;
Sage Weil2f2dc052009-10-06 11:31:09 -07002382 }
2383 } else {
2384 path = NULL;
2385 pathlen = 0;
2386 }
Sage Weil93cea5b2009-12-23 12:21:51 -08002387 err = ceph_pagelist_encode_string(pagelist, path, pathlen);
2388 if (err)
Sage Weile072f8a2010-08-26 09:26:37 -07002389 goto out_free;
Sage Weil2f2dc052009-10-06 11:31:09 -07002390
Sage Weil2f2dc052009-10-06 11:31:09 -07002391 spin_lock(&inode->i_lock);
2392 cap->seq = 0; /* reset cap seq */
2393 cap->issue_seq = 0; /* and issue_seq */
Sage Weil20cb34a2010-05-12 15:21:32 -07002394
2395 if (recon_state->flock) {
2396 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
2397 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2398 rec.v2.issued = cpu_to_le32(cap->issued);
2399 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2400 rec.v2.pathbase = cpu_to_le64(pathbase);
2401 rec.v2.flock_len = 0;
2402 reclen = sizeof(rec.v2);
2403 } else {
2404 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
2405 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2406 rec.v1.issued = cpu_to_le32(cap->issued);
2407 rec.v1.size = cpu_to_le64(inode->i_size);
2408 ceph_encode_timespec(&rec.v1.mtime, &inode->i_mtime);
2409 ceph_encode_timespec(&rec.v1.atime, &inode->i_atime);
2410 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2411 rec.v1.pathbase = cpu_to_le64(pathbase);
2412 reclen = sizeof(rec.v1);
2413 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002414 spin_unlock(&inode->i_lock);
2415
Greg Farnum40819f62010-08-02 15:34:23 -07002416 if (recon_state->flock) {
2417 int num_fcntl_locks, num_flock_locks;
Greg Farnumfca44512010-09-17 10:24:02 -07002418 struct ceph_pagelist_cursor trunc_point;
Greg Farnum40819f62010-08-02 15:34:23 -07002419
Greg Farnumfca44512010-09-17 10:24:02 -07002420 ceph_pagelist_set_cursor(pagelist, &trunc_point);
2421 do {
Sage Weil496e5952010-09-22 19:57:10 -07002422 lock_flocks();
Greg Farnumfca44512010-09-17 10:24:02 -07002423 ceph_count_locks(inode, &num_fcntl_locks,
2424 &num_flock_locks);
2425 rec.v2.flock_len = (2*sizeof(u32) +
2426 (num_fcntl_locks+num_flock_locks) *
2427 sizeof(struct ceph_filelock));
Sage Weil496e5952010-09-22 19:57:10 -07002428 unlock_flocks();
Greg Farnum40819f62010-08-02 15:34:23 -07002429
Greg Farnumfca44512010-09-17 10:24:02 -07002430 /* pre-alloc pagelist */
2431 ceph_pagelist_truncate(pagelist, &trunc_point);
2432 err = ceph_pagelist_append(pagelist, &rec, reclen);
2433 if (!err)
2434 err = ceph_pagelist_reserve(pagelist,
2435 rec.v2.flock_len);
2436
2437 /* encode locks */
2438 if (!err) {
Sage Weil496e5952010-09-22 19:57:10 -07002439 lock_flocks();
Greg Farnumfca44512010-09-17 10:24:02 -07002440 err = ceph_encode_locks(inode,
2441 pagelist,
2442 num_fcntl_locks,
2443 num_flock_locks);
Sage Weil496e5952010-09-22 19:57:10 -07002444 unlock_flocks();
Greg Farnumfca44512010-09-17 10:24:02 -07002445 }
2446 } while (err == -ENOSPC);
Sage Weil3612abb2010-09-07 15:59:27 -07002447 } else {
2448 err = ceph_pagelist_append(pagelist, &rec, reclen);
Greg Farnum40819f62010-08-02 15:34:23 -07002449 }
Sage Weil93cea5b2009-12-23 12:21:51 -08002450
Sage Weile072f8a2010-08-26 09:26:37 -07002451out_free:
Sage Weil2f2dc052009-10-06 11:31:09 -07002452 kfree(path);
Sage Weile072f8a2010-08-26 09:26:37 -07002453out_dput:
Sage Weil2f2dc052009-10-06 11:31:09 -07002454 dput(dentry);
Sage Weil93cea5b2009-12-23 12:21:51 -08002455 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002456}
2457
2458
2459/*
2460 * If an MDS fails and recovers, clients need to reconnect in order to
2461 * reestablish shared state. This includes all caps issued through
2462 * this session _and_ the snap_realm hierarchy. Because it's not
2463 * clear which snap realms the mds cares about, we send everything we
2464 * know about.. that ensures we'll then get any new info the
2465 * recovering MDS might have.
2466 *
2467 * This is a relatively heavyweight operation, but it's rare.
2468 *
2469 * called with mdsc->mutex held.
2470 */
Sage Weil34b6c852010-05-10 16:31:25 -07002471static void send_mds_reconnect(struct ceph_mds_client *mdsc,
2472 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07002473{
Sage Weil2f2dc052009-10-06 11:31:09 -07002474 struct ceph_msg *reply;
Sage Weila105f002010-02-15 14:37:55 -08002475 struct rb_node *p;
Sage Weil34b6c852010-05-10 16:31:25 -07002476 int mds = session->s_mds;
Sage Weil9abf82b2010-05-10 21:58:38 -07002477 int err = -ENOMEM;
Sage Weil93cea5b2009-12-23 12:21:51 -08002478 struct ceph_pagelist *pagelist;
Sage Weil20cb34a2010-05-12 15:21:32 -07002479 struct ceph_reconnect_state recon_state;
Sage Weil2f2dc052009-10-06 11:31:09 -07002480
Sage Weil34b6c852010-05-10 16:31:25 -07002481 pr_info("mds%d reconnect start\n", mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002482
Sage Weil93cea5b2009-12-23 12:21:51 -08002483 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
2484 if (!pagelist)
2485 goto fail_nopagelist;
2486 ceph_pagelist_init(pagelist);
2487
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002488 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -07002489 if (!reply)
Sage Weil93cea5b2009-12-23 12:21:51 -08002490 goto fail_nomsg;
Sage Weil93cea5b2009-12-23 12:21:51 -08002491
Sage Weil34b6c852010-05-10 16:31:25 -07002492 mutex_lock(&session->s_mutex);
2493 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
2494 session->s_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002495
Sage Weil34b6c852010-05-10 16:31:25 -07002496 ceph_con_open(&session->s_con,
2497 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
Sage Weil2f2dc052009-10-06 11:31:09 -07002498
Sage Weil34b6c852010-05-10 16:31:25 -07002499 /* replay unsafe requests */
2500 replay_unsafe_requests(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002501
2502 down_read(&mdsc->snap_rwsem);
2503
Sage Weil2f2dc052009-10-06 11:31:09 -07002504 dout("session %p state %s\n", session,
2505 session_state_name(session->s_state));
2506
Sage Weile01a5942010-05-10 15:36:44 -07002507 /* drop old cap expires; we're about to reestablish that state */
2508 discard_cap_releases(mdsc, session);
2509
Sage Weil2f2dc052009-10-06 11:31:09 -07002510 /* traverse this session's caps */
Sage Weil93cea5b2009-12-23 12:21:51 -08002511 err = ceph_pagelist_encode_32(pagelist, session->s_nr_caps);
2512 if (err)
2513 goto fail;
Sage Weil20cb34a2010-05-12 15:21:32 -07002514
2515 recon_state.pagelist = pagelist;
2516 recon_state.flock = session->s_con.peer_features & CEPH_FEATURE_FLOCK;
2517 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
Sage Weil2f2dc052009-10-06 11:31:09 -07002518 if (err < 0)
Sage Weil9abf82b2010-05-10 21:58:38 -07002519 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07002520
2521 /*
2522 * snaprealms. we provide mds with the ino, seq (version), and
2523 * parent for all of our realms. If the mds has any newer info,
2524 * it will tell us.
2525 */
Sage Weila105f002010-02-15 14:37:55 -08002526 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
2527 struct ceph_snap_realm *realm =
2528 rb_entry(p, struct ceph_snap_realm, node);
Sage Weil93cea5b2009-12-23 12:21:51 -08002529 struct ceph_mds_snaprealm_reconnect sr_rec;
Sage Weil2f2dc052009-10-06 11:31:09 -07002530
2531 dout(" adding snap realm %llx seq %lld parent %llx\n",
2532 realm->ino, realm->seq, realm->parent_ino);
Sage Weil93cea5b2009-12-23 12:21:51 -08002533 sr_rec.ino = cpu_to_le64(realm->ino);
2534 sr_rec.seq = cpu_to_le64(realm->seq);
2535 sr_rec.parent = cpu_to_le64(realm->parent_ino);
2536 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
2537 if (err)
2538 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07002539 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002540
Sage Weil93cea5b2009-12-23 12:21:51 -08002541 reply->pagelist = pagelist;
Sage Weil20cb34a2010-05-12 15:21:32 -07002542 if (recon_state.flock)
2543 reply->hdr.version = cpu_to_le16(2);
Sage Weil93cea5b2009-12-23 12:21:51 -08002544 reply->hdr.data_len = cpu_to_le32(pagelist->length);
2545 reply->nr_pages = calc_pages_for(0, pagelist->length);
Sage Weil2f2dc052009-10-06 11:31:09 -07002546 ceph_con_send(&session->s_con, reply);
2547
Sage Weil9abf82b2010-05-10 21:58:38 -07002548 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002549
Sage Weil9abf82b2010-05-10 21:58:38 -07002550 mutex_lock(&mdsc->mutex);
2551 __wake_requests(mdsc, &session->s_waiting);
2552 mutex_unlock(&mdsc->mutex);
2553
Sage Weil2f2dc052009-10-06 11:31:09 -07002554 up_read(&mdsc->snap_rwsem);
Sage Weil2f2dc052009-10-06 11:31:09 -07002555 return;
2556
Sage Weil93cea5b2009-12-23 12:21:51 -08002557fail:
Sage Weil2f2dc052009-10-06 11:31:09 -07002558 ceph_msg_put(reply);
Sage Weil9abf82b2010-05-10 21:58:38 -07002559 up_read(&mdsc->snap_rwsem);
2560 mutex_unlock(&session->s_mutex);
Sage Weil93cea5b2009-12-23 12:21:51 -08002561fail_nomsg:
2562 ceph_pagelist_release(pagelist);
2563 kfree(pagelist);
2564fail_nopagelist:
Sage Weil9abf82b2010-05-10 21:58:38 -07002565 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
Sage Weil9abf82b2010-05-10 21:58:38 -07002566 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07002567}
2568
2569
2570/*
2571 * compare old and new mdsmaps, kicking requests
2572 * and closing out old connections as necessary
2573 *
2574 * called under mdsc->mutex.
2575 */
2576static void check_new_map(struct ceph_mds_client *mdsc,
2577 struct ceph_mdsmap *newmap,
2578 struct ceph_mdsmap *oldmap)
2579{
2580 int i;
2581 int oldstate, newstate;
2582 struct ceph_mds_session *s;
2583
2584 dout("check_new_map new %u old %u\n",
2585 newmap->m_epoch, oldmap->m_epoch);
2586
2587 for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
2588 if (mdsc->sessions[i] == NULL)
2589 continue;
2590 s = mdsc->sessions[i];
2591 oldstate = ceph_mdsmap_get_state(oldmap, i);
2592 newstate = ceph_mdsmap_get_state(newmap, i);
2593
Sage Weil0deb01c2010-06-17 14:19:01 -07002594 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002595 i, ceph_mds_state_name(oldstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07002596 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
Sage Weil2f2dc052009-10-06 11:31:09 -07002597 ceph_mds_state_name(newstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07002598 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
Sage Weil2f2dc052009-10-06 11:31:09 -07002599 session_state_name(s->s_state));
2600
2601 if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
2602 ceph_mdsmap_get_addr(newmap, i),
2603 sizeof(struct ceph_entity_addr))) {
2604 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
2605 /* the session never opened, just close it
2606 * out now */
2607 __wake_requests(mdsc, &s->s_waiting);
Sage Weil2600d2d2010-02-22 15:12:16 -08002608 __unregister_session(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07002609 } else {
2610 /* just close it */
2611 mutex_unlock(&mdsc->mutex);
2612 mutex_lock(&s->s_mutex);
2613 mutex_lock(&mdsc->mutex);
2614 ceph_con_close(&s->s_con);
2615 mutex_unlock(&s->s_mutex);
2616 s->s_state = CEPH_MDS_SESSION_RESTARTING;
2617 }
2618
2619 /* kick any requests waiting on the recovering mds */
Sage Weil29790f22010-03-18 14:45:05 -07002620 kick_requests(mdsc, i);
Sage Weil2f2dc052009-10-06 11:31:09 -07002621 } else if (oldstate == newstate) {
2622 continue; /* nothing new with this mds */
2623 }
2624
2625 /*
2626 * send reconnect?
2627 */
2628 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
Sage Weil34b6c852010-05-10 16:31:25 -07002629 newstate >= CEPH_MDS_STATE_RECONNECT) {
2630 mutex_unlock(&mdsc->mutex);
2631 send_mds_reconnect(mdsc, s);
2632 mutex_lock(&mdsc->mutex);
2633 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002634
2635 /*
Sage Weil29790f22010-03-18 14:45:05 -07002636 * kick request on any mds that has gone active.
Sage Weil2f2dc052009-10-06 11:31:09 -07002637 */
2638 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
2639 newstate >= CEPH_MDS_STATE_ACTIVE) {
Sage Weil29790f22010-03-18 14:45:05 -07002640 if (oldstate != CEPH_MDS_STATE_CREATING &&
2641 oldstate != CEPH_MDS_STATE_STARTING)
2642 pr_info("mds%d recovery completed\n", s->s_mds);
2643 kick_requests(mdsc, i);
Sage Weil2f2dc052009-10-06 11:31:09 -07002644 ceph_kick_flushing_caps(mdsc, s);
Sage Weil0dc25702009-11-20 13:43:45 -08002645 wake_up_session_caps(s, 1);
Sage Weil2f2dc052009-10-06 11:31:09 -07002646 }
2647 }
Sage Weilcb170a22010-06-21 13:38:35 -07002648
2649 for (i = 0; i < newmap->m_max_mds && i < mdsc->max_sessions; i++) {
2650 s = mdsc->sessions[i];
2651 if (!s)
2652 continue;
2653 if (!ceph_mdsmap_is_laggy(newmap, i))
2654 continue;
2655 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2656 s->s_state == CEPH_MDS_SESSION_HUNG ||
2657 s->s_state == CEPH_MDS_SESSION_CLOSING) {
2658 dout(" connecting to export targets of laggy mds%d\n",
2659 i);
2660 __open_export_target_sessions(mdsc, s);
2661 }
2662 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002663}
2664
2665
2666
2667/*
2668 * leases
2669 */
2670
2671/*
2672 * caller must hold session s_mutex, dentry->d_lock
2673 */
2674void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
2675{
2676 struct ceph_dentry_info *di = ceph_dentry(dentry);
2677
2678 ceph_put_mds_session(di->lease_session);
2679 di->lease_session = NULL;
2680}
2681
Sage Weil2600d2d2010-02-22 15:12:16 -08002682static void handle_lease(struct ceph_mds_client *mdsc,
2683 struct ceph_mds_session *session,
2684 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002685{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002686 struct super_block *sb = mdsc->fsc->sb;
Sage Weil2f2dc052009-10-06 11:31:09 -07002687 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07002688 struct ceph_inode_info *ci;
2689 struct dentry *parent, *dentry;
2690 struct ceph_dentry_info *di;
Sage Weil2600d2d2010-02-22 15:12:16 -08002691 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002692 struct ceph_mds_lease *h = msg->front.iov_base;
Sage Weil1e5ea232010-06-04 10:05:40 -07002693 u32 seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002694 struct ceph_vino vino;
2695 int mask;
2696 struct qstr dname;
2697 int release = 0;
2698
Sage Weil2f2dc052009-10-06 11:31:09 -07002699 dout("handle_lease from mds%d\n", mds);
2700
2701 /* decode */
2702 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
2703 goto bad;
2704 vino.ino = le64_to_cpu(h->ino);
2705 vino.snap = CEPH_NOSNAP;
2706 mask = le16_to_cpu(h->mask);
Sage Weil1e5ea232010-06-04 10:05:40 -07002707 seq = le32_to_cpu(h->seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002708 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
2709 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
2710 if (dname.len != get_unaligned_le32(h+1))
2711 goto bad;
2712
Sage Weil2f2dc052009-10-06 11:31:09 -07002713 mutex_lock(&session->s_mutex);
2714 session->s_seq++;
2715
2716 /* lookup inode */
2717 inode = ceph_find_inode(sb, vino);
Sage Weil1e5ea232010-06-04 10:05:40 -07002718 dout("handle_lease %s, mask %d, ino %llx %p %.*s\n",
2719 ceph_lease_op_name(h->action), mask, vino.ino, inode,
2720 dname.len, dname.name);
Sage Weil2f2dc052009-10-06 11:31:09 -07002721 if (inode == NULL) {
2722 dout("handle_lease no inode %llx\n", vino.ino);
2723 goto release;
2724 }
2725 ci = ceph_inode(inode);
2726
2727 /* dentry */
2728 parent = d_find_alias(inode);
2729 if (!parent) {
2730 dout("no parent dentry on inode %p\n", inode);
2731 WARN_ON(1);
2732 goto release; /* hrm... */
2733 }
2734 dname.hash = full_name_hash(dname.name, dname.len);
2735 dentry = d_lookup(parent, &dname);
2736 dput(parent);
2737 if (!dentry)
2738 goto release;
2739
2740 spin_lock(&dentry->d_lock);
2741 di = ceph_dentry(dentry);
2742 switch (h->action) {
2743 case CEPH_MDS_LEASE_REVOKE:
2744 if (di && di->lease_session == session) {
Sage Weil1e5ea232010-06-04 10:05:40 -07002745 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
2746 h->seq = cpu_to_le32(di->lease_seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002747 __ceph_mdsc_drop_dentry_lease(dentry);
2748 }
2749 release = 1;
2750 break;
2751
2752 case CEPH_MDS_LEASE_RENEW:
2753 if (di && di->lease_session == session &&
2754 di->lease_gen == session->s_cap_gen &&
2755 di->lease_renew_from &&
2756 di->lease_renew_after == 0) {
2757 unsigned long duration =
2758 le32_to_cpu(h->duration_ms) * HZ / 1000;
2759
Sage Weil1e5ea232010-06-04 10:05:40 -07002760 di->lease_seq = seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002761 dentry->d_time = di->lease_renew_from + duration;
2762 di->lease_renew_after = di->lease_renew_from +
2763 (duration >> 1);
2764 di->lease_renew_from = 0;
2765 }
2766 break;
2767 }
2768 spin_unlock(&dentry->d_lock);
2769 dput(dentry);
2770
2771 if (!release)
2772 goto out;
2773
2774release:
2775 /* let's just reuse the same message */
2776 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
2777 ceph_msg_get(msg);
2778 ceph_con_send(&session->s_con, msg);
2779
2780out:
2781 iput(inode);
2782 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002783 return;
2784
2785bad:
2786 pr_err("corrupt lease message\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002787 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002788}
2789
2790void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
2791 struct inode *inode,
2792 struct dentry *dentry, char action,
2793 u32 seq)
2794{
2795 struct ceph_msg *msg;
2796 struct ceph_mds_lease *lease;
2797 int len = sizeof(*lease) + sizeof(u32);
2798 int dnamelen = 0;
2799
2800 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
2801 inode, dentry, ceph_lease_op_name(action), session->s_mds);
2802 dnamelen = dentry->d_name.len;
2803 len += dnamelen;
2804
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002805 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -07002806 if (!msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002807 return;
2808 lease = msg->front.iov_base;
2809 lease->action = action;
Sage Weildd1c9052010-05-25 16:45:25 -07002810 lease->mask = cpu_to_le16(1);
Sage Weil2f2dc052009-10-06 11:31:09 -07002811 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
2812 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
2813 lease->seq = cpu_to_le32(seq);
2814 put_unaligned_le32(dnamelen, lease + 1);
2815 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
2816
2817 /*
2818 * if this is a preemptive lease RELEASE, no need to
2819 * flush request stream, since the actual request will
2820 * soon follow.
2821 */
2822 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
2823
2824 ceph_con_send(&session->s_con, msg);
2825}
2826
2827/*
2828 * Preemptively release a lease we expect to invalidate anyway.
2829 * Pass @inode always, @dentry is optional.
2830 */
2831void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
2832 struct dentry *dentry, int mask)
2833{
2834 struct ceph_dentry_info *di;
2835 struct ceph_mds_session *session;
2836 u32 seq;
2837
2838 BUG_ON(inode == NULL);
2839 BUG_ON(dentry == NULL);
Sage Weildd1c9052010-05-25 16:45:25 -07002840 BUG_ON(mask == 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07002841
2842 /* is dentry lease valid? */
2843 spin_lock(&dentry->d_lock);
2844 di = ceph_dentry(dentry);
2845 if (!di || !di->lease_session ||
2846 di->lease_session->s_mds < 0 ||
2847 di->lease_gen != di->lease_session->s_cap_gen ||
2848 !time_before(jiffies, dentry->d_time)) {
2849 dout("lease_release inode %p dentry %p -- "
2850 "no lease on %d\n",
2851 inode, dentry, mask);
2852 spin_unlock(&dentry->d_lock);
2853 return;
2854 }
2855
2856 /* we do have a lease on this dentry; note mds and seq */
2857 session = ceph_get_mds_session(di->lease_session);
2858 seq = di->lease_seq;
2859 __ceph_mdsc_drop_dentry_lease(dentry);
2860 spin_unlock(&dentry->d_lock);
2861
2862 dout("lease_release inode %p dentry %p mask %d to mds%d\n",
2863 inode, dentry, mask, session->s_mds);
2864 ceph_mdsc_lease_send_msg(session, inode, dentry,
2865 CEPH_MDS_LEASE_RELEASE, seq);
2866 ceph_put_mds_session(session);
2867}
2868
2869/*
2870 * drop all leases (and dentry refs) in preparation for umount
2871 */
2872static void drop_leases(struct ceph_mds_client *mdsc)
2873{
2874 int i;
2875
2876 dout("drop_leases\n");
2877 mutex_lock(&mdsc->mutex);
2878 for (i = 0; i < mdsc->max_sessions; i++) {
2879 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2880 if (!s)
2881 continue;
2882 mutex_unlock(&mdsc->mutex);
2883 mutex_lock(&s->s_mutex);
2884 mutex_unlock(&s->s_mutex);
2885 ceph_put_mds_session(s);
2886 mutex_lock(&mdsc->mutex);
2887 }
2888 mutex_unlock(&mdsc->mutex);
2889}
2890
2891
2892
2893/*
2894 * delayed work -- periodically trim expired leases, renew caps with mds
2895 */
2896static void schedule_delayed(struct ceph_mds_client *mdsc)
2897{
2898 int delay = 5;
2899 unsigned hz = round_jiffies_relative(HZ * delay);
2900 schedule_delayed_work(&mdsc->delayed_work, hz);
2901}
2902
2903static void delayed_work(struct work_struct *work)
2904{
2905 int i;
2906 struct ceph_mds_client *mdsc =
2907 container_of(work, struct ceph_mds_client, delayed_work.work);
2908 int renew_interval;
2909 int renew_caps;
2910
2911 dout("mdsc delayed_work\n");
Sage Weilafcdaea2009-10-14 14:27:38 -07002912 ceph_check_delayed_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07002913
2914 mutex_lock(&mdsc->mutex);
2915 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
2916 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
2917 mdsc->last_renew_caps);
2918 if (renew_caps)
2919 mdsc->last_renew_caps = jiffies;
2920
2921 for (i = 0; i < mdsc->max_sessions; i++) {
2922 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2923 if (s == NULL)
2924 continue;
2925 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
2926 dout("resending session close request for mds%d\n",
2927 s->s_mds);
2928 request_close_session(mdsc, s);
2929 ceph_put_mds_session(s);
2930 continue;
2931 }
2932 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
2933 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
2934 s->s_state = CEPH_MDS_SESSION_HUNG;
2935 pr_info("mds%d hung\n", s->s_mds);
2936 }
2937 }
2938 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
2939 /* this mds is failed or recovering, just wait */
2940 ceph_put_mds_session(s);
2941 continue;
2942 }
2943 mutex_unlock(&mdsc->mutex);
2944
2945 mutex_lock(&s->s_mutex);
2946 if (renew_caps)
2947 send_renew_caps(mdsc, s);
2948 else
2949 ceph_con_keepalive(&s->s_con);
Sage Weilee6b2722010-06-10 12:55:52 -07002950 ceph_add_cap_releases(mdsc, s);
Sage Weilaab53dd2010-03-17 16:30:21 -07002951 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2952 s->s_state == CEPH_MDS_SESSION_HUNG)
Sage Weil3d7ded42010-06-09 16:47:10 -07002953 ceph_send_cap_releases(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07002954 mutex_unlock(&s->s_mutex);
2955 ceph_put_mds_session(s);
2956
2957 mutex_lock(&mdsc->mutex);
2958 }
2959 mutex_unlock(&mdsc->mutex);
2960
2961 schedule_delayed(mdsc);
2962}
2963
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002964int ceph_mdsc_init(struct ceph_fs_client *fsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07002965
Sage Weil2f2dc052009-10-06 11:31:09 -07002966{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002967 struct ceph_mds_client *mdsc;
2968
2969 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
2970 if (!mdsc)
2971 return -ENOMEM;
2972 mdsc->fsc = fsc;
2973 fsc->mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07002974 mutex_init(&mdsc->mutex);
2975 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08002976 if (mdsc->mdsmap == NULL)
2977 return -ENOMEM;
2978
Sage Weil2f2dc052009-10-06 11:31:09 -07002979 init_completion(&mdsc->safe_umount_waiters);
Sage Weilf3c60c52010-08-11 14:51:23 -07002980 init_waitqueue_head(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002981 INIT_LIST_HEAD(&mdsc->waiting_for_map);
2982 mdsc->sessions = NULL;
2983 mdsc->max_sessions = 0;
2984 mdsc->stopping = 0;
2985 init_rwsem(&mdsc->snap_rwsem);
Sage Weila105f002010-02-15 14:37:55 -08002986 mdsc->snap_realms = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07002987 INIT_LIST_HEAD(&mdsc->snap_empty);
2988 spin_lock_init(&mdsc->snap_empty_lock);
2989 mdsc->last_tid = 0;
Sage Weil44ca18f2010-02-15 12:08:46 -08002990 mdsc->request_tree = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07002991 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
2992 mdsc->last_renew_caps = jiffies;
2993 INIT_LIST_HEAD(&mdsc->cap_delay_list);
2994 spin_lock_init(&mdsc->cap_delay_lock);
2995 INIT_LIST_HEAD(&mdsc->snap_flush_list);
2996 spin_lock_init(&mdsc->snap_flush_lock);
2997 mdsc->cap_flush_seq = 0;
2998 INIT_LIST_HEAD(&mdsc->cap_dirty);
2999 mdsc->num_cap_flushing = 0;
3000 spin_lock_init(&mdsc->cap_dirty_lock);
3001 init_waitqueue_head(&mdsc->cap_flushing_wq);
3002 spin_lock_init(&mdsc->dentry_lru_lock);
3003 INIT_LIST_HEAD(&mdsc->dentry_lru);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08003004
Yehuda Sadeh37151662010-06-17 16:16:12 -07003005 ceph_caps_init(mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003006 ceph_adjust_min_caps(mdsc, fsc->min_caps);
Yehuda Sadeh37151662010-06-17 16:16:12 -07003007
Sage Weil5f44f142009-11-18 14:52:18 -08003008 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003009}
3010
3011/*
3012 * Wait for safe replies on open mds requests. If we time out, drop
3013 * all requests from the tree to avoid dangling dentry refs.
3014 */
3015static void wait_requests(struct ceph_mds_client *mdsc)
3016{
3017 struct ceph_mds_request *req;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003018 struct ceph_fs_client *fsc = mdsc->fsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07003019
3020 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003021 if (__get_oldest_req(mdsc)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003022 mutex_unlock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003023
Sage Weil2f2dc052009-10-06 11:31:09 -07003024 dout("wait_requests waiting for requests\n");
3025 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003026 fsc->client->options->mount_timeout * HZ);
Sage Weil2f2dc052009-10-06 11:31:09 -07003027
3028 /* tear down remaining requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08003029 mutex_lock(&mdsc->mutex);
3030 while ((req = __get_oldest_req(mdsc))) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003031 dout("wait_requests timed out on tid %llu\n",
3032 req->r_tid);
Sage Weil44ca18f2010-02-15 12:08:46 -08003033 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07003034 }
3035 }
3036 mutex_unlock(&mdsc->mutex);
3037 dout("wait_requests done\n");
3038}
3039
3040/*
3041 * called before mount is ro, and before dentries are torn down.
3042 * (hmm, does this still race with new lookups?)
3043 */
3044void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
3045{
3046 dout("pre_umount\n");
3047 mdsc->stopping = 1;
3048
3049 drop_leases(mdsc);
Sage Weilafcdaea2009-10-14 14:27:38 -07003050 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003051 wait_requests(mdsc);
Sage Weil17c688c2010-06-21 16:12:26 -07003052
3053 /*
3054 * wait for reply handlers to drop their request refs and
3055 * their inode/dcache refs
3056 */
3057 ceph_msgr_flush();
Sage Weil2f2dc052009-10-06 11:31:09 -07003058}
3059
3060/*
3061 * wait for all write mds requests to flush.
3062 */
3063static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
3064{
Sage Weil80fc7312010-03-16 15:28:54 -07003065 struct ceph_mds_request *req = NULL, *nextreq;
Sage Weil44ca18f2010-02-15 12:08:46 -08003066 struct rb_node *n;
Sage Weil2f2dc052009-10-06 11:31:09 -07003067
3068 mutex_lock(&mdsc->mutex);
3069 dout("wait_unsafe_requests want %lld\n", want_tid);
Sage Weil80fc7312010-03-16 15:28:54 -07003070restart:
Sage Weil44ca18f2010-02-15 12:08:46 -08003071 req = __get_oldest_req(mdsc);
3072 while (req && req->r_tid <= want_tid) {
Sage Weil80fc7312010-03-16 15:28:54 -07003073 /* find next request */
3074 n = rb_next(&req->r_node);
3075 if (n)
3076 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
3077 else
3078 nextreq = NULL;
Sage Weil44ca18f2010-02-15 12:08:46 -08003079 if ((req->r_op & CEPH_MDS_OP_WRITE)) {
3080 /* write op */
3081 ceph_mdsc_get_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07003082 if (nextreq)
3083 ceph_mdsc_get_request(nextreq);
Sage Weil44ca18f2010-02-15 12:08:46 -08003084 mutex_unlock(&mdsc->mutex);
3085 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3086 req->r_tid, want_tid);
3087 wait_for_completion(&req->r_safe_completion);
3088 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003089 ceph_mdsc_put_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07003090 if (!nextreq)
3091 break; /* next dne before, so we're done! */
3092 if (RB_EMPTY_NODE(&nextreq->r_node)) {
3093 /* next request was removed from tree */
3094 ceph_mdsc_put_request(nextreq);
3095 goto restart;
3096 }
3097 ceph_mdsc_put_request(nextreq); /* won't go away */
Sage Weil44ca18f2010-02-15 12:08:46 -08003098 }
Sage Weil80fc7312010-03-16 15:28:54 -07003099 req = nextreq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003100 }
3101 mutex_unlock(&mdsc->mutex);
3102 dout("wait_unsafe_requests done\n");
3103}
3104
3105void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
3106{
3107 u64 want_tid, want_flush;
3108
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003109 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
Sage Weil56b7cf92010-05-03 15:22:00 -07003110 return;
3111
Sage Weil2f2dc052009-10-06 11:31:09 -07003112 dout("sync\n");
3113 mutex_lock(&mdsc->mutex);
3114 want_tid = mdsc->last_tid;
3115 want_flush = mdsc->cap_flush_seq;
3116 mutex_unlock(&mdsc->mutex);
3117 dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
3118
Sage Weilafcdaea2009-10-14 14:27:38 -07003119 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003120
3121 wait_unsafe_requests(mdsc, want_tid);
3122 wait_event(mdsc->cap_flushing_wq, check_cap_flush(mdsc, want_flush));
3123}
3124
Sage Weilf3c60c52010-08-11 14:51:23 -07003125/*
3126 * true if all sessions are closed, or we force unmount
3127 */
3128bool done_closing_sessions(struct ceph_mds_client *mdsc)
3129{
3130 int i, n = 0;
3131
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003132 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
Sage Weilf3c60c52010-08-11 14:51:23 -07003133 return true;
3134
3135 mutex_lock(&mdsc->mutex);
3136 for (i = 0; i < mdsc->max_sessions; i++)
3137 if (mdsc->sessions[i])
3138 n++;
3139 mutex_unlock(&mdsc->mutex);
3140 return n == 0;
3141}
Sage Weil2f2dc052009-10-06 11:31:09 -07003142
3143/*
3144 * called after sb is ro.
3145 */
3146void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
3147{
3148 struct ceph_mds_session *session;
3149 int i;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003150 struct ceph_fs_client *fsc = mdsc->fsc;
3151 unsigned long timeout = fsc->client->options->mount_timeout * HZ;
Sage Weil2f2dc052009-10-06 11:31:09 -07003152
3153 dout("close_sessions\n");
3154
Sage Weil2f2dc052009-10-06 11:31:09 -07003155 /* close sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07003156 mutex_lock(&mdsc->mutex);
3157 for (i = 0; i < mdsc->max_sessions; i++) {
3158 session = __ceph_lookup_mds_session(mdsc, i);
3159 if (!session)
3160 continue;
Sage Weil2f2dc052009-10-06 11:31:09 -07003161 mutex_unlock(&mdsc->mutex);
Sage Weilf3c60c52010-08-11 14:51:23 -07003162 mutex_lock(&session->s_mutex);
3163 __close_session(mdsc, session);
3164 mutex_unlock(&session->s_mutex);
3165 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003166 mutex_lock(&mdsc->mutex);
3167 }
Sage Weilf3c60c52010-08-11 14:51:23 -07003168 mutex_unlock(&mdsc->mutex);
3169
3170 dout("waiting for sessions to close\n");
3171 wait_event_timeout(mdsc->session_close_wq, done_closing_sessions(mdsc),
3172 timeout);
Sage Weil2f2dc052009-10-06 11:31:09 -07003173
3174 /* tear down remaining sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07003175 mutex_lock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003176 for (i = 0; i < mdsc->max_sessions; i++) {
3177 if (mdsc->sessions[i]) {
3178 session = get_session(mdsc->sessions[i]);
Sage Weil2600d2d2010-02-22 15:12:16 -08003179 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003180 mutex_unlock(&mdsc->mutex);
3181 mutex_lock(&session->s_mutex);
3182 remove_session_caps(session);
3183 mutex_unlock(&session->s_mutex);
3184 ceph_put_mds_session(session);
3185 mutex_lock(&mdsc->mutex);
3186 }
3187 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003188 WARN_ON(!list_empty(&mdsc->cap_delay_list));
Sage Weil2f2dc052009-10-06 11:31:09 -07003189 mutex_unlock(&mdsc->mutex);
3190
3191 ceph_cleanup_empty_realms(mdsc);
3192
3193 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3194
3195 dout("stopped\n");
3196}
3197
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003198static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07003199{
3200 dout("stop\n");
3201 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3202 if (mdsc->mdsmap)
3203 ceph_mdsmap_destroy(mdsc->mdsmap);
3204 kfree(mdsc->sessions);
Yehuda Sadeh37151662010-06-17 16:16:12 -07003205 ceph_caps_finalize(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003206}
3207
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003208void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
3209{
3210 struct ceph_mds_client *mdsc = fsc->mdsc;
3211
3212 ceph_mdsc_stop(mdsc);
3213 fsc->mdsc = NULL;
3214 kfree(mdsc);
3215}
3216
Sage Weil2f2dc052009-10-06 11:31:09 -07003217
3218/*
3219 * handle mds map update.
3220 */
3221void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3222{
3223 u32 epoch;
3224 u32 maplen;
3225 void *p = msg->front.iov_base;
3226 void *end = p + msg->front.iov_len;
3227 struct ceph_mdsmap *newmap, *oldmap;
3228 struct ceph_fsid fsid;
3229 int err = -EINVAL;
3230
3231 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
3232 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003233 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
Sage Weil07433042009-11-18 16:50:41 -08003234 return;
Sage Weilc89136e2009-10-14 09:59:09 -07003235 epoch = ceph_decode_32(&p);
3236 maplen = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07003237 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
3238
3239 /* do we need it? */
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003240 ceph_monc_got_mdsmap(&mdsc->fsc->client->monc, epoch);
Sage Weil2f2dc052009-10-06 11:31:09 -07003241 mutex_lock(&mdsc->mutex);
3242 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
3243 dout("handle_map epoch %u <= our %u\n",
3244 epoch, mdsc->mdsmap->m_epoch);
3245 mutex_unlock(&mdsc->mutex);
3246 return;
3247 }
3248
3249 newmap = ceph_mdsmap_decode(&p, end);
3250 if (IS_ERR(newmap)) {
3251 err = PTR_ERR(newmap);
3252 goto bad_unlock;
3253 }
3254
3255 /* swap into place */
3256 if (mdsc->mdsmap) {
3257 oldmap = mdsc->mdsmap;
3258 mdsc->mdsmap = newmap;
3259 check_new_map(mdsc, newmap, oldmap);
3260 ceph_mdsmap_destroy(oldmap);
3261 } else {
3262 mdsc->mdsmap = newmap; /* first mds map */
3263 }
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003264 mdsc->fsc->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
Sage Weil2f2dc052009-10-06 11:31:09 -07003265
3266 __wake_requests(mdsc, &mdsc->waiting_for_map);
3267
3268 mutex_unlock(&mdsc->mutex);
3269 schedule_delayed(mdsc);
3270 return;
3271
3272bad_unlock:
3273 mutex_unlock(&mdsc->mutex);
3274bad:
3275 pr_err("error decoding mdsmap %d\n", err);
3276 return;
3277}
3278
3279static struct ceph_connection *con_get(struct ceph_connection *con)
3280{
3281 struct ceph_mds_session *s = con->private;
3282
3283 if (get_session(s)) {
Sage Weil2600d2d2010-02-22 15:12:16 -08003284 dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -07003285 return con;
3286 }
3287 dout("mdsc con_get %p FAIL\n", s);
3288 return NULL;
3289}
3290
3291static void con_put(struct ceph_connection *con)
3292{
3293 struct ceph_mds_session *s = con->private;
3294
Sage Weil2f2dc052009-10-06 11:31:09 -07003295 ceph_put_mds_session(s);
Sage Weil2600d2d2010-02-22 15:12:16 -08003296 dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -07003297}
3298
3299/*
3300 * if the client is unresponsive for long enough, the mds will kill
3301 * the session entirely.
3302 */
3303static void peer_reset(struct ceph_connection *con)
3304{
3305 struct ceph_mds_session *s = con->private;
Sage Weil7e70f0e2010-03-18 13:59:12 -07003306 struct ceph_mds_client *mdsc = s->s_mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07003307
Sage Weil7e70f0e2010-03-18 13:59:12 -07003308 pr_warning("mds%d closed our session\n", s->s_mds);
3309 send_mds_reconnect(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07003310}
3311
3312static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
3313{
3314 struct ceph_mds_session *s = con->private;
3315 struct ceph_mds_client *mdsc = s->s_mdsc;
3316 int type = le16_to_cpu(msg->hdr.type);
3317
Sage Weil2600d2d2010-02-22 15:12:16 -08003318 mutex_lock(&mdsc->mutex);
3319 if (__verify_registered_session(mdsc, s) < 0) {
3320 mutex_unlock(&mdsc->mutex);
3321 goto out;
3322 }
3323 mutex_unlock(&mdsc->mutex);
3324
Sage Weil2f2dc052009-10-06 11:31:09 -07003325 switch (type) {
3326 case CEPH_MSG_MDS_MAP:
3327 ceph_mdsc_handle_map(mdsc, msg);
3328 break;
3329 case CEPH_MSG_CLIENT_SESSION:
3330 handle_session(s, msg);
3331 break;
3332 case CEPH_MSG_CLIENT_REPLY:
3333 handle_reply(s, msg);
3334 break;
3335 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
Sage Weil2600d2d2010-02-22 15:12:16 -08003336 handle_forward(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003337 break;
3338 case CEPH_MSG_CLIENT_CAPS:
3339 ceph_handle_caps(s, msg);
3340 break;
3341 case CEPH_MSG_CLIENT_SNAP:
Sage Weil2600d2d2010-02-22 15:12:16 -08003342 ceph_handle_snap(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003343 break;
3344 case CEPH_MSG_CLIENT_LEASE:
Sage Weil2600d2d2010-02-22 15:12:16 -08003345 handle_lease(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003346 break;
3347
3348 default:
3349 pr_err("received unknown message type %d %s\n", type,
3350 ceph_msg_type_name(type));
3351 }
Sage Weil2600d2d2010-02-22 15:12:16 -08003352out:
Sage Weil2f2dc052009-10-06 11:31:09 -07003353 ceph_msg_put(msg);
3354}
3355
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003356/*
3357 * authentication
3358 */
3359static int get_authorizer(struct ceph_connection *con,
3360 void **buf, int *len, int *proto,
3361 void **reply_buf, int *reply_len, int force_new)
3362{
3363 struct ceph_mds_session *s = con->private;
3364 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003365 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003366 int ret = 0;
3367
3368 if (force_new && s->s_authorizer) {
3369 ac->ops->destroy_authorizer(ac, s->s_authorizer);
3370 s->s_authorizer = NULL;
3371 }
3372 if (s->s_authorizer == NULL) {
3373 if (ac->ops->create_authorizer) {
3374 ret = ac->ops->create_authorizer(
3375 ac, CEPH_ENTITY_TYPE_MDS,
3376 &s->s_authorizer,
3377 &s->s_authorizer_buf,
3378 &s->s_authorizer_buf_len,
3379 &s->s_authorizer_reply_buf,
3380 &s->s_authorizer_reply_buf_len);
3381 if (ret)
3382 return ret;
3383 }
3384 }
3385
3386 *proto = ac->protocol;
3387 *buf = s->s_authorizer_buf;
3388 *len = s->s_authorizer_buf_len;
3389 *reply_buf = s->s_authorizer_reply_buf;
3390 *reply_len = s->s_authorizer_reply_buf_len;
3391 return 0;
3392}
3393
3394
3395static int verify_authorizer_reply(struct ceph_connection *con, int len)
3396{
3397 struct ceph_mds_session *s = con->private;
3398 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003399 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003400
3401 return ac->ops->verify_authorizer_reply(ac, s->s_authorizer, len);
3402}
3403
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003404static int invalidate_authorizer(struct ceph_connection *con)
3405{
3406 struct ceph_mds_session *s = con->private;
3407 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003408 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003409
3410 if (ac->ops->invalidate_authorizer)
3411 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
3412
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003413 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003414}
3415
Tobias Klauser9e327892010-05-20 10:40:19 +02003416static const struct ceph_connection_operations mds_con_ops = {
Sage Weil2f2dc052009-10-06 11:31:09 -07003417 .get = con_get,
3418 .put = con_put,
3419 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003420 .get_authorizer = get_authorizer,
3421 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003422 .invalidate_authorizer = invalidate_authorizer,
Sage Weil2f2dc052009-10-06 11:31:09 -07003423 .peer_reset = peer_reset,
Sage Weil2f2dc052009-10-06 11:31:09 -07003424};
3425
Sage Weil2f2dc052009-10-06 11:31:09 -07003426/* eof */