blob: 88010f9a254da69d08c0801674afa42c627a9ea0 [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weil2f2dc052009-10-06 11:31:09 -07002
Sage Weil496e5952010-09-22 19:57:10 -07003#include <linux/fs.h>
Sage Weil2f2dc052009-10-06 11:31:09 -07004#include <linux/wait.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09005#include <linux/slab.h>
Yan, Zheng54008392014-03-29 13:41:15 +08006#include <linux/gfp.h>
Sage Weil2f2dc052009-10-06 11:31:09 -07007#include <linux/sched.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07008#include <linux/debugfs.h>
9#include <linux/seq_file.h>
John Spraydbd0c8b2014-09-09 19:26:01 +010010#include <linux/utsname.h>
Sage Weil2f2dc052009-10-06 11:31:09 -070011
Sage Weil2f2dc052009-10-06 11:31:09 -070012#include "super.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070013#include "mds_client.h"
14
Sage Weil1fe60e52012-07-30 16:23:22 -070015#include <linux/ceph/ceph_features.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070016#include <linux/ceph/messenger.h>
17#include <linux/ceph/decode.h>
18#include <linux/ceph/pagelist.h>
19#include <linux/ceph/auth.h>
20#include <linux/ceph/debugfs.h>
Sage Weil2f2dc052009-10-06 11:31:09 -070021
22/*
23 * A cluster of MDS (metadata server) daemons is responsible for
24 * managing the file system namespace (the directory hierarchy and
25 * inodes) and for coordinating shared access to storage. Metadata is
26 * partitioning hierarchically across a number of servers, and that
27 * partition varies over time as the cluster adjusts the distribution
28 * in order to balance load.
29 *
30 * The MDS client is primarily responsible to managing synchronous
31 * metadata requests for operations like open, unlink, and so forth.
32 * If there is a MDS failure, we find out about it when we (possibly
33 * request and) receive a new MDS map, and can resubmit affected
34 * requests.
35 *
36 * For the most part, though, we take advantage of a lossless
37 * communications channel to the MDS, and do not need to worry about
38 * timing out or resubmitting requests.
39 *
40 * We maintain a stateful "session" with each MDS we interact with.
41 * Within each session, we sent periodic heartbeat messages to ensure
42 * any capabilities or leases we have been issues remain valid. If
43 * the session times out and goes stale, our leases and capabilities
44 * are no longer valid.
45 */
46
Sage Weil20cb34a2010-05-12 15:21:32 -070047struct ceph_reconnect_state {
Yan, Zheng44c99752013-09-22 10:28:10 +080048 int nr_caps;
Sage Weil20cb34a2010-05-12 15:21:32 -070049 struct ceph_pagelist *pagelist;
50 bool flock;
51};
52
Sage Weil2f2dc052009-10-06 11:31:09 -070053static void __wake_requests(struct ceph_mds_client *mdsc,
54 struct list_head *head);
55
Tobias Klauser9e327892010-05-20 10:40:19 +020056static const struct ceph_connection_operations mds_con_ops;
Sage Weil2f2dc052009-10-06 11:31:09 -070057
58
59/*
60 * mds reply parsing
61 */
62
63/*
64 * parse individual inode info
65 */
66static int parse_reply_info_in(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -080067 struct ceph_mds_reply_info_in *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +020068 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -070069{
70 int err = -EIO;
71
72 info->in = *p;
73 *p += sizeof(struct ceph_mds_reply_inode) +
74 sizeof(*info->in->fragtree.splits) *
75 le32_to_cpu(info->in->fragtree.nsplits);
76
77 ceph_decode_32_safe(p, end, info->symlink_len, bad);
78 ceph_decode_need(p, end, info->symlink_len, bad);
79 info->symlink = *p;
80 *p += info->symlink_len;
81
Sage Weil14303d22010-12-14 17:37:52 -080082 if (features & CEPH_FEATURE_DIRLAYOUTHASH)
83 ceph_decode_copy_safe(p, end, &info->dir_layout,
84 sizeof(info->dir_layout), bad);
85 else
86 memset(&info->dir_layout, 0, sizeof(info->dir_layout));
87
Sage Weil2f2dc052009-10-06 11:31:09 -070088 ceph_decode_32_safe(p, end, info->xattr_len, bad);
89 ceph_decode_need(p, end, info->xattr_len, bad);
90 info->xattr_data = *p;
91 *p += info->xattr_len;
Yan, Zhengfb01d1f2014-11-14 21:29:55 +080092
93 if (features & CEPH_FEATURE_MDS_INLINE_DATA) {
94 ceph_decode_64_safe(p, end, info->inline_version, bad);
95 ceph_decode_32_safe(p, end, info->inline_len, bad);
96 ceph_decode_need(p, end, info->inline_len, bad);
97 info->inline_data = *p;
98 *p += info->inline_len;
99 } else
100 info->inline_version = CEPH_INLINE_NONE;
101
Sage Weil2f2dc052009-10-06 11:31:09 -0700102 return 0;
103bad:
104 return err;
105}
106
107/*
108 * parse a normal reply, which may contain a (dir+)dentry and/or a
109 * target inode.
110 */
111static int parse_reply_info_trace(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800112 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200113 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700114{
115 int err;
116
117 if (info->head->is_dentry) {
Sage Weil14303d22010-12-14 17:37:52 -0800118 err = parse_reply_info_in(p, end, &info->diri, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700119 if (err < 0)
120 goto out_bad;
121
122 if (unlikely(*p + sizeof(*info->dirfrag) > end))
123 goto bad;
124 info->dirfrag = *p;
125 *p += sizeof(*info->dirfrag) +
126 sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
127 if (unlikely(*p > end))
128 goto bad;
129
130 ceph_decode_32_safe(p, end, info->dname_len, bad);
131 ceph_decode_need(p, end, info->dname_len, bad);
132 info->dname = *p;
133 *p += info->dname_len;
134 info->dlease = *p;
135 *p += sizeof(*info->dlease);
136 }
137
138 if (info->head->is_target) {
Sage Weil14303d22010-12-14 17:37:52 -0800139 err = parse_reply_info_in(p, end, &info->targeti, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700140 if (err < 0)
141 goto out_bad;
142 }
143
144 if (unlikely(*p != end))
145 goto bad;
146 return 0;
147
148bad:
149 err = -EIO;
150out_bad:
151 pr_err("problem parsing mds trace %d\n", err);
152 return err;
153}
154
155/*
156 * parse readdir results
157 */
158static int parse_reply_info_dir(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800159 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200160 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700161{
162 u32 num, i = 0;
163 int err;
164
165 info->dir_dir = *p;
166 if (*p + sizeof(*info->dir_dir) > end)
167 goto bad;
168 *p += sizeof(*info->dir_dir) +
169 sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
170 if (*p > end)
171 goto bad;
172
173 ceph_decode_need(p, end, sizeof(num) + 2, bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700174 num = ceph_decode_32(p);
175 info->dir_end = ceph_decode_8(p);
176 info->dir_complete = ceph_decode_8(p);
Sage Weil2f2dc052009-10-06 11:31:09 -0700177 if (num == 0)
178 goto done;
179
Yan, Zheng54008392014-03-29 13:41:15 +0800180 BUG_ON(!info->dir_in);
Sage Weil2f2dc052009-10-06 11:31:09 -0700181 info->dir_dname = (void *)(info->dir_in + num);
182 info->dir_dname_len = (void *)(info->dir_dname + num);
183 info->dir_dlease = (void *)(info->dir_dname_len + num);
Yan, Zheng54008392014-03-29 13:41:15 +0800184 if ((unsigned long)(info->dir_dlease + num) >
185 (unsigned long)info->dir_in + info->dir_buf_size) {
186 pr_err("dir contents are larger than expected\n");
187 WARN_ON(1);
188 goto bad;
189 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700190
Yan, Zheng54008392014-03-29 13:41:15 +0800191 info->dir_nr = num;
Sage Weil2f2dc052009-10-06 11:31:09 -0700192 while (num) {
193 /* dentry */
194 ceph_decode_need(p, end, sizeof(u32)*2, bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700195 info->dir_dname_len[i] = ceph_decode_32(p);
Sage Weil2f2dc052009-10-06 11:31:09 -0700196 ceph_decode_need(p, end, info->dir_dname_len[i], bad);
197 info->dir_dname[i] = *p;
198 *p += info->dir_dname_len[i];
199 dout("parsed dir dname '%.*s'\n", info->dir_dname_len[i],
200 info->dir_dname[i]);
201 info->dir_dlease[i] = *p;
202 *p += sizeof(struct ceph_mds_reply_lease);
203
204 /* inode */
Sage Weil14303d22010-12-14 17:37:52 -0800205 err = parse_reply_info_in(p, end, &info->dir_in[i], features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700206 if (err < 0)
207 goto out_bad;
208 i++;
209 num--;
210 }
211
212done:
213 if (*p != end)
214 goto bad;
215 return 0;
216
217bad:
218 err = -EIO;
219out_bad:
220 pr_err("problem parsing dir contents %d\n", err);
221 return err;
222}
223
224/*
Herb Shiu25933ab2010-12-01 14:14:38 -0800225 * parse fcntl F_GETLK results
226 */
227static int parse_reply_info_filelock(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800228 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200229 u64 features)
Herb Shiu25933ab2010-12-01 14:14:38 -0800230{
231 if (*p + sizeof(*info->filelock_reply) > end)
232 goto bad;
233
234 info->filelock_reply = *p;
235 *p += sizeof(*info->filelock_reply);
236
237 if (unlikely(*p != end))
238 goto bad;
239 return 0;
240
241bad:
242 return -EIO;
243}
244
245/*
Sam Lang6e8575f2012-12-28 09:56:46 -0800246 * parse create results
247 */
248static int parse_reply_info_create(void **p, void *end,
249 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200250 u64 features)
Sam Lang6e8575f2012-12-28 09:56:46 -0800251{
252 if (features & CEPH_FEATURE_REPLY_CREATE_INODE) {
253 if (*p == end) {
254 info->has_create_ino = false;
255 } else {
256 info->has_create_ino = true;
257 info->ino = ceph_decode_64(p);
258 }
259 }
260
261 if (unlikely(*p != end))
262 goto bad;
263 return 0;
264
265bad:
266 return -EIO;
267}
268
269/*
Herb Shiu25933ab2010-12-01 14:14:38 -0800270 * parse extra results
271 */
272static int parse_reply_info_extra(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800273 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200274 u64 features)
Herb Shiu25933ab2010-12-01 14:14:38 -0800275{
276 if (info->head->op == CEPH_MDS_OP_GETFILELOCK)
Sage Weil14303d22010-12-14 17:37:52 -0800277 return parse_reply_info_filelock(p, end, info, features);
Yan, Zheng8a034492013-02-21 13:43:55 +0800278 else if (info->head->op == CEPH_MDS_OP_READDIR ||
279 info->head->op == CEPH_MDS_OP_LSSNAP)
Sage Weil14303d22010-12-14 17:37:52 -0800280 return parse_reply_info_dir(p, end, info, features);
Sam Lang6e8575f2012-12-28 09:56:46 -0800281 else if (info->head->op == CEPH_MDS_OP_CREATE)
282 return parse_reply_info_create(p, end, info, features);
283 else
284 return -EIO;
Herb Shiu25933ab2010-12-01 14:14:38 -0800285}
286
287/*
Sage Weil2f2dc052009-10-06 11:31:09 -0700288 * parse entire mds reply
289 */
290static int parse_reply_info(struct ceph_msg *msg,
Sage Weil14303d22010-12-14 17:37:52 -0800291 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200292 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700293{
294 void *p, *end;
295 u32 len;
296 int err;
297
298 info->head = msg->front.iov_base;
299 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
300 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
301
302 /* trace */
303 ceph_decode_32_safe(&p, end, len, bad);
304 if (len > 0) {
Xi Wang32852a82012-01-14 22:20:59 -0500305 ceph_decode_need(&p, end, len, bad);
Sage Weil14303d22010-12-14 17:37:52 -0800306 err = parse_reply_info_trace(&p, p+len, info, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700307 if (err < 0)
308 goto out_bad;
309 }
310
Herb Shiu25933ab2010-12-01 14:14:38 -0800311 /* extra */
Sage Weil2f2dc052009-10-06 11:31:09 -0700312 ceph_decode_32_safe(&p, end, len, bad);
313 if (len > 0) {
Xi Wang32852a82012-01-14 22:20:59 -0500314 ceph_decode_need(&p, end, len, bad);
Sage Weil14303d22010-12-14 17:37:52 -0800315 err = parse_reply_info_extra(&p, p+len, info, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700316 if (err < 0)
317 goto out_bad;
318 }
319
320 /* snap blob */
321 ceph_decode_32_safe(&p, end, len, bad);
322 info->snapblob_len = len;
323 info->snapblob = p;
324 p += len;
325
326 if (p != end)
327 goto bad;
328 return 0;
329
330bad:
331 err = -EIO;
332out_bad:
333 pr_err("mds parse_reply err %d\n", err);
334 return err;
335}
336
337static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
338{
Yan, Zheng54008392014-03-29 13:41:15 +0800339 if (!info->dir_in)
340 return;
341 free_pages((unsigned long)info->dir_in, get_order(info->dir_buf_size));
Sage Weil2f2dc052009-10-06 11:31:09 -0700342}
343
344
345/*
346 * sessions
347 */
John Spraya687eca2014-09-19 13:51:08 +0100348const char *ceph_session_state_name(int s)
Sage Weil2f2dc052009-10-06 11:31:09 -0700349{
350 switch (s) {
351 case CEPH_MDS_SESSION_NEW: return "new";
352 case CEPH_MDS_SESSION_OPENING: return "opening";
353 case CEPH_MDS_SESSION_OPEN: return "open";
354 case CEPH_MDS_SESSION_HUNG: return "hung";
355 case CEPH_MDS_SESSION_CLOSING: return "closing";
Sage Weil44ca18f2010-02-15 12:08:46 -0800356 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
Sage Weil2f2dc052009-10-06 11:31:09 -0700357 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
358 default: return "???";
359 }
360}
361
362static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
363{
364 if (atomic_inc_not_zero(&s->s_ref)) {
365 dout("mdsc get_session %p %d -> %d\n", s,
366 atomic_read(&s->s_ref)-1, atomic_read(&s->s_ref));
367 return s;
368 } else {
369 dout("mdsc get_session %p 0 -- FAIL", s);
370 return NULL;
371 }
372}
373
374void ceph_put_mds_session(struct ceph_mds_session *s)
375{
376 dout("mdsc put_session %p %d -> %d\n", s,
377 atomic_read(&s->s_ref), atomic_read(&s->s_ref)-1);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800378 if (atomic_dec_and_test(&s->s_ref)) {
Alex Elder6c4a1912012-05-16 15:16:38 -0500379 if (s->s_auth.authorizer)
Sage Weil27859f92013-03-25 10:26:14 -0700380 ceph_auth_destroy_authorizer(
381 s->s_mdsc->fsc->client->monc.auth,
382 s->s_auth.authorizer);
Sage Weil2f2dc052009-10-06 11:31:09 -0700383 kfree(s);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800384 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700385}
386
387/*
388 * called under mdsc->mutex
389 */
390struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
391 int mds)
392{
393 struct ceph_mds_session *session;
394
395 if (mds >= mdsc->max_sessions || mdsc->sessions[mds] == NULL)
396 return NULL;
397 session = mdsc->sessions[mds];
398 dout("lookup_mds_session %p %d\n", session,
399 atomic_read(&session->s_ref));
400 get_session(session);
401 return session;
402}
403
404static bool __have_session(struct ceph_mds_client *mdsc, int mds)
405{
406 if (mds >= mdsc->max_sessions)
407 return false;
408 return mdsc->sessions[mds];
409}
410
Sage Weil2600d2d2010-02-22 15:12:16 -0800411static int __verify_registered_session(struct ceph_mds_client *mdsc,
412 struct ceph_mds_session *s)
413{
414 if (s->s_mds >= mdsc->max_sessions ||
415 mdsc->sessions[s->s_mds] != s)
416 return -ENOENT;
417 return 0;
418}
419
Sage Weil2f2dc052009-10-06 11:31:09 -0700420/*
421 * create+register a new session for given mds.
422 * called under mdsc->mutex.
423 */
424static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
425 int mds)
426{
427 struct ceph_mds_session *s;
428
Nathaniel Yazdanic338c072013-08-04 21:04:30 -0700429 if (mds >= mdsc->mdsmap->m_max_mds)
430 return ERR_PTR(-EINVAL);
431
Sage Weil2f2dc052009-10-06 11:31:09 -0700432 s = kzalloc(sizeof(*s), GFP_NOFS);
Dan Carpenter4736b002010-03-20 15:30:16 +0300433 if (!s)
434 return ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -0700435 s->s_mdsc = mdsc;
436 s->s_mds = mds;
437 s->s_state = CEPH_MDS_SESSION_NEW;
438 s->s_ttl = 0;
439 s->s_seq = 0;
440 mutex_init(&s->s_mutex);
441
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700442 ceph_con_init(&s->s_con, s, &mds_con_ops, &mdsc->fsc->client->msgr);
Sage Weil2f2dc052009-10-06 11:31:09 -0700443
Alex Elderd8fb02a2012-01-12 17:48:10 -0800444 spin_lock_init(&s->s_gen_ttl_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700445 s->s_cap_gen = 0;
Alex Elder1ce208a2012-01-12 17:48:11 -0800446 s->s_cap_ttl = jiffies - 1;
Alex Elderd8fb02a2012-01-12 17:48:10 -0800447
448 spin_lock_init(&s->s_cap_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700449 s->s_renew_requested = 0;
450 s->s_renew_seq = 0;
451 INIT_LIST_HEAD(&s->s_caps);
452 s->s_nr_caps = 0;
Sage Weil5dacf092009-12-21 20:40:34 -0800453 s->s_trim_caps = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -0700454 atomic_set(&s->s_ref, 1);
455 INIT_LIST_HEAD(&s->s_waiting);
456 INIT_LIST_HEAD(&s->s_unsafe);
457 s->s_num_cap_releases = 0;
Yan, Zheng99a9c272013-09-22 11:08:14 +0800458 s->s_cap_reconnect = 0;
Sage Weil7c1332b2010-02-16 11:39:45 -0800459 s->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700460 INIT_LIST_HEAD(&s->s_cap_releases);
461 INIT_LIST_HEAD(&s->s_cap_releases_done);
462 INIT_LIST_HEAD(&s->s_cap_flushing);
463 INIT_LIST_HEAD(&s->s_cap_snaps_flushing);
464
465 dout("register_session mds%d\n", mds);
466 if (mds >= mdsc->max_sessions) {
467 int newmax = 1 << get_count_order(mds+1);
468 struct ceph_mds_session **sa;
469
470 dout("register_session realloc to %d\n", newmax);
471 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
472 if (sa == NULL)
Sage Weil42ce56e2009-11-18 11:22:36 -0800473 goto fail_realloc;
Sage Weil2f2dc052009-10-06 11:31:09 -0700474 if (mdsc->sessions) {
475 memcpy(sa, mdsc->sessions,
476 mdsc->max_sessions * sizeof(void *));
477 kfree(mdsc->sessions);
478 }
479 mdsc->sessions = sa;
480 mdsc->max_sessions = newmax;
481 }
482 mdsc->sessions[mds] = s;
Yan, Zheng86d8f672015-01-09 17:00:42 +0800483 atomic_inc(&mdsc->num_sessions);
Sage Weil2f2dc052009-10-06 11:31:09 -0700484 atomic_inc(&s->s_ref); /* one ref to sessions[], one to caller */
Sage Weil42ce56e2009-11-18 11:22:36 -0800485
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700486 ceph_con_open(&s->s_con, CEPH_ENTITY_TYPE_MDS, mds,
487 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
Sage Weil42ce56e2009-11-18 11:22:36 -0800488
Sage Weil2f2dc052009-10-06 11:31:09 -0700489 return s;
Sage Weil42ce56e2009-11-18 11:22:36 -0800490
491fail_realloc:
492 kfree(s);
493 return ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -0700494}
495
496/*
497 * called under mdsc->mutex
498 */
Sage Weil2600d2d2010-02-22 15:12:16 -0800499static void __unregister_session(struct ceph_mds_client *mdsc,
Sage Weil42ce56e2009-11-18 11:22:36 -0800500 struct ceph_mds_session *s)
Sage Weil2f2dc052009-10-06 11:31:09 -0700501{
Sage Weil2600d2d2010-02-22 15:12:16 -0800502 dout("__unregister_session mds%d %p\n", s->s_mds, s);
503 BUG_ON(mdsc->sessions[s->s_mds] != s);
Sage Weil42ce56e2009-11-18 11:22:36 -0800504 mdsc->sessions[s->s_mds] = NULL;
505 ceph_con_close(&s->s_con);
506 ceph_put_mds_session(s);
Yan, Zheng86d8f672015-01-09 17:00:42 +0800507 atomic_dec(&mdsc->num_sessions);
Sage Weil2f2dc052009-10-06 11:31:09 -0700508}
509
510/*
511 * drop session refs in request.
512 *
513 * should be last request ref, or hold mdsc->mutex
514 */
515static void put_request_session(struct ceph_mds_request *req)
516{
517 if (req->r_session) {
518 ceph_put_mds_session(req->r_session);
519 req->r_session = NULL;
520 }
521}
522
Sage Weil153c8e62009-12-07 12:31:09 -0800523void ceph_mdsc_release_request(struct kref *kref)
Sage Weil2f2dc052009-10-06 11:31:09 -0700524{
Sage Weil153c8e62009-12-07 12:31:09 -0800525 struct ceph_mds_request *req = container_of(kref,
526 struct ceph_mds_request,
527 r_kref);
Yan, Zheng54008392014-03-29 13:41:15 +0800528 destroy_reply_info(&req->r_reply_info);
Sage Weil153c8e62009-12-07 12:31:09 -0800529 if (req->r_request)
530 ceph_msg_put(req->r_request);
Yan, Zheng54008392014-03-29 13:41:15 +0800531 if (req->r_reply)
Sage Weil153c8e62009-12-07 12:31:09 -0800532 ceph_msg_put(req->r_reply);
Sage Weil153c8e62009-12-07 12:31:09 -0800533 if (req->r_inode) {
Sage Weil41b02e12011-07-26 11:31:14 -0700534 ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
Sage Weil153c8e62009-12-07 12:31:09 -0800535 iput(req->r_inode);
536 }
537 if (req->r_locked_dir)
Sage Weil41b02e12011-07-26 11:31:14 -0700538 ceph_put_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
SF Markus Elfringe96a6502014-11-02 15:20:59 +0100539 iput(req->r_target_inode);
Sage Weil153c8e62009-12-07 12:31:09 -0800540 if (req->r_dentry)
541 dput(req->r_dentry);
Sage Weil844d87c2013-02-05 13:40:09 -0800542 if (req->r_old_dentry)
543 dput(req->r_old_dentry);
544 if (req->r_old_dentry_dir) {
Sage Weil41b02e12011-07-26 11:31:14 -0700545 /*
546 * track (and drop pins for) r_old_dentry_dir
547 * separately, since r_old_dentry's d_parent may have
548 * changed between the dir mutex being dropped and
549 * this request being freed.
550 */
551 ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir),
552 CEPH_CAP_PIN);
Sage Weil41b02e12011-07-26 11:31:14 -0700553 iput(req->r_old_dentry_dir);
Sage Weil153c8e62009-12-07 12:31:09 -0800554 }
555 kfree(req->r_path1);
556 kfree(req->r_path2);
Yan, Zheng25e6bae2014-09-16 19:15:28 +0800557 if (req->r_pagelist)
558 ceph_pagelist_release(req->r_pagelist);
Sage Weil153c8e62009-12-07 12:31:09 -0800559 put_request_session(req);
Yehuda Sadeh37151662010-06-17 16:16:12 -0700560 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
Sage Weil153c8e62009-12-07 12:31:09 -0800561 kfree(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700562}
563
564/*
565 * lookup session, bump ref if found.
566 *
567 * called under mdsc->mutex.
568 */
569static struct ceph_mds_request *__lookup_request(struct ceph_mds_client *mdsc,
570 u64 tid)
571{
572 struct ceph_mds_request *req;
Sage Weil44ca18f2010-02-15 12:08:46 -0800573 struct rb_node *n = mdsc->request_tree.rb_node;
574
575 while (n) {
576 req = rb_entry(n, struct ceph_mds_request, r_node);
577 if (tid < req->r_tid)
578 n = n->rb_left;
579 else if (tid > req->r_tid)
580 n = n->rb_right;
581 else {
582 ceph_mdsc_get_request(req);
583 return req;
584 }
585 }
586 return NULL;
587}
588
589static void __insert_request(struct ceph_mds_client *mdsc,
590 struct ceph_mds_request *new)
591{
592 struct rb_node **p = &mdsc->request_tree.rb_node;
593 struct rb_node *parent = NULL;
594 struct ceph_mds_request *req = NULL;
595
596 while (*p) {
597 parent = *p;
598 req = rb_entry(parent, struct ceph_mds_request, r_node);
599 if (new->r_tid < req->r_tid)
600 p = &(*p)->rb_left;
601 else if (new->r_tid > req->r_tid)
602 p = &(*p)->rb_right;
603 else
604 BUG();
605 }
606
607 rb_link_node(&new->r_node, parent, p);
608 rb_insert_color(&new->r_node, &mdsc->request_tree);
Sage Weil2f2dc052009-10-06 11:31:09 -0700609}
610
611/*
612 * Register an in-flight request, and assign a tid. Link to directory
613 * are modifying (if any).
614 *
615 * Called under mdsc->mutex.
616 */
617static void __register_request(struct ceph_mds_client *mdsc,
618 struct ceph_mds_request *req,
619 struct inode *dir)
620{
621 req->r_tid = ++mdsc->last_tid;
622 if (req->r_num_caps)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700623 ceph_reserve_caps(mdsc, &req->r_caps_reservation,
624 req->r_num_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -0700625 dout("__register_request %p tid %lld\n", req, req->r_tid);
626 ceph_mdsc_get_request(req);
Sage Weil44ca18f2010-02-15 12:08:46 -0800627 __insert_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700628
Sage Weilcb4276c2010-11-08 07:28:52 -0800629 req->r_uid = current_fsuid();
630 req->r_gid = current_fsgid();
631
Sage Weil2f2dc052009-10-06 11:31:09 -0700632 if (dir) {
633 struct ceph_inode_info *ci = ceph_inode(dir);
634
Sage Weil3b663782011-05-18 16:12:12 -0700635 ihold(dir);
Sage Weil2f2dc052009-10-06 11:31:09 -0700636 spin_lock(&ci->i_unsafe_lock);
637 req->r_unsafe_dir = dir;
638 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
639 spin_unlock(&ci->i_unsafe_lock);
640 }
641}
642
643static void __unregister_request(struct ceph_mds_client *mdsc,
644 struct ceph_mds_request *req)
645{
646 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
Sage Weil44ca18f2010-02-15 12:08:46 -0800647 rb_erase(&req->r_node, &mdsc->request_tree);
Sage Weil80fc7312010-03-16 15:28:54 -0700648 RB_CLEAR_NODE(&req->r_node);
Sage Weil2f2dc052009-10-06 11:31:09 -0700649
650 if (req->r_unsafe_dir) {
651 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
652
653 spin_lock(&ci->i_unsafe_lock);
654 list_del_init(&req->r_unsafe_dir_item);
655 spin_unlock(&ci->i_unsafe_lock);
Sage Weil3b663782011-05-18 16:12:12 -0700656
657 iput(req->r_unsafe_dir);
658 req->r_unsafe_dir = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700659 }
Sage Weil94aa8ae2010-03-28 21:22:50 -0700660
Yan, Zhengfc55d2c2013-10-31 09:10:47 +0800661 complete_all(&req->r_safe_completion);
662
Sage Weil94aa8ae2010-03-28 21:22:50 -0700663 ceph_mdsc_put_request(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700664}
665
666/*
667 * Choose mds to send request to next. If there is a hint set in the
668 * request (e.g., due to a prior forward hint from the mds), use that.
669 * Otherwise, consult frag tree and/or caps to identify the
670 * appropriate mds. If all else fails, choose randomly.
671 *
672 * Called under mdsc->mutex.
673 */
H Hartley Sweeten7fd7d102011-09-23 13:22:11 -0700674static struct dentry *get_nonsnap_parent(struct dentry *dentry)
Sage Weileb6bb1c2010-08-16 09:21:27 -0700675{
Sage Weild79698d2011-07-26 11:31:26 -0700676 /*
677 * we don't need to worry about protecting the d_parent access
678 * here because we never renaming inside the snapped namespace
679 * except to resplice to another snapdir, and either the old or new
680 * result is a valid result.
681 */
David Howells2b0143b2015-03-17 22:25:59 +0000682 while (!IS_ROOT(dentry) && ceph_snap(d_inode(dentry)) != CEPH_NOSNAP)
Sage Weileb6bb1c2010-08-16 09:21:27 -0700683 dentry = dentry->d_parent;
684 return dentry;
685}
686
Sage Weil2f2dc052009-10-06 11:31:09 -0700687static int __choose_mds(struct ceph_mds_client *mdsc,
688 struct ceph_mds_request *req)
689{
690 struct inode *inode;
691 struct ceph_inode_info *ci;
692 struct ceph_cap *cap;
693 int mode = req->r_direct_mode;
694 int mds = -1;
695 u32 hash = req->r_direct_hash;
696 bool is_hash = req->r_direct_is_hash;
697
698 /*
699 * is there a specific mds we should try? ignore hint if we have
700 * no session and the mds is not up (active or recovering).
701 */
702 if (req->r_resend_mds >= 0 &&
703 (__have_session(mdsc, req->r_resend_mds) ||
704 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
705 dout("choose_mds using resend_mds mds%d\n",
706 req->r_resend_mds);
707 return req->r_resend_mds;
708 }
709
710 if (mode == USE_RANDOM_MDS)
711 goto random;
712
713 inode = NULL;
714 if (req->r_inode) {
715 inode = req->r_inode;
716 } else if (req->r_dentry) {
Sage Weild79698d2011-07-26 11:31:26 -0700717 /* ignore race with rename; old or new d_parent is okay */
718 struct dentry *parent = req->r_dentry->d_parent;
David Howells2b0143b2015-03-17 22:25:59 +0000719 struct inode *dir = d_inode(parent);
Sage Weileb6bb1c2010-08-16 09:21:27 -0700720
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700721 if (dir->i_sb != mdsc->fsc->sb) {
Sage Weileb6bb1c2010-08-16 09:21:27 -0700722 /* not this fs! */
David Howells2b0143b2015-03-17 22:25:59 +0000723 inode = d_inode(req->r_dentry);
Sage Weileb6bb1c2010-08-16 09:21:27 -0700724 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
725 /* direct snapped/virtual snapdir requests
726 * based on parent dir inode */
Sage Weild79698d2011-07-26 11:31:26 -0700727 struct dentry *dn = get_nonsnap_parent(parent);
David Howells2b0143b2015-03-17 22:25:59 +0000728 inode = d_inode(dn);
Sage Weileb6bb1c2010-08-16 09:21:27 -0700729 dout("__choose_mds using nonsnap parent %p\n", inode);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800730 } else {
Sage Weileb6bb1c2010-08-16 09:21:27 -0700731 /* dentry target */
David Howells2b0143b2015-03-17 22:25:59 +0000732 inode = d_inode(req->r_dentry);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800733 if (!inode || mode == USE_AUTH_MDS) {
734 /* dir + name */
735 inode = dir;
736 hash = ceph_dentry_hash(dir, req->r_dentry);
737 is_hash = true;
738 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700739 }
740 }
Sage Weileb6bb1c2010-08-16 09:21:27 -0700741
Sage Weil2f2dc052009-10-06 11:31:09 -0700742 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
743 (int)hash, mode);
744 if (!inode)
745 goto random;
746 ci = ceph_inode(inode);
747
748 if (is_hash && S_ISDIR(inode->i_mode)) {
749 struct ceph_inode_frag frag;
750 int found;
751
752 ceph_choose_frag(ci, hash, &frag, &found);
753 if (found) {
754 if (mode == USE_ANY_MDS && frag.ndist > 0) {
755 u8 r;
756
757 /* choose a random replica */
758 get_random_bytes(&r, 1);
759 r %= frag.ndist;
760 mds = frag.dist[r];
761 dout("choose_mds %p %llx.%llx "
762 "frag %u mds%d (%d/%d)\n",
763 inode, ceph_vinop(inode),
Sage Weild66bbd42011-01-21 21:16:46 -0800764 frag.frag, mds,
Sage Weil2f2dc052009-10-06 11:31:09 -0700765 (int)r, frag.ndist);
Sage Weild66bbd42011-01-21 21:16:46 -0800766 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
767 CEPH_MDS_STATE_ACTIVE)
768 return mds;
Sage Weil2f2dc052009-10-06 11:31:09 -0700769 }
770
771 /* since this file/dir wasn't known to be
772 * replicated, then we want to look for the
773 * authoritative mds. */
774 mode = USE_AUTH_MDS;
775 if (frag.mds >= 0) {
776 /* choose auth mds */
777 mds = frag.mds;
778 dout("choose_mds %p %llx.%llx "
779 "frag %u mds%d (auth)\n",
780 inode, ceph_vinop(inode), frag.frag, mds);
Sage Weild66bbd42011-01-21 21:16:46 -0800781 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
782 CEPH_MDS_STATE_ACTIVE)
783 return mds;
Sage Weil2f2dc052009-10-06 11:31:09 -0700784 }
785 }
786 }
787
Sage Weilbe655592011-11-30 09:47:09 -0800788 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700789 cap = NULL;
790 if (mode == USE_AUTH_MDS)
791 cap = ci->i_auth_cap;
792 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
793 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
794 if (!cap) {
Sage Weilbe655592011-11-30 09:47:09 -0800795 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700796 goto random;
797 }
798 mds = cap->session->s_mds;
799 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
800 inode, ceph_vinop(inode), mds,
801 cap == ci->i_auth_cap ? "auth " : "", cap);
Sage Weilbe655592011-11-30 09:47:09 -0800802 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700803 return mds;
804
805random:
806 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
807 dout("choose_mds chose random mds%d\n", mds);
808 return mds;
809}
810
811
812/*
813 * session messages
814 */
815static struct ceph_msg *create_session_msg(u32 op, u64 seq)
816{
817 struct ceph_msg *msg;
818 struct ceph_mds_session_head *h;
819
Sage Weilb61c2762011-08-09 15:03:46 -0700820 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
821 false);
Sage Weila79832f2010-04-01 16:06:19 -0700822 if (!msg) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700823 pr_err("create_session_msg ENOMEM creating msg\n");
Sage Weila79832f2010-04-01 16:06:19 -0700824 return NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700825 }
826 h = msg->front.iov_base;
827 h->op = cpu_to_le32(op);
828 h->seq = cpu_to_le64(seq);
John Spraydbd0c8b2014-09-09 19:26:01 +0100829
830 return msg;
831}
832
833/*
834 * session message, specialization for CEPH_SESSION_REQUEST_OPEN
835 * to include additional client metadata fields.
836 */
837static struct ceph_msg *create_session_open_msg(struct ceph_mds_client *mdsc, u64 seq)
838{
839 struct ceph_msg *msg;
840 struct ceph_mds_session_head *h;
841 int i = -1;
842 int metadata_bytes = 0;
843 int metadata_key_count = 0;
844 struct ceph_options *opt = mdsc->fsc->client->options;
845 void *p;
846
Yan, Zhenga6a5ce42015-01-16 10:54:43 +0800847 const char* metadata[][2] = {
John Spraydbd0c8b2014-09-09 19:26:01 +0100848 {"hostname", utsname()->nodename},
Yan, Zhenga6a5ce42015-01-16 10:54:43 +0800849 {"kernel_version", utsname()->release},
John Spraydbd0c8b2014-09-09 19:26:01 +0100850 {"entity_id", opt->name ? opt->name : ""},
851 {NULL, NULL}
852 };
853
854 /* Calculate serialized length of metadata */
855 metadata_bytes = 4; /* map length */
856 for (i = 0; metadata[i][0] != NULL; ++i) {
857 metadata_bytes += 8 + strlen(metadata[i][0]) +
858 strlen(metadata[i][1]);
859 metadata_key_count++;
860 }
861
862 /* Allocate the message */
863 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + metadata_bytes,
864 GFP_NOFS, false);
865 if (!msg) {
866 pr_err("create_session_msg ENOMEM creating msg\n");
867 return NULL;
868 }
869 h = msg->front.iov_base;
870 h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN);
871 h->seq = cpu_to_le64(seq);
872
873 /*
874 * Serialize client metadata into waiting buffer space, using
875 * the format that userspace expects for map<string, string>
John Spray7cfa0312014-10-30 17:15:26 +0000876 *
877 * ClientSession messages with metadata are v2
John Spraydbd0c8b2014-09-09 19:26:01 +0100878 */
John Spray7cfa0312014-10-30 17:15:26 +0000879 msg->hdr.version = cpu_to_le16(2);
880 msg->hdr.compat_version = cpu_to_le16(1);
John Spraydbd0c8b2014-09-09 19:26:01 +0100881
882 /* The write pointer, following the session_head structure */
883 p = msg->front.iov_base + sizeof(*h);
884
885 /* Number of entries in the map */
886 ceph_encode_32(&p, metadata_key_count);
887
888 /* Two length-prefixed strings for each entry in the map */
889 for (i = 0; metadata[i][0] != NULL; ++i) {
890 size_t const key_len = strlen(metadata[i][0]);
891 size_t const val_len = strlen(metadata[i][1]);
892
893 ceph_encode_32(&p, key_len);
894 memcpy(p, metadata[i][0], key_len);
895 p += key_len;
896 ceph_encode_32(&p, val_len);
897 memcpy(p, metadata[i][1], val_len);
898 p += val_len;
899 }
900
Sage Weil2f2dc052009-10-06 11:31:09 -0700901 return msg;
902}
903
904/*
905 * send session open request.
906 *
907 * called under mdsc->mutex
908 */
909static int __open_session(struct ceph_mds_client *mdsc,
910 struct ceph_mds_session *session)
911{
912 struct ceph_msg *msg;
913 int mstate;
914 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -0700915
916 /* wait for mds to go active? */
917 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
918 dout("open_session to mds%d (%s)\n", mds,
919 ceph_mds_state_name(mstate));
920 session->s_state = CEPH_MDS_SESSION_OPENING;
921 session->s_renew_requested = jiffies;
922
923 /* send connect message */
John Spraydbd0c8b2014-09-09 19:26:01 +0100924 msg = create_session_open_msg(mdsc, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -0700925 if (!msg)
926 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -0700927 ceph_con_send(&session->s_con, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -0700928 return 0;
929}
930
931/*
Sage Weiled0552a2010-06-21 13:38:25 -0700932 * open sessions for any export targets for the given mds
933 *
934 * called under mdsc->mutex
935 */
Yan, Zheng5d72d132013-11-24 14:33:01 +0800936static struct ceph_mds_session *
937__open_export_target_session(struct ceph_mds_client *mdsc, int target)
938{
939 struct ceph_mds_session *session;
940
941 session = __ceph_lookup_mds_session(mdsc, target);
942 if (!session) {
943 session = register_session(mdsc, target);
944 if (IS_ERR(session))
945 return session;
946 }
947 if (session->s_state == CEPH_MDS_SESSION_NEW ||
948 session->s_state == CEPH_MDS_SESSION_CLOSING)
949 __open_session(mdsc, session);
950
951 return session;
952}
953
954struct ceph_mds_session *
955ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target)
956{
957 struct ceph_mds_session *session;
958
959 dout("open_export_target_session to mds%d\n", target);
960
961 mutex_lock(&mdsc->mutex);
962 session = __open_export_target_session(mdsc, target);
963 mutex_unlock(&mdsc->mutex);
964
965 return session;
966}
967
Sage Weiled0552a2010-06-21 13:38:25 -0700968static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
969 struct ceph_mds_session *session)
970{
971 struct ceph_mds_info *mi;
972 struct ceph_mds_session *ts;
973 int i, mds = session->s_mds;
Sage Weiled0552a2010-06-21 13:38:25 -0700974
975 if (mds >= mdsc->mdsmap->m_max_mds)
976 return;
Yan, Zheng5d72d132013-11-24 14:33:01 +0800977
Sage Weiled0552a2010-06-21 13:38:25 -0700978 mi = &mdsc->mdsmap->m_info[mds];
979 dout("open_export_target_sessions for mds%d (%d targets)\n",
980 session->s_mds, mi->num_export_targets);
981
982 for (i = 0; i < mi->num_export_targets; i++) {
Yan, Zheng5d72d132013-11-24 14:33:01 +0800983 ts = __open_export_target_session(mdsc, mi->export_targets[i]);
984 if (!IS_ERR(ts))
985 ceph_put_mds_session(ts);
Sage Weiled0552a2010-06-21 13:38:25 -0700986 }
987}
988
Sage Weil154f42c2010-06-21 13:45:04 -0700989void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
990 struct ceph_mds_session *session)
991{
992 mutex_lock(&mdsc->mutex);
993 __open_export_target_sessions(mdsc, session);
994 mutex_unlock(&mdsc->mutex);
995}
996
Sage Weiled0552a2010-06-21 13:38:25 -0700997/*
Sage Weil2f2dc052009-10-06 11:31:09 -0700998 * session caps
999 */
1000
1001/*
1002 * Free preallocated cap messages assigned to this session
1003 */
1004static void cleanup_cap_releases(struct ceph_mds_session *session)
1005{
1006 struct ceph_msg *msg;
1007
1008 spin_lock(&session->s_cap_lock);
1009 while (!list_empty(&session->s_cap_releases)) {
1010 msg = list_first_entry(&session->s_cap_releases,
1011 struct ceph_msg, list_head);
1012 list_del_init(&msg->list_head);
1013 ceph_msg_put(msg);
1014 }
1015 while (!list_empty(&session->s_cap_releases_done)) {
1016 msg = list_first_entry(&session->s_cap_releases_done,
1017 struct ceph_msg, list_head);
1018 list_del_init(&msg->list_head);
1019 ceph_msg_put(msg);
1020 }
1021 spin_unlock(&session->s_cap_lock);
1022}
1023
Yan, Zheng1c841a92015-03-24 20:15:36 +08001024static void cleanup_session_requests(struct ceph_mds_client *mdsc,
1025 struct ceph_mds_session *session)
1026{
1027 struct ceph_mds_request *req;
1028 struct rb_node *p;
1029
1030 dout("cleanup_session_requests mds%d\n", session->s_mds);
1031 mutex_lock(&mdsc->mutex);
1032 while (!list_empty(&session->s_unsafe)) {
1033 req = list_first_entry(&session->s_unsafe,
1034 struct ceph_mds_request, r_unsafe_item);
1035 list_del_init(&req->r_unsafe_item);
1036 pr_info(" dropping unsafe request %llu\n", req->r_tid);
1037 __unregister_request(mdsc, req);
1038 }
1039 /* zero r_attempts, so kick_requests() will re-send requests */
1040 p = rb_first(&mdsc->request_tree);
1041 while (p) {
1042 req = rb_entry(p, struct ceph_mds_request, r_node);
1043 p = rb_next(p);
1044 if (req->r_session &&
1045 req->r_session->s_mds == session->s_mds)
1046 req->r_attempts = 0;
1047 }
1048 mutex_unlock(&mdsc->mutex);
1049}
1050
Sage Weil2f2dc052009-10-06 11:31:09 -07001051/*
Sage Weilf818a732010-05-11 20:56:31 -07001052 * Helper to safely iterate over all caps associated with a session, with
1053 * special care taken to handle a racing __ceph_remove_cap().
Sage Weil2f2dc052009-10-06 11:31:09 -07001054 *
Sage Weilf818a732010-05-11 20:56:31 -07001055 * Caller must hold session s_mutex.
Sage Weil2f2dc052009-10-06 11:31:09 -07001056 */
1057static int iterate_session_caps(struct ceph_mds_session *session,
1058 int (*cb)(struct inode *, struct ceph_cap *,
1059 void *), void *arg)
1060{
Sage Weil7c1332b2010-02-16 11:39:45 -08001061 struct list_head *p;
1062 struct ceph_cap *cap;
1063 struct inode *inode, *last_inode = NULL;
1064 struct ceph_cap *old_cap = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001065 int ret;
1066
1067 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
1068 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001069 p = session->s_caps.next;
1070 while (p != &session->s_caps) {
1071 cap = list_entry(p, struct ceph_cap, session_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -07001072 inode = igrab(&cap->ci->vfs_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001073 if (!inode) {
1074 p = p->next;
Sage Weil2f2dc052009-10-06 11:31:09 -07001075 continue;
Sage Weil7c1332b2010-02-16 11:39:45 -08001076 }
1077 session->s_cap_iterator = cap;
Sage Weil2f2dc052009-10-06 11:31:09 -07001078 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001079
1080 if (last_inode) {
1081 iput(last_inode);
1082 last_inode = NULL;
1083 }
1084 if (old_cap) {
Yehuda Sadeh37151662010-06-17 16:16:12 -07001085 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -08001086 old_cap = NULL;
1087 }
1088
Sage Weil2f2dc052009-10-06 11:31:09 -07001089 ret = cb(inode, cap, arg);
Sage Weil7c1332b2010-02-16 11:39:45 -08001090 last_inode = inode;
1091
Sage Weil2f2dc052009-10-06 11:31:09 -07001092 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001093 p = p->next;
1094 if (cap->ci == NULL) {
1095 dout("iterate_session_caps finishing cap %p removal\n",
1096 cap);
1097 BUG_ON(cap->session != session);
1098 list_del_init(&cap->session_caps);
1099 session->s_nr_caps--;
1100 cap->session = NULL;
1101 old_cap = cap; /* put_cap it w/o locks held */
1102 }
Sage Weil5dacf092009-12-21 20:40:34 -08001103 if (ret < 0)
1104 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07001105 }
Sage Weil5dacf092009-12-21 20:40:34 -08001106 ret = 0;
1107out:
Sage Weil7c1332b2010-02-16 11:39:45 -08001108 session->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001109 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001110
SF Markus Elfringe96a6502014-11-02 15:20:59 +01001111 iput(last_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001112 if (old_cap)
Yehuda Sadeh37151662010-06-17 16:16:12 -07001113 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -08001114
Sage Weil5dacf092009-12-21 20:40:34 -08001115 return ret;
Sage Weil2f2dc052009-10-06 11:31:09 -07001116}
1117
1118static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
Sage Weil6c99f252010-05-10 16:12:25 -07001119 void *arg)
Sage Weil2f2dc052009-10-06 11:31:09 -07001120{
1121 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil6c99f252010-05-10 16:12:25 -07001122 int drop = 0;
1123
Sage Weil2f2dc052009-10-06 11:31:09 -07001124 dout("removing cap %p, ci is %p, inode is %p\n",
1125 cap, ci, &ci->vfs_inode);
Sage Weilbe655592011-11-30 09:47:09 -08001126 spin_lock(&ci->i_ceph_lock);
Yan, Zhenga096b092013-09-22 10:15:58 +08001127 __ceph_remove_cap(cap, false);
Yan, Zheng571ade32015-03-24 11:36:08 +08001128 if (!ci->i_auth_cap) {
Sage Weil6c99f252010-05-10 16:12:25 -07001129 struct ceph_mds_client *mdsc =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001130 ceph_sb_to_client(inode->i_sb)->mdsc;
Sage Weil6c99f252010-05-10 16:12:25 -07001131
1132 spin_lock(&mdsc->cap_dirty_lock);
1133 if (!list_empty(&ci->i_dirty_item)) {
1134 pr_info(" dropping dirty %s state for %p %lld\n",
1135 ceph_cap_string(ci->i_dirty_caps),
1136 inode, ceph_ino(inode));
1137 ci->i_dirty_caps = 0;
1138 list_del_init(&ci->i_dirty_item);
1139 drop = 1;
1140 }
1141 if (!list_empty(&ci->i_flushing_item)) {
1142 pr_info(" dropping dirty+flushing %s state for %p %lld\n",
1143 ceph_cap_string(ci->i_flushing_caps),
1144 inode, ceph_ino(inode));
1145 ci->i_flushing_caps = 0;
1146 list_del_init(&ci->i_flushing_item);
1147 mdsc->num_cap_flushing--;
1148 drop = 1;
1149 }
Sage Weil6c99f252010-05-10 16:12:25 -07001150 spin_unlock(&mdsc->cap_dirty_lock);
1151 }
Sage Weilbe655592011-11-30 09:47:09 -08001152 spin_unlock(&ci->i_ceph_lock);
Sage Weil6c99f252010-05-10 16:12:25 -07001153 while (drop--)
1154 iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07001155 return 0;
1156}
1157
1158/*
1159 * caller must hold session s_mutex
1160 */
1161static void remove_session_caps(struct ceph_mds_session *session)
1162{
1163 dout("remove_session_caps on %p\n", session);
1164 iterate_session_caps(session, remove_session_caps_cb, NULL);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001165
1166 spin_lock(&session->s_cap_lock);
1167 if (session->s_nr_caps > 0) {
1168 struct super_block *sb = session->s_mdsc->fsc->sb;
1169 struct inode *inode;
1170 struct ceph_cap *cap, *prev = NULL;
1171 struct ceph_vino vino;
1172 /*
1173 * iterate_session_caps() skips inodes that are being
1174 * deleted, we need to wait until deletions are complete.
1175 * __wait_on_freeing_inode() is designed for the job,
1176 * but it is not exported, so use lookup inode function
1177 * to access it.
1178 */
1179 while (!list_empty(&session->s_caps)) {
1180 cap = list_entry(session->s_caps.next,
1181 struct ceph_cap, session_caps);
1182 if (cap == prev)
1183 break;
1184 prev = cap;
1185 vino = cap->ci->i_vino;
1186 spin_unlock(&session->s_cap_lock);
1187
Yan, Zhenged284c42013-09-02 15:19:53 +08001188 inode = ceph_find_inode(sb, vino);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001189 iput(inode);
1190
1191 spin_lock(&session->s_cap_lock);
1192 }
1193 }
1194 spin_unlock(&session->s_cap_lock);
1195
Sage Weil2f2dc052009-10-06 11:31:09 -07001196 BUG_ON(session->s_nr_caps > 0);
Sage Weil6c99f252010-05-10 16:12:25 -07001197 BUG_ON(!list_empty(&session->s_cap_flushing));
Sage Weil2f2dc052009-10-06 11:31:09 -07001198 cleanup_cap_releases(session);
1199}
1200
1201/*
1202 * wake up any threads waiting on this session's caps. if the cap is
1203 * old (didn't get renewed on the client reconnect), remove it now.
1204 *
1205 * caller must hold s_mutex.
1206 */
1207static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1208 void *arg)
1209{
Sage Weil0dc25702009-11-20 13:43:45 -08001210 struct ceph_inode_info *ci = ceph_inode(inode);
1211
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001212 wake_up_all(&ci->i_cap_wq);
Sage Weil0dc25702009-11-20 13:43:45 -08001213 if (arg) {
Sage Weilbe655592011-11-30 09:47:09 -08001214 spin_lock(&ci->i_ceph_lock);
Sage Weil0dc25702009-11-20 13:43:45 -08001215 ci->i_wanted_max_size = 0;
1216 ci->i_requested_max_size = 0;
Sage Weilbe655592011-11-30 09:47:09 -08001217 spin_unlock(&ci->i_ceph_lock);
Sage Weil0dc25702009-11-20 13:43:45 -08001218 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001219 return 0;
1220}
1221
Sage Weil0dc25702009-11-20 13:43:45 -08001222static void wake_up_session_caps(struct ceph_mds_session *session,
1223 int reconnect)
Sage Weil2f2dc052009-10-06 11:31:09 -07001224{
1225 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
Sage Weil0dc25702009-11-20 13:43:45 -08001226 iterate_session_caps(session, wake_up_session_cb,
1227 (void *)(unsigned long)reconnect);
Sage Weil2f2dc052009-10-06 11:31:09 -07001228}
1229
1230/*
1231 * Send periodic message to MDS renewing all currently held caps. The
1232 * ack will reset the expiration for all caps from this session.
1233 *
1234 * caller holds s_mutex
1235 */
1236static int send_renew_caps(struct ceph_mds_client *mdsc,
1237 struct ceph_mds_session *session)
1238{
1239 struct ceph_msg *msg;
1240 int state;
1241
1242 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1243 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1244 pr_info("mds%d caps stale\n", session->s_mds);
Sage Weile4cb4cb2010-03-18 13:43:09 -07001245 session->s_renew_requested = jiffies;
Sage Weil2f2dc052009-10-06 11:31:09 -07001246
1247 /* do not try to renew caps until a recovering mds has reconnected
1248 * with its clients. */
1249 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1250 if (state < CEPH_MDS_STATE_RECONNECT) {
1251 dout("send_renew_caps ignoring mds%d (%s)\n",
1252 session->s_mds, ceph_mds_state_name(state));
1253 return 0;
1254 }
1255
1256 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1257 ceph_mds_state_name(state));
Sage Weil2f2dc052009-10-06 11:31:09 -07001258 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1259 ++session->s_renew_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001260 if (!msg)
1261 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -07001262 ceph_con_send(&session->s_con, msg);
1263 return 0;
1264}
1265
Yan, Zheng186e4f72013-11-22 14:48:37 +08001266static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
1267 struct ceph_mds_session *session, u64 seq)
1268{
1269 struct ceph_msg *msg;
1270
1271 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
John Spraya687eca2014-09-19 13:51:08 +01001272 session->s_mds, ceph_session_state_name(session->s_state), seq);
Yan, Zheng186e4f72013-11-22 14:48:37 +08001273 msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
1274 if (!msg)
1275 return -ENOMEM;
1276 ceph_con_send(&session->s_con, msg);
1277 return 0;
1278}
1279
1280
Sage Weil2f2dc052009-10-06 11:31:09 -07001281/*
1282 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
Sage Weil0dc25702009-11-20 13:43:45 -08001283 *
1284 * Called under session->s_mutex
Sage Weil2f2dc052009-10-06 11:31:09 -07001285 */
1286static void renewed_caps(struct ceph_mds_client *mdsc,
1287 struct ceph_mds_session *session, int is_renew)
1288{
1289 int was_stale;
1290 int wake = 0;
1291
1292 spin_lock(&session->s_cap_lock);
Alex Elder1ce208a2012-01-12 17:48:11 -08001293 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
Sage Weil2f2dc052009-10-06 11:31:09 -07001294
1295 session->s_cap_ttl = session->s_renew_requested +
1296 mdsc->mdsmap->m_session_timeout*HZ;
1297
1298 if (was_stale) {
1299 if (time_before(jiffies, session->s_cap_ttl)) {
1300 pr_info("mds%d caps renewed\n", session->s_mds);
1301 wake = 1;
1302 } else {
1303 pr_info("mds%d caps still stale\n", session->s_mds);
1304 }
1305 }
1306 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1307 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1308 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1309 spin_unlock(&session->s_cap_lock);
1310
1311 if (wake)
Sage Weil0dc25702009-11-20 13:43:45 -08001312 wake_up_session_caps(session, 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07001313}
1314
1315/*
1316 * send a session close request
1317 */
1318static int request_close_session(struct ceph_mds_client *mdsc,
1319 struct ceph_mds_session *session)
1320{
1321 struct ceph_msg *msg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001322
1323 dout("request_close_session mds%d state %s seq %lld\n",
John Spraya687eca2014-09-19 13:51:08 +01001324 session->s_mds, ceph_session_state_name(session->s_state),
Sage Weil2f2dc052009-10-06 11:31:09 -07001325 session->s_seq);
1326 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001327 if (!msg)
1328 return -ENOMEM;
1329 ceph_con_send(&session->s_con, msg);
1330 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001331}
1332
1333/*
1334 * Called with s_mutex held.
1335 */
1336static int __close_session(struct ceph_mds_client *mdsc,
1337 struct ceph_mds_session *session)
1338{
1339 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1340 return 0;
1341 session->s_state = CEPH_MDS_SESSION_CLOSING;
1342 return request_close_session(mdsc, session);
1343}
1344
1345/*
1346 * Trim old(er) caps.
1347 *
1348 * Because we can't cache an inode without one or more caps, we do
1349 * this indirectly: if a cap is unused, we prune its aliases, at which
1350 * point the inode will hopefully get dropped to.
1351 *
1352 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1353 * memory pressure from the MDS, though, so it needn't be perfect.
1354 */
1355static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1356{
1357 struct ceph_mds_session *session = arg;
1358 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng979abfd2013-11-22 13:56:24 +08001359 int used, wanted, oissued, mine;
Sage Weil2f2dc052009-10-06 11:31:09 -07001360
1361 if (session->s_trim_caps <= 0)
1362 return -1;
1363
Sage Weilbe655592011-11-30 09:47:09 -08001364 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001365 mine = cap->issued | cap->implemented;
1366 used = __ceph_caps_used(ci);
Yan, Zheng979abfd2013-11-22 13:56:24 +08001367 wanted = __ceph_caps_file_wanted(ci);
Sage Weil2f2dc052009-10-06 11:31:09 -07001368 oissued = __ceph_caps_issued_other(ci, cap);
1369
Yan, Zheng979abfd2013-11-22 13:56:24 +08001370 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001371 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
Yan, Zheng979abfd2013-11-22 13:56:24 +08001372 ceph_cap_string(used), ceph_cap_string(wanted));
1373 if (cap == ci->i_auth_cap) {
Yan, Zheng622f3e22015-05-07 10:59:47 +08001374 if (ci->i_dirty_caps || ci->i_flushing_caps ||
1375 !list_empty(&ci->i_cap_snaps))
Yan, Zheng979abfd2013-11-22 13:56:24 +08001376 goto out;
1377 if ((used | wanted) & CEPH_CAP_ANY_WR)
1378 goto out;
1379 }
1380 if ((used | wanted) & ~oissued & mine)
Sage Weil2f2dc052009-10-06 11:31:09 -07001381 goto out; /* we need these caps */
1382
1383 session->s_trim_caps--;
1384 if (oissued) {
1385 /* we aren't the only cap.. just remove us */
Yan, Zhenga096b092013-09-22 10:15:58 +08001386 __ceph_remove_cap(cap, true);
Sage Weil2f2dc052009-10-06 11:31:09 -07001387 } else {
1388 /* try to drop referring dentries */
Sage Weilbe655592011-11-30 09:47:09 -08001389 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001390 d_prune_aliases(inode);
1391 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1392 inode, cap, atomic_read(&inode->i_count));
1393 return 0;
1394 }
1395
1396out:
Sage Weilbe655592011-11-30 09:47:09 -08001397 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001398 return 0;
1399}
1400
1401/*
1402 * Trim session cap count down to some max number.
1403 */
1404static int trim_caps(struct ceph_mds_client *mdsc,
1405 struct ceph_mds_session *session,
1406 int max_caps)
1407{
1408 int trim_caps = session->s_nr_caps - max_caps;
1409
1410 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1411 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1412 if (trim_caps > 0) {
1413 session->s_trim_caps = trim_caps;
1414 iterate_session_caps(session, trim_caps_cb, session);
1415 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1416 session->s_mds, session->s_nr_caps, max_caps,
1417 trim_caps - session->s_trim_caps);
Sage Weil5dacf092009-12-21 20:40:34 -08001418 session->s_trim_caps = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001419 }
Yan, Zhenga56371d2014-04-01 20:34:56 +08001420
1421 ceph_add_cap_releases(mdsc, session);
1422 ceph_send_cap_releases(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07001423 return 0;
1424}
1425
1426/*
1427 * Allocate cap_release messages. If there is a partially full message
1428 * in the queue, try to allocate enough to cover it's remainder, so that
1429 * we can send it immediately.
1430 *
1431 * Called under s_mutex.
1432 */
Sage Weil2b2300d2010-06-09 16:52:04 -07001433int ceph_add_cap_releases(struct ceph_mds_client *mdsc,
Sage Weilee6b2722010-06-10 12:55:52 -07001434 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001435{
Sage Weil38e8883e2010-06-10 12:57:11 -07001436 struct ceph_msg *msg, *partial = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001437 struct ceph_mds_cap_release *head;
1438 int err = -ENOMEM;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001439 int extra = mdsc->fsc->mount_options->cap_release_safety;
Sage Weil38e8883e2010-06-10 12:57:11 -07001440 int num;
Sage Weil2f2dc052009-10-06 11:31:09 -07001441
Sage Weil38e8883e2010-06-10 12:57:11 -07001442 dout("add_cap_releases %p mds%d extra %d\n", session, session->s_mds,
1443 extra);
Sage Weil2f2dc052009-10-06 11:31:09 -07001444
1445 spin_lock(&session->s_cap_lock);
1446
1447 if (!list_empty(&session->s_cap_releases)) {
1448 msg = list_first_entry(&session->s_cap_releases,
1449 struct ceph_msg,
1450 list_head);
1451 head = msg->front.iov_base;
Sage Weil38e8883e2010-06-10 12:57:11 -07001452 num = le32_to_cpu(head->num);
1453 if (num) {
1454 dout(" partial %p with (%d/%d)\n", msg, num,
1455 (int)CEPH_CAPS_PER_RELEASE);
1456 extra += CEPH_CAPS_PER_RELEASE - num;
1457 partial = msg;
1458 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001459 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001460 while (session->s_num_cap_releases < session->s_nr_caps + extra) {
1461 spin_unlock(&session->s_cap_lock);
Yehuda Sadeh34d23762010-04-06 14:33:58 -07001462 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE,
Sage Weilb61c2762011-08-09 15:03:46 -07001463 GFP_NOFS, false);
Sage Weil2f2dc052009-10-06 11:31:09 -07001464 if (!msg)
1465 goto out_unlocked;
1466 dout("add_cap_releases %p msg %p now %d\n", session, msg,
1467 (int)msg->front.iov_len);
1468 head = msg->front.iov_base;
1469 head->num = cpu_to_le32(0);
1470 msg->front.iov_len = sizeof(*head);
1471 spin_lock(&session->s_cap_lock);
1472 list_add(&msg->list_head, &session->s_cap_releases);
1473 session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
1474 }
1475
Sage Weil38e8883e2010-06-10 12:57:11 -07001476 if (partial) {
1477 head = partial->front.iov_base;
1478 num = le32_to_cpu(head->num);
1479 dout(" queueing partial %p with %d/%d\n", partial, num,
1480 (int)CEPH_CAPS_PER_RELEASE);
1481 list_move_tail(&partial->list_head,
1482 &session->s_cap_releases_done);
1483 session->s_num_cap_releases -= CEPH_CAPS_PER_RELEASE - num;
Sage Weil2f2dc052009-10-06 11:31:09 -07001484 }
1485 err = 0;
1486 spin_unlock(&session->s_cap_lock);
1487out_unlocked:
1488 return err;
1489}
1490
Yan, Zhengd3383a82015-01-08 21:30:12 +08001491static int check_cap_flush(struct inode *inode, u64 want_flush_seq)
1492{
1493 struct ceph_inode_info *ci = ceph_inode(inode);
1494 int ret;
1495 spin_lock(&ci->i_ceph_lock);
1496 if (ci->i_flushing_caps)
1497 ret = ci->i_cap_flush_seq >= want_flush_seq;
1498 else
1499 ret = 1;
1500 spin_unlock(&ci->i_ceph_lock);
1501 return ret;
1502}
1503
Sage Weil2f2dc052009-10-06 11:31:09 -07001504/*
1505 * flush all dirty inode data to disk.
1506 *
1507 * returns true if we've flushed through want_flush_seq
1508 */
Yan, Zhengd3383a82015-01-08 21:30:12 +08001509static void wait_caps_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
Sage Weil2f2dc052009-10-06 11:31:09 -07001510{
Yan, Zhengd3383a82015-01-08 21:30:12 +08001511 int mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07001512
1513 dout("check_cap_flush want %lld\n", want_flush_seq);
1514 mutex_lock(&mdsc->mutex);
Yan, Zhengd3383a82015-01-08 21:30:12 +08001515 for (mds = 0; mds < mdsc->max_sessions; mds++) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001516 struct ceph_mds_session *session = mdsc->sessions[mds];
Yan, Zhengd3383a82015-01-08 21:30:12 +08001517 struct inode *inode = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001518
1519 if (!session)
1520 continue;
1521 get_session(session);
1522 mutex_unlock(&mdsc->mutex);
1523
1524 mutex_lock(&session->s_mutex);
1525 if (!list_empty(&session->s_cap_flushing)) {
1526 struct ceph_inode_info *ci =
1527 list_entry(session->s_cap_flushing.next,
1528 struct ceph_inode_info,
1529 i_flushing_item);
Sage Weil2f2dc052009-10-06 11:31:09 -07001530
Yan, Zhengd3383a82015-01-08 21:30:12 +08001531 if (!check_cap_flush(&ci->vfs_inode, want_flush_seq)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001532 dout("check_cap_flush still flushing %p "
Yan, Zhengd3383a82015-01-08 21:30:12 +08001533 "seq %lld <= %lld to mds%d\n",
1534 &ci->vfs_inode, ci->i_cap_flush_seq,
1535 want_flush_seq, session->s_mds);
1536 inode = igrab(&ci->vfs_inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07001537 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001538 }
1539 mutex_unlock(&session->s_mutex);
1540 ceph_put_mds_session(session);
1541
Yan, Zhengd3383a82015-01-08 21:30:12 +08001542 if (inode) {
1543 wait_event(mdsc->cap_flushing_wq,
1544 check_cap_flush(inode, want_flush_seq));
1545 iput(inode);
1546 }
1547
Sage Weil2f2dc052009-10-06 11:31:09 -07001548 mutex_lock(&mdsc->mutex);
1549 }
1550
1551 mutex_unlock(&mdsc->mutex);
1552 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07001553}
1554
1555/*
1556 * called under s_mutex
1557 */
Sage Weil3d7ded42010-06-09 16:47:10 -07001558void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1559 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001560{
1561 struct ceph_msg *msg;
1562
1563 dout("send_cap_releases mds%d\n", session->s_mds);
Sage Weil0f8605f2010-05-05 15:51:35 -07001564 spin_lock(&session->s_cap_lock);
1565 while (!list_empty(&session->s_cap_releases_done)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001566 msg = list_first_entry(&session->s_cap_releases_done,
1567 struct ceph_msg, list_head);
1568 list_del_init(&msg->list_head);
1569 spin_unlock(&session->s_cap_lock);
1570 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1571 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1572 ceph_con_send(&session->s_con, msg);
Sage Weil0f8605f2010-05-05 15:51:35 -07001573 spin_lock(&session->s_cap_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001574 }
1575 spin_unlock(&session->s_cap_lock);
1576}
1577
Sage Weile01a5942010-05-10 15:36:44 -07001578static void discard_cap_releases(struct ceph_mds_client *mdsc,
1579 struct ceph_mds_session *session)
1580{
1581 struct ceph_msg *msg;
1582 struct ceph_mds_cap_release *head;
1583 unsigned num;
1584
1585 dout("discard_cap_releases mds%d\n", session->s_mds);
Sage Weile01a5942010-05-10 15:36:44 -07001586
Yan, Zheng00bd8ed2014-03-24 09:56:43 +08001587 if (!list_empty(&session->s_cap_releases)) {
1588 /* zero out the in-progress message */
1589 msg = list_first_entry(&session->s_cap_releases,
1590 struct ceph_msg, list_head);
1591 head = msg->front.iov_base;
1592 num = le32_to_cpu(head->num);
1593 dout("discard_cap_releases mds%d %p %u\n",
1594 session->s_mds, msg, num);
1595 head->num = cpu_to_le32(0);
1596 msg->front.iov_len = sizeof(*head);
1597 session->s_num_cap_releases += num;
1598 }
Sage Weile01a5942010-05-10 15:36:44 -07001599
1600 /* requeue completed messages */
1601 while (!list_empty(&session->s_cap_releases_done)) {
1602 msg = list_first_entry(&session->s_cap_releases_done,
1603 struct ceph_msg, list_head);
1604 list_del_init(&msg->list_head);
1605
1606 head = msg->front.iov_base;
1607 num = le32_to_cpu(head->num);
1608 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg,
1609 num);
1610 session->s_num_cap_releases += num;
1611 head->num = cpu_to_le32(0);
1612 msg->front.iov_len = sizeof(*head);
1613 list_add(&msg->list_head, &session->s_cap_releases);
1614 }
Sage Weile01a5942010-05-10 15:36:44 -07001615}
1616
Sage Weil2f2dc052009-10-06 11:31:09 -07001617/*
1618 * requests
1619 */
1620
Yan, Zheng54008392014-03-29 13:41:15 +08001621int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
1622 struct inode *dir)
1623{
1624 struct ceph_inode_info *ci = ceph_inode(dir);
1625 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1626 struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options;
1627 size_t size = sizeof(*rinfo->dir_in) + sizeof(*rinfo->dir_dname_len) +
1628 sizeof(*rinfo->dir_dname) + sizeof(*rinfo->dir_dlease);
1629 int order, num_entries;
1630
1631 spin_lock(&ci->i_ceph_lock);
1632 num_entries = ci->i_files + ci->i_subdirs;
1633 spin_unlock(&ci->i_ceph_lock);
1634 num_entries = max(num_entries, 1);
1635 num_entries = min(num_entries, opt->max_readdir);
1636
1637 order = get_order(size * num_entries);
1638 while (order >= 0) {
1639 rinfo->dir_in = (void*)__get_free_pages(GFP_NOFS | __GFP_NOWARN,
1640 order);
1641 if (rinfo->dir_in)
1642 break;
1643 order--;
1644 }
1645 if (!rinfo->dir_in)
1646 return -ENOMEM;
1647
1648 num_entries = (PAGE_SIZE << order) / size;
1649 num_entries = min(num_entries, opt->max_readdir);
1650
1651 rinfo->dir_buf_size = PAGE_SIZE << order;
1652 req->r_num_caps = num_entries + 1;
1653 req->r_args.readdir.max_entries = cpu_to_le32(num_entries);
1654 req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes);
1655 return 0;
1656}
1657
Sage Weil2f2dc052009-10-06 11:31:09 -07001658/*
1659 * Create an mds request.
1660 */
1661struct ceph_mds_request *
1662ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1663{
1664 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1665
1666 if (!req)
1667 return ERR_PTR(-ENOMEM);
1668
Sage Weilb4556392010-05-13 12:01:13 -07001669 mutex_init(&req->r_fill_mutex);
Yehuda Sadeh37151662010-06-17 16:16:12 -07001670 req->r_mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07001671 req->r_started = jiffies;
1672 req->r_resend_mds = -1;
1673 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1674 req->r_fmode = -1;
Sage Weil153c8e62009-12-07 12:31:09 -08001675 kref_init(&req->r_kref);
Sage Weil2f2dc052009-10-06 11:31:09 -07001676 INIT_LIST_HEAD(&req->r_wait);
1677 init_completion(&req->r_completion);
1678 init_completion(&req->r_safe_completion);
1679 INIT_LIST_HEAD(&req->r_unsafe_item);
1680
Sage Weilb8e69062014-05-21 17:41:08 -07001681 req->r_stamp = CURRENT_TIME;
1682
Sage Weil2f2dc052009-10-06 11:31:09 -07001683 req->r_op = op;
1684 req->r_direct_mode = mode;
1685 return req;
1686}
1687
1688/*
Sage Weil44ca18f2010-02-15 12:08:46 -08001689 * return oldest (lowest) request, tid in request tree, 0 if none.
Sage Weil2f2dc052009-10-06 11:31:09 -07001690 *
1691 * called under mdsc->mutex.
1692 */
Sage Weil44ca18f2010-02-15 12:08:46 -08001693static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1694{
1695 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1696 return NULL;
1697 return rb_entry(rb_first(&mdsc->request_tree),
1698 struct ceph_mds_request, r_node);
1699}
1700
Sage Weil2f2dc052009-10-06 11:31:09 -07001701static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1702{
Sage Weil44ca18f2010-02-15 12:08:46 -08001703 struct ceph_mds_request *req = __get_oldest_req(mdsc);
1704
1705 if (req)
1706 return req->r_tid;
1707 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001708}
1709
1710/*
1711 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1712 * on build_path_from_dentry in fs/cifs/dir.c.
1713 *
1714 * If @stop_on_nosnap, generate path relative to the first non-snapped
1715 * inode.
1716 *
1717 * Encode hidden .snap dirs as a double /, i.e.
1718 * foo/.snap/bar -> foo//bar
1719 */
1720char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1721 int stop_on_nosnap)
1722{
1723 struct dentry *temp;
1724 char *path;
1725 int len, pos;
Al Viro1b71fe22011-07-16 23:43:58 -04001726 unsigned seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07001727
1728 if (dentry == NULL)
1729 return ERR_PTR(-EINVAL);
1730
1731retry:
1732 len = 0;
Al Viro1b71fe22011-07-16 23:43:58 -04001733 seq = read_seqbegin(&rename_lock);
1734 rcu_read_lock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001735 for (temp = dentry; !IS_ROOT(temp);) {
David Howells2b0143b2015-03-17 22:25:59 +00001736 struct inode *inode = d_inode(temp);
Sage Weil2f2dc052009-10-06 11:31:09 -07001737 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1738 len++; /* slash only */
1739 else if (stop_on_nosnap && inode &&
1740 ceph_snap(inode) == CEPH_NOSNAP)
1741 break;
1742 else
1743 len += 1 + temp->d_name.len;
1744 temp = temp->d_parent;
Sage Weil2f2dc052009-10-06 11:31:09 -07001745 }
Al Viro1b71fe22011-07-16 23:43:58 -04001746 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001747 if (len)
1748 len--; /* no leading '/' */
1749
1750 path = kmalloc(len+1, GFP_NOFS);
1751 if (path == NULL)
1752 return ERR_PTR(-ENOMEM);
1753 pos = len;
1754 path[pos] = 0; /* trailing null */
Al Viro1b71fe22011-07-16 23:43:58 -04001755 rcu_read_lock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001756 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
Al Viro1b71fe22011-07-16 23:43:58 -04001757 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07001758
Al Viro1b71fe22011-07-16 23:43:58 -04001759 spin_lock(&temp->d_lock);
David Howells2b0143b2015-03-17 22:25:59 +00001760 inode = d_inode(temp);
Sage Weil2f2dc052009-10-06 11:31:09 -07001761 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
Sage Weil104648a2010-03-18 10:14:30 -07001762 dout("build_path path+%d: %p SNAPDIR\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001763 pos, temp);
1764 } else if (stop_on_nosnap && inode &&
1765 ceph_snap(inode) == CEPH_NOSNAP) {
Yehuda Sadeh9d5a09e2011-12-13 09:57:44 -08001766 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001767 break;
1768 } else {
1769 pos -= temp->d_name.len;
Al Viro1b71fe22011-07-16 23:43:58 -04001770 if (pos < 0) {
1771 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001772 break;
Al Viro1b71fe22011-07-16 23:43:58 -04001773 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001774 strncpy(path + pos, temp->d_name.name,
1775 temp->d_name.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07001776 }
Al Viro1b71fe22011-07-16 23:43:58 -04001777 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001778 if (pos)
1779 path[--pos] = '/';
1780 temp = temp->d_parent;
Sage Weil2f2dc052009-10-06 11:31:09 -07001781 }
Al Viro1b71fe22011-07-16 23:43:58 -04001782 rcu_read_unlock();
1783 if (pos != 0 || read_seqretry(&rename_lock, seq)) {
Sage Weil104648a2010-03-18 10:14:30 -07001784 pr_err("build_path did not end path lookup where "
Sage Weil2f2dc052009-10-06 11:31:09 -07001785 "expected, namelen is %d, pos is %d\n", len, pos);
1786 /* presumably this is only possible if racing with a
1787 rename of one of the parent directories (we can not
1788 lock the dentries above us to prevent this, but
1789 retrying should be harmless) */
1790 kfree(path);
1791 goto retry;
1792 }
1793
David Howells2b0143b2015-03-17 22:25:59 +00001794 *base = ceph_ino(d_inode(temp));
Sage Weil2f2dc052009-10-06 11:31:09 -07001795 *plen = len;
Sage Weil104648a2010-03-18 10:14:30 -07001796 dout("build_path on %p %d built %llx '%.*s'\n",
Al Viro84d08fa2013-07-05 18:59:33 +04001797 dentry, d_count(dentry), *base, len, path);
Sage Weil2f2dc052009-10-06 11:31:09 -07001798 return path;
1799}
1800
1801static int build_dentry_path(struct dentry *dentry,
1802 const char **ppath, int *ppathlen, u64 *pino,
1803 int *pfreepath)
1804{
1805 char *path;
1806
David Howells2b0143b2015-03-17 22:25:59 +00001807 if (ceph_snap(d_inode(dentry->d_parent)) == CEPH_NOSNAP) {
1808 *pino = ceph_ino(d_inode(dentry->d_parent));
Sage Weil2f2dc052009-10-06 11:31:09 -07001809 *ppath = dentry->d_name.name;
1810 *ppathlen = dentry->d_name.len;
1811 return 0;
1812 }
1813 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1814 if (IS_ERR(path))
1815 return PTR_ERR(path);
1816 *ppath = path;
1817 *pfreepath = 1;
1818 return 0;
1819}
1820
1821static int build_inode_path(struct inode *inode,
1822 const char **ppath, int *ppathlen, u64 *pino,
1823 int *pfreepath)
1824{
1825 struct dentry *dentry;
1826 char *path;
1827
1828 if (ceph_snap(inode) == CEPH_NOSNAP) {
1829 *pino = ceph_ino(inode);
1830 *ppathlen = 0;
1831 return 0;
1832 }
1833 dentry = d_find_alias(inode);
1834 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1835 dput(dentry);
1836 if (IS_ERR(path))
1837 return PTR_ERR(path);
1838 *ppath = path;
1839 *pfreepath = 1;
1840 return 0;
1841}
1842
1843/*
1844 * request arguments may be specified via an inode *, a dentry *, or
1845 * an explicit ino+path.
1846 */
1847static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1848 const char *rpath, u64 rino,
1849 const char **ppath, int *pathlen,
1850 u64 *ino, int *freepath)
1851{
1852 int r = 0;
1853
1854 if (rinode) {
1855 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1856 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1857 ceph_snap(rinode));
1858 } else if (rdentry) {
1859 r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
1860 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1861 *ppath);
Sage Weil795858d2011-08-15 13:02:37 -07001862 } else if (rpath || rino) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001863 *ino = rino;
1864 *ppath = rpath;
David Zafmanb0000562012-10-25 10:23:46 -07001865 *pathlen = rpath ? strlen(rpath) : 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001866 dout(" path %.*s\n", *pathlen, rpath);
1867 }
1868
1869 return r;
1870}
1871
1872/*
1873 * called under mdsc->mutex
1874 */
1875static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1876 struct ceph_mds_request *req,
Yan, Zheng6e6f0922015-02-27 08:54:08 +08001877 int mds, bool drop_cap_releases)
Sage Weil2f2dc052009-10-06 11:31:09 -07001878{
1879 struct ceph_msg *msg;
1880 struct ceph_mds_request_head *head;
1881 const char *path1 = NULL;
1882 const char *path2 = NULL;
1883 u64 ino1 = 0, ino2 = 0;
1884 int pathlen1 = 0, pathlen2 = 0;
1885 int freepath1 = 0, freepath2 = 0;
1886 int len;
1887 u16 releases;
1888 void *p, *end;
1889 int ret;
1890
1891 ret = set_request_path_attr(req->r_inode, req->r_dentry,
1892 req->r_path1, req->r_ino1.ino,
1893 &path1, &pathlen1, &ino1, &freepath1);
1894 if (ret < 0) {
1895 msg = ERR_PTR(ret);
1896 goto out;
1897 }
1898
1899 ret = set_request_path_attr(NULL, req->r_old_dentry,
1900 req->r_path2, req->r_ino2.ino,
1901 &path2, &pathlen2, &ino2, &freepath2);
1902 if (ret < 0) {
1903 msg = ERR_PTR(ret);
1904 goto out_free1;
1905 }
1906
1907 len = sizeof(*head) +
Sage Weilb8e69062014-05-21 17:41:08 -07001908 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) +
1909 sizeof(struct timespec);
Sage Weil2f2dc052009-10-06 11:31:09 -07001910
1911 /* calculate (max) length for cap releases */
1912 len += sizeof(struct ceph_mds_request_release) *
1913 (!!req->r_inode_drop + !!req->r_dentry_drop +
1914 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1915 if (req->r_dentry_drop)
1916 len += req->r_dentry->d_name.len;
1917 if (req->r_old_dentry_drop)
1918 len += req->r_old_dentry->d_name.len;
1919
Sage Weilb61c2762011-08-09 15:03:46 -07001920 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07001921 if (!msg) {
1922 msg = ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -07001923 goto out_free2;
Sage Weila79832f2010-04-01 16:06:19 -07001924 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001925
John Spray7cfa0312014-10-30 17:15:26 +00001926 msg->hdr.version = cpu_to_le16(2);
Sage Weil6df058c2009-12-22 11:24:33 -08001927 msg->hdr.tid = cpu_to_le64(req->r_tid);
1928
Sage Weil2f2dc052009-10-06 11:31:09 -07001929 head = msg->front.iov_base;
1930 p = msg->front.iov_base + sizeof(*head);
1931 end = msg->front.iov_base + msg->front.iov_len;
1932
1933 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1934 head->op = cpu_to_le32(req->r_op);
Eric W. Biedermanff3d0042013-01-31 04:01:53 -08001935 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid));
1936 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid));
Sage Weil2f2dc052009-10-06 11:31:09 -07001937 head->args = req->r_args;
1938
1939 ceph_encode_filepath(&p, end, ino1, path1);
1940 ceph_encode_filepath(&p, end, ino2, path2);
1941
Sage Weile979cf52010-07-15 14:58:39 -07001942 /* make note of release offset, in case we need to replay */
1943 req->r_request_release_offset = p - msg->front.iov_base;
1944
Sage Weil2f2dc052009-10-06 11:31:09 -07001945 /* cap releases */
1946 releases = 0;
1947 if (req->r_inode_drop)
1948 releases += ceph_encode_inode_release(&p,
David Howells2b0143b2015-03-17 22:25:59 +00001949 req->r_inode ? req->r_inode : d_inode(req->r_dentry),
Sage Weil2f2dc052009-10-06 11:31:09 -07001950 mds, req->r_inode_drop, req->r_inode_unless, 0);
1951 if (req->r_dentry_drop)
1952 releases += ceph_encode_dentry_release(&p, req->r_dentry,
1953 mds, req->r_dentry_drop, req->r_dentry_unless);
1954 if (req->r_old_dentry_drop)
1955 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
1956 mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
1957 if (req->r_old_inode_drop)
1958 releases += ceph_encode_inode_release(&p,
David Howells2b0143b2015-03-17 22:25:59 +00001959 d_inode(req->r_old_dentry),
Sage Weil2f2dc052009-10-06 11:31:09 -07001960 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
Yan, Zheng6e6f0922015-02-27 08:54:08 +08001961
1962 if (drop_cap_releases) {
1963 releases = 0;
1964 p = msg->front.iov_base + req->r_request_release_offset;
1965 }
1966
Sage Weil2f2dc052009-10-06 11:31:09 -07001967 head->num_releases = cpu_to_le16(releases);
1968
Sage Weilb8e69062014-05-21 17:41:08 -07001969 /* time stamp */
Yan, Zheng1f041a82015-01-13 15:20:52 +08001970 {
1971 struct ceph_timespec ts;
1972 ceph_encode_timespec(&ts, &req->r_stamp);
1973 ceph_encode_copy(&p, &ts, sizeof(ts));
1974 }
Sage Weilb8e69062014-05-21 17:41:08 -07001975
Sage Weil2f2dc052009-10-06 11:31:09 -07001976 BUG_ON(p > end);
1977 msg->front.iov_len = p - msg->front.iov_base;
1978 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1979
Yan, Zheng25e6bae2014-09-16 19:15:28 +08001980 if (req->r_pagelist) {
1981 struct ceph_pagelist *pagelist = req->r_pagelist;
1982 atomic_inc(&pagelist->refcnt);
1983 ceph_msg_data_add_pagelist(msg, pagelist);
1984 msg->hdr.data_len = cpu_to_le32(pagelist->length);
1985 } else {
1986 msg->hdr.data_len = 0;
Alex Elderebf18f42013-03-04 22:29:57 -06001987 }
Alex Elder02afca62013-02-14 12:16:43 -06001988
Sage Weil2f2dc052009-10-06 11:31:09 -07001989 msg->hdr.data_off = cpu_to_le16(0);
1990
1991out_free2:
1992 if (freepath2)
1993 kfree((char *)path2);
1994out_free1:
1995 if (freepath1)
1996 kfree((char *)path1);
1997out:
1998 return msg;
1999}
2000
2001/*
2002 * called under mdsc->mutex if error, under no mutex if
2003 * success.
2004 */
2005static void complete_request(struct ceph_mds_client *mdsc,
2006 struct ceph_mds_request *req)
2007{
2008 if (req->r_callback)
2009 req->r_callback(mdsc, req);
2010 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002011 complete_all(&req->r_completion);
Sage Weil2f2dc052009-10-06 11:31:09 -07002012}
2013
2014/*
2015 * called under mdsc->mutex
2016 */
2017static int __prepare_send_request(struct ceph_mds_client *mdsc,
2018 struct ceph_mds_request *req,
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002019 int mds, bool drop_cap_releases)
Sage Weil2f2dc052009-10-06 11:31:09 -07002020{
2021 struct ceph_mds_request_head *rhead;
2022 struct ceph_msg *msg;
2023 int flags = 0;
2024
Sage Weil2f2dc052009-10-06 11:31:09 -07002025 req->r_attempts++;
Greg Farnume55b71f2010-06-22 15:58:01 -07002026 if (req->r_inode) {
2027 struct ceph_cap *cap =
2028 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
2029
2030 if (cap)
2031 req->r_sent_on_mseq = cap->mseq;
2032 else
2033 req->r_sent_on_mseq = -1;
2034 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002035 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
2036 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
2037
Sage Weil01a92f12010-07-15 13:24:32 -07002038 if (req->r_got_unsafe) {
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002039 void *p;
Sage Weil01a92f12010-07-15 13:24:32 -07002040 /*
2041 * Replay. Do not regenerate message (and rebuild
2042 * paths, etc.); just use the original message.
2043 * Rebuilding paths will break for renames because
2044 * d_move mangles the src name.
2045 */
2046 msg = req->r_request;
2047 rhead = msg->front.iov_base;
2048
2049 flags = le32_to_cpu(rhead->flags);
2050 flags |= CEPH_MDS_FLAG_REPLAY;
2051 rhead->flags = cpu_to_le32(flags);
2052
2053 if (req->r_target_inode)
2054 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
2055
2056 rhead->num_retry = req->r_attempts - 1;
Sage Weile979cf52010-07-15 14:58:39 -07002057
2058 /* remove cap/dentry releases from message */
2059 rhead->num_releases = 0;
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002060
2061 /* time stamp */
2062 p = msg->front.iov_base + req->r_request_release_offset;
Yan, Zheng1f041a82015-01-13 15:20:52 +08002063 {
2064 struct ceph_timespec ts;
2065 ceph_encode_timespec(&ts, &req->r_stamp);
2066 ceph_encode_copy(&p, &ts, sizeof(ts));
2067 }
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002068
2069 msg->front.iov_len = p - msg->front.iov_base;
2070 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
Sage Weil01a92f12010-07-15 13:24:32 -07002071 return 0;
2072 }
2073
Sage Weil2f2dc052009-10-06 11:31:09 -07002074 if (req->r_request) {
2075 ceph_msg_put(req->r_request);
2076 req->r_request = NULL;
2077 }
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002078 msg = create_request_message(mdsc, req, mds, drop_cap_releases);
Sage Weil2f2dc052009-10-06 11:31:09 -07002079 if (IS_ERR(msg)) {
Sage Weile1518c72010-05-13 11:19:06 -07002080 req->r_err = PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002081 complete_request(mdsc, req);
Sage Weila79832f2010-04-01 16:06:19 -07002082 return PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002083 }
2084 req->r_request = msg;
2085
2086 rhead = msg->front.iov_base;
Sage Weil2f2dc052009-10-06 11:31:09 -07002087 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
2088 if (req->r_got_unsafe)
2089 flags |= CEPH_MDS_FLAG_REPLAY;
2090 if (req->r_locked_dir)
2091 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
2092 rhead->flags = cpu_to_le32(flags);
2093 rhead->num_fwd = req->r_num_fwd;
2094 rhead->num_retry = req->r_attempts - 1;
Sage Weil01a92f12010-07-15 13:24:32 -07002095 rhead->ino = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002096
2097 dout(" r_locked_dir = %p\n", req->r_locked_dir);
Sage Weil2f2dc052009-10-06 11:31:09 -07002098 return 0;
2099}
2100
2101/*
2102 * send request, or put it on the appropriate wait list.
2103 */
2104static int __do_request(struct ceph_mds_client *mdsc,
2105 struct ceph_mds_request *req)
2106{
2107 struct ceph_mds_session *session = NULL;
2108 int mds = -1;
2109 int err = -EAGAIN;
2110
Yan, Zhengeb1b8af2013-09-26 14:25:36 +08002111 if (req->r_err || req->r_got_result) {
2112 if (req->r_aborted)
2113 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002114 goto out;
Yan, Zhengeb1b8af2013-09-26 14:25:36 +08002115 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002116
2117 if (req->r_timeout &&
2118 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
2119 dout("do_request timed out\n");
2120 err = -EIO;
2121 goto finish;
2122 }
2123
Sage Weildc69e2e2010-11-02 13:49:00 -07002124 put_request_session(req);
2125
Sage Weil2f2dc052009-10-06 11:31:09 -07002126 mds = __choose_mds(mdsc, req);
2127 if (mds < 0 ||
2128 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
2129 dout("do_request no mds or not active, waiting for map\n");
2130 list_add(&req->r_wait, &mdsc->waiting_for_map);
2131 goto out;
2132 }
2133
2134 /* get, open session */
2135 session = __ceph_lookup_mds_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07002136 if (!session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002137 session = register_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07002138 if (IS_ERR(session)) {
2139 err = PTR_ERR(session);
2140 goto finish;
2141 }
2142 }
Sage Weildc69e2e2010-11-02 13:49:00 -07002143 req->r_session = get_session(session);
2144
Sage Weil2f2dc052009-10-06 11:31:09 -07002145 dout("do_request mds%d session %p state %s\n", mds, session,
John Spraya687eca2014-09-19 13:51:08 +01002146 ceph_session_state_name(session->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07002147 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
2148 session->s_state != CEPH_MDS_SESSION_HUNG) {
2149 if (session->s_state == CEPH_MDS_SESSION_NEW ||
2150 session->s_state == CEPH_MDS_SESSION_CLOSING)
2151 __open_session(mdsc, session);
2152 list_add(&req->r_wait, &session->s_waiting);
2153 goto out_session;
2154 }
2155
2156 /* send request */
Sage Weil2f2dc052009-10-06 11:31:09 -07002157 req->r_resend_mds = -1; /* forget any previous mds hint */
2158
2159 if (req->r_request_started == 0) /* note request start time */
2160 req->r_request_started = jiffies;
2161
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002162 err = __prepare_send_request(mdsc, req, mds, false);
Sage Weil2f2dc052009-10-06 11:31:09 -07002163 if (!err) {
2164 ceph_msg_get(req->r_request);
2165 ceph_con_send(&session->s_con, req->r_request);
2166 }
2167
2168out_session:
2169 ceph_put_mds_session(session);
2170out:
2171 return err;
2172
2173finish:
Sage Weile1518c72010-05-13 11:19:06 -07002174 req->r_err = err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002175 complete_request(mdsc, req);
2176 goto out;
2177}
2178
2179/*
2180 * called under mdsc->mutex
2181 */
2182static void __wake_requests(struct ceph_mds_client *mdsc,
2183 struct list_head *head)
2184{
Yan, Zhenged75ec22012-11-19 10:49:06 +08002185 struct ceph_mds_request *req;
2186 LIST_HEAD(tmp_list);
Sage Weil2f2dc052009-10-06 11:31:09 -07002187
Yan, Zhenged75ec22012-11-19 10:49:06 +08002188 list_splice_init(head, &tmp_list);
2189
2190 while (!list_empty(&tmp_list)) {
2191 req = list_entry(tmp_list.next,
2192 struct ceph_mds_request, r_wait);
Sage Weil2f2dc052009-10-06 11:31:09 -07002193 list_del_init(&req->r_wait);
Sage Weil7971bd92013-05-01 21:15:58 -07002194 dout(" wake request %p tid %llu\n", req, req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002195 __do_request(mdsc, req);
2196 }
2197}
2198
2199/*
2200 * Wake up threads with requests pending for @mds, so that they can
Sage Weil29790f22010-03-18 14:45:05 -07002201 * resubmit their requests to a possibly different mds.
Sage Weil2f2dc052009-10-06 11:31:09 -07002202 */
Sage Weil29790f22010-03-18 14:45:05 -07002203static void kick_requests(struct ceph_mds_client *mdsc, int mds)
Sage Weil2f2dc052009-10-06 11:31:09 -07002204{
Sage Weil44ca18f2010-02-15 12:08:46 -08002205 struct ceph_mds_request *req;
Yan, Zheng282c1052014-07-30 10:12:47 +08002206 struct rb_node *p = rb_first(&mdsc->request_tree);
Sage Weil2f2dc052009-10-06 11:31:09 -07002207
2208 dout("kick_requests mds%d\n", mds);
Yan, Zheng282c1052014-07-30 10:12:47 +08002209 while (p) {
Sage Weil44ca18f2010-02-15 12:08:46 -08002210 req = rb_entry(p, struct ceph_mds_request, r_node);
Yan, Zheng282c1052014-07-30 10:12:47 +08002211 p = rb_next(p);
Sage Weil44ca18f2010-02-15 12:08:46 -08002212 if (req->r_got_unsafe)
2213 continue;
Yan, Zheng3de22be2015-02-04 14:26:22 +08002214 if (req->r_attempts > 0)
2215 continue; /* only new requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08002216 if (req->r_session &&
2217 req->r_session->s_mds == mds) {
2218 dout(" kicking tid %llu\n", req->r_tid);
Yan, Zheng03974e82014-09-11 14:28:56 +08002219 list_del_init(&req->r_wait);
Sage Weil44ca18f2010-02-15 12:08:46 -08002220 __do_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002221 }
2222 }
2223}
2224
2225void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
2226 struct ceph_mds_request *req)
2227{
2228 dout("submit_request on %p\n", req);
2229 mutex_lock(&mdsc->mutex);
2230 __register_request(mdsc, req, NULL);
2231 __do_request(mdsc, req);
2232 mutex_unlock(&mdsc->mutex);
2233}
2234
2235/*
2236 * Synchrously perform an mds request. Take care of all of the
2237 * session setup, forwarding, retry details.
2238 */
2239int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
2240 struct inode *dir,
2241 struct ceph_mds_request *req)
2242{
2243 int err;
2244
2245 dout("do_request on %p\n", req);
2246
2247 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
2248 if (req->r_inode)
2249 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
2250 if (req->r_locked_dir)
2251 ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
Sage Weil844d87c2013-02-05 13:40:09 -08002252 if (req->r_old_dentry_dir)
Sage Weil41b02e12011-07-26 11:31:14 -07002253 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
2254 CEPH_CAP_PIN);
Sage Weil2f2dc052009-10-06 11:31:09 -07002255
2256 /* issue */
2257 mutex_lock(&mdsc->mutex);
2258 __register_request(mdsc, req, dir);
2259 __do_request(mdsc, req);
2260
Sage Weile1518c72010-05-13 11:19:06 -07002261 if (req->r_err) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002262 err = req->r_err;
Sage Weile1518c72010-05-13 11:19:06 -07002263 __unregister_request(mdsc, req);
2264 dout("do_request early error %d\n", err);
2265 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07002266 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002267
Sage Weile1518c72010-05-13 11:19:06 -07002268 /* wait */
2269 mutex_unlock(&mdsc->mutex);
2270 dout("do_request waiting\n");
2271 if (req->r_timeout) {
Sage Weilaa916472010-05-24 11:15:51 -07002272 err = (long)wait_for_completion_killable_timeout(
Sage Weile1518c72010-05-13 11:19:06 -07002273 &req->r_completion, req->r_timeout);
2274 if (err == 0)
2275 err = -EIO;
Yan, Zheng9280be22014-10-14 10:33:35 +08002276 } else if (req->r_wait_for_completion) {
2277 err = req->r_wait_for_completion(mdsc, req);
Sage Weile1518c72010-05-13 11:19:06 -07002278 } else {
Sage Weilaa916472010-05-24 11:15:51 -07002279 err = wait_for_completion_killable(&req->r_completion);
Sage Weile1518c72010-05-13 11:19:06 -07002280 }
2281 dout("do_request waited, got %d\n", err);
2282 mutex_lock(&mdsc->mutex);
2283
2284 /* only abort if we didn't race with a real reply */
2285 if (req->r_got_result) {
2286 err = le32_to_cpu(req->r_reply_info.head->result);
2287 } else if (err < 0) {
2288 dout("aborted request %lld with %d\n", req->r_tid, err);
Sage Weilb4556392010-05-13 12:01:13 -07002289
2290 /*
2291 * ensure we aren't running concurrently with
2292 * ceph_fill_trace or ceph_readdir_prepopulate, which
2293 * rely on locks (dir mutex) held by our caller.
2294 */
2295 mutex_lock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07002296 req->r_err = err;
2297 req->r_aborted = true;
Sage Weilb4556392010-05-13 12:01:13 -07002298 mutex_unlock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07002299
2300 if (req->r_locked_dir &&
Sage Weil167c9e32010-05-14 10:02:57 -07002301 (req->r_op & CEPH_MDS_OP_WRITE))
2302 ceph_invalidate_dir_request(req);
Sage Weile1518c72010-05-13 11:19:06 -07002303 } else {
2304 err = req->r_err;
2305 }
2306
2307out:
2308 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002309 dout("do_request %p done, result %d\n", req, err);
2310 return err;
2311}
2312
2313/*
Yan, Zheng2f276c52013-03-13 19:44:32 +08002314 * Invalidate dir's completeness, dentry lease state on an aborted MDS
Sage Weil167c9e32010-05-14 10:02:57 -07002315 * namespace request.
2316 */
2317void ceph_invalidate_dir_request(struct ceph_mds_request *req)
2318{
2319 struct inode *inode = req->r_locked_dir;
Sage Weil167c9e32010-05-14 10:02:57 -07002320
Yan, Zheng2f276c52013-03-13 19:44:32 +08002321 dout("invalidate_dir_request %p (complete, lease(s))\n", inode);
Sage Weil167c9e32010-05-14 10:02:57 -07002322
Yan, Zheng2f276c52013-03-13 19:44:32 +08002323 ceph_dir_clear_complete(inode);
Sage Weil167c9e32010-05-14 10:02:57 -07002324 if (req->r_dentry)
2325 ceph_invalidate_dentry_lease(req->r_dentry);
2326 if (req->r_old_dentry)
2327 ceph_invalidate_dentry_lease(req->r_old_dentry);
2328}
2329
2330/*
Sage Weil2f2dc052009-10-06 11:31:09 -07002331 * Handle mds reply.
2332 *
2333 * We take the session mutex and parse and process the reply immediately.
2334 * This preserves the logical ordering of replies, capabilities, etc., sent
2335 * by the MDS as they are applied to our local cache.
2336 */
2337static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2338{
2339 struct ceph_mds_client *mdsc = session->s_mdsc;
2340 struct ceph_mds_request *req;
2341 struct ceph_mds_reply_head *head = msg->front.iov_base;
2342 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
Yan, Zheng982d6012014-12-23 15:30:54 +08002343 struct ceph_snap_realm *realm;
Sage Weil2f2dc052009-10-06 11:31:09 -07002344 u64 tid;
2345 int err, result;
Sage Weil2600d2d2010-02-22 15:12:16 -08002346 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002347
Sage Weil2f2dc052009-10-06 11:31:09 -07002348 if (msg->front.iov_len < sizeof(*head)) {
2349 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002350 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002351 return;
2352 }
2353
2354 /* get request, session */
Sage Weil6df058c2009-12-22 11:24:33 -08002355 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002356 mutex_lock(&mdsc->mutex);
2357 req = __lookup_request(mdsc, tid);
2358 if (!req) {
2359 dout("handle_reply on unknown tid %llu\n", tid);
2360 mutex_unlock(&mdsc->mutex);
2361 return;
2362 }
2363 dout("handle_reply %p\n", req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002364
2365 /* correct session? */
Sage Weild96d6042010-03-20 20:50:58 -07002366 if (req->r_session != session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002367 pr_err("mdsc_handle_reply got %llu on session mds%d"
2368 " not mds%d\n", tid, session->s_mds,
2369 req->r_session ? req->r_session->s_mds : -1);
2370 mutex_unlock(&mdsc->mutex);
2371 goto out;
2372 }
2373
2374 /* dup? */
2375 if ((req->r_got_unsafe && !head->safe) ||
2376 (req->r_got_safe && head->safe)) {
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07002377 pr_warn("got a dup %s reply on %llu from mds%d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002378 head->safe ? "safe" : "unsafe", tid, mds);
2379 mutex_unlock(&mdsc->mutex);
2380 goto out;
2381 }
Sage Weil85792d0d2010-05-13 09:06:02 -07002382 if (req->r_got_safe && !head->safe) {
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07002383 pr_warn("got unsafe after safe on %llu from mds%d\n",
Sage Weil85792d0d2010-05-13 09:06:02 -07002384 tid, mds);
2385 mutex_unlock(&mdsc->mutex);
2386 goto out;
2387 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002388
2389 result = le32_to_cpu(head->result);
2390
2391 /*
Greg Farnume55b71f2010-06-22 15:58:01 -07002392 * Handle an ESTALE
2393 * if we're not talking to the authority, send to them
2394 * if the authority has changed while we weren't looking,
2395 * send to new authority
2396 * Otherwise we just have to return an ESTALE
Sage Weil2f2dc052009-10-06 11:31:09 -07002397 */
2398 if (result == -ESTALE) {
Greg Farnume55b71f2010-06-22 15:58:01 -07002399 dout("got ESTALE on request %llu", req->r_tid);
Yan, Zheng51da8e82014-07-14 10:48:29 +08002400 req->r_resend_mds = -1;
Yan, Zhengca18bed2013-11-22 14:21:44 +08002401 if (req->r_direct_mode != USE_AUTH_MDS) {
Greg Farnume55b71f2010-06-22 15:58:01 -07002402 dout("not using auth, setting for that now");
2403 req->r_direct_mode = USE_AUTH_MDS;
Sage Weil2f2dc052009-10-06 11:31:09 -07002404 __do_request(mdsc, req);
2405 mutex_unlock(&mdsc->mutex);
2406 goto out;
Greg Farnume55b71f2010-06-22 15:58:01 -07002407 } else {
Yan, Zhengca18bed2013-11-22 14:21:44 +08002408 int mds = __choose_mds(mdsc, req);
2409 if (mds >= 0 && mds != req->r_session->s_mds) {
2410 dout("but auth changed, so resending");
Greg Farnume55b71f2010-06-22 15:58:01 -07002411 __do_request(mdsc, req);
2412 mutex_unlock(&mdsc->mutex);
2413 goto out;
2414 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002415 }
Greg Farnume55b71f2010-06-22 15:58:01 -07002416 dout("have to return ESTALE on request %llu", req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002417 }
2418
Greg Farnume55b71f2010-06-22 15:58:01 -07002419
Sage Weil2f2dc052009-10-06 11:31:09 -07002420 if (head->safe) {
2421 req->r_got_safe = true;
2422 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002423
2424 if (req->r_got_unsafe) {
2425 /*
2426 * We already handled the unsafe response, now do the
2427 * cleanup. No need to examine the response; the MDS
2428 * doesn't include any result info in the safe
2429 * response. And even if it did, there is nothing
2430 * useful we could do with a revised return value.
2431 */
2432 dout("got safe reply %llu, mds%d\n", tid, mds);
2433 list_del_init(&req->r_unsafe_item);
2434
2435 /* last unsafe request during umount? */
Sage Weil44ca18f2010-02-15 12:08:46 -08002436 if (mdsc->stopping && !__get_oldest_req(mdsc))
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002437 complete_all(&mdsc->safe_umount_waiters);
Sage Weil2f2dc052009-10-06 11:31:09 -07002438 mutex_unlock(&mdsc->mutex);
2439 goto out;
2440 }
Sage Weile1518c72010-05-13 11:19:06 -07002441 } else {
Sage Weil2f2dc052009-10-06 11:31:09 -07002442 req->r_got_unsafe = true;
2443 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
2444 }
2445
2446 dout("handle_reply tid %lld result %d\n", tid, result);
2447 rinfo = &req->r_reply_info;
Sage Weil14303d22010-12-14 17:37:52 -08002448 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
Sage Weil2f2dc052009-10-06 11:31:09 -07002449 mutex_unlock(&mdsc->mutex);
2450
2451 mutex_lock(&session->s_mutex);
2452 if (err < 0) {
Herb Shiu25933ab2010-12-01 14:14:38 -08002453 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002454 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002455 goto out_err;
2456 }
2457
2458 /* snap trace */
Yan, Zheng982d6012014-12-23 15:30:54 +08002459 realm = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07002460 if (rinfo->snapblob_len) {
2461 down_write(&mdsc->snap_rwsem);
2462 ceph_update_snap_trace(mdsc, rinfo->snapblob,
Yan, Zheng982d6012014-12-23 15:30:54 +08002463 rinfo->snapblob + rinfo->snapblob_len,
2464 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP,
2465 &realm);
Sage Weil2f2dc052009-10-06 11:31:09 -07002466 downgrade_write(&mdsc->snap_rwsem);
2467 } else {
2468 down_read(&mdsc->snap_rwsem);
2469 }
2470
2471 /* insert trace into our cache */
Sage Weilb4556392010-05-13 12:01:13 -07002472 mutex_lock(&req->r_fill_mutex);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002473 err = ceph_fill_trace(mdsc->fsc->sb, req, req->r_session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002474 if (err == 0) {
Sam Lang6e8575f2012-12-28 09:56:46 -08002475 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
Yan, Zheng81c6aea2013-09-18 09:44:13 +08002476 req->r_op == CEPH_MDS_OP_LSSNAP))
Sage Weil2f2dc052009-10-06 11:31:09 -07002477 ceph_readdir_prepopulate(req, req->r_session);
Yehuda Sadeh37151662010-06-17 16:16:12 -07002478 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
Sage Weil2f2dc052009-10-06 11:31:09 -07002479 }
Sage Weilb4556392010-05-13 12:01:13 -07002480 mutex_unlock(&req->r_fill_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002481
2482 up_read(&mdsc->snap_rwsem);
Yan, Zheng982d6012014-12-23 15:30:54 +08002483 if (realm)
2484 ceph_put_snap_realm(mdsc, realm);
Sage Weil2f2dc052009-10-06 11:31:09 -07002485out_err:
Sage Weile1518c72010-05-13 11:19:06 -07002486 mutex_lock(&mdsc->mutex);
2487 if (!req->r_aborted) {
2488 if (err) {
2489 req->r_err = err;
2490 } else {
2491 req->r_reply = msg;
2492 ceph_msg_get(msg);
2493 req->r_got_result = true;
2494 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002495 } else {
Sage Weile1518c72010-05-13 11:19:06 -07002496 dout("reply arrived after request %lld was aborted\n", tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002497 }
Sage Weile1518c72010-05-13 11:19:06 -07002498 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002499
Sage Weilee6b2722010-06-10 12:55:52 -07002500 ceph_add_cap_releases(mdsc, req->r_session);
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 */
2892 discard_cap_releases(mdsc, session);
Yan, Zheng99a9c272013-09-22 11:08:14 +08002893 spin_unlock(&session->s_cap_lock);
Sage Weile01a5942010-05-10 15:36:44 -07002894
Yan, Zheng5d233712014-09-10 16:56:23 +08002895 /* trim unused caps to reduce MDS's cache rejoin time */
Yan, Zhengc0bd50e2015-04-07 15:51:08 +08002896 if (mdsc->fsc->sb->s_root)
2897 shrink_dcache_parent(mdsc->fsc->sb->s_root);
Yan, Zheng5d233712014-09-10 16:56:23 +08002898
2899 ceph_con_close(&session->s_con);
2900 ceph_con_open(&session->s_con,
2901 CEPH_ENTITY_TYPE_MDS, mds,
2902 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
2903
2904 /* replay unsafe requests */
2905 replay_unsafe_requests(mdsc, session);
2906
2907 down_read(&mdsc->snap_rwsem);
2908
Sage Weil2f2dc052009-10-06 11:31:09 -07002909 /* traverse this session's caps */
Yan, Zheng44c99752013-09-22 10:28:10 +08002910 s_nr_caps = session->s_nr_caps;
2911 err = ceph_pagelist_encode_32(pagelist, s_nr_caps);
Sage Weil93cea5b2009-12-23 12:21:51 -08002912 if (err)
2913 goto fail;
Sage Weil20cb34a2010-05-12 15:21:32 -07002914
Yan, Zheng44c99752013-09-22 10:28:10 +08002915 recon_state.nr_caps = 0;
Sage Weil20cb34a2010-05-12 15:21:32 -07002916 recon_state.pagelist = pagelist;
2917 recon_state.flock = session->s_con.peer_features & CEPH_FEATURE_FLOCK;
2918 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
Sage Weil2f2dc052009-10-06 11:31:09 -07002919 if (err < 0)
Sage Weil9abf82b2010-05-10 21:58:38 -07002920 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07002921
Yan, Zheng99a9c272013-09-22 11:08:14 +08002922 spin_lock(&session->s_cap_lock);
2923 session->s_cap_reconnect = 0;
2924 spin_unlock(&session->s_cap_lock);
2925
Sage Weil2f2dc052009-10-06 11:31:09 -07002926 /*
2927 * snaprealms. we provide mds with the ino, seq (version), and
2928 * parent for all of our realms. If the mds has any newer info,
2929 * it will tell us.
2930 */
Sage Weila105f002010-02-15 14:37:55 -08002931 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
2932 struct ceph_snap_realm *realm =
2933 rb_entry(p, struct ceph_snap_realm, node);
Sage Weil93cea5b2009-12-23 12:21:51 -08002934 struct ceph_mds_snaprealm_reconnect sr_rec;
Sage Weil2f2dc052009-10-06 11:31:09 -07002935
2936 dout(" adding snap realm %llx seq %lld parent %llx\n",
2937 realm->ino, realm->seq, realm->parent_ino);
Sage Weil93cea5b2009-12-23 12:21:51 -08002938 sr_rec.ino = cpu_to_le64(realm->ino);
2939 sr_rec.seq = cpu_to_le64(realm->seq);
2940 sr_rec.parent = cpu_to_le64(realm->parent_ino);
2941 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
2942 if (err)
2943 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07002944 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002945
Sage Weil20cb34a2010-05-12 15:21:32 -07002946 if (recon_state.flock)
2947 reply->hdr.version = cpu_to_le16(2);
Yan, Zheng44c99752013-09-22 10:28:10 +08002948
2949 /* raced with cap release? */
2950 if (s_nr_caps != recon_state.nr_caps) {
2951 struct page *page = list_first_entry(&pagelist->head,
2952 struct page, lru);
2953 __le32 *addr = kmap_atomic(page);
2954 *addr = cpu_to_le32(recon_state.nr_caps);
2955 kunmap_atomic(addr);
Alex Elderebf18f42013-03-04 22:29:57 -06002956 }
Yan, Zheng44c99752013-09-22 10:28:10 +08002957
2958 reply->hdr.data_len = cpu_to_le32(pagelist->length);
2959 ceph_msg_data_add_pagelist(reply, pagelist);
Sage Weil2f2dc052009-10-06 11:31:09 -07002960 ceph_con_send(&session->s_con, reply);
2961
Sage Weil9abf82b2010-05-10 21:58:38 -07002962 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002963
Sage Weil9abf82b2010-05-10 21:58:38 -07002964 mutex_lock(&mdsc->mutex);
2965 __wake_requests(mdsc, &session->s_waiting);
2966 mutex_unlock(&mdsc->mutex);
2967
Sage Weil2f2dc052009-10-06 11:31:09 -07002968 up_read(&mdsc->snap_rwsem);
Sage Weil2f2dc052009-10-06 11:31:09 -07002969 return;
2970
Sage Weil93cea5b2009-12-23 12:21:51 -08002971fail:
Sage Weil2f2dc052009-10-06 11:31:09 -07002972 ceph_msg_put(reply);
Sage Weil9abf82b2010-05-10 21:58:38 -07002973 up_read(&mdsc->snap_rwsem);
2974 mutex_unlock(&session->s_mutex);
Sage Weil93cea5b2009-12-23 12:21:51 -08002975fail_nomsg:
2976 ceph_pagelist_release(pagelist);
Sage Weil93cea5b2009-12-23 12:21:51 -08002977fail_nopagelist:
Sage Weil9abf82b2010-05-10 21:58:38 -07002978 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
Sage Weil9abf82b2010-05-10 21:58:38 -07002979 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07002980}
2981
2982
2983/*
2984 * compare old and new mdsmaps, kicking requests
2985 * and closing out old connections as necessary
2986 *
2987 * called under mdsc->mutex.
2988 */
2989static void check_new_map(struct ceph_mds_client *mdsc,
2990 struct ceph_mdsmap *newmap,
2991 struct ceph_mdsmap *oldmap)
2992{
2993 int i;
2994 int oldstate, newstate;
2995 struct ceph_mds_session *s;
2996
2997 dout("check_new_map new %u old %u\n",
2998 newmap->m_epoch, oldmap->m_epoch);
2999
3000 for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
3001 if (mdsc->sessions[i] == NULL)
3002 continue;
3003 s = mdsc->sessions[i];
3004 oldstate = ceph_mdsmap_get_state(oldmap, i);
3005 newstate = ceph_mdsmap_get_state(newmap, i);
3006
Sage Weil0deb01c2010-06-17 14:19:01 -07003007 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07003008 i, ceph_mds_state_name(oldstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07003009 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
Sage Weil2f2dc052009-10-06 11:31:09 -07003010 ceph_mds_state_name(newstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07003011 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
John Spraya687eca2014-09-19 13:51:08 +01003012 ceph_session_state_name(s->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07003013
Yan, Zheng3e8f43a2012-09-20 17:42:25 +08003014 if (i >= newmap->m_max_mds ||
3015 memcmp(ceph_mdsmap_get_addr(oldmap, i),
Sage Weil2f2dc052009-10-06 11:31:09 -07003016 ceph_mdsmap_get_addr(newmap, i),
3017 sizeof(struct ceph_entity_addr))) {
3018 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
3019 /* the session never opened, just close it
3020 * out now */
3021 __wake_requests(mdsc, &s->s_waiting);
Sage Weil2600d2d2010-02-22 15:12:16 -08003022 __unregister_session(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07003023 } else {
3024 /* just close it */
3025 mutex_unlock(&mdsc->mutex);
3026 mutex_lock(&s->s_mutex);
3027 mutex_lock(&mdsc->mutex);
3028 ceph_con_close(&s->s_con);
3029 mutex_unlock(&s->s_mutex);
3030 s->s_state = CEPH_MDS_SESSION_RESTARTING;
3031 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003032 } else if (oldstate == newstate) {
3033 continue; /* nothing new with this mds */
3034 }
3035
3036 /*
3037 * send reconnect?
3038 */
3039 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
Sage Weil34b6c852010-05-10 16:31:25 -07003040 newstate >= CEPH_MDS_STATE_RECONNECT) {
3041 mutex_unlock(&mdsc->mutex);
3042 send_mds_reconnect(mdsc, s);
3043 mutex_lock(&mdsc->mutex);
3044 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003045
3046 /*
Sage Weil29790f22010-03-18 14:45:05 -07003047 * kick request on any mds that has gone active.
Sage Weil2f2dc052009-10-06 11:31:09 -07003048 */
3049 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
3050 newstate >= CEPH_MDS_STATE_ACTIVE) {
Sage Weil29790f22010-03-18 14:45:05 -07003051 if (oldstate != CEPH_MDS_STATE_CREATING &&
3052 oldstate != CEPH_MDS_STATE_STARTING)
3053 pr_info("mds%d recovery completed\n", s->s_mds);
3054 kick_requests(mdsc, i);
Sage Weil2f2dc052009-10-06 11:31:09 -07003055 ceph_kick_flushing_caps(mdsc, s);
Sage Weil0dc25702009-11-20 13:43:45 -08003056 wake_up_session_caps(s, 1);
Sage Weil2f2dc052009-10-06 11:31:09 -07003057 }
3058 }
Sage Weilcb170a22010-06-21 13:38:35 -07003059
3060 for (i = 0; i < newmap->m_max_mds && i < mdsc->max_sessions; i++) {
3061 s = mdsc->sessions[i];
3062 if (!s)
3063 continue;
3064 if (!ceph_mdsmap_is_laggy(newmap, i))
3065 continue;
3066 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3067 s->s_state == CEPH_MDS_SESSION_HUNG ||
3068 s->s_state == CEPH_MDS_SESSION_CLOSING) {
3069 dout(" connecting to export targets of laggy mds%d\n",
3070 i);
3071 __open_export_target_sessions(mdsc, s);
3072 }
3073 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003074}
3075
3076
3077
3078/*
3079 * leases
3080 */
3081
3082/*
3083 * caller must hold session s_mutex, dentry->d_lock
3084 */
3085void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
3086{
3087 struct ceph_dentry_info *di = ceph_dentry(dentry);
3088
3089 ceph_put_mds_session(di->lease_session);
3090 di->lease_session = NULL;
3091}
3092
Sage Weil2600d2d2010-02-22 15:12:16 -08003093static void handle_lease(struct ceph_mds_client *mdsc,
3094 struct ceph_mds_session *session,
3095 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07003096{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003097 struct super_block *sb = mdsc->fsc->sb;
Sage Weil2f2dc052009-10-06 11:31:09 -07003098 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07003099 struct dentry *parent, *dentry;
3100 struct ceph_dentry_info *di;
Sage Weil2600d2d2010-02-22 15:12:16 -08003101 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07003102 struct ceph_mds_lease *h = msg->front.iov_base;
Sage Weil1e5ea232010-06-04 10:05:40 -07003103 u32 seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003104 struct ceph_vino vino;
Sage Weil2f2dc052009-10-06 11:31:09 -07003105 struct qstr dname;
3106 int release = 0;
3107
Sage Weil2f2dc052009-10-06 11:31:09 -07003108 dout("handle_lease from mds%d\n", mds);
3109
3110 /* decode */
3111 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
3112 goto bad;
3113 vino.ino = le64_to_cpu(h->ino);
3114 vino.snap = CEPH_NOSNAP;
Sage Weil1e5ea232010-06-04 10:05:40 -07003115 seq = le32_to_cpu(h->seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003116 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
3117 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
3118 if (dname.len != get_unaligned_le32(h+1))
3119 goto bad;
3120
Sage Weil2f2dc052009-10-06 11:31:09 -07003121 /* lookup inode */
3122 inode = ceph_find_inode(sb, vino);
Sage Weil2f90b852011-07-26 11:28:25 -07003123 dout("handle_lease %s, ino %llx %p %.*s\n",
3124 ceph_lease_op_name(h->action), vino.ino, inode,
Sage Weil1e5ea232010-06-04 10:05:40 -07003125 dname.len, dname.name);
Yan, Zheng6cd3bca2014-09-17 07:45:12 +08003126
3127 mutex_lock(&session->s_mutex);
3128 session->s_seq++;
3129
Sage Weil2f2dc052009-10-06 11:31:09 -07003130 if (inode == NULL) {
3131 dout("handle_lease no inode %llx\n", vino.ino);
3132 goto release;
3133 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003134
3135 /* dentry */
3136 parent = d_find_alias(inode);
3137 if (!parent) {
3138 dout("no parent dentry on inode %p\n", inode);
3139 WARN_ON(1);
3140 goto release; /* hrm... */
3141 }
3142 dname.hash = full_name_hash(dname.name, dname.len);
3143 dentry = d_lookup(parent, &dname);
3144 dput(parent);
3145 if (!dentry)
3146 goto release;
3147
3148 spin_lock(&dentry->d_lock);
3149 di = ceph_dentry(dentry);
3150 switch (h->action) {
3151 case CEPH_MDS_LEASE_REVOKE:
Sage Weil3d8eb7a2011-11-11 09:48:53 -08003152 if (di->lease_session == session) {
Sage Weil1e5ea232010-06-04 10:05:40 -07003153 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
3154 h->seq = cpu_to_le32(di->lease_seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003155 __ceph_mdsc_drop_dentry_lease(dentry);
3156 }
3157 release = 1;
3158 break;
3159
3160 case CEPH_MDS_LEASE_RENEW:
Sage Weil3d8eb7a2011-11-11 09:48:53 -08003161 if (di->lease_session == session &&
Sage Weil2f2dc052009-10-06 11:31:09 -07003162 di->lease_gen == session->s_cap_gen &&
3163 di->lease_renew_from &&
3164 di->lease_renew_after == 0) {
3165 unsigned long duration =
Nicholas Mc Guire3563dbdd2015-02-06 06:52:17 -05003166 msecs_to_jiffies(le32_to_cpu(h->duration_ms));
Sage Weil2f2dc052009-10-06 11:31:09 -07003167
Sage Weil1e5ea232010-06-04 10:05:40 -07003168 di->lease_seq = seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003169 dentry->d_time = di->lease_renew_from + duration;
3170 di->lease_renew_after = di->lease_renew_from +
3171 (duration >> 1);
3172 di->lease_renew_from = 0;
3173 }
3174 break;
3175 }
3176 spin_unlock(&dentry->d_lock);
3177 dput(dentry);
3178
3179 if (!release)
3180 goto out;
3181
3182release:
3183 /* let's just reuse the same message */
3184 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
3185 ceph_msg_get(msg);
3186 ceph_con_send(&session->s_con, msg);
3187
3188out:
3189 iput(inode);
3190 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003191 return;
3192
3193bad:
3194 pr_err("corrupt lease message\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08003195 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003196}
3197
3198void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
3199 struct inode *inode,
3200 struct dentry *dentry, char action,
3201 u32 seq)
3202{
3203 struct ceph_msg *msg;
3204 struct ceph_mds_lease *lease;
3205 int len = sizeof(*lease) + sizeof(u32);
3206 int dnamelen = 0;
3207
3208 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
3209 inode, dentry, ceph_lease_op_name(action), session->s_mds);
3210 dnamelen = dentry->d_name.len;
3211 len += dnamelen;
3212
Sage Weilb61c2762011-08-09 15:03:46 -07003213 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07003214 if (!msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07003215 return;
3216 lease = msg->front.iov_base;
3217 lease->action = action;
Sage Weil2f2dc052009-10-06 11:31:09 -07003218 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
3219 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
3220 lease->seq = cpu_to_le32(seq);
3221 put_unaligned_le32(dnamelen, lease + 1);
3222 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
3223
3224 /*
3225 * if this is a preemptive lease RELEASE, no need to
3226 * flush request stream, since the actual request will
3227 * soon follow.
3228 */
3229 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
3230
3231 ceph_con_send(&session->s_con, msg);
3232}
3233
3234/*
3235 * Preemptively release a lease we expect to invalidate anyway.
3236 * Pass @inode always, @dentry is optional.
3237 */
3238void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
Sage Weil2f90b852011-07-26 11:28:25 -07003239 struct dentry *dentry)
Sage Weil2f2dc052009-10-06 11:31:09 -07003240{
3241 struct ceph_dentry_info *di;
3242 struct ceph_mds_session *session;
3243 u32 seq;
3244
3245 BUG_ON(inode == NULL);
3246 BUG_ON(dentry == NULL);
Sage Weil2f2dc052009-10-06 11:31:09 -07003247
3248 /* is dentry lease valid? */
3249 spin_lock(&dentry->d_lock);
3250 di = ceph_dentry(dentry);
3251 if (!di || !di->lease_session ||
3252 di->lease_session->s_mds < 0 ||
3253 di->lease_gen != di->lease_session->s_cap_gen ||
3254 !time_before(jiffies, dentry->d_time)) {
3255 dout("lease_release inode %p dentry %p -- "
Sage Weil2f90b852011-07-26 11:28:25 -07003256 "no lease\n",
3257 inode, dentry);
Sage Weil2f2dc052009-10-06 11:31:09 -07003258 spin_unlock(&dentry->d_lock);
3259 return;
3260 }
3261
3262 /* we do have a lease on this dentry; note mds and seq */
3263 session = ceph_get_mds_session(di->lease_session);
3264 seq = di->lease_seq;
3265 __ceph_mdsc_drop_dentry_lease(dentry);
3266 spin_unlock(&dentry->d_lock);
3267
Sage Weil2f90b852011-07-26 11:28:25 -07003268 dout("lease_release inode %p dentry %p to mds%d\n",
3269 inode, dentry, session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003270 ceph_mdsc_lease_send_msg(session, inode, dentry,
3271 CEPH_MDS_LEASE_RELEASE, seq);
3272 ceph_put_mds_session(session);
3273}
3274
3275/*
3276 * drop all leases (and dentry refs) in preparation for umount
3277 */
3278static void drop_leases(struct ceph_mds_client *mdsc)
3279{
3280 int i;
3281
3282 dout("drop_leases\n");
3283 mutex_lock(&mdsc->mutex);
3284 for (i = 0; i < mdsc->max_sessions; i++) {
3285 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
3286 if (!s)
3287 continue;
3288 mutex_unlock(&mdsc->mutex);
3289 mutex_lock(&s->s_mutex);
3290 mutex_unlock(&s->s_mutex);
3291 ceph_put_mds_session(s);
3292 mutex_lock(&mdsc->mutex);
3293 }
3294 mutex_unlock(&mdsc->mutex);
3295}
3296
3297
3298
3299/*
3300 * delayed work -- periodically trim expired leases, renew caps with mds
3301 */
3302static void schedule_delayed(struct ceph_mds_client *mdsc)
3303{
3304 int delay = 5;
3305 unsigned hz = round_jiffies_relative(HZ * delay);
3306 schedule_delayed_work(&mdsc->delayed_work, hz);
3307}
3308
3309static void delayed_work(struct work_struct *work)
3310{
3311 int i;
3312 struct ceph_mds_client *mdsc =
3313 container_of(work, struct ceph_mds_client, delayed_work.work);
3314 int renew_interval;
3315 int renew_caps;
3316
3317 dout("mdsc delayed_work\n");
Sage Weilafcdaea2009-10-14 14:27:38 -07003318 ceph_check_delayed_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003319
3320 mutex_lock(&mdsc->mutex);
3321 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
3322 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
3323 mdsc->last_renew_caps);
3324 if (renew_caps)
3325 mdsc->last_renew_caps = jiffies;
3326
3327 for (i = 0; i < mdsc->max_sessions; i++) {
3328 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
3329 if (s == NULL)
3330 continue;
3331 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
3332 dout("resending session close request for mds%d\n",
3333 s->s_mds);
3334 request_close_session(mdsc, s);
3335 ceph_put_mds_session(s);
3336 continue;
3337 }
3338 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
3339 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
3340 s->s_state = CEPH_MDS_SESSION_HUNG;
3341 pr_info("mds%d hung\n", s->s_mds);
3342 }
3343 }
3344 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
3345 /* this mds is failed or recovering, just wait */
3346 ceph_put_mds_session(s);
3347 continue;
3348 }
3349 mutex_unlock(&mdsc->mutex);
3350
3351 mutex_lock(&s->s_mutex);
3352 if (renew_caps)
3353 send_renew_caps(mdsc, s);
3354 else
3355 ceph_con_keepalive(&s->s_con);
Sage Weilee6b2722010-06-10 12:55:52 -07003356 ceph_add_cap_releases(mdsc, s);
Sage Weilaab53dd2010-03-17 16:30:21 -07003357 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3358 s->s_state == CEPH_MDS_SESSION_HUNG)
Sage Weil3d7ded42010-06-09 16:47:10 -07003359 ceph_send_cap_releases(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07003360 mutex_unlock(&s->s_mutex);
3361 ceph_put_mds_session(s);
3362
3363 mutex_lock(&mdsc->mutex);
3364 }
3365 mutex_unlock(&mdsc->mutex);
3366
3367 schedule_delayed(mdsc);
3368}
3369
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003370int ceph_mdsc_init(struct ceph_fs_client *fsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07003371
Sage Weil2f2dc052009-10-06 11:31:09 -07003372{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003373 struct ceph_mds_client *mdsc;
3374
3375 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
3376 if (!mdsc)
3377 return -ENOMEM;
3378 mdsc->fsc = fsc;
3379 fsc->mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07003380 mutex_init(&mdsc->mutex);
3381 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
majianpengfb3101b2013-06-25 14:48:19 +08003382 if (mdsc->mdsmap == NULL) {
3383 kfree(mdsc);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08003384 return -ENOMEM;
majianpengfb3101b2013-06-25 14:48:19 +08003385 }
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08003386
Sage Weil2f2dc052009-10-06 11:31:09 -07003387 init_completion(&mdsc->safe_umount_waiters);
Sage Weilf3c60c52010-08-11 14:51:23 -07003388 init_waitqueue_head(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003389 INIT_LIST_HEAD(&mdsc->waiting_for_map);
3390 mdsc->sessions = NULL;
Yan, Zheng86d8f672015-01-09 17:00:42 +08003391 atomic_set(&mdsc->num_sessions, 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07003392 mdsc->max_sessions = 0;
3393 mdsc->stopping = 0;
3394 init_rwsem(&mdsc->snap_rwsem);
Sage Weila105f002010-02-15 14:37:55 -08003395 mdsc->snap_realms = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07003396 INIT_LIST_HEAD(&mdsc->snap_empty);
3397 spin_lock_init(&mdsc->snap_empty_lock);
3398 mdsc->last_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{
3430 struct ceph_mds_request *req;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003431 struct ceph_fs_client *fsc = mdsc->fsc;
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,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003439 fsc->client->options->mount_timeout * HZ);
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;
Sage Weil44ca18f2010-02-15 12:08:46 -08003492 if ((req->r_op & CEPH_MDS_OP_WRITE)) {
3493 /* write op */
3494 ceph_mdsc_get_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07003495 if (nextreq)
3496 ceph_mdsc_get_request(nextreq);
Sage Weil44ca18f2010-02-15 12:08:46 -08003497 mutex_unlock(&mdsc->mutex);
3498 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3499 req->r_tid, want_tid);
3500 wait_for_completion(&req->r_safe_completion);
3501 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003502 ceph_mdsc_put_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07003503 if (!nextreq)
3504 break; /* next dne before, so we're done! */
3505 if (RB_EMPTY_NODE(&nextreq->r_node)) {
3506 /* next request was removed from tree */
3507 ceph_mdsc_put_request(nextreq);
3508 goto restart;
3509 }
3510 ceph_mdsc_put_request(nextreq); /* won't go away */
Sage Weil44ca18f2010-02-15 12:08:46 -08003511 }
Sage Weil80fc7312010-03-16 15:28:54 -07003512 req = nextreq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003513 }
3514 mutex_unlock(&mdsc->mutex);
3515 dout("wait_unsafe_requests done\n");
3516}
3517
3518void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
3519{
3520 u64 want_tid, want_flush;
3521
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003522 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
Sage Weil56b7cf92010-05-03 15:22:00 -07003523 return;
3524
Sage Weil2f2dc052009-10-06 11:31:09 -07003525 dout("sync\n");
3526 mutex_lock(&mdsc->mutex);
3527 want_tid = mdsc->last_tid;
Sage Weil2f2dc052009-10-06 11:31:09 -07003528 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003529
Sage Weilafcdaea2009-10-14 14:27:38 -07003530 ceph_flush_dirty_caps(mdsc);
Yan, Zhengd3383a82015-01-08 21:30:12 +08003531 spin_lock(&mdsc->cap_dirty_lock);
3532 want_flush = mdsc->cap_flush_seq;
3533 spin_unlock(&mdsc->cap_dirty_lock);
3534
3535 dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
Sage Weil2f2dc052009-10-06 11:31:09 -07003536
3537 wait_unsafe_requests(mdsc, want_tid);
Yan, Zhengd3383a82015-01-08 21:30:12 +08003538 wait_caps_flush(mdsc, want_flush);
Sage Weil2f2dc052009-10-06 11:31:09 -07003539}
3540
Sage Weilf3c60c52010-08-11 14:51:23 -07003541/*
3542 * true if all sessions are closed, or we force unmount
3543 */
H Hartley Sweeten7fd7d102011-09-23 13:22:11 -07003544static bool done_closing_sessions(struct ceph_mds_client *mdsc)
Sage Weilf3c60c52010-08-11 14:51:23 -07003545{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003546 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
Sage Weilf3c60c52010-08-11 14:51:23 -07003547 return true;
Yan, Zheng86d8f672015-01-09 17:00:42 +08003548 return atomic_read(&mdsc->num_sessions) == 0;
Sage Weilf3c60c52010-08-11 14:51:23 -07003549}
Sage Weil2f2dc052009-10-06 11:31:09 -07003550
3551/*
3552 * called after sb is ro.
3553 */
3554void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
3555{
3556 struct ceph_mds_session *session;
3557 int i;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003558 struct ceph_fs_client *fsc = mdsc->fsc;
3559 unsigned long timeout = fsc->client->options->mount_timeout * HZ;
Sage Weil2f2dc052009-10-06 11:31:09 -07003560
3561 dout("close_sessions\n");
3562
Sage Weil2f2dc052009-10-06 11:31:09 -07003563 /* close sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07003564 mutex_lock(&mdsc->mutex);
3565 for (i = 0; i < mdsc->max_sessions; i++) {
3566 session = __ceph_lookup_mds_session(mdsc, i);
3567 if (!session)
3568 continue;
Sage Weil2f2dc052009-10-06 11:31:09 -07003569 mutex_unlock(&mdsc->mutex);
Sage Weilf3c60c52010-08-11 14:51:23 -07003570 mutex_lock(&session->s_mutex);
3571 __close_session(mdsc, session);
3572 mutex_unlock(&session->s_mutex);
3573 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003574 mutex_lock(&mdsc->mutex);
3575 }
Sage Weilf3c60c52010-08-11 14:51:23 -07003576 mutex_unlock(&mdsc->mutex);
3577
3578 dout("waiting for sessions to close\n");
3579 wait_event_timeout(mdsc->session_close_wq, done_closing_sessions(mdsc),
3580 timeout);
Sage Weil2f2dc052009-10-06 11:31:09 -07003581
3582 /* tear down remaining sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07003583 mutex_lock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003584 for (i = 0; i < mdsc->max_sessions; i++) {
3585 if (mdsc->sessions[i]) {
3586 session = get_session(mdsc->sessions[i]);
Sage Weil2600d2d2010-02-22 15:12:16 -08003587 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003588 mutex_unlock(&mdsc->mutex);
3589 mutex_lock(&session->s_mutex);
3590 remove_session_caps(session);
3591 mutex_unlock(&session->s_mutex);
3592 ceph_put_mds_session(session);
3593 mutex_lock(&mdsc->mutex);
3594 }
3595 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003596 WARN_ON(!list_empty(&mdsc->cap_delay_list));
Sage Weil2f2dc052009-10-06 11:31:09 -07003597 mutex_unlock(&mdsc->mutex);
3598
3599 ceph_cleanup_empty_realms(mdsc);
3600
3601 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3602
3603 dout("stopped\n");
3604}
3605
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003606static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07003607{
3608 dout("stop\n");
3609 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3610 if (mdsc->mdsmap)
3611 ceph_mdsmap_destroy(mdsc->mdsmap);
3612 kfree(mdsc->sessions);
Yehuda Sadeh37151662010-06-17 16:16:12 -07003613 ceph_caps_finalize(mdsc);
Yan, Zheng10183a62015-04-27 15:33:28 +08003614 ceph_pool_perm_destroy(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003615}
3616
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003617void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
3618{
3619 struct ceph_mds_client *mdsc = fsc->mdsc;
3620
Sage Weilef550f62011-03-25 13:27:48 -07003621 dout("mdsc_destroy %p\n", mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003622 ceph_mdsc_stop(mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07003623
3624 /* flush out any connection work with references to us */
3625 ceph_msgr_flush();
3626
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003627 fsc->mdsc = NULL;
3628 kfree(mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07003629 dout("mdsc_destroy %p done\n", mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003630}
3631
Sage Weil2f2dc052009-10-06 11:31:09 -07003632
3633/*
3634 * handle mds map update.
3635 */
3636void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3637{
3638 u32 epoch;
3639 u32 maplen;
3640 void *p = msg->front.iov_base;
3641 void *end = p + msg->front.iov_len;
3642 struct ceph_mdsmap *newmap, *oldmap;
3643 struct ceph_fsid fsid;
3644 int err = -EINVAL;
3645
3646 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
3647 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003648 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
Sage Weil07433042009-11-18 16:50:41 -08003649 return;
Sage Weilc89136e2009-10-14 09:59:09 -07003650 epoch = ceph_decode_32(&p);
3651 maplen = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07003652 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
3653
3654 /* do we need it? */
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003655 ceph_monc_got_mdsmap(&mdsc->fsc->client->monc, epoch);
Sage Weil2f2dc052009-10-06 11:31:09 -07003656 mutex_lock(&mdsc->mutex);
3657 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
3658 dout("handle_map epoch %u <= our %u\n",
3659 epoch, mdsc->mdsmap->m_epoch);
3660 mutex_unlock(&mdsc->mutex);
3661 return;
3662 }
3663
3664 newmap = ceph_mdsmap_decode(&p, end);
3665 if (IS_ERR(newmap)) {
3666 err = PTR_ERR(newmap);
3667 goto bad_unlock;
3668 }
3669
3670 /* swap into place */
3671 if (mdsc->mdsmap) {
3672 oldmap = mdsc->mdsmap;
3673 mdsc->mdsmap = newmap;
3674 check_new_map(mdsc, newmap, oldmap);
3675 ceph_mdsmap_destroy(oldmap);
3676 } else {
3677 mdsc->mdsmap = newmap; /* first mds map */
3678 }
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003679 mdsc->fsc->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
Sage Weil2f2dc052009-10-06 11:31:09 -07003680
3681 __wake_requests(mdsc, &mdsc->waiting_for_map);
3682
3683 mutex_unlock(&mdsc->mutex);
3684 schedule_delayed(mdsc);
3685 return;
3686
3687bad_unlock:
3688 mutex_unlock(&mdsc->mutex);
3689bad:
3690 pr_err("error decoding mdsmap %d\n", err);
3691 return;
3692}
3693
3694static struct ceph_connection *con_get(struct ceph_connection *con)
3695{
3696 struct ceph_mds_session *s = con->private;
3697
3698 if (get_session(s)) {
Sage Weil2600d2d2010-02-22 15:12:16 -08003699 dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -07003700 return con;
3701 }
3702 dout("mdsc con_get %p FAIL\n", s);
3703 return NULL;
3704}
3705
3706static void con_put(struct ceph_connection *con)
3707{
3708 struct ceph_mds_session *s = con->private;
3709
Henry C Chang7d8e18a2011-05-11 10:29:52 +00003710 dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref) - 1);
Sage Weil2f2dc052009-10-06 11:31:09 -07003711 ceph_put_mds_session(s);
3712}
3713
3714/*
3715 * if the client is unresponsive for long enough, the mds will kill
3716 * the session entirely.
3717 */
3718static void peer_reset(struct ceph_connection *con)
3719{
3720 struct ceph_mds_session *s = con->private;
Sage Weil7e70f0e2010-03-18 13:59:12 -07003721 struct ceph_mds_client *mdsc = s->s_mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07003722
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07003723 pr_warn("mds%d closed our session\n", s->s_mds);
Sage Weil7e70f0e2010-03-18 13:59:12 -07003724 send_mds_reconnect(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07003725}
3726
3727static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
3728{
3729 struct ceph_mds_session *s = con->private;
3730 struct ceph_mds_client *mdsc = s->s_mdsc;
3731 int type = le16_to_cpu(msg->hdr.type);
3732
Sage Weil2600d2d2010-02-22 15:12:16 -08003733 mutex_lock(&mdsc->mutex);
3734 if (__verify_registered_session(mdsc, s) < 0) {
3735 mutex_unlock(&mdsc->mutex);
3736 goto out;
3737 }
3738 mutex_unlock(&mdsc->mutex);
3739
Sage Weil2f2dc052009-10-06 11:31:09 -07003740 switch (type) {
3741 case CEPH_MSG_MDS_MAP:
3742 ceph_mdsc_handle_map(mdsc, msg);
3743 break;
3744 case CEPH_MSG_CLIENT_SESSION:
3745 handle_session(s, msg);
3746 break;
3747 case CEPH_MSG_CLIENT_REPLY:
3748 handle_reply(s, msg);
3749 break;
3750 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
Sage Weil2600d2d2010-02-22 15:12:16 -08003751 handle_forward(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003752 break;
3753 case CEPH_MSG_CLIENT_CAPS:
3754 ceph_handle_caps(s, msg);
3755 break;
3756 case CEPH_MSG_CLIENT_SNAP:
Sage Weil2600d2d2010-02-22 15:12:16 -08003757 ceph_handle_snap(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003758 break;
3759 case CEPH_MSG_CLIENT_LEASE:
Sage Weil2600d2d2010-02-22 15:12:16 -08003760 handle_lease(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003761 break;
3762
3763 default:
3764 pr_err("received unknown message type %d %s\n", type,
3765 ceph_msg_type_name(type));
3766 }
Sage Weil2600d2d2010-02-22 15:12:16 -08003767out:
Sage Weil2f2dc052009-10-06 11:31:09 -07003768 ceph_msg_put(msg);
3769}
3770
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003771/*
3772 * authentication
3773 */
Alex Eldera3530df2012-05-16 15:16:39 -05003774
3775/*
3776 * Note: returned pointer is the address of a structure that's
3777 * managed separately. Caller must *not* attempt to free it.
3778 */
3779static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05003780 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003781{
3782 struct ceph_mds_session *s = con->private;
3783 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003784 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05003785 struct ceph_auth_handshake *auth = &s->s_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003786
Alex Elder74f18692012-05-16 15:16:39 -05003787 if (force_new && auth->authorizer) {
Sage Weil27859f92013-03-25 10:26:14 -07003788 ceph_auth_destroy_authorizer(ac, auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05003789 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003790 }
Sage Weil27859f92013-03-25 10:26:14 -07003791 if (!auth->authorizer) {
3792 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
3793 auth);
Sage Weil0bed9b52013-03-25 10:26:01 -07003794 if (ret)
3795 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07003796 } else {
3797 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
3798 auth);
Alex Eldera2556512012-05-16 15:16:39 -05003799 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05003800 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003801 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003802 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05003803
Alex Eldera3530df2012-05-16 15:16:39 -05003804 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003805}
3806
3807
3808static int verify_authorizer_reply(struct ceph_connection *con, int len)
3809{
3810 struct ceph_mds_session *s = con->private;
3811 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003812 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003813
Sage Weil27859f92013-03-25 10:26:14 -07003814 return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003815}
3816
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003817static int invalidate_authorizer(struct ceph_connection *con)
3818{
3819 struct ceph_mds_session *s = con->private;
3820 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003821 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003822
Sage Weil27859f92013-03-25 10:26:14 -07003823 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003824
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003825 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003826}
3827
Alex Elder53ded492013-03-01 18:00:14 -06003828static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
3829 struct ceph_msg_header *hdr, int *skip)
3830{
3831 struct ceph_msg *msg;
3832 int type = (int) le16_to_cpu(hdr->type);
3833 int front_len = (int) le32_to_cpu(hdr->front_len);
3834
3835 if (con->in_msg)
3836 return con->in_msg;
3837
3838 *skip = 0;
3839 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
3840 if (!msg) {
3841 pr_err("unable to allocate msg type %d len %d\n",
3842 type, front_len);
3843 return NULL;
3844 }
Alex Elder53ded492013-03-01 18:00:14 -06003845
3846 return msg;
3847}
3848
Yan, Zheng33d07332014-11-04 16:33:37 +08003849static int sign_message(struct ceph_connection *con, struct ceph_msg *msg)
3850{
3851 struct ceph_mds_session *s = con->private;
3852 struct ceph_auth_handshake *auth = &s->s_auth;
3853 return ceph_auth_sign_message(auth, msg);
3854}
3855
3856static int check_message_signature(struct ceph_connection *con, struct ceph_msg *msg)
3857{
3858 struct ceph_mds_session *s = con->private;
3859 struct ceph_auth_handshake *auth = &s->s_auth;
3860 return ceph_auth_check_message_signature(auth, msg);
3861}
3862
Tobias Klauser9e327892010-05-20 10:40:19 +02003863static const struct ceph_connection_operations mds_con_ops = {
Sage Weil2f2dc052009-10-06 11:31:09 -07003864 .get = con_get,
3865 .put = con_put,
3866 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003867 .get_authorizer = get_authorizer,
3868 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003869 .invalidate_authorizer = invalidate_authorizer,
Sage Weil2f2dc052009-10-06 11:31:09 -07003870 .peer_reset = peer_reset,
Alex Elder53ded492013-03-01 18:00:14 -06003871 .alloc_msg = mds_alloc_msg,
Yan, Zheng33d07332014-11-04 16:33:37 +08003872 .sign_message = sign_message,
3873 .check_message_signature = check_message_signature,
Sage Weil2f2dc052009-10-06 11:31:09 -07003874};
3875
Sage Weil2f2dc052009-10-06 11:31:09 -07003876/* eof */