blob: 5be2d287a26c63cf5f465ba90082f1a8a32bea33 [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);
Sage Weil2f2dc052009-10-06 11:31:09 -0700461 INIT_LIST_HEAD(&s->s_cap_flushing);
462 INIT_LIST_HEAD(&s->s_cap_snaps_flushing);
463
464 dout("register_session mds%d\n", mds);
465 if (mds >= mdsc->max_sessions) {
466 int newmax = 1 << get_count_order(mds+1);
467 struct ceph_mds_session **sa;
468
469 dout("register_session realloc to %d\n", newmax);
470 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
471 if (sa == NULL)
Sage Weil42ce56e2009-11-18 11:22:36 -0800472 goto fail_realloc;
Sage Weil2f2dc052009-10-06 11:31:09 -0700473 if (mdsc->sessions) {
474 memcpy(sa, mdsc->sessions,
475 mdsc->max_sessions * sizeof(void *));
476 kfree(mdsc->sessions);
477 }
478 mdsc->sessions = sa;
479 mdsc->max_sessions = newmax;
480 }
481 mdsc->sessions[mds] = s;
Yan, Zheng86d8f672015-01-09 17:00:42 +0800482 atomic_inc(&mdsc->num_sessions);
Sage Weil2f2dc052009-10-06 11:31:09 -0700483 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);
Yan, Zheng86d8f672015-01-09 17:00:42 +0800506 atomic_dec(&mdsc->num_sessions);
Sage Weil2f2dc052009-10-06 11:31:09 -0700507}
508
509/*
510 * drop session refs in request.
511 *
512 * should be last request ref, or hold mdsc->mutex
513 */
514static void put_request_session(struct ceph_mds_request *req)
515{
516 if (req->r_session) {
517 ceph_put_mds_session(req->r_session);
518 req->r_session = NULL;
519 }
520}
521
Sage Weil153c8e62009-12-07 12:31:09 -0800522void ceph_mdsc_release_request(struct kref *kref)
Sage Weil2f2dc052009-10-06 11:31:09 -0700523{
Sage Weil153c8e62009-12-07 12:31:09 -0800524 struct ceph_mds_request *req = container_of(kref,
525 struct ceph_mds_request,
526 r_kref);
Yan, Zheng54008392014-03-29 13:41:15 +0800527 destroy_reply_info(&req->r_reply_info);
Sage Weil153c8e62009-12-07 12:31:09 -0800528 if (req->r_request)
529 ceph_msg_put(req->r_request);
Yan, Zheng54008392014-03-29 13:41:15 +0800530 if (req->r_reply)
Sage Weil153c8e62009-12-07 12:31:09 -0800531 ceph_msg_put(req->r_reply);
Sage Weil153c8e62009-12-07 12:31:09 -0800532 if (req->r_inode) {
Sage Weil41b02e12011-07-26 11:31:14 -0700533 ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
Sage Weil153c8e62009-12-07 12:31:09 -0800534 iput(req->r_inode);
535 }
536 if (req->r_locked_dir)
Sage Weil41b02e12011-07-26 11:31:14 -0700537 ceph_put_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
SF Markus Elfringe96a6502014-11-02 15:20:59 +0100538 iput(req->r_target_inode);
Sage Weil153c8e62009-12-07 12:31:09 -0800539 if (req->r_dentry)
540 dput(req->r_dentry);
Sage Weil844d87c2013-02-05 13:40:09 -0800541 if (req->r_old_dentry)
542 dput(req->r_old_dentry);
543 if (req->r_old_dentry_dir) {
Sage Weil41b02e12011-07-26 11:31:14 -0700544 /*
545 * track (and drop pins for) r_old_dentry_dir
546 * separately, since r_old_dentry's d_parent may have
547 * changed between the dir mutex being dropped and
548 * this request being freed.
549 */
550 ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir),
551 CEPH_CAP_PIN);
Sage Weil41b02e12011-07-26 11:31:14 -0700552 iput(req->r_old_dentry_dir);
Sage Weil153c8e62009-12-07 12:31:09 -0800553 }
554 kfree(req->r_path1);
555 kfree(req->r_path2);
Yan, Zheng25e6bae2014-09-16 19:15:28 +0800556 if (req->r_pagelist)
557 ceph_pagelist_release(req->r_pagelist);
Sage Weil153c8e62009-12-07 12:31:09 -0800558 put_request_session(req);
Yehuda Sadeh37151662010-06-17 16:16:12 -0700559 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
Sage Weil153c8e62009-12-07 12:31:09 -0800560 kfree(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700561}
562
563/*
564 * lookup session, bump ref if found.
565 *
566 * called under mdsc->mutex.
567 */
568static struct ceph_mds_request *__lookup_request(struct ceph_mds_client *mdsc,
569 u64 tid)
570{
571 struct ceph_mds_request *req;
Sage Weil44ca18f2010-02-15 12:08:46 -0800572 struct rb_node *n = mdsc->request_tree.rb_node;
573
574 while (n) {
575 req = rb_entry(n, struct ceph_mds_request, r_node);
576 if (tid < req->r_tid)
577 n = n->rb_left;
578 else if (tid > req->r_tid)
579 n = n->rb_right;
580 else {
581 ceph_mdsc_get_request(req);
582 return req;
583 }
584 }
585 return NULL;
586}
587
588static void __insert_request(struct ceph_mds_client *mdsc,
589 struct ceph_mds_request *new)
590{
591 struct rb_node **p = &mdsc->request_tree.rb_node;
592 struct rb_node *parent = NULL;
593 struct ceph_mds_request *req = NULL;
594
595 while (*p) {
596 parent = *p;
597 req = rb_entry(parent, struct ceph_mds_request, r_node);
598 if (new->r_tid < req->r_tid)
599 p = &(*p)->rb_left;
600 else if (new->r_tid > req->r_tid)
601 p = &(*p)->rb_right;
602 else
603 BUG();
604 }
605
606 rb_link_node(&new->r_node, parent, p);
607 rb_insert_color(&new->r_node, &mdsc->request_tree);
Sage Weil2f2dc052009-10-06 11:31:09 -0700608}
609
610/*
611 * Register an in-flight request, and assign a tid. Link to directory
612 * are modifying (if any).
613 *
614 * Called under mdsc->mutex.
615 */
616static void __register_request(struct ceph_mds_client *mdsc,
617 struct ceph_mds_request *req,
618 struct inode *dir)
619{
620 req->r_tid = ++mdsc->last_tid;
621 if (req->r_num_caps)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700622 ceph_reserve_caps(mdsc, &req->r_caps_reservation,
623 req->r_num_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -0700624 dout("__register_request %p tid %lld\n", req, req->r_tid);
625 ceph_mdsc_get_request(req);
Sage Weil44ca18f2010-02-15 12:08:46 -0800626 __insert_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700627
Sage Weilcb4276c2010-11-08 07:28:52 -0800628 req->r_uid = current_fsuid();
629 req->r_gid = current_fsgid();
630
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +0800631 if (mdsc->oldest_tid == 0 && req->r_op != CEPH_MDS_OP_SETFILELOCK)
632 mdsc->oldest_tid = req->r_tid;
633
Sage Weil2f2dc052009-10-06 11:31:09 -0700634 if (dir) {
635 struct ceph_inode_info *ci = ceph_inode(dir);
636
Sage Weil3b663782011-05-18 16:12:12 -0700637 ihold(dir);
Sage Weil2f2dc052009-10-06 11:31:09 -0700638 spin_lock(&ci->i_unsafe_lock);
639 req->r_unsafe_dir = dir;
640 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
641 spin_unlock(&ci->i_unsafe_lock);
642 }
643}
644
645static void __unregister_request(struct ceph_mds_client *mdsc,
646 struct ceph_mds_request *req)
647{
648 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +0800649
650 if (req->r_tid == mdsc->oldest_tid) {
651 struct rb_node *p = rb_next(&req->r_node);
652 mdsc->oldest_tid = 0;
653 while (p) {
654 struct ceph_mds_request *next_req =
655 rb_entry(p, struct ceph_mds_request, r_node);
656 if (next_req->r_op != CEPH_MDS_OP_SETFILELOCK) {
657 mdsc->oldest_tid = next_req->r_tid;
658 break;
659 }
660 p = rb_next(p);
661 }
662 }
663
Sage Weil44ca18f2010-02-15 12:08:46 -0800664 rb_erase(&req->r_node, &mdsc->request_tree);
Sage Weil80fc7312010-03-16 15:28:54 -0700665 RB_CLEAR_NODE(&req->r_node);
Sage Weil2f2dc052009-10-06 11:31:09 -0700666
667 if (req->r_unsafe_dir) {
668 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
669
670 spin_lock(&ci->i_unsafe_lock);
671 list_del_init(&req->r_unsafe_dir_item);
672 spin_unlock(&ci->i_unsafe_lock);
Sage Weil3b663782011-05-18 16:12:12 -0700673
674 iput(req->r_unsafe_dir);
675 req->r_unsafe_dir = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700676 }
Sage Weil94aa8ae2010-03-28 21:22:50 -0700677
Yan, Zhengfc55d2c2013-10-31 09:10:47 +0800678 complete_all(&req->r_safe_completion);
679
Sage Weil94aa8ae2010-03-28 21:22:50 -0700680 ceph_mdsc_put_request(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700681}
682
683/*
684 * Choose mds to send request to next. If there is a hint set in the
685 * request (e.g., due to a prior forward hint from the mds), use that.
686 * Otherwise, consult frag tree and/or caps to identify the
687 * appropriate mds. If all else fails, choose randomly.
688 *
689 * Called under mdsc->mutex.
690 */
H Hartley Sweeten7fd7d102011-09-23 13:22:11 -0700691static struct dentry *get_nonsnap_parent(struct dentry *dentry)
Sage Weileb6bb1c2010-08-16 09:21:27 -0700692{
Sage Weild79698d2011-07-26 11:31:26 -0700693 /*
694 * we don't need to worry about protecting the d_parent access
695 * here because we never renaming inside the snapped namespace
696 * except to resplice to another snapdir, and either the old or new
697 * result is a valid result.
698 */
David Howells2b0143b2015-03-17 22:25:59 +0000699 while (!IS_ROOT(dentry) && ceph_snap(d_inode(dentry)) != CEPH_NOSNAP)
Sage Weileb6bb1c2010-08-16 09:21:27 -0700700 dentry = dentry->d_parent;
701 return dentry;
702}
703
Sage Weil2f2dc052009-10-06 11:31:09 -0700704static int __choose_mds(struct ceph_mds_client *mdsc,
705 struct ceph_mds_request *req)
706{
707 struct inode *inode;
708 struct ceph_inode_info *ci;
709 struct ceph_cap *cap;
710 int mode = req->r_direct_mode;
711 int mds = -1;
712 u32 hash = req->r_direct_hash;
713 bool is_hash = req->r_direct_is_hash;
714
715 /*
716 * is there a specific mds we should try? ignore hint if we have
717 * no session and the mds is not up (active or recovering).
718 */
719 if (req->r_resend_mds >= 0 &&
720 (__have_session(mdsc, req->r_resend_mds) ||
721 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
722 dout("choose_mds using resend_mds mds%d\n",
723 req->r_resend_mds);
724 return req->r_resend_mds;
725 }
726
727 if (mode == USE_RANDOM_MDS)
728 goto random;
729
730 inode = NULL;
731 if (req->r_inode) {
732 inode = req->r_inode;
733 } else if (req->r_dentry) {
Sage Weild79698d2011-07-26 11:31:26 -0700734 /* ignore race with rename; old or new d_parent is okay */
735 struct dentry *parent = req->r_dentry->d_parent;
David Howells2b0143b2015-03-17 22:25:59 +0000736 struct inode *dir = d_inode(parent);
Sage Weileb6bb1c2010-08-16 09:21:27 -0700737
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700738 if (dir->i_sb != mdsc->fsc->sb) {
Sage Weileb6bb1c2010-08-16 09:21:27 -0700739 /* not this fs! */
David Howells2b0143b2015-03-17 22:25:59 +0000740 inode = d_inode(req->r_dentry);
Sage Weileb6bb1c2010-08-16 09:21:27 -0700741 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
742 /* direct snapped/virtual snapdir requests
743 * based on parent dir inode */
Sage Weild79698d2011-07-26 11:31:26 -0700744 struct dentry *dn = get_nonsnap_parent(parent);
David Howells2b0143b2015-03-17 22:25:59 +0000745 inode = d_inode(dn);
Sage Weileb6bb1c2010-08-16 09:21:27 -0700746 dout("__choose_mds using nonsnap parent %p\n", inode);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800747 } else {
Sage Weileb6bb1c2010-08-16 09:21:27 -0700748 /* dentry target */
David Howells2b0143b2015-03-17 22:25:59 +0000749 inode = d_inode(req->r_dentry);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800750 if (!inode || mode == USE_AUTH_MDS) {
751 /* dir + name */
752 inode = dir;
753 hash = ceph_dentry_hash(dir, req->r_dentry);
754 is_hash = true;
755 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700756 }
757 }
Sage Weileb6bb1c2010-08-16 09:21:27 -0700758
Sage Weil2f2dc052009-10-06 11:31:09 -0700759 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
760 (int)hash, mode);
761 if (!inode)
762 goto random;
763 ci = ceph_inode(inode);
764
765 if (is_hash && S_ISDIR(inode->i_mode)) {
766 struct ceph_inode_frag frag;
767 int found;
768
769 ceph_choose_frag(ci, hash, &frag, &found);
770 if (found) {
771 if (mode == USE_ANY_MDS && frag.ndist > 0) {
772 u8 r;
773
774 /* choose a random replica */
775 get_random_bytes(&r, 1);
776 r %= frag.ndist;
777 mds = frag.dist[r];
778 dout("choose_mds %p %llx.%llx "
779 "frag %u mds%d (%d/%d)\n",
780 inode, ceph_vinop(inode),
Sage Weild66bbd42011-01-21 21:16:46 -0800781 frag.frag, mds,
Sage Weil2f2dc052009-10-06 11:31:09 -0700782 (int)r, frag.ndist);
Sage Weild66bbd42011-01-21 21:16:46 -0800783 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
784 CEPH_MDS_STATE_ACTIVE)
785 return mds;
Sage Weil2f2dc052009-10-06 11:31:09 -0700786 }
787
788 /* since this file/dir wasn't known to be
789 * replicated, then we want to look for the
790 * authoritative mds. */
791 mode = USE_AUTH_MDS;
792 if (frag.mds >= 0) {
793 /* choose auth mds */
794 mds = frag.mds;
795 dout("choose_mds %p %llx.%llx "
796 "frag %u mds%d (auth)\n",
797 inode, ceph_vinop(inode), frag.frag, mds);
Sage Weild66bbd42011-01-21 21:16:46 -0800798 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
799 CEPH_MDS_STATE_ACTIVE)
800 return mds;
Sage Weil2f2dc052009-10-06 11:31:09 -0700801 }
802 }
803 }
804
Sage Weilbe655592011-11-30 09:47:09 -0800805 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700806 cap = NULL;
807 if (mode == USE_AUTH_MDS)
808 cap = ci->i_auth_cap;
809 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
810 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
811 if (!cap) {
Sage Weilbe655592011-11-30 09:47:09 -0800812 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700813 goto random;
814 }
815 mds = cap->session->s_mds;
816 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
817 inode, ceph_vinop(inode), mds,
818 cap == ci->i_auth_cap ? "auth " : "", cap);
Sage Weilbe655592011-11-30 09:47:09 -0800819 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700820 return mds;
821
822random:
823 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
824 dout("choose_mds chose random mds%d\n", mds);
825 return mds;
826}
827
828
829/*
830 * session messages
831 */
832static struct ceph_msg *create_session_msg(u32 op, u64 seq)
833{
834 struct ceph_msg *msg;
835 struct ceph_mds_session_head *h;
836
Sage Weilb61c2762011-08-09 15:03:46 -0700837 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
838 false);
Sage Weila79832f2010-04-01 16:06:19 -0700839 if (!msg) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700840 pr_err("create_session_msg ENOMEM creating msg\n");
Sage Weila79832f2010-04-01 16:06:19 -0700841 return NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700842 }
843 h = msg->front.iov_base;
844 h->op = cpu_to_le32(op);
845 h->seq = cpu_to_le64(seq);
John Spraydbd0c8b2014-09-09 19:26:01 +0100846
847 return msg;
848}
849
850/*
851 * session message, specialization for CEPH_SESSION_REQUEST_OPEN
852 * to include additional client metadata fields.
853 */
854static struct ceph_msg *create_session_open_msg(struct ceph_mds_client *mdsc, u64 seq)
855{
856 struct ceph_msg *msg;
857 struct ceph_mds_session_head *h;
858 int i = -1;
859 int metadata_bytes = 0;
860 int metadata_key_count = 0;
861 struct ceph_options *opt = mdsc->fsc->client->options;
862 void *p;
863
Yan, Zhenga6a5ce42015-01-16 10:54:43 +0800864 const char* metadata[][2] = {
John Spraydbd0c8b2014-09-09 19:26:01 +0100865 {"hostname", utsname()->nodename},
Yan, Zhenga6a5ce42015-01-16 10:54:43 +0800866 {"kernel_version", utsname()->release},
John Spraydbd0c8b2014-09-09 19:26:01 +0100867 {"entity_id", opt->name ? opt->name : ""},
868 {NULL, NULL}
869 };
870
871 /* Calculate serialized length of metadata */
872 metadata_bytes = 4; /* map length */
873 for (i = 0; metadata[i][0] != NULL; ++i) {
874 metadata_bytes += 8 + strlen(metadata[i][0]) +
875 strlen(metadata[i][1]);
876 metadata_key_count++;
877 }
878
879 /* Allocate the message */
880 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + metadata_bytes,
881 GFP_NOFS, false);
882 if (!msg) {
883 pr_err("create_session_msg ENOMEM creating msg\n");
884 return NULL;
885 }
886 h = msg->front.iov_base;
887 h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN);
888 h->seq = cpu_to_le64(seq);
889
890 /*
891 * Serialize client metadata into waiting buffer space, using
892 * the format that userspace expects for map<string, string>
John Spray7cfa0312014-10-30 17:15:26 +0000893 *
894 * ClientSession messages with metadata are v2
John Spraydbd0c8b2014-09-09 19:26:01 +0100895 */
John Spray7cfa0312014-10-30 17:15:26 +0000896 msg->hdr.version = cpu_to_le16(2);
897 msg->hdr.compat_version = cpu_to_le16(1);
John Spraydbd0c8b2014-09-09 19:26:01 +0100898
899 /* The write pointer, following the session_head structure */
900 p = msg->front.iov_base + sizeof(*h);
901
902 /* Number of entries in the map */
903 ceph_encode_32(&p, metadata_key_count);
904
905 /* Two length-prefixed strings for each entry in the map */
906 for (i = 0; metadata[i][0] != NULL; ++i) {
907 size_t const key_len = strlen(metadata[i][0]);
908 size_t const val_len = strlen(metadata[i][1]);
909
910 ceph_encode_32(&p, key_len);
911 memcpy(p, metadata[i][0], key_len);
912 p += key_len;
913 ceph_encode_32(&p, val_len);
914 memcpy(p, metadata[i][1], val_len);
915 p += val_len;
916 }
917
Sage Weil2f2dc052009-10-06 11:31:09 -0700918 return msg;
919}
920
921/*
922 * send session open request.
923 *
924 * called under mdsc->mutex
925 */
926static int __open_session(struct ceph_mds_client *mdsc,
927 struct ceph_mds_session *session)
928{
929 struct ceph_msg *msg;
930 int mstate;
931 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -0700932
933 /* wait for mds to go active? */
934 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
935 dout("open_session to mds%d (%s)\n", mds,
936 ceph_mds_state_name(mstate));
937 session->s_state = CEPH_MDS_SESSION_OPENING;
938 session->s_renew_requested = jiffies;
939
940 /* send connect message */
John Spraydbd0c8b2014-09-09 19:26:01 +0100941 msg = create_session_open_msg(mdsc, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -0700942 if (!msg)
943 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -0700944 ceph_con_send(&session->s_con, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -0700945 return 0;
946}
947
948/*
Sage Weiled0552a2010-06-21 13:38:25 -0700949 * open sessions for any export targets for the given mds
950 *
951 * called under mdsc->mutex
952 */
Yan, Zheng5d72d132013-11-24 14:33:01 +0800953static struct ceph_mds_session *
954__open_export_target_session(struct ceph_mds_client *mdsc, int target)
955{
956 struct ceph_mds_session *session;
957
958 session = __ceph_lookup_mds_session(mdsc, target);
959 if (!session) {
960 session = register_session(mdsc, target);
961 if (IS_ERR(session))
962 return session;
963 }
964 if (session->s_state == CEPH_MDS_SESSION_NEW ||
965 session->s_state == CEPH_MDS_SESSION_CLOSING)
966 __open_session(mdsc, session);
967
968 return session;
969}
970
971struct ceph_mds_session *
972ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target)
973{
974 struct ceph_mds_session *session;
975
976 dout("open_export_target_session to mds%d\n", target);
977
978 mutex_lock(&mdsc->mutex);
979 session = __open_export_target_session(mdsc, target);
980 mutex_unlock(&mdsc->mutex);
981
982 return session;
983}
984
Sage Weiled0552a2010-06-21 13:38:25 -0700985static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
986 struct ceph_mds_session *session)
987{
988 struct ceph_mds_info *mi;
989 struct ceph_mds_session *ts;
990 int i, mds = session->s_mds;
Sage Weiled0552a2010-06-21 13:38:25 -0700991
992 if (mds >= mdsc->mdsmap->m_max_mds)
993 return;
Yan, Zheng5d72d132013-11-24 14:33:01 +0800994
Sage Weiled0552a2010-06-21 13:38:25 -0700995 mi = &mdsc->mdsmap->m_info[mds];
996 dout("open_export_target_sessions for mds%d (%d targets)\n",
997 session->s_mds, mi->num_export_targets);
998
999 for (i = 0; i < mi->num_export_targets; i++) {
Yan, Zheng5d72d132013-11-24 14:33:01 +08001000 ts = __open_export_target_session(mdsc, mi->export_targets[i]);
1001 if (!IS_ERR(ts))
1002 ceph_put_mds_session(ts);
Sage Weiled0552a2010-06-21 13:38:25 -07001003 }
1004}
1005
Sage Weil154f42c2010-06-21 13:45:04 -07001006void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
1007 struct ceph_mds_session *session)
1008{
1009 mutex_lock(&mdsc->mutex);
1010 __open_export_target_sessions(mdsc, session);
1011 mutex_unlock(&mdsc->mutex);
1012}
1013
Sage Weiled0552a2010-06-21 13:38:25 -07001014/*
Sage Weil2f2dc052009-10-06 11:31:09 -07001015 * session caps
1016 */
1017
Yan, Zheng745a8e32015-05-14 17:22:42 +08001018/* caller holds s_cap_lock, we drop it */
1019static void cleanup_cap_releases(struct ceph_mds_client *mdsc,
1020 struct ceph_mds_session *session)
1021 __releases(session->s_cap_lock)
Sage Weil2f2dc052009-10-06 11:31:09 -07001022{
Yan, Zheng745a8e32015-05-14 17:22:42 +08001023 LIST_HEAD(tmp_list);
1024 list_splice_init(&session->s_cap_releases, &tmp_list);
1025 session->s_num_cap_releases = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001026 spin_unlock(&session->s_cap_lock);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001027
1028 dout("cleanup_cap_releases mds%d\n", session->s_mds);
1029 while (!list_empty(&tmp_list)) {
1030 struct ceph_cap *cap;
1031 /* zero out the in-progress message */
1032 cap = list_first_entry(&tmp_list,
1033 struct ceph_cap, session_caps);
1034 list_del(&cap->session_caps);
1035 ceph_put_cap(mdsc, cap);
1036 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001037}
1038
Yan, Zheng1c841a92015-03-24 20:15:36 +08001039static void cleanup_session_requests(struct ceph_mds_client *mdsc,
1040 struct ceph_mds_session *session)
1041{
1042 struct ceph_mds_request *req;
1043 struct rb_node *p;
1044
1045 dout("cleanup_session_requests mds%d\n", session->s_mds);
1046 mutex_lock(&mdsc->mutex);
1047 while (!list_empty(&session->s_unsafe)) {
1048 req = list_first_entry(&session->s_unsafe,
1049 struct ceph_mds_request, r_unsafe_item);
1050 list_del_init(&req->r_unsafe_item);
1051 pr_info(" dropping unsafe request %llu\n", req->r_tid);
1052 __unregister_request(mdsc, req);
1053 }
1054 /* zero r_attempts, so kick_requests() will re-send requests */
1055 p = rb_first(&mdsc->request_tree);
1056 while (p) {
1057 req = rb_entry(p, struct ceph_mds_request, r_node);
1058 p = rb_next(p);
1059 if (req->r_session &&
1060 req->r_session->s_mds == session->s_mds)
1061 req->r_attempts = 0;
1062 }
1063 mutex_unlock(&mdsc->mutex);
1064}
1065
Sage Weil2f2dc052009-10-06 11:31:09 -07001066/*
Sage Weilf818a732010-05-11 20:56:31 -07001067 * Helper to safely iterate over all caps associated with a session, with
1068 * special care taken to handle a racing __ceph_remove_cap().
Sage Weil2f2dc052009-10-06 11:31:09 -07001069 *
Sage Weilf818a732010-05-11 20:56:31 -07001070 * Caller must hold session s_mutex.
Sage Weil2f2dc052009-10-06 11:31:09 -07001071 */
1072static int iterate_session_caps(struct ceph_mds_session *session,
1073 int (*cb)(struct inode *, struct ceph_cap *,
1074 void *), void *arg)
1075{
Sage Weil7c1332b2010-02-16 11:39:45 -08001076 struct list_head *p;
1077 struct ceph_cap *cap;
1078 struct inode *inode, *last_inode = NULL;
1079 struct ceph_cap *old_cap = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001080 int ret;
1081
1082 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
1083 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001084 p = session->s_caps.next;
1085 while (p != &session->s_caps) {
1086 cap = list_entry(p, struct ceph_cap, session_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -07001087 inode = igrab(&cap->ci->vfs_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001088 if (!inode) {
1089 p = p->next;
Sage Weil2f2dc052009-10-06 11:31:09 -07001090 continue;
Sage Weil7c1332b2010-02-16 11:39:45 -08001091 }
1092 session->s_cap_iterator = cap;
Sage Weil2f2dc052009-10-06 11:31:09 -07001093 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001094
1095 if (last_inode) {
1096 iput(last_inode);
1097 last_inode = NULL;
1098 }
1099 if (old_cap) {
Yehuda Sadeh37151662010-06-17 16:16:12 -07001100 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -08001101 old_cap = NULL;
1102 }
1103
Sage Weil2f2dc052009-10-06 11:31:09 -07001104 ret = cb(inode, cap, arg);
Sage Weil7c1332b2010-02-16 11:39:45 -08001105 last_inode = inode;
1106
Sage Weil2f2dc052009-10-06 11:31:09 -07001107 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001108 p = p->next;
1109 if (cap->ci == NULL) {
1110 dout("iterate_session_caps finishing cap %p removal\n",
1111 cap);
1112 BUG_ON(cap->session != session);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001113 cap->session = NULL;
Sage Weil7c1332b2010-02-16 11:39:45 -08001114 list_del_init(&cap->session_caps);
1115 session->s_nr_caps--;
Yan, Zheng745a8e32015-05-14 17:22:42 +08001116 if (cap->queue_release) {
1117 list_add_tail(&cap->session_caps,
1118 &session->s_cap_releases);
1119 session->s_num_cap_releases++;
1120 } else {
1121 old_cap = cap; /* put_cap it w/o locks held */
1122 }
Sage Weil7c1332b2010-02-16 11:39:45 -08001123 }
Sage Weil5dacf092009-12-21 20:40:34 -08001124 if (ret < 0)
1125 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07001126 }
Sage Weil5dacf092009-12-21 20:40:34 -08001127 ret = 0;
1128out:
Sage Weil7c1332b2010-02-16 11:39:45 -08001129 session->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001130 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001131
SF Markus Elfringe96a6502014-11-02 15:20:59 +01001132 iput(last_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001133 if (old_cap)
Yehuda Sadeh37151662010-06-17 16:16:12 -07001134 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -08001135
Sage Weil5dacf092009-12-21 20:40:34 -08001136 return ret;
Sage Weil2f2dc052009-10-06 11:31:09 -07001137}
1138
1139static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
Sage Weil6c99f252010-05-10 16:12:25 -07001140 void *arg)
Sage Weil2f2dc052009-10-06 11:31:09 -07001141{
1142 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil6c99f252010-05-10 16:12:25 -07001143 int drop = 0;
1144
Sage Weil2f2dc052009-10-06 11:31:09 -07001145 dout("removing cap %p, ci is %p, inode is %p\n",
1146 cap, ci, &ci->vfs_inode);
Sage Weilbe655592011-11-30 09:47:09 -08001147 spin_lock(&ci->i_ceph_lock);
Yan, Zhenga096b092013-09-22 10:15:58 +08001148 __ceph_remove_cap(cap, false);
Yan, Zheng571ade32015-03-24 11:36:08 +08001149 if (!ci->i_auth_cap) {
Sage Weil6c99f252010-05-10 16:12:25 -07001150 struct ceph_mds_client *mdsc =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001151 ceph_sb_to_client(inode->i_sb)->mdsc;
Sage Weil6c99f252010-05-10 16:12:25 -07001152
1153 spin_lock(&mdsc->cap_dirty_lock);
1154 if (!list_empty(&ci->i_dirty_item)) {
1155 pr_info(" dropping dirty %s state for %p %lld\n",
1156 ceph_cap_string(ci->i_dirty_caps),
1157 inode, ceph_ino(inode));
1158 ci->i_dirty_caps = 0;
1159 list_del_init(&ci->i_dirty_item);
1160 drop = 1;
1161 }
1162 if (!list_empty(&ci->i_flushing_item)) {
1163 pr_info(" dropping dirty+flushing %s state for %p %lld\n",
1164 ceph_cap_string(ci->i_flushing_caps),
1165 inode, ceph_ino(inode));
1166 ci->i_flushing_caps = 0;
1167 list_del_init(&ci->i_flushing_item);
1168 mdsc->num_cap_flushing--;
1169 drop = 1;
1170 }
Sage Weil6c99f252010-05-10 16:12:25 -07001171 spin_unlock(&mdsc->cap_dirty_lock);
1172 }
Sage Weilbe655592011-11-30 09:47:09 -08001173 spin_unlock(&ci->i_ceph_lock);
Sage Weil6c99f252010-05-10 16:12:25 -07001174 while (drop--)
1175 iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07001176 return 0;
1177}
1178
1179/*
1180 * caller must hold session s_mutex
1181 */
1182static void remove_session_caps(struct ceph_mds_session *session)
1183{
1184 dout("remove_session_caps on %p\n", session);
1185 iterate_session_caps(session, remove_session_caps_cb, NULL);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001186
1187 spin_lock(&session->s_cap_lock);
1188 if (session->s_nr_caps > 0) {
1189 struct super_block *sb = session->s_mdsc->fsc->sb;
1190 struct inode *inode;
1191 struct ceph_cap *cap, *prev = NULL;
1192 struct ceph_vino vino;
1193 /*
1194 * iterate_session_caps() skips inodes that are being
1195 * deleted, we need to wait until deletions are complete.
1196 * __wait_on_freeing_inode() is designed for the job,
1197 * but it is not exported, so use lookup inode function
1198 * to access it.
1199 */
1200 while (!list_empty(&session->s_caps)) {
1201 cap = list_entry(session->s_caps.next,
1202 struct ceph_cap, session_caps);
1203 if (cap == prev)
1204 break;
1205 prev = cap;
1206 vino = cap->ci->i_vino;
1207 spin_unlock(&session->s_cap_lock);
1208
Yan, Zhenged284c42013-09-02 15:19:53 +08001209 inode = ceph_find_inode(sb, vino);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001210 iput(inode);
1211
1212 spin_lock(&session->s_cap_lock);
1213 }
1214 }
Yan, Zheng745a8e32015-05-14 17:22:42 +08001215
1216 // drop cap expires and unlock s_cap_lock
1217 cleanup_cap_releases(session->s_mdsc, session);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001218
Sage Weil2f2dc052009-10-06 11:31:09 -07001219 BUG_ON(session->s_nr_caps > 0);
Sage Weil6c99f252010-05-10 16:12:25 -07001220 BUG_ON(!list_empty(&session->s_cap_flushing));
Sage Weil2f2dc052009-10-06 11:31:09 -07001221}
1222
1223/*
1224 * wake up any threads waiting on this session's caps. if the cap is
1225 * old (didn't get renewed on the client reconnect), remove it now.
1226 *
1227 * caller must hold s_mutex.
1228 */
1229static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1230 void *arg)
1231{
Sage Weil0dc25702009-11-20 13:43:45 -08001232 struct ceph_inode_info *ci = ceph_inode(inode);
1233
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001234 wake_up_all(&ci->i_cap_wq);
Sage Weil0dc25702009-11-20 13:43:45 -08001235 if (arg) {
Sage Weilbe655592011-11-30 09:47:09 -08001236 spin_lock(&ci->i_ceph_lock);
Sage Weil0dc25702009-11-20 13:43:45 -08001237 ci->i_wanted_max_size = 0;
1238 ci->i_requested_max_size = 0;
Sage Weilbe655592011-11-30 09:47:09 -08001239 spin_unlock(&ci->i_ceph_lock);
Sage Weil0dc25702009-11-20 13:43:45 -08001240 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001241 return 0;
1242}
1243
Sage Weil0dc25702009-11-20 13:43:45 -08001244static void wake_up_session_caps(struct ceph_mds_session *session,
1245 int reconnect)
Sage Weil2f2dc052009-10-06 11:31:09 -07001246{
1247 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
Sage Weil0dc25702009-11-20 13:43:45 -08001248 iterate_session_caps(session, wake_up_session_cb,
1249 (void *)(unsigned long)reconnect);
Sage Weil2f2dc052009-10-06 11:31:09 -07001250}
1251
1252/*
1253 * Send periodic message to MDS renewing all currently held caps. The
1254 * ack will reset the expiration for all caps from this session.
1255 *
1256 * caller holds s_mutex
1257 */
1258static int send_renew_caps(struct ceph_mds_client *mdsc,
1259 struct ceph_mds_session *session)
1260{
1261 struct ceph_msg *msg;
1262 int state;
1263
1264 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1265 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1266 pr_info("mds%d caps stale\n", session->s_mds);
Sage Weile4cb4cb2010-03-18 13:43:09 -07001267 session->s_renew_requested = jiffies;
Sage Weil2f2dc052009-10-06 11:31:09 -07001268
1269 /* do not try to renew caps until a recovering mds has reconnected
1270 * with its clients. */
1271 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1272 if (state < CEPH_MDS_STATE_RECONNECT) {
1273 dout("send_renew_caps ignoring mds%d (%s)\n",
1274 session->s_mds, ceph_mds_state_name(state));
1275 return 0;
1276 }
1277
1278 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1279 ceph_mds_state_name(state));
Sage Weil2f2dc052009-10-06 11:31:09 -07001280 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1281 ++session->s_renew_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001282 if (!msg)
1283 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -07001284 ceph_con_send(&session->s_con, msg);
1285 return 0;
1286}
1287
Yan, Zheng186e4f72013-11-22 14:48:37 +08001288static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
1289 struct ceph_mds_session *session, u64 seq)
1290{
1291 struct ceph_msg *msg;
1292
1293 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
John Spraya687eca2014-09-19 13:51:08 +01001294 session->s_mds, ceph_session_state_name(session->s_state), seq);
Yan, Zheng186e4f72013-11-22 14:48:37 +08001295 msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
1296 if (!msg)
1297 return -ENOMEM;
1298 ceph_con_send(&session->s_con, msg);
1299 return 0;
1300}
1301
1302
Sage Weil2f2dc052009-10-06 11:31:09 -07001303/*
1304 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
Sage Weil0dc25702009-11-20 13:43:45 -08001305 *
1306 * Called under session->s_mutex
Sage Weil2f2dc052009-10-06 11:31:09 -07001307 */
1308static void renewed_caps(struct ceph_mds_client *mdsc,
1309 struct ceph_mds_session *session, int is_renew)
1310{
1311 int was_stale;
1312 int wake = 0;
1313
1314 spin_lock(&session->s_cap_lock);
Alex Elder1ce208a2012-01-12 17:48:11 -08001315 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
Sage Weil2f2dc052009-10-06 11:31:09 -07001316
1317 session->s_cap_ttl = session->s_renew_requested +
1318 mdsc->mdsmap->m_session_timeout*HZ;
1319
1320 if (was_stale) {
1321 if (time_before(jiffies, session->s_cap_ttl)) {
1322 pr_info("mds%d caps renewed\n", session->s_mds);
1323 wake = 1;
1324 } else {
1325 pr_info("mds%d caps still stale\n", session->s_mds);
1326 }
1327 }
1328 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1329 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1330 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1331 spin_unlock(&session->s_cap_lock);
1332
1333 if (wake)
Sage Weil0dc25702009-11-20 13:43:45 -08001334 wake_up_session_caps(session, 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07001335}
1336
1337/*
1338 * send a session close request
1339 */
1340static int request_close_session(struct ceph_mds_client *mdsc,
1341 struct ceph_mds_session *session)
1342{
1343 struct ceph_msg *msg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001344
1345 dout("request_close_session mds%d state %s seq %lld\n",
John Spraya687eca2014-09-19 13:51:08 +01001346 session->s_mds, ceph_session_state_name(session->s_state),
Sage Weil2f2dc052009-10-06 11:31:09 -07001347 session->s_seq);
1348 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001349 if (!msg)
1350 return -ENOMEM;
1351 ceph_con_send(&session->s_con, msg);
1352 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001353}
1354
1355/*
1356 * Called with s_mutex held.
1357 */
1358static int __close_session(struct ceph_mds_client *mdsc,
1359 struct ceph_mds_session *session)
1360{
1361 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1362 return 0;
1363 session->s_state = CEPH_MDS_SESSION_CLOSING;
1364 return request_close_session(mdsc, session);
1365}
1366
1367/*
1368 * Trim old(er) caps.
1369 *
1370 * Because we can't cache an inode without one or more caps, we do
1371 * this indirectly: if a cap is unused, we prune its aliases, at which
1372 * point the inode will hopefully get dropped to.
1373 *
1374 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1375 * memory pressure from the MDS, though, so it needn't be perfect.
1376 */
1377static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1378{
1379 struct ceph_mds_session *session = arg;
1380 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng979abfd2013-11-22 13:56:24 +08001381 int used, wanted, oissued, mine;
Sage Weil2f2dc052009-10-06 11:31:09 -07001382
1383 if (session->s_trim_caps <= 0)
1384 return -1;
1385
Sage Weilbe655592011-11-30 09:47:09 -08001386 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001387 mine = cap->issued | cap->implemented;
1388 used = __ceph_caps_used(ci);
Yan, Zheng979abfd2013-11-22 13:56:24 +08001389 wanted = __ceph_caps_file_wanted(ci);
Sage Weil2f2dc052009-10-06 11:31:09 -07001390 oissued = __ceph_caps_issued_other(ci, cap);
1391
Yan, Zheng979abfd2013-11-22 13:56:24 +08001392 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001393 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
Yan, Zheng979abfd2013-11-22 13:56:24 +08001394 ceph_cap_string(used), ceph_cap_string(wanted));
1395 if (cap == ci->i_auth_cap) {
Yan, Zheng622f3e22015-05-07 10:59:47 +08001396 if (ci->i_dirty_caps || ci->i_flushing_caps ||
1397 !list_empty(&ci->i_cap_snaps))
Yan, Zheng979abfd2013-11-22 13:56:24 +08001398 goto out;
1399 if ((used | wanted) & CEPH_CAP_ANY_WR)
1400 goto out;
1401 }
1402 if ((used | wanted) & ~oissued & mine)
Sage Weil2f2dc052009-10-06 11:31:09 -07001403 goto out; /* we need these caps */
1404
1405 session->s_trim_caps--;
1406 if (oissued) {
1407 /* we aren't the only cap.. just remove us */
Yan, Zhenga096b092013-09-22 10:15:58 +08001408 __ceph_remove_cap(cap, true);
Sage Weil2f2dc052009-10-06 11:31:09 -07001409 } else {
1410 /* try to drop referring dentries */
Sage Weilbe655592011-11-30 09:47:09 -08001411 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001412 d_prune_aliases(inode);
1413 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1414 inode, cap, atomic_read(&inode->i_count));
1415 return 0;
1416 }
1417
1418out:
Sage Weilbe655592011-11-30 09:47:09 -08001419 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001420 return 0;
1421}
1422
1423/*
1424 * Trim session cap count down to some max number.
1425 */
1426static int trim_caps(struct ceph_mds_client *mdsc,
1427 struct ceph_mds_session *session,
1428 int max_caps)
1429{
1430 int trim_caps = session->s_nr_caps - max_caps;
1431
1432 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1433 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1434 if (trim_caps > 0) {
1435 session->s_trim_caps = trim_caps;
1436 iterate_session_caps(session, trim_caps_cb, session);
1437 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1438 session->s_mds, session->s_nr_caps, max_caps,
1439 trim_caps - session->s_trim_caps);
Sage Weil5dacf092009-12-21 20:40:34 -08001440 session->s_trim_caps = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001441 }
Yan, Zhenga56371d2014-04-01 20:34:56 +08001442
Yan, Zhenga56371d2014-04-01 20:34:56 +08001443 ceph_send_cap_releases(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07001444 return 0;
1445}
1446
Yan, Zhengaffbc192015-05-05 21:22:13 +08001447static int check_cap_flush(struct ceph_inode_info *ci,
1448 u64 want_flush_seq, u64 want_snap_seq)
Yan, Zhengd3383a82015-01-08 21:30:12 +08001449{
Yan, Zhengaffbc192015-05-05 21:22:13 +08001450 int ret1 = 1, ret2 = 1;
Yan, Zhengd3383a82015-01-08 21:30:12 +08001451 spin_lock(&ci->i_ceph_lock);
Yan, Zhengaffbc192015-05-05 21:22:13 +08001452 if (want_flush_seq > 0 && ci->i_flushing_caps)
1453 ret1 = ci->i_cap_flush_seq >= want_flush_seq;
1454
1455 if (want_snap_seq > 0 && !list_empty(&ci->i_cap_snaps)) {
1456 struct ceph_cap_snap *capsnap =
1457 list_first_entry(&ci->i_cap_snaps,
1458 struct ceph_cap_snap, ci_item);
1459 ret2 = capsnap->follows >= want_snap_seq;
1460 }
Yan, Zhengd3383a82015-01-08 21:30:12 +08001461 spin_unlock(&ci->i_ceph_lock);
Yan, Zhengaffbc192015-05-05 21:22:13 +08001462 return ret1 && ret2;
Yan, Zhengd3383a82015-01-08 21:30:12 +08001463}
1464
Sage Weil2f2dc052009-10-06 11:31:09 -07001465/*
1466 * flush all dirty inode data to disk.
1467 *
1468 * returns true if we've flushed through want_flush_seq
1469 */
Yan, Zhengaffbc192015-05-05 21:22:13 +08001470static void wait_caps_flush(struct ceph_mds_client *mdsc,
1471 u64 want_flush_seq, u64 want_snap_seq)
Sage Weil2f2dc052009-10-06 11:31:09 -07001472{
Yan, Zhengd3383a82015-01-08 21:30:12 +08001473 int mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07001474
1475 dout("check_cap_flush want %lld\n", want_flush_seq);
1476 mutex_lock(&mdsc->mutex);
Yan, Zhengaffbc192015-05-05 21:22:13 +08001477 for (mds = 0; mds < mdsc->max_sessions; ) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001478 struct ceph_mds_session *session = mdsc->sessions[mds];
Yan, Zhengaffbc192015-05-05 21:22:13 +08001479 struct inode *inode1 = NULL, *inode2 = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001480
Yan, Zhengaffbc192015-05-05 21:22:13 +08001481 if (!session) {
1482 mds++;
Sage Weil2f2dc052009-10-06 11:31:09 -07001483 continue;
Yan, Zhengaffbc192015-05-05 21:22:13 +08001484 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001485 get_session(session);
1486 mutex_unlock(&mdsc->mutex);
1487
1488 mutex_lock(&session->s_mutex);
1489 if (!list_empty(&session->s_cap_flushing)) {
1490 struct ceph_inode_info *ci =
Yan, Zhengaffbc192015-05-05 21:22:13 +08001491 list_first_entry(&session->s_cap_flushing,
1492 struct ceph_inode_info,
1493 i_flushing_item);
Sage Weil2f2dc052009-10-06 11:31:09 -07001494
Yan, Zhengaffbc192015-05-05 21:22:13 +08001495 if (!check_cap_flush(ci, want_flush_seq, 0)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001496 dout("check_cap_flush still flushing %p "
Yan, Zhengd3383a82015-01-08 21:30:12 +08001497 "seq %lld <= %lld to mds%d\n",
1498 &ci->vfs_inode, ci->i_cap_flush_seq,
Yan, Zhengaffbc192015-05-05 21:22:13 +08001499 want_flush_seq, mds);
1500 inode1 = igrab(&ci->vfs_inode);
1501 }
1502 }
1503 if (!list_empty(&session->s_cap_snaps_flushing)) {
1504 struct ceph_cap_snap *capsnap =
1505 list_first_entry(&session->s_cap_snaps_flushing,
1506 struct ceph_cap_snap,
1507 flushing_item);
1508 struct ceph_inode_info *ci = capsnap->ci;
1509 if (!check_cap_flush(ci, 0, want_snap_seq)) {
1510 dout("check_cap_flush still flushing snap %p "
1511 "follows %lld <= %lld to mds%d\n",
1512 &ci->vfs_inode, capsnap->follows,
1513 want_snap_seq, mds);
1514 inode2 = igrab(&ci->vfs_inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07001515 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001516 }
1517 mutex_unlock(&session->s_mutex);
1518 ceph_put_mds_session(session);
1519
Yan, Zhengaffbc192015-05-05 21:22:13 +08001520 if (inode1) {
Yan, Zhengd3383a82015-01-08 21:30:12 +08001521 wait_event(mdsc->cap_flushing_wq,
Yan, Zhengaffbc192015-05-05 21:22:13 +08001522 check_cap_flush(ceph_inode(inode1),
1523 want_flush_seq, 0));
1524 iput(inode1);
Yan, Zhengd3383a82015-01-08 21:30:12 +08001525 }
Yan, Zhengaffbc192015-05-05 21:22:13 +08001526 if (inode2) {
1527 wait_event(mdsc->cap_flushing_wq,
1528 check_cap_flush(ceph_inode(inode2),
1529 0, want_snap_seq));
1530 iput(inode2);
1531 }
1532
1533 if (!inode1 && !inode2)
1534 mds++;
Yan, Zhengd3383a82015-01-08 21:30:12 +08001535
Sage Weil2f2dc052009-10-06 11:31:09 -07001536 mutex_lock(&mdsc->mutex);
1537 }
1538
1539 mutex_unlock(&mdsc->mutex);
1540 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07001541}
1542
1543/*
1544 * called under s_mutex
1545 */
Sage Weil3d7ded42010-06-09 16:47:10 -07001546void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1547 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001548{
Yan, Zheng745a8e32015-05-14 17:22:42 +08001549 struct ceph_msg *msg = NULL;
1550 struct ceph_mds_cap_release *head;
1551 struct ceph_mds_cap_item *item;
1552 struct ceph_cap *cap;
1553 LIST_HEAD(tmp_list);
1554 int num_cap_releases;
Sage Weil2f2dc052009-10-06 11:31:09 -07001555
Sage Weil0f8605f2010-05-05 15:51:35 -07001556 spin_lock(&session->s_cap_lock);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001557again:
1558 list_splice_init(&session->s_cap_releases, &tmp_list);
1559 num_cap_releases = session->s_num_cap_releases;
1560 session->s_num_cap_releases = 0;
1561 spin_unlock(&session->s_cap_lock);
1562
1563 while (!list_empty(&tmp_list)) {
1564 if (!msg) {
1565 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE,
1566 PAGE_CACHE_SIZE, GFP_NOFS, false);
1567 if (!msg)
1568 goto out_err;
1569 head = msg->front.iov_base;
1570 head->num = cpu_to_le32(0);
1571 msg->front.iov_len = sizeof(*head);
1572 }
1573 cap = list_first_entry(&tmp_list, struct ceph_cap,
1574 session_caps);
1575 list_del(&cap->session_caps);
1576 num_cap_releases--;
1577
1578 head = msg->front.iov_base;
1579 le32_add_cpu(&head->num, 1);
1580 item = msg->front.iov_base + msg->front.iov_len;
1581 item->ino = cpu_to_le64(cap->cap_ino);
1582 item->cap_id = cpu_to_le64(cap->cap_id);
1583 item->migrate_seq = cpu_to_le32(cap->mseq);
1584 item->seq = cpu_to_le32(cap->issue_seq);
1585 msg->front.iov_len += sizeof(*item);
1586
1587 ceph_put_cap(mdsc, cap);
1588
1589 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
1590 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1591 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1592 ceph_con_send(&session->s_con, msg);
1593 msg = NULL;
1594 }
1595 }
1596
1597 BUG_ON(num_cap_releases != 0);
1598
1599 spin_lock(&session->s_cap_lock);
1600 if (!list_empty(&session->s_cap_releases))
1601 goto again;
1602 spin_unlock(&session->s_cap_lock);
1603
1604 if (msg) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001605 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1606 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1607 ceph_con_send(&session->s_con, msg);
1608 }
Yan, Zheng745a8e32015-05-14 17:22:42 +08001609 return;
1610out_err:
1611 pr_err("send_cap_releases mds%d, failed to allocate message\n",
1612 session->s_mds);
1613 spin_lock(&session->s_cap_lock);
1614 list_splice(&tmp_list, &session->s_cap_releases);
1615 session->s_num_cap_releases += num_cap_releases;
Sage Weil2f2dc052009-10-06 11:31:09 -07001616 spin_unlock(&session->s_cap_lock);
1617}
1618
1619/*
1620 * requests
1621 */
1622
Yan, Zheng54008392014-03-29 13:41:15 +08001623int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
1624 struct inode *dir)
1625{
1626 struct ceph_inode_info *ci = ceph_inode(dir);
1627 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1628 struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options;
1629 size_t size = sizeof(*rinfo->dir_in) + sizeof(*rinfo->dir_dname_len) +
1630 sizeof(*rinfo->dir_dname) + sizeof(*rinfo->dir_dlease);
1631 int order, num_entries;
1632
1633 spin_lock(&ci->i_ceph_lock);
1634 num_entries = ci->i_files + ci->i_subdirs;
1635 spin_unlock(&ci->i_ceph_lock);
1636 num_entries = max(num_entries, 1);
1637 num_entries = min(num_entries, opt->max_readdir);
1638
1639 order = get_order(size * num_entries);
1640 while (order >= 0) {
1641 rinfo->dir_in = (void*)__get_free_pages(GFP_NOFS | __GFP_NOWARN,
1642 order);
1643 if (rinfo->dir_in)
1644 break;
1645 order--;
1646 }
1647 if (!rinfo->dir_in)
1648 return -ENOMEM;
1649
1650 num_entries = (PAGE_SIZE << order) / size;
1651 num_entries = min(num_entries, opt->max_readdir);
1652
1653 rinfo->dir_buf_size = PAGE_SIZE << order;
1654 req->r_num_caps = num_entries + 1;
1655 req->r_args.readdir.max_entries = cpu_to_le32(num_entries);
1656 req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes);
1657 return 0;
1658}
1659
Sage Weil2f2dc052009-10-06 11:31:09 -07001660/*
1661 * Create an mds request.
1662 */
1663struct ceph_mds_request *
1664ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1665{
1666 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1667
1668 if (!req)
1669 return ERR_PTR(-ENOMEM);
1670
Sage Weilb4556392010-05-13 12:01:13 -07001671 mutex_init(&req->r_fill_mutex);
Yehuda Sadeh37151662010-06-17 16:16:12 -07001672 req->r_mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07001673 req->r_started = jiffies;
1674 req->r_resend_mds = -1;
1675 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1676 req->r_fmode = -1;
Sage Weil153c8e62009-12-07 12:31:09 -08001677 kref_init(&req->r_kref);
Sage Weil2f2dc052009-10-06 11:31:09 -07001678 INIT_LIST_HEAD(&req->r_wait);
1679 init_completion(&req->r_completion);
1680 init_completion(&req->r_safe_completion);
1681 INIT_LIST_HEAD(&req->r_unsafe_item);
1682
Sage Weilb8e69062014-05-21 17:41:08 -07001683 req->r_stamp = CURRENT_TIME;
1684
Sage Weil2f2dc052009-10-06 11:31:09 -07001685 req->r_op = op;
1686 req->r_direct_mode = mode;
1687 return req;
1688}
1689
1690/*
Sage Weil44ca18f2010-02-15 12:08:46 -08001691 * return oldest (lowest) request, tid in request tree, 0 if none.
Sage Weil2f2dc052009-10-06 11:31:09 -07001692 *
1693 * called under mdsc->mutex.
1694 */
Sage Weil44ca18f2010-02-15 12:08:46 -08001695static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1696{
1697 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1698 return NULL;
1699 return rb_entry(rb_first(&mdsc->request_tree),
1700 struct ceph_mds_request, r_node);
1701}
1702
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08001703static inline u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07001704{
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08001705 return mdsc->oldest_tid;
Sage Weil2f2dc052009-10-06 11:31:09 -07001706}
1707
1708/*
1709 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1710 * on build_path_from_dentry in fs/cifs/dir.c.
1711 *
1712 * If @stop_on_nosnap, generate path relative to the first non-snapped
1713 * inode.
1714 *
1715 * Encode hidden .snap dirs as a double /, i.e.
1716 * foo/.snap/bar -> foo//bar
1717 */
1718char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1719 int stop_on_nosnap)
1720{
1721 struct dentry *temp;
1722 char *path;
1723 int len, pos;
Al Viro1b71fe22011-07-16 23:43:58 -04001724 unsigned seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07001725
1726 if (dentry == NULL)
1727 return ERR_PTR(-EINVAL);
1728
1729retry:
1730 len = 0;
Al Viro1b71fe22011-07-16 23:43:58 -04001731 seq = read_seqbegin(&rename_lock);
1732 rcu_read_lock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001733 for (temp = dentry; !IS_ROOT(temp);) {
David Howells2b0143b2015-03-17 22:25:59 +00001734 struct inode *inode = d_inode(temp);
Sage Weil2f2dc052009-10-06 11:31:09 -07001735 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1736 len++; /* slash only */
1737 else if (stop_on_nosnap && inode &&
1738 ceph_snap(inode) == CEPH_NOSNAP)
1739 break;
1740 else
1741 len += 1 + temp->d_name.len;
1742 temp = temp->d_parent;
Sage Weil2f2dc052009-10-06 11:31:09 -07001743 }
Al Viro1b71fe22011-07-16 23:43:58 -04001744 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001745 if (len)
1746 len--; /* no leading '/' */
1747
1748 path = kmalloc(len+1, GFP_NOFS);
1749 if (path == NULL)
1750 return ERR_PTR(-ENOMEM);
1751 pos = len;
1752 path[pos] = 0; /* trailing null */
Al Viro1b71fe22011-07-16 23:43:58 -04001753 rcu_read_lock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001754 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
Al Viro1b71fe22011-07-16 23:43:58 -04001755 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07001756
Al Viro1b71fe22011-07-16 23:43:58 -04001757 spin_lock(&temp->d_lock);
David Howells2b0143b2015-03-17 22:25:59 +00001758 inode = d_inode(temp);
Sage Weil2f2dc052009-10-06 11:31:09 -07001759 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
Sage Weil104648a2010-03-18 10:14:30 -07001760 dout("build_path path+%d: %p SNAPDIR\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001761 pos, temp);
1762 } else if (stop_on_nosnap && inode &&
1763 ceph_snap(inode) == CEPH_NOSNAP) {
Yehuda Sadeh9d5a09e2011-12-13 09:57:44 -08001764 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001765 break;
1766 } else {
1767 pos -= temp->d_name.len;
Al Viro1b71fe22011-07-16 23:43:58 -04001768 if (pos < 0) {
1769 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001770 break;
Al Viro1b71fe22011-07-16 23:43:58 -04001771 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001772 strncpy(path + pos, temp->d_name.name,
1773 temp->d_name.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07001774 }
Al Viro1b71fe22011-07-16 23:43:58 -04001775 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001776 if (pos)
1777 path[--pos] = '/';
1778 temp = temp->d_parent;
Sage Weil2f2dc052009-10-06 11:31:09 -07001779 }
Al Viro1b71fe22011-07-16 23:43:58 -04001780 rcu_read_unlock();
1781 if (pos != 0 || read_seqretry(&rename_lock, seq)) {
Sage Weil104648a2010-03-18 10:14:30 -07001782 pr_err("build_path did not end path lookup where "
Sage Weil2f2dc052009-10-06 11:31:09 -07001783 "expected, namelen is %d, pos is %d\n", len, pos);
1784 /* presumably this is only possible if racing with a
1785 rename of one of the parent directories (we can not
1786 lock the dentries above us to prevent this, but
1787 retrying should be harmless) */
1788 kfree(path);
1789 goto retry;
1790 }
1791
David Howells2b0143b2015-03-17 22:25:59 +00001792 *base = ceph_ino(d_inode(temp));
Sage Weil2f2dc052009-10-06 11:31:09 -07001793 *plen = len;
Sage Weil104648a2010-03-18 10:14:30 -07001794 dout("build_path on %p %d built %llx '%.*s'\n",
Al Viro84d08fa2013-07-05 18:59:33 +04001795 dentry, d_count(dentry), *base, len, path);
Sage Weil2f2dc052009-10-06 11:31:09 -07001796 return path;
1797}
1798
1799static int build_dentry_path(struct dentry *dentry,
1800 const char **ppath, int *ppathlen, u64 *pino,
1801 int *pfreepath)
1802{
1803 char *path;
1804
David Howells2b0143b2015-03-17 22:25:59 +00001805 if (ceph_snap(d_inode(dentry->d_parent)) == CEPH_NOSNAP) {
1806 *pino = ceph_ino(d_inode(dentry->d_parent));
Sage Weil2f2dc052009-10-06 11:31:09 -07001807 *ppath = dentry->d_name.name;
1808 *ppathlen = dentry->d_name.len;
1809 return 0;
1810 }
1811 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1812 if (IS_ERR(path))
1813 return PTR_ERR(path);
1814 *ppath = path;
1815 *pfreepath = 1;
1816 return 0;
1817}
1818
1819static int build_inode_path(struct inode *inode,
1820 const char **ppath, int *ppathlen, u64 *pino,
1821 int *pfreepath)
1822{
1823 struct dentry *dentry;
1824 char *path;
1825
1826 if (ceph_snap(inode) == CEPH_NOSNAP) {
1827 *pino = ceph_ino(inode);
1828 *ppathlen = 0;
1829 return 0;
1830 }
1831 dentry = d_find_alias(inode);
1832 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1833 dput(dentry);
1834 if (IS_ERR(path))
1835 return PTR_ERR(path);
1836 *ppath = path;
1837 *pfreepath = 1;
1838 return 0;
1839}
1840
1841/*
1842 * request arguments may be specified via an inode *, a dentry *, or
1843 * an explicit ino+path.
1844 */
1845static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1846 const char *rpath, u64 rino,
1847 const char **ppath, int *pathlen,
1848 u64 *ino, int *freepath)
1849{
1850 int r = 0;
1851
1852 if (rinode) {
1853 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1854 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1855 ceph_snap(rinode));
1856 } else if (rdentry) {
1857 r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
1858 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1859 *ppath);
Sage Weil795858d2011-08-15 13:02:37 -07001860 } else if (rpath || rino) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001861 *ino = rino;
1862 *ppath = rpath;
David Zafmanb0000562012-10-25 10:23:46 -07001863 *pathlen = rpath ? strlen(rpath) : 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001864 dout(" path %.*s\n", *pathlen, rpath);
1865 }
1866
1867 return r;
1868}
1869
1870/*
1871 * called under mdsc->mutex
1872 */
1873static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1874 struct ceph_mds_request *req,
Yan, Zheng6e6f0922015-02-27 08:54:08 +08001875 int mds, bool drop_cap_releases)
Sage Weil2f2dc052009-10-06 11:31:09 -07001876{
1877 struct ceph_msg *msg;
1878 struct ceph_mds_request_head *head;
1879 const char *path1 = NULL;
1880 const char *path2 = NULL;
1881 u64 ino1 = 0, ino2 = 0;
1882 int pathlen1 = 0, pathlen2 = 0;
1883 int freepath1 = 0, freepath2 = 0;
1884 int len;
1885 u16 releases;
1886 void *p, *end;
1887 int ret;
1888
1889 ret = set_request_path_attr(req->r_inode, req->r_dentry,
1890 req->r_path1, req->r_ino1.ino,
1891 &path1, &pathlen1, &ino1, &freepath1);
1892 if (ret < 0) {
1893 msg = ERR_PTR(ret);
1894 goto out;
1895 }
1896
1897 ret = set_request_path_attr(NULL, req->r_old_dentry,
1898 req->r_path2, req->r_ino2.ino,
1899 &path2, &pathlen2, &ino2, &freepath2);
1900 if (ret < 0) {
1901 msg = ERR_PTR(ret);
1902 goto out_free1;
1903 }
1904
1905 len = sizeof(*head) +
Sage Weilb8e69062014-05-21 17:41:08 -07001906 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) +
1907 sizeof(struct timespec);
Sage Weil2f2dc052009-10-06 11:31:09 -07001908
1909 /* calculate (max) length for cap releases */
1910 len += sizeof(struct ceph_mds_request_release) *
1911 (!!req->r_inode_drop + !!req->r_dentry_drop +
1912 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1913 if (req->r_dentry_drop)
1914 len += req->r_dentry->d_name.len;
1915 if (req->r_old_dentry_drop)
1916 len += req->r_old_dentry->d_name.len;
1917
Sage Weilb61c2762011-08-09 15:03:46 -07001918 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07001919 if (!msg) {
1920 msg = ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -07001921 goto out_free2;
Sage Weila79832f2010-04-01 16:06:19 -07001922 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001923
John Spray7cfa0312014-10-30 17:15:26 +00001924 msg->hdr.version = cpu_to_le16(2);
Sage Weil6df058c2009-12-22 11:24:33 -08001925 msg->hdr.tid = cpu_to_le64(req->r_tid);
1926
Sage Weil2f2dc052009-10-06 11:31:09 -07001927 head = msg->front.iov_base;
1928 p = msg->front.iov_base + sizeof(*head);
1929 end = msg->front.iov_base + msg->front.iov_len;
1930
1931 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1932 head->op = cpu_to_le32(req->r_op);
Eric W. Biedermanff3d0042013-01-31 04:01:53 -08001933 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid));
1934 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid));
Sage Weil2f2dc052009-10-06 11:31:09 -07001935 head->args = req->r_args;
1936
1937 ceph_encode_filepath(&p, end, ino1, path1);
1938 ceph_encode_filepath(&p, end, ino2, path2);
1939
Sage Weile979cf52010-07-15 14:58:39 -07001940 /* make note of release offset, in case we need to replay */
1941 req->r_request_release_offset = p - msg->front.iov_base;
1942
Sage Weil2f2dc052009-10-06 11:31:09 -07001943 /* cap releases */
1944 releases = 0;
1945 if (req->r_inode_drop)
1946 releases += ceph_encode_inode_release(&p,
David Howells2b0143b2015-03-17 22:25:59 +00001947 req->r_inode ? req->r_inode : d_inode(req->r_dentry),
Sage Weil2f2dc052009-10-06 11:31:09 -07001948 mds, req->r_inode_drop, req->r_inode_unless, 0);
1949 if (req->r_dentry_drop)
1950 releases += ceph_encode_dentry_release(&p, req->r_dentry,
1951 mds, req->r_dentry_drop, req->r_dentry_unless);
1952 if (req->r_old_dentry_drop)
1953 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
1954 mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
1955 if (req->r_old_inode_drop)
1956 releases += ceph_encode_inode_release(&p,
David Howells2b0143b2015-03-17 22:25:59 +00001957 d_inode(req->r_old_dentry),
Sage Weil2f2dc052009-10-06 11:31:09 -07001958 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
Yan, Zheng6e6f0922015-02-27 08:54:08 +08001959
1960 if (drop_cap_releases) {
1961 releases = 0;
1962 p = msg->front.iov_base + req->r_request_release_offset;
1963 }
1964
Sage Weil2f2dc052009-10-06 11:31:09 -07001965 head->num_releases = cpu_to_le16(releases);
1966
Sage Weilb8e69062014-05-21 17:41:08 -07001967 /* time stamp */
Yan, Zheng1f041a82015-01-13 15:20:52 +08001968 {
1969 struct ceph_timespec ts;
1970 ceph_encode_timespec(&ts, &req->r_stamp);
1971 ceph_encode_copy(&p, &ts, sizeof(ts));
1972 }
Sage Weilb8e69062014-05-21 17:41:08 -07001973
Sage Weil2f2dc052009-10-06 11:31:09 -07001974 BUG_ON(p > end);
1975 msg->front.iov_len = p - msg->front.iov_base;
1976 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1977
Yan, Zheng25e6bae2014-09-16 19:15:28 +08001978 if (req->r_pagelist) {
1979 struct ceph_pagelist *pagelist = req->r_pagelist;
1980 atomic_inc(&pagelist->refcnt);
1981 ceph_msg_data_add_pagelist(msg, pagelist);
1982 msg->hdr.data_len = cpu_to_le32(pagelist->length);
1983 } else {
1984 msg->hdr.data_len = 0;
Alex Elderebf18f42013-03-04 22:29:57 -06001985 }
Alex Elder02afca62013-02-14 12:16:43 -06001986
Sage Weil2f2dc052009-10-06 11:31:09 -07001987 msg->hdr.data_off = cpu_to_le16(0);
1988
1989out_free2:
1990 if (freepath2)
1991 kfree((char *)path2);
1992out_free1:
1993 if (freepath1)
1994 kfree((char *)path1);
1995out:
1996 return msg;
1997}
1998
1999/*
2000 * called under mdsc->mutex if error, under no mutex if
2001 * success.
2002 */
2003static void complete_request(struct ceph_mds_client *mdsc,
2004 struct ceph_mds_request *req)
2005{
2006 if (req->r_callback)
2007 req->r_callback(mdsc, req);
2008 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002009 complete_all(&req->r_completion);
Sage Weil2f2dc052009-10-06 11:31:09 -07002010}
2011
2012/*
2013 * called under mdsc->mutex
2014 */
2015static int __prepare_send_request(struct ceph_mds_client *mdsc,
2016 struct ceph_mds_request *req,
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002017 int mds, bool drop_cap_releases)
Sage Weil2f2dc052009-10-06 11:31:09 -07002018{
2019 struct ceph_mds_request_head *rhead;
2020 struct ceph_msg *msg;
2021 int flags = 0;
2022
Sage Weil2f2dc052009-10-06 11:31:09 -07002023 req->r_attempts++;
Greg Farnume55b71f2010-06-22 15:58:01 -07002024 if (req->r_inode) {
2025 struct ceph_cap *cap =
2026 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
2027
2028 if (cap)
2029 req->r_sent_on_mseq = cap->mseq;
2030 else
2031 req->r_sent_on_mseq = -1;
2032 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002033 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
2034 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
2035
Sage Weil01a92f12010-07-15 13:24:32 -07002036 if (req->r_got_unsafe) {
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002037 void *p;
Sage Weil01a92f12010-07-15 13:24:32 -07002038 /*
2039 * Replay. Do not regenerate message (and rebuild
2040 * paths, etc.); just use the original message.
2041 * Rebuilding paths will break for renames because
2042 * d_move mangles the src name.
2043 */
2044 msg = req->r_request;
2045 rhead = msg->front.iov_base;
2046
2047 flags = le32_to_cpu(rhead->flags);
2048 flags |= CEPH_MDS_FLAG_REPLAY;
2049 rhead->flags = cpu_to_le32(flags);
2050
2051 if (req->r_target_inode)
2052 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
2053
2054 rhead->num_retry = req->r_attempts - 1;
Sage Weile979cf52010-07-15 14:58:39 -07002055
2056 /* remove cap/dentry releases from message */
2057 rhead->num_releases = 0;
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002058
2059 /* time stamp */
2060 p = msg->front.iov_base + req->r_request_release_offset;
Yan, Zheng1f041a82015-01-13 15:20:52 +08002061 {
2062 struct ceph_timespec ts;
2063 ceph_encode_timespec(&ts, &req->r_stamp);
2064 ceph_encode_copy(&p, &ts, sizeof(ts));
2065 }
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002066
2067 msg->front.iov_len = p - msg->front.iov_base;
2068 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
Sage Weil01a92f12010-07-15 13:24:32 -07002069 return 0;
2070 }
2071
Sage Weil2f2dc052009-10-06 11:31:09 -07002072 if (req->r_request) {
2073 ceph_msg_put(req->r_request);
2074 req->r_request = NULL;
2075 }
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002076 msg = create_request_message(mdsc, req, mds, drop_cap_releases);
Sage Weil2f2dc052009-10-06 11:31:09 -07002077 if (IS_ERR(msg)) {
Sage Weile1518c72010-05-13 11:19:06 -07002078 req->r_err = PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002079 complete_request(mdsc, req);
Sage Weila79832f2010-04-01 16:06:19 -07002080 return PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002081 }
2082 req->r_request = msg;
2083
2084 rhead = msg->front.iov_base;
Sage Weil2f2dc052009-10-06 11:31:09 -07002085 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
2086 if (req->r_got_unsafe)
2087 flags |= CEPH_MDS_FLAG_REPLAY;
2088 if (req->r_locked_dir)
2089 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
2090 rhead->flags = cpu_to_le32(flags);
2091 rhead->num_fwd = req->r_num_fwd;
2092 rhead->num_retry = req->r_attempts - 1;
Sage Weil01a92f12010-07-15 13:24:32 -07002093 rhead->ino = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002094
2095 dout(" r_locked_dir = %p\n", req->r_locked_dir);
Sage Weil2f2dc052009-10-06 11:31:09 -07002096 return 0;
2097}
2098
2099/*
2100 * send request, or put it on the appropriate wait list.
2101 */
2102static int __do_request(struct ceph_mds_client *mdsc,
2103 struct ceph_mds_request *req)
2104{
2105 struct ceph_mds_session *session = NULL;
2106 int mds = -1;
2107 int err = -EAGAIN;
2108
Yan, Zhengeb1b8af2013-09-26 14:25:36 +08002109 if (req->r_err || req->r_got_result) {
2110 if (req->r_aborted)
2111 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002112 goto out;
Yan, Zhengeb1b8af2013-09-26 14:25:36 +08002113 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002114
2115 if (req->r_timeout &&
2116 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
2117 dout("do_request timed out\n");
2118 err = -EIO;
2119 goto finish;
2120 }
2121
Sage Weildc69e2e2010-11-02 13:49:00 -07002122 put_request_session(req);
2123
Sage Weil2f2dc052009-10-06 11:31:09 -07002124 mds = __choose_mds(mdsc, req);
2125 if (mds < 0 ||
2126 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
2127 dout("do_request no mds or not active, waiting for map\n");
2128 list_add(&req->r_wait, &mdsc->waiting_for_map);
2129 goto out;
2130 }
2131
2132 /* get, open session */
2133 session = __ceph_lookup_mds_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07002134 if (!session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002135 session = register_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07002136 if (IS_ERR(session)) {
2137 err = PTR_ERR(session);
2138 goto finish;
2139 }
2140 }
Sage Weildc69e2e2010-11-02 13:49:00 -07002141 req->r_session = get_session(session);
2142
Sage Weil2f2dc052009-10-06 11:31:09 -07002143 dout("do_request mds%d session %p state %s\n", mds, session,
John Spraya687eca2014-09-19 13:51:08 +01002144 ceph_session_state_name(session->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07002145 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
2146 session->s_state != CEPH_MDS_SESSION_HUNG) {
2147 if (session->s_state == CEPH_MDS_SESSION_NEW ||
2148 session->s_state == CEPH_MDS_SESSION_CLOSING)
2149 __open_session(mdsc, session);
2150 list_add(&req->r_wait, &session->s_waiting);
2151 goto out_session;
2152 }
2153
2154 /* send request */
Sage Weil2f2dc052009-10-06 11:31:09 -07002155 req->r_resend_mds = -1; /* forget any previous mds hint */
2156
2157 if (req->r_request_started == 0) /* note request start time */
2158 req->r_request_started = jiffies;
2159
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002160 err = __prepare_send_request(mdsc, req, mds, false);
Sage Weil2f2dc052009-10-06 11:31:09 -07002161 if (!err) {
2162 ceph_msg_get(req->r_request);
2163 ceph_con_send(&session->s_con, req->r_request);
2164 }
2165
2166out_session:
2167 ceph_put_mds_session(session);
2168out:
2169 return err;
2170
2171finish:
Sage Weile1518c72010-05-13 11:19:06 -07002172 req->r_err = err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002173 complete_request(mdsc, req);
2174 goto out;
2175}
2176
2177/*
2178 * called under mdsc->mutex
2179 */
2180static void __wake_requests(struct ceph_mds_client *mdsc,
2181 struct list_head *head)
2182{
Yan, Zhenged75ec22012-11-19 10:49:06 +08002183 struct ceph_mds_request *req;
2184 LIST_HEAD(tmp_list);
Sage Weil2f2dc052009-10-06 11:31:09 -07002185
Yan, Zhenged75ec22012-11-19 10:49:06 +08002186 list_splice_init(head, &tmp_list);
2187
2188 while (!list_empty(&tmp_list)) {
2189 req = list_entry(tmp_list.next,
2190 struct ceph_mds_request, r_wait);
Sage Weil2f2dc052009-10-06 11:31:09 -07002191 list_del_init(&req->r_wait);
Sage Weil7971bd92013-05-01 21:15:58 -07002192 dout(" wake request %p tid %llu\n", req, req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002193 __do_request(mdsc, req);
2194 }
2195}
2196
2197/*
2198 * Wake up threads with requests pending for @mds, so that they can
Sage Weil29790f22010-03-18 14:45:05 -07002199 * resubmit their requests to a possibly different mds.
Sage Weil2f2dc052009-10-06 11:31:09 -07002200 */
Sage Weil29790f22010-03-18 14:45:05 -07002201static void kick_requests(struct ceph_mds_client *mdsc, int mds)
Sage Weil2f2dc052009-10-06 11:31:09 -07002202{
Sage Weil44ca18f2010-02-15 12:08:46 -08002203 struct ceph_mds_request *req;
Yan, Zheng282c1052014-07-30 10:12:47 +08002204 struct rb_node *p = rb_first(&mdsc->request_tree);
Sage Weil2f2dc052009-10-06 11:31:09 -07002205
2206 dout("kick_requests mds%d\n", mds);
Yan, Zheng282c1052014-07-30 10:12:47 +08002207 while (p) {
Sage Weil44ca18f2010-02-15 12:08:46 -08002208 req = rb_entry(p, struct ceph_mds_request, r_node);
Yan, Zheng282c1052014-07-30 10:12:47 +08002209 p = rb_next(p);
Sage Weil44ca18f2010-02-15 12:08:46 -08002210 if (req->r_got_unsafe)
2211 continue;
Yan, Zheng3de22be2015-02-04 14:26:22 +08002212 if (req->r_attempts > 0)
2213 continue; /* only new requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08002214 if (req->r_session &&
2215 req->r_session->s_mds == mds) {
2216 dout(" kicking tid %llu\n", req->r_tid);
Yan, Zheng03974e82014-09-11 14:28:56 +08002217 list_del_init(&req->r_wait);
Sage Weil44ca18f2010-02-15 12:08:46 -08002218 __do_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002219 }
2220 }
2221}
2222
2223void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
2224 struct ceph_mds_request *req)
2225{
2226 dout("submit_request on %p\n", req);
2227 mutex_lock(&mdsc->mutex);
2228 __register_request(mdsc, req, NULL);
2229 __do_request(mdsc, req);
2230 mutex_unlock(&mdsc->mutex);
2231}
2232
2233/*
2234 * Synchrously perform an mds request. Take care of all of the
2235 * session setup, forwarding, retry details.
2236 */
2237int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
2238 struct inode *dir,
2239 struct ceph_mds_request *req)
2240{
2241 int err;
2242
2243 dout("do_request on %p\n", req);
2244
2245 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
2246 if (req->r_inode)
2247 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
2248 if (req->r_locked_dir)
2249 ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
Sage Weil844d87c2013-02-05 13:40:09 -08002250 if (req->r_old_dentry_dir)
Sage Weil41b02e12011-07-26 11:31:14 -07002251 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
2252 CEPH_CAP_PIN);
Sage Weil2f2dc052009-10-06 11:31:09 -07002253
2254 /* issue */
2255 mutex_lock(&mdsc->mutex);
2256 __register_request(mdsc, req, dir);
2257 __do_request(mdsc, req);
2258
Sage Weile1518c72010-05-13 11:19:06 -07002259 if (req->r_err) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002260 err = req->r_err;
Sage Weile1518c72010-05-13 11:19:06 -07002261 __unregister_request(mdsc, req);
2262 dout("do_request early error %d\n", err);
2263 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07002264 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002265
Sage Weile1518c72010-05-13 11:19:06 -07002266 /* wait */
2267 mutex_unlock(&mdsc->mutex);
2268 dout("do_request waiting\n");
Ilya Dryomov5be73032015-05-19 12:05:38 +03002269 if (!req->r_timeout && req->r_wait_for_completion) {
Yan, Zheng9280be22014-10-14 10:33:35 +08002270 err = req->r_wait_for_completion(mdsc, req);
Sage Weile1518c72010-05-13 11:19:06 -07002271 } else {
Ilya Dryomov5be73032015-05-19 12:05:38 +03002272 long timeleft = wait_for_completion_killable_timeout(
2273 &req->r_completion,
2274 ceph_timeout_jiffies(req->r_timeout));
2275 if (timeleft > 0)
2276 err = 0;
2277 else if (!timeleft)
2278 err = -EIO; /* timed out */
2279 else
2280 err = timeleft; /* killed */
Sage Weile1518c72010-05-13 11:19:06 -07002281 }
2282 dout("do_request waited, got %d\n", err);
2283 mutex_lock(&mdsc->mutex);
2284
2285 /* only abort if we didn't race with a real reply */
2286 if (req->r_got_result) {
2287 err = le32_to_cpu(req->r_reply_info.head->result);
2288 } else if (err < 0) {
2289 dout("aborted request %lld with %d\n", req->r_tid, err);
Sage Weilb4556392010-05-13 12:01:13 -07002290
2291 /*
2292 * ensure we aren't running concurrently with
2293 * ceph_fill_trace or ceph_readdir_prepopulate, which
2294 * rely on locks (dir mutex) held by our caller.
2295 */
2296 mutex_lock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07002297 req->r_err = err;
2298 req->r_aborted = true;
Sage Weilb4556392010-05-13 12:01:13 -07002299 mutex_unlock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07002300
2301 if (req->r_locked_dir &&
Sage Weil167c9e32010-05-14 10:02:57 -07002302 (req->r_op & CEPH_MDS_OP_WRITE))
2303 ceph_invalidate_dir_request(req);
Sage Weile1518c72010-05-13 11:19:06 -07002304 } else {
2305 err = req->r_err;
2306 }
2307
2308out:
2309 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002310 dout("do_request %p done, result %d\n", req, err);
2311 return err;
2312}
2313
2314/*
Yan, Zheng2f276c52013-03-13 19:44:32 +08002315 * Invalidate dir's completeness, dentry lease state on an aborted MDS
Sage Weil167c9e32010-05-14 10:02:57 -07002316 * namespace request.
2317 */
2318void ceph_invalidate_dir_request(struct ceph_mds_request *req)
2319{
2320 struct inode *inode = req->r_locked_dir;
Sage Weil167c9e32010-05-14 10:02:57 -07002321
Yan, Zheng2f276c52013-03-13 19:44:32 +08002322 dout("invalidate_dir_request %p (complete, lease(s))\n", inode);
Sage Weil167c9e32010-05-14 10:02:57 -07002323
Yan, Zheng2f276c52013-03-13 19:44:32 +08002324 ceph_dir_clear_complete(inode);
Sage Weil167c9e32010-05-14 10:02:57 -07002325 if (req->r_dentry)
2326 ceph_invalidate_dentry_lease(req->r_dentry);
2327 if (req->r_old_dentry)
2328 ceph_invalidate_dentry_lease(req->r_old_dentry);
2329}
2330
2331/*
Sage Weil2f2dc052009-10-06 11:31:09 -07002332 * Handle mds reply.
2333 *
2334 * We take the session mutex and parse and process the reply immediately.
2335 * This preserves the logical ordering of replies, capabilities, etc., sent
2336 * by the MDS as they are applied to our local cache.
2337 */
2338static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2339{
2340 struct ceph_mds_client *mdsc = session->s_mdsc;
2341 struct ceph_mds_request *req;
2342 struct ceph_mds_reply_head *head = msg->front.iov_base;
2343 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
Yan, Zheng982d6012014-12-23 15:30:54 +08002344 struct ceph_snap_realm *realm;
Sage Weil2f2dc052009-10-06 11:31:09 -07002345 u64 tid;
2346 int err, result;
Sage Weil2600d2d2010-02-22 15:12:16 -08002347 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002348
Sage Weil2f2dc052009-10-06 11:31:09 -07002349 if (msg->front.iov_len < sizeof(*head)) {
2350 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002351 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002352 return;
2353 }
2354
2355 /* get request, session */
Sage Weil6df058c2009-12-22 11:24:33 -08002356 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002357 mutex_lock(&mdsc->mutex);
2358 req = __lookup_request(mdsc, tid);
2359 if (!req) {
2360 dout("handle_reply on unknown tid %llu\n", tid);
2361 mutex_unlock(&mdsc->mutex);
2362 return;
2363 }
2364 dout("handle_reply %p\n", req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002365
2366 /* correct session? */
Sage Weild96d6042010-03-20 20:50:58 -07002367 if (req->r_session != session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002368 pr_err("mdsc_handle_reply got %llu on session mds%d"
2369 " not mds%d\n", tid, session->s_mds,
2370 req->r_session ? req->r_session->s_mds : -1);
2371 mutex_unlock(&mdsc->mutex);
2372 goto out;
2373 }
2374
2375 /* dup? */
2376 if ((req->r_got_unsafe && !head->safe) ||
2377 (req->r_got_safe && head->safe)) {
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07002378 pr_warn("got a dup %s reply on %llu from mds%d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002379 head->safe ? "safe" : "unsafe", tid, mds);
2380 mutex_unlock(&mdsc->mutex);
2381 goto out;
2382 }
Sage Weil85792d0d2010-05-13 09:06:02 -07002383 if (req->r_got_safe && !head->safe) {
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07002384 pr_warn("got unsafe after safe on %llu from mds%d\n",
Sage Weil85792d0d2010-05-13 09:06:02 -07002385 tid, mds);
2386 mutex_unlock(&mdsc->mutex);
2387 goto out;
2388 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002389
2390 result = le32_to_cpu(head->result);
2391
2392 /*
Greg Farnume55b71f2010-06-22 15:58:01 -07002393 * Handle an ESTALE
2394 * if we're not talking to the authority, send to them
2395 * if the authority has changed while we weren't looking,
2396 * send to new authority
2397 * Otherwise we just have to return an ESTALE
Sage Weil2f2dc052009-10-06 11:31:09 -07002398 */
2399 if (result == -ESTALE) {
Greg Farnume55b71f2010-06-22 15:58:01 -07002400 dout("got ESTALE on request %llu", req->r_tid);
Yan, Zheng51da8e82014-07-14 10:48:29 +08002401 req->r_resend_mds = -1;
Yan, Zhengca18bed2013-11-22 14:21:44 +08002402 if (req->r_direct_mode != USE_AUTH_MDS) {
Greg Farnume55b71f2010-06-22 15:58:01 -07002403 dout("not using auth, setting for that now");
2404 req->r_direct_mode = USE_AUTH_MDS;
Sage Weil2f2dc052009-10-06 11:31:09 -07002405 __do_request(mdsc, req);
2406 mutex_unlock(&mdsc->mutex);
2407 goto out;
Greg Farnume55b71f2010-06-22 15:58:01 -07002408 } else {
Yan, Zhengca18bed2013-11-22 14:21:44 +08002409 int mds = __choose_mds(mdsc, req);
2410 if (mds >= 0 && mds != req->r_session->s_mds) {
2411 dout("but auth changed, so resending");
Greg Farnume55b71f2010-06-22 15:58:01 -07002412 __do_request(mdsc, req);
2413 mutex_unlock(&mdsc->mutex);
2414 goto out;
2415 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002416 }
Greg Farnume55b71f2010-06-22 15:58:01 -07002417 dout("have to return ESTALE on request %llu", req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002418 }
2419
Greg Farnume55b71f2010-06-22 15:58:01 -07002420
Sage Weil2f2dc052009-10-06 11:31:09 -07002421 if (head->safe) {
2422 req->r_got_safe = true;
2423 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002424
2425 if (req->r_got_unsafe) {
2426 /*
2427 * We already handled the unsafe response, now do the
2428 * cleanup. No need to examine the response; the MDS
2429 * doesn't include any result info in the safe
2430 * response. And even if it did, there is nothing
2431 * useful we could do with a revised return value.
2432 */
2433 dout("got safe reply %llu, mds%d\n", tid, mds);
2434 list_del_init(&req->r_unsafe_item);
2435
2436 /* last unsafe request during umount? */
Sage Weil44ca18f2010-02-15 12:08:46 -08002437 if (mdsc->stopping && !__get_oldest_req(mdsc))
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002438 complete_all(&mdsc->safe_umount_waiters);
Sage Weil2f2dc052009-10-06 11:31:09 -07002439 mutex_unlock(&mdsc->mutex);
2440 goto out;
2441 }
Sage Weile1518c72010-05-13 11:19:06 -07002442 } else {
Sage Weil2f2dc052009-10-06 11:31:09 -07002443 req->r_got_unsafe = true;
2444 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
2445 }
2446
2447 dout("handle_reply tid %lld result %d\n", tid, result);
2448 rinfo = &req->r_reply_info;
Sage Weil14303d22010-12-14 17:37:52 -08002449 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
Sage Weil2f2dc052009-10-06 11:31:09 -07002450 mutex_unlock(&mdsc->mutex);
2451
2452 mutex_lock(&session->s_mutex);
2453 if (err < 0) {
Herb Shiu25933ab2010-12-01 14:14:38 -08002454 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002455 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002456 goto out_err;
2457 }
2458
2459 /* snap trace */
Yan, Zheng982d6012014-12-23 15:30:54 +08002460 realm = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07002461 if (rinfo->snapblob_len) {
2462 down_write(&mdsc->snap_rwsem);
2463 ceph_update_snap_trace(mdsc, rinfo->snapblob,
Yan, Zheng982d6012014-12-23 15:30:54 +08002464 rinfo->snapblob + rinfo->snapblob_len,
2465 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP,
2466 &realm);
Sage Weil2f2dc052009-10-06 11:31:09 -07002467 downgrade_write(&mdsc->snap_rwsem);
2468 } else {
2469 down_read(&mdsc->snap_rwsem);
2470 }
2471
2472 /* insert trace into our cache */
Sage Weilb4556392010-05-13 12:01:13 -07002473 mutex_lock(&req->r_fill_mutex);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002474 err = ceph_fill_trace(mdsc->fsc->sb, req, req->r_session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002475 if (err == 0) {
Sam Lang6e8575f2012-12-28 09:56:46 -08002476 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
Yan, Zheng81c6aea2013-09-18 09:44:13 +08002477 req->r_op == CEPH_MDS_OP_LSSNAP))
Sage Weil2f2dc052009-10-06 11:31:09 -07002478 ceph_readdir_prepopulate(req, req->r_session);
Yehuda Sadeh37151662010-06-17 16:16:12 -07002479 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
Sage Weil2f2dc052009-10-06 11:31:09 -07002480 }
Sage Weilb4556392010-05-13 12:01:13 -07002481 mutex_unlock(&req->r_fill_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002482
2483 up_read(&mdsc->snap_rwsem);
Yan, Zheng982d6012014-12-23 15:30:54 +08002484 if (realm)
2485 ceph_put_snap_realm(mdsc, realm);
Sage Weil2f2dc052009-10-06 11:31:09 -07002486out_err:
Sage Weile1518c72010-05-13 11:19:06 -07002487 mutex_lock(&mdsc->mutex);
2488 if (!req->r_aborted) {
2489 if (err) {
2490 req->r_err = err;
2491 } else {
2492 req->r_reply = msg;
2493 ceph_msg_get(msg);
2494 req->r_got_result = true;
2495 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002496 } else {
Sage Weile1518c72010-05-13 11:19:06 -07002497 dout("reply arrived after request %lld was aborted\n", tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002498 }
Sage Weile1518c72010-05-13 11:19:06 -07002499 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002500
Sage Weil2f2dc052009-10-06 11:31:09 -07002501 mutex_unlock(&session->s_mutex);
2502
2503 /* kick calling process */
2504 complete_request(mdsc, req);
2505out:
2506 ceph_mdsc_put_request(req);
2507 return;
2508}
2509
2510
2511
2512/*
2513 * handle mds notification that our request has been forwarded.
2514 */
Sage Weil2600d2d2010-02-22 15:12:16 -08002515static void handle_forward(struct ceph_mds_client *mdsc,
2516 struct ceph_mds_session *session,
2517 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002518{
2519 struct ceph_mds_request *req;
Sage Weila1ea7872010-02-23 14:02:44 -08002520 u64 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002521 u32 next_mds;
2522 u32 fwd_seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002523 int err = -EINVAL;
2524 void *p = msg->front.iov_base;
2525 void *end = p + msg->front.iov_len;
Sage Weil2f2dc052009-10-06 11:31:09 -07002526
Sage Weila1ea7872010-02-23 14:02:44 -08002527 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002528 next_mds = ceph_decode_32(&p);
2529 fwd_seq = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07002530
2531 mutex_lock(&mdsc->mutex);
2532 req = __lookup_request(mdsc, tid);
2533 if (!req) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07002534 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002535 goto out; /* dup reply? */
2536 }
2537
Sage Weil2a8e5e32010-05-28 16:43:16 -07002538 if (req->r_aborted) {
2539 dout("forward tid %llu aborted, unregistering\n", tid);
2540 __unregister_request(mdsc, req);
2541 } else if (fwd_seq <= req->r_num_fwd) {
2542 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002543 tid, next_mds, req->r_num_fwd, fwd_seq);
2544 } else {
2545 /* resend. forward race not possible; mds would drop */
Sage Weil2a8e5e32010-05-28 16:43:16 -07002546 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2547 BUG_ON(req->r_err);
2548 BUG_ON(req->r_got_result);
Yan, Zheng3de22be2015-02-04 14:26:22 +08002549 req->r_attempts = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002550 req->r_num_fwd = fwd_seq;
2551 req->r_resend_mds = next_mds;
2552 put_request_session(req);
2553 __do_request(mdsc, req);
2554 }
2555 ceph_mdsc_put_request(req);
2556out:
2557 mutex_unlock(&mdsc->mutex);
2558 return;
2559
2560bad:
2561 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2562}
2563
2564/*
2565 * handle a mds session control message
2566 */
2567static void handle_session(struct ceph_mds_session *session,
2568 struct ceph_msg *msg)
2569{
2570 struct ceph_mds_client *mdsc = session->s_mdsc;
2571 u32 op;
2572 u64 seq;
Sage Weil2600d2d2010-02-22 15:12:16 -08002573 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002574 struct ceph_mds_session_head *h = msg->front.iov_base;
2575 int wake = 0;
2576
Sage Weil2f2dc052009-10-06 11:31:09 -07002577 /* decode */
2578 if (msg->front.iov_len != sizeof(*h))
2579 goto bad;
2580 op = le32_to_cpu(h->op);
2581 seq = le64_to_cpu(h->seq);
2582
2583 mutex_lock(&mdsc->mutex);
Sage Weil2600d2d2010-02-22 15:12:16 -08002584 if (op == CEPH_SESSION_CLOSE)
2585 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002586 /* FIXME: this ttl calculation is generous */
2587 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2588 mutex_unlock(&mdsc->mutex);
2589
2590 mutex_lock(&session->s_mutex);
2591
2592 dout("handle_session mds%d %s %p state %s seq %llu\n",
2593 mds, ceph_session_op_name(op), session,
John Spraya687eca2014-09-19 13:51:08 +01002594 ceph_session_state_name(session->s_state), seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002595
2596 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2597 session->s_state = CEPH_MDS_SESSION_OPEN;
2598 pr_info("mds%d came back\n", session->s_mds);
2599 }
2600
2601 switch (op) {
2602 case CEPH_SESSION_OPEN:
Sage Weil29790f22010-03-18 14:45:05 -07002603 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2604 pr_info("mds%d reconnect success\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002605 session->s_state = CEPH_MDS_SESSION_OPEN;
2606 renewed_caps(mdsc, session, 0);
2607 wake = 1;
2608 if (mdsc->stopping)
2609 __close_session(mdsc, session);
2610 break;
2611
2612 case CEPH_SESSION_RENEWCAPS:
2613 if (session->s_renew_seq == seq)
2614 renewed_caps(mdsc, session, 1);
2615 break;
2616
2617 case CEPH_SESSION_CLOSE:
Sage Weil29790f22010-03-18 14:45:05 -07002618 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2619 pr_info("mds%d reconnect denied\n", session->s_mds);
Yan, Zheng1c841a92015-03-24 20:15:36 +08002620 cleanup_session_requests(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002621 remove_session_caps(session);
Yan, Zheng656e4382014-09-11 14:25:18 +08002622 wake = 2; /* for good measure */
Sage Weilf3c60c52010-08-11 14:51:23 -07002623 wake_up_all(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002624 break;
2625
2626 case CEPH_SESSION_STALE:
2627 pr_info("mds%d caps went stale, renewing\n",
2628 session->s_mds);
Alex Elderd8fb02a2012-01-12 17:48:10 -08002629 spin_lock(&session->s_gen_ttl_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002630 session->s_cap_gen++;
Alex Elder1ce208a2012-01-12 17:48:11 -08002631 session->s_cap_ttl = jiffies - 1;
Alex Elderd8fb02a2012-01-12 17:48:10 -08002632 spin_unlock(&session->s_gen_ttl_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002633 send_renew_caps(mdsc, session);
2634 break;
2635
2636 case CEPH_SESSION_RECALL_STATE:
2637 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2638 break;
2639
Yan, Zheng186e4f72013-11-22 14:48:37 +08002640 case CEPH_SESSION_FLUSHMSG:
2641 send_flushmsg_ack(mdsc, session, seq);
2642 break;
2643
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08002644 case CEPH_SESSION_FORCE_RO:
2645 dout("force_session_readonly %p\n", session);
2646 spin_lock(&session->s_cap_lock);
2647 session->s_readonly = true;
2648 spin_unlock(&session->s_cap_lock);
2649 wake_up_session_caps(session, 0);
2650 break;
2651
Sage Weil2f2dc052009-10-06 11:31:09 -07002652 default:
2653 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2654 WARN_ON(1);
2655 }
2656
2657 mutex_unlock(&session->s_mutex);
2658 if (wake) {
2659 mutex_lock(&mdsc->mutex);
2660 __wake_requests(mdsc, &session->s_waiting);
Yan, Zheng656e4382014-09-11 14:25:18 +08002661 if (wake == 2)
2662 kick_requests(mdsc, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002663 mutex_unlock(&mdsc->mutex);
2664 }
2665 return;
2666
2667bad:
2668 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2669 (int)msg->front.iov_len);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002670 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002671 return;
2672}
2673
2674
2675/*
2676 * called under session->mutex.
2677 */
2678static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2679 struct ceph_mds_session *session)
2680{
2681 struct ceph_mds_request *req, *nreq;
Yan, Zheng3de22be2015-02-04 14:26:22 +08002682 struct rb_node *p;
Sage Weil2f2dc052009-10-06 11:31:09 -07002683 int err;
2684
2685 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2686
2687 mutex_lock(&mdsc->mutex);
2688 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002689 err = __prepare_send_request(mdsc, req, session->s_mds, true);
Sage Weil2f2dc052009-10-06 11:31:09 -07002690 if (!err) {
2691 ceph_msg_get(req->r_request);
2692 ceph_con_send(&session->s_con, req->r_request);
2693 }
2694 }
Yan, Zheng3de22be2015-02-04 14:26:22 +08002695
2696 /*
2697 * also re-send old requests when MDS enters reconnect stage. So that MDS
2698 * can process completed request in clientreplay stage.
2699 */
2700 p = rb_first(&mdsc->request_tree);
2701 while (p) {
2702 req = rb_entry(p, struct ceph_mds_request, r_node);
2703 p = rb_next(p);
2704 if (req->r_got_unsafe)
2705 continue;
2706 if (req->r_attempts == 0)
2707 continue; /* only old requests */
2708 if (req->r_session &&
2709 req->r_session->s_mds == session->s_mds) {
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002710 err = __prepare_send_request(mdsc, req,
2711 session->s_mds, true);
Yan, Zheng3de22be2015-02-04 14:26:22 +08002712 if (!err) {
2713 ceph_msg_get(req->r_request);
2714 ceph_con_send(&session->s_con, req->r_request);
2715 }
2716 }
2717 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002718 mutex_unlock(&mdsc->mutex);
2719}
2720
2721/*
2722 * Encode information about a cap for a reconnect with the MDS.
2723 */
Sage Weil2f2dc052009-10-06 11:31:09 -07002724static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2725 void *arg)
2726{
Sage Weil20cb34a2010-05-12 15:21:32 -07002727 union {
2728 struct ceph_mds_cap_reconnect v2;
2729 struct ceph_mds_cap_reconnect_v1 v1;
2730 } rec;
2731 size_t reclen;
Sage Weil2f2dc052009-10-06 11:31:09 -07002732 struct ceph_inode_info *ci;
Sage Weil20cb34a2010-05-12 15:21:32 -07002733 struct ceph_reconnect_state *recon_state = arg;
2734 struct ceph_pagelist *pagelist = recon_state->pagelist;
Sage Weil2f2dc052009-10-06 11:31:09 -07002735 char *path;
2736 int pathlen, err;
2737 u64 pathbase;
2738 struct dentry *dentry;
2739
2740 ci = cap->ci;
2741
2742 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2743 inode, ceph_vinop(inode), cap, cap->cap_id,
2744 ceph_cap_string(cap->issued));
Sage Weil93cea5b2009-12-23 12:21:51 -08002745 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2746 if (err)
2747 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002748
2749 dentry = d_find_alias(inode);
2750 if (dentry) {
2751 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2752 if (IS_ERR(path)) {
2753 err = PTR_ERR(path);
Sage Weile072f8a2010-08-26 09:26:37 -07002754 goto out_dput;
Sage Weil2f2dc052009-10-06 11:31:09 -07002755 }
2756 } else {
2757 path = NULL;
2758 pathlen = 0;
2759 }
Sage Weil93cea5b2009-12-23 12:21:51 -08002760 err = ceph_pagelist_encode_string(pagelist, path, pathlen);
2761 if (err)
Sage Weile072f8a2010-08-26 09:26:37 -07002762 goto out_free;
Sage Weil2f2dc052009-10-06 11:31:09 -07002763
Sage Weilbe655592011-11-30 09:47:09 -08002764 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002765 cap->seq = 0; /* reset cap seq */
2766 cap->issue_seq = 0; /* and issue_seq */
Yan, Zheng667ca052013-05-31 16:25:36 +08002767 cap->mseq = 0; /* and migrate_seq */
Yan, Zheng99a9c272013-09-22 11:08:14 +08002768 cap->cap_gen = cap->session->s_cap_gen;
Sage Weil20cb34a2010-05-12 15:21:32 -07002769
2770 if (recon_state->flock) {
2771 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
2772 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2773 rec.v2.issued = cpu_to_le32(cap->issued);
2774 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2775 rec.v2.pathbase = cpu_to_le64(pathbase);
2776 rec.v2.flock_len = 0;
2777 reclen = sizeof(rec.v2);
2778 } else {
2779 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
2780 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2781 rec.v1.issued = cpu_to_le32(cap->issued);
2782 rec.v1.size = cpu_to_le64(inode->i_size);
2783 ceph_encode_timespec(&rec.v1.mtime, &inode->i_mtime);
2784 ceph_encode_timespec(&rec.v1.atime, &inode->i_atime);
2785 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2786 rec.v1.pathbase = cpu_to_le64(pathbase);
2787 reclen = sizeof(rec.v1);
2788 }
Sage Weilbe655592011-11-30 09:47:09 -08002789 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002790
Greg Farnum40819f62010-08-02 15:34:23 -07002791 if (recon_state->flock) {
2792 int num_fcntl_locks, num_flock_locks;
Jim Schutt39be95e2013-05-15 13:03:35 -05002793 struct ceph_filelock *flocks;
Greg Farnum40819f62010-08-02 15:34:23 -07002794
Jim Schutt39be95e2013-05-15 13:03:35 -05002795encode_again:
Jim Schutt39be95e2013-05-15 13:03:35 -05002796 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
Jim Schutt39be95e2013-05-15 13:03:35 -05002797 flocks = kmalloc((num_fcntl_locks+num_flock_locks) *
2798 sizeof(struct ceph_filelock), GFP_NOFS);
2799 if (!flocks) {
2800 err = -ENOMEM;
2801 goto out_free;
2802 }
Jim Schutt39be95e2013-05-15 13:03:35 -05002803 err = ceph_encode_locks_to_buffer(inode, flocks,
2804 num_fcntl_locks,
2805 num_flock_locks);
Jim Schutt39be95e2013-05-15 13:03:35 -05002806 if (err) {
2807 kfree(flocks);
2808 if (err == -ENOSPC)
2809 goto encode_again;
2810 goto out_free;
2811 }
2812 /*
2813 * number of encoded locks is stable, so copy to pagelist
2814 */
2815 rec.v2.flock_len = cpu_to_le32(2*sizeof(u32) +
2816 (num_fcntl_locks+num_flock_locks) *
2817 sizeof(struct ceph_filelock));
2818 err = ceph_pagelist_append(pagelist, &rec, reclen);
2819 if (!err)
2820 err = ceph_locks_to_pagelist(flocks, pagelist,
2821 num_fcntl_locks,
2822 num_flock_locks);
2823 kfree(flocks);
Sage Weil3612abb2010-09-07 15:59:27 -07002824 } else {
2825 err = ceph_pagelist_append(pagelist, &rec, reclen);
Greg Farnum40819f62010-08-02 15:34:23 -07002826 }
Yan, Zheng44c99752013-09-22 10:28:10 +08002827
2828 recon_state->nr_caps++;
Sage Weile072f8a2010-08-26 09:26:37 -07002829out_free:
Sage Weil2f2dc052009-10-06 11:31:09 -07002830 kfree(path);
Sage Weile072f8a2010-08-26 09:26:37 -07002831out_dput:
Sage Weil2f2dc052009-10-06 11:31:09 -07002832 dput(dentry);
Sage Weil93cea5b2009-12-23 12:21:51 -08002833 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002834}
2835
2836
2837/*
2838 * If an MDS fails and recovers, clients need to reconnect in order to
2839 * reestablish shared state. This includes all caps issued through
2840 * this session _and_ the snap_realm hierarchy. Because it's not
2841 * clear which snap realms the mds cares about, we send everything we
2842 * know about.. that ensures we'll then get any new info the
2843 * recovering MDS might have.
2844 *
2845 * This is a relatively heavyweight operation, but it's rare.
2846 *
2847 * called with mdsc->mutex held.
2848 */
Sage Weil34b6c852010-05-10 16:31:25 -07002849static void send_mds_reconnect(struct ceph_mds_client *mdsc,
2850 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07002851{
Sage Weil2f2dc052009-10-06 11:31:09 -07002852 struct ceph_msg *reply;
Sage Weila105f002010-02-15 14:37:55 -08002853 struct rb_node *p;
Sage Weil34b6c852010-05-10 16:31:25 -07002854 int mds = session->s_mds;
Sage Weil9abf82b2010-05-10 21:58:38 -07002855 int err = -ENOMEM;
Yan, Zheng44c99752013-09-22 10:28:10 +08002856 int s_nr_caps;
Sage Weil93cea5b2009-12-23 12:21:51 -08002857 struct ceph_pagelist *pagelist;
Sage Weil20cb34a2010-05-12 15:21:32 -07002858 struct ceph_reconnect_state recon_state;
Sage Weil2f2dc052009-10-06 11:31:09 -07002859
Sage Weil34b6c852010-05-10 16:31:25 -07002860 pr_info("mds%d reconnect start\n", mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002861
Sage Weil93cea5b2009-12-23 12:21:51 -08002862 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
2863 if (!pagelist)
2864 goto fail_nopagelist;
2865 ceph_pagelist_init(pagelist);
2866
Sage Weilb61c2762011-08-09 15:03:46 -07002867 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07002868 if (!reply)
Sage Weil93cea5b2009-12-23 12:21:51 -08002869 goto fail_nomsg;
Sage Weil93cea5b2009-12-23 12:21:51 -08002870
Sage Weil34b6c852010-05-10 16:31:25 -07002871 mutex_lock(&session->s_mutex);
2872 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
2873 session->s_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002874
Sage Weil2f2dc052009-10-06 11:31:09 -07002875 dout("session %p state %s\n", session,
John Spraya687eca2014-09-19 13:51:08 +01002876 ceph_session_state_name(session->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07002877
Yan, Zheng99a9c272013-09-22 11:08:14 +08002878 spin_lock(&session->s_gen_ttl_lock);
2879 session->s_cap_gen++;
2880 spin_unlock(&session->s_gen_ttl_lock);
2881
2882 spin_lock(&session->s_cap_lock);
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08002883 /* don't know if session is readonly */
2884 session->s_readonly = 0;
Yan, Zheng99a9c272013-09-22 11:08:14 +08002885 /*
2886 * notify __ceph_remove_cap() that we are composing cap reconnect.
2887 * If a cap get released before being added to the cap reconnect,
2888 * __ceph_remove_cap() should skip queuing cap release.
2889 */
2890 session->s_cap_reconnect = 1;
Sage Weile01a5942010-05-10 15:36:44 -07002891 /* drop old cap expires; we're about to reestablish that state */
Yan, Zheng745a8e32015-05-14 17:22:42 +08002892 cleanup_cap_releases(mdsc, session);
Sage Weile01a5942010-05-10 15:36:44 -07002893
Yan, Zheng5d233712014-09-10 16:56:23 +08002894 /* trim unused caps to reduce MDS's cache rejoin time */
Yan, Zhengc0bd50e2015-04-07 15:51:08 +08002895 if (mdsc->fsc->sb->s_root)
2896 shrink_dcache_parent(mdsc->fsc->sb->s_root);
Yan, Zheng5d233712014-09-10 16:56:23 +08002897
2898 ceph_con_close(&session->s_con);
2899 ceph_con_open(&session->s_con,
2900 CEPH_ENTITY_TYPE_MDS, mds,
2901 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
2902
2903 /* replay unsafe requests */
2904 replay_unsafe_requests(mdsc, session);
2905
2906 down_read(&mdsc->snap_rwsem);
2907
Sage Weil2f2dc052009-10-06 11:31:09 -07002908 /* traverse this session's caps */
Yan, Zheng44c99752013-09-22 10:28:10 +08002909 s_nr_caps = session->s_nr_caps;
2910 err = ceph_pagelist_encode_32(pagelist, s_nr_caps);
Sage Weil93cea5b2009-12-23 12:21:51 -08002911 if (err)
2912 goto fail;
Sage Weil20cb34a2010-05-12 15:21:32 -07002913
Yan, Zheng44c99752013-09-22 10:28:10 +08002914 recon_state.nr_caps = 0;
Sage Weil20cb34a2010-05-12 15:21:32 -07002915 recon_state.pagelist = pagelist;
2916 recon_state.flock = session->s_con.peer_features & CEPH_FEATURE_FLOCK;
2917 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
Sage Weil2f2dc052009-10-06 11:31:09 -07002918 if (err < 0)
Sage Weil9abf82b2010-05-10 21:58:38 -07002919 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07002920
Yan, Zheng99a9c272013-09-22 11:08:14 +08002921 spin_lock(&session->s_cap_lock);
2922 session->s_cap_reconnect = 0;
2923 spin_unlock(&session->s_cap_lock);
2924
Sage Weil2f2dc052009-10-06 11:31:09 -07002925 /*
2926 * snaprealms. we provide mds with the ino, seq (version), and
2927 * parent for all of our realms. If the mds has any newer info,
2928 * it will tell us.
2929 */
Sage Weila105f002010-02-15 14:37:55 -08002930 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
2931 struct ceph_snap_realm *realm =
2932 rb_entry(p, struct ceph_snap_realm, node);
Sage Weil93cea5b2009-12-23 12:21:51 -08002933 struct ceph_mds_snaprealm_reconnect sr_rec;
Sage Weil2f2dc052009-10-06 11:31:09 -07002934
2935 dout(" adding snap realm %llx seq %lld parent %llx\n",
2936 realm->ino, realm->seq, realm->parent_ino);
Sage Weil93cea5b2009-12-23 12:21:51 -08002937 sr_rec.ino = cpu_to_le64(realm->ino);
2938 sr_rec.seq = cpu_to_le64(realm->seq);
2939 sr_rec.parent = cpu_to_le64(realm->parent_ino);
2940 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
2941 if (err)
2942 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07002943 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002944
Sage Weil20cb34a2010-05-12 15:21:32 -07002945 if (recon_state.flock)
2946 reply->hdr.version = cpu_to_le16(2);
Yan, Zheng44c99752013-09-22 10:28:10 +08002947
2948 /* raced with cap release? */
2949 if (s_nr_caps != recon_state.nr_caps) {
2950 struct page *page = list_first_entry(&pagelist->head,
2951 struct page, lru);
2952 __le32 *addr = kmap_atomic(page);
2953 *addr = cpu_to_le32(recon_state.nr_caps);
2954 kunmap_atomic(addr);
Alex Elderebf18f42013-03-04 22:29:57 -06002955 }
Yan, Zheng44c99752013-09-22 10:28:10 +08002956
2957 reply->hdr.data_len = cpu_to_le32(pagelist->length);
2958 ceph_msg_data_add_pagelist(reply, pagelist);
Sage Weil2f2dc052009-10-06 11:31:09 -07002959 ceph_con_send(&session->s_con, reply);
2960
Sage Weil9abf82b2010-05-10 21:58:38 -07002961 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002962
Sage Weil9abf82b2010-05-10 21:58:38 -07002963 mutex_lock(&mdsc->mutex);
2964 __wake_requests(mdsc, &session->s_waiting);
2965 mutex_unlock(&mdsc->mutex);
2966
Sage Weil2f2dc052009-10-06 11:31:09 -07002967 up_read(&mdsc->snap_rwsem);
Sage Weil2f2dc052009-10-06 11:31:09 -07002968 return;
2969
Sage Weil93cea5b2009-12-23 12:21:51 -08002970fail:
Sage Weil2f2dc052009-10-06 11:31:09 -07002971 ceph_msg_put(reply);
Sage Weil9abf82b2010-05-10 21:58:38 -07002972 up_read(&mdsc->snap_rwsem);
2973 mutex_unlock(&session->s_mutex);
Sage Weil93cea5b2009-12-23 12:21:51 -08002974fail_nomsg:
2975 ceph_pagelist_release(pagelist);
Sage Weil93cea5b2009-12-23 12:21:51 -08002976fail_nopagelist:
Sage Weil9abf82b2010-05-10 21:58:38 -07002977 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
Sage Weil9abf82b2010-05-10 21:58:38 -07002978 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07002979}
2980
2981
2982/*
2983 * compare old and new mdsmaps, kicking requests
2984 * and closing out old connections as necessary
2985 *
2986 * called under mdsc->mutex.
2987 */
2988static void check_new_map(struct ceph_mds_client *mdsc,
2989 struct ceph_mdsmap *newmap,
2990 struct ceph_mdsmap *oldmap)
2991{
2992 int i;
2993 int oldstate, newstate;
2994 struct ceph_mds_session *s;
2995
2996 dout("check_new_map new %u old %u\n",
2997 newmap->m_epoch, oldmap->m_epoch);
2998
2999 for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
3000 if (mdsc->sessions[i] == NULL)
3001 continue;
3002 s = mdsc->sessions[i];
3003 oldstate = ceph_mdsmap_get_state(oldmap, i);
3004 newstate = ceph_mdsmap_get_state(newmap, i);
3005
Sage Weil0deb01c2010-06-17 14:19:01 -07003006 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07003007 i, ceph_mds_state_name(oldstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07003008 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
Sage Weil2f2dc052009-10-06 11:31:09 -07003009 ceph_mds_state_name(newstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07003010 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
John Spraya687eca2014-09-19 13:51:08 +01003011 ceph_session_state_name(s->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07003012
Yan, Zheng3e8f43a2012-09-20 17:42:25 +08003013 if (i >= newmap->m_max_mds ||
3014 memcmp(ceph_mdsmap_get_addr(oldmap, i),
Sage Weil2f2dc052009-10-06 11:31:09 -07003015 ceph_mdsmap_get_addr(newmap, i),
3016 sizeof(struct ceph_entity_addr))) {
3017 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
3018 /* the session never opened, just close it
3019 * out now */
3020 __wake_requests(mdsc, &s->s_waiting);
Sage Weil2600d2d2010-02-22 15:12:16 -08003021 __unregister_session(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07003022 } else {
3023 /* just close it */
3024 mutex_unlock(&mdsc->mutex);
3025 mutex_lock(&s->s_mutex);
3026 mutex_lock(&mdsc->mutex);
3027 ceph_con_close(&s->s_con);
3028 mutex_unlock(&s->s_mutex);
3029 s->s_state = CEPH_MDS_SESSION_RESTARTING;
3030 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003031 } else if (oldstate == newstate) {
3032 continue; /* nothing new with this mds */
3033 }
3034
3035 /*
3036 * send reconnect?
3037 */
3038 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
Sage Weil34b6c852010-05-10 16:31:25 -07003039 newstate >= CEPH_MDS_STATE_RECONNECT) {
3040 mutex_unlock(&mdsc->mutex);
3041 send_mds_reconnect(mdsc, s);
3042 mutex_lock(&mdsc->mutex);
3043 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003044
3045 /*
Sage Weil29790f22010-03-18 14:45:05 -07003046 * kick request on any mds that has gone active.
Sage Weil2f2dc052009-10-06 11:31:09 -07003047 */
3048 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
3049 newstate >= CEPH_MDS_STATE_ACTIVE) {
Sage Weil29790f22010-03-18 14:45:05 -07003050 if (oldstate != CEPH_MDS_STATE_CREATING &&
3051 oldstate != CEPH_MDS_STATE_STARTING)
3052 pr_info("mds%d recovery completed\n", s->s_mds);
3053 kick_requests(mdsc, i);
Sage Weil2f2dc052009-10-06 11:31:09 -07003054 ceph_kick_flushing_caps(mdsc, s);
Sage Weil0dc25702009-11-20 13:43:45 -08003055 wake_up_session_caps(s, 1);
Sage Weil2f2dc052009-10-06 11:31:09 -07003056 }
3057 }
Sage Weilcb170a22010-06-21 13:38:35 -07003058
3059 for (i = 0; i < newmap->m_max_mds && i < mdsc->max_sessions; i++) {
3060 s = mdsc->sessions[i];
3061 if (!s)
3062 continue;
3063 if (!ceph_mdsmap_is_laggy(newmap, i))
3064 continue;
3065 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3066 s->s_state == CEPH_MDS_SESSION_HUNG ||
3067 s->s_state == CEPH_MDS_SESSION_CLOSING) {
3068 dout(" connecting to export targets of laggy mds%d\n",
3069 i);
3070 __open_export_target_sessions(mdsc, s);
3071 }
3072 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003073}
3074
3075
3076
3077/*
3078 * leases
3079 */
3080
3081/*
3082 * caller must hold session s_mutex, dentry->d_lock
3083 */
3084void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
3085{
3086 struct ceph_dentry_info *di = ceph_dentry(dentry);
3087
3088 ceph_put_mds_session(di->lease_session);
3089 di->lease_session = NULL;
3090}
3091
Sage Weil2600d2d2010-02-22 15:12:16 -08003092static void handle_lease(struct ceph_mds_client *mdsc,
3093 struct ceph_mds_session *session,
3094 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07003095{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003096 struct super_block *sb = mdsc->fsc->sb;
Sage Weil2f2dc052009-10-06 11:31:09 -07003097 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07003098 struct dentry *parent, *dentry;
3099 struct ceph_dentry_info *di;
Sage Weil2600d2d2010-02-22 15:12:16 -08003100 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07003101 struct ceph_mds_lease *h = msg->front.iov_base;
Sage Weil1e5ea232010-06-04 10:05:40 -07003102 u32 seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003103 struct ceph_vino vino;
Sage Weil2f2dc052009-10-06 11:31:09 -07003104 struct qstr dname;
3105 int release = 0;
3106
Sage Weil2f2dc052009-10-06 11:31:09 -07003107 dout("handle_lease from mds%d\n", mds);
3108
3109 /* decode */
3110 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
3111 goto bad;
3112 vino.ino = le64_to_cpu(h->ino);
3113 vino.snap = CEPH_NOSNAP;
Sage Weil1e5ea232010-06-04 10:05:40 -07003114 seq = le32_to_cpu(h->seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003115 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
3116 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
3117 if (dname.len != get_unaligned_le32(h+1))
3118 goto bad;
3119
Sage Weil2f2dc052009-10-06 11:31:09 -07003120 /* lookup inode */
3121 inode = ceph_find_inode(sb, vino);
Sage Weil2f90b852011-07-26 11:28:25 -07003122 dout("handle_lease %s, ino %llx %p %.*s\n",
3123 ceph_lease_op_name(h->action), vino.ino, inode,
Sage Weil1e5ea232010-06-04 10:05:40 -07003124 dname.len, dname.name);
Yan, Zheng6cd3bca2014-09-17 07:45:12 +08003125
3126 mutex_lock(&session->s_mutex);
3127 session->s_seq++;
3128
Sage Weil2f2dc052009-10-06 11:31:09 -07003129 if (inode == NULL) {
3130 dout("handle_lease no inode %llx\n", vino.ino);
3131 goto release;
3132 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003133
3134 /* dentry */
3135 parent = d_find_alias(inode);
3136 if (!parent) {
3137 dout("no parent dentry on inode %p\n", inode);
3138 WARN_ON(1);
3139 goto release; /* hrm... */
3140 }
3141 dname.hash = full_name_hash(dname.name, dname.len);
3142 dentry = d_lookup(parent, &dname);
3143 dput(parent);
3144 if (!dentry)
3145 goto release;
3146
3147 spin_lock(&dentry->d_lock);
3148 di = ceph_dentry(dentry);
3149 switch (h->action) {
3150 case CEPH_MDS_LEASE_REVOKE:
Sage Weil3d8eb7a2011-11-11 09:48:53 -08003151 if (di->lease_session == session) {
Sage Weil1e5ea232010-06-04 10:05:40 -07003152 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
3153 h->seq = cpu_to_le32(di->lease_seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003154 __ceph_mdsc_drop_dentry_lease(dentry);
3155 }
3156 release = 1;
3157 break;
3158
3159 case CEPH_MDS_LEASE_RENEW:
Sage Weil3d8eb7a2011-11-11 09:48:53 -08003160 if (di->lease_session == session &&
Sage Weil2f2dc052009-10-06 11:31:09 -07003161 di->lease_gen == session->s_cap_gen &&
3162 di->lease_renew_from &&
3163 di->lease_renew_after == 0) {
3164 unsigned long duration =
Nicholas Mc Guire3563dbdd2015-02-06 06:52:17 -05003165 msecs_to_jiffies(le32_to_cpu(h->duration_ms));
Sage Weil2f2dc052009-10-06 11:31:09 -07003166
Sage Weil1e5ea232010-06-04 10:05:40 -07003167 di->lease_seq = seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003168 dentry->d_time = di->lease_renew_from + duration;
3169 di->lease_renew_after = di->lease_renew_from +
3170 (duration >> 1);
3171 di->lease_renew_from = 0;
3172 }
3173 break;
3174 }
3175 spin_unlock(&dentry->d_lock);
3176 dput(dentry);
3177
3178 if (!release)
3179 goto out;
3180
3181release:
3182 /* let's just reuse the same message */
3183 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
3184 ceph_msg_get(msg);
3185 ceph_con_send(&session->s_con, msg);
3186
3187out:
3188 iput(inode);
3189 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003190 return;
3191
3192bad:
3193 pr_err("corrupt lease message\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08003194 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003195}
3196
3197void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
3198 struct inode *inode,
3199 struct dentry *dentry, char action,
3200 u32 seq)
3201{
3202 struct ceph_msg *msg;
3203 struct ceph_mds_lease *lease;
3204 int len = sizeof(*lease) + sizeof(u32);
3205 int dnamelen = 0;
3206
3207 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
3208 inode, dentry, ceph_lease_op_name(action), session->s_mds);
3209 dnamelen = dentry->d_name.len;
3210 len += dnamelen;
3211
Sage Weilb61c2762011-08-09 15:03:46 -07003212 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07003213 if (!msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07003214 return;
3215 lease = msg->front.iov_base;
3216 lease->action = action;
Sage Weil2f2dc052009-10-06 11:31:09 -07003217 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
3218 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
3219 lease->seq = cpu_to_le32(seq);
3220 put_unaligned_le32(dnamelen, lease + 1);
3221 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
3222
3223 /*
3224 * if this is a preemptive lease RELEASE, no need to
3225 * flush request stream, since the actual request will
3226 * soon follow.
3227 */
3228 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
3229
3230 ceph_con_send(&session->s_con, msg);
3231}
3232
3233/*
3234 * Preemptively release a lease we expect to invalidate anyway.
3235 * Pass @inode always, @dentry is optional.
3236 */
3237void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
Sage Weil2f90b852011-07-26 11:28:25 -07003238 struct dentry *dentry)
Sage Weil2f2dc052009-10-06 11:31:09 -07003239{
3240 struct ceph_dentry_info *di;
3241 struct ceph_mds_session *session;
3242 u32 seq;
3243
3244 BUG_ON(inode == NULL);
3245 BUG_ON(dentry == NULL);
Sage Weil2f2dc052009-10-06 11:31:09 -07003246
3247 /* is dentry lease valid? */
3248 spin_lock(&dentry->d_lock);
3249 di = ceph_dentry(dentry);
3250 if (!di || !di->lease_session ||
3251 di->lease_session->s_mds < 0 ||
3252 di->lease_gen != di->lease_session->s_cap_gen ||
3253 !time_before(jiffies, dentry->d_time)) {
3254 dout("lease_release inode %p dentry %p -- "
Sage Weil2f90b852011-07-26 11:28:25 -07003255 "no lease\n",
3256 inode, dentry);
Sage Weil2f2dc052009-10-06 11:31:09 -07003257 spin_unlock(&dentry->d_lock);
3258 return;
3259 }
3260
3261 /* we do have a lease on this dentry; note mds and seq */
3262 session = ceph_get_mds_session(di->lease_session);
3263 seq = di->lease_seq;
3264 __ceph_mdsc_drop_dentry_lease(dentry);
3265 spin_unlock(&dentry->d_lock);
3266
Sage Weil2f90b852011-07-26 11:28:25 -07003267 dout("lease_release inode %p dentry %p to mds%d\n",
3268 inode, dentry, session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003269 ceph_mdsc_lease_send_msg(session, inode, dentry,
3270 CEPH_MDS_LEASE_RELEASE, seq);
3271 ceph_put_mds_session(session);
3272}
3273
3274/*
3275 * drop all leases (and dentry refs) in preparation for umount
3276 */
3277static void drop_leases(struct ceph_mds_client *mdsc)
3278{
3279 int i;
3280
3281 dout("drop_leases\n");
3282 mutex_lock(&mdsc->mutex);
3283 for (i = 0; i < mdsc->max_sessions; i++) {
3284 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
3285 if (!s)
3286 continue;
3287 mutex_unlock(&mdsc->mutex);
3288 mutex_lock(&s->s_mutex);
3289 mutex_unlock(&s->s_mutex);
3290 ceph_put_mds_session(s);
3291 mutex_lock(&mdsc->mutex);
3292 }
3293 mutex_unlock(&mdsc->mutex);
3294}
3295
3296
3297
3298/*
3299 * delayed work -- periodically trim expired leases, renew caps with mds
3300 */
3301static void schedule_delayed(struct ceph_mds_client *mdsc)
3302{
3303 int delay = 5;
3304 unsigned hz = round_jiffies_relative(HZ * delay);
3305 schedule_delayed_work(&mdsc->delayed_work, hz);
3306}
3307
3308static void delayed_work(struct work_struct *work)
3309{
3310 int i;
3311 struct ceph_mds_client *mdsc =
3312 container_of(work, struct ceph_mds_client, delayed_work.work);
3313 int renew_interval;
3314 int renew_caps;
3315
3316 dout("mdsc delayed_work\n");
Sage Weilafcdaea2009-10-14 14:27:38 -07003317 ceph_check_delayed_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003318
3319 mutex_lock(&mdsc->mutex);
3320 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
3321 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
3322 mdsc->last_renew_caps);
3323 if (renew_caps)
3324 mdsc->last_renew_caps = jiffies;
3325
3326 for (i = 0; i < mdsc->max_sessions; i++) {
3327 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
3328 if (s == NULL)
3329 continue;
3330 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
3331 dout("resending session close request for mds%d\n",
3332 s->s_mds);
3333 request_close_session(mdsc, s);
3334 ceph_put_mds_session(s);
3335 continue;
3336 }
3337 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
3338 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
3339 s->s_state = CEPH_MDS_SESSION_HUNG;
3340 pr_info("mds%d hung\n", s->s_mds);
3341 }
3342 }
3343 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
3344 /* this mds is failed or recovering, just wait */
3345 ceph_put_mds_session(s);
3346 continue;
3347 }
3348 mutex_unlock(&mdsc->mutex);
3349
3350 mutex_lock(&s->s_mutex);
3351 if (renew_caps)
3352 send_renew_caps(mdsc, s);
3353 else
3354 ceph_con_keepalive(&s->s_con);
Sage Weilaab53dd2010-03-17 16:30:21 -07003355 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3356 s->s_state == CEPH_MDS_SESSION_HUNG)
Sage Weil3d7ded42010-06-09 16:47:10 -07003357 ceph_send_cap_releases(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07003358 mutex_unlock(&s->s_mutex);
3359 ceph_put_mds_session(s);
3360
3361 mutex_lock(&mdsc->mutex);
3362 }
3363 mutex_unlock(&mdsc->mutex);
3364
3365 schedule_delayed(mdsc);
3366}
3367
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003368int ceph_mdsc_init(struct ceph_fs_client *fsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07003369
Sage Weil2f2dc052009-10-06 11:31:09 -07003370{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003371 struct ceph_mds_client *mdsc;
3372
3373 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
3374 if (!mdsc)
3375 return -ENOMEM;
3376 mdsc->fsc = fsc;
3377 fsc->mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07003378 mutex_init(&mdsc->mutex);
3379 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
majianpengfb3101b2013-06-25 14:48:19 +08003380 if (mdsc->mdsmap == NULL) {
3381 kfree(mdsc);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08003382 return -ENOMEM;
majianpengfb3101b2013-06-25 14:48:19 +08003383 }
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08003384
Sage Weil2f2dc052009-10-06 11:31:09 -07003385 init_completion(&mdsc->safe_umount_waiters);
Sage Weilf3c60c52010-08-11 14:51:23 -07003386 init_waitqueue_head(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003387 INIT_LIST_HEAD(&mdsc->waiting_for_map);
3388 mdsc->sessions = NULL;
Yan, Zheng86d8f672015-01-09 17:00:42 +08003389 atomic_set(&mdsc->num_sessions, 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07003390 mdsc->max_sessions = 0;
3391 mdsc->stopping = 0;
Yan, Zhengaffbc192015-05-05 21:22:13 +08003392 mdsc->last_snap_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003393 init_rwsem(&mdsc->snap_rwsem);
Sage Weila105f002010-02-15 14:37:55 -08003394 mdsc->snap_realms = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07003395 INIT_LIST_HEAD(&mdsc->snap_empty);
3396 spin_lock_init(&mdsc->snap_empty_lock);
3397 mdsc->last_tid = 0;
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08003398 mdsc->oldest_tid = 0;
Sage Weil44ca18f2010-02-15 12:08:46 -08003399 mdsc->request_tree = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07003400 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
3401 mdsc->last_renew_caps = jiffies;
3402 INIT_LIST_HEAD(&mdsc->cap_delay_list);
3403 spin_lock_init(&mdsc->cap_delay_lock);
3404 INIT_LIST_HEAD(&mdsc->snap_flush_list);
3405 spin_lock_init(&mdsc->snap_flush_lock);
3406 mdsc->cap_flush_seq = 0;
3407 INIT_LIST_HEAD(&mdsc->cap_dirty);
Sage Weildb354052011-05-24 11:46:31 -07003408 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
Sage Weil2f2dc052009-10-06 11:31:09 -07003409 mdsc->num_cap_flushing = 0;
3410 spin_lock_init(&mdsc->cap_dirty_lock);
3411 init_waitqueue_head(&mdsc->cap_flushing_wq);
3412 spin_lock_init(&mdsc->dentry_lru_lock);
3413 INIT_LIST_HEAD(&mdsc->dentry_lru);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08003414
Yehuda Sadeh37151662010-06-17 16:16:12 -07003415 ceph_caps_init(mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003416 ceph_adjust_min_caps(mdsc, fsc->min_caps);
Yehuda Sadeh37151662010-06-17 16:16:12 -07003417
Yan, Zheng10183a62015-04-27 15:33:28 +08003418 init_rwsem(&mdsc->pool_perm_rwsem);
3419 mdsc->pool_perm_tree = RB_ROOT;
3420
Sage Weil5f44f142009-11-18 14:52:18 -08003421 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003422}
3423
3424/*
3425 * Wait for safe replies on open mds requests. If we time out, drop
3426 * all requests from the tree to avoid dangling dentry refs.
3427 */
3428static void wait_requests(struct ceph_mds_client *mdsc)
3429{
Ilya Dryomova319bf52015-05-15 12:02:17 +03003430 struct ceph_options *opts = mdsc->fsc->client->options;
Sage Weil2f2dc052009-10-06 11:31:09 -07003431 struct ceph_mds_request *req;
Sage Weil2f2dc052009-10-06 11:31:09 -07003432
3433 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003434 if (__get_oldest_req(mdsc)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003435 mutex_unlock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003436
Sage Weil2f2dc052009-10-06 11:31:09 -07003437 dout("wait_requests waiting for requests\n");
3438 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
Ilya Dryomova319bf52015-05-15 12:02:17 +03003439 ceph_timeout_jiffies(opts->mount_timeout));
Sage Weil2f2dc052009-10-06 11:31:09 -07003440
3441 /* tear down remaining requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08003442 mutex_lock(&mdsc->mutex);
3443 while ((req = __get_oldest_req(mdsc))) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003444 dout("wait_requests timed out on tid %llu\n",
3445 req->r_tid);
Sage Weil44ca18f2010-02-15 12:08:46 -08003446 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07003447 }
3448 }
3449 mutex_unlock(&mdsc->mutex);
3450 dout("wait_requests done\n");
3451}
3452
3453/*
3454 * called before mount is ro, and before dentries are torn down.
3455 * (hmm, does this still race with new lookups?)
3456 */
3457void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
3458{
3459 dout("pre_umount\n");
3460 mdsc->stopping = 1;
3461
3462 drop_leases(mdsc);
Sage Weilafcdaea2009-10-14 14:27:38 -07003463 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003464 wait_requests(mdsc);
Sage Weil17c688c2010-06-21 16:12:26 -07003465
3466 /*
3467 * wait for reply handlers to drop their request refs and
3468 * their inode/dcache refs
3469 */
3470 ceph_msgr_flush();
Sage Weil2f2dc052009-10-06 11:31:09 -07003471}
3472
3473/*
3474 * wait for all write mds requests to flush.
3475 */
3476static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
3477{
Sage Weil80fc7312010-03-16 15:28:54 -07003478 struct ceph_mds_request *req = NULL, *nextreq;
Sage Weil44ca18f2010-02-15 12:08:46 -08003479 struct rb_node *n;
Sage Weil2f2dc052009-10-06 11:31:09 -07003480
3481 mutex_lock(&mdsc->mutex);
3482 dout("wait_unsafe_requests want %lld\n", want_tid);
Sage Weil80fc7312010-03-16 15:28:54 -07003483restart:
Sage Weil44ca18f2010-02-15 12:08:46 -08003484 req = __get_oldest_req(mdsc);
3485 while (req && req->r_tid <= want_tid) {
Sage Weil80fc7312010-03-16 15:28:54 -07003486 /* find next request */
3487 n = rb_next(&req->r_node);
3488 if (n)
3489 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
3490 else
3491 nextreq = NULL;
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08003492 if (req->r_op != CEPH_MDS_OP_SETFILELOCK &&
3493 (req->r_op & CEPH_MDS_OP_WRITE)) {
Sage Weil44ca18f2010-02-15 12:08:46 -08003494 /* write op */
3495 ceph_mdsc_get_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07003496 if (nextreq)
3497 ceph_mdsc_get_request(nextreq);
Sage Weil44ca18f2010-02-15 12:08:46 -08003498 mutex_unlock(&mdsc->mutex);
3499 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3500 req->r_tid, want_tid);
3501 wait_for_completion(&req->r_safe_completion);
3502 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003503 ceph_mdsc_put_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07003504 if (!nextreq)
3505 break; /* next dne before, so we're done! */
3506 if (RB_EMPTY_NODE(&nextreq->r_node)) {
3507 /* next request was removed from tree */
3508 ceph_mdsc_put_request(nextreq);
3509 goto restart;
3510 }
3511 ceph_mdsc_put_request(nextreq); /* won't go away */
Sage Weil44ca18f2010-02-15 12:08:46 -08003512 }
Sage Weil80fc7312010-03-16 15:28:54 -07003513 req = nextreq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003514 }
3515 mutex_unlock(&mdsc->mutex);
3516 dout("wait_unsafe_requests done\n");
3517}
3518
3519void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
3520{
Yan, Zhengaffbc192015-05-05 21:22:13 +08003521 u64 want_tid, want_flush, want_snap;
Sage Weil2f2dc052009-10-06 11:31:09 -07003522
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003523 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
Sage Weil56b7cf92010-05-03 15:22:00 -07003524 return;
3525
Sage Weil2f2dc052009-10-06 11:31:09 -07003526 dout("sync\n");
3527 mutex_lock(&mdsc->mutex);
3528 want_tid = mdsc->last_tid;
Sage Weil2f2dc052009-10-06 11:31:09 -07003529 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003530
Sage Weilafcdaea2009-10-14 14:27:38 -07003531 ceph_flush_dirty_caps(mdsc);
Yan, Zhengd3383a82015-01-08 21:30:12 +08003532 spin_lock(&mdsc->cap_dirty_lock);
3533 want_flush = mdsc->cap_flush_seq;
3534 spin_unlock(&mdsc->cap_dirty_lock);
3535
Yan, Zhengaffbc192015-05-05 21:22:13 +08003536 down_read(&mdsc->snap_rwsem);
3537 want_snap = mdsc->last_snap_seq;
3538 up_read(&mdsc->snap_rwsem);
3539
3540 dout("sync want tid %lld flush_seq %lld snap_seq %lld\n",
3541 want_tid, want_flush, want_snap);
Sage Weil2f2dc052009-10-06 11:31:09 -07003542
3543 wait_unsafe_requests(mdsc, want_tid);
Yan, Zhengaffbc192015-05-05 21:22:13 +08003544 wait_caps_flush(mdsc, want_flush, want_snap);
Sage Weil2f2dc052009-10-06 11:31:09 -07003545}
3546
Sage Weilf3c60c52010-08-11 14:51:23 -07003547/*
3548 * true if all sessions are closed, or we force unmount
3549 */
H Hartley Sweeten7fd7d102011-09-23 13:22:11 -07003550static bool done_closing_sessions(struct ceph_mds_client *mdsc)
Sage Weilf3c60c52010-08-11 14:51:23 -07003551{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003552 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
Sage Weilf3c60c52010-08-11 14:51:23 -07003553 return true;
Yan, Zheng86d8f672015-01-09 17:00:42 +08003554 return atomic_read(&mdsc->num_sessions) == 0;
Sage Weilf3c60c52010-08-11 14:51:23 -07003555}
Sage Weil2f2dc052009-10-06 11:31:09 -07003556
3557/*
3558 * called after sb is ro.
3559 */
3560void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
3561{
Ilya Dryomova319bf52015-05-15 12:02:17 +03003562 struct ceph_options *opts = mdsc->fsc->client->options;
Sage Weil2f2dc052009-10-06 11:31:09 -07003563 struct ceph_mds_session *session;
3564 int i;
Sage Weil2f2dc052009-10-06 11:31:09 -07003565
3566 dout("close_sessions\n");
3567
Sage Weil2f2dc052009-10-06 11:31:09 -07003568 /* close sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07003569 mutex_lock(&mdsc->mutex);
3570 for (i = 0; i < mdsc->max_sessions; i++) {
3571 session = __ceph_lookup_mds_session(mdsc, i);
3572 if (!session)
3573 continue;
Sage Weil2f2dc052009-10-06 11:31:09 -07003574 mutex_unlock(&mdsc->mutex);
Sage Weilf3c60c52010-08-11 14:51:23 -07003575 mutex_lock(&session->s_mutex);
3576 __close_session(mdsc, session);
3577 mutex_unlock(&session->s_mutex);
3578 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003579 mutex_lock(&mdsc->mutex);
3580 }
Sage Weilf3c60c52010-08-11 14:51:23 -07003581 mutex_unlock(&mdsc->mutex);
3582
3583 dout("waiting for sessions to close\n");
3584 wait_event_timeout(mdsc->session_close_wq, done_closing_sessions(mdsc),
Ilya Dryomova319bf52015-05-15 12:02:17 +03003585 ceph_timeout_jiffies(opts->mount_timeout));
Sage Weil2f2dc052009-10-06 11:31:09 -07003586
3587 /* tear down remaining sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07003588 mutex_lock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003589 for (i = 0; i < mdsc->max_sessions; i++) {
3590 if (mdsc->sessions[i]) {
3591 session = get_session(mdsc->sessions[i]);
Sage Weil2600d2d2010-02-22 15:12:16 -08003592 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003593 mutex_unlock(&mdsc->mutex);
3594 mutex_lock(&session->s_mutex);
3595 remove_session_caps(session);
3596 mutex_unlock(&session->s_mutex);
3597 ceph_put_mds_session(session);
3598 mutex_lock(&mdsc->mutex);
3599 }
3600 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003601 WARN_ON(!list_empty(&mdsc->cap_delay_list));
Sage Weil2f2dc052009-10-06 11:31:09 -07003602 mutex_unlock(&mdsc->mutex);
3603
3604 ceph_cleanup_empty_realms(mdsc);
3605
3606 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3607
3608 dout("stopped\n");
3609}
3610
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003611static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07003612{
3613 dout("stop\n");
3614 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3615 if (mdsc->mdsmap)
3616 ceph_mdsmap_destroy(mdsc->mdsmap);
3617 kfree(mdsc->sessions);
Yehuda Sadeh37151662010-06-17 16:16:12 -07003618 ceph_caps_finalize(mdsc);
Yan, Zheng10183a62015-04-27 15:33:28 +08003619 ceph_pool_perm_destroy(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003620}
3621
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003622void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
3623{
3624 struct ceph_mds_client *mdsc = fsc->mdsc;
3625
Sage Weilef550f62011-03-25 13:27:48 -07003626 dout("mdsc_destroy %p\n", mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003627 ceph_mdsc_stop(mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07003628
3629 /* flush out any connection work with references to us */
3630 ceph_msgr_flush();
3631
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003632 fsc->mdsc = NULL;
3633 kfree(mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07003634 dout("mdsc_destroy %p done\n", mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003635}
3636
Sage Weil2f2dc052009-10-06 11:31:09 -07003637
3638/*
3639 * handle mds map update.
3640 */
3641void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3642{
3643 u32 epoch;
3644 u32 maplen;
3645 void *p = msg->front.iov_base;
3646 void *end = p + msg->front.iov_len;
3647 struct ceph_mdsmap *newmap, *oldmap;
3648 struct ceph_fsid fsid;
3649 int err = -EINVAL;
3650
3651 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
3652 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003653 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
Sage Weil07433042009-11-18 16:50:41 -08003654 return;
Sage Weilc89136e2009-10-14 09:59:09 -07003655 epoch = ceph_decode_32(&p);
3656 maplen = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07003657 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
3658
3659 /* do we need it? */
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003660 ceph_monc_got_mdsmap(&mdsc->fsc->client->monc, epoch);
Sage Weil2f2dc052009-10-06 11:31:09 -07003661 mutex_lock(&mdsc->mutex);
3662 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
3663 dout("handle_map epoch %u <= our %u\n",
3664 epoch, mdsc->mdsmap->m_epoch);
3665 mutex_unlock(&mdsc->mutex);
3666 return;
3667 }
3668
3669 newmap = ceph_mdsmap_decode(&p, end);
3670 if (IS_ERR(newmap)) {
3671 err = PTR_ERR(newmap);
3672 goto bad_unlock;
3673 }
3674
3675 /* swap into place */
3676 if (mdsc->mdsmap) {
3677 oldmap = mdsc->mdsmap;
3678 mdsc->mdsmap = newmap;
3679 check_new_map(mdsc, newmap, oldmap);
3680 ceph_mdsmap_destroy(oldmap);
3681 } else {
3682 mdsc->mdsmap = newmap; /* first mds map */
3683 }
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003684 mdsc->fsc->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
Sage Weil2f2dc052009-10-06 11:31:09 -07003685
3686 __wake_requests(mdsc, &mdsc->waiting_for_map);
3687
3688 mutex_unlock(&mdsc->mutex);
3689 schedule_delayed(mdsc);
3690 return;
3691
3692bad_unlock:
3693 mutex_unlock(&mdsc->mutex);
3694bad:
3695 pr_err("error decoding mdsmap %d\n", err);
3696 return;
3697}
3698
3699static struct ceph_connection *con_get(struct ceph_connection *con)
3700{
3701 struct ceph_mds_session *s = con->private;
3702
3703 if (get_session(s)) {
Sage Weil2600d2d2010-02-22 15:12:16 -08003704 dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -07003705 return con;
3706 }
3707 dout("mdsc con_get %p FAIL\n", s);
3708 return NULL;
3709}
3710
3711static void con_put(struct ceph_connection *con)
3712{
3713 struct ceph_mds_session *s = con->private;
3714
Henry C Chang7d8e18a2011-05-11 10:29:52 +00003715 dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref) - 1);
Sage Weil2f2dc052009-10-06 11:31:09 -07003716 ceph_put_mds_session(s);
3717}
3718
3719/*
3720 * if the client is unresponsive for long enough, the mds will kill
3721 * the session entirely.
3722 */
3723static void peer_reset(struct ceph_connection *con)
3724{
3725 struct ceph_mds_session *s = con->private;
Sage Weil7e70f0e2010-03-18 13:59:12 -07003726 struct ceph_mds_client *mdsc = s->s_mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07003727
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07003728 pr_warn("mds%d closed our session\n", s->s_mds);
Sage Weil7e70f0e2010-03-18 13:59:12 -07003729 send_mds_reconnect(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07003730}
3731
3732static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
3733{
3734 struct ceph_mds_session *s = con->private;
3735 struct ceph_mds_client *mdsc = s->s_mdsc;
3736 int type = le16_to_cpu(msg->hdr.type);
3737
Sage Weil2600d2d2010-02-22 15:12:16 -08003738 mutex_lock(&mdsc->mutex);
3739 if (__verify_registered_session(mdsc, s) < 0) {
3740 mutex_unlock(&mdsc->mutex);
3741 goto out;
3742 }
3743 mutex_unlock(&mdsc->mutex);
3744
Sage Weil2f2dc052009-10-06 11:31:09 -07003745 switch (type) {
3746 case CEPH_MSG_MDS_MAP:
3747 ceph_mdsc_handle_map(mdsc, msg);
3748 break;
3749 case CEPH_MSG_CLIENT_SESSION:
3750 handle_session(s, msg);
3751 break;
3752 case CEPH_MSG_CLIENT_REPLY:
3753 handle_reply(s, msg);
3754 break;
3755 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
Sage Weil2600d2d2010-02-22 15:12:16 -08003756 handle_forward(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003757 break;
3758 case CEPH_MSG_CLIENT_CAPS:
3759 ceph_handle_caps(s, msg);
3760 break;
3761 case CEPH_MSG_CLIENT_SNAP:
Sage Weil2600d2d2010-02-22 15:12:16 -08003762 ceph_handle_snap(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003763 break;
3764 case CEPH_MSG_CLIENT_LEASE:
Sage Weil2600d2d2010-02-22 15:12:16 -08003765 handle_lease(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003766 break;
3767
3768 default:
3769 pr_err("received unknown message type %d %s\n", type,
3770 ceph_msg_type_name(type));
3771 }
Sage Weil2600d2d2010-02-22 15:12:16 -08003772out:
Sage Weil2f2dc052009-10-06 11:31:09 -07003773 ceph_msg_put(msg);
3774}
3775
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003776/*
3777 * authentication
3778 */
Alex Eldera3530df2012-05-16 15:16:39 -05003779
3780/*
3781 * Note: returned pointer is the address of a structure that's
3782 * managed separately. Caller must *not* attempt to free it.
3783 */
3784static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05003785 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003786{
3787 struct ceph_mds_session *s = con->private;
3788 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003789 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05003790 struct ceph_auth_handshake *auth = &s->s_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003791
Alex Elder74f18692012-05-16 15:16:39 -05003792 if (force_new && auth->authorizer) {
Sage Weil27859f92013-03-25 10:26:14 -07003793 ceph_auth_destroy_authorizer(ac, auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05003794 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003795 }
Sage Weil27859f92013-03-25 10:26:14 -07003796 if (!auth->authorizer) {
3797 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
3798 auth);
Sage Weil0bed9b52013-03-25 10:26:01 -07003799 if (ret)
3800 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07003801 } else {
3802 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
3803 auth);
Alex Eldera2556512012-05-16 15:16:39 -05003804 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05003805 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003806 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003807 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05003808
Alex Eldera3530df2012-05-16 15:16:39 -05003809 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003810}
3811
3812
3813static int verify_authorizer_reply(struct ceph_connection *con, int len)
3814{
3815 struct ceph_mds_session *s = con->private;
3816 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003817 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003818
Sage Weil27859f92013-03-25 10:26:14 -07003819 return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003820}
3821
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003822static int invalidate_authorizer(struct ceph_connection *con)
3823{
3824 struct ceph_mds_session *s = con->private;
3825 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003826 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003827
Sage Weil27859f92013-03-25 10:26:14 -07003828 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003829
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003830 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003831}
3832
Alex Elder53ded492013-03-01 18:00:14 -06003833static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
3834 struct ceph_msg_header *hdr, int *skip)
3835{
3836 struct ceph_msg *msg;
3837 int type = (int) le16_to_cpu(hdr->type);
3838 int front_len = (int) le32_to_cpu(hdr->front_len);
3839
3840 if (con->in_msg)
3841 return con->in_msg;
3842
3843 *skip = 0;
3844 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
3845 if (!msg) {
3846 pr_err("unable to allocate msg type %d len %d\n",
3847 type, front_len);
3848 return NULL;
3849 }
Alex Elder53ded492013-03-01 18:00:14 -06003850
3851 return msg;
3852}
3853
Yan, Zheng33d07332014-11-04 16:33:37 +08003854static int sign_message(struct ceph_connection *con, struct ceph_msg *msg)
3855{
3856 struct ceph_mds_session *s = con->private;
3857 struct ceph_auth_handshake *auth = &s->s_auth;
3858 return ceph_auth_sign_message(auth, msg);
3859}
3860
3861static int check_message_signature(struct ceph_connection *con, struct ceph_msg *msg)
3862{
3863 struct ceph_mds_session *s = con->private;
3864 struct ceph_auth_handshake *auth = &s->s_auth;
3865 return ceph_auth_check_message_signature(auth, msg);
3866}
3867
Tobias Klauser9e327892010-05-20 10:40:19 +02003868static const struct ceph_connection_operations mds_con_ops = {
Sage Weil2f2dc052009-10-06 11:31:09 -07003869 .get = con_get,
3870 .put = con_put,
3871 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003872 .get_authorizer = get_authorizer,
3873 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003874 .invalidate_authorizer = invalidate_authorizer,
Sage Weil2f2dc052009-10-06 11:31:09 -07003875 .peer_reset = peer_reset,
Alex Elder53ded492013-03-01 18:00:14 -06003876 .alloc_msg = mds_alloc_msg,
Yan, Zheng33d07332014-11-04 16:33:37 +08003877 .sign_message = sign_message,
3878 .check_message_signature = check_message_signature,
Sage Weil2f2dc052009-10-06 11:31:09 -07003879};
3880
Sage Weil2f2dc052009-10-06 11:31:09 -07003881/* eof */