blob: 8a09cd5a659efa2e1c045ef2d3c9d6e86c10eb42 [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weil2f2dc052009-10-06 11:31:09 -07002
Sage Weil496e5952010-09-22 19:57:10 -07003#include <linux/fs.h>
Sage Weil2f2dc052009-10-06 11:31:09 -07004#include <linux/wait.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09005#include <linux/slab.h>
Sage Weil2f2dc052009-10-06 11:31:09 -07006#include <linux/sched.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07007#include <linux/debugfs.h>
8#include <linux/seq_file.h>
Sage Weil2f2dc052009-10-06 11:31:09 -07009
Sage Weil2f2dc052009-10-06 11:31:09 -070010#include "super.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070011#include "mds_client.h"
12
13#include <linux/ceph/messenger.h>
14#include <linux/ceph/decode.h>
15#include <linux/ceph/pagelist.h>
16#include <linux/ceph/auth.h>
17#include <linux/ceph/debugfs.h>
Sage Weil2f2dc052009-10-06 11:31:09 -070018
19/*
20 * A cluster of MDS (metadata server) daemons is responsible for
21 * managing the file system namespace (the directory hierarchy and
22 * inodes) and for coordinating shared access to storage. Metadata is
23 * partitioning hierarchically across a number of servers, and that
24 * partition varies over time as the cluster adjusts the distribution
25 * in order to balance load.
26 *
27 * The MDS client is primarily responsible to managing synchronous
28 * metadata requests for operations like open, unlink, and so forth.
29 * If there is a MDS failure, we find out about it when we (possibly
30 * request and) receive a new MDS map, and can resubmit affected
31 * requests.
32 *
33 * For the most part, though, we take advantage of a lossless
34 * communications channel to the MDS, and do not need to worry about
35 * timing out or resubmitting requests.
36 *
37 * We maintain a stateful "session" with each MDS we interact with.
38 * Within each session, we sent periodic heartbeat messages to ensure
39 * any capabilities or leases we have been issues remain valid. If
40 * the session times out and goes stale, our leases and capabilities
41 * are no longer valid.
42 */
43
Sage Weil20cb34a2010-05-12 15:21:32 -070044struct ceph_reconnect_state {
45 struct ceph_pagelist *pagelist;
46 bool flock;
47};
48
Sage Weil2f2dc052009-10-06 11:31:09 -070049static void __wake_requests(struct ceph_mds_client *mdsc,
50 struct list_head *head);
51
Tobias Klauser9e327892010-05-20 10:40:19 +020052static const struct ceph_connection_operations mds_con_ops;
Sage Weil2f2dc052009-10-06 11:31:09 -070053
54
55/*
56 * mds reply parsing
57 */
58
59/*
60 * parse individual inode info
61 */
62static int parse_reply_info_in(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -080063 struct ceph_mds_reply_info_in *info,
64 int features)
Sage Weil2f2dc052009-10-06 11:31:09 -070065{
66 int err = -EIO;
67
68 info->in = *p;
69 *p += sizeof(struct ceph_mds_reply_inode) +
70 sizeof(*info->in->fragtree.splits) *
71 le32_to_cpu(info->in->fragtree.nsplits);
72
73 ceph_decode_32_safe(p, end, info->symlink_len, bad);
74 ceph_decode_need(p, end, info->symlink_len, bad);
75 info->symlink = *p;
76 *p += info->symlink_len;
77
Sage Weil14303d22010-12-14 17:37:52 -080078 if (features & CEPH_FEATURE_DIRLAYOUTHASH)
79 ceph_decode_copy_safe(p, end, &info->dir_layout,
80 sizeof(info->dir_layout), bad);
81 else
82 memset(&info->dir_layout, 0, sizeof(info->dir_layout));
83
Sage Weil2f2dc052009-10-06 11:31:09 -070084 ceph_decode_32_safe(p, end, info->xattr_len, bad);
85 ceph_decode_need(p, end, info->xattr_len, bad);
86 info->xattr_data = *p;
87 *p += info->xattr_len;
88 return 0;
89bad:
90 return err;
91}
92
93/*
94 * parse a normal reply, which may contain a (dir+)dentry and/or a
95 * target inode.
96 */
97static int parse_reply_info_trace(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -080098 struct ceph_mds_reply_info_parsed *info,
99 int features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700100{
101 int err;
102
103 if (info->head->is_dentry) {
Sage Weil14303d22010-12-14 17:37:52 -0800104 err = parse_reply_info_in(p, end, &info->diri, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700105 if (err < 0)
106 goto out_bad;
107
108 if (unlikely(*p + sizeof(*info->dirfrag) > end))
109 goto bad;
110 info->dirfrag = *p;
111 *p += sizeof(*info->dirfrag) +
112 sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
113 if (unlikely(*p > end))
114 goto bad;
115
116 ceph_decode_32_safe(p, end, info->dname_len, bad);
117 ceph_decode_need(p, end, info->dname_len, bad);
118 info->dname = *p;
119 *p += info->dname_len;
120 info->dlease = *p;
121 *p += sizeof(*info->dlease);
122 }
123
124 if (info->head->is_target) {
Sage Weil14303d22010-12-14 17:37:52 -0800125 err = parse_reply_info_in(p, end, &info->targeti, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700126 if (err < 0)
127 goto out_bad;
128 }
129
130 if (unlikely(*p != end))
131 goto bad;
132 return 0;
133
134bad:
135 err = -EIO;
136out_bad:
137 pr_err("problem parsing mds trace %d\n", err);
138 return err;
139}
140
141/*
142 * parse readdir results
143 */
144static int parse_reply_info_dir(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800145 struct ceph_mds_reply_info_parsed *info,
146 int features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700147{
148 u32 num, i = 0;
149 int err;
150
151 info->dir_dir = *p;
152 if (*p + sizeof(*info->dir_dir) > end)
153 goto bad;
154 *p += sizeof(*info->dir_dir) +
155 sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
156 if (*p > end)
157 goto bad;
158
159 ceph_decode_need(p, end, sizeof(num) + 2, bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700160 num = ceph_decode_32(p);
161 info->dir_end = ceph_decode_8(p);
162 info->dir_complete = ceph_decode_8(p);
Sage Weil2f2dc052009-10-06 11:31:09 -0700163 if (num == 0)
164 goto done;
165
166 /* alloc large array */
167 info->dir_nr = num;
168 info->dir_in = kcalloc(num, sizeof(*info->dir_in) +
169 sizeof(*info->dir_dname) +
170 sizeof(*info->dir_dname_len) +
171 sizeof(*info->dir_dlease),
172 GFP_NOFS);
173 if (info->dir_in == NULL) {
174 err = -ENOMEM;
175 goto out_bad;
176 }
177 info->dir_dname = (void *)(info->dir_in + num);
178 info->dir_dname_len = (void *)(info->dir_dname + num);
179 info->dir_dlease = (void *)(info->dir_dname_len + num);
180
181 while (num) {
182 /* dentry */
183 ceph_decode_need(p, end, sizeof(u32)*2, bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700184 info->dir_dname_len[i] = ceph_decode_32(p);
Sage Weil2f2dc052009-10-06 11:31:09 -0700185 ceph_decode_need(p, end, info->dir_dname_len[i], bad);
186 info->dir_dname[i] = *p;
187 *p += info->dir_dname_len[i];
188 dout("parsed dir dname '%.*s'\n", info->dir_dname_len[i],
189 info->dir_dname[i]);
190 info->dir_dlease[i] = *p;
191 *p += sizeof(struct ceph_mds_reply_lease);
192
193 /* inode */
Sage Weil14303d22010-12-14 17:37:52 -0800194 err = parse_reply_info_in(p, end, &info->dir_in[i], features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700195 if (err < 0)
196 goto out_bad;
197 i++;
198 num--;
199 }
200
201done:
202 if (*p != end)
203 goto bad;
204 return 0;
205
206bad:
207 err = -EIO;
208out_bad:
209 pr_err("problem parsing dir contents %d\n", err);
210 return err;
211}
212
213/*
Herb Shiu25933ab2010-12-01 14:14:38 -0800214 * parse fcntl F_GETLK results
215 */
216static int parse_reply_info_filelock(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800217 struct ceph_mds_reply_info_parsed *info,
218 int features)
Herb Shiu25933ab2010-12-01 14:14:38 -0800219{
220 if (*p + sizeof(*info->filelock_reply) > end)
221 goto bad;
222
223 info->filelock_reply = *p;
224 *p += sizeof(*info->filelock_reply);
225
226 if (unlikely(*p != end))
227 goto bad;
228 return 0;
229
230bad:
231 return -EIO;
232}
233
234/*
235 * parse extra results
236 */
237static int parse_reply_info_extra(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800238 struct ceph_mds_reply_info_parsed *info,
239 int features)
Herb Shiu25933ab2010-12-01 14:14:38 -0800240{
241 if (info->head->op == CEPH_MDS_OP_GETFILELOCK)
Sage Weil14303d22010-12-14 17:37:52 -0800242 return parse_reply_info_filelock(p, end, info, features);
Herb Shiu25933ab2010-12-01 14:14:38 -0800243 else
Sage Weil14303d22010-12-14 17:37:52 -0800244 return parse_reply_info_dir(p, end, info, features);
Herb Shiu25933ab2010-12-01 14:14:38 -0800245}
246
247/*
Sage Weil2f2dc052009-10-06 11:31:09 -0700248 * parse entire mds reply
249 */
250static int parse_reply_info(struct ceph_msg *msg,
Sage Weil14303d22010-12-14 17:37:52 -0800251 struct ceph_mds_reply_info_parsed *info,
252 int features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700253{
254 void *p, *end;
255 u32 len;
256 int err;
257
258 info->head = msg->front.iov_base;
259 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
260 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
261
262 /* trace */
263 ceph_decode_32_safe(&p, end, len, bad);
264 if (len > 0) {
Sage Weil14303d22010-12-14 17:37:52 -0800265 err = parse_reply_info_trace(&p, p+len, info, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700266 if (err < 0)
267 goto out_bad;
268 }
269
Herb Shiu25933ab2010-12-01 14:14:38 -0800270 /* extra */
Sage Weil2f2dc052009-10-06 11:31:09 -0700271 ceph_decode_32_safe(&p, end, len, bad);
272 if (len > 0) {
Sage Weil14303d22010-12-14 17:37:52 -0800273 err = parse_reply_info_extra(&p, p+len, info, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700274 if (err < 0)
275 goto out_bad;
276 }
277
278 /* snap blob */
279 ceph_decode_32_safe(&p, end, len, bad);
280 info->snapblob_len = len;
281 info->snapblob = p;
282 p += len;
283
284 if (p != end)
285 goto bad;
286 return 0;
287
288bad:
289 err = -EIO;
290out_bad:
291 pr_err("mds parse_reply err %d\n", err);
292 return err;
293}
294
295static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
296{
297 kfree(info->dir_in);
298}
299
300
301/*
302 * sessions
303 */
304static const char *session_state_name(int s)
305{
306 switch (s) {
307 case CEPH_MDS_SESSION_NEW: return "new";
308 case CEPH_MDS_SESSION_OPENING: return "opening";
309 case CEPH_MDS_SESSION_OPEN: return "open";
310 case CEPH_MDS_SESSION_HUNG: return "hung";
311 case CEPH_MDS_SESSION_CLOSING: return "closing";
Sage Weil44ca18f2010-02-15 12:08:46 -0800312 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
Sage Weil2f2dc052009-10-06 11:31:09 -0700313 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
314 default: return "???";
315 }
316}
317
318static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
319{
320 if (atomic_inc_not_zero(&s->s_ref)) {
321 dout("mdsc get_session %p %d -> %d\n", s,
322 atomic_read(&s->s_ref)-1, atomic_read(&s->s_ref));
323 return s;
324 } else {
325 dout("mdsc get_session %p 0 -- FAIL", s);
326 return NULL;
327 }
328}
329
330void ceph_put_mds_session(struct ceph_mds_session *s)
331{
332 dout("mdsc put_session %p %d -> %d\n", s,
333 atomic_read(&s->s_ref), atomic_read(&s->s_ref)-1);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800334 if (atomic_dec_and_test(&s->s_ref)) {
335 if (s->s_authorizer)
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700336 s->s_mdsc->fsc->client->monc.auth->ops->destroy_authorizer(
337 s->s_mdsc->fsc->client->monc.auth,
338 s->s_authorizer);
Sage Weil2f2dc052009-10-06 11:31:09 -0700339 kfree(s);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800340 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700341}
342
343/*
344 * called under mdsc->mutex
345 */
346struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
347 int mds)
348{
349 struct ceph_mds_session *session;
350
351 if (mds >= mdsc->max_sessions || mdsc->sessions[mds] == NULL)
352 return NULL;
353 session = mdsc->sessions[mds];
354 dout("lookup_mds_session %p %d\n", session,
355 atomic_read(&session->s_ref));
356 get_session(session);
357 return session;
358}
359
360static bool __have_session(struct ceph_mds_client *mdsc, int mds)
361{
362 if (mds >= mdsc->max_sessions)
363 return false;
364 return mdsc->sessions[mds];
365}
366
Sage Weil2600d2d2010-02-22 15:12:16 -0800367static int __verify_registered_session(struct ceph_mds_client *mdsc,
368 struct ceph_mds_session *s)
369{
370 if (s->s_mds >= mdsc->max_sessions ||
371 mdsc->sessions[s->s_mds] != s)
372 return -ENOENT;
373 return 0;
374}
375
Sage Weil2f2dc052009-10-06 11:31:09 -0700376/*
377 * create+register a new session for given mds.
378 * called under mdsc->mutex.
379 */
380static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
381 int mds)
382{
383 struct ceph_mds_session *s;
384
385 s = kzalloc(sizeof(*s), GFP_NOFS);
Dan Carpenter4736b002010-03-20 15:30:16 +0300386 if (!s)
387 return ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -0700388 s->s_mdsc = mdsc;
389 s->s_mds = mds;
390 s->s_state = CEPH_MDS_SESSION_NEW;
391 s->s_ttl = 0;
392 s->s_seq = 0;
393 mutex_init(&s->s_mutex);
394
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700395 ceph_con_init(mdsc->fsc->client->msgr, &s->s_con);
Sage Weil2f2dc052009-10-06 11:31:09 -0700396 s->s_con.private = s;
397 s->s_con.ops = &mds_con_ops;
398 s->s_con.peer_name.type = CEPH_ENTITY_TYPE_MDS;
399 s->s_con.peer_name.num = cpu_to_le64(mds);
Sage Weil2f2dc052009-10-06 11:31:09 -0700400
401 spin_lock_init(&s->s_cap_lock);
402 s->s_cap_gen = 0;
403 s->s_cap_ttl = 0;
404 s->s_renew_requested = 0;
405 s->s_renew_seq = 0;
406 INIT_LIST_HEAD(&s->s_caps);
407 s->s_nr_caps = 0;
Sage Weil5dacf092009-12-21 20:40:34 -0800408 s->s_trim_caps = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -0700409 atomic_set(&s->s_ref, 1);
410 INIT_LIST_HEAD(&s->s_waiting);
411 INIT_LIST_HEAD(&s->s_unsafe);
412 s->s_num_cap_releases = 0;
Sage Weil7c1332b2010-02-16 11:39:45 -0800413 s->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700414 INIT_LIST_HEAD(&s->s_cap_releases);
415 INIT_LIST_HEAD(&s->s_cap_releases_done);
416 INIT_LIST_HEAD(&s->s_cap_flushing);
417 INIT_LIST_HEAD(&s->s_cap_snaps_flushing);
418
419 dout("register_session mds%d\n", mds);
420 if (mds >= mdsc->max_sessions) {
421 int newmax = 1 << get_count_order(mds+1);
422 struct ceph_mds_session **sa;
423
424 dout("register_session realloc to %d\n", newmax);
425 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
426 if (sa == NULL)
Sage Weil42ce56e2009-11-18 11:22:36 -0800427 goto fail_realloc;
Sage Weil2f2dc052009-10-06 11:31:09 -0700428 if (mdsc->sessions) {
429 memcpy(sa, mdsc->sessions,
430 mdsc->max_sessions * sizeof(void *));
431 kfree(mdsc->sessions);
432 }
433 mdsc->sessions = sa;
434 mdsc->max_sessions = newmax;
435 }
436 mdsc->sessions[mds] = s;
437 atomic_inc(&s->s_ref); /* one ref to sessions[], one to caller */
Sage Weil42ce56e2009-11-18 11:22:36 -0800438
439 ceph_con_open(&s->s_con, ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
440
Sage Weil2f2dc052009-10-06 11:31:09 -0700441 return s;
Sage Weil42ce56e2009-11-18 11:22:36 -0800442
443fail_realloc:
444 kfree(s);
445 return ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -0700446}
447
448/*
449 * called under mdsc->mutex
450 */
Sage Weil2600d2d2010-02-22 15:12:16 -0800451static void __unregister_session(struct ceph_mds_client *mdsc,
Sage Weil42ce56e2009-11-18 11:22:36 -0800452 struct ceph_mds_session *s)
Sage Weil2f2dc052009-10-06 11:31:09 -0700453{
Sage Weil2600d2d2010-02-22 15:12:16 -0800454 dout("__unregister_session mds%d %p\n", s->s_mds, s);
455 BUG_ON(mdsc->sessions[s->s_mds] != s);
Sage Weil42ce56e2009-11-18 11:22:36 -0800456 mdsc->sessions[s->s_mds] = NULL;
457 ceph_con_close(&s->s_con);
458 ceph_put_mds_session(s);
Sage Weil2f2dc052009-10-06 11:31:09 -0700459}
460
461/*
462 * drop session refs in request.
463 *
464 * should be last request ref, or hold mdsc->mutex
465 */
466static void put_request_session(struct ceph_mds_request *req)
467{
468 if (req->r_session) {
469 ceph_put_mds_session(req->r_session);
470 req->r_session = NULL;
471 }
472}
473
Sage Weil153c8e62009-12-07 12:31:09 -0800474void ceph_mdsc_release_request(struct kref *kref)
Sage Weil2f2dc052009-10-06 11:31:09 -0700475{
Sage Weil153c8e62009-12-07 12:31:09 -0800476 struct ceph_mds_request *req = container_of(kref,
477 struct ceph_mds_request,
478 r_kref);
479 if (req->r_request)
480 ceph_msg_put(req->r_request);
481 if (req->r_reply) {
482 ceph_msg_put(req->r_reply);
483 destroy_reply_info(&req->r_reply_info);
Sage Weil2f2dc052009-10-06 11:31:09 -0700484 }
Sage Weil153c8e62009-12-07 12:31:09 -0800485 if (req->r_inode) {
486 ceph_put_cap_refs(ceph_inode(req->r_inode),
487 CEPH_CAP_PIN);
488 iput(req->r_inode);
489 }
490 if (req->r_locked_dir)
491 ceph_put_cap_refs(ceph_inode(req->r_locked_dir),
492 CEPH_CAP_PIN);
493 if (req->r_target_inode)
494 iput(req->r_target_inode);
495 if (req->r_dentry)
496 dput(req->r_dentry);
497 if (req->r_old_dentry) {
498 ceph_put_cap_refs(
499 ceph_inode(req->r_old_dentry->d_parent->d_inode),
500 CEPH_CAP_PIN);
501 dput(req->r_old_dentry);
502 }
503 kfree(req->r_path1);
504 kfree(req->r_path2);
505 put_request_session(req);
Yehuda Sadeh37151662010-06-17 16:16:12 -0700506 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
Sage Weil153c8e62009-12-07 12:31:09 -0800507 kfree(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700508}
509
510/*
511 * lookup session, bump ref if found.
512 *
513 * called under mdsc->mutex.
514 */
515static struct ceph_mds_request *__lookup_request(struct ceph_mds_client *mdsc,
516 u64 tid)
517{
518 struct ceph_mds_request *req;
Sage Weil44ca18f2010-02-15 12:08:46 -0800519 struct rb_node *n = mdsc->request_tree.rb_node;
520
521 while (n) {
522 req = rb_entry(n, struct ceph_mds_request, r_node);
523 if (tid < req->r_tid)
524 n = n->rb_left;
525 else if (tid > req->r_tid)
526 n = n->rb_right;
527 else {
528 ceph_mdsc_get_request(req);
529 return req;
530 }
531 }
532 return NULL;
533}
534
535static void __insert_request(struct ceph_mds_client *mdsc,
536 struct ceph_mds_request *new)
537{
538 struct rb_node **p = &mdsc->request_tree.rb_node;
539 struct rb_node *parent = NULL;
540 struct ceph_mds_request *req = NULL;
541
542 while (*p) {
543 parent = *p;
544 req = rb_entry(parent, struct ceph_mds_request, r_node);
545 if (new->r_tid < req->r_tid)
546 p = &(*p)->rb_left;
547 else if (new->r_tid > req->r_tid)
548 p = &(*p)->rb_right;
549 else
550 BUG();
551 }
552
553 rb_link_node(&new->r_node, parent, p);
554 rb_insert_color(&new->r_node, &mdsc->request_tree);
Sage Weil2f2dc052009-10-06 11:31:09 -0700555}
556
557/*
558 * Register an in-flight request, and assign a tid. Link to directory
559 * are modifying (if any).
560 *
561 * Called under mdsc->mutex.
562 */
563static void __register_request(struct ceph_mds_client *mdsc,
564 struct ceph_mds_request *req,
565 struct inode *dir)
566{
567 req->r_tid = ++mdsc->last_tid;
568 if (req->r_num_caps)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700569 ceph_reserve_caps(mdsc, &req->r_caps_reservation,
570 req->r_num_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -0700571 dout("__register_request %p tid %lld\n", req, req->r_tid);
572 ceph_mdsc_get_request(req);
Sage Weil44ca18f2010-02-15 12:08:46 -0800573 __insert_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700574
Sage Weilcb4276c2010-11-08 07:28:52 -0800575 req->r_uid = current_fsuid();
576 req->r_gid = current_fsgid();
577
Sage Weil2f2dc052009-10-06 11:31:09 -0700578 if (dir) {
579 struct ceph_inode_info *ci = ceph_inode(dir);
580
Sage Weil3b663782011-05-18 16:12:12 -0700581 ihold(dir);
Sage Weil2f2dc052009-10-06 11:31:09 -0700582 spin_lock(&ci->i_unsafe_lock);
583 req->r_unsafe_dir = dir;
584 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
585 spin_unlock(&ci->i_unsafe_lock);
586 }
587}
588
589static void __unregister_request(struct ceph_mds_client *mdsc,
590 struct ceph_mds_request *req)
591{
592 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
Sage Weil44ca18f2010-02-15 12:08:46 -0800593 rb_erase(&req->r_node, &mdsc->request_tree);
Sage Weil80fc7312010-03-16 15:28:54 -0700594 RB_CLEAR_NODE(&req->r_node);
Sage Weil2f2dc052009-10-06 11:31:09 -0700595
596 if (req->r_unsafe_dir) {
597 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
598
599 spin_lock(&ci->i_unsafe_lock);
600 list_del_init(&req->r_unsafe_dir_item);
601 spin_unlock(&ci->i_unsafe_lock);
Sage Weil3b663782011-05-18 16:12:12 -0700602
603 iput(req->r_unsafe_dir);
604 req->r_unsafe_dir = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700605 }
Sage Weil94aa8ae2010-03-28 21:22:50 -0700606
607 ceph_mdsc_put_request(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700608}
609
610/*
611 * Choose mds to send request to next. If there is a hint set in the
612 * request (e.g., due to a prior forward hint from the mds), use that.
613 * Otherwise, consult frag tree and/or caps to identify the
614 * appropriate mds. If all else fails, choose randomly.
615 *
616 * Called under mdsc->mutex.
617 */
Sage Weileb6bb1c2010-08-16 09:21:27 -0700618struct dentry *get_nonsnap_parent(struct dentry *dentry)
619{
620 while (!IS_ROOT(dentry) && ceph_snap(dentry->d_inode) != CEPH_NOSNAP)
621 dentry = dentry->d_parent;
622 return dentry;
623}
624
Sage Weil2f2dc052009-10-06 11:31:09 -0700625static int __choose_mds(struct ceph_mds_client *mdsc,
626 struct ceph_mds_request *req)
627{
628 struct inode *inode;
629 struct ceph_inode_info *ci;
630 struct ceph_cap *cap;
631 int mode = req->r_direct_mode;
632 int mds = -1;
633 u32 hash = req->r_direct_hash;
634 bool is_hash = req->r_direct_is_hash;
635
636 /*
637 * is there a specific mds we should try? ignore hint if we have
638 * no session and the mds is not up (active or recovering).
639 */
640 if (req->r_resend_mds >= 0 &&
641 (__have_session(mdsc, req->r_resend_mds) ||
642 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
643 dout("choose_mds using resend_mds mds%d\n",
644 req->r_resend_mds);
645 return req->r_resend_mds;
646 }
647
648 if (mode == USE_RANDOM_MDS)
649 goto random;
650
651 inode = NULL;
652 if (req->r_inode) {
653 inode = req->r_inode;
654 } else if (req->r_dentry) {
Sage Weileb6bb1c2010-08-16 09:21:27 -0700655 struct inode *dir = req->r_dentry->d_parent->d_inode;
656
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700657 if (dir->i_sb != mdsc->fsc->sb) {
Sage Weileb6bb1c2010-08-16 09:21:27 -0700658 /* not this fs! */
659 inode = req->r_dentry->d_inode;
660 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
661 /* direct snapped/virtual snapdir requests
662 * based on parent dir inode */
663 struct dentry *dn =
664 get_nonsnap_parent(req->r_dentry->d_parent);
665 inode = dn->d_inode;
666 dout("__choose_mds using nonsnap parent %p\n", inode);
667 } else if (req->r_dentry->d_inode) {
668 /* dentry target */
Sage Weil2f2dc052009-10-06 11:31:09 -0700669 inode = req->r_dentry->d_inode;
670 } else {
Sage Weileb6bb1c2010-08-16 09:21:27 -0700671 /* dir + name */
672 inode = dir;
Sage Weile5f86dc2011-07-26 11:30:55 -0700673 hash = ceph_dentry_hash(dir, req->r_dentry);
Sage Weil2f2dc052009-10-06 11:31:09 -0700674 is_hash = true;
675 }
676 }
Sage Weileb6bb1c2010-08-16 09:21:27 -0700677
Sage Weil2f2dc052009-10-06 11:31:09 -0700678 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
679 (int)hash, mode);
680 if (!inode)
681 goto random;
682 ci = ceph_inode(inode);
683
684 if (is_hash && S_ISDIR(inode->i_mode)) {
685 struct ceph_inode_frag frag;
686 int found;
687
688 ceph_choose_frag(ci, hash, &frag, &found);
689 if (found) {
690 if (mode == USE_ANY_MDS && frag.ndist > 0) {
691 u8 r;
692
693 /* choose a random replica */
694 get_random_bytes(&r, 1);
695 r %= frag.ndist;
696 mds = frag.dist[r];
697 dout("choose_mds %p %llx.%llx "
698 "frag %u mds%d (%d/%d)\n",
699 inode, ceph_vinop(inode),
Sage Weild66bbd42011-01-21 21:16:46 -0800700 frag.frag, mds,
Sage Weil2f2dc052009-10-06 11:31:09 -0700701 (int)r, frag.ndist);
Sage Weild66bbd42011-01-21 21:16:46 -0800702 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
703 CEPH_MDS_STATE_ACTIVE)
704 return mds;
Sage Weil2f2dc052009-10-06 11:31:09 -0700705 }
706
707 /* since this file/dir wasn't known to be
708 * replicated, then we want to look for the
709 * authoritative mds. */
710 mode = USE_AUTH_MDS;
711 if (frag.mds >= 0) {
712 /* choose auth mds */
713 mds = frag.mds;
714 dout("choose_mds %p %llx.%llx "
715 "frag %u mds%d (auth)\n",
716 inode, ceph_vinop(inode), frag.frag, mds);
Sage Weild66bbd42011-01-21 21:16:46 -0800717 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
718 CEPH_MDS_STATE_ACTIVE)
719 return mds;
Sage Weil2f2dc052009-10-06 11:31:09 -0700720 }
721 }
722 }
723
724 spin_lock(&inode->i_lock);
725 cap = NULL;
726 if (mode == USE_AUTH_MDS)
727 cap = ci->i_auth_cap;
728 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
729 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
730 if (!cap) {
731 spin_unlock(&inode->i_lock);
732 goto random;
733 }
734 mds = cap->session->s_mds;
735 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
736 inode, ceph_vinop(inode), mds,
737 cap == ci->i_auth_cap ? "auth " : "", cap);
738 spin_unlock(&inode->i_lock);
739 return mds;
740
741random:
742 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
743 dout("choose_mds chose random mds%d\n", mds);
744 return mds;
745}
746
747
748/*
749 * session messages
750 */
751static struct ceph_msg *create_session_msg(u32 op, u64 seq)
752{
753 struct ceph_msg *msg;
754 struct ceph_mds_session_head *h;
755
Yehuda Sadeh34d23762010-04-06 14:33:58 -0700756 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -0700757 if (!msg) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700758 pr_err("create_session_msg ENOMEM creating msg\n");
Sage Weila79832f2010-04-01 16:06:19 -0700759 return NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700760 }
761 h = msg->front.iov_base;
762 h->op = cpu_to_le32(op);
763 h->seq = cpu_to_le64(seq);
764 return msg;
765}
766
767/*
768 * send session open request.
769 *
770 * called under mdsc->mutex
771 */
772static int __open_session(struct ceph_mds_client *mdsc,
773 struct ceph_mds_session *session)
774{
775 struct ceph_msg *msg;
776 int mstate;
777 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -0700778
779 /* wait for mds to go active? */
780 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
781 dout("open_session to mds%d (%s)\n", mds,
782 ceph_mds_state_name(mstate));
783 session->s_state = CEPH_MDS_SESSION_OPENING;
784 session->s_renew_requested = jiffies;
785
786 /* send connect message */
787 msg = create_session_msg(CEPH_SESSION_REQUEST_OPEN, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -0700788 if (!msg)
789 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -0700790 ceph_con_send(&session->s_con, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -0700791 return 0;
792}
793
794/*
Sage Weiled0552a2010-06-21 13:38:25 -0700795 * open sessions for any export targets for the given mds
796 *
797 * called under mdsc->mutex
798 */
799static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
800 struct ceph_mds_session *session)
801{
802 struct ceph_mds_info *mi;
803 struct ceph_mds_session *ts;
804 int i, mds = session->s_mds;
805 int target;
806
807 if (mds >= mdsc->mdsmap->m_max_mds)
808 return;
809 mi = &mdsc->mdsmap->m_info[mds];
810 dout("open_export_target_sessions for mds%d (%d targets)\n",
811 session->s_mds, mi->num_export_targets);
812
813 for (i = 0; i < mi->num_export_targets; i++) {
814 target = mi->export_targets[i];
815 ts = __ceph_lookup_mds_session(mdsc, target);
816 if (!ts) {
817 ts = register_session(mdsc, target);
818 if (IS_ERR(ts))
819 return;
820 }
821 if (session->s_state == CEPH_MDS_SESSION_NEW ||
822 session->s_state == CEPH_MDS_SESSION_CLOSING)
823 __open_session(mdsc, session);
824 else
825 dout(" mds%d target mds%d %p is %s\n", session->s_mds,
826 i, ts, session_state_name(ts->s_state));
827 ceph_put_mds_session(ts);
828 }
829}
830
Sage Weil154f42c2010-06-21 13:45:04 -0700831void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
832 struct ceph_mds_session *session)
833{
834 mutex_lock(&mdsc->mutex);
835 __open_export_target_sessions(mdsc, session);
836 mutex_unlock(&mdsc->mutex);
837}
838
Sage Weiled0552a2010-06-21 13:38:25 -0700839/*
Sage Weil2f2dc052009-10-06 11:31:09 -0700840 * session caps
841 */
842
843/*
844 * Free preallocated cap messages assigned to this session
845 */
846static void cleanup_cap_releases(struct ceph_mds_session *session)
847{
848 struct ceph_msg *msg;
849
850 spin_lock(&session->s_cap_lock);
851 while (!list_empty(&session->s_cap_releases)) {
852 msg = list_first_entry(&session->s_cap_releases,
853 struct ceph_msg, list_head);
854 list_del_init(&msg->list_head);
855 ceph_msg_put(msg);
856 }
857 while (!list_empty(&session->s_cap_releases_done)) {
858 msg = list_first_entry(&session->s_cap_releases_done,
859 struct ceph_msg, list_head);
860 list_del_init(&msg->list_head);
861 ceph_msg_put(msg);
862 }
863 spin_unlock(&session->s_cap_lock);
864}
865
866/*
Sage Weilf818a732010-05-11 20:56:31 -0700867 * Helper to safely iterate over all caps associated with a session, with
868 * special care taken to handle a racing __ceph_remove_cap().
Sage Weil2f2dc052009-10-06 11:31:09 -0700869 *
Sage Weilf818a732010-05-11 20:56:31 -0700870 * Caller must hold session s_mutex.
Sage Weil2f2dc052009-10-06 11:31:09 -0700871 */
872static int iterate_session_caps(struct ceph_mds_session *session,
873 int (*cb)(struct inode *, struct ceph_cap *,
874 void *), void *arg)
875{
Sage Weil7c1332b2010-02-16 11:39:45 -0800876 struct list_head *p;
877 struct ceph_cap *cap;
878 struct inode *inode, *last_inode = NULL;
879 struct ceph_cap *old_cap = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700880 int ret;
881
882 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
883 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800884 p = session->s_caps.next;
885 while (p != &session->s_caps) {
886 cap = list_entry(p, struct ceph_cap, session_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -0700887 inode = igrab(&cap->ci->vfs_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -0800888 if (!inode) {
889 p = p->next;
Sage Weil2f2dc052009-10-06 11:31:09 -0700890 continue;
Sage Weil7c1332b2010-02-16 11:39:45 -0800891 }
892 session->s_cap_iterator = cap;
Sage Weil2f2dc052009-10-06 11:31:09 -0700893 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800894
895 if (last_inode) {
896 iput(last_inode);
897 last_inode = NULL;
898 }
899 if (old_cap) {
Yehuda Sadeh37151662010-06-17 16:16:12 -0700900 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -0800901 old_cap = NULL;
902 }
903
Sage Weil2f2dc052009-10-06 11:31:09 -0700904 ret = cb(inode, cap, arg);
Sage Weil7c1332b2010-02-16 11:39:45 -0800905 last_inode = inode;
906
Sage Weil2f2dc052009-10-06 11:31:09 -0700907 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800908 p = p->next;
909 if (cap->ci == NULL) {
910 dout("iterate_session_caps finishing cap %p removal\n",
911 cap);
912 BUG_ON(cap->session != session);
913 list_del_init(&cap->session_caps);
914 session->s_nr_caps--;
915 cap->session = NULL;
916 old_cap = cap; /* put_cap it w/o locks held */
917 }
Sage Weil5dacf092009-12-21 20:40:34 -0800918 if (ret < 0)
919 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -0700920 }
Sage Weil5dacf092009-12-21 20:40:34 -0800921 ret = 0;
922out:
Sage Weil7c1332b2010-02-16 11:39:45 -0800923 session->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700924 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800925
926 if (last_inode)
927 iput(last_inode);
928 if (old_cap)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700929 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -0800930
Sage Weil5dacf092009-12-21 20:40:34 -0800931 return ret;
Sage Weil2f2dc052009-10-06 11:31:09 -0700932}
933
934static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
Sage Weil6c99f252010-05-10 16:12:25 -0700935 void *arg)
Sage Weil2f2dc052009-10-06 11:31:09 -0700936{
937 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil6c99f252010-05-10 16:12:25 -0700938 int drop = 0;
939
Sage Weil2f2dc052009-10-06 11:31:09 -0700940 dout("removing cap %p, ci is %p, inode is %p\n",
941 cap, ci, &ci->vfs_inode);
Sage Weil6c99f252010-05-10 16:12:25 -0700942 spin_lock(&inode->i_lock);
943 __ceph_remove_cap(cap);
944 if (!__ceph_is_any_real_caps(ci)) {
945 struct ceph_mds_client *mdsc =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700946 ceph_sb_to_client(inode->i_sb)->mdsc;
Sage Weil6c99f252010-05-10 16:12:25 -0700947
948 spin_lock(&mdsc->cap_dirty_lock);
949 if (!list_empty(&ci->i_dirty_item)) {
950 pr_info(" dropping dirty %s state for %p %lld\n",
951 ceph_cap_string(ci->i_dirty_caps),
952 inode, ceph_ino(inode));
953 ci->i_dirty_caps = 0;
954 list_del_init(&ci->i_dirty_item);
955 drop = 1;
956 }
957 if (!list_empty(&ci->i_flushing_item)) {
958 pr_info(" dropping dirty+flushing %s state for %p %lld\n",
959 ceph_cap_string(ci->i_flushing_caps),
960 inode, ceph_ino(inode));
961 ci->i_flushing_caps = 0;
962 list_del_init(&ci->i_flushing_item);
963 mdsc->num_cap_flushing--;
964 drop = 1;
965 }
966 if (drop && ci->i_wrbuffer_ref) {
967 pr_info(" dropping dirty data for %p %lld\n",
968 inode, ceph_ino(inode));
969 ci->i_wrbuffer_ref = 0;
970 ci->i_wrbuffer_ref_head = 0;
971 drop++;
972 }
973 spin_unlock(&mdsc->cap_dirty_lock);
974 }
975 spin_unlock(&inode->i_lock);
976 while (drop--)
977 iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -0700978 return 0;
979}
980
981/*
982 * caller must hold session s_mutex
983 */
984static void remove_session_caps(struct ceph_mds_session *session)
985{
986 dout("remove_session_caps on %p\n", session);
987 iterate_session_caps(session, remove_session_caps_cb, NULL);
988 BUG_ON(session->s_nr_caps > 0);
Sage Weil6c99f252010-05-10 16:12:25 -0700989 BUG_ON(!list_empty(&session->s_cap_flushing));
Sage Weil2f2dc052009-10-06 11:31:09 -0700990 cleanup_cap_releases(session);
991}
992
993/*
994 * wake up any threads waiting on this session's caps. if the cap is
995 * old (didn't get renewed on the client reconnect), remove it now.
996 *
997 * caller must hold s_mutex.
998 */
999static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1000 void *arg)
1001{
Sage Weil0dc25702009-11-20 13:43:45 -08001002 struct ceph_inode_info *ci = ceph_inode(inode);
1003
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001004 wake_up_all(&ci->i_cap_wq);
Sage Weil0dc25702009-11-20 13:43:45 -08001005 if (arg) {
1006 spin_lock(&inode->i_lock);
1007 ci->i_wanted_max_size = 0;
1008 ci->i_requested_max_size = 0;
1009 spin_unlock(&inode->i_lock);
1010 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001011 return 0;
1012}
1013
Sage Weil0dc25702009-11-20 13:43:45 -08001014static void wake_up_session_caps(struct ceph_mds_session *session,
1015 int reconnect)
Sage Weil2f2dc052009-10-06 11:31:09 -07001016{
1017 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
Sage Weil0dc25702009-11-20 13:43:45 -08001018 iterate_session_caps(session, wake_up_session_cb,
1019 (void *)(unsigned long)reconnect);
Sage Weil2f2dc052009-10-06 11:31:09 -07001020}
1021
1022/*
1023 * Send periodic message to MDS renewing all currently held caps. The
1024 * ack will reset the expiration for all caps from this session.
1025 *
1026 * caller holds s_mutex
1027 */
1028static int send_renew_caps(struct ceph_mds_client *mdsc,
1029 struct ceph_mds_session *session)
1030{
1031 struct ceph_msg *msg;
1032 int state;
1033
1034 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1035 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1036 pr_info("mds%d caps stale\n", session->s_mds);
Sage Weile4cb4cb2010-03-18 13:43:09 -07001037 session->s_renew_requested = jiffies;
Sage Weil2f2dc052009-10-06 11:31:09 -07001038
1039 /* do not try to renew caps until a recovering mds has reconnected
1040 * with its clients. */
1041 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1042 if (state < CEPH_MDS_STATE_RECONNECT) {
1043 dout("send_renew_caps ignoring mds%d (%s)\n",
1044 session->s_mds, ceph_mds_state_name(state));
1045 return 0;
1046 }
1047
1048 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1049 ceph_mds_state_name(state));
Sage Weil2f2dc052009-10-06 11:31:09 -07001050 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1051 ++session->s_renew_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001052 if (!msg)
1053 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -07001054 ceph_con_send(&session->s_con, msg);
1055 return 0;
1056}
1057
1058/*
1059 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
Sage Weil0dc25702009-11-20 13:43:45 -08001060 *
1061 * Called under session->s_mutex
Sage Weil2f2dc052009-10-06 11:31:09 -07001062 */
1063static void renewed_caps(struct ceph_mds_client *mdsc,
1064 struct ceph_mds_session *session, int is_renew)
1065{
1066 int was_stale;
1067 int wake = 0;
1068
1069 spin_lock(&session->s_cap_lock);
1070 was_stale = is_renew && (session->s_cap_ttl == 0 ||
1071 time_after_eq(jiffies, session->s_cap_ttl));
1072
1073 session->s_cap_ttl = session->s_renew_requested +
1074 mdsc->mdsmap->m_session_timeout*HZ;
1075
1076 if (was_stale) {
1077 if (time_before(jiffies, session->s_cap_ttl)) {
1078 pr_info("mds%d caps renewed\n", session->s_mds);
1079 wake = 1;
1080 } else {
1081 pr_info("mds%d caps still stale\n", session->s_mds);
1082 }
1083 }
1084 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1085 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1086 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1087 spin_unlock(&session->s_cap_lock);
1088
1089 if (wake)
Sage Weil0dc25702009-11-20 13:43:45 -08001090 wake_up_session_caps(session, 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07001091}
1092
1093/*
1094 * send a session close request
1095 */
1096static int request_close_session(struct ceph_mds_client *mdsc,
1097 struct ceph_mds_session *session)
1098{
1099 struct ceph_msg *msg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001100
1101 dout("request_close_session mds%d state %s seq %lld\n",
1102 session->s_mds, session_state_name(session->s_state),
1103 session->s_seq);
1104 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001105 if (!msg)
1106 return -ENOMEM;
1107 ceph_con_send(&session->s_con, msg);
1108 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001109}
1110
1111/*
1112 * Called with s_mutex held.
1113 */
1114static int __close_session(struct ceph_mds_client *mdsc,
1115 struct ceph_mds_session *session)
1116{
1117 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1118 return 0;
1119 session->s_state = CEPH_MDS_SESSION_CLOSING;
1120 return request_close_session(mdsc, session);
1121}
1122
1123/*
1124 * Trim old(er) caps.
1125 *
1126 * Because we can't cache an inode without one or more caps, we do
1127 * this indirectly: if a cap is unused, we prune its aliases, at which
1128 * point the inode will hopefully get dropped to.
1129 *
1130 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1131 * memory pressure from the MDS, though, so it needn't be perfect.
1132 */
1133static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1134{
1135 struct ceph_mds_session *session = arg;
1136 struct ceph_inode_info *ci = ceph_inode(inode);
1137 int used, oissued, mine;
1138
1139 if (session->s_trim_caps <= 0)
1140 return -1;
1141
1142 spin_lock(&inode->i_lock);
1143 mine = cap->issued | cap->implemented;
1144 used = __ceph_caps_used(ci);
1145 oissued = __ceph_caps_issued_other(ci, cap);
1146
1147 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n",
1148 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
1149 ceph_cap_string(used));
1150 if (ci->i_dirty_caps)
1151 goto out; /* dirty caps */
1152 if ((used & ~oissued) & mine)
1153 goto out; /* we need these caps */
1154
1155 session->s_trim_caps--;
1156 if (oissued) {
1157 /* we aren't the only cap.. just remove us */
Sage Weil7c1332b2010-02-16 11:39:45 -08001158 __ceph_remove_cap(cap);
Sage Weil2f2dc052009-10-06 11:31:09 -07001159 } else {
1160 /* try to drop referring dentries */
1161 spin_unlock(&inode->i_lock);
1162 d_prune_aliases(inode);
1163 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1164 inode, cap, atomic_read(&inode->i_count));
1165 return 0;
1166 }
1167
1168out:
1169 spin_unlock(&inode->i_lock);
1170 return 0;
1171}
1172
1173/*
1174 * Trim session cap count down to some max number.
1175 */
1176static int trim_caps(struct ceph_mds_client *mdsc,
1177 struct ceph_mds_session *session,
1178 int max_caps)
1179{
1180 int trim_caps = session->s_nr_caps - max_caps;
1181
1182 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1183 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1184 if (trim_caps > 0) {
1185 session->s_trim_caps = trim_caps;
1186 iterate_session_caps(session, trim_caps_cb, session);
1187 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1188 session->s_mds, session->s_nr_caps, max_caps,
1189 trim_caps - session->s_trim_caps);
Sage Weil5dacf092009-12-21 20:40:34 -08001190 session->s_trim_caps = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001191 }
1192 return 0;
1193}
1194
1195/*
1196 * Allocate cap_release messages. If there is a partially full message
1197 * in the queue, try to allocate enough to cover it's remainder, so that
1198 * we can send it immediately.
1199 *
1200 * Called under s_mutex.
1201 */
Sage Weil2b2300d2010-06-09 16:52:04 -07001202int ceph_add_cap_releases(struct ceph_mds_client *mdsc,
Sage Weilee6b2722010-06-10 12:55:52 -07001203 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001204{
Sage Weil38e8883e2010-06-10 12:57:11 -07001205 struct ceph_msg *msg, *partial = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001206 struct ceph_mds_cap_release *head;
1207 int err = -ENOMEM;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001208 int extra = mdsc->fsc->mount_options->cap_release_safety;
Sage Weil38e8883e2010-06-10 12:57:11 -07001209 int num;
Sage Weil2f2dc052009-10-06 11:31:09 -07001210
Sage Weil38e8883e2010-06-10 12:57:11 -07001211 dout("add_cap_releases %p mds%d extra %d\n", session, session->s_mds,
1212 extra);
Sage Weil2f2dc052009-10-06 11:31:09 -07001213
1214 spin_lock(&session->s_cap_lock);
1215
1216 if (!list_empty(&session->s_cap_releases)) {
1217 msg = list_first_entry(&session->s_cap_releases,
1218 struct ceph_msg,
1219 list_head);
1220 head = msg->front.iov_base;
Sage Weil38e8883e2010-06-10 12:57:11 -07001221 num = le32_to_cpu(head->num);
1222 if (num) {
1223 dout(" partial %p with (%d/%d)\n", msg, num,
1224 (int)CEPH_CAPS_PER_RELEASE);
1225 extra += CEPH_CAPS_PER_RELEASE - num;
1226 partial = msg;
1227 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001228 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001229 while (session->s_num_cap_releases < session->s_nr_caps + extra) {
1230 spin_unlock(&session->s_cap_lock);
Yehuda Sadeh34d23762010-04-06 14:33:58 -07001231 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE,
1232 GFP_NOFS);
Sage Weil2f2dc052009-10-06 11:31:09 -07001233 if (!msg)
1234 goto out_unlocked;
1235 dout("add_cap_releases %p msg %p now %d\n", session, msg,
1236 (int)msg->front.iov_len);
1237 head = msg->front.iov_base;
1238 head->num = cpu_to_le32(0);
1239 msg->front.iov_len = sizeof(*head);
1240 spin_lock(&session->s_cap_lock);
1241 list_add(&msg->list_head, &session->s_cap_releases);
1242 session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
1243 }
1244
Sage Weil38e8883e2010-06-10 12:57:11 -07001245 if (partial) {
1246 head = partial->front.iov_base;
1247 num = le32_to_cpu(head->num);
1248 dout(" queueing partial %p with %d/%d\n", partial, num,
1249 (int)CEPH_CAPS_PER_RELEASE);
1250 list_move_tail(&partial->list_head,
1251 &session->s_cap_releases_done);
1252 session->s_num_cap_releases -= CEPH_CAPS_PER_RELEASE - num;
Sage Weil2f2dc052009-10-06 11:31:09 -07001253 }
1254 err = 0;
1255 spin_unlock(&session->s_cap_lock);
1256out_unlocked:
1257 return err;
1258}
1259
1260/*
1261 * flush all dirty inode data to disk.
1262 *
1263 * returns true if we've flushed through want_flush_seq
1264 */
1265static int check_cap_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
1266{
1267 int mds, ret = 1;
1268
1269 dout("check_cap_flush want %lld\n", want_flush_seq);
1270 mutex_lock(&mdsc->mutex);
1271 for (mds = 0; ret && mds < mdsc->max_sessions; mds++) {
1272 struct ceph_mds_session *session = mdsc->sessions[mds];
1273
1274 if (!session)
1275 continue;
1276 get_session(session);
1277 mutex_unlock(&mdsc->mutex);
1278
1279 mutex_lock(&session->s_mutex);
1280 if (!list_empty(&session->s_cap_flushing)) {
1281 struct ceph_inode_info *ci =
1282 list_entry(session->s_cap_flushing.next,
1283 struct ceph_inode_info,
1284 i_flushing_item);
1285 struct inode *inode = &ci->vfs_inode;
1286
1287 spin_lock(&inode->i_lock);
1288 if (ci->i_cap_flush_seq <= want_flush_seq) {
1289 dout("check_cap_flush still flushing %p "
1290 "seq %lld <= %lld to mds%d\n", inode,
1291 ci->i_cap_flush_seq, want_flush_seq,
1292 session->s_mds);
1293 ret = 0;
1294 }
1295 spin_unlock(&inode->i_lock);
1296 }
1297 mutex_unlock(&session->s_mutex);
1298 ceph_put_mds_session(session);
1299
1300 if (!ret)
1301 return ret;
1302 mutex_lock(&mdsc->mutex);
1303 }
1304
1305 mutex_unlock(&mdsc->mutex);
1306 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
1307 return ret;
1308}
1309
1310/*
1311 * called under s_mutex
1312 */
Sage Weil3d7ded42010-06-09 16:47:10 -07001313void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1314 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001315{
1316 struct ceph_msg *msg;
1317
1318 dout("send_cap_releases mds%d\n", session->s_mds);
Sage Weil0f8605f2010-05-05 15:51:35 -07001319 spin_lock(&session->s_cap_lock);
1320 while (!list_empty(&session->s_cap_releases_done)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001321 msg = list_first_entry(&session->s_cap_releases_done,
1322 struct ceph_msg, list_head);
1323 list_del_init(&msg->list_head);
1324 spin_unlock(&session->s_cap_lock);
1325 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1326 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1327 ceph_con_send(&session->s_con, msg);
Sage Weil0f8605f2010-05-05 15:51:35 -07001328 spin_lock(&session->s_cap_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001329 }
1330 spin_unlock(&session->s_cap_lock);
1331}
1332
Sage Weile01a5942010-05-10 15:36:44 -07001333static void discard_cap_releases(struct ceph_mds_client *mdsc,
1334 struct ceph_mds_session *session)
1335{
1336 struct ceph_msg *msg;
1337 struct ceph_mds_cap_release *head;
1338 unsigned num;
1339
1340 dout("discard_cap_releases mds%d\n", session->s_mds);
1341 spin_lock(&session->s_cap_lock);
1342
1343 /* zero out the in-progress message */
1344 msg = list_first_entry(&session->s_cap_releases,
1345 struct ceph_msg, list_head);
1346 head = msg->front.iov_base;
1347 num = le32_to_cpu(head->num);
1348 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg, num);
1349 head->num = cpu_to_le32(0);
1350 session->s_num_cap_releases += num;
1351
1352 /* requeue completed messages */
1353 while (!list_empty(&session->s_cap_releases_done)) {
1354 msg = list_first_entry(&session->s_cap_releases_done,
1355 struct ceph_msg, list_head);
1356 list_del_init(&msg->list_head);
1357
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,
1361 num);
1362 session->s_num_cap_releases += num;
1363 head->num = cpu_to_le32(0);
1364 msg->front.iov_len = sizeof(*head);
1365 list_add(&msg->list_head, &session->s_cap_releases);
1366 }
1367
1368 spin_unlock(&session->s_cap_lock);
1369}
1370
Sage Weil2f2dc052009-10-06 11:31:09 -07001371/*
1372 * requests
1373 */
1374
1375/*
1376 * Create an mds request.
1377 */
1378struct ceph_mds_request *
1379ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1380{
1381 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1382
1383 if (!req)
1384 return ERR_PTR(-ENOMEM);
1385
Sage Weilb4556392010-05-13 12:01:13 -07001386 mutex_init(&req->r_fill_mutex);
Yehuda Sadeh37151662010-06-17 16:16:12 -07001387 req->r_mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07001388 req->r_started = jiffies;
1389 req->r_resend_mds = -1;
1390 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1391 req->r_fmode = -1;
Sage Weil153c8e62009-12-07 12:31:09 -08001392 kref_init(&req->r_kref);
Sage Weil2f2dc052009-10-06 11:31:09 -07001393 INIT_LIST_HEAD(&req->r_wait);
1394 init_completion(&req->r_completion);
1395 init_completion(&req->r_safe_completion);
1396 INIT_LIST_HEAD(&req->r_unsafe_item);
1397
1398 req->r_op = op;
1399 req->r_direct_mode = mode;
1400 return req;
1401}
1402
1403/*
Sage Weil44ca18f2010-02-15 12:08:46 -08001404 * return oldest (lowest) request, tid in request tree, 0 if none.
Sage Weil2f2dc052009-10-06 11:31:09 -07001405 *
1406 * called under mdsc->mutex.
1407 */
Sage Weil44ca18f2010-02-15 12:08:46 -08001408static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1409{
1410 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1411 return NULL;
1412 return rb_entry(rb_first(&mdsc->request_tree),
1413 struct ceph_mds_request, r_node);
1414}
1415
Sage Weil2f2dc052009-10-06 11:31:09 -07001416static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1417{
Sage Weil44ca18f2010-02-15 12:08:46 -08001418 struct ceph_mds_request *req = __get_oldest_req(mdsc);
1419
1420 if (req)
1421 return req->r_tid;
1422 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001423}
1424
1425/*
1426 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1427 * on build_path_from_dentry in fs/cifs/dir.c.
1428 *
1429 * If @stop_on_nosnap, generate path relative to the first non-snapped
1430 * inode.
1431 *
1432 * Encode hidden .snap dirs as a double /, i.e.
1433 * foo/.snap/bar -> foo//bar
1434 */
1435char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1436 int stop_on_nosnap)
1437{
1438 struct dentry *temp;
1439 char *path;
1440 int len, pos;
Al Viro1b71fe22011-07-16 23:43:58 -04001441 unsigned seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07001442
1443 if (dentry == NULL)
1444 return ERR_PTR(-EINVAL);
1445
1446retry:
1447 len = 0;
Al Viro1b71fe22011-07-16 23:43:58 -04001448 seq = read_seqbegin(&rename_lock);
1449 rcu_read_lock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001450 for (temp = dentry; !IS_ROOT(temp);) {
1451 struct inode *inode = temp->d_inode;
1452 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1453 len++; /* slash only */
1454 else if (stop_on_nosnap && inode &&
1455 ceph_snap(inode) == CEPH_NOSNAP)
1456 break;
1457 else
1458 len += 1 + temp->d_name.len;
1459 temp = temp->d_parent;
1460 if (temp == NULL) {
Al Viro1b71fe22011-07-16 23:43:58 -04001461 rcu_read_unlock();
Sage Weil6c99f252010-05-10 16:12:25 -07001462 pr_err("build_path corrupt dentry %p\n", dentry);
Sage Weil2f2dc052009-10-06 11:31:09 -07001463 return ERR_PTR(-EINVAL);
1464 }
1465 }
Al Viro1b71fe22011-07-16 23:43:58 -04001466 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001467 if (len)
1468 len--; /* no leading '/' */
1469
1470 path = kmalloc(len+1, GFP_NOFS);
1471 if (path == NULL)
1472 return ERR_PTR(-ENOMEM);
1473 pos = len;
1474 path[pos] = 0; /* trailing null */
Al Viro1b71fe22011-07-16 23:43:58 -04001475 rcu_read_lock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001476 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
Al Viro1b71fe22011-07-16 23:43:58 -04001477 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07001478
Al Viro1b71fe22011-07-16 23:43:58 -04001479 spin_lock(&temp->d_lock);
1480 inode = temp->d_inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07001481 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
Sage Weil104648a2010-03-18 10:14:30 -07001482 dout("build_path path+%d: %p SNAPDIR\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001483 pos, temp);
1484 } else if (stop_on_nosnap && inode &&
1485 ceph_snap(inode) == CEPH_NOSNAP) {
1486 break;
1487 } else {
1488 pos -= temp->d_name.len;
Al Viro1b71fe22011-07-16 23:43:58 -04001489 if (pos < 0) {
1490 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001491 break;
Al Viro1b71fe22011-07-16 23:43:58 -04001492 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001493 strncpy(path + pos, temp->d_name.name,
1494 temp->d_name.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07001495 }
Al Viro1b71fe22011-07-16 23:43:58 -04001496 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001497 if (pos)
1498 path[--pos] = '/';
1499 temp = temp->d_parent;
1500 if (temp == NULL) {
Al Viro1b71fe22011-07-16 23:43:58 -04001501 rcu_read_unlock();
Sage Weil104648a2010-03-18 10:14:30 -07001502 pr_err("build_path corrupt dentry\n");
Sage Weil2f2dc052009-10-06 11:31:09 -07001503 kfree(path);
1504 return ERR_PTR(-EINVAL);
1505 }
1506 }
Al Viro1b71fe22011-07-16 23:43:58 -04001507 rcu_read_unlock();
1508 if (pos != 0 || read_seqretry(&rename_lock, seq)) {
Sage Weil104648a2010-03-18 10:14:30 -07001509 pr_err("build_path did not end path lookup where "
Sage Weil2f2dc052009-10-06 11:31:09 -07001510 "expected, namelen is %d, pos is %d\n", len, pos);
1511 /* presumably this is only possible if racing with a
1512 rename of one of the parent directories (we can not
1513 lock the dentries above us to prevent this, but
1514 retrying should be harmless) */
1515 kfree(path);
1516 goto retry;
1517 }
1518
1519 *base = ceph_ino(temp->d_inode);
1520 *plen = len;
Sage Weil104648a2010-03-18 10:14:30 -07001521 dout("build_path on %p %d built %llx '%.*s'\n",
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001522 dentry, dentry->d_count, *base, len, path);
Sage Weil2f2dc052009-10-06 11:31:09 -07001523 return path;
1524}
1525
1526static int build_dentry_path(struct dentry *dentry,
1527 const char **ppath, int *ppathlen, u64 *pino,
1528 int *pfreepath)
1529{
1530 char *path;
1531
1532 if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) {
1533 *pino = ceph_ino(dentry->d_parent->d_inode);
1534 *ppath = dentry->d_name.name;
1535 *ppathlen = dentry->d_name.len;
1536 return 0;
1537 }
1538 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1539 if (IS_ERR(path))
1540 return PTR_ERR(path);
1541 *ppath = path;
1542 *pfreepath = 1;
1543 return 0;
1544}
1545
1546static int build_inode_path(struct inode *inode,
1547 const char **ppath, int *ppathlen, u64 *pino,
1548 int *pfreepath)
1549{
1550 struct dentry *dentry;
1551 char *path;
1552
1553 if (ceph_snap(inode) == CEPH_NOSNAP) {
1554 *pino = ceph_ino(inode);
1555 *ppathlen = 0;
1556 return 0;
1557 }
1558 dentry = d_find_alias(inode);
1559 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1560 dput(dentry);
1561 if (IS_ERR(path))
1562 return PTR_ERR(path);
1563 *ppath = path;
1564 *pfreepath = 1;
1565 return 0;
1566}
1567
1568/*
1569 * request arguments may be specified via an inode *, a dentry *, or
1570 * an explicit ino+path.
1571 */
1572static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1573 const char *rpath, u64 rino,
1574 const char **ppath, int *pathlen,
1575 u64 *ino, int *freepath)
1576{
1577 int r = 0;
1578
1579 if (rinode) {
1580 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1581 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1582 ceph_snap(rinode));
1583 } else if (rdentry) {
1584 r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
1585 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1586 *ppath);
1587 } else if (rpath) {
1588 *ino = rino;
1589 *ppath = rpath;
1590 *pathlen = strlen(rpath);
1591 dout(" path %.*s\n", *pathlen, rpath);
1592 }
1593
1594 return r;
1595}
1596
1597/*
1598 * called under mdsc->mutex
1599 */
1600static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1601 struct ceph_mds_request *req,
1602 int mds)
1603{
1604 struct ceph_msg *msg;
1605 struct ceph_mds_request_head *head;
1606 const char *path1 = NULL;
1607 const char *path2 = NULL;
1608 u64 ino1 = 0, ino2 = 0;
1609 int pathlen1 = 0, pathlen2 = 0;
1610 int freepath1 = 0, freepath2 = 0;
1611 int len;
1612 u16 releases;
1613 void *p, *end;
1614 int ret;
1615
1616 ret = set_request_path_attr(req->r_inode, req->r_dentry,
1617 req->r_path1, req->r_ino1.ino,
1618 &path1, &pathlen1, &ino1, &freepath1);
1619 if (ret < 0) {
1620 msg = ERR_PTR(ret);
1621 goto out;
1622 }
1623
1624 ret = set_request_path_attr(NULL, req->r_old_dentry,
1625 req->r_path2, req->r_ino2.ino,
1626 &path2, &pathlen2, &ino2, &freepath2);
1627 if (ret < 0) {
1628 msg = ERR_PTR(ret);
1629 goto out_free1;
1630 }
1631
1632 len = sizeof(*head) +
Sage Weilac8839d2010-01-27 14:28:10 -08001633 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64));
Sage Weil2f2dc052009-10-06 11:31:09 -07001634
1635 /* calculate (max) length for cap releases */
1636 len += sizeof(struct ceph_mds_request_release) *
1637 (!!req->r_inode_drop + !!req->r_dentry_drop +
1638 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1639 if (req->r_dentry_drop)
1640 len += req->r_dentry->d_name.len;
1641 if (req->r_old_dentry_drop)
1642 len += req->r_old_dentry->d_name.len;
1643
Yehuda Sadeh34d23762010-04-06 14:33:58 -07001644 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -07001645 if (!msg) {
1646 msg = ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -07001647 goto out_free2;
Sage Weila79832f2010-04-01 16:06:19 -07001648 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001649
Sage Weil6df058c2009-12-22 11:24:33 -08001650 msg->hdr.tid = cpu_to_le64(req->r_tid);
1651
Sage Weil2f2dc052009-10-06 11:31:09 -07001652 head = msg->front.iov_base;
1653 p = msg->front.iov_base + sizeof(*head);
1654 end = msg->front.iov_base + msg->front.iov_len;
1655
1656 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1657 head->op = cpu_to_le32(req->r_op);
Sage Weilcb4276c2010-11-08 07:28:52 -08001658 head->caller_uid = cpu_to_le32(req->r_uid);
1659 head->caller_gid = cpu_to_le32(req->r_gid);
Sage Weil2f2dc052009-10-06 11:31:09 -07001660 head->args = req->r_args;
1661
1662 ceph_encode_filepath(&p, end, ino1, path1);
1663 ceph_encode_filepath(&p, end, ino2, path2);
1664
Sage Weile979cf52010-07-15 14:58:39 -07001665 /* make note of release offset, in case we need to replay */
1666 req->r_request_release_offset = p - msg->front.iov_base;
1667
Sage Weil2f2dc052009-10-06 11:31:09 -07001668 /* cap releases */
1669 releases = 0;
1670 if (req->r_inode_drop)
1671 releases += ceph_encode_inode_release(&p,
1672 req->r_inode ? req->r_inode : req->r_dentry->d_inode,
1673 mds, req->r_inode_drop, req->r_inode_unless, 0);
1674 if (req->r_dentry_drop)
1675 releases += ceph_encode_dentry_release(&p, req->r_dentry,
1676 mds, req->r_dentry_drop, req->r_dentry_unless);
1677 if (req->r_old_dentry_drop)
1678 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
1679 mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
1680 if (req->r_old_inode_drop)
1681 releases += ceph_encode_inode_release(&p,
1682 req->r_old_dentry->d_inode,
1683 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
1684 head->num_releases = cpu_to_le16(releases);
1685
1686 BUG_ON(p > end);
1687 msg->front.iov_len = p - msg->front.iov_base;
1688 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1689
1690 msg->pages = req->r_pages;
1691 msg->nr_pages = req->r_num_pages;
1692 msg->hdr.data_len = cpu_to_le32(req->r_data_len);
1693 msg->hdr.data_off = cpu_to_le16(0);
1694
1695out_free2:
1696 if (freepath2)
1697 kfree((char *)path2);
1698out_free1:
1699 if (freepath1)
1700 kfree((char *)path1);
1701out:
1702 return msg;
1703}
1704
1705/*
1706 * called under mdsc->mutex if error, under no mutex if
1707 * success.
1708 */
1709static void complete_request(struct ceph_mds_client *mdsc,
1710 struct ceph_mds_request *req)
1711{
1712 if (req->r_callback)
1713 req->r_callback(mdsc, req);
1714 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001715 complete_all(&req->r_completion);
Sage Weil2f2dc052009-10-06 11:31:09 -07001716}
1717
1718/*
1719 * called under mdsc->mutex
1720 */
1721static int __prepare_send_request(struct ceph_mds_client *mdsc,
1722 struct ceph_mds_request *req,
1723 int mds)
1724{
1725 struct ceph_mds_request_head *rhead;
1726 struct ceph_msg *msg;
1727 int flags = 0;
1728
Sage Weil2f2dc052009-10-06 11:31:09 -07001729 req->r_attempts++;
Greg Farnume55b71f2010-06-22 15:58:01 -07001730 if (req->r_inode) {
1731 struct ceph_cap *cap =
1732 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
1733
1734 if (cap)
1735 req->r_sent_on_mseq = cap->mseq;
1736 else
1737 req->r_sent_on_mseq = -1;
1738 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001739 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
1740 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
1741
Sage Weil01a92f12010-07-15 13:24:32 -07001742 if (req->r_got_unsafe) {
1743 /*
1744 * Replay. Do not regenerate message (and rebuild
1745 * paths, etc.); just use the original message.
1746 * Rebuilding paths will break for renames because
1747 * d_move mangles the src name.
1748 */
1749 msg = req->r_request;
1750 rhead = msg->front.iov_base;
1751
1752 flags = le32_to_cpu(rhead->flags);
1753 flags |= CEPH_MDS_FLAG_REPLAY;
1754 rhead->flags = cpu_to_le32(flags);
1755
1756 if (req->r_target_inode)
1757 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
1758
1759 rhead->num_retry = req->r_attempts - 1;
Sage Weile979cf52010-07-15 14:58:39 -07001760
1761 /* remove cap/dentry releases from message */
1762 rhead->num_releases = 0;
1763 msg->hdr.front_len = cpu_to_le32(req->r_request_release_offset);
1764 msg->front.iov_len = req->r_request_release_offset;
Sage Weil01a92f12010-07-15 13:24:32 -07001765 return 0;
1766 }
1767
Sage Weil2f2dc052009-10-06 11:31:09 -07001768 if (req->r_request) {
1769 ceph_msg_put(req->r_request);
1770 req->r_request = NULL;
1771 }
1772 msg = create_request_message(mdsc, req, mds);
1773 if (IS_ERR(msg)) {
Sage Weile1518c72010-05-13 11:19:06 -07001774 req->r_err = PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001775 complete_request(mdsc, req);
Sage Weila79832f2010-04-01 16:06:19 -07001776 return PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001777 }
1778 req->r_request = msg;
1779
1780 rhead = msg->front.iov_base;
Sage Weil2f2dc052009-10-06 11:31:09 -07001781 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
1782 if (req->r_got_unsafe)
1783 flags |= CEPH_MDS_FLAG_REPLAY;
1784 if (req->r_locked_dir)
1785 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
1786 rhead->flags = cpu_to_le32(flags);
1787 rhead->num_fwd = req->r_num_fwd;
1788 rhead->num_retry = req->r_attempts - 1;
Sage Weil01a92f12010-07-15 13:24:32 -07001789 rhead->ino = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001790
1791 dout(" r_locked_dir = %p\n", req->r_locked_dir);
Sage Weil2f2dc052009-10-06 11:31:09 -07001792 return 0;
1793}
1794
1795/*
1796 * send request, or put it on the appropriate wait list.
1797 */
1798static int __do_request(struct ceph_mds_client *mdsc,
1799 struct ceph_mds_request *req)
1800{
1801 struct ceph_mds_session *session = NULL;
1802 int mds = -1;
1803 int err = -EAGAIN;
1804
Sage Weile1518c72010-05-13 11:19:06 -07001805 if (req->r_err || req->r_got_result)
Sage Weil2f2dc052009-10-06 11:31:09 -07001806 goto out;
1807
1808 if (req->r_timeout &&
1809 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
1810 dout("do_request timed out\n");
1811 err = -EIO;
1812 goto finish;
1813 }
1814
Sage Weildc69e2e2010-11-02 13:49:00 -07001815 put_request_session(req);
1816
Sage Weil2f2dc052009-10-06 11:31:09 -07001817 mds = __choose_mds(mdsc, req);
1818 if (mds < 0 ||
1819 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
1820 dout("do_request no mds or not active, waiting for map\n");
1821 list_add(&req->r_wait, &mdsc->waiting_for_map);
1822 goto out;
1823 }
1824
1825 /* get, open session */
1826 session = __ceph_lookup_mds_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07001827 if (!session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001828 session = register_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07001829 if (IS_ERR(session)) {
1830 err = PTR_ERR(session);
1831 goto finish;
1832 }
1833 }
Sage Weildc69e2e2010-11-02 13:49:00 -07001834 req->r_session = get_session(session);
1835
Sage Weil2f2dc052009-10-06 11:31:09 -07001836 dout("do_request mds%d session %p state %s\n", mds, session,
1837 session_state_name(session->s_state));
1838 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
1839 session->s_state != CEPH_MDS_SESSION_HUNG) {
1840 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1841 session->s_state == CEPH_MDS_SESSION_CLOSING)
1842 __open_session(mdsc, session);
1843 list_add(&req->r_wait, &session->s_waiting);
1844 goto out_session;
1845 }
1846
1847 /* send request */
Sage Weil2f2dc052009-10-06 11:31:09 -07001848 req->r_resend_mds = -1; /* forget any previous mds hint */
1849
1850 if (req->r_request_started == 0) /* note request start time */
1851 req->r_request_started = jiffies;
1852
1853 err = __prepare_send_request(mdsc, req, mds);
1854 if (!err) {
1855 ceph_msg_get(req->r_request);
1856 ceph_con_send(&session->s_con, req->r_request);
1857 }
1858
1859out_session:
1860 ceph_put_mds_session(session);
1861out:
1862 return err;
1863
1864finish:
Sage Weile1518c72010-05-13 11:19:06 -07001865 req->r_err = err;
Sage Weil2f2dc052009-10-06 11:31:09 -07001866 complete_request(mdsc, req);
1867 goto out;
1868}
1869
1870/*
1871 * called under mdsc->mutex
1872 */
1873static void __wake_requests(struct ceph_mds_client *mdsc,
1874 struct list_head *head)
1875{
1876 struct ceph_mds_request *req, *nreq;
1877
1878 list_for_each_entry_safe(req, nreq, head, r_wait) {
1879 list_del_init(&req->r_wait);
1880 __do_request(mdsc, req);
1881 }
1882}
1883
1884/*
1885 * Wake up threads with requests pending for @mds, so that they can
Sage Weil29790f22010-03-18 14:45:05 -07001886 * resubmit their requests to a possibly different mds.
Sage Weil2f2dc052009-10-06 11:31:09 -07001887 */
Sage Weil29790f22010-03-18 14:45:05 -07001888static void kick_requests(struct ceph_mds_client *mdsc, int mds)
Sage Weil2f2dc052009-10-06 11:31:09 -07001889{
Sage Weil44ca18f2010-02-15 12:08:46 -08001890 struct ceph_mds_request *req;
1891 struct rb_node *p;
Sage Weil2f2dc052009-10-06 11:31:09 -07001892
1893 dout("kick_requests mds%d\n", mds);
Sage Weil44ca18f2010-02-15 12:08:46 -08001894 for (p = rb_first(&mdsc->request_tree); p; p = rb_next(p)) {
1895 req = rb_entry(p, struct ceph_mds_request, r_node);
1896 if (req->r_got_unsafe)
1897 continue;
1898 if (req->r_session &&
1899 req->r_session->s_mds == mds) {
1900 dout(" kicking tid %llu\n", req->r_tid);
Sage Weil44ca18f2010-02-15 12:08:46 -08001901 __do_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07001902 }
1903 }
1904}
1905
1906void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
1907 struct ceph_mds_request *req)
1908{
1909 dout("submit_request on %p\n", req);
1910 mutex_lock(&mdsc->mutex);
1911 __register_request(mdsc, req, NULL);
1912 __do_request(mdsc, req);
1913 mutex_unlock(&mdsc->mutex);
1914}
1915
1916/*
1917 * Synchrously perform an mds request. Take care of all of the
1918 * session setup, forwarding, retry details.
1919 */
1920int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
1921 struct inode *dir,
1922 struct ceph_mds_request *req)
1923{
1924 int err;
1925
1926 dout("do_request on %p\n", req);
1927
1928 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
1929 if (req->r_inode)
1930 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
1931 if (req->r_locked_dir)
1932 ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
1933 if (req->r_old_dentry)
1934 ceph_get_cap_refs(
1935 ceph_inode(req->r_old_dentry->d_parent->d_inode),
1936 CEPH_CAP_PIN);
1937
1938 /* issue */
1939 mutex_lock(&mdsc->mutex);
1940 __register_request(mdsc, req, dir);
1941 __do_request(mdsc, req);
1942
Sage Weile1518c72010-05-13 11:19:06 -07001943 if (req->r_err) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001944 err = req->r_err;
Sage Weile1518c72010-05-13 11:19:06 -07001945 __unregister_request(mdsc, req);
1946 dout("do_request early error %d\n", err);
1947 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07001948 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001949
Sage Weile1518c72010-05-13 11:19:06 -07001950 /* wait */
1951 mutex_unlock(&mdsc->mutex);
1952 dout("do_request waiting\n");
1953 if (req->r_timeout) {
Sage Weilaa916472010-05-24 11:15:51 -07001954 err = (long)wait_for_completion_killable_timeout(
Sage Weile1518c72010-05-13 11:19:06 -07001955 &req->r_completion, req->r_timeout);
1956 if (err == 0)
1957 err = -EIO;
1958 } else {
Sage Weilaa916472010-05-24 11:15:51 -07001959 err = wait_for_completion_killable(&req->r_completion);
Sage Weile1518c72010-05-13 11:19:06 -07001960 }
1961 dout("do_request waited, got %d\n", err);
1962 mutex_lock(&mdsc->mutex);
1963
1964 /* only abort if we didn't race with a real reply */
1965 if (req->r_got_result) {
1966 err = le32_to_cpu(req->r_reply_info.head->result);
1967 } else if (err < 0) {
1968 dout("aborted request %lld with %d\n", req->r_tid, err);
Sage Weilb4556392010-05-13 12:01:13 -07001969
1970 /*
1971 * ensure we aren't running concurrently with
1972 * ceph_fill_trace or ceph_readdir_prepopulate, which
1973 * rely on locks (dir mutex) held by our caller.
1974 */
1975 mutex_lock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07001976 req->r_err = err;
1977 req->r_aborted = true;
Sage Weilb4556392010-05-13 12:01:13 -07001978 mutex_unlock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07001979
1980 if (req->r_locked_dir &&
Sage Weil167c9e32010-05-14 10:02:57 -07001981 (req->r_op & CEPH_MDS_OP_WRITE))
1982 ceph_invalidate_dir_request(req);
Sage Weile1518c72010-05-13 11:19:06 -07001983 } else {
1984 err = req->r_err;
1985 }
1986
1987out:
1988 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07001989 dout("do_request %p done, result %d\n", req, err);
1990 return err;
1991}
1992
1993/*
Sage Weil167c9e32010-05-14 10:02:57 -07001994 * Invalidate dir I_COMPLETE, dentry lease state on an aborted MDS
1995 * namespace request.
1996 */
1997void ceph_invalidate_dir_request(struct ceph_mds_request *req)
1998{
1999 struct inode *inode = req->r_locked_dir;
2000 struct ceph_inode_info *ci = ceph_inode(inode);
2001
2002 dout("invalidate_dir_request %p (I_COMPLETE, lease(s))\n", inode);
2003 spin_lock(&inode->i_lock);
2004 ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
2005 ci->i_release_count++;
2006 spin_unlock(&inode->i_lock);
2007
2008 if (req->r_dentry)
2009 ceph_invalidate_dentry_lease(req->r_dentry);
2010 if (req->r_old_dentry)
2011 ceph_invalidate_dentry_lease(req->r_old_dentry);
2012}
2013
2014/*
Sage Weil2f2dc052009-10-06 11:31:09 -07002015 * Handle mds reply.
2016 *
2017 * We take the session mutex and parse and process the reply immediately.
2018 * This preserves the logical ordering of replies, capabilities, etc., sent
2019 * by the MDS as they are applied to our local cache.
2020 */
2021static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2022{
2023 struct ceph_mds_client *mdsc = session->s_mdsc;
2024 struct ceph_mds_request *req;
2025 struct ceph_mds_reply_head *head = msg->front.iov_base;
2026 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
2027 u64 tid;
2028 int err, result;
Sage Weil2600d2d2010-02-22 15:12:16 -08002029 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002030
Sage Weil2f2dc052009-10-06 11:31:09 -07002031 if (msg->front.iov_len < sizeof(*head)) {
2032 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002033 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002034 return;
2035 }
2036
2037 /* get request, session */
Sage Weil6df058c2009-12-22 11:24:33 -08002038 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002039 mutex_lock(&mdsc->mutex);
2040 req = __lookup_request(mdsc, tid);
2041 if (!req) {
2042 dout("handle_reply on unknown tid %llu\n", tid);
2043 mutex_unlock(&mdsc->mutex);
2044 return;
2045 }
2046 dout("handle_reply %p\n", req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002047
2048 /* correct session? */
Sage Weild96d6042010-03-20 20:50:58 -07002049 if (req->r_session != session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002050 pr_err("mdsc_handle_reply got %llu on session mds%d"
2051 " not mds%d\n", tid, session->s_mds,
2052 req->r_session ? req->r_session->s_mds : -1);
2053 mutex_unlock(&mdsc->mutex);
2054 goto out;
2055 }
2056
2057 /* dup? */
2058 if ((req->r_got_unsafe && !head->safe) ||
2059 (req->r_got_safe && head->safe)) {
2060 pr_warning("got a dup %s reply on %llu from mds%d\n",
2061 head->safe ? "safe" : "unsafe", tid, mds);
2062 mutex_unlock(&mdsc->mutex);
2063 goto out;
2064 }
Sage Weil85792d0d2010-05-13 09:06:02 -07002065 if (req->r_got_safe && !head->safe) {
2066 pr_warning("got unsafe after safe on %llu from mds%d\n",
2067 tid, mds);
2068 mutex_unlock(&mdsc->mutex);
2069 goto out;
2070 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002071
2072 result = le32_to_cpu(head->result);
2073
2074 /*
Greg Farnume55b71f2010-06-22 15:58:01 -07002075 * Handle an ESTALE
2076 * if we're not talking to the authority, send to them
2077 * if the authority has changed while we weren't looking,
2078 * send to new authority
2079 * Otherwise we just have to return an ESTALE
Sage Weil2f2dc052009-10-06 11:31:09 -07002080 */
2081 if (result == -ESTALE) {
Greg Farnume55b71f2010-06-22 15:58:01 -07002082 dout("got ESTALE on request %llu", req->r_tid);
Sage Weil213c99e2010-08-03 10:25:11 -07002083 if (!req->r_inode) {
2084 /* do nothing; not an authority problem */
2085 } else if (req->r_direct_mode != USE_AUTH_MDS) {
Greg Farnume55b71f2010-06-22 15:58:01 -07002086 dout("not using auth, setting for that now");
2087 req->r_direct_mode = USE_AUTH_MDS;
Sage Weil2f2dc052009-10-06 11:31:09 -07002088 __do_request(mdsc, req);
2089 mutex_unlock(&mdsc->mutex);
2090 goto out;
Greg Farnume55b71f2010-06-22 15:58:01 -07002091 } else {
2092 struct ceph_inode_info *ci = ceph_inode(req->r_inode);
Sage Weil4af25fd2010-11-02 13:41:47 -07002093 struct ceph_cap *cap = NULL;
2094
2095 if (req->r_session)
2096 cap = ceph_get_cap_for_mds(ci,
2097 req->r_session->s_mds);
Greg Farnume55b71f2010-06-22 15:58:01 -07002098
2099 dout("already using auth");
2100 if ((!cap || cap != ci->i_auth_cap) ||
2101 (cap->mseq != req->r_sent_on_mseq)) {
2102 dout("but cap changed, so resending");
2103 __do_request(mdsc, req);
2104 mutex_unlock(&mdsc->mutex);
2105 goto out;
2106 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002107 }
Greg Farnume55b71f2010-06-22 15:58:01 -07002108 dout("have to return ESTALE on request %llu", req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002109 }
2110
Greg Farnume55b71f2010-06-22 15:58:01 -07002111
Sage Weil2f2dc052009-10-06 11:31:09 -07002112 if (head->safe) {
2113 req->r_got_safe = true;
2114 __unregister_request(mdsc, req);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002115 complete_all(&req->r_safe_completion);
Sage Weil2f2dc052009-10-06 11:31:09 -07002116
2117 if (req->r_got_unsafe) {
2118 /*
2119 * We already handled the unsafe response, now do the
2120 * cleanup. No need to examine the response; the MDS
2121 * doesn't include any result info in the safe
2122 * response. And even if it did, there is nothing
2123 * useful we could do with a revised return value.
2124 */
2125 dout("got safe reply %llu, mds%d\n", tid, mds);
2126 list_del_init(&req->r_unsafe_item);
2127
2128 /* last unsafe request during umount? */
Sage Weil44ca18f2010-02-15 12:08:46 -08002129 if (mdsc->stopping && !__get_oldest_req(mdsc))
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002130 complete_all(&mdsc->safe_umount_waiters);
Sage Weil2f2dc052009-10-06 11:31:09 -07002131 mutex_unlock(&mdsc->mutex);
2132 goto out;
2133 }
Sage Weile1518c72010-05-13 11:19:06 -07002134 } else {
Sage Weil2f2dc052009-10-06 11:31:09 -07002135 req->r_got_unsafe = true;
2136 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
2137 }
2138
2139 dout("handle_reply tid %lld result %d\n", tid, result);
2140 rinfo = &req->r_reply_info;
Sage Weil14303d22010-12-14 17:37:52 -08002141 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
Sage Weil2f2dc052009-10-06 11:31:09 -07002142 mutex_unlock(&mdsc->mutex);
2143
2144 mutex_lock(&session->s_mutex);
2145 if (err < 0) {
Herb Shiu25933ab2010-12-01 14:14:38 -08002146 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002147 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002148 goto out_err;
2149 }
2150
2151 /* snap trace */
2152 if (rinfo->snapblob_len) {
2153 down_write(&mdsc->snap_rwsem);
2154 ceph_update_snap_trace(mdsc, rinfo->snapblob,
2155 rinfo->snapblob + rinfo->snapblob_len,
2156 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP);
2157 downgrade_write(&mdsc->snap_rwsem);
2158 } else {
2159 down_read(&mdsc->snap_rwsem);
2160 }
2161
2162 /* insert trace into our cache */
Sage Weilb4556392010-05-13 12:01:13 -07002163 mutex_lock(&req->r_fill_mutex);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002164 err = ceph_fill_trace(mdsc->fsc->sb, req, req->r_session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002165 if (err == 0) {
Herb Shiu25933ab2010-12-01 14:14:38 -08002166 if (result == 0 && req->r_op != CEPH_MDS_OP_GETFILELOCK &&
2167 rinfo->dir_nr)
Sage Weil2f2dc052009-10-06 11:31:09 -07002168 ceph_readdir_prepopulate(req, req->r_session);
Yehuda Sadeh37151662010-06-17 16:16:12 -07002169 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
Sage Weil2f2dc052009-10-06 11:31:09 -07002170 }
Sage Weilb4556392010-05-13 12:01:13 -07002171 mutex_unlock(&req->r_fill_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002172
2173 up_read(&mdsc->snap_rwsem);
2174out_err:
Sage Weile1518c72010-05-13 11:19:06 -07002175 mutex_lock(&mdsc->mutex);
2176 if (!req->r_aborted) {
2177 if (err) {
2178 req->r_err = err;
2179 } else {
2180 req->r_reply = msg;
2181 ceph_msg_get(msg);
2182 req->r_got_result = true;
2183 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002184 } else {
Sage Weile1518c72010-05-13 11:19:06 -07002185 dout("reply arrived after request %lld was aborted\n", tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002186 }
Sage Weile1518c72010-05-13 11:19:06 -07002187 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002188
Sage Weilee6b2722010-06-10 12:55:52 -07002189 ceph_add_cap_releases(mdsc, req->r_session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002190 mutex_unlock(&session->s_mutex);
2191
2192 /* kick calling process */
2193 complete_request(mdsc, req);
2194out:
2195 ceph_mdsc_put_request(req);
2196 return;
2197}
2198
2199
2200
2201/*
2202 * handle mds notification that our request has been forwarded.
2203 */
Sage Weil2600d2d2010-02-22 15:12:16 -08002204static void handle_forward(struct ceph_mds_client *mdsc,
2205 struct ceph_mds_session *session,
2206 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002207{
2208 struct ceph_mds_request *req;
Sage Weila1ea7872010-02-23 14:02:44 -08002209 u64 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002210 u32 next_mds;
2211 u32 fwd_seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002212 int err = -EINVAL;
2213 void *p = msg->front.iov_base;
2214 void *end = p + msg->front.iov_len;
Sage Weil2f2dc052009-10-06 11:31:09 -07002215
Sage Weila1ea7872010-02-23 14:02:44 -08002216 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002217 next_mds = ceph_decode_32(&p);
2218 fwd_seq = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07002219
2220 mutex_lock(&mdsc->mutex);
2221 req = __lookup_request(mdsc, tid);
2222 if (!req) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07002223 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002224 goto out; /* dup reply? */
2225 }
2226
Sage Weil2a8e5e32010-05-28 16:43:16 -07002227 if (req->r_aborted) {
2228 dout("forward tid %llu aborted, unregistering\n", tid);
2229 __unregister_request(mdsc, req);
2230 } else if (fwd_seq <= req->r_num_fwd) {
2231 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002232 tid, next_mds, req->r_num_fwd, fwd_seq);
2233 } else {
2234 /* resend. forward race not possible; mds would drop */
Sage Weil2a8e5e32010-05-28 16:43:16 -07002235 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2236 BUG_ON(req->r_err);
2237 BUG_ON(req->r_got_result);
Sage Weil2f2dc052009-10-06 11:31:09 -07002238 req->r_num_fwd = fwd_seq;
2239 req->r_resend_mds = next_mds;
2240 put_request_session(req);
2241 __do_request(mdsc, req);
2242 }
2243 ceph_mdsc_put_request(req);
2244out:
2245 mutex_unlock(&mdsc->mutex);
2246 return;
2247
2248bad:
2249 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2250}
2251
2252/*
2253 * handle a mds session control message
2254 */
2255static void handle_session(struct ceph_mds_session *session,
2256 struct ceph_msg *msg)
2257{
2258 struct ceph_mds_client *mdsc = session->s_mdsc;
2259 u32 op;
2260 u64 seq;
Sage Weil2600d2d2010-02-22 15:12:16 -08002261 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002262 struct ceph_mds_session_head *h = msg->front.iov_base;
2263 int wake = 0;
2264
Sage Weil2f2dc052009-10-06 11:31:09 -07002265 /* decode */
2266 if (msg->front.iov_len != sizeof(*h))
2267 goto bad;
2268 op = le32_to_cpu(h->op);
2269 seq = le64_to_cpu(h->seq);
2270
2271 mutex_lock(&mdsc->mutex);
Sage Weil2600d2d2010-02-22 15:12:16 -08002272 if (op == CEPH_SESSION_CLOSE)
2273 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002274 /* FIXME: this ttl calculation is generous */
2275 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2276 mutex_unlock(&mdsc->mutex);
2277
2278 mutex_lock(&session->s_mutex);
2279
2280 dout("handle_session mds%d %s %p state %s seq %llu\n",
2281 mds, ceph_session_op_name(op), session,
2282 session_state_name(session->s_state), seq);
2283
2284 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2285 session->s_state = CEPH_MDS_SESSION_OPEN;
2286 pr_info("mds%d came back\n", session->s_mds);
2287 }
2288
2289 switch (op) {
2290 case CEPH_SESSION_OPEN:
Sage Weil29790f22010-03-18 14:45:05 -07002291 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2292 pr_info("mds%d reconnect success\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002293 session->s_state = CEPH_MDS_SESSION_OPEN;
2294 renewed_caps(mdsc, session, 0);
2295 wake = 1;
2296 if (mdsc->stopping)
2297 __close_session(mdsc, session);
2298 break;
2299
2300 case CEPH_SESSION_RENEWCAPS:
2301 if (session->s_renew_seq == seq)
2302 renewed_caps(mdsc, session, 1);
2303 break;
2304
2305 case CEPH_SESSION_CLOSE:
Sage Weil29790f22010-03-18 14:45:05 -07002306 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2307 pr_info("mds%d reconnect denied\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002308 remove_session_caps(session);
2309 wake = 1; /* for good measure */
Sage Weilf3c60c52010-08-11 14:51:23 -07002310 wake_up_all(&mdsc->session_close_wq);
Sage Weil29790f22010-03-18 14:45:05 -07002311 kick_requests(mdsc, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002312 break;
2313
2314 case CEPH_SESSION_STALE:
2315 pr_info("mds%d caps went stale, renewing\n",
2316 session->s_mds);
2317 spin_lock(&session->s_cap_lock);
2318 session->s_cap_gen++;
2319 session->s_cap_ttl = 0;
2320 spin_unlock(&session->s_cap_lock);
2321 send_renew_caps(mdsc, session);
2322 break;
2323
2324 case CEPH_SESSION_RECALL_STATE:
2325 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2326 break;
2327
2328 default:
2329 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2330 WARN_ON(1);
2331 }
2332
2333 mutex_unlock(&session->s_mutex);
2334 if (wake) {
2335 mutex_lock(&mdsc->mutex);
2336 __wake_requests(mdsc, &session->s_waiting);
2337 mutex_unlock(&mdsc->mutex);
2338 }
2339 return;
2340
2341bad:
2342 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2343 (int)msg->front.iov_len);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002344 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002345 return;
2346}
2347
2348
2349/*
2350 * called under session->mutex.
2351 */
2352static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2353 struct ceph_mds_session *session)
2354{
2355 struct ceph_mds_request *req, *nreq;
2356 int err;
2357
2358 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2359
2360 mutex_lock(&mdsc->mutex);
2361 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
2362 err = __prepare_send_request(mdsc, req, session->s_mds);
2363 if (!err) {
2364 ceph_msg_get(req->r_request);
2365 ceph_con_send(&session->s_con, req->r_request);
2366 }
2367 }
2368 mutex_unlock(&mdsc->mutex);
2369}
2370
2371/*
2372 * Encode information about a cap for a reconnect with the MDS.
2373 */
Sage Weil2f2dc052009-10-06 11:31:09 -07002374static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2375 void *arg)
2376{
Sage Weil20cb34a2010-05-12 15:21:32 -07002377 union {
2378 struct ceph_mds_cap_reconnect v2;
2379 struct ceph_mds_cap_reconnect_v1 v1;
2380 } rec;
2381 size_t reclen;
Sage Weil2f2dc052009-10-06 11:31:09 -07002382 struct ceph_inode_info *ci;
Sage Weil20cb34a2010-05-12 15:21:32 -07002383 struct ceph_reconnect_state *recon_state = arg;
2384 struct ceph_pagelist *pagelist = recon_state->pagelist;
Sage Weil2f2dc052009-10-06 11:31:09 -07002385 char *path;
2386 int pathlen, err;
2387 u64 pathbase;
2388 struct dentry *dentry;
2389
2390 ci = cap->ci;
2391
2392 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2393 inode, ceph_vinop(inode), cap, cap->cap_id,
2394 ceph_cap_string(cap->issued));
Sage Weil93cea5b2009-12-23 12:21:51 -08002395 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2396 if (err)
2397 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002398
2399 dentry = d_find_alias(inode);
2400 if (dentry) {
2401 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2402 if (IS_ERR(path)) {
2403 err = PTR_ERR(path);
Sage Weile072f8a2010-08-26 09:26:37 -07002404 goto out_dput;
Sage Weil2f2dc052009-10-06 11:31:09 -07002405 }
2406 } else {
2407 path = NULL;
2408 pathlen = 0;
2409 }
Sage Weil93cea5b2009-12-23 12:21:51 -08002410 err = ceph_pagelist_encode_string(pagelist, path, pathlen);
2411 if (err)
Sage Weile072f8a2010-08-26 09:26:37 -07002412 goto out_free;
Sage Weil2f2dc052009-10-06 11:31:09 -07002413
Sage Weil2f2dc052009-10-06 11:31:09 -07002414 spin_lock(&inode->i_lock);
2415 cap->seq = 0; /* reset cap seq */
2416 cap->issue_seq = 0; /* and issue_seq */
Sage Weil20cb34a2010-05-12 15:21:32 -07002417
2418 if (recon_state->flock) {
2419 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
2420 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2421 rec.v2.issued = cpu_to_le32(cap->issued);
2422 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2423 rec.v2.pathbase = cpu_to_le64(pathbase);
2424 rec.v2.flock_len = 0;
2425 reclen = sizeof(rec.v2);
2426 } else {
2427 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
2428 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2429 rec.v1.issued = cpu_to_le32(cap->issued);
2430 rec.v1.size = cpu_to_le64(inode->i_size);
2431 ceph_encode_timespec(&rec.v1.mtime, &inode->i_mtime);
2432 ceph_encode_timespec(&rec.v1.atime, &inode->i_atime);
2433 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2434 rec.v1.pathbase = cpu_to_le64(pathbase);
2435 reclen = sizeof(rec.v1);
2436 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002437 spin_unlock(&inode->i_lock);
2438
Greg Farnum40819f62010-08-02 15:34:23 -07002439 if (recon_state->flock) {
2440 int num_fcntl_locks, num_flock_locks;
Greg Farnumfca44512010-09-17 10:24:02 -07002441 struct ceph_pagelist_cursor trunc_point;
Greg Farnum40819f62010-08-02 15:34:23 -07002442
Greg Farnumfca44512010-09-17 10:24:02 -07002443 ceph_pagelist_set_cursor(pagelist, &trunc_point);
2444 do {
Sage Weil496e5952010-09-22 19:57:10 -07002445 lock_flocks();
Greg Farnumfca44512010-09-17 10:24:02 -07002446 ceph_count_locks(inode, &num_fcntl_locks,
2447 &num_flock_locks);
2448 rec.v2.flock_len = (2*sizeof(u32) +
2449 (num_fcntl_locks+num_flock_locks) *
2450 sizeof(struct ceph_filelock));
Sage Weil496e5952010-09-22 19:57:10 -07002451 unlock_flocks();
Greg Farnum40819f62010-08-02 15:34:23 -07002452
Greg Farnumfca44512010-09-17 10:24:02 -07002453 /* pre-alloc pagelist */
2454 ceph_pagelist_truncate(pagelist, &trunc_point);
2455 err = ceph_pagelist_append(pagelist, &rec, reclen);
2456 if (!err)
2457 err = ceph_pagelist_reserve(pagelist,
2458 rec.v2.flock_len);
2459
2460 /* encode locks */
2461 if (!err) {
Sage Weil496e5952010-09-22 19:57:10 -07002462 lock_flocks();
Greg Farnumfca44512010-09-17 10:24:02 -07002463 err = ceph_encode_locks(inode,
2464 pagelist,
2465 num_fcntl_locks,
2466 num_flock_locks);
Sage Weil496e5952010-09-22 19:57:10 -07002467 unlock_flocks();
Greg Farnumfca44512010-09-17 10:24:02 -07002468 }
2469 } while (err == -ENOSPC);
Sage Weil3612abb2010-09-07 15:59:27 -07002470 } else {
2471 err = ceph_pagelist_append(pagelist, &rec, reclen);
Greg Farnum40819f62010-08-02 15:34:23 -07002472 }
Sage Weil93cea5b2009-12-23 12:21:51 -08002473
Sage Weile072f8a2010-08-26 09:26:37 -07002474out_free:
Sage Weil2f2dc052009-10-06 11:31:09 -07002475 kfree(path);
Sage Weile072f8a2010-08-26 09:26:37 -07002476out_dput:
Sage Weil2f2dc052009-10-06 11:31:09 -07002477 dput(dentry);
Sage Weil93cea5b2009-12-23 12:21:51 -08002478 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002479}
2480
2481
2482/*
2483 * If an MDS fails and recovers, clients need to reconnect in order to
2484 * reestablish shared state. This includes all caps issued through
2485 * this session _and_ the snap_realm hierarchy. Because it's not
2486 * clear which snap realms the mds cares about, we send everything we
2487 * know about.. that ensures we'll then get any new info the
2488 * recovering MDS might have.
2489 *
2490 * This is a relatively heavyweight operation, but it's rare.
2491 *
2492 * called with mdsc->mutex held.
2493 */
Sage Weil34b6c852010-05-10 16:31:25 -07002494static void send_mds_reconnect(struct ceph_mds_client *mdsc,
2495 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07002496{
Sage Weil2f2dc052009-10-06 11:31:09 -07002497 struct ceph_msg *reply;
Sage Weila105f002010-02-15 14:37:55 -08002498 struct rb_node *p;
Sage Weil34b6c852010-05-10 16:31:25 -07002499 int mds = session->s_mds;
Sage Weil9abf82b2010-05-10 21:58:38 -07002500 int err = -ENOMEM;
Sage Weil93cea5b2009-12-23 12:21:51 -08002501 struct ceph_pagelist *pagelist;
Sage Weil20cb34a2010-05-12 15:21:32 -07002502 struct ceph_reconnect_state recon_state;
Sage Weil2f2dc052009-10-06 11:31:09 -07002503
Sage Weil34b6c852010-05-10 16:31:25 -07002504 pr_info("mds%d reconnect start\n", mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002505
Sage Weil93cea5b2009-12-23 12:21:51 -08002506 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
2507 if (!pagelist)
2508 goto fail_nopagelist;
2509 ceph_pagelist_init(pagelist);
2510
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002511 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -07002512 if (!reply)
Sage Weil93cea5b2009-12-23 12:21:51 -08002513 goto fail_nomsg;
Sage Weil93cea5b2009-12-23 12:21:51 -08002514
Sage Weil34b6c852010-05-10 16:31:25 -07002515 mutex_lock(&session->s_mutex);
2516 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
2517 session->s_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002518
Sage Weil34b6c852010-05-10 16:31:25 -07002519 ceph_con_open(&session->s_con,
2520 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
Sage Weil2f2dc052009-10-06 11:31:09 -07002521
Sage Weil34b6c852010-05-10 16:31:25 -07002522 /* replay unsafe requests */
2523 replay_unsafe_requests(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002524
2525 down_read(&mdsc->snap_rwsem);
2526
Sage Weil2f2dc052009-10-06 11:31:09 -07002527 dout("session %p state %s\n", session,
2528 session_state_name(session->s_state));
2529
Sage Weile01a5942010-05-10 15:36:44 -07002530 /* drop old cap expires; we're about to reestablish that state */
2531 discard_cap_releases(mdsc, session);
2532
Sage Weil2f2dc052009-10-06 11:31:09 -07002533 /* traverse this session's caps */
Sage Weil93cea5b2009-12-23 12:21:51 -08002534 err = ceph_pagelist_encode_32(pagelist, session->s_nr_caps);
2535 if (err)
2536 goto fail;
Sage Weil20cb34a2010-05-12 15:21:32 -07002537
2538 recon_state.pagelist = pagelist;
2539 recon_state.flock = session->s_con.peer_features & CEPH_FEATURE_FLOCK;
2540 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
Sage Weil2f2dc052009-10-06 11:31:09 -07002541 if (err < 0)
Sage Weil9abf82b2010-05-10 21:58:38 -07002542 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07002543
2544 /*
2545 * snaprealms. we provide mds with the ino, seq (version), and
2546 * parent for all of our realms. If the mds has any newer info,
2547 * it will tell us.
2548 */
Sage Weila105f002010-02-15 14:37:55 -08002549 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
2550 struct ceph_snap_realm *realm =
2551 rb_entry(p, struct ceph_snap_realm, node);
Sage Weil93cea5b2009-12-23 12:21:51 -08002552 struct ceph_mds_snaprealm_reconnect sr_rec;
Sage Weil2f2dc052009-10-06 11:31:09 -07002553
2554 dout(" adding snap realm %llx seq %lld parent %llx\n",
2555 realm->ino, realm->seq, realm->parent_ino);
Sage Weil93cea5b2009-12-23 12:21:51 -08002556 sr_rec.ino = cpu_to_le64(realm->ino);
2557 sr_rec.seq = cpu_to_le64(realm->seq);
2558 sr_rec.parent = cpu_to_le64(realm->parent_ino);
2559 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
2560 if (err)
2561 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07002562 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002563
Sage Weil93cea5b2009-12-23 12:21:51 -08002564 reply->pagelist = pagelist;
Sage Weil20cb34a2010-05-12 15:21:32 -07002565 if (recon_state.flock)
2566 reply->hdr.version = cpu_to_le16(2);
Sage Weil93cea5b2009-12-23 12:21:51 -08002567 reply->hdr.data_len = cpu_to_le32(pagelist->length);
2568 reply->nr_pages = calc_pages_for(0, pagelist->length);
Sage Weil2f2dc052009-10-06 11:31:09 -07002569 ceph_con_send(&session->s_con, reply);
2570
Sage Weil9abf82b2010-05-10 21:58:38 -07002571 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002572
Sage Weil9abf82b2010-05-10 21:58:38 -07002573 mutex_lock(&mdsc->mutex);
2574 __wake_requests(mdsc, &session->s_waiting);
2575 mutex_unlock(&mdsc->mutex);
2576
Sage Weil2f2dc052009-10-06 11:31:09 -07002577 up_read(&mdsc->snap_rwsem);
Sage Weil2f2dc052009-10-06 11:31:09 -07002578 return;
2579
Sage Weil93cea5b2009-12-23 12:21:51 -08002580fail:
Sage Weil2f2dc052009-10-06 11:31:09 -07002581 ceph_msg_put(reply);
Sage Weil9abf82b2010-05-10 21:58:38 -07002582 up_read(&mdsc->snap_rwsem);
2583 mutex_unlock(&session->s_mutex);
Sage Weil93cea5b2009-12-23 12:21:51 -08002584fail_nomsg:
2585 ceph_pagelist_release(pagelist);
2586 kfree(pagelist);
2587fail_nopagelist:
Sage Weil9abf82b2010-05-10 21:58:38 -07002588 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
Sage Weil9abf82b2010-05-10 21:58:38 -07002589 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07002590}
2591
2592
2593/*
2594 * compare old and new mdsmaps, kicking requests
2595 * and closing out old connections as necessary
2596 *
2597 * called under mdsc->mutex.
2598 */
2599static void check_new_map(struct ceph_mds_client *mdsc,
2600 struct ceph_mdsmap *newmap,
2601 struct ceph_mdsmap *oldmap)
2602{
2603 int i;
2604 int oldstate, newstate;
2605 struct ceph_mds_session *s;
2606
2607 dout("check_new_map new %u old %u\n",
2608 newmap->m_epoch, oldmap->m_epoch);
2609
2610 for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
2611 if (mdsc->sessions[i] == NULL)
2612 continue;
2613 s = mdsc->sessions[i];
2614 oldstate = ceph_mdsmap_get_state(oldmap, i);
2615 newstate = ceph_mdsmap_get_state(newmap, i);
2616
Sage Weil0deb01c2010-06-17 14:19:01 -07002617 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002618 i, ceph_mds_state_name(oldstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07002619 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
Sage Weil2f2dc052009-10-06 11:31:09 -07002620 ceph_mds_state_name(newstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07002621 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
Sage Weil2f2dc052009-10-06 11:31:09 -07002622 session_state_name(s->s_state));
2623
2624 if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
2625 ceph_mdsmap_get_addr(newmap, i),
2626 sizeof(struct ceph_entity_addr))) {
2627 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
2628 /* the session never opened, just close it
2629 * out now */
2630 __wake_requests(mdsc, &s->s_waiting);
Sage Weil2600d2d2010-02-22 15:12:16 -08002631 __unregister_session(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07002632 } else {
2633 /* just close it */
2634 mutex_unlock(&mdsc->mutex);
2635 mutex_lock(&s->s_mutex);
2636 mutex_lock(&mdsc->mutex);
2637 ceph_con_close(&s->s_con);
2638 mutex_unlock(&s->s_mutex);
2639 s->s_state = CEPH_MDS_SESSION_RESTARTING;
2640 }
2641
2642 /* kick any requests waiting on the recovering mds */
Sage Weil29790f22010-03-18 14:45:05 -07002643 kick_requests(mdsc, i);
Sage Weil2f2dc052009-10-06 11:31:09 -07002644 } else if (oldstate == newstate) {
2645 continue; /* nothing new with this mds */
2646 }
2647
2648 /*
2649 * send reconnect?
2650 */
2651 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
Sage Weil34b6c852010-05-10 16:31:25 -07002652 newstate >= CEPH_MDS_STATE_RECONNECT) {
2653 mutex_unlock(&mdsc->mutex);
2654 send_mds_reconnect(mdsc, s);
2655 mutex_lock(&mdsc->mutex);
2656 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002657
2658 /*
Sage Weil29790f22010-03-18 14:45:05 -07002659 * kick request on any mds that has gone active.
Sage Weil2f2dc052009-10-06 11:31:09 -07002660 */
2661 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
2662 newstate >= CEPH_MDS_STATE_ACTIVE) {
Sage Weil29790f22010-03-18 14:45:05 -07002663 if (oldstate != CEPH_MDS_STATE_CREATING &&
2664 oldstate != CEPH_MDS_STATE_STARTING)
2665 pr_info("mds%d recovery completed\n", s->s_mds);
2666 kick_requests(mdsc, i);
Sage Weil2f2dc052009-10-06 11:31:09 -07002667 ceph_kick_flushing_caps(mdsc, s);
Sage Weil0dc25702009-11-20 13:43:45 -08002668 wake_up_session_caps(s, 1);
Sage Weil2f2dc052009-10-06 11:31:09 -07002669 }
2670 }
Sage Weilcb170a22010-06-21 13:38:35 -07002671
2672 for (i = 0; i < newmap->m_max_mds && i < mdsc->max_sessions; i++) {
2673 s = mdsc->sessions[i];
2674 if (!s)
2675 continue;
2676 if (!ceph_mdsmap_is_laggy(newmap, i))
2677 continue;
2678 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2679 s->s_state == CEPH_MDS_SESSION_HUNG ||
2680 s->s_state == CEPH_MDS_SESSION_CLOSING) {
2681 dout(" connecting to export targets of laggy mds%d\n",
2682 i);
2683 __open_export_target_sessions(mdsc, s);
2684 }
2685 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002686}
2687
2688
2689
2690/*
2691 * leases
2692 */
2693
2694/*
2695 * caller must hold session s_mutex, dentry->d_lock
2696 */
2697void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
2698{
2699 struct ceph_dentry_info *di = ceph_dentry(dentry);
2700
2701 ceph_put_mds_session(di->lease_session);
2702 di->lease_session = NULL;
2703}
2704
Sage Weil2600d2d2010-02-22 15:12:16 -08002705static void handle_lease(struct ceph_mds_client *mdsc,
2706 struct ceph_mds_session *session,
2707 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002708{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002709 struct super_block *sb = mdsc->fsc->sb;
Sage Weil2f2dc052009-10-06 11:31:09 -07002710 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07002711 struct dentry *parent, *dentry;
2712 struct ceph_dentry_info *di;
Sage Weil2600d2d2010-02-22 15:12:16 -08002713 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002714 struct ceph_mds_lease *h = msg->front.iov_base;
Sage Weil1e5ea232010-06-04 10:05:40 -07002715 u32 seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002716 struct ceph_vino vino;
Sage Weil2f2dc052009-10-06 11:31:09 -07002717 struct qstr dname;
2718 int release = 0;
2719
Sage Weil2f2dc052009-10-06 11:31:09 -07002720 dout("handle_lease from mds%d\n", mds);
2721
2722 /* decode */
2723 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
2724 goto bad;
2725 vino.ino = le64_to_cpu(h->ino);
2726 vino.snap = CEPH_NOSNAP;
Sage Weil1e5ea232010-06-04 10:05:40 -07002727 seq = le32_to_cpu(h->seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002728 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
2729 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
2730 if (dname.len != get_unaligned_le32(h+1))
2731 goto bad;
2732
Sage Weil2f2dc052009-10-06 11:31:09 -07002733 mutex_lock(&session->s_mutex);
2734 session->s_seq++;
2735
2736 /* lookup inode */
2737 inode = ceph_find_inode(sb, vino);
Sage Weil2f90b852011-07-26 11:28:25 -07002738 dout("handle_lease %s, ino %llx %p %.*s\n",
2739 ceph_lease_op_name(h->action), vino.ino, inode,
Sage Weil1e5ea232010-06-04 10:05:40 -07002740 dname.len, dname.name);
Sage Weil2f2dc052009-10-06 11:31:09 -07002741 if (inode == NULL) {
2742 dout("handle_lease no inode %llx\n", vino.ino);
2743 goto release;
2744 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002745
2746 /* dentry */
2747 parent = d_find_alias(inode);
2748 if (!parent) {
2749 dout("no parent dentry on inode %p\n", inode);
2750 WARN_ON(1);
2751 goto release; /* hrm... */
2752 }
2753 dname.hash = full_name_hash(dname.name, dname.len);
2754 dentry = d_lookup(parent, &dname);
2755 dput(parent);
2756 if (!dentry)
2757 goto release;
2758
2759 spin_lock(&dentry->d_lock);
2760 di = ceph_dentry(dentry);
2761 switch (h->action) {
2762 case CEPH_MDS_LEASE_REVOKE:
2763 if (di && di->lease_session == session) {
Sage Weil1e5ea232010-06-04 10:05:40 -07002764 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
2765 h->seq = cpu_to_le32(di->lease_seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002766 __ceph_mdsc_drop_dentry_lease(dentry);
2767 }
2768 release = 1;
2769 break;
2770
2771 case CEPH_MDS_LEASE_RENEW:
2772 if (di && di->lease_session == session &&
2773 di->lease_gen == session->s_cap_gen &&
2774 di->lease_renew_from &&
2775 di->lease_renew_after == 0) {
2776 unsigned long duration =
2777 le32_to_cpu(h->duration_ms) * HZ / 1000;
2778
Sage Weil1e5ea232010-06-04 10:05:40 -07002779 di->lease_seq = seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002780 dentry->d_time = di->lease_renew_from + duration;
2781 di->lease_renew_after = di->lease_renew_from +
2782 (duration >> 1);
2783 di->lease_renew_from = 0;
2784 }
2785 break;
2786 }
2787 spin_unlock(&dentry->d_lock);
2788 dput(dentry);
2789
2790 if (!release)
2791 goto out;
2792
2793release:
2794 /* let's just reuse the same message */
2795 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
2796 ceph_msg_get(msg);
2797 ceph_con_send(&session->s_con, msg);
2798
2799out:
2800 iput(inode);
2801 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002802 return;
2803
2804bad:
2805 pr_err("corrupt lease message\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002806 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002807}
2808
2809void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
2810 struct inode *inode,
2811 struct dentry *dentry, char action,
2812 u32 seq)
2813{
2814 struct ceph_msg *msg;
2815 struct ceph_mds_lease *lease;
2816 int len = sizeof(*lease) + sizeof(u32);
2817 int dnamelen = 0;
2818
2819 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
2820 inode, dentry, ceph_lease_op_name(action), session->s_mds);
2821 dnamelen = dentry->d_name.len;
2822 len += dnamelen;
2823
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002824 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -07002825 if (!msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002826 return;
2827 lease = msg->front.iov_base;
2828 lease->action = action;
Sage Weil2f2dc052009-10-06 11:31:09 -07002829 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
2830 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
2831 lease->seq = cpu_to_le32(seq);
2832 put_unaligned_le32(dnamelen, lease + 1);
2833 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
2834
2835 /*
2836 * if this is a preemptive lease RELEASE, no need to
2837 * flush request stream, since the actual request will
2838 * soon follow.
2839 */
2840 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
2841
2842 ceph_con_send(&session->s_con, msg);
2843}
2844
2845/*
2846 * Preemptively release a lease we expect to invalidate anyway.
2847 * Pass @inode always, @dentry is optional.
2848 */
2849void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
Sage Weil2f90b852011-07-26 11:28:25 -07002850 struct dentry *dentry)
Sage Weil2f2dc052009-10-06 11:31:09 -07002851{
2852 struct ceph_dentry_info *di;
2853 struct ceph_mds_session *session;
2854 u32 seq;
2855
2856 BUG_ON(inode == NULL);
2857 BUG_ON(dentry == NULL);
Sage Weil2f2dc052009-10-06 11:31:09 -07002858
2859 /* is dentry lease valid? */
2860 spin_lock(&dentry->d_lock);
2861 di = ceph_dentry(dentry);
2862 if (!di || !di->lease_session ||
2863 di->lease_session->s_mds < 0 ||
2864 di->lease_gen != di->lease_session->s_cap_gen ||
2865 !time_before(jiffies, dentry->d_time)) {
2866 dout("lease_release inode %p dentry %p -- "
Sage Weil2f90b852011-07-26 11:28:25 -07002867 "no lease\n",
2868 inode, dentry);
Sage Weil2f2dc052009-10-06 11:31:09 -07002869 spin_unlock(&dentry->d_lock);
2870 return;
2871 }
2872
2873 /* we do have a lease on this dentry; note mds and seq */
2874 session = ceph_get_mds_session(di->lease_session);
2875 seq = di->lease_seq;
2876 __ceph_mdsc_drop_dentry_lease(dentry);
2877 spin_unlock(&dentry->d_lock);
2878
Sage Weil2f90b852011-07-26 11:28:25 -07002879 dout("lease_release inode %p dentry %p to mds%d\n",
2880 inode, dentry, session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002881 ceph_mdsc_lease_send_msg(session, inode, dentry,
2882 CEPH_MDS_LEASE_RELEASE, seq);
2883 ceph_put_mds_session(session);
2884}
2885
2886/*
2887 * drop all leases (and dentry refs) in preparation for umount
2888 */
2889static void drop_leases(struct ceph_mds_client *mdsc)
2890{
2891 int i;
2892
2893 dout("drop_leases\n");
2894 mutex_lock(&mdsc->mutex);
2895 for (i = 0; i < mdsc->max_sessions; i++) {
2896 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2897 if (!s)
2898 continue;
2899 mutex_unlock(&mdsc->mutex);
2900 mutex_lock(&s->s_mutex);
2901 mutex_unlock(&s->s_mutex);
2902 ceph_put_mds_session(s);
2903 mutex_lock(&mdsc->mutex);
2904 }
2905 mutex_unlock(&mdsc->mutex);
2906}
2907
2908
2909
2910/*
2911 * delayed work -- periodically trim expired leases, renew caps with mds
2912 */
2913static void schedule_delayed(struct ceph_mds_client *mdsc)
2914{
2915 int delay = 5;
2916 unsigned hz = round_jiffies_relative(HZ * delay);
2917 schedule_delayed_work(&mdsc->delayed_work, hz);
2918}
2919
2920static void delayed_work(struct work_struct *work)
2921{
2922 int i;
2923 struct ceph_mds_client *mdsc =
2924 container_of(work, struct ceph_mds_client, delayed_work.work);
2925 int renew_interval;
2926 int renew_caps;
2927
2928 dout("mdsc delayed_work\n");
Sage Weilafcdaea2009-10-14 14:27:38 -07002929 ceph_check_delayed_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07002930
2931 mutex_lock(&mdsc->mutex);
2932 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
2933 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
2934 mdsc->last_renew_caps);
2935 if (renew_caps)
2936 mdsc->last_renew_caps = jiffies;
2937
2938 for (i = 0; i < mdsc->max_sessions; i++) {
2939 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2940 if (s == NULL)
2941 continue;
2942 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
2943 dout("resending session close request for mds%d\n",
2944 s->s_mds);
2945 request_close_session(mdsc, s);
2946 ceph_put_mds_session(s);
2947 continue;
2948 }
2949 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
2950 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
2951 s->s_state = CEPH_MDS_SESSION_HUNG;
2952 pr_info("mds%d hung\n", s->s_mds);
2953 }
2954 }
2955 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
2956 /* this mds is failed or recovering, just wait */
2957 ceph_put_mds_session(s);
2958 continue;
2959 }
2960 mutex_unlock(&mdsc->mutex);
2961
2962 mutex_lock(&s->s_mutex);
2963 if (renew_caps)
2964 send_renew_caps(mdsc, s);
2965 else
2966 ceph_con_keepalive(&s->s_con);
Sage Weilee6b2722010-06-10 12:55:52 -07002967 ceph_add_cap_releases(mdsc, s);
Sage Weilaab53dd2010-03-17 16:30:21 -07002968 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2969 s->s_state == CEPH_MDS_SESSION_HUNG)
Sage Weil3d7ded42010-06-09 16:47:10 -07002970 ceph_send_cap_releases(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07002971 mutex_unlock(&s->s_mutex);
2972 ceph_put_mds_session(s);
2973
2974 mutex_lock(&mdsc->mutex);
2975 }
2976 mutex_unlock(&mdsc->mutex);
2977
2978 schedule_delayed(mdsc);
2979}
2980
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002981int ceph_mdsc_init(struct ceph_fs_client *fsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07002982
Sage Weil2f2dc052009-10-06 11:31:09 -07002983{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002984 struct ceph_mds_client *mdsc;
2985
2986 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
2987 if (!mdsc)
2988 return -ENOMEM;
2989 mdsc->fsc = fsc;
2990 fsc->mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07002991 mutex_init(&mdsc->mutex);
2992 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08002993 if (mdsc->mdsmap == NULL)
2994 return -ENOMEM;
2995
Sage Weil2f2dc052009-10-06 11:31:09 -07002996 init_completion(&mdsc->safe_umount_waiters);
Sage Weilf3c60c52010-08-11 14:51:23 -07002997 init_waitqueue_head(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002998 INIT_LIST_HEAD(&mdsc->waiting_for_map);
2999 mdsc->sessions = NULL;
3000 mdsc->max_sessions = 0;
3001 mdsc->stopping = 0;
3002 init_rwsem(&mdsc->snap_rwsem);
Sage Weila105f002010-02-15 14:37:55 -08003003 mdsc->snap_realms = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07003004 INIT_LIST_HEAD(&mdsc->snap_empty);
3005 spin_lock_init(&mdsc->snap_empty_lock);
3006 mdsc->last_tid = 0;
Sage Weil44ca18f2010-02-15 12:08:46 -08003007 mdsc->request_tree = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07003008 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
3009 mdsc->last_renew_caps = jiffies;
3010 INIT_LIST_HEAD(&mdsc->cap_delay_list);
3011 spin_lock_init(&mdsc->cap_delay_lock);
3012 INIT_LIST_HEAD(&mdsc->snap_flush_list);
3013 spin_lock_init(&mdsc->snap_flush_lock);
3014 mdsc->cap_flush_seq = 0;
3015 INIT_LIST_HEAD(&mdsc->cap_dirty);
Sage Weildb354052011-05-24 11:46:31 -07003016 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
Sage Weil2f2dc052009-10-06 11:31:09 -07003017 mdsc->num_cap_flushing = 0;
3018 spin_lock_init(&mdsc->cap_dirty_lock);
3019 init_waitqueue_head(&mdsc->cap_flushing_wq);
3020 spin_lock_init(&mdsc->dentry_lru_lock);
3021 INIT_LIST_HEAD(&mdsc->dentry_lru);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08003022
Yehuda Sadeh37151662010-06-17 16:16:12 -07003023 ceph_caps_init(mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003024 ceph_adjust_min_caps(mdsc, fsc->min_caps);
Yehuda Sadeh37151662010-06-17 16:16:12 -07003025
Sage Weil5f44f142009-11-18 14:52:18 -08003026 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003027}
3028
3029/*
3030 * Wait for safe replies on open mds requests. If we time out, drop
3031 * all requests from the tree to avoid dangling dentry refs.
3032 */
3033static void wait_requests(struct ceph_mds_client *mdsc)
3034{
3035 struct ceph_mds_request *req;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003036 struct ceph_fs_client *fsc = mdsc->fsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07003037
3038 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003039 if (__get_oldest_req(mdsc)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003040 mutex_unlock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003041
Sage Weil2f2dc052009-10-06 11:31:09 -07003042 dout("wait_requests waiting for requests\n");
3043 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003044 fsc->client->options->mount_timeout * HZ);
Sage Weil2f2dc052009-10-06 11:31:09 -07003045
3046 /* tear down remaining requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08003047 mutex_lock(&mdsc->mutex);
3048 while ((req = __get_oldest_req(mdsc))) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003049 dout("wait_requests timed out on tid %llu\n",
3050 req->r_tid);
Sage Weil44ca18f2010-02-15 12:08:46 -08003051 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07003052 }
3053 }
3054 mutex_unlock(&mdsc->mutex);
3055 dout("wait_requests done\n");
3056}
3057
3058/*
3059 * called before mount is ro, and before dentries are torn down.
3060 * (hmm, does this still race with new lookups?)
3061 */
3062void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
3063{
3064 dout("pre_umount\n");
3065 mdsc->stopping = 1;
3066
3067 drop_leases(mdsc);
Sage Weilafcdaea2009-10-14 14:27:38 -07003068 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003069 wait_requests(mdsc);
Sage Weil17c688c2010-06-21 16:12:26 -07003070
3071 /*
3072 * wait for reply handlers to drop their request refs and
3073 * their inode/dcache refs
3074 */
3075 ceph_msgr_flush();
Sage Weil2f2dc052009-10-06 11:31:09 -07003076}
3077
3078/*
3079 * wait for all write mds requests to flush.
3080 */
3081static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
3082{
Sage Weil80fc7312010-03-16 15:28:54 -07003083 struct ceph_mds_request *req = NULL, *nextreq;
Sage Weil44ca18f2010-02-15 12:08:46 -08003084 struct rb_node *n;
Sage Weil2f2dc052009-10-06 11:31:09 -07003085
3086 mutex_lock(&mdsc->mutex);
3087 dout("wait_unsafe_requests want %lld\n", want_tid);
Sage Weil80fc7312010-03-16 15:28:54 -07003088restart:
Sage Weil44ca18f2010-02-15 12:08:46 -08003089 req = __get_oldest_req(mdsc);
3090 while (req && req->r_tid <= want_tid) {
Sage Weil80fc7312010-03-16 15:28:54 -07003091 /* find next request */
3092 n = rb_next(&req->r_node);
3093 if (n)
3094 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
3095 else
3096 nextreq = NULL;
Sage Weil44ca18f2010-02-15 12:08:46 -08003097 if ((req->r_op & CEPH_MDS_OP_WRITE)) {
3098 /* write op */
3099 ceph_mdsc_get_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07003100 if (nextreq)
3101 ceph_mdsc_get_request(nextreq);
Sage Weil44ca18f2010-02-15 12:08:46 -08003102 mutex_unlock(&mdsc->mutex);
3103 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3104 req->r_tid, want_tid);
3105 wait_for_completion(&req->r_safe_completion);
3106 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003107 ceph_mdsc_put_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07003108 if (!nextreq)
3109 break; /* next dne before, so we're done! */
3110 if (RB_EMPTY_NODE(&nextreq->r_node)) {
3111 /* next request was removed from tree */
3112 ceph_mdsc_put_request(nextreq);
3113 goto restart;
3114 }
3115 ceph_mdsc_put_request(nextreq); /* won't go away */
Sage Weil44ca18f2010-02-15 12:08:46 -08003116 }
Sage Weil80fc7312010-03-16 15:28:54 -07003117 req = nextreq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003118 }
3119 mutex_unlock(&mdsc->mutex);
3120 dout("wait_unsafe_requests done\n");
3121}
3122
3123void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
3124{
3125 u64 want_tid, want_flush;
3126
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003127 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
Sage Weil56b7cf92010-05-03 15:22:00 -07003128 return;
3129
Sage Weil2f2dc052009-10-06 11:31:09 -07003130 dout("sync\n");
3131 mutex_lock(&mdsc->mutex);
3132 want_tid = mdsc->last_tid;
3133 want_flush = mdsc->cap_flush_seq;
3134 mutex_unlock(&mdsc->mutex);
3135 dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
3136
Sage Weilafcdaea2009-10-14 14:27:38 -07003137 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003138
3139 wait_unsafe_requests(mdsc, want_tid);
3140 wait_event(mdsc->cap_flushing_wq, check_cap_flush(mdsc, want_flush));
3141}
3142
Sage Weilf3c60c52010-08-11 14:51:23 -07003143/*
3144 * true if all sessions are closed, or we force unmount
3145 */
3146bool done_closing_sessions(struct ceph_mds_client *mdsc)
3147{
3148 int i, n = 0;
3149
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003150 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
Sage Weilf3c60c52010-08-11 14:51:23 -07003151 return true;
3152
3153 mutex_lock(&mdsc->mutex);
3154 for (i = 0; i < mdsc->max_sessions; i++)
3155 if (mdsc->sessions[i])
3156 n++;
3157 mutex_unlock(&mdsc->mutex);
3158 return n == 0;
3159}
Sage Weil2f2dc052009-10-06 11:31:09 -07003160
3161/*
3162 * called after sb is ro.
3163 */
3164void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
3165{
3166 struct ceph_mds_session *session;
3167 int i;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003168 struct ceph_fs_client *fsc = mdsc->fsc;
3169 unsigned long timeout = fsc->client->options->mount_timeout * HZ;
Sage Weil2f2dc052009-10-06 11:31:09 -07003170
3171 dout("close_sessions\n");
3172
Sage Weil2f2dc052009-10-06 11:31:09 -07003173 /* close sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07003174 mutex_lock(&mdsc->mutex);
3175 for (i = 0; i < mdsc->max_sessions; i++) {
3176 session = __ceph_lookup_mds_session(mdsc, i);
3177 if (!session)
3178 continue;
Sage Weil2f2dc052009-10-06 11:31:09 -07003179 mutex_unlock(&mdsc->mutex);
Sage Weilf3c60c52010-08-11 14:51:23 -07003180 mutex_lock(&session->s_mutex);
3181 __close_session(mdsc, session);
3182 mutex_unlock(&session->s_mutex);
3183 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003184 mutex_lock(&mdsc->mutex);
3185 }
Sage Weilf3c60c52010-08-11 14:51:23 -07003186 mutex_unlock(&mdsc->mutex);
3187
3188 dout("waiting for sessions to close\n");
3189 wait_event_timeout(mdsc->session_close_wq, done_closing_sessions(mdsc),
3190 timeout);
Sage Weil2f2dc052009-10-06 11:31:09 -07003191
3192 /* tear down remaining sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07003193 mutex_lock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003194 for (i = 0; i < mdsc->max_sessions; i++) {
3195 if (mdsc->sessions[i]) {
3196 session = get_session(mdsc->sessions[i]);
Sage Weil2600d2d2010-02-22 15:12:16 -08003197 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003198 mutex_unlock(&mdsc->mutex);
3199 mutex_lock(&session->s_mutex);
3200 remove_session_caps(session);
3201 mutex_unlock(&session->s_mutex);
3202 ceph_put_mds_session(session);
3203 mutex_lock(&mdsc->mutex);
3204 }
3205 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003206 WARN_ON(!list_empty(&mdsc->cap_delay_list));
Sage Weil2f2dc052009-10-06 11:31:09 -07003207 mutex_unlock(&mdsc->mutex);
3208
3209 ceph_cleanup_empty_realms(mdsc);
3210
3211 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3212
3213 dout("stopped\n");
3214}
3215
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003216static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07003217{
3218 dout("stop\n");
3219 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3220 if (mdsc->mdsmap)
3221 ceph_mdsmap_destroy(mdsc->mdsmap);
3222 kfree(mdsc->sessions);
Yehuda Sadeh37151662010-06-17 16:16:12 -07003223 ceph_caps_finalize(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003224}
3225
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003226void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
3227{
3228 struct ceph_mds_client *mdsc = fsc->mdsc;
3229
Sage Weilef550f62011-03-25 13:27:48 -07003230 dout("mdsc_destroy %p\n", mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003231 ceph_mdsc_stop(mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07003232
3233 /* flush out any connection work with references to us */
3234 ceph_msgr_flush();
3235
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003236 fsc->mdsc = NULL;
3237 kfree(mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07003238 dout("mdsc_destroy %p done\n", mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003239}
3240
Sage Weil2f2dc052009-10-06 11:31:09 -07003241
3242/*
3243 * handle mds map update.
3244 */
3245void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3246{
3247 u32 epoch;
3248 u32 maplen;
3249 void *p = msg->front.iov_base;
3250 void *end = p + msg->front.iov_len;
3251 struct ceph_mdsmap *newmap, *oldmap;
3252 struct ceph_fsid fsid;
3253 int err = -EINVAL;
3254
3255 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
3256 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003257 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
Sage Weil07433042009-11-18 16:50:41 -08003258 return;
Sage Weilc89136e2009-10-14 09:59:09 -07003259 epoch = ceph_decode_32(&p);
3260 maplen = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07003261 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
3262
3263 /* do we need it? */
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003264 ceph_monc_got_mdsmap(&mdsc->fsc->client->monc, epoch);
Sage Weil2f2dc052009-10-06 11:31:09 -07003265 mutex_lock(&mdsc->mutex);
3266 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
3267 dout("handle_map epoch %u <= our %u\n",
3268 epoch, mdsc->mdsmap->m_epoch);
3269 mutex_unlock(&mdsc->mutex);
3270 return;
3271 }
3272
3273 newmap = ceph_mdsmap_decode(&p, end);
3274 if (IS_ERR(newmap)) {
3275 err = PTR_ERR(newmap);
3276 goto bad_unlock;
3277 }
3278
3279 /* swap into place */
3280 if (mdsc->mdsmap) {
3281 oldmap = mdsc->mdsmap;
3282 mdsc->mdsmap = newmap;
3283 check_new_map(mdsc, newmap, oldmap);
3284 ceph_mdsmap_destroy(oldmap);
3285 } else {
3286 mdsc->mdsmap = newmap; /* first mds map */
3287 }
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003288 mdsc->fsc->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
Sage Weil2f2dc052009-10-06 11:31:09 -07003289
3290 __wake_requests(mdsc, &mdsc->waiting_for_map);
3291
3292 mutex_unlock(&mdsc->mutex);
3293 schedule_delayed(mdsc);
3294 return;
3295
3296bad_unlock:
3297 mutex_unlock(&mdsc->mutex);
3298bad:
3299 pr_err("error decoding mdsmap %d\n", err);
3300 return;
3301}
3302
3303static struct ceph_connection *con_get(struct ceph_connection *con)
3304{
3305 struct ceph_mds_session *s = con->private;
3306
3307 if (get_session(s)) {
Sage Weil2600d2d2010-02-22 15:12:16 -08003308 dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -07003309 return con;
3310 }
3311 dout("mdsc con_get %p FAIL\n", s);
3312 return NULL;
3313}
3314
3315static void con_put(struct ceph_connection *con)
3316{
3317 struct ceph_mds_session *s = con->private;
3318
Henry C Chang7d8e18a2011-05-11 10:29:52 +00003319 dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref) - 1);
Sage Weil2f2dc052009-10-06 11:31:09 -07003320 ceph_put_mds_session(s);
3321}
3322
3323/*
3324 * if the client is unresponsive for long enough, the mds will kill
3325 * the session entirely.
3326 */
3327static void peer_reset(struct ceph_connection *con)
3328{
3329 struct ceph_mds_session *s = con->private;
Sage Weil7e70f0e2010-03-18 13:59:12 -07003330 struct ceph_mds_client *mdsc = s->s_mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07003331
Sage Weil7e70f0e2010-03-18 13:59:12 -07003332 pr_warning("mds%d closed our session\n", s->s_mds);
3333 send_mds_reconnect(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07003334}
3335
3336static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
3337{
3338 struct ceph_mds_session *s = con->private;
3339 struct ceph_mds_client *mdsc = s->s_mdsc;
3340 int type = le16_to_cpu(msg->hdr.type);
3341
Sage Weil2600d2d2010-02-22 15:12:16 -08003342 mutex_lock(&mdsc->mutex);
3343 if (__verify_registered_session(mdsc, s) < 0) {
3344 mutex_unlock(&mdsc->mutex);
3345 goto out;
3346 }
3347 mutex_unlock(&mdsc->mutex);
3348
Sage Weil2f2dc052009-10-06 11:31:09 -07003349 switch (type) {
3350 case CEPH_MSG_MDS_MAP:
3351 ceph_mdsc_handle_map(mdsc, msg);
3352 break;
3353 case CEPH_MSG_CLIENT_SESSION:
3354 handle_session(s, msg);
3355 break;
3356 case CEPH_MSG_CLIENT_REPLY:
3357 handle_reply(s, msg);
3358 break;
3359 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
Sage Weil2600d2d2010-02-22 15:12:16 -08003360 handle_forward(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003361 break;
3362 case CEPH_MSG_CLIENT_CAPS:
3363 ceph_handle_caps(s, msg);
3364 break;
3365 case CEPH_MSG_CLIENT_SNAP:
Sage Weil2600d2d2010-02-22 15:12:16 -08003366 ceph_handle_snap(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003367 break;
3368 case CEPH_MSG_CLIENT_LEASE:
Sage Weil2600d2d2010-02-22 15:12:16 -08003369 handle_lease(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003370 break;
3371
3372 default:
3373 pr_err("received unknown message type %d %s\n", type,
3374 ceph_msg_type_name(type));
3375 }
Sage Weil2600d2d2010-02-22 15:12:16 -08003376out:
Sage Weil2f2dc052009-10-06 11:31:09 -07003377 ceph_msg_put(msg);
3378}
3379
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003380/*
3381 * authentication
3382 */
3383static int get_authorizer(struct ceph_connection *con,
3384 void **buf, int *len, int *proto,
3385 void **reply_buf, int *reply_len, int force_new)
3386{
3387 struct ceph_mds_session *s = con->private;
3388 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003389 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003390 int ret = 0;
3391
3392 if (force_new && s->s_authorizer) {
3393 ac->ops->destroy_authorizer(ac, s->s_authorizer);
3394 s->s_authorizer = NULL;
3395 }
3396 if (s->s_authorizer == NULL) {
3397 if (ac->ops->create_authorizer) {
3398 ret = ac->ops->create_authorizer(
3399 ac, CEPH_ENTITY_TYPE_MDS,
3400 &s->s_authorizer,
3401 &s->s_authorizer_buf,
3402 &s->s_authorizer_buf_len,
3403 &s->s_authorizer_reply_buf,
3404 &s->s_authorizer_reply_buf_len);
3405 if (ret)
3406 return ret;
3407 }
3408 }
3409
3410 *proto = ac->protocol;
3411 *buf = s->s_authorizer_buf;
3412 *len = s->s_authorizer_buf_len;
3413 *reply_buf = s->s_authorizer_reply_buf;
3414 *reply_len = s->s_authorizer_reply_buf_len;
3415 return 0;
3416}
3417
3418
3419static int verify_authorizer_reply(struct ceph_connection *con, int len)
3420{
3421 struct ceph_mds_session *s = con->private;
3422 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003423 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003424
3425 return ac->ops->verify_authorizer_reply(ac, s->s_authorizer, len);
3426}
3427
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003428static int invalidate_authorizer(struct ceph_connection *con)
3429{
3430 struct ceph_mds_session *s = con->private;
3431 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003432 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003433
3434 if (ac->ops->invalidate_authorizer)
3435 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
3436
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003437 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003438}
3439
Tobias Klauser9e327892010-05-20 10:40:19 +02003440static const struct ceph_connection_operations mds_con_ops = {
Sage Weil2f2dc052009-10-06 11:31:09 -07003441 .get = con_get,
3442 .put = con_put,
3443 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003444 .get_authorizer = get_authorizer,
3445 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003446 .invalidate_authorizer = invalidate_authorizer,
Sage Weil2f2dc052009-10-06 11:31:09 -07003447 .peer_reset = peer_reset,
Sage Weil2f2dc052009-10-06 11:31:09 -07003448};
3449
Sage Weil2f2dc052009-10-06 11:31:09 -07003450/* eof */