blob: 1b468250e94752e6eedf63283cba95a236fb0380 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002#include <linux/ceph/ceph_debug.h>
Sage Weil2f2dc052009-10-06 11:31:09 -07003
Sage Weil496e5952010-09-22 19:57:10 -07004#include <linux/fs.h>
Sage Weil2f2dc052009-10-06 11:31:09 -07005#include <linux/wait.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09006#include <linux/slab.h>
Yan, Zheng54008392014-03-29 13:41:15 +08007#include <linux/gfp.h>
Sage Weil2f2dc052009-10-06 11:31:09 -07008#include <linux/sched.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07009#include <linux/debugfs.h>
10#include <linux/seq_file.h>
Yan, Zheng3e0708b2015-05-22 16:38:02 +080011#include <linux/ratelimit.h>
Sage Weil2f2dc052009-10-06 11:31:09 -070012
Sage Weil2f2dc052009-10-06 11:31:09 -070013#include "super.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070014#include "mds_client.h"
15
Sage Weil1fe60e52012-07-30 16:23:22 -070016#include <linux/ceph/ceph_features.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070017#include <linux/ceph/messenger.h>
18#include <linux/ceph/decode.h>
19#include <linux/ceph/pagelist.h>
20#include <linux/ceph/auth.h>
21#include <linux/ceph/debugfs.h>
Sage Weil2f2dc052009-10-06 11:31:09 -070022
23/*
24 * A cluster of MDS (metadata server) daemons is responsible for
25 * managing the file system namespace (the directory hierarchy and
26 * inodes) and for coordinating shared access to storage. Metadata is
27 * partitioning hierarchically across a number of servers, and that
28 * partition varies over time as the cluster adjusts the distribution
29 * in order to balance load.
30 *
31 * The MDS client is primarily responsible to managing synchronous
32 * metadata requests for operations like open, unlink, and so forth.
33 * If there is a MDS failure, we find out about it when we (possibly
34 * request and) receive a new MDS map, and can resubmit affected
35 * requests.
36 *
37 * For the most part, though, we take advantage of a lossless
38 * communications channel to the MDS, and do not need to worry about
39 * timing out or resubmitting requests.
40 *
41 * We maintain a stateful "session" with each MDS we interact with.
42 * Within each session, we sent periodic heartbeat messages to ensure
43 * any capabilities or leases we have been issues remain valid. If
44 * the session times out and goes stale, our leases and capabilities
45 * are no longer valid.
46 */
47
Sage Weil20cb34a2010-05-12 15:21:32 -070048struct ceph_reconnect_state {
Yan, Zheng44c99752013-09-22 10:28:10 +080049 int nr_caps;
Sage Weil20cb34a2010-05-12 15:21:32 -070050 struct ceph_pagelist *pagelist;
Yan, Zheng121f22a2016-07-04 22:05:18 +080051 unsigned msg_version;
Sage Weil20cb34a2010-05-12 15:21:32 -070052};
53
Sage Weil2f2dc052009-10-06 11:31:09 -070054static void __wake_requests(struct ceph_mds_client *mdsc,
55 struct list_head *head);
56
Tobias Klauser9e327892010-05-20 10:40:19 +020057static const struct ceph_connection_operations mds_con_ops;
Sage Weil2f2dc052009-10-06 11:31:09 -070058
59
60/*
61 * mds reply parsing
62 */
63
64/*
65 * parse individual inode info
66 */
67static int parse_reply_info_in(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -080068 struct ceph_mds_reply_info_in *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +020069 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -070070{
71 int err = -EIO;
72
73 info->in = *p;
74 *p += sizeof(struct ceph_mds_reply_inode) +
75 sizeof(*info->in->fragtree.splits) *
76 le32_to_cpu(info->in->fragtree.nsplits);
77
78 ceph_decode_32_safe(p, end, info->symlink_len, bad);
79 ceph_decode_need(p, end, info->symlink_len, bad);
80 info->symlink = *p;
81 *p += info->symlink_len;
82
Sage Weil14303d22010-12-14 17:37:52 -080083 if (features & CEPH_FEATURE_DIRLAYOUTHASH)
84 ceph_decode_copy_safe(p, end, &info->dir_layout,
85 sizeof(info->dir_layout), bad);
86 else
87 memset(&info->dir_layout, 0, sizeof(info->dir_layout));
88
Sage Weil2f2dc052009-10-06 11:31:09 -070089 ceph_decode_32_safe(p, end, info->xattr_len, bad);
90 ceph_decode_need(p, end, info->xattr_len, bad);
91 info->xattr_data = *p;
92 *p += info->xattr_len;
Yan, Zhengfb01d1f2014-11-14 21:29:55 +080093
94 if (features & CEPH_FEATURE_MDS_INLINE_DATA) {
95 ceph_decode_64_safe(p, end, info->inline_version, bad);
96 ceph_decode_32_safe(p, end, info->inline_len, bad);
97 ceph_decode_need(p, end, info->inline_len, bad);
98 info->inline_data = *p;
99 *p += info->inline_len;
100 } else
101 info->inline_version = CEPH_INLINE_NONE;
102
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800103 info->pool_ns_len = 0;
104 info->pool_ns_data = NULL;
Yan, Zheng5ea5c5e2016-02-14 18:06:41 +0800105 if (features & CEPH_FEATURE_FS_FILE_LAYOUT_V2) {
106 ceph_decode_32_safe(p, end, info->pool_ns_len, bad);
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800107 if (info->pool_ns_len > 0) {
108 ceph_decode_need(p, end, info->pool_ns_len, bad);
109 info->pool_ns_data = *p;
110 *p += info->pool_ns_len;
111 }
Yan, Zheng5ea5c5e2016-02-14 18:06:41 +0800112 }
113
Sage Weil2f2dc052009-10-06 11:31:09 -0700114 return 0;
115bad:
116 return err;
117}
118
119/*
120 * parse a normal reply, which may contain a (dir+)dentry and/or a
121 * target inode.
122 */
123static int parse_reply_info_trace(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800124 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200125 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700126{
127 int err;
128
129 if (info->head->is_dentry) {
Sage Weil14303d22010-12-14 17:37:52 -0800130 err = parse_reply_info_in(p, end, &info->diri, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700131 if (err < 0)
132 goto out_bad;
133
134 if (unlikely(*p + sizeof(*info->dirfrag) > end))
135 goto bad;
136 info->dirfrag = *p;
137 *p += sizeof(*info->dirfrag) +
138 sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
139 if (unlikely(*p > end))
140 goto bad;
141
142 ceph_decode_32_safe(p, end, info->dname_len, bad);
143 ceph_decode_need(p, end, info->dname_len, bad);
144 info->dname = *p;
145 *p += info->dname_len;
146 info->dlease = *p;
147 *p += sizeof(*info->dlease);
148 }
149
150 if (info->head->is_target) {
Sage Weil14303d22010-12-14 17:37:52 -0800151 err = parse_reply_info_in(p, end, &info->targeti, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700152 if (err < 0)
153 goto out_bad;
154 }
155
156 if (unlikely(*p != end))
157 goto bad;
158 return 0;
159
160bad:
161 err = -EIO;
162out_bad:
163 pr_err("problem parsing mds trace %d\n", err);
164 return err;
165}
166
167/*
168 * parse readdir results
169 */
170static int parse_reply_info_dir(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800171 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200172 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700173{
174 u32 num, i = 0;
175 int err;
176
177 info->dir_dir = *p;
178 if (*p + sizeof(*info->dir_dir) > end)
179 goto bad;
180 *p += sizeof(*info->dir_dir) +
181 sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
182 if (*p > end)
183 goto bad;
184
185 ceph_decode_need(p, end, sizeof(num) + 2, bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700186 num = ceph_decode_32(p);
Yan, Zheng956d39d2016-04-27 17:48:30 +0800187 {
188 u16 flags = ceph_decode_16(p);
189 info->dir_end = !!(flags & CEPH_READDIR_FRAG_END);
190 info->dir_complete = !!(flags & CEPH_READDIR_FRAG_COMPLETE);
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800191 info->hash_order = !!(flags & CEPH_READDIR_HASH_ORDER);
Yan, Zheng79162542017-04-05 12:54:05 -0400192 info->offset_hash = !!(flags & CEPH_READDIR_OFFSET_HASH);
Yan, Zheng956d39d2016-04-27 17:48:30 +0800193 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700194 if (num == 0)
195 goto done;
196
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800197 BUG_ON(!info->dir_entries);
198 if ((unsigned long)(info->dir_entries + num) >
199 (unsigned long)info->dir_entries + info->dir_buf_size) {
Yan, Zheng54008392014-03-29 13:41:15 +0800200 pr_err("dir contents are larger than expected\n");
201 WARN_ON(1);
202 goto bad;
203 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700204
Yan, Zheng54008392014-03-29 13:41:15 +0800205 info->dir_nr = num;
Sage Weil2f2dc052009-10-06 11:31:09 -0700206 while (num) {
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800207 struct ceph_mds_reply_dir_entry *rde = info->dir_entries + i;
Sage Weil2f2dc052009-10-06 11:31:09 -0700208 /* dentry */
209 ceph_decode_need(p, end, sizeof(u32)*2, bad);
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800210 rde->name_len = ceph_decode_32(p);
211 ceph_decode_need(p, end, rde->name_len, bad);
212 rde->name = *p;
213 *p += rde->name_len;
214 dout("parsed dir dname '%.*s'\n", rde->name_len, rde->name);
215 rde->lease = *p;
Sage Weil2f2dc052009-10-06 11:31:09 -0700216 *p += sizeof(struct ceph_mds_reply_lease);
217
218 /* inode */
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800219 err = parse_reply_info_in(p, end, &rde->inode, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700220 if (err < 0)
221 goto out_bad;
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800222 /* ceph_readdir_prepopulate() will update it */
223 rde->offset = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -0700224 i++;
225 num--;
226 }
227
228done:
229 if (*p != end)
230 goto bad;
231 return 0;
232
233bad:
234 err = -EIO;
235out_bad:
236 pr_err("problem parsing dir contents %d\n", err);
237 return err;
238}
239
240/*
Herb Shiu25933ab2010-12-01 14:14:38 -0800241 * parse fcntl F_GETLK results
242 */
243static int parse_reply_info_filelock(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800244 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200245 u64 features)
Herb Shiu25933ab2010-12-01 14:14:38 -0800246{
247 if (*p + sizeof(*info->filelock_reply) > end)
248 goto bad;
249
250 info->filelock_reply = *p;
251 *p += sizeof(*info->filelock_reply);
252
253 if (unlikely(*p != end))
254 goto bad;
255 return 0;
256
257bad:
258 return -EIO;
259}
260
261/*
Sam Lang6e8575f2012-12-28 09:56:46 -0800262 * parse create results
263 */
264static int parse_reply_info_create(void **p, void *end,
265 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200266 u64 features)
Sam Lang6e8575f2012-12-28 09:56:46 -0800267{
268 if (features & CEPH_FEATURE_REPLY_CREATE_INODE) {
269 if (*p == end) {
270 info->has_create_ino = false;
271 } else {
272 info->has_create_ino = true;
273 info->ino = ceph_decode_64(p);
274 }
275 }
276
277 if (unlikely(*p != end))
278 goto bad;
279 return 0;
280
281bad:
282 return -EIO;
283}
284
285/*
Herb Shiu25933ab2010-12-01 14:14:38 -0800286 * parse extra results
287 */
288static int parse_reply_info_extra(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800289 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200290 u64 features)
Herb Shiu25933ab2010-12-01 14:14:38 -0800291{
Jeff Layton6df8c9d2017-01-12 14:42:41 -0500292 u32 op = le32_to_cpu(info->head->op);
293
294 if (op == CEPH_MDS_OP_GETFILELOCK)
Sage Weil14303d22010-12-14 17:37:52 -0800295 return parse_reply_info_filelock(p, end, info, features);
Jeff Layton6df8c9d2017-01-12 14:42:41 -0500296 else if (op == CEPH_MDS_OP_READDIR || op == CEPH_MDS_OP_LSSNAP)
Sage Weil14303d22010-12-14 17:37:52 -0800297 return parse_reply_info_dir(p, end, info, features);
Jeff Layton6df8c9d2017-01-12 14:42:41 -0500298 else if (op == CEPH_MDS_OP_CREATE)
Sam Lang6e8575f2012-12-28 09:56:46 -0800299 return parse_reply_info_create(p, end, info, features);
300 else
301 return -EIO;
Herb Shiu25933ab2010-12-01 14:14:38 -0800302}
303
304/*
Sage Weil2f2dc052009-10-06 11:31:09 -0700305 * parse entire mds reply
306 */
307static int parse_reply_info(struct ceph_msg *msg,
Sage Weil14303d22010-12-14 17:37:52 -0800308 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200309 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700310{
311 void *p, *end;
312 u32 len;
313 int err;
314
315 info->head = msg->front.iov_base;
316 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
317 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
318
319 /* trace */
320 ceph_decode_32_safe(&p, end, len, bad);
321 if (len > 0) {
Xi Wang32852a82012-01-14 22:20:59 -0500322 ceph_decode_need(&p, end, len, bad);
Sage Weil14303d22010-12-14 17:37:52 -0800323 err = parse_reply_info_trace(&p, p+len, info, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700324 if (err < 0)
325 goto out_bad;
326 }
327
Herb Shiu25933ab2010-12-01 14:14:38 -0800328 /* extra */
Sage Weil2f2dc052009-10-06 11:31:09 -0700329 ceph_decode_32_safe(&p, end, len, bad);
330 if (len > 0) {
Xi Wang32852a82012-01-14 22:20:59 -0500331 ceph_decode_need(&p, end, len, bad);
Sage Weil14303d22010-12-14 17:37:52 -0800332 err = parse_reply_info_extra(&p, p+len, info, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700333 if (err < 0)
334 goto out_bad;
335 }
336
337 /* snap blob */
338 ceph_decode_32_safe(&p, end, len, bad);
339 info->snapblob_len = len;
340 info->snapblob = p;
341 p += len;
342
343 if (p != end)
344 goto bad;
345 return 0;
346
347bad:
348 err = -EIO;
349out_bad:
350 pr_err("mds parse_reply err %d\n", err);
351 return err;
352}
353
354static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
355{
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800356 if (!info->dir_entries)
Yan, Zheng54008392014-03-29 13:41:15 +0800357 return;
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800358 free_pages((unsigned long)info->dir_entries, get_order(info->dir_buf_size));
Sage Weil2f2dc052009-10-06 11:31:09 -0700359}
360
361
362/*
363 * sessions
364 */
John Spraya687eca2014-09-19 13:51:08 +0100365const char *ceph_session_state_name(int s)
Sage Weil2f2dc052009-10-06 11:31:09 -0700366{
367 switch (s) {
368 case CEPH_MDS_SESSION_NEW: return "new";
369 case CEPH_MDS_SESSION_OPENING: return "opening";
370 case CEPH_MDS_SESSION_OPEN: return "open";
371 case CEPH_MDS_SESSION_HUNG: return "hung";
372 case CEPH_MDS_SESSION_CLOSING: return "closing";
Sage Weil44ca18f2010-02-15 12:08:46 -0800373 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
Sage Weil2f2dc052009-10-06 11:31:09 -0700374 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
Yan, Zhengfcff4152016-09-14 16:39:51 +0800375 case CEPH_MDS_SESSION_REJECTED: return "rejected";
Sage Weil2f2dc052009-10-06 11:31:09 -0700376 default: return "???";
377 }
378}
379
380static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
381{
Elena Reshetova3997c012017-03-03 11:15:06 +0200382 if (refcount_inc_not_zero(&s->s_ref)) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700383 dout("mdsc get_session %p %d -> %d\n", s,
Elena Reshetova3997c012017-03-03 11:15:06 +0200384 refcount_read(&s->s_ref)-1, refcount_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -0700385 return s;
386 } else {
387 dout("mdsc get_session %p 0 -- FAIL", s);
388 return NULL;
389 }
390}
391
392void ceph_put_mds_session(struct ceph_mds_session *s)
393{
394 dout("mdsc put_session %p %d -> %d\n", s,
Elena Reshetova3997c012017-03-03 11:15:06 +0200395 refcount_read(&s->s_ref), refcount_read(&s->s_ref)-1);
396 if (refcount_dec_and_test(&s->s_ref)) {
Alex Elder6c4a1912012-05-16 15:16:38 -0500397 if (s->s_auth.authorizer)
Ilya Dryomov6c1ea262016-04-11 19:34:49 +0200398 ceph_auth_destroy_authorizer(s->s_auth.authorizer);
Sage Weil2f2dc052009-10-06 11:31:09 -0700399 kfree(s);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800400 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700401}
402
403/*
404 * called under mdsc->mutex
405 */
406struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
407 int mds)
408{
409 struct ceph_mds_session *session;
410
Markus Elfringd37b1d92017-08-20 20:22:02 +0200411 if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
Sage Weil2f2dc052009-10-06 11:31:09 -0700412 return NULL;
413 session = mdsc->sessions[mds];
414 dout("lookup_mds_session %p %d\n", session,
Elena Reshetova3997c012017-03-03 11:15:06 +0200415 refcount_read(&session->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -0700416 get_session(session);
417 return session;
418}
419
420static bool __have_session(struct ceph_mds_client *mdsc, int mds)
421{
422 if (mds >= mdsc->max_sessions)
423 return false;
424 return mdsc->sessions[mds];
425}
426
Sage Weil2600d2d2010-02-22 15:12:16 -0800427static int __verify_registered_session(struct ceph_mds_client *mdsc,
428 struct ceph_mds_session *s)
429{
430 if (s->s_mds >= mdsc->max_sessions ||
431 mdsc->sessions[s->s_mds] != s)
432 return -ENOENT;
433 return 0;
434}
435
Sage Weil2f2dc052009-10-06 11:31:09 -0700436/*
437 * create+register a new session for given mds.
438 * called under mdsc->mutex.
439 */
440static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
441 int mds)
442{
443 struct ceph_mds_session *s;
444
Yan, Zheng76201b62017-03-28 17:04:13 +0800445 if (mds >= mdsc->mdsmap->m_num_mds)
Nathaniel Yazdanic338c072013-08-04 21:04:30 -0700446 return ERR_PTR(-EINVAL);
447
Sage Weil2f2dc052009-10-06 11:31:09 -0700448 s = kzalloc(sizeof(*s), GFP_NOFS);
Dan Carpenter4736b002010-03-20 15:30:16 +0300449 if (!s)
450 return ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -0700451 s->s_mdsc = mdsc;
452 s->s_mds = mds;
453 s->s_state = CEPH_MDS_SESSION_NEW;
454 s->s_ttl = 0;
455 s->s_seq = 0;
456 mutex_init(&s->s_mutex);
457
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700458 ceph_con_init(&s->s_con, s, &mds_con_ops, &mdsc->fsc->client->msgr);
Sage Weil2f2dc052009-10-06 11:31:09 -0700459
Alex Elderd8fb02a2012-01-12 17:48:10 -0800460 spin_lock_init(&s->s_gen_ttl_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700461 s->s_cap_gen = 0;
Alex Elder1ce208a2012-01-12 17:48:11 -0800462 s->s_cap_ttl = jiffies - 1;
Alex Elderd8fb02a2012-01-12 17:48:10 -0800463
464 spin_lock_init(&s->s_cap_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700465 s->s_renew_requested = 0;
466 s->s_renew_seq = 0;
467 INIT_LIST_HEAD(&s->s_caps);
468 s->s_nr_caps = 0;
Sage Weil5dacf092009-12-21 20:40:34 -0800469 s->s_trim_caps = 0;
Elena Reshetova3997c012017-03-03 11:15:06 +0200470 refcount_set(&s->s_ref, 1);
Sage Weil2f2dc052009-10-06 11:31:09 -0700471 INIT_LIST_HEAD(&s->s_waiting);
472 INIT_LIST_HEAD(&s->s_unsafe);
473 s->s_num_cap_releases = 0;
Yan, Zheng99a9c272013-09-22 11:08:14 +0800474 s->s_cap_reconnect = 0;
Sage Weil7c1332b2010-02-16 11:39:45 -0800475 s->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700476 INIT_LIST_HEAD(&s->s_cap_releases);
Sage Weil2f2dc052009-10-06 11:31:09 -0700477 INIT_LIST_HEAD(&s->s_cap_flushing);
Sage Weil2f2dc052009-10-06 11:31:09 -0700478
479 dout("register_session mds%d\n", mds);
480 if (mds >= mdsc->max_sessions) {
481 int newmax = 1 << get_count_order(mds+1);
482 struct ceph_mds_session **sa;
483
484 dout("register_session realloc to %d\n", newmax);
485 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
Markus Elfringd37b1d92017-08-20 20:22:02 +0200486 if (!sa)
Sage Weil42ce56e2009-11-18 11:22:36 -0800487 goto fail_realloc;
Sage Weil2f2dc052009-10-06 11:31:09 -0700488 if (mdsc->sessions) {
489 memcpy(sa, mdsc->sessions,
490 mdsc->max_sessions * sizeof(void *));
491 kfree(mdsc->sessions);
492 }
493 mdsc->sessions = sa;
494 mdsc->max_sessions = newmax;
495 }
496 mdsc->sessions[mds] = s;
Yan, Zheng86d8f672015-01-09 17:00:42 +0800497 atomic_inc(&mdsc->num_sessions);
Elena Reshetova3997c012017-03-03 11:15:06 +0200498 refcount_inc(&s->s_ref); /* one ref to sessions[], one to caller */
Sage Weil42ce56e2009-11-18 11:22:36 -0800499
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700500 ceph_con_open(&s->s_con, CEPH_ENTITY_TYPE_MDS, mds,
501 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
Sage Weil42ce56e2009-11-18 11:22:36 -0800502
Sage Weil2f2dc052009-10-06 11:31:09 -0700503 return s;
Sage Weil42ce56e2009-11-18 11:22:36 -0800504
505fail_realloc:
506 kfree(s);
507 return ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -0700508}
509
510/*
511 * called under mdsc->mutex
512 */
Sage Weil2600d2d2010-02-22 15:12:16 -0800513static void __unregister_session(struct ceph_mds_client *mdsc,
Sage Weil42ce56e2009-11-18 11:22:36 -0800514 struct ceph_mds_session *s)
Sage Weil2f2dc052009-10-06 11:31:09 -0700515{
Sage Weil2600d2d2010-02-22 15:12:16 -0800516 dout("__unregister_session mds%d %p\n", s->s_mds, s);
517 BUG_ON(mdsc->sessions[s->s_mds] != s);
Sage Weil42ce56e2009-11-18 11:22:36 -0800518 mdsc->sessions[s->s_mds] = NULL;
519 ceph_con_close(&s->s_con);
520 ceph_put_mds_session(s);
Yan, Zheng86d8f672015-01-09 17:00:42 +0800521 atomic_dec(&mdsc->num_sessions);
Sage Weil2f2dc052009-10-06 11:31:09 -0700522}
523
524/*
525 * drop session refs in request.
526 *
527 * should be last request ref, or hold mdsc->mutex
528 */
529static void put_request_session(struct ceph_mds_request *req)
530{
531 if (req->r_session) {
532 ceph_put_mds_session(req->r_session);
533 req->r_session = NULL;
534 }
535}
536
Sage Weil153c8e62009-12-07 12:31:09 -0800537void ceph_mdsc_release_request(struct kref *kref)
Sage Weil2f2dc052009-10-06 11:31:09 -0700538{
Sage Weil153c8e62009-12-07 12:31:09 -0800539 struct ceph_mds_request *req = container_of(kref,
540 struct ceph_mds_request,
541 r_kref);
Yan, Zheng54008392014-03-29 13:41:15 +0800542 destroy_reply_info(&req->r_reply_info);
Sage Weil153c8e62009-12-07 12:31:09 -0800543 if (req->r_request)
544 ceph_msg_put(req->r_request);
Yan, Zheng54008392014-03-29 13:41:15 +0800545 if (req->r_reply)
Sage Weil153c8e62009-12-07 12:31:09 -0800546 ceph_msg_put(req->r_reply);
Sage Weil153c8e62009-12-07 12:31:09 -0800547 if (req->r_inode) {
Sage Weil41b02e12011-07-26 11:31:14 -0700548 ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
Sage Weil153c8e62009-12-07 12:31:09 -0800549 iput(req->r_inode);
550 }
Jeff Layton3dd69aa2017-01-31 10:28:26 -0500551 if (req->r_parent)
552 ceph_put_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
SF Markus Elfringe96a6502014-11-02 15:20:59 +0100553 iput(req->r_target_inode);
Sage Weil153c8e62009-12-07 12:31:09 -0800554 if (req->r_dentry)
555 dput(req->r_dentry);
Sage Weil844d87c2013-02-05 13:40:09 -0800556 if (req->r_old_dentry)
557 dput(req->r_old_dentry);
558 if (req->r_old_dentry_dir) {
Sage Weil41b02e12011-07-26 11:31:14 -0700559 /*
560 * track (and drop pins for) r_old_dentry_dir
561 * separately, since r_old_dentry's d_parent may have
562 * changed between the dir mutex being dropped and
563 * this request being freed.
564 */
565 ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir),
566 CEPH_CAP_PIN);
Sage Weil41b02e12011-07-26 11:31:14 -0700567 iput(req->r_old_dentry_dir);
Sage Weil153c8e62009-12-07 12:31:09 -0800568 }
569 kfree(req->r_path1);
570 kfree(req->r_path2);
Yan, Zheng25e6bae2014-09-16 19:15:28 +0800571 if (req->r_pagelist)
572 ceph_pagelist_release(req->r_pagelist);
Sage Weil153c8e62009-12-07 12:31:09 -0800573 put_request_session(req);
Yehuda Sadeh37151662010-06-17 16:16:12 -0700574 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
Sage Weil153c8e62009-12-07 12:31:09 -0800575 kfree(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700576}
577
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200578DEFINE_RB_FUNCS(request, struct ceph_mds_request, r_tid, r_node)
579
Sage Weil2f2dc052009-10-06 11:31:09 -0700580/*
581 * lookup session, bump ref if found.
582 *
583 * called under mdsc->mutex.
584 */
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200585static struct ceph_mds_request *
586lookup_get_request(struct ceph_mds_client *mdsc, u64 tid)
Sage Weil2f2dc052009-10-06 11:31:09 -0700587{
588 struct ceph_mds_request *req;
Sage Weil44ca18f2010-02-15 12:08:46 -0800589
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200590 req = lookup_request(&mdsc->request_tree, tid);
591 if (req)
592 ceph_mdsc_get_request(req);
Sage Weil44ca18f2010-02-15 12:08:46 -0800593
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200594 return req;
Sage Weil2f2dc052009-10-06 11:31:09 -0700595}
596
597/*
598 * Register an in-flight request, and assign a tid. Link to directory
599 * are modifying (if any).
600 *
601 * Called under mdsc->mutex.
602 */
603static void __register_request(struct ceph_mds_client *mdsc,
604 struct ceph_mds_request *req,
605 struct inode *dir)
606{
607 req->r_tid = ++mdsc->last_tid;
608 if (req->r_num_caps)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700609 ceph_reserve_caps(mdsc, &req->r_caps_reservation,
610 req->r_num_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -0700611 dout("__register_request %p tid %lld\n", req, req->r_tid);
612 ceph_mdsc_get_request(req);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200613 insert_request(&mdsc->request_tree, req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700614
Sage Weilcb4276c2010-11-08 07:28:52 -0800615 req->r_uid = current_fsuid();
616 req->r_gid = current_fsgid();
617
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +0800618 if (mdsc->oldest_tid == 0 && req->r_op != CEPH_MDS_OP_SETFILELOCK)
619 mdsc->oldest_tid = req->r_tid;
620
Sage Weil2f2dc052009-10-06 11:31:09 -0700621 if (dir) {
Sage Weil3b663782011-05-18 16:12:12 -0700622 ihold(dir);
Sage Weil2f2dc052009-10-06 11:31:09 -0700623 req->r_unsafe_dir = dir;
Sage Weil2f2dc052009-10-06 11:31:09 -0700624 }
625}
626
627static void __unregister_request(struct ceph_mds_client *mdsc,
628 struct ceph_mds_request *req)
629{
630 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +0800631
Jeff Laytondf963ea2017-02-14 10:09:40 -0500632 /* Never leave an unregistered request on an unsafe list! */
633 list_del_init(&req->r_unsafe_item);
634
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +0800635 if (req->r_tid == mdsc->oldest_tid) {
636 struct rb_node *p = rb_next(&req->r_node);
637 mdsc->oldest_tid = 0;
638 while (p) {
639 struct ceph_mds_request *next_req =
640 rb_entry(p, struct ceph_mds_request, r_node);
641 if (next_req->r_op != CEPH_MDS_OP_SETFILELOCK) {
642 mdsc->oldest_tid = next_req->r_tid;
643 break;
644 }
645 p = rb_next(p);
646 }
647 }
648
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200649 erase_request(&mdsc->request_tree, req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700650
Jeff Laytonbc2de10d2017-02-01 13:49:09 -0500651 if (req->r_unsafe_dir &&
652 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700653 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
Sage Weil2f2dc052009-10-06 11:31:09 -0700654 spin_lock(&ci->i_unsafe_lock);
655 list_del_init(&req->r_unsafe_dir_item);
656 spin_unlock(&ci->i_unsafe_lock);
Yan, Zheng4c06ace2015-10-27 17:18:00 +0800657 }
Jeff Laytonbc2de10d2017-02-01 13:49:09 -0500658 if (req->r_target_inode &&
659 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Yan, Zheng68cd5b42015-10-27 18:36:06 +0800660 struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
661 spin_lock(&ci->i_unsafe_lock);
662 list_del_init(&req->r_unsafe_target_item);
663 spin_unlock(&ci->i_unsafe_lock);
664 }
Sage Weil3b663782011-05-18 16:12:12 -0700665
Yan, Zheng4c06ace2015-10-27 17:18:00 +0800666 if (req->r_unsafe_dir) {
Sage Weil3b663782011-05-18 16:12:12 -0700667 iput(req->r_unsafe_dir);
668 req->r_unsafe_dir = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700669 }
Sage Weil94aa8ae2010-03-28 21:22:50 -0700670
Yan, Zhengfc55d2c2013-10-31 09:10:47 +0800671 complete_all(&req->r_safe_completion);
672
Sage Weil94aa8ae2010-03-28 21:22:50 -0700673 ceph_mdsc_put_request(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700674}
675
676/*
Jeff Layton30c71232016-12-15 08:37:56 -0500677 * Walk back up the dentry tree until we hit a dentry representing a
678 * non-snapshot inode. We do this using the rcu_read_lock (which must be held
679 * when calling this) to ensure that the objects won't disappear while we're
680 * working with them. Once we hit a candidate dentry, we attempt to take a
681 * reference to it, and return that as the result.
682 */
Dan Carpenterf1075482017-02-23 13:39:59 +0300683static struct inode *get_nonsnap_parent(struct dentry *dentry)
684{
685 struct inode *inode = NULL;
Jeff Layton30c71232016-12-15 08:37:56 -0500686
687 while (dentry && !IS_ROOT(dentry)) {
688 inode = d_inode_rcu(dentry);
689 if (!inode || ceph_snap(inode) == CEPH_NOSNAP)
690 break;
691 dentry = dentry->d_parent;
692 }
693 if (inode)
694 inode = igrab(inode);
695 return inode;
696}
697
698/*
Sage Weil2f2dc052009-10-06 11:31:09 -0700699 * Choose mds to send request to next. If there is a hint set in the
700 * request (e.g., due to a prior forward hint from the mds), use that.
701 * Otherwise, consult frag tree and/or caps to identify the
702 * appropriate mds. If all else fails, choose randomly.
703 *
704 * Called under mdsc->mutex.
705 */
706static int __choose_mds(struct ceph_mds_client *mdsc,
707 struct ceph_mds_request *req)
708{
709 struct inode *inode;
710 struct ceph_inode_info *ci;
711 struct ceph_cap *cap;
712 int mode = req->r_direct_mode;
713 int mds = -1;
714 u32 hash = req->r_direct_hash;
Jeff Laytonbc2de10d2017-02-01 13:49:09 -0500715 bool is_hash = test_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags);
Sage Weil2f2dc052009-10-06 11:31:09 -0700716
717 /*
718 * is there a specific mds we should try? ignore hint if we have
719 * no session and the mds is not up (active or recovering).
720 */
721 if (req->r_resend_mds >= 0 &&
722 (__have_session(mdsc, req->r_resend_mds) ||
723 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
724 dout("choose_mds using resend_mds mds%d\n",
725 req->r_resend_mds);
726 return req->r_resend_mds;
727 }
728
729 if (mode == USE_RANDOM_MDS)
730 goto random;
731
732 inode = NULL;
733 if (req->r_inode) {
Yan, Zheng5d37ca12017-07-26 12:48:08 +0800734 if (ceph_snap(req->r_inode) != CEPH_SNAPDIR) {
735 inode = req->r_inode;
736 ihold(inode);
737 } else {
Yan, Zheng38f340c2017-09-22 11:41:06 +0800738 /* req->r_dentry is non-null for LSSNAP request */
739 rcu_read_lock();
740 inode = get_nonsnap_parent(req->r_dentry);
741 rcu_read_unlock();
742 dout("__choose_mds using snapdir's parent %p\n", inode);
Yan, Zheng5d37ca12017-07-26 12:48:08 +0800743 }
Yan, Zheng38f340c2017-09-22 11:41:06 +0800744 } else if (req->r_dentry) {
Sage Weild79698d2011-07-26 11:31:26 -0700745 /* ignore race with rename; old or new d_parent is okay */
Jeff Layton30c71232016-12-15 08:37:56 -0500746 struct dentry *parent;
747 struct inode *dir;
Sage Weileb6bb1c2010-08-16 09:21:27 -0700748
Jeff Layton30c71232016-12-15 08:37:56 -0500749 rcu_read_lock();
750 parent = req->r_dentry->d_parent;
Jeff Layton3dd69aa2017-01-31 10:28:26 -0500751 dir = req->r_parent ? : d_inode_rcu(parent);
Jeff Layton30c71232016-12-15 08:37:56 -0500752
753 if (!dir || dir->i_sb != mdsc->fsc->sb) {
754 /* not this fs or parent went negative */
David Howells2b0143b2015-03-17 22:25:59 +0000755 inode = d_inode(req->r_dentry);
Jeff Layton30c71232016-12-15 08:37:56 -0500756 if (inode)
757 ihold(inode);
Sage Weileb6bb1c2010-08-16 09:21:27 -0700758 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
759 /* direct snapped/virtual snapdir requests
760 * based on parent dir inode */
Jeff Layton30c71232016-12-15 08:37:56 -0500761 inode = get_nonsnap_parent(parent);
Sage Weileb6bb1c2010-08-16 09:21:27 -0700762 dout("__choose_mds using nonsnap parent %p\n", inode);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800763 } else {
Sage Weileb6bb1c2010-08-16 09:21:27 -0700764 /* dentry target */
David Howells2b0143b2015-03-17 22:25:59 +0000765 inode = d_inode(req->r_dentry);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800766 if (!inode || mode == USE_AUTH_MDS) {
767 /* dir + name */
Jeff Layton30c71232016-12-15 08:37:56 -0500768 inode = igrab(dir);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800769 hash = ceph_dentry_hash(dir, req->r_dentry);
770 is_hash = true;
Jeff Layton30c71232016-12-15 08:37:56 -0500771 } else {
772 ihold(inode);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800773 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700774 }
Jeff Layton30c71232016-12-15 08:37:56 -0500775 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -0700776 }
Sage Weileb6bb1c2010-08-16 09:21:27 -0700777
Sage Weil2f2dc052009-10-06 11:31:09 -0700778 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
779 (int)hash, mode);
780 if (!inode)
781 goto random;
782 ci = ceph_inode(inode);
783
784 if (is_hash && S_ISDIR(inode->i_mode)) {
785 struct ceph_inode_frag frag;
786 int found;
787
788 ceph_choose_frag(ci, hash, &frag, &found);
789 if (found) {
790 if (mode == USE_ANY_MDS && frag.ndist > 0) {
791 u8 r;
792
793 /* choose a random replica */
794 get_random_bytes(&r, 1);
795 r %= frag.ndist;
796 mds = frag.dist[r];
797 dout("choose_mds %p %llx.%llx "
798 "frag %u mds%d (%d/%d)\n",
799 inode, ceph_vinop(inode),
Sage Weild66bbd42011-01-21 21:16:46 -0800800 frag.frag, mds,
Sage Weil2f2dc052009-10-06 11:31:09 -0700801 (int)r, frag.ndist);
Sage Weild66bbd42011-01-21 21:16:46 -0800802 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
803 CEPH_MDS_STATE_ACTIVE)
Jeff Layton30c71232016-12-15 08:37:56 -0500804 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -0700805 }
806
807 /* since this file/dir wasn't known to be
808 * replicated, then we want to look for the
809 * authoritative mds. */
810 mode = USE_AUTH_MDS;
811 if (frag.mds >= 0) {
812 /* choose auth mds */
813 mds = frag.mds;
814 dout("choose_mds %p %llx.%llx "
815 "frag %u mds%d (auth)\n",
816 inode, ceph_vinop(inode), frag.frag, mds);
Sage Weild66bbd42011-01-21 21:16:46 -0800817 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
818 CEPH_MDS_STATE_ACTIVE)
Jeff Layton30c71232016-12-15 08:37:56 -0500819 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -0700820 }
821 }
822 }
823
Sage Weilbe655592011-11-30 09:47:09 -0800824 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700825 cap = NULL;
826 if (mode == USE_AUTH_MDS)
827 cap = ci->i_auth_cap;
828 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
829 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
830 if (!cap) {
Sage Weilbe655592011-11-30 09:47:09 -0800831 spin_unlock(&ci->i_ceph_lock);
Jeff Layton30c71232016-12-15 08:37:56 -0500832 iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -0700833 goto random;
834 }
835 mds = cap->session->s_mds;
836 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
837 inode, ceph_vinop(inode), mds,
838 cap == ci->i_auth_cap ? "auth " : "", cap);
Sage Weilbe655592011-11-30 09:47:09 -0800839 spin_unlock(&ci->i_ceph_lock);
Jeff Layton30c71232016-12-15 08:37:56 -0500840out:
841 iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -0700842 return mds;
843
844random:
845 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
846 dout("choose_mds chose random mds%d\n", mds);
847 return mds;
848}
849
850
851/*
852 * session messages
853 */
854static struct ceph_msg *create_session_msg(u32 op, u64 seq)
855{
856 struct ceph_msg *msg;
857 struct ceph_mds_session_head *h;
858
Sage Weilb61c2762011-08-09 15:03:46 -0700859 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
860 false);
Sage Weila79832f2010-04-01 16:06:19 -0700861 if (!msg) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700862 pr_err("create_session_msg ENOMEM creating msg\n");
Sage Weila79832f2010-04-01 16:06:19 -0700863 return NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700864 }
865 h = msg->front.iov_base;
866 h->op = cpu_to_le32(op);
867 h->seq = cpu_to_le64(seq);
John Spraydbd0c8b2014-09-09 19:26:01 +0100868
869 return msg;
870}
871
872/*
873 * session message, specialization for CEPH_SESSION_REQUEST_OPEN
874 * to include additional client metadata fields.
875 */
876static struct ceph_msg *create_session_open_msg(struct ceph_mds_client *mdsc, u64 seq)
877{
878 struct ceph_msg *msg;
879 struct ceph_mds_session_head *h;
880 int i = -1;
881 int metadata_bytes = 0;
882 int metadata_key_count = 0;
883 struct ceph_options *opt = mdsc->fsc->client->options;
Yan, Zheng3f384952016-04-21 11:09:55 +0800884 struct ceph_mount_options *fsopt = mdsc->fsc->mount_options;
John Spraydbd0c8b2014-09-09 19:26:01 +0100885 void *p;
886
Yan, Zhenga6a5ce42015-01-16 10:54:43 +0800887 const char* metadata[][2] = {
Yan, Zheng717e6f22017-09-11 12:10:08 +0800888 {"hostname", mdsc->nodename},
889 {"kernel_version", init_utsname()->release},
Yan, Zheng3f384952016-04-21 11:09:55 +0800890 {"entity_id", opt->name ? : ""},
891 {"root", fsopt->server_path ? : "/"},
John Spraydbd0c8b2014-09-09 19:26:01 +0100892 {NULL, NULL}
893 };
894
895 /* Calculate serialized length of metadata */
896 metadata_bytes = 4; /* map length */
Markus Elfringd37b1d92017-08-20 20:22:02 +0200897 for (i = 0; metadata[i][0]; ++i) {
John Spraydbd0c8b2014-09-09 19:26:01 +0100898 metadata_bytes += 8 + strlen(metadata[i][0]) +
899 strlen(metadata[i][1]);
900 metadata_key_count++;
901 }
902
903 /* Allocate the message */
904 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + metadata_bytes,
905 GFP_NOFS, false);
906 if (!msg) {
907 pr_err("create_session_msg ENOMEM creating msg\n");
908 return NULL;
909 }
910 h = msg->front.iov_base;
911 h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN);
912 h->seq = cpu_to_le64(seq);
913
914 /*
915 * Serialize client metadata into waiting buffer space, using
916 * the format that userspace expects for map<string, string>
John Spray7cfa0312014-10-30 17:15:26 +0000917 *
918 * ClientSession messages with metadata are v2
John Spraydbd0c8b2014-09-09 19:26:01 +0100919 */
John Spray7cfa0312014-10-30 17:15:26 +0000920 msg->hdr.version = cpu_to_le16(2);
921 msg->hdr.compat_version = cpu_to_le16(1);
John Spraydbd0c8b2014-09-09 19:26:01 +0100922
923 /* The write pointer, following the session_head structure */
924 p = msg->front.iov_base + sizeof(*h);
925
926 /* Number of entries in the map */
927 ceph_encode_32(&p, metadata_key_count);
928
929 /* Two length-prefixed strings for each entry in the map */
Markus Elfringd37b1d92017-08-20 20:22:02 +0200930 for (i = 0; metadata[i][0]; ++i) {
John Spraydbd0c8b2014-09-09 19:26:01 +0100931 size_t const key_len = strlen(metadata[i][0]);
932 size_t const val_len = strlen(metadata[i][1]);
933
934 ceph_encode_32(&p, key_len);
935 memcpy(p, metadata[i][0], key_len);
936 p += key_len;
937 ceph_encode_32(&p, val_len);
938 memcpy(p, metadata[i][1], val_len);
939 p += val_len;
940 }
941
Sage Weil2f2dc052009-10-06 11:31:09 -0700942 return msg;
943}
944
945/*
946 * send session open request.
947 *
948 * called under mdsc->mutex
949 */
950static int __open_session(struct ceph_mds_client *mdsc,
951 struct ceph_mds_session *session)
952{
953 struct ceph_msg *msg;
954 int mstate;
955 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -0700956
957 /* wait for mds to go active? */
958 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
959 dout("open_session to mds%d (%s)\n", mds,
960 ceph_mds_state_name(mstate));
961 session->s_state = CEPH_MDS_SESSION_OPENING;
962 session->s_renew_requested = jiffies;
963
964 /* send connect message */
John Spraydbd0c8b2014-09-09 19:26:01 +0100965 msg = create_session_open_msg(mdsc, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -0700966 if (!msg)
967 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -0700968 ceph_con_send(&session->s_con, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -0700969 return 0;
970}
971
972/*
Sage Weiled0552a2010-06-21 13:38:25 -0700973 * open sessions for any export targets for the given mds
974 *
975 * called under mdsc->mutex
976 */
Yan, Zheng5d72d132013-11-24 14:33:01 +0800977static struct ceph_mds_session *
978__open_export_target_session(struct ceph_mds_client *mdsc, int target)
979{
980 struct ceph_mds_session *session;
981
982 session = __ceph_lookup_mds_session(mdsc, target);
983 if (!session) {
984 session = register_session(mdsc, target);
985 if (IS_ERR(session))
986 return session;
987 }
988 if (session->s_state == CEPH_MDS_SESSION_NEW ||
989 session->s_state == CEPH_MDS_SESSION_CLOSING)
990 __open_session(mdsc, session);
991
992 return session;
993}
994
995struct ceph_mds_session *
996ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target)
997{
998 struct ceph_mds_session *session;
999
1000 dout("open_export_target_session to mds%d\n", target);
1001
1002 mutex_lock(&mdsc->mutex);
1003 session = __open_export_target_session(mdsc, target);
1004 mutex_unlock(&mdsc->mutex);
1005
1006 return session;
1007}
1008
Sage Weiled0552a2010-06-21 13:38:25 -07001009static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
1010 struct ceph_mds_session *session)
1011{
1012 struct ceph_mds_info *mi;
1013 struct ceph_mds_session *ts;
1014 int i, mds = session->s_mds;
Sage Weiled0552a2010-06-21 13:38:25 -07001015
Yan, Zheng76201b62017-03-28 17:04:13 +08001016 if (mds >= mdsc->mdsmap->m_num_mds)
Sage Weiled0552a2010-06-21 13:38:25 -07001017 return;
Yan, Zheng5d72d132013-11-24 14:33:01 +08001018
Sage Weiled0552a2010-06-21 13:38:25 -07001019 mi = &mdsc->mdsmap->m_info[mds];
1020 dout("open_export_target_sessions for mds%d (%d targets)\n",
1021 session->s_mds, mi->num_export_targets);
1022
1023 for (i = 0; i < mi->num_export_targets; i++) {
Yan, Zheng5d72d132013-11-24 14:33:01 +08001024 ts = __open_export_target_session(mdsc, mi->export_targets[i]);
1025 if (!IS_ERR(ts))
1026 ceph_put_mds_session(ts);
Sage Weiled0552a2010-06-21 13:38:25 -07001027 }
1028}
1029
Sage Weil154f42c2010-06-21 13:45:04 -07001030void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
1031 struct ceph_mds_session *session)
1032{
1033 mutex_lock(&mdsc->mutex);
1034 __open_export_target_sessions(mdsc, session);
1035 mutex_unlock(&mdsc->mutex);
1036}
1037
Sage Weiled0552a2010-06-21 13:38:25 -07001038/*
Sage Weil2f2dc052009-10-06 11:31:09 -07001039 * session caps
1040 */
1041
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001042static void detach_cap_releases(struct ceph_mds_session *session,
1043 struct list_head *target)
Sage Weil2f2dc052009-10-06 11:31:09 -07001044{
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001045 lockdep_assert_held(&session->s_cap_lock);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001046
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001047 list_splice_init(&session->s_cap_releases, target);
1048 session->s_num_cap_releases = 0;
1049 dout("dispose_cap_releases mds%d\n", session->s_mds);
1050}
1051
1052static void dispose_cap_releases(struct ceph_mds_client *mdsc,
1053 struct list_head *dispose)
1054{
1055 while (!list_empty(dispose)) {
Yan, Zheng745a8e32015-05-14 17:22:42 +08001056 struct ceph_cap *cap;
1057 /* zero out the in-progress message */
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001058 cap = list_first_entry(dispose, struct ceph_cap, session_caps);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001059 list_del(&cap->session_caps);
1060 ceph_put_cap(mdsc, cap);
1061 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001062}
1063
Yan, Zheng1c841a92015-03-24 20:15:36 +08001064static void cleanup_session_requests(struct ceph_mds_client *mdsc,
1065 struct ceph_mds_session *session)
1066{
1067 struct ceph_mds_request *req;
1068 struct rb_node *p;
1069
1070 dout("cleanup_session_requests mds%d\n", session->s_mds);
1071 mutex_lock(&mdsc->mutex);
1072 while (!list_empty(&session->s_unsafe)) {
1073 req = list_first_entry(&session->s_unsafe,
1074 struct ceph_mds_request, r_unsafe_item);
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001075 pr_warn_ratelimited(" dropping unsafe request %llu\n",
1076 req->r_tid);
Yan, Zheng1c841a92015-03-24 20:15:36 +08001077 __unregister_request(mdsc, req);
1078 }
1079 /* zero r_attempts, so kick_requests() will re-send requests */
1080 p = rb_first(&mdsc->request_tree);
1081 while (p) {
1082 req = rb_entry(p, struct ceph_mds_request, r_node);
1083 p = rb_next(p);
1084 if (req->r_session &&
1085 req->r_session->s_mds == session->s_mds)
1086 req->r_attempts = 0;
1087 }
1088 mutex_unlock(&mdsc->mutex);
1089}
1090
Sage Weil2f2dc052009-10-06 11:31:09 -07001091/*
Sage Weilf818a732010-05-11 20:56:31 -07001092 * Helper to safely iterate over all caps associated with a session, with
1093 * special care taken to handle a racing __ceph_remove_cap().
Sage Weil2f2dc052009-10-06 11:31:09 -07001094 *
Sage Weilf818a732010-05-11 20:56:31 -07001095 * Caller must hold session s_mutex.
Sage Weil2f2dc052009-10-06 11:31:09 -07001096 */
1097static int iterate_session_caps(struct ceph_mds_session *session,
1098 int (*cb)(struct inode *, struct ceph_cap *,
1099 void *), void *arg)
1100{
Sage Weil7c1332b2010-02-16 11:39:45 -08001101 struct list_head *p;
1102 struct ceph_cap *cap;
1103 struct inode *inode, *last_inode = NULL;
1104 struct ceph_cap *old_cap = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001105 int ret;
1106
1107 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
1108 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001109 p = session->s_caps.next;
1110 while (p != &session->s_caps) {
1111 cap = list_entry(p, struct ceph_cap, session_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -07001112 inode = igrab(&cap->ci->vfs_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001113 if (!inode) {
1114 p = p->next;
Sage Weil2f2dc052009-10-06 11:31:09 -07001115 continue;
Sage Weil7c1332b2010-02-16 11:39:45 -08001116 }
1117 session->s_cap_iterator = cap;
Sage Weil2f2dc052009-10-06 11:31:09 -07001118 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001119
1120 if (last_inode) {
1121 iput(last_inode);
1122 last_inode = NULL;
1123 }
1124 if (old_cap) {
Yehuda Sadeh37151662010-06-17 16:16:12 -07001125 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -08001126 old_cap = NULL;
1127 }
1128
Sage Weil2f2dc052009-10-06 11:31:09 -07001129 ret = cb(inode, cap, arg);
Sage Weil7c1332b2010-02-16 11:39:45 -08001130 last_inode = inode;
1131
Sage Weil2f2dc052009-10-06 11:31:09 -07001132 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001133 p = p->next;
Markus Elfringd37b1d92017-08-20 20:22:02 +02001134 if (!cap->ci) {
Sage Weil7c1332b2010-02-16 11:39:45 -08001135 dout("iterate_session_caps finishing cap %p removal\n",
1136 cap);
1137 BUG_ON(cap->session != session);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001138 cap->session = NULL;
Sage Weil7c1332b2010-02-16 11:39:45 -08001139 list_del_init(&cap->session_caps);
1140 session->s_nr_caps--;
Yan, Zheng745a8e32015-05-14 17:22:42 +08001141 if (cap->queue_release) {
1142 list_add_tail(&cap->session_caps,
1143 &session->s_cap_releases);
1144 session->s_num_cap_releases++;
1145 } else {
1146 old_cap = cap; /* put_cap it w/o locks held */
1147 }
Sage Weil7c1332b2010-02-16 11:39:45 -08001148 }
Sage Weil5dacf092009-12-21 20:40:34 -08001149 if (ret < 0)
1150 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07001151 }
Sage Weil5dacf092009-12-21 20:40:34 -08001152 ret = 0;
1153out:
Sage Weil7c1332b2010-02-16 11:39:45 -08001154 session->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001155 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001156
SF Markus Elfringe96a6502014-11-02 15:20:59 +01001157 iput(last_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001158 if (old_cap)
Yehuda Sadeh37151662010-06-17 16:16:12 -07001159 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -08001160
Sage Weil5dacf092009-12-21 20:40:34 -08001161 return ret;
Sage Weil2f2dc052009-10-06 11:31:09 -07001162}
1163
1164static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
Sage Weil6c99f252010-05-10 16:12:25 -07001165 void *arg)
Sage Weil2f2dc052009-10-06 11:31:09 -07001166{
Yan, Zheng6c93df52016-04-15 13:56:12 +08001167 struct ceph_fs_client *fsc = (struct ceph_fs_client *)arg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001168 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001169 LIST_HEAD(to_remove);
Yan, Zheng6c93df52016-04-15 13:56:12 +08001170 bool drop = false;
1171 bool invalidate = false;
Sage Weil6c99f252010-05-10 16:12:25 -07001172
Sage Weil2f2dc052009-10-06 11:31:09 -07001173 dout("removing cap %p, ci is %p, inode is %p\n",
1174 cap, ci, &ci->vfs_inode);
Sage Weilbe655592011-11-30 09:47:09 -08001175 spin_lock(&ci->i_ceph_lock);
Yan, Zhenga096b092013-09-22 10:15:58 +08001176 __ceph_remove_cap(cap, false);
Yan, Zheng571ade32015-03-24 11:36:08 +08001177 if (!ci->i_auth_cap) {
Yan, Zheng553adfd2015-06-09 15:48:57 +08001178 struct ceph_cap_flush *cf;
Yan, Zheng6c93df52016-04-15 13:56:12 +08001179 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil6c99f252010-05-10 16:12:25 -07001180
Yan, Zheng77310322016-04-08 15:27:16 +08001181 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
1182
Yan, Zheng6c93df52016-04-15 13:56:12 +08001183 if (ci->i_wrbuffer_ref > 0 &&
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01001184 READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
Yan, Zheng6c93df52016-04-15 13:56:12 +08001185 invalidate = true;
1186
Yan, Zhenge4500b52016-07-06 11:12:56 +08001187 while (!list_empty(&ci->i_cap_flush_list)) {
1188 cf = list_first_entry(&ci->i_cap_flush_list,
1189 struct ceph_cap_flush, i_list);
Wei Yongjun8cdcc072016-08-13 09:10:28 +00001190 list_move(&cf->i_list, &to_remove);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001191 }
1192
Sage Weil6c99f252010-05-10 16:12:25 -07001193 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zheng8310b082015-06-09 17:20:12 +08001194
Yan, Zhenge4500b52016-07-06 11:12:56 +08001195 list_for_each_entry(cf, &to_remove, i_list)
1196 list_del(&cf->g_list);
Yan, Zheng8310b082015-06-09 17:20:12 +08001197
Sage Weil6c99f252010-05-10 16:12:25 -07001198 if (!list_empty(&ci->i_dirty_item)) {
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001199 pr_warn_ratelimited(
1200 " dropping dirty %s state for %p %lld\n",
Sage Weil6c99f252010-05-10 16:12:25 -07001201 ceph_cap_string(ci->i_dirty_caps),
1202 inode, ceph_ino(inode));
1203 ci->i_dirty_caps = 0;
1204 list_del_init(&ci->i_dirty_item);
Yan, Zheng6c93df52016-04-15 13:56:12 +08001205 drop = true;
Sage Weil6c99f252010-05-10 16:12:25 -07001206 }
1207 if (!list_empty(&ci->i_flushing_item)) {
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001208 pr_warn_ratelimited(
1209 " dropping dirty+flushing %s state for %p %lld\n",
Sage Weil6c99f252010-05-10 16:12:25 -07001210 ceph_cap_string(ci->i_flushing_caps),
1211 inode, ceph_ino(inode));
1212 ci->i_flushing_caps = 0;
1213 list_del_init(&ci->i_flushing_item);
1214 mdsc->num_cap_flushing--;
Yan, Zheng6c93df52016-04-15 13:56:12 +08001215 drop = true;
Sage Weil6c99f252010-05-10 16:12:25 -07001216 }
Sage Weil6c99f252010-05-10 16:12:25 -07001217 spin_unlock(&mdsc->cap_dirty_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001218
Yan, Zhengb3f8d682017-09-11 10:58:55 +08001219 if (atomic_read(&ci->i_filelock_ref) > 0) {
1220 /* make further file lock syscall return -EIO */
1221 ci->i_ceph_flags |= CEPH_I_ERROR_FILELOCK;
1222 pr_warn_ratelimited(" dropping file locks for %p %lld\n",
1223 inode, ceph_ino(inode));
1224 }
1225
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001226 if (!ci->i_dirty_caps && ci->i_prealloc_cap_flush) {
Yan, Zhenge4500b52016-07-06 11:12:56 +08001227 list_add(&ci->i_prealloc_cap_flush->i_list, &to_remove);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001228 ci->i_prealloc_cap_flush = NULL;
1229 }
Sage Weil6c99f252010-05-10 16:12:25 -07001230 }
Sage Weilbe655592011-11-30 09:47:09 -08001231 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001232 while (!list_empty(&to_remove)) {
1233 struct ceph_cap_flush *cf;
1234 cf = list_first_entry(&to_remove,
Yan, Zhenge4500b52016-07-06 11:12:56 +08001235 struct ceph_cap_flush, i_list);
1236 list_del(&cf->i_list);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001237 ceph_free_cap_flush(cf);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001238 }
Yan, Zheng77310322016-04-08 15:27:16 +08001239
1240 wake_up_all(&ci->i_cap_wq);
Yan, Zheng6c93df52016-04-15 13:56:12 +08001241 if (invalidate)
1242 ceph_queue_invalidate(inode);
Yan, Zheng77310322016-04-08 15:27:16 +08001243 if (drop)
Sage Weil6c99f252010-05-10 16:12:25 -07001244 iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07001245 return 0;
1246}
1247
1248/*
1249 * caller must hold session s_mutex
1250 */
1251static void remove_session_caps(struct ceph_mds_session *session)
1252{
Yan, Zheng6c93df52016-04-15 13:56:12 +08001253 struct ceph_fs_client *fsc = session->s_mdsc->fsc;
1254 struct super_block *sb = fsc->sb;
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001255 LIST_HEAD(dispose);
1256
Sage Weil2f2dc052009-10-06 11:31:09 -07001257 dout("remove_session_caps on %p\n", session);
Yan, Zheng6c93df52016-04-15 13:56:12 +08001258 iterate_session_caps(session, remove_session_caps_cb, fsc);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001259
Yan, Zhengc8799fc2016-07-07 15:22:38 +08001260 wake_up_all(&fsc->mdsc->cap_flushing_wq);
1261
Yan, Zheng6f60f882013-07-24 12:22:11 +08001262 spin_lock(&session->s_cap_lock);
1263 if (session->s_nr_caps > 0) {
Yan, Zheng6f60f882013-07-24 12:22:11 +08001264 struct inode *inode;
1265 struct ceph_cap *cap, *prev = NULL;
1266 struct ceph_vino vino;
1267 /*
1268 * iterate_session_caps() skips inodes that are being
1269 * deleted, we need to wait until deletions are complete.
1270 * __wait_on_freeing_inode() is designed for the job,
1271 * but it is not exported, so use lookup inode function
1272 * to access it.
1273 */
1274 while (!list_empty(&session->s_caps)) {
1275 cap = list_entry(session->s_caps.next,
1276 struct ceph_cap, session_caps);
1277 if (cap == prev)
1278 break;
1279 prev = cap;
1280 vino = cap->ci->i_vino;
1281 spin_unlock(&session->s_cap_lock);
1282
Yan, Zhenged284c42013-09-02 15:19:53 +08001283 inode = ceph_find_inode(sb, vino);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001284 iput(inode);
1285
1286 spin_lock(&session->s_cap_lock);
1287 }
1288 }
Yan, Zheng745a8e32015-05-14 17:22:42 +08001289
1290 // drop cap expires and unlock s_cap_lock
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001291 detach_cap_releases(session, &dispose);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001292
Sage Weil2f2dc052009-10-06 11:31:09 -07001293 BUG_ON(session->s_nr_caps > 0);
Sage Weil6c99f252010-05-10 16:12:25 -07001294 BUG_ON(!list_empty(&session->s_cap_flushing));
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001295 spin_unlock(&session->s_cap_lock);
1296 dispose_cap_releases(session->s_mdsc, &dispose);
Sage Weil2f2dc052009-10-06 11:31:09 -07001297}
1298
1299/*
1300 * wake up any threads waiting on this session's caps. if the cap is
1301 * old (didn't get renewed on the client reconnect), remove it now.
1302 *
1303 * caller must hold s_mutex.
1304 */
1305static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1306 void *arg)
1307{
Sage Weil0dc25702009-11-20 13:43:45 -08001308 struct ceph_inode_info *ci = ceph_inode(inode);
1309
Sage Weil0dc25702009-11-20 13:43:45 -08001310 if (arg) {
Sage Weilbe655592011-11-30 09:47:09 -08001311 spin_lock(&ci->i_ceph_lock);
Sage Weil0dc25702009-11-20 13:43:45 -08001312 ci->i_wanted_max_size = 0;
1313 ci->i_requested_max_size = 0;
Sage Weilbe655592011-11-30 09:47:09 -08001314 spin_unlock(&ci->i_ceph_lock);
Sage Weil0dc25702009-11-20 13:43:45 -08001315 }
Yan, Zhenge5360302016-05-19 19:15:19 +08001316 wake_up_all(&ci->i_cap_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07001317 return 0;
1318}
1319
Sage Weil0dc25702009-11-20 13:43:45 -08001320static void wake_up_session_caps(struct ceph_mds_session *session,
1321 int reconnect)
Sage Weil2f2dc052009-10-06 11:31:09 -07001322{
1323 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
Sage Weil0dc25702009-11-20 13:43:45 -08001324 iterate_session_caps(session, wake_up_session_cb,
1325 (void *)(unsigned long)reconnect);
Sage Weil2f2dc052009-10-06 11:31:09 -07001326}
1327
1328/*
1329 * Send periodic message to MDS renewing all currently held caps. The
1330 * ack will reset the expiration for all caps from this session.
1331 *
1332 * caller holds s_mutex
1333 */
1334static int send_renew_caps(struct ceph_mds_client *mdsc,
1335 struct ceph_mds_session *session)
1336{
1337 struct ceph_msg *msg;
1338 int state;
1339
1340 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1341 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1342 pr_info("mds%d caps stale\n", session->s_mds);
Sage Weile4cb4cb2010-03-18 13:43:09 -07001343 session->s_renew_requested = jiffies;
Sage Weil2f2dc052009-10-06 11:31:09 -07001344
1345 /* do not try to renew caps until a recovering mds has reconnected
1346 * with its clients. */
1347 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1348 if (state < CEPH_MDS_STATE_RECONNECT) {
1349 dout("send_renew_caps ignoring mds%d (%s)\n",
1350 session->s_mds, ceph_mds_state_name(state));
1351 return 0;
1352 }
1353
1354 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1355 ceph_mds_state_name(state));
Sage Weil2f2dc052009-10-06 11:31:09 -07001356 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1357 ++session->s_renew_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001358 if (!msg)
1359 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -07001360 ceph_con_send(&session->s_con, msg);
1361 return 0;
1362}
1363
Yan, Zheng186e4f72013-11-22 14:48:37 +08001364static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
1365 struct ceph_mds_session *session, u64 seq)
1366{
1367 struct ceph_msg *msg;
1368
1369 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
John Spraya687eca2014-09-19 13:51:08 +01001370 session->s_mds, ceph_session_state_name(session->s_state), seq);
Yan, Zheng186e4f72013-11-22 14:48:37 +08001371 msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
1372 if (!msg)
1373 return -ENOMEM;
1374 ceph_con_send(&session->s_con, msg);
1375 return 0;
1376}
1377
1378
Sage Weil2f2dc052009-10-06 11:31:09 -07001379/*
1380 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
Sage Weil0dc25702009-11-20 13:43:45 -08001381 *
1382 * Called under session->s_mutex
Sage Weil2f2dc052009-10-06 11:31:09 -07001383 */
1384static void renewed_caps(struct ceph_mds_client *mdsc,
1385 struct ceph_mds_session *session, int is_renew)
1386{
1387 int was_stale;
1388 int wake = 0;
1389
1390 spin_lock(&session->s_cap_lock);
Alex Elder1ce208a2012-01-12 17:48:11 -08001391 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
Sage Weil2f2dc052009-10-06 11:31:09 -07001392
1393 session->s_cap_ttl = session->s_renew_requested +
1394 mdsc->mdsmap->m_session_timeout*HZ;
1395
1396 if (was_stale) {
1397 if (time_before(jiffies, session->s_cap_ttl)) {
1398 pr_info("mds%d caps renewed\n", session->s_mds);
1399 wake = 1;
1400 } else {
1401 pr_info("mds%d caps still stale\n", session->s_mds);
1402 }
1403 }
1404 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1405 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1406 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1407 spin_unlock(&session->s_cap_lock);
1408
1409 if (wake)
Sage Weil0dc25702009-11-20 13:43:45 -08001410 wake_up_session_caps(session, 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07001411}
1412
1413/*
1414 * send a session close request
1415 */
1416static int request_close_session(struct ceph_mds_client *mdsc,
1417 struct ceph_mds_session *session)
1418{
1419 struct ceph_msg *msg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001420
1421 dout("request_close_session mds%d state %s seq %lld\n",
John Spraya687eca2014-09-19 13:51:08 +01001422 session->s_mds, ceph_session_state_name(session->s_state),
Sage Weil2f2dc052009-10-06 11:31:09 -07001423 session->s_seq);
1424 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001425 if (!msg)
1426 return -ENOMEM;
1427 ceph_con_send(&session->s_con, msg);
Yan, Zhengfcff4152016-09-14 16:39:51 +08001428 return 1;
Sage Weil2f2dc052009-10-06 11:31:09 -07001429}
1430
1431/*
1432 * Called with s_mutex held.
1433 */
1434static int __close_session(struct ceph_mds_client *mdsc,
1435 struct ceph_mds_session *session)
1436{
1437 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1438 return 0;
1439 session->s_state = CEPH_MDS_SESSION_CLOSING;
1440 return request_close_session(mdsc, session);
1441}
1442
Yan, Zheng040d7862017-11-30 11:59:22 +08001443static bool drop_negative_children(struct dentry *dentry)
1444{
1445 struct dentry *child;
1446 bool all_negative = true;
1447
1448 if (!d_is_dir(dentry))
1449 goto out;
1450
1451 spin_lock(&dentry->d_lock);
1452 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
1453 if (d_really_is_positive(child)) {
1454 all_negative = false;
1455 break;
1456 }
1457 }
1458 spin_unlock(&dentry->d_lock);
1459
1460 if (all_negative)
1461 shrink_dcache_parent(dentry);
1462out:
1463 return all_negative;
1464}
1465
Sage Weil2f2dc052009-10-06 11:31:09 -07001466/*
1467 * Trim old(er) caps.
1468 *
1469 * Because we can't cache an inode without one or more caps, we do
1470 * this indirectly: if a cap is unused, we prune its aliases, at which
1471 * point the inode will hopefully get dropped to.
1472 *
1473 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1474 * memory pressure from the MDS, though, so it needn't be perfect.
1475 */
1476static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1477{
1478 struct ceph_mds_session *session = arg;
1479 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng979abfd2013-11-22 13:56:24 +08001480 int used, wanted, oissued, mine;
Sage Weil2f2dc052009-10-06 11:31:09 -07001481
1482 if (session->s_trim_caps <= 0)
1483 return -1;
1484
Sage Weilbe655592011-11-30 09:47:09 -08001485 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001486 mine = cap->issued | cap->implemented;
1487 used = __ceph_caps_used(ci);
Yan, Zheng979abfd2013-11-22 13:56:24 +08001488 wanted = __ceph_caps_file_wanted(ci);
Sage Weil2f2dc052009-10-06 11:31:09 -07001489 oissued = __ceph_caps_issued_other(ci, cap);
1490
Yan, Zheng979abfd2013-11-22 13:56:24 +08001491 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001492 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
Yan, Zheng979abfd2013-11-22 13:56:24 +08001493 ceph_cap_string(used), ceph_cap_string(wanted));
1494 if (cap == ci->i_auth_cap) {
Yan, Zheng622f3e22015-05-07 10:59:47 +08001495 if (ci->i_dirty_caps || ci->i_flushing_caps ||
1496 !list_empty(&ci->i_cap_snaps))
Yan, Zheng979abfd2013-11-22 13:56:24 +08001497 goto out;
1498 if ((used | wanted) & CEPH_CAP_ANY_WR)
1499 goto out;
Yan, Zheng89aa5932017-09-08 15:23:18 +08001500 /* Note: it's possible that i_filelock_ref becomes non-zero
1501 * after dropping auth caps. It doesn't hurt because reply
1502 * of lock mds request will re-add auth caps. */
1503 if (atomic_read(&ci->i_filelock_ref) > 0)
1504 goto out;
Yan, Zheng979abfd2013-11-22 13:56:24 +08001505 }
Yan, Zheng5e804ac2015-10-26 16:08:43 +08001506 /* The inode has cached pages, but it's no longer used.
1507 * we can safely drop it */
1508 if (wanted == 0 && used == CEPH_CAP_FILE_CACHE &&
1509 !(oissued & CEPH_CAP_FILE_CACHE)) {
1510 used = 0;
1511 oissued = 0;
1512 }
Yan, Zheng979abfd2013-11-22 13:56:24 +08001513 if ((used | wanted) & ~oissued & mine)
Sage Weil2f2dc052009-10-06 11:31:09 -07001514 goto out; /* we need these caps */
1515
Sage Weil2f2dc052009-10-06 11:31:09 -07001516 if (oissued) {
1517 /* we aren't the only cap.. just remove us */
Yan, Zhenga096b092013-09-22 10:15:58 +08001518 __ceph_remove_cap(cap, true);
Yan, Zheng040d7862017-11-30 11:59:22 +08001519 session->s_trim_caps--;
Sage Weil2f2dc052009-10-06 11:31:09 -07001520 } else {
Yan, Zheng040d7862017-11-30 11:59:22 +08001521 struct dentry *dentry;
Yan, Zheng5e804ac2015-10-26 16:08:43 +08001522 /* try dropping referring dentries */
Sage Weilbe655592011-11-30 09:47:09 -08001523 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng040d7862017-11-30 11:59:22 +08001524 dentry = d_find_any_alias(inode);
1525 if (dentry && drop_negative_children(dentry)) {
1526 int count;
1527 dput(dentry);
1528 d_prune_aliases(inode);
1529 count = atomic_read(&inode->i_count);
1530 if (count == 1)
1531 session->s_trim_caps--;
1532 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1533 inode, cap, count);
1534 } else {
1535 dput(dentry);
1536 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001537 return 0;
1538 }
1539
1540out:
Sage Weilbe655592011-11-30 09:47:09 -08001541 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001542 return 0;
1543}
1544
1545/*
1546 * Trim session cap count down to some max number.
1547 */
1548static int trim_caps(struct ceph_mds_client *mdsc,
1549 struct ceph_mds_session *session,
1550 int max_caps)
1551{
1552 int trim_caps = session->s_nr_caps - max_caps;
1553
1554 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1555 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1556 if (trim_caps > 0) {
1557 session->s_trim_caps = trim_caps;
1558 iterate_session_caps(session, trim_caps_cb, session);
1559 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1560 session->s_mds, session->s_nr_caps, max_caps,
1561 trim_caps - session->s_trim_caps);
Sage Weil5dacf092009-12-21 20:40:34 -08001562 session->s_trim_caps = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001563 }
Yan, Zhenga56371d2014-04-01 20:34:56 +08001564
Yan, Zhenga56371d2014-04-01 20:34:56 +08001565 ceph_send_cap_releases(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07001566 return 0;
1567}
1568
Yan, Zheng8310b082015-06-09 17:20:12 +08001569static int check_caps_flush(struct ceph_mds_client *mdsc,
1570 u64 want_flush_tid)
1571{
Yan, Zheng8310b082015-06-09 17:20:12 +08001572 int ret = 1;
1573
1574 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zhenge4500b52016-07-06 11:12:56 +08001575 if (!list_empty(&mdsc->cap_flush_list)) {
1576 struct ceph_cap_flush *cf =
1577 list_first_entry(&mdsc->cap_flush_list,
1578 struct ceph_cap_flush, g_list);
1579 if (cf->tid <= want_flush_tid) {
1580 dout("check_caps_flush still flushing tid "
1581 "%llu <= %llu\n", cf->tid, want_flush_tid);
1582 ret = 0;
1583 }
Yan, Zheng8310b082015-06-09 17:20:12 +08001584 }
1585 spin_unlock(&mdsc->cap_dirty_lock);
1586 return ret;
Yan, Zhengd3383a82015-01-08 21:30:12 +08001587}
1588
Sage Weil2f2dc052009-10-06 11:31:09 -07001589/*
1590 * flush all dirty inode data to disk.
1591 *
Yan, Zheng8310b082015-06-09 17:20:12 +08001592 * returns true if we've flushed through want_flush_tid
Sage Weil2f2dc052009-10-06 11:31:09 -07001593 */
Yan, Zhengaffbc192015-05-05 21:22:13 +08001594static void wait_caps_flush(struct ceph_mds_client *mdsc,
Yan, Zheng0e294382016-07-04 18:06:41 +08001595 u64 want_flush_tid)
Sage Weil2f2dc052009-10-06 11:31:09 -07001596{
Yan, Zheng0e294382016-07-04 18:06:41 +08001597 dout("check_caps_flush want %llu\n", want_flush_tid);
Yan, Zheng8310b082015-06-09 17:20:12 +08001598
1599 wait_event(mdsc->cap_flushing_wq,
1600 check_caps_flush(mdsc, want_flush_tid));
1601
1602 dout("check_caps_flush ok, flushed thru %llu\n", want_flush_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07001603}
1604
1605/*
1606 * called under s_mutex
1607 */
Sage Weil3d7ded42010-06-09 16:47:10 -07001608void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1609 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001610{
Yan, Zheng745a8e32015-05-14 17:22:42 +08001611 struct ceph_msg *msg = NULL;
1612 struct ceph_mds_cap_release *head;
1613 struct ceph_mds_cap_item *item;
Jeff Layton92475f02017-04-13 11:07:04 -04001614 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
Yan, Zheng745a8e32015-05-14 17:22:42 +08001615 struct ceph_cap *cap;
1616 LIST_HEAD(tmp_list);
1617 int num_cap_releases;
Jeff Layton92475f02017-04-13 11:07:04 -04001618 __le32 barrier, *cap_barrier;
1619
1620 down_read(&osdc->lock);
1621 barrier = cpu_to_le32(osdc->epoch_barrier);
1622 up_read(&osdc->lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001623
Sage Weil0f8605f2010-05-05 15:51:35 -07001624 spin_lock(&session->s_cap_lock);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001625again:
1626 list_splice_init(&session->s_cap_releases, &tmp_list);
1627 num_cap_releases = session->s_num_cap_releases;
1628 session->s_num_cap_releases = 0;
1629 spin_unlock(&session->s_cap_lock);
1630
1631 while (!list_empty(&tmp_list)) {
1632 if (!msg) {
1633 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001634 PAGE_SIZE, GFP_NOFS, false);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001635 if (!msg)
1636 goto out_err;
1637 head = msg->front.iov_base;
1638 head->num = cpu_to_le32(0);
1639 msg->front.iov_len = sizeof(*head);
Jeff Layton92475f02017-04-13 11:07:04 -04001640
1641 msg->hdr.version = cpu_to_le16(2);
1642 msg->hdr.compat_version = cpu_to_le16(1);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001643 }
Jeff Layton92475f02017-04-13 11:07:04 -04001644
Yan, Zheng745a8e32015-05-14 17:22:42 +08001645 cap = list_first_entry(&tmp_list, struct ceph_cap,
1646 session_caps);
1647 list_del(&cap->session_caps);
1648 num_cap_releases--;
1649
1650 head = msg->front.iov_base;
1651 le32_add_cpu(&head->num, 1);
1652 item = msg->front.iov_base + msg->front.iov_len;
1653 item->ino = cpu_to_le64(cap->cap_ino);
1654 item->cap_id = cpu_to_le64(cap->cap_id);
1655 item->migrate_seq = cpu_to_le32(cap->mseq);
1656 item->seq = cpu_to_le32(cap->issue_seq);
1657 msg->front.iov_len += sizeof(*item);
1658
1659 ceph_put_cap(mdsc, cap);
1660
1661 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
Jeff Layton92475f02017-04-13 11:07:04 -04001662 // Append cap_barrier field
1663 cap_barrier = msg->front.iov_base + msg->front.iov_len;
1664 *cap_barrier = barrier;
1665 msg->front.iov_len += sizeof(*cap_barrier);
1666
Yan, Zheng745a8e32015-05-14 17:22:42 +08001667 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1668 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1669 ceph_con_send(&session->s_con, msg);
1670 msg = NULL;
1671 }
1672 }
1673
1674 BUG_ON(num_cap_releases != 0);
1675
1676 spin_lock(&session->s_cap_lock);
1677 if (!list_empty(&session->s_cap_releases))
1678 goto again;
1679 spin_unlock(&session->s_cap_lock);
1680
1681 if (msg) {
Jeff Layton92475f02017-04-13 11:07:04 -04001682 // Append cap_barrier field
1683 cap_barrier = msg->front.iov_base + msg->front.iov_len;
1684 *cap_barrier = barrier;
1685 msg->front.iov_len += sizeof(*cap_barrier);
1686
Sage Weil2f2dc052009-10-06 11:31:09 -07001687 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1688 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1689 ceph_con_send(&session->s_con, msg);
1690 }
Yan, Zheng745a8e32015-05-14 17:22:42 +08001691 return;
1692out_err:
1693 pr_err("send_cap_releases mds%d, failed to allocate message\n",
1694 session->s_mds);
1695 spin_lock(&session->s_cap_lock);
1696 list_splice(&tmp_list, &session->s_cap_releases);
1697 session->s_num_cap_releases += num_cap_releases;
Sage Weil2f2dc052009-10-06 11:31:09 -07001698 spin_unlock(&session->s_cap_lock);
1699}
1700
1701/*
1702 * requests
1703 */
1704
Yan, Zheng54008392014-03-29 13:41:15 +08001705int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
1706 struct inode *dir)
1707{
1708 struct ceph_inode_info *ci = ceph_inode(dir);
1709 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1710 struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options;
Yan, Zheng2a5beea2016-04-28 09:37:39 +08001711 size_t size = sizeof(struct ceph_mds_reply_dir_entry);
Yan, Zheng54008392014-03-29 13:41:15 +08001712 int order, num_entries;
1713
1714 spin_lock(&ci->i_ceph_lock);
1715 num_entries = ci->i_files + ci->i_subdirs;
1716 spin_unlock(&ci->i_ceph_lock);
1717 num_entries = max(num_entries, 1);
1718 num_entries = min(num_entries, opt->max_readdir);
1719
1720 order = get_order(size * num_entries);
1721 while (order >= 0) {
Yan, Zheng2a5beea2016-04-28 09:37:39 +08001722 rinfo->dir_entries = (void*)__get_free_pages(GFP_KERNEL |
1723 __GFP_NOWARN,
1724 order);
1725 if (rinfo->dir_entries)
Yan, Zheng54008392014-03-29 13:41:15 +08001726 break;
1727 order--;
1728 }
Yan, Zheng2a5beea2016-04-28 09:37:39 +08001729 if (!rinfo->dir_entries)
Yan, Zheng54008392014-03-29 13:41:15 +08001730 return -ENOMEM;
1731
1732 num_entries = (PAGE_SIZE << order) / size;
1733 num_entries = min(num_entries, opt->max_readdir);
1734
1735 rinfo->dir_buf_size = PAGE_SIZE << order;
1736 req->r_num_caps = num_entries + 1;
1737 req->r_args.readdir.max_entries = cpu_to_le32(num_entries);
1738 req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes);
1739 return 0;
1740}
1741
Sage Weil2f2dc052009-10-06 11:31:09 -07001742/*
1743 * Create an mds request.
1744 */
1745struct ceph_mds_request *
1746ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1747{
1748 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1749
1750 if (!req)
1751 return ERR_PTR(-ENOMEM);
1752
Sage Weilb4556392010-05-13 12:01:13 -07001753 mutex_init(&req->r_fill_mutex);
Yehuda Sadeh37151662010-06-17 16:16:12 -07001754 req->r_mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07001755 req->r_started = jiffies;
1756 req->r_resend_mds = -1;
1757 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08001758 INIT_LIST_HEAD(&req->r_unsafe_target_item);
Sage Weil2f2dc052009-10-06 11:31:09 -07001759 req->r_fmode = -1;
Sage Weil153c8e62009-12-07 12:31:09 -08001760 kref_init(&req->r_kref);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02001761 RB_CLEAR_NODE(&req->r_node);
Sage Weil2f2dc052009-10-06 11:31:09 -07001762 INIT_LIST_HEAD(&req->r_wait);
1763 init_completion(&req->r_completion);
1764 init_completion(&req->r_safe_completion);
1765 INIT_LIST_HEAD(&req->r_unsafe_item);
1766
Yan, Zheng56199012017-06-01 16:44:53 +08001767 req->r_stamp = timespec_trunc(current_kernel_time(), mdsc->fsc->sb->s_time_gran);
Sage Weilb8e69062014-05-21 17:41:08 -07001768
Sage Weil2f2dc052009-10-06 11:31:09 -07001769 req->r_op = op;
1770 req->r_direct_mode = mode;
1771 return req;
1772}
1773
1774/*
Sage Weil44ca18f2010-02-15 12:08:46 -08001775 * return oldest (lowest) request, tid in request tree, 0 if none.
Sage Weil2f2dc052009-10-06 11:31:09 -07001776 *
1777 * called under mdsc->mutex.
1778 */
Sage Weil44ca18f2010-02-15 12:08:46 -08001779static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1780{
1781 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1782 return NULL;
1783 return rb_entry(rb_first(&mdsc->request_tree),
1784 struct ceph_mds_request, r_node);
1785}
1786
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08001787static inline u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07001788{
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08001789 return mdsc->oldest_tid;
Sage Weil2f2dc052009-10-06 11:31:09 -07001790}
1791
1792/*
1793 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1794 * on build_path_from_dentry in fs/cifs/dir.c.
1795 *
1796 * If @stop_on_nosnap, generate path relative to the first non-snapped
1797 * inode.
1798 *
1799 * Encode hidden .snap dirs as a double /, i.e.
1800 * foo/.snap/bar -> foo//bar
1801 */
1802char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1803 int stop_on_nosnap)
1804{
1805 struct dentry *temp;
1806 char *path;
1807 int len, pos;
Al Viro1b71fe22011-07-16 23:43:58 -04001808 unsigned seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07001809
Markus Elfringd37b1d92017-08-20 20:22:02 +02001810 if (!dentry)
Sage Weil2f2dc052009-10-06 11:31:09 -07001811 return ERR_PTR(-EINVAL);
1812
1813retry:
1814 len = 0;
Al Viro1b71fe22011-07-16 23:43:58 -04001815 seq = read_seqbegin(&rename_lock);
1816 rcu_read_lock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001817 for (temp = dentry; !IS_ROOT(temp);) {
David Howells2b0143b2015-03-17 22:25:59 +00001818 struct inode *inode = d_inode(temp);
Sage Weil2f2dc052009-10-06 11:31:09 -07001819 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1820 len++; /* slash only */
1821 else if (stop_on_nosnap && inode &&
1822 ceph_snap(inode) == CEPH_NOSNAP)
1823 break;
1824 else
1825 len += 1 + temp->d_name.len;
1826 temp = temp->d_parent;
Sage Weil2f2dc052009-10-06 11:31:09 -07001827 }
Al Viro1b71fe22011-07-16 23:43:58 -04001828 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001829 if (len)
1830 len--; /* no leading '/' */
1831
1832 path = kmalloc(len+1, GFP_NOFS);
Markus Elfringd37b1d92017-08-20 20:22:02 +02001833 if (!path)
Sage Weil2f2dc052009-10-06 11:31:09 -07001834 return ERR_PTR(-ENOMEM);
1835 pos = len;
1836 path[pos] = 0; /* trailing null */
Al Viro1b71fe22011-07-16 23:43:58 -04001837 rcu_read_lock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001838 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
Al Viro1b71fe22011-07-16 23:43:58 -04001839 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07001840
Al Viro1b71fe22011-07-16 23:43:58 -04001841 spin_lock(&temp->d_lock);
David Howells2b0143b2015-03-17 22:25:59 +00001842 inode = d_inode(temp);
Sage Weil2f2dc052009-10-06 11:31:09 -07001843 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
Sage Weil104648a2010-03-18 10:14:30 -07001844 dout("build_path path+%d: %p SNAPDIR\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001845 pos, temp);
1846 } else if (stop_on_nosnap && inode &&
1847 ceph_snap(inode) == CEPH_NOSNAP) {
Yehuda Sadeh9d5a09e2011-12-13 09:57:44 -08001848 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001849 break;
1850 } else {
1851 pos -= temp->d_name.len;
Al Viro1b71fe22011-07-16 23:43:58 -04001852 if (pos < 0) {
1853 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001854 break;
Al Viro1b71fe22011-07-16 23:43:58 -04001855 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001856 strncpy(path + pos, temp->d_name.name,
1857 temp->d_name.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07001858 }
Al Viro1b71fe22011-07-16 23:43:58 -04001859 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001860 if (pos)
1861 path[--pos] = '/';
1862 temp = temp->d_parent;
Sage Weil2f2dc052009-10-06 11:31:09 -07001863 }
Al Viro1b71fe22011-07-16 23:43:58 -04001864 rcu_read_unlock();
1865 if (pos != 0 || read_seqretry(&rename_lock, seq)) {
Sage Weil104648a2010-03-18 10:14:30 -07001866 pr_err("build_path did not end path lookup where "
Sage Weil2f2dc052009-10-06 11:31:09 -07001867 "expected, namelen is %d, pos is %d\n", len, pos);
1868 /* presumably this is only possible if racing with a
1869 rename of one of the parent directories (we can not
1870 lock the dentries above us to prevent this, but
1871 retrying should be harmless) */
1872 kfree(path);
1873 goto retry;
1874 }
1875
David Howells2b0143b2015-03-17 22:25:59 +00001876 *base = ceph_ino(d_inode(temp));
Sage Weil2f2dc052009-10-06 11:31:09 -07001877 *plen = len;
Sage Weil104648a2010-03-18 10:14:30 -07001878 dout("build_path on %p %d built %llx '%.*s'\n",
Al Viro84d08fa2013-07-05 18:59:33 +04001879 dentry, d_count(dentry), *base, len, path);
Sage Weil2f2dc052009-10-06 11:31:09 -07001880 return path;
1881}
1882
Jeff Laytonfd36a712016-12-15 08:37:58 -05001883static int build_dentry_path(struct dentry *dentry, struct inode *dir,
Sage Weil2f2dc052009-10-06 11:31:09 -07001884 const char **ppath, int *ppathlen, u64 *pino,
1885 int *pfreepath)
1886{
1887 char *path;
1888
Jeff Laytonc6b0b652016-12-15 08:37:57 -05001889 rcu_read_lock();
Jeff Laytonfd36a712016-12-15 08:37:58 -05001890 if (!dir)
1891 dir = d_inode_rcu(dentry->d_parent);
Jeff Laytonc6b0b652016-12-15 08:37:57 -05001892 if (dir && ceph_snap(dir) == CEPH_NOSNAP) {
1893 *pino = ceph_ino(dir);
1894 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001895 *ppath = dentry->d_name.name;
1896 *ppathlen = dentry->d_name.len;
1897 return 0;
1898 }
Jeff Laytonc6b0b652016-12-15 08:37:57 -05001899 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001900 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1901 if (IS_ERR(path))
1902 return PTR_ERR(path);
1903 *ppath = path;
1904 *pfreepath = 1;
1905 return 0;
1906}
1907
1908static int build_inode_path(struct inode *inode,
1909 const char **ppath, int *ppathlen, u64 *pino,
1910 int *pfreepath)
1911{
1912 struct dentry *dentry;
1913 char *path;
1914
1915 if (ceph_snap(inode) == CEPH_NOSNAP) {
1916 *pino = ceph_ino(inode);
1917 *ppathlen = 0;
1918 return 0;
1919 }
1920 dentry = d_find_alias(inode);
1921 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1922 dput(dentry);
1923 if (IS_ERR(path))
1924 return PTR_ERR(path);
1925 *ppath = path;
1926 *pfreepath = 1;
1927 return 0;
1928}
1929
1930/*
1931 * request arguments may be specified via an inode *, a dentry *, or
1932 * an explicit ino+path.
1933 */
1934static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
Jeff Laytonfd36a712016-12-15 08:37:58 -05001935 struct inode *rdiri, const char *rpath,
1936 u64 rino, const char **ppath, int *pathlen,
Sage Weil2f2dc052009-10-06 11:31:09 -07001937 u64 *ino, int *freepath)
1938{
1939 int r = 0;
1940
1941 if (rinode) {
1942 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1943 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1944 ceph_snap(rinode));
1945 } else if (rdentry) {
Jeff Laytonfd36a712016-12-15 08:37:58 -05001946 r = build_dentry_path(rdentry, rdiri, ppath, pathlen, ino,
1947 freepath);
Sage Weil2f2dc052009-10-06 11:31:09 -07001948 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1949 *ppath);
Sage Weil795858d2011-08-15 13:02:37 -07001950 } else if (rpath || rino) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001951 *ino = rino;
1952 *ppath = rpath;
David Zafmanb0000562012-10-25 10:23:46 -07001953 *pathlen = rpath ? strlen(rpath) : 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001954 dout(" path %.*s\n", *pathlen, rpath);
1955 }
1956
1957 return r;
1958}
1959
1960/*
1961 * called under mdsc->mutex
1962 */
1963static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1964 struct ceph_mds_request *req,
Yan, Zheng6e6f0922015-02-27 08:54:08 +08001965 int mds, bool drop_cap_releases)
Sage Weil2f2dc052009-10-06 11:31:09 -07001966{
1967 struct ceph_msg *msg;
1968 struct ceph_mds_request_head *head;
1969 const char *path1 = NULL;
1970 const char *path2 = NULL;
1971 u64 ino1 = 0, ino2 = 0;
1972 int pathlen1 = 0, pathlen2 = 0;
1973 int freepath1 = 0, freepath2 = 0;
1974 int len;
1975 u16 releases;
1976 void *p, *end;
1977 int ret;
1978
1979 ret = set_request_path_attr(req->r_inode, req->r_dentry,
Jeff Layton3dd69aa2017-01-31 10:28:26 -05001980 req->r_parent, req->r_path1, req->r_ino1.ino,
Sage Weil2f2dc052009-10-06 11:31:09 -07001981 &path1, &pathlen1, &ino1, &freepath1);
1982 if (ret < 0) {
1983 msg = ERR_PTR(ret);
1984 goto out;
1985 }
1986
1987 ret = set_request_path_attr(NULL, req->r_old_dentry,
Jeff Laytonfd36a712016-12-15 08:37:58 -05001988 req->r_old_dentry_dir,
Sage Weil2f2dc052009-10-06 11:31:09 -07001989 req->r_path2, req->r_ino2.ino,
1990 &path2, &pathlen2, &ino2, &freepath2);
1991 if (ret < 0) {
1992 msg = ERR_PTR(ret);
1993 goto out_free1;
1994 }
1995
1996 len = sizeof(*head) +
Sage Weilb8e69062014-05-21 17:41:08 -07001997 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) +
Arnd Bergmann777d7382015-09-30 15:04:42 +02001998 sizeof(struct ceph_timespec);
Sage Weil2f2dc052009-10-06 11:31:09 -07001999
2000 /* calculate (max) length for cap releases */
2001 len += sizeof(struct ceph_mds_request_release) *
2002 (!!req->r_inode_drop + !!req->r_dentry_drop +
2003 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
2004 if (req->r_dentry_drop)
2005 len += req->r_dentry->d_name.len;
2006 if (req->r_old_dentry_drop)
2007 len += req->r_old_dentry->d_name.len;
2008
Sage Weilb61c2762011-08-09 15:03:46 -07002009 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07002010 if (!msg) {
2011 msg = ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -07002012 goto out_free2;
Sage Weila79832f2010-04-01 16:06:19 -07002013 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002014
John Spray7cfa0312014-10-30 17:15:26 +00002015 msg->hdr.version = cpu_to_le16(2);
Sage Weil6df058c2009-12-22 11:24:33 -08002016 msg->hdr.tid = cpu_to_le64(req->r_tid);
2017
Sage Weil2f2dc052009-10-06 11:31:09 -07002018 head = msg->front.iov_base;
2019 p = msg->front.iov_base + sizeof(*head);
2020 end = msg->front.iov_base + msg->front.iov_len;
2021
2022 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
2023 head->op = cpu_to_le32(req->r_op);
Eric W. Biedermanff3d0042013-01-31 04:01:53 -08002024 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid));
2025 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid));
Sage Weil2f2dc052009-10-06 11:31:09 -07002026 head->args = req->r_args;
2027
2028 ceph_encode_filepath(&p, end, ino1, path1);
2029 ceph_encode_filepath(&p, end, ino2, path2);
2030
Sage Weile979cf52010-07-15 14:58:39 -07002031 /* make note of release offset, in case we need to replay */
2032 req->r_request_release_offset = p - msg->front.iov_base;
2033
Sage Weil2f2dc052009-10-06 11:31:09 -07002034 /* cap releases */
2035 releases = 0;
2036 if (req->r_inode_drop)
2037 releases += ceph_encode_inode_release(&p,
David Howells2b0143b2015-03-17 22:25:59 +00002038 req->r_inode ? req->r_inode : d_inode(req->r_dentry),
Sage Weil2f2dc052009-10-06 11:31:09 -07002039 mds, req->r_inode_drop, req->r_inode_unless, 0);
2040 if (req->r_dentry_drop)
2041 releases += ceph_encode_dentry_release(&p, req->r_dentry,
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002042 req->r_parent, mds, req->r_dentry_drop,
Jeff Laytonca6c8ae2016-12-15 08:37:59 -05002043 req->r_dentry_unless);
Sage Weil2f2dc052009-10-06 11:31:09 -07002044 if (req->r_old_dentry_drop)
2045 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
Jeff Laytonca6c8ae2016-12-15 08:37:59 -05002046 req->r_old_dentry_dir, mds,
2047 req->r_old_dentry_drop,
2048 req->r_old_dentry_unless);
Sage Weil2f2dc052009-10-06 11:31:09 -07002049 if (req->r_old_inode_drop)
2050 releases += ceph_encode_inode_release(&p,
David Howells2b0143b2015-03-17 22:25:59 +00002051 d_inode(req->r_old_dentry),
Sage Weil2f2dc052009-10-06 11:31:09 -07002052 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002053
2054 if (drop_cap_releases) {
2055 releases = 0;
2056 p = msg->front.iov_base + req->r_request_release_offset;
2057 }
2058
Sage Weil2f2dc052009-10-06 11:31:09 -07002059 head->num_releases = cpu_to_le16(releases);
2060
Sage Weilb8e69062014-05-21 17:41:08 -07002061 /* time stamp */
Yan, Zheng1f041a82015-01-13 15:20:52 +08002062 {
2063 struct ceph_timespec ts;
2064 ceph_encode_timespec(&ts, &req->r_stamp);
2065 ceph_encode_copy(&p, &ts, sizeof(ts));
2066 }
Sage Weilb8e69062014-05-21 17:41:08 -07002067
Sage Weil2f2dc052009-10-06 11:31:09 -07002068 BUG_ON(p > end);
2069 msg->front.iov_len = p - msg->front.iov_base;
2070 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2071
Yan, Zheng25e6bae2014-09-16 19:15:28 +08002072 if (req->r_pagelist) {
2073 struct ceph_pagelist *pagelist = req->r_pagelist;
Elena Reshetova0e1a5ee2017-03-17 14:10:29 +02002074 refcount_inc(&pagelist->refcnt);
Yan, Zheng25e6bae2014-09-16 19:15:28 +08002075 ceph_msg_data_add_pagelist(msg, pagelist);
2076 msg->hdr.data_len = cpu_to_le32(pagelist->length);
2077 } else {
2078 msg->hdr.data_len = 0;
Alex Elderebf18f42013-03-04 22:29:57 -06002079 }
Alex Elder02afca62013-02-14 12:16:43 -06002080
Sage Weil2f2dc052009-10-06 11:31:09 -07002081 msg->hdr.data_off = cpu_to_le16(0);
2082
2083out_free2:
2084 if (freepath2)
2085 kfree((char *)path2);
2086out_free1:
2087 if (freepath1)
2088 kfree((char *)path1);
2089out:
2090 return msg;
2091}
2092
2093/*
2094 * called under mdsc->mutex if error, under no mutex if
2095 * success.
2096 */
2097static void complete_request(struct ceph_mds_client *mdsc,
2098 struct ceph_mds_request *req)
2099{
2100 if (req->r_callback)
2101 req->r_callback(mdsc, req);
2102 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002103 complete_all(&req->r_completion);
Sage Weil2f2dc052009-10-06 11:31:09 -07002104}
2105
2106/*
2107 * called under mdsc->mutex
2108 */
2109static int __prepare_send_request(struct ceph_mds_client *mdsc,
2110 struct ceph_mds_request *req,
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002111 int mds, bool drop_cap_releases)
Sage Weil2f2dc052009-10-06 11:31:09 -07002112{
2113 struct ceph_mds_request_head *rhead;
2114 struct ceph_msg *msg;
2115 int flags = 0;
2116
Sage Weil2f2dc052009-10-06 11:31:09 -07002117 req->r_attempts++;
Greg Farnume55b71f2010-06-22 15:58:01 -07002118 if (req->r_inode) {
2119 struct ceph_cap *cap =
2120 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
2121
2122 if (cap)
2123 req->r_sent_on_mseq = cap->mseq;
2124 else
2125 req->r_sent_on_mseq = -1;
2126 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002127 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
2128 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
2129
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002130 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002131 void *p;
Sage Weil01a92f12010-07-15 13:24:32 -07002132 /*
2133 * Replay. Do not regenerate message (and rebuild
2134 * paths, etc.); just use the original message.
2135 * Rebuilding paths will break for renames because
2136 * d_move mangles the src name.
2137 */
2138 msg = req->r_request;
2139 rhead = msg->front.iov_base;
2140
2141 flags = le32_to_cpu(rhead->flags);
2142 flags |= CEPH_MDS_FLAG_REPLAY;
2143 rhead->flags = cpu_to_le32(flags);
2144
2145 if (req->r_target_inode)
2146 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
2147
2148 rhead->num_retry = req->r_attempts - 1;
Sage Weile979cf52010-07-15 14:58:39 -07002149
2150 /* remove cap/dentry releases from message */
2151 rhead->num_releases = 0;
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002152
2153 /* time stamp */
2154 p = msg->front.iov_base + req->r_request_release_offset;
Yan, Zheng1f041a82015-01-13 15:20:52 +08002155 {
2156 struct ceph_timespec ts;
2157 ceph_encode_timespec(&ts, &req->r_stamp);
2158 ceph_encode_copy(&p, &ts, sizeof(ts));
2159 }
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002160
2161 msg->front.iov_len = p - msg->front.iov_base;
2162 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
Sage Weil01a92f12010-07-15 13:24:32 -07002163 return 0;
2164 }
2165
Sage Weil2f2dc052009-10-06 11:31:09 -07002166 if (req->r_request) {
2167 ceph_msg_put(req->r_request);
2168 req->r_request = NULL;
2169 }
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002170 msg = create_request_message(mdsc, req, mds, drop_cap_releases);
Sage Weil2f2dc052009-10-06 11:31:09 -07002171 if (IS_ERR(msg)) {
Sage Weile1518c72010-05-13 11:19:06 -07002172 req->r_err = PTR_ERR(msg);
Sage Weila79832f2010-04-01 16:06:19 -07002173 return PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002174 }
2175 req->r_request = msg;
2176
2177 rhead = msg->front.iov_base;
Sage Weil2f2dc052009-10-06 11:31:09 -07002178 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002179 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Sage Weil2f2dc052009-10-06 11:31:09 -07002180 flags |= CEPH_MDS_FLAG_REPLAY;
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002181 if (req->r_parent)
Sage Weil2f2dc052009-10-06 11:31:09 -07002182 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
2183 rhead->flags = cpu_to_le32(flags);
2184 rhead->num_fwd = req->r_num_fwd;
2185 rhead->num_retry = req->r_attempts - 1;
Sage Weil01a92f12010-07-15 13:24:32 -07002186 rhead->ino = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002187
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002188 dout(" r_parent = %p\n", req->r_parent);
Sage Weil2f2dc052009-10-06 11:31:09 -07002189 return 0;
2190}
2191
2192/*
2193 * send request, or put it on the appropriate wait list.
2194 */
2195static int __do_request(struct ceph_mds_client *mdsc,
2196 struct ceph_mds_request *req)
2197{
2198 struct ceph_mds_session *session = NULL;
2199 int mds = -1;
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002200 int err = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002201
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002202 if (req->r_err || test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
2203 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
Yan, Zhengeb1b8af2013-09-26 14:25:36 +08002204 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002205 goto out;
Yan, Zhengeb1b8af2013-09-26 14:25:36 +08002206 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002207
2208 if (req->r_timeout &&
2209 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
2210 dout("do_request timed out\n");
2211 err = -EIO;
2212 goto finish;
2213 }
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01002214 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002215 dout("do_request forced umount\n");
2216 err = -EIO;
2217 goto finish;
2218 }
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01002219 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_MOUNTING) {
Yan, Zhenge9e427f2016-11-10 16:02:06 +08002220 if (mdsc->mdsmap_err) {
2221 err = mdsc->mdsmap_err;
2222 dout("do_request mdsmap err %d\n", err);
2223 goto finish;
2224 }
Yan, Zhengcc8e8342017-01-04 16:21:58 +08002225 if (mdsc->mdsmap->m_epoch == 0) {
2226 dout("do_request no mdsmap, waiting for map\n");
2227 list_add(&req->r_wait, &mdsc->waiting_for_map);
2228 goto finish;
2229 }
Yan, Zhenge9e427f2016-11-10 16:02:06 +08002230 if (!(mdsc->fsc->mount_options->flags &
2231 CEPH_MOUNT_OPT_MOUNTWAIT) &&
2232 !ceph_mdsmap_is_cluster_available(mdsc->mdsmap)) {
2233 err = -ENOENT;
2234 pr_info("probably no mds server is up\n");
2235 goto finish;
2236 }
2237 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002238
Sage Weildc69e2e2010-11-02 13:49:00 -07002239 put_request_session(req);
2240
Sage Weil2f2dc052009-10-06 11:31:09 -07002241 mds = __choose_mds(mdsc, req);
2242 if (mds < 0 ||
2243 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
2244 dout("do_request no mds or not active, waiting for map\n");
2245 list_add(&req->r_wait, &mdsc->waiting_for_map);
2246 goto out;
2247 }
2248
2249 /* get, open session */
2250 session = __ceph_lookup_mds_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07002251 if (!session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002252 session = register_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07002253 if (IS_ERR(session)) {
2254 err = PTR_ERR(session);
2255 goto finish;
2256 }
2257 }
Sage Weildc69e2e2010-11-02 13:49:00 -07002258 req->r_session = get_session(session);
2259
Sage Weil2f2dc052009-10-06 11:31:09 -07002260 dout("do_request mds%d session %p state %s\n", mds, session,
John Spraya687eca2014-09-19 13:51:08 +01002261 ceph_session_state_name(session->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07002262 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
2263 session->s_state != CEPH_MDS_SESSION_HUNG) {
Yan, Zhengfcff4152016-09-14 16:39:51 +08002264 if (session->s_state == CEPH_MDS_SESSION_REJECTED) {
2265 err = -EACCES;
2266 goto out_session;
2267 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002268 if (session->s_state == CEPH_MDS_SESSION_NEW ||
2269 session->s_state == CEPH_MDS_SESSION_CLOSING)
2270 __open_session(mdsc, session);
2271 list_add(&req->r_wait, &session->s_waiting);
2272 goto out_session;
2273 }
2274
2275 /* send request */
Sage Weil2f2dc052009-10-06 11:31:09 -07002276 req->r_resend_mds = -1; /* forget any previous mds hint */
2277
2278 if (req->r_request_started == 0) /* note request start time */
2279 req->r_request_started = jiffies;
2280
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002281 err = __prepare_send_request(mdsc, req, mds, false);
Sage Weil2f2dc052009-10-06 11:31:09 -07002282 if (!err) {
2283 ceph_msg_get(req->r_request);
2284 ceph_con_send(&session->s_con, req->r_request);
2285 }
2286
2287out_session:
2288 ceph_put_mds_session(session);
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002289finish:
2290 if (err) {
2291 dout("__do_request early error %d\n", err);
2292 req->r_err = err;
2293 complete_request(mdsc, req);
2294 __unregister_request(mdsc, req);
2295 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002296out:
2297 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002298}
2299
2300/*
2301 * called under mdsc->mutex
2302 */
2303static void __wake_requests(struct ceph_mds_client *mdsc,
2304 struct list_head *head)
2305{
Yan, Zhenged75ec22012-11-19 10:49:06 +08002306 struct ceph_mds_request *req;
2307 LIST_HEAD(tmp_list);
Sage Weil2f2dc052009-10-06 11:31:09 -07002308
Yan, Zhenged75ec22012-11-19 10:49:06 +08002309 list_splice_init(head, &tmp_list);
2310
2311 while (!list_empty(&tmp_list)) {
2312 req = list_entry(tmp_list.next,
2313 struct ceph_mds_request, r_wait);
Sage Weil2f2dc052009-10-06 11:31:09 -07002314 list_del_init(&req->r_wait);
Sage Weil7971bd92013-05-01 21:15:58 -07002315 dout(" wake request %p tid %llu\n", req, req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002316 __do_request(mdsc, req);
2317 }
2318}
2319
2320/*
2321 * Wake up threads with requests pending for @mds, so that they can
Sage Weil29790f22010-03-18 14:45:05 -07002322 * resubmit their requests to a possibly different mds.
Sage Weil2f2dc052009-10-06 11:31:09 -07002323 */
Sage Weil29790f22010-03-18 14:45:05 -07002324static void kick_requests(struct ceph_mds_client *mdsc, int mds)
Sage Weil2f2dc052009-10-06 11:31:09 -07002325{
Sage Weil44ca18f2010-02-15 12:08:46 -08002326 struct ceph_mds_request *req;
Yan, Zheng282c1052014-07-30 10:12:47 +08002327 struct rb_node *p = rb_first(&mdsc->request_tree);
Sage Weil2f2dc052009-10-06 11:31:09 -07002328
2329 dout("kick_requests mds%d\n", mds);
Yan, Zheng282c1052014-07-30 10:12:47 +08002330 while (p) {
Sage Weil44ca18f2010-02-15 12:08:46 -08002331 req = rb_entry(p, struct ceph_mds_request, r_node);
Yan, Zheng282c1052014-07-30 10:12:47 +08002332 p = rb_next(p);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002333 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Sage Weil44ca18f2010-02-15 12:08:46 -08002334 continue;
Yan, Zheng3de22be2015-02-04 14:26:22 +08002335 if (req->r_attempts > 0)
2336 continue; /* only new requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08002337 if (req->r_session &&
2338 req->r_session->s_mds == mds) {
2339 dout(" kicking tid %llu\n", req->r_tid);
Yan, Zheng03974e82014-09-11 14:28:56 +08002340 list_del_init(&req->r_wait);
Sage Weil44ca18f2010-02-15 12:08:46 -08002341 __do_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002342 }
2343 }
2344}
2345
2346void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
2347 struct ceph_mds_request *req)
2348{
2349 dout("submit_request on %p\n", req);
2350 mutex_lock(&mdsc->mutex);
2351 __register_request(mdsc, req, NULL);
2352 __do_request(mdsc, req);
2353 mutex_unlock(&mdsc->mutex);
2354}
2355
2356/*
2357 * Synchrously perform an mds request. Take care of all of the
2358 * session setup, forwarding, retry details.
2359 */
2360int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
2361 struct inode *dir,
2362 struct ceph_mds_request *req)
2363{
2364 int err;
2365
2366 dout("do_request on %p\n", req);
2367
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002368 /* take CAP_PIN refs for r_inode, r_parent, r_old_dentry */
Sage Weil2f2dc052009-10-06 11:31:09 -07002369 if (req->r_inode)
2370 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002371 if (req->r_parent)
2372 ceph_get_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
Sage Weil844d87c2013-02-05 13:40:09 -08002373 if (req->r_old_dentry_dir)
Sage Weil41b02e12011-07-26 11:31:14 -07002374 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
2375 CEPH_CAP_PIN);
Sage Weil2f2dc052009-10-06 11:31:09 -07002376
2377 /* issue */
2378 mutex_lock(&mdsc->mutex);
2379 __register_request(mdsc, req, dir);
2380 __do_request(mdsc, req);
2381
Sage Weile1518c72010-05-13 11:19:06 -07002382 if (req->r_err) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002383 err = req->r_err;
Sage Weile1518c72010-05-13 11:19:06 -07002384 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07002385 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002386
Sage Weile1518c72010-05-13 11:19:06 -07002387 /* wait */
2388 mutex_unlock(&mdsc->mutex);
2389 dout("do_request waiting\n");
Ilya Dryomov5be73032015-05-19 12:05:38 +03002390 if (!req->r_timeout && req->r_wait_for_completion) {
Yan, Zheng9280be22014-10-14 10:33:35 +08002391 err = req->r_wait_for_completion(mdsc, req);
Sage Weile1518c72010-05-13 11:19:06 -07002392 } else {
Ilya Dryomov5be73032015-05-19 12:05:38 +03002393 long timeleft = wait_for_completion_killable_timeout(
2394 &req->r_completion,
2395 ceph_timeout_jiffies(req->r_timeout));
2396 if (timeleft > 0)
2397 err = 0;
2398 else if (!timeleft)
2399 err = -EIO; /* timed out */
2400 else
2401 err = timeleft; /* killed */
Sage Weile1518c72010-05-13 11:19:06 -07002402 }
2403 dout("do_request waited, got %d\n", err);
2404 mutex_lock(&mdsc->mutex);
2405
2406 /* only abort if we didn't race with a real reply */
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002407 if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
Sage Weile1518c72010-05-13 11:19:06 -07002408 err = le32_to_cpu(req->r_reply_info.head->result);
2409 } else if (err < 0) {
2410 dout("aborted request %lld with %d\n", req->r_tid, err);
Sage Weilb4556392010-05-13 12:01:13 -07002411
2412 /*
2413 * ensure we aren't running concurrently with
2414 * ceph_fill_trace or ceph_readdir_prepopulate, which
2415 * rely on locks (dir mutex) held by our caller.
2416 */
2417 mutex_lock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07002418 req->r_err = err;
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002419 set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
Sage Weilb4556392010-05-13 12:01:13 -07002420 mutex_unlock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07002421
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002422 if (req->r_parent &&
Sage Weil167c9e32010-05-14 10:02:57 -07002423 (req->r_op & CEPH_MDS_OP_WRITE))
2424 ceph_invalidate_dir_request(req);
Sage Weile1518c72010-05-13 11:19:06 -07002425 } else {
2426 err = req->r_err;
2427 }
2428
2429out:
2430 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002431 dout("do_request %p done, result %d\n", req, err);
2432 return err;
2433}
2434
2435/*
Yan, Zheng2f276c52013-03-13 19:44:32 +08002436 * Invalidate dir's completeness, dentry lease state on an aborted MDS
Sage Weil167c9e32010-05-14 10:02:57 -07002437 * namespace request.
2438 */
2439void ceph_invalidate_dir_request(struct ceph_mds_request *req)
2440{
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002441 struct inode *inode = req->r_parent;
Sage Weil167c9e32010-05-14 10:02:57 -07002442
Yan, Zheng2f276c52013-03-13 19:44:32 +08002443 dout("invalidate_dir_request %p (complete, lease(s))\n", inode);
Sage Weil167c9e32010-05-14 10:02:57 -07002444
Yan, Zheng2f276c52013-03-13 19:44:32 +08002445 ceph_dir_clear_complete(inode);
Sage Weil167c9e32010-05-14 10:02:57 -07002446 if (req->r_dentry)
2447 ceph_invalidate_dentry_lease(req->r_dentry);
2448 if (req->r_old_dentry)
2449 ceph_invalidate_dentry_lease(req->r_old_dentry);
2450}
2451
2452/*
Sage Weil2f2dc052009-10-06 11:31:09 -07002453 * Handle mds reply.
2454 *
2455 * We take the session mutex and parse and process the reply immediately.
2456 * This preserves the logical ordering of replies, capabilities, etc., sent
2457 * by the MDS as they are applied to our local cache.
2458 */
2459static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2460{
2461 struct ceph_mds_client *mdsc = session->s_mdsc;
2462 struct ceph_mds_request *req;
2463 struct ceph_mds_reply_head *head = msg->front.iov_base;
2464 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
Yan, Zheng982d6012014-12-23 15:30:54 +08002465 struct ceph_snap_realm *realm;
Sage Weil2f2dc052009-10-06 11:31:09 -07002466 u64 tid;
2467 int err, result;
Sage Weil2600d2d2010-02-22 15:12:16 -08002468 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002469
Sage Weil2f2dc052009-10-06 11:31:09 -07002470 if (msg->front.iov_len < sizeof(*head)) {
2471 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002472 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002473 return;
2474 }
2475
2476 /* get request, session */
Sage Weil6df058c2009-12-22 11:24:33 -08002477 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002478 mutex_lock(&mdsc->mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02002479 req = lookup_get_request(mdsc, tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002480 if (!req) {
2481 dout("handle_reply on unknown tid %llu\n", tid);
2482 mutex_unlock(&mdsc->mutex);
2483 return;
2484 }
2485 dout("handle_reply %p\n", req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002486
2487 /* correct session? */
Sage Weild96d6042010-03-20 20:50:58 -07002488 if (req->r_session != session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002489 pr_err("mdsc_handle_reply got %llu on session mds%d"
2490 " not mds%d\n", tid, session->s_mds,
2491 req->r_session ? req->r_session->s_mds : -1);
2492 mutex_unlock(&mdsc->mutex);
2493 goto out;
2494 }
2495
2496 /* dup? */
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002497 if ((test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags) && !head->safe) ||
2498 (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags) && head->safe)) {
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07002499 pr_warn("got a dup %s reply on %llu from mds%d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002500 head->safe ? "safe" : "unsafe", tid, mds);
2501 mutex_unlock(&mdsc->mutex);
2502 goto out;
2503 }
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002504 if (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags)) {
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07002505 pr_warn("got unsafe after safe on %llu from mds%d\n",
Sage Weil85792d0d2010-05-13 09:06:02 -07002506 tid, mds);
2507 mutex_unlock(&mdsc->mutex);
2508 goto out;
2509 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002510
2511 result = le32_to_cpu(head->result);
2512
2513 /*
Greg Farnume55b71f2010-06-22 15:58:01 -07002514 * Handle an ESTALE
2515 * if we're not talking to the authority, send to them
2516 * if the authority has changed while we weren't looking,
2517 * send to new authority
2518 * Otherwise we just have to return an ESTALE
Sage Weil2f2dc052009-10-06 11:31:09 -07002519 */
2520 if (result == -ESTALE) {
Greg Farnume55b71f2010-06-22 15:58:01 -07002521 dout("got ESTALE on request %llu", req->r_tid);
Yan, Zheng51da8e82014-07-14 10:48:29 +08002522 req->r_resend_mds = -1;
Yan, Zhengca18bed2013-11-22 14:21:44 +08002523 if (req->r_direct_mode != USE_AUTH_MDS) {
Greg Farnume55b71f2010-06-22 15:58:01 -07002524 dout("not using auth, setting for that now");
2525 req->r_direct_mode = USE_AUTH_MDS;
Sage Weil2f2dc052009-10-06 11:31:09 -07002526 __do_request(mdsc, req);
2527 mutex_unlock(&mdsc->mutex);
2528 goto out;
Greg Farnume55b71f2010-06-22 15:58:01 -07002529 } else {
Yan, Zhengca18bed2013-11-22 14:21:44 +08002530 int mds = __choose_mds(mdsc, req);
2531 if (mds >= 0 && mds != req->r_session->s_mds) {
2532 dout("but auth changed, so resending");
Greg Farnume55b71f2010-06-22 15:58:01 -07002533 __do_request(mdsc, req);
2534 mutex_unlock(&mdsc->mutex);
2535 goto out;
2536 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002537 }
Greg Farnume55b71f2010-06-22 15:58:01 -07002538 dout("have to return ESTALE on request %llu", req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002539 }
2540
Greg Farnume55b71f2010-06-22 15:58:01 -07002541
Sage Weil2f2dc052009-10-06 11:31:09 -07002542 if (head->safe) {
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002543 set_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags);
Sage Weil2f2dc052009-10-06 11:31:09 -07002544 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002545
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002546 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002547 /*
2548 * We already handled the unsafe response, now do the
2549 * cleanup. No need to examine the response; the MDS
2550 * doesn't include any result info in the safe
2551 * response. And even if it did, there is nothing
2552 * useful we could do with a revised return value.
2553 */
2554 dout("got safe reply %llu, mds%d\n", tid, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002555
2556 /* last unsafe request during umount? */
Sage Weil44ca18f2010-02-15 12:08:46 -08002557 if (mdsc->stopping && !__get_oldest_req(mdsc))
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002558 complete_all(&mdsc->safe_umount_waiters);
Sage Weil2f2dc052009-10-06 11:31:09 -07002559 mutex_unlock(&mdsc->mutex);
2560 goto out;
2561 }
Sage Weile1518c72010-05-13 11:19:06 -07002562 } else {
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002563 set_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags);
Sage Weil2f2dc052009-10-06 11:31:09 -07002564 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
Yan, Zheng4c06ace2015-10-27 17:18:00 +08002565 if (req->r_unsafe_dir) {
2566 struct ceph_inode_info *ci =
2567 ceph_inode(req->r_unsafe_dir);
2568 spin_lock(&ci->i_unsafe_lock);
2569 list_add_tail(&req->r_unsafe_dir_item,
2570 &ci->i_unsafe_dirops);
2571 spin_unlock(&ci->i_unsafe_lock);
2572 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002573 }
2574
2575 dout("handle_reply tid %lld result %d\n", tid, result);
2576 rinfo = &req->r_reply_info;
Sage Weil14303d22010-12-14 17:37:52 -08002577 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
Sage Weil2f2dc052009-10-06 11:31:09 -07002578 mutex_unlock(&mdsc->mutex);
2579
2580 mutex_lock(&session->s_mutex);
2581 if (err < 0) {
Herb Shiu25933ab2010-12-01 14:14:38 -08002582 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002583 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002584 goto out_err;
2585 }
2586
2587 /* snap trace */
Yan, Zheng982d6012014-12-23 15:30:54 +08002588 realm = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07002589 if (rinfo->snapblob_len) {
2590 down_write(&mdsc->snap_rwsem);
2591 ceph_update_snap_trace(mdsc, rinfo->snapblob,
Yan, Zheng982d6012014-12-23 15:30:54 +08002592 rinfo->snapblob + rinfo->snapblob_len,
2593 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP,
2594 &realm);
Sage Weil2f2dc052009-10-06 11:31:09 -07002595 downgrade_write(&mdsc->snap_rwsem);
2596 } else {
2597 down_read(&mdsc->snap_rwsem);
2598 }
2599
2600 /* insert trace into our cache */
Sage Weilb4556392010-05-13 12:01:13 -07002601 mutex_lock(&req->r_fill_mutex);
Yan, Zheng315f2402016-03-07 10:34:50 +08002602 current->journal_info = req;
Jeff Laytonf5a03b02017-01-31 11:06:13 -05002603 err = ceph_fill_trace(mdsc->fsc->sb, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002604 if (err == 0) {
Sam Lang6e8575f2012-12-28 09:56:46 -08002605 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
Yan, Zheng81c6aea2013-09-18 09:44:13 +08002606 req->r_op == CEPH_MDS_OP_LSSNAP))
Sage Weil2f2dc052009-10-06 11:31:09 -07002607 ceph_readdir_prepopulate(req, req->r_session);
Yehuda Sadeh37151662010-06-17 16:16:12 -07002608 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
Sage Weil2f2dc052009-10-06 11:31:09 -07002609 }
Yan, Zheng315f2402016-03-07 10:34:50 +08002610 current->journal_info = NULL;
Sage Weilb4556392010-05-13 12:01:13 -07002611 mutex_unlock(&req->r_fill_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002612
2613 up_read(&mdsc->snap_rwsem);
Yan, Zheng982d6012014-12-23 15:30:54 +08002614 if (realm)
2615 ceph_put_snap_realm(mdsc, realm);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002616
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002617 if (err == 0 && req->r_target_inode &&
2618 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002619 struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
2620 spin_lock(&ci->i_unsafe_lock);
2621 list_add_tail(&req->r_unsafe_target_item, &ci->i_unsafe_iops);
2622 spin_unlock(&ci->i_unsafe_lock);
2623 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002624out_err:
Sage Weile1518c72010-05-13 11:19:06 -07002625 mutex_lock(&mdsc->mutex);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002626 if (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
Sage Weile1518c72010-05-13 11:19:06 -07002627 if (err) {
2628 req->r_err = err;
2629 } else {
Jianpeng Ma5fdb1382015-08-18 10:30:38 +08002630 req->r_reply = ceph_msg_get(msg);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002631 set_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags);
Sage Weile1518c72010-05-13 11:19:06 -07002632 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002633 } else {
Sage Weile1518c72010-05-13 11:19:06 -07002634 dout("reply arrived after request %lld was aborted\n", tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002635 }
Sage Weile1518c72010-05-13 11:19:06 -07002636 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002637
Sage Weil2f2dc052009-10-06 11:31:09 -07002638 mutex_unlock(&session->s_mutex);
2639
2640 /* kick calling process */
2641 complete_request(mdsc, req);
2642out:
2643 ceph_mdsc_put_request(req);
2644 return;
2645}
2646
2647
2648
2649/*
2650 * handle mds notification that our request has been forwarded.
2651 */
Sage Weil2600d2d2010-02-22 15:12:16 -08002652static void handle_forward(struct ceph_mds_client *mdsc,
2653 struct ceph_mds_session *session,
2654 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002655{
2656 struct ceph_mds_request *req;
Sage Weila1ea7872010-02-23 14:02:44 -08002657 u64 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002658 u32 next_mds;
2659 u32 fwd_seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002660 int err = -EINVAL;
2661 void *p = msg->front.iov_base;
2662 void *end = p + msg->front.iov_len;
Sage Weil2f2dc052009-10-06 11:31:09 -07002663
Sage Weila1ea7872010-02-23 14:02:44 -08002664 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002665 next_mds = ceph_decode_32(&p);
2666 fwd_seq = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07002667
2668 mutex_lock(&mdsc->mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02002669 req = lookup_get_request(mdsc, tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002670 if (!req) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07002671 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002672 goto out; /* dup reply? */
2673 }
2674
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002675 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07002676 dout("forward tid %llu aborted, unregistering\n", tid);
2677 __unregister_request(mdsc, req);
2678 } else if (fwd_seq <= req->r_num_fwd) {
2679 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002680 tid, next_mds, req->r_num_fwd, fwd_seq);
2681 } else {
2682 /* resend. forward race not possible; mds would drop */
Sage Weil2a8e5e32010-05-28 16:43:16 -07002683 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2684 BUG_ON(req->r_err);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002685 BUG_ON(test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags));
Yan, Zheng3de22be2015-02-04 14:26:22 +08002686 req->r_attempts = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002687 req->r_num_fwd = fwd_seq;
2688 req->r_resend_mds = next_mds;
2689 put_request_session(req);
2690 __do_request(mdsc, req);
2691 }
2692 ceph_mdsc_put_request(req);
2693out:
2694 mutex_unlock(&mdsc->mutex);
2695 return;
2696
2697bad:
2698 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2699}
2700
2701/*
2702 * handle a mds session control message
2703 */
2704static void handle_session(struct ceph_mds_session *session,
2705 struct ceph_msg *msg)
2706{
2707 struct ceph_mds_client *mdsc = session->s_mdsc;
2708 u32 op;
2709 u64 seq;
Sage Weil2600d2d2010-02-22 15:12:16 -08002710 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002711 struct ceph_mds_session_head *h = msg->front.iov_base;
2712 int wake = 0;
2713
Sage Weil2f2dc052009-10-06 11:31:09 -07002714 /* decode */
2715 if (msg->front.iov_len != sizeof(*h))
2716 goto bad;
2717 op = le32_to_cpu(h->op);
2718 seq = le64_to_cpu(h->seq);
2719
2720 mutex_lock(&mdsc->mutex);
Yan, Zheng0a07fc82017-03-29 15:30:24 +08002721 if (op == CEPH_SESSION_CLOSE) {
2722 get_session(session);
Sage Weil2600d2d2010-02-22 15:12:16 -08002723 __unregister_session(mdsc, session);
Yan, Zheng0a07fc82017-03-29 15:30:24 +08002724 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002725 /* FIXME: this ttl calculation is generous */
2726 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2727 mutex_unlock(&mdsc->mutex);
2728
2729 mutex_lock(&session->s_mutex);
2730
2731 dout("handle_session mds%d %s %p state %s seq %llu\n",
2732 mds, ceph_session_op_name(op), session,
John Spraya687eca2014-09-19 13:51:08 +01002733 ceph_session_state_name(session->s_state), seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002734
2735 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2736 session->s_state = CEPH_MDS_SESSION_OPEN;
2737 pr_info("mds%d came back\n", session->s_mds);
2738 }
2739
2740 switch (op) {
2741 case CEPH_SESSION_OPEN:
Sage Weil29790f22010-03-18 14:45:05 -07002742 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2743 pr_info("mds%d reconnect success\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002744 session->s_state = CEPH_MDS_SESSION_OPEN;
2745 renewed_caps(mdsc, session, 0);
2746 wake = 1;
2747 if (mdsc->stopping)
2748 __close_session(mdsc, session);
2749 break;
2750
2751 case CEPH_SESSION_RENEWCAPS:
2752 if (session->s_renew_seq == seq)
2753 renewed_caps(mdsc, session, 1);
2754 break;
2755
2756 case CEPH_SESSION_CLOSE:
Sage Weil29790f22010-03-18 14:45:05 -07002757 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2758 pr_info("mds%d reconnect denied\n", session->s_mds);
Yan, Zheng1c841a92015-03-24 20:15:36 +08002759 cleanup_session_requests(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002760 remove_session_caps(session);
Yan, Zheng656e4382014-09-11 14:25:18 +08002761 wake = 2; /* for good measure */
Sage Weilf3c60c52010-08-11 14:51:23 -07002762 wake_up_all(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002763 break;
2764
2765 case CEPH_SESSION_STALE:
2766 pr_info("mds%d caps went stale, renewing\n",
2767 session->s_mds);
Alex Elderd8fb02a2012-01-12 17:48:10 -08002768 spin_lock(&session->s_gen_ttl_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002769 session->s_cap_gen++;
Alex Elder1ce208a2012-01-12 17:48:11 -08002770 session->s_cap_ttl = jiffies - 1;
Alex Elderd8fb02a2012-01-12 17:48:10 -08002771 spin_unlock(&session->s_gen_ttl_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002772 send_renew_caps(mdsc, session);
2773 break;
2774
2775 case CEPH_SESSION_RECALL_STATE:
2776 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2777 break;
2778
Yan, Zheng186e4f72013-11-22 14:48:37 +08002779 case CEPH_SESSION_FLUSHMSG:
2780 send_flushmsg_ack(mdsc, session, seq);
2781 break;
2782
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08002783 case CEPH_SESSION_FORCE_RO:
2784 dout("force_session_readonly %p\n", session);
2785 spin_lock(&session->s_cap_lock);
2786 session->s_readonly = true;
2787 spin_unlock(&session->s_cap_lock);
2788 wake_up_session_caps(session, 0);
2789 break;
2790
Yan, Zhengfcff4152016-09-14 16:39:51 +08002791 case CEPH_SESSION_REJECT:
2792 WARN_ON(session->s_state != CEPH_MDS_SESSION_OPENING);
2793 pr_info("mds%d rejected session\n", session->s_mds);
2794 session->s_state = CEPH_MDS_SESSION_REJECTED;
2795 cleanup_session_requests(mdsc, session);
2796 remove_session_caps(session);
2797 wake = 2; /* for good measure */
2798 break;
2799
Sage Weil2f2dc052009-10-06 11:31:09 -07002800 default:
2801 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2802 WARN_ON(1);
2803 }
2804
2805 mutex_unlock(&session->s_mutex);
2806 if (wake) {
2807 mutex_lock(&mdsc->mutex);
2808 __wake_requests(mdsc, &session->s_waiting);
Yan, Zheng656e4382014-09-11 14:25:18 +08002809 if (wake == 2)
2810 kick_requests(mdsc, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002811 mutex_unlock(&mdsc->mutex);
2812 }
Yan, Zheng0a07fc82017-03-29 15:30:24 +08002813 if (op == CEPH_SESSION_CLOSE)
2814 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002815 return;
2816
2817bad:
2818 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2819 (int)msg->front.iov_len);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002820 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002821 return;
2822}
2823
2824
2825/*
2826 * called under session->mutex.
2827 */
2828static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2829 struct ceph_mds_session *session)
2830{
2831 struct ceph_mds_request *req, *nreq;
Yan, Zheng3de22be2015-02-04 14:26:22 +08002832 struct rb_node *p;
Sage Weil2f2dc052009-10-06 11:31:09 -07002833 int err;
2834
2835 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2836
2837 mutex_lock(&mdsc->mutex);
2838 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002839 err = __prepare_send_request(mdsc, req, session->s_mds, true);
Sage Weil2f2dc052009-10-06 11:31:09 -07002840 if (!err) {
2841 ceph_msg_get(req->r_request);
2842 ceph_con_send(&session->s_con, req->r_request);
2843 }
2844 }
Yan, Zheng3de22be2015-02-04 14:26:22 +08002845
2846 /*
2847 * also re-send old requests when MDS enters reconnect stage. So that MDS
2848 * can process completed request in clientreplay stage.
2849 */
2850 p = rb_first(&mdsc->request_tree);
2851 while (p) {
2852 req = rb_entry(p, struct ceph_mds_request, r_node);
2853 p = rb_next(p);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002854 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Yan, Zheng3de22be2015-02-04 14:26:22 +08002855 continue;
2856 if (req->r_attempts == 0)
2857 continue; /* only old requests */
2858 if (req->r_session &&
2859 req->r_session->s_mds == session->s_mds) {
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002860 err = __prepare_send_request(mdsc, req,
2861 session->s_mds, true);
Yan, Zheng3de22be2015-02-04 14:26:22 +08002862 if (!err) {
2863 ceph_msg_get(req->r_request);
2864 ceph_con_send(&session->s_con, req->r_request);
2865 }
2866 }
2867 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002868 mutex_unlock(&mdsc->mutex);
2869}
2870
2871/*
2872 * Encode information about a cap for a reconnect with the MDS.
2873 */
Sage Weil2f2dc052009-10-06 11:31:09 -07002874static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2875 void *arg)
2876{
Sage Weil20cb34a2010-05-12 15:21:32 -07002877 union {
2878 struct ceph_mds_cap_reconnect v2;
2879 struct ceph_mds_cap_reconnect_v1 v1;
2880 } rec;
Yan, Zhengb3f8d682017-09-11 10:58:55 +08002881 struct ceph_inode_info *ci = cap->ci;
Sage Weil20cb34a2010-05-12 15:21:32 -07002882 struct ceph_reconnect_state *recon_state = arg;
2883 struct ceph_pagelist *pagelist = recon_state->pagelist;
Sage Weil2f2dc052009-10-06 11:31:09 -07002884 char *path;
2885 int pathlen, err;
2886 u64 pathbase;
Yan, Zheng3469ed02016-07-05 09:32:31 +08002887 u64 snap_follows;
Sage Weil2f2dc052009-10-06 11:31:09 -07002888 struct dentry *dentry;
2889
Sage Weil2f2dc052009-10-06 11:31:09 -07002890 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2891 inode, ceph_vinop(inode), cap, cap->cap_id,
2892 ceph_cap_string(cap->issued));
Sage Weil93cea5b2009-12-23 12:21:51 -08002893 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2894 if (err)
2895 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002896
2897 dentry = d_find_alias(inode);
2898 if (dentry) {
2899 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2900 if (IS_ERR(path)) {
2901 err = PTR_ERR(path);
Sage Weile072f8a2010-08-26 09:26:37 -07002902 goto out_dput;
Sage Weil2f2dc052009-10-06 11:31:09 -07002903 }
2904 } else {
2905 path = NULL;
2906 pathlen = 0;
Ilya Dryomov4eacd4c2016-08-09 16:12:09 +02002907 pathbase = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002908 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002909
Sage Weilbe655592011-11-30 09:47:09 -08002910 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002911 cap->seq = 0; /* reset cap seq */
2912 cap->issue_seq = 0; /* and issue_seq */
Yan, Zheng667ca052013-05-31 16:25:36 +08002913 cap->mseq = 0; /* and migrate_seq */
Yan, Zheng99a9c272013-09-22 11:08:14 +08002914 cap->cap_gen = cap->session->s_cap_gen;
Sage Weil20cb34a2010-05-12 15:21:32 -07002915
Yan, Zheng121f22a2016-07-04 22:05:18 +08002916 if (recon_state->msg_version >= 2) {
Sage Weil20cb34a2010-05-12 15:21:32 -07002917 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
2918 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2919 rec.v2.issued = cpu_to_le32(cap->issued);
2920 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2921 rec.v2.pathbase = cpu_to_le64(pathbase);
Jeff Laytonec1dff22017-10-31 15:51:14 -04002922 rec.v2.flock_len = (__force __le32)
2923 ((ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) ? 0 : 1);
Sage Weil20cb34a2010-05-12 15:21:32 -07002924 } else {
2925 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
2926 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2927 rec.v1.issued = cpu_to_le32(cap->issued);
2928 rec.v1.size = cpu_to_le64(inode->i_size);
2929 ceph_encode_timespec(&rec.v1.mtime, &inode->i_mtime);
2930 ceph_encode_timespec(&rec.v1.atime, &inode->i_atime);
2931 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2932 rec.v1.pathbase = cpu_to_le64(pathbase);
Sage Weil20cb34a2010-05-12 15:21:32 -07002933 }
Yan, Zheng3469ed02016-07-05 09:32:31 +08002934
2935 if (list_empty(&ci->i_cap_snaps)) {
Yan, Zheng92776fd2017-08-16 21:42:39 +08002936 snap_follows = ci->i_head_snapc ? ci->i_head_snapc->seq : 0;
Yan, Zheng3469ed02016-07-05 09:32:31 +08002937 } else {
2938 struct ceph_cap_snap *capsnap =
2939 list_first_entry(&ci->i_cap_snaps,
2940 struct ceph_cap_snap, ci_item);
2941 snap_follows = capsnap->follows;
Sage Weil93cea5b2009-12-23 12:21:51 -08002942 }
Sage Weilbe655592011-11-30 09:47:09 -08002943 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002944
Yan, Zheng121f22a2016-07-04 22:05:18 +08002945 if (recon_state->msg_version >= 2) {
Greg Farnum40819f62010-08-02 15:34:23 -07002946 int num_fcntl_locks, num_flock_locks;
Yan, Zheng4deb14a2017-09-11 10:36:28 +08002947 struct ceph_filelock *flocks = NULL;
Yan, Zheng121f22a2016-07-04 22:05:18 +08002948 size_t struct_len, total_len = 0;
2949 u8 struct_v = 0;
Greg Farnum40819f62010-08-02 15:34:23 -07002950
Jim Schutt39be95e2013-05-15 13:03:35 -05002951encode_again:
Yan, Zhengb3f8d682017-09-11 10:58:55 +08002952 if (rec.v2.flock_len) {
2953 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
2954 } else {
2955 num_fcntl_locks = 0;
2956 num_flock_locks = 0;
2957 }
Yan, Zheng4deb14a2017-09-11 10:36:28 +08002958 if (num_fcntl_locks + num_flock_locks > 0) {
2959 flocks = kmalloc((num_fcntl_locks + num_flock_locks) *
2960 sizeof(struct ceph_filelock), GFP_NOFS);
2961 if (!flocks) {
2962 err = -ENOMEM;
2963 goto out_free;
2964 }
2965 err = ceph_encode_locks_to_buffer(inode, flocks,
2966 num_fcntl_locks,
2967 num_flock_locks);
2968 if (err) {
2969 kfree(flocks);
2970 flocks = NULL;
2971 if (err == -ENOSPC)
2972 goto encode_again;
2973 goto out_free;
2974 }
2975 } else {
Jim Schutt39be95e2013-05-15 13:03:35 -05002976 kfree(flocks);
Yan, Zheng4deb14a2017-09-11 10:36:28 +08002977 flocks = NULL;
Jim Schutt39be95e2013-05-15 13:03:35 -05002978 }
Yan, Zheng121f22a2016-07-04 22:05:18 +08002979
2980 if (recon_state->msg_version >= 3) {
2981 /* version, compat_version and struct_len */
2982 total_len = 2 * sizeof(u8) + sizeof(u32);
Yan, Zheng3469ed02016-07-05 09:32:31 +08002983 struct_v = 2;
Yan, Zheng121f22a2016-07-04 22:05:18 +08002984 }
Jim Schutt39be95e2013-05-15 13:03:35 -05002985 /*
2986 * number of encoded locks is stable, so copy to pagelist
2987 */
Yan, Zheng121f22a2016-07-04 22:05:18 +08002988 struct_len = 2 * sizeof(u32) +
2989 (num_fcntl_locks + num_flock_locks) *
2990 sizeof(struct ceph_filelock);
2991 rec.v2.flock_len = cpu_to_le32(struct_len);
2992
2993 struct_len += sizeof(rec.v2);
2994 struct_len += sizeof(u32) + pathlen;
2995
Yan, Zheng3469ed02016-07-05 09:32:31 +08002996 if (struct_v >= 2)
2997 struct_len += sizeof(u64); /* snap_follows */
2998
Yan, Zheng121f22a2016-07-04 22:05:18 +08002999 total_len += struct_len;
3000 err = ceph_pagelist_reserve(pagelist, total_len);
3001
3002 if (!err) {
3003 if (recon_state->msg_version >= 3) {
3004 ceph_pagelist_encode_8(pagelist, struct_v);
3005 ceph_pagelist_encode_8(pagelist, 1);
3006 ceph_pagelist_encode_32(pagelist, struct_len);
3007 }
3008 ceph_pagelist_encode_string(pagelist, path, pathlen);
3009 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v2));
3010 ceph_locks_to_pagelist(flocks, pagelist,
3011 num_fcntl_locks,
3012 num_flock_locks);
Yan, Zheng3469ed02016-07-05 09:32:31 +08003013 if (struct_v >= 2)
3014 ceph_pagelist_encode_64(pagelist, snap_follows);
Yan, Zheng121f22a2016-07-04 22:05:18 +08003015 }
Jim Schutt39be95e2013-05-15 13:03:35 -05003016 kfree(flocks);
Sage Weil3612abb2010-09-07 15:59:27 -07003017 } else {
Yan, Zheng121f22a2016-07-04 22:05:18 +08003018 size_t size = sizeof(u32) + pathlen + sizeof(rec.v1);
3019 err = ceph_pagelist_reserve(pagelist, size);
3020 if (!err) {
3021 ceph_pagelist_encode_string(pagelist, path, pathlen);
3022 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v1));
3023 }
Greg Farnum40819f62010-08-02 15:34:23 -07003024 }
Yan, Zheng44c99752013-09-22 10:28:10 +08003025
3026 recon_state->nr_caps++;
Sage Weile072f8a2010-08-26 09:26:37 -07003027out_free:
Sage Weil2f2dc052009-10-06 11:31:09 -07003028 kfree(path);
Sage Weile072f8a2010-08-26 09:26:37 -07003029out_dput:
Sage Weil2f2dc052009-10-06 11:31:09 -07003030 dput(dentry);
Sage Weil93cea5b2009-12-23 12:21:51 -08003031 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07003032}
3033
3034
3035/*
3036 * If an MDS fails and recovers, clients need to reconnect in order to
3037 * reestablish shared state. This includes all caps issued through
3038 * this session _and_ the snap_realm hierarchy. Because it's not
3039 * clear which snap realms the mds cares about, we send everything we
3040 * know about.. that ensures we'll then get any new info the
3041 * recovering MDS might have.
3042 *
3043 * This is a relatively heavyweight operation, but it's rare.
3044 *
3045 * called with mdsc->mutex held.
3046 */
Sage Weil34b6c852010-05-10 16:31:25 -07003047static void send_mds_reconnect(struct ceph_mds_client *mdsc,
3048 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07003049{
Sage Weil2f2dc052009-10-06 11:31:09 -07003050 struct ceph_msg *reply;
Sage Weila105f002010-02-15 14:37:55 -08003051 struct rb_node *p;
Sage Weil34b6c852010-05-10 16:31:25 -07003052 int mds = session->s_mds;
Sage Weil9abf82b2010-05-10 21:58:38 -07003053 int err = -ENOMEM;
Yan, Zheng44c99752013-09-22 10:28:10 +08003054 int s_nr_caps;
Sage Weil93cea5b2009-12-23 12:21:51 -08003055 struct ceph_pagelist *pagelist;
Sage Weil20cb34a2010-05-12 15:21:32 -07003056 struct ceph_reconnect_state recon_state;
Jeff Laytonc8a96a32017-10-19 08:53:58 -04003057 LIST_HEAD(dispose);
Sage Weil2f2dc052009-10-06 11:31:09 -07003058
Sage Weil34b6c852010-05-10 16:31:25 -07003059 pr_info("mds%d reconnect start\n", mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003060
Sage Weil93cea5b2009-12-23 12:21:51 -08003061 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
3062 if (!pagelist)
3063 goto fail_nopagelist;
3064 ceph_pagelist_init(pagelist);
3065
Sage Weilb61c2762011-08-09 15:03:46 -07003066 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07003067 if (!reply)
Sage Weil93cea5b2009-12-23 12:21:51 -08003068 goto fail_nomsg;
Sage Weil93cea5b2009-12-23 12:21:51 -08003069
Sage Weil34b6c852010-05-10 16:31:25 -07003070 mutex_lock(&session->s_mutex);
3071 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
3072 session->s_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003073
Sage Weil2f2dc052009-10-06 11:31:09 -07003074 dout("session %p state %s\n", session,
John Spraya687eca2014-09-19 13:51:08 +01003075 ceph_session_state_name(session->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07003076
Yan, Zheng99a9c272013-09-22 11:08:14 +08003077 spin_lock(&session->s_gen_ttl_lock);
3078 session->s_cap_gen++;
3079 spin_unlock(&session->s_gen_ttl_lock);
3080
3081 spin_lock(&session->s_cap_lock);
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08003082 /* don't know if session is readonly */
3083 session->s_readonly = 0;
Yan, Zheng99a9c272013-09-22 11:08:14 +08003084 /*
3085 * notify __ceph_remove_cap() that we are composing cap reconnect.
3086 * If a cap get released before being added to the cap reconnect,
3087 * __ceph_remove_cap() should skip queuing cap release.
3088 */
3089 session->s_cap_reconnect = 1;
Sage Weile01a5942010-05-10 15:36:44 -07003090 /* drop old cap expires; we're about to reestablish that state */
Jeff Laytonc8a96a32017-10-19 08:53:58 -04003091 detach_cap_releases(session, &dispose);
3092 spin_unlock(&session->s_cap_lock);
3093 dispose_cap_releases(mdsc, &dispose);
Sage Weile01a5942010-05-10 15:36:44 -07003094
Yan, Zheng5d233712014-09-10 16:56:23 +08003095 /* trim unused caps to reduce MDS's cache rejoin time */
Yan, Zhengc0bd50e2015-04-07 15:51:08 +08003096 if (mdsc->fsc->sb->s_root)
3097 shrink_dcache_parent(mdsc->fsc->sb->s_root);
Yan, Zheng5d233712014-09-10 16:56:23 +08003098
3099 ceph_con_close(&session->s_con);
3100 ceph_con_open(&session->s_con,
3101 CEPH_ENTITY_TYPE_MDS, mds,
3102 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
3103
3104 /* replay unsafe requests */
3105 replay_unsafe_requests(mdsc, session);
3106
3107 down_read(&mdsc->snap_rwsem);
3108
Sage Weil2f2dc052009-10-06 11:31:09 -07003109 /* traverse this session's caps */
Yan, Zheng44c99752013-09-22 10:28:10 +08003110 s_nr_caps = session->s_nr_caps;
3111 err = ceph_pagelist_encode_32(pagelist, s_nr_caps);
Sage Weil93cea5b2009-12-23 12:21:51 -08003112 if (err)
3113 goto fail;
Sage Weil20cb34a2010-05-12 15:21:32 -07003114
Yan, Zheng44c99752013-09-22 10:28:10 +08003115 recon_state.nr_caps = 0;
Sage Weil20cb34a2010-05-12 15:21:32 -07003116 recon_state.pagelist = pagelist;
Yan, Zheng121f22a2016-07-04 22:05:18 +08003117 if (session->s_con.peer_features & CEPH_FEATURE_MDSENC)
3118 recon_state.msg_version = 3;
3119 else if (session->s_con.peer_features & CEPH_FEATURE_FLOCK)
3120 recon_state.msg_version = 2;
3121 else
3122 recon_state.msg_version = 1;
Sage Weil20cb34a2010-05-12 15:21:32 -07003123 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
Sage Weil2f2dc052009-10-06 11:31:09 -07003124 if (err < 0)
Sage Weil9abf82b2010-05-10 21:58:38 -07003125 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07003126
Yan, Zheng99a9c272013-09-22 11:08:14 +08003127 spin_lock(&session->s_cap_lock);
3128 session->s_cap_reconnect = 0;
3129 spin_unlock(&session->s_cap_lock);
3130
Sage Weil2f2dc052009-10-06 11:31:09 -07003131 /*
3132 * snaprealms. we provide mds with the ino, seq (version), and
3133 * parent for all of our realms. If the mds has any newer info,
3134 * it will tell us.
3135 */
Sage Weila105f002010-02-15 14:37:55 -08003136 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
3137 struct ceph_snap_realm *realm =
3138 rb_entry(p, struct ceph_snap_realm, node);
Sage Weil93cea5b2009-12-23 12:21:51 -08003139 struct ceph_mds_snaprealm_reconnect sr_rec;
Sage Weil2f2dc052009-10-06 11:31:09 -07003140
3141 dout(" adding snap realm %llx seq %lld parent %llx\n",
3142 realm->ino, realm->seq, realm->parent_ino);
Sage Weil93cea5b2009-12-23 12:21:51 -08003143 sr_rec.ino = cpu_to_le64(realm->ino);
3144 sr_rec.seq = cpu_to_le64(realm->seq);
3145 sr_rec.parent = cpu_to_le64(realm->parent_ino);
3146 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
3147 if (err)
3148 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07003149 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003150
Yan, Zheng121f22a2016-07-04 22:05:18 +08003151 reply->hdr.version = cpu_to_le16(recon_state.msg_version);
Yan, Zheng44c99752013-09-22 10:28:10 +08003152
3153 /* raced with cap release? */
3154 if (s_nr_caps != recon_state.nr_caps) {
3155 struct page *page = list_first_entry(&pagelist->head,
3156 struct page, lru);
3157 __le32 *addr = kmap_atomic(page);
3158 *addr = cpu_to_le32(recon_state.nr_caps);
3159 kunmap_atomic(addr);
Alex Elderebf18f42013-03-04 22:29:57 -06003160 }
Yan, Zheng44c99752013-09-22 10:28:10 +08003161
3162 reply->hdr.data_len = cpu_to_le32(pagelist->length);
3163 ceph_msg_data_add_pagelist(reply, pagelist);
Yan, Zhenge548e9b2015-06-10 15:17:56 +08003164
3165 ceph_early_kick_flushing_caps(mdsc, session);
3166
Sage Weil2f2dc052009-10-06 11:31:09 -07003167 ceph_con_send(&session->s_con, reply);
3168
Sage Weil9abf82b2010-05-10 21:58:38 -07003169 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003170
Sage Weil9abf82b2010-05-10 21:58:38 -07003171 mutex_lock(&mdsc->mutex);
3172 __wake_requests(mdsc, &session->s_waiting);
3173 mutex_unlock(&mdsc->mutex);
3174
Sage Weil2f2dc052009-10-06 11:31:09 -07003175 up_read(&mdsc->snap_rwsem);
Sage Weil2f2dc052009-10-06 11:31:09 -07003176 return;
3177
Sage Weil93cea5b2009-12-23 12:21:51 -08003178fail:
Sage Weil2f2dc052009-10-06 11:31:09 -07003179 ceph_msg_put(reply);
Sage Weil9abf82b2010-05-10 21:58:38 -07003180 up_read(&mdsc->snap_rwsem);
3181 mutex_unlock(&session->s_mutex);
Sage Weil93cea5b2009-12-23 12:21:51 -08003182fail_nomsg:
3183 ceph_pagelist_release(pagelist);
Sage Weil93cea5b2009-12-23 12:21:51 -08003184fail_nopagelist:
Sage Weil9abf82b2010-05-10 21:58:38 -07003185 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
Sage Weil9abf82b2010-05-10 21:58:38 -07003186 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07003187}
3188
3189
3190/*
3191 * compare old and new mdsmaps, kicking requests
3192 * and closing out old connections as necessary
3193 *
3194 * called under mdsc->mutex.
3195 */
3196static void check_new_map(struct ceph_mds_client *mdsc,
3197 struct ceph_mdsmap *newmap,
3198 struct ceph_mdsmap *oldmap)
3199{
3200 int i;
3201 int oldstate, newstate;
3202 struct ceph_mds_session *s;
3203
3204 dout("check_new_map new %u old %u\n",
3205 newmap->m_epoch, oldmap->m_epoch);
3206
Yan, Zheng76201b62017-03-28 17:04:13 +08003207 for (i = 0; i < oldmap->m_num_mds && i < mdsc->max_sessions; i++) {
Markus Elfringd37b1d92017-08-20 20:22:02 +02003208 if (!mdsc->sessions[i])
Sage Weil2f2dc052009-10-06 11:31:09 -07003209 continue;
3210 s = mdsc->sessions[i];
3211 oldstate = ceph_mdsmap_get_state(oldmap, i);
3212 newstate = ceph_mdsmap_get_state(newmap, i);
3213
Sage Weil0deb01c2010-06-17 14:19:01 -07003214 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07003215 i, ceph_mds_state_name(oldstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07003216 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
Sage Weil2f2dc052009-10-06 11:31:09 -07003217 ceph_mds_state_name(newstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07003218 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
John Spraya687eca2014-09-19 13:51:08 +01003219 ceph_session_state_name(s->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07003220
Yan, Zheng76201b62017-03-28 17:04:13 +08003221 if (i >= newmap->m_num_mds ||
Yan, Zheng3e8f43a2012-09-20 17:42:25 +08003222 memcmp(ceph_mdsmap_get_addr(oldmap, i),
Sage Weil2f2dc052009-10-06 11:31:09 -07003223 ceph_mdsmap_get_addr(newmap, i),
3224 sizeof(struct ceph_entity_addr))) {
3225 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
3226 /* the session never opened, just close it
3227 * out now */
Yan, Zheng0a07fc82017-03-29 15:30:24 +08003228 get_session(s);
Sage Weil2600d2d2010-02-22 15:12:16 -08003229 __unregister_session(mdsc, s);
Yan, Zheng0a07fc82017-03-29 15:30:24 +08003230 __wake_requests(mdsc, &s->s_waiting);
3231 ceph_put_mds_session(s);
Yan, Zheng28275282017-03-28 17:56:29 +08003232 } else if (i >= newmap->m_num_mds) {
3233 /* force close session for stopped mds */
3234 get_session(s);
3235 __unregister_session(mdsc, s);
3236 __wake_requests(mdsc, &s->s_waiting);
3237 kick_requests(mdsc, i);
3238 mutex_unlock(&mdsc->mutex);
3239
3240 mutex_lock(&s->s_mutex);
3241 cleanup_session_requests(mdsc, s);
3242 remove_session_caps(s);
3243 mutex_unlock(&s->s_mutex);
3244
3245 ceph_put_mds_session(s);
3246
3247 mutex_lock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003248 } else {
3249 /* just close it */
3250 mutex_unlock(&mdsc->mutex);
3251 mutex_lock(&s->s_mutex);
3252 mutex_lock(&mdsc->mutex);
3253 ceph_con_close(&s->s_con);
3254 mutex_unlock(&s->s_mutex);
3255 s->s_state = CEPH_MDS_SESSION_RESTARTING;
3256 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003257 } else if (oldstate == newstate) {
3258 continue; /* nothing new with this mds */
3259 }
3260
3261 /*
3262 * send reconnect?
3263 */
3264 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
Sage Weil34b6c852010-05-10 16:31:25 -07003265 newstate >= CEPH_MDS_STATE_RECONNECT) {
3266 mutex_unlock(&mdsc->mutex);
3267 send_mds_reconnect(mdsc, s);
3268 mutex_lock(&mdsc->mutex);
3269 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003270
3271 /*
Sage Weil29790f22010-03-18 14:45:05 -07003272 * kick request on any mds that has gone active.
Sage Weil2f2dc052009-10-06 11:31:09 -07003273 */
3274 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
3275 newstate >= CEPH_MDS_STATE_ACTIVE) {
Sage Weil29790f22010-03-18 14:45:05 -07003276 if (oldstate != CEPH_MDS_STATE_CREATING &&
3277 oldstate != CEPH_MDS_STATE_STARTING)
3278 pr_info("mds%d recovery completed\n", s->s_mds);
3279 kick_requests(mdsc, i);
Sage Weil2f2dc052009-10-06 11:31:09 -07003280 ceph_kick_flushing_caps(mdsc, s);
Sage Weil0dc25702009-11-20 13:43:45 -08003281 wake_up_session_caps(s, 1);
Sage Weil2f2dc052009-10-06 11:31:09 -07003282 }
3283 }
Sage Weilcb170a22010-06-21 13:38:35 -07003284
Yan, Zheng76201b62017-03-28 17:04:13 +08003285 for (i = 0; i < newmap->m_num_mds && i < mdsc->max_sessions; i++) {
Sage Weilcb170a22010-06-21 13:38:35 -07003286 s = mdsc->sessions[i];
3287 if (!s)
3288 continue;
3289 if (!ceph_mdsmap_is_laggy(newmap, i))
3290 continue;
3291 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3292 s->s_state == CEPH_MDS_SESSION_HUNG ||
3293 s->s_state == CEPH_MDS_SESSION_CLOSING) {
3294 dout(" connecting to export targets of laggy mds%d\n",
3295 i);
3296 __open_export_target_sessions(mdsc, s);
3297 }
3298 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003299}
3300
3301
3302
3303/*
3304 * leases
3305 */
3306
3307/*
3308 * caller must hold session s_mutex, dentry->d_lock
3309 */
3310void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
3311{
3312 struct ceph_dentry_info *di = ceph_dentry(dentry);
3313
3314 ceph_put_mds_session(di->lease_session);
3315 di->lease_session = NULL;
3316}
3317
Sage Weil2600d2d2010-02-22 15:12:16 -08003318static void handle_lease(struct ceph_mds_client *mdsc,
3319 struct ceph_mds_session *session,
3320 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07003321{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003322 struct super_block *sb = mdsc->fsc->sb;
Sage Weil2f2dc052009-10-06 11:31:09 -07003323 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07003324 struct dentry *parent, *dentry;
3325 struct ceph_dentry_info *di;
Sage Weil2600d2d2010-02-22 15:12:16 -08003326 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07003327 struct ceph_mds_lease *h = msg->front.iov_base;
Sage Weil1e5ea232010-06-04 10:05:40 -07003328 u32 seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003329 struct ceph_vino vino;
Sage Weil2f2dc052009-10-06 11:31:09 -07003330 struct qstr dname;
3331 int release = 0;
3332
Sage Weil2f2dc052009-10-06 11:31:09 -07003333 dout("handle_lease from mds%d\n", mds);
3334
3335 /* decode */
3336 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
3337 goto bad;
3338 vino.ino = le64_to_cpu(h->ino);
3339 vino.snap = CEPH_NOSNAP;
Sage Weil1e5ea232010-06-04 10:05:40 -07003340 seq = le32_to_cpu(h->seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003341 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
3342 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
3343 if (dname.len != get_unaligned_le32(h+1))
3344 goto bad;
3345
Sage Weil2f2dc052009-10-06 11:31:09 -07003346 /* lookup inode */
3347 inode = ceph_find_inode(sb, vino);
Sage Weil2f90b852011-07-26 11:28:25 -07003348 dout("handle_lease %s, ino %llx %p %.*s\n",
3349 ceph_lease_op_name(h->action), vino.ino, inode,
Sage Weil1e5ea232010-06-04 10:05:40 -07003350 dname.len, dname.name);
Yan, Zheng6cd3bca2014-09-17 07:45:12 +08003351
3352 mutex_lock(&session->s_mutex);
3353 session->s_seq++;
3354
Markus Elfringd37b1d92017-08-20 20:22:02 +02003355 if (!inode) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003356 dout("handle_lease no inode %llx\n", vino.ino);
3357 goto release;
3358 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003359
3360 /* dentry */
3361 parent = d_find_alias(inode);
3362 if (!parent) {
3363 dout("no parent dentry on inode %p\n", inode);
3364 WARN_ON(1);
3365 goto release; /* hrm... */
3366 }
Linus Torvalds8387ff22016-06-10 07:51:30 -07003367 dname.hash = full_name_hash(parent, dname.name, dname.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07003368 dentry = d_lookup(parent, &dname);
3369 dput(parent);
3370 if (!dentry)
3371 goto release;
3372
3373 spin_lock(&dentry->d_lock);
3374 di = ceph_dentry(dentry);
3375 switch (h->action) {
3376 case CEPH_MDS_LEASE_REVOKE:
Sage Weil3d8eb7a2011-11-11 09:48:53 -08003377 if (di->lease_session == session) {
Sage Weil1e5ea232010-06-04 10:05:40 -07003378 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
3379 h->seq = cpu_to_le32(di->lease_seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003380 __ceph_mdsc_drop_dentry_lease(dentry);
3381 }
3382 release = 1;
3383 break;
3384
3385 case CEPH_MDS_LEASE_RENEW:
Sage Weil3d8eb7a2011-11-11 09:48:53 -08003386 if (di->lease_session == session &&
Sage Weil2f2dc052009-10-06 11:31:09 -07003387 di->lease_gen == session->s_cap_gen &&
3388 di->lease_renew_from &&
3389 di->lease_renew_after == 0) {
3390 unsigned long duration =
Nicholas Mc Guire3563dbdd2015-02-06 06:52:17 -05003391 msecs_to_jiffies(le32_to_cpu(h->duration_ms));
Sage Weil2f2dc052009-10-06 11:31:09 -07003392
Sage Weil1e5ea232010-06-04 10:05:40 -07003393 di->lease_seq = seq;
Miklos Szeredi9b16f03c2016-06-22 16:35:04 +02003394 di->time = di->lease_renew_from + duration;
Sage Weil2f2dc052009-10-06 11:31:09 -07003395 di->lease_renew_after = di->lease_renew_from +
3396 (duration >> 1);
3397 di->lease_renew_from = 0;
3398 }
3399 break;
3400 }
3401 spin_unlock(&dentry->d_lock);
3402 dput(dentry);
3403
3404 if (!release)
3405 goto out;
3406
3407release:
3408 /* let's just reuse the same message */
3409 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
3410 ceph_msg_get(msg);
3411 ceph_con_send(&session->s_con, msg);
3412
3413out:
3414 iput(inode);
3415 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003416 return;
3417
3418bad:
3419 pr_err("corrupt lease message\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08003420 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003421}
3422
3423void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
3424 struct inode *inode,
3425 struct dentry *dentry, char action,
3426 u32 seq)
3427{
3428 struct ceph_msg *msg;
3429 struct ceph_mds_lease *lease;
3430 int len = sizeof(*lease) + sizeof(u32);
3431 int dnamelen = 0;
3432
3433 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
3434 inode, dentry, ceph_lease_op_name(action), session->s_mds);
3435 dnamelen = dentry->d_name.len;
3436 len += dnamelen;
3437
Sage Weilb61c2762011-08-09 15:03:46 -07003438 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07003439 if (!msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07003440 return;
3441 lease = msg->front.iov_base;
3442 lease->action = action;
Sage Weil2f2dc052009-10-06 11:31:09 -07003443 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
3444 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
3445 lease->seq = cpu_to_le32(seq);
3446 put_unaligned_le32(dnamelen, lease + 1);
3447 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
3448
3449 /*
3450 * if this is a preemptive lease RELEASE, no need to
3451 * flush request stream, since the actual request will
3452 * soon follow.
3453 */
3454 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
3455
3456 ceph_con_send(&session->s_con, msg);
3457}
3458
3459/*
Sage Weil2f2dc052009-10-06 11:31:09 -07003460 * drop all leases (and dentry refs) in preparation for umount
3461 */
3462static void drop_leases(struct ceph_mds_client *mdsc)
3463{
3464 int i;
3465
3466 dout("drop_leases\n");
3467 mutex_lock(&mdsc->mutex);
3468 for (i = 0; i < mdsc->max_sessions; i++) {
3469 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
3470 if (!s)
3471 continue;
3472 mutex_unlock(&mdsc->mutex);
3473 mutex_lock(&s->s_mutex);
3474 mutex_unlock(&s->s_mutex);
3475 ceph_put_mds_session(s);
3476 mutex_lock(&mdsc->mutex);
3477 }
3478 mutex_unlock(&mdsc->mutex);
3479}
3480
3481
3482
3483/*
3484 * delayed work -- periodically trim expired leases, renew caps with mds
3485 */
3486static void schedule_delayed(struct ceph_mds_client *mdsc)
3487{
3488 int delay = 5;
3489 unsigned hz = round_jiffies_relative(HZ * delay);
3490 schedule_delayed_work(&mdsc->delayed_work, hz);
3491}
3492
3493static void delayed_work(struct work_struct *work)
3494{
3495 int i;
3496 struct ceph_mds_client *mdsc =
3497 container_of(work, struct ceph_mds_client, delayed_work.work);
3498 int renew_interval;
3499 int renew_caps;
3500
3501 dout("mdsc delayed_work\n");
Sage Weilafcdaea2009-10-14 14:27:38 -07003502 ceph_check_delayed_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003503
3504 mutex_lock(&mdsc->mutex);
3505 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
3506 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
3507 mdsc->last_renew_caps);
3508 if (renew_caps)
3509 mdsc->last_renew_caps = jiffies;
3510
3511 for (i = 0; i < mdsc->max_sessions; i++) {
3512 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
Markus Elfringd37b1d92017-08-20 20:22:02 +02003513 if (!s)
Sage Weil2f2dc052009-10-06 11:31:09 -07003514 continue;
3515 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
3516 dout("resending session close request for mds%d\n",
3517 s->s_mds);
3518 request_close_session(mdsc, s);
3519 ceph_put_mds_session(s);
3520 continue;
3521 }
3522 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
3523 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
3524 s->s_state = CEPH_MDS_SESSION_HUNG;
3525 pr_info("mds%d hung\n", s->s_mds);
3526 }
3527 }
3528 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
3529 /* this mds is failed or recovering, just wait */
3530 ceph_put_mds_session(s);
3531 continue;
3532 }
3533 mutex_unlock(&mdsc->mutex);
3534
3535 mutex_lock(&s->s_mutex);
3536 if (renew_caps)
3537 send_renew_caps(mdsc, s);
3538 else
3539 ceph_con_keepalive(&s->s_con);
Sage Weilaab53dd2010-03-17 16:30:21 -07003540 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3541 s->s_state == CEPH_MDS_SESSION_HUNG)
Sage Weil3d7ded42010-06-09 16:47:10 -07003542 ceph_send_cap_releases(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07003543 mutex_unlock(&s->s_mutex);
3544 ceph_put_mds_session(s);
3545
3546 mutex_lock(&mdsc->mutex);
3547 }
3548 mutex_unlock(&mdsc->mutex);
3549
3550 schedule_delayed(mdsc);
3551}
3552
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003553int ceph_mdsc_init(struct ceph_fs_client *fsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07003554
Sage Weil2f2dc052009-10-06 11:31:09 -07003555{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003556 struct ceph_mds_client *mdsc;
3557
3558 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
3559 if (!mdsc)
3560 return -ENOMEM;
3561 mdsc->fsc = fsc;
3562 fsc->mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07003563 mutex_init(&mdsc->mutex);
3564 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
Markus Elfringd37b1d92017-08-20 20:22:02 +02003565 if (!mdsc->mdsmap) {
majianpengfb3101b2013-06-25 14:48:19 +08003566 kfree(mdsc);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08003567 return -ENOMEM;
majianpengfb3101b2013-06-25 14:48:19 +08003568 }
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08003569
Sage Weil2f2dc052009-10-06 11:31:09 -07003570 init_completion(&mdsc->safe_umount_waiters);
Sage Weilf3c60c52010-08-11 14:51:23 -07003571 init_waitqueue_head(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003572 INIT_LIST_HEAD(&mdsc->waiting_for_map);
3573 mdsc->sessions = NULL;
Yan, Zheng86d8f672015-01-09 17:00:42 +08003574 atomic_set(&mdsc->num_sessions, 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07003575 mdsc->max_sessions = 0;
3576 mdsc->stopping = 0;
Yan, Zhengaffbc192015-05-05 21:22:13 +08003577 mdsc->last_snap_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003578 init_rwsem(&mdsc->snap_rwsem);
Sage Weila105f002010-02-15 14:37:55 -08003579 mdsc->snap_realms = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07003580 INIT_LIST_HEAD(&mdsc->snap_empty);
3581 spin_lock_init(&mdsc->snap_empty_lock);
3582 mdsc->last_tid = 0;
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08003583 mdsc->oldest_tid = 0;
Sage Weil44ca18f2010-02-15 12:08:46 -08003584 mdsc->request_tree = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07003585 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
3586 mdsc->last_renew_caps = jiffies;
3587 INIT_LIST_HEAD(&mdsc->cap_delay_list);
3588 spin_lock_init(&mdsc->cap_delay_lock);
3589 INIT_LIST_HEAD(&mdsc->snap_flush_list);
3590 spin_lock_init(&mdsc->snap_flush_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08003591 mdsc->last_cap_flush_tid = 1;
Yan, Zhenge4500b52016-07-06 11:12:56 +08003592 INIT_LIST_HEAD(&mdsc->cap_flush_list);
Sage Weil2f2dc052009-10-06 11:31:09 -07003593 INIT_LIST_HEAD(&mdsc->cap_dirty);
Sage Weildb354052011-05-24 11:46:31 -07003594 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
Sage Weil2f2dc052009-10-06 11:31:09 -07003595 mdsc->num_cap_flushing = 0;
3596 spin_lock_init(&mdsc->cap_dirty_lock);
3597 init_waitqueue_head(&mdsc->cap_flushing_wq);
3598 spin_lock_init(&mdsc->dentry_lru_lock);
3599 INIT_LIST_HEAD(&mdsc->dentry_lru);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08003600
Yehuda Sadeh37151662010-06-17 16:16:12 -07003601 ceph_caps_init(mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003602 ceph_adjust_min_caps(mdsc, fsc->min_caps);
Yehuda Sadeh37151662010-06-17 16:16:12 -07003603
Yan, Zheng10183a62015-04-27 15:33:28 +08003604 init_rwsem(&mdsc->pool_perm_rwsem);
3605 mdsc->pool_perm_tree = RB_ROOT;
3606
Yan, Zheng717e6f22017-09-11 12:10:08 +08003607 strncpy(mdsc->nodename, utsname()->nodename,
3608 sizeof(mdsc->nodename) - 1);
Sage Weil5f44f142009-11-18 14:52:18 -08003609 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003610}
3611
3612/*
3613 * Wait for safe replies on open mds requests. If we time out, drop
3614 * all requests from the tree to avoid dangling dentry refs.
3615 */
3616static void wait_requests(struct ceph_mds_client *mdsc)
3617{
Ilya Dryomova319bf52015-05-15 12:02:17 +03003618 struct ceph_options *opts = mdsc->fsc->client->options;
Sage Weil2f2dc052009-10-06 11:31:09 -07003619 struct ceph_mds_request *req;
Sage Weil2f2dc052009-10-06 11:31:09 -07003620
3621 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003622 if (__get_oldest_req(mdsc)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003623 mutex_unlock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003624
Sage Weil2f2dc052009-10-06 11:31:09 -07003625 dout("wait_requests waiting for requests\n");
3626 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
Ilya Dryomova319bf52015-05-15 12:02:17 +03003627 ceph_timeout_jiffies(opts->mount_timeout));
Sage Weil2f2dc052009-10-06 11:31:09 -07003628
3629 /* tear down remaining requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08003630 mutex_lock(&mdsc->mutex);
3631 while ((req = __get_oldest_req(mdsc))) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003632 dout("wait_requests timed out on tid %llu\n",
3633 req->r_tid);
Sage Weil44ca18f2010-02-15 12:08:46 -08003634 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07003635 }
3636 }
3637 mutex_unlock(&mdsc->mutex);
3638 dout("wait_requests done\n");
3639}
3640
3641/*
3642 * called before mount is ro, and before dentries are torn down.
3643 * (hmm, does this still race with new lookups?)
3644 */
3645void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
3646{
3647 dout("pre_umount\n");
3648 mdsc->stopping = 1;
3649
3650 drop_leases(mdsc);
Sage Weilafcdaea2009-10-14 14:27:38 -07003651 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003652 wait_requests(mdsc);
Sage Weil17c688c2010-06-21 16:12:26 -07003653
3654 /*
3655 * wait for reply handlers to drop their request refs and
3656 * their inode/dcache refs
3657 */
3658 ceph_msgr_flush();
Sage Weil2f2dc052009-10-06 11:31:09 -07003659}
3660
3661/*
3662 * wait for all write mds requests to flush.
3663 */
3664static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
3665{
Sage Weil80fc7312010-03-16 15:28:54 -07003666 struct ceph_mds_request *req = NULL, *nextreq;
Sage Weil44ca18f2010-02-15 12:08:46 -08003667 struct rb_node *n;
Sage Weil2f2dc052009-10-06 11:31:09 -07003668
3669 mutex_lock(&mdsc->mutex);
3670 dout("wait_unsafe_requests want %lld\n", want_tid);
Sage Weil80fc7312010-03-16 15:28:54 -07003671restart:
Sage Weil44ca18f2010-02-15 12:08:46 -08003672 req = __get_oldest_req(mdsc);
3673 while (req && req->r_tid <= want_tid) {
Sage Weil80fc7312010-03-16 15:28:54 -07003674 /* find next request */
3675 n = rb_next(&req->r_node);
3676 if (n)
3677 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
3678 else
3679 nextreq = NULL;
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08003680 if (req->r_op != CEPH_MDS_OP_SETFILELOCK &&
3681 (req->r_op & CEPH_MDS_OP_WRITE)) {
Sage Weil44ca18f2010-02-15 12:08:46 -08003682 /* write op */
3683 ceph_mdsc_get_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07003684 if (nextreq)
3685 ceph_mdsc_get_request(nextreq);
Sage Weil44ca18f2010-02-15 12:08:46 -08003686 mutex_unlock(&mdsc->mutex);
3687 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3688 req->r_tid, want_tid);
3689 wait_for_completion(&req->r_safe_completion);
3690 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003691 ceph_mdsc_put_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07003692 if (!nextreq)
3693 break; /* next dne before, so we're done! */
3694 if (RB_EMPTY_NODE(&nextreq->r_node)) {
3695 /* next request was removed from tree */
3696 ceph_mdsc_put_request(nextreq);
3697 goto restart;
3698 }
3699 ceph_mdsc_put_request(nextreq); /* won't go away */
Sage Weil44ca18f2010-02-15 12:08:46 -08003700 }
Sage Weil80fc7312010-03-16 15:28:54 -07003701 req = nextreq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003702 }
3703 mutex_unlock(&mdsc->mutex);
3704 dout("wait_unsafe_requests done\n");
3705}
3706
3707void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
3708{
Yan, Zheng0e294382016-07-04 18:06:41 +08003709 u64 want_tid, want_flush;
Sage Weil2f2dc052009-10-06 11:31:09 -07003710
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01003711 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
Sage Weil56b7cf92010-05-03 15:22:00 -07003712 return;
3713
Sage Weil2f2dc052009-10-06 11:31:09 -07003714 dout("sync\n");
3715 mutex_lock(&mdsc->mutex);
3716 want_tid = mdsc->last_tid;
Sage Weil2f2dc052009-10-06 11:31:09 -07003717 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003718
Sage Weilafcdaea2009-10-14 14:27:38 -07003719 ceph_flush_dirty_caps(mdsc);
Yan, Zhengd3383a82015-01-08 21:30:12 +08003720 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zheng8310b082015-06-09 17:20:12 +08003721 want_flush = mdsc->last_cap_flush_tid;
Yan, Zhengc8799fc2016-07-07 15:22:38 +08003722 if (!list_empty(&mdsc->cap_flush_list)) {
3723 struct ceph_cap_flush *cf =
3724 list_last_entry(&mdsc->cap_flush_list,
3725 struct ceph_cap_flush, g_list);
3726 cf->wake = true;
3727 }
Yan, Zhengd3383a82015-01-08 21:30:12 +08003728 spin_unlock(&mdsc->cap_dirty_lock);
3729
Yan, Zheng0e294382016-07-04 18:06:41 +08003730 dout("sync want tid %lld flush_seq %lld\n",
3731 want_tid, want_flush);
Sage Weil2f2dc052009-10-06 11:31:09 -07003732
3733 wait_unsafe_requests(mdsc, want_tid);
Yan, Zheng0e294382016-07-04 18:06:41 +08003734 wait_caps_flush(mdsc, want_flush);
Sage Weil2f2dc052009-10-06 11:31:09 -07003735}
3736
Sage Weilf3c60c52010-08-11 14:51:23 -07003737/*
3738 * true if all sessions are closed, or we force unmount
3739 */
Yan, Zhengfcff4152016-09-14 16:39:51 +08003740static bool done_closing_sessions(struct ceph_mds_client *mdsc, int skipped)
Sage Weilf3c60c52010-08-11 14:51:23 -07003741{
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01003742 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
Sage Weilf3c60c52010-08-11 14:51:23 -07003743 return true;
Yan, Zhengfcff4152016-09-14 16:39:51 +08003744 return atomic_read(&mdsc->num_sessions) <= skipped;
Sage Weilf3c60c52010-08-11 14:51:23 -07003745}
Sage Weil2f2dc052009-10-06 11:31:09 -07003746
3747/*
3748 * called after sb is ro.
3749 */
3750void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
3751{
Ilya Dryomova319bf52015-05-15 12:02:17 +03003752 struct ceph_options *opts = mdsc->fsc->client->options;
Sage Weil2f2dc052009-10-06 11:31:09 -07003753 struct ceph_mds_session *session;
3754 int i;
Yan, Zhengfcff4152016-09-14 16:39:51 +08003755 int skipped = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003756
3757 dout("close_sessions\n");
3758
Sage Weil2f2dc052009-10-06 11:31:09 -07003759 /* close sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07003760 mutex_lock(&mdsc->mutex);
3761 for (i = 0; i < mdsc->max_sessions; i++) {
3762 session = __ceph_lookup_mds_session(mdsc, i);
3763 if (!session)
3764 continue;
Sage Weil2f2dc052009-10-06 11:31:09 -07003765 mutex_unlock(&mdsc->mutex);
Sage Weilf3c60c52010-08-11 14:51:23 -07003766 mutex_lock(&session->s_mutex);
Yan, Zhengfcff4152016-09-14 16:39:51 +08003767 if (__close_session(mdsc, session) <= 0)
3768 skipped++;
Sage Weilf3c60c52010-08-11 14:51:23 -07003769 mutex_unlock(&session->s_mutex);
3770 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003771 mutex_lock(&mdsc->mutex);
3772 }
Sage Weilf3c60c52010-08-11 14:51:23 -07003773 mutex_unlock(&mdsc->mutex);
3774
3775 dout("waiting for sessions to close\n");
Yan, Zhengfcff4152016-09-14 16:39:51 +08003776 wait_event_timeout(mdsc->session_close_wq,
3777 done_closing_sessions(mdsc, skipped),
Ilya Dryomova319bf52015-05-15 12:02:17 +03003778 ceph_timeout_jiffies(opts->mount_timeout));
Sage Weil2f2dc052009-10-06 11:31:09 -07003779
3780 /* tear down remaining sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07003781 mutex_lock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003782 for (i = 0; i < mdsc->max_sessions; i++) {
3783 if (mdsc->sessions[i]) {
3784 session = get_session(mdsc->sessions[i]);
Sage Weil2600d2d2010-02-22 15:12:16 -08003785 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003786 mutex_unlock(&mdsc->mutex);
3787 mutex_lock(&session->s_mutex);
3788 remove_session_caps(session);
3789 mutex_unlock(&session->s_mutex);
3790 ceph_put_mds_session(session);
3791 mutex_lock(&mdsc->mutex);
3792 }
3793 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003794 WARN_ON(!list_empty(&mdsc->cap_delay_list));
Sage Weil2f2dc052009-10-06 11:31:09 -07003795 mutex_unlock(&mdsc->mutex);
3796
3797 ceph_cleanup_empty_realms(mdsc);
3798
3799 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3800
3801 dout("stopped\n");
3802}
3803
Yan, Zheng48fec5d2015-07-01 16:27:46 +08003804void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc)
3805{
3806 struct ceph_mds_session *session;
3807 int mds;
3808
3809 dout("force umount\n");
3810
3811 mutex_lock(&mdsc->mutex);
3812 for (mds = 0; mds < mdsc->max_sessions; mds++) {
3813 session = __ceph_lookup_mds_session(mdsc, mds);
3814 if (!session)
3815 continue;
3816 mutex_unlock(&mdsc->mutex);
3817 mutex_lock(&session->s_mutex);
3818 __close_session(mdsc, session);
3819 if (session->s_state == CEPH_MDS_SESSION_CLOSING) {
3820 cleanup_session_requests(mdsc, session);
3821 remove_session_caps(session);
3822 }
3823 mutex_unlock(&session->s_mutex);
3824 ceph_put_mds_session(session);
3825 mutex_lock(&mdsc->mutex);
3826 kick_requests(mdsc, mds);
3827 }
3828 __wake_requests(mdsc, &mdsc->waiting_for_map);
3829 mutex_unlock(&mdsc->mutex);
3830}
3831
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003832static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07003833{
3834 dout("stop\n");
3835 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3836 if (mdsc->mdsmap)
3837 ceph_mdsmap_destroy(mdsc->mdsmap);
3838 kfree(mdsc->sessions);
Yehuda Sadeh37151662010-06-17 16:16:12 -07003839 ceph_caps_finalize(mdsc);
Yan, Zheng10183a62015-04-27 15:33:28 +08003840 ceph_pool_perm_destroy(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003841}
3842
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003843void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
3844{
3845 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weilef550f62011-03-25 13:27:48 -07003846 dout("mdsc_destroy %p\n", mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07003847
3848 /* flush out any connection work with references to us */
3849 ceph_msgr_flush();
3850
Yan, Zheng62a65f32017-06-22 16:26:34 +08003851 ceph_mdsc_stop(mdsc);
3852
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003853 fsc->mdsc = NULL;
3854 kfree(mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07003855 dout("mdsc_destroy %p done\n", mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003856}
3857
Yan, Zheng430afba2016-07-08 11:25:38 +08003858void ceph_mdsc_handle_fsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3859{
3860 struct ceph_fs_client *fsc = mdsc->fsc;
3861 const char *mds_namespace = fsc->mount_options->mds_namespace;
3862 void *p = msg->front.iov_base;
3863 void *end = p + msg->front.iov_len;
3864 u32 epoch;
3865 u32 map_len;
3866 u32 num_fs;
3867 u32 mount_fscid = (u32)-1;
3868 u8 struct_v, struct_cv;
3869 int err = -EINVAL;
3870
3871 ceph_decode_need(&p, end, sizeof(u32), bad);
3872 epoch = ceph_decode_32(&p);
3873
3874 dout("handle_fsmap epoch %u\n", epoch);
3875
3876 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
3877 struct_v = ceph_decode_8(&p);
3878 struct_cv = ceph_decode_8(&p);
3879 map_len = ceph_decode_32(&p);
3880
3881 ceph_decode_need(&p, end, sizeof(u32) * 3, bad);
3882 p += sizeof(u32) * 2; /* skip epoch and legacy_client_fscid */
3883
3884 num_fs = ceph_decode_32(&p);
3885 while (num_fs-- > 0) {
3886 void *info_p, *info_end;
3887 u32 info_len;
3888 u8 info_v, info_cv;
3889 u32 fscid, namelen;
3890
3891 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
3892 info_v = ceph_decode_8(&p);
3893 info_cv = ceph_decode_8(&p);
3894 info_len = ceph_decode_32(&p);
3895 ceph_decode_need(&p, end, info_len, bad);
3896 info_p = p;
3897 info_end = p + info_len;
3898 p = info_end;
3899
3900 ceph_decode_need(&info_p, info_end, sizeof(u32) * 2, bad);
3901 fscid = ceph_decode_32(&info_p);
3902 namelen = ceph_decode_32(&info_p);
3903 ceph_decode_need(&info_p, info_end, namelen, bad);
3904
3905 if (mds_namespace &&
3906 strlen(mds_namespace) == namelen &&
3907 !strncmp(mds_namespace, (char *)info_p, namelen)) {
3908 mount_fscid = fscid;
3909 break;
3910 }
3911 }
3912
3913 ceph_monc_got_map(&fsc->client->monc, CEPH_SUB_FSMAP, epoch);
3914 if (mount_fscid != (u32)-1) {
3915 fsc->client->monc.fs_cluster_id = mount_fscid;
3916 ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP,
3917 0, true);
3918 ceph_monc_renew_subs(&fsc->client->monc);
3919 } else {
3920 err = -ENOENT;
3921 goto err_out;
3922 }
3923 return;
Ilya Dryomov76bd6ec2017-10-16 10:32:50 +02003924
Yan, Zheng430afba2016-07-08 11:25:38 +08003925bad:
3926 pr_err("error decoding fsmap\n");
3927err_out:
3928 mutex_lock(&mdsc->mutex);
Ilya Dryomov76bd6ec2017-10-16 10:32:50 +02003929 mdsc->mdsmap_err = err;
Yan, Zheng430afba2016-07-08 11:25:38 +08003930 __wake_requests(mdsc, &mdsc->waiting_for_map);
3931 mutex_unlock(&mdsc->mutex);
Yan, Zheng430afba2016-07-08 11:25:38 +08003932}
Sage Weil2f2dc052009-10-06 11:31:09 -07003933
3934/*
3935 * handle mds map update.
3936 */
Yan, Zheng430afba2016-07-08 11:25:38 +08003937void ceph_mdsc_handle_mdsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07003938{
3939 u32 epoch;
3940 u32 maplen;
3941 void *p = msg->front.iov_base;
3942 void *end = p + msg->front.iov_len;
3943 struct ceph_mdsmap *newmap, *oldmap;
3944 struct ceph_fsid fsid;
3945 int err = -EINVAL;
3946
3947 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
3948 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003949 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
Sage Weil07433042009-11-18 16:50:41 -08003950 return;
Sage Weilc89136e2009-10-14 09:59:09 -07003951 epoch = ceph_decode_32(&p);
3952 maplen = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07003953 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
3954
3955 /* do we need it? */
Sage Weil2f2dc052009-10-06 11:31:09 -07003956 mutex_lock(&mdsc->mutex);
3957 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
3958 dout("handle_map epoch %u <= our %u\n",
3959 epoch, mdsc->mdsmap->m_epoch);
3960 mutex_unlock(&mdsc->mutex);
3961 return;
3962 }
3963
3964 newmap = ceph_mdsmap_decode(&p, end);
3965 if (IS_ERR(newmap)) {
3966 err = PTR_ERR(newmap);
3967 goto bad_unlock;
3968 }
3969
3970 /* swap into place */
3971 if (mdsc->mdsmap) {
3972 oldmap = mdsc->mdsmap;
3973 mdsc->mdsmap = newmap;
3974 check_new_map(mdsc, newmap, oldmap);
3975 ceph_mdsmap_destroy(oldmap);
3976 } else {
3977 mdsc->mdsmap = newmap; /* first mds map */
3978 }
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003979 mdsc->fsc->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
Sage Weil2f2dc052009-10-06 11:31:09 -07003980
3981 __wake_requests(mdsc, &mdsc->waiting_for_map);
Ilya Dryomov82dcaba2016-01-19 16:19:06 +01003982 ceph_monc_got_map(&mdsc->fsc->client->monc, CEPH_SUB_MDSMAP,
3983 mdsc->mdsmap->m_epoch);
Sage Weil2f2dc052009-10-06 11:31:09 -07003984
3985 mutex_unlock(&mdsc->mutex);
3986 schedule_delayed(mdsc);
3987 return;
3988
3989bad_unlock:
3990 mutex_unlock(&mdsc->mutex);
3991bad:
3992 pr_err("error decoding mdsmap %d\n", err);
3993 return;
3994}
3995
3996static struct ceph_connection *con_get(struct ceph_connection *con)
3997{
3998 struct ceph_mds_session *s = con->private;
3999
4000 if (get_session(s)) {
Elena Reshetova3997c012017-03-03 11:15:06 +02004001 dout("mdsc con_get %p ok (%d)\n", s, refcount_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -07004002 return con;
4003 }
4004 dout("mdsc con_get %p FAIL\n", s);
4005 return NULL;
4006}
4007
4008static void con_put(struct ceph_connection *con)
4009{
4010 struct ceph_mds_session *s = con->private;
4011
Elena Reshetova3997c012017-03-03 11:15:06 +02004012 dout("mdsc con_put %p (%d)\n", s, refcount_read(&s->s_ref) - 1);
Sage Weil2f2dc052009-10-06 11:31:09 -07004013 ceph_put_mds_session(s);
4014}
4015
4016/*
4017 * if the client is unresponsive for long enough, the mds will kill
4018 * the session entirely.
4019 */
4020static void peer_reset(struct ceph_connection *con)
4021{
4022 struct ceph_mds_session *s = con->private;
Sage Weil7e70f0e2010-03-18 13:59:12 -07004023 struct ceph_mds_client *mdsc = s->s_mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07004024
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07004025 pr_warn("mds%d closed our session\n", s->s_mds);
Sage Weil7e70f0e2010-03-18 13:59:12 -07004026 send_mds_reconnect(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07004027}
4028
4029static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
4030{
4031 struct ceph_mds_session *s = con->private;
4032 struct ceph_mds_client *mdsc = s->s_mdsc;
4033 int type = le16_to_cpu(msg->hdr.type);
4034
Sage Weil2600d2d2010-02-22 15:12:16 -08004035 mutex_lock(&mdsc->mutex);
4036 if (__verify_registered_session(mdsc, s) < 0) {
4037 mutex_unlock(&mdsc->mutex);
4038 goto out;
4039 }
4040 mutex_unlock(&mdsc->mutex);
4041
Sage Weil2f2dc052009-10-06 11:31:09 -07004042 switch (type) {
4043 case CEPH_MSG_MDS_MAP:
Yan, Zheng430afba2016-07-08 11:25:38 +08004044 ceph_mdsc_handle_mdsmap(mdsc, msg);
4045 break;
4046 case CEPH_MSG_FS_MAP_USER:
4047 ceph_mdsc_handle_fsmap(mdsc, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004048 break;
4049 case CEPH_MSG_CLIENT_SESSION:
4050 handle_session(s, msg);
4051 break;
4052 case CEPH_MSG_CLIENT_REPLY:
4053 handle_reply(s, msg);
4054 break;
4055 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
Sage Weil2600d2d2010-02-22 15:12:16 -08004056 handle_forward(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004057 break;
4058 case CEPH_MSG_CLIENT_CAPS:
4059 ceph_handle_caps(s, msg);
4060 break;
4061 case CEPH_MSG_CLIENT_SNAP:
Sage Weil2600d2d2010-02-22 15:12:16 -08004062 ceph_handle_snap(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004063 break;
4064 case CEPH_MSG_CLIENT_LEASE:
Sage Weil2600d2d2010-02-22 15:12:16 -08004065 handle_lease(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004066 break;
4067
4068 default:
4069 pr_err("received unknown message type %d %s\n", type,
4070 ceph_msg_type_name(type));
4071 }
Sage Weil2600d2d2010-02-22 15:12:16 -08004072out:
Sage Weil2f2dc052009-10-06 11:31:09 -07004073 ceph_msg_put(msg);
4074}
4075
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004076/*
4077 * authentication
4078 */
Alex Eldera3530df2012-05-16 15:16:39 -05004079
4080/*
4081 * Note: returned pointer is the address of a structure that's
4082 * managed separately. Caller must *not* attempt to free it.
4083 */
4084static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05004085 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004086{
4087 struct ceph_mds_session *s = con->private;
4088 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004089 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05004090 struct ceph_auth_handshake *auth = &s->s_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004091
Alex Elder74f18692012-05-16 15:16:39 -05004092 if (force_new && auth->authorizer) {
Ilya Dryomov6c1ea262016-04-11 19:34:49 +02004093 ceph_auth_destroy_authorizer(auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05004094 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004095 }
Sage Weil27859f92013-03-25 10:26:14 -07004096 if (!auth->authorizer) {
4097 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4098 auth);
Sage Weil0bed9b52013-03-25 10:26:01 -07004099 if (ret)
4100 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07004101 } else {
4102 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4103 auth);
Alex Eldera2556512012-05-16 15:16:39 -05004104 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05004105 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004106 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004107 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05004108
Alex Eldera3530df2012-05-16 15:16:39 -05004109 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004110}
4111
4112
Ilya Dryomov0dde5842016-12-02 16:35:09 +01004113static int verify_authorizer_reply(struct ceph_connection *con)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004114{
4115 struct ceph_mds_session *s = con->private;
4116 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004117 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004118
Ilya Dryomov0dde5842016-12-02 16:35:09 +01004119 return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004120}
4121
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004122static int invalidate_authorizer(struct ceph_connection *con)
4123{
4124 struct ceph_mds_session *s = con->private;
4125 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004126 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004127
Sage Weil27859f92013-03-25 10:26:14 -07004128 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004129
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004130 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004131}
4132
Alex Elder53ded492013-03-01 18:00:14 -06004133static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
4134 struct ceph_msg_header *hdr, int *skip)
4135{
4136 struct ceph_msg *msg;
4137 int type = (int) le16_to_cpu(hdr->type);
4138 int front_len = (int) le32_to_cpu(hdr->front_len);
4139
4140 if (con->in_msg)
4141 return con->in_msg;
4142
4143 *skip = 0;
4144 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
4145 if (!msg) {
4146 pr_err("unable to allocate msg type %d len %d\n",
4147 type, front_len);
4148 return NULL;
4149 }
Alex Elder53ded492013-03-01 18:00:14 -06004150
4151 return msg;
4152}
4153
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004154static int mds_sign_message(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08004155{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004156 struct ceph_mds_session *s = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08004157 struct ceph_auth_handshake *auth = &s->s_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004158
Yan, Zheng33d07332014-11-04 16:33:37 +08004159 return ceph_auth_sign_message(auth, msg);
4160}
4161
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004162static int mds_check_message_signature(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08004163{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004164 struct ceph_mds_session *s = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08004165 struct ceph_auth_handshake *auth = &s->s_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004166
Yan, Zheng33d07332014-11-04 16:33:37 +08004167 return ceph_auth_check_message_signature(auth, msg);
4168}
4169
Tobias Klauser9e327892010-05-20 10:40:19 +02004170static const struct ceph_connection_operations mds_con_ops = {
Sage Weil2f2dc052009-10-06 11:31:09 -07004171 .get = con_get,
4172 .put = con_put,
4173 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004174 .get_authorizer = get_authorizer,
4175 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004176 .invalidate_authorizer = invalidate_authorizer,
Sage Weil2f2dc052009-10-06 11:31:09 -07004177 .peer_reset = peer_reset,
Alex Elder53ded492013-03-01 18:00:14 -06004178 .alloc_msg = mds_alloc_msg,
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004179 .sign_message = mds_sign_message,
4180 .check_message_signature = mds_check_message_signature,
Sage Weil2f2dc052009-10-06 11:31:09 -07004181};
4182
Sage Weil2f2dc052009-10-06 11:31:09 -07004183/* eof */