blob: f60b07b0feb075042e91c3cdabea569bd01ee158 [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),
Sage Weild66bbd42011-01-21 21:16:46 -0800696 frag.frag, mds,
Sage Weil2f2dc052009-10-06 11:31:09 -0700697 (int)r, frag.ndist);
Sage Weild66bbd42011-01-21 21:16:46 -0800698 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
699 CEPH_MDS_STATE_ACTIVE)
700 return mds;
Sage Weil2f2dc052009-10-06 11:31:09 -0700701 }
702
703 /* since this file/dir wasn't known to be
704 * replicated, then we want to look for the
705 * authoritative mds. */
706 mode = USE_AUTH_MDS;
707 if (frag.mds >= 0) {
708 /* choose auth mds */
709 mds = frag.mds;
710 dout("choose_mds %p %llx.%llx "
711 "frag %u mds%d (auth)\n",
712 inode, ceph_vinop(inode), frag.frag, mds);
Sage Weild66bbd42011-01-21 21:16:46 -0800713 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
714 CEPH_MDS_STATE_ACTIVE)
715 return mds;
Sage Weil2f2dc052009-10-06 11:31:09 -0700716 }
717 }
718 }
719
720 spin_lock(&inode->i_lock);
721 cap = NULL;
722 if (mode == USE_AUTH_MDS)
723 cap = ci->i_auth_cap;
724 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
725 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
726 if (!cap) {
727 spin_unlock(&inode->i_lock);
728 goto random;
729 }
730 mds = cap->session->s_mds;
731 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
732 inode, ceph_vinop(inode), mds,
733 cap == ci->i_auth_cap ? "auth " : "", cap);
734 spin_unlock(&inode->i_lock);
735 return mds;
736
737random:
738 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
739 dout("choose_mds chose random mds%d\n", mds);
740 return mds;
741}
742
743
744/*
745 * session messages
746 */
747static struct ceph_msg *create_session_msg(u32 op, u64 seq)
748{
749 struct ceph_msg *msg;
750 struct ceph_mds_session_head *h;
751
Yehuda Sadeh34d23762010-04-06 14:33:58 -0700752 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -0700753 if (!msg) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700754 pr_err("create_session_msg ENOMEM creating msg\n");
Sage Weila79832f2010-04-01 16:06:19 -0700755 return NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700756 }
757 h = msg->front.iov_base;
758 h->op = cpu_to_le32(op);
759 h->seq = cpu_to_le64(seq);
760 return msg;
761}
762
763/*
764 * send session open request.
765 *
766 * called under mdsc->mutex
767 */
768static int __open_session(struct ceph_mds_client *mdsc,
769 struct ceph_mds_session *session)
770{
771 struct ceph_msg *msg;
772 int mstate;
773 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -0700774
775 /* wait for mds to go active? */
776 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
777 dout("open_session to mds%d (%s)\n", mds,
778 ceph_mds_state_name(mstate));
779 session->s_state = CEPH_MDS_SESSION_OPENING;
780 session->s_renew_requested = jiffies;
781
782 /* send connect message */
783 msg = create_session_msg(CEPH_SESSION_REQUEST_OPEN, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -0700784 if (!msg)
785 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -0700786 ceph_con_send(&session->s_con, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -0700787 return 0;
788}
789
790/*
Sage Weiled0552a2010-06-21 13:38:25 -0700791 * open sessions for any export targets for the given mds
792 *
793 * called under mdsc->mutex
794 */
795static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
796 struct ceph_mds_session *session)
797{
798 struct ceph_mds_info *mi;
799 struct ceph_mds_session *ts;
800 int i, mds = session->s_mds;
801 int target;
802
803 if (mds >= mdsc->mdsmap->m_max_mds)
804 return;
805 mi = &mdsc->mdsmap->m_info[mds];
806 dout("open_export_target_sessions for mds%d (%d targets)\n",
807 session->s_mds, mi->num_export_targets);
808
809 for (i = 0; i < mi->num_export_targets; i++) {
810 target = mi->export_targets[i];
811 ts = __ceph_lookup_mds_session(mdsc, target);
812 if (!ts) {
813 ts = register_session(mdsc, target);
814 if (IS_ERR(ts))
815 return;
816 }
817 if (session->s_state == CEPH_MDS_SESSION_NEW ||
818 session->s_state == CEPH_MDS_SESSION_CLOSING)
819 __open_session(mdsc, session);
820 else
821 dout(" mds%d target mds%d %p is %s\n", session->s_mds,
822 i, ts, session_state_name(ts->s_state));
823 ceph_put_mds_session(ts);
824 }
825}
826
Sage Weil154f42c2010-06-21 13:45:04 -0700827void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
828 struct ceph_mds_session *session)
829{
830 mutex_lock(&mdsc->mutex);
831 __open_export_target_sessions(mdsc, session);
832 mutex_unlock(&mdsc->mutex);
833}
834
Sage Weiled0552a2010-06-21 13:38:25 -0700835/*
Sage Weil2f2dc052009-10-06 11:31:09 -0700836 * session caps
837 */
838
839/*
840 * Free preallocated cap messages assigned to this session
841 */
842static void cleanup_cap_releases(struct ceph_mds_session *session)
843{
844 struct ceph_msg *msg;
845
846 spin_lock(&session->s_cap_lock);
847 while (!list_empty(&session->s_cap_releases)) {
848 msg = list_first_entry(&session->s_cap_releases,
849 struct ceph_msg, list_head);
850 list_del_init(&msg->list_head);
851 ceph_msg_put(msg);
852 }
853 while (!list_empty(&session->s_cap_releases_done)) {
854 msg = list_first_entry(&session->s_cap_releases_done,
855 struct ceph_msg, list_head);
856 list_del_init(&msg->list_head);
857 ceph_msg_put(msg);
858 }
859 spin_unlock(&session->s_cap_lock);
860}
861
862/*
Sage Weilf818a732010-05-11 20:56:31 -0700863 * Helper to safely iterate over all caps associated with a session, with
864 * special care taken to handle a racing __ceph_remove_cap().
Sage Weil2f2dc052009-10-06 11:31:09 -0700865 *
Sage Weilf818a732010-05-11 20:56:31 -0700866 * Caller must hold session s_mutex.
Sage Weil2f2dc052009-10-06 11:31:09 -0700867 */
868static int iterate_session_caps(struct ceph_mds_session *session,
869 int (*cb)(struct inode *, struct ceph_cap *,
870 void *), void *arg)
871{
Sage Weil7c1332b2010-02-16 11:39:45 -0800872 struct list_head *p;
873 struct ceph_cap *cap;
874 struct inode *inode, *last_inode = NULL;
875 struct ceph_cap *old_cap = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700876 int ret;
877
878 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
879 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800880 p = session->s_caps.next;
881 while (p != &session->s_caps) {
882 cap = list_entry(p, struct ceph_cap, session_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -0700883 inode = igrab(&cap->ci->vfs_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -0800884 if (!inode) {
885 p = p->next;
Sage Weil2f2dc052009-10-06 11:31:09 -0700886 continue;
Sage Weil7c1332b2010-02-16 11:39:45 -0800887 }
888 session->s_cap_iterator = cap;
Sage Weil2f2dc052009-10-06 11:31:09 -0700889 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800890
891 if (last_inode) {
892 iput(last_inode);
893 last_inode = NULL;
894 }
895 if (old_cap) {
Yehuda Sadeh37151662010-06-17 16:16:12 -0700896 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -0800897 old_cap = NULL;
898 }
899
Sage Weil2f2dc052009-10-06 11:31:09 -0700900 ret = cb(inode, cap, arg);
Sage Weil7c1332b2010-02-16 11:39:45 -0800901 last_inode = inode;
902
Sage Weil2f2dc052009-10-06 11:31:09 -0700903 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800904 p = p->next;
905 if (cap->ci == NULL) {
906 dout("iterate_session_caps finishing cap %p removal\n",
907 cap);
908 BUG_ON(cap->session != session);
909 list_del_init(&cap->session_caps);
910 session->s_nr_caps--;
911 cap->session = NULL;
912 old_cap = cap; /* put_cap it w/o locks held */
913 }
Sage Weil5dacf092009-12-21 20:40:34 -0800914 if (ret < 0)
915 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -0700916 }
Sage Weil5dacf092009-12-21 20:40:34 -0800917 ret = 0;
918out:
Sage Weil7c1332b2010-02-16 11:39:45 -0800919 session->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700920 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800921
922 if (last_inode)
923 iput(last_inode);
924 if (old_cap)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700925 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -0800926
Sage Weil5dacf092009-12-21 20:40:34 -0800927 return ret;
Sage Weil2f2dc052009-10-06 11:31:09 -0700928}
929
930static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
Sage Weil6c99f252010-05-10 16:12:25 -0700931 void *arg)
Sage Weil2f2dc052009-10-06 11:31:09 -0700932{
933 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil6c99f252010-05-10 16:12:25 -0700934 int drop = 0;
935
Sage Weil2f2dc052009-10-06 11:31:09 -0700936 dout("removing cap %p, ci is %p, inode is %p\n",
937 cap, ci, &ci->vfs_inode);
Sage Weil6c99f252010-05-10 16:12:25 -0700938 spin_lock(&inode->i_lock);
939 __ceph_remove_cap(cap);
940 if (!__ceph_is_any_real_caps(ci)) {
941 struct ceph_mds_client *mdsc =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700942 ceph_sb_to_client(inode->i_sb)->mdsc;
Sage Weil6c99f252010-05-10 16:12:25 -0700943
944 spin_lock(&mdsc->cap_dirty_lock);
945 if (!list_empty(&ci->i_dirty_item)) {
946 pr_info(" dropping dirty %s state for %p %lld\n",
947 ceph_cap_string(ci->i_dirty_caps),
948 inode, ceph_ino(inode));
949 ci->i_dirty_caps = 0;
950 list_del_init(&ci->i_dirty_item);
951 drop = 1;
952 }
953 if (!list_empty(&ci->i_flushing_item)) {
954 pr_info(" dropping dirty+flushing %s state for %p %lld\n",
955 ceph_cap_string(ci->i_flushing_caps),
956 inode, ceph_ino(inode));
957 ci->i_flushing_caps = 0;
958 list_del_init(&ci->i_flushing_item);
959 mdsc->num_cap_flushing--;
960 drop = 1;
961 }
962 if (drop && ci->i_wrbuffer_ref) {
963 pr_info(" dropping dirty data for %p %lld\n",
964 inode, ceph_ino(inode));
965 ci->i_wrbuffer_ref = 0;
966 ci->i_wrbuffer_ref_head = 0;
967 drop++;
968 }
969 spin_unlock(&mdsc->cap_dirty_lock);
970 }
971 spin_unlock(&inode->i_lock);
972 while (drop--)
973 iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -0700974 return 0;
975}
976
977/*
978 * caller must hold session s_mutex
979 */
980static void remove_session_caps(struct ceph_mds_session *session)
981{
982 dout("remove_session_caps on %p\n", session);
983 iterate_session_caps(session, remove_session_caps_cb, NULL);
984 BUG_ON(session->s_nr_caps > 0);
Sage Weil6c99f252010-05-10 16:12:25 -0700985 BUG_ON(!list_empty(&session->s_cap_flushing));
Sage Weil2f2dc052009-10-06 11:31:09 -0700986 cleanup_cap_releases(session);
987}
988
989/*
990 * wake up any threads waiting on this session's caps. if the cap is
991 * old (didn't get renewed on the client reconnect), remove it now.
992 *
993 * caller must hold s_mutex.
994 */
995static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
996 void *arg)
997{
Sage Weil0dc25702009-11-20 13:43:45 -0800998 struct ceph_inode_info *ci = ceph_inode(inode);
999
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001000 wake_up_all(&ci->i_cap_wq);
Sage Weil0dc25702009-11-20 13:43:45 -08001001 if (arg) {
1002 spin_lock(&inode->i_lock);
1003 ci->i_wanted_max_size = 0;
1004 ci->i_requested_max_size = 0;
1005 spin_unlock(&inode->i_lock);
1006 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001007 return 0;
1008}
1009
Sage Weil0dc25702009-11-20 13:43:45 -08001010static void wake_up_session_caps(struct ceph_mds_session *session,
1011 int reconnect)
Sage Weil2f2dc052009-10-06 11:31:09 -07001012{
1013 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
Sage Weil0dc25702009-11-20 13:43:45 -08001014 iterate_session_caps(session, wake_up_session_cb,
1015 (void *)(unsigned long)reconnect);
Sage Weil2f2dc052009-10-06 11:31:09 -07001016}
1017
1018/*
1019 * Send periodic message to MDS renewing all currently held caps. The
1020 * ack will reset the expiration for all caps from this session.
1021 *
1022 * caller holds s_mutex
1023 */
1024static int send_renew_caps(struct ceph_mds_client *mdsc,
1025 struct ceph_mds_session *session)
1026{
1027 struct ceph_msg *msg;
1028 int state;
1029
1030 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1031 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1032 pr_info("mds%d caps stale\n", session->s_mds);
Sage Weile4cb4cb2010-03-18 13:43:09 -07001033 session->s_renew_requested = jiffies;
Sage Weil2f2dc052009-10-06 11:31:09 -07001034
1035 /* do not try to renew caps until a recovering mds has reconnected
1036 * with its clients. */
1037 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1038 if (state < CEPH_MDS_STATE_RECONNECT) {
1039 dout("send_renew_caps ignoring mds%d (%s)\n",
1040 session->s_mds, ceph_mds_state_name(state));
1041 return 0;
1042 }
1043
1044 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1045 ceph_mds_state_name(state));
Sage Weil2f2dc052009-10-06 11:31:09 -07001046 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1047 ++session->s_renew_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001048 if (!msg)
1049 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -07001050 ceph_con_send(&session->s_con, msg);
1051 return 0;
1052}
1053
1054/*
1055 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
Sage Weil0dc25702009-11-20 13:43:45 -08001056 *
1057 * Called under session->s_mutex
Sage Weil2f2dc052009-10-06 11:31:09 -07001058 */
1059static void renewed_caps(struct ceph_mds_client *mdsc,
1060 struct ceph_mds_session *session, int is_renew)
1061{
1062 int was_stale;
1063 int wake = 0;
1064
1065 spin_lock(&session->s_cap_lock);
1066 was_stale = is_renew && (session->s_cap_ttl == 0 ||
1067 time_after_eq(jiffies, session->s_cap_ttl));
1068
1069 session->s_cap_ttl = session->s_renew_requested +
1070 mdsc->mdsmap->m_session_timeout*HZ;
1071
1072 if (was_stale) {
1073 if (time_before(jiffies, session->s_cap_ttl)) {
1074 pr_info("mds%d caps renewed\n", session->s_mds);
1075 wake = 1;
1076 } else {
1077 pr_info("mds%d caps still stale\n", session->s_mds);
1078 }
1079 }
1080 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1081 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1082 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1083 spin_unlock(&session->s_cap_lock);
1084
1085 if (wake)
Sage Weil0dc25702009-11-20 13:43:45 -08001086 wake_up_session_caps(session, 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07001087}
1088
1089/*
1090 * send a session close request
1091 */
1092static int request_close_session(struct ceph_mds_client *mdsc,
1093 struct ceph_mds_session *session)
1094{
1095 struct ceph_msg *msg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001096
1097 dout("request_close_session mds%d state %s seq %lld\n",
1098 session->s_mds, session_state_name(session->s_state),
1099 session->s_seq);
1100 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001101 if (!msg)
1102 return -ENOMEM;
1103 ceph_con_send(&session->s_con, msg);
1104 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001105}
1106
1107/*
1108 * Called with s_mutex held.
1109 */
1110static int __close_session(struct ceph_mds_client *mdsc,
1111 struct ceph_mds_session *session)
1112{
1113 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1114 return 0;
1115 session->s_state = CEPH_MDS_SESSION_CLOSING;
1116 return request_close_session(mdsc, session);
1117}
1118
1119/*
1120 * Trim old(er) caps.
1121 *
1122 * Because we can't cache an inode without one or more caps, we do
1123 * this indirectly: if a cap is unused, we prune its aliases, at which
1124 * point the inode will hopefully get dropped to.
1125 *
1126 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1127 * memory pressure from the MDS, though, so it needn't be perfect.
1128 */
1129static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1130{
1131 struct ceph_mds_session *session = arg;
1132 struct ceph_inode_info *ci = ceph_inode(inode);
1133 int used, oissued, mine;
1134
1135 if (session->s_trim_caps <= 0)
1136 return -1;
1137
1138 spin_lock(&inode->i_lock);
1139 mine = cap->issued | cap->implemented;
1140 used = __ceph_caps_used(ci);
1141 oissued = __ceph_caps_issued_other(ci, cap);
1142
1143 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n",
1144 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
1145 ceph_cap_string(used));
1146 if (ci->i_dirty_caps)
1147 goto out; /* dirty caps */
1148 if ((used & ~oissued) & mine)
1149 goto out; /* we need these caps */
1150
1151 session->s_trim_caps--;
1152 if (oissued) {
1153 /* we aren't the only cap.. just remove us */
Sage Weil7c1332b2010-02-16 11:39:45 -08001154 __ceph_remove_cap(cap);
Sage Weil2f2dc052009-10-06 11:31:09 -07001155 } else {
1156 /* try to drop referring dentries */
1157 spin_unlock(&inode->i_lock);
1158 d_prune_aliases(inode);
1159 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1160 inode, cap, atomic_read(&inode->i_count));
1161 return 0;
1162 }
1163
1164out:
1165 spin_unlock(&inode->i_lock);
1166 return 0;
1167}
1168
1169/*
1170 * Trim session cap count down to some max number.
1171 */
1172static int trim_caps(struct ceph_mds_client *mdsc,
1173 struct ceph_mds_session *session,
1174 int max_caps)
1175{
1176 int trim_caps = session->s_nr_caps - max_caps;
1177
1178 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1179 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1180 if (trim_caps > 0) {
1181 session->s_trim_caps = trim_caps;
1182 iterate_session_caps(session, trim_caps_cb, session);
1183 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1184 session->s_mds, session->s_nr_caps, max_caps,
1185 trim_caps - session->s_trim_caps);
Sage Weil5dacf092009-12-21 20:40:34 -08001186 session->s_trim_caps = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001187 }
1188 return 0;
1189}
1190
1191/*
1192 * Allocate cap_release messages. If there is a partially full message
1193 * in the queue, try to allocate enough to cover it's remainder, so that
1194 * we can send it immediately.
1195 *
1196 * Called under s_mutex.
1197 */
Sage Weil2b2300d2010-06-09 16:52:04 -07001198int ceph_add_cap_releases(struct ceph_mds_client *mdsc,
Sage Weilee6b2722010-06-10 12:55:52 -07001199 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001200{
Sage Weil38e8883e2010-06-10 12:57:11 -07001201 struct ceph_msg *msg, *partial = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001202 struct ceph_mds_cap_release *head;
1203 int err = -ENOMEM;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001204 int extra = mdsc->fsc->mount_options->cap_release_safety;
Sage Weil38e8883e2010-06-10 12:57:11 -07001205 int num;
Sage Weil2f2dc052009-10-06 11:31:09 -07001206
Sage Weil38e8883e2010-06-10 12:57:11 -07001207 dout("add_cap_releases %p mds%d extra %d\n", session, session->s_mds,
1208 extra);
Sage Weil2f2dc052009-10-06 11:31:09 -07001209
1210 spin_lock(&session->s_cap_lock);
1211
1212 if (!list_empty(&session->s_cap_releases)) {
1213 msg = list_first_entry(&session->s_cap_releases,
1214 struct ceph_msg,
1215 list_head);
1216 head = msg->front.iov_base;
Sage Weil38e8883e2010-06-10 12:57:11 -07001217 num = le32_to_cpu(head->num);
1218 if (num) {
1219 dout(" partial %p with (%d/%d)\n", msg, num,
1220 (int)CEPH_CAPS_PER_RELEASE);
1221 extra += CEPH_CAPS_PER_RELEASE - num;
1222 partial = msg;
1223 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001224 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001225 while (session->s_num_cap_releases < session->s_nr_caps + extra) {
1226 spin_unlock(&session->s_cap_lock);
Yehuda Sadeh34d23762010-04-06 14:33:58 -07001227 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE,
1228 GFP_NOFS);
Sage Weil2f2dc052009-10-06 11:31:09 -07001229 if (!msg)
1230 goto out_unlocked;
1231 dout("add_cap_releases %p msg %p now %d\n", session, msg,
1232 (int)msg->front.iov_len);
1233 head = msg->front.iov_base;
1234 head->num = cpu_to_le32(0);
1235 msg->front.iov_len = sizeof(*head);
1236 spin_lock(&session->s_cap_lock);
1237 list_add(&msg->list_head, &session->s_cap_releases);
1238 session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
1239 }
1240
Sage Weil38e8883e2010-06-10 12:57:11 -07001241 if (partial) {
1242 head = partial->front.iov_base;
1243 num = le32_to_cpu(head->num);
1244 dout(" queueing partial %p with %d/%d\n", partial, num,
1245 (int)CEPH_CAPS_PER_RELEASE);
1246 list_move_tail(&partial->list_head,
1247 &session->s_cap_releases_done);
1248 session->s_num_cap_releases -= CEPH_CAPS_PER_RELEASE - num;
Sage Weil2f2dc052009-10-06 11:31:09 -07001249 }
1250 err = 0;
1251 spin_unlock(&session->s_cap_lock);
1252out_unlocked:
1253 return err;
1254}
1255
1256/*
1257 * flush all dirty inode data to disk.
1258 *
1259 * returns true if we've flushed through want_flush_seq
1260 */
1261static int check_cap_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
1262{
1263 int mds, ret = 1;
1264
1265 dout("check_cap_flush want %lld\n", want_flush_seq);
1266 mutex_lock(&mdsc->mutex);
1267 for (mds = 0; ret && mds < mdsc->max_sessions; mds++) {
1268 struct ceph_mds_session *session = mdsc->sessions[mds];
1269
1270 if (!session)
1271 continue;
1272 get_session(session);
1273 mutex_unlock(&mdsc->mutex);
1274
1275 mutex_lock(&session->s_mutex);
1276 if (!list_empty(&session->s_cap_flushing)) {
1277 struct ceph_inode_info *ci =
1278 list_entry(session->s_cap_flushing.next,
1279 struct ceph_inode_info,
1280 i_flushing_item);
1281 struct inode *inode = &ci->vfs_inode;
1282
1283 spin_lock(&inode->i_lock);
1284 if (ci->i_cap_flush_seq <= want_flush_seq) {
1285 dout("check_cap_flush still flushing %p "
1286 "seq %lld <= %lld to mds%d\n", inode,
1287 ci->i_cap_flush_seq, want_flush_seq,
1288 session->s_mds);
1289 ret = 0;
1290 }
1291 spin_unlock(&inode->i_lock);
1292 }
1293 mutex_unlock(&session->s_mutex);
1294 ceph_put_mds_session(session);
1295
1296 if (!ret)
1297 return ret;
1298 mutex_lock(&mdsc->mutex);
1299 }
1300
1301 mutex_unlock(&mdsc->mutex);
1302 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
1303 return ret;
1304}
1305
1306/*
1307 * called under s_mutex
1308 */
Sage Weil3d7ded42010-06-09 16:47:10 -07001309void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1310 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001311{
1312 struct ceph_msg *msg;
1313
1314 dout("send_cap_releases mds%d\n", session->s_mds);
Sage Weil0f8605f2010-05-05 15:51:35 -07001315 spin_lock(&session->s_cap_lock);
1316 while (!list_empty(&session->s_cap_releases_done)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001317 msg = list_first_entry(&session->s_cap_releases_done,
1318 struct ceph_msg, list_head);
1319 list_del_init(&msg->list_head);
1320 spin_unlock(&session->s_cap_lock);
1321 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1322 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1323 ceph_con_send(&session->s_con, msg);
Sage Weil0f8605f2010-05-05 15:51:35 -07001324 spin_lock(&session->s_cap_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001325 }
1326 spin_unlock(&session->s_cap_lock);
1327}
1328
Sage Weile01a5942010-05-10 15:36:44 -07001329static void discard_cap_releases(struct ceph_mds_client *mdsc,
1330 struct ceph_mds_session *session)
1331{
1332 struct ceph_msg *msg;
1333 struct ceph_mds_cap_release *head;
1334 unsigned num;
1335
1336 dout("discard_cap_releases mds%d\n", session->s_mds);
1337 spin_lock(&session->s_cap_lock);
1338
1339 /* zero out the in-progress message */
1340 msg = list_first_entry(&session->s_cap_releases,
1341 struct ceph_msg, list_head);
1342 head = msg->front.iov_base;
1343 num = le32_to_cpu(head->num);
1344 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg, num);
1345 head->num = cpu_to_le32(0);
1346 session->s_num_cap_releases += num;
1347
1348 /* requeue completed messages */
1349 while (!list_empty(&session->s_cap_releases_done)) {
1350 msg = list_first_entry(&session->s_cap_releases_done,
1351 struct ceph_msg, list_head);
1352 list_del_init(&msg->list_head);
1353
1354 head = msg->front.iov_base;
1355 num = le32_to_cpu(head->num);
1356 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg,
1357 num);
1358 session->s_num_cap_releases += num;
1359 head->num = cpu_to_le32(0);
1360 msg->front.iov_len = sizeof(*head);
1361 list_add(&msg->list_head, &session->s_cap_releases);
1362 }
1363
1364 spin_unlock(&session->s_cap_lock);
1365}
1366
Sage Weil2f2dc052009-10-06 11:31:09 -07001367/*
1368 * requests
1369 */
1370
1371/*
1372 * Create an mds request.
1373 */
1374struct ceph_mds_request *
1375ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1376{
1377 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1378
1379 if (!req)
1380 return ERR_PTR(-ENOMEM);
1381
Sage Weilb4556392010-05-13 12:01:13 -07001382 mutex_init(&req->r_fill_mutex);
Yehuda Sadeh37151662010-06-17 16:16:12 -07001383 req->r_mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07001384 req->r_started = jiffies;
1385 req->r_resend_mds = -1;
1386 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1387 req->r_fmode = -1;
Sage Weil153c8e62009-12-07 12:31:09 -08001388 kref_init(&req->r_kref);
Sage Weil2f2dc052009-10-06 11:31:09 -07001389 INIT_LIST_HEAD(&req->r_wait);
1390 init_completion(&req->r_completion);
1391 init_completion(&req->r_safe_completion);
1392 INIT_LIST_HEAD(&req->r_unsafe_item);
1393
1394 req->r_op = op;
1395 req->r_direct_mode = mode;
1396 return req;
1397}
1398
1399/*
Sage Weil44ca18f2010-02-15 12:08:46 -08001400 * return oldest (lowest) request, tid in request tree, 0 if none.
Sage Weil2f2dc052009-10-06 11:31:09 -07001401 *
1402 * called under mdsc->mutex.
1403 */
Sage Weil44ca18f2010-02-15 12:08:46 -08001404static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1405{
1406 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1407 return NULL;
1408 return rb_entry(rb_first(&mdsc->request_tree),
1409 struct ceph_mds_request, r_node);
1410}
1411
Sage Weil2f2dc052009-10-06 11:31:09 -07001412static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1413{
Sage Weil44ca18f2010-02-15 12:08:46 -08001414 struct ceph_mds_request *req = __get_oldest_req(mdsc);
1415
1416 if (req)
1417 return req->r_tid;
1418 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001419}
1420
1421/*
1422 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1423 * on build_path_from_dentry in fs/cifs/dir.c.
1424 *
1425 * If @stop_on_nosnap, generate path relative to the first non-snapped
1426 * inode.
1427 *
1428 * Encode hidden .snap dirs as a double /, i.e.
1429 * foo/.snap/bar -> foo//bar
1430 */
1431char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1432 int stop_on_nosnap)
1433{
1434 struct dentry *temp;
1435 char *path;
1436 int len, pos;
1437
1438 if (dentry == NULL)
1439 return ERR_PTR(-EINVAL);
1440
1441retry:
1442 len = 0;
1443 for (temp = dentry; !IS_ROOT(temp);) {
1444 struct inode *inode = temp->d_inode;
1445 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1446 len++; /* slash only */
1447 else if (stop_on_nosnap && inode &&
1448 ceph_snap(inode) == CEPH_NOSNAP)
1449 break;
1450 else
1451 len += 1 + temp->d_name.len;
1452 temp = temp->d_parent;
1453 if (temp == NULL) {
Sage Weil6c99f252010-05-10 16:12:25 -07001454 pr_err("build_path corrupt dentry %p\n", dentry);
Sage Weil2f2dc052009-10-06 11:31:09 -07001455 return ERR_PTR(-EINVAL);
1456 }
1457 }
1458 if (len)
1459 len--; /* no leading '/' */
1460
1461 path = kmalloc(len+1, GFP_NOFS);
1462 if (path == NULL)
1463 return ERR_PTR(-ENOMEM);
1464 pos = len;
1465 path[pos] = 0; /* trailing null */
1466 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
1467 struct inode *inode = temp->d_inode;
1468
1469 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
Sage Weil104648a2010-03-18 10:14:30 -07001470 dout("build_path path+%d: %p SNAPDIR\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001471 pos, temp);
1472 } else if (stop_on_nosnap && inode &&
1473 ceph_snap(inode) == CEPH_NOSNAP) {
1474 break;
1475 } else {
1476 pos -= temp->d_name.len;
1477 if (pos < 0)
1478 break;
1479 strncpy(path + pos, temp->d_name.name,
1480 temp->d_name.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07001481 }
1482 if (pos)
1483 path[--pos] = '/';
1484 temp = temp->d_parent;
1485 if (temp == NULL) {
Sage Weil104648a2010-03-18 10:14:30 -07001486 pr_err("build_path corrupt dentry\n");
Sage Weil2f2dc052009-10-06 11:31:09 -07001487 kfree(path);
1488 return ERR_PTR(-EINVAL);
1489 }
1490 }
1491 if (pos != 0) {
Sage Weil104648a2010-03-18 10:14:30 -07001492 pr_err("build_path did not end path lookup where "
Sage Weil2f2dc052009-10-06 11:31:09 -07001493 "expected, namelen is %d, pos is %d\n", len, pos);
1494 /* presumably this is only possible if racing with a
1495 rename of one of the parent directories (we can not
1496 lock the dentries above us to prevent this, but
1497 retrying should be harmless) */
1498 kfree(path);
1499 goto retry;
1500 }
1501
1502 *base = ceph_ino(temp->d_inode);
1503 *plen = len;
Sage Weil104648a2010-03-18 10:14:30 -07001504 dout("build_path on %p %d built %llx '%.*s'\n",
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001505 dentry, dentry->d_count, *base, len, path);
Sage Weil2f2dc052009-10-06 11:31:09 -07001506 return path;
1507}
1508
1509static int build_dentry_path(struct dentry *dentry,
1510 const char **ppath, int *ppathlen, u64 *pino,
1511 int *pfreepath)
1512{
1513 char *path;
1514
1515 if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) {
1516 *pino = ceph_ino(dentry->d_parent->d_inode);
1517 *ppath = dentry->d_name.name;
1518 *ppathlen = dentry->d_name.len;
1519 return 0;
1520 }
1521 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1522 if (IS_ERR(path))
1523 return PTR_ERR(path);
1524 *ppath = path;
1525 *pfreepath = 1;
1526 return 0;
1527}
1528
1529static int build_inode_path(struct inode *inode,
1530 const char **ppath, int *ppathlen, u64 *pino,
1531 int *pfreepath)
1532{
1533 struct dentry *dentry;
1534 char *path;
1535
1536 if (ceph_snap(inode) == CEPH_NOSNAP) {
1537 *pino = ceph_ino(inode);
1538 *ppathlen = 0;
1539 return 0;
1540 }
1541 dentry = d_find_alias(inode);
1542 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1543 dput(dentry);
1544 if (IS_ERR(path))
1545 return PTR_ERR(path);
1546 *ppath = path;
1547 *pfreepath = 1;
1548 return 0;
1549}
1550
1551/*
1552 * request arguments may be specified via an inode *, a dentry *, or
1553 * an explicit ino+path.
1554 */
1555static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1556 const char *rpath, u64 rino,
1557 const char **ppath, int *pathlen,
1558 u64 *ino, int *freepath)
1559{
1560 int r = 0;
1561
1562 if (rinode) {
1563 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1564 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1565 ceph_snap(rinode));
1566 } else if (rdentry) {
1567 r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
1568 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1569 *ppath);
1570 } else if (rpath) {
1571 *ino = rino;
1572 *ppath = rpath;
1573 *pathlen = strlen(rpath);
1574 dout(" path %.*s\n", *pathlen, rpath);
1575 }
1576
1577 return r;
1578}
1579
1580/*
1581 * called under mdsc->mutex
1582 */
1583static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1584 struct ceph_mds_request *req,
1585 int mds)
1586{
1587 struct ceph_msg *msg;
1588 struct ceph_mds_request_head *head;
1589 const char *path1 = NULL;
1590 const char *path2 = NULL;
1591 u64 ino1 = 0, ino2 = 0;
1592 int pathlen1 = 0, pathlen2 = 0;
1593 int freepath1 = 0, freepath2 = 0;
1594 int len;
1595 u16 releases;
1596 void *p, *end;
1597 int ret;
1598
1599 ret = set_request_path_attr(req->r_inode, req->r_dentry,
1600 req->r_path1, req->r_ino1.ino,
1601 &path1, &pathlen1, &ino1, &freepath1);
1602 if (ret < 0) {
1603 msg = ERR_PTR(ret);
1604 goto out;
1605 }
1606
1607 ret = set_request_path_attr(NULL, req->r_old_dentry,
1608 req->r_path2, req->r_ino2.ino,
1609 &path2, &pathlen2, &ino2, &freepath2);
1610 if (ret < 0) {
1611 msg = ERR_PTR(ret);
1612 goto out_free1;
1613 }
1614
1615 len = sizeof(*head) +
Sage Weilac8839d2010-01-27 14:28:10 -08001616 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64));
Sage Weil2f2dc052009-10-06 11:31:09 -07001617
1618 /* calculate (max) length for cap releases */
1619 len += sizeof(struct ceph_mds_request_release) *
1620 (!!req->r_inode_drop + !!req->r_dentry_drop +
1621 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1622 if (req->r_dentry_drop)
1623 len += req->r_dentry->d_name.len;
1624 if (req->r_old_dentry_drop)
1625 len += req->r_old_dentry->d_name.len;
1626
Yehuda Sadeh34d23762010-04-06 14:33:58 -07001627 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -07001628 if (!msg) {
1629 msg = ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -07001630 goto out_free2;
Sage Weila79832f2010-04-01 16:06:19 -07001631 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001632
Sage Weil6df058c2009-12-22 11:24:33 -08001633 msg->hdr.tid = cpu_to_le64(req->r_tid);
1634
Sage Weil2f2dc052009-10-06 11:31:09 -07001635 head = msg->front.iov_base;
1636 p = msg->front.iov_base + sizeof(*head);
1637 end = msg->front.iov_base + msg->front.iov_len;
1638
1639 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1640 head->op = cpu_to_le32(req->r_op);
Sage Weilcb4276c2010-11-08 07:28:52 -08001641 head->caller_uid = cpu_to_le32(req->r_uid);
1642 head->caller_gid = cpu_to_le32(req->r_gid);
Sage Weil2f2dc052009-10-06 11:31:09 -07001643 head->args = req->r_args;
1644
1645 ceph_encode_filepath(&p, end, ino1, path1);
1646 ceph_encode_filepath(&p, end, ino2, path2);
1647
Sage Weile979cf52010-07-15 14:58:39 -07001648 /* make note of release offset, in case we need to replay */
1649 req->r_request_release_offset = p - msg->front.iov_base;
1650
Sage Weil2f2dc052009-10-06 11:31:09 -07001651 /* cap releases */
1652 releases = 0;
1653 if (req->r_inode_drop)
1654 releases += ceph_encode_inode_release(&p,
1655 req->r_inode ? req->r_inode : req->r_dentry->d_inode,
1656 mds, req->r_inode_drop, req->r_inode_unless, 0);
1657 if (req->r_dentry_drop)
1658 releases += ceph_encode_dentry_release(&p, req->r_dentry,
1659 mds, req->r_dentry_drop, req->r_dentry_unless);
1660 if (req->r_old_dentry_drop)
1661 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
1662 mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
1663 if (req->r_old_inode_drop)
1664 releases += ceph_encode_inode_release(&p,
1665 req->r_old_dentry->d_inode,
1666 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
1667 head->num_releases = cpu_to_le16(releases);
1668
1669 BUG_ON(p > end);
1670 msg->front.iov_len = p - msg->front.iov_base;
1671 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1672
1673 msg->pages = req->r_pages;
1674 msg->nr_pages = req->r_num_pages;
1675 msg->hdr.data_len = cpu_to_le32(req->r_data_len);
1676 msg->hdr.data_off = cpu_to_le16(0);
1677
1678out_free2:
1679 if (freepath2)
1680 kfree((char *)path2);
1681out_free1:
1682 if (freepath1)
1683 kfree((char *)path1);
1684out:
1685 return msg;
1686}
1687
1688/*
1689 * called under mdsc->mutex if error, under no mutex if
1690 * success.
1691 */
1692static void complete_request(struct ceph_mds_client *mdsc,
1693 struct ceph_mds_request *req)
1694{
1695 if (req->r_callback)
1696 req->r_callback(mdsc, req);
1697 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001698 complete_all(&req->r_completion);
Sage Weil2f2dc052009-10-06 11:31:09 -07001699}
1700
1701/*
1702 * called under mdsc->mutex
1703 */
1704static int __prepare_send_request(struct ceph_mds_client *mdsc,
1705 struct ceph_mds_request *req,
1706 int mds)
1707{
1708 struct ceph_mds_request_head *rhead;
1709 struct ceph_msg *msg;
1710 int flags = 0;
1711
Sage Weil2f2dc052009-10-06 11:31:09 -07001712 req->r_attempts++;
Greg Farnume55b71f2010-06-22 15:58:01 -07001713 if (req->r_inode) {
1714 struct ceph_cap *cap =
1715 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
1716
1717 if (cap)
1718 req->r_sent_on_mseq = cap->mseq;
1719 else
1720 req->r_sent_on_mseq = -1;
1721 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001722 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
1723 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
1724
Sage Weil01a92f12010-07-15 13:24:32 -07001725 if (req->r_got_unsafe) {
1726 /*
1727 * Replay. Do not regenerate message (and rebuild
1728 * paths, etc.); just use the original message.
1729 * Rebuilding paths will break for renames because
1730 * d_move mangles the src name.
1731 */
1732 msg = req->r_request;
1733 rhead = msg->front.iov_base;
1734
1735 flags = le32_to_cpu(rhead->flags);
1736 flags |= CEPH_MDS_FLAG_REPLAY;
1737 rhead->flags = cpu_to_le32(flags);
1738
1739 if (req->r_target_inode)
1740 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
1741
1742 rhead->num_retry = req->r_attempts - 1;
Sage Weile979cf52010-07-15 14:58:39 -07001743
1744 /* remove cap/dentry releases from message */
1745 rhead->num_releases = 0;
1746 msg->hdr.front_len = cpu_to_le32(req->r_request_release_offset);
1747 msg->front.iov_len = req->r_request_release_offset;
Sage Weil01a92f12010-07-15 13:24:32 -07001748 return 0;
1749 }
1750
Sage Weil2f2dc052009-10-06 11:31:09 -07001751 if (req->r_request) {
1752 ceph_msg_put(req->r_request);
1753 req->r_request = NULL;
1754 }
1755 msg = create_request_message(mdsc, req, mds);
1756 if (IS_ERR(msg)) {
Sage Weile1518c72010-05-13 11:19:06 -07001757 req->r_err = PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001758 complete_request(mdsc, req);
Sage Weila79832f2010-04-01 16:06:19 -07001759 return PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001760 }
1761 req->r_request = msg;
1762
1763 rhead = msg->front.iov_base;
Sage Weil2f2dc052009-10-06 11:31:09 -07001764 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
1765 if (req->r_got_unsafe)
1766 flags |= CEPH_MDS_FLAG_REPLAY;
1767 if (req->r_locked_dir)
1768 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
1769 rhead->flags = cpu_to_le32(flags);
1770 rhead->num_fwd = req->r_num_fwd;
1771 rhead->num_retry = req->r_attempts - 1;
Sage Weil01a92f12010-07-15 13:24:32 -07001772 rhead->ino = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001773
1774 dout(" r_locked_dir = %p\n", req->r_locked_dir);
Sage Weil2f2dc052009-10-06 11:31:09 -07001775 return 0;
1776}
1777
1778/*
1779 * send request, or put it on the appropriate wait list.
1780 */
1781static int __do_request(struct ceph_mds_client *mdsc,
1782 struct ceph_mds_request *req)
1783{
1784 struct ceph_mds_session *session = NULL;
1785 int mds = -1;
1786 int err = -EAGAIN;
1787
Sage Weile1518c72010-05-13 11:19:06 -07001788 if (req->r_err || req->r_got_result)
Sage Weil2f2dc052009-10-06 11:31:09 -07001789 goto out;
1790
1791 if (req->r_timeout &&
1792 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
1793 dout("do_request timed out\n");
1794 err = -EIO;
1795 goto finish;
1796 }
1797
Sage Weildc69e2e2010-11-02 13:49:00 -07001798 put_request_session(req);
1799
Sage Weil2f2dc052009-10-06 11:31:09 -07001800 mds = __choose_mds(mdsc, req);
1801 if (mds < 0 ||
1802 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
1803 dout("do_request no mds or not active, waiting for map\n");
1804 list_add(&req->r_wait, &mdsc->waiting_for_map);
1805 goto out;
1806 }
1807
1808 /* get, open session */
1809 session = __ceph_lookup_mds_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07001810 if (!session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001811 session = register_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07001812 if (IS_ERR(session)) {
1813 err = PTR_ERR(session);
1814 goto finish;
1815 }
1816 }
Sage Weildc69e2e2010-11-02 13:49:00 -07001817 req->r_session = get_session(session);
1818
Sage Weil2f2dc052009-10-06 11:31:09 -07001819 dout("do_request mds%d session %p state %s\n", mds, session,
1820 session_state_name(session->s_state));
1821 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
1822 session->s_state != CEPH_MDS_SESSION_HUNG) {
1823 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1824 session->s_state == CEPH_MDS_SESSION_CLOSING)
1825 __open_session(mdsc, session);
1826 list_add(&req->r_wait, &session->s_waiting);
1827 goto out_session;
1828 }
1829
1830 /* send request */
Sage Weil2f2dc052009-10-06 11:31:09 -07001831 req->r_resend_mds = -1; /* forget any previous mds hint */
1832
1833 if (req->r_request_started == 0) /* note request start time */
1834 req->r_request_started = jiffies;
1835
1836 err = __prepare_send_request(mdsc, req, mds);
1837 if (!err) {
1838 ceph_msg_get(req->r_request);
1839 ceph_con_send(&session->s_con, req->r_request);
1840 }
1841
1842out_session:
1843 ceph_put_mds_session(session);
1844out:
1845 return err;
1846
1847finish:
Sage Weile1518c72010-05-13 11:19:06 -07001848 req->r_err = err;
Sage Weil2f2dc052009-10-06 11:31:09 -07001849 complete_request(mdsc, req);
1850 goto out;
1851}
1852
1853/*
1854 * called under mdsc->mutex
1855 */
1856static void __wake_requests(struct ceph_mds_client *mdsc,
1857 struct list_head *head)
1858{
1859 struct ceph_mds_request *req, *nreq;
1860
1861 list_for_each_entry_safe(req, nreq, head, r_wait) {
1862 list_del_init(&req->r_wait);
1863 __do_request(mdsc, req);
1864 }
1865}
1866
1867/*
1868 * Wake up threads with requests pending for @mds, so that they can
Sage Weil29790f22010-03-18 14:45:05 -07001869 * resubmit their requests to a possibly different mds.
Sage Weil2f2dc052009-10-06 11:31:09 -07001870 */
Sage Weil29790f22010-03-18 14:45:05 -07001871static void kick_requests(struct ceph_mds_client *mdsc, int mds)
Sage Weil2f2dc052009-10-06 11:31:09 -07001872{
Sage Weil44ca18f2010-02-15 12:08:46 -08001873 struct ceph_mds_request *req;
1874 struct rb_node *p;
Sage Weil2f2dc052009-10-06 11:31:09 -07001875
1876 dout("kick_requests mds%d\n", mds);
Sage Weil44ca18f2010-02-15 12:08:46 -08001877 for (p = rb_first(&mdsc->request_tree); p; p = rb_next(p)) {
1878 req = rb_entry(p, struct ceph_mds_request, r_node);
1879 if (req->r_got_unsafe)
1880 continue;
1881 if (req->r_session &&
1882 req->r_session->s_mds == mds) {
1883 dout(" kicking tid %llu\n", req->r_tid);
Sage Weil44ca18f2010-02-15 12:08:46 -08001884 __do_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07001885 }
1886 }
1887}
1888
1889void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
1890 struct ceph_mds_request *req)
1891{
1892 dout("submit_request on %p\n", req);
1893 mutex_lock(&mdsc->mutex);
1894 __register_request(mdsc, req, NULL);
1895 __do_request(mdsc, req);
1896 mutex_unlock(&mdsc->mutex);
1897}
1898
1899/*
1900 * Synchrously perform an mds request. Take care of all of the
1901 * session setup, forwarding, retry details.
1902 */
1903int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
1904 struct inode *dir,
1905 struct ceph_mds_request *req)
1906{
1907 int err;
1908
1909 dout("do_request on %p\n", req);
1910
1911 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
1912 if (req->r_inode)
1913 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
1914 if (req->r_locked_dir)
1915 ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
1916 if (req->r_old_dentry)
1917 ceph_get_cap_refs(
1918 ceph_inode(req->r_old_dentry->d_parent->d_inode),
1919 CEPH_CAP_PIN);
1920
1921 /* issue */
1922 mutex_lock(&mdsc->mutex);
1923 __register_request(mdsc, req, dir);
1924 __do_request(mdsc, req);
1925
Sage Weile1518c72010-05-13 11:19:06 -07001926 if (req->r_err) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001927 err = req->r_err;
Sage Weile1518c72010-05-13 11:19:06 -07001928 __unregister_request(mdsc, req);
1929 dout("do_request early error %d\n", err);
1930 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07001931 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001932
Sage Weile1518c72010-05-13 11:19:06 -07001933 /* wait */
1934 mutex_unlock(&mdsc->mutex);
1935 dout("do_request waiting\n");
1936 if (req->r_timeout) {
Sage Weilaa916472010-05-24 11:15:51 -07001937 err = (long)wait_for_completion_killable_timeout(
Sage Weile1518c72010-05-13 11:19:06 -07001938 &req->r_completion, req->r_timeout);
1939 if (err == 0)
1940 err = -EIO;
1941 } else {
Sage Weilaa916472010-05-24 11:15:51 -07001942 err = wait_for_completion_killable(&req->r_completion);
Sage Weile1518c72010-05-13 11:19:06 -07001943 }
1944 dout("do_request waited, got %d\n", err);
1945 mutex_lock(&mdsc->mutex);
1946
1947 /* only abort if we didn't race with a real reply */
1948 if (req->r_got_result) {
1949 err = le32_to_cpu(req->r_reply_info.head->result);
1950 } else if (err < 0) {
1951 dout("aborted request %lld with %d\n", req->r_tid, err);
Sage Weilb4556392010-05-13 12:01:13 -07001952
1953 /*
1954 * ensure we aren't running concurrently with
1955 * ceph_fill_trace or ceph_readdir_prepopulate, which
1956 * rely on locks (dir mutex) held by our caller.
1957 */
1958 mutex_lock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07001959 req->r_err = err;
1960 req->r_aborted = true;
Sage Weilb4556392010-05-13 12:01:13 -07001961 mutex_unlock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07001962
1963 if (req->r_locked_dir &&
Sage Weil167c9e32010-05-14 10:02:57 -07001964 (req->r_op & CEPH_MDS_OP_WRITE))
1965 ceph_invalidate_dir_request(req);
Sage Weile1518c72010-05-13 11:19:06 -07001966 } else {
1967 err = req->r_err;
1968 }
1969
1970out:
1971 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07001972 dout("do_request %p done, result %d\n", req, err);
1973 return err;
1974}
1975
1976/*
Sage Weil167c9e32010-05-14 10:02:57 -07001977 * Invalidate dir I_COMPLETE, dentry lease state on an aborted MDS
1978 * namespace request.
1979 */
1980void ceph_invalidate_dir_request(struct ceph_mds_request *req)
1981{
1982 struct inode *inode = req->r_locked_dir;
1983 struct ceph_inode_info *ci = ceph_inode(inode);
1984
1985 dout("invalidate_dir_request %p (I_COMPLETE, lease(s))\n", inode);
1986 spin_lock(&inode->i_lock);
1987 ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
1988 ci->i_release_count++;
1989 spin_unlock(&inode->i_lock);
1990
1991 if (req->r_dentry)
1992 ceph_invalidate_dentry_lease(req->r_dentry);
1993 if (req->r_old_dentry)
1994 ceph_invalidate_dentry_lease(req->r_old_dentry);
1995}
1996
1997/*
Sage Weil2f2dc052009-10-06 11:31:09 -07001998 * Handle mds reply.
1999 *
2000 * We take the session mutex and parse and process the reply immediately.
2001 * This preserves the logical ordering of replies, capabilities, etc., sent
2002 * by the MDS as they are applied to our local cache.
2003 */
2004static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2005{
2006 struct ceph_mds_client *mdsc = session->s_mdsc;
2007 struct ceph_mds_request *req;
2008 struct ceph_mds_reply_head *head = msg->front.iov_base;
2009 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
2010 u64 tid;
2011 int err, result;
Sage Weil2600d2d2010-02-22 15:12:16 -08002012 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002013
Sage Weil2f2dc052009-10-06 11:31:09 -07002014 if (msg->front.iov_len < sizeof(*head)) {
2015 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002016 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002017 return;
2018 }
2019
2020 /* get request, session */
Sage Weil6df058c2009-12-22 11:24:33 -08002021 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002022 mutex_lock(&mdsc->mutex);
2023 req = __lookup_request(mdsc, tid);
2024 if (!req) {
2025 dout("handle_reply on unknown tid %llu\n", tid);
2026 mutex_unlock(&mdsc->mutex);
2027 return;
2028 }
2029 dout("handle_reply %p\n", req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002030
2031 /* correct session? */
Sage Weild96d6042010-03-20 20:50:58 -07002032 if (req->r_session != session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002033 pr_err("mdsc_handle_reply got %llu on session mds%d"
2034 " not mds%d\n", tid, session->s_mds,
2035 req->r_session ? req->r_session->s_mds : -1);
2036 mutex_unlock(&mdsc->mutex);
2037 goto out;
2038 }
2039
2040 /* dup? */
2041 if ((req->r_got_unsafe && !head->safe) ||
2042 (req->r_got_safe && head->safe)) {
2043 pr_warning("got a dup %s reply on %llu from mds%d\n",
2044 head->safe ? "safe" : "unsafe", tid, mds);
2045 mutex_unlock(&mdsc->mutex);
2046 goto out;
2047 }
Sage Weil85792d0d2010-05-13 09:06:02 -07002048 if (req->r_got_safe && !head->safe) {
2049 pr_warning("got unsafe after safe on %llu from mds%d\n",
2050 tid, mds);
2051 mutex_unlock(&mdsc->mutex);
2052 goto out;
2053 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002054
2055 result = le32_to_cpu(head->result);
2056
2057 /*
Greg Farnume55b71f2010-06-22 15:58:01 -07002058 * Handle an ESTALE
2059 * if we're not talking to the authority, send to them
2060 * if the authority has changed while we weren't looking,
2061 * send to new authority
2062 * Otherwise we just have to return an ESTALE
Sage Weil2f2dc052009-10-06 11:31:09 -07002063 */
2064 if (result == -ESTALE) {
Greg Farnume55b71f2010-06-22 15:58:01 -07002065 dout("got ESTALE on request %llu", req->r_tid);
Sage Weil213c99e2010-08-03 10:25:11 -07002066 if (!req->r_inode) {
2067 /* do nothing; not an authority problem */
2068 } else if (req->r_direct_mode != USE_AUTH_MDS) {
Greg Farnume55b71f2010-06-22 15:58:01 -07002069 dout("not using auth, setting for that now");
2070 req->r_direct_mode = USE_AUTH_MDS;
Sage Weil2f2dc052009-10-06 11:31:09 -07002071 __do_request(mdsc, req);
2072 mutex_unlock(&mdsc->mutex);
2073 goto out;
Greg Farnume55b71f2010-06-22 15:58:01 -07002074 } else {
2075 struct ceph_inode_info *ci = ceph_inode(req->r_inode);
Sage Weil4af25fd2010-11-02 13:41:47 -07002076 struct ceph_cap *cap = NULL;
2077
2078 if (req->r_session)
2079 cap = ceph_get_cap_for_mds(ci,
2080 req->r_session->s_mds);
Greg Farnume55b71f2010-06-22 15:58:01 -07002081
2082 dout("already using auth");
2083 if ((!cap || cap != ci->i_auth_cap) ||
2084 (cap->mseq != req->r_sent_on_mseq)) {
2085 dout("but cap changed, so resending");
2086 __do_request(mdsc, req);
2087 mutex_unlock(&mdsc->mutex);
2088 goto out;
2089 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002090 }
Greg Farnume55b71f2010-06-22 15:58:01 -07002091 dout("have to return ESTALE on request %llu", req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002092 }
2093
Greg Farnume55b71f2010-06-22 15:58:01 -07002094
Sage Weil2f2dc052009-10-06 11:31:09 -07002095 if (head->safe) {
2096 req->r_got_safe = true;
2097 __unregister_request(mdsc, req);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002098 complete_all(&req->r_safe_completion);
Sage Weil2f2dc052009-10-06 11:31:09 -07002099
2100 if (req->r_got_unsafe) {
2101 /*
2102 * We already handled the unsafe response, now do the
2103 * cleanup. No need to examine the response; the MDS
2104 * doesn't include any result info in the safe
2105 * response. And even if it did, there is nothing
2106 * useful we could do with a revised return value.
2107 */
2108 dout("got safe reply %llu, mds%d\n", tid, mds);
2109 list_del_init(&req->r_unsafe_item);
2110
2111 /* last unsafe request during umount? */
Sage Weil44ca18f2010-02-15 12:08:46 -08002112 if (mdsc->stopping && !__get_oldest_req(mdsc))
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002113 complete_all(&mdsc->safe_umount_waiters);
Sage Weil2f2dc052009-10-06 11:31:09 -07002114 mutex_unlock(&mdsc->mutex);
2115 goto out;
2116 }
Sage Weile1518c72010-05-13 11:19:06 -07002117 } else {
Sage Weil2f2dc052009-10-06 11:31:09 -07002118 req->r_got_unsafe = true;
2119 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
2120 }
2121
2122 dout("handle_reply tid %lld result %d\n", tid, result);
2123 rinfo = &req->r_reply_info;
Sage Weil14303d22010-12-14 17:37:52 -08002124 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
Sage Weil2f2dc052009-10-06 11:31:09 -07002125 mutex_unlock(&mdsc->mutex);
2126
2127 mutex_lock(&session->s_mutex);
2128 if (err < 0) {
Herb Shiu25933ab2010-12-01 14:14:38 -08002129 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002130 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002131 goto out_err;
2132 }
2133
2134 /* snap trace */
2135 if (rinfo->snapblob_len) {
2136 down_write(&mdsc->snap_rwsem);
2137 ceph_update_snap_trace(mdsc, rinfo->snapblob,
2138 rinfo->snapblob + rinfo->snapblob_len,
2139 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP);
2140 downgrade_write(&mdsc->snap_rwsem);
2141 } else {
2142 down_read(&mdsc->snap_rwsem);
2143 }
2144
2145 /* insert trace into our cache */
Sage Weilb4556392010-05-13 12:01:13 -07002146 mutex_lock(&req->r_fill_mutex);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002147 err = ceph_fill_trace(mdsc->fsc->sb, req, req->r_session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002148 if (err == 0) {
Herb Shiu25933ab2010-12-01 14:14:38 -08002149 if (result == 0 && req->r_op != CEPH_MDS_OP_GETFILELOCK &&
2150 rinfo->dir_nr)
Sage Weil2f2dc052009-10-06 11:31:09 -07002151 ceph_readdir_prepopulate(req, req->r_session);
Yehuda Sadeh37151662010-06-17 16:16:12 -07002152 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
Sage Weil2f2dc052009-10-06 11:31:09 -07002153 }
Sage Weilb4556392010-05-13 12:01:13 -07002154 mutex_unlock(&req->r_fill_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002155
2156 up_read(&mdsc->snap_rwsem);
2157out_err:
Sage Weile1518c72010-05-13 11:19:06 -07002158 mutex_lock(&mdsc->mutex);
2159 if (!req->r_aborted) {
2160 if (err) {
2161 req->r_err = err;
2162 } else {
2163 req->r_reply = msg;
2164 ceph_msg_get(msg);
2165 req->r_got_result = true;
2166 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002167 } else {
Sage Weile1518c72010-05-13 11:19:06 -07002168 dout("reply arrived after request %lld was aborted\n", tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002169 }
Sage Weile1518c72010-05-13 11:19:06 -07002170 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002171
Sage Weilee6b2722010-06-10 12:55:52 -07002172 ceph_add_cap_releases(mdsc, req->r_session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002173 mutex_unlock(&session->s_mutex);
2174
2175 /* kick calling process */
2176 complete_request(mdsc, req);
2177out:
2178 ceph_mdsc_put_request(req);
2179 return;
2180}
2181
2182
2183
2184/*
2185 * handle mds notification that our request has been forwarded.
2186 */
Sage Weil2600d2d2010-02-22 15:12:16 -08002187static void handle_forward(struct ceph_mds_client *mdsc,
2188 struct ceph_mds_session *session,
2189 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002190{
2191 struct ceph_mds_request *req;
Sage Weila1ea7872010-02-23 14:02:44 -08002192 u64 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002193 u32 next_mds;
2194 u32 fwd_seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002195 int err = -EINVAL;
2196 void *p = msg->front.iov_base;
2197 void *end = p + msg->front.iov_len;
Sage Weil2f2dc052009-10-06 11:31:09 -07002198
Sage Weila1ea7872010-02-23 14:02:44 -08002199 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002200 next_mds = ceph_decode_32(&p);
2201 fwd_seq = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07002202
2203 mutex_lock(&mdsc->mutex);
2204 req = __lookup_request(mdsc, tid);
2205 if (!req) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07002206 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002207 goto out; /* dup reply? */
2208 }
2209
Sage Weil2a8e5e32010-05-28 16:43:16 -07002210 if (req->r_aborted) {
2211 dout("forward tid %llu aborted, unregistering\n", tid);
2212 __unregister_request(mdsc, req);
2213 } else if (fwd_seq <= req->r_num_fwd) {
2214 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002215 tid, next_mds, req->r_num_fwd, fwd_seq);
2216 } else {
2217 /* resend. forward race not possible; mds would drop */
Sage Weil2a8e5e32010-05-28 16:43:16 -07002218 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2219 BUG_ON(req->r_err);
2220 BUG_ON(req->r_got_result);
Sage Weil2f2dc052009-10-06 11:31:09 -07002221 req->r_num_fwd = fwd_seq;
2222 req->r_resend_mds = next_mds;
2223 put_request_session(req);
2224 __do_request(mdsc, req);
2225 }
2226 ceph_mdsc_put_request(req);
2227out:
2228 mutex_unlock(&mdsc->mutex);
2229 return;
2230
2231bad:
2232 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2233}
2234
2235/*
2236 * handle a mds session control message
2237 */
2238static void handle_session(struct ceph_mds_session *session,
2239 struct ceph_msg *msg)
2240{
2241 struct ceph_mds_client *mdsc = session->s_mdsc;
2242 u32 op;
2243 u64 seq;
Sage Weil2600d2d2010-02-22 15:12:16 -08002244 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002245 struct ceph_mds_session_head *h = msg->front.iov_base;
2246 int wake = 0;
2247
Sage Weil2f2dc052009-10-06 11:31:09 -07002248 /* decode */
2249 if (msg->front.iov_len != sizeof(*h))
2250 goto bad;
2251 op = le32_to_cpu(h->op);
2252 seq = le64_to_cpu(h->seq);
2253
2254 mutex_lock(&mdsc->mutex);
Sage Weil2600d2d2010-02-22 15:12:16 -08002255 if (op == CEPH_SESSION_CLOSE)
2256 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002257 /* FIXME: this ttl calculation is generous */
2258 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2259 mutex_unlock(&mdsc->mutex);
2260
2261 mutex_lock(&session->s_mutex);
2262
2263 dout("handle_session mds%d %s %p state %s seq %llu\n",
2264 mds, ceph_session_op_name(op), session,
2265 session_state_name(session->s_state), seq);
2266
2267 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2268 session->s_state = CEPH_MDS_SESSION_OPEN;
2269 pr_info("mds%d came back\n", session->s_mds);
2270 }
2271
2272 switch (op) {
2273 case CEPH_SESSION_OPEN:
Sage Weil29790f22010-03-18 14:45:05 -07002274 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2275 pr_info("mds%d reconnect success\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002276 session->s_state = CEPH_MDS_SESSION_OPEN;
2277 renewed_caps(mdsc, session, 0);
2278 wake = 1;
2279 if (mdsc->stopping)
2280 __close_session(mdsc, session);
2281 break;
2282
2283 case CEPH_SESSION_RENEWCAPS:
2284 if (session->s_renew_seq == seq)
2285 renewed_caps(mdsc, session, 1);
2286 break;
2287
2288 case CEPH_SESSION_CLOSE:
Sage Weil29790f22010-03-18 14:45:05 -07002289 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2290 pr_info("mds%d reconnect denied\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002291 remove_session_caps(session);
2292 wake = 1; /* for good measure */
Sage Weilf3c60c52010-08-11 14:51:23 -07002293 wake_up_all(&mdsc->session_close_wq);
Sage Weil29790f22010-03-18 14:45:05 -07002294 kick_requests(mdsc, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002295 break;
2296
2297 case CEPH_SESSION_STALE:
2298 pr_info("mds%d caps went stale, renewing\n",
2299 session->s_mds);
2300 spin_lock(&session->s_cap_lock);
2301 session->s_cap_gen++;
2302 session->s_cap_ttl = 0;
2303 spin_unlock(&session->s_cap_lock);
2304 send_renew_caps(mdsc, session);
2305 break;
2306
2307 case CEPH_SESSION_RECALL_STATE:
2308 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2309 break;
2310
2311 default:
2312 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2313 WARN_ON(1);
2314 }
2315
2316 mutex_unlock(&session->s_mutex);
2317 if (wake) {
2318 mutex_lock(&mdsc->mutex);
2319 __wake_requests(mdsc, &session->s_waiting);
2320 mutex_unlock(&mdsc->mutex);
2321 }
2322 return;
2323
2324bad:
2325 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2326 (int)msg->front.iov_len);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002327 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002328 return;
2329}
2330
2331
2332/*
2333 * called under session->mutex.
2334 */
2335static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2336 struct ceph_mds_session *session)
2337{
2338 struct ceph_mds_request *req, *nreq;
2339 int err;
2340
2341 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2342
2343 mutex_lock(&mdsc->mutex);
2344 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
2345 err = __prepare_send_request(mdsc, req, session->s_mds);
2346 if (!err) {
2347 ceph_msg_get(req->r_request);
2348 ceph_con_send(&session->s_con, req->r_request);
2349 }
2350 }
2351 mutex_unlock(&mdsc->mutex);
2352}
2353
2354/*
2355 * Encode information about a cap for a reconnect with the MDS.
2356 */
Sage Weil2f2dc052009-10-06 11:31:09 -07002357static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2358 void *arg)
2359{
Sage Weil20cb34a2010-05-12 15:21:32 -07002360 union {
2361 struct ceph_mds_cap_reconnect v2;
2362 struct ceph_mds_cap_reconnect_v1 v1;
2363 } rec;
2364 size_t reclen;
Sage Weil2f2dc052009-10-06 11:31:09 -07002365 struct ceph_inode_info *ci;
Sage Weil20cb34a2010-05-12 15:21:32 -07002366 struct ceph_reconnect_state *recon_state = arg;
2367 struct ceph_pagelist *pagelist = recon_state->pagelist;
Sage Weil2f2dc052009-10-06 11:31:09 -07002368 char *path;
2369 int pathlen, err;
2370 u64 pathbase;
2371 struct dentry *dentry;
2372
2373 ci = cap->ci;
2374
2375 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2376 inode, ceph_vinop(inode), cap, cap->cap_id,
2377 ceph_cap_string(cap->issued));
Sage Weil93cea5b2009-12-23 12:21:51 -08002378 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2379 if (err)
2380 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002381
2382 dentry = d_find_alias(inode);
2383 if (dentry) {
2384 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2385 if (IS_ERR(path)) {
2386 err = PTR_ERR(path);
Sage Weile072f8a2010-08-26 09:26:37 -07002387 goto out_dput;
Sage Weil2f2dc052009-10-06 11:31:09 -07002388 }
2389 } else {
2390 path = NULL;
2391 pathlen = 0;
2392 }
Sage Weil93cea5b2009-12-23 12:21:51 -08002393 err = ceph_pagelist_encode_string(pagelist, path, pathlen);
2394 if (err)
Sage Weile072f8a2010-08-26 09:26:37 -07002395 goto out_free;
Sage Weil2f2dc052009-10-06 11:31:09 -07002396
Sage Weil2f2dc052009-10-06 11:31:09 -07002397 spin_lock(&inode->i_lock);
2398 cap->seq = 0; /* reset cap seq */
2399 cap->issue_seq = 0; /* and issue_seq */
Sage Weil20cb34a2010-05-12 15:21:32 -07002400
2401 if (recon_state->flock) {
2402 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
2403 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2404 rec.v2.issued = cpu_to_le32(cap->issued);
2405 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2406 rec.v2.pathbase = cpu_to_le64(pathbase);
2407 rec.v2.flock_len = 0;
2408 reclen = sizeof(rec.v2);
2409 } else {
2410 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
2411 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2412 rec.v1.issued = cpu_to_le32(cap->issued);
2413 rec.v1.size = cpu_to_le64(inode->i_size);
2414 ceph_encode_timespec(&rec.v1.mtime, &inode->i_mtime);
2415 ceph_encode_timespec(&rec.v1.atime, &inode->i_atime);
2416 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2417 rec.v1.pathbase = cpu_to_le64(pathbase);
2418 reclen = sizeof(rec.v1);
2419 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002420 spin_unlock(&inode->i_lock);
2421
Greg Farnum40819f62010-08-02 15:34:23 -07002422 if (recon_state->flock) {
2423 int num_fcntl_locks, num_flock_locks;
Greg Farnumfca44512010-09-17 10:24:02 -07002424 struct ceph_pagelist_cursor trunc_point;
Greg Farnum40819f62010-08-02 15:34:23 -07002425
Greg Farnumfca44512010-09-17 10:24:02 -07002426 ceph_pagelist_set_cursor(pagelist, &trunc_point);
2427 do {
Sage Weil496e5952010-09-22 19:57:10 -07002428 lock_flocks();
Greg Farnumfca44512010-09-17 10:24:02 -07002429 ceph_count_locks(inode, &num_fcntl_locks,
2430 &num_flock_locks);
2431 rec.v2.flock_len = (2*sizeof(u32) +
2432 (num_fcntl_locks+num_flock_locks) *
2433 sizeof(struct ceph_filelock));
Sage Weil496e5952010-09-22 19:57:10 -07002434 unlock_flocks();
Greg Farnum40819f62010-08-02 15:34:23 -07002435
Greg Farnumfca44512010-09-17 10:24:02 -07002436 /* pre-alloc pagelist */
2437 ceph_pagelist_truncate(pagelist, &trunc_point);
2438 err = ceph_pagelist_append(pagelist, &rec, reclen);
2439 if (!err)
2440 err = ceph_pagelist_reserve(pagelist,
2441 rec.v2.flock_len);
2442
2443 /* encode locks */
2444 if (!err) {
Sage Weil496e5952010-09-22 19:57:10 -07002445 lock_flocks();
Greg Farnumfca44512010-09-17 10:24:02 -07002446 err = ceph_encode_locks(inode,
2447 pagelist,
2448 num_fcntl_locks,
2449 num_flock_locks);
Sage Weil496e5952010-09-22 19:57:10 -07002450 unlock_flocks();
Greg Farnumfca44512010-09-17 10:24:02 -07002451 }
2452 } while (err == -ENOSPC);
Sage Weil3612abb2010-09-07 15:59:27 -07002453 } else {
2454 err = ceph_pagelist_append(pagelist, &rec, reclen);
Greg Farnum40819f62010-08-02 15:34:23 -07002455 }
Sage Weil93cea5b2009-12-23 12:21:51 -08002456
Sage Weile072f8a2010-08-26 09:26:37 -07002457out_free:
Sage Weil2f2dc052009-10-06 11:31:09 -07002458 kfree(path);
Sage Weile072f8a2010-08-26 09:26:37 -07002459out_dput:
Sage Weil2f2dc052009-10-06 11:31:09 -07002460 dput(dentry);
Sage Weil93cea5b2009-12-23 12:21:51 -08002461 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002462}
2463
2464
2465/*
2466 * If an MDS fails and recovers, clients need to reconnect in order to
2467 * reestablish shared state. This includes all caps issued through
2468 * this session _and_ the snap_realm hierarchy. Because it's not
2469 * clear which snap realms the mds cares about, we send everything we
2470 * know about.. that ensures we'll then get any new info the
2471 * recovering MDS might have.
2472 *
2473 * This is a relatively heavyweight operation, but it's rare.
2474 *
2475 * called with mdsc->mutex held.
2476 */
Sage Weil34b6c852010-05-10 16:31:25 -07002477static void send_mds_reconnect(struct ceph_mds_client *mdsc,
2478 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07002479{
Sage Weil2f2dc052009-10-06 11:31:09 -07002480 struct ceph_msg *reply;
Sage Weila105f002010-02-15 14:37:55 -08002481 struct rb_node *p;
Sage Weil34b6c852010-05-10 16:31:25 -07002482 int mds = session->s_mds;
Sage Weil9abf82b2010-05-10 21:58:38 -07002483 int err = -ENOMEM;
Sage Weil93cea5b2009-12-23 12:21:51 -08002484 struct ceph_pagelist *pagelist;
Sage Weil20cb34a2010-05-12 15:21:32 -07002485 struct ceph_reconnect_state recon_state;
Sage Weil2f2dc052009-10-06 11:31:09 -07002486
Sage Weil34b6c852010-05-10 16:31:25 -07002487 pr_info("mds%d reconnect start\n", mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002488
Sage Weil93cea5b2009-12-23 12:21:51 -08002489 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
2490 if (!pagelist)
2491 goto fail_nopagelist;
2492 ceph_pagelist_init(pagelist);
2493
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002494 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -07002495 if (!reply)
Sage Weil93cea5b2009-12-23 12:21:51 -08002496 goto fail_nomsg;
Sage Weil93cea5b2009-12-23 12:21:51 -08002497
Sage Weil34b6c852010-05-10 16:31:25 -07002498 mutex_lock(&session->s_mutex);
2499 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
2500 session->s_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002501
Sage Weil34b6c852010-05-10 16:31:25 -07002502 ceph_con_open(&session->s_con,
2503 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
Sage Weil2f2dc052009-10-06 11:31:09 -07002504
Sage Weil34b6c852010-05-10 16:31:25 -07002505 /* replay unsafe requests */
2506 replay_unsafe_requests(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002507
2508 down_read(&mdsc->snap_rwsem);
2509
Sage Weil2f2dc052009-10-06 11:31:09 -07002510 dout("session %p state %s\n", session,
2511 session_state_name(session->s_state));
2512
Sage Weile01a5942010-05-10 15:36:44 -07002513 /* drop old cap expires; we're about to reestablish that state */
2514 discard_cap_releases(mdsc, session);
2515
Sage Weil2f2dc052009-10-06 11:31:09 -07002516 /* traverse this session's caps */
Sage Weil93cea5b2009-12-23 12:21:51 -08002517 err = ceph_pagelist_encode_32(pagelist, session->s_nr_caps);
2518 if (err)
2519 goto fail;
Sage Weil20cb34a2010-05-12 15:21:32 -07002520
2521 recon_state.pagelist = pagelist;
2522 recon_state.flock = session->s_con.peer_features & CEPH_FEATURE_FLOCK;
2523 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
Sage Weil2f2dc052009-10-06 11:31:09 -07002524 if (err < 0)
Sage Weil9abf82b2010-05-10 21:58:38 -07002525 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07002526
2527 /*
2528 * snaprealms. we provide mds with the ino, seq (version), and
2529 * parent for all of our realms. If the mds has any newer info,
2530 * it will tell us.
2531 */
Sage Weila105f002010-02-15 14:37:55 -08002532 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
2533 struct ceph_snap_realm *realm =
2534 rb_entry(p, struct ceph_snap_realm, node);
Sage Weil93cea5b2009-12-23 12:21:51 -08002535 struct ceph_mds_snaprealm_reconnect sr_rec;
Sage Weil2f2dc052009-10-06 11:31:09 -07002536
2537 dout(" adding snap realm %llx seq %lld parent %llx\n",
2538 realm->ino, realm->seq, realm->parent_ino);
Sage Weil93cea5b2009-12-23 12:21:51 -08002539 sr_rec.ino = cpu_to_le64(realm->ino);
2540 sr_rec.seq = cpu_to_le64(realm->seq);
2541 sr_rec.parent = cpu_to_le64(realm->parent_ino);
2542 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
2543 if (err)
2544 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07002545 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002546
Sage Weil93cea5b2009-12-23 12:21:51 -08002547 reply->pagelist = pagelist;
Sage Weil20cb34a2010-05-12 15:21:32 -07002548 if (recon_state.flock)
2549 reply->hdr.version = cpu_to_le16(2);
Sage Weil93cea5b2009-12-23 12:21:51 -08002550 reply->hdr.data_len = cpu_to_le32(pagelist->length);
2551 reply->nr_pages = calc_pages_for(0, pagelist->length);
Sage Weil2f2dc052009-10-06 11:31:09 -07002552 ceph_con_send(&session->s_con, reply);
2553
Sage Weil9abf82b2010-05-10 21:58:38 -07002554 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002555
Sage Weil9abf82b2010-05-10 21:58:38 -07002556 mutex_lock(&mdsc->mutex);
2557 __wake_requests(mdsc, &session->s_waiting);
2558 mutex_unlock(&mdsc->mutex);
2559
Sage Weil2f2dc052009-10-06 11:31:09 -07002560 up_read(&mdsc->snap_rwsem);
Sage Weil2f2dc052009-10-06 11:31:09 -07002561 return;
2562
Sage Weil93cea5b2009-12-23 12:21:51 -08002563fail:
Sage Weil2f2dc052009-10-06 11:31:09 -07002564 ceph_msg_put(reply);
Sage Weil9abf82b2010-05-10 21:58:38 -07002565 up_read(&mdsc->snap_rwsem);
2566 mutex_unlock(&session->s_mutex);
Sage Weil93cea5b2009-12-23 12:21:51 -08002567fail_nomsg:
2568 ceph_pagelist_release(pagelist);
2569 kfree(pagelist);
2570fail_nopagelist:
Sage Weil9abf82b2010-05-10 21:58:38 -07002571 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
Sage Weil9abf82b2010-05-10 21:58:38 -07002572 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07002573}
2574
2575
2576/*
2577 * compare old and new mdsmaps, kicking requests
2578 * and closing out old connections as necessary
2579 *
2580 * called under mdsc->mutex.
2581 */
2582static void check_new_map(struct ceph_mds_client *mdsc,
2583 struct ceph_mdsmap *newmap,
2584 struct ceph_mdsmap *oldmap)
2585{
2586 int i;
2587 int oldstate, newstate;
2588 struct ceph_mds_session *s;
2589
2590 dout("check_new_map new %u old %u\n",
2591 newmap->m_epoch, oldmap->m_epoch);
2592
2593 for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
2594 if (mdsc->sessions[i] == NULL)
2595 continue;
2596 s = mdsc->sessions[i];
2597 oldstate = ceph_mdsmap_get_state(oldmap, i);
2598 newstate = ceph_mdsmap_get_state(newmap, i);
2599
Sage Weil0deb01c2010-06-17 14:19:01 -07002600 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002601 i, ceph_mds_state_name(oldstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07002602 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
Sage Weil2f2dc052009-10-06 11:31:09 -07002603 ceph_mds_state_name(newstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07002604 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
Sage Weil2f2dc052009-10-06 11:31:09 -07002605 session_state_name(s->s_state));
2606
2607 if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
2608 ceph_mdsmap_get_addr(newmap, i),
2609 sizeof(struct ceph_entity_addr))) {
2610 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
2611 /* the session never opened, just close it
2612 * out now */
2613 __wake_requests(mdsc, &s->s_waiting);
Sage Weil2600d2d2010-02-22 15:12:16 -08002614 __unregister_session(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07002615 } else {
2616 /* just close it */
2617 mutex_unlock(&mdsc->mutex);
2618 mutex_lock(&s->s_mutex);
2619 mutex_lock(&mdsc->mutex);
2620 ceph_con_close(&s->s_con);
2621 mutex_unlock(&s->s_mutex);
2622 s->s_state = CEPH_MDS_SESSION_RESTARTING;
2623 }
2624
2625 /* kick any requests waiting on the recovering mds */
Sage Weil29790f22010-03-18 14:45:05 -07002626 kick_requests(mdsc, i);
Sage Weil2f2dc052009-10-06 11:31:09 -07002627 } else if (oldstate == newstate) {
2628 continue; /* nothing new with this mds */
2629 }
2630
2631 /*
2632 * send reconnect?
2633 */
2634 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
Sage Weil34b6c852010-05-10 16:31:25 -07002635 newstate >= CEPH_MDS_STATE_RECONNECT) {
2636 mutex_unlock(&mdsc->mutex);
2637 send_mds_reconnect(mdsc, s);
2638 mutex_lock(&mdsc->mutex);
2639 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002640
2641 /*
Sage Weil29790f22010-03-18 14:45:05 -07002642 * kick request on any mds that has gone active.
Sage Weil2f2dc052009-10-06 11:31:09 -07002643 */
2644 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
2645 newstate >= CEPH_MDS_STATE_ACTIVE) {
Sage Weil29790f22010-03-18 14:45:05 -07002646 if (oldstate != CEPH_MDS_STATE_CREATING &&
2647 oldstate != CEPH_MDS_STATE_STARTING)
2648 pr_info("mds%d recovery completed\n", s->s_mds);
2649 kick_requests(mdsc, i);
Sage Weil2f2dc052009-10-06 11:31:09 -07002650 ceph_kick_flushing_caps(mdsc, s);
Sage Weil0dc25702009-11-20 13:43:45 -08002651 wake_up_session_caps(s, 1);
Sage Weil2f2dc052009-10-06 11:31:09 -07002652 }
2653 }
Sage Weilcb170a22010-06-21 13:38:35 -07002654
2655 for (i = 0; i < newmap->m_max_mds && i < mdsc->max_sessions; i++) {
2656 s = mdsc->sessions[i];
2657 if (!s)
2658 continue;
2659 if (!ceph_mdsmap_is_laggy(newmap, i))
2660 continue;
2661 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2662 s->s_state == CEPH_MDS_SESSION_HUNG ||
2663 s->s_state == CEPH_MDS_SESSION_CLOSING) {
2664 dout(" connecting to export targets of laggy mds%d\n",
2665 i);
2666 __open_export_target_sessions(mdsc, s);
2667 }
2668 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002669}
2670
2671
2672
2673/*
2674 * leases
2675 */
2676
2677/*
2678 * caller must hold session s_mutex, dentry->d_lock
2679 */
2680void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
2681{
2682 struct ceph_dentry_info *di = ceph_dentry(dentry);
2683
2684 ceph_put_mds_session(di->lease_session);
2685 di->lease_session = NULL;
2686}
2687
Sage Weil2600d2d2010-02-22 15:12:16 -08002688static void handle_lease(struct ceph_mds_client *mdsc,
2689 struct ceph_mds_session *session,
2690 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002691{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002692 struct super_block *sb = mdsc->fsc->sb;
Sage Weil2f2dc052009-10-06 11:31:09 -07002693 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07002694 struct ceph_inode_info *ci;
2695 struct dentry *parent, *dentry;
2696 struct ceph_dentry_info *di;
Sage Weil2600d2d2010-02-22 15:12:16 -08002697 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002698 struct ceph_mds_lease *h = msg->front.iov_base;
Sage Weil1e5ea232010-06-04 10:05:40 -07002699 u32 seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002700 struct ceph_vino vino;
2701 int mask;
2702 struct qstr dname;
2703 int release = 0;
2704
Sage Weil2f2dc052009-10-06 11:31:09 -07002705 dout("handle_lease from mds%d\n", mds);
2706
2707 /* decode */
2708 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
2709 goto bad;
2710 vino.ino = le64_to_cpu(h->ino);
2711 vino.snap = CEPH_NOSNAP;
2712 mask = le16_to_cpu(h->mask);
Sage Weil1e5ea232010-06-04 10:05:40 -07002713 seq = le32_to_cpu(h->seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002714 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
2715 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
2716 if (dname.len != get_unaligned_le32(h+1))
2717 goto bad;
2718
Sage Weil2f2dc052009-10-06 11:31:09 -07002719 mutex_lock(&session->s_mutex);
2720 session->s_seq++;
2721
2722 /* lookup inode */
2723 inode = ceph_find_inode(sb, vino);
Sage Weil1e5ea232010-06-04 10:05:40 -07002724 dout("handle_lease %s, mask %d, ino %llx %p %.*s\n",
2725 ceph_lease_op_name(h->action), mask, vino.ino, inode,
2726 dname.len, dname.name);
Sage Weil2f2dc052009-10-06 11:31:09 -07002727 if (inode == NULL) {
2728 dout("handle_lease no inode %llx\n", vino.ino);
2729 goto release;
2730 }
2731 ci = ceph_inode(inode);
2732
2733 /* dentry */
2734 parent = d_find_alias(inode);
2735 if (!parent) {
2736 dout("no parent dentry on inode %p\n", inode);
2737 WARN_ON(1);
2738 goto release; /* hrm... */
2739 }
2740 dname.hash = full_name_hash(dname.name, dname.len);
2741 dentry = d_lookup(parent, &dname);
2742 dput(parent);
2743 if (!dentry)
2744 goto release;
2745
2746 spin_lock(&dentry->d_lock);
2747 di = ceph_dentry(dentry);
2748 switch (h->action) {
2749 case CEPH_MDS_LEASE_REVOKE:
2750 if (di && di->lease_session == session) {
Sage Weil1e5ea232010-06-04 10:05:40 -07002751 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
2752 h->seq = cpu_to_le32(di->lease_seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002753 __ceph_mdsc_drop_dentry_lease(dentry);
2754 }
2755 release = 1;
2756 break;
2757
2758 case CEPH_MDS_LEASE_RENEW:
2759 if (di && di->lease_session == session &&
2760 di->lease_gen == session->s_cap_gen &&
2761 di->lease_renew_from &&
2762 di->lease_renew_after == 0) {
2763 unsigned long duration =
2764 le32_to_cpu(h->duration_ms) * HZ / 1000;
2765
Sage Weil1e5ea232010-06-04 10:05:40 -07002766 di->lease_seq = seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002767 dentry->d_time = di->lease_renew_from + duration;
2768 di->lease_renew_after = di->lease_renew_from +
2769 (duration >> 1);
2770 di->lease_renew_from = 0;
2771 }
2772 break;
2773 }
2774 spin_unlock(&dentry->d_lock);
2775 dput(dentry);
2776
2777 if (!release)
2778 goto out;
2779
2780release:
2781 /* let's just reuse the same message */
2782 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
2783 ceph_msg_get(msg);
2784 ceph_con_send(&session->s_con, msg);
2785
2786out:
2787 iput(inode);
2788 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002789 return;
2790
2791bad:
2792 pr_err("corrupt lease message\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002793 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002794}
2795
2796void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
2797 struct inode *inode,
2798 struct dentry *dentry, char action,
2799 u32 seq)
2800{
2801 struct ceph_msg *msg;
2802 struct ceph_mds_lease *lease;
2803 int len = sizeof(*lease) + sizeof(u32);
2804 int dnamelen = 0;
2805
2806 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
2807 inode, dentry, ceph_lease_op_name(action), session->s_mds);
2808 dnamelen = dentry->d_name.len;
2809 len += dnamelen;
2810
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002811 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -07002812 if (!msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002813 return;
2814 lease = msg->front.iov_base;
2815 lease->action = action;
Sage Weildd1c9052010-05-25 16:45:25 -07002816 lease->mask = cpu_to_le16(1);
Sage Weil2f2dc052009-10-06 11:31:09 -07002817 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
2818 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
2819 lease->seq = cpu_to_le32(seq);
2820 put_unaligned_le32(dnamelen, lease + 1);
2821 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
2822
2823 /*
2824 * if this is a preemptive lease RELEASE, no need to
2825 * flush request stream, since the actual request will
2826 * soon follow.
2827 */
2828 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
2829
2830 ceph_con_send(&session->s_con, msg);
2831}
2832
2833/*
2834 * Preemptively release a lease we expect to invalidate anyway.
2835 * Pass @inode always, @dentry is optional.
2836 */
2837void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
2838 struct dentry *dentry, int mask)
2839{
2840 struct ceph_dentry_info *di;
2841 struct ceph_mds_session *session;
2842 u32 seq;
2843
2844 BUG_ON(inode == NULL);
2845 BUG_ON(dentry == NULL);
Sage Weildd1c9052010-05-25 16:45:25 -07002846 BUG_ON(mask == 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07002847
2848 /* is dentry lease valid? */
2849 spin_lock(&dentry->d_lock);
2850 di = ceph_dentry(dentry);
2851 if (!di || !di->lease_session ||
2852 di->lease_session->s_mds < 0 ||
2853 di->lease_gen != di->lease_session->s_cap_gen ||
2854 !time_before(jiffies, dentry->d_time)) {
2855 dout("lease_release inode %p dentry %p -- "
2856 "no lease on %d\n",
2857 inode, dentry, mask);
2858 spin_unlock(&dentry->d_lock);
2859 return;
2860 }
2861
2862 /* we do have a lease on this dentry; note mds and seq */
2863 session = ceph_get_mds_session(di->lease_session);
2864 seq = di->lease_seq;
2865 __ceph_mdsc_drop_dentry_lease(dentry);
2866 spin_unlock(&dentry->d_lock);
2867
2868 dout("lease_release inode %p dentry %p mask %d to mds%d\n",
2869 inode, dentry, mask, session->s_mds);
2870 ceph_mdsc_lease_send_msg(session, inode, dentry,
2871 CEPH_MDS_LEASE_RELEASE, seq);
2872 ceph_put_mds_session(session);
2873}
2874
2875/*
2876 * drop all leases (and dentry refs) in preparation for umount
2877 */
2878static void drop_leases(struct ceph_mds_client *mdsc)
2879{
2880 int i;
2881
2882 dout("drop_leases\n");
2883 mutex_lock(&mdsc->mutex);
2884 for (i = 0; i < mdsc->max_sessions; i++) {
2885 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2886 if (!s)
2887 continue;
2888 mutex_unlock(&mdsc->mutex);
2889 mutex_lock(&s->s_mutex);
2890 mutex_unlock(&s->s_mutex);
2891 ceph_put_mds_session(s);
2892 mutex_lock(&mdsc->mutex);
2893 }
2894 mutex_unlock(&mdsc->mutex);
2895}
2896
2897
2898
2899/*
2900 * delayed work -- periodically trim expired leases, renew caps with mds
2901 */
2902static void schedule_delayed(struct ceph_mds_client *mdsc)
2903{
2904 int delay = 5;
2905 unsigned hz = round_jiffies_relative(HZ * delay);
2906 schedule_delayed_work(&mdsc->delayed_work, hz);
2907}
2908
2909static void delayed_work(struct work_struct *work)
2910{
2911 int i;
2912 struct ceph_mds_client *mdsc =
2913 container_of(work, struct ceph_mds_client, delayed_work.work);
2914 int renew_interval;
2915 int renew_caps;
2916
2917 dout("mdsc delayed_work\n");
Sage Weilafcdaea2009-10-14 14:27:38 -07002918 ceph_check_delayed_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07002919
2920 mutex_lock(&mdsc->mutex);
2921 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
2922 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
2923 mdsc->last_renew_caps);
2924 if (renew_caps)
2925 mdsc->last_renew_caps = jiffies;
2926
2927 for (i = 0; i < mdsc->max_sessions; i++) {
2928 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2929 if (s == NULL)
2930 continue;
2931 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
2932 dout("resending session close request for mds%d\n",
2933 s->s_mds);
2934 request_close_session(mdsc, s);
2935 ceph_put_mds_session(s);
2936 continue;
2937 }
2938 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
2939 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
2940 s->s_state = CEPH_MDS_SESSION_HUNG;
2941 pr_info("mds%d hung\n", s->s_mds);
2942 }
2943 }
2944 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
2945 /* this mds is failed or recovering, just wait */
2946 ceph_put_mds_session(s);
2947 continue;
2948 }
2949 mutex_unlock(&mdsc->mutex);
2950
2951 mutex_lock(&s->s_mutex);
2952 if (renew_caps)
2953 send_renew_caps(mdsc, s);
2954 else
2955 ceph_con_keepalive(&s->s_con);
Sage Weilee6b2722010-06-10 12:55:52 -07002956 ceph_add_cap_releases(mdsc, s);
Sage Weilaab53dd2010-03-17 16:30:21 -07002957 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2958 s->s_state == CEPH_MDS_SESSION_HUNG)
Sage Weil3d7ded42010-06-09 16:47:10 -07002959 ceph_send_cap_releases(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07002960 mutex_unlock(&s->s_mutex);
2961 ceph_put_mds_session(s);
2962
2963 mutex_lock(&mdsc->mutex);
2964 }
2965 mutex_unlock(&mdsc->mutex);
2966
2967 schedule_delayed(mdsc);
2968}
2969
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002970int ceph_mdsc_init(struct ceph_fs_client *fsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07002971
Sage Weil2f2dc052009-10-06 11:31:09 -07002972{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002973 struct ceph_mds_client *mdsc;
2974
2975 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
2976 if (!mdsc)
2977 return -ENOMEM;
2978 mdsc->fsc = fsc;
2979 fsc->mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07002980 mutex_init(&mdsc->mutex);
2981 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08002982 if (mdsc->mdsmap == NULL)
2983 return -ENOMEM;
2984
Sage Weil2f2dc052009-10-06 11:31:09 -07002985 init_completion(&mdsc->safe_umount_waiters);
Sage Weilf3c60c52010-08-11 14:51:23 -07002986 init_waitqueue_head(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002987 INIT_LIST_HEAD(&mdsc->waiting_for_map);
2988 mdsc->sessions = NULL;
2989 mdsc->max_sessions = 0;
2990 mdsc->stopping = 0;
2991 init_rwsem(&mdsc->snap_rwsem);
Sage Weila105f002010-02-15 14:37:55 -08002992 mdsc->snap_realms = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07002993 INIT_LIST_HEAD(&mdsc->snap_empty);
2994 spin_lock_init(&mdsc->snap_empty_lock);
2995 mdsc->last_tid = 0;
Sage Weil44ca18f2010-02-15 12:08:46 -08002996 mdsc->request_tree = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07002997 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
2998 mdsc->last_renew_caps = jiffies;
2999 INIT_LIST_HEAD(&mdsc->cap_delay_list);
3000 spin_lock_init(&mdsc->cap_delay_lock);
3001 INIT_LIST_HEAD(&mdsc->snap_flush_list);
3002 spin_lock_init(&mdsc->snap_flush_lock);
3003 mdsc->cap_flush_seq = 0;
3004 INIT_LIST_HEAD(&mdsc->cap_dirty);
3005 mdsc->num_cap_flushing = 0;
3006 spin_lock_init(&mdsc->cap_dirty_lock);
3007 init_waitqueue_head(&mdsc->cap_flushing_wq);
3008 spin_lock_init(&mdsc->dentry_lru_lock);
3009 INIT_LIST_HEAD(&mdsc->dentry_lru);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08003010
Yehuda Sadeh37151662010-06-17 16:16:12 -07003011 ceph_caps_init(mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003012 ceph_adjust_min_caps(mdsc, fsc->min_caps);
Yehuda Sadeh37151662010-06-17 16:16:12 -07003013
Sage Weil5f44f142009-11-18 14:52:18 -08003014 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003015}
3016
3017/*
3018 * Wait for safe replies on open mds requests. If we time out, drop
3019 * all requests from the tree to avoid dangling dentry refs.
3020 */
3021static void wait_requests(struct ceph_mds_client *mdsc)
3022{
3023 struct ceph_mds_request *req;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003024 struct ceph_fs_client *fsc = mdsc->fsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07003025
3026 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003027 if (__get_oldest_req(mdsc)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003028 mutex_unlock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003029
Sage Weil2f2dc052009-10-06 11:31:09 -07003030 dout("wait_requests waiting for requests\n");
3031 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003032 fsc->client->options->mount_timeout * HZ);
Sage Weil2f2dc052009-10-06 11:31:09 -07003033
3034 /* tear down remaining requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08003035 mutex_lock(&mdsc->mutex);
3036 while ((req = __get_oldest_req(mdsc))) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003037 dout("wait_requests timed out on tid %llu\n",
3038 req->r_tid);
Sage Weil44ca18f2010-02-15 12:08:46 -08003039 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07003040 }
3041 }
3042 mutex_unlock(&mdsc->mutex);
3043 dout("wait_requests done\n");
3044}
3045
3046/*
3047 * called before mount is ro, and before dentries are torn down.
3048 * (hmm, does this still race with new lookups?)
3049 */
3050void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
3051{
3052 dout("pre_umount\n");
3053 mdsc->stopping = 1;
3054
3055 drop_leases(mdsc);
Sage Weilafcdaea2009-10-14 14:27:38 -07003056 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003057 wait_requests(mdsc);
Sage Weil17c688c2010-06-21 16:12:26 -07003058
3059 /*
3060 * wait for reply handlers to drop their request refs and
3061 * their inode/dcache refs
3062 */
3063 ceph_msgr_flush();
Sage Weil2f2dc052009-10-06 11:31:09 -07003064}
3065
3066/*
3067 * wait for all write mds requests to flush.
3068 */
3069static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
3070{
Sage Weil80fc7312010-03-16 15:28:54 -07003071 struct ceph_mds_request *req = NULL, *nextreq;
Sage Weil44ca18f2010-02-15 12:08:46 -08003072 struct rb_node *n;
Sage Weil2f2dc052009-10-06 11:31:09 -07003073
3074 mutex_lock(&mdsc->mutex);
3075 dout("wait_unsafe_requests want %lld\n", want_tid);
Sage Weil80fc7312010-03-16 15:28:54 -07003076restart:
Sage Weil44ca18f2010-02-15 12:08:46 -08003077 req = __get_oldest_req(mdsc);
3078 while (req && req->r_tid <= want_tid) {
Sage Weil80fc7312010-03-16 15:28:54 -07003079 /* find next request */
3080 n = rb_next(&req->r_node);
3081 if (n)
3082 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
3083 else
3084 nextreq = NULL;
Sage Weil44ca18f2010-02-15 12:08:46 -08003085 if ((req->r_op & CEPH_MDS_OP_WRITE)) {
3086 /* write op */
3087 ceph_mdsc_get_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07003088 if (nextreq)
3089 ceph_mdsc_get_request(nextreq);
Sage Weil44ca18f2010-02-15 12:08:46 -08003090 mutex_unlock(&mdsc->mutex);
3091 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3092 req->r_tid, want_tid);
3093 wait_for_completion(&req->r_safe_completion);
3094 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003095 ceph_mdsc_put_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07003096 if (!nextreq)
3097 break; /* next dne before, so we're done! */
3098 if (RB_EMPTY_NODE(&nextreq->r_node)) {
3099 /* next request was removed from tree */
3100 ceph_mdsc_put_request(nextreq);
3101 goto restart;
3102 }
3103 ceph_mdsc_put_request(nextreq); /* won't go away */
Sage Weil44ca18f2010-02-15 12:08:46 -08003104 }
Sage Weil80fc7312010-03-16 15:28:54 -07003105 req = nextreq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003106 }
3107 mutex_unlock(&mdsc->mutex);
3108 dout("wait_unsafe_requests done\n");
3109}
3110
3111void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
3112{
3113 u64 want_tid, want_flush;
3114
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003115 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
Sage Weil56b7cf92010-05-03 15:22:00 -07003116 return;
3117
Sage Weil2f2dc052009-10-06 11:31:09 -07003118 dout("sync\n");
3119 mutex_lock(&mdsc->mutex);
3120 want_tid = mdsc->last_tid;
3121 want_flush = mdsc->cap_flush_seq;
3122 mutex_unlock(&mdsc->mutex);
3123 dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
3124
Sage Weilafcdaea2009-10-14 14:27:38 -07003125 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003126
3127 wait_unsafe_requests(mdsc, want_tid);
3128 wait_event(mdsc->cap_flushing_wq, check_cap_flush(mdsc, want_flush));
3129}
3130
Sage Weilf3c60c52010-08-11 14:51:23 -07003131/*
3132 * true if all sessions are closed, or we force unmount
3133 */
3134bool done_closing_sessions(struct ceph_mds_client *mdsc)
3135{
3136 int i, n = 0;
3137
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003138 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
Sage Weilf3c60c52010-08-11 14:51:23 -07003139 return true;
3140
3141 mutex_lock(&mdsc->mutex);
3142 for (i = 0; i < mdsc->max_sessions; i++)
3143 if (mdsc->sessions[i])
3144 n++;
3145 mutex_unlock(&mdsc->mutex);
3146 return n == 0;
3147}
Sage Weil2f2dc052009-10-06 11:31:09 -07003148
3149/*
3150 * called after sb is ro.
3151 */
3152void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
3153{
3154 struct ceph_mds_session *session;
3155 int i;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003156 struct ceph_fs_client *fsc = mdsc->fsc;
3157 unsigned long timeout = fsc->client->options->mount_timeout * HZ;
Sage Weil2f2dc052009-10-06 11:31:09 -07003158
3159 dout("close_sessions\n");
3160
Sage Weil2f2dc052009-10-06 11:31:09 -07003161 /* close sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07003162 mutex_lock(&mdsc->mutex);
3163 for (i = 0; i < mdsc->max_sessions; i++) {
3164 session = __ceph_lookup_mds_session(mdsc, i);
3165 if (!session)
3166 continue;
Sage Weil2f2dc052009-10-06 11:31:09 -07003167 mutex_unlock(&mdsc->mutex);
Sage Weilf3c60c52010-08-11 14:51:23 -07003168 mutex_lock(&session->s_mutex);
3169 __close_session(mdsc, session);
3170 mutex_unlock(&session->s_mutex);
3171 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003172 mutex_lock(&mdsc->mutex);
3173 }
Sage Weilf3c60c52010-08-11 14:51:23 -07003174 mutex_unlock(&mdsc->mutex);
3175
3176 dout("waiting for sessions to close\n");
3177 wait_event_timeout(mdsc->session_close_wq, done_closing_sessions(mdsc),
3178 timeout);
Sage Weil2f2dc052009-10-06 11:31:09 -07003179
3180 /* tear down remaining sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07003181 mutex_lock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003182 for (i = 0; i < mdsc->max_sessions; i++) {
3183 if (mdsc->sessions[i]) {
3184 session = get_session(mdsc->sessions[i]);
Sage Weil2600d2d2010-02-22 15:12:16 -08003185 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003186 mutex_unlock(&mdsc->mutex);
3187 mutex_lock(&session->s_mutex);
3188 remove_session_caps(session);
3189 mutex_unlock(&session->s_mutex);
3190 ceph_put_mds_session(session);
3191 mutex_lock(&mdsc->mutex);
3192 }
3193 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003194 WARN_ON(!list_empty(&mdsc->cap_delay_list));
Sage Weil2f2dc052009-10-06 11:31:09 -07003195 mutex_unlock(&mdsc->mutex);
3196
3197 ceph_cleanup_empty_realms(mdsc);
3198
3199 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3200
3201 dout("stopped\n");
3202}
3203
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003204static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07003205{
3206 dout("stop\n");
3207 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3208 if (mdsc->mdsmap)
3209 ceph_mdsmap_destroy(mdsc->mdsmap);
3210 kfree(mdsc->sessions);
Yehuda Sadeh37151662010-06-17 16:16:12 -07003211 ceph_caps_finalize(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003212}
3213
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003214void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
3215{
3216 struct ceph_mds_client *mdsc = fsc->mdsc;
3217
Sage Weilef550f62011-03-25 13:27:48 -07003218 dout("mdsc_destroy %p\n", mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003219 ceph_mdsc_stop(mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07003220
3221 /* flush out any connection work with references to us */
3222 ceph_msgr_flush();
3223
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003224 fsc->mdsc = NULL;
3225 kfree(mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07003226 dout("mdsc_destroy %p done\n", mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003227}
3228
Sage Weil2f2dc052009-10-06 11:31:09 -07003229
3230/*
3231 * handle mds map update.
3232 */
3233void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3234{
3235 u32 epoch;
3236 u32 maplen;
3237 void *p = msg->front.iov_base;
3238 void *end = p + msg->front.iov_len;
3239 struct ceph_mdsmap *newmap, *oldmap;
3240 struct ceph_fsid fsid;
3241 int err = -EINVAL;
3242
3243 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
3244 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003245 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
Sage Weil07433042009-11-18 16:50:41 -08003246 return;
Sage Weilc89136e2009-10-14 09:59:09 -07003247 epoch = ceph_decode_32(&p);
3248 maplen = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07003249 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
3250
3251 /* do we need it? */
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003252 ceph_monc_got_mdsmap(&mdsc->fsc->client->monc, epoch);
Sage Weil2f2dc052009-10-06 11:31:09 -07003253 mutex_lock(&mdsc->mutex);
3254 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
3255 dout("handle_map epoch %u <= our %u\n",
3256 epoch, mdsc->mdsmap->m_epoch);
3257 mutex_unlock(&mdsc->mutex);
3258 return;
3259 }
3260
3261 newmap = ceph_mdsmap_decode(&p, end);
3262 if (IS_ERR(newmap)) {
3263 err = PTR_ERR(newmap);
3264 goto bad_unlock;
3265 }
3266
3267 /* swap into place */
3268 if (mdsc->mdsmap) {
3269 oldmap = mdsc->mdsmap;
3270 mdsc->mdsmap = newmap;
3271 check_new_map(mdsc, newmap, oldmap);
3272 ceph_mdsmap_destroy(oldmap);
3273 } else {
3274 mdsc->mdsmap = newmap; /* first mds map */
3275 }
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003276 mdsc->fsc->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
Sage Weil2f2dc052009-10-06 11:31:09 -07003277
3278 __wake_requests(mdsc, &mdsc->waiting_for_map);
3279
3280 mutex_unlock(&mdsc->mutex);
3281 schedule_delayed(mdsc);
3282 return;
3283
3284bad_unlock:
3285 mutex_unlock(&mdsc->mutex);
3286bad:
3287 pr_err("error decoding mdsmap %d\n", err);
3288 return;
3289}
3290
3291static struct ceph_connection *con_get(struct ceph_connection *con)
3292{
3293 struct ceph_mds_session *s = con->private;
3294
3295 if (get_session(s)) {
Sage Weil2600d2d2010-02-22 15:12:16 -08003296 dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -07003297 return con;
3298 }
3299 dout("mdsc con_get %p FAIL\n", s);
3300 return NULL;
3301}
3302
3303static void con_put(struct ceph_connection *con)
3304{
3305 struct ceph_mds_session *s = con->private;
3306
Sage Weil2f2dc052009-10-06 11:31:09 -07003307 ceph_put_mds_session(s);
Sage Weil2600d2d2010-02-22 15:12:16 -08003308 dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -07003309}
3310
3311/*
3312 * if the client is unresponsive for long enough, the mds will kill
3313 * the session entirely.
3314 */
3315static void peer_reset(struct ceph_connection *con)
3316{
3317 struct ceph_mds_session *s = con->private;
Sage Weil7e70f0e2010-03-18 13:59:12 -07003318 struct ceph_mds_client *mdsc = s->s_mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07003319
Sage Weil7e70f0e2010-03-18 13:59:12 -07003320 pr_warning("mds%d closed our session\n", s->s_mds);
3321 send_mds_reconnect(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07003322}
3323
3324static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
3325{
3326 struct ceph_mds_session *s = con->private;
3327 struct ceph_mds_client *mdsc = s->s_mdsc;
3328 int type = le16_to_cpu(msg->hdr.type);
3329
Sage Weil2600d2d2010-02-22 15:12:16 -08003330 mutex_lock(&mdsc->mutex);
3331 if (__verify_registered_session(mdsc, s) < 0) {
3332 mutex_unlock(&mdsc->mutex);
3333 goto out;
3334 }
3335 mutex_unlock(&mdsc->mutex);
3336
Sage Weil2f2dc052009-10-06 11:31:09 -07003337 switch (type) {
3338 case CEPH_MSG_MDS_MAP:
3339 ceph_mdsc_handle_map(mdsc, msg);
3340 break;
3341 case CEPH_MSG_CLIENT_SESSION:
3342 handle_session(s, msg);
3343 break;
3344 case CEPH_MSG_CLIENT_REPLY:
3345 handle_reply(s, msg);
3346 break;
3347 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
Sage Weil2600d2d2010-02-22 15:12:16 -08003348 handle_forward(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003349 break;
3350 case CEPH_MSG_CLIENT_CAPS:
3351 ceph_handle_caps(s, msg);
3352 break;
3353 case CEPH_MSG_CLIENT_SNAP:
Sage Weil2600d2d2010-02-22 15:12:16 -08003354 ceph_handle_snap(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003355 break;
3356 case CEPH_MSG_CLIENT_LEASE:
Sage Weil2600d2d2010-02-22 15:12:16 -08003357 handle_lease(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003358 break;
3359
3360 default:
3361 pr_err("received unknown message type %d %s\n", type,
3362 ceph_msg_type_name(type));
3363 }
Sage Weil2600d2d2010-02-22 15:12:16 -08003364out:
Sage Weil2f2dc052009-10-06 11:31:09 -07003365 ceph_msg_put(msg);
3366}
3367
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003368/*
3369 * authentication
3370 */
3371static int get_authorizer(struct ceph_connection *con,
3372 void **buf, int *len, int *proto,
3373 void **reply_buf, int *reply_len, int force_new)
3374{
3375 struct ceph_mds_session *s = con->private;
3376 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003377 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003378 int ret = 0;
3379
3380 if (force_new && s->s_authorizer) {
3381 ac->ops->destroy_authorizer(ac, s->s_authorizer);
3382 s->s_authorizer = NULL;
3383 }
3384 if (s->s_authorizer == NULL) {
3385 if (ac->ops->create_authorizer) {
3386 ret = ac->ops->create_authorizer(
3387 ac, CEPH_ENTITY_TYPE_MDS,
3388 &s->s_authorizer,
3389 &s->s_authorizer_buf,
3390 &s->s_authorizer_buf_len,
3391 &s->s_authorizer_reply_buf,
3392 &s->s_authorizer_reply_buf_len);
3393 if (ret)
3394 return ret;
3395 }
3396 }
3397
3398 *proto = ac->protocol;
3399 *buf = s->s_authorizer_buf;
3400 *len = s->s_authorizer_buf_len;
3401 *reply_buf = s->s_authorizer_reply_buf;
3402 *reply_len = s->s_authorizer_reply_buf_len;
3403 return 0;
3404}
3405
3406
3407static int verify_authorizer_reply(struct ceph_connection *con, int len)
3408{
3409 struct ceph_mds_session *s = con->private;
3410 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003411 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003412
3413 return ac->ops->verify_authorizer_reply(ac, s->s_authorizer, len);
3414}
3415
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003416static int invalidate_authorizer(struct ceph_connection *con)
3417{
3418 struct ceph_mds_session *s = con->private;
3419 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003420 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003421
3422 if (ac->ops->invalidate_authorizer)
3423 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
3424
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003425 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003426}
3427
Tobias Klauser9e327892010-05-20 10:40:19 +02003428static const struct ceph_connection_operations mds_con_ops = {
Sage Weil2f2dc052009-10-06 11:31:09 -07003429 .get = con_get,
3430 .put = con_put,
3431 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003432 .get_authorizer = get_authorizer,
3433 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003434 .invalidate_authorizer = invalidate_authorizer,
Sage Weil2f2dc052009-10-06 11:31:09 -07003435 .peer_reset = peer_reset,
Sage Weil2f2dc052009-10-06 11:31:09 -07003436};
3437
Sage Weil2f2dc052009-10-06 11:31:09 -07003438/* eof */