blob: 2e8f90f96540291ab412703133d2a4278edbac71 [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{
Zhi Zhange30ee582018-01-24 21:24:33 +0800607 int ret = 0;
608
Sage Weil2f2dc052009-10-06 11:31:09 -0700609 req->r_tid = ++mdsc->last_tid;
Zhi Zhange30ee582018-01-24 21:24:33 +0800610 if (req->r_num_caps) {
611 ret = ceph_reserve_caps(mdsc, &req->r_caps_reservation,
612 req->r_num_caps);
613 if (ret < 0) {
614 pr_err("__register_request %p "
615 "failed to reserve caps: %d\n", req, ret);
616 /* set req->r_err to fail early from __do_request */
617 req->r_err = ret;
618 return;
619 }
620 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700621 dout("__register_request %p tid %lld\n", req, req->r_tid);
622 ceph_mdsc_get_request(req);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200623 insert_request(&mdsc->request_tree, req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700624
Sage Weilcb4276c2010-11-08 07:28:52 -0800625 req->r_uid = current_fsuid();
626 req->r_gid = current_fsgid();
627
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +0800628 if (mdsc->oldest_tid == 0 && req->r_op != CEPH_MDS_OP_SETFILELOCK)
629 mdsc->oldest_tid = req->r_tid;
630
Sage Weil2f2dc052009-10-06 11:31:09 -0700631 if (dir) {
Sage Weil3b663782011-05-18 16:12:12 -0700632 ihold(dir);
Sage Weil2f2dc052009-10-06 11:31:09 -0700633 req->r_unsafe_dir = dir;
Sage Weil2f2dc052009-10-06 11:31:09 -0700634 }
635}
636
637static void __unregister_request(struct ceph_mds_client *mdsc,
638 struct ceph_mds_request *req)
639{
640 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +0800641
Jeff Laytondf963ea2017-02-14 10:09:40 -0500642 /* Never leave an unregistered request on an unsafe list! */
643 list_del_init(&req->r_unsafe_item);
644
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +0800645 if (req->r_tid == mdsc->oldest_tid) {
646 struct rb_node *p = rb_next(&req->r_node);
647 mdsc->oldest_tid = 0;
648 while (p) {
649 struct ceph_mds_request *next_req =
650 rb_entry(p, struct ceph_mds_request, r_node);
651 if (next_req->r_op != CEPH_MDS_OP_SETFILELOCK) {
652 mdsc->oldest_tid = next_req->r_tid;
653 break;
654 }
655 p = rb_next(p);
656 }
657 }
658
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200659 erase_request(&mdsc->request_tree, req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700660
Jeff Laytonbc2de10d2017-02-01 13:49:09 -0500661 if (req->r_unsafe_dir &&
662 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700663 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
Sage Weil2f2dc052009-10-06 11:31:09 -0700664 spin_lock(&ci->i_unsafe_lock);
665 list_del_init(&req->r_unsafe_dir_item);
666 spin_unlock(&ci->i_unsafe_lock);
Yan, Zheng4c06ace2015-10-27 17:18:00 +0800667 }
Jeff Laytonbc2de10d2017-02-01 13:49:09 -0500668 if (req->r_target_inode &&
669 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Yan, Zheng68cd5b42015-10-27 18:36:06 +0800670 struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
671 spin_lock(&ci->i_unsafe_lock);
672 list_del_init(&req->r_unsafe_target_item);
673 spin_unlock(&ci->i_unsafe_lock);
674 }
Sage Weil3b663782011-05-18 16:12:12 -0700675
Yan, Zheng4c06ace2015-10-27 17:18:00 +0800676 if (req->r_unsafe_dir) {
Sage Weil3b663782011-05-18 16:12:12 -0700677 iput(req->r_unsafe_dir);
678 req->r_unsafe_dir = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700679 }
Sage Weil94aa8ae2010-03-28 21:22:50 -0700680
Yan, Zhengfc55d2c2013-10-31 09:10:47 +0800681 complete_all(&req->r_safe_completion);
682
Sage Weil94aa8ae2010-03-28 21:22:50 -0700683 ceph_mdsc_put_request(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700684}
685
686/*
Jeff Layton30c71232016-12-15 08:37:56 -0500687 * Walk back up the dentry tree until we hit a dentry representing a
688 * non-snapshot inode. We do this using the rcu_read_lock (which must be held
689 * when calling this) to ensure that the objects won't disappear while we're
690 * working with them. Once we hit a candidate dentry, we attempt to take a
691 * reference to it, and return that as the result.
692 */
Dan Carpenterf1075482017-02-23 13:39:59 +0300693static struct inode *get_nonsnap_parent(struct dentry *dentry)
694{
695 struct inode *inode = NULL;
Jeff Layton30c71232016-12-15 08:37:56 -0500696
697 while (dentry && !IS_ROOT(dentry)) {
698 inode = d_inode_rcu(dentry);
699 if (!inode || ceph_snap(inode) == CEPH_NOSNAP)
700 break;
701 dentry = dentry->d_parent;
702 }
703 if (inode)
704 inode = igrab(inode);
705 return inode;
706}
707
708/*
Sage Weil2f2dc052009-10-06 11:31:09 -0700709 * Choose mds to send request to next. If there is a hint set in the
710 * request (e.g., due to a prior forward hint from the mds), use that.
711 * Otherwise, consult frag tree and/or caps to identify the
712 * appropriate mds. If all else fails, choose randomly.
713 *
714 * Called under mdsc->mutex.
715 */
716static int __choose_mds(struct ceph_mds_client *mdsc,
717 struct ceph_mds_request *req)
718{
719 struct inode *inode;
720 struct ceph_inode_info *ci;
721 struct ceph_cap *cap;
722 int mode = req->r_direct_mode;
723 int mds = -1;
724 u32 hash = req->r_direct_hash;
Jeff Laytonbc2de10d2017-02-01 13:49:09 -0500725 bool is_hash = test_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags);
Sage Weil2f2dc052009-10-06 11:31:09 -0700726
727 /*
728 * is there a specific mds we should try? ignore hint if we have
729 * no session and the mds is not up (active or recovering).
730 */
731 if (req->r_resend_mds >= 0 &&
732 (__have_session(mdsc, req->r_resend_mds) ||
733 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
734 dout("choose_mds using resend_mds mds%d\n",
735 req->r_resend_mds);
736 return req->r_resend_mds;
737 }
738
739 if (mode == USE_RANDOM_MDS)
740 goto random;
741
742 inode = NULL;
743 if (req->r_inode) {
Yan, Zheng5d37ca12017-07-26 12:48:08 +0800744 if (ceph_snap(req->r_inode) != CEPH_SNAPDIR) {
745 inode = req->r_inode;
746 ihold(inode);
747 } else {
Yan, Zheng38f340c2017-09-22 11:41:06 +0800748 /* req->r_dentry is non-null for LSSNAP request */
749 rcu_read_lock();
750 inode = get_nonsnap_parent(req->r_dentry);
751 rcu_read_unlock();
752 dout("__choose_mds using snapdir's parent %p\n", inode);
Yan, Zheng5d37ca12017-07-26 12:48:08 +0800753 }
Yan, Zheng38f340c2017-09-22 11:41:06 +0800754 } else if (req->r_dentry) {
Sage Weild79698d2011-07-26 11:31:26 -0700755 /* ignore race with rename; old or new d_parent is okay */
Jeff Layton30c71232016-12-15 08:37:56 -0500756 struct dentry *parent;
757 struct inode *dir;
Sage Weileb6bb1c2010-08-16 09:21:27 -0700758
Jeff Layton30c71232016-12-15 08:37:56 -0500759 rcu_read_lock();
760 parent = req->r_dentry->d_parent;
Jeff Layton3dd69aa2017-01-31 10:28:26 -0500761 dir = req->r_parent ? : d_inode_rcu(parent);
Jeff Layton30c71232016-12-15 08:37:56 -0500762
763 if (!dir || dir->i_sb != mdsc->fsc->sb) {
764 /* not this fs or parent went negative */
David Howells2b0143b2015-03-17 22:25:59 +0000765 inode = d_inode(req->r_dentry);
Jeff Layton30c71232016-12-15 08:37:56 -0500766 if (inode)
767 ihold(inode);
Sage Weileb6bb1c2010-08-16 09:21:27 -0700768 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
769 /* direct snapped/virtual snapdir requests
770 * based on parent dir inode */
Jeff Layton30c71232016-12-15 08:37:56 -0500771 inode = get_nonsnap_parent(parent);
Sage Weileb6bb1c2010-08-16 09:21:27 -0700772 dout("__choose_mds using nonsnap parent %p\n", inode);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800773 } else {
Sage Weileb6bb1c2010-08-16 09:21:27 -0700774 /* dentry target */
David Howells2b0143b2015-03-17 22:25:59 +0000775 inode = d_inode(req->r_dentry);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800776 if (!inode || mode == USE_AUTH_MDS) {
777 /* dir + name */
Jeff Layton30c71232016-12-15 08:37:56 -0500778 inode = igrab(dir);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800779 hash = ceph_dentry_hash(dir, req->r_dentry);
780 is_hash = true;
Jeff Layton30c71232016-12-15 08:37:56 -0500781 } else {
782 ihold(inode);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800783 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700784 }
Jeff Layton30c71232016-12-15 08:37:56 -0500785 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -0700786 }
Sage Weileb6bb1c2010-08-16 09:21:27 -0700787
Sage Weil2f2dc052009-10-06 11:31:09 -0700788 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
789 (int)hash, mode);
790 if (!inode)
791 goto random;
792 ci = ceph_inode(inode);
793
794 if (is_hash && S_ISDIR(inode->i_mode)) {
795 struct ceph_inode_frag frag;
796 int found;
797
798 ceph_choose_frag(ci, hash, &frag, &found);
799 if (found) {
800 if (mode == USE_ANY_MDS && frag.ndist > 0) {
801 u8 r;
802
803 /* choose a random replica */
804 get_random_bytes(&r, 1);
805 r %= frag.ndist;
806 mds = frag.dist[r];
807 dout("choose_mds %p %llx.%llx "
808 "frag %u mds%d (%d/%d)\n",
809 inode, ceph_vinop(inode),
Sage Weild66bbd42011-01-21 21:16:46 -0800810 frag.frag, mds,
Sage Weil2f2dc052009-10-06 11:31:09 -0700811 (int)r, frag.ndist);
Sage Weild66bbd42011-01-21 21:16:46 -0800812 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
813 CEPH_MDS_STATE_ACTIVE)
Jeff Layton30c71232016-12-15 08:37:56 -0500814 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -0700815 }
816
817 /* since this file/dir wasn't known to be
818 * replicated, then we want to look for the
819 * authoritative mds. */
820 mode = USE_AUTH_MDS;
821 if (frag.mds >= 0) {
822 /* choose auth mds */
823 mds = frag.mds;
824 dout("choose_mds %p %llx.%llx "
825 "frag %u mds%d (auth)\n",
826 inode, ceph_vinop(inode), frag.frag, mds);
Sage Weild66bbd42011-01-21 21:16:46 -0800827 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
828 CEPH_MDS_STATE_ACTIVE)
Jeff Layton30c71232016-12-15 08:37:56 -0500829 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -0700830 }
831 }
832 }
833
Sage Weilbe655592011-11-30 09:47:09 -0800834 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700835 cap = NULL;
836 if (mode == USE_AUTH_MDS)
837 cap = ci->i_auth_cap;
838 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
839 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
840 if (!cap) {
Sage Weilbe655592011-11-30 09:47:09 -0800841 spin_unlock(&ci->i_ceph_lock);
Jeff Layton30c71232016-12-15 08:37:56 -0500842 iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -0700843 goto random;
844 }
845 mds = cap->session->s_mds;
846 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
847 inode, ceph_vinop(inode), mds,
848 cap == ci->i_auth_cap ? "auth " : "", cap);
Sage Weilbe655592011-11-30 09:47:09 -0800849 spin_unlock(&ci->i_ceph_lock);
Jeff Layton30c71232016-12-15 08:37:56 -0500850out:
851 iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -0700852 return mds;
853
854random:
855 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
856 dout("choose_mds chose random mds%d\n", mds);
857 return mds;
858}
859
860
861/*
862 * session messages
863 */
864static struct ceph_msg *create_session_msg(u32 op, u64 seq)
865{
866 struct ceph_msg *msg;
867 struct ceph_mds_session_head *h;
868
Sage Weilb61c2762011-08-09 15:03:46 -0700869 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
870 false);
Sage Weila79832f2010-04-01 16:06:19 -0700871 if (!msg) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700872 pr_err("create_session_msg ENOMEM creating msg\n");
Sage Weila79832f2010-04-01 16:06:19 -0700873 return NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700874 }
875 h = msg->front.iov_base;
876 h->op = cpu_to_le32(op);
877 h->seq = cpu_to_le64(seq);
John Spraydbd0c8b2014-09-09 19:26:01 +0100878
879 return msg;
880}
881
882/*
883 * session message, specialization for CEPH_SESSION_REQUEST_OPEN
884 * to include additional client metadata fields.
885 */
886static struct ceph_msg *create_session_open_msg(struct ceph_mds_client *mdsc, u64 seq)
887{
888 struct ceph_msg *msg;
889 struct ceph_mds_session_head *h;
890 int i = -1;
891 int metadata_bytes = 0;
892 int metadata_key_count = 0;
893 struct ceph_options *opt = mdsc->fsc->client->options;
Yan, Zheng3f384952016-04-21 11:09:55 +0800894 struct ceph_mount_options *fsopt = mdsc->fsc->mount_options;
John Spraydbd0c8b2014-09-09 19:26:01 +0100895 void *p;
896
Yan, Zhenga6a5ce42015-01-16 10:54:43 +0800897 const char* metadata[][2] = {
Yan, Zheng717e6f22017-09-11 12:10:08 +0800898 {"hostname", mdsc->nodename},
899 {"kernel_version", init_utsname()->release},
Yan, Zheng3f384952016-04-21 11:09:55 +0800900 {"entity_id", opt->name ? : ""},
901 {"root", fsopt->server_path ? : "/"},
John Spraydbd0c8b2014-09-09 19:26:01 +0100902 {NULL, NULL}
903 };
904
905 /* Calculate serialized length of metadata */
906 metadata_bytes = 4; /* map length */
Markus Elfringd37b1d92017-08-20 20:22:02 +0200907 for (i = 0; metadata[i][0]; ++i) {
John Spraydbd0c8b2014-09-09 19:26:01 +0100908 metadata_bytes += 8 + strlen(metadata[i][0]) +
909 strlen(metadata[i][1]);
910 metadata_key_count++;
911 }
912
913 /* Allocate the message */
914 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + metadata_bytes,
915 GFP_NOFS, false);
916 if (!msg) {
917 pr_err("create_session_msg ENOMEM creating msg\n");
918 return NULL;
919 }
920 h = msg->front.iov_base;
921 h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN);
922 h->seq = cpu_to_le64(seq);
923
924 /*
925 * Serialize client metadata into waiting buffer space, using
926 * the format that userspace expects for map<string, string>
John Spray7cfa0312014-10-30 17:15:26 +0000927 *
928 * ClientSession messages with metadata are v2
John Spraydbd0c8b2014-09-09 19:26:01 +0100929 */
John Spray7cfa0312014-10-30 17:15:26 +0000930 msg->hdr.version = cpu_to_le16(2);
931 msg->hdr.compat_version = cpu_to_le16(1);
John Spraydbd0c8b2014-09-09 19:26:01 +0100932
933 /* The write pointer, following the session_head structure */
934 p = msg->front.iov_base + sizeof(*h);
935
936 /* Number of entries in the map */
937 ceph_encode_32(&p, metadata_key_count);
938
939 /* Two length-prefixed strings for each entry in the map */
Markus Elfringd37b1d92017-08-20 20:22:02 +0200940 for (i = 0; metadata[i][0]; ++i) {
John Spraydbd0c8b2014-09-09 19:26:01 +0100941 size_t const key_len = strlen(metadata[i][0]);
942 size_t const val_len = strlen(metadata[i][1]);
943
944 ceph_encode_32(&p, key_len);
945 memcpy(p, metadata[i][0], key_len);
946 p += key_len;
947 ceph_encode_32(&p, val_len);
948 memcpy(p, metadata[i][1], val_len);
949 p += val_len;
950 }
951
Sage Weil2f2dc052009-10-06 11:31:09 -0700952 return msg;
953}
954
955/*
956 * send session open request.
957 *
958 * called under mdsc->mutex
959 */
960static int __open_session(struct ceph_mds_client *mdsc,
961 struct ceph_mds_session *session)
962{
963 struct ceph_msg *msg;
964 int mstate;
965 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -0700966
967 /* wait for mds to go active? */
968 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
969 dout("open_session to mds%d (%s)\n", mds,
970 ceph_mds_state_name(mstate));
971 session->s_state = CEPH_MDS_SESSION_OPENING;
972 session->s_renew_requested = jiffies;
973
974 /* send connect message */
John Spraydbd0c8b2014-09-09 19:26:01 +0100975 msg = create_session_open_msg(mdsc, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -0700976 if (!msg)
977 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -0700978 ceph_con_send(&session->s_con, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -0700979 return 0;
980}
981
982/*
Sage Weiled0552a2010-06-21 13:38:25 -0700983 * open sessions for any export targets for the given mds
984 *
985 * called under mdsc->mutex
986 */
Yan, Zheng5d72d132013-11-24 14:33:01 +0800987static struct ceph_mds_session *
988__open_export_target_session(struct ceph_mds_client *mdsc, int target)
989{
990 struct ceph_mds_session *session;
991
992 session = __ceph_lookup_mds_session(mdsc, target);
993 if (!session) {
994 session = register_session(mdsc, target);
995 if (IS_ERR(session))
996 return session;
997 }
998 if (session->s_state == CEPH_MDS_SESSION_NEW ||
999 session->s_state == CEPH_MDS_SESSION_CLOSING)
1000 __open_session(mdsc, session);
1001
1002 return session;
1003}
1004
1005struct ceph_mds_session *
1006ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target)
1007{
1008 struct ceph_mds_session *session;
1009
1010 dout("open_export_target_session to mds%d\n", target);
1011
1012 mutex_lock(&mdsc->mutex);
1013 session = __open_export_target_session(mdsc, target);
1014 mutex_unlock(&mdsc->mutex);
1015
1016 return session;
1017}
1018
Sage Weiled0552a2010-06-21 13:38:25 -07001019static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
1020 struct ceph_mds_session *session)
1021{
1022 struct ceph_mds_info *mi;
1023 struct ceph_mds_session *ts;
1024 int i, mds = session->s_mds;
Sage Weiled0552a2010-06-21 13:38:25 -07001025
Yan, Zheng76201b62017-03-28 17:04:13 +08001026 if (mds >= mdsc->mdsmap->m_num_mds)
Sage Weiled0552a2010-06-21 13:38:25 -07001027 return;
Yan, Zheng5d72d132013-11-24 14:33:01 +08001028
Sage Weiled0552a2010-06-21 13:38:25 -07001029 mi = &mdsc->mdsmap->m_info[mds];
1030 dout("open_export_target_sessions for mds%d (%d targets)\n",
1031 session->s_mds, mi->num_export_targets);
1032
1033 for (i = 0; i < mi->num_export_targets; i++) {
Yan, Zheng5d72d132013-11-24 14:33:01 +08001034 ts = __open_export_target_session(mdsc, mi->export_targets[i]);
1035 if (!IS_ERR(ts))
1036 ceph_put_mds_session(ts);
Sage Weiled0552a2010-06-21 13:38:25 -07001037 }
1038}
1039
Sage Weil154f42c2010-06-21 13:45:04 -07001040void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
1041 struct ceph_mds_session *session)
1042{
1043 mutex_lock(&mdsc->mutex);
1044 __open_export_target_sessions(mdsc, session);
1045 mutex_unlock(&mdsc->mutex);
1046}
1047
Sage Weiled0552a2010-06-21 13:38:25 -07001048/*
Sage Weil2f2dc052009-10-06 11:31:09 -07001049 * session caps
1050 */
1051
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001052static void detach_cap_releases(struct ceph_mds_session *session,
1053 struct list_head *target)
Sage Weil2f2dc052009-10-06 11:31:09 -07001054{
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001055 lockdep_assert_held(&session->s_cap_lock);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001056
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001057 list_splice_init(&session->s_cap_releases, target);
1058 session->s_num_cap_releases = 0;
1059 dout("dispose_cap_releases mds%d\n", session->s_mds);
1060}
1061
1062static void dispose_cap_releases(struct ceph_mds_client *mdsc,
1063 struct list_head *dispose)
1064{
1065 while (!list_empty(dispose)) {
Yan, Zheng745a8e32015-05-14 17:22:42 +08001066 struct ceph_cap *cap;
1067 /* zero out the in-progress message */
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001068 cap = list_first_entry(dispose, struct ceph_cap, session_caps);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001069 list_del(&cap->session_caps);
1070 ceph_put_cap(mdsc, cap);
1071 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001072}
1073
Yan, Zheng1c841a92015-03-24 20:15:36 +08001074static void cleanup_session_requests(struct ceph_mds_client *mdsc,
1075 struct ceph_mds_session *session)
1076{
1077 struct ceph_mds_request *req;
1078 struct rb_node *p;
1079
1080 dout("cleanup_session_requests mds%d\n", session->s_mds);
1081 mutex_lock(&mdsc->mutex);
1082 while (!list_empty(&session->s_unsafe)) {
1083 req = list_first_entry(&session->s_unsafe,
1084 struct ceph_mds_request, r_unsafe_item);
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001085 pr_warn_ratelimited(" dropping unsafe request %llu\n",
1086 req->r_tid);
Yan, Zheng1c841a92015-03-24 20:15:36 +08001087 __unregister_request(mdsc, req);
1088 }
1089 /* zero r_attempts, so kick_requests() will re-send requests */
1090 p = rb_first(&mdsc->request_tree);
1091 while (p) {
1092 req = rb_entry(p, struct ceph_mds_request, r_node);
1093 p = rb_next(p);
1094 if (req->r_session &&
1095 req->r_session->s_mds == session->s_mds)
1096 req->r_attempts = 0;
1097 }
1098 mutex_unlock(&mdsc->mutex);
1099}
1100
Sage Weil2f2dc052009-10-06 11:31:09 -07001101/*
Sage Weilf818a732010-05-11 20:56:31 -07001102 * Helper to safely iterate over all caps associated with a session, with
1103 * special care taken to handle a racing __ceph_remove_cap().
Sage Weil2f2dc052009-10-06 11:31:09 -07001104 *
Sage Weilf818a732010-05-11 20:56:31 -07001105 * Caller must hold session s_mutex.
Sage Weil2f2dc052009-10-06 11:31:09 -07001106 */
1107static int iterate_session_caps(struct ceph_mds_session *session,
1108 int (*cb)(struct inode *, struct ceph_cap *,
1109 void *), void *arg)
1110{
Sage Weil7c1332b2010-02-16 11:39:45 -08001111 struct list_head *p;
1112 struct ceph_cap *cap;
1113 struct inode *inode, *last_inode = NULL;
1114 struct ceph_cap *old_cap = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001115 int ret;
1116
1117 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
1118 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001119 p = session->s_caps.next;
1120 while (p != &session->s_caps) {
1121 cap = list_entry(p, struct ceph_cap, session_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -07001122 inode = igrab(&cap->ci->vfs_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001123 if (!inode) {
1124 p = p->next;
Sage Weil2f2dc052009-10-06 11:31:09 -07001125 continue;
Sage Weil7c1332b2010-02-16 11:39:45 -08001126 }
1127 session->s_cap_iterator = cap;
Sage Weil2f2dc052009-10-06 11:31:09 -07001128 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001129
1130 if (last_inode) {
1131 iput(last_inode);
1132 last_inode = NULL;
1133 }
1134 if (old_cap) {
Yehuda Sadeh37151662010-06-17 16:16:12 -07001135 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -08001136 old_cap = NULL;
1137 }
1138
Sage Weil2f2dc052009-10-06 11:31:09 -07001139 ret = cb(inode, cap, arg);
Sage Weil7c1332b2010-02-16 11:39:45 -08001140 last_inode = inode;
1141
Sage Weil2f2dc052009-10-06 11:31:09 -07001142 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001143 p = p->next;
Markus Elfringd37b1d92017-08-20 20:22:02 +02001144 if (!cap->ci) {
Sage Weil7c1332b2010-02-16 11:39:45 -08001145 dout("iterate_session_caps finishing cap %p removal\n",
1146 cap);
1147 BUG_ON(cap->session != session);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001148 cap->session = NULL;
Sage Weil7c1332b2010-02-16 11:39:45 -08001149 list_del_init(&cap->session_caps);
1150 session->s_nr_caps--;
Yan, Zheng745a8e32015-05-14 17:22:42 +08001151 if (cap->queue_release) {
1152 list_add_tail(&cap->session_caps,
1153 &session->s_cap_releases);
1154 session->s_num_cap_releases++;
1155 } else {
1156 old_cap = cap; /* put_cap it w/o locks held */
1157 }
Sage Weil7c1332b2010-02-16 11:39:45 -08001158 }
Sage Weil5dacf092009-12-21 20:40:34 -08001159 if (ret < 0)
1160 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07001161 }
Sage Weil5dacf092009-12-21 20:40:34 -08001162 ret = 0;
1163out:
Sage Weil7c1332b2010-02-16 11:39:45 -08001164 session->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001165 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001166
SF Markus Elfringe96a6502014-11-02 15:20:59 +01001167 iput(last_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001168 if (old_cap)
Yehuda Sadeh37151662010-06-17 16:16:12 -07001169 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -08001170
Sage Weil5dacf092009-12-21 20:40:34 -08001171 return ret;
Sage Weil2f2dc052009-10-06 11:31:09 -07001172}
1173
1174static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
Sage Weil6c99f252010-05-10 16:12:25 -07001175 void *arg)
Sage Weil2f2dc052009-10-06 11:31:09 -07001176{
Yan, Zheng6c93df52016-04-15 13:56:12 +08001177 struct ceph_fs_client *fsc = (struct ceph_fs_client *)arg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001178 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001179 LIST_HEAD(to_remove);
Yan, Zheng6c93df52016-04-15 13:56:12 +08001180 bool drop = false;
1181 bool invalidate = false;
Sage Weil6c99f252010-05-10 16:12:25 -07001182
Sage Weil2f2dc052009-10-06 11:31:09 -07001183 dout("removing cap %p, ci is %p, inode is %p\n",
1184 cap, ci, &ci->vfs_inode);
Sage Weilbe655592011-11-30 09:47:09 -08001185 spin_lock(&ci->i_ceph_lock);
Yan, Zhenga096b092013-09-22 10:15:58 +08001186 __ceph_remove_cap(cap, false);
Yan, Zheng571ade32015-03-24 11:36:08 +08001187 if (!ci->i_auth_cap) {
Yan, Zheng553adfd2015-06-09 15:48:57 +08001188 struct ceph_cap_flush *cf;
Yan, Zheng6c93df52016-04-15 13:56:12 +08001189 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil6c99f252010-05-10 16:12:25 -07001190
Yan, Zheng77310322016-04-08 15:27:16 +08001191 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
1192
Yan, Zheng6c93df52016-04-15 13:56:12 +08001193 if (ci->i_wrbuffer_ref > 0 &&
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01001194 READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
Yan, Zheng6c93df52016-04-15 13:56:12 +08001195 invalidate = true;
1196
Yan, Zhenge4500b52016-07-06 11:12:56 +08001197 while (!list_empty(&ci->i_cap_flush_list)) {
1198 cf = list_first_entry(&ci->i_cap_flush_list,
1199 struct ceph_cap_flush, i_list);
Wei Yongjun8cdcc072016-08-13 09:10:28 +00001200 list_move(&cf->i_list, &to_remove);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001201 }
1202
Sage Weil6c99f252010-05-10 16:12:25 -07001203 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zheng8310b082015-06-09 17:20:12 +08001204
Yan, Zhenge4500b52016-07-06 11:12:56 +08001205 list_for_each_entry(cf, &to_remove, i_list)
1206 list_del(&cf->g_list);
Yan, Zheng8310b082015-06-09 17:20:12 +08001207
Sage Weil6c99f252010-05-10 16:12:25 -07001208 if (!list_empty(&ci->i_dirty_item)) {
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001209 pr_warn_ratelimited(
1210 " dropping dirty %s state for %p %lld\n",
Sage Weil6c99f252010-05-10 16:12:25 -07001211 ceph_cap_string(ci->i_dirty_caps),
1212 inode, ceph_ino(inode));
1213 ci->i_dirty_caps = 0;
1214 list_del_init(&ci->i_dirty_item);
Yan, Zheng6c93df52016-04-15 13:56:12 +08001215 drop = true;
Sage Weil6c99f252010-05-10 16:12:25 -07001216 }
1217 if (!list_empty(&ci->i_flushing_item)) {
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001218 pr_warn_ratelimited(
1219 " dropping dirty+flushing %s state for %p %lld\n",
Sage Weil6c99f252010-05-10 16:12:25 -07001220 ceph_cap_string(ci->i_flushing_caps),
1221 inode, ceph_ino(inode));
1222 ci->i_flushing_caps = 0;
1223 list_del_init(&ci->i_flushing_item);
1224 mdsc->num_cap_flushing--;
Yan, Zheng6c93df52016-04-15 13:56:12 +08001225 drop = true;
Sage Weil6c99f252010-05-10 16:12:25 -07001226 }
Sage Weil6c99f252010-05-10 16:12:25 -07001227 spin_unlock(&mdsc->cap_dirty_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001228
Yan, Zhengb3f8d682017-09-11 10:58:55 +08001229 if (atomic_read(&ci->i_filelock_ref) > 0) {
1230 /* make further file lock syscall return -EIO */
1231 ci->i_ceph_flags |= CEPH_I_ERROR_FILELOCK;
1232 pr_warn_ratelimited(" dropping file locks for %p %lld\n",
1233 inode, ceph_ino(inode));
1234 }
1235
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001236 if (!ci->i_dirty_caps && ci->i_prealloc_cap_flush) {
Yan, Zhenge4500b52016-07-06 11:12:56 +08001237 list_add(&ci->i_prealloc_cap_flush->i_list, &to_remove);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001238 ci->i_prealloc_cap_flush = NULL;
1239 }
Sage Weil6c99f252010-05-10 16:12:25 -07001240 }
Sage Weilbe655592011-11-30 09:47:09 -08001241 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001242 while (!list_empty(&to_remove)) {
1243 struct ceph_cap_flush *cf;
1244 cf = list_first_entry(&to_remove,
Yan, Zhenge4500b52016-07-06 11:12:56 +08001245 struct ceph_cap_flush, i_list);
1246 list_del(&cf->i_list);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001247 ceph_free_cap_flush(cf);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001248 }
Yan, Zheng77310322016-04-08 15:27:16 +08001249
1250 wake_up_all(&ci->i_cap_wq);
Yan, Zheng6c93df52016-04-15 13:56:12 +08001251 if (invalidate)
1252 ceph_queue_invalidate(inode);
Yan, Zheng77310322016-04-08 15:27:16 +08001253 if (drop)
Sage Weil6c99f252010-05-10 16:12:25 -07001254 iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07001255 return 0;
1256}
1257
1258/*
1259 * caller must hold session s_mutex
1260 */
1261static void remove_session_caps(struct ceph_mds_session *session)
1262{
Yan, Zheng6c93df52016-04-15 13:56:12 +08001263 struct ceph_fs_client *fsc = session->s_mdsc->fsc;
1264 struct super_block *sb = fsc->sb;
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001265 LIST_HEAD(dispose);
1266
Sage Weil2f2dc052009-10-06 11:31:09 -07001267 dout("remove_session_caps on %p\n", session);
Yan, Zheng6c93df52016-04-15 13:56:12 +08001268 iterate_session_caps(session, remove_session_caps_cb, fsc);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001269
Yan, Zhengc8799fc2016-07-07 15:22:38 +08001270 wake_up_all(&fsc->mdsc->cap_flushing_wq);
1271
Yan, Zheng6f60f882013-07-24 12:22:11 +08001272 spin_lock(&session->s_cap_lock);
1273 if (session->s_nr_caps > 0) {
Yan, Zheng6f60f882013-07-24 12:22:11 +08001274 struct inode *inode;
1275 struct ceph_cap *cap, *prev = NULL;
1276 struct ceph_vino vino;
1277 /*
1278 * iterate_session_caps() skips inodes that are being
1279 * deleted, we need to wait until deletions are complete.
1280 * __wait_on_freeing_inode() is designed for the job,
1281 * but it is not exported, so use lookup inode function
1282 * to access it.
1283 */
1284 while (!list_empty(&session->s_caps)) {
1285 cap = list_entry(session->s_caps.next,
1286 struct ceph_cap, session_caps);
1287 if (cap == prev)
1288 break;
1289 prev = cap;
1290 vino = cap->ci->i_vino;
1291 spin_unlock(&session->s_cap_lock);
1292
Yan, Zhenged284c42013-09-02 15:19:53 +08001293 inode = ceph_find_inode(sb, vino);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001294 iput(inode);
1295
1296 spin_lock(&session->s_cap_lock);
1297 }
1298 }
Yan, Zheng745a8e32015-05-14 17:22:42 +08001299
1300 // drop cap expires and unlock s_cap_lock
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001301 detach_cap_releases(session, &dispose);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001302
Sage Weil2f2dc052009-10-06 11:31:09 -07001303 BUG_ON(session->s_nr_caps > 0);
Sage Weil6c99f252010-05-10 16:12:25 -07001304 BUG_ON(!list_empty(&session->s_cap_flushing));
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001305 spin_unlock(&session->s_cap_lock);
1306 dispose_cap_releases(session->s_mdsc, &dispose);
Sage Weil2f2dc052009-10-06 11:31:09 -07001307}
1308
1309/*
1310 * wake up any threads waiting on this session's caps. if the cap is
1311 * old (didn't get renewed on the client reconnect), remove it now.
1312 *
1313 * caller must hold s_mutex.
1314 */
1315static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1316 void *arg)
1317{
Sage Weil0dc25702009-11-20 13:43:45 -08001318 struct ceph_inode_info *ci = ceph_inode(inode);
1319
Sage Weil0dc25702009-11-20 13:43:45 -08001320 if (arg) {
Sage Weilbe655592011-11-30 09:47:09 -08001321 spin_lock(&ci->i_ceph_lock);
Sage Weil0dc25702009-11-20 13:43:45 -08001322 ci->i_wanted_max_size = 0;
1323 ci->i_requested_max_size = 0;
Sage Weilbe655592011-11-30 09:47:09 -08001324 spin_unlock(&ci->i_ceph_lock);
Sage Weil0dc25702009-11-20 13:43:45 -08001325 }
Yan, Zhenge5360302016-05-19 19:15:19 +08001326 wake_up_all(&ci->i_cap_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07001327 return 0;
1328}
1329
Sage Weil0dc25702009-11-20 13:43:45 -08001330static void wake_up_session_caps(struct ceph_mds_session *session,
1331 int reconnect)
Sage Weil2f2dc052009-10-06 11:31:09 -07001332{
1333 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
Sage Weil0dc25702009-11-20 13:43:45 -08001334 iterate_session_caps(session, wake_up_session_cb,
1335 (void *)(unsigned long)reconnect);
Sage Weil2f2dc052009-10-06 11:31:09 -07001336}
1337
1338/*
1339 * Send periodic message to MDS renewing all currently held caps. The
1340 * ack will reset the expiration for all caps from this session.
1341 *
1342 * caller holds s_mutex
1343 */
1344static int send_renew_caps(struct ceph_mds_client *mdsc,
1345 struct ceph_mds_session *session)
1346{
1347 struct ceph_msg *msg;
1348 int state;
1349
1350 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1351 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1352 pr_info("mds%d caps stale\n", session->s_mds);
Sage Weile4cb4cb2010-03-18 13:43:09 -07001353 session->s_renew_requested = jiffies;
Sage Weil2f2dc052009-10-06 11:31:09 -07001354
1355 /* do not try to renew caps until a recovering mds has reconnected
1356 * with its clients. */
1357 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1358 if (state < CEPH_MDS_STATE_RECONNECT) {
1359 dout("send_renew_caps ignoring mds%d (%s)\n",
1360 session->s_mds, ceph_mds_state_name(state));
1361 return 0;
1362 }
1363
1364 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1365 ceph_mds_state_name(state));
Sage Weil2f2dc052009-10-06 11:31:09 -07001366 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1367 ++session->s_renew_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001368 if (!msg)
1369 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -07001370 ceph_con_send(&session->s_con, msg);
1371 return 0;
1372}
1373
Yan, Zheng186e4f72013-11-22 14:48:37 +08001374static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
1375 struct ceph_mds_session *session, u64 seq)
1376{
1377 struct ceph_msg *msg;
1378
1379 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
John Spraya687eca2014-09-19 13:51:08 +01001380 session->s_mds, ceph_session_state_name(session->s_state), seq);
Yan, Zheng186e4f72013-11-22 14:48:37 +08001381 msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
1382 if (!msg)
1383 return -ENOMEM;
1384 ceph_con_send(&session->s_con, msg);
1385 return 0;
1386}
1387
1388
Sage Weil2f2dc052009-10-06 11:31:09 -07001389/*
1390 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
Sage Weil0dc25702009-11-20 13:43:45 -08001391 *
1392 * Called under session->s_mutex
Sage Weil2f2dc052009-10-06 11:31:09 -07001393 */
1394static void renewed_caps(struct ceph_mds_client *mdsc,
1395 struct ceph_mds_session *session, int is_renew)
1396{
1397 int was_stale;
1398 int wake = 0;
1399
1400 spin_lock(&session->s_cap_lock);
Alex Elder1ce208a2012-01-12 17:48:11 -08001401 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
Sage Weil2f2dc052009-10-06 11:31:09 -07001402
1403 session->s_cap_ttl = session->s_renew_requested +
1404 mdsc->mdsmap->m_session_timeout*HZ;
1405
1406 if (was_stale) {
1407 if (time_before(jiffies, session->s_cap_ttl)) {
1408 pr_info("mds%d caps renewed\n", session->s_mds);
1409 wake = 1;
1410 } else {
1411 pr_info("mds%d caps still stale\n", session->s_mds);
1412 }
1413 }
1414 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1415 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1416 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1417 spin_unlock(&session->s_cap_lock);
1418
1419 if (wake)
Sage Weil0dc25702009-11-20 13:43:45 -08001420 wake_up_session_caps(session, 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07001421}
1422
1423/*
1424 * send a session close request
1425 */
1426static int request_close_session(struct ceph_mds_client *mdsc,
1427 struct ceph_mds_session *session)
1428{
1429 struct ceph_msg *msg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001430
1431 dout("request_close_session mds%d state %s seq %lld\n",
John Spraya687eca2014-09-19 13:51:08 +01001432 session->s_mds, ceph_session_state_name(session->s_state),
Sage Weil2f2dc052009-10-06 11:31:09 -07001433 session->s_seq);
1434 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001435 if (!msg)
1436 return -ENOMEM;
1437 ceph_con_send(&session->s_con, msg);
Yan, Zhengfcff4152016-09-14 16:39:51 +08001438 return 1;
Sage Weil2f2dc052009-10-06 11:31:09 -07001439}
1440
1441/*
1442 * Called with s_mutex held.
1443 */
1444static int __close_session(struct ceph_mds_client *mdsc,
1445 struct ceph_mds_session *session)
1446{
1447 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1448 return 0;
1449 session->s_state = CEPH_MDS_SESSION_CLOSING;
1450 return request_close_session(mdsc, session);
1451}
1452
Yan, Zheng040d7862017-11-30 11:59:22 +08001453static bool drop_negative_children(struct dentry *dentry)
1454{
1455 struct dentry *child;
1456 bool all_negative = true;
1457
1458 if (!d_is_dir(dentry))
1459 goto out;
1460
1461 spin_lock(&dentry->d_lock);
1462 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
1463 if (d_really_is_positive(child)) {
1464 all_negative = false;
1465 break;
1466 }
1467 }
1468 spin_unlock(&dentry->d_lock);
1469
1470 if (all_negative)
1471 shrink_dcache_parent(dentry);
1472out:
1473 return all_negative;
1474}
1475
Sage Weil2f2dc052009-10-06 11:31:09 -07001476/*
1477 * Trim old(er) caps.
1478 *
1479 * Because we can't cache an inode without one or more caps, we do
1480 * this indirectly: if a cap is unused, we prune its aliases, at which
1481 * point the inode will hopefully get dropped to.
1482 *
1483 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1484 * memory pressure from the MDS, though, so it needn't be perfect.
1485 */
1486static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1487{
1488 struct ceph_mds_session *session = arg;
1489 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng979abfd2013-11-22 13:56:24 +08001490 int used, wanted, oissued, mine;
Sage Weil2f2dc052009-10-06 11:31:09 -07001491
1492 if (session->s_trim_caps <= 0)
1493 return -1;
1494
Sage Weilbe655592011-11-30 09:47:09 -08001495 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001496 mine = cap->issued | cap->implemented;
1497 used = __ceph_caps_used(ci);
Yan, Zheng979abfd2013-11-22 13:56:24 +08001498 wanted = __ceph_caps_file_wanted(ci);
Sage Weil2f2dc052009-10-06 11:31:09 -07001499 oissued = __ceph_caps_issued_other(ci, cap);
1500
Yan, Zheng979abfd2013-11-22 13:56:24 +08001501 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001502 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
Yan, Zheng979abfd2013-11-22 13:56:24 +08001503 ceph_cap_string(used), ceph_cap_string(wanted));
1504 if (cap == ci->i_auth_cap) {
Yan, Zheng622f3e22015-05-07 10:59:47 +08001505 if (ci->i_dirty_caps || ci->i_flushing_caps ||
1506 !list_empty(&ci->i_cap_snaps))
Yan, Zheng979abfd2013-11-22 13:56:24 +08001507 goto out;
1508 if ((used | wanted) & CEPH_CAP_ANY_WR)
1509 goto out;
Yan, Zheng89aa5932017-09-08 15:23:18 +08001510 /* Note: it's possible that i_filelock_ref becomes non-zero
1511 * after dropping auth caps. It doesn't hurt because reply
1512 * of lock mds request will re-add auth caps. */
1513 if (atomic_read(&ci->i_filelock_ref) > 0)
1514 goto out;
Yan, Zheng979abfd2013-11-22 13:56:24 +08001515 }
Yan, Zheng5e804ac2015-10-26 16:08:43 +08001516 /* The inode has cached pages, but it's no longer used.
1517 * we can safely drop it */
1518 if (wanted == 0 && used == CEPH_CAP_FILE_CACHE &&
1519 !(oissued & CEPH_CAP_FILE_CACHE)) {
1520 used = 0;
1521 oissued = 0;
1522 }
Yan, Zheng979abfd2013-11-22 13:56:24 +08001523 if ((used | wanted) & ~oissued & mine)
Sage Weil2f2dc052009-10-06 11:31:09 -07001524 goto out; /* we need these caps */
1525
Sage Weil2f2dc052009-10-06 11:31:09 -07001526 if (oissued) {
1527 /* we aren't the only cap.. just remove us */
Yan, Zhenga096b092013-09-22 10:15:58 +08001528 __ceph_remove_cap(cap, true);
Yan, Zheng040d7862017-11-30 11:59:22 +08001529 session->s_trim_caps--;
Sage Weil2f2dc052009-10-06 11:31:09 -07001530 } else {
Yan, Zheng040d7862017-11-30 11:59:22 +08001531 struct dentry *dentry;
Yan, Zheng5e804ac2015-10-26 16:08:43 +08001532 /* try dropping referring dentries */
Sage Weilbe655592011-11-30 09:47:09 -08001533 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng040d7862017-11-30 11:59:22 +08001534 dentry = d_find_any_alias(inode);
1535 if (dentry && drop_negative_children(dentry)) {
1536 int count;
1537 dput(dentry);
1538 d_prune_aliases(inode);
1539 count = atomic_read(&inode->i_count);
1540 if (count == 1)
1541 session->s_trim_caps--;
1542 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1543 inode, cap, count);
1544 } else {
1545 dput(dentry);
1546 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001547 return 0;
1548 }
1549
1550out:
Sage Weilbe655592011-11-30 09:47:09 -08001551 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001552 return 0;
1553}
1554
1555/*
1556 * Trim session cap count down to some max number.
1557 */
Zhi Zhange30ee582018-01-24 21:24:33 +08001558int ceph_trim_caps(struct ceph_mds_client *mdsc,
1559 struct ceph_mds_session *session,
1560 int max_caps)
Sage Weil2f2dc052009-10-06 11:31:09 -07001561{
1562 int trim_caps = session->s_nr_caps - max_caps;
1563
1564 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1565 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1566 if (trim_caps > 0) {
1567 session->s_trim_caps = trim_caps;
1568 iterate_session_caps(session, trim_caps_cb, session);
1569 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1570 session->s_mds, session->s_nr_caps, max_caps,
1571 trim_caps - session->s_trim_caps);
Sage Weil5dacf092009-12-21 20:40:34 -08001572 session->s_trim_caps = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001573 }
Yan, Zhenga56371d2014-04-01 20:34:56 +08001574
Yan, Zhenga56371d2014-04-01 20:34:56 +08001575 ceph_send_cap_releases(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07001576 return 0;
1577}
1578
Yan, Zheng8310b082015-06-09 17:20:12 +08001579static int check_caps_flush(struct ceph_mds_client *mdsc,
1580 u64 want_flush_tid)
1581{
Yan, Zheng8310b082015-06-09 17:20:12 +08001582 int ret = 1;
1583
1584 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zhenge4500b52016-07-06 11:12:56 +08001585 if (!list_empty(&mdsc->cap_flush_list)) {
1586 struct ceph_cap_flush *cf =
1587 list_first_entry(&mdsc->cap_flush_list,
1588 struct ceph_cap_flush, g_list);
1589 if (cf->tid <= want_flush_tid) {
1590 dout("check_caps_flush still flushing tid "
1591 "%llu <= %llu\n", cf->tid, want_flush_tid);
1592 ret = 0;
1593 }
Yan, Zheng8310b082015-06-09 17:20:12 +08001594 }
1595 spin_unlock(&mdsc->cap_dirty_lock);
1596 return ret;
Yan, Zhengd3383a82015-01-08 21:30:12 +08001597}
1598
Sage Weil2f2dc052009-10-06 11:31:09 -07001599/*
1600 * flush all dirty inode data to disk.
1601 *
Yan, Zheng8310b082015-06-09 17:20:12 +08001602 * returns true if we've flushed through want_flush_tid
Sage Weil2f2dc052009-10-06 11:31:09 -07001603 */
Yan, Zhengaffbc192015-05-05 21:22:13 +08001604static void wait_caps_flush(struct ceph_mds_client *mdsc,
Yan, Zheng0e294382016-07-04 18:06:41 +08001605 u64 want_flush_tid)
Sage Weil2f2dc052009-10-06 11:31:09 -07001606{
Yan, Zheng0e294382016-07-04 18:06:41 +08001607 dout("check_caps_flush want %llu\n", want_flush_tid);
Yan, Zheng8310b082015-06-09 17:20:12 +08001608
1609 wait_event(mdsc->cap_flushing_wq,
1610 check_caps_flush(mdsc, want_flush_tid));
1611
1612 dout("check_caps_flush ok, flushed thru %llu\n", want_flush_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07001613}
1614
1615/*
1616 * called under s_mutex
1617 */
Sage Weil3d7ded42010-06-09 16:47:10 -07001618void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1619 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001620{
Yan, Zheng745a8e32015-05-14 17:22:42 +08001621 struct ceph_msg *msg = NULL;
1622 struct ceph_mds_cap_release *head;
1623 struct ceph_mds_cap_item *item;
Jeff Layton92475f02017-04-13 11:07:04 -04001624 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
Yan, Zheng745a8e32015-05-14 17:22:42 +08001625 struct ceph_cap *cap;
1626 LIST_HEAD(tmp_list);
1627 int num_cap_releases;
Jeff Layton92475f02017-04-13 11:07:04 -04001628 __le32 barrier, *cap_barrier;
1629
1630 down_read(&osdc->lock);
1631 barrier = cpu_to_le32(osdc->epoch_barrier);
1632 up_read(&osdc->lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001633
Sage Weil0f8605f2010-05-05 15:51:35 -07001634 spin_lock(&session->s_cap_lock);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001635again:
1636 list_splice_init(&session->s_cap_releases, &tmp_list);
1637 num_cap_releases = session->s_num_cap_releases;
1638 session->s_num_cap_releases = 0;
1639 spin_unlock(&session->s_cap_lock);
1640
1641 while (!list_empty(&tmp_list)) {
1642 if (!msg) {
1643 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001644 PAGE_SIZE, GFP_NOFS, false);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001645 if (!msg)
1646 goto out_err;
1647 head = msg->front.iov_base;
1648 head->num = cpu_to_le32(0);
1649 msg->front.iov_len = sizeof(*head);
Jeff Layton92475f02017-04-13 11:07:04 -04001650
1651 msg->hdr.version = cpu_to_le16(2);
1652 msg->hdr.compat_version = cpu_to_le16(1);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001653 }
Jeff Layton92475f02017-04-13 11:07:04 -04001654
Yan, Zheng745a8e32015-05-14 17:22:42 +08001655 cap = list_first_entry(&tmp_list, struct ceph_cap,
1656 session_caps);
1657 list_del(&cap->session_caps);
1658 num_cap_releases--;
1659
1660 head = msg->front.iov_base;
1661 le32_add_cpu(&head->num, 1);
1662 item = msg->front.iov_base + msg->front.iov_len;
1663 item->ino = cpu_to_le64(cap->cap_ino);
1664 item->cap_id = cpu_to_le64(cap->cap_id);
1665 item->migrate_seq = cpu_to_le32(cap->mseq);
1666 item->seq = cpu_to_le32(cap->issue_seq);
1667 msg->front.iov_len += sizeof(*item);
1668
1669 ceph_put_cap(mdsc, cap);
1670
1671 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
Jeff Layton92475f02017-04-13 11:07:04 -04001672 // Append cap_barrier field
1673 cap_barrier = msg->front.iov_base + msg->front.iov_len;
1674 *cap_barrier = barrier;
1675 msg->front.iov_len += sizeof(*cap_barrier);
1676
Yan, Zheng745a8e32015-05-14 17:22:42 +08001677 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1678 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1679 ceph_con_send(&session->s_con, msg);
1680 msg = NULL;
1681 }
1682 }
1683
1684 BUG_ON(num_cap_releases != 0);
1685
1686 spin_lock(&session->s_cap_lock);
1687 if (!list_empty(&session->s_cap_releases))
1688 goto again;
1689 spin_unlock(&session->s_cap_lock);
1690
1691 if (msg) {
Jeff Layton92475f02017-04-13 11:07:04 -04001692 // Append cap_barrier field
1693 cap_barrier = msg->front.iov_base + msg->front.iov_len;
1694 *cap_barrier = barrier;
1695 msg->front.iov_len += sizeof(*cap_barrier);
1696
Sage Weil2f2dc052009-10-06 11:31:09 -07001697 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1698 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1699 ceph_con_send(&session->s_con, msg);
1700 }
Yan, Zheng745a8e32015-05-14 17:22:42 +08001701 return;
1702out_err:
1703 pr_err("send_cap_releases mds%d, failed to allocate message\n",
1704 session->s_mds);
1705 spin_lock(&session->s_cap_lock);
1706 list_splice(&tmp_list, &session->s_cap_releases);
1707 session->s_num_cap_releases += num_cap_releases;
Sage Weil2f2dc052009-10-06 11:31:09 -07001708 spin_unlock(&session->s_cap_lock);
1709}
1710
1711/*
1712 * requests
1713 */
1714
Yan, Zheng54008392014-03-29 13:41:15 +08001715int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
1716 struct inode *dir)
1717{
1718 struct ceph_inode_info *ci = ceph_inode(dir);
1719 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1720 struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options;
Yan, Zheng2a5beea2016-04-28 09:37:39 +08001721 size_t size = sizeof(struct ceph_mds_reply_dir_entry);
Yan, Zheng54008392014-03-29 13:41:15 +08001722 int order, num_entries;
1723
1724 spin_lock(&ci->i_ceph_lock);
1725 num_entries = ci->i_files + ci->i_subdirs;
1726 spin_unlock(&ci->i_ceph_lock);
1727 num_entries = max(num_entries, 1);
1728 num_entries = min(num_entries, opt->max_readdir);
1729
1730 order = get_order(size * num_entries);
1731 while (order >= 0) {
Yan, Zheng2a5beea2016-04-28 09:37:39 +08001732 rinfo->dir_entries = (void*)__get_free_pages(GFP_KERNEL |
1733 __GFP_NOWARN,
1734 order);
1735 if (rinfo->dir_entries)
Yan, Zheng54008392014-03-29 13:41:15 +08001736 break;
1737 order--;
1738 }
Yan, Zheng2a5beea2016-04-28 09:37:39 +08001739 if (!rinfo->dir_entries)
Yan, Zheng54008392014-03-29 13:41:15 +08001740 return -ENOMEM;
1741
1742 num_entries = (PAGE_SIZE << order) / size;
1743 num_entries = min(num_entries, opt->max_readdir);
1744
1745 rinfo->dir_buf_size = PAGE_SIZE << order;
1746 req->r_num_caps = num_entries + 1;
1747 req->r_args.readdir.max_entries = cpu_to_le32(num_entries);
1748 req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes);
1749 return 0;
1750}
1751
Sage Weil2f2dc052009-10-06 11:31:09 -07001752/*
1753 * Create an mds request.
1754 */
1755struct ceph_mds_request *
1756ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1757{
1758 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1759
1760 if (!req)
1761 return ERR_PTR(-ENOMEM);
1762
Sage Weilb4556392010-05-13 12:01:13 -07001763 mutex_init(&req->r_fill_mutex);
Yehuda Sadeh37151662010-06-17 16:16:12 -07001764 req->r_mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07001765 req->r_started = jiffies;
1766 req->r_resend_mds = -1;
1767 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08001768 INIT_LIST_HEAD(&req->r_unsafe_target_item);
Sage Weil2f2dc052009-10-06 11:31:09 -07001769 req->r_fmode = -1;
Sage Weil153c8e62009-12-07 12:31:09 -08001770 kref_init(&req->r_kref);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02001771 RB_CLEAR_NODE(&req->r_node);
Sage Weil2f2dc052009-10-06 11:31:09 -07001772 INIT_LIST_HEAD(&req->r_wait);
1773 init_completion(&req->r_completion);
1774 init_completion(&req->r_safe_completion);
1775 INIT_LIST_HEAD(&req->r_unsafe_item);
1776
Yan, Zheng56199012017-06-01 16:44:53 +08001777 req->r_stamp = timespec_trunc(current_kernel_time(), mdsc->fsc->sb->s_time_gran);
Sage Weilb8e69062014-05-21 17:41:08 -07001778
Sage Weil2f2dc052009-10-06 11:31:09 -07001779 req->r_op = op;
1780 req->r_direct_mode = mode;
1781 return req;
1782}
1783
1784/*
Sage Weil44ca18f2010-02-15 12:08:46 -08001785 * return oldest (lowest) request, tid in request tree, 0 if none.
Sage Weil2f2dc052009-10-06 11:31:09 -07001786 *
1787 * called under mdsc->mutex.
1788 */
Sage Weil44ca18f2010-02-15 12:08:46 -08001789static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1790{
1791 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1792 return NULL;
1793 return rb_entry(rb_first(&mdsc->request_tree),
1794 struct ceph_mds_request, r_node);
1795}
1796
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08001797static inline u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07001798{
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08001799 return mdsc->oldest_tid;
Sage Weil2f2dc052009-10-06 11:31:09 -07001800}
1801
1802/*
1803 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1804 * on build_path_from_dentry in fs/cifs/dir.c.
1805 *
1806 * If @stop_on_nosnap, generate path relative to the first non-snapped
1807 * inode.
1808 *
1809 * Encode hidden .snap dirs as a double /, i.e.
1810 * foo/.snap/bar -> foo//bar
1811 */
1812char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1813 int stop_on_nosnap)
1814{
1815 struct dentry *temp;
1816 char *path;
1817 int len, pos;
Al Viro1b71fe22011-07-16 23:43:58 -04001818 unsigned seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07001819
Markus Elfringd37b1d92017-08-20 20:22:02 +02001820 if (!dentry)
Sage Weil2f2dc052009-10-06 11:31:09 -07001821 return ERR_PTR(-EINVAL);
1822
1823retry:
1824 len = 0;
Al Viro1b71fe22011-07-16 23:43:58 -04001825 seq = read_seqbegin(&rename_lock);
1826 rcu_read_lock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001827 for (temp = dentry; !IS_ROOT(temp);) {
David Howells2b0143b2015-03-17 22:25:59 +00001828 struct inode *inode = d_inode(temp);
Sage Weil2f2dc052009-10-06 11:31:09 -07001829 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1830 len++; /* slash only */
1831 else if (stop_on_nosnap && inode &&
1832 ceph_snap(inode) == CEPH_NOSNAP)
1833 break;
1834 else
1835 len += 1 + temp->d_name.len;
1836 temp = temp->d_parent;
Sage Weil2f2dc052009-10-06 11:31:09 -07001837 }
Al Viro1b71fe22011-07-16 23:43:58 -04001838 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001839 if (len)
1840 len--; /* no leading '/' */
1841
1842 path = kmalloc(len+1, GFP_NOFS);
Markus Elfringd37b1d92017-08-20 20:22:02 +02001843 if (!path)
Sage Weil2f2dc052009-10-06 11:31:09 -07001844 return ERR_PTR(-ENOMEM);
1845 pos = len;
1846 path[pos] = 0; /* trailing null */
Al Viro1b71fe22011-07-16 23:43:58 -04001847 rcu_read_lock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001848 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
Al Viro1b71fe22011-07-16 23:43:58 -04001849 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07001850
Al Viro1b71fe22011-07-16 23:43:58 -04001851 spin_lock(&temp->d_lock);
David Howells2b0143b2015-03-17 22:25:59 +00001852 inode = d_inode(temp);
Sage Weil2f2dc052009-10-06 11:31:09 -07001853 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
Sage Weil104648a2010-03-18 10:14:30 -07001854 dout("build_path path+%d: %p SNAPDIR\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001855 pos, temp);
1856 } else if (stop_on_nosnap && inode &&
1857 ceph_snap(inode) == CEPH_NOSNAP) {
Yehuda Sadeh9d5a09e2011-12-13 09:57:44 -08001858 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001859 break;
1860 } else {
1861 pos -= temp->d_name.len;
Al Viro1b71fe22011-07-16 23:43:58 -04001862 if (pos < 0) {
1863 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001864 break;
Al Viro1b71fe22011-07-16 23:43:58 -04001865 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001866 strncpy(path + pos, temp->d_name.name,
1867 temp->d_name.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07001868 }
Al Viro1b71fe22011-07-16 23:43:58 -04001869 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001870 if (pos)
1871 path[--pos] = '/';
1872 temp = temp->d_parent;
Sage Weil2f2dc052009-10-06 11:31:09 -07001873 }
Al Viro1b71fe22011-07-16 23:43:58 -04001874 rcu_read_unlock();
1875 if (pos != 0 || read_seqretry(&rename_lock, seq)) {
Sage Weil104648a2010-03-18 10:14:30 -07001876 pr_err("build_path did not end path lookup where "
Sage Weil2f2dc052009-10-06 11:31:09 -07001877 "expected, namelen is %d, pos is %d\n", len, pos);
1878 /* presumably this is only possible if racing with a
1879 rename of one of the parent directories (we can not
1880 lock the dentries above us to prevent this, but
1881 retrying should be harmless) */
1882 kfree(path);
1883 goto retry;
1884 }
1885
David Howells2b0143b2015-03-17 22:25:59 +00001886 *base = ceph_ino(d_inode(temp));
Sage Weil2f2dc052009-10-06 11:31:09 -07001887 *plen = len;
Sage Weil104648a2010-03-18 10:14:30 -07001888 dout("build_path on %p %d built %llx '%.*s'\n",
Al Viro84d08fa2013-07-05 18:59:33 +04001889 dentry, d_count(dentry), *base, len, path);
Sage Weil2f2dc052009-10-06 11:31:09 -07001890 return path;
1891}
1892
Jeff Laytonfd36a712016-12-15 08:37:58 -05001893static int build_dentry_path(struct dentry *dentry, struct inode *dir,
Sage Weil2f2dc052009-10-06 11:31:09 -07001894 const char **ppath, int *ppathlen, u64 *pino,
1895 int *pfreepath)
1896{
1897 char *path;
1898
Jeff Laytonc6b0b652016-12-15 08:37:57 -05001899 rcu_read_lock();
Jeff Laytonfd36a712016-12-15 08:37:58 -05001900 if (!dir)
1901 dir = d_inode_rcu(dentry->d_parent);
Jeff Laytonc6b0b652016-12-15 08:37:57 -05001902 if (dir && ceph_snap(dir) == CEPH_NOSNAP) {
1903 *pino = ceph_ino(dir);
1904 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001905 *ppath = dentry->d_name.name;
1906 *ppathlen = dentry->d_name.len;
1907 return 0;
1908 }
Jeff Laytonc6b0b652016-12-15 08:37:57 -05001909 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001910 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1911 if (IS_ERR(path))
1912 return PTR_ERR(path);
1913 *ppath = path;
1914 *pfreepath = 1;
1915 return 0;
1916}
1917
1918static int build_inode_path(struct inode *inode,
1919 const char **ppath, int *ppathlen, u64 *pino,
1920 int *pfreepath)
1921{
1922 struct dentry *dentry;
1923 char *path;
1924
1925 if (ceph_snap(inode) == CEPH_NOSNAP) {
1926 *pino = ceph_ino(inode);
1927 *ppathlen = 0;
1928 return 0;
1929 }
1930 dentry = d_find_alias(inode);
1931 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1932 dput(dentry);
1933 if (IS_ERR(path))
1934 return PTR_ERR(path);
1935 *ppath = path;
1936 *pfreepath = 1;
1937 return 0;
1938}
1939
1940/*
1941 * request arguments may be specified via an inode *, a dentry *, or
1942 * an explicit ino+path.
1943 */
1944static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
Jeff Laytonfd36a712016-12-15 08:37:58 -05001945 struct inode *rdiri, const char *rpath,
1946 u64 rino, const char **ppath, int *pathlen,
Sage Weil2f2dc052009-10-06 11:31:09 -07001947 u64 *ino, int *freepath)
1948{
1949 int r = 0;
1950
1951 if (rinode) {
1952 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1953 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1954 ceph_snap(rinode));
1955 } else if (rdentry) {
Jeff Laytonfd36a712016-12-15 08:37:58 -05001956 r = build_dentry_path(rdentry, rdiri, ppath, pathlen, ino,
1957 freepath);
Sage Weil2f2dc052009-10-06 11:31:09 -07001958 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1959 *ppath);
Sage Weil795858d2011-08-15 13:02:37 -07001960 } else if (rpath || rino) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001961 *ino = rino;
1962 *ppath = rpath;
David Zafmanb0000562012-10-25 10:23:46 -07001963 *pathlen = rpath ? strlen(rpath) : 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001964 dout(" path %.*s\n", *pathlen, rpath);
1965 }
1966
1967 return r;
1968}
1969
1970/*
1971 * called under mdsc->mutex
1972 */
1973static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1974 struct ceph_mds_request *req,
Yan, Zheng6e6f0922015-02-27 08:54:08 +08001975 int mds, bool drop_cap_releases)
Sage Weil2f2dc052009-10-06 11:31:09 -07001976{
1977 struct ceph_msg *msg;
1978 struct ceph_mds_request_head *head;
1979 const char *path1 = NULL;
1980 const char *path2 = NULL;
1981 u64 ino1 = 0, ino2 = 0;
1982 int pathlen1 = 0, pathlen2 = 0;
1983 int freepath1 = 0, freepath2 = 0;
1984 int len;
1985 u16 releases;
1986 void *p, *end;
1987 int ret;
1988
1989 ret = set_request_path_attr(req->r_inode, req->r_dentry,
Jeff Layton3dd69aa2017-01-31 10:28:26 -05001990 req->r_parent, req->r_path1, req->r_ino1.ino,
Sage Weil2f2dc052009-10-06 11:31:09 -07001991 &path1, &pathlen1, &ino1, &freepath1);
1992 if (ret < 0) {
1993 msg = ERR_PTR(ret);
1994 goto out;
1995 }
1996
1997 ret = set_request_path_attr(NULL, req->r_old_dentry,
Jeff Laytonfd36a712016-12-15 08:37:58 -05001998 req->r_old_dentry_dir,
Sage Weil2f2dc052009-10-06 11:31:09 -07001999 req->r_path2, req->r_ino2.ino,
2000 &path2, &pathlen2, &ino2, &freepath2);
2001 if (ret < 0) {
2002 msg = ERR_PTR(ret);
2003 goto out_free1;
2004 }
2005
2006 len = sizeof(*head) +
Sage Weilb8e69062014-05-21 17:41:08 -07002007 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) +
Arnd Bergmann777d7382015-09-30 15:04:42 +02002008 sizeof(struct ceph_timespec);
Sage Weil2f2dc052009-10-06 11:31:09 -07002009
2010 /* calculate (max) length for cap releases */
2011 len += sizeof(struct ceph_mds_request_release) *
2012 (!!req->r_inode_drop + !!req->r_dentry_drop +
2013 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
2014 if (req->r_dentry_drop)
2015 len += req->r_dentry->d_name.len;
2016 if (req->r_old_dentry_drop)
2017 len += req->r_old_dentry->d_name.len;
2018
Sage Weilb61c2762011-08-09 15:03:46 -07002019 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07002020 if (!msg) {
2021 msg = ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -07002022 goto out_free2;
Sage Weila79832f2010-04-01 16:06:19 -07002023 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002024
John Spray7cfa0312014-10-30 17:15:26 +00002025 msg->hdr.version = cpu_to_le16(2);
Sage Weil6df058c2009-12-22 11:24:33 -08002026 msg->hdr.tid = cpu_to_le64(req->r_tid);
2027
Sage Weil2f2dc052009-10-06 11:31:09 -07002028 head = msg->front.iov_base;
2029 p = msg->front.iov_base + sizeof(*head);
2030 end = msg->front.iov_base + msg->front.iov_len;
2031
2032 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
2033 head->op = cpu_to_le32(req->r_op);
Eric W. Biedermanff3d0042013-01-31 04:01:53 -08002034 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid));
2035 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid));
Sage Weil2f2dc052009-10-06 11:31:09 -07002036 head->args = req->r_args;
2037
2038 ceph_encode_filepath(&p, end, ino1, path1);
2039 ceph_encode_filepath(&p, end, ino2, path2);
2040
Sage Weile979cf52010-07-15 14:58:39 -07002041 /* make note of release offset, in case we need to replay */
2042 req->r_request_release_offset = p - msg->front.iov_base;
2043
Sage Weil2f2dc052009-10-06 11:31:09 -07002044 /* cap releases */
2045 releases = 0;
2046 if (req->r_inode_drop)
2047 releases += ceph_encode_inode_release(&p,
David Howells2b0143b2015-03-17 22:25:59 +00002048 req->r_inode ? req->r_inode : d_inode(req->r_dentry),
Sage Weil2f2dc052009-10-06 11:31:09 -07002049 mds, req->r_inode_drop, req->r_inode_unless, 0);
2050 if (req->r_dentry_drop)
2051 releases += ceph_encode_dentry_release(&p, req->r_dentry,
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002052 req->r_parent, mds, req->r_dentry_drop,
Jeff Laytonca6c8ae2016-12-15 08:37:59 -05002053 req->r_dentry_unless);
Sage Weil2f2dc052009-10-06 11:31:09 -07002054 if (req->r_old_dentry_drop)
2055 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
Jeff Laytonca6c8ae2016-12-15 08:37:59 -05002056 req->r_old_dentry_dir, mds,
2057 req->r_old_dentry_drop,
2058 req->r_old_dentry_unless);
Sage Weil2f2dc052009-10-06 11:31:09 -07002059 if (req->r_old_inode_drop)
2060 releases += ceph_encode_inode_release(&p,
David Howells2b0143b2015-03-17 22:25:59 +00002061 d_inode(req->r_old_dentry),
Sage Weil2f2dc052009-10-06 11:31:09 -07002062 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002063
2064 if (drop_cap_releases) {
2065 releases = 0;
2066 p = msg->front.iov_base + req->r_request_release_offset;
2067 }
2068
Sage Weil2f2dc052009-10-06 11:31:09 -07002069 head->num_releases = cpu_to_le16(releases);
2070
Sage Weilb8e69062014-05-21 17:41:08 -07002071 /* time stamp */
Yan, Zheng1f041a82015-01-13 15:20:52 +08002072 {
2073 struct ceph_timespec ts;
2074 ceph_encode_timespec(&ts, &req->r_stamp);
2075 ceph_encode_copy(&p, &ts, sizeof(ts));
2076 }
Sage Weilb8e69062014-05-21 17:41:08 -07002077
Sage Weil2f2dc052009-10-06 11:31:09 -07002078 BUG_ON(p > end);
2079 msg->front.iov_len = p - msg->front.iov_base;
2080 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2081
Yan, Zheng25e6bae2014-09-16 19:15:28 +08002082 if (req->r_pagelist) {
2083 struct ceph_pagelist *pagelist = req->r_pagelist;
Elena Reshetova0e1a5ee2017-03-17 14:10:29 +02002084 refcount_inc(&pagelist->refcnt);
Yan, Zheng25e6bae2014-09-16 19:15:28 +08002085 ceph_msg_data_add_pagelist(msg, pagelist);
2086 msg->hdr.data_len = cpu_to_le32(pagelist->length);
2087 } else {
2088 msg->hdr.data_len = 0;
Alex Elderebf18f42013-03-04 22:29:57 -06002089 }
Alex Elder02afca62013-02-14 12:16:43 -06002090
Sage Weil2f2dc052009-10-06 11:31:09 -07002091 msg->hdr.data_off = cpu_to_le16(0);
2092
2093out_free2:
2094 if (freepath2)
2095 kfree((char *)path2);
2096out_free1:
2097 if (freepath1)
2098 kfree((char *)path1);
2099out:
2100 return msg;
2101}
2102
2103/*
2104 * called under mdsc->mutex if error, under no mutex if
2105 * success.
2106 */
2107static void complete_request(struct ceph_mds_client *mdsc,
2108 struct ceph_mds_request *req)
2109{
2110 if (req->r_callback)
2111 req->r_callback(mdsc, req);
2112 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002113 complete_all(&req->r_completion);
Sage Weil2f2dc052009-10-06 11:31:09 -07002114}
2115
2116/*
2117 * called under mdsc->mutex
2118 */
2119static int __prepare_send_request(struct ceph_mds_client *mdsc,
2120 struct ceph_mds_request *req,
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002121 int mds, bool drop_cap_releases)
Sage Weil2f2dc052009-10-06 11:31:09 -07002122{
2123 struct ceph_mds_request_head *rhead;
2124 struct ceph_msg *msg;
2125 int flags = 0;
2126
Sage Weil2f2dc052009-10-06 11:31:09 -07002127 req->r_attempts++;
Greg Farnume55b71f2010-06-22 15:58:01 -07002128 if (req->r_inode) {
2129 struct ceph_cap *cap =
2130 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
2131
2132 if (cap)
2133 req->r_sent_on_mseq = cap->mseq;
2134 else
2135 req->r_sent_on_mseq = -1;
2136 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002137 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
2138 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
2139
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002140 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002141 void *p;
Sage Weil01a92f12010-07-15 13:24:32 -07002142 /*
2143 * Replay. Do not regenerate message (and rebuild
2144 * paths, etc.); just use the original message.
2145 * Rebuilding paths will break for renames because
2146 * d_move mangles the src name.
2147 */
2148 msg = req->r_request;
2149 rhead = msg->front.iov_base;
2150
2151 flags = le32_to_cpu(rhead->flags);
2152 flags |= CEPH_MDS_FLAG_REPLAY;
2153 rhead->flags = cpu_to_le32(flags);
2154
2155 if (req->r_target_inode)
2156 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
2157
2158 rhead->num_retry = req->r_attempts - 1;
Sage Weile979cf52010-07-15 14:58:39 -07002159
2160 /* remove cap/dentry releases from message */
2161 rhead->num_releases = 0;
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002162
2163 /* time stamp */
2164 p = msg->front.iov_base + req->r_request_release_offset;
Yan, Zheng1f041a82015-01-13 15:20:52 +08002165 {
2166 struct ceph_timespec ts;
2167 ceph_encode_timespec(&ts, &req->r_stamp);
2168 ceph_encode_copy(&p, &ts, sizeof(ts));
2169 }
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002170
2171 msg->front.iov_len = p - msg->front.iov_base;
2172 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
Sage Weil01a92f12010-07-15 13:24:32 -07002173 return 0;
2174 }
2175
Sage Weil2f2dc052009-10-06 11:31:09 -07002176 if (req->r_request) {
2177 ceph_msg_put(req->r_request);
2178 req->r_request = NULL;
2179 }
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002180 msg = create_request_message(mdsc, req, mds, drop_cap_releases);
Sage Weil2f2dc052009-10-06 11:31:09 -07002181 if (IS_ERR(msg)) {
Sage Weile1518c72010-05-13 11:19:06 -07002182 req->r_err = PTR_ERR(msg);
Sage Weila79832f2010-04-01 16:06:19 -07002183 return PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002184 }
2185 req->r_request = msg;
2186
2187 rhead = msg->front.iov_base;
Sage Weil2f2dc052009-10-06 11:31:09 -07002188 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002189 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Sage Weil2f2dc052009-10-06 11:31:09 -07002190 flags |= CEPH_MDS_FLAG_REPLAY;
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002191 if (req->r_parent)
Sage Weil2f2dc052009-10-06 11:31:09 -07002192 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
2193 rhead->flags = cpu_to_le32(flags);
2194 rhead->num_fwd = req->r_num_fwd;
2195 rhead->num_retry = req->r_attempts - 1;
Sage Weil01a92f12010-07-15 13:24:32 -07002196 rhead->ino = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002197
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002198 dout(" r_parent = %p\n", req->r_parent);
Sage Weil2f2dc052009-10-06 11:31:09 -07002199 return 0;
2200}
2201
2202/*
2203 * send request, or put it on the appropriate wait list.
2204 */
2205static int __do_request(struct ceph_mds_client *mdsc,
2206 struct ceph_mds_request *req)
2207{
2208 struct ceph_mds_session *session = NULL;
2209 int mds = -1;
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002210 int err = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002211
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002212 if (req->r_err || test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
2213 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
Yan, Zhengeb1b8af2013-09-26 14:25:36 +08002214 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002215 goto out;
Yan, Zhengeb1b8af2013-09-26 14:25:36 +08002216 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002217
2218 if (req->r_timeout &&
2219 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
2220 dout("do_request timed out\n");
2221 err = -EIO;
2222 goto finish;
2223 }
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01002224 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002225 dout("do_request forced umount\n");
2226 err = -EIO;
2227 goto finish;
2228 }
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01002229 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_MOUNTING) {
Yan, Zhenge9e427f2016-11-10 16:02:06 +08002230 if (mdsc->mdsmap_err) {
2231 err = mdsc->mdsmap_err;
2232 dout("do_request mdsmap err %d\n", err);
2233 goto finish;
2234 }
Yan, Zhengcc8e8342017-01-04 16:21:58 +08002235 if (mdsc->mdsmap->m_epoch == 0) {
2236 dout("do_request no mdsmap, waiting for map\n");
2237 list_add(&req->r_wait, &mdsc->waiting_for_map);
2238 goto finish;
2239 }
Yan, Zhenge9e427f2016-11-10 16:02:06 +08002240 if (!(mdsc->fsc->mount_options->flags &
2241 CEPH_MOUNT_OPT_MOUNTWAIT) &&
2242 !ceph_mdsmap_is_cluster_available(mdsc->mdsmap)) {
2243 err = -ENOENT;
2244 pr_info("probably no mds server is up\n");
2245 goto finish;
2246 }
2247 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002248
Sage Weildc69e2e2010-11-02 13:49:00 -07002249 put_request_session(req);
2250
Sage Weil2f2dc052009-10-06 11:31:09 -07002251 mds = __choose_mds(mdsc, req);
2252 if (mds < 0 ||
2253 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
2254 dout("do_request no mds or not active, waiting for map\n");
2255 list_add(&req->r_wait, &mdsc->waiting_for_map);
2256 goto out;
2257 }
2258
2259 /* get, open session */
2260 session = __ceph_lookup_mds_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07002261 if (!session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002262 session = register_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07002263 if (IS_ERR(session)) {
2264 err = PTR_ERR(session);
2265 goto finish;
2266 }
2267 }
Sage Weildc69e2e2010-11-02 13:49:00 -07002268 req->r_session = get_session(session);
2269
Sage Weil2f2dc052009-10-06 11:31:09 -07002270 dout("do_request mds%d session %p state %s\n", mds, session,
John Spraya687eca2014-09-19 13:51:08 +01002271 ceph_session_state_name(session->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07002272 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
2273 session->s_state != CEPH_MDS_SESSION_HUNG) {
Yan, Zhengfcff4152016-09-14 16:39:51 +08002274 if (session->s_state == CEPH_MDS_SESSION_REJECTED) {
2275 err = -EACCES;
2276 goto out_session;
2277 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002278 if (session->s_state == CEPH_MDS_SESSION_NEW ||
2279 session->s_state == CEPH_MDS_SESSION_CLOSING)
2280 __open_session(mdsc, session);
2281 list_add(&req->r_wait, &session->s_waiting);
2282 goto out_session;
2283 }
2284
2285 /* send request */
Sage Weil2f2dc052009-10-06 11:31:09 -07002286 req->r_resend_mds = -1; /* forget any previous mds hint */
2287
2288 if (req->r_request_started == 0) /* note request start time */
2289 req->r_request_started = jiffies;
2290
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002291 err = __prepare_send_request(mdsc, req, mds, false);
Sage Weil2f2dc052009-10-06 11:31:09 -07002292 if (!err) {
2293 ceph_msg_get(req->r_request);
2294 ceph_con_send(&session->s_con, req->r_request);
2295 }
2296
2297out_session:
2298 ceph_put_mds_session(session);
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002299finish:
2300 if (err) {
2301 dout("__do_request early error %d\n", err);
2302 req->r_err = err;
2303 complete_request(mdsc, req);
2304 __unregister_request(mdsc, req);
2305 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002306out:
2307 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002308}
2309
2310/*
2311 * called under mdsc->mutex
2312 */
2313static void __wake_requests(struct ceph_mds_client *mdsc,
2314 struct list_head *head)
2315{
Yan, Zhenged75ec22012-11-19 10:49:06 +08002316 struct ceph_mds_request *req;
2317 LIST_HEAD(tmp_list);
Sage Weil2f2dc052009-10-06 11:31:09 -07002318
Yan, Zhenged75ec22012-11-19 10:49:06 +08002319 list_splice_init(head, &tmp_list);
2320
2321 while (!list_empty(&tmp_list)) {
2322 req = list_entry(tmp_list.next,
2323 struct ceph_mds_request, r_wait);
Sage Weil2f2dc052009-10-06 11:31:09 -07002324 list_del_init(&req->r_wait);
Sage Weil7971bd92013-05-01 21:15:58 -07002325 dout(" wake request %p tid %llu\n", req, req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002326 __do_request(mdsc, req);
2327 }
2328}
2329
2330/*
2331 * Wake up threads with requests pending for @mds, so that they can
Sage Weil29790f22010-03-18 14:45:05 -07002332 * resubmit their requests to a possibly different mds.
Sage Weil2f2dc052009-10-06 11:31:09 -07002333 */
Sage Weil29790f22010-03-18 14:45:05 -07002334static void kick_requests(struct ceph_mds_client *mdsc, int mds)
Sage Weil2f2dc052009-10-06 11:31:09 -07002335{
Sage Weil44ca18f2010-02-15 12:08:46 -08002336 struct ceph_mds_request *req;
Yan, Zheng282c1052014-07-30 10:12:47 +08002337 struct rb_node *p = rb_first(&mdsc->request_tree);
Sage Weil2f2dc052009-10-06 11:31:09 -07002338
2339 dout("kick_requests mds%d\n", mds);
Yan, Zheng282c1052014-07-30 10:12:47 +08002340 while (p) {
Sage Weil44ca18f2010-02-15 12:08:46 -08002341 req = rb_entry(p, struct ceph_mds_request, r_node);
Yan, Zheng282c1052014-07-30 10:12:47 +08002342 p = rb_next(p);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002343 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Sage Weil44ca18f2010-02-15 12:08:46 -08002344 continue;
Yan, Zheng3de22be2015-02-04 14:26:22 +08002345 if (req->r_attempts > 0)
2346 continue; /* only new requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08002347 if (req->r_session &&
2348 req->r_session->s_mds == mds) {
2349 dout(" kicking tid %llu\n", req->r_tid);
Yan, Zheng03974e82014-09-11 14:28:56 +08002350 list_del_init(&req->r_wait);
Sage Weil44ca18f2010-02-15 12:08:46 -08002351 __do_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002352 }
2353 }
2354}
2355
2356void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
2357 struct ceph_mds_request *req)
2358{
2359 dout("submit_request on %p\n", req);
2360 mutex_lock(&mdsc->mutex);
2361 __register_request(mdsc, req, NULL);
2362 __do_request(mdsc, req);
2363 mutex_unlock(&mdsc->mutex);
2364}
2365
2366/*
2367 * Synchrously perform an mds request. Take care of all of the
2368 * session setup, forwarding, retry details.
2369 */
2370int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
2371 struct inode *dir,
2372 struct ceph_mds_request *req)
2373{
2374 int err;
2375
2376 dout("do_request on %p\n", req);
2377
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002378 /* take CAP_PIN refs for r_inode, r_parent, r_old_dentry */
Sage Weil2f2dc052009-10-06 11:31:09 -07002379 if (req->r_inode)
2380 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002381 if (req->r_parent)
2382 ceph_get_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
Sage Weil844d87c2013-02-05 13:40:09 -08002383 if (req->r_old_dentry_dir)
Sage Weil41b02e12011-07-26 11:31:14 -07002384 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
2385 CEPH_CAP_PIN);
Sage Weil2f2dc052009-10-06 11:31:09 -07002386
2387 /* issue */
2388 mutex_lock(&mdsc->mutex);
2389 __register_request(mdsc, req, dir);
2390 __do_request(mdsc, req);
2391
Sage Weile1518c72010-05-13 11:19:06 -07002392 if (req->r_err) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002393 err = req->r_err;
Sage Weile1518c72010-05-13 11:19:06 -07002394 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07002395 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002396
Sage Weile1518c72010-05-13 11:19:06 -07002397 /* wait */
2398 mutex_unlock(&mdsc->mutex);
2399 dout("do_request waiting\n");
Ilya Dryomov5be73032015-05-19 12:05:38 +03002400 if (!req->r_timeout && req->r_wait_for_completion) {
Yan, Zheng9280be22014-10-14 10:33:35 +08002401 err = req->r_wait_for_completion(mdsc, req);
Sage Weile1518c72010-05-13 11:19:06 -07002402 } else {
Ilya Dryomov5be73032015-05-19 12:05:38 +03002403 long timeleft = wait_for_completion_killable_timeout(
2404 &req->r_completion,
2405 ceph_timeout_jiffies(req->r_timeout));
2406 if (timeleft > 0)
2407 err = 0;
2408 else if (!timeleft)
2409 err = -EIO; /* timed out */
2410 else
2411 err = timeleft; /* killed */
Sage Weile1518c72010-05-13 11:19:06 -07002412 }
2413 dout("do_request waited, got %d\n", err);
2414 mutex_lock(&mdsc->mutex);
2415
2416 /* only abort if we didn't race with a real reply */
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002417 if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
Sage Weile1518c72010-05-13 11:19:06 -07002418 err = le32_to_cpu(req->r_reply_info.head->result);
2419 } else if (err < 0) {
2420 dout("aborted request %lld with %d\n", req->r_tid, err);
Sage Weilb4556392010-05-13 12:01:13 -07002421
2422 /*
2423 * ensure we aren't running concurrently with
2424 * ceph_fill_trace or ceph_readdir_prepopulate, which
2425 * rely on locks (dir mutex) held by our caller.
2426 */
2427 mutex_lock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07002428 req->r_err = err;
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002429 set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
Sage Weilb4556392010-05-13 12:01:13 -07002430 mutex_unlock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07002431
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002432 if (req->r_parent &&
Sage Weil167c9e32010-05-14 10:02:57 -07002433 (req->r_op & CEPH_MDS_OP_WRITE))
2434 ceph_invalidate_dir_request(req);
Sage Weile1518c72010-05-13 11:19:06 -07002435 } else {
2436 err = req->r_err;
2437 }
2438
2439out:
2440 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002441 dout("do_request %p done, result %d\n", req, err);
2442 return err;
2443}
2444
2445/*
Yan, Zheng2f276c52013-03-13 19:44:32 +08002446 * Invalidate dir's completeness, dentry lease state on an aborted MDS
Sage Weil167c9e32010-05-14 10:02:57 -07002447 * namespace request.
2448 */
2449void ceph_invalidate_dir_request(struct ceph_mds_request *req)
2450{
Yan, Zheng8d8f3712017-11-24 11:51:32 +08002451 struct inode *dir = req->r_parent;
2452 struct inode *old_dir = req->r_old_dentry_dir;
Sage Weil167c9e32010-05-14 10:02:57 -07002453
Yan, Zheng8d8f3712017-11-24 11:51:32 +08002454 dout("invalidate_dir_request %p %p (complete, lease(s))\n", dir, old_dir);
Sage Weil167c9e32010-05-14 10:02:57 -07002455
Yan, Zheng8d8f3712017-11-24 11:51:32 +08002456 ceph_dir_clear_complete(dir);
2457 if (old_dir)
2458 ceph_dir_clear_complete(old_dir);
Sage Weil167c9e32010-05-14 10:02:57 -07002459 if (req->r_dentry)
2460 ceph_invalidate_dentry_lease(req->r_dentry);
2461 if (req->r_old_dentry)
2462 ceph_invalidate_dentry_lease(req->r_old_dentry);
2463}
2464
2465/*
Sage Weil2f2dc052009-10-06 11:31:09 -07002466 * Handle mds reply.
2467 *
2468 * We take the session mutex and parse and process the reply immediately.
2469 * This preserves the logical ordering of replies, capabilities, etc., sent
2470 * by the MDS as they are applied to our local cache.
2471 */
2472static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2473{
2474 struct ceph_mds_client *mdsc = session->s_mdsc;
2475 struct ceph_mds_request *req;
2476 struct ceph_mds_reply_head *head = msg->front.iov_base;
2477 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
Yan, Zheng982d6012014-12-23 15:30:54 +08002478 struct ceph_snap_realm *realm;
Sage Weil2f2dc052009-10-06 11:31:09 -07002479 u64 tid;
2480 int err, result;
Sage Weil2600d2d2010-02-22 15:12:16 -08002481 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002482
Sage Weil2f2dc052009-10-06 11:31:09 -07002483 if (msg->front.iov_len < sizeof(*head)) {
2484 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002485 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002486 return;
2487 }
2488
2489 /* get request, session */
Sage Weil6df058c2009-12-22 11:24:33 -08002490 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002491 mutex_lock(&mdsc->mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02002492 req = lookup_get_request(mdsc, tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002493 if (!req) {
2494 dout("handle_reply on unknown tid %llu\n", tid);
2495 mutex_unlock(&mdsc->mutex);
2496 return;
2497 }
2498 dout("handle_reply %p\n", req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002499
2500 /* correct session? */
Sage Weild96d6042010-03-20 20:50:58 -07002501 if (req->r_session != session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002502 pr_err("mdsc_handle_reply got %llu on session mds%d"
2503 " not mds%d\n", tid, session->s_mds,
2504 req->r_session ? req->r_session->s_mds : -1);
2505 mutex_unlock(&mdsc->mutex);
2506 goto out;
2507 }
2508
2509 /* dup? */
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002510 if ((test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags) && !head->safe) ||
2511 (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags) && head->safe)) {
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07002512 pr_warn("got a dup %s reply on %llu from mds%d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002513 head->safe ? "safe" : "unsafe", tid, mds);
2514 mutex_unlock(&mdsc->mutex);
2515 goto out;
2516 }
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002517 if (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags)) {
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07002518 pr_warn("got unsafe after safe on %llu from mds%d\n",
Sage Weil85792d0d2010-05-13 09:06:02 -07002519 tid, mds);
2520 mutex_unlock(&mdsc->mutex);
2521 goto out;
2522 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002523
2524 result = le32_to_cpu(head->result);
2525
2526 /*
Greg Farnume55b71f2010-06-22 15:58:01 -07002527 * Handle an ESTALE
2528 * if we're not talking to the authority, send to them
2529 * if the authority has changed while we weren't looking,
2530 * send to new authority
2531 * Otherwise we just have to return an ESTALE
Sage Weil2f2dc052009-10-06 11:31:09 -07002532 */
2533 if (result == -ESTALE) {
Greg Farnume55b71f2010-06-22 15:58:01 -07002534 dout("got ESTALE on request %llu", req->r_tid);
Yan, Zheng51da8e82014-07-14 10:48:29 +08002535 req->r_resend_mds = -1;
Yan, Zhengca18bed2013-11-22 14:21:44 +08002536 if (req->r_direct_mode != USE_AUTH_MDS) {
Greg Farnume55b71f2010-06-22 15:58:01 -07002537 dout("not using auth, setting for that now");
2538 req->r_direct_mode = USE_AUTH_MDS;
Sage Weil2f2dc052009-10-06 11:31:09 -07002539 __do_request(mdsc, req);
2540 mutex_unlock(&mdsc->mutex);
2541 goto out;
Greg Farnume55b71f2010-06-22 15:58:01 -07002542 } else {
Yan, Zhengca18bed2013-11-22 14:21:44 +08002543 int mds = __choose_mds(mdsc, req);
2544 if (mds >= 0 && mds != req->r_session->s_mds) {
2545 dout("but auth changed, so resending");
Greg Farnume55b71f2010-06-22 15:58:01 -07002546 __do_request(mdsc, req);
2547 mutex_unlock(&mdsc->mutex);
2548 goto out;
2549 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002550 }
Greg Farnume55b71f2010-06-22 15:58:01 -07002551 dout("have to return ESTALE on request %llu", req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002552 }
2553
Greg Farnume55b71f2010-06-22 15:58:01 -07002554
Sage Weil2f2dc052009-10-06 11:31:09 -07002555 if (head->safe) {
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002556 set_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags);
Sage Weil2f2dc052009-10-06 11:31:09 -07002557 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002558
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002559 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002560 /*
2561 * We already handled the unsafe response, now do the
2562 * cleanup. No need to examine the response; the MDS
2563 * doesn't include any result info in the safe
2564 * response. And even if it did, there is nothing
2565 * useful we could do with a revised return value.
2566 */
2567 dout("got safe reply %llu, mds%d\n", tid, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002568
2569 /* last unsafe request during umount? */
Sage Weil44ca18f2010-02-15 12:08:46 -08002570 if (mdsc->stopping && !__get_oldest_req(mdsc))
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002571 complete_all(&mdsc->safe_umount_waiters);
Sage Weil2f2dc052009-10-06 11:31:09 -07002572 mutex_unlock(&mdsc->mutex);
2573 goto out;
2574 }
Sage Weile1518c72010-05-13 11:19:06 -07002575 } else {
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002576 set_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags);
Sage Weil2f2dc052009-10-06 11:31:09 -07002577 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
Yan, Zheng4c06ace2015-10-27 17:18:00 +08002578 if (req->r_unsafe_dir) {
2579 struct ceph_inode_info *ci =
2580 ceph_inode(req->r_unsafe_dir);
2581 spin_lock(&ci->i_unsafe_lock);
2582 list_add_tail(&req->r_unsafe_dir_item,
2583 &ci->i_unsafe_dirops);
2584 spin_unlock(&ci->i_unsafe_lock);
2585 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002586 }
2587
2588 dout("handle_reply tid %lld result %d\n", tid, result);
2589 rinfo = &req->r_reply_info;
Sage Weil14303d22010-12-14 17:37:52 -08002590 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
Sage Weil2f2dc052009-10-06 11:31:09 -07002591 mutex_unlock(&mdsc->mutex);
2592
2593 mutex_lock(&session->s_mutex);
2594 if (err < 0) {
Herb Shiu25933ab2010-12-01 14:14:38 -08002595 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002596 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002597 goto out_err;
2598 }
2599
2600 /* snap trace */
Yan, Zheng982d6012014-12-23 15:30:54 +08002601 realm = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07002602 if (rinfo->snapblob_len) {
2603 down_write(&mdsc->snap_rwsem);
2604 ceph_update_snap_trace(mdsc, rinfo->snapblob,
Yan, Zheng982d6012014-12-23 15:30:54 +08002605 rinfo->snapblob + rinfo->snapblob_len,
2606 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP,
2607 &realm);
Sage Weil2f2dc052009-10-06 11:31:09 -07002608 downgrade_write(&mdsc->snap_rwsem);
2609 } else {
2610 down_read(&mdsc->snap_rwsem);
2611 }
2612
2613 /* insert trace into our cache */
Sage Weilb4556392010-05-13 12:01:13 -07002614 mutex_lock(&req->r_fill_mutex);
Yan, Zheng315f2402016-03-07 10:34:50 +08002615 current->journal_info = req;
Jeff Laytonf5a03b02017-01-31 11:06:13 -05002616 err = ceph_fill_trace(mdsc->fsc->sb, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002617 if (err == 0) {
Sam Lang6e8575f2012-12-28 09:56:46 -08002618 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
Yan, Zheng81c6aea2013-09-18 09:44:13 +08002619 req->r_op == CEPH_MDS_OP_LSSNAP))
Sage Weil2f2dc052009-10-06 11:31:09 -07002620 ceph_readdir_prepopulate(req, req->r_session);
Yehuda Sadeh37151662010-06-17 16:16:12 -07002621 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
Sage Weil2f2dc052009-10-06 11:31:09 -07002622 }
Yan, Zheng315f2402016-03-07 10:34:50 +08002623 current->journal_info = NULL;
Sage Weilb4556392010-05-13 12:01:13 -07002624 mutex_unlock(&req->r_fill_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002625
2626 up_read(&mdsc->snap_rwsem);
Yan, Zheng982d6012014-12-23 15:30:54 +08002627 if (realm)
2628 ceph_put_snap_realm(mdsc, realm);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002629
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002630 if (err == 0 && req->r_target_inode &&
2631 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002632 struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
2633 spin_lock(&ci->i_unsafe_lock);
2634 list_add_tail(&req->r_unsafe_target_item, &ci->i_unsafe_iops);
2635 spin_unlock(&ci->i_unsafe_lock);
2636 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002637out_err:
Sage Weile1518c72010-05-13 11:19:06 -07002638 mutex_lock(&mdsc->mutex);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002639 if (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
Sage Weile1518c72010-05-13 11:19:06 -07002640 if (err) {
2641 req->r_err = err;
2642 } else {
Jianpeng Ma5fdb1382015-08-18 10:30:38 +08002643 req->r_reply = ceph_msg_get(msg);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002644 set_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags);
Sage Weile1518c72010-05-13 11:19:06 -07002645 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002646 } else {
Sage Weile1518c72010-05-13 11:19:06 -07002647 dout("reply arrived after request %lld was aborted\n", tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002648 }
Sage Weile1518c72010-05-13 11:19:06 -07002649 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002650
Sage Weil2f2dc052009-10-06 11:31:09 -07002651 mutex_unlock(&session->s_mutex);
2652
2653 /* kick calling process */
2654 complete_request(mdsc, req);
2655out:
2656 ceph_mdsc_put_request(req);
2657 return;
2658}
2659
2660
2661
2662/*
2663 * handle mds notification that our request has been forwarded.
2664 */
Sage Weil2600d2d2010-02-22 15:12:16 -08002665static void handle_forward(struct ceph_mds_client *mdsc,
2666 struct ceph_mds_session *session,
2667 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002668{
2669 struct ceph_mds_request *req;
Sage Weila1ea7872010-02-23 14:02:44 -08002670 u64 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002671 u32 next_mds;
2672 u32 fwd_seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002673 int err = -EINVAL;
2674 void *p = msg->front.iov_base;
2675 void *end = p + msg->front.iov_len;
Sage Weil2f2dc052009-10-06 11:31:09 -07002676
Sage Weila1ea7872010-02-23 14:02:44 -08002677 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002678 next_mds = ceph_decode_32(&p);
2679 fwd_seq = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07002680
2681 mutex_lock(&mdsc->mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02002682 req = lookup_get_request(mdsc, tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002683 if (!req) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07002684 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002685 goto out; /* dup reply? */
2686 }
2687
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002688 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07002689 dout("forward tid %llu aborted, unregistering\n", tid);
2690 __unregister_request(mdsc, req);
2691 } else if (fwd_seq <= req->r_num_fwd) {
2692 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002693 tid, next_mds, req->r_num_fwd, fwd_seq);
2694 } else {
2695 /* resend. forward race not possible; mds would drop */
Sage Weil2a8e5e32010-05-28 16:43:16 -07002696 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2697 BUG_ON(req->r_err);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002698 BUG_ON(test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags));
Yan, Zheng3de22be2015-02-04 14:26:22 +08002699 req->r_attempts = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002700 req->r_num_fwd = fwd_seq;
2701 req->r_resend_mds = next_mds;
2702 put_request_session(req);
2703 __do_request(mdsc, req);
2704 }
2705 ceph_mdsc_put_request(req);
2706out:
2707 mutex_unlock(&mdsc->mutex);
2708 return;
2709
2710bad:
2711 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2712}
2713
2714/*
2715 * handle a mds session control message
2716 */
2717static void handle_session(struct ceph_mds_session *session,
2718 struct ceph_msg *msg)
2719{
2720 struct ceph_mds_client *mdsc = session->s_mdsc;
2721 u32 op;
2722 u64 seq;
Sage Weil2600d2d2010-02-22 15:12:16 -08002723 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002724 struct ceph_mds_session_head *h = msg->front.iov_base;
2725 int wake = 0;
2726
Sage Weil2f2dc052009-10-06 11:31:09 -07002727 /* decode */
2728 if (msg->front.iov_len != sizeof(*h))
2729 goto bad;
2730 op = le32_to_cpu(h->op);
2731 seq = le64_to_cpu(h->seq);
2732
2733 mutex_lock(&mdsc->mutex);
Yan, Zheng0a07fc82017-03-29 15:30:24 +08002734 if (op == CEPH_SESSION_CLOSE) {
2735 get_session(session);
Sage Weil2600d2d2010-02-22 15:12:16 -08002736 __unregister_session(mdsc, session);
Yan, Zheng0a07fc82017-03-29 15:30:24 +08002737 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002738 /* FIXME: this ttl calculation is generous */
2739 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2740 mutex_unlock(&mdsc->mutex);
2741
2742 mutex_lock(&session->s_mutex);
2743
2744 dout("handle_session mds%d %s %p state %s seq %llu\n",
2745 mds, ceph_session_op_name(op), session,
John Spraya687eca2014-09-19 13:51:08 +01002746 ceph_session_state_name(session->s_state), seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002747
2748 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2749 session->s_state = CEPH_MDS_SESSION_OPEN;
2750 pr_info("mds%d came back\n", session->s_mds);
2751 }
2752
2753 switch (op) {
2754 case CEPH_SESSION_OPEN:
Sage Weil29790f22010-03-18 14:45:05 -07002755 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2756 pr_info("mds%d reconnect success\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002757 session->s_state = CEPH_MDS_SESSION_OPEN;
2758 renewed_caps(mdsc, session, 0);
2759 wake = 1;
2760 if (mdsc->stopping)
2761 __close_session(mdsc, session);
2762 break;
2763
2764 case CEPH_SESSION_RENEWCAPS:
2765 if (session->s_renew_seq == seq)
2766 renewed_caps(mdsc, session, 1);
2767 break;
2768
2769 case CEPH_SESSION_CLOSE:
Sage Weil29790f22010-03-18 14:45:05 -07002770 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2771 pr_info("mds%d reconnect denied\n", session->s_mds);
Yan, Zheng1c841a92015-03-24 20:15:36 +08002772 cleanup_session_requests(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002773 remove_session_caps(session);
Yan, Zheng656e4382014-09-11 14:25:18 +08002774 wake = 2; /* for good measure */
Sage Weilf3c60c52010-08-11 14:51:23 -07002775 wake_up_all(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002776 break;
2777
2778 case CEPH_SESSION_STALE:
2779 pr_info("mds%d caps went stale, renewing\n",
2780 session->s_mds);
Alex Elderd8fb02a2012-01-12 17:48:10 -08002781 spin_lock(&session->s_gen_ttl_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002782 session->s_cap_gen++;
Alex Elder1ce208a2012-01-12 17:48:11 -08002783 session->s_cap_ttl = jiffies - 1;
Alex Elderd8fb02a2012-01-12 17:48:10 -08002784 spin_unlock(&session->s_gen_ttl_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002785 send_renew_caps(mdsc, session);
2786 break;
2787
2788 case CEPH_SESSION_RECALL_STATE:
Zhi Zhange30ee582018-01-24 21:24:33 +08002789 ceph_trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
Sage Weil2f2dc052009-10-06 11:31:09 -07002790 break;
2791
Yan, Zheng186e4f72013-11-22 14:48:37 +08002792 case CEPH_SESSION_FLUSHMSG:
2793 send_flushmsg_ack(mdsc, session, seq);
2794 break;
2795
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08002796 case CEPH_SESSION_FORCE_RO:
2797 dout("force_session_readonly %p\n", session);
2798 spin_lock(&session->s_cap_lock);
2799 session->s_readonly = true;
2800 spin_unlock(&session->s_cap_lock);
2801 wake_up_session_caps(session, 0);
2802 break;
2803
Yan, Zhengfcff4152016-09-14 16:39:51 +08002804 case CEPH_SESSION_REJECT:
2805 WARN_ON(session->s_state != CEPH_MDS_SESSION_OPENING);
2806 pr_info("mds%d rejected session\n", session->s_mds);
2807 session->s_state = CEPH_MDS_SESSION_REJECTED;
2808 cleanup_session_requests(mdsc, session);
2809 remove_session_caps(session);
2810 wake = 2; /* for good measure */
2811 break;
2812
Sage Weil2f2dc052009-10-06 11:31:09 -07002813 default:
2814 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2815 WARN_ON(1);
2816 }
2817
2818 mutex_unlock(&session->s_mutex);
2819 if (wake) {
2820 mutex_lock(&mdsc->mutex);
2821 __wake_requests(mdsc, &session->s_waiting);
Yan, Zheng656e4382014-09-11 14:25:18 +08002822 if (wake == 2)
2823 kick_requests(mdsc, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002824 mutex_unlock(&mdsc->mutex);
2825 }
Yan, Zheng0a07fc82017-03-29 15:30:24 +08002826 if (op == CEPH_SESSION_CLOSE)
2827 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002828 return;
2829
2830bad:
2831 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2832 (int)msg->front.iov_len);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002833 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002834 return;
2835}
2836
2837
2838/*
2839 * called under session->mutex.
2840 */
2841static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2842 struct ceph_mds_session *session)
2843{
2844 struct ceph_mds_request *req, *nreq;
Yan, Zheng3de22be2015-02-04 14:26:22 +08002845 struct rb_node *p;
Sage Weil2f2dc052009-10-06 11:31:09 -07002846 int err;
2847
2848 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2849
2850 mutex_lock(&mdsc->mutex);
2851 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002852 err = __prepare_send_request(mdsc, req, session->s_mds, true);
Sage Weil2f2dc052009-10-06 11:31:09 -07002853 if (!err) {
2854 ceph_msg_get(req->r_request);
2855 ceph_con_send(&session->s_con, req->r_request);
2856 }
2857 }
Yan, Zheng3de22be2015-02-04 14:26:22 +08002858
2859 /*
2860 * also re-send old requests when MDS enters reconnect stage. So that MDS
2861 * can process completed request in clientreplay stage.
2862 */
2863 p = rb_first(&mdsc->request_tree);
2864 while (p) {
2865 req = rb_entry(p, struct ceph_mds_request, r_node);
2866 p = rb_next(p);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002867 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Yan, Zheng3de22be2015-02-04 14:26:22 +08002868 continue;
2869 if (req->r_attempts == 0)
2870 continue; /* only old requests */
2871 if (req->r_session &&
2872 req->r_session->s_mds == session->s_mds) {
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002873 err = __prepare_send_request(mdsc, req,
2874 session->s_mds, true);
Yan, Zheng3de22be2015-02-04 14:26:22 +08002875 if (!err) {
2876 ceph_msg_get(req->r_request);
2877 ceph_con_send(&session->s_con, req->r_request);
2878 }
2879 }
2880 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002881 mutex_unlock(&mdsc->mutex);
2882}
2883
2884/*
2885 * Encode information about a cap for a reconnect with the MDS.
2886 */
Sage Weil2f2dc052009-10-06 11:31:09 -07002887static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2888 void *arg)
2889{
Sage Weil20cb34a2010-05-12 15:21:32 -07002890 union {
2891 struct ceph_mds_cap_reconnect v2;
2892 struct ceph_mds_cap_reconnect_v1 v1;
2893 } rec;
Yan, Zhengb3f8d682017-09-11 10:58:55 +08002894 struct ceph_inode_info *ci = cap->ci;
Sage Weil20cb34a2010-05-12 15:21:32 -07002895 struct ceph_reconnect_state *recon_state = arg;
2896 struct ceph_pagelist *pagelist = recon_state->pagelist;
Sage Weil2f2dc052009-10-06 11:31:09 -07002897 char *path;
2898 int pathlen, err;
2899 u64 pathbase;
Yan, Zheng3469ed02016-07-05 09:32:31 +08002900 u64 snap_follows;
Sage Weil2f2dc052009-10-06 11:31:09 -07002901 struct dentry *dentry;
2902
Sage Weil2f2dc052009-10-06 11:31:09 -07002903 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2904 inode, ceph_vinop(inode), cap, cap->cap_id,
2905 ceph_cap_string(cap->issued));
Sage Weil93cea5b2009-12-23 12:21:51 -08002906 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2907 if (err)
2908 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002909
2910 dentry = d_find_alias(inode);
2911 if (dentry) {
2912 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2913 if (IS_ERR(path)) {
2914 err = PTR_ERR(path);
Sage Weile072f8a2010-08-26 09:26:37 -07002915 goto out_dput;
Sage Weil2f2dc052009-10-06 11:31:09 -07002916 }
2917 } else {
2918 path = NULL;
2919 pathlen = 0;
Ilya Dryomov4eacd4c2016-08-09 16:12:09 +02002920 pathbase = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002921 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002922
Sage Weilbe655592011-11-30 09:47:09 -08002923 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002924 cap->seq = 0; /* reset cap seq */
2925 cap->issue_seq = 0; /* and issue_seq */
Yan, Zheng667ca052013-05-31 16:25:36 +08002926 cap->mseq = 0; /* and migrate_seq */
Yan, Zheng99a9c272013-09-22 11:08:14 +08002927 cap->cap_gen = cap->session->s_cap_gen;
Sage Weil20cb34a2010-05-12 15:21:32 -07002928
Yan, Zheng121f22a2016-07-04 22:05:18 +08002929 if (recon_state->msg_version >= 2) {
Sage Weil20cb34a2010-05-12 15:21:32 -07002930 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
2931 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2932 rec.v2.issued = cpu_to_le32(cap->issued);
2933 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2934 rec.v2.pathbase = cpu_to_le64(pathbase);
Jeff Laytonec1dff22017-10-31 15:51:14 -04002935 rec.v2.flock_len = (__force __le32)
2936 ((ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) ? 0 : 1);
Sage Weil20cb34a2010-05-12 15:21:32 -07002937 } else {
2938 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
2939 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2940 rec.v1.issued = cpu_to_le32(cap->issued);
2941 rec.v1.size = cpu_to_le64(inode->i_size);
2942 ceph_encode_timespec(&rec.v1.mtime, &inode->i_mtime);
2943 ceph_encode_timespec(&rec.v1.atime, &inode->i_atime);
2944 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2945 rec.v1.pathbase = cpu_to_le64(pathbase);
Sage Weil20cb34a2010-05-12 15:21:32 -07002946 }
Yan, Zheng3469ed02016-07-05 09:32:31 +08002947
2948 if (list_empty(&ci->i_cap_snaps)) {
Yan, Zheng92776fd2017-08-16 21:42:39 +08002949 snap_follows = ci->i_head_snapc ? ci->i_head_snapc->seq : 0;
Yan, Zheng3469ed02016-07-05 09:32:31 +08002950 } else {
2951 struct ceph_cap_snap *capsnap =
2952 list_first_entry(&ci->i_cap_snaps,
2953 struct ceph_cap_snap, ci_item);
2954 snap_follows = capsnap->follows;
Sage Weil93cea5b2009-12-23 12:21:51 -08002955 }
Sage Weilbe655592011-11-30 09:47:09 -08002956 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002957
Yan, Zheng121f22a2016-07-04 22:05:18 +08002958 if (recon_state->msg_version >= 2) {
Greg Farnum40819f62010-08-02 15:34:23 -07002959 int num_fcntl_locks, num_flock_locks;
Yan, Zheng4deb14a2017-09-11 10:36:28 +08002960 struct ceph_filelock *flocks = NULL;
Yan, Zheng121f22a2016-07-04 22:05:18 +08002961 size_t struct_len, total_len = 0;
2962 u8 struct_v = 0;
Greg Farnum40819f62010-08-02 15:34:23 -07002963
Jim Schutt39be95e2013-05-15 13:03:35 -05002964encode_again:
Yan, Zhengb3f8d682017-09-11 10:58:55 +08002965 if (rec.v2.flock_len) {
2966 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
2967 } else {
2968 num_fcntl_locks = 0;
2969 num_flock_locks = 0;
2970 }
Yan, Zheng4deb14a2017-09-11 10:36:28 +08002971 if (num_fcntl_locks + num_flock_locks > 0) {
2972 flocks = kmalloc((num_fcntl_locks + num_flock_locks) *
2973 sizeof(struct ceph_filelock), GFP_NOFS);
2974 if (!flocks) {
2975 err = -ENOMEM;
2976 goto out_free;
2977 }
2978 err = ceph_encode_locks_to_buffer(inode, flocks,
2979 num_fcntl_locks,
2980 num_flock_locks);
2981 if (err) {
2982 kfree(flocks);
2983 flocks = NULL;
2984 if (err == -ENOSPC)
2985 goto encode_again;
2986 goto out_free;
2987 }
2988 } else {
Jim Schutt39be95e2013-05-15 13:03:35 -05002989 kfree(flocks);
Yan, Zheng4deb14a2017-09-11 10:36:28 +08002990 flocks = NULL;
Jim Schutt39be95e2013-05-15 13:03:35 -05002991 }
Yan, Zheng121f22a2016-07-04 22:05:18 +08002992
2993 if (recon_state->msg_version >= 3) {
2994 /* version, compat_version and struct_len */
2995 total_len = 2 * sizeof(u8) + sizeof(u32);
Yan, Zheng3469ed02016-07-05 09:32:31 +08002996 struct_v = 2;
Yan, Zheng121f22a2016-07-04 22:05:18 +08002997 }
Jim Schutt39be95e2013-05-15 13:03:35 -05002998 /*
2999 * number of encoded locks is stable, so copy to pagelist
3000 */
Yan, Zheng121f22a2016-07-04 22:05:18 +08003001 struct_len = 2 * sizeof(u32) +
3002 (num_fcntl_locks + num_flock_locks) *
3003 sizeof(struct ceph_filelock);
3004 rec.v2.flock_len = cpu_to_le32(struct_len);
3005
3006 struct_len += sizeof(rec.v2);
3007 struct_len += sizeof(u32) + pathlen;
3008
Yan, Zheng3469ed02016-07-05 09:32:31 +08003009 if (struct_v >= 2)
3010 struct_len += sizeof(u64); /* snap_follows */
3011
Yan, Zheng121f22a2016-07-04 22:05:18 +08003012 total_len += struct_len;
3013 err = ceph_pagelist_reserve(pagelist, total_len);
3014
3015 if (!err) {
3016 if (recon_state->msg_version >= 3) {
3017 ceph_pagelist_encode_8(pagelist, struct_v);
3018 ceph_pagelist_encode_8(pagelist, 1);
3019 ceph_pagelist_encode_32(pagelist, struct_len);
3020 }
3021 ceph_pagelist_encode_string(pagelist, path, pathlen);
3022 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v2));
3023 ceph_locks_to_pagelist(flocks, pagelist,
3024 num_fcntl_locks,
3025 num_flock_locks);
Yan, Zheng3469ed02016-07-05 09:32:31 +08003026 if (struct_v >= 2)
3027 ceph_pagelist_encode_64(pagelist, snap_follows);
Yan, Zheng121f22a2016-07-04 22:05:18 +08003028 }
Jim Schutt39be95e2013-05-15 13:03:35 -05003029 kfree(flocks);
Sage Weil3612abb2010-09-07 15:59:27 -07003030 } else {
Yan, Zheng121f22a2016-07-04 22:05:18 +08003031 size_t size = sizeof(u32) + pathlen + sizeof(rec.v1);
3032 err = ceph_pagelist_reserve(pagelist, size);
3033 if (!err) {
3034 ceph_pagelist_encode_string(pagelist, path, pathlen);
3035 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v1));
3036 }
Greg Farnum40819f62010-08-02 15:34:23 -07003037 }
Yan, Zheng44c99752013-09-22 10:28:10 +08003038
3039 recon_state->nr_caps++;
Sage Weile072f8a2010-08-26 09:26:37 -07003040out_free:
Sage Weil2f2dc052009-10-06 11:31:09 -07003041 kfree(path);
Sage Weile072f8a2010-08-26 09:26:37 -07003042out_dput:
Sage Weil2f2dc052009-10-06 11:31:09 -07003043 dput(dentry);
Sage Weil93cea5b2009-12-23 12:21:51 -08003044 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07003045}
3046
3047
3048/*
3049 * If an MDS fails and recovers, clients need to reconnect in order to
3050 * reestablish shared state. This includes all caps issued through
3051 * this session _and_ the snap_realm hierarchy. Because it's not
3052 * clear which snap realms the mds cares about, we send everything we
3053 * know about.. that ensures we'll then get any new info the
3054 * recovering MDS might have.
3055 *
3056 * This is a relatively heavyweight operation, but it's rare.
3057 *
3058 * called with mdsc->mutex held.
3059 */
Sage Weil34b6c852010-05-10 16:31:25 -07003060static void send_mds_reconnect(struct ceph_mds_client *mdsc,
3061 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07003062{
Sage Weil2f2dc052009-10-06 11:31:09 -07003063 struct ceph_msg *reply;
Sage Weila105f002010-02-15 14:37:55 -08003064 struct rb_node *p;
Sage Weil34b6c852010-05-10 16:31:25 -07003065 int mds = session->s_mds;
Sage Weil9abf82b2010-05-10 21:58:38 -07003066 int err = -ENOMEM;
Yan, Zheng44c99752013-09-22 10:28:10 +08003067 int s_nr_caps;
Sage Weil93cea5b2009-12-23 12:21:51 -08003068 struct ceph_pagelist *pagelist;
Sage Weil20cb34a2010-05-12 15:21:32 -07003069 struct ceph_reconnect_state recon_state;
Jeff Laytonc8a96a32017-10-19 08:53:58 -04003070 LIST_HEAD(dispose);
Sage Weil2f2dc052009-10-06 11:31:09 -07003071
Sage Weil34b6c852010-05-10 16:31:25 -07003072 pr_info("mds%d reconnect start\n", mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003073
Sage Weil93cea5b2009-12-23 12:21:51 -08003074 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
3075 if (!pagelist)
3076 goto fail_nopagelist;
3077 ceph_pagelist_init(pagelist);
3078
Sage Weilb61c2762011-08-09 15:03:46 -07003079 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07003080 if (!reply)
Sage Weil93cea5b2009-12-23 12:21:51 -08003081 goto fail_nomsg;
Sage Weil93cea5b2009-12-23 12:21:51 -08003082
Sage Weil34b6c852010-05-10 16:31:25 -07003083 mutex_lock(&session->s_mutex);
3084 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
3085 session->s_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003086
Sage Weil2f2dc052009-10-06 11:31:09 -07003087 dout("session %p state %s\n", session,
John Spraya687eca2014-09-19 13:51:08 +01003088 ceph_session_state_name(session->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07003089
Yan, Zheng99a9c272013-09-22 11:08:14 +08003090 spin_lock(&session->s_gen_ttl_lock);
3091 session->s_cap_gen++;
3092 spin_unlock(&session->s_gen_ttl_lock);
3093
3094 spin_lock(&session->s_cap_lock);
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08003095 /* don't know if session is readonly */
3096 session->s_readonly = 0;
Yan, Zheng99a9c272013-09-22 11:08:14 +08003097 /*
3098 * notify __ceph_remove_cap() that we are composing cap reconnect.
3099 * If a cap get released before being added to the cap reconnect,
3100 * __ceph_remove_cap() should skip queuing cap release.
3101 */
3102 session->s_cap_reconnect = 1;
Sage Weile01a5942010-05-10 15:36:44 -07003103 /* drop old cap expires; we're about to reestablish that state */
Jeff Laytonc8a96a32017-10-19 08:53:58 -04003104 detach_cap_releases(session, &dispose);
3105 spin_unlock(&session->s_cap_lock);
3106 dispose_cap_releases(mdsc, &dispose);
Sage Weile01a5942010-05-10 15:36:44 -07003107
Yan, Zheng5d233712014-09-10 16:56:23 +08003108 /* trim unused caps to reduce MDS's cache rejoin time */
Yan, Zhengc0bd50e2015-04-07 15:51:08 +08003109 if (mdsc->fsc->sb->s_root)
3110 shrink_dcache_parent(mdsc->fsc->sb->s_root);
Yan, Zheng5d233712014-09-10 16:56:23 +08003111
3112 ceph_con_close(&session->s_con);
3113 ceph_con_open(&session->s_con,
3114 CEPH_ENTITY_TYPE_MDS, mds,
3115 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
3116
3117 /* replay unsafe requests */
3118 replay_unsafe_requests(mdsc, session);
3119
3120 down_read(&mdsc->snap_rwsem);
3121
Sage Weil2f2dc052009-10-06 11:31:09 -07003122 /* traverse this session's caps */
Yan, Zheng44c99752013-09-22 10:28:10 +08003123 s_nr_caps = session->s_nr_caps;
3124 err = ceph_pagelist_encode_32(pagelist, s_nr_caps);
Sage Weil93cea5b2009-12-23 12:21:51 -08003125 if (err)
3126 goto fail;
Sage Weil20cb34a2010-05-12 15:21:32 -07003127
Yan, Zheng44c99752013-09-22 10:28:10 +08003128 recon_state.nr_caps = 0;
Sage Weil20cb34a2010-05-12 15:21:32 -07003129 recon_state.pagelist = pagelist;
Yan, Zheng121f22a2016-07-04 22:05:18 +08003130 if (session->s_con.peer_features & CEPH_FEATURE_MDSENC)
3131 recon_state.msg_version = 3;
3132 else if (session->s_con.peer_features & CEPH_FEATURE_FLOCK)
3133 recon_state.msg_version = 2;
3134 else
3135 recon_state.msg_version = 1;
Sage Weil20cb34a2010-05-12 15:21:32 -07003136 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
Sage Weil2f2dc052009-10-06 11:31:09 -07003137 if (err < 0)
Sage Weil9abf82b2010-05-10 21:58:38 -07003138 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07003139
Yan, Zheng99a9c272013-09-22 11:08:14 +08003140 spin_lock(&session->s_cap_lock);
3141 session->s_cap_reconnect = 0;
3142 spin_unlock(&session->s_cap_lock);
3143
Sage Weil2f2dc052009-10-06 11:31:09 -07003144 /*
3145 * snaprealms. we provide mds with the ino, seq (version), and
3146 * parent for all of our realms. If the mds has any newer info,
3147 * it will tell us.
3148 */
Sage Weila105f002010-02-15 14:37:55 -08003149 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
3150 struct ceph_snap_realm *realm =
3151 rb_entry(p, struct ceph_snap_realm, node);
Sage Weil93cea5b2009-12-23 12:21:51 -08003152 struct ceph_mds_snaprealm_reconnect sr_rec;
Sage Weil2f2dc052009-10-06 11:31:09 -07003153
3154 dout(" adding snap realm %llx seq %lld parent %llx\n",
3155 realm->ino, realm->seq, realm->parent_ino);
Sage Weil93cea5b2009-12-23 12:21:51 -08003156 sr_rec.ino = cpu_to_le64(realm->ino);
3157 sr_rec.seq = cpu_to_le64(realm->seq);
3158 sr_rec.parent = cpu_to_le64(realm->parent_ino);
3159 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
3160 if (err)
3161 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07003162 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003163
Yan, Zheng121f22a2016-07-04 22:05:18 +08003164 reply->hdr.version = cpu_to_le16(recon_state.msg_version);
Yan, Zheng44c99752013-09-22 10:28:10 +08003165
3166 /* raced with cap release? */
3167 if (s_nr_caps != recon_state.nr_caps) {
3168 struct page *page = list_first_entry(&pagelist->head,
3169 struct page, lru);
3170 __le32 *addr = kmap_atomic(page);
3171 *addr = cpu_to_le32(recon_state.nr_caps);
3172 kunmap_atomic(addr);
Alex Elderebf18f42013-03-04 22:29:57 -06003173 }
Yan, Zheng44c99752013-09-22 10:28:10 +08003174
3175 reply->hdr.data_len = cpu_to_le32(pagelist->length);
3176 ceph_msg_data_add_pagelist(reply, pagelist);
Yan, Zhenge548e9b2015-06-10 15:17:56 +08003177
3178 ceph_early_kick_flushing_caps(mdsc, session);
3179
Sage Weil2f2dc052009-10-06 11:31:09 -07003180 ceph_con_send(&session->s_con, reply);
3181
Sage Weil9abf82b2010-05-10 21:58:38 -07003182 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003183
Sage Weil9abf82b2010-05-10 21:58:38 -07003184 mutex_lock(&mdsc->mutex);
3185 __wake_requests(mdsc, &session->s_waiting);
3186 mutex_unlock(&mdsc->mutex);
3187
Sage Weil2f2dc052009-10-06 11:31:09 -07003188 up_read(&mdsc->snap_rwsem);
Sage Weil2f2dc052009-10-06 11:31:09 -07003189 return;
3190
Sage Weil93cea5b2009-12-23 12:21:51 -08003191fail:
Sage Weil2f2dc052009-10-06 11:31:09 -07003192 ceph_msg_put(reply);
Sage Weil9abf82b2010-05-10 21:58:38 -07003193 up_read(&mdsc->snap_rwsem);
3194 mutex_unlock(&session->s_mutex);
Sage Weil93cea5b2009-12-23 12:21:51 -08003195fail_nomsg:
3196 ceph_pagelist_release(pagelist);
Sage Weil93cea5b2009-12-23 12:21:51 -08003197fail_nopagelist:
Sage Weil9abf82b2010-05-10 21:58:38 -07003198 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
Sage Weil9abf82b2010-05-10 21:58:38 -07003199 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07003200}
3201
3202
3203/*
3204 * compare old and new mdsmaps, kicking requests
3205 * and closing out old connections as necessary
3206 *
3207 * called under mdsc->mutex.
3208 */
3209static void check_new_map(struct ceph_mds_client *mdsc,
3210 struct ceph_mdsmap *newmap,
3211 struct ceph_mdsmap *oldmap)
3212{
3213 int i;
3214 int oldstate, newstate;
3215 struct ceph_mds_session *s;
3216
3217 dout("check_new_map new %u old %u\n",
3218 newmap->m_epoch, oldmap->m_epoch);
3219
Yan, Zheng76201b62017-03-28 17:04:13 +08003220 for (i = 0; i < oldmap->m_num_mds && i < mdsc->max_sessions; i++) {
Markus Elfringd37b1d92017-08-20 20:22:02 +02003221 if (!mdsc->sessions[i])
Sage Weil2f2dc052009-10-06 11:31:09 -07003222 continue;
3223 s = mdsc->sessions[i];
3224 oldstate = ceph_mdsmap_get_state(oldmap, i);
3225 newstate = ceph_mdsmap_get_state(newmap, i);
3226
Sage Weil0deb01c2010-06-17 14:19:01 -07003227 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07003228 i, ceph_mds_state_name(oldstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07003229 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
Sage Weil2f2dc052009-10-06 11:31:09 -07003230 ceph_mds_state_name(newstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07003231 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
John Spraya687eca2014-09-19 13:51:08 +01003232 ceph_session_state_name(s->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07003233
Yan, Zheng76201b62017-03-28 17:04:13 +08003234 if (i >= newmap->m_num_mds ||
Yan, Zheng3e8f43a2012-09-20 17:42:25 +08003235 memcmp(ceph_mdsmap_get_addr(oldmap, i),
Sage Weil2f2dc052009-10-06 11:31:09 -07003236 ceph_mdsmap_get_addr(newmap, i),
3237 sizeof(struct ceph_entity_addr))) {
3238 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
3239 /* the session never opened, just close it
3240 * out now */
Yan, Zheng0a07fc82017-03-29 15:30:24 +08003241 get_session(s);
Sage Weil2600d2d2010-02-22 15:12:16 -08003242 __unregister_session(mdsc, s);
Yan, Zheng0a07fc82017-03-29 15:30:24 +08003243 __wake_requests(mdsc, &s->s_waiting);
3244 ceph_put_mds_session(s);
Yan, Zheng28275282017-03-28 17:56:29 +08003245 } else if (i >= newmap->m_num_mds) {
3246 /* force close session for stopped mds */
3247 get_session(s);
3248 __unregister_session(mdsc, s);
3249 __wake_requests(mdsc, &s->s_waiting);
3250 kick_requests(mdsc, i);
3251 mutex_unlock(&mdsc->mutex);
3252
3253 mutex_lock(&s->s_mutex);
3254 cleanup_session_requests(mdsc, s);
3255 remove_session_caps(s);
3256 mutex_unlock(&s->s_mutex);
3257
3258 ceph_put_mds_session(s);
3259
3260 mutex_lock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003261 } else {
3262 /* just close it */
3263 mutex_unlock(&mdsc->mutex);
3264 mutex_lock(&s->s_mutex);
3265 mutex_lock(&mdsc->mutex);
3266 ceph_con_close(&s->s_con);
3267 mutex_unlock(&s->s_mutex);
3268 s->s_state = CEPH_MDS_SESSION_RESTARTING;
3269 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003270 } else if (oldstate == newstate) {
3271 continue; /* nothing new with this mds */
3272 }
3273
3274 /*
3275 * send reconnect?
3276 */
3277 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
Sage Weil34b6c852010-05-10 16:31:25 -07003278 newstate >= CEPH_MDS_STATE_RECONNECT) {
3279 mutex_unlock(&mdsc->mutex);
3280 send_mds_reconnect(mdsc, s);
3281 mutex_lock(&mdsc->mutex);
3282 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003283
3284 /*
Sage Weil29790f22010-03-18 14:45:05 -07003285 * kick request on any mds that has gone active.
Sage Weil2f2dc052009-10-06 11:31:09 -07003286 */
3287 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
3288 newstate >= CEPH_MDS_STATE_ACTIVE) {
Sage Weil29790f22010-03-18 14:45:05 -07003289 if (oldstate != CEPH_MDS_STATE_CREATING &&
3290 oldstate != CEPH_MDS_STATE_STARTING)
3291 pr_info("mds%d recovery completed\n", s->s_mds);
3292 kick_requests(mdsc, i);
Sage Weil2f2dc052009-10-06 11:31:09 -07003293 ceph_kick_flushing_caps(mdsc, s);
Sage Weil0dc25702009-11-20 13:43:45 -08003294 wake_up_session_caps(s, 1);
Sage Weil2f2dc052009-10-06 11:31:09 -07003295 }
3296 }
Sage Weilcb170a22010-06-21 13:38:35 -07003297
Yan, Zheng76201b62017-03-28 17:04:13 +08003298 for (i = 0; i < newmap->m_num_mds && i < mdsc->max_sessions; i++) {
Sage Weilcb170a22010-06-21 13:38:35 -07003299 s = mdsc->sessions[i];
3300 if (!s)
3301 continue;
3302 if (!ceph_mdsmap_is_laggy(newmap, i))
3303 continue;
3304 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3305 s->s_state == CEPH_MDS_SESSION_HUNG ||
3306 s->s_state == CEPH_MDS_SESSION_CLOSING) {
3307 dout(" connecting to export targets of laggy mds%d\n",
3308 i);
3309 __open_export_target_sessions(mdsc, s);
3310 }
3311 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003312}
3313
3314
3315
3316/*
3317 * leases
3318 */
3319
3320/*
3321 * caller must hold session s_mutex, dentry->d_lock
3322 */
3323void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
3324{
3325 struct ceph_dentry_info *di = ceph_dentry(dentry);
3326
3327 ceph_put_mds_session(di->lease_session);
3328 di->lease_session = NULL;
3329}
3330
Sage Weil2600d2d2010-02-22 15:12:16 -08003331static void handle_lease(struct ceph_mds_client *mdsc,
3332 struct ceph_mds_session *session,
3333 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07003334{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003335 struct super_block *sb = mdsc->fsc->sb;
Sage Weil2f2dc052009-10-06 11:31:09 -07003336 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07003337 struct dentry *parent, *dentry;
3338 struct ceph_dentry_info *di;
Sage Weil2600d2d2010-02-22 15:12:16 -08003339 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07003340 struct ceph_mds_lease *h = msg->front.iov_base;
Sage Weil1e5ea232010-06-04 10:05:40 -07003341 u32 seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003342 struct ceph_vino vino;
Sage Weil2f2dc052009-10-06 11:31:09 -07003343 struct qstr dname;
3344 int release = 0;
3345
Sage Weil2f2dc052009-10-06 11:31:09 -07003346 dout("handle_lease from mds%d\n", mds);
3347
3348 /* decode */
3349 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
3350 goto bad;
3351 vino.ino = le64_to_cpu(h->ino);
3352 vino.snap = CEPH_NOSNAP;
Sage Weil1e5ea232010-06-04 10:05:40 -07003353 seq = le32_to_cpu(h->seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003354 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
3355 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
3356 if (dname.len != get_unaligned_le32(h+1))
3357 goto bad;
3358
Sage Weil2f2dc052009-10-06 11:31:09 -07003359 /* lookup inode */
3360 inode = ceph_find_inode(sb, vino);
Sage Weil2f90b852011-07-26 11:28:25 -07003361 dout("handle_lease %s, ino %llx %p %.*s\n",
3362 ceph_lease_op_name(h->action), vino.ino, inode,
Sage Weil1e5ea232010-06-04 10:05:40 -07003363 dname.len, dname.name);
Yan, Zheng6cd3bca2014-09-17 07:45:12 +08003364
3365 mutex_lock(&session->s_mutex);
3366 session->s_seq++;
3367
Markus Elfringd37b1d92017-08-20 20:22:02 +02003368 if (!inode) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003369 dout("handle_lease no inode %llx\n", vino.ino);
3370 goto release;
3371 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003372
3373 /* dentry */
3374 parent = d_find_alias(inode);
3375 if (!parent) {
3376 dout("no parent dentry on inode %p\n", inode);
3377 WARN_ON(1);
3378 goto release; /* hrm... */
3379 }
Linus Torvalds8387ff22016-06-10 07:51:30 -07003380 dname.hash = full_name_hash(parent, dname.name, dname.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07003381 dentry = d_lookup(parent, &dname);
3382 dput(parent);
3383 if (!dentry)
3384 goto release;
3385
3386 spin_lock(&dentry->d_lock);
3387 di = ceph_dentry(dentry);
3388 switch (h->action) {
3389 case CEPH_MDS_LEASE_REVOKE:
Sage Weil3d8eb7a2011-11-11 09:48:53 -08003390 if (di->lease_session == session) {
Sage Weil1e5ea232010-06-04 10:05:40 -07003391 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
3392 h->seq = cpu_to_le32(di->lease_seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003393 __ceph_mdsc_drop_dentry_lease(dentry);
3394 }
3395 release = 1;
3396 break;
3397
3398 case CEPH_MDS_LEASE_RENEW:
Sage Weil3d8eb7a2011-11-11 09:48:53 -08003399 if (di->lease_session == session &&
Sage Weil2f2dc052009-10-06 11:31:09 -07003400 di->lease_gen == session->s_cap_gen &&
3401 di->lease_renew_from &&
3402 di->lease_renew_after == 0) {
3403 unsigned long duration =
Nicholas Mc Guire3563dbdd2015-02-06 06:52:17 -05003404 msecs_to_jiffies(le32_to_cpu(h->duration_ms));
Sage Weil2f2dc052009-10-06 11:31:09 -07003405
Sage Weil1e5ea232010-06-04 10:05:40 -07003406 di->lease_seq = seq;
Miklos Szeredi9b16f03c2016-06-22 16:35:04 +02003407 di->time = di->lease_renew_from + duration;
Sage Weil2f2dc052009-10-06 11:31:09 -07003408 di->lease_renew_after = di->lease_renew_from +
3409 (duration >> 1);
3410 di->lease_renew_from = 0;
3411 }
3412 break;
3413 }
3414 spin_unlock(&dentry->d_lock);
3415 dput(dentry);
3416
3417 if (!release)
3418 goto out;
3419
3420release:
3421 /* let's just reuse the same message */
3422 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
3423 ceph_msg_get(msg);
3424 ceph_con_send(&session->s_con, msg);
3425
3426out:
3427 iput(inode);
3428 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003429 return;
3430
3431bad:
3432 pr_err("corrupt lease message\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08003433 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003434}
3435
3436void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
3437 struct inode *inode,
3438 struct dentry *dentry, char action,
3439 u32 seq)
3440{
3441 struct ceph_msg *msg;
3442 struct ceph_mds_lease *lease;
3443 int len = sizeof(*lease) + sizeof(u32);
3444 int dnamelen = 0;
3445
3446 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
3447 inode, dentry, ceph_lease_op_name(action), session->s_mds);
3448 dnamelen = dentry->d_name.len;
3449 len += dnamelen;
3450
Sage Weilb61c2762011-08-09 15:03:46 -07003451 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07003452 if (!msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07003453 return;
3454 lease = msg->front.iov_base;
3455 lease->action = action;
Sage Weil2f2dc052009-10-06 11:31:09 -07003456 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
3457 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
3458 lease->seq = cpu_to_le32(seq);
3459 put_unaligned_le32(dnamelen, lease + 1);
3460 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
3461
3462 /*
3463 * if this is a preemptive lease RELEASE, no need to
3464 * flush request stream, since the actual request will
3465 * soon follow.
3466 */
3467 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
3468
3469 ceph_con_send(&session->s_con, msg);
3470}
3471
3472/*
Sage Weil2f2dc052009-10-06 11:31:09 -07003473 * drop all leases (and dentry refs) in preparation for umount
3474 */
3475static void drop_leases(struct ceph_mds_client *mdsc)
3476{
3477 int i;
3478
3479 dout("drop_leases\n");
3480 mutex_lock(&mdsc->mutex);
3481 for (i = 0; i < mdsc->max_sessions; i++) {
3482 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
3483 if (!s)
3484 continue;
3485 mutex_unlock(&mdsc->mutex);
3486 mutex_lock(&s->s_mutex);
3487 mutex_unlock(&s->s_mutex);
3488 ceph_put_mds_session(s);
3489 mutex_lock(&mdsc->mutex);
3490 }
3491 mutex_unlock(&mdsc->mutex);
3492}
3493
3494
3495
3496/*
3497 * delayed work -- periodically trim expired leases, renew caps with mds
3498 */
3499static void schedule_delayed(struct ceph_mds_client *mdsc)
3500{
3501 int delay = 5;
3502 unsigned hz = round_jiffies_relative(HZ * delay);
3503 schedule_delayed_work(&mdsc->delayed_work, hz);
3504}
3505
3506static void delayed_work(struct work_struct *work)
3507{
3508 int i;
3509 struct ceph_mds_client *mdsc =
3510 container_of(work, struct ceph_mds_client, delayed_work.work);
3511 int renew_interval;
3512 int renew_caps;
3513
3514 dout("mdsc delayed_work\n");
Sage Weilafcdaea2009-10-14 14:27:38 -07003515 ceph_check_delayed_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003516
3517 mutex_lock(&mdsc->mutex);
3518 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
3519 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
3520 mdsc->last_renew_caps);
3521 if (renew_caps)
3522 mdsc->last_renew_caps = jiffies;
3523
3524 for (i = 0; i < mdsc->max_sessions; i++) {
3525 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
Markus Elfringd37b1d92017-08-20 20:22:02 +02003526 if (!s)
Sage Weil2f2dc052009-10-06 11:31:09 -07003527 continue;
3528 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
3529 dout("resending session close request for mds%d\n",
3530 s->s_mds);
3531 request_close_session(mdsc, s);
3532 ceph_put_mds_session(s);
3533 continue;
3534 }
3535 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
3536 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
3537 s->s_state = CEPH_MDS_SESSION_HUNG;
3538 pr_info("mds%d hung\n", s->s_mds);
3539 }
3540 }
3541 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
3542 /* this mds is failed or recovering, just wait */
3543 ceph_put_mds_session(s);
3544 continue;
3545 }
3546 mutex_unlock(&mdsc->mutex);
3547
3548 mutex_lock(&s->s_mutex);
3549 if (renew_caps)
3550 send_renew_caps(mdsc, s);
3551 else
3552 ceph_con_keepalive(&s->s_con);
Sage Weilaab53dd2010-03-17 16:30:21 -07003553 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3554 s->s_state == CEPH_MDS_SESSION_HUNG)
Sage Weil3d7ded42010-06-09 16:47:10 -07003555 ceph_send_cap_releases(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07003556 mutex_unlock(&s->s_mutex);
3557 ceph_put_mds_session(s);
3558
3559 mutex_lock(&mdsc->mutex);
3560 }
3561 mutex_unlock(&mdsc->mutex);
3562
3563 schedule_delayed(mdsc);
3564}
3565
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003566int ceph_mdsc_init(struct ceph_fs_client *fsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07003567
Sage Weil2f2dc052009-10-06 11:31:09 -07003568{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003569 struct ceph_mds_client *mdsc;
3570
3571 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
3572 if (!mdsc)
3573 return -ENOMEM;
3574 mdsc->fsc = fsc;
3575 fsc->mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07003576 mutex_init(&mdsc->mutex);
3577 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
Markus Elfringd37b1d92017-08-20 20:22:02 +02003578 if (!mdsc->mdsmap) {
majianpengfb3101b2013-06-25 14:48:19 +08003579 kfree(mdsc);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08003580 return -ENOMEM;
majianpengfb3101b2013-06-25 14:48:19 +08003581 }
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08003582
Sage Weil2f2dc052009-10-06 11:31:09 -07003583 init_completion(&mdsc->safe_umount_waiters);
Sage Weilf3c60c52010-08-11 14:51:23 -07003584 init_waitqueue_head(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003585 INIT_LIST_HEAD(&mdsc->waiting_for_map);
3586 mdsc->sessions = NULL;
Yan, Zheng86d8f672015-01-09 17:00:42 +08003587 atomic_set(&mdsc->num_sessions, 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07003588 mdsc->max_sessions = 0;
3589 mdsc->stopping = 0;
Yan, Zhengaffbc192015-05-05 21:22:13 +08003590 mdsc->last_snap_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003591 init_rwsem(&mdsc->snap_rwsem);
Sage Weila105f002010-02-15 14:37:55 -08003592 mdsc->snap_realms = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07003593 INIT_LIST_HEAD(&mdsc->snap_empty);
3594 spin_lock_init(&mdsc->snap_empty_lock);
3595 mdsc->last_tid = 0;
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08003596 mdsc->oldest_tid = 0;
Sage Weil44ca18f2010-02-15 12:08:46 -08003597 mdsc->request_tree = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07003598 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
3599 mdsc->last_renew_caps = jiffies;
3600 INIT_LIST_HEAD(&mdsc->cap_delay_list);
3601 spin_lock_init(&mdsc->cap_delay_lock);
3602 INIT_LIST_HEAD(&mdsc->snap_flush_list);
3603 spin_lock_init(&mdsc->snap_flush_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08003604 mdsc->last_cap_flush_tid = 1;
Yan, Zhenge4500b52016-07-06 11:12:56 +08003605 INIT_LIST_HEAD(&mdsc->cap_flush_list);
Sage Weil2f2dc052009-10-06 11:31:09 -07003606 INIT_LIST_HEAD(&mdsc->cap_dirty);
Sage Weildb354052011-05-24 11:46:31 -07003607 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
Sage Weil2f2dc052009-10-06 11:31:09 -07003608 mdsc->num_cap_flushing = 0;
3609 spin_lock_init(&mdsc->cap_dirty_lock);
3610 init_waitqueue_head(&mdsc->cap_flushing_wq);
3611 spin_lock_init(&mdsc->dentry_lru_lock);
3612 INIT_LIST_HEAD(&mdsc->dentry_lru);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08003613
Yehuda Sadeh37151662010-06-17 16:16:12 -07003614 ceph_caps_init(mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003615 ceph_adjust_min_caps(mdsc, fsc->min_caps);
Yehuda Sadeh37151662010-06-17 16:16:12 -07003616
Yan, Zheng10183a62015-04-27 15:33:28 +08003617 init_rwsem(&mdsc->pool_perm_rwsem);
3618 mdsc->pool_perm_tree = RB_ROOT;
3619
Yan, Zheng717e6f22017-09-11 12:10:08 +08003620 strncpy(mdsc->nodename, utsname()->nodename,
3621 sizeof(mdsc->nodename) - 1);
Sage Weil5f44f142009-11-18 14:52:18 -08003622 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003623}
3624
3625/*
3626 * Wait for safe replies on open mds requests. If we time out, drop
3627 * all requests from the tree to avoid dangling dentry refs.
3628 */
3629static void wait_requests(struct ceph_mds_client *mdsc)
3630{
Ilya Dryomova319bf52015-05-15 12:02:17 +03003631 struct ceph_options *opts = mdsc->fsc->client->options;
Sage Weil2f2dc052009-10-06 11:31:09 -07003632 struct ceph_mds_request *req;
Sage Weil2f2dc052009-10-06 11:31:09 -07003633
3634 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003635 if (__get_oldest_req(mdsc)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003636 mutex_unlock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003637
Sage Weil2f2dc052009-10-06 11:31:09 -07003638 dout("wait_requests waiting for requests\n");
3639 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
Ilya Dryomova319bf52015-05-15 12:02:17 +03003640 ceph_timeout_jiffies(opts->mount_timeout));
Sage Weil2f2dc052009-10-06 11:31:09 -07003641
3642 /* tear down remaining requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08003643 mutex_lock(&mdsc->mutex);
3644 while ((req = __get_oldest_req(mdsc))) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003645 dout("wait_requests timed out on tid %llu\n",
3646 req->r_tid);
Sage Weil44ca18f2010-02-15 12:08:46 -08003647 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07003648 }
3649 }
3650 mutex_unlock(&mdsc->mutex);
3651 dout("wait_requests done\n");
3652}
3653
3654/*
3655 * called before mount is ro, and before dentries are torn down.
3656 * (hmm, does this still race with new lookups?)
3657 */
3658void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
3659{
3660 dout("pre_umount\n");
3661 mdsc->stopping = 1;
3662
3663 drop_leases(mdsc);
Sage Weilafcdaea2009-10-14 14:27:38 -07003664 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003665 wait_requests(mdsc);
Sage Weil17c688c2010-06-21 16:12:26 -07003666
3667 /*
3668 * wait for reply handlers to drop their request refs and
3669 * their inode/dcache refs
3670 */
3671 ceph_msgr_flush();
Sage Weil2f2dc052009-10-06 11:31:09 -07003672}
3673
3674/*
3675 * wait for all write mds requests to flush.
3676 */
3677static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
3678{
Sage Weil80fc7312010-03-16 15:28:54 -07003679 struct ceph_mds_request *req = NULL, *nextreq;
Sage Weil44ca18f2010-02-15 12:08:46 -08003680 struct rb_node *n;
Sage Weil2f2dc052009-10-06 11:31:09 -07003681
3682 mutex_lock(&mdsc->mutex);
3683 dout("wait_unsafe_requests want %lld\n", want_tid);
Sage Weil80fc7312010-03-16 15:28:54 -07003684restart:
Sage Weil44ca18f2010-02-15 12:08:46 -08003685 req = __get_oldest_req(mdsc);
3686 while (req && req->r_tid <= want_tid) {
Sage Weil80fc7312010-03-16 15:28:54 -07003687 /* find next request */
3688 n = rb_next(&req->r_node);
3689 if (n)
3690 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
3691 else
3692 nextreq = NULL;
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08003693 if (req->r_op != CEPH_MDS_OP_SETFILELOCK &&
3694 (req->r_op & CEPH_MDS_OP_WRITE)) {
Sage Weil44ca18f2010-02-15 12:08:46 -08003695 /* write op */
3696 ceph_mdsc_get_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07003697 if (nextreq)
3698 ceph_mdsc_get_request(nextreq);
Sage Weil44ca18f2010-02-15 12:08:46 -08003699 mutex_unlock(&mdsc->mutex);
3700 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3701 req->r_tid, want_tid);
3702 wait_for_completion(&req->r_safe_completion);
3703 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003704 ceph_mdsc_put_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07003705 if (!nextreq)
3706 break; /* next dne before, so we're done! */
3707 if (RB_EMPTY_NODE(&nextreq->r_node)) {
3708 /* next request was removed from tree */
3709 ceph_mdsc_put_request(nextreq);
3710 goto restart;
3711 }
3712 ceph_mdsc_put_request(nextreq); /* won't go away */
Sage Weil44ca18f2010-02-15 12:08:46 -08003713 }
Sage Weil80fc7312010-03-16 15:28:54 -07003714 req = nextreq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003715 }
3716 mutex_unlock(&mdsc->mutex);
3717 dout("wait_unsafe_requests done\n");
3718}
3719
3720void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
3721{
Yan, Zheng0e294382016-07-04 18:06:41 +08003722 u64 want_tid, want_flush;
Sage Weil2f2dc052009-10-06 11:31:09 -07003723
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01003724 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
Sage Weil56b7cf92010-05-03 15:22:00 -07003725 return;
3726
Sage Weil2f2dc052009-10-06 11:31:09 -07003727 dout("sync\n");
3728 mutex_lock(&mdsc->mutex);
3729 want_tid = mdsc->last_tid;
Sage Weil2f2dc052009-10-06 11:31:09 -07003730 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003731
Sage Weilafcdaea2009-10-14 14:27:38 -07003732 ceph_flush_dirty_caps(mdsc);
Yan, Zhengd3383a82015-01-08 21:30:12 +08003733 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zheng8310b082015-06-09 17:20:12 +08003734 want_flush = mdsc->last_cap_flush_tid;
Yan, Zhengc8799fc2016-07-07 15:22:38 +08003735 if (!list_empty(&mdsc->cap_flush_list)) {
3736 struct ceph_cap_flush *cf =
3737 list_last_entry(&mdsc->cap_flush_list,
3738 struct ceph_cap_flush, g_list);
3739 cf->wake = true;
3740 }
Yan, Zhengd3383a82015-01-08 21:30:12 +08003741 spin_unlock(&mdsc->cap_dirty_lock);
3742
Yan, Zheng0e294382016-07-04 18:06:41 +08003743 dout("sync want tid %lld flush_seq %lld\n",
3744 want_tid, want_flush);
Sage Weil2f2dc052009-10-06 11:31:09 -07003745
3746 wait_unsafe_requests(mdsc, want_tid);
Yan, Zheng0e294382016-07-04 18:06:41 +08003747 wait_caps_flush(mdsc, want_flush);
Sage Weil2f2dc052009-10-06 11:31:09 -07003748}
3749
Sage Weilf3c60c52010-08-11 14:51:23 -07003750/*
3751 * true if all sessions are closed, or we force unmount
3752 */
Yan, Zhengfcff4152016-09-14 16:39:51 +08003753static bool done_closing_sessions(struct ceph_mds_client *mdsc, int skipped)
Sage Weilf3c60c52010-08-11 14:51:23 -07003754{
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01003755 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
Sage Weilf3c60c52010-08-11 14:51:23 -07003756 return true;
Yan, Zhengfcff4152016-09-14 16:39:51 +08003757 return atomic_read(&mdsc->num_sessions) <= skipped;
Sage Weilf3c60c52010-08-11 14:51:23 -07003758}
Sage Weil2f2dc052009-10-06 11:31:09 -07003759
3760/*
3761 * called after sb is ro.
3762 */
3763void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
3764{
Ilya Dryomova319bf52015-05-15 12:02:17 +03003765 struct ceph_options *opts = mdsc->fsc->client->options;
Sage Weil2f2dc052009-10-06 11:31:09 -07003766 struct ceph_mds_session *session;
3767 int i;
Yan, Zhengfcff4152016-09-14 16:39:51 +08003768 int skipped = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003769
3770 dout("close_sessions\n");
3771
Sage Weil2f2dc052009-10-06 11:31:09 -07003772 /* close sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07003773 mutex_lock(&mdsc->mutex);
3774 for (i = 0; i < mdsc->max_sessions; i++) {
3775 session = __ceph_lookup_mds_session(mdsc, i);
3776 if (!session)
3777 continue;
Sage Weil2f2dc052009-10-06 11:31:09 -07003778 mutex_unlock(&mdsc->mutex);
Sage Weilf3c60c52010-08-11 14:51:23 -07003779 mutex_lock(&session->s_mutex);
Yan, Zhengfcff4152016-09-14 16:39:51 +08003780 if (__close_session(mdsc, session) <= 0)
3781 skipped++;
Sage Weilf3c60c52010-08-11 14:51:23 -07003782 mutex_unlock(&session->s_mutex);
3783 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003784 mutex_lock(&mdsc->mutex);
3785 }
Sage Weilf3c60c52010-08-11 14:51:23 -07003786 mutex_unlock(&mdsc->mutex);
3787
3788 dout("waiting for sessions to close\n");
Yan, Zhengfcff4152016-09-14 16:39:51 +08003789 wait_event_timeout(mdsc->session_close_wq,
3790 done_closing_sessions(mdsc, skipped),
Ilya Dryomova319bf52015-05-15 12:02:17 +03003791 ceph_timeout_jiffies(opts->mount_timeout));
Sage Weil2f2dc052009-10-06 11:31:09 -07003792
3793 /* tear down remaining sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07003794 mutex_lock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003795 for (i = 0; i < mdsc->max_sessions; i++) {
3796 if (mdsc->sessions[i]) {
3797 session = get_session(mdsc->sessions[i]);
Sage Weil2600d2d2010-02-22 15:12:16 -08003798 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003799 mutex_unlock(&mdsc->mutex);
3800 mutex_lock(&session->s_mutex);
3801 remove_session_caps(session);
3802 mutex_unlock(&session->s_mutex);
3803 ceph_put_mds_session(session);
3804 mutex_lock(&mdsc->mutex);
3805 }
3806 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003807 WARN_ON(!list_empty(&mdsc->cap_delay_list));
Sage Weil2f2dc052009-10-06 11:31:09 -07003808 mutex_unlock(&mdsc->mutex);
3809
3810 ceph_cleanup_empty_realms(mdsc);
3811
3812 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3813
3814 dout("stopped\n");
3815}
3816
Yan, Zheng48fec5d2015-07-01 16:27:46 +08003817void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc)
3818{
3819 struct ceph_mds_session *session;
3820 int mds;
3821
3822 dout("force umount\n");
3823
3824 mutex_lock(&mdsc->mutex);
3825 for (mds = 0; mds < mdsc->max_sessions; mds++) {
3826 session = __ceph_lookup_mds_session(mdsc, mds);
3827 if (!session)
3828 continue;
3829 mutex_unlock(&mdsc->mutex);
3830 mutex_lock(&session->s_mutex);
3831 __close_session(mdsc, session);
3832 if (session->s_state == CEPH_MDS_SESSION_CLOSING) {
3833 cleanup_session_requests(mdsc, session);
3834 remove_session_caps(session);
3835 }
3836 mutex_unlock(&session->s_mutex);
3837 ceph_put_mds_session(session);
3838 mutex_lock(&mdsc->mutex);
3839 kick_requests(mdsc, mds);
3840 }
3841 __wake_requests(mdsc, &mdsc->waiting_for_map);
3842 mutex_unlock(&mdsc->mutex);
3843}
3844
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003845static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07003846{
3847 dout("stop\n");
3848 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3849 if (mdsc->mdsmap)
3850 ceph_mdsmap_destroy(mdsc->mdsmap);
3851 kfree(mdsc->sessions);
Yehuda Sadeh37151662010-06-17 16:16:12 -07003852 ceph_caps_finalize(mdsc);
Yan, Zheng10183a62015-04-27 15:33:28 +08003853 ceph_pool_perm_destroy(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003854}
3855
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003856void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
3857{
3858 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weilef550f62011-03-25 13:27:48 -07003859 dout("mdsc_destroy %p\n", mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07003860
3861 /* flush out any connection work with references to us */
3862 ceph_msgr_flush();
3863
Yan, Zheng62a65f32017-06-22 16:26:34 +08003864 ceph_mdsc_stop(mdsc);
3865
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003866 fsc->mdsc = NULL;
3867 kfree(mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07003868 dout("mdsc_destroy %p done\n", mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003869}
3870
Yan, Zheng430afba2016-07-08 11:25:38 +08003871void ceph_mdsc_handle_fsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3872{
3873 struct ceph_fs_client *fsc = mdsc->fsc;
3874 const char *mds_namespace = fsc->mount_options->mds_namespace;
3875 void *p = msg->front.iov_base;
3876 void *end = p + msg->front.iov_len;
3877 u32 epoch;
3878 u32 map_len;
3879 u32 num_fs;
3880 u32 mount_fscid = (u32)-1;
3881 u8 struct_v, struct_cv;
3882 int err = -EINVAL;
3883
3884 ceph_decode_need(&p, end, sizeof(u32), bad);
3885 epoch = ceph_decode_32(&p);
3886
3887 dout("handle_fsmap epoch %u\n", epoch);
3888
3889 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
3890 struct_v = ceph_decode_8(&p);
3891 struct_cv = ceph_decode_8(&p);
3892 map_len = ceph_decode_32(&p);
3893
3894 ceph_decode_need(&p, end, sizeof(u32) * 3, bad);
3895 p += sizeof(u32) * 2; /* skip epoch and legacy_client_fscid */
3896
3897 num_fs = ceph_decode_32(&p);
3898 while (num_fs-- > 0) {
3899 void *info_p, *info_end;
3900 u32 info_len;
3901 u8 info_v, info_cv;
3902 u32 fscid, namelen;
3903
3904 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
3905 info_v = ceph_decode_8(&p);
3906 info_cv = ceph_decode_8(&p);
3907 info_len = ceph_decode_32(&p);
3908 ceph_decode_need(&p, end, info_len, bad);
3909 info_p = p;
3910 info_end = p + info_len;
3911 p = info_end;
3912
3913 ceph_decode_need(&info_p, info_end, sizeof(u32) * 2, bad);
3914 fscid = ceph_decode_32(&info_p);
3915 namelen = ceph_decode_32(&info_p);
3916 ceph_decode_need(&info_p, info_end, namelen, bad);
3917
3918 if (mds_namespace &&
3919 strlen(mds_namespace) == namelen &&
3920 !strncmp(mds_namespace, (char *)info_p, namelen)) {
3921 mount_fscid = fscid;
3922 break;
3923 }
3924 }
3925
3926 ceph_monc_got_map(&fsc->client->monc, CEPH_SUB_FSMAP, epoch);
3927 if (mount_fscid != (u32)-1) {
3928 fsc->client->monc.fs_cluster_id = mount_fscid;
3929 ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP,
3930 0, true);
3931 ceph_monc_renew_subs(&fsc->client->monc);
3932 } else {
3933 err = -ENOENT;
3934 goto err_out;
3935 }
3936 return;
Ilya Dryomov76bd6ec2017-10-16 10:32:50 +02003937
Yan, Zheng430afba2016-07-08 11:25:38 +08003938bad:
3939 pr_err("error decoding fsmap\n");
3940err_out:
3941 mutex_lock(&mdsc->mutex);
Ilya Dryomov76bd6ec2017-10-16 10:32:50 +02003942 mdsc->mdsmap_err = err;
Yan, Zheng430afba2016-07-08 11:25:38 +08003943 __wake_requests(mdsc, &mdsc->waiting_for_map);
3944 mutex_unlock(&mdsc->mutex);
Yan, Zheng430afba2016-07-08 11:25:38 +08003945}
Sage Weil2f2dc052009-10-06 11:31:09 -07003946
3947/*
3948 * handle mds map update.
3949 */
Yan, Zheng430afba2016-07-08 11:25:38 +08003950void ceph_mdsc_handle_mdsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07003951{
3952 u32 epoch;
3953 u32 maplen;
3954 void *p = msg->front.iov_base;
3955 void *end = p + msg->front.iov_len;
3956 struct ceph_mdsmap *newmap, *oldmap;
3957 struct ceph_fsid fsid;
3958 int err = -EINVAL;
3959
3960 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
3961 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003962 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
Sage Weil07433042009-11-18 16:50:41 -08003963 return;
Sage Weilc89136e2009-10-14 09:59:09 -07003964 epoch = ceph_decode_32(&p);
3965 maplen = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07003966 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
3967
3968 /* do we need it? */
Sage Weil2f2dc052009-10-06 11:31:09 -07003969 mutex_lock(&mdsc->mutex);
3970 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
3971 dout("handle_map epoch %u <= our %u\n",
3972 epoch, mdsc->mdsmap->m_epoch);
3973 mutex_unlock(&mdsc->mutex);
3974 return;
3975 }
3976
3977 newmap = ceph_mdsmap_decode(&p, end);
3978 if (IS_ERR(newmap)) {
3979 err = PTR_ERR(newmap);
3980 goto bad_unlock;
3981 }
3982
3983 /* swap into place */
3984 if (mdsc->mdsmap) {
3985 oldmap = mdsc->mdsmap;
3986 mdsc->mdsmap = newmap;
3987 check_new_map(mdsc, newmap, oldmap);
3988 ceph_mdsmap_destroy(oldmap);
3989 } else {
3990 mdsc->mdsmap = newmap; /* first mds map */
3991 }
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003992 mdsc->fsc->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
Sage Weil2f2dc052009-10-06 11:31:09 -07003993
3994 __wake_requests(mdsc, &mdsc->waiting_for_map);
Ilya Dryomov82dcaba2016-01-19 16:19:06 +01003995 ceph_monc_got_map(&mdsc->fsc->client->monc, CEPH_SUB_MDSMAP,
3996 mdsc->mdsmap->m_epoch);
Sage Weil2f2dc052009-10-06 11:31:09 -07003997
3998 mutex_unlock(&mdsc->mutex);
3999 schedule_delayed(mdsc);
4000 return;
4001
4002bad_unlock:
4003 mutex_unlock(&mdsc->mutex);
4004bad:
4005 pr_err("error decoding mdsmap %d\n", err);
4006 return;
4007}
4008
4009static struct ceph_connection *con_get(struct ceph_connection *con)
4010{
4011 struct ceph_mds_session *s = con->private;
4012
4013 if (get_session(s)) {
Elena Reshetova3997c012017-03-03 11:15:06 +02004014 dout("mdsc con_get %p ok (%d)\n", s, refcount_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -07004015 return con;
4016 }
4017 dout("mdsc con_get %p FAIL\n", s);
4018 return NULL;
4019}
4020
4021static void con_put(struct ceph_connection *con)
4022{
4023 struct ceph_mds_session *s = con->private;
4024
Elena Reshetova3997c012017-03-03 11:15:06 +02004025 dout("mdsc con_put %p (%d)\n", s, refcount_read(&s->s_ref) - 1);
Sage Weil2f2dc052009-10-06 11:31:09 -07004026 ceph_put_mds_session(s);
4027}
4028
4029/*
4030 * if the client is unresponsive for long enough, the mds will kill
4031 * the session entirely.
4032 */
4033static void peer_reset(struct ceph_connection *con)
4034{
4035 struct ceph_mds_session *s = con->private;
Sage Weil7e70f0e2010-03-18 13:59:12 -07004036 struct ceph_mds_client *mdsc = s->s_mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07004037
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07004038 pr_warn("mds%d closed our session\n", s->s_mds);
Sage Weil7e70f0e2010-03-18 13:59:12 -07004039 send_mds_reconnect(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07004040}
4041
4042static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
4043{
4044 struct ceph_mds_session *s = con->private;
4045 struct ceph_mds_client *mdsc = s->s_mdsc;
4046 int type = le16_to_cpu(msg->hdr.type);
4047
Sage Weil2600d2d2010-02-22 15:12:16 -08004048 mutex_lock(&mdsc->mutex);
4049 if (__verify_registered_session(mdsc, s) < 0) {
4050 mutex_unlock(&mdsc->mutex);
4051 goto out;
4052 }
4053 mutex_unlock(&mdsc->mutex);
4054
Sage Weil2f2dc052009-10-06 11:31:09 -07004055 switch (type) {
4056 case CEPH_MSG_MDS_MAP:
Yan, Zheng430afba2016-07-08 11:25:38 +08004057 ceph_mdsc_handle_mdsmap(mdsc, msg);
4058 break;
4059 case CEPH_MSG_FS_MAP_USER:
4060 ceph_mdsc_handle_fsmap(mdsc, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004061 break;
4062 case CEPH_MSG_CLIENT_SESSION:
4063 handle_session(s, msg);
4064 break;
4065 case CEPH_MSG_CLIENT_REPLY:
4066 handle_reply(s, msg);
4067 break;
4068 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
Sage Weil2600d2d2010-02-22 15:12:16 -08004069 handle_forward(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004070 break;
4071 case CEPH_MSG_CLIENT_CAPS:
4072 ceph_handle_caps(s, msg);
4073 break;
4074 case CEPH_MSG_CLIENT_SNAP:
Sage Weil2600d2d2010-02-22 15:12:16 -08004075 ceph_handle_snap(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004076 break;
4077 case CEPH_MSG_CLIENT_LEASE:
Sage Weil2600d2d2010-02-22 15:12:16 -08004078 handle_lease(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004079 break;
4080
4081 default:
4082 pr_err("received unknown message type %d %s\n", type,
4083 ceph_msg_type_name(type));
4084 }
Sage Weil2600d2d2010-02-22 15:12:16 -08004085out:
Sage Weil2f2dc052009-10-06 11:31:09 -07004086 ceph_msg_put(msg);
4087}
4088
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004089/*
4090 * authentication
4091 */
Alex Eldera3530df2012-05-16 15:16:39 -05004092
4093/*
4094 * Note: returned pointer is the address of a structure that's
4095 * managed separately. Caller must *not* attempt to free it.
4096 */
4097static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05004098 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004099{
4100 struct ceph_mds_session *s = con->private;
4101 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004102 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05004103 struct ceph_auth_handshake *auth = &s->s_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004104
Alex Elder74f18692012-05-16 15:16:39 -05004105 if (force_new && auth->authorizer) {
Ilya Dryomov6c1ea262016-04-11 19:34:49 +02004106 ceph_auth_destroy_authorizer(auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05004107 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004108 }
Sage Weil27859f92013-03-25 10:26:14 -07004109 if (!auth->authorizer) {
4110 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4111 auth);
Sage Weil0bed9b52013-03-25 10:26:01 -07004112 if (ret)
4113 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07004114 } else {
4115 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4116 auth);
Alex Eldera2556512012-05-16 15:16:39 -05004117 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05004118 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004119 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004120 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05004121
Alex Eldera3530df2012-05-16 15:16:39 -05004122 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004123}
4124
4125
Ilya Dryomov0dde5842016-12-02 16:35:09 +01004126static int verify_authorizer_reply(struct ceph_connection *con)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004127{
4128 struct ceph_mds_session *s = con->private;
4129 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004130 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004131
Ilya Dryomov0dde5842016-12-02 16:35:09 +01004132 return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004133}
4134
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004135static int invalidate_authorizer(struct ceph_connection *con)
4136{
4137 struct ceph_mds_session *s = con->private;
4138 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004139 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004140
Sage Weil27859f92013-03-25 10:26:14 -07004141 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004142
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004143 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004144}
4145
Alex Elder53ded492013-03-01 18:00:14 -06004146static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
4147 struct ceph_msg_header *hdr, int *skip)
4148{
4149 struct ceph_msg *msg;
4150 int type = (int) le16_to_cpu(hdr->type);
4151 int front_len = (int) le32_to_cpu(hdr->front_len);
4152
4153 if (con->in_msg)
4154 return con->in_msg;
4155
4156 *skip = 0;
4157 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
4158 if (!msg) {
4159 pr_err("unable to allocate msg type %d len %d\n",
4160 type, front_len);
4161 return NULL;
4162 }
Alex Elder53ded492013-03-01 18:00:14 -06004163
4164 return msg;
4165}
4166
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004167static int mds_sign_message(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08004168{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004169 struct ceph_mds_session *s = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08004170 struct ceph_auth_handshake *auth = &s->s_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004171
Yan, Zheng33d07332014-11-04 16:33:37 +08004172 return ceph_auth_sign_message(auth, msg);
4173}
4174
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004175static int mds_check_message_signature(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08004176{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004177 struct ceph_mds_session *s = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08004178 struct ceph_auth_handshake *auth = &s->s_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004179
Yan, Zheng33d07332014-11-04 16:33:37 +08004180 return ceph_auth_check_message_signature(auth, msg);
4181}
4182
Tobias Klauser9e327892010-05-20 10:40:19 +02004183static const struct ceph_connection_operations mds_con_ops = {
Sage Weil2f2dc052009-10-06 11:31:09 -07004184 .get = con_get,
4185 .put = con_put,
4186 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004187 .get_authorizer = get_authorizer,
4188 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004189 .invalidate_authorizer = invalidate_authorizer,
Sage Weil2f2dc052009-10-06 11:31:09 -07004190 .peer_reset = peer_reset,
Alex Elder53ded492013-03-01 18:00:14 -06004191 .alloc_msg = mds_alloc_msg,
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004192 .sign_message = mds_sign_message,
4193 .check_message_signature = mds_check_message_signature,
Sage Weil2f2dc052009-10-06 11:31:09 -07004194};
4195
Sage Weil2f2dc052009-10-06 11:31:09 -07004196/* eof */