blob: 34bc35084ae336ce18b51aca9bbe98162093ea17 [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) {
Sage Weil41b02e12011-07-26 11:31:14 -0700486 ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
Sage Weil153c8e62009-12-07 12:31:09 -0800487 iput(req->r_inode);
488 }
489 if (req->r_locked_dir)
Sage Weil41b02e12011-07-26 11:31:14 -0700490 ceph_put_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
Sage Weil153c8e62009-12-07 12:31:09 -0800491 if (req->r_target_inode)
492 iput(req->r_target_inode);
493 if (req->r_dentry)
494 dput(req->r_dentry);
495 if (req->r_old_dentry) {
Sage Weil41b02e12011-07-26 11:31:14 -0700496 /*
497 * track (and drop pins for) r_old_dentry_dir
498 * separately, since r_old_dentry's d_parent may have
499 * changed between the dir mutex being dropped and
500 * this request being freed.
501 */
502 ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir),
503 CEPH_CAP_PIN);
Sage Weil153c8e62009-12-07 12:31:09 -0800504 dput(req->r_old_dentry);
Sage Weil41b02e12011-07-26 11:31:14 -0700505 iput(req->r_old_dentry_dir);
Sage Weil153c8e62009-12-07 12:31:09 -0800506 }
507 kfree(req->r_path1);
508 kfree(req->r_path2);
509 put_request_session(req);
Yehuda Sadeh37151662010-06-17 16:16:12 -0700510 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
Sage Weil153c8e62009-12-07 12:31:09 -0800511 kfree(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700512}
513
514/*
515 * lookup session, bump ref if found.
516 *
517 * called under mdsc->mutex.
518 */
519static struct ceph_mds_request *__lookup_request(struct ceph_mds_client *mdsc,
520 u64 tid)
521{
522 struct ceph_mds_request *req;
Sage Weil44ca18f2010-02-15 12:08:46 -0800523 struct rb_node *n = mdsc->request_tree.rb_node;
524
525 while (n) {
526 req = rb_entry(n, struct ceph_mds_request, r_node);
527 if (tid < req->r_tid)
528 n = n->rb_left;
529 else if (tid > req->r_tid)
530 n = n->rb_right;
531 else {
532 ceph_mdsc_get_request(req);
533 return req;
534 }
535 }
536 return NULL;
537}
538
539static void __insert_request(struct ceph_mds_client *mdsc,
540 struct ceph_mds_request *new)
541{
542 struct rb_node **p = &mdsc->request_tree.rb_node;
543 struct rb_node *parent = NULL;
544 struct ceph_mds_request *req = NULL;
545
546 while (*p) {
547 parent = *p;
548 req = rb_entry(parent, struct ceph_mds_request, r_node);
549 if (new->r_tid < req->r_tid)
550 p = &(*p)->rb_left;
551 else if (new->r_tid > req->r_tid)
552 p = &(*p)->rb_right;
553 else
554 BUG();
555 }
556
557 rb_link_node(&new->r_node, parent, p);
558 rb_insert_color(&new->r_node, &mdsc->request_tree);
Sage Weil2f2dc052009-10-06 11:31:09 -0700559}
560
561/*
562 * Register an in-flight request, and assign a tid. Link to directory
563 * are modifying (if any).
564 *
565 * Called under mdsc->mutex.
566 */
567static void __register_request(struct ceph_mds_client *mdsc,
568 struct ceph_mds_request *req,
569 struct inode *dir)
570{
571 req->r_tid = ++mdsc->last_tid;
572 if (req->r_num_caps)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700573 ceph_reserve_caps(mdsc, &req->r_caps_reservation,
574 req->r_num_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -0700575 dout("__register_request %p tid %lld\n", req, req->r_tid);
576 ceph_mdsc_get_request(req);
Sage Weil44ca18f2010-02-15 12:08:46 -0800577 __insert_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700578
Sage Weilcb4276c2010-11-08 07:28:52 -0800579 req->r_uid = current_fsuid();
580 req->r_gid = current_fsgid();
581
Sage Weil2f2dc052009-10-06 11:31:09 -0700582 if (dir) {
583 struct ceph_inode_info *ci = ceph_inode(dir);
584
Sage Weil3b663782011-05-18 16:12:12 -0700585 ihold(dir);
Sage Weil2f2dc052009-10-06 11:31:09 -0700586 spin_lock(&ci->i_unsafe_lock);
587 req->r_unsafe_dir = dir;
588 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
589 spin_unlock(&ci->i_unsafe_lock);
590 }
591}
592
593static void __unregister_request(struct ceph_mds_client *mdsc,
594 struct ceph_mds_request *req)
595{
596 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
Sage Weil44ca18f2010-02-15 12:08:46 -0800597 rb_erase(&req->r_node, &mdsc->request_tree);
Sage Weil80fc7312010-03-16 15:28:54 -0700598 RB_CLEAR_NODE(&req->r_node);
Sage Weil2f2dc052009-10-06 11:31:09 -0700599
600 if (req->r_unsafe_dir) {
601 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
602
603 spin_lock(&ci->i_unsafe_lock);
604 list_del_init(&req->r_unsafe_dir_item);
605 spin_unlock(&ci->i_unsafe_lock);
Sage Weil3b663782011-05-18 16:12:12 -0700606
607 iput(req->r_unsafe_dir);
608 req->r_unsafe_dir = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700609 }
Sage Weil94aa8ae2010-03-28 21:22:50 -0700610
611 ceph_mdsc_put_request(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700612}
613
614/*
615 * Choose mds to send request to next. If there is a hint set in the
616 * request (e.g., due to a prior forward hint from the mds), use that.
617 * Otherwise, consult frag tree and/or caps to identify the
618 * appropriate mds. If all else fails, choose randomly.
619 *
620 * Called under mdsc->mutex.
621 */
H Hartley Sweeten7fd7d102011-09-23 13:22:11 -0700622static struct dentry *get_nonsnap_parent(struct dentry *dentry)
Sage Weileb6bb1c2010-08-16 09:21:27 -0700623{
Sage Weild79698d2011-07-26 11:31:26 -0700624 /*
625 * we don't need to worry about protecting the d_parent access
626 * here because we never renaming inside the snapped namespace
627 * except to resplice to another snapdir, and either the old or new
628 * result is a valid result.
629 */
Sage Weileb6bb1c2010-08-16 09:21:27 -0700630 while (!IS_ROOT(dentry) && ceph_snap(dentry->d_inode) != CEPH_NOSNAP)
631 dentry = dentry->d_parent;
632 return dentry;
633}
634
Sage Weil2f2dc052009-10-06 11:31:09 -0700635static int __choose_mds(struct ceph_mds_client *mdsc,
636 struct ceph_mds_request *req)
637{
638 struct inode *inode;
639 struct ceph_inode_info *ci;
640 struct ceph_cap *cap;
641 int mode = req->r_direct_mode;
642 int mds = -1;
643 u32 hash = req->r_direct_hash;
644 bool is_hash = req->r_direct_is_hash;
645
646 /*
647 * is there a specific mds we should try? ignore hint if we have
648 * no session and the mds is not up (active or recovering).
649 */
650 if (req->r_resend_mds >= 0 &&
651 (__have_session(mdsc, req->r_resend_mds) ||
652 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
653 dout("choose_mds using resend_mds mds%d\n",
654 req->r_resend_mds);
655 return req->r_resend_mds;
656 }
657
658 if (mode == USE_RANDOM_MDS)
659 goto random;
660
661 inode = NULL;
662 if (req->r_inode) {
663 inode = req->r_inode;
664 } else if (req->r_dentry) {
Sage Weild79698d2011-07-26 11:31:26 -0700665 /* ignore race with rename; old or new d_parent is okay */
666 struct dentry *parent = req->r_dentry->d_parent;
667 struct inode *dir = parent->d_inode;
Sage Weileb6bb1c2010-08-16 09:21:27 -0700668
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700669 if (dir->i_sb != mdsc->fsc->sb) {
Sage Weileb6bb1c2010-08-16 09:21:27 -0700670 /* not this fs! */
671 inode = req->r_dentry->d_inode;
672 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
673 /* direct snapped/virtual snapdir requests
674 * based on parent dir inode */
Sage Weild79698d2011-07-26 11:31:26 -0700675 struct dentry *dn = get_nonsnap_parent(parent);
Sage Weileb6bb1c2010-08-16 09:21:27 -0700676 inode = dn->d_inode;
677 dout("__choose_mds using nonsnap parent %p\n", inode);
678 } else if (req->r_dentry->d_inode) {
679 /* dentry target */
Sage Weil2f2dc052009-10-06 11:31:09 -0700680 inode = req->r_dentry->d_inode;
681 } else {
Sage Weileb6bb1c2010-08-16 09:21:27 -0700682 /* dir + name */
683 inode = dir;
Sage Weile5f86dc2011-07-26 11:30:55 -0700684 hash = ceph_dentry_hash(dir, req->r_dentry);
Sage Weil2f2dc052009-10-06 11:31:09 -0700685 is_hash = true;
686 }
687 }
Sage Weileb6bb1c2010-08-16 09:21:27 -0700688
Sage Weil2f2dc052009-10-06 11:31:09 -0700689 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
690 (int)hash, mode);
691 if (!inode)
692 goto random;
693 ci = ceph_inode(inode);
694
695 if (is_hash && S_ISDIR(inode->i_mode)) {
696 struct ceph_inode_frag frag;
697 int found;
698
699 ceph_choose_frag(ci, hash, &frag, &found);
700 if (found) {
701 if (mode == USE_ANY_MDS && frag.ndist > 0) {
702 u8 r;
703
704 /* choose a random replica */
705 get_random_bytes(&r, 1);
706 r %= frag.ndist;
707 mds = frag.dist[r];
708 dout("choose_mds %p %llx.%llx "
709 "frag %u mds%d (%d/%d)\n",
710 inode, ceph_vinop(inode),
Sage Weild66bbd42011-01-21 21:16:46 -0800711 frag.frag, mds,
Sage Weil2f2dc052009-10-06 11:31:09 -0700712 (int)r, frag.ndist);
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 /* since this file/dir wasn't known to be
719 * replicated, then we want to look for the
720 * authoritative mds. */
721 mode = USE_AUTH_MDS;
722 if (frag.mds >= 0) {
723 /* choose auth mds */
724 mds = frag.mds;
725 dout("choose_mds %p %llx.%llx "
726 "frag %u mds%d (auth)\n",
727 inode, ceph_vinop(inode), frag.frag, mds);
Sage Weild66bbd42011-01-21 21:16:46 -0800728 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
729 CEPH_MDS_STATE_ACTIVE)
730 return mds;
Sage Weil2f2dc052009-10-06 11:31:09 -0700731 }
732 }
733 }
734
Sage Weilbe655592011-11-30 09:47:09 -0800735 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700736 cap = NULL;
737 if (mode == USE_AUTH_MDS)
738 cap = ci->i_auth_cap;
739 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
740 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
741 if (!cap) {
Sage Weilbe655592011-11-30 09:47:09 -0800742 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700743 goto random;
744 }
745 mds = cap->session->s_mds;
746 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
747 inode, ceph_vinop(inode), mds,
748 cap == ci->i_auth_cap ? "auth " : "", cap);
Sage Weilbe655592011-11-30 09:47:09 -0800749 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700750 return mds;
751
752random:
753 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
754 dout("choose_mds chose random mds%d\n", mds);
755 return mds;
756}
757
758
759/*
760 * session messages
761 */
762static struct ceph_msg *create_session_msg(u32 op, u64 seq)
763{
764 struct ceph_msg *msg;
765 struct ceph_mds_session_head *h;
766
Sage Weilb61c2762011-08-09 15:03:46 -0700767 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
768 false);
Sage Weila79832f2010-04-01 16:06:19 -0700769 if (!msg) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700770 pr_err("create_session_msg ENOMEM creating msg\n");
Sage Weila79832f2010-04-01 16:06:19 -0700771 return NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700772 }
773 h = msg->front.iov_base;
774 h->op = cpu_to_le32(op);
775 h->seq = cpu_to_le64(seq);
776 return msg;
777}
778
779/*
780 * send session open request.
781 *
782 * called under mdsc->mutex
783 */
784static int __open_session(struct ceph_mds_client *mdsc,
785 struct ceph_mds_session *session)
786{
787 struct ceph_msg *msg;
788 int mstate;
789 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -0700790
791 /* wait for mds to go active? */
792 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
793 dout("open_session to mds%d (%s)\n", mds,
794 ceph_mds_state_name(mstate));
795 session->s_state = CEPH_MDS_SESSION_OPENING;
796 session->s_renew_requested = jiffies;
797
798 /* send connect message */
799 msg = create_session_msg(CEPH_SESSION_REQUEST_OPEN, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -0700800 if (!msg)
801 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -0700802 ceph_con_send(&session->s_con, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -0700803 return 0;
804}
805
806/*
Sage Weiled0552a2010-06-21 13:38:25 -0700807 * open sessions for any export targets for the given mds
808 *
809 * called under mdsc->mutex
810 */
811static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
812 struct ceph_mds_session *session)
813{
814 struct ceph_mds_info *mi;
815 struct ceph_mds_session *ts;
816 int i, mds = session->s_mds;
817 int target;
818
819 if (mds >= mdsc->mdsmap->m_max_mds)
820 return;
821 mi = &mdsc->mdsmap->m_info[mds];
822 dout("open_export_target_sessions for mds%d (%d targets)\n",
823 session->s_mds, mi->num_export_targets);
824
825 for (i = 0; i < mi->num_export_targets; i++) {
826 target = mi->export_targets[i];
827 ts = __ceph_lookup_mds_session(mdsc, target);
828 if (!ts) {
829 ts = register_session(mdsc, target);
830 if (IS_ERR(ts))
831 return;
832 }
833 if (session->s_state == CEPH_MDS_SESSION_NEW ||
834 session->s_state == CEPH_MDS_SESSION_CLOSING)
835 __open_session(mdsc, session);
836 else
837 dout(" mds%d target mds%d %p is %s\n", session->s_mds,
838 i, ts, session_state_name(ts->s_state));
839 ceph_put_mds_session(ts);
840 }
841}
842
Sage Weil154f42c2010-06-21 13:45:04 -0700843void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
844 struct ceph_mds_session *session)
845{
846 mutex_lock(&mdsc->mutex);
847 __open_export_target_sessions(mdsc, session);
848 mutex_unlock(&mdsc->mutex);
849}
850
Sage Weiled0552a2010-06-21 13:38:25 -0700851/*
Sage Weil2f2dc052009-10-06 11:31:09 -0700852 * session caps
853 */
854
855/*
856 * Free preallocated cap messages assigned to this session
857 */
858static void cleanup_cap_releases(struct ceph_mds_session *session)
859{
860 struct ceph_msg *msg;
861
862 spin_lock(&session->s_cap_lock);
863 while (!list_empty(&session->s_cap_releases)) {
864 msg = list_first_entry(&session->s_cap_releases,
865 struct ceph_msg, list_head);
866 list_del_init(&msg->list_head);
867 ceph_msg_put(msg);
868 }
869 while (!list_empty(&session->s_cap_releases_done)) {
870 msg = list_first_entry(&session->s_cap_releases_done,
871 struct ceph_msg, list_head);
872 list_del_init(&msg->list_head);
873 ceph_msg_put(msg);
874 }
875 spin_unlock(&session->s_cap_lock);
876}
877
878/*
Sage Weilf818a732010-05-11 20:56:31 -0700879 * Helper to safely iterate over all caps associated with a session, with
880 * special care taken to handle a racing __ceph_remove_cap().
Sage Weil2f2dc052009-10-06 11:31:09 -0700881 *
Sage Weilf818a732010-05-11 20:56:31 -0700882 * Caller must hold session s_mutex.
Sage Weil2f2dc052009-10-06 11:31:09 -0700883 */
884static int iterate_session_caps(struct ceph_mds_session *session,
885 int (*cb)(struct inode *, struct ceph_cap *,
886 void *), void *arg)
887{
Sage Weil7c1332b2010-02-16 11:39:45 -0800888 struct list_head *p;
889 struct ceph_cap *cap;
890 struct inode *inode, *last_inode = NULL;
891 struct ceph_cap *old_cap = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700892 int ret;
893
894 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
895 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800896 p = session->s_caps.next;
897 while (p != &session->s_caps) {
898 cap = list_entry(p, struct ceph_cap, session_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -0700899 inode = igrab(&cap->ci->vfs_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -0800900 if (!inode) {
901 p = p->next;
Sage Weil2f2dc052009-10-06 11:31:09 -0700902 continue;
Sage Weil7c1332b2010-02-16 11:39:45 -0800903 }
904 session->s_cap_iterator = cap;
Sage Weil2f2dc052009-10-06 11:31:09 -0700905 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800906
907 if (last_inode) {
908 iput(last_inode);
909 last_inode = NULL;
910 }
911 if (old_cap) {
Yehuda Sadeh37151662010-06-17 16:16:12 -0700912 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -0800913 old_cap = NULL;
914 }
915
Sage Weil2f2dc052009-10-06 11:31:09 -0700916 ret = cb(inode, cap, arg);
Sage Weil7c1332b2010-02-16 11:39:45 -0800917 last_inode = inode;
918
Sage Weil2f2dc052009-10-06 11:31:09 -0700919 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800920 p = p->next;
921 if (cap->ci == NULL) {
922 dout("iterate_session_caps finishing cap %p removal\n",
923 cap);
924 BUG_ON(cap->session != session);
925 list_del_init(&cap->session_caps);
926 session->s_nr_caps--;
927 cap->session = NULL;
928 old_cap = cap; /* put_cap it w/o locks held */
929 }
Sage Weil5dacf092009-12-21 20:40:34 -0800930 if (ret < 0)
931 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -0700932 }
Sage Weil5dacf092009-12-21 20:40:34 -0800933 ret = 0;
934out:
Sage Weil7c1332b2010-02-16 11:39:45 -0800935 session->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700936 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800937
938 if (last_inode)
939 iput(last_inode);
940 if (old_cap)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700941 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -0800942
Sage Weil5dacf092009-12-21 20:40:34 -0800943 return ret;
Sage Weil2f2dc052009-10-06 11:31:09 -0700944}
945
946static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
Sage Weil6c99f252010-05-10 16:12:25 -0700947 void *arg)
Sage Weil2f2dc052009-10-06 11:31:09 -0700948{
949 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil6c99f252010-05-10 16:12:25 -0700950 int drop = 0;
951
Sage Weil2f2dc052009-10-06 11:31:09 -0700952 dout("removing cap %p, ci is %p, inode is %p\n",
953 cap, ci, &ci->vfs_inode);
Sage Weilbe655592011-11-30 09:47:09 -0800954 spin_lock(&ci->i_ceph_lock);
Sage Weil6c99f252010-05-10 16:12:25 -0700955 __ceph_remove_cap(cap);
956 if (!__ceph_is_any_real_caps(ci)) {
957 struct ceph_mds_client *mdsc =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700958 ceph_sb_to_client(inode->i_sb)->mdsc;
Sage Weil6c99f252010-05-10 16:12:25 -0700959
960 spin_lock(&mdsc->cap_dirty_lock);
961 if (!list_empty(&ci->i_dirty_item)) {
962 pr_info(" dropping dirty %s state for %p %lld\n",
963 ceph_cap_string(ci->i_dirty_caps),
964 inode, ceph_ino(inode));
965 ci->i_dirty_caps = 0;
966 list_del_init(&ci->i_dirty_item);
967 drop = 1;
968 }
969 if (!list_empty(&ci->i_flushing_item)) {
970 pr_info(" dropping dirty+flushing %s state for %p %lld\n",
971 ceph_cap_string(ci->i_flushing_caps),
972 inode, ceph_ino(inode));
973 ci->i_flushing_caps = 0;
974 list_del_init(&ci->i_flushing_item);
975 mdsc->num_cap_flushing--;
976 drop = 1;
977 }
978 if (drop && ci->i_wrbuffer_ref) {
979 pr_info(" dropping dirty data for %p %lld\n",
980 inode, ceph_ino(inode));
981 ci->i_wrbuffer_ref = 0;
982 ci->i_wrbuffer_ref_head = 0;
983 drop++;
984 }
985 spin_unlock(&mdsc->cap_dirty_lock);
986 }
Sage Weilbe655592011-11-30 09:47:09 -0800987 spin_unlock(&ci->i_ceph_lock);
Sage Weil6c99f252010-05-10 16:12:25 -0700988 while (drop--)
989 iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -0700990 return 0;
991}
992
993/*
994 * caller must hold session s_mutex
995 */
996static void remove_session_caps(struct ceph_mds_session *session)
997{
998 dout("remove_session_caps on %p\n", session);
999 iterate_session_caps(session, remove_session_caps_cb, NULL);
1000 BUG_ON(session->s_nr_caps > 0);
Sage Weil6c99f252010-05-10 16:12:25 -07001001 BUG_ON(!list_empty(&session->s_cap_flushing));
Sage Weil2f2dc052009-10-06 11:31:09 -07001002 cleanup_cap_releases(session);
1003}
1004
1005/*
1006 * wake up any threads waiting on this session's caps. if the cap is
1007 * old (didn't get renewed on the client reconnect), remove it now.
1008 *
1009 * caller must hold s_mutex.
1010 */
1011static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1012 void *arg)
1013{
Sage Weil0dc25702009-11-20 13:43:45 -08001014 struct ceph_inode_info *ci = ceph_inode(inode);
1015
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001016 wake_up_all(&ci->i_cap_wq);
Sage Weil0dc25702009-11-20 13:43:45 -08001017 if (arg) {
Sage Weilbe655592011-11-30 09:47:09 -08001018 spin_lock(&ci->i_ceph_lock);
Sage Weil0dc25702009-11-20 13:43:45 -08001019 ci->i_wanted_max_size = 0;
1020 ci->i_requested_max_size = 0;
Sage Weilbe655592011-11-30 09:47:09 -08001021 spin_unlock(&ci->i_ceph_lock);
Sage Weil0dc25702009-11-20 13:43:45 -08001022 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001023 return 0;
1024}
1025
Sage Weil0dc25702009-11-20 13:43:45 -08001026static void wake_up_session_caps(struct ceph_mds_session *session,
1027 int reconnect)
Sage Weil2f2dc052009-10-06 11:31:09 -07001028{
1029 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
Sage Weil0dc25702009-11-20 13:43:45 -08001030 iterate_session_caps(session, wake_up_session_cb,
1031 (void *)(unsigned long)reconnect);
Sage Weil2f2dc052009-10-06 11:31:09 -07001032}
1033
1034/*
1035 * Send periodic message to MDS renewing all currently held caps. The
1036 * ack will reset the expiration for all caps from this session.
1037 *
1038 * caller holds s_mutex
1039 */
1040static int send_renew_caps(struct ceph_mds_client *mdsc,
1041 struct ceph_mds_session *session)
1042{
1043 struct ceph_msg *msg;
1044 int state;
1045
1046 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1047 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1048 pr_info("mds%d caps stale\n", session->s_mds);
Sage Weile4cb4cb2010-03-18 13:43:09 -07001049 session->s_renew_requested = jiffies;
Sage Weil2f2dc052009-10-06 11:31:09 -07001050
1051 /* do not try to renew caps until a recovering mds has reconnected
1052 * with its clients. */
1053 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1054 if (state < CEPH_MDS_STATE_RECONNECT) {
1055 dout("send_renew_caps ignoring mds%d (%s)\n",
1056 session->s_mds, ceph_mds_state_name(state));
1057 return 0;
1058 }
1059
1060 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1061 ceph_mds_state_name(state));
Sage Weil2f2dc052009-10-06 11:31:09 -07001062 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1063 ++session->s_renew_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001064 if (!msg)
1065 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -07001066 ceph_con_send(&session->s_con, msg);
1067 return 0;
1068}
1069
1070/*
1071 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
Sage Weil0dc25702009-11-20 13:43:45 -08001072 *
1073 * Called under session->s_mutex
Sage Weil2f2dc052009-10-06 11:31:09 -07001074 */
1075static void renewed_caps(struct ceph_mds_client *mdsc,
1076 struct ceph_mds_session *session, int is_renew)
1077{
1078 int was_stale;
1079 int wake = 0;
1080
1081 spin_lock(&session->s_cap_lock);
1082 was_stale = is_renew && (session->s_cap_ttl == 0 ||
1083 time_after_eq(jiffies, session->s_cap_ttl));
1084
1085 session->s_cap_ttl = session->s_renew_requested +
1086 mdsc->mdsmap->m_session_timeout*HZ;
1087
1088 if (was_stale) {
1089 if (time_before(jiffies, session->s_cap_ttl)) {
1090 pr_info("mds%d caps renewed\n", session->s_mds);
1091 wake = 1;
1092 } else {
1093 pr_info("mds%d caps still stale\n", session->s_mds);
1094 }
1095 }
1096 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1097 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1098 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1099 spin_unlock(&session->s_cap_lock);
1100
1101 if (wake)
Sage Weil0dc25702009-11-20 13:43:45 -08001102 wake_up_session_caps(session, 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07001103}
1104
1105/*
1106 * send a session close request
1107 */
1108static int request_close_session(struct ceph_mds_client *mdsc,
1109 struct ceph_mds_session *session)
1110{
1111 struct ceph_msg *msg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001112
1113 dout("request_close_session mds%d state %s seq %lld\n",
1114 session->s_mds, session_state_name(session->s_state),
1115 session->s_seq);
1116 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001117 if (!msg)
1118 return -ENOMEM;
1119 ceph_con_send(&session->s_con, msg);
1120 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001121}
1122
1123/*
1124 * Called with s_mutex held.
1125 */
1126static int __close_session(struct ceph_mds_client *mdsc,
1127 struct ceph_mds_session *session)
1128{
1129 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1130 return 0;
1131 session->s_state = CEPH_MDS_SESSION_CLOSING;
1132 return request_close_session(mdsc, session);
1133}
1134
1135/*
1136 * Trim old(er) caps.
1137 *
1138 * Because we can't cache an inode without one or more caps, we do
1139 * this indirectly: if a cap is unused, we prune its aliases, at which
1140 * point the inode will hopefully get dropped to.
1141 *
1142 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1143 * memory pressure from the MDS, though, so it needn't be perfect.
1144 */
1145static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1146{
1147 struct ceph_mds_session *session = arg;
1148 struct ceph_inode_info *ci = ceph_inode(inode);
1149 int used, oissued, mine;
1150
1151 if (session->s_trim_caps <= 0)
1152 return -1;
1153
Sage Weilbe655592011-11-30 09:47:09 -08001154 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001155 mine = cap->issued | cap->implemented;
1156 used = __ceph_caps_used(ci);
1157 oissued = __ceph_caps_issued_other(ci, cap);
1158
1159 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n",
1160 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
1161 ceph_cap_string(used));
1162 if (ci->i_dirty_caps)
1163 goto out; /* dirty caps */
1164 if ((used & ~oissued) & mine)
1165 goto out; /* we need these caps */
1166
1167 session->s_trim_caps--;
1168 if (oissued) {
1169 /* we aren't the only cap.. just remove us */
Sage Weil7c1332b2010-02-16 11:39:45 -08001170 __ceph_remove_cap(cap);
Sage Weil2f2dc052009-10-06 11:31:09 -07001171 } else {
1172 /* try to drop referring dentries */
Sage Weilbe655592011-11-30 09:47:09 -08001173 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001174 d_prune_aliases(inode);
1175 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1176 inode, cap, atomic_read(&inode->i_count));
1177 return 0;
1178 }
1179
1180out:
Sage Weilbe655592011-11-30 09:47:09 -08001181 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001182 return 0;
1183}
1184
1185/*
1186 * Trim session cap count down to some max number.
1187 */
1188static int trim_caps(struct ceph_mds_client *mdsc,
1189 struct ceph_mds_session *session,
1190 int max_caps)
1191{
1192 int trim_caps = session->s_nr_caps - max_caps;
1193
1194 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1195 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1196 if (trim_caps > 0) {
1197 session->s_trim_caps = trim_caps;
1198 iterate_session_caps(session, trim_caps_cb, session);
1199 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1200 session->s_mds, session->s_nr_caps, max_caps,
1201 trim_caps - session->s_trim_caps);
Sage Weil5dacf092009-12-21 20:40:34 -08001202 session->s_trim_caps = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001203 }
1204 return 0;
1205}
1206
1207/*
1208 * Allocate cap_release messages. If there is a partially full message
1209 * in the queue, try to allocate enough to cover it's remainder, so that
1210 * we can send it immediately.
1211 *
1212 * Called under s_mutex.
1213 */
Sage Weil2b2300d2010-06-09 16:52:04 -07001214int ceph_add_cap_releases(struct ceph_mds_client *mdsc,
Sage Weilee6b2722010-06-10 12:55:52 -07001215 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001216{
Sage Weil38e8883e2010-06-10 12:57:11 -07001217 struct ceph_msg *msg, *partial = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001218 struct ceph_mds_cap_release *head;
1219 int err = -ENOMEM;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001220 int extra = mdsc->fsc->mount_options->cap_release_safety;
Sage Weil38e8883e2010-06-10 12:57:11 -07001221 int num;
Sage Weil2f2dc052009-10-06 11:31:09 -07001222
Sage Weil38e8883e2010-06-10 12:57:11 -07001223 dout("add_cap_releases %p mds%d extra %d\n", session, session->s_mds,
1224 extra);
Sage Weil2f2dc052009-10-06 11:31:09 -07001225
1226 spin_lock(&session->s_cap_lock);
1227
1228 if (!list_empty(&session->s_cap_releases)) {
1229 msg = list_first_entry(&session->s_cap_releases,
1230 struct ceph_msg,
1231 list_head);
1232 head = msg->front.iov_base;
Sage Weil38e8883e2010-06-10 12:57:11 -07001233 num = le32_to_cpu(head->num);
1234 if (num) {
1235 dout(" partial %p with (%d/%d)\n", msg, num,
1236 (int)CEPH_CAPS_PER_RELEASE);
1237 extra += CEPH_CAPS_PER_RELEASE - num;
1238 partial = msg;
1239 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001240 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001241 while (session->s_num_cap_releases < session->s_nr_caps + extra) {
1242 spin_unlock(&session->s_cap_lock);
Yehuda Sadeh34d23762010-04-06 14:33:58 -07001243 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE,
Sage Weilb61c2762011-08-09 15:03:46 -07001244 GFP_NOFS, false);
Sage Weil2f2dc052009-10-06 11:31:09 -07001245 if (!msg)
1246 goto out_unlocked;
1247 dout("add_cap_releases %p msg %p now %d\n", session, msg,
1248 (int)msg->front.iov_len);
1249 head = msg->front.iov_base;
1250 head->num = cpu_to_le32(0);
1251 msg->front.iov_len = sizeof(*head);
1252 spin_lock(&session->s_cap_lock);
1253 list_add(&msg->list_head, &session->s_cap_releases);
1254 session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
1255 }
1256
Sage Weil38e8883e2010-06-10 12:57:11 -07001257 if (partial) {
1258 head = partial->front.iov_base;
1259 num = le32_to_cpu(head->num);
1260 dout(" queueing partial %p with %d/%d\n", partial, num,
1261 (int)CEPH_CAPS_PER_RELEASE);
1262 list_move_tail(&partial->list_head,
1263 &session->s_cap_releases_done);
1264 session->s_num_cap_releases -= CEPH_CAPS_PER_RELEASE - num;
Sage Weil2f2dc052009-10-06 11:31:09 -07001265 }
1266 err = 0;
1267 spin_unlock(&session->s_cap_lock);
1268out_unlocked:
1269 return err;
1270}
1271
1272/*
1273 * flush all dirty inode data to disk.
1274 *
1275 * returns true if we've flushed through want_flush_seq
1276 */
1277static int check_cap_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
1278{
1279 int mds, ret = 1;
1280
1281 dout("check_cap_flush want %lld\n", want_flush_seq);
1282 mutex_lock(&mdsc->mutex);
1283 for (mds = 0; ret && mds < mdsc->max_sessions; mds++) {
1284 struct ceph_mds_session *session = mdsc->sessions[mds];
1285
1286 if (!session)
1287 continue;
1288 get_session(session);
1289 mutex_unlock(&mdsc->mutex);
1290
1291 mutex_lock(&session->s_mutex);
1292 if (!list_empty(&session->s_cap_flushing)) {
1293 struct ceph_inode_info *ci =
1294 list_entry(session->s_cap_flushing.next,
1295 struct ceph_inode_info,
1296 i_flushing_item);
1297 struct inode *inode = &ci->vfs_inode;
1298
Sage Weilbe655592011-11-30 09:47:09 -08001299 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001300 if (ci->i_cap_flush_seq <= want_flush_seq) {
1301 dout("check_cap_flush still flushing %p "
1302 "seq %lld <= %lld to mds%d\n", inode,
1303 ci->i_cap_flush_seq, want_flush_seq,
1304 session->s_mds);
1305 ret = 0;
1306 }
Sage Weilbe655592011-11-30 09:47:09 -08001307 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001308 }
1309 mutex_unlock(&session->s_mutex);
1310 ceph_put_mds_session(session);
1311
1312 if (!ret)
1313 return ret;
1314 mutex_lock(&mdsc->mutex);
1315 }
1316
1317 mutex_unlock(&mdsc->mutex);
1318 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
1319 return ret;
1320}
1321
1322/*
1323 * called under s_mutex
1324 */
Sage Weil3d7ded42010-06-09 16:47:10 -07001325void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1326 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001327{
1328 struct ceph_msg *msg;
1329
1330 dout("send_cap_releases mds%d\n", session->s_mds);
Sage Weil0f8605f2010-05-05 15:51:35 -07001331 spin_lock(&session->s_cap_lock);
1332 while (!list_empty(&session->s_cap_releases_done)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001333 msg = list_first_entry(&session->s_cap_releases_done,
1334 struct ceph_msg, list_head);
1335 list_del_init(&msg->list_head);
1336 spin_unlock(&session->s_cap_lock);
1337 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1338 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1339 ceph_con_send(&session->s_con, msg);
Sage Weil0f8605f2010-05-05 15:51:35 -07001340 spin_lock(&session->s_cap_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001341 }
1342 spin_unlock(&session->s_cap_lock);
1343}
1344
Sage Weile01a5942010-05-10 15:36:44 -07001345static void discard_cap_releases(struct ceph_mds_client *mdsc,
1346 struct ceph_mds_session *session)
1347{
1348 struct ceph_msg *msg;
1349 struct ceph_mds_cap_release *head;
1350 unsigned num;
1351
1352 dout("discard_cap_releases mds%d\n", session->s_mds);
1353 spin_lock(&session->s_cap_lock);
1354
1355 /* zero out the in-progress message */
1356 msg = list_first_entry(&session->s_cap_releases,
1357 struct ceph_msg, list_head);
1358 head = msg->front.iov_base;
1359 num = le32_to_cpu(head->num);
1360 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg, num);
1361 head->num = cpu_to_le32(0);
1362 session->s_num_cap_releases += num;
1363
1364 /* requeue completed messages */
1365 while (!list_empty(&session->s_cap_releases_done)) {
1366 msg = list_first_entry(&session->s_cap_releases_done,
1367 struct ceph_msg, list_head);
1368 list_del_init(&msg->list_head);
1369
1370 head = msg->front.iov_base;
1371 num = le32_to_cpu(head->num);
1372 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg,
1373 num);
1374 session->s_num_cap_releases += num;
1375 head->num = cpu_to_le32(0);
1376 msg->front.iov_len = sizeof(*head);
1377 list_add(&msg->list_head, &session->s_cap_releases);
1378 }
1379
1380 spin_unlock(&session->s_cap_lock);
1381}
1382
Sage Weil2f2dc052009-10-06 11:31:09 -07001383/*
1384 * requests
1385 */
1386
1387/*
1388 * Create an mds request.
1389 */
1390struct ceph_mds_request *
1391ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1392{
1393 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1394
1395 if (!req)
1396 return ERR_PTR(-ENOMEM);
1397
Sage Weilb4556392010-05-13 12:01:13 -07001398 mutex_init(&req->r_fill_mutex);
Yehuda Sadeh37151662010-06-17 16:16:12 -07001399 req->r_mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07001400 req->r_started = jiffies;
1401 req->r_resend_mds = -1;
1402 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1403 req->r_fmode = -1;
Sage Weil153c8e62009-12-07 12:31:09 -08001404 kref_init(&req->r_kref);
Sage Weil2f2dc052009-10-06 11:31:09 -07001405 INIT_LIST_HEAD(&req->r_wait);
1406 init_completion(&req->r_completion);
1407 init_completion(&req->r_safe_completion);
1408 INIT_LIST_HEAD(&req->r_unsafe_item);
1409
1410 req->r_op = op;
1411 req->r_direct_mode = mode;
1412 return req;
1413}
1414
1415/*
Sage Weil44ca18f2010-02-15 12:08:46 -08001416 * return oldest (lowest) request, tid in request tree, 0 if none.
Sage Weil2f2dc052009-10-06 11:31:09 -07001417 *
1418 * called under mdsc->mutex.
1419 */
Sage Weil44ca18f2010-02-15 12:08:46 -08001420static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1421{
1422 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1423 return NULL;
1424 return rb_entry(rb_first(&mdsc->request_tree),
1425 struct ceph_mds_request, r_node);
1426}
1427
Sage Weil2f2dc052009-10-06 11:31:09 -07001428static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1429{
Sage Weil44ca18f2010-02-15 12:08:46 -08001430 struct ceph_mds_request *req = __get_oldest_req(mdsc);
1431
1432 if (req)
1433 return req->r_tid;
1434 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001435}
1436
1437/*
1438 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1439 * on build_path_from_dentry in fs/cifs/dir.c.
1440 *
1441 * If @stop_on_nosnap, generate path relative to the first non-snapped
1442 * inode.
1443 *
1444 * Encode hidden .snap dirs as a double /, i.e.
1445 * foo/.snap/bar -> foo//bar
1446 */
1447char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1448 int stop_on_nosnap)
1449{
1450 struct dentry *temp;
1451 char *path;
1452 int len, pos;
Al Viro1b71fe22011-07-16 23:43:58 -04001453 unsigned seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07001454
1455 if (dentry == NULL)
1456 return ERR_PTR(-EINVAL);
1457
1458retry:
1459 len = 0;
Al Viro1b71fe22011-07-16 23:43:58 -04001460 seq = read_seqbegin(&rename_lock);
1461 rcu_read_lock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001462 for (temp = dentry; !IS_ROOT(temp);) {
1463 struct inode *inode = temp->d_inode;
1464 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1465 len++; /* slash only */
1466 else if (stop_on_nosnap && inode &&
1467 ceph_snap(inode) == CEPH_NOSNAP)
1468 break;
1469 else
1470 len += 1 + temp->d_name.len;
1471 temp = temp->d_parent;
1472 if (temp == NULL) {
Al Viro1b71fe22011-07-16 23:43:58 -04001473 rcu_read_unlock();
Sage Weil6c99f252010-05-10 16:12:25 -07001474 pr_err("build_path corrupt dentry %p\n", dentry);
Sage Weil2f2dc052009-10-06 11:31:09 -07001475 return ERR_PTR(-EINVAL);
1476 }
1477 }
Al Viro1b71fe22011-07-16 23:43:58 -04001478 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001479 if (len)
1480 len--; /* no leading '/' */
1481
1482 path = kmalloc(len+1, GFP_NOFS);
1483 if (path == NULL)
1484 return ERR_PTR(-ENOMEM);
1485 pos = len;
1486 path[pos] = 0; /* trailing null */
Al Viro1b71fe22011-07-16 23:43:58 -04001487 rcu_read_lock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001488 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
Al Viro1b71fe22011-07-16 23:43:58 -04001489 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07001490
Al Viro1b71fe22011-07-16 23:43:58 -04001491 spin_lock(&temp->d_lock);
1492 inode = temp->d_inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07001493 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
Sage Weil104648a2010-03-18 10:14:30 -07001494 dout("build_path path+%d: %p SNAPDIR\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001495 pos, temp);
1496 } else if (stop_on_nosnap && inode &&
1497 ceph_snap(inode) == CEPH_NOSNAP) {
1498 break;
1499 } else {
1500 pos -= temp->d_name.len;
Al Viro1b71fe22011-07-16 23:43:58 -04001501 if (pos < 0) {
1502 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001503 break;
Al Viro1b71fe22011-07-16 23:43:58 -04001504 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001505 strncpy(path + pos, temp->d_name.name,
1506 temp->d_name.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07001507 }
Al Viro1b71fe22011-07-16 23:43:58 -04001508 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001509 if (pos)
1510 path[--pos] = '/';
1511 temp = temp->d_parent;
1512 if (temp == NULL) {
Al Viro1b71fe22011-07-16 23:43:58 -04001513 rcu_read_unlock();
Sage Weil104648a2010-03-18 10:14:30 -07001514 pr_err("build_path corrupt dentry\n");
Sage Weil2f2dc052009-10-06 11:31:09 -07001515 kfree(path);
1516 return ERR_PTR(-EINVAL);
1517 }
1518 }
Al Viro1b71fe22011-07-16 23:43:58 -04001519 rcu_read_unlock();
1520 if (pos != 0 || read_seqretry(&rename_lock, seq)) {
Sage Weil104648a2010-03-18 10:14:30 -07001521 pr_err("build_path did not end path lookup where "
Sage Weil2f2dc052009-10-06 11:31:09 -07001522 "expected, namelen is %d, pos is %d\n", len, pos);
1523 /* presumably this is only possible if racing with a
1524 rename of one of the parent directories (we can not
1525 lock the dentries above us to prevent this, but
1526 retrying should be harmless) */
1527 kfree(path);
1528 goto retry;
1529 }
1530
1531 *base = ceph_ino(temp->d_inode);
1532 *plen = len;
Sage Weil104648a2010-03-18 10:14:30 -07001533 dout("build_path on %p %d built %llx '%.*s'\n",
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001534 dentry, dentry->d_count, *base, len, path);
Sage Weil2f2dc052009-10-06 11:31:09 -07001535 return path;
1536}
1537
1538static int build_dentry_path(struct dentry *dentry,
1539 const char **ppath, int *ppathlen, u64 *pino,
1540 int *pfreepath)
1541{
1542 char *path;
1543
1544 if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) {
1545 *pino = ceph_ino(dentry->d_parent->d_inode);
1546 *ppath = dentry->d_name.name;
1547 *ppathlen = dentry->d_name.len;
1548 return 0;
1549 }
1550 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1551 if (IS_ERR(path))
1552 return PTR_ERR(path);
1553 *ppath = path;
1554 *pfreepath = 1;
1555 return 0;
1556}
1557
1558static int build_inode_path(struct inode *inode,
1559 const char **ppath, int *ppathlen, u64 *pino,
1560 int *pfreepath)
1561{
1562 struct dentry *dentry;
1563 char *path;
1564
1565 if (ceph_snap(inode) == CEPH_NOSNAP) {
1566 *pino = ceph_ino(inode);
1567 *ppathlen = 0;
1568 return 0;
1569 }
1570 dentry = d_find_alias(inode);
1571 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1572 dput(dentry);
1573 if (IS_ERR(path))
1574 return PTR_ERR(path);
1575 *ppath = path;
1576 *pfreepath = 1;
1577 return 0;
1578}
1579
1580/*
1581 * request arguments may be specified via an inode *, a dentry *, or
1582 * an explicit ino+path.
1583 */
1584static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1585 const char *rpath, u64 rino,
1586 const char **ppath, int *pathlen,
1587 u64 *ino, int *freepath)
1588{
1589 int r = 0;
1590
1591 if (rinode) {
1592 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1593 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1594 ceph_snap(rinode));
1595 } else if (rdentry) {
1596 r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
1597 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1598 *ppath);
Sage Weil795858d2011-08-15 13:02:37 -07001599 } else if (rpath || rino) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001600 *ino = rino;
1601 *ppath = rpath;
1602 *pathlen = strlen(rpath);
1603 dout(" path %.*s\n", *pathlen, rpath);
1604 }
1605
1606 return r;
1607}
1608
1609/*
1610 * called under mdsc->mutex
1611 */
1612static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1613 struct ceph_mds_request *req,
1614 int mds)
1615{
1616 struct ceph_msg *msg;
1617 struct ceph_mds_request_head *head;
1618 const char *path1 = NULL;
1619 const char *path2 = NULL;
1620 u64 ino1 = 0, ino2 = 0;
1621 int pathlen1 = 0, pathlen2 = 0;
1622 int freepath1 = 0, freepath2 = 0;
1623 int len;
1624 u16 releases;
1625 void *p, *end;
1626 int ret;
1627
1628 ret = set_request_path_attr(req->r_inode, req->r_dentry,
1629 req->r_path1, req->r_ino1.ino,
1630 &path1, &pathlen1, &ino1, &freepath1);
1631 if (ret < 0) {
1632 msg = ERR_PTR(ret);
1633 goto out;
1634 }
1635
1636 ret = set_request_path_attr(NULL, req->r_old_dentry,
1637 req->r_path2, req->r_ino2.ino,
1638 &path2, &pathlen2, &ino2, &freepath2);
1639 if (ret < 0) {
1640 msg = ERR_PTR(ret);
1641 goto out_free1;
1642 }
1643
1644 len = sizeof(*head) +
Sage Weilac8839d2010-01-27 14:28:10 -08001645 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64));
Sage Weil2f2dc052009-10-06 11:31:09 -07001646
1647 /* calculate (max) length for cap releases */
1648 len += sizeof(struct ceph_mds_request_release) *
1649 (!!req->r_inode_drop + !!req->r_dentry_drop +
1650 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1651 if (req->r_dentry_drop)
1652 len += req->r_dentry->d_name.len;
1653 if (req->r_old_dentry_drop)
1654 len += req->r_old_dentry->d_name.len;
1655
Sage Weilb61c2762011-08-09 15:03:46 -07001656 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07001657 if (!msg) {
1658 msg = ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -07001659 goto out_free2;
Sage Weila79832f2010-04-01 16:06:19 -07001660 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001661
Sage Weil6df058c2009-12-22 11:24:33 -08001662 msg->hdr.tid = cpu_to_le64(req->r_tid);
1663
Sage Weil2f2dc052009-10-06 11:31:09 -07001664 head = msg->front.iov_base;
1665 p = msg->front.iov_base + sizeof(*head);
1666 end = msg->front.iov_base + msg->front.iov_len;
1667
1668 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1669 head->op = cpu_to_le32(req->r_op);
Sage Weilcb4276c2010-11-08 07:28:52 -08001670 head->caller_uid = cpu_to_le32(req->r_uid);
1671 head->caller_gid = cpu_to_le32(req->r_gid);
Sage Weil2f2dc052009-10-06 11:31:09 -07001672 head->args = req->r_args;
1673
1674 ceph_encode_filepath(&p, end, ino1, path1);
1675 ceph_encode_filepath(&p, end, ino2, path2);
1676
Sage Weile979cf52010-07-15 14:58:39 -07001677 /* make note of release offset, in case we need to replay */
1678 req->r_request_release_offset = p - msg->front.iov_base;
1679
Sage Weil2f2dc052009-10-06 11:31:09 -07001680 /* cap releases */
1681 releases = 0;
1682 if (req->r_inode_drop)
1683 releases += ceph_encode_inode_release(&p,
1684 req->r_inode ? req->r_inode : req->r_dentry->d_inode,
1685 mds, req->r_inode_drop, req->r_inode_unless, 0);
1686 if (req->r_dentry_drop)
1687 releases += ceph_encode_dentry_release(&p, req->r_dentry,
1688 mds, req->r_dentry_drop, req->r_dentry_unless);
1689 if (req->r_old_dentry_drop)
1690 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
1691 mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
1692 if (req->r_old_inode_drop)
1693 releases += ceph_encode_inode_release(&p,
1694 req->r_old_dentry->d_inode,
1695 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
1696 head->num_releases = cpu_to_le16(releases);
1697
1698 BUG_ON(p > end);
1699 msg->front.iov_len = p - msg->front.iov_base;
1700 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1701
1702 msg->pages = req->r_pages;
1703 msg->nr_pages = req->r_num_pages;
1704 msg->hdr.data_len = cpu_to_le32(req->r_data_len);
1705 msg->hdr.data_off = cpu_to_le16(0);
1706
1707out_free2:
1708 if (freepath2)
1709 kfree((char *)path2);
1710out_free1:
1711 if (freepath1)
1712 kfree((char *)path1);
1713out:
1714 return msg;
1715}
1716
1717/*
1718 * called under mdsc->mutex if error, under no mutex if
1719 * success.
1720 */
1721static void complete_request(struct ceph_mds_client *mdsc,
1722 struct ceph_mds_request *req)
1723{
1724 if (req->r_callback)
1725 req->r_callback(mdsc, req);
1726 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001727 complete_all(&req->r_completion);
Sage Weil2f2dc052009-10-06 11:31:09 -07001728}
1729
1730/*
1731 * called under mdsc->mutex
1732 */
1733static int __prepare_send_request(struct ceph_mds_client *mdsc,
1734 struct ceph_mds_request *req,
1735 int mds)
1736{
1737 struct ceph_mds_request_head *rhead;
1738 struct ceph_msg *msg;
1739 int flags = 0;
1740
Sage Weil2f2dc052009-10-06 11:31:09 -07001741 req->r_attempts++;
Greg Farnume55b71f2010-06-22 15:58:01 -07001742 if (req->r_inode) {
1743 struct ceph_cap *cap =
1744 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
1745
1746 if (cap)
1747 req->r_sent_on_mseq = cap->mseq;
1748 else
1749 req->r_sent_on_mseq = -1;
1750 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001751 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
1752 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
1753
Sage Weil01a92f12010-07-15 13:24:32 -07001754 if (req->r_got_unsafe) {
1755 /*
1756 * Replay. Do not regenerate message (and rebuild
1757 * paths, etc.); just use the original message.
1758 * Rebuilding paths will break for renames because
1759 * d_move mangles the src name.
1760 */
1761 msg = req->r_request;
1762 rhead = msg->front.iov_base;
1763
1764 flags = le32_to_cpu(rhead->flags);
1765 flags |= CEPH_MDS_FLAG_REPLAY;
1766 rhead->flags = cpu_to_le32(flags);
1767
1768 if (req->r_target_inode)
1769 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
1770
1771 rhead->num_retry = req->r_attempts - 1;
Sage Weile979cf52010-07-15 14:58:39 -07001772
1773 /* remove cap/dentry releases from message */
1774 rhead->num_releases = 0;
1775 msg->hdr.front_len = cpu_to_le32(req->r_request_release_offset);
1776 msg->front.iov_len = req->r_request_release_offset;
Sage Weil01a92f12010-07-15 13:24:32 -07001777 return 0;
1778 }
1779
Sage Weil2f2dc052009-10-06 11:31:09 -07001780 if (req->r_request) {
1781 ceph_msg_put(req->r_request);
1782 req->r_request = NULL;
1783 }
1784 msg = create_request_message(mdsc, req, mds);
1785 if (IS_ERR(msg)) {
Sage Weile1518c72010-05-13 11:19:06 -07001786 req->r_err = PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001787 complete_request(mdsc, req);
Sage Weila79832f2010-04-01 16:06:19 -07001788 return PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001789 }
1790 req->r_request = msg;
1791
1792 rhead = msg->front.iov_base;
Sage Weil2f2dc052009-10-06 11:31:09 -07001793 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
1794 if (req->r_got_unsafe)
1795 flags |= CEPH_MDS_FLAG_REPLAY;
1796 if (req->r_locked_dir)
1797 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
1798 rhead->flags = cpu_to_le32(flags);
1799 rhead->num_fwd = req->r_num_fwd;
1800 rhead->num_retry = req->r_attempts - 1;
Sage Weil01a92f12010-07-15 13:24:32 -07001801 rhead->ino = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001802
1803 dout(" r_locked_dir = %p\n", req->r_locked_dir);
Sage Weil2f2dc052009-10-06 11:31:09 -07001804 return 0;
1805}
1806
1807/*
1808 * send request, or put it on the appropriate wait list.
1809 */
1810static int __do_request(struct ceph_mds_client *mdsc,
1811 struct ceph_mds_request *req)
1812{
1813 struct ceph_mds_session *session = NULL;
1814 int mds = -1;
1815 int err = -EAGAIN;
1816
Sage Weile1518c72010-05-13 11:19:06 -07001817 if (req->r_err || req->r_got_result)
Sage Weil2f2dc052009-10-06 11:31:09 -07001818 goto out;
1819
1820 if (req->r_timeout &&
1821 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
1822 dout("do_request timed out\n");
1823 err = -EIO;
1824 goto finish;
1825 }
1826
Sage Weildc69e2e2010-11-02 13:49:00 -07001827 put_request_session(req);
1828
Sage Weil2f2dc052009-10-06 11:31:09 -07001829 mds = __choose_mds(mdsc, req);
1830 if (mds < 0 ||
1831 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
1832 dout("do_request no mds or not active, waiting for map\n");
1833 list_add(&req->r_wait, &mdsc->waiting_for_map);
1834 goto out;
1835 }
1836
1837 /* get, open session */
1838 session = __ceph_lookup_mds_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07001839 if (!session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001840 session = register_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07001841 if (IS_ERR(session)) {
1842 err = PTR_ERR(session);
1843 goto finish;
1844 }
1845 }
Sage Weildc69e2e2010-11-02 13:49:00 -07001846 req->r_session = get_session(session);
1847
Sage Weil2f2dc052009-10-06 11:31:09 -07001848 dout("do_request mds%d session %p state %s\n", mds, session,
1849 session_state_name(session->s_state));
1850 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
1851 session->s_state != CEPH_MDS_SESSION_HUNG) {
1852 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1853 session->s_state == CEPH_MDS_SESSION_CLOSING)
1854 __open_session(mdsc, session);
1855 list_add(&req->r_wait, &session->s_waiting);
1856 goto out_session;
1857 }
1858
1859 /* send request */
Sage Weil2f2dc052009-10-06 11:31:09 -07001860 req->r_resend_mds = -1; /* forget any previous mds hint */
1861
1862 if (req->r_request_started == 0) /* note request start time */
1863 req->r_request_started = jiffies;
1864
1865 err = __prepare_send_request(mdsc, req, mds);
1866 if (!err) {
1867 ceph_msg_get(req->r_request);
1868 ceph_con_send(&session->s_con, req->r_request);
1869 }
1870
1871out_session:
1872 ceph_put_mds_session(session);
1873out:
1874 return err;
1875
1876finish:
Sage Weile1518c72010-05-13 11:19:06 -07001877 req->r_err = err;
Sage Weil2f2dc052009-10-06 11:31:09 -07001878 complete_request(mdsc, req);
1879 goto out;
1880}
1881
1882/*
1883 * called under mdsc->mutex
1884 */
1885static void __wake_requests(struct ceph_mds_client *mdsc,
1886 struct list_head *head)
1887{
1888 struct ceph_mds_request *req, *nreq;
1889
1890 list_for_each_entry_safe(req, nreq, head, r_wait) {
1891 list_del_init(&req->r_wait);
1892 __do_request(mdsc, req);
1893 }
1894}
1895
1896/*
1897 * Wake up threads with requests pending for @mds, so that they can
Sage Weil29790f22010-03-18 14:45:05 -07001898 * resubmit their requests to a possibly different mds.
Sage Weil2f2dc052009-10-06 11:31:09 -07001899 */
Sage Weil29790f22010-03-18 14:45:05 -07001900static void kick_requests(struct ceph_mds_client *mdsc, int mds)
Sage Weil2f2dc052009-10-06 11:31:09 -07001901{
Sage Weil44ca18f2010-02-15 12:08:46 -08001902 struct ceph_mds_request *req;
1903 struct rb_node *p;
Sage Weil2f2dc052009-10-06 11:31:09 -07001904
1905 dout("kick_requests mds%d\n", mds);
Sage Weil44ca18f2010-02-15 12:08:46 -08001906 for (p = rb_first(&mdsc->request_tree); p; p = rb_next(p)) {
1907 req = rb_entry(p, struct ceph_mds_request, r_node);
1908 if (req->r_got_unsafe)
1909 continue;
1910 if (req->r_session &&
1911 req->r_session->s_mds == mds) {
1912 dout(" kicking tid %llu\n", req->r_tid);
Sage Weil44ca18f2010-02-15 12:08:46 -08001913 __do_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07001914 }
1915 }
1916}
1917
1918void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
1919 struct ceph_mds_request *req)
1920{
1921 dout("submit_request on %p\n", req);
1922 mutex_lock(&mdsc->mutex);
1923 __register_request(mdsc, req, NULL);
1924 __do_request(mdsc, req);
1925 mutex_unlock(&mdsc->mutex);
1926}
1927
1928/*
1929 * Synchrously perform an mds request. Take care of all of the
1930 * session setup, forwarding, retry details.
1931 */
1932int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
1933 struct inode *dir,
1934 struct ceph_mds_request *req)
1935{
1936 int err;
1937
1938 dout("do_request on %p\n", req);
1939
1940 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
1941 if (req->r_inode)
1942 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
1943 if (req->r_locked_dir)
1944 ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
1945 if (req->r_old_dentry)
Sage Weil41b02e12011-07-26 11:31:14 -07001946 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
1947 CEPH_CAP_PIN);
Sage Weil2f2dc052009-10-06 11:31:09 -07001948
1949 /* issue */
1950 mutex_lock(&mdsc->mutex);
1951 __register_request(mdsc, req, dir);
1952 __do_request(mdsc, req);
1953
Sage Weile1518c72010-05-13 11:19:06 -07001954 if (req->r_err) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001955 err = req->r_err;
Sage Weile1518c72010-05-13 11:19:06 -07001956 __unregister_request(mdsc, req);
1957 dout("do_request early error %d\n", err);
1958 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07001959 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001960
Sage Weile1518c72010-05-13 11:19:06 -07001961 /* wait */
1962 mutex_unlock(&mdsc->mutex);
1963 dout("do_request waiting\n");
1964 if (req->r_timeout) {
Sage Weilaa916472010-05-24 11:15:51 -07001965 err = (long)wait_for_completion_killable_timeout(
Sage Weile1518c72010-05-13 11:19:06 -07001966 &req->r_completion, req->r_timeout);
1967 if (err == 0)
1968 err = -EIO;
1969 } else {
Sage Weilaa916472010-05-24 11:15:51 -07001970 err = wait_for_completion_killable(&req->r_completion);
Sage Weile1518c72010-05-13 11:19:06 -07001971 }
1972 dout("do_request waited, got %d\n", err);
1973 mutex_lock(&mdsc->mutex);
1974
1975 /* only abort if we didn't race with a real reply */
1976 if (req->r_got_result) {
1977 err = le32_to_cpu(req->r_reply_info.head->result);
1978 } else if (err < 0) {
1979 dout("aborted request %lld with %d\n", req->r_tid, err);
Sage Weilb4556392010-05-13 12:01:13 -07001980
1981 /*
1982 * ensure we aren't running concurrently with
1983 * ceph_fill_trace or ceph_readdir_prepopulate, which
1984 * rely on locks (dir mutex) held by our caller.
1985 */
1986 mutex_lock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07001987 req->r_err = err;
1988 req->r_aborted = true;
Sage Weilb4556392010-05-13 12:01:13 -07001989 mutex_unlock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07001990
1991 if (req->r_locked_dir &&
Sage Weil167c9e32010-05-14 10:02:57 -07001992 (req->r_op & CEPH_MDS_OP_WRITE))
1993 ceph_invalidate_dir_request(req);
Sage Weile1518c72010-05-13 11:19:06 -07001994 } else {
1995 err = req->r_err;
1996 }
1997
1998out:
1999 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002000 dout("do_request %p done, result %d\n", req, err);
2001 return err;
2002}
2003
2004/*
Sage Weilc6ffe102011-11-03 09:23:36 -07002005 * Invalidate dir D_COMPLETE, dentry lease state on an aborted MDS
Sage Weil167c9e32010-05-14 10:02:57 -07002006 * namespace request.
2007 */
2008void ceph_invalidate_dir_request(struct ceph_mds_request *req)
2009{
2010 struct inode *inode = req->r_locked_dir;
2011 struct ceph_inode_info *ci = ceph_inode(inode);
2012
Sage Weilc6ffe102011-11-03 09:23:36 -07002013 dout("invalidate_dir_request %p (D_COMPLETE, lease(s))\n", inode);
Sage Weilbe655592011-11-30 09:47:09 -08002014 spin_lock(&ci->i_ceph_lock);
Sage Weilc6ffe102011-11-03 09:23:36 -07002015 ceph_dir_clear_complete(inode);
Sage Weil167c9e32010-05-14 10:02:57 -07002016 ci->i_release_count++;
Sage Weilbe655592011-11-30 09:47:09 -08002017 spin_unlock(&ci->i_ceph_lock);
Sage Weil167c9e32010-05-14 10:02:57 -07002018
2019 if (req->r_dentry)
2020 ceph_invalidate_dentry_lease(req->r_dentry);
2021 if (req->r_old_dentry)
2022 ceph_invalidate_dentry_lease(req->r_old_dentry);
2023}
2024
2025/*
Sage Weil2f2dc052009-10-06 11:31:09 -07002026 * Handle mds reply.
2027 *
2028 * We take the session mutex and parse and process the reply immediately.
2029 * This preserves the logical ordering of replies, capabilities, etc., sent
2030 * by the MDS as they are applied to our local cache.
2031 */
2032static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2033{
2034 struct ceph_mds_client *mdsc = session->s_mdsc;
2035 struct ceph_mds_request *req;
2036 struct ceph_mds_reply_head *head = msg->front.iov_base;
2037 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
2038 u64 tid;
2039 int err, result;
Sage Weil2600d2d2010-02-22 15:12:16 -08002040 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002041
Sage Weil2f2dc052009-10-06 11:31:09 -07002042 if (msg->front.iov_len < sizeof(*head)) {
2043 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002044 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002045 return;
2046 }
2047
2048 /* get request, session */
Sage Weil6df058c2009-12-22 11:24:33 -08002049 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002050 mutex_lock(&mdsc->mutex);
2051 req = __lookup_request(mdsc, tid);
2052 if (!req) {
2053 dout("handle_reply on unknown tid %llu\n", tid);
2054 mutex_unlock(&mdsc->mutex);
2055 return;
2056 }
2057 dout("handle_reply %p\n", req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002058
2059 /* correct session? */
Sage Weild96d6042010-03-20 20:50:58 -07002060 if (req->r_session != session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002061 pr_err("mdsc_handle_reply got %llu on session mds%d"
2062 " not mds%d\n", tid, session->s_mds,
2063 req->r_session ? req->r_session->s_mds : -1);
2064 mutex_unlock(&mdsc->mutex);
2065 goto out;
2066 }
2067
2068 /* dup? */
2069 if ((req->r_got_unsafe && !head->safe) ||
2070 (req->r_got_safe && head->safe)) {
2071 pr_warning("got a dup %s reply on %llu from mds%d\n",
2072 head->safe ? "safe" : "unsafe", tid, mds);
2073 mutex_unlock(&mdsc->mutex);
2074 goto out;
2075 }
Sage Weil85792d0d2010-05-13 09:06:02 -07002076 if (req->r_got_safe && !head->safe) {
2077 pr_warning("got unsafe after safe on %llu from mds%d\n",
2078 tid, mds);
2079 mutex_unlock(&mdsc->mutex);
2080 goto out;
2081 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002082
2083 result = le32_to_cpu(head->result);
2084
2085 /*
Greg Farnume55b71f2010-06-22 15:58:01 -07002086 * Handle an ESTALE
2087 * if we're not talking to the authority, send to them
2088 * if the authority has changed while we weren't looking,
2089 * send to new authority
2090 * Otherwise we just have to return an ESTALE
Sage Weil2f2dc052009-10-06 11:31:09 -07002091 */
2092 if (result == -ESTALE) {
Greg Farnume55b71f2010-06-22 15:58:01 -07002093 dout("got ESTALE on request %llu", req->r_tid);
Sage Weil213c99e2010-08-03 10:25:11 -07002094 if (!req->r_inode) {
2095 /* do nothing; not an authority problem */
2096 } else if (req->r_direct_mode != USE_AUTH_MDS) {
Greg Farnume55b71f2010-06-22 15:58:01 -07002097 dout("not using auth, setting for that now");
2098 req->r_direct_mode = USE_AUTH_MDS;
Sage Weil2f2dc052009-10-06 11:31:09 -07002099 __do_request(mdsc, req);
2100 mutex_unlock(&mdsc->mutex);
2101 goto out;
Greg Farnume55b71f2010-06-22 15:58:01 -07002102 } else {
2103 struct ceph_inode_info *ci = ceph_inode(req->r_inode);
Sage Weil4af25fd2010-11-02 13:41:47 -07002104 struct ceph_cap *cap = NULL;
2105
2106 if (req->r_session)
2107 cap = ceph_get_cap_for_mds(ci,
2108 req->r_session->s_mds);
Greg Farnume55b71f2010-06-22 15:58:01 -07002109
2110 dout("already using auth");
2111 if ((!cap || cap != ci->i_auth_cap) ||
2112 (cap->mseq != req->r_sent_on_mseq)) {
2113 dout("but cap changed, so resending");
2114 __do_request(mdsc, req);
2115 mutex_unlock(&mdsc->mutex);
2116 goto out;
2117 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002118 }
Greg Farnume55b71f2010-06-22 15:58:01 -07002119 dout("have to return ESTALE on request %llu", req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002120 }
2121
Greg Farnume55b71f2010-06-22 15:58:01 -07002122
Sage Weil2f2dc052009-10-06 11:31:09 -07002123 if (head->safe) {
2124 req->r_got_safe = true;
2125 __unregister_request(mdsc, req);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002126 complete_all(&req->r_safe_completion);
Sage Weil2f2dc052009-10-06 11:31:09 -07002127
2128 if (req->r_got_unsafe) {
2129 /*
2130 * We already handled the unsafe response, now do the
2131 * cleanup. No need to examine the response; the MDS
2132 * doesn't include any result info in the safe
2133 * response. And even if it did, there is nothing
2134 * useful we could do with a revised return value.
2135 */
2136 dout("got safe reply %llu, mds%d\n", tid, mds);
2137 list_del_init(&req->r_unsafe_item);
2138
2139 /* last unsafe request during umount? */
Sage Weil44ca18f2010-02-15 12:08:46 -08002140 if (mdsc->stopping && !__get_oldest_req(mdsc))
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002141 complete_all(&mdsc->safe_umount_waiters);
Sage Weil2f2dc052009-10-06 11:31:09 -07002142 mutex_unlock(&mdsc->mutex);
2143 goto out;
2144 }
Sage Weile1518c72010-05-13 11:19:06 -07002145 } else {
Sage Weil2f2dc052009-10-06 11:31:09 -07002146 req->r_got_unsafe = true;
2147 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
2148 }
2149
2150 dout("handle_reply tid %lld result %d\n", tid, result);
2151 rinfo = &req->r_reply_info;
Sage Weil14303d22010-12-14 17:37:52 -08002152 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
Sage Weil2f2dc052009-10-06 11:31:09 -07002153 mutex_unlock(&mdsc->mutex);
2154
2155 mutex_lock(&session->s_mutex);
2156 if (err < 0) {
Herb Shiu25933ab2010-12-01 14:14:38 -08002157 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002158 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002159 goto out_err;
2160 }
2161
2162 /* snap trace */
2163 if (rinfo->snapblob_len) {
2164 down_write(&mdsc->snap_rwsem);
2165 ceph_update_snap_trace(mdsc, rinfo->snapblob,
2166 rinfo->snapblob + rinfo->snapblob_len,
2167 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP);
2168 downgrade_write(&mdsc->snap_rwsem);
2169 } else {
2170 down_read(&mdsc->snap_rwsem);
2171 }
2172
2173 /* insert trace into our cache */
Sage Weilb4556392010-05-13 12:01:13 -07002174 mutex_lock(&req->r_fill_mutex);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002175 err = ceph_fill_trace(mdsc->fsc->sb, req, req->r_session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002176 if (err == 0) {
Herb Shiu25933ab2010-12-01 14:14:38 -08002177 if (result == 0 && req->r_op != CEPH_MDS_OP_GETFILELOCK &&
2178 rinfo->dir_nr)
Sage Weil2f2dc052009-10-06 11:31:09 -07002179 ceph_readdir_prepopulate(req, req->r_session);
Yehuda Sadeh37151662010-06-17 16:16:12 -07002180 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
Sage Weil2f2dc052009-10-06 11:31:09 -07002181 }
Sage Weilb4556392010-05-13 12:01:13 -07002182 mutex_unlock(&req->r_fill_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002183
2184 up_read(&mdsc->snap_rwsem);
2185out_err:
Sage Weile1518c72010-05-13 11:19:06 -07002186 mutex_lock(&mdsc->mutex);
2187 if (!req->r_aborted) {
2188 if (err) {
2189 req->r_err = err;
2190 } else {
2191 req->r_reply = msg;
2192 ceph_msg_get(msg);
2193 req->r_got_result = true;
2194 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002195 } else {
Sage Weile1518c72010-05-13 11:19:06 -07002196 dout("reply arrived after request %lld was aborted\n", tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002197 }
Sage Weile1518c72010-05-13 11:19:06 -07002198 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002199
Sage Weilee6b2722010-06-10 12:55:52 -07002200 ceph_add_cap_releases(mdsc, req->r_session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002201 mutex_unlock(&session->s_mutex);
2202
2203 /* kick calling process */
2204 complete_request(mdsc, req);
2205out:
2206 ceph_mdsc_put_request(req);
2207 return;
2208}
2209
2210
2211
2212/*
2213 * handle mds notification that our request has been forwarded.
2214 */
Sage Weil2600d2d2010-02-22 15:12:16 -08002215static void handle_forward(struct ceph_mds_client *mdsc,
2216 struct ceph_mds_session *session,
2217 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002218{
2219 struct ceph_mds_request *req;
Sage Weila1ea7872010-02-23 14:02:44 -08002220 u64 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002221 u32 next_mds;
2222 u32 fwd_seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002223 int err = -EINVAL;
2224 void *p = msg->front.iov_base;
2225 void *end = p + msg->front.iov_len;
Sage Weil2f2dc052009-10-06 11:31:09 -07002226
Sage Weila1ea7872010-02-23 14:02:44 -08002227 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002228 next_mds = ceph_decode_32(&p);
2229 fwd_seq = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07002230
2231 mutex_lock(&mdsc->mutex);
2232 req = __lookup_request(mdsc, tid);
2233 if (!req) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07002234 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002235 goto out; /* dup reply? */
2236 }
2237
Sage Weil2a8e5e32010-05-28 16:43:16 -07002238 if (req->r_aborted) {
2239 dout("forward tid %llu aborted, unregistering\n", tid);
2240 __unregister_request(mdsc, req);
2241 } else if (fwd_seq <= req->r_num_fwd) {
2242 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002243 tid, next_mds, req->r_num_fwd, fwd_seq);
2244 } else {
2245 /* resend. forward race not possible; mds would drop */
Sage Weil2a8e5e32010-05-28 16:43:16 -07002246 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2247 BUG_ON(req->r_err);
2248 BUG_ON(req->r_got_result);
Sage Weil2f2dc052009-10-06 11:31:09 -07002249 req->r_num_fwd = fwd_seq;
2250 req->r_resend_mds = next_mds;
2251 put_request_session(req);
2252 __do_request(mdsc, req);
2253 }
2254 ceph_mdsc_put_request(req);
2255out:
2256 mutex_unlock(&mdsc->mutex);
2257 return;
2258
2259bad:
2260 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2261}
2262
2263/*
2264 * handle a mds session control message
2265 */
2266static void handle_session(struct ceph_mds_session *session,
2267 struct ceph_msg *msg)
2268{
2269 struct ceph_mds_client *mdsc = session->s_mdsc;
2270 u32 op;
2271 u64 seq;
Sage Weil2600d2d2010-02-22 15:12:16 -08002272 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002273 struct ceph_mds_session_head *h = msg->front.iov_base;
2274 int wake = 0;
2275
Sage Weil2f2dc052009-10-06 11:31:09 -07002276 /* decode */
2277 if (msg->front.iov_len != sizeof(*h))
2278 goto bad;
2279 op = le32_to_cpu(h->op);
2280 seq = le64_to_cpu(h->seq);
2281
2282 mutex_lock(&mdsc->mutex);
Sage Weil2600d2d2010-02-22 15:12:16 -08002283 if (op == CEPH_SESSION_CLOSE)
2284 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002285 /* FIXME: this ttl calculation is generous */
2286 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2287 mutex_unlock(&mdsc->mutex);
2288
2289 mutex_lock(&session->s_mutex);
2290
2291 dout("handle_session mds%d %s %p state %s seq %llu\n",
2292 mds, ceph_session_op_name(op), session,
2293 session_state_name(session->s_state), seq);
2294
2295 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2296 session->s_state = CEPH_MDS_SESSION_OPEN;
2297 pr_info("mds%d came back\n", session->s_mds);
2298 }
2299
2300 switch (op) {
2301 case CEPH_SESSION_OPEN:
Sage Weil29790f22010-03-18 14:45:05 -07002302 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2303 pr_info("mds%d reconnect success\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002304 session->s_state = CEPH_MDS_SESSION_OPEN;
2305 renewed_caps(mdsc, session, 0);
2306 wake = 1;
2307 if (mdsc->stopping)
2308 __close_session(mdsc, session);
2309 break;
2310
2311 case CEPH_SESSION_RENEWCAPS:
2312 if (session->s_renew_seq == seq)
2313 renewed_caps(mdsc, session, 1);
2314 break;
2315
2316 case CEPH_SESSION_CLOSE:
Sage Weil29790f22010-03-18 14:45:05 -07002317 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2318 pr_info("mds%d reconnect denied\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002319 remove_session_caps(session);
2320 wake = 1; /* for good measure */
Sage Weilf3c60c52010-08-11 14:51:23 -07002321 wake_up_all(&mdsc->session_close_wq);
Sage Weil29790f22010-03-18 14:45:05 -07002322 kick_requests(mdsc, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002323 break;
2324
2325 case CEPH_SESSION_STALE:
2326 pr_info("mds%d caps went stale, renewing\n",
2327 session->s_mds);
2328 spin_lock(&session->s_cap_lock);
2329 session->s_cap_gen++;
2330 session->s_cap_ttl = 0;
2331 spin_unlock(&session->s_cap_lock);
2332 send_renew_caps(mdsc, session);
2333 break;
2334
2335 case CEPH_SESSION_RECALL_STATE:
2336 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2337 break;
2338
2339 default:
2340 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2341 WARN_ON(1);
2342 }
2343
2344 mutex_unlock(&session->s_mutex);
2345 if (wake) {
2346 mutex_lock(&mdsc->mutex);
2347 __wake_requests(mdsc, &session->s_waiting);
2348 mutex_unlock(&mdsc->mutex);
2349 }
2350 return;
2351
2352bad:
2353 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2354 (int)msg->front.iov_len);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002355 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002356 return;
2357}
2358
2359
2360/*
2361 * called under session->mutex.
2362 */
2363static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2364 struct ceph_mds_session *session)
2365{
2366 struct ceph_mds_request *req, *nreq;
2367 int err;
2368
2369 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2370
2371 mutex_lock(&mdsc->mutex);
2372 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
2373 err = __prepare_send_request(mdsc, req, session->s_mds);
2374 if (!err) {
2375 ceph_msg_get(req->r_request);
2376 ceph_con_send(&session->s_con, req->r_request);
2377 }
2378 }
2379 mutex_unlock(&mdsc->mutex);
2380}
2381
2382/*
2383 * Encode information about a cap for a reconnect with the MDS.
2384 */
Sage Weil2f2dc052009-10-06 11:31:09 -07002385static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2386 void *arg)
2387{
Sage Weil20cb34a2010-05-12 15:21:32 -07002388 union {
2389 struct ceph_mds_cap_reconnect v2;
2390 struct ceph_mds_cap_reconnect_v1 v1;
2391 } rec;
2392 size_t reclen;
Sage Weil2f2dc052009-10-06 11:31:09 -07002393 struct ceph_inode_info *ci;
Sage Weil20cb34a2010-05-12 15:21:32 -07002394 struct ceph_reconnect_state *recon_state = arg;
2395 struct ceph_pagelist *pagelist = recon_state->pagelist;
Sage Weil2f2dc052009-10-06 11:31:09 -07002396 char *path;
2397 int pathlen, err;
2398 u64 pathbase;
2399 struct dentry *dentry;
2400
2401 ci = cap->ci;
2402
2403 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2404 inode, ceph_vinop(inode), cap, cap->cap_id,
2405 ceph_cap_string(cap->issued));
Sage Weil93cea5b2009-12-23 12:21:51 -08002406 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2407 if (err)
2408 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002409
2410 dentry = d_find_alias(inode);
2411 if (dentry) {
2412 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2413 if (IS_ERR(path)) {
2414 err = PTR_ERR(path);
Sage Weile072f8a2010-08-26 09:26:37 -07002415 goto out_dput;
Sage Weil2f2dc052009-10-06 11:31:09 -07002416 }
2417 } else {
2418 path = NULL;
2419 pathlen = 0;
2420 }
Sage Weil93cea5b2009-12-23 12:21:51 -08002421 err = ceph_pagelist_encode_string(pagelist, path, pathlen);
2422 if (err)
Sage Weile072f8a2010-08-26 09:26:37 -07002423 goto out_free;
Sage Weil2f2dc052009-10-06 11:31:09 -07002424
Sage Weilbe655592011-11-30 09:47:09 -08002425 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002426 cap->seq = 0; /* reset cap seq */
2427 cap->issue_seq = 0; /* and issue_seq */
Sage Weil20cb34a2010-05-12 15:21:32 -07002428
2429 if (recon_state->flock) {
2430 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
2431 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2432 rec.v2.issued = cpu_to_le32(cap->issued);
2433 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2434 rec.v2.pathbase = cpu_to_le64(pathbase);
2435 rec.v2.flock_len = 0;
2436 reclen = sizeof(rec.v2);
2437 } else {
2438 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
2439 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2440 rec.v1.issued = cpu_to_le32(cap->issued);
2441 rec.v1.size = cpu_to_le64(inode->i_size);
2442 ceph_encode_timespec(&rec.v1.mtime, &inode->i_mtime);
2443 ceph_encode_timespec(&rec.v1.atime, &inode->i_atime);
2444 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2445 rec.v1.pathbase = cpu_to_le64(pathbase);
2446 reclen = sizeof(rec.v1);
2447 }
Sage Weilbe655592011-11-30 09:47:09 -08002448 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002449
Greg Farnum40819f62010-08-02 15:34:23 -07002450 if (recon_state->flock) {
2451 int num_fcntl_locks, num_flock_locks;
Greg Farnumfca44512010-09-17 10:24:02 -07002452 struct ceph_pagelist_cursor trunc_point;
Greg Farnum40819f62010-08-02 15:34:23 -07002453
Greg Farnumfca44512010-09-17 10:24:02 -07002454 ceph_pagelist_set_cursor(pagelist, &trunc_point);
2455 do {
Sage Weil496e5952010-09-22 19:57:10 -07002456 lock_flocks();
Greg Farnumfca44512010-09-17 10:24:02 -07002457 ceph_count_locks(inode, &num_fcntl_locks,
2458 &num_flock_locks);
2459 rec.v2.flock_len = (2*sizeof(u32) +
2460 (num_fcntl_locks+num_flock_locks) *
2461 sizeof(struct ceph_filelock));
Sage Weil496e5952010-09-22 19:57:10 -07002462 unlock_flocks();
Greg Farnum40819f62010-08-02 15:34:23 -07002463
Greg Farnumfca44512010-09-17 10:24:02 -07002464 /* pre-alloc pagelist */
2465 ceph_pagelist_truncate(pagelist, &trunc_point);
2466 err = ceph_pagelist_append(pagelist, &rec, reclen);
2467 if (!err)
2468 err = ceph_pagelist_reserve(pagelist,
2469 rec.v2.flock_len);
2470
2471 /* encode locks */
2472 if (!err) {
Sage Weil496e5952010-09-22 19:57:10 -07002473 lock_flocks();
Greg Farnumfca44512010-09-17 10:24:02 -07002474 err = ceph_encode_locks(inode,
2475 pagelist,
2476 num_fcntl_locks,
2477 num_flock_locks);
Sage Weil496e5952010-09-22 19:57:10 -07002478 unlock_flocks();
Greg Farnumfca44512010-09-17 10:24:02 -07002479 }
2480 } while (err == -ENOSPC);
Sage Weil3612abb2010-09-07 15:59:27 -07002481 } else {
2482 err = ceph_pagelist_append(pagelist, &rec, reclen);
Greg Farnum40819f62010-08-02 15:34:23 -07002483 }
Sage Weil93cea5b2009-12-23 12:21:51 -08002484
Sage Weile072f8a2010-08-26 09:26:37 -07002485out_free:
Sage Weil2f2dc052009-10-06 11:31:09 -07002486 kfree(path);
Sage Weile072f8a2010-08-26 09:26:37 -07002487out_dput:
Sage Weil2f2dc052009-10-06 11:31:09 -07002488 dput(dentry);
Sage Weil93cea5b2009-12-23 12:21:51 -08002489 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002490}
2491
2492
2493/*
2494 * If an MDS fails and recovers, clients need to reconnect in order to
2495 * reestablish shared state. This includes all caps issued through
2496 * this session _and_ the snap_realm hierarchy. Because it's not
2497 * clear which snap realms the mds cares about, we send everything we
2498 * know about.. that ensures we'll then get any new info the
2499 * recovering MDS might have.
2500 *
2501 * This is a relatively heavyweight operation, but it's rare.
2502 *
2503 * called with mdsc->mutex held.
2504 */
Sage Weil34b6c852010-05-10 16:31:25 -07002505static void send_mds_reconnect(struct ceph_mds_client *mdsc,
2506 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07002507{
Sage Weil2f2dc052009-10-06 11:31:09 -07002508 struct ceph_msg *reply;
Sage Weila105f002010-02-15 14:37:55 -08002509 struct rb_node *p;
Sage Weil34b6c852010-05-10 16:31:25 -07002510 int mds = session->s_mds;
Sage Weil9abf82b2010-05-10 21:58:38 -07002511 int err = -ENOMEM;
Sage Weil93cea5b2009-12-23 12:21:51 -08002512 struct ceph_pagelist *pagelist;
Sage Weil20cb34a2010-05-12 15:21:32 -07002513 struct ceph_reconnect_state recon_state;
Sage Weil2f2dc052009-10-06 11:31:09 -07002514
Sage Weil34b6c852010-05-10 16:31:25 -07002515 pr_info("mds%d reconnect start\n", mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002516
Sage Weil93cea5b2009-12-23 12:21:51 -08002517 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
2518 if (!pagelist)
2519 goto fail_nopagelist;
2520 ceph_pagelist_init(pagelist);
2521
Sage Weilb61c2762011-08-09 15:03:46 -07002522 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07002523 if (!reply)
Sage Weil93cea5b2009-12-23 12:21:51 -08002524 goto fail_nomsg;
Sage Weil93cea5b2009-12-23 12:21:51 -08002525
Sage Weil34b6c852010-05-10 16:31:25 -07002526 mutex_lock(&session->s_mutex);
2527 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
2528 session->s_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002529
Sage Weil34b6c852010-05-10 16:31:25 -07002530 ceph_con_open(&session->s_con,
2531 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
Sage Weil2f2dc052009-10-06 11:31:09 -07002532
Sage Weil34b6c852010-05-10 16:31:25 -07002533 /* replay unsafe requests */
2534 replay_unsafe_requests(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002535
2536 down_read(&mdsc->snap_rwsem);
2537
Sage Weil2f2dc052009-10-06 11:31:09 -07002538 dout("session %p state %s\n", session,
2539 session_state_name(session->s_state));
2540
Sage Weile01a5942010-05-10 15:36:44 -07002541 /* drop old cap expires; we're about to reestablish that state */
2542 discard_cap_releases(mdsc, session);
2543
Sage Weil2f2dc052009-10-06 11:31:09 -07002544 /* traverse this session's caps */
Sage Weil93cea5b2009-12-23 12:21:51 -08002545 err = ceph_pagelist_encode_32(pagelist, session->s_nr_caps);
2546 if (err)
2547 goto fail;
Sage Weil20cb34a2010-05-12 15:21:32 -07002548
2549 recon_state.pagelist = pagelist;
2550 recon_state.flock = session->s_con.peer_features & CEPH_FEATURE_FLOCK;
2551 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
Sage Weil2f2dc052009-10-06 11:31:09 -07002552 if (err < 0)
Sage Weil9abf82b2010-05-10 21:58:38 -07002553 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07002554
2555 /*
2556 * snaprealms. we provide mds with the ino, seq (version), and
2557 * parent for all of our realms. If the mds has any newer info,
2558 * it will tell us.
2559 */
Sage Weila105f002010-02-15 14:37:55 -08002560 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
2561 struct ceph_snap_realm *realm =
2562 rb_entry(p, struct ceph_snap_realm, node);
Sage Weil93cea5b2009-12-23 12:21:51 -08002563 struct ceph_mds_snaprealm_reconnect sr_rec;
Sage Weil2f2dc052009-10-06 11:31:09 -07002564
2565 dout(" adding snap realm %llx seq %lld parent %llx\n",
2566 realm->ino, realm->seq, realm->parent_ino);
Sage Weil93cea5b2009-12-23 12:21:51 -08002567 sr_rec.ino = cpu_to_le64(realm->ino);
2568 sr_rec.seq = cpu_to_le64(realm->seq);
2569 sr_rec.parent = cpu_to_le64(realm->parent_ino);
2570 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
2571 if (err)
2572 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07002573 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002574
Sage Weil93cea5b2009-12-23 12:21:51 -08002575 reply->pagelist = pagelist;
Sage Weil20cb34a2010-05-12 15:21:32 -07002576 if (recon_state.flock)
2577 reply->hdr.version = cpu_to_le16(2);
Sage Weil93cea5b2009-12-23 12:21:51 -08002578 reply->hdr.data_len = cpu_to_le32(pagelist->length);
2579 reply->nr_pages = calc_pages_for(0, pagelist->length);
Sage Weil2f2dc052009-10-06 11:31:09 -07002580 ceph_con_send(&session->s_con, reply);
2581
Sage Weil9abf82b2010-05-10 21:58:38 -07002582 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002583
Sage Weil9abf82b2010-05-10 21:58:38 -07002584 mutex_lock(&mdsc->mutex);
2585 __wake_requests(mdsc, &session->s_waiting);
2586 mutex_unlock(&mdsc->mutex);
2587
Sage Weil2f2dc052009-10-06 11:31:09 -07002588 up_read(&mdsc->snap_rwsem);
Sage Weil2f2dc052009-10-06 11:31:09 -07002589 return;
2590
Sage Weil93cea5b2009-12-23 12:21:51 -08002591fail:
Sage Weil2f2dc052009-10-06 11:31:09 -07002592 ceph_msg_put(reply);
Sage Weil9abf82b2010-05-10 21:58:38 -07002593 up_read(&mdsc->snap_rwsem);
2594 mutex_unlock(&session->s_mutex);
Sage Weil93cea5b2009-12-23 12:21:51 -08002595fail_nomsg:
2596 ceph_pagelist_release(pagelist);
2597 kfree(pagelist);
2598fail_nopagelist:
Sage Weil9abf82b2010-05-10 21:58:38 -07002599 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
Sage Weil9abf82b2010-05-10 21:58:38 -07002600 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07002601}
2602
2603
2604/*
2605 * compare old and new mdsmaps, kicking requests
2606 * and closing out old connections as necessary
2607 *
2608 * called under mdsc->mutex.
2609 */
2610static void check_new_map(struct ceph_mds_client *mdsc,
2611 struct ceph_mdsmap *newmap,
2612 struct ceph_mdsmap *oldmap)
2613{
2614 int i;
2615 int oldstate, newstate;
2616 struct ceph_mds_session *s;
2617
2618 dout("check_new_map new %u old %u\n",
2619 newmap->m_epoch, oldmap->m_epoch);
2620
2621 for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
2622 if (mdsc->sessions[i] == NULL)
2623 continue;
2624 s = mdsc->sessions[i];
2625 oldstate = ceph_mdsmap_get_state(oldmap, i);
2626 newstate = ceph_mdsmap_get_state(newmap, i);
2627
Sage Weil0deb01c2010-06-17 14:19:01 -07002628 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002629 i, ceph_mds_state_name(oldstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07002630 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
Sage Weil2f2dc052009-10-06 11:31:09 -07002631 ceph_mds_state_name(newstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07002632 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
Sage Weil2f2dc052009-10-06 11:31:09 -07002633 session_state_name(s->s_state));
2634
2635 if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
2636 ceph_mdsmap_get_addr(newmap, i),
2637 sizeof(struct ceph_entity_addr))) {
2638 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
2639 /* the session never opened, just close it
2640 * out now */
2641 __wake_requests(mdsc, &s->s_waiting);
Sage Weil2600d2d2010-02-22 15:12:16 -08002642 __unregister_session(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07002643 } else {
2644 /* just close it */
2645 mutex_unlock(&mdsc->mutex);
2646 mutex_lock(&s->s_mutex);
2647 mutex_lock(&mdsc->mutex);
2648 ceph_con_close(&s->s_con);
2649 mutex_unlock(&s->s_mutex);
2650 s->s_state = CEPH_MDS_SESSION_RESTARTING;
2651 }
2652
2653 /* kick any requests waiting on the recovering mds */
Sage Weil29790f22010-03-18 14:45:05 -07002654 kick_requests(mdsc, i);
Sage Weil2f2dc052009-10-06 11:31:09 -07002655 } else if (oldstate == newstate) {
2656 continue; /* nothing new with this mds */
2657 }
2658
2659 /*
2660 * send reconnect?
2661 */
2662 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
Sage Weil34b6c852010-05-10 16:31:25 -07002663 newstate >= CEPH_MDS_STATE_RECONNECT) {
2664 mutex_unlock(&mdsc->mutex);
2665 send_mds_reconnect(mdsc, s);
2666 mutex_lock(&mdsc->mutex);
2667 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002668
2669 /*
Sage Weil29790f22010-03-18 14:45:05 -07002670 * kick request on any mds that has gone active.
Sage Weil2f2dc052009-10-06 11:31:09 -07002671 */
2672 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
2673 newstate >= CEPH_MDS_STATE_ACTIVE) {
Sage Weil29790f22010-03-18 14:45:05 -07002674 if (oldstate != CEPH_MDS_STATE_CREATING &&
2675 oldstate != CEPH_MDS_STATE_STARTING)
2676 pr_info("mds%d recovery completed\n", s->s_mds);
2677 kick_requests(mdsc, i);
Sage Weil2f2dc052009-10-06 11:31:09 -07002678 ceph_kick_flushing_caps(mdsc, s);
Sage Weil0dc25702009-11-20 13:43:45 -08002679 wake_up_session_caps(s, 1);
Sage Weil2f2dc052009-10-06 11:31:09 -07002680 }
2681 }
Sage Weilcb170a22010-06-21 13:38:35 -07002682
2683 for (i = 0; i < newmap->m_max_mds && i < mdsc->max_sessions; i++) {
2684 s = mdsc->sessions[i];
2685 if (!s)
2686 continue;
2687 if (!ceph_mdsmap_is_laggy(newmap, i))
2688 continue;
2689 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2690 s->s_state == CEPH_MDS_SESSION_HUNG ||
2691 s->s_state == CEPH_MDS_SESSION_CLOSING) {
2692 dout(" connecting to export targets of laggy mds%d\n",
2693 i);
2694 __open_export_target_sessions(mdsc, s);
2695 }
2696 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002697}
2698
2699
2700
2701/*
2702 * leases
2703 */
2704
2705/*
2706 * caller must hold session s_mutex, dentry->d_lock
2707 */
2708void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
2709{
2710 struct ceph_dentry_info *di = ceph_dentry(dentry);
2711
2712 ceph_put_mds_session(di->lease_session);
2713 di->lease_session = NULL;
2714}
2715
Sage Weil2600d2d2010-02-22 15:12:16 -08002716static void handle_lease(struct ceph_mds_client *mdsc,
2717 struct ceph_mds_session *session,
2718 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002719{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002720 struct super_block *sb = mdsc->fsc->sb;
Sage Weil2f2dc052009-10-06 11:31:09 -07002721 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07002722 struct dentry *parent, *dentry;
2723 struct ceph_dentry_info *di;
Sage Weil2600d2d2010-02-22 15:12:16 -08002724 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002725 struct ceph_mds_lease *h = msg->front.iov_base;
Sage Weil1e5ea232010-06-04 10:05:40 -07002726 u32 seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002727 struct ceph_vino vino;
Sage Weil2f2dc052009-10-06 11:31:09 -07002728 struct qstr dname;
2729 int release = 0;
2730
Sage Weil2f2dc052009-10-06 11:31:09 -07002731 dout("handle_lease from mds%d\n", mds);
2732
2733 /* decode */
2734 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
2735 goto bad;
2736 vino.ino = le64_to_cpu(h->ino);
2737 vino.snap = CEPH_NOSNAP;
Sage Weil1e5ea232010-06-04 10:05:40 -07002738 seq = le32_to_cpu(h->seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002739 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
2740 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
2741 if (dname.len != get_unaligned_le32(h+1))
2742 goto bad;
2743
Sage Weil2f2dc052009-10-06 11:31:09 -07002744 mutex_lock(&session->s_mutex);
2745 session->s_seq++;
2746
2747 /* lookup inode */
2748 inode = ceph_find_inode(sb, vino);
Sage Weil2f90b852011-07-26 11:28:25 -07002749 dout("handle_lease %s, ino %llx %p %.*s\n",
2750 ceph_lease_op_name(h->action), vino.ino, inode,
Sage Weil1e5ea232010-06-04 10:05:40 -07002751 dname.len, dname.name);
Sage Weil2f2dc052009-10-06 11:31:09 -07002752 if (inode == NULL) {
2753 dout("handle_lease no inode %llx\n", vino.ino);
2754 goto release;
2755 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002756
2757 /* dentry */
2758 parent = d_find_alias(inode);
2759 if (!parent) {
2760 dout("no parent dentry on inode %p\n", inode);
2761 WARN_ON(1);
2762 goto release; /* hrm... */
2763 }
2764 dname.hash = full_name_hash(dname.name, dname.len);
2765 dentry = d_lookup(parent, &dname);
2766 dput(parent);
2767 if (!dentry)
2768 goto release;
2769
2770 spin_lock(&dentry->d_lock);
2771 di = ceph_dentry(dentry);
2772 switch (h->action) {
2773 case CEPH_MDS_LEASE_REVOKE:
2774 if (di && di->lease_session == session) {
Sage Weil1e5ea232010-06-04 10:05:40 -07002775 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
2776 h->seq = cpu_to_le32(di->lease_seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002777 __ceph_mdsc_drop_dentry_lease(dentry);
2778 }
2779 release = 1;
2780 break;
2781
2782 case CEPH_MDS_LEASE_RENEW:
2783 if (di && di->lease_session == session &&
2784 di->lease_gen == session->s_cap_gen &&
2785 di->lease_renew_from &&
2786 di->lease_renew_after == 0) {
2787 unsigned long duration =
2788 le32_to_cpu(h->duration_ms) * HZ / 1000;
2789
Sage Weil1e5ea232010-06-04 10:05:40 -07002790 di->lease_seq = seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002791 dentry->d_time = di->lease_renew_from + duration;
2792 di->lease_renew_after = di->lease_renew_from +
2793 (duration >> 1);
2794 di->lease_renew_from = 0;
2795 }
2796 break;
2797 }
2798 spin_unlock(&dentry->d_lock);
2799 dput(dentry);
2800
2801 if (!release)
2802 goto out;
2803
2804release:
2805 /* let's just reuse the same message */
2806 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
2807 ceph_msg_get(msg);
2808 ceph_con_send(&session->s_con, msg);
2809
2810out:
2811 iput(inode);
2812 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002813 return;
2814
2815bad:
2816 pr_err("corrupt lease message\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002817 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002818}
2819
2820void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
2821 struct inode *inode,
2822 struct dentry *dentry, char action,
2823 u32 seq)
2824{
2825 struct ceph_msg *msg;
2826 struct ceph_mds_lease *lease;
2827 int len = sizeof(*lease) + sizeof(u32);
2828 int dnamelen = 0;
2829
2830 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
2831 inode, dentry, ceph_lease_op_name(action), session->s_mds);
2832 dnamelen = dentry->d_name.len;
2833 len += dnamelen;
2834
Sage Weilb61c2762011-08-09 15:03:46 -07002835 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07002836 if (!msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002837 return;
2838 lease = msg->front.iov_base;
2839 lease->action = action;
Sage Weil2f2dc052009-10-06 11:31:09 -07002840 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
2841 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
2842 lease->seq = cpu_to_le32(seq);
2843 put_unaligned_le32(dnamelen, lease + 1);
2844 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
2845
2846 /*
2847 * if this is a preemptive lease RELEASE, no need to
2848 * flush request stream, since the actual request will
2849 * soon follow.
2850 */
2851 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
2852
2853 ceph_con_send(&session->s_con, msg);
2854}
2855
2856/*
2857 * Preemptively release a lease we expect to invalidate anyway.
2858 * Pass @inode always, @dentry is optional.
2859 */
2860void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
Sage Weil2f90b852011-07-26 11:28:25 -07002861 struct dentry *dentry)
Sage Weil2f2dc052009-10-06 11:31:09 -07002862{
2863 struct ceph_dentry_info *di;
2864 struct ceph_mds_session *session;
2865 u32 seq;
2866
2867 BUG_ON(inode == NULL);
2868 BUG_ON(dentry == NULL);
Sage Weil2f2dc052009-10-06 11:31:09 -07002869
2870 /* is dentry lease valid? */
2871 spin_lock(&dentry->d_lock);
2872 di = ceph_dentry(dentry);
2873 if (!di || !di->lease_session ||
2874 di->lease_session->s_mds < 0 ||
2875 di->lease_gen != di->lease_session->s_cap_gen ||
2876 !time_before(jiffies, dentry->d_time)) {
2877 dout("lease_release inode %p dentry %p -- "
Sage Weil2f90b852011-07-26 11:28:25 -07002878 "no lease\n",
2879 inode, dentry);
Sage Weil2f2dc052009-10-06 11:31:09 -07002880 spin_unlock(&dentry->d_lock);
2881 return;
2882 }
2883
2884 /* we do have a lease on this dentry; note mds and seq */
2885 session = ceph_get_mds_session(di->lease_session);
2886 seq = di->lease_seq;
2887 __ceph_mdsc_drop_dentry_lease(dentry);
2888 spin_unlock(&dentry->d_lock);
2889
Sage Weil2f90b852011-07-26 11:28:25 -07002890 dout("lease_release inode %p dentry %p to mds%d\n",
2891 inode, dentry, session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002892 ceph_mdsc_lease_send_msg(session, inode, dentry,
2893 CEPH_MDS_LEASE_RELEASE, seq);
2894 ceph_put_mds_session(session);
2895}
2896
2897/*
2898 * drop all leases (and dentry refs) in preparation for umount
2899 */
2900static void drop_leases(struct ceph_mds_client *mdsc)
2901{
2902 int i;
2903
2904 dout("drop_leases\n");
2905 mutex_lock(&mdsc->mutex);
2906 for (i = 0; i < mdsc->max_sessions; i++) {
2907 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2908 if (!s)
2909 continue;
2910 mutex_unlock(&mdsc->mutex);
2911 mutex_lock(&s->s_mutex);
2912 mutex_unlock(&s->s_mutex);
2913 ceph_put_mds_session(s);
2914 mutex_lock(&mdsc->mutex);
2915 }
2916 mutex_unlock(&mdsc->mutex);
2917}
2918
2919
2920
2921/*
2922 * delayed work -- periodically trim expired leases, renew caps with mds
2923 */
2924static void schedule_delayed(struct ceph_mds_client *mdsc)
2925{
2926 int delay = 5;
2927 unsigned hz = round_jiffies_relative(HZ * delay);
2928 schedule_delayed_work(&mdsc->delayed_work, hz);
2929}
2930
2931static void delayed_work(struct work_struct *work)
2932{
2933 int i;
2934 struct ceph_mds_client *mdsc =
2935 container_of(work, struct ceph_mds_client, delayed_work.work);
2936 int renew_interval;
2937 int renew_caps;
2938
2939 dout("mdsc delayed_work\n");
Sage Weilafcdaea2009-10-14 14:27:38 -07002940 ceph_check_delayed_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07002941
2942 mutex_lock(&mdsc->mutex);
2943 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
2944 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
2945 mdsc->last_renew_caps);
2946 if (renew_caps)
2947 mdsc->last_renew_caps = jiffies;
2948
2949 for (i = 0; i < mdsc->max_sessions; i++) {
2950 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2951 if (s == NULL)
2952 continue;
2953 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
2954 dout("resending session close request for mds%d\n",
2955 s->s_mds);
2956 request_close_session(mdsc, s);
2957 ceph_put_mds_session(s);
2958 continue;
2959 }
2960 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
2961 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
2962 s->s_state = CEPH_MDS_SESSION_HUNG;
2963 pr_info("mds%d hung\n", s->s_mds);
2964 }
2965 }
2966 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
2967 /* this mds is failed or recovering, just wait */
2968 ceph_put_mds_session(s);
2969 continue;
2970 }
2971 mutex_unlock(&mdsc->mutex);
2972
2973 mutex_lock(&s->s_mutex);
2974 if (renew_caps)
2975 send_renew_caps(mdsc, s);
2976 else
2977 ceph_con_keepalive(&s->s_con);
Sage Weilee6b2722010-06-10 12:55:52 -07002978 ceph_add_cap_releases(mdsc, s);
Sage Weilaab53dd2010-03-17 16:30:21 -07002979 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2980 s->s_state == CEPH_MDS_SESSION_HUNG)
Sage Weil3d7ded42010-06-09 16:47:10 -07002981 ceph_send_cap_releases(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07002982 mutex_unlock(&s->s_mutex);
2983 ceph_put_mds_session(s);
2984
2985 mutex_lock(&mdsc->mutex);
2986 }
2987 mutex_unlock(&mdsc->mutex);
2988
2989 schedule_delayed(mdsc);
2990}
2991
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002992int ceph_mdsc_init(struct ceph_fs_client *fsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07002993
Sage Weil2f2dc052009-10-06 11:31:09 -07002994{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002995 struct ceph_mds_client *mdsc;
2996
2997 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
2998 if (!mdsc)
2999 return -ENOMEM;
3000 mdsc->fsc = fsc;
3001 fsc->mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07003002 mutex_init(&mdsc->mutex);
3003 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08003004 if (mdsc->mdsmap == NULL)
3005 return -ENOMEM;
3006
Sage Weil2f2dc052009-10-06 11:31:09 -07003007 init_completion(&mdsc->safe_umount_waiters);
Sage Weilf3c60c52010-08-11 14:51:23 -07003008 init_waitqueue_head(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003009 INIT_LIST_HEAD(&mdsc->waiting_for_map);
3010 mdsc->sessions = NULL;
3011 mdsc->max_sessions = 0;
3012 mdsc->stopping = 0;
3013 init_rwsem(&mdsc->snap_rwsem);
Sage Weila105f002010-02-15 14:37:55 -08003014 mdsc->snap_realms = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07003015 INIT_LIST_HEAD(&mdsc->snap_empty);
3016 spin_lock_init(&mdsc->snap_empty_lock);
3017 mdsc->last_tid = 0;
Sage Weil44ca18f2010-02-15 12:08:46 -08003018 mdsc->request_tree = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07003019 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
3020 mdsc->last_renew_caps = jiffies;
3021 INIT_LIST_HEAD(&mdsc->cap_delay_list);
3022 spin_lock_init(&mdsc->cap_delay_lock);
3023 INIT_LIST_HEAD(&mdsc->snap_flush_list);
3024 spin_lock_init(&mdsc->snap_flush_lock);
3025 mdsc->cap_flush_seq = 0;
3026 INIT_LIST_HEAD(&mdsc->cap_dirty);
Sage Weildb354052011-05-24 11:46:31 -07003027 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
Sage Weil2f2dc052009-10-06 11:31:09 -07003028 mdsc->num_cap_flushing = 0;
3029 spin_lock_init(&mdsc->cap_dirty_lock);
3030 init_waitqueue_head(&mdsc->cap_flushing_wq);
3031 spin_lock_init(&mdsc->dentry_lru_lock);
3032 INIT_LIST_HEAD(&mdsc->dentry_lru);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08003033
Yehuda Sadeh37151662010-06-17 16:16:12 -07003034 ceph_caps_init(mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003035 ceph_adjust_min_caps(mdsc, fsc->min_caps);
Yehuda Sadeh37151662010-06-17 16:16:12 -07003036
Sage Weil5f44f142009-11-18 14:52:18 -08003037 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003038}
3039
3040/*
3041 * Wait for safe replies on open mds requests. If we time out, drop
3042 * all requests from the tree to avoid dangling dentry refs.
3043 */
3044static void wait_requests(struct ceph_mds_client *mdsc)
3045{
3046 struct ceph_mds_request *req;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003047 struct ceph_fs_client *fsc = mdsc->fsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07003048
3049 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003050 if (__get_oldest_req(mdsc)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003051 mutex_unlock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003052
Sage Weil2f2dc052009-10-06 11:31:09 -07003053 dout("wait_requests waiting for requests\n");
3054 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003055 fsc->client->options->mount_timeout * HZ);
Sage Weil2f2dc052009-10-06 11:31:09 -07003056
3057 /* tear down remaining requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08003058 mutex_lock(&mdsc->mutex);
3059 while ((req = __get_oldest_req(mdsc))) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003060 dout("wait_requests timed out on tid %llu\n",
3061 req->r_tid);
Sage Weil44ca18f2010-02-15 12:08:46 -08003062 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07003063 }
3064 }
3065 mutex_unlock(&mdsc->mutex);
3066 dout("wait_requests done\n");
3067}
3068
3069/*
3070 * called before mount is ro, and before dentries are torn down.
3071 * (hmm, does this still race with new lookups?)
3072 */
3073void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
3074{
3075 dout("pre_umount\n");
3076 mdsc->stopping = 1;
3077
3078 drop_leases(mdsc);
Sage Weilafcdaea2009-10-14 14:27:38 -07003079 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003080 wait_requests(mdsc);
Sage Weil17c688c2010-06-21 16:12:26 -07003081
3082 /*
3083 * wait for reply handlers to drop their request refs and
3084 * their inode/dcache refs
3085 */
3086 ceph_msgr_flush();
Sage Weil2f2dc052009-10-06 11:31:09 -07003087}
3088
3089/*
3090 * wait for all write mds requests to flush.
3091 */
3092static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
3093{
Sage Weil80fc7312010-03-16 15:28:54 -07003094 struct ceph_mds_request *req = NULL, *nextreq;
Sage Weil44ca18f2010-02-15 12:08:46 -08003095 struct rb_node *n;
Sage Weil2f2dc052009-10-06 11:31:09 -07003096
3097 mutex_lock(&mdsc->mutex);
3098 dout("wait_unsafe_requests want %lld\n", want_tid);
Sage Weil80fc7312010-03-16 15:28:54 -07003099restart:
Sage Weil44ca18f2010-02-15 12:08:46 -08003100 req = __get_oldest_req(mdsc);
3101 while (req && req->r_tid <= want_tid) {
Sage Weil80fc7312010-03-16 15:28:54 -07003102 /* find next request */
3103 n = rb_next(&req->r_node);
3104 if (n)
3105 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
3106 else
3107 nextreq = NULL;
Sage Weil44ca18f2010-02-15 12:08:46 -08003108 if ((req->r_op & CEPH_MDS_OP_WRITE)) {
3109 /* write op */
3110 ceph_mdsc_get_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07003111 if (nextreq)
3112 ceph_mdsc_get_request(nextreq);
Sage Weil44ca18f2010-02-15 12:08:46 -08003113 mutex_unlock(&mdsc->mutex);
3114 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3115 req->r_tid, want_tid);
3116 wait_for_completion(&req->r_safe_completion);
3117 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003118 ceph_mdsc_put_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07003119 if (!nextreq)
3120 break; /* next dne before, so we're done! */
3121 if (RB_EMPTY_NODE(&nextreq->r_node)) {
3122 /* next request was removed from tree */
3123 ceph_mdsc_put_request(nextreq);
3124 goto restart;
3125 }
3126 ceph_mdsc_put_request(nextreq); /* won't go away */
Sage Weil44ca18f2010-02-15 12:08:46 -08003127 }
Sage Weil80fc7312010-03-16 15:28:54 -07003128 req = nextreq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003129 }
3130 mutex_unlock(&mdsc->mutex);
3131 dout("wait_unsafe_requests done\n");
3132}
3133
3134void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
3135{
3136 u64 want_tid, want_flush;
3137
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003138 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
Sage Weil56b7cf92010-05-03 15:22:00 -07003139 return;
3140
Sage Weil2f2dc052009-10-06 11:31:09 -07003141 dout("sync\n");
3142 mutex_lock(&mdsc->mutex);
3143 want_tid = mdsc->last_tid;
3144 want_flush = mdsc->cap_flush_seq;
3145 mutex_unlock(&mdsc->mutex);
3146 dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
3147
Sage Weilafcdaea2009-10-14 14:27:38 -07003148 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003149
3150 wait_unsafe_requests(mdsc, want_tid);
3151 wait_event(mdsc->cap_flushing_wq, check_cap_flush(mdsc, want_flush));
3152}
3153
Sage Weilf3c60c52010-08-11 14:51:23 -07003154/*
3155 * true if all sessions are closed, or we force unmount
3156 */
H Hartley Sweeten7fd7d102011-09-23 13:22:11 -07003157static bool done_closing_sessions(struct ceph_mds_client *mdsc)
Sage Weilf3c60c52010-08-11 14:51:23 -07003158{
3159 int i, n = 0;
3160
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003161 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
Sage Weilf3c60c52010-08-11 14:51:23 -07003162 return true;
3163
3164 mutex_lock(&mdsc->mutex);
3165 for (i = 0; i < mdsc->max_sessions; i++)
3166 if (mdsc->sessions[i])
3167 n++;
3168 mutex_unlock(&mdsc->mutex);
3169 return n == 0;
3170}
Sage Weil2f2dc052009-10-06 11:31:09 -07003171
3172/*
3173 * called after sb is ro.
3174 */
3175void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
3176{
3177 struct ceph_mds_session *session;
3178 int i;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003179 struct ceph_fs_client *fsc = mdsc->fsc;
3180 unsigned long timeout = fsc->client->options->mount_timeout * HZ;
Sage Weil2f2dc052009-10-06 11:31:09 -07003181
3182 dout("close_sessions\n");
3183
Sage Weil2f2dc052009-10-06 11:31:09 -07003184 /* close sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07003185 mutex_lock(&mdsc->mutex);
3186 for (i = 0; i < mdsc->max_sessions; i++) {
3187 session = __ceph_lookup_mds_session(mdsc, i);
3188 if (!session)
3189 continue;
Sage Weil2f2dc052009-10-06 11:31:09 -07003190 mutex_unlock(&mdsc->mutex);
Sage Weilf3c60c52010-08-11 14:51:23 -07003191 mutex_lock(&session->s_mutex);
3192 __close_session(mdsc, session);
3193 mutex_unlock(&session->s_mutex);
3194 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003195 mutex_lock(&mdsc->mutex);
3196 }
Sage Weilf3c60c52010-08-11 14:51:23 -07003197 mutex_unlock(&mdsc->mutex);
3198
3199 dout("waiting for sessions to close\n");
3200 wait_event_timeout(mdsc->session_close_wq, done_closing_sessions(mdsc),
3201 timeout);
Sage Weil2f2dc052009-10-06 11:31:09 -07003202
3203 /* tear down remaining sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07003204 mutex_lock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003205 for (i = 0; i < mdsc->max_sessions; i++) {
3206 if (mdsc->sessions[i]) {
3207 session = get_session(mdsc->sessions[i]);
Sage Weil2600d2d2010-02-22 15:12:16 -08003208 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003209 mutex_unlock(&mdsc->mutex);
3210 mutex_lock(&session->s_mutex);
3211 remove_session_caps(session);
3212 mutex_unlock(&session->s_mutex);
3213 ceph_put_mds_session(session);
3214 mutex_lock(&mdsc->mutex);
3215 }
3216 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003217 WARN_ON(!list_empty(&mdsc->cap_delay_list));
Sage Weil2f2dc052009-10-06 11:31:09 -07003218 mutex_unlock(&mdsc->mutex);
3219
3220 ceph_cleanup_empty_realms(mdsc);
3221
3222 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3223
3224 dout("stopped\n");
3225}
3226
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003227static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07003228{
3229 dout("stop\n");
3230 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3231 if (mdsc->mdsmap)
3232 ceph_mdsmap_destroy(mdsc->mdsmap);
3233 kfree(mdsc->sessions);
Yehuda Sadeh37151662010-06-17 16:16:12 -07003234 ceph_caps_finalize(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003235}
3236
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003237void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
3238{
3239 struct ceph_mds_client *mdsc = fsc->mdsc;
3240
Sage Weilef550f62011-03-25 13:27:48 -07003241 dout("mdsc_destroy %p\n", mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003242 ceph_mdsc_stop(mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07003243
3244 /* flush out any connection work with references to us */
3245 ceph_msgr_flush();
3246
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003247 fsc->mdsc = NULL;
3248 kfree(mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07003249 dout("mdsc_destroy %p done\n", mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003250}
3251
Sage Weil2f2dc052009-10-06 11:31:09 -07003252
3253/*
3254 * handle mds map update.
3255 */
3256void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3257{
3258 u32 epoch;
3259 u32 maplen;
3260 void *p = msg->front.iov_base;
3261 void *end = p + msg->front.iov_len;
3262 struct ceph_mdsmap *newmap, *oldmap;
3263 struct ceph_fsid fsid;
3264 int err = -EINVAL;
3265
3266 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
3267 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003268 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
Sage Weil07433042009-11-18 16:50:41 -08003269 return;
Sage Weilc89136e2009-10-14 09:59:09 -07003270 epoch = ceph_decode_32(&p);
3271 maplen = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07003272 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
3273
3274 /* do we need it? */
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003275 ceph_monc_got_mdsmap(&mdsc->fsc->client->monc, epoch);
Sage Weil2f2dc052009-10-06 11:31:09 -07003276 mutex_lock(&mdsc->mutex);
3277 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
3278 dout("handle_map epoch %u <= our %u\n",
3279 epoch, mdsc->mdsmap->m_epoch);
3280 mutex_unlock(&mdsc->mutex);
3281 return;
3282 }
3283
3284 newmap = ceph_mdsmap_decode(&p, end);
3285 if (IS_ERR(newmap)) {
3286 err = PTR_ERR(newmap);
3287 goto bad_unlock;
3288 }
3289
3290 /* swap into place */
3291 if (mdsc->mdsmap) {
3292 oldmap = mdsc->mdsmap;
3293 mdsc->mdsmap = newmap;
3294 check_new_map(mdsc, newmap, oldmap);
3295 ceph_mdsmap_destroy(oldmap);
3296 } else {
3297 mdsc->mdsmap = newmap; /* first mds map */
3298 }
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003299 mdsc->fsc->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
Sage Weil2f2dc052009-10-06 11:31:09 -07003300
3301 __wake_requests(mdsc, &mdsc->waiting_for_map);
3302
3303 mutex_unlock(&mdsc->mutex);
3304 schedule_delayed(mdsc);
3305 return;
3306
3307bad_unlock:
3308 mutex_unlock(&mdsc->mutex);
3309bad:
3310 pr_err("error decoding mdsmap %d\n", err);
3311 return;
3312}
3313
3314static struct ceph_connection *con_get(struct ceph_connection *con)
3315{
3316 struct ceph_mds_session *s = con->private;
3317
3318 if (get_session(s)) {
Sage Weil2600d2d2010-02-22 15:12:16 -08003319 dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -07003320 return con;
3321 }
3322 dout("mdsc con_get %p FAIL\n", s);
3323 return NULL;
3324}
3325
3326static void con_put(struct ceph_connection *con)
3327{
3328 struct ceph_mds_session *s = con->private;
3329
Henry C Chang7d8e18a2011-05-11 10:29:52 +00003330 dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref) - 1);
Sage Weil2f2dc052009-10-06 11:31:09 -07003331 ceph_put_mds_session(s);
3332}
3333
3334/*
3335 * if the client is unresponsive for long enough, the mds will kill
3336 * the session entirely.
3337 */
3338static void peer_reset(struct ceph_connection *con)
3339{
3340 struct ceph_mds_session *s = con->private;
Sage Weil7e70f0e2010-03-18 13:59:12 -07003341 struct ceph_mds_client *mdsc = s->s_mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07003342
Sage Weil7e70f0e2010-03-18 13:59:12 -07003343 pr_warning("mds%d closed our session\n", s->s_mds);
3344 send_mds_reconnect(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07003345}
3346
3347static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
3348{
3349 struct ceph_mds_session *s = con->private;
3350 struct ceph_mds_client *mdsc = s->s_mdsc;
3351 int type = le16_to_cpu(msg->hdr.type);
3352
Sage Weil2600d2d2010-02-22 15:12:16 -08003353 mutex_lock(&mdsc->mutex);
3354 if (__verify_registered_session(mdsc, s) < 0) {
3355 mutex_unlock(&mdsc->mutex);
3356 goto out;
3357 }
3358 mutex_unlock(&mdsc->mutex);
3359
Sage Weil2f2dc052009-10-06 11:31:09 -07003360 switch (type) {
3361 case CEPH_MSG_MDS_MAP:
3362 ceph_mdsc_handle_map(mdsc, msg);
3363 break;
3364 case CEPH_MSG_CLIENT_SESSION:
3365 handle_session(s, msg);
3366 break;
3367 case CEPH_MSG_CLIENT_REPLY:
3368 handle_reply(s, msg);
3369 break;
3370 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
Sage Weil2600d2d2010-02-22 15:12:16 -08003371 handle_forward(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003372 break;
3373 case CEPH_MSG_CLIENT_CAPS:
3374 ceph_handle_caps(s, msg);
3375 break;
3376 case CEPH_MSG_CLIENT_SNAP:
Sage Weil2600d2d2010-02-22 15:12:16 -08003377 ceph_handle_snap(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003378 break;
3379 case CEPH_MSG_CLIENT_LEASE:
Sage Weil2600d2d2010-02-22 15:12:16 -08003380 handle_lease(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003381 break;
3382
3383 default:
3384 pr_err("received unknown message type %d %s\n", type,
3385 ceph_msg_type_name(type));
3386 }
Sage Weil2600d2d2010-02-22 15:12:16 -08003387out:
Sage Weil2f2dc052009-10-06 11:31:09 -07003388 ceph_msg_put(msg);
3389}
3390
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003391/*
3392 * authentication
3393 */
3394static int get_authorizer(struct ceph_connection *con,
3395 void **buf, int *len, int *proto,
3396 void **reply_buf, int *reply_len, int force_new)
3397{
3398 struct ceph_mds_session *s = con->private;
3399 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003400 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003401 int ret = 0;
3402
3403 if (force_new && s->s_authorizer) {
3404 ac->ops->destroy_authorizer(ac, s->s_authorizer);
3405 s->s_authorizer = NULL;
3406 }
3407 if (s->s_authorizer == NULL) {
3408 if (ac->ops->create_authorizer) {
3409 ret = ac->ops->create_authorizer(
3410 ac, CEPH_ENTITY_TYPE_MDS,
3411 &s->s_authorizer,
3412 &s->s_authorizer_buf,
3413 &s->s_authorizer_buf_len,
3414 &s->s_authorizer_reply_buf,
3415 &s->s_authorizer_reply_buf_len);
3416 if (ret)
3417 return ret;
3418 }
3419 }
3420
3421 *proto = ac->protocol;
3422 *buf = s->s_authorizer_buf;
3423 *len = s->s_authorizer_buf_len;
3424 *reply_buf = s->s_authorizer_reply_buf;
3425 *reply_len = s->s_authorizer_reply_buf_len;
3426 return 0;
3427}
3428
3429
3430static int verify_authorizer_reply(struct ceph_connection *con, int len)
3431{
3432 struct ceph_mds_session *s = con->private;
3433 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003434 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003435
3436 return ac->ops->verify_authorizer_reply(ac, s->s_authorizer, len);
3437}
3438
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003439static int invalidate_authorizer(struct ceph_connection *con)
3440{
3441 struct ceph_mds_session *s = con->private;
3442 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003443 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003444
3445 if (ac->ops->invalidate_authorizer)
3446 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
3447
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003448 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003449}
3450
Tobias Klauser9e327892010-05-20 10:40:19 +02003451static const struct ceph_connection_operations mds_con_ops = {
Sage Weil2f2dc052009-10-06 11:31:09 -07003452 .get = con_get,
3453 .put = con_put,
3454 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003455 .get_authorizer = get_authorizer,
3456 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003457 .invalidate_authorizer = invalidate_authorizer,
Sage Weil2f2dc052009-10-06 11:31:09 -07003458 .peer_reset = peer_reset,
Sage Weil2f2dc052009-10-06 11:31:09 -07003459};
3460
Sage Weil2f2dc052009-10-06 11:31:09 -07003461/* eof */