blob: d2171f4a698072d34bad5f68be85c3ae4aa19914 [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>
Yan, Zheng54008392014-03-29 13:41:15 +08006#include <linux/gfp.h>
Sage Weil2f2dc052009-10-06 11:31:09 -07007#include <linux/sched.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07008#include <linux/debugfs.h>
9#include <linux/seq_file.h>
John Spraydbd0c8b2014-09-09 19:26:01 +010010#include <linux/utsname.h>
Sage Weil2f2dc052009-10-06 11:31:09 -070011
Sage Weil2f2dc052009-10-06 11:31:09 -070012#include "super.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070013#include "mds_client.h"
14
Sage Weil1fe60e52012-07-30 16:23:22 -070015#include <linux/ceph/ceph_features.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070016#include <linux/ceph/messenger.h>
17#include <linux/ceph/decode.h>
18#include <linux/ceph/pagelist.h>
19#include <linux/ceph/auth.h>
20#include <linux/ceph/debugfs.h>
Sage Weil2f2dc052009-10-06 11:31:09 -070021
22/*
23 * A cluster of MDS (metadata server) daemons is responsible for
24 * managing the file system namespace (the directory hierarchy and
25 * inodes) and for coordinating shared access to storage. Metadata is
26 * partitioning hierarchically across a number of servers, and that
27 * partition varies over time as the cluster adjusts the distribution
28 * in order to balance load.
29 *
30 * The MDS client is primarily responsible to managing synchronous
31 * metadata requests for operations like open, unlink, and so forth.
32 * If there is a MDS failure, we find out about it when we (possibly
33 * request and) receive a new MDS map, and can resubmit affected
34 * requests.
35 *
36 * For the most part, though, we take advantage of a lossless
37 * communications channel to the MDS, and do not need to worry about
38 * timing out or resubmitting requests.
39 *
40 * We maintain a stateful "session" with each MDS we interact with.
41 * Within each session, we sent periodic heartbeat messages to ensure
42 * any capabilities or leases we have been issues remain valid. If
43 * the session times out and goes stale, our leases and capabilities
44 * are no longer valid.
45 */
46
Sage Weil20cb34a2010-05-12 15:21:32 -070047struct ceph_reconnect_state {
Yan, Zheng44c99752013-09-22 10:28:10 +080048 int nr_caps;
Sage Weil20cb34a2010-05-12 15:21:32 -070049 struct ceph_pagelist *pagelist;
50 bool flock;
51};
52
Sage Weil2f2dc052009-10-06 11:31:09 -070053static void __wake_requests(struct ceph_mds_client *mdsc,
54 struct list_head *head);
55
Tobias Klauser9e327892010-05-20 10:40:19 +020056static const struct ceph_connection_operations mds_con_ops;
Sage Weil2f2dc052009-10-06 11:31:09 -070057
58
59/*
60 * mds reply parsing
61 */
62
63/*
64 * parse individual inode info
65 */
66static int parse_reply_info_in(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -080067 struct ceph_mds_reply_info_in *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +020068 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -070069{
70 int err = -EIO;
71
72 info->in = *p;
73 *p += sizeof(struct ceph_mds_reply_inode) +
74 sizeof(*info->in->fragtree.splits) *
75 le32_to_cpu(info->in->fragtree.nsplits);
76
77 ceph_decode_32_safe(p, end, info->symlink_len, bad);
78 ceph_decode_need(p, end, info->symlink_len, bad);
79 info->symlink = *p;
80 *p += info->symlink_len;
81
Sage Weil14303d22010-12-14 17:37:52 -080082 if (features & CEPH_FEATURE_DIRLAYOUTHASH)
83 ceph_decode_copy_safe(p, end, &info->dir_layout,
84 sizeof(info->dir_layout), bad);
85 else
86 memset(&info->dir_layout, 0, sizeof(info->dir_layout));
87
Sage Weil2f2dc052009-10-06 11:31:09 -070088 ceph_decode_32_safe(p, end, info->xattr_len, bad);
89 ceph_decode_need(p, end, info->xattr_len, bad);
90 info->xattr_data = *p;
91 *p += info->xattr_len;
Yan, Zhengfb01d1f2014-11-14 21:29:55 +080092
93 if (features & CEPH_FEATURE_MDS_INLINE_DATA) {
94 ceph_decode_64_safe(p, end, info->inline_version, bad);
95 ceph_decode_32_safe(p, end, info->inline_len, bad);
96 ceph_decode_need(p, end, info->inline_len, bad);
97 info->inline_data = *p;
98 *p += info->inline_len;
99 } else
100 info->inline_version = CEPH_INLINE_NONE;
101
Sage Weil2f2dc052009-10-06 11:31:09 -0700102 return 0;
103bad:
104 return err;
105}
106
107/*
108 * parse a normal reply, which may contain a (dir+)dentry and/or a
109 * target inode.
110 */
111static int parse_reply_info_trace(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800112 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200113 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700114{
115 int err;
116
117 if (info->head->is_dentry) {
Sage Weil14303d22010-12-14 17:37:52 -0800118 err = parse_reply_info_in(p, end, &info->diri, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700119 if (err < 0)
120 goto out_bad;
121
122 if (unlikely(*p + sizeof(*info->dirfrag) > end))
123 goto bad;
124 info->dirfrag = *p;
125 *p += sizeof(*info->dirfrag) +
126 sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
127 if (unlikely(*p > end))
128 goto bad;
129
130 ceph_decode_32_safe(p, end, info->dname_len, bad);
131 ceph_decode_need(p, end, info->dname_len, bad);
132 info->dname = *p;
133 *p += info->dname_len;
134 info->dlease = *p;
135 *p += sizeof(*info->dlease);
136 }
137
138 if (info->head->is_target) {
Sage Weil14303d22010-12-14 17:37:52 -0800139 err = parse_reply_info_in(p, end, &info->targeti, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700140 if (err < 0)
141 goto out_bad;
142 }
143
144 if (unlikely(*p != end))
145 goto bad;
146 return 0;
147
148bad:
149 err = -EIO;
150out_bad:
151 pr_err("problem parsing mds trace %d\n", err);
152 return err;
153}
154
155/*
156 * parse readdir results
157 */
158static int parse_reply_info_dir(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800159 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200160 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700161{
162 u32 num, i = 0;
163 int err;
164
165 info->dir_dir = *p;
166 if (*p + sizeof(*info->dir_dir) > end)
167 goto bad;
168 *p += sizeof(*info->dir_dir) +
169 sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
170 if (*p > end)
171 goto bad;
172
173 ceph_decode_need(p, end, sizeof(num) + 2, bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700174 num = ceph_decode_32(p);
175 info->dir_end = ceph_decode_8(p);
176 info->dir_complete = ceph_decode_8(p);
Sage Weil2f2dc052009-10-06 11:31:09 -0700177 if (num == 0)
178 goto done;
179
Yan, Zheng54008392014-03-29 13:41:15 +0800180 BUG_ON(!info->dir_in);
Sage Weil2f2dc052009-10-06 11:31:09 -0700181 info->dir_dname = (void *)(info->dir_in + num);
182 info->dir_dname_len = (void *)(info->dir_dname + num);
183 info->dir_dlease = (void *)(info->dir_dname_len + num);
Yan, Zheng54008392014-03-29 13:41:15 +0800184 if ((unsigned long)(info->dir_dlease + num) >
185 (unsigned long)info->dir_in + info->dir_buf_size) {
186 pr_err("dir contents are larger than expected\n");
187 WARN_ON(1);
188 goto bad;
189 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700190
Yan, Zheng54008392014-03-29 13:41:15 +0800191 info->dir_nr = num;
Sage Weil2f2dc052009-10-06 11:31:09 -0700192 while (num) {
193 /* dentry */
194 ceph_decode_need(p, end, sizeof(u32)*2, bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700195 info->dir_dname_len[i] = ceph_decode_32(p);
Sage Weil2f2dc052009-10-06 11:31:09 -0700196 ceph_decode_need(p, end, info->dir_dname_len[i], bad);
197 info->dir_dname[i] = *p;
198 *p += info->dir_dname_len[i];
199 dout("parsed dir dname '%.*s'\n", info->dir_dname_len[i],
200 info->dir_dname[i]);
201 info->dir_dlease[i] = *p;
202 *p += sizeof(struct ceph_mds_reply_lease);
203
204 /* inode */
Sage Weil14303d22010-12-14 17:37:52 -0800205 err = parse_reply_info_in(p, end, &info->dir_in[i], features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700206 if (err < 0)
207 goto out_bad;
208 i++;
209 num--;
210 }
211
212done:
213 if (*p != end)
214 goto bad;
215 return 0;
216
217bad:
218 err = -EIO;
219out_bad:
220 pr_err("problem parsing dir contents %d\n", err);
221 return err;
222}
223
224/*
Herb Shiu25933ab2010-12-01 14:14:38 -0800225 * parse fcntl F_GETLK results
226 */
227static int parse_reply_info_filelock(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800228 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200229 u64 features)
Herb Shiu25933ab2010-12-01 14:14:38 -0800230{
231 if (*p + sizeof(*info->filelock_reply) > end)
232 goto bad;
233
234 info->filelock_reply = *p;
235 *p += sizeof(*info->filelock_reply);
236
237 if (unlikely(*p != end))
238 goto bad;
239 return 0;
240
241bad:
242 return -EIO;
243}
244
245/*
Sam Lang6e8575f2012-12-28 09:56:46 -0800246 * parse create results
247 */
248static int parse_reply_info_create(void **p, void *end,
249 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200250 u64 features)
Sam Lang6e8575f2012-12-28 09:56:46 -0800251{
252 if (features & CEPH_FEATURE_REPLY_CREATE_INODE) {
253 if (*p == end) {
254 info->has_create_ino = false;
255 } else {
256 info->has_create_ino = true;
257 info->ino = ceph_decode_64(p);
258 }
259 }
260
261 if (unlikely(*p != end))
262 goto bad;
263 return 0;
264
265bad:
266 return -EIO;
267}
268
269/*
Herb Shiu25933ab2010-12-01 14:14:38 -0800270 * parse extra results
271 */
272static int parse_reply_info_extra(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800273 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200274 u64 features)
Herb Shiu25933ab2010-12-01 14:14:38 -0800275{
276 if (info->head->op == CEPH_MDS_OP_GETFILELOCK)
Sage Weil14303d22010-12-14 17:37:52 -0800277 return parse_reply_info_filelock(p, end, info, features);
Yan, Zheng8a034492013-02-21 13:43:55 +0800278 else if (info->head->op == CEPH_MDS_OP_READDIR ||
279 info->head->op == CEPH_MDS_OP_LSSNAP)
Sage Weil14303d22010-12-14 17:37:52 -0800280 return parse_reply_info_dir(p, end, info, features);
Sam Lang6e8575f2012-12-28 09:56:46 -0800281 else if (info->head->op == CEPH_MDS_OP_CREATE)
282 return parse_reply_info_create(p, end, info, features);
283 else
284 return -EIO;
Herb Shiu25933ab2010-12-01 14:14:38 -0800285}
286
287/*
Sage Weil2f2dc052009-10-06 11:31:09 -0700288 * parse entire mds reply
289 */
290static int parse_reply_info(struct ceph_msg *msg,
Sage Weil14303d22010-12-14 17:37:52 -0800291 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200292 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700293{
294 void *p, *end;
295 u32 len;
296 int err;
297
298 info->head = msg->front.iov_base;
299 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
300 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
301
302 /* trace */
303 ceph_decode_32_safe(&p, end, len, bad);
304 if (len > 0) {
Xi Wang32852a82012-01-14 22:20:59 -0500305 ceph_decode_need(&p, end, len, bad);
Sage Weil14303d22010-12-14 17:37:52 -0800306 err = parse_reply_info_trace(&p, p+len, info, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700307 if (err < 0)
308 goto out_bad;
309 }
310
Herb Shiu25933ab2010-12-01 14:14:38 -0800311 /* extra */
Sage Weil2f2dc052009-10-06 11:31:09 -0700312 ceph_decode_32_safe(&p, end, len, bad);
313 if (len > 0) {
Xi Wang32852a82012-01-14 22:20:59 -0500314 ceph_decode_need(&p, end, len, bad);
Sage Weil14303d22010-12-14 17:37:52 -0800315 err = parse_reply_info_extra(&p, p+len, info, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700316 if (err < 0)
317 goto out_bad;
318 }
319
320 /* snap blob */
321 ceph_decode_32_safe(&p, end, len, bad);
322 info->snapblob_len = len;
323 info->snapblob = p;
324 p += len;
325
326 if (p != end)
327 goto bad;
328 return 0;
329
330bad:
331 err = -EIO;
332out_bad:
333 pr_err("mds parse_reply err %d\n", err);
334 return err;
335}
336
337static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
338{
Yan, Zheng54008392014-03-29 13:41:15 +0800339 if (!info->dir_in)
340 return;
341 free_pages((unsigned long)info->dir_in, get_order(info->dir_buf_size));
Sage Weil2f2dc052009-10-06 11:31:09 -0700342}
343
344
345/*
346 * sessions
347 */
John Spraya687eca2014-09-19 13:51:08 +0100348const char *ceph_session_state_name(int s)
Sage Weil2f2dc052009-10-06 11:31:09 -0700349{
350 switch (s) {
351 case CEPH_MDS_SESSION_NEW: return "new";
352 case CEPH_MDS_SESSION_OPENING: return "opening";
353 case CEPH_MDS_SESSION_OPEN: return "open";
354 case CEPH_MDS_SESSION_HUNG: return "hung";
355 case CEPH_MDS_SESSION_CLOSING: return "closing";
Sage Weil44ca18f2010-02-15 12:08:46 -0800356 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
Sage Weil2f2dc052009-10-06 11:31:09 -0700357 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
358 default: return "???";
359 }
360}
361
362static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
363{
364 if (atomic_inc_not_zero(&s->s_ref)) {
365 dout("mdsc get_session %p %d -> %d\n", s,
366 atomic_read(&s->s_ref)-1, atomic_read(&s->s_ref));
367 return s;
368 } else {
369 dout("mdsc get_session %p 0 -- FAIL", s);
370 return NULL;
371 }
372}
373
374void ceph_put_mds_session(struct ceph_mds_session *s)
375{
376 dout("mdsc put_session %p %d -> %d\n", s,
377 atomic_read(&s->s_ref), atomic_read(&s->s_ref)-1);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800378 if (atomic_dec_and_test(&s->s_ref)) {
Alex Elder6c4a1912012-05-16 15:16:38 -0500379 if (s->s_auth.authorizer)
Sage Weil27859f92013-03-25 10:26:14 -0700380 ceph_auth_destroy_authorizer(
381 s->s_mdsc->fsc->client->monc.auth,
382 s->s_auth.authorizer);
Sage Weil2f2dc052009-10-06 11:31:09 -0700383 kfree(s);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800384 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700385}
386
387/*
388 * called under mdsc->mutex
389 */
390struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
391 int mds)
392{
393 struct ceph_mds_session *session;
394
395 if (mds >= mdsc->max_sessions || mdsc->sessions[mds] == NULL)
396 return NULL;
397 session = mdsc->sessions[mds];
398 dout("lookup_mds_session %p %d\n", session,
399 atomic_read(&session->s_ref));
400 get_session(session);
401 return session;
402}
403
404static bool __have_session(struct ceph_mds_client *mdsc, int mds)
405{
406 if (mds >= mdsc->max_sessions)
407 return false;
408 return mdsc->sessions[mds];
409}
410
Sage Weil2600d2d2010-02-22 15:12:16 -0800411static int __verify_registered_session(struct ceph_mds_client *mdsc,
412 struct ceph_mds_session *s)
413{
414 if (s->s_mds >= mdsc->max_sessions ||
415 mdsc->sessions[s->s_mds] != s)
416 return -ENOENT;
417 return 0;
418}
419
Sage Weil2f2dc052009-10-06 11:31:09 -0700420/*
421 * create+register a new session for given mds.
422 * called under mdsc->mutex.
423 */
424static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
425 int mds)
426{
427 struct ceph_mds_session *s;
428
Nathaniel Yazdanic338c072013-08-04 21:04:30 -0700429 if (mds >= mdsc->mdsmap->m_max_mds)
430 return ERR_PTR(-EINVAL);
431
Sage Weil2f2dc052009-10-06 11:31:09 -0700432 s = kzalloc(sizeof(*s), GFP_NOFS);
Dan Carpenter4736b002010-03-20 15:30:16 +0300433 if (!s)
434 return ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -0700435 s->s_mdsc = mdsc;
436 s->s_mds = mds;
437 s->s_state = CEPH_MDS_SESSION_NEW;
438 s->s_ttl = 0;
439 s->s_seq = 0;
440 mutex_init(&s->s_mutex);
441
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700442 ceph_con_init(&s->s_con, s, &mds_con_ops, &mdsc->fsc->client->msgr);
Sage Weil2f2dc052009-10-06 11:31:09 -0700443
Alex Elderd8fb02a2012-01-12 17:48:10 -0800444 spin_lock_init(&s->s_gen_ttl_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700445 s->s_cap_gen = 0;
Alex Elder1ce208a2012-01-12 17:48:11 -0800446 s->s_cap_ttl = jiffies - 1;
Alex Elderd8fb02a2012-01-12 17:48:10 -0800447
448 spin_lock_init(&s->s_cap_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700449 s->s_renew_requested = 0;
450 s->s_renew_seq = 0;
451 INIT_LIST_HEAD(&s->s_caps);
452 s->s_nr_caps = 0;
Sage Weil5dacf092009-12-21 20:40:34 -0800453 s->s_trim_caps = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -0700454 atomic_set(&s->s_ref, 1);
455 INIT_LIST_HEAD(&s->s_waiting);
456 INIT_LIST_HEAD(&s->s_unsafe);
457 s->s_num_cap_releases = 0;
Yan, Zheng99a9c272013-09-22 11:08:14 +0800458 s->s_cap_reconnect = 0;
Sage Weil7c1332b2010-02-16 11:39:45 -0800459 s->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700460 INIT_LIST_HEAD(&s->s_cap_releases);
461 INIT_LIST_HEAD(&s->s_cap_releases_done);
462 INIT_LIST_HEAD(&s->s_cap_flushing);
463 INIT_LIST_HEAD(&s->s_cap_snaps_flushing);
464
465 dout("register_session mds%d\n", mds);
466 if (mds >= mdsc->max_sessions) {
467 int newmax = 1 << get_count_order(mds+1);
468 struct ceph_mds_session **sa;
469
470 dout("register_session realloc to %d\n", newmax);
471 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
472 if (sa == NULL)
Sage Weil42ce56e2009-11-18 11:22:36 -0800473 goto fail_realloc;
Sage Weil2f2dc052009-10-06 11:31:09 -0700474 if (mdsc->sessions) {
475 memcpy(sa, mdsc->sessions,
476 mdsc->max_sessions * sizeof(void *));
477 kfree(mdsc->sessions);
478 }
479 mdsc->sessions = sa;
480 mdsc->max_sessions = newmax;
481 }
482 mdsc->sessions[mds] = s;
483 atomic_inc(&s->s_ref); /* one ref to sessions[], one to caller */
Sage Weil42ce56e2009-11-18 11:22:36 -0800484
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700485 ceph_con_open(&s->s_con, CEPH_ENTITY_TYPE_MDS, mds,
486 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
Sage Weil42ce56e2009-11-18 11:22:36 -0800487
Sage Weil2f2dc052009-10-06 11:31:09 -0700488 return s;
Sage Weil42ce56e2009-11-18 11:22:36 -0800489
490fail_realloc:
491 kfree(s);
492 return ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -0700493}
494
495/*
496 * called under mdsc->mutex
497 */
Sage Weil2600d2d2010-02-22 15:12:16 -0800498static void __unregister_session(struct ceph_mds_client *mdsc,
Sage Weil42ce56e2009-11-18 11:22:36 -0800499 struct ceph_mds_session *s)
Sage Weil2f2dc052009-10-06 11:31:09 -0700500{
Sage Weil2600d2d2010-02-22 15:12:16 -0800501 dout("__unregister_session mds%d %p\n", s->s_mds, s);
502 BUG_ON(mdsc->sessions[s->s_mds] != s);
Sage Weil42ce56e2009-11-18 11:22:36 -0800503 mdsc->sessions[s->s_mds] = NULL;
504 ceph_con_close(&s->s_con);
505 ceph_put_mds_session(s);
Sage Weil2f2dc052009-10-06 11:31:09 -0700506}
507
508/*
509 * drop session refs in request.
510 *
511 * should be last request ref, or hold mdsc->mutex
512 */
513static void put_request_session(struct ceph_mds_request *req)
514{
515 if (req->r_session) {
516 ceph_put_mds_session(req->r_session);
517 req->r_session = NULL;
518 }
519}
520
Sage Weil153c8e62009-12-07 12:31:09 -0800521void ceph_mdsc_release_request(struct kref *kref)
Sage Weil2f2dc052009-10-06 11:31:09 -0700522{
Sage Weil153c8e62009-12-07 12:31:09 -0800523 struct ceph_mds_request *req = container_of(kref,
524 struct ceph_mds_request,
525 r_kref);
Yan, Zheng54008392014-03-29 13:41:15 +0800526 destroy_reply_info(&req->r_reply_info);
Sage Weil153c8e62009-12-07 12:31:09 -0800527 if (req->r_request)
528 ceph_msg_put(req->r_request);
Yan, Zheng54008392014-03-29 13:41:15 +0800529 if (req->r_reply)
Sage Weil153c8e62009-12-07 12:31:09 -0800530 ceph_msg_put(req->r_reply);
Sage Weil153c8e62009-12-07 12:31:09 -0800531 if (req->r_inode) {
Sage Weil41b02e12011-07-26 11:31:14 -0700532 ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
Sage Weil153c8e62009-12-07 12:31:09 -0800533 iput(req->r_inode);
534 }
535 if (req->r_locked_dir)
Sage Weil41b02e12011-07-26 11:31:14 -0700536 ceph_put_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
SF Markus Elfringe96a6502014-11-02 15:20:59 +0100537 iput(req->r_target_inode);
Sage Weil153c8e62009-12-07 12:31:09 -0800538 if (req->r_dentry)
539 dput(req->r_dentry);
Sage Weil844d87c2013-02-05 13:40:09 -0800540 if (req->r_old_dentry)
541 dput(req->r_old_dentry);
542 if (req->r_old_dentry_dir) {
Sage Weil41b02e12011-07-26 11:31:14 -0700543 /*
544 * track (and drop pins for) r_old_dentry_dir
545 * separately, since r_old_dentry's d_parent may have
546 * changed between the dir mutex being dropped and
547 * this request being freed.
548 */
549 ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir),
550 CEPH_CAP_PIN);
Sage Weil41b02e12011-07-26 11:31:14 -0700551 iput(req->r_old_dentry_dir);
Sage Weil153c8e62009-12-07 12:31:09 -0800552 }
553 kfree(req->r_path1);
554 kfree(req->r_path2);
Yan, Zheng25e6bae2014-09-16 19:15:28 +0800555 if (req->r_pagelist)
556 ceph_pagelist_release(req->r_pagelist);
Sage Weil153c8e62009-12-07 12:31:09 -0800557 put_request_session(req);
Yehuda Sadeh37151662010-06-17 16:16:12 -0700558 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
Sage Weil153c8e62009-12-07 12:31:09 -0800559 kfree(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700560}
561
562/*
563 * lookup session, bump ref if found.
564 *
565 * called under mdsc->mutex.
566 */
567static struct ceph_mds_request *__lookup_request(struct ceph_mds_client *mdsc,
568 u64 tid)
569{
570 struct ceph_mds_request *req;
Sage Weil44ca18f2010-02-15 12:08:46 -0800571 struct rb_node *n = mdsc->request_tree.rb_node;
572
573 while (n) {
574 req = rb_entry(n, struct ceph_mds_request, r_node);
575 if (tid < req->r_tid)
576 n = n->rb_left;
577 else if (tid > req->r_tid)
578 n = n->rb_right;
579 else {
580 ceph_mdsc_get_request(req);
581 return req;
582 }
583 }
584 return NULL;
585}
586
587static void __insert_request(struct ceph_mds_client *mdsc,
588 struct ceph_mds_request *new)
589{
590 struct rb_node **p = &mdsc->request_tree.rb_node;
591 struct rb_node *parent = NULL;
592 struct ceph_mds_request *req = NULL;
593
594 while (*p) {
595 parent = *p;
596 req = rb_entry(parent, struct ceph_mds_request, r_node);
597 if (new->r_tid < req->r_tid)
598 p = &(*p)->rb_left;
599 else if (new->r_tid > req->r_tid)
600 p = &(*p)->rb_right;
601 else
602 BUG();
603 }
604
605 rb_link_node(&new->r_node, parent, p);
606 rb_insert_color(&new->r_node, &mdsc->request_tree);
Sage Weil2f2dc052009-10-06 11:31:09 -0700607}
608
609/*
610 * Register an in-flight request, and assign a tid. Link to directory
611 * are modifying (if any).
612 *
613 * Called under mdsc->mutex.
614 */
615static void __register_request(struct ceph_mds_client *mdsc,
616 struct ceph_mds_request *req,
617 struct inode *dir)
618{
619 req->r_tid = ++mdsc->last_tid;
620 if (req->r_num_caps)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700621 ceph_reserve_caps(mdsc, &req->r_caps_reservation,
622 req->r_num_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -0700623 dout("__register_request %p tid %lld\n", req, req->r_tid);
624 ceph_mdsc_get_request(req);
Sage Weil44ca18f2010-02-15 12:08:46 -0800625 __insert_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700626
Sage Weilcb4276c2010-11-08 07:28:52 -0800627 req->r_uid = current_fsuid();
628 req->r_gid = current_fsgid();
629
Sage Weil2f2dc052009-10-06 11:31:09 -0700630 if (dir) {
631 struct ceph_inode_info *ci = ceph_inode(dir);
632
Sage Weil3b663782011-05-18 16:12:12 -0700633 ihold(dir);
Sage Weil2f2dc052009-10-06 11:31:09 -0700634 spin_lock(&ci->i_unsafe_lock);
635 req->r_unsafe_dir = dir;
636 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
637 spin_unlock(&ci->i_unsafe_lock);
638 }
639}
640
641static void __unregister_request(struct ceph_mds_client *mdsc,
642 struct ceph_mds_request *req)
643{
644 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
Sage Weil44ca18f2010-02-15 12:08:46 -0800645 rb_erase(&req->r_node, &mdsc->request_tree);
Sage Weil80fc7312010-03-16 15:28:54 -0700646 RB_CLEAR_NODE(&req->r_node);
Sage Weil2f2dc052009-10-06 11:31:09 -0700647
648 if (req->r_unsafe_dir) {
649 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
650
651 spin_lock(&ci->i_unsafe_lock);
652 list_del_init(&req->r_unsafe_dir_item);
653 spin_unlock(&ci->i_unsafe_lock);
Sage Weil3b663782011-05-18 16:12:12 -0700654
655 iput(req->r_unsafe_dir);
656 req->r_unsafe_dir = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700657 }
Sage Weil94aa8ae2010-03-28 21:22:50 -0700658
Yan, Zhengfc55d2c2013-10-31 09:10:47 +0800659 complete_all(&req->r_safe_completion);
660
Sage Weil94aa8ae2010-03-28 21:22:50 -0700661 ceph_mdsc_put_request(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700662}
663
664/*
665 * Choose mds to send request to next. If there is a hint set in the
666 * request (e.g., due to a prior forward hint from the mds), use that.
667 * Otherwise, consult frag tree and/or caps to identify the
668 * appropriate mds. If all else fails, choose randomly.
669 *
670 * Called under mdsc->mutex.
671 */
H Hartley Sweeten7fd7d102011-09-23 13:22:11 -0700672static struct dentry *get_nonsnap_parent(struct dentry *dentry)
Sage Weileb6bb1c2010-08-16 09:21:27 -0700673{
Sage Weild79698d2011-07-26 11:31:26 -0700674 /*
675 * we don't need to worry about protecting the d_parent access
676 * here because we never renaming inside the snapped namespace
677 * except to resplice to another snapdir, and either the old or new
678 * result is a valid result.
679 */
Sage Weileb6bb1c2010-08-16 09:21:27 -0700680 while (!IS_ROOT(dentry) && ceph_snap(dentry->d_inode) != CEPH_NOSNAP)
681 dentry = dentry->d_parent;
682 return dentry;
683}
684
Sage Weil2f2dc052009-10-06 11:31:09 -0700685static int __choose_mds(struct ceph_mds_client *mdsc,
686 struct ceph_mds_request *req)
687{
688 struct inode *inode;
689 struct ceph_inode_info *ci;
690 struct ceph_cap *cap;
691 int mode = req->r_direct_mode;
692 int mds = -1;
693 u32 hash = req->r_direct_hash;
694 bool is_hash = req->r_direct_is_hash;
695
696 /*
697 * is there a specific mds we should try? ignore hint if we have
698 * no session and the mds is not up (active or recovering).
699 */
700 if (req->r_resend_mds >= 0 &&
701 (__have_session(mdsc, req->r_resend_mds) ||
702 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
703 dout("choose_mds using resend_mds mds%d\n",
704 req->r_resend_mds);
705 return req->r_resend_mds;
706 }
707
708 if (mode == USE_RANDOM_MDS)
709 goto random;
710
711 inode = NULL;
712 if (req->r_inode) {
713 inode = req->r_inode;
714 } else if (req->r_dentry) {
Sage Weild79698d2011-07-26 11:31:26 -0700715 /* ignore race with rename; old or new d_parent is okay */
716 struct dentry *parent = req->r_dentry->d_parent;
717 struct inode *dir = parent->d_inode;
Sage Weileb6bb1c2010-08-16 09:21:27 -0700718
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700719 if (dir->i_sb != mdsc->fsc->sb) {
Sage Weileb6bb1c2010-08-16 09:21:27 -0700720 /* not this fs! */
721 inode = req->r_dentry->d_inode;
722 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
723 /* direct snapped/virtual snapdir requests
724 * based on parent dir inode */
Sage Weild79698d2011-07-26 11:31:26 -0700725 struct dentry *dn = get_nonsnap_parent(parent);
Sage Weileb6bb1c2010-08-16 09:21:27 -0700726 inode = dn->d_inode;
727 dout("__choose_mds using nonsnap parent %p\n", inode);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800728 } else {
Sage Weileb6bb1c2010-08-16 09:21:27 -0700729 /* dentry target */
Sage Weil2f2dc052009-10-06 11:31:09 -0700730 inode = req->r_dentry->d_inode;
Yan, Zhengca18bed2013-11-22 14:21:44 +0800731 if (!inode || mode == USE_AUTH_MDS) {
732 /* dir + name */
733 inode = dir;
734 hash = ceph_dentry_hash(dir, req->r_dentry);
735 is_hash = true;
736 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700737 }
738 }
Sage Weileb6bb1c2010-08-16 09:21:27 -0700739
Sage Weil2f2dc052009-10-06 11:31:09 -0700740 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
741 (int)hash, mode);
742 if (!inode)
743 goto random;
744 ci = ceph_inode(inode);
745
746 if (is_hash && S_ISDIR(inode->i_mode)) {
747 struct ceph_inode_frag frag;
748 int found;
749
750 ceph_choose_frag(ci, hash, &frag, &found);
751 if (found) {
752 if (mode == USE_ANY_MDS && frag.ndist > 0) {
753 u8 r;
754
755 /* choose a random replica */
756 get_random_bytes(&r, 1);
757 r %= frag.ndist;
758 mds = frag.dist[r];
759 dout("choose_mds %p %llx.%llx "
760 "frag %u mds%d (%d/%d)\n",
761 inode, ceph_vinop(inode),
Sage Weild66bbd42011-01-21 21:16:46 -0800762 frag.frag, mds,
Sage Weil2f2dc052009-10-06 11:31:09 -0700763 (int)r, frag.ndist);
Sage Weild66bbd42011-01-21 21:16:46 -0800764 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
765 CEPH_MDS_STATE_ACTIVE)
766 return mds;
Sage Weil2f2dc052009-10-06 11:31:09 -0700767 }
768
769 /* since this file/dir wasn't known to be
770 * replicated, then we want to look for the
771 * authoritative mds. */
772 mode = USE_AUTH_MDS;
773 if (frag.mds >= 0) {
774 /* choose auth mds */
775 mds = frag.mds;
776 dout("choose_mds %p %llx.%llx "
777 "frag %u mds%d (auth)\n",
778 inode, ceph_vinop(inode), frag.frag, mds);
Sage Weild66bbd42011-01-21 21:16:46 -0800779 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
780 CEPH_MDS_STATE_ACTIVE)
781 return mds;
Sage Weil2f2dc052009-10-06 11:31:09 -0700782 }
783 }
784 }
785
Sage Weilbe655592011-11-30 09:47:09 -0800786 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700787 cap = NULL;
788 if (mode == USE_AUTH_MDS)
789 cap = ci->i_auth_cap;
790 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
791 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
792 if (!cap) {
Sage Weilbe655592011-11-30 09:47:09 -0800793 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700794 goto random;
795 }
796 mds = cap->session->s_mds;
797 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
798 inode, ceph_vinop(inode), mds,
799 cap == ci->i_auth_cap ? "auth " : "", cap);
Sage Weilbe655592011-11-30 09:47:09 -0800800 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700801 return mds;
802
803random:
804 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
805 dout("choose_mds chose random mds%d\n", mds);
806 return mds;
807}
808
809
810/*
811 * session messages
812 */
813static struct ceph_msg *create_session_msg(u32 op, u64 seq)
814{
815 struct ceph_msg *msg;
816 struct ceph_mds_session_head *h;
817
Sage Weilb61c2762011-08-09 15:03:46 -0700818 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
819 false);
Sage Weila79832f2010-04-01 16:06:19 -0700820 if (!msg) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700821 pr_err("create_session_msg ENOMEM creating msg\n");
Sage Weila79832f2010-04-01 16:06:19 -0700822 return NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700823 }
824 h = msg->front.iov_base;
825 h->op = cpu_to_le32(op);
826 h->seq = cpu_to_le64(seq);
John Spraydbd0c8b2014-09-09 19:26:01 +0100827
828 return msg;
829}
830
831/*
832 * session message, specialization for CEPH_SESSION_REQUEST_OPEN
833 * to include additional client metadata fields.
834 */
835static struct ceph_msg *create_session_open_msg(struct ceph_mds_client *mdsc, u64 seq)
836{
837 struct ceph_msg *msg;
838 struct ceph_mds_session_head *h;
839 int i = -1;
840 int metadata_bytes = 0;
841 int metadata_key_count = 0;
842 struct ceph_options *opt = mdsc->fsc->client->options;
843 void *p;
844
845 const char* metadata[3][2] = {
846 {"hostname", utsname()->nodename},
847 {"entity_id", opt->name ? opt->name : ""},
848 {NULL, NULL}
849 };
850
851 /* Calculate serialized length of metadata */
852 metadata_bytes = 4; /* map length */
853 for (i = 0; metadata[i][0] != NULL; ++i) {
854 metadata_bytes += 8 + strlen(metadata[i][0]) +
855 strlen(metadata[i][1]);
856 metadata_key_count++;
857 }
858
859 /* Allocate the message */
860 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + metadata_bytes,
861 GFP_NOFS, false);
862 if (!msg) {
863 pr_err("create_session_msg ENOMEM creating msg\n");
864 return NULL;
865 }
866 h = msg->front.iov_base;
867 h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN);
868 h->seq = cpu_to_le64(seq);
869
870 /*
871 * Serialize client metadata into waiting buffer space, using
872 * the format that userspace expects for map<string, string>
John Spray7cfa0312014-10-30 17:15:26 +0000873 *
874 * ClientSession messages with metadata are v2
John Spraydbd0c8b2014-09-09 19:26:01 +0100875 */
John Spray7cfa0312014-10-30 17:15:26 +0000876 msg->hdr.version = cpu_to_le16(2);
877 msg->hdr.compat_version = cpu_to_le16(1);
John Spraydbd0c8b2014-09-09 19:26:01 +0100878
879 /* The write pointer, following the session_head structure */
880 p = msg->front.iov_base + sizeof(*h);
881
882 /* Number of entries in the map */
883 ceph_encode_32(&p, metadata_key_count);
884
885 /* Two length-prefixed strings for each entry in the map */
886 for (i = 0; metadata[i][0] != NULL; ++i) {
887 size_t const key_len = strlen(metadata[i][0]);
888 size_t const val_len = strlen(metadata[i][1]);
889
890 ceph_encode_32(&p, key_len);
891 memcpy(p, metadata[i][0], key_len);
892 p += key_len;
893 ceph_encode_32(&p, val_len);
894 memcpy(p, metadata[i][1], val_len);
895 p += val_len;
896 }
897
Sage Weil2f2dc052009-10-06 11:31:09 -0700898 return msg;
899}
900
901/*
902 * send session open request.
903 *
904 * called under mdsc->mutex
905 */
906static int __open_session(struct ceph_mds_client *mdsc,
907 struct ceph_mds_session *session)
908{
909 struct ceph_msg *msg;
910 int mstate;
911 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -0700912
913 /* wait for mds to go active? */
914 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
915 dout("open_session to mds%d (%s)\n", mds,
916 ceph_mds_state_name(mstate));
917 session->s_state = CEPH_MDS_SESSION_OPENING;
918 session->s_renew_requested = jiffies;
919
920 /* send connect message */
John Spraydbd0c8b2014-09-09 19:26:01 +0100921 msg = create_session_open_msg(mdsc, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -0700922 if (!msg)
923 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -0700924 ceph_con_send(&session->s_con, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -0700925 return 0;
926}
927
928/*
Sage Weiled0552a2010-06-21 13:38:25 -0700929 * open sessions for any export targets for the given mds
930 *
931 * called under mdsc->mutex
932 */
Yan, Zheng5d72d132013-11-24 14:33:01 +0800933static struct ceph_mds_session *
934__open_export_target_session(struct ceph_mds_client *mdsc, int target)
935{
936 struct ceph_mds_session *session;
937
938 session = __ceph_lookup_mds_session(mdsc, target);
939 if (!session) {
940 session = register_session(mdsc, target);
941 if (IS_ERR(session))
942 return session;
943 }
944 if (session->s_state == CEPH_MDS_SESSION_NEW ||
945 session->s_state == CEPH_MDS_SESSION_CLOSING)
946 __open_session(mdsc, session);
947
948 return session;
949}
950
951struct ceph_mds_session *
952ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target)
953{
954 struct ceph_mds_session *session;
955
956 dout("open_export_target_session to mds%d\n", target);
957
958 mutex_lock(&mdsc->mutex);
959 session = __open_export_target_session(mdsc, target);
960 mutex_unlock(&mdsc->mutex);
961
962 return session;
963}
964
Sage Weiled0552a2010-06-21 13:38:25 -0700965static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
966 struct ceph_mds_session *session)
967{
968 struct ceph_mds_info *mi;
969 struct ceph_mds_session *ts;
970 int i, mds = session->s_mds;
Sage Weiled0552a2010-06-21 13:38:25 -0700971
972 if (mds >= mdsc->mdsmap->m_max_mds)
973 return;
Yan, Zheng5d72d132013-11-24 14:33:01 +0800974
Sage Weiled0552a2010-06-21 13:38:25 -0700975 mi = &mdsc->mdsmap->m_info[mds];
976 dout("open_export_target_sessions for mds%d (%d targets)\n",
977 session->s_mds, mi->num_export_targets);
978
979 for (i = 0; i < mi->num_export_targets; i++) {
Yan, Zheng5d72d132013-11-24 14:33:01 +0800980 ts = __open_export_target_session(mdsc, mi->export_targets[i]);
981 if (!IS_ERR(ts))
982 ceph_put_mds_session(ts);
Sage Weiled0552a2010-06-21 13:38:25 -0700983 }
984}
985
Sage Weil154f42c2010-06-21 13:45:04 -0700986void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
987 struct ceph_mds_session *session)
988{
989 mutex_lock(&mdsc->mutex);
990 __open_export_target_sessions(mdsc, session);
991 mutex_unlock(&mdsc->mutex);
992}
993
Sage Weiled0552a2010-06-21 13:38:25 -0700994/*
Sage Weil2f2dc052009-10-06 11:31:09 -0700995 * session caps
996 */
997
998/*
999 * Free preallocated cap messages assigned to this session
1000 */
1001static void cleanup_cap_releases(struct ceph_mds_session *session)
1002{
1003 struct ceph_msg *msg;
1004
1005 spin_lock(&session->s_cap_lock);
1006 while (!list_empty(&session->s_cap_releases)) {
1007 msg = list_first_entry(&session->s_cap_releases,
1008 struct ceph_msg, list_head);
1009 list_del_init(&msg->list_head);
1010 ceph_msg_put(msg);
1011 }
1012 while (!list_empty(&session->s_cap_releases_done)) {
1013 msg = list_first_entry(&session->s_cap_releases_done,
1014 struct ceph_msg, list_head);
1015 list_del_init(&msg->list_head);
1016 ceph_msg_put(msg);
1017 }
1018 spin_unlock(&session->s_cap_lock);
1019}
1020
1021/*
Sage Weilf818a732010-05-11 20:56:31 -07001022 * Helper to safely iterate over all caps associated with a session, with
1023 * special care taken to handle a racing __ceph_remove_cap().
Sage Weil2f2dc052009-10-06 11:31:09 -07001024 *
Sage Weilf818a732010-05-11 20:56:31 -07001025 * Caller must hold session s_mutex.
Sage Weil2f2dc052009-10-06 11:31:09 -07001026 */
1027static int iterate_session_caps(struct ceph_mds_session *session,
1028 int (*cb)(struct inode *, struct ceph_cap *,
1029 void *), void *arg)
1030{
Sage Weil7c1332b2010-02-16 11:39:45 -08001031 struct list_head *p;
1032 struct ceph_cap *cap;
1033 struct inode *inode, *last_inode = NULL;
1034 struct ceph_cap *old_cap = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001035 int ret;
1036
1037 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
1038 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001039 p = session->s_caps.next;
1040 while (p != &session->s_caps) {
1041 cap = list_entry(p, struct ceph_cap, session_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -07001042 inode = igrab(&cap->ci->vfs_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001043 if (!inode) {
1044 p = p->next;
Sage Weil2f2dc052009-10-06 11:31:09 -07001045 continue;
Sage Weil7c1332b2010-02-16 11:39:45 -08001046 }
1047 session->s_cap_iterator = cap;
Sage Weil2f2dc052009-10-06 11:31:09 -07001048 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001049
1050 if (last_inode) {
1051 iput(last_inode);
1052 last_inode = NULL;
1053 }
1054 if (old_cap) {
Yehuda Sadeh37151662010-06-17 16:16:12 -07001055 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -08001056 old_cap = NULL;
1057 }
1058
Sage Weil2f2dc052009-10-06 11:31:09 -07001059 ret = cb(inode, cap, arg);
Sage Weil7c1332b2010-02-16 11:39:45 -08001060 last_inode = inode;
1061
Sage Weil2f2dc052009-10-06 11:31:09 -07001062 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001063 p = p->next;
1064 if (cap->ci == NULL) {
1065 dout("iterate_session_caps finishing cap %p removal\n",
1066 cap);
1067 BUG_ON(cap->session != session);
1068 list_del_init(&cap->session_caps);
1069 session->s_nr_caps--;
1070 cap->session = NULL;
1071 old_cap = cap; /* put_cap it w/o locks held */
1072 }
Sage Weil5dacf092009-12-21 20:40:34 -08001073 if (ret < 0)
1074 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07001075 }
Sage Weil5dacf092009-12-21 20:40:34 -08001076 ret = 0;
1077out:
Sage Weil7c1332b2010-02-16 11:39:45 -08001078 session->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001079 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001080
SF Markus Elfringe96a6502014-11-02 15:20:59 +01001081 iput(last_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001082 if (old_cap)
Yehuda Sadeh37151662010-06-17 16:16:12 -07001083 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -08001084
Sage Weil5dacf092009-12-21 20:40:34 -08001085 return ret;
Sage Weil2f2dc052009-10-06 11:31:09 -07001086}
1087
1088static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
Sage Weil6c99f252010-05-10 16:12:25 -07001089 void *arg)
Sage Weil2f2dc052009-10-06 11:31:09 -07001090{
1091 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil6c99f252010-05-10 16:12:25 -07001092 int drop = 0;
1093
Sage Weil2f2dc052009-10-06 11:31:09 -07001094 dout("removing cap %p, ci is %p, inode is %p\n",
1095 cap, ci, &ci->vfs_inode);
Sage Weilbe655592011-11-30 09:47:09 -08001096 spin_lock(&ci->i_ceph_lock);
Yan, Zhenga096b092013-09-22 10:15:58 +08001097 __ceph_remove_cap(cap, false);
Sage Weil6c99f252010-05-10 16:12:25 -07001098 if (!__ceph_is_any_real_caps(ci)) {
1099 struct ceph_mds_client *mdsc =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001100 ceph_sb_to_client(inode->i_sb)->mdsc;
Sage Weil6c99f252010-05-10 16:12:25 -07001101
1102 spin_lock(&mdsc->cap_dirty_lock);
1103 if (!list_empty(&ci->i_dirty_item)) {
1104 pr_info(" dropping dirty %s state for %p %lld\n",
1105 ceph_cap_string(ci->i_dirty_caps),
1106 inode, ceph_ino(inode));
1107 ci->i_dirty_caps = 0;
1108 list_del_init(&ci->i_dirty_item);
1109 drop = 1;
1110 }
1111 if (!list_empty(&ci->i_flushing_item)) {
1112 pr_info(" dropping dirty+flushing %s state for %p %lld\n",
1113 ceph_cap_string(ci->i_flushing_caps),
1114 inode, ceph_ino(inode));
1115 ci->i_flushing_caps = 0;
1116 list_del_init(&ci->i_flushing_item);
1117 mdsc->num_cap_flushing--;
1118 drop = 1;
1119 }
1120 if (drop && ci->i_wrbuffer_ref) {
1121 pr_info(" dropping dirty data for %p %lld\n",
1122 inode, ceph_ino(inode));
1123 ci->i_wrbuffer_ref = 0;
1124 ci->i_wrbuffer_ref_head = 0;
1125 drop++;
1126 }
1127 spin_unlock(&mdsc->cap_dirty_lock);
1128 }
Sage Weilbe655592011-11-30 09:47:09 -08001129 spin_unlock(&ci->i_ceph_lock);
Sage Weil6c99f252010-05-10 16:12:25 -07001130 while (drop--)
1131 iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07001132 return 0;
1133}
1134
1135/*
1136 * caller must hold session s_mutex
1137 */
1138static void remove_session_caps(struct ceph_mds_session *session)
1139{
1140 dout("remove_session_caps on %p\n", session);
1141 iterate_session_caps(session, remove_session_caps_cb, NULL);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001142
1143 spin_lock(&session->s_cap_lock);
1144 if (session->s_nr_caps > 0) {
1145 struct super_block *sb = session->s_mdsc->fsc->sb;
1146 struct inode *inode;
1147 struct ceph_cap *cap, *prev = NULL;
1148 struct ceph_vino vino;
1149 /*
1150 * iterate_session_caps() skips inodes that are being
1151 * deleted, we need to wait until deletions are complete.
1152 * __wait_on_freeing_inode() is designed for the job,
1153 * but it is not exported, so use lookup inode function
1154 * to access it.
1155 */
1156 while (!list_empty(&session->s_caps)) {
1157 cap = list_entry(session->s_caps.next,
1158 struct ceph_cap, session_caps);
1159 if (cap == prev)
1160 break;
1161 prev = cap;
1162 vino = cap->ci->i_vino;
1163 spin_unlock(&session->s_cap_lock);
1164
Yan, Zhenged284c42013-09-02 15:19:53 +08001165 inode = ceph_find_inode(sb, vino);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001166 iput(inode);
1167
1168 spin_lock(&session->s_cap_lock);
1169 }
1170 }
1171 spin_unlock(&session->s_cap_lock);
1172
Sage Weil2f2dc052009-10-06 11:31:09 -07001173 BUG_ON(session->s_nr_caps > 0);
Sage Weil6c99f252010-05-10 16:12:25 -07001174 BUG_ON(!list_empty(&session->s_cap_flushing));
Sage Weil2f2dc052009-10-06 11:31:09 -07001175 cleanup_cap_releases(session);
1176}
1177
1178/*
1179 * wake up any threads waiting on this session's caps. if the cap is
1180 * old (didn't get renewed on the client reconnect), remove it now.
1181 *
1182 * caller must hold s_mutex.
1183 */
1184static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1185 void *arg)
1186{
Sage Weil0dc25702009-11-20 13:43:45 -08001187 struct ceph_inode_info *ci = ceph_inode(inode);
1188
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001189 wake_up_all(&ci->i_cap_wq);
Sage Weil0dc25702009-11-20 13:43:45 -08001190 if (arg) {
Sage Weilbe655592011-11-30 09:47:09 -08001191 spin_lock(&ci->i_ceph_lock);
Sage Weil0dc25702009-11-20 13:43:45 -08001192 ci->i_wanted_max_size = 0;
1193 ci->i_requested_max_size = 0;
Sage Weilbe655592011-11-30 09:47:09 -08001194 spin_unlock(&ci->i_ceph_lock);
Sage Weil0dc25702009-11-20 13:43:45 -08001195 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001196 return 0;
1197}
1198
Sage Weil0dc25702009-11-20 13:43:45 -08001199static void wake_up_session_caps(struct ceph_mds_session *session,
1200 int reconnect)
Sage Weil2f2dc052009-10-06 11:31:09 -07001201{
1202 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
Sage Weil0dc25702009-11-20 13:43:45 -08001203 iterate_session_caps(session, wake_up_session_cb,
1204 (void *)(unsigned long)reconnect);
Sage Weil2f2dc052009-10-06 11:31:09 -07001205}
1206
1207/*
1208 * Send periodic message to MDS renewing all currently held caps. The
1209 * ack will reset the expiration for all caps from this session.
1210 *
1211 * caller holds s_mutex
1212 */
1213static int send_renew_caps(struct ceph_mds_client *mdsc,
1214 struct ceph_mds_session *session)
1215{
1216 struct ceph_msg *msg;
1217 int state;
1218
1219 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1220 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1221 pr_info("mds%d caps stale\n", session->s_mds);
Sage Weile4cb4cb2010-03-18 13:43:09 -07001222 session->s_renew_requested = jiffies;
Sage Weil2f2dc052009-10-06 11:31:09 -07001223
1224 /* do not try to renew caps until a recovering mds has reconnected
1225 * with its clients. */
1226 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1227 if (state < CEPH_MDS_STATE_RECONNECT) {
1228 dout("send_renew_caps ignoring mds%d (%s)\n",
1229 session->s_mds, ceph_mds_state_name(state));
1230 return 0;
1231 }
1232
1233 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1234 ceph_mds_state_name(state));
Sage Weil2f2dc052009-10-06 11:31:09 -07001235 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1236 ++session->s_renew_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001237 if (!msg)
1238 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -07001239 ceph_con_send(&session->s_con, msg);
1240 return 0;
1241}
1242
Yan, Zheng186e4f72013-11-22 14:48:37 +08001243static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
1244 struct ceph_mds_session *session, u64 seq)
1245{
1246 struct ceph_msg *msg;
1247
1248 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
John Spraya687eca2014-09-19 13:51:08 +01001249 session->s_mds, ceph_session_state_name(session->s_state), seq);
Yan, Zheng186e4f72013-11-22 14:48:37 +08001250 msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
1251 if (!msg)
1252 return -ENOMEM;
1253 ceph_con_send(&session->s_con, msg);
1254 return 0;
1255}
1256
1257
Sage Weil2f2dc052009-10-06 11:31:09 -07001258/*
1259 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
Sage Weil0dc25702009-11-20 13:43:45 -08001260 *
1261 * Called under session->s_mutex
Sage Weil2f2dc052009-10-06 11:31:09 -07001262 */
1263static void renewed_caps(struct ceph_mds_client *mdsc,
1264 struct ceph_mds_session *session, int is_renew)
1265{
1266 int was_stale;
1267 int wake = 0;
1268
1269 spin_lock(&session->s_cap_lock);
Alex Elder1ce208a2012-01-12 17:48:11 -08001270 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
Sage Weil2f2dc052009-10-06 11:31:09 -07001271
1272 session->s_cap_ttl = session->s_renew_requested +
1273 mdsc->mdsmap->m_session_timeout*HZ;
1274
1275 if (was_stale) {
1276 if (time_before(jiffies, session->s_cap_ttl)) {
1277 pr_info("mds%d caps renewed\n", session->s_mds);
1278 wake = 1;
1279 } else {
1280 pr_info("mds%d caps still stale\n", session->s_mds);
1281 }
1282 }
1283 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1284 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1285 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1286 spin_unlock(&session->s_cap_lock);
1287
1288 if (wake)
Sage Weil0dc25702009-11-20 13:43:45 -08001289 wake_up_session_caps(session, 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07001290}
1291
1292/*
1293 * send a session close request
1294 */
1295static int request_close_session(struct ceph_mds_client *mdsc,
1296 struct ceph_mds_session *session)
1297{
1298 struct ceph_msg *msg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001299
1300 dout("request_close_session mds%d state %s seq %lld\n",
John Spraya687eca2014-09-19 13:51:08 +01001301 session->s_mds, ceph_session_state_name(session->s_state),
Sage Weil2f2dc052009-10-06 11:31:09 -07001302 session->s_seq);
1303 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001304 if (!msg)
1305 return -ENOMEM;
1306 ceph_con_send(&session->s_con, msg);
1307 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001308}
1309
1310/*
1311 * Called with s_mutex held.
1312 */
1313static int __close_session(struct ceph_mds_client *mdsc,
1314 struct ceph_mds_session *session)
1315{
1316 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1317 return 0;
1318 session->s_state = CEPH_MDS_SESSION_CLOSING;
1319 return request_close_session(mdsc, session);
1320}
1321
1322/*
1323 * Trim old(er) caps.
1324 *
1325 * Because we can't cache an inode without one or more caps, we do
1326 * this indirectly: if a cap is unused, we prune its aliases, at which
1327 * point the inode will hopefully get dropped to.
1328 *
1329 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1330 * memory pressure from the MDS, though, so it needn't be perfect.
1331 */
1332static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1333{
1334 struct ceph_mds_session *session = arg;
1335 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng979abfd2013-11-22 13:56:24 +08001336 int used, wanted, oissued, mine;
Sage Weil2f2dc052009-10-06 11:31:09 -07001337
1338 if (session->s_trim_caps <= 0)
1339 return -1;
1340
Sage Weilbe655592011-11-30 09:47:09 -08001341 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001342 mine = cap->issued | cap->implemented;
1343 used = __ceph_caps_used(ci);
Yan, Zheng979abfd2013-11-22 13:56:24 +08001344 wanted = __ceph_caps_file_wanted(ci);
Sage Weil2f2dc052009-10-06 11:31:09 -07001345 oissued = __ceph_caps_issued_other(ci, cap);
1346
Yan, Zheng979abfd2013-11-22 13:56:24 +08001347 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001348 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
Yan, Zheng979abfd2013-11-22 13:56:24 +08001349 ceph_cap_string(used), ceph_cap_string(wanted));
1350 if (cap == ci->i_auth_cap) {
1351 if (ci->i_dirty_caps | ci->i_flushing_caps)
1352 goto out;
1353 if ((used | wanted) & CEPH_CAP_ANY_WR)
1354 goto out;
1355 }
1356 if ((used | wanted) & ~oissued & mine)
Sage Weil2f2dc052009-10-06 11:31:09 -07001357 goto out; /* we need these caps */
1358
1359 session->s_trim_caps--;
1360 if (oissued) {
1361 /* we aren't the only cap.. just remove us */
Yan, Zhenga096b092013-09-22 10:15:58 +08001362 __ceph_remove_cap(cap, true);
Sage Weil2f2dc052009-10-06 11:31:09 -07001363 } else {
1364 /* try to drop referring dentries */
Sage Weilbe655592011-11-30 09:47:09 -08001365 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001366 d_prune_aliases(inode);
1367 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1368 inode, cap, atomic_read(&inode->i_count));
1369 return 0;
1370 }
1371
1372out:
Sage Weilbe655592011-11-30 09:47:09 -08001373 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001374 return 0;
1375}
1376
1377/*
1378 * Trim session cap count down to some max number.
1379 */
1380static int trim_caps(struct ceph_mds_client *mdsc,
1381 struct ceph_mds_session *session,
1382 int max_caps)
1383{
1384 int trim_caps = session->s_nr_caps - max_caps;
1385
1386 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1387 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1388 if (trim_caps > 0) {
1389 session->s_trim_caps = trim_caps;
1390 iterate_session_caps(session, trim_caps_cb, session);
1391 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1392 session->s_mds, session->s_nr_caps, max_caps,
1393 trim_caps - session->s_trim_caps);
Sage Weil5dacf092009-12-21 20:40:34 -08001394 session->s_trim_caps = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001395 }
Yan, Zhenga56371d2014-04-01 20:34:56 +08001396
1397 ceph_add_cap_releases(mdsc, session);
1398 ceph_send_cap_releases(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07001399 return 0;
1400}
1401
1402/*
1403 * Allocate cap_release messages. If there is a partially full message
1404 * in the queue, try to allocate enough to cover it's remainder, so that
1405 * we can send it immediately.
1406 *
1407 * Called under s_mutex.
1408 */
Sage Weil2b2300d2010-06-09 16:52:04 -07001409int ceph_add_cap_releases(struct ceph_mds_client *mdsc,
Sage Weilee6b2722010-06-10 12:55:52 -07001410 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001411{
Sage Weil38e8883e2010-06-10 12:57:11 -07001412 struct ceph_msg *msg, *partial = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001413 struct ceph_mds_cap_release *head;
1414 int err = -ENOMEM;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001415 int extra = mdsc->fsc->mount_options->cap_release_safety;
Sage Weil38e8883e2010-06-10 12:57:11 -07001416 int num;
Sage Weil2f2dc052009-10-06 11:31:09 -07001417
Sage Weil38e8883e2010-06-10 12:57:11 -07001418 dout("add_cap_releases %p mds%d extra %d\n", session, session->s_mds,
1419 extra);
Sage Weil2f2dc052009-10-06 11:31:09 -07001420
1421 spin_lock(&session->s_cap_lock);
1422
1423 if (!list_empty(&session->s_cap_releases)) {
1424 msg = list_first_entry(&session->s_cap_releases,
1425 struct ceph_msg,
1426 list_head);
1427 head = msg->front.iov_base;
Sage Weil38e8883e2010-06-10 12:57:11 -07001428 num = le32_to_cpu(head->num);
1429 if (num) {
1430 dout(" partial %p with (%d/%d)\n", msg, num,
1431 (int)CEPH_CAPS_PER_RELEASE);
1432 extra += CEPH_CAPS_PER_RELEASE - num;
1433 partial = msg;
1434 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001435 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001436 while (session->s_num_cap_releases < session->s_nr_caps + extra) {
1437 spin_unlock(&session->s_cap_lock);
Yehuda Sadeh34d23762010-04-06 14:33:58 -07001438 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE,
Sage Weilb61c2762011-08-09 15:03:46 -07001439 GFP_NOFS, false);
Sage Weil2f2dc052009-10-06 11:31:09 -07001440 if (!msg)
1441 goto out_unlocked;
1442 dout("add_cap_releases %p msg %p now %d\n", session, msg,
1443 (int)msg->front.iov_len);
1444 head = msg->front.iov_base;
1445 head->num = cpu_to_le32(0);
1446 msg->front.iov_len = sizeof(*head);
1447 spin_lock(&session->s_cap_lock);
1448 list_add(&msg->list_head, &session->s_cap_releases);
1449 session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
1450 }
1451
Sage Weil38e8883e2010-06-10 12:57:11 -07001452 if (partial) {
1453 head = partial->front.iov_base;
1454 num = le32_to_cpu(head->num);
1455 dout(" queueing partial %p with %d/%d\n", partial, num,
1456 (int)CEPH_CAPS_PER_RELEASE);
1457 list_move_tail(&partial->list_head,
1458 &session->s_cap_releases_done);
1459 session->s_num_cap_releases -= CEPH_CAPS_PER_RELEASE - num;
Sage Weil2f2dc052009-10-06 11:31:09 -07001460 }
1461 err = 0;
1462 spin_unlock(&session->s_cap_lock);
1463out_unlocked:
1464 return err;
1465}
1466
1467/*
1468 * flush all dirty inode data to disk.
1469 *
1470 * returns true if we've flushed through want_flush_seq
1471 */
1472static int check_cap_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
1473{
1474 int mds, ret = 1;
1475
1476 dout("check_cap_flush want %lld\n", want_flush_seq);
1477 mutex_lock(&mdsc->mutex);
1478 for (mds = 0; ret && mds < mdsc->max_sessions; mds++) {
1479 struct ceph_mds_session *session = mdsc->sessions[mds];
1480
1481 if (!session)
1482 continue;
1483 get_session(session);
1484 mutex_unlock(&mdsc->mutex);
1485
1486 mutex_lock(&session->s_mutex);
1487 if (!list_empty(&session->s_cap_flushing)) {
1488 struct ceph_inode_info *ci =
1489 list_entry(session->s_cap_flushing.next,
1490 struct ceph_inode_info,
1491 i_flushing_item);
1492 struct inode *inode = &ci->vfs_inode;
1493
Sage Weilbe655592011-11-30 09:47:09 -08001494 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001495 if (ci->i_cap_flush_seq <= want_flush_seq) {
1496 dout("check_cap_flush still flushing %p "
1497 "seq %lld <= %lld to mds%d\n", inode,
1498 ci->i_cap_flush_seq, want_flush_seq,
1499 session->s_mds);
1500 ret = 0;
1501 }
Sage Weilbe655592011-11-30 09:47:09 -08001502 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001503 }
1504 mutex_unlock(&session->s_mutex);
1505 ceph_put_mds_session(session);
1506
1507 if (!ret)
1508 return ret;
1509 mutex_lock(&mdsc->mutex);
1510 }
1511
1512 mutex_unlock(&mdsc->mutex);
1513 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
1514 return ret;
1515}
1516
1517/*
1518 * called under s_mutex
1519 */
Sage Weil3d7ded42010-06-09 16:47:10 -07001520void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1521 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001522{
1523 struct ceph_msg *msg;
1524
1525 dout("send_cap_releases mds%d\n", session->s_mds);
Sage Weil0f8605f2010-05-05 15:51:35 -07001526 spin_lock(&session->s_cap_lock);
1527 while (!list_empty(&session->s_cap_releases_done)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001528 msg = list_first_entry(&session->s_cap_releases_done,
1529 struct ceph_msg, list_head);
1530 list_del_init(&msg->list_head);
1531 spin_unlock(&session->s_cap_lock);
1532 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1533 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1534 ceph_con_send(&session->s_con, msg);
Sage Weil0f8605f2010-05-05 15:51:35 -07001535 spin_lock(&session->s_cap_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001536 }
1537 spin_unlock(&session->s_cap_lock);
1538}
1539
Sage Weile01a5942010-05-10 15:36:44 -07001540static void discard_cap_releases(struct ceph_mds_client *mdsc,
1541 struct ceph_mds_session *session)
1542{
1543 struct ceph_msg *msg;
1544 struct ceph_mds_cap_release *head;
1545 unsigned num;
1546
1547 dout("discard_cap_releases mds%d\n", session->s_mds);
Sage Weile01a5942010-05-10 15:36:44 -07001548
Yan, Zheng00bd8ed2014-03-24 09:56:43 +08001549 if (!list_empty(&session->s_cap_releases)) {
1550 /* zero out the in-progress message */
1551 msg = list_first_entry(&session->s_cap_releases,
1552 struct ceph_msg, list_head);
1553 head = msg->front.iov_base;
1554 num = le32_to_cpu(head->num);
1555 dout("discard_cap_releases mds%d %p %u\n",
1556 session->s_mds, msg, num);
1557 head->num = cpu_to_le32(0);
1558 msg->front.iov_len = sizeof(*head);
1559 session->s_num_cap_releases += num;
1560 }
Sage Weile01a5942010-05-10 15:36:44 -07001561
1562 /* requeue completed messages */
1563 while (!list_empty(&session->s_cap_releases_done)) {
1564 msg = list_first_entry(&session->s_cap_releases_done,
1565 struct ceph_msg, list_head);
1566 list_del_init(&msg->list_head);
1567
1568 head = msg->front.iov_base;
1569 num = le32_to_cpu(head->num);
1570 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg,
1571 num);
1572 session->s_num_cap_releases += num;
1573 head->num = cpu_to_le32(0);
1574 msg->front.iov_len = sizeof(*head);
1575 list_add(&msg->list_head, &session->s_cap_releases);
1576 }
Sage Weile01a5942010-05-10 15:36:44 -07001577}
1578
Sage Weil2f2dc052009-10-06 11:31:09 -07001579/*
1580 * requests
1581 */
1582
Yan, Zheng54008392014-03-29 13:41:15 +08001583int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
1584 struct inode *dir)
1585{
1586 struct ceph_inode_info *ci = ceph_inode(dir);
1587 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1588 struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options;
1589 size_t size = sizeof(*rinfo->dir_in) + sizeof(*rinfo->dir_dname_len) +
1590 sizeof(*rinfo->dir_dname) + sizeof(*rinfo->dir_dlease);
1591 int order, num_entries;
1592
1593 spin_lock(&ci->i_ceph_lock);
1594 num_entries = ci->i_files + ci->i_subdirs;
1595 spin_unlock(&ci->i_ceph_lock);
1596 num_entries = max(num_entries, 1);
1597 num_entries = min(num_entries, opt->max_readdir);
1598
1599 order = get_order(size * num_entries);
1600 while (order >= 0) {
1601 rinfo->dir_in = (void*)__get_free_pages(GFP_NOFS | __GFP_NOWARN,
1602 order);
1603 if (rinfo->dir_in)
1604 break;
1605 order--;
1606 }
1607 if (!rinfo->dir_in)
1608 return -ENOMEM;
1609
1610 num_entries = (PAGE_SIZE << order) / size;
1611 num_entries = min(num_entries, opt->max_readdir);
1612
1613 rinfo->dir_buf_size = PAGE_SIZE << order;
1614 req->r_num_caps = num_entries + 1;
1615 req->r_args.readdir.max_entries = cpu_to_le32(num_entries);
1616 req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes);
1617 return 0;
1618}
1619
Sage Weil2f2dc052009-10-06 11:31:09 -07001620/*
1621 * Create an mds request.
1622 */
1623struct ceph_mds_request *
1624ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1625{
1626 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1627
1628 if (!req)
1629 return ERR_PTR(-ENOMEM);
1630
Sage Weilb4556392010-05-13 12:01:13 -07001631 mutex_init(&req->r_fill_mutex);
Yehuda Sadeh37151662010-06-17 16:16:12 -07001632 req->r_mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07001633 req->r_started = jiffies;
1634 req->r_resend_mds = -1;
1635 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1636 req->r_fmode = -1;
Sage Weil153c8e62009-12-07 12:31:09 -08001637 kref_init(&req->r_kref);
Sage Weil2f2dc052009-10-06 11:31:09 -07001638 INIT_LIST_HEAD(&req->r_wait);
1639 init_completion(&req->r_completion);
1640 init_completion(&req->r_safe_completion);
1641 INIT_LIST_HEAD(&req->r_unsafe_item);
1642
Sage Weilb8e69062014-05-21 17:41:08 -07001643 req->r_stamp = CURRENT_TIME;
1644
Sage Weil2f2dc052009-10-06 11:31:09 -07001645 req->r_op = op;
1646 req->r_direct_mode = mode;
1647 return req;
1648}
1649
1650/*
Sage Weil44ca18f2010-02-15 12:08:46 -08001651 * return oldest (lowest) request, tid in request tree, 0 if none.
Sage Weil2f2dc052009-10-06 11:31:09 -07001652 *
1653 * called under mdsc->mutex.
1654 */
Sage Weil44ca18f2010-02-15 12:08:46 -08001655static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1656{
1657 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1658 return NULL;
1659 return rb_entry(rb_first(&mdsc->request_tree),
1660 struct ceph_mds_request, r_node);
1661}
1662
Sage Weil2f2dc052009-10-06 11:31:09 -07001663static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1664{
Sage Weil44ca18f2010-02-15 12:08:46 -08001665 struct ceph_mds_request *req = __get_oldest_req(mdsc);
1666
1667 if (req)
1668 return req->r_tid;
1669 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001670}
1671
1672/*
1673 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1674 * on build_path_from_dentry in fs/cifs/dir.c.
1675 *
1676 * If @stop_on_nosnap, generate path relative to the first non-snapped
1677 * inode.
1678 *
1679 * Encode hidden .snap dirs as a double /, i.e.
1680 * foo/.snap/bar -> foo//bar
1681 */
1682char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1683 int stop_on_nosnap)
1684{
1685 struct dentry *temp;
1686 char *path;
1687 int len, pos;
Al Viro1b71fe22011-07-16 23:43:58 -04001688 unsigned seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07001689
1690 if (dentry == NULL)
1691 return ERR_PTR(-EINVAL);
1692
1693retry:
1694 len = 0;
Al Viro1b71fe22011-07-16 23:43:58 -04001695 seq = read_seqbegin(&rename_lock);
1696 rcu_read_lock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001697 for (temp = dentry; !IS_ROOT(temp);) {
1698 struct inode *inode = temp->d_inode;
1699 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1700 len++; /* slash only */
1701 else if (stop_on_nosnap && inode &&
1702 ceph_snap(inode) == CEPH_NOSNAP)
1703 break;
1704 else
1705 len += 1 + temp->d_name.len;
1706 temp = temp->d_parent;
Sage Weil2f2dc052009-10-06 11:31:09 -07001707 }
Al Viro1b71fe22011-07-16 23:43:58 -04001708 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001709 if (len)
1710 len--; /* no leading '/' */
1711
1712 path = kmalloc(len+1, GFP_NOFS);
1713 if (path == NULL)
1714 return ERR_PTR(-ENOMEM);
1715 pos = len;
1716 path[pos] = 0; /* trailing null */
Al Viro1b71fe22011-07-16 23:43:58 -04001717 rcu_read_lock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001718 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
Al Viro1b71fe22011-07-16 23:43:58 -04001719 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07001720
Al Viro1b71fe22011-07-16 23:43:58 -04001721 spin_lock(&temp->d_lock);
1722 inode = temp->d_inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07001723 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
Sage Weil104648a2010-03-18 10:14:30 -07001724 dout("build_path path+%d: %p SNAPDIR\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001725 pos, temp);
1726 } else if (stop_on_nosnap && inode &&
1727 ceph_snap(inode) == CEPH_NOSNAP) {
Yehuda Sadeh9d5a09e2011-12-13 09:57:44 -08001728 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001729 break;
1730 } else {
1731 pos -= temp->d_name.len;
Al Viro1b71fe22011-07-16 23:43:58 -04001732 if (pos < 0) {
1733 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001734 break;
Al Viro1b71fe22011-07-16 23:43:58 -04001735 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001736 strncpy(path + pos, temp->d_name.name,
1737 temp->d_name.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07001738 }
Al Viro1b71fe22011-07-16 23:43:58 -04001739 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001740 if (pos)
1741 path[--pos] = '/';
1742 temp = temp->d_parent;
Sage Weil2f2dc052009-10-06 11:31:09 -07001743 }
Al Viro1b71fe22011-07-16 23:43:58 -04001744 rcu_read_unlock();
1745 if (pos != 0 || read_seqretry(&rename_lock, seq)) {
Sage Weil104648a2010-03-18 10:14:30 -07001746 pr_err("build_path did not end path lookup where "
Sage Weil2f2dc052009-10-06 11:31:09 -07001747 "expected, namelen is %d, pos is %d\n", len, pos);
1748 /* presumably this is only possible if racing with a
1749 rename of one of the parent directories (we can not
1750 lock the dentries above us to prevent this, but
1751 retrying should be harmless) */
1752 kfree(path);
1753 goto retry;
1754 }
1755
1756 *base = ceph_ino(temp->d_inode);
1757 *plen = len;
Sage Weil104648a2010-03-18 10:14:30 -07001758 dout("build_path on %p %d built %llx '%.*s'\n",
Al Viro84d08fa2013-07-05 18:59:33 +04001759 dentry, d_count(dentry), *base, len, path);
Sage Weil2f2dc052009-10-06 11:31:09 -07001760 return path;
1761}
1762
1763static int build_dentry_path(struct dentry *dentry,
1764 const char **ppath, int *ppathlen, u64 *pino,
1765 int *pfreepath)
1766{
1767 char *path;
1768
1769 if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) {
1770 *pino = ceph_ino(dentry->d_parent->d_inode);
1771 *ppath = dentry->d_name.name;
1772 *ppathlen = dentry->d_name.len;
1773 return 0;
1774 }
1775 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1776 if (IS_ERR(path))
1777 return PTR_ERR(path);
1778 *ppath = path;
1779 *pfreepath = 1;
1780 return 0;
1781}
1782
1783static int build_inode_path(struct inode *inode,
1784 const char **ppath, int *ppathlen, u64 *pino,
1785 int *pfreepath)
1786{
1787 struct dentry *dentry;
1788 char *path;
1789
1790 if (ceph_snap(inode) == CEPH_NOSNAP) {
1791 *pino = ceph_ino(inode);
1792 *ppathlen = 0;
1793 return 0;
1794 }
1795 dentry = d_find_alias(inode);
1796 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1797 dput(dentry);
1798 if (IS_ERR(path))
1799 return PTR_ERR(path);
1800 *ppath = path;
1801 *pfreepath = 1;
1802 return 0;
1803}
1804
1805/*
1806 * request arguments may be specified via an inode *, a dentry *, or
1807 * an explicit ino+path.
1808 */
1809static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1810 const char *rpath, u64 rino,
1811 const char **ppath, int *pathlen,
1812 u64 *ino, int *freepath)
1813{
1814 int r = 0;
1815
1816 if (rinode) {
1817 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1818 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1819 ceph_snap(rinode));
1820 } else if (rdentry) {
1821 r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
1822 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1823 *ppath);
Sage Weil795858d2011-08-15 13:02:37 -07001824 } else if (rpath || rino) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001825 *ino = rino;
1826 *ppath = rpath;
David Zafmanb0000562012-10-25 10:23:46 -07001827 *pathlen = rpath ? strlen(rpath) : 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001828 dout(" path %.*s\n", *pathlen, rpath);
1829 }
1830
1831 return r;
1832}
1833
1834/*
1835 * called under mdsc->mutex
1836 */
1837static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1838 struct ceph_mds_request *req,
1839 int mds)
1840{
1841 struct ceph_msg *msg;
1842 struct ceph_mds_request_head *head;
1843 const char *path1 = NULL;
1844 const char *path2 = NULL;
1845 u64 ino1 = 0, ino2 = 0;
1846 int pathlen1 = 0, pathlen2 = 0;
1847 int freepath1 = 0, freepath2 = 0;
1848 int len;
1849 u16 releases;
1850 void *p, *end;
1851 int ret;
1852
1853 ret = set_request_path_attr(req->r_inode, req->r_dentry,
1854 req->r_path1, req->r_ino1.ino,
1855 &path1, &pathlen1, &ino1, &freepath1);
1856 if (ret < 0) {
1857 msg = ERR_PTR(ret);
1858 goto out;
1859 }
1860
1861 ret = set_request_path_attr(NULL, req->r_old_dentry,
1862 req->r_path2, req->r_ino2.ino,
1863 &path2, &pathlen2, &ino2, &freepath2);
1864 if (ret < 0) {
1865 msg = ERR_PTR(ret);
1866 goto out_free1;
1867 }
1868
1869 len = sizeof(*head) +
Sage Weilb8e69062014-05-21 17:41:08 -07001870 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) +
1871 sizeof(struct timespec);
Sage Weil2f2dc052009-10-06 11:31:09 -07001872
1873 /* calculate (max) length for cap releases */
1874 len += sizeof(struct ceph_mds_request_release) *
1875 (!!req->r_inode_drop + !!req->r_dentry_drop +
1876 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1877 if (req->r_dentry_drop)
1878 len += req->r_dentry->d_name.len;
1879 if (req->r_old_dentry_drop)
1880 len += req->r_old_dentry->d_name.len;
1881
Sage Weilb61c2762011-08-09 15:03:46 -07001882 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07001883 if (!msg) {
1884 msg = ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -07001885 goto out_free2;
Sage Weila79832f2010-04-01 16:06:19 -07001886 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001887
John Spray7cfa0312014-10-30 17:15:26 +00001888 msg->hdr.version = cpu_to_le16(2);
Sage Weil6df058c2009-12-22 11:24:33 -08001889 msg->hdr.tid = cpu_to_le64(req->r_tid);
1890
Sage Weil2f2dc052009-10-06 11:31:09 -07001891 head = msg->front.iov_base;
1892 p = msg->front.iov_base + sizeof(*head);
1893 end = msg->front.iov_base + msg->front.iov_len;
1894
1895 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1896 head->op = cpu_to_le32(req->r_op);
Eric W. Biedermanff3d0042013-01-31 04:01:53 -08001897 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid));
1898 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid));
Sage Weil2f2dc052009-10-06 11:31:09 -07001899 head->args = req->r_args;
1900
1901 ceph_encode_filepath(&p, end, ino1, path1);
1902 ceph_encode_filepath(&p, end, ino2, path2);
1903
Sage Weile979cf52010-07-15 14:58:39 -07001904 /* make note of release offset, in case we need to replay */
1905 req->r_request_release_offset = p - msg->front.iov_base;
1906
Sage Weil2f2dc052009-10-06 11:31:09 -07001907 /* cap releases */
1908 releases = 0;
1909 if (req->r_inode_drop)
1910 releases += ceph_encode_inode_release(&p,
1911 req->r_inode ? req->r_inode : req->r_dentry->d_inode,
1912 mds, req->r_inode_drop, req->r_inode_unless, 0);
1913 if (req->r_dentry_drop)
1914 releases += ceph_encode_dentry_release(&p, req->r_dentry,
1915 mds, req->r_dentry_drop, req->r_dentry_unless);
1916 if (req->r_old_dentry_drop)
1917 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
1918 mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
1919 if (req->r_old_inode_drop)
1920 releases += ceph_encode_inode_release(&p,
1921 req->r_old_dentry->d_inode,
1922 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
1923 head->num_releases = cpu_to_le16(releases);
1924
Sage Weilb8e69062014-05-21 17:41:08 -07001925 /* time stamp */
1926 ceph_encode_copy(&p, &req->r_stamp, sizeof(req->r_stamp));
1927
Sage Weil2f2dc052009-10-06 11:31:09 -07001928 BUG_ON(p > end);
1929 msg->front.iov_len = p - msg->front.iov_base;
1930 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1931
Yan, Zheng25e6bae2014-09-16 19:15:28 +08001932 if (req->r_pagelist) {
1933 struct ceph_pagelist *pagelist = req->r_pagelist;
1934 atomic_inc(&pagelist->refcnt);
1935 ceph_msg_data_add_pagelist(msg, pagelist);
1936 msg->hdr.data_len = cpu_to_le32(pagelist->length);
1937 } else {
1938 msg->hdr.data_len = 0;
Alex Elderebf18f42013-03-04 22:29:57 -06001939 }
Alex Elder02afca62013-02-14 12:16:43 -06001940
Sage Weil2f2dc052009-10-06 11:31:09 -07001941 msg->hdr.data_off = cpu_to_le16(0);
1942
1943out_free2:
1944 if (freepath2)
1945 kfree((char *)path2);
1946out_free1:
1947 if (freepath1)
1948 kfree((char *)path1);
1949out:
1950 return msg;
1951}
1952
1953/*
1954 * called under mdsc->mutex if error, under no mutex if
1955 * success.
1956 */
1957static void complete_request(struct ceph_mds_client *mdsc,
1958 struct ceph_mds_request *req)
1959{
1960 if (req->r_callback)
1961 req->r_callback(mdsc, req);
1962 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001963 complete_all(&req->r_completion);
Sage Weil2f2dc052009-10-06 11:31:09 -07001964}
1965
1966/*
1967 * called under mdsc->mutex
1968 */
1969static int __prepare_send_request(struct ceph_mds_client *mdsc,
1970 struct ceph_mds_request *req,
1971 int mds)
1972{
1973 struct ceph_mds_request_head *rhead;
1974 struct ceph_msg *msg;
1975 int flags = 0;
1976
Sage Weil2f2dc052009-10-06 11:31:09 -07001977 req->r_attempts++;
Greg Farnume55b71f2010-06-22 15:58:01 -07001978 if (req->r_inode) {
1979 struct ceph_cap *cap =
1980 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
1981
1982 if (cap)
1983 req->r_sent_on_mseq = cap->mseq;
1984 else
1985 req->r_sent_on_mseq = -1;
1986 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001987 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
1988 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
1989
Sage Weil01a92f12010-07-15 13:24:32 -07001990 if (req->r_got_unsafe) {
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08001991 void *p;
Sage Weil01a92f12010-07-15 13:24:32 -07001992 /*
1993 * Replay. Do not regenerate message (and rebuild
1994 * paths, etc.); just use the original message.
1995 * Rebuilding paths will break for renames because
1996 * d_move mangles the src name.
1997 */
1998 msg = req->r_request;
1999 rhead = msg->front.iov_base;
2000
2001 flags = le32_to_cpu(rhead->flags);
2002 flags |= CEPH_MDS_FLAG_REPLAY;
2003 rhead->flags = cpu_to_le32(flags);
2004
2005 if (req->r_target_inode)
2006 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
2007
2008 rhead->num_retry = req->r_attempts - 1;
Sage Weile979cf52010-07-15 14:58:39 -07002009
2010 /* remove cap/dentry releases from message */
2011 rhead->num_releases = 0;
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002012
2013 /* time stamp */
2014 p = msg->front.iov_base + req->r_request_release_offset;
2015 ceph_encode_copy(&p, &req->r_stamp, sizeof(req->r_stamp));
2016
2017 msg->front.iov_len = p - msg->front.iov_base;
2018 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
Sage Weil01a92f12010-07-15 13:24:32 -07002019 return 0;
2020 }
2021
Sage Weil2f2dc052009-10-06 11:31:09 -07002022 if (req->r_request) {
2023 ceph_msg_put(req->r_request);
2024 req->r_request = NULL;
2025 }
2026 msg = create_request_message(mdsc, req, mds);
2027 if (IS_ERR(msg)) {
Sage Weile1518c72010-05-13 11:19:06 -07002028 req->r_err = PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002029 complete_request(mdsc, req);
Sage Weila79832f2010-04-01 16:06:19 -07002030 return PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002031 }
2032 req->r_request = msg;
2033
2034 rhead = msg->front.iov_base;
Sage Weil2f2dc052009-10-06 11:31:09 -07002035 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
2036 if (req->r_got_unsafe)
2037 flags |= CEPH_MDS_FLAG_REPLAY;
2038 if (req->r_locked_dir)
2039 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
2040 rhead->flags = cpu_to_le32(flags);
2041 rhead->num_fwd = req->r_num_fwd;
2042 rhead->num_retry = req->r_attempts - 1;
Sage Weil01a92f12010-07-15 13:24:32 -07002043 rhead->ino = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002044
2045 dout(" r_locked_dir = %p\n", req->r_locked_dir);
Sage Weil2f2dc052009-10-06 11:31:09 -07002046 return 0;
2047}
2048
2049/*
2050 * send request, or put it on the appropriate wait list.
2051 */
2052static int __do_request(struct ceph_mds_client *mdsc,
2053 struct ceph_mds_request *req)
2054{
2055 struct ceph_mds_session *session = NULL;
2056 int mds = -1;
2057 int err = -EAGAIN;
2058
Yan, Zhengeb1b8af2013-09-26 14:25:36 +08002059 if (req->r_err || req->r_got_result) {
2060 if (req->r_aborted)
2061 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002062 goto out;
Yan, Zhengeb1b8af2013-09-26 14:25:36 +08002063 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002064
2065 if (req->r_timeout &&
2066 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
2067 dout("do_request timed out\n");
2068 err = -EIO;
2069 goto finish;
2070 }
2071
Sage Weildc69e2e2010-11-02 13:49:00 -07002072 put_request_session(req);
2073
Sage Weil2f2dc052009-10-06 11:31:09 -07002074 mds = __choose_mds(mdsc, req);
2075 if (mds < 0 ||
2076 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
2077 dout("do_request no mds or not active, waiting for map\n");
2078 list_add(&req->r_wait, &mdsc->waiting_for_map);
2079 goto out;
2080 }
2081
2082 /* get, open session */
2083 session = __ceph_lookup_mds_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07002084 if (!session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002085 session = register_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07002086 if (IS_ERR(session)) {
2087 err = PTR_ERR(session);
2088 goto finish;
2089 }
2090 }
Sage Weildc69e2e2010-11-02 13:49:00 -07002091 req->r_session = get_session(session);
2092
Sage Weil2f2dc052009-10-06 11:31:09 -07002093 dout("do_request mds%d session %p state %s\n", mds, session,
John Spraya687eca2014-09-19 13:51:08 +01002094 ceph_session_state_name(session->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07002095 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
2096 session->s_state != CEPH_MDS_SESSION_HUNG) {
2097 if (session->s_state == CEPH_MDS_SESSION_NEW ||
2098 session->s_state == CEPH_MDS_SESSION_CLOSING)
2099 __open_session(mdsc, session);
2100 list_add(&req->r_wait, &session->s_waiting);
2101 goto out_session;
2102 }
2103
2104 /* send request */
Sage Weil2f2dc052009-10-06 11:31:09 -07002105 req->r_resend_mds = -1; /* forget any previous mds hint */
2106
2107 if (req->r_request_started == 0) /* note request start time */
2108 req->r_request_started = jiffies;
2109
2110 err = __prepare_send_request(mdsc, req, mds);
2111 if (!err) {
2112 ceph_msg_get(req->r_request);
2113 ceph_con_send(&session->s_con, req->r_request);
2114 }
2115
2116out_session:
2117 ceph_put_mds_session(session);
2118out:
2119 return err;
2120
2121finish:
Sage Weile1518c72010-05-13 11:19:06 -07002122 req->r_err = err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002123 complete_request(mdsc, req);
2124 goto out;
2125}
2126
2127/*
2128 * called under mdsc->mutex
2129 */
2130static void __wake_requests(struct ceph_mds_client *mdsc,
2131 struct list_head *head)
2132{
Yan, Zhenged75ec22012-11-19 10:49:06 +08002133 struct ceph_mds_request *req;
2134 LIST_HEAD(tmp_list);
Sage Weil2f2dc052009-10-06 11:31:09 -07002135
Yan, Zhenged75ec22012-11-19 10:49:06 +08002136 list_splice_init(head, &tmp_list);
2137
2138 while (!list_empty(&tmp_list)) {
2139 req = list_entry(tmp_list.next,
2140 struct ceph_mds_request, r_wait);
Sage Weil2f2dc052009-10-06 11:31:09 -07002141 list_del_init(&req->r_wait);
Sage Weil7971bd92013-05-01 21:15:58 -07002142 dout(" wake request %p tid %llu\n", req, req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002143 __do_request(mdsc, req);
2144 }
2145}
2146
2147/*
2148 * Wake up threads with requests pending for @mds, so that they can
Sage Weil29790f22010-03-18 14:45:05 -07002149 * resubmit their requests to a possibly different mds.
Sage Weil2f2dc052009-10-06 11:31:09 -07002150 */
Sage Weil29790f22010-03-18 14:45:05 -07002151static void kick_requests(struct ceph_mds_client *mdsc, int mds)
Sage Weil2f2dc052009-10-06 11:31:09 -07002152{
Sage Weil44ca18f2010-02-15 12:08:46 -08002153 struct ceph_mds_request *req;
Yan, Zheng282c1052014-07-30 10:12:47 +08002154 struct rb_node *p = rb_first(&mdsc->request_tree);
Sage Weil2f2dc052009-10-06 11:31:09 -07002155
2156 dout("kick_requests mds%d\n", mds);
Yan, Zheng282c1052014-07-30 10:12:47 +08002157 while (p) {
Sage Weil44ca18f2010-02-15 12:08:46 -08002158 req = rb_entry(p, struct ceph_mds_request, r_node);
Yan, Zheng282c1052014-07-30 10:12:47 +08002159 p = rb_next(p);
Sage Weil44ca18f2010-02-15 12:08:46 -08002160 if (req->r_got_unsafe)
2161 continue;
2162 if (req->r_session &&
2163 req->r_session->s_mds == mds) {
2164 dout(" kicking tid %llu\n", req->r_tid);
Yan, Zheng03974e82014-09-11 14:28:56 +08002165 list_del_init(&req->r_wait);
Sage Weil44ca18f2010-02-15 12:08:46 -08002166 __do_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002167 }
2168 }
2169}
2170
2171void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
2172 struct ceph_mds_request *req)
2173{
2174 dout("submit_request on %p\n", req);
2175 mutex_lock(&mdsc->mutex);
2176 __register_request(mdsc, req, NULL);
2177 __do_request(mdsc, req);
2178 mutex_unlock(&mdsc->mutex);
2179}
2180
2181/*
2182 * Synchrously perform an mds request. Take care of all of the
2183 * session setup, forwarding, retry details.
2184 */
2185int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
2186 struct inode *dir,
2187 struct ceph_mds_request *req)
2188{
2189 int err;
2190
2191 dout("do_request on %p\n", req);
2192
2193 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
2194 if (req->r_inode)
2195 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
2196 if (req->r_locked_dir)
2197 ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
Sage Weil844d87c2013-02-05 13:40:09 -08002198 if (req->r_old_dentry_dir)
Sage Weil41b02e12011-07-26 11:31:14 -07002199 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
2200 CEPH_CAP_PIN);
Sage Weil2f2dc052009-10-06 11:31:09 -07002201
2202 /* issue */
2203 mutex_lock(&mdsc->mutex);
2204 __register_request(mdsc, req, dir);
2205 __do_request(mdsc, req);
2206
Sage Weile1518c72010-05-13 11:19:06 -07002207 if (req->r_err) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002208 err = req->r_err;
Sage Weile1518c72010-05-13 11:19:06 -07002209 __unregister_request(mdsc, req);
2210 dout("do_request early error %d\n", err);
2211 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07002212 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002213
Sage Weile1518c72010-05-13 11:19:06 -07002214 /* wait */
2215 mutex_unlock(&mdsc->mutex);
2216 dout("do_request waiting\n");
2217 if (req->r_timeout) {
Sage Weilaa916472010-05-24 11:15:51 -07002218 err = (long)wait_for_completion_killable_timeout(
Sage Weile1518c72010-05-13 11:19:06 -07002219 &req->r_completion, req->r_timeout);
2220 if (err == 0)
2221 err = -EIO;
Yan, Zheng9280be22014-10-14 10:33:35 +08002222 } else if (req->r_wait_for_completion) {
2223 err = req->r_wait_for_completion(mdsc, req);
Sage Weile1518c72010-05-13 11:19:06 -07002224 } else {
Sage Weilaa916472010-05-24 11:15:51 -07002225 err = wait_for_completion_killable(&req->r_completion);
Sage Weile1518c72010-05-13 11:19:06 -07002226 }
2227 dout("do_request waited, got %d\n", err);
2228 mutex_lock(&mdsc->mutex);
2229
2230 /* only abort if we didn't race with a real reply */
2231 if (req->r_got_result) {
2232 err = le32_to_cpu(req->r_reply_info.head->result);
2233 } else if (err < 0) {
2234 dout("aborted request %lld with %d\n", req->r_tid, err);
Sage Weilb4556392010-05-13 12:01:13 -07002235
2236 /*
2237 * ensure we aren't running concurrently with
2238 * ceph_fill_trace or ceph_readdir_prepopulate, which
2239 * rely on locks (dir mutex) held by our caller.
2240 */
2241 mutex_lock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07002242 req->r_err = err;
2243 req->r_aborted = true;
Sage Weilb4556392010-05-13 12:01:13 -07002244 mutex_unlock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07002245
2246 if (req->r_locked_dir &&
Sage Weil167c9e32010-05-14 10:02:57 -07002247 (req->r_op & CEPH_MDS_OP_WRITE))
2248 ceph_invalidate_dir_request(req);
Sage Weile1518c72010-05-13 11:19:06 -07002249 } else {
2250 err = req->r_err;
2251 }
2252
2253out:
2254 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002255 dout("do_request %p done, result %d\n", req, err);
2256 return err;
2257}
2258
2259/*
Yan, Zheng2f276c52013-03-13 19:44:32 +08002260 * Invalidate dir's completeness, dentry lease state on an aborted MDS
Sage Weil167c9e32010-05-14 10:02:57 -07002261 * namespace request.
2262 */
2263void ceph_invalidate_dir_request(struct ceph_mds_request *req)
2264{
2265 struct inode *inode = req->r_locked_dir;
Sage Weil167c9e32010-05-14 10:02:57 -07002266
Yan, Zheng2f276c52013-03-13 19:44:32 +08002267 dout("invalidate_dir_request %p (complete, lease(s))\n", inode);
Sage Weil167c9e32010-05-14 10:02:57 -07002268
Yan, Zheng2f276c52013-03-13 19:44:32 +08002269 ceph_dir_clear_complete(inode);
Sage Weil167c9e32010-05-14 10:02:57 -07002270 if (req->r_dentry)
2271 ceph_invalidate_dentry_lease(req->r_dentry);
2272 if (req->r_old_dentry)
2273 ceph_invalidate_dentry_lease(req->r_old_dentry);
2274}
2275
2276/*
Sage Weil2f2dc052009-10-06 11:31:09 -07002277 * Handle mds reply.
2278 *
2279 * We take the session mutex and parse and process the reply immediately.
2280 * This preserves the logical ordering of replies, capabilities, etc., sent
2281 * by the MDS as they are applied to our local cache.
2282 */
2283static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2284{
2285 struct ceph_mds_client *mdsc = session->s_mdsc;
2286 struct ceph_mds_request *req;
2287 struct ceph_mds_reply_head *head = msg->front.iov_base;
2288 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
2289 u64 tid;
2290 int err, result;
Sage Weil2600d2d2010-02-22 15:12:16 -08002291 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002292
Sage Weil2f2dc052009-10-06 11:31:09 -07002293 if (msg->front.iov_len < sizeof(*head)) {
2294 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002295 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002296 return;
2297 }
2298
2299 /* get request, session */
Sage Weil6df058c2009-12-22 11:24:33 -08002300 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002301 mutex_lock(&mdsc->mutex);
2302 req = __lookup_request(mdsc, tid);
2303 if (!req) {
2304 dout("handle_reply on unknown tid %llu\n", tid);
2305 mutex_unlock(&mdsc->mutex);
2306 return;
2307 }
2308 dout("handle_reply %p\n", req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002309
2310 /* correct session? */
Sage Weild96d6042010-03-20 20:50:58 -07002311 if (req->r_session != session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002312 pr_err("mdsc_handle_reply got %llu on session mds%d"
2313 " not mds%d\n", tid, session->s_mds,
2314 req->r_session ? req->r_session->s_mds : -1);
2315 mutex_unlock(&mdsc->mutex);
2316 goto out;
2317 }
2318
2319 /* dup? */
2320 if ((req->r_got_unsafe && !head->safe) ||
2321 (req->r_got_safe && head->safe)) {
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07002322 pr_warn("got a dup %s reply on %llu from mds%d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002323 head->safe ? "safe" : "unsafe", tid, mds);
2324 mutex_unlock(&mdsc->mutex);
2325 goto out;
2326 }
Sage Weil85792d0d2010-05-13 09:06:02 -07002327 if (req->r_got_safe && !head->safe) {
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07002328 pr_warn("got unsafe after safe on %llu from mds%d\n",
Sage Weil85792d0d2010-05-13 09:06:02 -07002329 tid, mds);
2330 mutex_unlock(&mdsc->mutex);
2331 goto out;
2332 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002333
2334 result = le32_to_cpu(head->result);
2335
2336 /*
Greg Farnume55b71f2010-06-22 15:58:01 -07002337 * Handle an ESTALE
2338 * if we're not talking to the authority, send to them
2339 * if the authority has changed while we weren't looking,
2340 * send to new authority
2341 * Otherwise we just have to return an ESTALE
Sage Weil2f2dc052009-10-06 11:31:09 -07002342 */
2343 if (result == -ESTALE) {
Greg Farnume55b71f2010-06-22 15:58:01 -07002344 dout("got ESTALE on request %llu", req->r_tid);
Yan, Zheng51da8e82014-07-14 10:48:29 +08002345 req->r_resend_mds = -1;
Yan, Zhengca18bed2013-11-22 14:21:44 +08002346 if (req->r_direct_mode != USE_AUTH_MDS) {
Greg Farnume55b71f2010-06-22 15:58:01 -07002347 dout("not using auth, setting for that now");
2348 req->r_direct_mode = USE_AUTH_MDS;
Sage Weil2f2dc052009-10-06 11:31:09 -07002349 __do_request(mdsc, req);
2350 mutex_unlock(&mdsc->mutex);
2351 goto out;
Greg Farnume55b71f2010-06-22 15:58:01 -07002352 } else {
Yan, Zhengca18bed2013-11-22 14:21:44 +08002353 int mds = __choose_mds(mdsc, req);
2354 if (mds >= 0 && mds != req->r_session->s_mds) {
2355 dout("but auth changed, so resending");
Greg Farnume55b71f2010-06-22 15:58:01 -07002356 __do_request(mdsc, req);
2357 mutex_unlock(&mdsc->mutex);
2358 goto out;
2359 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002360 }
Greg Farnume55b71f2010-06-22 15:58:01 -07002361 dout("have to return ESTALE on request %llu", req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002362 }
2363
Greg Farnume55b71f2010-06-22 15:58:01 -07002364
Sage Weil2f2dc052009-10-06 11:31:09 -07002365 if (head->safe) {
2366 req->r_got_safe = true;
2367 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002368
2369 if (req->r_got_unsafe) {
2370 /*
2371 * We already handled the unsafe response, now do the
2372 * cleanup. No need to examine the response; the MDS
2373 * doesn't include any result info in the safe
2374 * response. And even if it did, there is nothing
2375 * useful we could do with a revised return value.
2376 */
2377 dout("got safe reply %llu, mds%d\n", tid, mds);
2378 list_del_init(&req->r_unsafe_item);
2379
2380 /* last unsafe request during umount? */
Sage Weil44ca18f2010-02-15 12:08:46 -08002381 if (mdsc->stopping && !__get_oldest_req(mdsc))
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002382 complete_all(&mdsc->safe_umount_waiters);
Sage Weil2f2dc052009-10-06 11:31:09 -07002383 mutex_unlock(&mdsc->mutex);
2384 goto out;
2385 }
Sage Weile1518c72010-05-13 11:19:06 -07002386 } else {
Sage Weil2f2dc052009-10-06 11:31:09 -07002387 req->r_got_unsafe = true;
2388 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
2389 }
2390
2391 dout("handle_reply tid %lld result %d\n", tid, result);
2392 rinfo = &req->r_reply_info;
Sage Weil14303d22010-12-14 17:37:52 -08002393 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
Sage Weil2f2dc052009-10-06 11:31:09 -07002394 mutex_unlock(&mdsc->mutex);
2395
2396 mutex_lock(&session->s_mutex);
2397 if (err < 0) {
Herb Shiu25933ab2010-12-01 14:14:38 -08002398 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002399 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002400 goto out_err;
2401 }
2402
2403 /* snap trace */
2404 if (rinfo->snapblob_len) {
2405 down_write(&mdsc->snap_rwsem);
2406 ceph_update_snap_trace(mdsc, rinfo->snapblob,
2407 rinfo->snapblob + rinfo->snapblob_len,
2408 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP);
2409 downgrade_write(&mdsc->snap_rwsem);
2410 } else {
2411 down_read(&mdsc->snap_rwsem);
2412 }
2413
2414 /* insert trace into our cache */
Sage Weilb4556392010-05-13 12:01:13 -07002415 mutex_lock(&req->r_fill_mutex);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002416 err = ceph_fill_trace(mdsc->fsc->sb, req, req->r_session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002417 if (err == 0) {
Sam Lang6e8575f2012-12-28 09:56:46 -08002418 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
Yan, Zheng81c6aea2013-09-18 09:44:13 +08002419 req->r_op == CEPH_MDS_OP_LSSNAP))
Sage Weil2f2dc052009-10-06 11:31:09 -07002420 ceph_readdir_prepopulate(req, req->r_session);
Yehuda Sadeh37151662010-06-17 16:16:12 -07002421 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
Sage Weil2f2dc052009-10-06 11:31:09 -07002422 }
Sage Weilb4556392010-05-13 12:01:13 -07002423 mutex_unlock(&req->r_fill_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002424
2425 up_read(&mdsc->snap_rwsem);
2426out_err:
Sage Weile1518c72010-05-13 11:19:06 -07002427 mutex_lock(&mdsc->mutex);
2428 if (!req->r_aborted) {
2429 if (err) {
2430 req->r_err = err;
2431 } else {
2432 req->r_reply = msg;
2433 ceph_msg_get(msg);
2434 req->r_got_result = true;
2435 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002436 } else {
Sage Weile1518c72010-05-13 11:19:06 -07002437 dout("reply arrived after request %lld was aborted\n", tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002438 }
Sage Weile1518c72010-05-13 11:19:06 -07002439 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002440
Sage Weilee6b2722010-06-10 12:55:52 -07002441 ceph_add_cap_releases(mdsc, req->r_session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002442 mutex_unlock(&session->s_mutex);
2443
2444 /* kick calling process */
2445 complete_request(mdsc, req);
2446out:
2447 ceph_mdsc_put_request(req);
2448 return;
2449}
2450
2451
2452
2453/*
2454 * handle mds notification that our request has been forwarded.
2455 */
Sage Weil2600d2d2010-02-22 15:12:16 -08002456static void handle_forward(struct ceph_mds_client *mdsc,
2457 struct ceph_mds_session *session,
2458 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002459{
2460 struct ceph_mds_request *req;
Sage Weila1ea7872010-02-23 14:02:44 -08002461 u64 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002462 u32 next_mds;
2463 u32 fwd_seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002464 int err = -EINVAL;
2465 void *p = msg->front.iov_base;
2466 void *end = p + msg->front.iov_len;
Sage Weil2f2dc052009-10-06 11:31:09 -07002467
Sage Weila1ea7872010-02-23 14:02:44 -08002468 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002469 next_mds = ceph_decode_32(&p);
2470 fwd_seq = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07002471
2472 mutex_lock(&mdsc->mutex);
2473 req = __lookup_request(mdsc, tid);
2474 if (!req) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07002475 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002476 goto out; /* dup reply? */
2477 }
2478
Sage Weil2a8e5e32010-05-28 16:43:16 -07002479 if (req->r_aborted) {
2480 dout("forward tid %llu aborted, unregistering\n", tid);
2481 __unregister_request(mdsc, req);
2482 } else if (fwd_seq <= req->r_num_fwd) {
2483 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002484 tid, next_mds, req->r_num_fwd, fwd_seq);
2485 } else {
2486 /* resend. forward race not possible; mds would drop */
Sage Weil2a8e5e32010-05-28 16:43:16 -07002487 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2488 BUG_ON(req->r_err);
2489 BUG_ON(req->r_got_result);
Sage Weil2f2dc052009-10-06 11:31:09 -07002490 req->r_num_fwd = fwd_seq;
2491 req->r_resend_mds = next_mds;
2492 put_request_session(req);
2493 __do_request(mdsc, req);
2494 }
2495 ceph_mdsc_put_request(req);
2496out:
2497 mutex_unlock(&mdsc->mutex);
2498 return;
2499
2500bad:
2501 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2502}
2503
2504/*
2505 * handle a mds session control message
2506 */
2507static void handle_session(struct ceph_mds_session *session,
2508 struct ceph_msg *msg)
2509{
2510 struct ceph_mds_client *mdsc = session->s_mdsc;
2511 u32 op;
2512 u64 seq;
Sage Weil2600d2d2010-02-22 15:12:16 -08002513 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002514 struct ceph_mds_session_head *h = msg->front.iov_base;
2515 int wake = 0;
2516
Sage Weil2f2dc052009-10-06 11:31:09 -07002517 /* decode */
2518 if (msg->front.iov_len != sizeof(*h))
2519 goto bad;
2520 op = le32_to_cpu(h->op);
2521 seq = le64_to_cpu(h->seq);
2522
2523 mutex_lock(&mdsc->mutex);
Sage Weil2600d2d2010-02-22 15:12:16 -08002524 if (op == CEPH_SESSION_CLOSE)
2525 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002526 /* FIXME: this ttl calculation is generous */
2527 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2528 mutex_unlock(&mdsc->mutex);
2529
2530 mutex_lock(&session->s_mutex);
2531
2532 dout("handle_session mds%d %s %p state %s seq %llu\n",
2533 mds, ceph_session_op_name(op), session,
John Spraya687eca2014-09-19 13:51:08 +01002534 ceph_session_state_name(session->s_state), seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002535
2536 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2537 session->s_state = CEPH_MDS_SESSION_OPEN;
2538 pr_info("mds%d came back\n", session->s_mds);
2539 }
2540
2541 switch (op) {
2542 case CEPH_SESSION_OPEN:
Sage Weil29790f22010-03-18 14:45:05 -07002543 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2544 pr_info("mds%d reconnect success\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002545 session->s_state = CEPH_MDS_SESSION_OPEN;
2546 renewed_caps(mdsc, session, 0);
2547 wake = 1;
2548 if (mdsc->stopping)
2549 __close_session(mdsc, session);
2550 break;
2551
2552 case CEPH_SESSION_RENEWCAPS:
2553 if (session->s_renew_seq == seq)
2554 renewed_caps(mdsc, session, 1);
2555 break;
2556
2557 case CEPH_SESSION_CLOSE:
Sage Weil29790f22010-03-18 14:45:05 -07002558 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2559 pr_info("mds%d reconnect denied\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002560 remove_session_caps(session);
Yan, Zheng656e4382014-09-11 14:25:18 +08002561 wake = 2; /* for good measure */
Sage Weilf3c60c52010-08-11 14:51:23 -07002562 wake_up_all(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002563 break;
2564
2565 case CEPH_SESSION_STALE:
2566 pr_info("mds%d caps went stale, renewing\n",
2567 session->s_mds);
Alex Elderd8fb02a2012-01-12 17:48:10 -08002568 spin_lock(&session->s_gen_ttl_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002569 session->s_cap_gen++;
Alex Elder1ce208a2012-01-12 17:48:11 -08002570 session->s_cap_ttl = jiffies - 1;
Alex Elderd8fb02a2012-01-12 17:48:10 -08002571 spin_unlock(&session->s_gen_ttl_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002572 send_renew_caps(mdsc, session);
2573 break;
2574
2575 case CEPH_SESSION_RECALL_STATE:
2576 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2577 break;
2578
Yan, Zheng186e4f72013-11-22 14:48:37 +08002579 case CEPH_SESSION_FLUSHMSG:
2580 send_flushmsg_ack(mdsc, session, seq);
2581 break;
2582
Sage Weil2f2dc052009-10-06 11:31:09 -07002583 default:
2584 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2585 WARN_ON(1);
2586 }
2587
2588 mutex_unlock(&session->s_mutex);
2589 if (wake) {
2590 mutex_lock(&mdsc->mutex);
2591 __wake_requests(mdsc, &session->s_waiting);
Yan, Zheng656e4382014-09-11 14:25:18 +08002592 if (wake == 2)
2593 kick_requests(mdsc, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002594 mutex_unlock(&mdsc->mutex);
2595 }
2596 return;
2597
2598bad:
2599 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2600 (int)msg->front.iov_len);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002601 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002602 return;
2603}
2604
2605
2606/*
2607 * called under session->mutex.
2608 */
2609static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2610 struct ceph_mds_session *session)
2611{
2612 struct ceph_mds_request *req, *nreq;
2613 int err;
2614
2615 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2616
2617 mutex_lock(&mdsc->mutex);
2618 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
2619 err = __prepare_send_request(mdsc, req, session->s_mds);
2620 if (!err) {
2621 ceph_msg_get(req->r_request);
2622 ceph_con_send(&session->s_con, req->r_request);
2623 }
2624 }
2625 mutex_unlock(&mdsc->mutex);
2626}
2627
2628/*
2629 * Encode information about a cap for a reconnect with the MDS.
2630 */
Sage Weil2f2dc052009-10-06 11:31:09 -07002631static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2632 void *arg)
2633{
Sage Weil20cb34a2010-05-12 15:21:32 -07002634 union {
2635 struct ceph_mds_cap_reconnect v2;
2636 struct ceph_mds_cap_reconnect_v1 v1;
2637 } rec;
2638 size_t reclen;
Sage Weil2f2dc052009-10-06 11:31:09 -07002639 struct ceph_inode_info *ci;
Sage Weil20cb34a2010-05-12 15:21:32 -07002640 struct ceph_reconnect_state *recon_state = arg;
2641 struct ceph_pagelist *pagelist = recon_state->pagelist;
Sage Weil2f2dc052009-10-06 11:31:09 -07002642 char *path;
2643 int pathlen, err;
2644 u64 pathbase;
2645 struct dentry *dentry;
2646
2647 ci = cap->ci;
2648
2649 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2650 inode, ceph_vinop(inode), cap, cap->cap_id,
2651 ceph_cap_string(cap->issued));
Sage Weil93cea5b2009-12-23 12:21:51 -08002652 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2653 if (err)
2654 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002655
2656 dentry = d_find_alias(inode);
2657 if (dentry) {
2658 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2659 if (IS_ERR(path)) {
2660 err = PTR_ERR(path);
Sage Weile072f8a2010-08-26 09:26:37 -07002661 goto out_dput;
Sage Weil2f2dc052009-10-06 11:31:09 -07002662 }
2663 } else {
2664 path = NULL;
2665 pathlen = 0;
2666 }
Sage Weil93cea5b2009-12-23 12:21:51 -08002667 err = ceph_pagelist_encode_string(pagelist, path, pathlen);
2668 if (err)
Sage Weile072f8a2010-08-26 09:26:37 -07002669 goto out_free;
Sage Weil2f2dc052009-10-06 11:31:09 -07002670
Sage Weilbe655592011-11-30 09:47:09 -08002671 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002672 cap->seq = 0; /* reset cap seq */
2673 cap->issue_seq = 0; /* and issue_seq */
Yan, Zheng667ca052013-05-31 16:25:36 +08002674 cap->mseq = 0; /* and migrate_seq */
Yan, Zheng99a9c272013-09-22 11:08:14 +08002675 cap->cap_gen = cap->session->s_cap_gen;
Sage Weil20cb34a2010-05-12 15:21:32 -07002676
2677 if (recon_state->flock) {
2678 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
2679 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2680 rec.v2.issued = cpu_to_le32(cap->issued);
2681 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2682 rec.v2.pathbase = cpu_to_le64(pathbase);
2683 rec.v2.flock_len = 0;
2684 reclen = sizeof(rec.v2);
2685 } else {
2686 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
2687 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2688 rec.v1.issued = cpu_to_le32(cap->issued);
2689 rec.v1.size = cpu_to_le64(inode->i_size);
2690 ceph_encode_timespec(&rec.v1.mtime, &inode->i_mtime);
2691 ceph_encode_timespec(&rec.v1.atime, &inode->i_atime);
2692 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2693 rec.v1.pathbase = cpu_to_le64(pathbase);
2694 reclen = sizeof(rec.v1);
2695 }
Sage Weilbe655592011-11-30 09:47:09 -08002696 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002697
Greg Farnum40819f62010-08-02 15:34:23 -07002698 if (recon_state->flock) {
2699 int num_fcntl_locks, num_flock_locks;
Jim Schutt39be95e2013-05-15 13:03:35 -05002700 struct ceph_filelock *flocks;
Greg Farnum40819f62010-08-02 15:34:23 -07002701
Jim Schutt39be95e2013-05-15 13:03:35 -05002702encode_again:
Jeff Layton1c8c6012013-06-21 08:58:15 -04002703 spin_lock(&inode->i_lock);
Jim Schutt39be95e2013-05-15 13:03:35 -05002704 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
Jeff Layton1c8c6012013-06-21 08:58:15 -04002705 spin_unlock(&inode->i_lock);
Jim Schutt39be95e2013-05-15 13:03:35 -05002706 flocks = kmalloc((num_fcntl_locks+num_flock_locks) *
2707 sizeof(struct ceph_filelock), GFP_NOFS);
2708 if (!flocks) {
2709 err = -ENOMEM;
2710 goto out_free;
2711 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04002712 spin_lock(&inode->i_lock);
Jim Schutt39be95e2013-05-15 13:03:35 -05002713 err = ceph_encode_locks_to_buffer(inode, flocks,
2714 num_fcntl_locks,
2715 num_flock_locks);
Jeff Layton1c8c6012013-06-21 08:58:15 -04002716 spin_unlock(&inode->i_lock);
Jim Schutt39be95e2013-05-15 13:03:35 -05002717 if (err) {
2718 kfree(flocks);
2719 if (err == -ENOSPC)
2720 goto encode_again;
2721 goto out_free;
2722 }
2723 /*
2724 * number of encoded locks is stable, so copy to pagelist
2725 */
2726 rec.v2.flock_len = cpu_to_le32(2*sizeof(u32) +
2727 (num_fcntl_locks+num_flock_locks) *
2728 sizeof(struct ceph_filelock));
2729 err = ceph_pagelist_append(pagelist, &rec, reclen);
2730 if (!err)
2731 err = ceph_locks_to_pagelist(flocks, pagelist,
2732 num_fcntl_locks,
2733 num_flock_locks);
2734 kfree(flocks);
Sage Weil3612abb2010-09-07 15:59:27 -07002735 } else {
2736 err = ceph_pagelist_append(pagelist, &rec, reclen);
Greg Farnum40819f62010-08-02 15:34:23 -07002737 }
Yan, Zheng44c99752013-09-22 10:28:10 +08002738
2739 recon_state->nr_caps++;
Sage Weile072f8a2010-08-26 09:26:37 -07002740out_free:
Sage Weil2f2dc052009-10-06 11:31:09 -07002741 kfree(path);
Sage Weile072f8a2010-08-26 09:26:37 -07002742out_dput:
Sage Weil2f2dc052009-10-06 11:31:09 -07002743 dput(dentry);
Sage Weil93cea5b2009-12-23 12:21:51 -08002744 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002745}
2746
2747
2748/*
2749 * If an MDS fails and recovers, clients need to reconnect in order to
2750 * reestablish shared state. This includes all caps issued through
2751 * this session _and_ the snap_realm hierarchy. Because it's not
2752 * clear which snap realms the mds cares about, we send everything we
2753 * know about.. that ensures we'll then get any new info the
2754 * recovering MDS might have.
2755 *
2756 * This is a relatively heavyweight operation, but it's rare.
2757 *
2758 * called with mdsc->mutex held.
2759 */
Sage Weil34b6c852010-05-10 16:31:25 -07002760static void send_mds_reconnect(struct ceph_mds_client *mdsc,
2761 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07002762{
Sage Weil2f2dc052009-10-06 11:31:09 -07002763 struct ceph_msg *reply;
Sage Weila105f002010-02-15 14:37:55 -08002764 struct rb_node *p;
Sage Weil34b6c852010-05-10 16:31:25 -07002765 int mds = session->s_mds;
Sage Weil9abf82b2010-05-10 21:58:38 -07002766 int err = -ENOMEM;
Yan, Zheng44c99752013-09-22 10:28:10 +08002767 int s_nr_caps;
Sage Weil93cea5b2009-12-23 12:21:51 -08002768 struct ceph_pagelist *pagelist;
Sage Weil20cb34a2010-05-12 15:21:32 -07002769 struct ceph_reconnect_state recon_state;
Sage Weil2f2dc052009-10-06 11:31:09 -07002770
Sage Weil34b6c852010-05-10 16:31:25 -07002771 pr_info("mds%d reconnect start\n", mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002772
Sage Weil93cea5b2009-12-23 12:21:51 -08002773 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
2774 if (!pagelist)
2775 goto fail_nopagelist;
2776 ceph_pagelist_init(pagelist);
2777
Sage Weilb61c2762011-08-09 15:03:46 -07002778 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07002779 if (!reply)
Sage Weil93cea5b2009-12-23 12:21:51 -08002780 goto fail_nomsg;
Sage Weil93cea5b2009-12-23 12:21:51 -08002781
Sage Weil34b6c852010-05-10 16:31:25 -07002782 mutex_lock(&session->s_mutex);
2783 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
2784 session->s_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002785
Sage Weil2f2dc052009-10-06 11:31:09 -07002786 dout("session %p state %s\n", session,
John Spraya687eca2014-09-19 13:51:08 +01002787 ceph_session_state_name(session->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07002788
Yan, Zheng99a9c272013-09-22 11:08:14 +08002789 spin_lock(&session->s_gen_ttl_lock);
2790 session->s_cap_gen++;
2791 spin_unlock(&session->s_gen_ttl_lock);
2792
2793 spin_lock(&session->s_cap_lock);
2794 /*
2795 * notify __ceph_remove_cap() that we are composing cap reconnect.
2796 * If a cap get released before being added to the cap reconnect,
2797 * __ceph_remove_cap() should skip queuing cap release.
2798 */
2799 session->s_cap_reconnect = 1;
Sage Weile01a5942010-05-10 15:36:44 -07002800 /* drop old cap expires; we're about to reestablish that state */
2801 discard_cap_releases(mdsc, session);
Yan, Zheng99a9c272013-09-22 11:08:14 +08002802 spin_unlock(&session->s_cap_lock);
Sage Weile01a5942010-05-10 15:36:44 -07002803
Yan, Zheng5d233712014-09-10 16:56:23 +08002804 /* trim unused caps to reduce MDS's cache rejoin time */
2805 shrink_dcache_parent(mdsc->fsc->sb->s_root);
2806
2807 ceph_con_close(&session->s_con);
2808 ceph_con_open(&session->s_con,
2809 CEPH_ENTITY_TYPE_MDS, mds,
2810 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
2811
2812 /* replay unsafe requests */
2813 replay_unsafe_requests(mdsc, session);
2814
2815 down_read(&mdsc->snap_rwsem);
2816
Sage Weil2f2dc052009-10-06 11:31:09 -07002817 /* traverse this session's caps */
Yan, Zheng44c99752013-09-22 10:28:10 +08002818 s_nr_caps = session->s_nr_caps;
2819 err = ceph_pagelist_encode_32(pagelist, s_nr_caps);
Sage Weil93cea5b2009-12-23 12:21:51 -08002820 if (err)
2821 goto fail;
Sage Weil20cb34a2010-05-12 15:21:32 -07002822
Yan, Zheng44c99752013-09-22 10:28:10 +08002823 recon_state.nr_caps = 0;
Sage Weil20cb34a2010-05-12 15:21:32 -07002824 recon_state.pagelist = pagelist;
2825 recon_state.flock = session->s_con.peer_features & CEPH_FEATURE_FLOCK;
2826 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
Sage Weil2f2dc052009-10-06 11:31:09 -07002827 if (err < 0)
Sage Weil9abf82b2010-05-10 21:58:38 -07002828 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07002829
Yan, Zheng99a9c272013-09-22 11:08:14 +08002830 spin_lock(&session->s_cap_lock);
2831 session->s_cap_reconnect = 0;
2832 spin_unlock(&session->s_cap_lock);
2833
Sage Weil2f2dc052009-10-06 11:31:09 -07002834 /*
2835 * snaprealms. we provide mds with the ino, seq (version), and
2836 * parent for all of our realms. If the mds has any newer info,
2837 * it will tell us.
2838 */
Sage Weila105f002010-02-15 14:37:55 -08002839 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
2840 struct ceph_snap_realm *realm =
2841 rb_entry(p, struct ceph_snap_realm, node);
Sage Weil93cea5b2009-12-23 12:21:51 -08002842 struct ceph_mds_snaprealm_reconnect sr_rec;
Sage Weil2f2dc052009-10-06 11:31:09 -07002843
2844 dout(" adding snap realm %llx seq %lld parent %llx\n",
2845 realm->ino, realm->seq, realm->parent_ino);
Sage Weil93cea5b2009-12-23 12:21:51 -08002846 sr_rec.ino = cpu_to_le64(realm->ino);
2847 sr_rec.seq = cpu_to_le64(realm->seq);
2848 sr_rec.parent = cpu_to_le64(realm->parent_ino);
2849 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
2850 if (err)
2851 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07002852 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002853
Sage Weil20cb34a2010-05-12 15:21:32 -07002854 if (recon_state.flock)
2855 reply->hdr.version = cpu_to_le16(2);
Yan, Zheng44c99752013-09-22 10:28:10 +08002856
2857 /* raced with cap release? */
2858 if (s_nr_caps != recon_state.nr_caps) {
2859 struct page *page = list_first_entry(&pagelist->head,
2860 struct page, lru);
2861 __le32 *addr = kmap_atomic(page);
2862 *addr = cpu_to_le32(recon_state.nr_caps);
2863 kunmap_atomic(addr);
Alex Elderebf18f42013-03-04 22:29:57 -06002864 }
Yan, Zheng44c99752013-09-22 10:28:10 +08002865
2866 reply->hdr.data_len = cpu_to_le32(pagelist->length);
2867 ceph_msg_data_add_pagelist(reply, pagelist);
Sage Weil2f2dc052009-10-06 11:31:09 -07002868 ceph_con_send(&session->s_con, reply);
2869
Sage Weil9abf82b2010-05-10 21:58:38 -07002870 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002871
Sage Weil9abf82b2010-05-10 21:58:38 -07002872 mutex_lock(&mdsc->mutex);
2873 __wake_requests(mdsc, &session->s_waiting);
2874 mutex_unlock(&mdsc->mutex);
2875
Sage Weil2f2dc052009-10-06 11:31:09 -07002876 up_read(&mdsc->snap_rwsem);
Sage Weil2f2dc052009-10-06 11:31:09 -07002877 return;
2878
Sage Weil93cea5b2009-12-23 12:21:51 -08002879fail:
Sage Weil2f2dc052009-10-06 11:31:09 -07002880 ceph_msg_put(reply);
Sage Weil9abf82b2010-05-10 21:58:38 -07002881 up_read(&mdsc->snap_rwsem);
2882 mutex_unlock(&session->s_mutex);
Sage Weil93cea5b2009-12-23 12:21:51 -08002883fail_nomsg:
2884 ceph_pagelist_release(pagelist);
Sage Weil93cea5b2009-12-23 12:21:51 -08002885fail_nopagelist:
Sage Weil9abf82b2010-05-10 21:58:38 -07002886 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
Sage Weil9abf82b2010-05-10 21:58:38 -07002887 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07002888}
2889
2890
2891/*
2892 * compare old and new mdsmaps, kicking requests
2893 * and closing out old connections as necessary
2894 *
2895 * called under mdsc->mutex.
2896 */
2897static void check_new_map(struct ceph_mds_client *mdsc,
2898 struct ceph_mdsmap *newmap,
2899 struct ceph_mdsmap *oldmap)
2900{
2901 int i;
2902 int oldstate, newstate;
2903 struct ceph_mds_session *s;
2904
2905 dout("check_new_map new %u old %u\n",
2906 newmap->m_epoch, oldmap->m_epoch);
2907
2908 for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
2909 if (mdsc->sessions[i] == NULL)
2910 continue;
2911 s = mdsc->sessions[i];
2912 oldstate = ceph_mdsmap_get_state(oldmap, i);
2913 newstate = ceph_mdsmap_get_state(newmap, i);
2914
Sage Weil0deb01c2010-06-17 14:19:01 -07002915 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002916 i, ceph_mds_state_name(oldstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07002917 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
Sage Weil2f2dc052009-10-06 11:31:09 -07002918 ceph_mds_state_name(newstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07002919 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
John Spraya687eca2014-09-19 13:51:08 +01002920 ceph_session_state_name(s->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07002921
Yan, Zheng3e8f43a2012-09-20 17:42:25 +08002922 if (i >= newmap->m_max_mds ||
2923 memcmp(ceph_mdsmap_get_addr(oldmap, i),
Sage Weil2f2dc052009-10-06 11:31:09 -07002924 ceph_mdsmap_get_addr(newmap, i),
2925 sizeof(struct ceph_entity_addr))) {
2926 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
2927 /* the session never opened, just close it
2928 * out now */
2929 __wake_requests(mdsc, &s->s_waiting);
Sage Weil2600d2d2010-02-22 15:12:16 -08002930 __unregister_session(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07002931 } else {
2932 /* just close it */
2933 mutex_unlock(&mdsc->mutex);
2934 mutex_lock(&s->s_mutex);
2935 mutex_lock(&mdsc->mutex);
2936 ceph_con_close(&s->s_con);
2937 mutex_unlock(&s->s_mutex);
2938 s->s_state = CEPH_MDS_SESSION_RESTARTING;
2939 }
2940
2941 /* kick any requests waiting on the recovering mds */
Sage Weil29790f22010-03-18 14:45:05 -07002942 kick_requests(mdsc, i);
Sage Weil2f2dc052009-10-06 11:31:09 -07002943 } else if (oldstate == newstate) {
2944 continue; /* nothing new with this mds */
2945 }
2946
2947 /*
2948 * send reconnect?
2949 */
2950 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
Sage Weil34b6c852010-05-10 16:31:25 -07002951 newstate >= CEPH_MDS_STATE_RECONNECT) {
2952 mutex_unlock(&mdsc->mutex);
2953 send_mds_reconnect(mdsc, s);
2954 mutex_lock(&mdsc->mutex);
2955 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002956
2957 /*
Sage Weil29790f22010-03-18 14:45:05 -07002958 * kick request on any mds that has gone active.
Sage Weil2f2dc052009-10-06 11:31:09 -07002959 */
2960 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
2961 newstate >= CEPH_MDS_STATE_ACTIVE) {
Sage Weil29790f22010-03-18 14:45:05 -07002962 if (oldstate != CEPH_MDS_STATE_CREATING &&
2963 oldstate != CEPH_MDS_STATE_STARTING)
2964 pr_info("mds%d recovery completed\n", s->s_mds);
2965 kick_requests(mdsc, i);
Sage Weil2f2dc052009-10-06 11:31:09 -07002966 ceph_kick_flushing_caps(mdsc, s);
Sage Weil0dc25702009-11-20 13:43:45 -08002967 wake_up_session_caps(s, 1);
Sage Weil2f2dc052009-10-06 11:31:09 -07002968 }
2969 }
Sage Weilcb170a22010-06-21 13:38:35 -07002970
2971 for (i = 0; i < newmap->m_max_mds && i < mdsc->max_sessions; i++) {
2972 s = mdsc->sessions[i];
2973 if (!s)
2974 continue;
2975 if (!ceph_mdsmap_is_laggy(newmap, i))
2976 continue;
2977 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2978 s->s_state == CEPH_MDS_SESSION_HUNG ||
2979 s->s_state == CEPH_MDS_SESSION_CLOSING) {
2980 dout(" connecting to export targets of laggy mds%d\n",
2981 i);
2982 __open_export_target_sessions(mdsc, s);
2983 }
2984 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002985}
2986
2987
2988
2989/*
2990 * leases
2991 */
2992
2993/*
2994 * caller must hold session s_mutex, dentry->d_lock
2995 */
2996void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
2997{
2998 struct ceph_dentry_info *di = ceph_dentry(dentry);
2999
3000 ceph_put_mds_session(di->lease_session);
3001 di->lease_session = NULL;
3002}
3003
Sage Weil2600d2d2010-02-22 15:12:16 -08003004static void handle_lease(struct ceph_mds_client *mdsc,
3005 struct ceph_mds_session *session,
3006 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07003007{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003008 struct super_block *sb = mdsc->fsc->sb;
Sage Weil2f2dc052009-10-06 11:31:09 -07003009 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07003010 struct dentry *parent, *dentry;
3011 struct ceph_dentry_info *di;
Sage Weil2600d2d2010-02-22 15:12:16 -08003012 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07003013 struct ceph_mds_lease *h = msg->front.iov_base;
Sage Weil1e5ea232010-06-04 10:05:40 -07003014 u32 seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003015 struct ceph_vino vino;
Sage Weil2f2dc052009-10-06 11:31:09 -07003016 struct qstr dname;
3017 int release = 0;
3018
Sage Weil2f2dc052009-10-06 11:31:09 -07003019 dout("handle_lease from mds%d\n", mds);
3020
3021 /* decode */
3022 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
3023 goto bad;
3024 vino.ino = le64_to_cpu(h->ino);
3025 vino.snap = CEPH_NOSNAP;
Sage Weil1e5ea232010-06-04 10:05:40 -07003026 seq = le32_to_cpu(h->seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003027 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
3028 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
3029 if (dname.len != get_unaligned_le32(h+1))
3030 goto bad;
3031
Sage Weil2f2dc052009-10-06 11:31:09 -07003032 /* lookup inode */
3033 inode = ceph_find_inode(sb, vino);
Sage Weil2f90b852011-07-26 11:28:25 -07003034 dout("handle_lease %s, ino %llx %p %.*s\n",
3035 ceph_lease_op_name(h->action), vino.ino, inode,
Sage Weil1e5ea232010-06-04 10:05:40 -07003036 dname.len, dname.name);
Yan, Zheng6cd3bca2014-09-17 07:45:12 +08003037
3038 mutex_lock(&session->s_mutex);
3039 session->s_seq++;
3040
Sage Weil2f2dc052009-10-06 11:31:09 -07003041 if (inode == NULL) {
3042 dout("handle_lease no inode %llx\n", vino.ino);
3043 goto release;
3044 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003045
3046 /* dentry */
3047 parent = d_find_alias(inode);
3048 if (!parent) {
3049 dout("no parent dentry on inode %p\n", inode);
3050 WARN_ON(1);
3051 goto release; /* hrm... */
3052 }
3053 dname.hash = full_name_hash(dname.name, dname.len);
3054 dentry = d_lookup(parent, &dname);
3055 dput(parent);
3056 if (!dentry)
3057 goto release;
3058
3059 spin_lock(&dentry->d_lock);
3060 di = ceph_dentry(dentry);
3061 switch (h->action) {
3062 case CEPH_MDS_LEASE_REVOKE:
Sage Weil3d8eb7a2011-11-11 09:48:53 -08003063 if (di->lease_session == session) {
Sage Weil1e5ea232010-06-04 10:05:40 -07003064 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
3065 h->seq = cpu_to_le32(di->lease_seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003066 __ceph_mdsc_drop_dentry_lease(dentry);
3067 }
3068 release = 1;
3069 break;
3070
3071 case CEPH_MDS_LEASE_RENEW:
Sage Weil3d8eb7a2011-11-11 09:48:53 -08003072 if (di->lease_session == session &&
Sage Weil2f2dc052009-10-06 11:31:09 -07003073 di->lease_gen == session->s_cap_gen &&
3074 di->lease_renew_from &&
3075 di->lease_renew_after == 0) {
3076 unsigned long duration =
3077 le32_to_cpu(h->duration_ms) * HZ / 1000;
3078
Sage Weil1e5ea232010-06-04 10:05:40 -07003079 di->lease_seq = seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003080 dentry->d_time = di->lease_renew_from + duration;
3081 di->lease_renew_after = di->lease_renew_from +
3082 (duration >> 1);
3083 di->lease_renew_from = 0;
3084 }
3085 break;
3086 }
3087 spin_unlock(&dentry->d_lock);
3088 dput(dentry);
3089
3090 if (!release)
3091 goto out;
3092
3093release:
3094 /* let's just reuse the same message */
3095 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
3096 ceph_msg_get(msg);
3097 ceph_con_send(&session->s_con, msg);
3098
3099out:
3100 iput(inode);
3101 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003102 return;
3103
3104bad:
3105 pr_err("corrupt lease message\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08003106 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003107}
3108
3109void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
3110 struct inode *inode,
3111 struct dentry *dentry, char action,
3112 u32 seq)
3113{
3114 struct ceph_msg *msg;
3115 struct ceph_mds_lease *lease;
3116 int len = sizeof(*lease) + sizeof(u32);
3117 int dnamelen = 0;
3118
3119 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
3120 inode, dentry, ceph_lease_op_name(action), session->s_mds);
3121 dnamelen = dentry->d_name.len;
3122 len += dnamelen;
3123
Sage Weilb61c2762011-08-09 15:03:46 -07003124 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07003125 if (!msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07003126 return;
3127 lease = msg->front.iov_base;
3128 lease->action = action;
Sage Weil2f2dc052009-10-06 11:31:09 -07003129 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
3130 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
3131 lease->seq = cpu_to_le32(seq);
3132 put_unaligned_le32(dnamelen, lease + 1);
3133 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
3134
3135 /*
3136 * if this is a preemptive lease RELEASE, no need to
3137 * flush request stream, since the actual request will
3138 * soon follow.
3139 */
3140 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
3141
3142 ceph_con_send(&session->s_con, msg);
3143}
3144
3145/*
3146 * Preemptively release a lease we expect to invalidate anyway.
3147 * Pass @inode always, @dentry is optional.
3148 */
3149void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
Sage Weil2f90b852011-07-26 11:28:25 -07003150 struct dentry *dentry)
Sage Weil2f2dc052009-10-06 11:31:09 -07003151{
3152 struct ceph_dentry_info *di;
3153 struct ceph_mds_session *session;
3154 u32 seq;
3155
3156 BUG_ON(inode == NULL);
3157 BUG_ON(dentry == NULL);
Sage Weil2f2dc052009-10-06 11:31:09 -07003158
3159 /* is dentry lease valid? */
3160 spin_lock(&dentry->d_lock);
3161 di = ceph_dentry(dentry);
3162 if (!di || !di->lease_session ||
3163 di->lease_session->s_mds < 0 ||
3164 di->lease_gen != di->lease_session->s_cap_gen ||
3165 !time_before(jiffies, dentry->d_time)) {
3166 dout("lease_release inode %p dentry %p -- "
Sage Weil2f90b852011-07-26 11:28:25 -07003167 "no lease\n",
3168 inode, dentry);
Sage Weil2f2dc052009-10-06 11:31:09 -07003169 spin_unlock(&dentry->d_lock);
3170 return;
3171 }
3172
3173 /* we do have a lease on this dentry; note mds and seq */
3174 session = ceph_get_mds_session(di->lease_session);
3175 seq = di->lease_seq;
3176 __ceph_mdsc_drop_dentry_lease(dentry);
3177 spin_unlock(&dentry->d_lock);
3178
Sage Weil2f90b852011-07-26 11:28:25 -07003179 dout("lease_release inode %p dentry %p to mds%d\n",
3180 inode, dentry, session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003181 ceph_mdsc_lease_send_msg(session, inode, dentry,
3182 CEPH_MDS_LEASE_RELEASE, seq);
3183 ceph_put_mds_session(session);
3184}
3185
3186/*
3187 * drop all leases (and dentry refs) in preparation for umount
3188 */
3189static void drop_leases(struct ceph_mds_client *mdsc)
3190{
3191 int i;
3192
3193 dout("drop_leases\n");
3194 mutex_lock(&mdsc->mutex);
3195 for (i = 0; i < mdsc->max_sessions; i++) {
3196 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
3197 if (!s)
3198 continue;
3199 mutex_unlock(&mdsc->mutex);
3200 mutex_lock(&s->s_mutex);
3201 mutex_unlock(&s->s_mutex);
3202 ceph_put_mds_session(s);
3203 mutex_lock(&mdsc->mutex);
3204 }
3205 mutex_unlock(&mdsc->mutex);
3206}
3207
3208
3209
3210/*
3211 * delayed work -- periodically trim expired leases, renew caps with mds
3212 */
3213static void schedule_delayed(struct ceph_mds_client *mdsc)
3214{
3215 int delay = 5;
3216 unsigned hz = round_jiffies_relative(HZ * delay);
3217 schedule_delayed_work(&mdsc->delayed_work, hz);
3218}
3219
3220static void delayed_work(struct work_struct *work)
3221{
3222 int i;
3223 struct ceph_mds_client *mdsc =
3224 container_of(work, struct ceph_mds_client, delayed_work.work);
3225 int renew_interval;
3226 int renew_caps;
3227
3228 dout("mdsc delayed_work\n");
Sage Weilafcdaea2009-10-14 14:27:38 -07003229 ceph_check_delayed_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003230
3231 mutex_lock(&mdsc->mutex);
3232 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
3233 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
3234 mdsc->last_renew_caps);
3235 if (renew_caps)
3236 mdsc->last_renew_caps = jiffies;
3237
3238 for (i = 0; i < mdsc->max_sessions; i++) {
3239 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
3240 if (s == NULL)
3241 continue;
3242 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
3243 dout("resending session close request for mds%d\n",
3244 s->s_mds);
3245 request_close_session(mdsc, s);
3246 ceph_put_mds_session(s);
3247 continue;
3248 }
3249 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
3250 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
3251 s->s_state = CEPH_MDS_SESSION_HUNG;
3252 pr_info("mds%d hung\n", s->s_mds);
3253 }
3254 }
3255 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
3256 /* this mds is failed or recovering, just wait */
3257 ceph_put_mds_session(s);
3258 continue;
3259 }
3260 mutex_unlock(&mdsc->mutex);
3261
3262 mutex_lock(&s->s_mutex);
3263 if (renew_caps)
3264 send_renew_caps(mdsc, s);
3265 else
3266 ceph_con_keepalive(&s->s_con);
Sage Weilee6b2722010-06-10 12:55:52 -07003267 ceph_add_cap_releases(mdsc, s);
Sage Weilaab53dd2010-03-17 16:30:21 -07003268 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3269 s->s_state == CEPH_MDS_SESSION_HUNG)
Sage Weil3d7ded42010-06-09 16:47:10 -07003270 ceph_send_cap_releases(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07003271 mutex_unlock(&s->s_mutex);
3272 ceph_put_mds_session(s);
3273
3274 mutex_lock(&mdsc->mutex);
3275 }
3276 mutex_unlock(&mdsc->mutex);
3277
3278 schedule_delayed(mdsc);
3279}
3280
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003281int ceph_mdsc_init(struct ceph_fs_client *fsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07003282
Sage Weil2f2dc052009-10-06 11:31:09 -07003283{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003284 struct ceph_mds_client *mdsc;
3285
3286 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
3287 if (!mdsc)
3288 return -ENOMEM;
3289 mdsc->fsc = fsc;
3290 fsc->mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07003291 mutex_init(&mdsc->mutex);
3292 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
majianpengfb3101b2013-06-25 14:48:19 +08003293 if (mdsc->mdsmap == NULL) {
3294 kfree(mdsc);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08003295 return -ENOMEM;
majianpengfb3101b2013-06-25 14:48:19 +08003296 }
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08003297
Sage Weil2f2dc052009-10-06 11:31:09 -07003298 init_completion(&mdsc->safe_umount_waiters);
Sage Weilf3c60c52010-08-11 14:51:23 -07003299 init_waitqueue_head(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003300 INIT_LIST_HEAD(&mdsc->waiting_for_map);
3301 mdsc->sessions = NULL;
3302 mdsc->max_sessions = 0;
3303 mdsc->stopping = 0;
3304 init_rwsem(&mdsc->snap_rwsem);
Sage Weila105f002010-02-15 14:37:55 -08003305 mdsc->snap_realms = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07003306 INIT_LIST_HEAD(&mdsc->snap_empty);
3307 spin_lock_init(&mdsc->snap_empty_lock);
3308 mdsc->last_tid = 0;
Sage Weil44ca18f2010-02-15 12:08:46 -08003309 mdsc->request_tree = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07003310 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
3311 mdsc->last_renew_caps = jiffies;
3312 INIT_LIST_HEAD(&mdsc->cap_delay_list);
3313 spin_lock_init(&mdsc->cap_delay_lock);
3314 INIT_LIST_HEAD(&mdsc->snap_flush_list);
3315 spin_lock_init(&mdsc->snap_flush_lock);
3316 mdsc->cap_flush_seq = 0;
3317 INIT_LIST_HEAD(&mdsc->cap_dirty);
Sage Weildb354052011-05-24 11:46:31 -07003318 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
Sage Weil2f2dc052009-10-06 11:31:09 -07003319 mdsc->num_cap_flushing = 0;
3320 spin_lock_init(&mdsc->cap_dirty_lock);
3321 init_waitqueue_head(&mdsc->cap_flushing_wq);
3322 spin_lock_init(&mdsc->dentry_lru_lock);
3323 INIT_LIST_HEAD(&mdsc->dentry_lru);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08003324
Yehuda Sadeh37151662010-06-17 16:16:12 -07003325 ceph_caps_init(mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003326 ceph_adjust_min_caps(mdsc, fsc->min_caps);
Yehuda Sadeh37151662010-06-17 16:16:12 -07003327
Sage Weil5f44f142009-11-18 14:52:18 -08003328 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003329}
3330
3331/*
3332 * Wait for safe replies on open mds requests. If we time out, drop
3333 * all requests from the tree to avoid dangling dentry refs.
3334 */
3335static void wait_requests(struct ceph_mds_client *mdsc)
3336{
3337 struct ceph_mds_request *req;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003338 struct ceph_fs_client *fsc = mdsc->fsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07003339
3340 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003341 if (__get_oldest_req(mdsc)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003342 mutex_unlock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003343
Sage Weil2f2dc052009-10-06 11:31:09 -07003344 dout("wait_requests waiting for requests\n");
3345 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003346 fsc->client->options->mount_timeout * HZ);
Sage Weil2f2dc052009-10-06 11:31:09 -07003347
3348 /* tear down remaining requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08003349 mutex_lock(&mdsc->mutex);
3350 while ((req = __get_oldest_req(mdsc))) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003351 dout("wait_requests timed out on tid %llu\n",
3352 req->r_tid);
Sage Weil44ca18f2010-02-15 12:08:46 -08003353 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07003354 }
3355 }
3356 mutex_unlock(&mdsc->mutex);
3357 dout("wait_requests done\n");
3358}
3359
3360/*
3361 * called before mount is ro, and before dentries are torn down.
3362 * (hmm, does this still race with new lookups?)
3363 */
3364void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
3365{
3366 dout("pre_umount\n");
3367 mdsc->stopping = 1;
3368
3369 drop_leases(mdsc);
Sage Weilafcdaea2009-10-14 14:27:38 -07003370 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003371 wait_requests(mdsc);
Sage Weil17c688c2010-06-21 16:12:26 -07003372
3373 /*
3374 * wait for reply handlers to drop their request refs and
3375 * their inode/dcache refs
3376 */
3377 ceph_msgr_flush();
Sage Weil2f2dc052009-10-06 11:31:09 -07003378}
3379
3380/*
3381 * wait for all write mds requests to flush.
3382 */
3383static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
3384{
Sage Weil80fc7312010-03-16 15:28:54 -07003385 struct ceph_mds_request *req = NULL, *nextreq;
Sage Weil44ca18f2010-02-15 12:08:46 -08003386 struct rb_node *n;
Sage Weil2f2dc052009-10-06 11:31:09 -07003387
3388 mutex_lock(&mdsc->mutex);
3389 dout("wait_unsafe_requests want %lld\n", want_tid);
Sage Weil80fc7312010-03-16 15:28:54 -07003390restart:
Sage Weil44ca18f2010-02-15 12:08:46 -08003391 req = __get_oldest_req(mdsc);
3392 while (req && req->r_tid <= want_tid) {
Sage Weil80fc7312010-03-16 15:28:54 -07003393 /* find next request */
3394 n = rb_next(&req->r_node);
3395 if (n)
3396 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
3397 else
3398 nextreq = NULL;
Sage Weil44ca18f2010-02-15 12:08:46 -08003399 if ((req->r_op & CEPH_MDS_OP_WRITE)) {
3400 /* write op */
3401 ceph_mdsc_get_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07003402 if (nextreq)
3403 ceph_mdsc_get_request(nextreq);
Sage Weil44ca18f2010-02-15 12:08:46 -08003404 mutex_unlock(&mdsc->mutex);
3405 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3406 req->r_tid, want_tid);
3407 wait_for_completion(&req->r_safe_completion);
3408 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003409 ceph_mdsc_put_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07003410 if (!nextreq)
3411 break; /* next dne before, so we're done! */
3412 if (RB_EMPTY_NODE(&nextreq->r_node)) {
3413 /* next request was removed from tree */
3414 ceph_mdsc_put_request(nextreq);
3415 goto restart;
3416 }
3417 ceph_mdsc_put_request(nextreq); /* won't go away */
Sage Weil44ca18f2010-02-15 12:08:46 -08003418 }
Sage Weil80fc7312010-03-16 15:28:54 -07003419 req = nextreq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003420 }
3421 mutex_unlock(&mdsc->mutex);
3422 dout("wait_unsafe_requests done\n");
3423}
3424
3425void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
3426{
3427 u64 want_tid, want_flush;
3428
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003429 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
Sage Weil56b7cf92010-05-03 15:22:00 -07003430 return;
3431
Sage Weil2f2dc052009-10-06 11:31:09 -07003432 dout("sync\n");
3433 mutex_lock(&mdsc->mutex);
3434 want_tid = mdsc->last_tid;
3435 want_flush = mdsc->cap_flush_seq;
3436 mutex_unlock(&mdsc->mutex);
3437 dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
3438
Sage Weilafcdaea2009-10-14 14:27:38 -07003439 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003440
3441 wait_unsafe_requests(mdsc, want_tid);
3442 wait_event(mdsc->cap_flushing_wq, check_cap_flush(mdsc, want_flush));
3443}
3444
Sage Weilf3c60c52010-08-11 14:51:23 -07003445/*
3446 * true if all sessions are closed, or we force unmount
3447 */
H Hartley Sweeten7fd7d102011-09-23 13:22:11 -07003448static bool done_closing_sessions(struct ceph_mds_client *mdsc)
Sage Weilf3c60c52010-08-11 14:51:23 -07003449{
3450 int i, n = 0;
3451
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003452 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
Sage Weilf3c60c52010-08-11 14:51:23 -07003453 return true;
3454
3455 mutex_lock(&mdsc->mutex);
3456 for (i = 0; i < mdsc->max_sessions; i++)
3457 if (mdsc->sessions[i])
3458 n++;
3459 mutex_unlock(&mdsc->mutex);
3460 return n == 0;
3461}
Sage Weil2f2dc052009-10-06 11:31:09 -07003462
3463/*
3464 * called after sb is ro.
3465 */
3466void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
3467{
3468 struct ceph_mds_session *session;
3469 int i;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003470 struct ceph_fs_client *fsc = mdsc->fsc;
3471 unsigned long timeout = fsc->client->options->mount_timeout * HZ;
Sage Weil2f2dc052009-10-06 11:31:09 -07003472
3473 dout("close_sessions\n");
3474
Sage Weil2f2dc052009-10-06 11:31:09 -07003475 /* close sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07003476 mutex_lock(&mdsc->mutex);
3477 for (i = 0; i < mdsc->max_sessions; i++) {
3478 session = __ceph_lookup_mds_session(mdsc, i);
3479 if (!session)
3480 continue;
Sage Weil2f2dc052009-10-06 11:31:09 -07003481 mutex_unlock(&mdsc->mutex);
Sage Weilf3c60c52010-08-11 14:51:23 -07003482 mutex_lock(&session->s_mutex);
3483 __close_session(mdsc, session);
3484 mutex_unlock(&session->s_mutex);
3485 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003486 mutex_lock(&mdsc->mutex);
3487 }
Sage Weilf3c60c52010-08-11 14:51:23 -07003488 mutex_unlock(&mdsc->mutex);
3489
3490 dout("waiting for sessions to close\n");
3491 wait_event_timeout(mdsc->session_close_wq, done_closing_sessions(mdsc),
3492 timeout);
Sage Weil2f2dc052009-10-06 11:31:09 -07003493
3494 /* tear down remaining sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07003495 mutex_lock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003496 for (i = 0; i < mdsc->max_sessions; i++) {
3497 if (mdsc->sessions[i]) {
3498 session = get_session(mdsc->sessions[i]);
Sage Weil2600d2d2010-02-22 15:12:16 -08003499 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003500 mutex_unlock(&mdsc->mutex);
3501 mutex_lock(&session->s_mutex);
3502 remove_session_caps(session);
3503 mutex_unlock(&session->s_mutex);
3504 ceph_put_mds_session(session);
3505 mutex_lock(&mdsc->mutex);
3506 }
3507 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003508 WARN_ON(!list_empty(&mdsc->cap_delay_list));
Sage Weil2f2dc052009-10-06 11:31:09 -07003509 mutex_unlock(&mdsc->mutex);
3510
3511 ceph_cleanup_empty_realms(mdsc);
3512
3513 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3514
3515 dout("stopped\n");
3516}
3517
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003518static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07003519{
3520 dout("stop\n");
3521 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3522 if (mdsc->mdsmap)
3523 ceph_mdsmap_destroy(mdsc->mdsmap);
3524 kfree(mdsc->sessions);
Yehuda Sadeh37151662010-06-17 16:16:12 -07003525 ceph_caps_finalize(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003526}
3527
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003528void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
3529{
3530 struct ceph_mds_client *mdsc = fsc->mdsc;
3531
Sage Weilef550f62011-03-25 13:27:48 -07003532 dout("mdsc_destroy %p\n", mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003533 ceph_mdsc_stop(mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07003534
3535 /* flush out any connection work with references to us */
3536 ceph_msgr_flush();
3537
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003538 fsc->mdsc = NULL;
3539 kfree(mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07003540 dout("mdsc_destroy %p done\n", mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003541}
3542
Sage Weil2f2dc052009-10-06 11:31:09 -07003543
3544/*
3545 * handle mds map update.
3546 */
3547void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3548{
3549 u32 epoch;
3550 u32 maplen;
3551 void *p = msg->front.iov_base;
3552 void *end = p + msg->front.iov_len;
3553 struct ceph_mdsmap *newmap, *oldmap;
3554 struct ceph_fsid fsid;
3555 int err = -EINVAL;
3556
3557 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
3558 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003559 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
Sage Weil07433042009-11-18 16:50:41 -08003560 return;
Sage Weilc89136e2009-10-14 09:59:09 -07003561 epoch = ceph_decode_32(&p);
3562 maplen = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07003563 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
3564
3565 /* do we need it? */
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003566 ceph_monc_got_mdsmap(&mdsc->fsc->client->monc, epoch);
Sage Weil2f2dc052009-10-06 11:31:09 -07003567 mutex_lock(&mdsc->mutex);
3568 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
3569 dout("handle_map epoch %u <= our %u\n",
3570 epoch, mdsc->mdsmap->m_epoch);
3571 mutex_unlock(&mdsc->mutex);
3572 return;
3573 }
3574
3575 newmap = ceph_mdsmap_decode(&p, end);
3576 if (IS_ERR(newmap)) {
3577 err = PTR_ERR(newmap);
3578 goto bad_unlock;
3579 }
3580
3581 /* swap into place */
3582 if (mdsc->mdsmap) {
3583 oldmap = mdsc->mdsmap;
3584 mdsc->mdsmap = newmap;
3585 check_new_map(mdsc, newmap, oldmap);
3586 ceph_mdsmap_destroy(oldmap);
3587 } else {
3588 mdsc->mdsmap = newmap; /* first mds map */
3589 }
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003590 mdsc->fsc->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
Sage Weil2f2dc052009-10-06 11:31:09 -07003591
3592 __wake_requests(mdsc, &mdsc->waiting_for_map);
3593
3594 mutex_unlock(&mdsc->mutex);
3595 schedule_delayed(mdsc);
3596 return;
3597
3598bad_unlock:
3599 mutex_unlock(&mdsc->mutex);
3600bad:
3601 pr_err("error decoding mdsmap %d\n", err);
3602 return;
3603}
3604
3605static struct ceph_connection *con_get(struct ceph_connection *con)
3606{
3607 struct ceph_mds_session *s = con->private;
3608
3609 if (get_session(s)) {
Sage Weil2600d2d2010-02-22 15:12:16 -08003610 dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -07003611 return con;
3612 }
3613 dout("mdsc con_get %p FAIL\n", s);
3614 return NULL;
3615}
3616
3617static void con_put(struct ceph_connection *con)
3618{
3619 struct ceph_mds_session *s = con->private;
3620
Henry C Chang7d8e18a2011-05-11 10:29:52 +00003621 dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref) - 1);
Sage Weil2f2dc052009-10-06 11:31:09 -07003622 ceph_put_mds_session(s);
3623}
3624
3625/*
3626 * if the client is unresponsive for long enough, the mds will kill
3627 * the session entirely.
3628 */
3629static void peer_reset(struct ceph_connection *con)
3630{
3631 struct ceph_mds_session *s = con->private;
Sage Weil7e70f0e2010-03-18 13:59:12 -07003632 struct ceph_mds_client *mdsc = s->s_mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07003633
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07003634 pr_warn("mds%d closed our session\n", s->s_mds);
Sage Weil7e70f0e2010-03-18 13:59:12 -07003635 send_mds_reconnect(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07003636}
3637
3638static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
3639{
3640 struct ceph_mds_session *s = con->private;
3641 struct ceph_mds_client *mdsc = s->s_mdsc;
3642 int type = le16_to_cpu(msg->hdr.type);
3643
Sage Weil2600d2d2010-02-22 15:12:16 -08003644 mutex_lock(&mdsc->mutex);
3645 if (__verify_registered_session(mdsc, s) < 0) {
3646 mutex_unlock(&mdsc->mutex);
3647 goto out;
3648 }
3649 mutex_unlock(&mdsc->mutex);
3650
Sage Weil2f2dc052009-10-06 11:31:09 -07003651 switch (type) {
3652 case CEPH_MSG_MDS_MAP:
3653 ceph_mdsc_handle_map(mdsc, msg);
3654 break;
3655 case CEPH_MSG_CLIENT_SESSION:
3656 handle_session(s, msg);
3657 break;
3658 case CEPH_MSG_CLIENT_REPLY:
3659 handle_reply(s, msg);
3660 break;
3661 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
Sage Weil2600d2d2010-02-22 15:12:16 -08003662 handle_forward(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003663 break;
3664 case CEPH_MSG_CLIENT_CAPS:
3665 ceph_handle_caps(s, msg);
3666 break;
3667 case CEPH_MSG_CLIENT_SNAP:
Sage Weil2600d2d2010-02-22 15:12:16 -08003668 ceph_handle_snap(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003669 break;
3670 case CEPH_MSG_CLIENT_LEASE:
Sage Weil2600d2d2010-02-22 15:12:16 -08003671 handle_lease(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003672 break;
3673
3674 default:
3675 pr_err("received unknown message type %d %s\n", type,
3676 ceph_msg_type_name(type));
3677 }
Sage Weil2600d2d2010-02-22 15:12:16 -08003678out:
Sage Weil2f2dc052009-10-06 11:31:09 -07003679 ceph_msg_put(msg);
3680}
3681
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003682/*
3683 * authentication
3684 */
Alex Eldera3530df2012-05-16 15:16:39 -05003685
3686/*
3687 * Note: returned pointer is the address of a structure that's
3688 * managed separately. Caller must *not* attempt to free it.
3689 */
3690static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05003691 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003692{
3693 struct ceph_mds_session *s = con->private;
3694 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003695 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05003696 struct ceph_auth_handshake *auth = &s->s_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003697
Alex Elder74f18692012-05-16 15:16:39 -05003698 if (force_new && auth->authorizer) {
Sage Weil27859f92013-03-25 10:26:14 -07003699 ceph_auth_destroy_authorizer(ac, auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05003700 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003701 }
Sage Weil27859f92013-03-25 10:26:14 -07003702 if (!auth->authorizer) {
3703 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
3704 auth);
Sage Weil0bed9b52013-03-25 10:26:01 -07003705 if (ret)
3706 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07003707 } else {
3708 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
3709 auth);
Alex Eldera2556512012-05-16 15:16:39 -05003710 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05003711 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003712 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003713 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05003714
Alex Eldera3530df2012-05-16 15:16:39 -05003715 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003716}
3717
3718
3719static int verify_authorizer_reply(struct ceph_connection *con, int len)
3720{
3721 struct ceph_mds_session *s = con->private;
3722 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003723 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003724
Sage Weil27859f92013-03-25 10:26:14 -07003725 return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003726}
3727
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003728static int invalidate_authorizer(struct ceph_connection *con)
3729{
3730 struct ceph_mds_session *s = con->private;
3731 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003732 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003733
Sage Weil27859f92013-03-25 10:26:14 -07003734 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003735
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003736 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003737}
3738
Alex Elder53ded492013-03-01 18:00:14 -06003739static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
3740 struct ceph_msg_header *hdr, int *skip)
3741{
3742 struct ceph_msg *msg;
3743 int type = (int) le16_to_cpu(hdr->type);
3744 int front_len = (int) le32_to_cpu(hdr->front_len);
3745
3746 if (con->in_msg)
3747 return con->in_msg;
3748
3749 *skip = 0;
3750 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
3751 if (!msg) {
3752 pr_err("unable to allocate msg type %d len %d\n",
3753 type, front_len);
3754 return NULL;
3755 }
Alex Elder53ded492013-03-01 18:00:14 -06003756
3757 return msg;
3758}
3759
Yan, Zheng33d07332014-11-04 16:33:37 +08003760static int sign_message(struct ceph_connection *con, struct ceph_msg *msg)
3761{
3762 struct ceph_mds_session *s = con->private;
3763 struct ceph_auth_handshake *auth = &s->s_auth;
3764 return ceph_auth_sign_message(auth, msg);
3765}
3766
3767static int check_message_signature(struct ceph_connection *con, struct ceph_msg *msg)
3768{
3769 struct ceph_mds_session *s = con->private;
3770 struct ceph_auth_handshake *auth = &s->s_auth;
3771 return ceph_auth_check_message_signature(auth, msg);
3772}
3773
Tobias Klauser9e327892010-05-20 10:40:19 +02003774static const struct ceph_connection_operations mds_con_ops = {
Sage Weil2f2dc052009-10-06 11:31:09 -07003775 .get = con_get,
3776 .put = con_put,
3777 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003778 .get_authorizer = get_authorizer,
3779 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003780 .invalidate_authorizer = invalidate_authorizer,
Sage Weil2f2dc052009-10-06 11:31:09 -07003781 .peer_reset = peer_reset,
Alex Elder53ded492013-03-01 18:00:14 -06003782 .alloc_msg = mds_alloc_msg,
Yan, Zheng33d07332014-11-04 16:33:37 +08003783 .sign_message = sign_message,
3784 .check_message_signature = check_message_signature,
Sage Weil2f2dc052009-10-06 11:31:09 -07003785};
3786
Sage Weil2f2dc052009-10-06 11:31:09 -07003787/* eof */