blob: dc8bc664a8716a5c946c70efa2dc5911cfb32962 [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
Luis Henriquesfb18a572018-01-05 10:47:18 +0000103 if (features & CEPH_FEATURE_MDS_QUOTA) {
104 u8 struct_v, struct_compat;
105 u32 struct_len;
106
107 /*
108 * both struct_v and struct_compat are expected to be >= 1
109 */
110 ceph_decode_8_safe(p, end, struct_v, bad);
111 ceph_decode_8_safe(p, end, struct_compat, bad);
112 if (!struct_v || !struct_compat)
113 goto bad;
114 ceph_decode_32_safe(p, end, struct_len, bad);
115 ceph_decode_need(p, end, struct_len, bad);
116 ceph_decode_64_safe(p, end, info->max_bytes, bad);
117 ceph_decode_64_safe(p, end, info->max_files, bad);
118 } else {
119 info->max_bytes = 0;
120 info->max_files = 0;
121 }
122
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800123 info->pool_ns_len = 0;
124 info->pool_ns_data = NULL;
Yan, Zheng5ea5c5e2016-02-14 18:06:41 +0800125 if (features & CEPH_FEATURE_FS_FILE_LAYOUT_V2) {
126 ceph_decode_32_safe(p, end, info->pool_ns_len, bad);
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800127 if (info->pool_ns_len > 0) {
128 ceph_decode_need(p, end, info->pool_ns_len, bad);
129 info->pool_ns_data = *p;
130 *p += info->pool_ns_len;
131 }
Yan, Zheng5ea5c5e2016-02-14 18:06:41 +0800132 }
133
Sage Weil2f2dc052009-10-06 11:31:09 -0700134 return 0;
135bad:
136 return err;
137}
138
139/*
140 * parse a normal reply, which may contain a (dir+)dentry and/or a
141 * target inode.
142 */
143static int parse_reply_info_trace(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800144 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200145 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700146{
147 int err;
148
149 if (info->head->is_dentry) {
Sage Weil14303d22010-12-14 17:37:52 -0800150 err = parse_reply_info_in(p, end, &info->diri, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700151 if (err < 0)
152 goto out_bad;
153
154 if (unlikely(*p + sizeof(*info->dirfrag) > end))
155 goto bad;
156 info->dirfrag = *p;
157 *p += sizeof(*info->dirfrag) +
158 sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
159 if (unlikely(*p > end))
160 goto bad;
161
162 ceph_decode_32_safe(p, end, info->dname_len, bad);
163 ceph_decode_need(p, end, info->dname_len, bad);
164 info->dname = *p;
165 *p += info->dname_len;
166 info->dlease = *p;
167 *p += sizeof(*info->dlease);
168 }
169
170 if (info->head->is_target) {
Sage Weil14303d22010-12-14 17:37:52 -0800171 err = parse_reply_info_in(p, end, &info->targeti, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700172 if (err < 0)
173 goto out_bad;
174 }
175
176 if (unlikely(*p != end))
177 goto bad;
178 return 0;
179
180bad:
181 err = -EIO;
182out_bad:
183 pr_err("problem parsing mds trace %d\n", err);
184 return err;
185}
186
187/*
188 * parse readdir results
189 */
190static int parse_reply_info_dir(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800191 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200192 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700193{
194 u32 num, i = 0;
195 int err;
196
197 info->dir_dir = *p;
198 if (*p + sizeof(*info->dir_dir) > end)
199 goto bad;
200 *p += sizeof(*info->dir_dir) +
201 sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
202 if (*p > end)
203 goto bad;
204
205 ceph_decode_need(p, end, sizeof(num) + 2, bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700206 num = ceph_decode_32(p);
Yan, Zheng956d39d2016-04-27 17:48:30 +0800207 {
208 u16 flags = ceph_decode_16(p);
209 info->dir_end = !!(flags & CEPH_READDIR_FRAG_END);
210 info->dir_complete = !!(flags & CEPH_READDIR_FRAG_COMPLETE);
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800211 info->hash_order = !!(flags & CEPH_READDIR_HASH_ORDER);
Yan, Zheng79162542017-04-05 12:54:05 -0400212 info->offset_hash = !!(flags & CEPH_READDIR_OFFSET_HASH);
Yan, Zheng956d39d2016-04-27 17:48:30 +0800213 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700214 if (num == 0)
215 goto done;
216
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800217 BUG_ON(!info->dir_entries);
218 if ((unsigned long)(info->dir_entries + num) >
219 (unsigned long)info->dir_entries + info->dir_buf_size) {
Yan, Zheng54008392014-03-29 13:41:15 +0800220 pr_err("dir contents are larger than expected\n");
221 WARN_ON(1);
222 goto bad;
223 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700224
Yan, Zheng54008392014-03-29 13:41:15 +0800225 info->dir_nr = num;
Sage Weil2f2dc052009-10-06 11:31:09 -0700226 while (num) {
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800227 struct ceph_mds_reply_dir_entry *rde = info->dir_entries + i;
Sage Weil2f2dc052009-10-06 11:31:09 -0700228 /* dentry */
229 ceph_decode_need(p, end, sizeof(u32)*2, bad);
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800230 rde->name_len = ceph_decode_32(p);
231 ceph_decode_need(p, end, rde->name_len, bad);
232 rde->name = *p;
233 *p += rde->name_len;
234 dout("parsed dir dname '%.*s'\n", rde->name_len, rde->name);
235 rde->lease = *p;
Sage Weil2f2dc052009-10-06 11:31:09 -0700236 *p += sizeof(struct ceph_mds_reply_lease);
237
238 /* inode */
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800239 err = parse_reply_info_in(p, end, &rde->inode, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700240 if (err < 0)
241 goto out_bad;
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800242 /* ceph_readdir_prepopulate() will update it */
243 rde->offset = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -0700244 i++;
245 num--;
246 }
247
248done:
249 if (*p != end)
250 goto bad;
251 return 0;
252
253bad:
254 err = -EIO;
255out_bad:
256 pr_err("problem parsing dir contents %d\n", err);
257 return err;
258}
259
260/*
Herb Shiu25933ab2010-12-01 14:14:38 -0800261 * parse fcntl F_GETLK results
262 */
263static int parse_reply_info_filelock(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800264 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200265 u64 features)
Herb Shiu25933ab2010-12-01 14:14:38 -0800266{
267 if (*p + sizeof(*info->filelock_reply) > end)
268 goto bad;
269
270 info->filelock_reply = *p;
271 *p += sizeof(*info->filelock_reply);
272
273 if (unlikely(*p != end))
274 goto bad;
275 return 0;
276
277bad:
278 return -EIO;
279}
280
281/*
Sam Lang6e8575f2012-12-28 09:56:46 -0800282 * parse create results
283 */
284static int parse_reply_info_create(void **p, void *end,
285 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200286 u64 features)
Sam Lang6e8575f2012-12-28 09:56:46 -0800287{
288 if (features & CEPH_FEATURE_REPLY_CREATE_INODE) {
289 if (*p == end) {
290 info->has_create_ino = false;
291 } else {
292 info->has_create_ino = true;
293 info->ino = ceph_decode_64(p);
294 }
295 }
296
297 if (unlikely(*p != end))
298 goto bad;
299 return 0;
300
301bad:
302 return -EIO;
303}
304
305/*
Herb Shiu25933ab2010-12-01 14:14:38 -0800306 * parse extra results
307 */
308static int parse_reply_info_extra(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800309 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200310 u64 features)
Herb Shiu25933ab2010-12-01 14:14:38 -0800311{
Jeff Layton6df8c9d2017-01-12 14:42:41 -0500312 u32 op = le32_to_cpu(info->head->op);
313
314 if (op == CEPH_MDS_OP_GETFILELOCK)
Sage Weil14303d22010-12-14 17:37:52 -0800315 return parse_reply_info_filelock(p, end, info, features);
Jeff Layton6df8c9d2017-01-12 14:42:41 -0500316 else if (op == CEPH_MDS_OP_READDIR || op == CEPH_MDS_OP_LSSNAP)
Sage Weil14303d22010-12-14 17:37:52 -0800317 return parse_reply_info_dir(p, end, info, features);
Jeff Layton6df8c9d2017-01-12 14:42:41 -0500318 else if (op == CEPH_MDS_OP_CREATE)
Sam Lang6e8575f2012-12-28 09:56:46 -0800319 return parse_reply_info_create(p, end, info, features);
320 else
321 return -EIO;
Herb Shiu25933ab2010-12-01 14:14:38 -0800322}
323
324/*
Sage Weil2f2dc052009-10-06 11:31:09 -0700325 * parse entire mds reply
326 */
327static int parse_reply_info(struct ceph_msg *msg,
Sage Weil14303d22010-12-14 17:37:52 -0800328 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200329 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700330{
331 void *p, *end;
332 u32 len;
333 int err;
334
335 info->head = msg->front.iov_base;
336 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
337 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
338
339 /* trace */
340 ceph_decode_32_safe(&p, end, len, bad);
341 if (len > 0) {
Xi Wang32852a82012-01-14 22:20:59 -0500342 ceph_decode_need(&p, end, len, bad);
Sage Weil14303d22010-12-14 17:37:52 -0800343 err = parse_reply_info_trace(&p, p+len, info, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700344 if (err < 0)
345 goto out_bad;
346 }
347
Herb Shiu25933ab2010-12-01 14:14:38 -0800348 /* extra */
Sage Weil2f2dc052009-10-06 11:31:09 -0700349 ceph_decode_32_safe(&p, end, len, bad);
350 if (len > 0) {
Xi Wang32852a82012-01-14 22:20:59 -0500351 ceph_decode_need(&p, end, len, bad);
Sage Weil14303d22010-12-14 17:37:52 -0800352 err = parse_reply_info_extra(&p, p+len, info, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700353 if (err < 0)
354 goto out_bad;
355 }
356
357 /* snap blob */
358 ceph_decode_32_safe(&p, end, len, bad);
359 info->snapblob_len = len;
360 info->snapblob = p;
361 p += len;
362
363 if (p != end)
364 goto bad;
365 return 0;
366
367bad:
368 err = -EIO;
369out_bad:
370 pr_err("mds parse_reply err %d\n", err);
371 return err;
372}
373
374static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
375{
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800376 if (!info->dir_entries)
Yan, Zheng54008392014-03-29 13:41:15 +0800377 return;
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800378 free_pages((unsigned long)info->dir_entries, get_order(info->dir_buf_size));
Sage Weil2f2dc052009-10-06 11:31:09 -0700379}
380
381
382/*
383 * sessions
384 */
John Spraya687eca2014-09-19 13:51:08 +0100385const char *ceph_session_state_name(int s)
Sage Weil2f2dc052009-10-06 11:31:09 -0700386{
387 switch (s) {
388 case CEPH_MDS_SESSION_NEW: return "new";
389 case CEPH_MDS_SESSION_OPENING: return "opening";
390 case CEPH_MDS_SESSION_OPEN: return "open";
391 case CEPH_MDS_SESSION_HUNG: return "hung";
392 case CEPH_MDS_SESSION_CLOSING: return "closing";
Sage Weil44ca18f2010-02-15 12:08:46 -0800393 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
Sage Weil2f2dc052009-10-06 11:31:09 -0700394 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
Yan, Zhengfcff4152016-09-14 16:39:51 +0800395 case CEPH_MDS_SESSION_REJECTED: return "rejected";
Sage Weil2f2dc052009-10-06 11:31:09 -0700396 default: return "???";
397 }
398}
399
400static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
401{
Elena Reshetova3997c012017-03-03 11:15:06 +0200402 if (refcount_inc_not_zero(&s->s_ref)) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700403 dout("mdsc get_session %p %d -> %d\n", s,
Elena Reshetova3997c012017-03-03 11:15:06 +0200404 refcount_read(&s->s_ref)-1, refcount_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -0700405 return s;
406 } else {
Chengguang Xu4c069a52018-01-30 16:29:17 +0800407 dout("mdsc get_session %p 0 -- FAIL\n", s);
Sage Weil2f2dc052009-10-06 11:31:09 -0700408 return NULL;
409 }
410}
411
412void ceph_put_mds_session(struct ceph_mds_session *s)
413{
414 dout("mdsc put_session %p %d -> %d\n", s,
Elena Reshetova3997c012017-03-03 11:15:06 +0200415 refcount_read(&s->s_ref), refcount_read(&s->s_ref)-1);
416 if (refcount_dec_and_test(&s->s_ref)) {
Alex Elder6c4a1912012-05-16 15:16:38 -0500417 if (s->s_auth.authorizer)
Ilya Dryomov6c1ea262016-04-11 19:34:49 +0200418 ceph_auth_destroy_authorizer(s->s_auth.authorizer);
Sage Weil2f2dc052009-10-06 11:31:09 -0700419 kfree(s);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800420 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700421}
422
423/*
424 * called under mdsc->mutex
425 */
426struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
427 int mds)
428{
429 struct ceph_mds_session *session;
430
Markus Elfringd37b1d92017-08-20 20:22:02 +0200431 if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
Sage Weil2f2dc052009-10-06 11:31:09 -0700432 return NULL;
433 session = mdsc->sessions[mds];
434 dout("lookup_mds_session %p %d\n", session,
Elena Reshetova3997c012017-03-03 11:15:06 +0200435 refcount_read(&session->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -0700436 get_session(session);
437 return session;
438}
439
440static bool __have_session(struct ceph_mds_client *mdsc, int mds)
441{
Chengguang Xu98cfda82018-03-13 10:43:45 +0800442 if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
Sage Weil2f2dc052009-10-06 11:31:09 -0700443 return false;
Chengguang Xu98cfda82018-03-13 10:43:45 +0800444 else
445 return true;
Sage Weil2f2dc052009-10-06 11:31:09 -0700446}
447
Sage Weil2600d2d2010-02-22 15:12:16 -0800448static int __verify_registered_session(struct ceph_mds_client *mdsc,
449 struct ceph_mds_session *s)
450{
451 if (s->s_mds >= mdsc->max_sessions ||
452 mdsc->sessions[s->s_mds] != s)
453 return -ENOENT;
454 return 0;
455}
456
Sage Weil2f2dc052009-10-06 11:31:09 -0700457/*
458 * create+register a new session for given mds.
459 * called under mdsc->mutex.
460 */
461static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
462 int mds)
463{
464 struct ceph_mds_session *s;
465
Yan, Zheng76201b62017-03-28 17:04:13 +0800466 if (mds >= mdsc->mdsmap->m_num_mds)
Nathaniel Yazdanic338c072013-08-04 21:04:30 -0700467 return ERR_PTR(-EINVAL);
468
Sage Weil2f2dc052009-10-06 11:31:09 -0700469 s = kzalloc(sizeof(*s), GFP_NOFS);
Dan Carpenter4736b002010-03-20 15:30:16 +0300470 if (!s)
471 return ERR_PTR(-ENOMEM);
Chengguang Xu47474d02018-03-13 23:01:07 +0800472
473 if (mds >= mdsc->max_sessions) {
474 int newmax = 1 << get_count_order(mds + 1);
475 struct ceph_mds_session **sa;
476
477 dout("%s: realloc to %d\n", __func__, newmax);
478 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
479 if (!sa)
480 goto fail_realloc;
481 if (mdsc->sessions) {
482 memcpy(sa, mdsc->sessions,
483 mdsc->max_sessions * sizeof(void *));
484 kfree(mdsc->sessions);
485 }
486 mdsc->sessions = sa;
487 mdsc->max_sessions = newmax;
488 }
489
490 dout("%s: mds%d\n", __func__, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -0700491 s->s_mdsc = mdsc;
492 s->s_mds = mds;
493 s->s_state = CEPH_MDS_SESSION_NEW;
494 s->s_ttl = 0;
495 s->s_seq = 0;
496 mutex_init(&s->s_mutex);
497
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700498 ceph_con_init(&s->s_con, s, &mds_con_ops, &mdsc->fsc->client->msgr);
Sage Weil2f2dc052009-10-06 11:31:09 -0700499
Alex Elderd8fb02a2012-01-12 17:48:10 -0800500 spin_lock_init(&s->s_gen_ttl_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700501 s->s_cap_gen = 0;
Alex Elder1ce208a2012-01-12 17:48:11 -0800502 s->s_cap_ttl = jiffies - 1;
Alex Elderd8fb02a2012-01-12 17:48:10 -0800503
504 spin_lock_init(&s->s_cap_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700505 s->s_renew_requested = 0;
506 s->s_renew_seq = 0;
507 INIT_LIST_HEAD(&s->s_caps);
508 s->s_nr_caps = 0;
Sage Weil5dacf092009-12-21 20:40:34 -0800509 s->s_trim_caps = 0;
Elena Reshetova3997c012017-03-03 11:15:06 +0200510 refcount_set(&s->s_ref, 1);
Sage Weil2f2dc052009-10-06 11:31:09 -0700511 INIT_LIST_HEAD(&s->s_waiting);
512 INIT_LIST_HEAD(&s->s_unsafe);
513 s->s_num_cap_releases = 0;
Yan, Zheng99a9c272013-09-22 11:08:14 +0800514 s->s_cap_reconnect = 0;
Sage Weil7c1332b2010-02-16 11:39:45 -0800515 s->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700516 INIT_LIST_HEAD(&s->s_cap_releases);
Sage Weil2f2dc052009-10-06 11:31:09 -0700517 INIT_LIST_HEAD(&s->s_cap_flushing);
Sage Weil2f2dc052009-10-06 11:31:09 -0700518
Sage Weil2f2dc052009-10-06 11:31:09 -0700519 mdsc->sessions[mds] = s;
Yan, Zheng86d8f672015-01-09 17:00:42 +0800520 atomic_inc(&mdsc->num_sessions);
Elena Reshetova3997c012017-03-03 11:15:06 +0200521 refcount_inc(&s->s_ref); /* one ref to sessions[], one to caller */
Sage Weil42ce56e2009-11-18 11:22:36 -0800522
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700523 ceph_con_open(&s->s_con, CEPH_ENTITY_TYPE_MDS, mds,
524 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
Sage Weil42ce56e2009-11-18 11:22:36 -0800525
Sage Weil2f2dc052009-10-06 11:31:09 -0700526 return s;
Sage Weil42ce56e2009-11-18 11:22:36 -0800527
528fail_realloc:
529 kfree(s);
530 return ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -0700531}
532
533/*
534 * called under mdsc->mutex
535 */
Sage Weil2600d2d2010-02-22 15:12:16 -0800536static void __unregister_session(struct ceph_mds_client *mdsc,
Sage Weil42ce56e2009-11-18 11:22:36 -0800537 struct ceph_mds_session *s)
Sage Weil2f2dc052009-10-06 11:31:09 -0700538{
Sage Weil2600d2d2010-02-22 15:12:16 -0800539 dout("__unregister_session mds%d %p\n", s->s_mds, s);
540 BUG_ON(mdsc->sessions[s->s_mds] != s);
Sage Weil42ce56e2009-11-18 11:22:36 -0800541 mdsc->sessions[s->s_mds] = NULL;
542 ceph_con_close(&s->s_con);
543 ceph_put_mds_session(s);
Yan, Zheng86d8f672015-01-09 17:00:42 +0800544 atomic_dec(&mdsc->num_sessions);
Sage Weil2f2dc052009-10-06 11:31:09 -0700545}
546
547/*
548 * drop session refs in request.
549 *
550 * should be last request ref, or hold mdsc->mutex
551 */
552static void put_request_session(struct ceph_mds_request *req)
553{
554 if (req->r_session) {
555 ceph_put_mds_session(req->r_session);
556 req->r_session = NULL;
557 }
558}
559
Sage Weil153c8e62009-12-07 12:31:09 -0800560void ceph_mdsc_release_request(struct kref *kref)
Sage Weil2f2dc052009-10-06 11:31:09 -0700561{
Sage Weil153c8e62009-12-07 12:31:09 -0800562 struct ceph_mds_request *req = container_of(kref,
563 struct ceph_mds_request,
564 r_kref);
Yan, Zheng54008392014-03-29 13:41:15 +0800565 destroy_reply_info(&req->r_reply_info);
Sage Weil153c8e62009-12-07 12:31:09 -0800566 if (req->r_request)
567 ceph_msg_put(req->r_request);
Yan, Zheng54008392014-03-29 13:41:15 +0800568 if (req->r_reply)
Sage Weil153c8e62009-12-07 12:31:09 -0800569 ceph_msg_put(req->r_reply);
Sage Weil153c8e62009-12-07 12:31:09 -0800570 if (req->r_inode) {
Sage Weil41b02e12011-07-26 11:31:14 -0700571 ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
Sage Weil153c8e62009-12-07 12:31:09 -0800572 iput(req->r_inode);
573 }
Jeff Layton3dd69aa2017-01-31 10:28:26 -0500574 if (req->r_parent)
575 ceph_put_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
SF Markus Elfringe96a6502014-11-02 15:20:59 +0100576 iput(req->r_target_inode);
Sage Weil153c8e62009-12-07 12:31:09 -0800577 if (req->r_dentry)
578 dput(req->r_dentry);
Sage Weil844d87c2013-02-05 13:40:09 -0800579 if (req->r_old_dentry)
580 dput(req->r_old_dentry);
581 if (req->r_old_dentry_dir) {
Sage Weil41b02e12011-07-26 11:31:14 -0700582 /*
583 * track (and drop pins for) r_old_dentry_dir
584 * separately, since r_old_dentry's d_parent may have
585 * changed between the dir mutex being dropped and
586 * this request being freed.
587 */
588 ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir),
589 CEPH_CAP_PIN);
Sage Weil41b02e12011-07-26 11:31:14 -0700590 iput(req->r_old_dentry_dir);
Sage Weil153c8e62009-12-07 12:31:09 -0800591 }
592 kfree(req->r_path1);
593 kfree(req->r_path2);
Yan, Zheng25e6bae2014-09-16 19:15:28 +0800594 if (req->r_pagelist)
595 ceph_pagelist_release(req->r_pagelist);
Sage Weil153c8e62009-12-07 12:31:09 -0800596 put_request_session(req);
Yehuda Sadeh37151662010-06-17 16:16:12 -0700597 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
Sage Weil153c8e62009-12-07 12:31:09 -0800598 kfree(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700599}
600
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200601DEFINE_RB_FUNCS(request, struct ceph_mds_request, r_tid, r_node)
602
Sage Weil2f2dc052009-10-06 11:31:09 -0700603/*
604 * lookup session, bump ref if found.
605 *
606 * called under mdsc->mutex.
607 */
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200608static struct ceph_mds_request *
609lookup_get_request(struct ceph_mds_client *mdsc, u64 tid)
Sage Weil2f2dc052009-10-06 11:31:09 -0700610{
611 struct ceph_mds_request *req;
Sage Weil44ca18f2010-02-15 12:08:46 -0800612
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200613 req = lookup_request(&mdsc->request_tree, tid);
614 if (req)
615 ceph_mdsc_get_request(req);
Sage Weil44ca18f2010-02-15 12:08:46 -0800616
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200617 return req;
Sage Weil2f2dc052009-10-06 11:31:09 -0700618}
619
620/*
621 * Register an in-flight request, and assign a tid. Link to directory
622 * are modifying (if any).
623 *
624 * Called under mdsc->mutex.
625 */
626static void __register_request(struct ceph_mds_client *mdsc,
627 struct ceph_mds_request *req,
628 struct inode *dir)
629{
Zhi Zhange30ee582018-01-24 21:24:33 +0800630 int ret = 0;
631
Sage Weil2f2dc052009-10-06 11:31:09 -0700632 req->r_tid = ++mdsc->last_tid;
Zhi Zhange30ee582018-01-24 21:24:33 +0800633 if (req->r_num_caps) {
634 ret = ceph_reserve_caps(mdsc, &req->r_caps_reservation,
635 req->r_num_caps);
636 if (ret < 0) {
637 pr_err("__register_request %p "
638 "failed to reserve caps: %d\n", req, ret);
639 /* set req->r_err to fail early from __do_request */
640 req->r_err = ret;
641 return;
642 }
643 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700644 dout("__register_request %p tid %lld\n", req, req->r_tid);
645 ceph_mdsc_get_request(req);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200646 insert_request(&mdsc->request_tree, req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700647
Sage Weilcb4276c2010-11-08 07:28:52 -0800648 req->r_uid = current_fsuid();
649 req->r_gid = current_fsgid();
650
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +0800651 if (mdsc->oldest_tid == 0 && req->r_op != CEPH_MDS_OP_SETFILELOCK)
652 mdsc->oldest_tid = req->r_tid;
653
Sage Weil2f2dc052009-10-06 11:31:09 -0700654 if (dir) {
Sage Weil3b663782011-05-18 16:12:12 -0700655 ihold(dir);
Sage Weil2f2dc052009-10-06 11:31:09 -0700656 req->r_unsafe_dir = dir;
Sage Weil2f2dc052009-10-06 11:31:09 -0700657 }
658}
659
660static void __unregister_request(struct ceph_mds_client *mdsc,
661 struct ceph_mds_request *req)
662{
663 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +0800664
Jeff Laytondf963ea2017-02-14 10:09:40 -0500665 /* Never leave an unregistered request on an unsafe list! */
666 list_del_init(&req->r_unsafe_item);
667
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +0800668 if (req->r_tid == mdsc->oldest_tid) {
669 struct rb_node *p = rb_next(&req->r_node);
670 mdsc->oldest_tid = 0;
671 while (p) {
672 struct ceph_mds_request *next_req =
673 rb_entry(p, struct ceph_mds_request, r_node);
674 if (next_req->r_op != CEPH_MDS_OP_SETFILELOCK) {
675 mdsc->oldest_tid = next_req->r_tid;
676 break;
677 }
678 p = rb_next(p);
679 }
680 }
681
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200682 erase_request(&mdsc->request_tree, req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700683
Jeff Laytonbc2de10d2017-02-01 13:49:09 -0500684 if (req->r_unsafe_dir &&
685 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700686 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
Sage Weil2f2dc052009-10-06 11:31:09 -0700687 spin_lock(&ci->i_unsafe_lock);
688 list_del_init(&req->r_unsafe_dir_item);
689 spin_unlock(&ci->i_unsafe_lock);
Yan, Zheng4c06ace2015-10-27 17:18:00 +0800690 }
Jeff Laytonbc2de10d2017-02-01 13:49:09 -0500691 if (req->r_target_inode &&
692 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Yan, Zheng68cd5b4b2015-10-27 18:36:06 +0800693 struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
694 spin_lock(&ci->i_unsafe_lock);
695 list_del_init(&req->r_unsafe_target_item);
696 spin_unlock(&ci->i_unsafe_lock);
697 }
Sage Weil3b663782011-05-18 16:12:12 -0700698
Yan, Zheng4c06ace2015-10-27 17:18:00 +0800699 if (req->r_unsafe_dir) {
Sage Weil3b663782011-05-18 16:12:12 -0700700 iput(req->r_unsafe_dir);
701 req->r_unsafe_dir = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700702 }
Sage Weil94aa8ae2010-03-28 21:22:50 -0700703
Yan, Zhengfc55d2c2013-10-31 09:10:47 +0800704 complete_all(&req->r_safe_completion);
705
Sage Weil94aa8ae2010-03-28 21:22:50 -0700706 ceph_mdsc_put_request(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700707}
708
709/*
Jeff Layton30c71232016-12-15 08:37:56 -0500710 * Walk back up the dentry tree until we hit a dentry representing a
711 * non-snapshot inode. We do this using the rcu_read_lock (which must be held
712 * when calling this) to ensure that the objects won't disappear while we're
713 * working with them. Once we hit a candidate dentry, we attempt to take a
714 * reference to it, and return that as the result.
715 */
Dan Carpenterf1075482017-02-23 13:39:59 +0300716static struct inode *get_nonsnap_parent(struct dentry *dentry)
717{
718 struct inode *inode = NULL;
Jeff Layton30c71232016-12-15 08:37:56 -0500719
720 while (dentry && !IS_ROOT(dentry)) {
721 inode = d_inode_rcu(dentry);
722 if (!inode || ceph_snap(inode) == CEPH_NOSNAP)
723 break;
724 dentry = dentry->d_parent;
725 }
726 if (inode)
727 inode = igrab(inode);
728 return inode;
729}
730
731/*
Sage Weil2f2dc052009-10-06 11:31:09 -0700732 * Choose mds to send request to next. If there is a hint set in the
733 * request (e.g., due to a prior forward hint from the mds), use that.
734 * Otherwise, consult frag tree and/or caps to identify the
735 * appropriate mds. If all else fails, choose randomly.
736 *
737 * Called under mdsc->mutex.
738 */
739static int __choose_mds(struct ceph_mds_client *mdsc,
740 struct ceph_mds_request *req)
741{
742 struct inode *inode;
743 struct ceph_inode_info *ci;
744 struct ceph_cap *cap;
745 int mode = req->r_direct_mode;
746 int mds = -1;
747 u32 hash = req->r_direct_hash;
Jeff Laytonbc2de10d2017-02-01 13:49:09 -0500748 bool is_hash = test_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags);
Sage Weil2f2dc052009-10-06 11:31:09 -0700749
750 /*
751 * is there a specific mds we should try? ignore hint if we have
752 * no session and the mds is not up (active or recovering).
753 */
754 if (req->r_resend_mds >= 0 &&
755 (__have_session(mdsc, req->r_resend_mds) ||
756 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
757 dout("choose_mds using resend_mds mds%d\n",
758 req->r_resend_mds);
759 return req->r_resend_mds;
760 }
761
762 if (mode == USE_RANDOM_MDS)
763 goto random;
764
765 inode = NULL;
766 if (req->r_inode) {
Yan, Zheng5d37ca12017-07-26 12:48:08 +0800767 if (ceph_snap(req->r_inode) != CEPH_SNAPDIR) {
768 inode = req->r_inode;
769 ihold(inode);
770 } else {
Yan, Zheng38f340c2017-09-22 11:41:06 +0800771 /* req->r_dentry is non-null for LSSNAP request */
772 rcu_read_lock();
773 inode = get_nonsnap_parent(req->r_dentry);
774 rcu_read_unlock();
775 dout("__choose_mds using snapdir's parent %p\n", inode);
Yan, Zheng5d37ca12017-07-26 12:48:08 +0800776 }
Yan, Zheng38f340c2017-09-22 11:41:06 +0800777 } else if (req->r_dentry) {
Sage Weild79698d2011-07-26 11:31:26 -0700778 /* ignore race with rename; old or new d_parent is okay */
Jeff Layton30c71232016-12-15 08:37:56 -0500779 struct dentry *parent;
780 struct inode *dir;
Sage Weileb6bb1c2010-08-16 09:21:27 -0700781
Jeff Layton30c71232016-12-15 08:37:56 -0500782 rcu_read_lock();
783 parent = req->r_dentry->d_parent;
Jeff Layton3dd69aa2017-01-31 10:28:26 -0500784 dir = req->r_parent ? : d_inode_rcu(parent);
Jeff Layton30c71232016-12-15 08:37:56 -0500785
786 if (!dir || dir->i_sb != mdsc->fsc->sb) {
787 /* not this fs or parent went negative */
David Howells2b0143b2015-03-17 22:25:59 +0000788 inode = d_inode(req->r_dentry);
Jeff Layton30c71232016-12-15 08:37:56 -0500789 if (inode)
790 ihold(inode);
Sage Weileb6bb1c2010-08-16 09:21:27 -0700791 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
792 /* direct snapped/virtual snapdir requests
793 * based on parent dir inode */
Jeff Layton30c71232016-12-15 08:37:56 -0500794 inode = get_nonsnap_parent(parent);
Sage Weileb6bb1c2010-08-16 09:21:27 -0700795 dout("__choose_mds using nonsnap parent %p\n", inode);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800796 } else {
Sage Weileb6bb1c2010-08-16 09:21:27 -0700797 /* dentry target */
David Howells2b0143b2015-03-17 22:25:59 +0000798 inode = d_inode(req->r_dentry);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800799 if (!inode || mode == USE_AUTH_MDS) {
800 /* dir + name */
Jeff Layton30c71232016-12-15 08:37:56 -0500801 inode = igrab(dir);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800802 hash = ceph_dentry_hash(dir, req->r_dentry);
803 is_hash = true;
Jeff Layton30c71232016-12-15 08:37:56 -0500804 } else {
805 ihold(inode);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800806 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700807 }
Jeff Layton30c71232016-12-15 08:37:56 -0500808 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -0700809 }
Sage Weileb6bb1c2010-08-16 09:21:27 -0700810
Sage Weil2f2dc052009-10-06 11:31:09 -0700811 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
812 (int)hash, mode);
813 if (!inode)
814 goto random;
815 ci = ceph_inode(inode);
816
817 if (is_hash && S_ISDIR(inode->i_mode)) {
818 struct ceph_inode_frag frag;
819 int found;
820
821 ceph_choose_frag(ci, hash, &frag, &found);
822 if (found) {
823 if (mode == USE_ANY_MDS && frag.ndist > 0) {
824 u8 r;
825
826 /* choose a random replica */
827 get_random_bytes(&r, 1);
828 r %= frag.ndist;
829 mds = frag.dist[r];
830 dout("choose_mds %p %llx.%llx "
831 "frag %u mds%d (%d/%d)\n",
832 inode, ceph_vinop(inode),
Sage Weild66bbd42011-01-21 21:16:46 -0800833 frag.frag, mds,
Sage Weil2f2dc052009-10-06 11:31:09 -0700834 (int)r, frag.ndist);
Sage Weild66bbd42011-01-21 21:16:46 -0800835 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
836 CEPH_MDS_STATE_ACTIVE)
Jeff Layton30c71232016-12-15 08:37:56 -0500837 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -0700838 }
839
840 /* since this file/dir wasn't known to be
841 * replicated, then we want to look for the
842 * authoritative mds. */
843 mode = USE_AUTH_MDS;
844 if (frag.mds >= 0) {
845 /* choose auth mds */
846 mds = frag.mds;
847 dout("choose_mds %p %llx.%llx "
848 "frag %u mds%d (auth)\n",
849 inode, ceph_vinop(inode), frag.frag, mds);
Sage Weild66bbd42011-01-21 21:16:46 -0800850 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
851 CEPH_MDS_STATE_ACTIVE)
Jeff Layton30c71232016-12-15 08:37:56 -0500852 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -0700853 }
854 }
855 }
856
Sage Weilbe655592011-11-30 09:47:09 -0800857 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700858 cap = NULL;
859 if (mode == USE_AUTH_MDS)
860 cap = ci->i_auth_cap;
861 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
862 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
863 if (!cap) {
Sage Weilbe655592011-11-30 09:47:09 -0800864 spin_unlock(&ci->i_ceph_lock);
Jeff Layton30c71232016-12-15 08:37:56 -0500865 iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -0700866 goto random;
867 }
868 mds = cap->session->s_mds;
869 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
870 inode, ceph_vinop(inode), mds,
871 cap == ci->i_auth_cap ? "auth " : "", cap);
Sage Weilbe655592011-11-30 09:47:09 -0800872 spin_unlock(&ci->i_ceph_lock);
Jeff Layton30c71232016-12-15 08:37:56 -0500873out:
874 iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -0700875 return mds;
876
877random:
878 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
879 dout("choose_mds chose random mds%d\n", mds);
880 return mds;
881}
882
883
884/*
885 * session messages
886 */
887static struct ceph_msg *create_session_msg(u32 op, u64 seq)
888{
889 struct ceph_msg *msg;
890 struct ceph_mds_session_head *h;
891
Sage Weilb61c2762011-08-09 15:03:46 -0700892 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
893 false);
Sage Weila79832f2010-04-01 16:06:19 -0700894 if (!msg) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700895 pr_err("create_session_msg ENOMEM creating msg\n");
Sage Weila79832f2010-04-01 16:06:19 -0700896 return NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700897 }
898 h = msg->front.iov_base;
899 h->op = cpu_to_le32(op);
900 h->seq = cpu_to_le64(seq);
John Spraydbd0c8b2014-09-09 19:26:01 +0100901
902 return msg;
903}
904
905/*
906 * session message, specialization for CEPH_SESSION_REQUEST_OPEN
907 * to include additional client metadata fields.
908 */
909static struct ceph_msg *create_session_open_msg(struct ceph_mds_client *mdsc, u64 seq)
910{
911 struct ceph_msg *msg;
912 struct ceph_mds_session_head *h;
913 int i = -1;
914 int metadata_bytes = 0;
915 int metadata_key_count = 0;
916 struct ceph_options *opt = mdsc->fsc->client->options;
Yan, Zheng3f384952016-04-21 11:09:55 +0800917 struct ceph_mount_options *fsopt = mdsc->fsc->mount_options;
John Spraydbd0c8b2014-09-09 19:26:01 +0100918 void *p;
919
Yan, Zhenga6a5ce42015-01-16 10:54:43 +0800920 const char* metadata[][2] = {
Yan, Zheng717e6f22017-09-11 12:10:08 +0800921 {"hostname", mdsc->nodename},
922 {"kernel_version", init_utsname()->release},
Yan, Zheng3f384952016-04-21 11:09:55 +0800923 {"entity_id", opt->name ? : ""},
924 {"root", fsopt->server_path ? : "/"},
John Spraydbd0c8b2014-09-09 19:26:01 +0100925 {NULL, NULL}
926 };
927
928 /* Calculate serialized length of metadata */
929 metadata_bytes = 4; /* map length */
Markus Elfringd37b1d92017-08-20 20:22:02 +0200930 for (i = 0; metadata[i][0]; ++i) {
John Spraydbd0c8b2014-09-09 19:26:01 +0100931 metadata_bytes += 8 + strlen(metadata[i][0]) +
932 strlen(metadata[i][1]);
933 metadata_key_count++;
934 }
935
936 /* Allocate the message */
937 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + metadata_bytes,
938 GFP_NOFS, false);
939 if (!msg) {
940 pr_err("create_session_msg ENOMEM creating msg\n");
941 return NULL;
942 }
943 h = msg->front.iov_base;
944 h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN);
945 h->seq = cpu_to_le64(seq);
946
947 /*
948 * Serialize client metadata into waiting buffer space, using
949 * the format that userspace expects for map<string, string>
John Spray7cfa0312014-10-30 17:15:26 +0000950 *
951 * ClientSession messages with metadata are v2
John Spraydbd0c8b2014-09-09 19:26:01 +0100952 */
John Spray7cfa0312014-10-30 17:15:26 +0000953 msg->hdr.version = cpu_to_le16(2);
954 msg->hdr.compat_version = cpu_to_le16(1);
John Spraydbd0c8b2014-09-09 19:26:01 +0100955
956 /* The write pointer, following the session_head structure */
957 p = msg->front.iov_base + sizeof(*h);
958
959 /* Number of entries in the map */
960 ceph_encode_32(&p, metadata_key_count);
961
962 /* Two length-prefixed strings for each entry in the map */
Markus Elfringd37b1d92017-08-20 20:22:02 +0200963 for (i = 0; metadata[i][0]; ++i) {
John Spraydbd0c8b2014-09-09 19:26:01 +0100964 size_t const key_len = strlen(metadata[i][0]);
965 size_t const val_len = strlen(metadata[i][1]);
966
967 ceph_encode_32(&p, key_len);
968 memcpy(p, metadata[i][0], key_len);
969 p += key_len;
970 ceph_encode_32(&p, val_len);
971 memcpy(p, metadata[i][1], val_len);
972 p += val_len;
973 }
974
Sage Weil2f2dc052009-10-06 11:31:09 -0700975 return msg;
976}
977
978/*
979 * send session open request.
980 *
981 * called under mdsc->mutex
982 */
983static int __open_session(struct ceph_mds_client *mdsc,
984 struct ceph_mds_session *session)
985{
986 struct ceph_msg *msg;
987 int mstate;
988 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -0700989
990 /* wait for mds to go active? */
991 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
992 dout("open_session to mds%d (%s)\n", mds,
993 ceph_mds_state_name(mstate));
994 session->s_state = CEPH_MDS_SESSION_OPENING;
995 session->s_renew_requested = jiffies;
996
997 /* send connect message */
John Spraydbd0c8b2014-09-09 19:26:01 +0100998 msg = create_session_open_msg(mdsc, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -0700999 if (!msg)
1000 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -07001001 ceph_con_send(&session->s_con, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001002 return 0;
1003}
1004
1005/*
Sage Weiled0552a2010-06-21 13:38:25 -07001006 * open sessions for any export targets for the given mds
1007 *
1008 * called under mdsc->mutex
1009 */
Yan, Zheng5d72d132013-11-24 14:33:01 +08001010static struct ceph_mds_session *
1011__open_export_target_session(struct ceph_mds_client *mdsc, int target)
1012{
1013 struct ceph_mds_session *session;
1014
1015 session = __ceph_lookup_mds_session(mdsc, target);
1016 if (!session) {
1017 session = register_session(mdsc, target);
1018 if (IS_ERR(session))
1019 return session;
1020 }
1021 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1022 session->s_state == CEPH_MDS_SESSION_CLOSING)
1023 __open_session(mdsc, session);
1024
1025 return session;
1026}
1027
1028struct ceph_mds_session *
1029ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target)
1030{
1031 struct ceph_mds_session *session;
1032
1033 dout("open_export_target_session to mds%d\n", target);
1034
1035 mutex_lock(&mdsc->mutex);
1036 session = __open_export_target_session(mdsc, target);
1037 mutex_unlock(&mdsc->mutex);
1038
1039 return session;
1040}
1041
Sage Weiled0552a2010-06-21 13:38:25 -07001042static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
1043 struct ceph_mds_session *session)
1044{
1045 struct ceph_mds_info *mi;
1046 struct ceph_mds_session *ts;
1047 int i, mds = session->s_mds;
Sage Weiled0552a2010-06-21 13:38:25 -07001048
Yan, Zheng76201b62017-03-28 17:04:13 +08001049 if (mds >= mdsc->mdsmap->m_num_mds)
Sage Weiled0552a2010-06-21 13:38:25 -07001050 return;
Yan, Zheng5d72d132013-11-24 14:33:01 +08001051
Sage Weiled0552a2010-06-21 13:38:25 -07001052 mi = &mdsc->mdsmap->m_info[mds];
1053 dout("open_export_target_sessions for mds%d (%d targets)\n",
1054 session->s_mds, mi->num_export_targets);
1055
1056 for (i = 0; i < mi->num_export_targets; i++) {
Yan, Zheng5d72d132013-11-24 14:33:01 +08001057 ts = __open_export_target_session(mdsc, mi->export_targets[i]);
1058 if (!IS_ERR(ts))
1059 ceph_put_mds_session(ts);
Sage Weiled0552a2010-06-21 13:38:25 -07001060 }
1061}
1062
Sage Weil154f42c2010-06-21 13:45:04 -07001063void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
1064 struct ceph_mds_session *session)
1065{
1066 mutex_lock(&mdsc->mutex);
1067 __open_export_target_sessions(mdsc, session);
1068 mutex_unlock(&mdsc->mutex);
1069}
1070
Sage Weiled0552a2010-06-21 13:38:25 -07001071/*
Sage Weil2f2dc052009-10-06 11:31:09 -07001072 * session caps
1073 */
1074
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001075static void detach_cap_releases(struct ceph_mds_session *session,
1076 struct list_head *target)
Sage Weil2f2dc052009-10-06 11:31:09 -07001077{
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001078 lockdep_assert_held(&session->s_cap_lock);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001079
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001080 list_splice_init(&session->s_cap_releases, target);
1081 session->s_num_cap_releases = 0;
1082 dout("dispose_cap_releases mds%d\n", session->s_mds);
1083}
1084
1085static void dispose_cap_releases(struct ceph_mds_client *mdsc,
1086 struct list_head *dispose)
1087{
1088 while (!list_empty(dispose)) {
Yan, Zheng745a8e32015-05-14 17:22:42 +08001089 struct ceph_cap *cap;
1090 /* zero out the in-progress message */
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001091 cap = list_first_entry(dispose, struct ceph_cap, session_caps);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001092 list_del(&cap->session_caps);
1093 ceph_put_cap(mdsc, cap);
1094 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001095}
1096
Yan, Zheng1c841a92015-03-24 20:15:36 +08001097static void cleanup_session_requests(struct ceph_mds_client *mdsc,
1098 struct ceph_mds_session *session)
1099{
1100 struct ceph_mds_request *req;
1101 struct rb_node *p;
1102
1103 dout("cleanup_session_requests mds%d\n", session->s_mds);
1104 mutex_lock(&mdsc->mutex);
1105 while (!list_empty(&session->s_unsafe)) {
1106 req = list_first_entry(&session->s_unsafe,
1107 struct ceph_mds_request, r_unsafe_item);
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001108 pr_warn_ratelimited(" dropping unsafe request %llu\n",
1109 req->r_tid);
Yan, Zheng1c841a92015-03-24 20:15:36 +08001110 __unregister_request(mdsc, req);
1111 }
1112 /* zero r_attempts, so kick_requests() will re-send requests */
1113 p = rb_first(&mdsc->request_tree);
1114 while (p) {
1115 req = rb_entry(p, struct ceph_mds_request, r_node);
1116 p = rb_next(p);
1117 if (req->r_session &&
1118 req->r_session->s_mds == session->s_mds)
1119 req->r_attempts = 0;
1120 }
1121 mutex_unlock(&mdsc->mutex);
1122}
1123
Sage Weil2f2dc052009-10-06 11:31:09 -07001124/*
Sage Weilf818a732010-05-11 20:56:31 -07001125 * Helper to safely iterate over all caps associated with a session, with
1126 * special care taken to handle a racing __ceph_remove_cap().
Sage Weil2f2dc052009-10-06 11:31:09 -07001127 *
Sage Weilf818a732010-05-11 20:56:31 -07001128 * Caller must hold session s_mutex.
Sage Weil2f2dc052009-10-06 11:31:09 -07001129 */
1130static int iterate_session_caps(struct ceph_mds_session *session,
1131 int (*cb)(struct inode *, struct ceph_cap *,
1132 void *), void *arg)
1133{
Sage Weil7c1332b2010-02-16 11:39:45 -08001134 struct list_head *p;
1135 struct ceph_cap *cap;
1136 struct inode *inode, *last_inode = NULL;
1137 struct ceph_cap *old_cap = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001138 int ret;
1139
1140 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
1141 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001142 p = session->s_caps.next;
1143 while (p != &session->s_caps) {
1144 cap = list_entry(p, struct ceph_cap, session_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -07001145 inode = igrab(&cap->ci->vfs_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001146 if (!inode) {
1147 p = p->next;
Sage Weil2f2dc052009-10-06 11:31:09 -07001148 continue;
Sage Weil7c1332b2010-02-16 11:39:45 -08001149 }
1150 session->s_cap_iterator = cap;
Sage Weil2f2dc052009-10-06 11:31:09 -07001151 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001152
1153 if (last_inode) {
1154 iput(last_inode);
1155 last_inode = NULL;
1156 }
1157 if (old_cap) {
Yehuda Sadeh37151662010-06-17 16:16:12 -07001158 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -08001159 old_cap = NULL;
1160 }
1161
Sage Weil2f2dc052009-10-06 11:31:09 -07001162 ret = cb(inode, cap, arg);
Sage Weil7c1332b2010-02-16 11:39:45 -08001163 last_inode = inode;
1164
Sage Weil2f2dc052009-10-06 11:31:09 -07001165 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001166 p = p->next;
Markus Elfringd37b1d92017-08-20 20:22:02 +02001167 if (!cap->ci) {
Sage Weil7c1332b2010-02-16 11:39:45 -08001168 dout("iterate_session_caps finishing cap %p removal\n",
1169 cap);
1170 BUG_ON(cap->session != session);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001171 cap->session = NULL;
Sage Weil7c1332b2010-02-16 11:39:45 -08001172 list_del_init(&cap->session_caps);
1173 session->s_nr_caps--;
Yan, Zheng745a8e32015-05-14 17:22:42 +08001174 if (cap->queue_release) {
1175 list_add_tail(&cap->session_caps,
1176 &session->s_cap_releases);
1177 session->s_num_cap_releases++;
1178 } else {
1179 old_cap = cap; /* put_cap it w/o locks held */
1180 }
Sage Weil7c1332b2010-02-16 11:39:45 -08001181 }
Sage Weil5dacf092009-12-21 20:40:34 -08001182 if (ret < 0)
1183 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07001184 }
Sage Weil5dacf092009-12-21 20:40:34 -08001185 ret = 0;
1186out:
Sage Weil7c1332b2010-02-16 11:39:45 -08001187 session->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001188 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001189
SF Markus Elfringe96a6502014-11-02 15:20:59 +01001190 iput(last_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001191 if (old_cap)
Yehuda Sadeh37151662010-06-17 16:16:12 -07001192 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -08001193
Sage Weil5dacf092009-12-21 20:40:34 -08001194 return ret;
Sage Weil2f2dc052009-10-06 11:31:09 -07001195}
1196
1197static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
Sage Weil6c99f252010-05-10 16:12:25 -07001198 void *arg)
Sage Weil2f2dc052009-10-06 11:31:09 -07001199{
Yan, Zheng6c93df52016-04-15 13:56:12 +08001200 struct ceph_fs_client *fsc = (struct ceph_fs_client *)arg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001201 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001202 LIST_HEAD(to_remove);
Yan, Zheng6c93df52016-04-15 13:56:12 +08001203 bool drop = false;
1204 bool invalidate = false;
Sage Weil6c99f252010-05-10 16:12:25 -07001205
Sage Weil2f2dc052009-10-06 11:31:09 -07001206 dout("removing cap %p, ci is %p, inode is %p\n",
1207 cap, ci, &ci->vfs_inode);
Sage Weilbe655592011-11-30 09:47:09 -08001208 spin_lock(&ci->i_ceph_lock);
Yan, Zhenga096b092013-09-22 10:15:58 +08001209 __ceph_remove_cap(cap, false);
Yan, Zheng571ade32015-03-24 11:36:08 +08001210 if (!ci->i_auth_cap) {
Yan, Zheng553adfd2015-06-09 15:48:57 +08001211 struct ceph_cap_flush *cf;
Yan, Zheng6c93df52016-04-15 13:56:12 +08001212 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil6c99f252010-05-10 16:12:25 -07001213
Yan, Zheng77310322016-04-08 15:27:16 +08001214 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
1215
Yan, Zheng6c93df52016-04-15 13:56:12 +08001216 if (ci->i_wrbuffer_ref > 0 &&
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01001217 READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
Yan, Zheng6c93df52016-04-15 13:56:12 +08001218 invalidate = true;
1219
Yan, Zhenge4500b52016-07-06 11:12:56 +08001220 while (!list_empty(&ci->i_cap_flush_list)) {
1221 cf = list_first_entry(&ci->i_cap_flush_list,
1222 struct ceph_cap_flush, i_list);
Wei Yongjun8cdcc072016-08-13 09:10:28 +00001223 list_move(&cf->i_list, &to_remove);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001224 }
1225
Sage Weil6c99f252010-05-10 16:12:25 -07001226 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zheng8310b082015-06-09 17:20:12 +08001227
Yan, Zhenge4500b52016-07-06 11:12:56 +08001228 list_for_each_entry(cf, &to_remove, i_list)
1229 list_del(&cf->g_list);
Yan, Zheng8310b082015-06-09 17:20:12 +08001230
Sage Weil6c99f252010-05-10 16:12:25 -07001231 if (!list_empty(&ci->i_dirty_item)) {
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001232 pr_warn_ratelimited(
1233 " dropping dirty %s state for %p %lld\n",
Sage Weil6c99f252010-05-10 16:12:25 -07001234 ceph_cap_string(ci->i_dirty_caps),
1235 inode, ceph_ino(inode));
1236 ci->i_dirty_caps = 0;
1237 list_del_init(&ci->i_dirty_item);
Yan, Zheng6c93df52016-04-15 13:56:12 +08001238 drop = true;
Sage Weil6c99f252010-05-10 16:12:25 -07001239 }
1240 if (!list_empty(&ci->i_flushing_item)) {
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001241 pr_warn_ratelimited(
1242 " dropping dirty+flushing %s state for %p %lld\n",
Sage Weil6c99f252010-05-10 16:12:25 -07001243 ceph_cap_string(ci->i_flushing_caps),
1244 inode, ceph_ino(inode));
1245 ci->i_flushing_caps = 0;
1246 list_del_init(&ci->i_flushing_item);
1247 mdsc->num_cap_flushing--;
Yan, Zheng6c93df52016-04-15 13:56:12 +08001248 drop = true;
Sage Weil6c99f252010-05-10 16:12:25 -07001249 }
Sage Weil6c99f252010-05-10 16:12:25 -07001250 spin_unlock(&mdsc->cap_dirty_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001251
Yan, Zhengb3f8d682017-09-11 10:58:55 +08001252 if (atomic_read(&ci->i_filelock_ref) > 0) {
1253 /* make further file lock syscall return -EIO */
1254 ci->i_ceph_flags |= CEPH_I_ERROR_FILELOCK;
1255 pr_warn_ratelimited(" dropping file locks for %p %lld\n",
1256 inode, ceph_ino(inode));
1257 }
1258
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001259 if (!ci->i_dirty_caps && ci->i_prealloc_cap_flush) {
Yan, Zhenge4500b52016-07-06 11:12:56 +08001260 list_add(&ci->i_prealloc_cap_flush->i_list, &to_remove);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001261 ci->i_prealloc_cap_flush = NULL;
1262 }
Sage Weil6c99f252010-05-10 16:12:25 -07001263 }
Sage Weilbe655592011-11-30 09:47:09 -08001264 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001265 while (!list_empty(&to_remove)) {
1266 struct ceph_cap_flush *cf;
1267 cf = list_first_entry(&to_remove,
Yan, Zhenge4500b52016-07-06 11:12:56 +08001268 struct ceph_cap_flush, i_list);
1269 list_del(&cf->i_list);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001270 ceph_free_cap_flush(cf);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001271 }
Yan, Zheng77310322016-04-08 15:27:16 +08001272
1273 wake_up_all(&ci->i_cap_wq);
Yan, Zheng6c93df52016-04-15 13:56:12 +08001274 if (invalidate)
1275 ceph_queue_invalidate(inode);
Yan, Zheng77310322016-04-08 15:27:16 +08001276 if (drop)
Sage Weil6c99f252010-05-10 16:12:25 -07001277 iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07001278 return 0;
1279}
1280
1281/*
1282 * caller must hold session s_mutex
1283 */
1284static void remove_session_caps(struct ceph_mds_session *session)
1285{
Yan, Zheng6c93df52016-04-15 13:56:12 +08001286 struct ceph_fs_client *fsc = session->s_mdsc->fsc;
1287 struct super_block *sb = fsc->sb;
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001288 LIST_HEAD(dispose);
1289
Sage Weil2f2dc052009-10-06 11:31:09 -07001290 dout("remove_session_caps on %p\n", session);
Yan, Zheng6c93df52016-04-15 13:56:12 +08001291 iterate_session_caps(session, remove_session_caps_cb, fsc);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001292
Yan, Zhengc8799fc2016-07-07 15:22:38 +08001293 wake_up_all(&fsc->mdsc->cap_flushing_wq);
1294
Yan, Zheng6f60f882013-07-24 12:22:11 +08001295 spin_lock(&session->s_cap_lock);
1296 if (session->s_nr_caps > 0) {
Yan, Zheng6f60f882013-07-24 12:22:11 +08001297 struct inode *inode;
1298 struct ceph_cap *cap, *prev = NULL;
1299 struct ceph_vino vino;
1300 /*
1301 * iterate_session_caps() skips inodes that are being
1302 * deleted, we need to wait until deletions are complete.
1303 * __wait_on_freeing_inode() is designed for the job,
1304 * but it is not exported, so use lookup inode function
1305 * to access it.
1306 */
1307 while (!list_empty(&session->s_caps)) {
1308 cap = list_entry(session->s_caps.next,
1309 struct ceph_cap, session_caps);
1310 if (cap == prev)
1311 break;
1312 prev = cap;
1313 vino = cap->ci->i_vino;
1314 spin_unlock(&session->s_cap_lock);
1315
Yan, Zhenged284c42013-09-02 15:19:53 +08001316 inode = ceph_find_inode(sb, vino);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001317 iput(inode);
1318
1319 spin_lock(&session->s_cap_lock);
1320 }
1321 }
Yan, Zheng745a8e32015-05-14 17:22:42 +08001322
1323 // drop cap expires and unlock s_cap_lock
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001324 detach_cap_releases(session, &dispose);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001325
Sage Weil2f2dc052009-10-06 11:31:09 -07001326 BUG_ON(session->s_nr_caps > 0);
Sage Weil6c99f252010-05-10 16:12:25 -07001327 BUG_ON(!list_empty(&session->s_cap_flushing));
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001328 spin_unlock(&session->s_cap_lock);
1329 dispose_cap_releases(session->s_mdsc, &dispose);
Sage Weil2f2dc052009-10-06 11:31:09 -07001330}
1331
1332/*
1333 * wake up any threads waiting on this session's caps. if the cap is
1334 * old (didn't get renewed on the client reconnect), remove it now.
1335 *
1336 * caller must hold s_mutex.
1337 */
1338static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1339 void *arg)
1340{
Sage Weil0dc25702009-11-20 13:43:45 -08001341 struct ceph_inode_info *ci = ceph_inode(inode);
1342
Sage Weil0dc25702009-11-20 13:43:45 -08001343 if (arg) {
Sage Weilbe655592011-11-30 09:47:09 -08001344 spin_lock(&ci->i_ceph_lock);
Sage Weil0dc25702009-11-20 13:43:45 -08001345 ci->i_wanted_max_size = 0;
1346 ci->i_requested_max_size = 0;
Sage Weilbe655592011-11-30 09:47:09 -08001347 spin_unlock(&ci->i_ceph_lock);
Sage Weil0dc25702009-11-20 13:43:45 -08001348 }
Yan, Zhenge5360302016-05-19 19:15:19 +08001349 wake_up_all(&ci->i_cap_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07001350 return 0;
1351}
1352
Sage Weil0dc25702009-11-20 13:43:45 -08001353static void wake_up_session_caps(struct ceph_mds_session *session,
1354 int reconnect)
Sage Weil2f2dc052009-10-06 11:31:09 -07001355{
1356 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
Sage Weil0dc25702009-11-20 13:43:45 -08001357 iterate_session_caps(session, wake_up_session_cb,
1358 (void *)(unsigned long)reconnect);
Sage Weil2f2dc052009-10-06 11:31:09 -07001359}
1360
1361/*
1362 * Send periodic message to MDS renewing all currently held caps. The
1363 * ack will reset the expiration for all caps from this session.
1364 *
1365 * caller holds s_mutex
1366 */
1367static int send_renew_caps(struct ceph_mds_client *mdsc,
1368 struct ceph_mds_session *session)
1369{
1370 struct ceph_msg *msg;
1371 int state;
1372
1373 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1374 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1375 pr_info("mds%d caps stale\n", session->s_mds);
Sage Weile4cb4cb2010-03-18 13:43:09 -07001376 session->s_renew_requested = jiffies;
Sage Weil2f2dc052009-10-06 11:31:09 -07001377
1378 /* do not try to renew caps until a recovering mds has reconnected
1379 * with its clients. */
1380 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1381 if (state < CEPH_MDS_STATE_RECONNECT) {
1382 dout("send_renew_caps ignoring mds%d (%s)\n",
1383 session->s_mds, ceph_mds_state_name(state));
1384 return 0;
1385 }
1386
1387 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1388 ceph_mds_state_name(state));
Sage Weil2f2dc052009-10-06 11:31:09 -07001389 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1390 ++session->s_renew_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001391 if (!msg)
1392 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -07001393 ceph_con_send(&session->s_con, msg);
1394 return 0;
1395}
1396
Yan, Zheng186e4f72013-11-22 14:48:37 +08001397static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
1398 struct ceph_mds_session *session, u64 seq)
1399{
1400 struct ceph_msg *msg;
1401
1402 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
John Spraya687eca2014-09-19 13:51:08 +01001403 session->s_mds, ceph_session_state_name(session->s_state), seq);
Yan, Zheng186e4f72013-11-22 14:48:37 +08001404 msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
1405 if (!msg)
1406 return -ENOMEM;
1407 ceph_con_send(&session->s_con, msg);
1408 return 0;
1409}
1410
1411
Sage Weil2f2dc052009-10-06 11:31:09 -07001412/*
1413 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
Sage Weil0dc25702009-11-20 13:43:45 -08001414 *
1415 * Called under session->s_mutex
Sage Weil2f2dc052009-10-06 11:31:09 -07001416 */
1417static void renewed_caps(struct ceph_mds_client *mdsc,
1418 struct ceph_mds_session *session, int is_renew)
1419{
1420 int was_stale;
1421 int wake = 0;
1422
1423 spin_lock(&session->s_cap_lock);
Alex Elder1ce208a2012-01-12 17:48:11 -08001424 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
Sage Weil2f2dc052009-10-06 11:31:09 -07001425
1426 session->s_cap_ttl = session->s_renew_requested +
1427 mdsc->mdsmap->m_session_timeout*HZ;
1428
1429 if (was_stale) {
1430 if (time_before(jiffies, session->s_cap_ttl)) {
1431 pr_info("mds%d caps renewed\n", session->s_mds);
1432 wake = 1;
1433 } else {
1434 pr_info("mds%d caps still stale\n", session->s_mds);
1435 }
1436 }
1437 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1438 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1439 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1440 spin_unlock(&session->s_cap_lock);
1441
1442 if (wake)
Sage Weil0dc25702009-11-20 13:43:45 -08001443 wake_up_session_caps(session, 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07001444}
1445
1446/*
1447 * send a session close request
1448 */
1449static int request_close_session(struct ceph_mds_client *mdsc,
1450 struct ceph_mds_session *session)
1451{
1452 struct ceph_msg *msg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001453
1454 dout("request_close_session mds%d state %s seq %lld\n",
John Spraya687eca2014-09-19 13:51:08 +01001455 session->s_mds, ceph_session_state_name(session->s_state),
Sage Weil2f2dc052009-10-06 11:31:09 -07001456 session->s_seq);
1457 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001458 if (!msg)
1459 return -ENOMEM;
1460 ceph_con_send(&session->s_con, msg);
Yan, Zhengfcff4152016-09-14 16:39:51 +08001461 return 1;
Sage Weil2f2dc052009-10-06 11:31:09 -07001462}
1463
1464/*
1465 * Called with s_mutex held.
1466 */
1467static int __close_session(struct ceph_mds_client *mdsc,
1468 struct ceph_mds_session *session)
1469{
1470 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1471 return 0;
1472 session->s_state = CEPH_MDS_SESSION_CLOSING;
1473 return request_close_session(mdsc, session);
1474}
1475
Yan, Zheng040d7862017-11-30 11:59:22 +08001476static bool drop_negative_children(struct dentry *dentry)
1477{
1478 struct dentry *child;
1479 bool all_negative = true;
1480
1481 if (!d_is_dir(dentry))
1482 goto out;
1483
1484 spin_lock(&dentry->d_lock);
1485 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
1486 if (d_really_is_positive(child)) {
1487 all_negative = false;
1488 break;
1489 }
1490 }
1491 spin_unlock(&dentry->d_lock);
1492
1493 if (all_negative)
1494 shrink_dcache_parent(dentry);
1495out:
1496 return all_negative;
1497}
1498
Sage Weil2f2dc052009-10-06 11:31:09 -07001499/*
1500 * Trim old(er) caps.
1501 *
1502 * Because we can't cache an inode without one or more caps, we do
1503 * this indirectly: if a cap is unused, we prune its aliases, at which
1504 * point the inode will hopefully get dropped to.
1505 *
1506 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1507 * memory pressure from the MDS, though, so it needn't be perfect.
1508 */
1509static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1510{
1511 struct ceph_mds_session *session = arg;
1512 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng979abfd2013-11-22 13:56:24 +08001513 int used, wanted, oissued, mine;
Sage Weil2f2dc052009-10-06 11:31:09 -07001514
1515 if (session->s_trim_caps <= 0)
1516 return -1;
1517
Sage Weilbe655592011-11-30 09:47:09 -08001518 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001519 mine = cap->issued | cap->implemented;
1520 used = __ceph_caps_used(ci);
Yan, Zheng979abfd2013-11-22 13:56:24 +08001521 wanted = __ceph_caps_file_wanted(ci);
Sage Weil2f2dc052009-10-06 11:31:09 -07001522 oissued = __ceph_caps_issued_other(ci, cap);
1523
Yan, Zheng979abfd2013-11-22 13:56:24 +08001524 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001525 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
Yan, Zheng979abfd2013-11-22 13:56:24 +08001526 ceph_cap_string(used), ceph_cap_string(wanted));
1527 if (cap == ci->i_auth_cap) {
Yan, Zheng622f3e22015-05-07 10:59:47 +08001528 if (ci->i_dirty_caps || ci->i_flushing_caps ||
1529 !list_empty(&ci->i_cap_snaps))
Yan, Zheng979abfd2013-11-22 13:56:24 +08001530 goto out;
1531 if ((used | wanted) & CEPH_CAP_ANY_WR)
1532 goto out;
Yan, Zheng89aa5932017-09-08 15:23:18 +08001533 /* Note: it's possible that i_filelock_ref becomes non-zero
1534 * after dropping auth caps. It doesn't hurt because reply
1535 * of lock mds request will re-add auth caps. */
1536 if (atomic_read(&ci->i_filelock_ref) > 0)
1537 goto out;
Yan, Zheng979abfd2013-11-22 13:56:24 +08001538 }
Yan, Zheng5e804ac2015-10-26 16:08:43 +08001539 /* The inode has cached pages, but it's no longer used.
1540 * we can safely drop it */
1541 if (wanted == 0 && used == CEPH_CAP_FILE_CACHE &&
1542 !(oissued & CEPH_CAP_FILE_CACHE)) {
1543 used = 0;
1544 oissued = 0;
1545 }
Yan, Zheng979abfd2013-11-22 13:56:24 +08001546 if ((used | wanted) & ~oissued & mine)
Sage Weil2f2dc052009-10-06 11:31:09 -07001547 goto out; /* we need these caps */
1548
Sage Weil2f2dc052009-10-06 11:31:09 -07001549 if (oissued) {
1550 /* we aren't the only cap.. just remove us */
Yan, Zhenga096b092013-09-22 10:15:58 +08001551 __ceph_remove_cap(cap, true);
Yan, Zheng040d7862017-11-30 11:59:22 +08001552 session->s_trim_caps--;
Sage Weil2f2dc052009-10-06 11:31:09 -07001553 } else {
Yan, Zheng040d7862017-11-30 11:59:22 +08001554 struct dentry *dentry;
Yan, Zheng5e804ac2015-10-26 16:08:43 +08001555 /* try dropping referring dentries */
Sage Weilbe655592011-11-30 09:47:09 -08001556 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng040d7862017-11-30 11:59:22 +08001557 dentry = d_find_any_alias(inode);
1558 if (dentry && drop_negative_children(dentry)) {
1559 int count;
1560 dput(dentry);
1561 d_prune_aliases(inode);
1562 count = atomic_read(&inode->i_count);
1563 if (count == 1)
1564 session->s_trim_caps--;
1565 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1566 inode, cap, count);
1567 } else {
1568 dput(dentry);
1569 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001570 return 0;
1571 }
1572
1573out:
Sage Weilbe655592011-11-30 09:47:09 -08001574 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001575 return 0;
1576}
1577
1578/*
1579 * Trim session cap count down to some max number.
1580 */
Zhi Zhange30ee582018-01-24 21:24:33 +08001581int ceph_trim_caps(struct ceph_mds_client *mdsc,
1582 struct ceph_mds_session *session,
1583 int max_caps)
Sage Weil2f2dc052009-10-06 11:31:09 -07001584{
1585 int trim_caps = session->s_nr_caps - max_caps;
1586
1587 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1588 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1589 if (trim_caps > 0) {
1590 session->s_trim_caps = trim_caps;
1591 iterate_session_caps(session, trim_caps_cb, session);
1592 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1593 session->s_mds, session->s_nr_caps, max_caps,
1594 trim_caps - session->s_trim_caps);
Sage Weil5dacf092009-12-21 20:40:34 -08001595 session->s_trim_caps = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001596 }
Yan, Zhenga56371d2014-04-01 20:34:56 +08001597
Yan, Zhenga56371d2014-04-01 20:34:56 +08001598 ceph_send_cap_releases(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07001599 return 0;
1600}
1601
Yan, Zheng8310b082015-06-09 17:20:12 +08001602static int check_caps_flush(struct ceph_mds_client *mdsc,
1603 u64 want_flush_tid)
1604{
Yan, Zheng8310b082015-06-09 17:20:12 +08001605 int ret = 1;
1606
1607 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zhenge4500b52016-07-06 11:12:56 +08001608 if (!list_empty(&mdsc->cap_flush_list)) {
1609 struct ceph_cap_flush *cf =
1610 list_first_entry(&mdsc->cap_flush_list,
1611 struct ceph_cap_flush, g_list);
1612 if (cf->tid <= want_flush_tid) {
1613 dout("check_caps_flush still flushing tid "
1614 "%llu <= %llu\n", cf->tid, want_flush_tid);
1615 ret = 0;
1616 }
Yan, Zheng8310b082015-06-09 17:20:12 +08001617 }
1618 spin_unlock(&mdsc->cap_dirty_lock);
1619 return ret;
Yan, Zhengd3383a82015-01-08 21:30:12 +08001620}
1621
Sage Weil2f2dc052009-10-06 11:31:09 -07001622/*
1623 * flush all dirty inode data to disk.
1624 *
Yan, Zheng8310b082015-06-09 17:20:12 +08001625 * returns true if we've flushed through want_flush_tid
Sage Weil2f2dc052009-10-06 11:31:09 -07001626 */
Yan, Zhengaffbc192015-05-05 21:22:13 +08001627static void wait_caps_flush(struct ceph_mds_client *mdsc,
Yan, Zheng0e294382016-07-04 18:06:41 +08001628 u64 want_flush_tid)
Sage Weil2f2dc052009-10-06 11:31:09 -07001629{
Yan, Zheng0e294382016-07-04 18:06:41 +08001630 dout("check_caps_flush want %llu\n", want_flush_tid);
Yan, Zheng8310b082015-06-09 17:20:12 +08001631
1632 wait_event(mdsc->cap_flushing_wq,
1633 check_caps_flush(mdsc, want_flush_tid));
1634
1635 dout("check_caps_flush ok, flushed thru %llu\n", want_flush_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07001636}
1637
1638/*
1639 * called under s_mutex
1640 */
Sage Weil3d7ded42010-06-09 16:47:10 -07001641void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1642 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001643{
Yan, Zheng745a8e32015-05-14 17:22:42 +08001644 struct ceph_msg *msg = NULL;
1645 struct ceph_mds_cap_release *head;
1646 struct ceph_mds_cap_item *item;
Jeff Layton92475f02017-04-13 11:07:04 -04001647 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
Yan, Zheng745a8e32015-05-14 17:22:42 +08001648 struct ceph_cap *cap;
1649 LIST_HEAD(tmp_list);
1650 int num_cap_releases;
Jeff Layton92475f02017-04-13 11:07:04 -04001651 __le32 barrier, *cap_barrier;
1652
1653 down_read(&osdc->lock);
1654 barrier = cpu_to_le32(osdc->epoch_barrier);
1655 up_read(&osdc->lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001656
Sage Weil0f8605f2010-05-05 15:51:35 -07001657 spin_lock(&session->s_cap_lock);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001658again:
1659 list_splice_init(&session->s_cap_releases, &tmp_list);
1660 num_cap_releases = session->s_num_cap_releases;
1661 session->s_num_cap_releases = 0;
1662 spin_unlock(&session->s_cap_lock);
1663
1664 while (!list_empty(&tmp_list)) {
1665 if (!msg) {
1666 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001667 PAGE_SIZE, GFP_NOFS, false);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001668 if (!msg)
1669 goto out_err;
1670 head = msg->front.iov_base;
1671 head->num = cpu_to_le32(0);
1672 msg->front.iov_len = sizeof(*head);
Jeff Layton92475f02017-04-13 11:07:04 -04001673
1674 msg->hdr.version = cpu_to_le16(2);
1675 msg->hdr.compat_version = cpu_to_le16(1);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001676 }
Jeff Layton92475f02017-04-13 11:07:04 -04001677
Yan, Zheng745a8e32015-05-14 17:22:42 +08001678 cap = list_first_entry(&tmp_list, struct ceph_cap,
1679 session_caps);
1680 list_del(&cap->session_caps);
1681 num_cap_releases--;
1682
1683 head = msg->front.iov_base;
1684 le32_add_cpu(&head->num, 1);
1685 item = msg->front.iov_base + msg->front.iov_len;
1686 item->ino = cpu_to_le64(cap->cap_ino);
1687 item->cap_id = cpu_to_le64(cap->cap_id);
1688 item->migrate_seq = cpu_to_le32(cap->mseq);
1689 item->seq = cpu_to_le32(cap->issue_seq);
1690 msg->front.iov_len += sizeof(*item);
1691
1692 ceph_put_cap(mdsc, cap);
1693
1694 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
Jeff Layton92475f02017-04-13 11:07:04 -04001695 // Append cap_barrier field
1696 cap_barrier = msg->front.iov_base + msg->front.iov_len;
1697 *cap_barrier = barrier;
1698 msg->front.iov_len += sizeof(*cap_barrier);
1699
Yan, Zheng745a8e32015-05-14 17:22:42 +08001700 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1701 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1702 ceph_con_send(&session->s_con, msg);
1703 msg = NULL;
1704 }
1705 }
1706
1707 BUG_ON(num_cap_releases != 0);
1708
1709 spin_lock(&session->s_cap_lock);
1710 if (!list_empty(&session->s_cap_releases))
1711 goto again;
1712 spin_unlock(&session->s_cap_lock);
1713
1714 if (msg) {
Jeff Layton92475f02017-04-13 11:07:04 -04001715 // Append cap_barrier field
1716 cap_barrier = msg->front.iov_base + msg->front.iov_len;
1717 *cap_barrier = barrier;
1718 msg->front.iov_len += sizeof(*cap_barrier);
1719
Sage Weil2f2dc052009-10-06 11:31:09 -07001720 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1721 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1722 ceph_con_send(&session->s_con, msg);
1723 }
Yan, Zheng745a8e32015-05-14 17:22:42 +08001724 return;
1725out_err:
1726 pr_err("send_cap_releases mds%d, failed to allocate message\n",
1727 session->s_mds);
1728 spin_lock(&session->s_cap_lock);
1729 list_splice(&tmp_list, &session->s_cap_releases);
1730 session->s_num_cap_releases += num_cap_releases;
Sage Weil2f2dc052009-10-06 11:31:09 -07001731 spin_unlock(&session->s_cap_lock);
1732}
1733
1734/*
1735 * requests
1736 */
1737
Yan, Zheng54008392014-03-29 13:41:15 +08001738int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
1739 struct inode *dir)
1740{
1741 struct ceph_inode_info *ci = ceph_inode(dir);
1742 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1743 struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options;
Yan, Zheng2a5beea2016-04-28 09:37:39 +08001744 size_t size = sizeof(struct ceph_mds_reply_dir_entry);
Yan, Zheng54008392014-03-29 13:41:15 +08001745 int order, num_entries;
1746
1747 spin_lock(&ci->i_ceph_lock);
1748 num_entries = ci->i_files + ci->i_subdirs;
1749 spin_unlock(&ci->i_ceph_lock);
1750 num_entries = max(num_entries, 1);
1751 num_entries = min(num_entries, opt->max_readdir);
1752
1753 order = get_order(size * num_entries);
1754 while (order >= 0) {
Yan, Zheng2a5beea2016-04-28 09:37:39 +08001755 rinfo->dir_entries = (void*)__get_free_pages(GFP_KERNEL |
1756 __GFP_NOWARN,
1757 order);
1758 if (rinfo->dir_entries)
Yan, Zheng54008392014-03-29 13:41:15 +08001759 break;
1760 order--;
1761 }
Yan, Zheng2a5beea2016-04-28 09:37:39 +08001762 if (!rinfo->dir_entries)
Yan, Zheng54008392014-03-29 13:41:15 +08001763 return -ENOMEM;
1764
1765 num_entries = (PAGE_SIZE << order) / size;
1766 num_entries = min(num_entries, opt->max_readdir);
1767
1768 rinfo->dir_buf_size = PAGE_SIZE << order;
1769 req->r_num_caps = num_entries + 1;
1770 req->r_args.readdir.max_entries = cpu_to_le32(num_entries);
1771 req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes);
1772 return 0;
1773}
1774
Sage Weil2f2dc052009-10-06 11:31:09 -07001775/*
1776 * Create an mds request.
1777 */
1778struct ceph_mds_request *
1779ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1780{
1781 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1782
1783 if (!req)
1784 return ERR_PTR(-ENOMEM);
1785
Sage Weilb4556392010-05-13 12:01:13 -07001786 mutex_init(&req->r_fill_mutex);
Yehuda Sadeh37151662010-06-17 16:16:12 -07001787 req->r_mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07001788 req->r_started = jiffies;
1789 req->r_resend_mds = -1;
1790 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
Yan, Zheng68cd5b4b2015-10-27 18:36:06 +08001791 INIT_LIST_HEAD(&req->r_unsafe_target_item);
Sage Weil2f2dc052009-10-06 11:31:09 -07001792 req->r_fmode = -1;
Sage Weil153c8e62009-12-07 12:31:09 -08001793 kref_init(&req->r_kref);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02001794 RB_CLEAR_NODE(&req->r_node);
Sage Weil2f2dc052009-10-06 11:31:09 -07001795 INIT_LIST_HEAD(&req->r_wait);
1796 init_completion(&req->r_completion);
1797 init_completion(&req->r_safe_completion);
1798 INIT_LIST_HEAD(&req->r_unsafe_item);
1799
Yan, Zheng56199012017-06-01 16:44:53 +08001800 req->r_stamp = timespec_trunc(current_kernel_time(), mdsc->fsc->sb->s_time_gran);
Sage Weilb8e69062014-05-21 17:41:08 -07001801
Sage Weil2f2dc052009-10-06 11:31:09 -07001802 req->r_op = op;
1803 req->r_direct_mode = mode;
1804 return req;
1805}
1806
1807/*
Sage Weil44ca18f2010-02-15 12:08:46 -08001808 * return oldest (lowest) request, tid in request tree, 0 if none.
Sage Weil2f2dc052009-10-06 11:31:09 -07001809 *
1810 * called under mdsc->mutex.
1811 */
Sage Weil44ca18f2010-02-15 12:08:46 -08001812static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1813{
1814 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1815 return NULL;
1816 return rb_entry(rb_first(&mdsc->request_tree),
1817 struct ceph_mds_request, r_node);
1818}
1819
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08001820static inline u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07001821{
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08001822 return mdsc->oldest_tid;
Sage Weil2f2dc052009-10-06 11:31:09 -07001823}
1824
1825/*
1826 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1827 * on build_path_from_dentry in fs/cifs/dir.c.
1828 *
1829 * If @stop_on_nosnap, generate path relative to the first non-snapped
1830 * inode.
1831 *
1832 * Encode hidden .snap dirs as a double /, i.e.
1833 * foo/.snap/bar -> foo//bar
1834 */
1835char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1836 int stop_on_nosnap)
1837{
1838 struct dentry *temp;
1839 char *path;
1840 int len, pos;
Al Viro1b71fe22011-07-16 23:43:58 -04001841 unsigned seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07001842
Markus Elfringd37b1d92017-08-20 20:22:02 +02001843 if (!dentry)
Sage Weil2f2dc052009-10-06 11:31:09 -07001844 return ERR_PTR(-EINVAL);
1845
1846retry:
1847 len = 0;
Al Viro1b71fe22011-07-16 23:43:58 -04001848 seq = read_seqbegin(&rename_lock);
1849 rcu_read_lock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001850 for (temp = dentry; !IS_ROOT(temp);) {
David Howells2b0143b2015-03-17 22:25:59 +00001851 struct inode *inode = d_inode(temp);
Sage Weil2f2dc052009-10-06 11:31:09 -07001852 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1853 len++; /* slash only */
1854 else if (stop_on_nosnap && inode &&
1855 ceph_snap(inode) == CEPH_NOSNAP)
1856 break;
1857 else
1858 len += 1 + temp->d_name.len;
1859 temp = temp->d_parent;
Sage Weil2f2dc052009-10-06 11:31:09 -07001860 }
Al Viro1b71fe22011-07-16 23:43:58 -04001861 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001862 if (len)
1863 len--; /* no leading '/' */
1864
1865 path = kmalloc(len+1, GFP_NOFS);
Markus Elfringd37b1d92017-08-20 20:22:02 +02001866 if (!path)
Sage Weil2f2dc052009-10-06 11:31:09 -07001867 return ERR_PTR(-ENOMEM);
1868 pos = len;
1869 path[pos] = 0; /* trailing null */
Al Viro1b71fe22011-07-16 23:43:58 -04001870 rcu_read_lock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001871 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
Al Viro1b71fe22011-07-16 23:43:58 -04001872 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07001873
Al Viro1b71fe22011-07-16 23:43:58 -04001874 spin_lock(&temp->d_lock);
David Howells2b0143b2015-03-17 22:25:59 +00001875 inode = d_inode(temp);
Sage Weil2f2dc052009-10-06 11:31:09 -07001876 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
Sage Weil104648a2010-03-18 10:14:30 -07001877 dout("build_path path+%d: %p SNAPDIR\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001878 pos, temp);
1879 } else if (stop_on_nosnap && inode &&
1880 ceph_snap(inode) == CEPH_NOSNAP) {
Yehuda Sadeh9d5a09e2011-12-13 09:57:44 -08001881 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001882 break;
1883 } else {
1884 pos -= temp->d_name.len;
Al Viro1b71fe22011-07-16 23:43:58 -04001885 if (pos < 0) {
1886 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001887 break;
Al Viro1b71fe22011-07-16 23:43:58 -04001888 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001889 strncpy(path + pos, temp->d_name.name,
1890 temp->d_name.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07001891 }
Al Viro1b71fe22011-07-16 23:43:58 -04001892 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001893 if (pos)
1894 path[--pos] = '/';
1895 temp = temp->d_parent;
Sage Weil2f2dc052009-10-06 11:31:09 -07001896 }
Al Viro1b71fe22011-07-16 23:43:58 -04001897 rcu_read_unlock();
1898 if (pos != 0 || read_seqretry(&rename_lock, seq)) {
Sage Weil104648a2010-03-18 10:14:30 -07001899 pr_err("build_path did not end path lookup where "
Sage Weil2f2dc052009-10-06 11:31:09 -07001900 "expected, namelen is %d, pos is %d\n", len, pos);
1901 /* presumably this is only possible if racing with a
1902 rename of one of the parent directories (we can not
1903 lock the dentries above us to prevent this, but
1904 retrying should be harmless) */
1905 kfree(path);
1906 goto retry;
1907 }
1908
David Howells2b0143b2015-03-17 22:25:59 +00001909 *base = ceph_ino(d_inode(temp));
Sage Weil2f2dc052009-10-06 11:31:09 -07001910 *plen = len;
Sage Weil104648a2010-03-18 10:14:30 -07001911 dout("build_path on %p %d built %llx '%.*s'\n",
Al Viro84d08fa2013-07-05 18:59:33 +04001912 dentry, d_count(dentry), *base, len, path);
Sage Weil2f2dc052009-10-06 11:31:09 -07001913 return path;
1914}
1915
Jeff Laytonfd36a712016-12-15 08:37:58 -05001916static int build_dentry_path(struct dentry *dentry, struct inode *dir,
Sage Weil2f2dc052009-10-06 11:31:09 -07001917 const char **ppath, int *ppathlen, u64 *pino,
1918 int *pfreepath)
1919{
1920 char *path;
1921
Jeff Laytonc6b0b652016-12-15 08:37:57 -05001922 rcu_read_lock();
Jeff Laytonfd36a712016-12-15 08:37:58 -05001923 if (!dir)
1924 dir = d_inode_rcu(dentry->d_parent);
Jeff Laytonc6b0b652016-12-15 08:37:57 -05001925 if (dir && ceph_snap(dir) == CEPH_NOSNAP) {
1926 *pino = ceph_ino(dir);
1927 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001928 *ppath = dentry->d_name.name;
1929 *ppathlen = dentry->d_name.len;
1930 return 0;
1931 }
Jeff Laytonc6b0b652016-12-15 08:37:57 -05001932 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001933 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1934 if (IS_ERR(path))
1935 return PTR_ERR(path);
1936 *ppath = path;
1937 *pfreepath = 1;
1938 return 0;
1939}
1940
1941static int build_inode_path(struct inode *inode,
1942 const char **ppath, int *ppathlen, u64 *pino,
1943 int *pfreepath)
1944{
1945 struct dentry *dentry;
1946 char *path;
1947
1948 if (ceph_snap(inode) == CEPH_NOSNAP) {
1949 *pino = ceph_ino(inode);
1950 *ppathlen = 0;
1951 return 0;
1952 }
1953 dentry = d_find_alias(inode);
1954 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1955 dput(dentry);
1956 if (IS_ERR(path))
1957 return PTR_ERR(path);
1958 *ppath = path;
1959 *pfreepath = 1;
1960 return 0;
1961}
1962
1963/*
1964 * request arguments may be specified via an inode *, a dentry *, or
1965 * an explicit ino+path.
1966 */
1967static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
Jeff Laytonfd36a712016-12-15 08:37:58 -05001968 struct inode *rdiri, const char *rpath,
1969 u64 rino, const char **ppath, int *pathlen,
Sage Weil2f2dc052009-10-06 11:31:09 -07001970 u64 *ino, int *freepath)
1971{
1972 int r = 0;
1973
1974 if (rinode) {
1975 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1976 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1977 ceph_snap(rinode));
1978 } else if (rdentry) {
Jeff Laytonfd36a712016-12-15 08:37:58 -05001979 r = build_dentry_path(rdentry, rdiri, ppath, pathlen, ino,
1980 freepath);
Sage Weil2f2dc052009-10-06 11:31:09 -07001981 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1982 *ppath);
Sage Weil795858d2011-08-15 13:02:37 -07001983 } else if (rpath || rino) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001984 *ino = rino;
1985 *ppath = rpath;
David Zafmanb0000562012-10-25 10:23:46 -07001986 *pathlen = rpath ? strlen(rpath) : 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001987 dout(" path %.*s\n", *pathlen, rpath);
1988 }
1989
1990 return r;
1991}
1992
1993/*
1994 * called under mdsc->mutex
1995 */
1996static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1997 struct ceph_mds_request *req,
Yan, Zheng6e6f0922015-02-27 08:54:08 +08001998 int mds, bool drop_cap_releases)
Sage Weil2f2dc052009-10-06 11:31:09 -07001999{
2000 struct ceph_msg *msg;
2001 struct ceph_mds_request_head *head;
2002 const char *path1 = NULL;
2003 const char *path2 = NULL;
2004 u64 ino1 = 0, ino2 = 0;
2005 int pathlen1 = 0, pathlen2 = 0;
2006 int freepath1 = 0, freepath2 = 0;
2007 int len;
2008 u16 releases;
2009 void *p, *end;
2010 int ret;
2011
2012 ret = set_request_path_attr(req->r_inode, req->r_dentry,
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002013 req->r_parent, req->r_path1, req->r_ino1.ino,
Sage Weil2f2dc052009-10-06 11:31:09 -07002014 &path1, &pathlen1, &ino1, &freepath1);
2015 if (ret < 0) {
2016 msg = ERR_PTR(ret);
2017 goto out;
2018 }
2019
2020 ret = set_request_path_attr(NULL, req->r_old_dentry,
Jeff Laytonfd36a712016-12-15 08:37:58 -05002021 req->r_old_dentry_dir,
Sage Weil2f2dc052009-10-06 11:31:09 -07002022 req->r_path2, req->r_ino2.ino,
2023 &path2, &pathlen2, &ino2, &freepath2);
2024 if (ret < 0) {
2025 msg = ERR_PTR(ret);
2026 goto out_free1;
2027 }
2028
2029 len = sizeof(*head) +
Sage Weilb8e69062014-05-21 17:41:08 -07002030 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) +
Arnd Bergmann777d7382015-09-30 15:04:42 +02002031 sizeof(struct ceph_timespec);
Sage Weil2f2dc052009-10-06 11:31:09 -07002032
2033 /* calculate (max) length for cap releases */
2034 len += sizeof(struct ceph_mds_request_release) *
2035 (!!req->r_inode_drop + !!req->r_dentry_drop +
2036 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
2037 if (req->r_dentry_drop)
2038 len += req->r_dentry->d_name.len;
2039 if (req->r_old_dentry_drop)
2040 len += req->r_old_dentry->d_name.len;
2041
Sage Weilb61c2762011-08-09 15:03:46 -07002042 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07002043 if (!msg) {
2044 msg = ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -07002045 goto out_free2;
Sage Weila79832f2010-04-01 16:06:19 -07002046 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002047
John Spray7cfa0312014-10-30 17:15:26 +00002048 msg->hdr.version = cpu_to_le16(2);
Sage Weil6df058c2009-12-22 11:24:33 -08002049 msg->hdr.tid = cpu_to_le64(req->r_tid);
2050
Sage Weil2f2dc052009-10-06 11:31:09 -07002051 head = msg->front.iov_base;
2052 p = msg->front.iov_base + sizeof(*head);
2053 end = msg->front.iov_base + msg->front.iov_len;
2054
2055 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
2056 head->op = cpu_to_le32(req->r_op);
Eric W. Biedermanff3d0042013-01-31 04:01:53 -08002057 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid));
2058 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid));
Sage Weil2f2dc052009-10-06 11:31:09 -07002059 head->args = req->r_args;
2060
2061 ceph_encode_filepath(&p, end, ino1, path1);
2062 ceph_encode_filepath(&p, end, ino2, path2);
2063
Sage Weile979cf52010-07-15 14:58:39 -07002064 /* make note of release offset, in case we need to replay */
2065 req->r_request_release_offset = p - msg->front.iov_base;
2066
Sage Weil2f2dc052009-10-06 11:31:09 -07002067 /* cap releases */
2068 releases = 0;
2069 if (req->r_inode_drop)
2070 releases += ceph_encode_inode_release(&p,
David Howells2b0143b2015-03-17 22:25:59 +00002071 req->r_inode ? req->r_inode : d_inode(req->r_dentry),
Sage Weil2f2dc052009-10-06 11:31:09 -07002072 mds, req->r_inode_drop, req->r_inode_unless, 0);
2073 if (req->r_dentry_drop)
2074 releases += ceph_encode_dentry_release(&p, req->r_dentry,
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002075 req->r_parent, mds, req->r_dentry_drop,
Jeff Laytonca6c8ae2016-12-15 08:37:59 -05002076 req->r_dentry_unless);
Sage Weil2f2dc052009-10-06 11:31:09 -07002077 if (req->r_old_dentry_drop)
2078 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
Jeff Laytonca6c8ae2016-12-15 08:37:59 -05002079 req->r_old_dentry_dir, mds,
2080 req->r_old_dentry_drop,
2081 req->r_old_dentry_unless);
Sage Weil2f2dc052009-10-06 11:31:09 -07002082 if (req->r_old_inode_drop)
2083 releases += ceph_encode_inode_release(&p,
David Howells2b0143b2015-03-17 22:25:59 +00002084 d_inode(req->r_old_dentry),
Sage Weil2f2dc052009-10-06 11:31:09 -07002085 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002086
2087 if (drop_cap_releases) {
2088 releases = 0;
2089 p = msg->front.iov_base + req->r_request_release_offset;
2090 }
2091
Sage Weil2f2dc052009-10-06 11:31:09 -07002092 head->num_releases = cpu_to_le16(releases);
2093
Sage Weilb8e69062014-05-21 17:41:08 -07002094 /* time stamp */
Yan, Zheng1f041a82015-01-13 15:20:52 +08002095 {
2096 struct ceph_timespec ts;
2097 ceph_encode_timespec(&ts, &req->r_stamp);
2098 ceph_encode_copy(&p, &ts, sizeof(ts));
2099 }
Sage Weilb8e69062014-05-21 17:41:08 -07002100
Sage Weil2f2dc052009-10-06 11:31:09 -07002101 BUG_ON(p > end);
2102 msg->front.iov_len = p - msg->front.iov_base;
2103 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2104
Yan, Zheng25e6bae2014-09-16 19:15:28 +08002105 if (req->r_pagelist) {
2106 struct ceph_pagelist *pagelist = req->r_pagelist;
Elena Reshetova0e1a5ee2017-03-17 14:10:29 +02002107 refcount_inc(&pagelist->refcnt);
Yan, Zheng25e6bae2014-09-16 19:15:28 +08002108 ceph_msg_data_add_pagelist(msg, pagelist);
2109 msg->hdr.data_len = cpu_to_le32(pagelist->length);
2110 } else {
2111 msg->hdr.data_len = 0;
Alex Elderebf18f42013-03-04 22:29:57 -06002112 }
Alex Elder02afca62013-02-14 12:16:43 -06002113
Sage Weil2f2dc052009-10-06 11:31:09 -07002114 msg->hdr.data_off = cpu_to_le16(0);
2115
2116out_free2:
2117 if (freepath2)
2118 kfree((char *)path2);
2119out_free1:
2120 if (freepath1)
2121 kfree((char *)path1);
2122out:
2123 return msg;
2124}
2125
2126/*
2127 * called under mdsc->mutex if error, under no mutex if
2128 * success.
2129 */
2130static void complete_request(struct ceph_mds_client *mdsc,
2131 struct ceph_mds_request *req)
2132{
2133 if (req->r_callback)
2134 req->r_callback(mdsc, req);
2135 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002136 complete_all(&req->r_completion);
Sage Weil2f2dc052009-10-06 11:31:09 -07002137}
2138
2139/*
2140 * called under mdsc->mutex
2141 */
2142static int __prepare_send_request(struct ceph_mds_client *mdsc,
2143 struct ceph_mds_request *req,
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002144 int mds, bool drop_cap_releases)
Sage Weil2f2dc052009-10-06 11:31:09 -07002145{
2146 struct ceph_mds_request_head *rhead;
2147 struct ceph_msg *msg;
2148 int flags = 0;
2149
Sage Weil2f2dc052009-10-06 11:31:09 -07002150 req->r_attempts++;
Greg Farnume55b71f2010-06-22 15:58:01 -07002151 if (req->r_inode) {
2152 struct ceph_cap *cap =
2153 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
2154
2155 if (cap)
2156 req->r_sent_on_mseq = cap->mseq;
2157 else
2158 req->r_sent_on_mseq = -1;
2159 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002160 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
2161 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
2162
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002163 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002164 void *p;
Sage Weil01a92f12010-07-15 13:24:32 -07002165 /*
2166 * Replay. Do not regenerate message (and rebuild
2167 * paths, etc.); just use the original message.
2168 * Rebuilding paths will break for renames because
2169 * d_move mangles the src name.
2170 */
2171 msg = req->r_request;
2172 rhead = msg->front.iov_base;
2173
2174 flags = le32_to_cpu(rhead->flags);
2175 flags |= CEPH_MDS_FLAG_REPLAY;
2176 rhead->flags = cpu_to_le32(flags);
2177
2178 if (req->r_target_inode)
2179 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
2180
2181 rhead->num_retry = req->r_attempts - 1;
Sage Weile979cf52010-07-15 14:58:39 -07002182
2183 /* remove cap/dentry releases from message */
2184 rhead->num_releases = 0;
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002185
2186 /* time stamp */
2187 p = msg->front.iov_base + req->r_request_release_offset;
Yan, Zheng1f041a82015-01-13 15:20:52 +08002188 {
2189 struct ceph_timespec ts;
2190 ceph_encode_timespec(&ts, &req->r_stamp);
2191 ceph_encode_copy(&p, &ts, sizeof(ts));
2192 }
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002193
2194 msg->front.iov_len = p - msg->front.iov_base;
2195 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
Sage Weil01a92f12010-07-15 13:24:32 -07002196 return 0;
2197 }
2198
Sage Weil2f2dc052009-10-06 11:31:09 -07002199 if (req->r_request) {
2200 ceph_msg_put(req->r_request);
2201 req->r_request = NULL;
2202 }
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002203 msg = create_request_message(mdsc, req, mds, drop_cap_releases);
Sage Weil2f2dc052009-10-06 11:31:09 -07002204 if (IS_ERR(msg)) {
Sage Weile1518c72010-05-13 11:19:06 -07002205 req->r_err = PTR_ERR(msg);
Sage Weila79832f2010-04-01 16:06:19 -07002206 return PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002207 }
2208 req->r_request = msg;
2209
2210 rhead = msg->front.iov_base;
Sage Weil2f2dc052009-10-06 11:31:09 -07002211 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002212 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Sage Weil2f2dc052009-10-06 11:31:09 -07002213 flags |= CEPH_MDS_FLAG_REPLAY;
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002214 if (req->r_parent)
Sage Weil2f2dc052009-10-06 11:31:09 -07002215 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
2216 rhead->flags = cpu_to_le32(flags);
2217 rhead->num_fwd = req->r_num_fwd;
2218 rhead->num_retry = req->r_attempts - 1;
Sage Weil01a92f12010-07-15 13:24:32 -07002219 rhead->ino = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002220
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002221 dout(" r_parent = %p\n", req->r_parent);
Sage Weil2f2dc052009-10-06 11:31:09 -07002222 return 0;
2223}
2224
2225/*
2226 * send request, or put it on the appropriate wait list.
2227 */
2228static int __do_request(struct ceph_mds_client *mdsc,
2229 struct ceph_mds_request *req)
2230{
2231 struct ceph_mds_session *session = NULL;
2232 int mds = -1;
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002233 int err = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002234
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002235 if (req->r_err || test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
2236 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
Yan, Zhengeb1b8af2013-09-26 14:25:36 +08002237 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002238 goto out;
Yan, Zhengeb1b8af2013-09-26 14:25:36 +08002239 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002240
2241 if (req->r_timeout &&
2242 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
2243 dout("do_request timed out\n");
2244 err = -EIO;
2245 goto finish;
2246 }
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01002247 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002248 dout("do_request forced umount\n");
2249 err = -EIO;
2250 goto finish;
2251 }
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01002252 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_MOUNTING) {
Yan, Zhenge9e427f2016-11-10 16:02:06 +08002253 if (mdsc->mdsmap_err) {
2254 err = mdsc->mdsmap_err;
2255 dout("do_request mdsmap err %d\n", err);
2256 goto finish;
2257 }
Yan, Zhengcc8e8342017-01-04 16:21:58 +08002258 if (mdsc->mdsmap->m_epoch == 0) {
2259 dout("do_request no mdsmap, waiting for map\n");
2260 list_add(&req->r_wait, &mdsc->waiting_for_map);
2261 goto finish;
2262 }
Yan, Zhenge9e427f2016-11-10 16:02:06 +08002263 if (!(mdsc->fsc->mount_options->flags &
2264 CEPH_MOUNT_OPT_MOUNTWAIT) &&
2265 !ceph_mdsmap_is_cluster_available(mdsc->mdsmap)) {
2266 err = -ENOENT;
2267 pr_info("probably no mds server is up\n");
2268 goto finish;
2269 }
2270 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002271
Sage Weildc69e2e92010-11-02 13:49:00 -07002272 put_request_session(req);
2273
Sage Weil2f2dc052009-10-06 11:31:09 -07002274 mds = __choose_mds(mdsc, req);
2275 if (mds < 0 ||
2276 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
2277 dout("do_request no mds or not active, waiting for map\n");
2278 list_add(&req->r_wait, &mdsc->waiting_for_map);
2279 goto out;
2280 }
2281
2282 /* get, open session */
2283 session = __ceph_lookup_mds_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07002284 if (!session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002285 session = register_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07002286 if (IS_ERR(session)) {
2287 err = PTR_ERR(session);
2288 goto finish;
2289 }
2290 }
Sage Weildc69e2e92010-11-02 13:49:00 -07002291 req->r_session = get_session(session);
2292
Sage Weil2f2dc052009-10-06 11:31:09 -07002293 dout("do_request mds%d session %p state %s\n", mds, session,
John Spraya687eca2014-09-19 13:51:08 +01002294 ceph_session_state_name(session->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07002295 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
2296 session->s_state != CEPH_MDS_SESSION_HUNG) {
Yan, Zhengfcff4152016-09-14 16:39:51 +08002297 if (session->s_state == CEPH_MDS_SESSION_REJECTED) {
2298 err = -EACCES;
2299 goto out_session;
2300 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002301 if (session->s_state == CEPH_MDS_SESSION_NEW ||
2302 session->s_state == CEPH_MDS_SESSION_CLOSING)
2303 __open_session(mdsc, session);
2304 list_add(&req->r_wait, &session->s_waiting);
2305 goto out_session;
2306 }
2307
2308 /* send request */
Sage Weil2f2dc052009-10-06 11:31:09 -07002309 req->r_resend_mds = -1; /* forget any previous mds hint */
2310
2311 if (req->r_request_started == 0) /* note request start time */
2312 req->r_request_started = jiffies;
2313
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002314 err = __prepare_send_request(mdsc, req, mds, false);
Sage Weil2f2dc052009-10-06 11:31:09 -07002315 if (!err) {
2316 ceph_msg_get(req->r_request);
2317 ceph_con_send(&session->s_con, req->r_request);
2318 }
2319
2320out_session:
2321 ceph_put_mds_session(session);
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002322finish:
2323 if (err) {
2324 dout("__do_request early error %d\n", err);
2325 req->r_err = err;
2326 complete_request(mdsc, req);
2327 __unregister_request(mdsc, req);
2328 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002329out:
2330 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002331}
2332
2333/*
2334 * called under mdsc->mutex
2335 */
2336static void __wake_requests(struct ceph_mds_client *mdsc,
2337 struct list_head *head)
2338{
Yan, Zhenged75ec22012-11-19 10:49:06 +08002339 struct ceph_mds_request *req;
2340 LIST_HEAD(tmp_list);
Sage Weil2f2dc052009-10-06 11:31:09 -07002341
Yan, Zhenged75ec22012-11-19 10:49:06 +08002342 list_splice_init(head, &tmp_list);
2343
2344 while (!list_empty(&tmp_list)) {
2345 req = list_entry(tmp_list.next,
2346 struct ceph_mds_request, r_wait);
Sage Weil2f2dc052009-10-06 11:31:09 -07002347 list_del_init(&req->r_wait);
Sage Weil7971bd92013-05-01 21:15:58 -07002348 dout(" wake request %p tid %llu\n", req, req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002349 __do_request(mdsc, req);
2350 }
2351}
2352
2353/*
2354 * Wake up threads with requests pending for @mds, so that they can
Sage Weil29790f22010-03-18 14:45:05 -07002355 * resubmit their requests to a possibly different mds.
Sage Weil2f2dc052009-10-06 11:31:09 -07002356 */
Sage Weil29790f22010-03-18 14:45:05 -07002357static void kick_requests(struct ceph_mds_client *mdsc, int mds)
Sage Weil2f2dc052009-10-06 11:31:09 -07002358{
Sage Weil44ca18f2010-02-15 12:08:46 -08002359 struct ceph_mds_request *req;
Yan, Zheng282c1052014-07-30 10:12:47 +08002360 struct rb_node *p = rb_first(&mdsc->request_tree);
Sage Weil2f2dc052009-10-06 11:31:09 -07002361
2362 dout("kick_requests mds%d\n", mds);
Yan, Zheng282c1052014-07-30 10:12:47 +08002363 while (p) {
Sage Weil44ca18f2010-02-15 12:08:46 -08002364 req = rb_entry(p, struct ceph_mds_request, r_node);
Yan, Zheng282c1052014-07-30 10:12:47 +08002365 p = rb_next(p);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002366 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Sage Weil44ca18f2010-02-15 12:08:46 -08002367 continue;
Yan, Zheng3de22be2015-02-04 14:26:22 +08002368 if (req->r_attempts > 0)
2369 continue; /* only new requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08002370 if (req->r_session &&
2371 req->r_session->s_mds == mds) {
2372 dout(" kicking tid %llu\n", req->r_tid);
Yan, Zheng03974e82014-09-11 14:28:56 +08002373 list_del_init(&req->r_wait);
Sage Weil44ca18f2010-02-15 12:08:46 -08002374 __do_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002375 }
2376 }
2377}
2378
2379void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
2380 struct ceph_mds_request *req)
2381{
2382 dout("submit_request on %p\n", req);
2383 mutex_lock(&mdsc->mutex);
2384 __register_request(mdsc, req, NULL);
2385 __do_request(mdsc, req);
2386 mutex_unlock(&mdsc->mutex);
2387}
2388
2389/*
2390 * Synchrously perform an mds request. Take care of all of the
2391 * session setup, forwarding, retry details.
2392 */
2393int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
2394 struct inode *dir,
2395 struct ceph_mds_request *req)
2396{
2397 int err;
2398
2399 dout("do_request on %p\n", req);
2400
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002401 /* take CAP_PIN refs for r_inode, r_parent, r_old_dentry */
Sage Weil2f2dc052009-10-06 11:31:09 -07002402 if (req->r_inode)
2403 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002404 if (req->r_parent)
2405 ceph_get_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
Sage Weil844d87c2013-02-05 13:40:09 -08002406 if (req->r_old_dentry_dir)
Sage Weil41b02e12011-07-26 11:31:14 -07002407 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
2408 CEPH_CAP_PIN);
Sage Weil2f2dc052009-10-06 11:31:09 -07002409
2410 /* issue */
2411 mutex_lock(&mdsc->mutex);
2412 __register_request(mdsc, req, dir);
2413 __do_request(mdsc, req);
2414
Sage Weile1518c72010-05-13 11:19:06 -07002415 if (req->r_err) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002416 err = req->r_err;
Sage Weile1518c72010-05-13 11:19:06 -07002417 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07002418 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002419
Sage Weile1518c72010-05-13 11:19:06 -07002420 /* wait */
2421 mutex_unlock(&mdsc->mutex);
2422 dout("do_request waiting\n");
Ilya Dryomov5be73032015-05-19 12:05:38 +03002423 if (!req->r_timeout && req->r_wait_for_completion) {
Yan, Zheng9280be22014-10-14 10:33:35 +08002424 err = req->r_wait_for_completion(mdsc, req);
Sage Weile1518c72010-05-13 11:19:06 -07002425 } else {
Ilya Dryomov5be73032015-05-19 12:05:38 +03002426 long timeleft = wait_for_completion_killable_timeout(
2427 &req->r_completion,
2428 ceph_timeout_jiffies(req->r_timeout));
2429 if (timeleft > 0)
2430 err = 0;
2431 else if (!timeleft)
2432 err = -EIO; /* timed out */
2433 else
2434 err = timeleft; /* killed */
Sage Weile1518c72010-05-13 11:19:06 -07002435 }
2436 dout("do_request waited, got %d\n", err);
2437 mutex_lock(&mdsc->mutex);
2438
2439 /* only abort if we didn't race with a real reply */
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002440 if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
Sage Weile1518c72010-05-13 11:19:06 -07002441 err = le32_to_cpu(req->r_reply_info.head->result);
2442 } else if (err < 0) {
2443 dout("aborted request %lld with %d\n", req->r_tid, err);
Sage Weilb4556392010-05-13 12:01:13 -07002444
2445 /*
2446 * ensure we aren't running concurrently with
2447 * ceph_fill_trace or ceph_readdir_prepopulate, which
2448 * rely on locks (dir mutex) held by our caller.
2449 */
2450 mutex_lock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07002451 req->r_err = err;
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002452 set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
Sage Weilb4556392010-05-13 12:01:13 -07002453 mutex_unlock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07002454
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002455 if (req->r_parent &&
Sage Weil167c9e32010-05-14 10:02:57 -07002456 (req->r_op & CEPH_MDS_OP_WRITE))
2457 ceph_invalidate_dir_request(req);
Sage Weile1518c72010-05-13 11:19:06 -07002458 } else {
2459 err = req->r_err;
2460 }
2461
2462out:
2463 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002464 dout("do_request %p done, result %d\n", req, err);
2465 return err;
2466}
2467
2468/*
Yan, Zheng2f276c52013-03-13 19:44:32 +08002469 * Invalidate dir's completeness, dentry lease state on an aborted MDS
Sage Weil167c9e32010-05-14 10:02:57 -07002470 * namespace request.
2471 */
2472void ceph_invalidate_dir_request(struct ceph_mds_request *req)
2473{
Yan, Zheng8d8f3712017-11-24 11:51:32 +08002474 struct inode *dir = req->r_parent;
2475 struct inode *old_dir = req->r_old_dentry_dir;
Sage Weil167c9e32010-05-14 10:02:57 -07002476
Yan, Zheng8d8f3712017-11-24 11:51:32 +08002477 dout("invalidate_dir_request %p %p (complete, lease(s))\n", dir, old_dir);
Sage Weil167c9e32010-05-14 10:02:57 -07002478
Yan, Zheng8d8f3712017-11-24 11:51:32 +08002479 ceph_dir_clear_complete(dir);
2480 if (old_dir)
2481 ceph_dir_clear_complete(old_dir);
Sage Weil167c9e32010-05-14 10:02:57 -07002482 if (req->r_dentry)
2483 ceph_invalidate_dentry_lease(req->r_dentry);
2484 if (req->r_old_dentry)
2485 ceph_invalidate_dentry_lease(req->r_old_dentry);
2486}
2487
2488/*
Sage Weil2f2dc052009-10-06 11:31:09 -07002489 * Handle mds reply.
2490 *
2491 * We take the session mutex and parse and process the reply immediately.
2492 * This preserves the logical ordering of replies, capabilities, etc., sent
2493 * by the MDS as they are applied to our local cache.
2494 */
2495static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2496{
2497 struct ceph_mds_client *mdsc = session->s_mdsc;
2498 struct ceph_mds_request *req;
2499 struct ceph_mds_reply_head *head = msg->front.iov_base;
2500 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
Yan, Zheng982d6012014-12-23 15:30:54 +08002501 struct ceph_snap_realm *realm;
Sage Weil2f2dc052009-10-06 11:31:09 -07002502 u64 tid;
2503 int err, result;
Sage Weil2600d2d2010-02-22 15:12:16 -08002504 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002505
Sage Weil2f2dc052009-10-06 11:31:09 -07002506 if (msg->front.iov_len < sizeof(*head)) {
2507 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002508 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002509 return;
2510 }
2511
2512 /* get request, session */
Sage Weil6df058c2009-12-22 11:24:33 -08002513 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002514 mutex_lock(&mdsc->mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02002515 req = lookup_get_request(mdsc, tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002516 if (!req) {
2517 dout("handle_reply on unknown tid %llu\n", tid);
2518 mutex_unlock(&mdsc->mutex);
2519 return;
2520 }
2521 dout("handle_reply %p\n", req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002522
2523 /* correct session? */
Sage Weild96d6042010-03-20 20:50:58 -07002524 if (req->r_session != session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002525 pr_err("mdsc_handle_reply got %llu on session mds%d"
2526 " not mds%d\n", tid, session->s_mds,
2527 req->r_session ? req->r_session->s_mds : -1);
2528 mutex_unlock(&mdsc->mutex);
2529 goto out;
2530 }
2531
2532 /* dup? */
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002533 if ((test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags) && !head->safe) ||
2534 (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags) && head->safe)) {
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07002535 pr_warn("got a dup %s reply on %llu from mds%d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002536 head->safe ? "safe" : "unsafe", tid, mds);
2537 mutex_unlock(&mdsc->mutex);
2538 goto out;
2539 }
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002540 if (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags)) {
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07002541 pr_warn("got unsafe after safe on %llu from mds%d\n",
Sage Weil85792d0d2010-05-13 09:06:02 -07002542 tid, mds);
2543 mutex_unlock(&mdsc->mutex);
2544 goto out;
2545 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002546
2547 result = le32_to_cpu(head->result);
2548
2549 /*
Greg Farnume55b71f2010-06-22 15:58:01 -07002550 * Handle an ESTALE
2551 * if we're not talking to the authority, send to them
2552 * if the authority has changed while we weren't looking,
2553 * send to new authority
2554 * Otherwise we just have to return an ESTALE
Sage Weil2f2dc052009-10-06 11:31:09 -07002555 */
2556 if (result == -ESTALE) {
Chengguang Xu4c069a52018-01-30 16:29:17 +08002557 dout("got ESTALE on request %llu\n", req->r_tid);
Yan, Zheng51da8e82014-07-14 10:48:29 +08002558 req->r_resend_mds = -1;
Yan, Zhengca18bed2013-11-22 14:21:44 +08002559 if (req->r_direct_mode != USE_AUTH_MDS) {
Chengguang Xu4c069a52018-01-30 16:29:17 +08002560 dout("not using auth, setting for that now\n");
Greg Farnume55b71f2010-06-22 15:58:01 -07002561 req->r_direct_mode = USE_AUTH_MDS;
Sage Weil2f2dc052009-10-06 11:31:09 -07002562 __do_request(mdsc, req);
2563 mutex_unlock(&mdsc->mutex);
2564 goto out;
Greg Farnume55b71f2010-06-22 15:58:01 -07002565 } else {
Yan, Zhengca18bed2013-11-22 14:21:44 +08002566 int mds = __choose_mds(mdsc, req);
2567 if (mds >= 0 && mds != req->r_session->s_mds) {
Chengguang Xu4c069a52018-01-30 16:29:17 +08002568 dout("but auth changed, so resending\n");
Greg Farnume55b71f2010-06-22 15:58:01 -07002569 __do_request(mdsc, req);
2570 mutex_unlock(&mdsc->mutex);
2571 goto out;
2572 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002573 }
Chengguang Xu4c069a52018-01-30 16:29:17 +08002574 dout("have to return ESTALE on request %llu\n", req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002575 }
2576
Greg Farnume55b71f2010-06-22 15:58:01 -07002577
Sage Weil2f2dc052009-10-06 11:31:09 -07002578 if (head->safe) {
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002579 set_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags);
Sage Weil2f2dc052009-10-06 11:31:09 -07002580 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002581
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002582 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002583 /*
2584 * We already handled the unsafe response, now do the
2585 * cleanup. No need to examine the response; the MDS
2586 * doesn't include any result info in the safe
2587 * response. And even if it did, there is nothing
2588 * useful we could do with a revised return value.
2589 */
2590 dout("got safe reply %llu, mds%d\n", tid, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002591
2592 /* last unsafe request during umount? */
Sage Weil44ca18f2010-02-15 12:08:46 -08002593 if (mdsc->stopping && !__get_oldest_req(mdsc))
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002594 complete_all(&mdsc->safe_umount_waiters);
Sage Weil2f2dc052009-10-06 11:31:09 -07002595 mutex_unlock(&mdsc->mutex);
2596 goto out;
2597 }
Sage Weile1518c72010-05-13 11:19:06 -07002598 } else {
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002599 set_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags);
Sage Weil2f2dc052009-10-06 11:31:09 -07002600 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
Yan, Zheng4c06ace2015-10-27 17:18:00 +08002601 if (req->r_unsafe_dir) {
2602 struct ceph_inode_info *ci =
2603 ceph_inode(req->r_unsafe_dir);
2604 spin_lock(&ci->i_unsafe_lock);
2605 list_add_tail(&req->r_unsafe_dir_item,
2606 &ci->i_unsafe_dirops);
2607 spin_unlock(&ci->i_unsafe_lock);
2608 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002609 }
2610
2611 dout("handle_reply tid %lld result %d\n", tid, result);
2612 rinfo = &req->r_reply_info;
Sage Weil14303d22010-12-14 17:37:52 -08002613 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
Sage Weil2f2dc052009-10-06 11:31:09 -07002614 mutex_unlock(&mdsc->mutex);
2615
2616 mutex_lock(&session->s_mutex);
2617 if (err < 0) {
Herb Shiu25933ab2010-12-01 14:14:38 -08002618 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002619 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002620 goto out_err;
2621 }
2622
2623 /* snap trace */
Yan, Zheng982d6012014-12-23 15:30:54 +08002624 realm = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07002625 if (rinfo->snapblob_len) {
2626 down_write(&mdsc->snap_rwsem);
2627 ceph_update_snap_trace(mdsc, rinfo->snapblob,
Yan, Zheng982d6012014-12-23 15:30:54 +08002628 rinfo->snapblob + rinfo->snapblob_len,
2629 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP,
2630 &realm);
Sage Weil2f2dc052009-10-06 11:31:09 -07002631 downgrade_write(&mdsc->snap_rwsem);
2632 } else {
2633 down_read(&mdsc->snap_rwsem);
2634 }
2635
2636 /* insert trace into our cache */
Sage Weilb4556392010-05-13 12:01:13 -07002637 mutex_lock(&req->r_fill_mutex);
Yan, Zheng315f2402016-03-07 10:34:50 +08002638 current->journal_info = req;
Jeff Laytonf5a03b02017-01-31 11:06:13 -05002639 err = ceph_fill_trace(mdsc->fsc->sb, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002640 if (err == 0) {
Sam Lang6e8575f2012-12-28 09:56:46 -08002641 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
Yan, Zheng81c6aea2013-09-18 09:44:13 +08002642 req->r_op == CEPH_MDS_OP_LSSNAP))
Sage Weil2f2dc052009-10-06 11:31:09 -07002643 ceph_readdir_prepopulate(req, req->r_session);
Yehuda Sadeh37151662010-06-17 16:16:12 -07002644 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
Sage Weil2f2dc052009-10-06 11:31:09 -07002645 }
Yan, Zheng315f2402016-03-07 10:34:50 +08002646 current->journal_info = NULL;
Sage Weilb4556392010-05-13 12:01:13 -07002647 mutex_unlock(&req->r_fill_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002648
2649 up_read(&mdsc->snap_rwsem);
Yan, Zheng982d6012014-12-23 15:30:54 +08002650 if (realm)
2651 ceph_put_snap_realm(mdsc, realm);
Yan, Zheng68cd5b4b2015-10-27 18:36:06 +08002652
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002653 if (err == 0 && req->r_target_inode &&
2654 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Yan, Zheng68cd5b4b2015-10-27 18:36:06 +08002655 struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
2656 spin_lock(&ci->i_unsafe_lock);
2657 list_add_tail(&req->r_unsafe_target_item, &ci->i_unsafe_iops);
2658 spin_unlock(&ci->i_unsafe_lock);
2659 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002660out_err:
Sage Weile1518c72010-05-13 11:19:06 -07002661 mutex_lock(&mdsc->mutex);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002662 if (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
Sage Weile1518c72010-05-13 11:19:06 -07002663 if (err) {
2664 req->r_err = err;
2665 } else {
Jianpeng Ma5fdb1382015-08-18 10:30:38 +08002666 req->r_reply = ceph_msg_get(msg);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002667 set_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags);
Sage Weile1518c72010-05-13 11:19:06 -07002668 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002669 } else {
Sage Weile1518c72010-05-13 11:19:06 -07002670 dout("reply arrived after request %lld was aborted\n", tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002671 }
Sage Weile1518c72010-05-13 11:19:06 -07002672 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002673
Sage Weil2f2dc052009-10-06 11:31:09 -07002674 mutex_unlock(&session->s_mutex);
2675
2676 /* kick calling process */
2677 complete_request(mdsc, req);
2678out:
2679 ceph_mdsc_put_request(req);
2680 return;
2681}
2682
2683
2684
2685/*
2686 * handle mds notification that our request has been forwarded.
2687 */
Sage Weil2600d2d2010-02-22 15:12:16 -08002688static void handle_forward(struct ceph_mds_client *mdsc,
2689 struct ceph_mds_session *session,
2690 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002691{
2692 struct ceph_mds_request *req;
Sage Weila1ea7872010-02-23 14:02:44 -08002693 u64 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002694 u32 next_mds;
2695 u32 fwd_seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002696 int err = -EINVAL;
2697 void *p = msg->front.iov_base;
2698 void *end = p + msg->front.iov_len;
Sage Weil2f2dc052009-10-06 11:31:09 -07002699
Sage Weila1ea7872010-02-23 14:02:44 -08002700 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002701 next_mds = ceph_decode_32(&p);
2702 fwd_seq = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07002703
2704 mutex_lock(&mdsc->mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02002705 req = lookup_get_request(mdsc, tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002706 if (!req) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07002707 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002708 goto out; /* dup reply? */
2709 }
2710
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002711 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07002712 dout("forward tid %llu aborted, unregistering\n", tid);
2713 __unregister_request(mdsc, req);
2714 } else if (fwd_seq <= req->r_num_fwd) {
2715 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002716 tid, next_mds, req->r_num_fwd, fwd_seq);
2717 } else {
2718 /* resend. forward race not possible; mds would drop */
Sage Weil2a8e5e32010-05-28 16:43:16 -07002719 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2720 BUG_ON(req->r_err);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002721 BUG_ON(test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags));
Yan, Zheng3de22be2015-02-04 14:26:22 +08002722 req->r_attempts = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002723 req->r_num_fwd = fwd_seq;
2724 req->r_resend_mds = next_mds;
2725 put_request_session(req);
2726 __do_request(mdsc, req);
2727 }
2728 ceph_mdsc_put_request(req);
2729out:
2730 mutex_unlock(&mdsc->mutex);
2731 return;
2732
2733bad:
2734 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2735}
2736
2737/*
2738 * handle a mds session control message
2739 */
2740static void handle_session(struct ceph_mds_session *session,
2741 struct ceph_msg *msg)
2742{
2743 struct ceph_mds_client *mdsc = session->s_mdsc;
2744 u32 op;
2745 u64 seq;
Sage Weil2600d2d2010-02-22 15:12:16 -08002746 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002747 struct ceph_mds_session_head *h = msg->front.iov_base;
2748 int wake = 0;
2749
Sage Weil2f2dc052009-10-06 11:31:09 -07002750 /* decode */
2751 if (msg->front.iov_len != sizeof(*h))
2752 goto bad;
2753 op = le32_to_cpu(h->op);
2754 seq = le64_to_cpu(h->seq);
2755
2756 mutex_lock(&mdsc->mutex);
Yan, Zheng0a07fc82017-03-29 15:30:24 +08002757 if (op == CEPH_SESSION_CLOSE) {
2758 get_session(session);
Sage Weil2600d2d2010-02-22 15:12:16 -08002759 __unregister_session(mdsc, session);
Yan, Zheng0a07fc82017-03-29 15:30:24 +08002760 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002761 /* FIXME: this ttl calculation is generous */
2762 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2763 mutex_unlock(&mdsc->mutex);
2764
2765 mutex_lock(&session->s_mutex);
2766
2767 dout("handle_session mds%d %s %p state %s seq %llu\n",
2768 mds, ceph_session_op_name(op), session,
John Spraya687eca2014-09-19 13:51:08 +01002769 ceph_session_state_name(session->s_state), seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002770
2771 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2772 session->s_state = CEPH_MDS_SESSION_OPEN;
2773 pr_info("mds%d came back\n", session->s_mds);
2774 }
2775
2776 switch (op) {
2777 case CEPH_SESSION_OPEN:
Sage Weil29790f22010-03-18 14:45:05 -07002778 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2779 pr_info("mds%d reconnect success\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002780 session->s_state = CEPH_MDS_SESSION_OPEN;
2781 renewed_caps(mdsc, session, 0);
2782 wake = 1;
2783 if (mdsc->stopping)
2784 __close_session(mdsc, session);
2785 break;
2786
2787 case CEPH_SESSION_RENEWCAPS:
2788 if (session->s_renew_seq == seq)
2789 renewed_caps(mdsc, session, 1);
2790 break;
2791
2792 case CEPH_SESSION_CLOSE:
Sage Weil29790f22010-03-18 14:45:05 -07002793 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2794 pr_info("mds%d reconnect denied\n", session->s_mds);
Yan, Zheng1c841a92015-03-24 20:15:36 +08002795 cleanup_session_requests(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002796 remove_session_caps(session);
Yan, Zheng656e4382014-09-11 14:25:18 +08002797 wake = 2; /* for good measure */
Sage Weilf3c60c52010-08-11 14:51:23 -07002798 wake_up_all(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002799 break;
2800
2801 case CEPH_SESSION_STALE:
2802 pr_info("mds%d caps went stale, renewing\n",
2803 session->s_mds);
Alex Elderd8fb02a2012-01-12 17:48:10 -08002804 spin_lock(&session->s_gen_ttl_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002805 session->s_cap_gen++;
Alex Elder1ce208a2012-01-12 17:48:11 -08002806 session->s_cap_ttl = jiffies - 1;
Alex Elderd8fb02a2012-01-12 17:48:10 -08002807 spin_unlock(&session->s_gen_ttl_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002808 send_renew_caps(mdsc, session);
2809 break;
2810
2811 case CEPH_SESSION_RECALL_STATE:
Zhi Zhange30ee582018-01-24 21:24:33 +08002812 ceph_trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
Sage Weil2f2dc052009-10-06 11:31:09 -07002813 break;
2814
Yan, Zheng186e4f72013-11-22 14:48:37 +08002815 case CEPH_SESSION_FLUSHMSG:
2816 send_flushmsg_ack(mdsc, session, seq);
2817 break;
2818
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08002819 case CEPH_SESSION_FORCE_RO:
2820 dout("force_session_readonly %p\n", session);
2821 spin_lock(&session->s_cap_lock);
2822 session->s_readonly = true;
2823 spin_unlock(&session->s_cap_lock);
2824 wake_up_session_caps(session, 0);
2825 break;
2826
Yan, Zhengfcff4152016-09-14 16:39:51 +08002827 case CEPH_SESSION_REJECT:
2828 WARN_ON(session->s_state != CEPH_MDS_SESSION_OPENING);
2829 pr_info("mds%d rejected session\n", session->s_mds);
2830 session->s_state = CEPH_MDS_SESSION_REJECTED;
2831 cleanup_session_requests(mdsc, session);
2832 remove_session_caps(session);
2833 wake = 2; /* for good measure */
2834 break;
2835
Sage Weil2f2dc052009-10-06 11:31:09 -07002836 default:
2837 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2838 WARN_ON(1);
2839 }
2840
2841 mutex_unlock(&session->s_mutex);
2842 if (wake) {
2843 mutex_lock(&mdsc->mutex);
2844 __wake_requests(mdsc, &session->s_waiting);
Yan, Zheng656e4382014-09-11 14:25:18 +08002845 if (wake == 2)
2846 kick_requests(mdsc, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002847 mutex_unlock(&mdsc->mutex);
2848 }
Yan, Zheng0a07fc82017-03-29 15:30:24 +08002849 if (op == CEPH_SESSION_CLOSE)
2850 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002851 return;
2852
2853bad:
2854 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2855 (int)msg->front.iov_len);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002856 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002857 return;
2858}
2859
2860
2861/*
2862 * called under session->mutex.
2863 */
2864static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2865 struct ceph_mds_session *session)
2866{
2867 struct ceph_mds_request *req, *nreq;
Yan, Zheng3de22be2015-02-04 14:26:22 +08002868 struct rb_node *p;
Sage Weil2f2dc052009-10-06 11:31:09 -07002869 int err;
2870
2871 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2872
2873 mutex_lock(&mdsc->mutex);
2874 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002875 err = __prepare_send_request(mdsc, req, session->s_mds, true);
Sage Weil2f2dc052009-10-06 11:31:09 -07002876 if (!err) {
2877 ceph_msg_get(req->r_request);
2878 ceph_con_send(&session->s_con, req->r_request);
2879 }
2880 }
Yan, Zheng3de22be2015-02-04 14:26:22 +08002881
2882 /*
2883 * also re-send old requests when MDS enters reconnect stage. So that MDS
2884 * can process completed request in clientreplay stage.
2885 */
2886 p = rb_first(&mdsc->request_tree);
2887 while (p) {
2888 req = rb_entry(p, struct ceph_mds_request, r_node);
2889 p = rb_next(p);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002890 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Yan, Zheng3de22be2015-02-04 14:26:22 +08002891 continue;
2892 if (req->r_attempts == 0)
2893 continue; /* only old requests */
2894 if (req->r_session &&
2895 req->r_session->s_mds == session->s_mds) {
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002896 err = __prepare_send_request(mdsc, req,
2897 session->s_mds, true);
Yan, Zheng3de22be2015-02-04 14:26:22 +08002898 if (!err) {
2899 ceph_msg_get(req->r_request);
2900 ceph_con_send(&session->s_con, req->r_request);
2901 }
2902 }
2903 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002904 mutex_unlock(&mdsc->mutex);
2905}
2906
2907/*
2908 * Encode information about a cap for a reconnect with the MDS.
2909 */
Sage Weil2f2dc052009-10-06 11:31:09 -07002910static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2911 void *arg)
2912{
Sage Weil20cb34a2010-05-12 15:21:32 -07002913 union {
2914 struct ceph_mds_cap_reconnect v2;
2915 struct ceph_mds_cap_reconnect_v1 v1;
2916 } rec;
Yan, Zhengb3f8d682017-09-11 10:58:55 +08002917 struct ceph_inode_info *ci = cap->ci;
Sage Weil20cb34a2010-05-12 15:21:32 -07002918 struct ceph_reconnect_state *recon_state = arg;
2919 struct ceph_pagelist *pagelist = recon_state->pagelist;
Sage Weil2f2dc052009-10-06 11:31:09 -07002920 char *path;
2921 int pathlen, err;
2922 u64 pathbase;
Yan, Zheng3469ed02016-07-05 09:32:31 +08002923 u64 snap_follows;
Sage Weil2f2dc052009-10-06 11:31:09 -07002924 struct dentry *dentry;
2925
Sage Weil2f2dc052009-10-06 11:31:09 -07002926 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2927 inode, ceph_vinop(inode), cap, cap->cap_id,
2928 ceph_cap_string(cap->issued));
Sage Weil93cea5b2009-12-23 12:21:51 -08002929 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2930 if (err)
2931 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002932
2933 dentry = d_find_alias(inode);
2934 if (dentry) {
2935 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2936 if (IS_ERR(path)) {
2937 err = PTR_ERR(path);
Sage Weile072f8a2010-08-26 09:26:37 -07002938 goto out_dput;
Sage Weil2f2dc052009-10-06 11:31:09 -07002939 }
2940 } else {
2941 path = NULL;
2942 pathlen = 0;
Ilya Dryomov4eacd4c2016-08-09 16:12:09 +02002943 pathbase = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002944 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002945
Sage Weilbe655592011-11-30 09:47:09 -08002946 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002947 cap->seq = 0; /* reset cap seq */
2948 cap->issue_seq = 0; /* and issue_seq */
Yan, Zheng667ca052013-05-31 16:25:36 +08002949 cap->mseq = 0; /* and migrate_seq */
Yan, Zheng99a9c272013-09-22 11:08:14 +08002950 cap->cap_gen = cap->session->s_cap_gen;
Sage Weil20cb34a2010-05-12 15:21:32 -07002951
Yan, Zheng121f22a2016-07-04 22:05:18 +08002952 if (recon_state->msg_version >= 2) {
Sage Weil20cb34a2010-05-12 15:21:32 -07002953 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
2954 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2955 rec.v2.issued = cpu_to_le32(cap->issued);
2956 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2957 rec.v2.pathbase = cpu_to_le64(pathbase);
Jeff Laytonec1dff22017-10-31 15:51:14 -04002958 rec.v2.flock_len = (__force __le32)
2959 ((ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) ? 0 : 1);
Sage Weil20cb34a2010-05-12 15:21:32 -07002960 } else {
Deepa Dinamani95582b02018-05-08 19:36:02 -07002961 struct timespec ts;
Sage Weil20cb34a2010-05-12 15:21:32 -07002962 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
2963 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2964 rec.v1.issued = cpu_to_le32(cap->issued);
2965 rec.v1.size = cpu_to_le64(inode->i_size);
Deepa Dinamani95582b02018-05-08 19:36:02 -07002966 ts = timespec64_to_timespec(inode->i_mtime);
2967 ceph_encode_timespec(&rec.v1.mtime, &ts);
2968 ts = timespec64_to_timespec(inode->i_atime);
2969 ceph_encode_timespec(&rec.v1.atime, &ts);
Sage Weil20cb34a2010-05-12 15:21:32 -07002970 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2971 rec.v1.pathbase = cpu_to_le64(pathbase);
Sage Weil20cb34a2010-05-12 15:21:32 -07002972 }
Yan, Zheng3469ed02016-07-05 09:32:31 +08002973
2974 if (list_empty(&ci->i_cap_snaps)) {
Yan, Zheng92776fd2017-08-16 21:42:39 +08002975 snap_follows = ci->i_head_snapc ? ci->i_head_snapc->seq : 0;
Yan, Zheng3469ed02016-07-05 09:32:31 +08002976 } else {
2977 struct ceph_cap_snap *capsnap =
2978 list_first_entry(&ci->i_cap_snaps,
2979 struct ceph_cap_snap, ci_item);
2980 snap_follows = capsnap->follows;
Sage Weil93cea5b2009-12-23 12:21:51 -08002981 }
Sage Weilbe655592011-11-30 09:47:09 -08002982 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002983
Yan, Zheng121f22a2016-07-04 22:05:18 +08002984 if (recon_state->msg_version >= 2) {
Greg Farnum40819f62010-08-02 15:34:23 -07002985 int num_fcntl_locks, num_flock_locks;
Yan, Zheng4deb14a2017-09-11 10:36:28 +08002986 struct ceph_filelock *flocks = NULL;
Yan, Zheng121f22a2016-07-04 22:05:18 +08002987 size_t struct_len, total_len = 0;
2988 u8 struct_v = 0;
Greg Farnum40819f62010-08-02 15:34:23 -07002989
Jim Schutt39be95e2013-05-15 13:03:35 -05002990encode_again:
Yan, Zhengb3f8d682017-09-11 10:58:55 +08002991 if (rec.v2.flock_len) {
2992 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
2993 } else {
2994 num_fcntl_locks = 0;
2995 num_flock_locks = 0;
2996 }
Yan, Zheng4deb14a2017-09-11 10:36:28 +08002997 if (num_fcntl_locks + num_flock_locks > 0) {
Kees Cook6da2ec52018-06-12 13:55:00 -07002998 flocks = kmalloc_array(num_fcntl_locks + num_flock_locks,
2999 sizeof(struct ceph_filelock),
3000 GFP_NOFS);
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003001 if (!flocks) {
3002 err = -ENOMEM;
3003 goto out_free;
3004 }
3005 err = ceph_encode_locks_to_buffer(inode, flocks,
3006 num_fcntl_locks,
3007 num_flock_locks);
3008 if (err) {
3009 kfree(flocks);
3010 flocks = NULL;
3011 if (err == -ENOSPC)
3012 goto encode_again;
3013 goto out_free;
3014 }
3015 } else {
Jim Schutt39be95e2013-05-15 13:03:35 -05003016 kfree(flocks);
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003017 flocks = NULL;
Jim Schutt39be95e2013-05-15 13:03:35 -05003018 }
Yan, Zheng121f22a2016-07-04 22:05:18 +08003019
3020 if (recon_state->msg_version >= 3) {
3021 /* version, compat_version and struct_len */
3022 total_len = 2 * sizeof(u8) + sizeof(u32);
Yan, Zheng3469ed02016-07-05 09:32:31 +08003023 struct_v = 2;
Yan, Zheng121f22a2016-07-04 22:05:18 +08003024 }
Jim Schutt39be95e2013-05-15 13:03:35 -05003025 /*
3026 * number of encoded locks is stable, so copy to pagelist
3027 */
Yan, Zheng121f22a2016-07-04 22:05:18 +08003028 struct_len = 2 * sizeof(u32) +
3029 (num_fcntl_locks + num_flock_locks) *
3030 sizeof(struct ceph_filelock);
3031 rec.v2.flock_len = cpu_to_le32(struct_len);
3032
3033 struct_len += sizeof(rec.v2);
3034 struct_len += sizeof(u32) + pathlen;
3035
Yan, Zheng3469ed02016-07-05 09:32:31 +08003036 if (struct_v >= 2)
3037 struct_len += sizeof(u64); /* snap_follows */
3038
Yan, Zheng121f22a2016-07-04 22:05:18 +08003039 total_len += struct_len;
3040 err = ceph_pagelist_reserve(pagelist, total_len);
3041
3042 if (!err) {
3043 if (recon_state->msg_version >= 3) {
3044 ceph_pagelist_encode_8(pagelist, struct_v);
3045 ceph_pagelist_encode_8(pagelist, 1);
3046 ceph_pagelist_encode_32(pagelist, struct_len);
3047 }
3048 ceph_pagelist_encode_string(pagelist, path, pathlen);
3049 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v2));
3050 ceph_locks_to_pagelist(flocks, pagelist,
3051 num_fcntl_locks,
3052 num_flock_locks);
Yan, Zheng3469ed02016-07-05 09:32:31 +08003053 if (struct_v >= 2)
3054 ceph_pagelist_encode_64(pagelist, snap_follows);
Yan, Zheng121f22a2016-07-04 22:05:18 +08003055 }
Jim Schutt39be95e2013-05-15 13:03:35 -05003056 kfree(flocks);
Sage Weil3612abb2010-09-07 15:59:27 -07003057 } else {
Yan, Zheng121f22a2016-07-04 22:05:18 +08003058 size_t size = sizeof(u32) + pathlen + sizeof(rec.v1);
3059 err = ceph_pagelist_reserve(pagelist, size);
3060 if (!err) {
3061 ceph_pagelist_encode_string(pagelist, path, pathlen);
3062 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v1));
3063 }
Greg Farnum40819f62010-08-02 15:34:23 -07003064 }
Yan, Zheng44c99752013-09-22 10:28:10 +08003065
3066 recon_state->nr_caps++;
Sage Weile072f8a2010-08-26 09:26:37 -07003067out_free:
Sage Weil2f2dc052009-10-06 11:31:09 -07003068 kfree(path);
Sage Weile072f8a2010-08-26 09:26:37 -07003069out_dput:
Sage Weil2f2dc052009-10-06 11:31:09 -07003070 dput(dentry);
Sage Weil93cea5b2009-12-23 12:21:51 -08003071 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07003072}
3073
3074
3075/*
3076 * If an MDS fails and recovers, clients need to reconnect in order to
3077 * reestablish shared state. This includes all caps issued through
3078 * this session _and_ the snap_realm hierarchy. Because it's not
3079 * clear which snap realms the mds cares about, we send everything we
3080 * know about.. that ensures we'll then get any new info the
3081 * recovering MDS might have.
3082 *
3083 * This is a relatively heavyweight operation, but it's rare.
3084 *
3085 * called with mdsc->mutex held.
3086 */
Sage Weil34b6c852010-05-10 16:31:25 -07003087static void send_mds_reconnect(struct ceph_mds_client *mdsc,
3088 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07003089{
Sage Weil2f2dc052009-10-06 11:31:09 -07003090 struct ceph_msg *reply;
Sage Weila105f002010-02-15 14:37:55 -08003091 struct rb_node *p;
Sage Weil34b6c852010-05-10 16:31:25 -07003092 int mds = session->s_mds;
Sage Weil9abf82b2010-05-10 21:58:38 -07003093 int err = -ENOMEM;
Yan, Zheng44c99752013-09-22 10:28:10 +08003094 int s_nr_caps;
Sage Weil93cea5b2009-12-23 12:21:51 -08003095 struct ceph_pagelist *pagelist;
Sage Weil20cb34a2010-05-12 15:21:32 -07003096 struct ceph_reconnect_state recon_state;
Jeff Laytonc8a96a32017-10-19 08:53:58 -04003097 LIST_HEAD(dispose);
Sage Weil2f2dc052009-10-06 11:31:09 -07003098
Sage Weil34b6c852010-05-10 16:31:25 -07003099 pr_info("mds%d reconnect start\n", mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003100
Sage Weil93cea5b2009-12-23 12:21:51 -08003101 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
3102 if (!pagelist)
3103 goto fail_nopagelist;
3104 ceph_pagelist_init(pagelist);
3105
Sage Weilb61c2762011-08-09 15:03:46 -07003106 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07003107 if (!reply)
Sage Weil93cea5b2009-12-23 12:21:51 -08003108 goto fail_nomsg;
Sage Weil93cea5b2009-12-23 12:21:51 -08003109
Sage Weil34b6c852010-05-10 16:31:25 -07003110 mutex_lock(&session->s_mutex);
3111 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
3112 session->s_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003113
Sage Weil2f2dc052009-10-06 11:31:09 -07003114 dout("session %p state %s\n", session,
John Spraya687eca2014-09-19 13:51:08 +01003115 ceph_session_state_name(session->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07003116
Yan, Zheng99a9c272013-09-22 11:08:14 +08003117 spin_lock(&session->s_gen_ttl_lock);
3118 session->s_cap_gen++;
3119 spin_unlock(&session->s_gen_ttl_lock);
3120
3121 spin_lock(&session->s_cap_lock);
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08003122 /* don't know if session is readonly */
3123 session->s_readonly = 0;
Yan, Zheng99a9c272013-09-22 11:08:14 +08003124 /*
3125 * notify __ceph_remove_cap() that we are composing cap reconnect.
3126 * If a cap get released before being added to the cap reconnect,
3127 * __ceph_remove_cap() should skip queuing cap release.
3128 */
3129 session->s_cap_reconnect = 1;
Sage Weile01a5942010-05-10 15:36:44 -07003130 /* drop old cap expires; we're about to reestablish that state */
Jeff Laytonc8a96a32017-10-19 08:53:58 -04003131 detach_cap_releases(session, &dispose);
3132 spin_unlock(&session->s_cap_lock);
3133 dispose_cap_releases(mdsc, &dispose);
Sage Weile01a5942010-05-10 15:36:44 -07003134
Yan, Zheng5d233712014-09-10 16:56:23 +08003135 /* trim unused caps to reduce MDS's cache rejoin time */
Yan, Zhengc0bd50e2015-04-07 15:51:08 +08003136 if (mdsc->fsc->sb->s_root)
3137 shrink_dcache_parent(mdsc->fsc->sb->s_root);
Yan, Zheng5d233712014-09-10 16:56:23 +08003138
3139 ceph_con_close(&session->s_con);
3140 ceph_con_open(&session->s_con,
3141 CEPH_ENTITY_TYPE_MDS, mds,
3142 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
3143
3144 /* replay unsafe requests */
3145 replay_unsafe_requests(mdsc, session);
3146
3147 down_read(&mdsc->snap_rwsem);
3148
Sage Weil2f2dc052009-10-06 11:31:09 -07003149 /* traverse this session's caps */
Yan, Zheng44c99752013-09-22 10:28:10 +08003150 s_nr_caps = session->s_nr_caps;
3151 err = ceph_pagelist_encode_32(pagelist, s_nr_caps);
Sage Weil93cea5b2009-12-23 12:21:51 -08003152 if (err)
3153 goto fail;
Sage Weil20cb34a2010-05-12 15:21:32 -07003154
Yan, Zheng44c99752013-09-22 10:28:10 +08003155 recon_state.nr_caps = 0;
Sage Weil20cb34a2010-05-12 15:21:32 -07003156 recon_state.pagelist = pagelist;
Yan, Zheng121f22a2016-07-04 22:05:18 +08003157 if (session->s_con.peer_features & CEPH_FEATURE_MDSENC)
3158 recon_state.msg_version = 3;
3159 else if (session->s_con.peer_features & CEPH_FEATURE_FLOCK)
3160 recon_state.msg_version = 2;
3161 else
3162 recon_state.msg_version = 1;
Sage Weil20cb34a2010-05-12 15:21:32 -07003163 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
Sage Weil2f2dc052009-10-06 11:31:09 -07003164 if (err < 0)
Sage Weil9abf82b2010-05-10 21:58:38 -07003165 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07003166
Yan, Zheng99a9c272013-09-22 11:08:14 +08003167 spin_lock(&session->s_cap_lock);
3168 session->s_cap_reconnect = 0;
3169 spin_unlock(&session->s_cap_lock);
3170
Sage Weil2f2dc052009-10-06 11:31:09 -07003171 /*
3172 * snaprealms. we provide mds with the ino, seq (version), and
3173 * parent for all of our realms. If the mds has any newer info,
3174 * it will tell us.
3175 */
Sage Weila105f002010-02-15 14:37:55 -08003176 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
3177 struct ceph_snap_realm *realm =
3178 rb_entry(p, struct ceph_snap_realm, node);
Sage Weil93cea5b2009-12-23 12:21:51 -08003179 struct ceph_mds_snaprealm_reconnect sr_rec;
Sage Weil2f2dc052009-10-06 11:31:09 -07003180
3181 dout(" adding snap realm %llx seq %lld parent %llx\n",
3182 realm->ino, realm->seq, realm->parent_ino);
Sage Weil93cea5b2009-12-23 12:21:51 -08003183 sr_rec.ino = cpu_to_le64(realm->ino);
3184 sr_rec.seq = cpu_to_le64(realm->seq);
3185 sr_rec.parent = cpu_to_le64(realm->parent_ino);
3186 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
3187 if (err)
3188 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07003189 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003190
Yan, Zheng121f22a2016-07-04 22:05:18 +08003191 reply->hdr.version = cpu_to_le16(recon_state.msg_version);
Yan, Zheng44c99752013-09-22 10:28:10 +08003192
3193 /* raced with cap release? */
3194 if (s_nr_caps != recon_state.nr_caps) {
3195 struct page *page = list_first_entry(&pagelist->head,
3196 struct page, lru);
3197 __le32 *addr = kmap_atomic(page);
3198 *addr = cpu_to_le32(recon_state.nr_caps);
3199 kunmap_atomic(addr);
Alex Elderebf18f42013-03-04 22:29:57 -06003200 }
Yan, Zheng44c99752013-09-22 10:28:10 +08003201
3202 reply->hdr.data_len = cpu_to_le32(pagelist->length);
3203 ceph_msg_data_add_pagelist(reply, pagelist);
Yan, Zhenge548e9b2015-06-10 15:17:56 +08003204
3205 ceph_early_kick_flushing_caps(mdsc, session);
3206
Sage Weil2f2dc052009-10-06 11:31:09 -07003207 ceph_con_send(&session->s_con, reply);
3208
Sage Weil9abf82b2010-05-10 21:58:38 -07003209 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003210
Sage Weil9abf82b2010-05-10 21:58:38 -07003211 mutex_lock(&mdsc->mutex);
3212 __wake_requests(mdsc, &session->s_waiting);
3213 mutex_unlock(&mdsc->mutex);
3214
Sage Weil2f2dc052009-10-06 11:31:09 -07003215 up_read(&mdsc->snap_rwsem);
Sage Weil2f2dc052009-10-06 11:31:09 -07003216 return;
3217
Sage Weil93cea5b2009-12-23 12:21:51 -08003218fail:
Sage Weil2f2dc052009-10-06 11:31:09 -07003219 ceph_msg_put(reply);
Sage Weil9abf82b2010-05-10 21:58:38 -07003220 up_read(&mdsc->snap_rwsem);
3221 mutex_unlock(&session->s_mutex);
Sage Weil93cea5b2009-12-23 12:21:51 -08003222fail_nomsg:
3223 ceph_pagelist_release(pagelist);
Sage Weil93cea5b2009-12-23 12:21:51 -08003224fail_nopagelist:
Sage Weil9abf82b2010-05-10 21:58:38 -07003225 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
Sage Weil9abf82b2010-05-10 21:58:38 -07003226 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07003227}
3228
3229
3230/*
3231 * compare old and new mdsmaps, kicking requests
3232 * and closing out old connections as necessary
3233 *
3234 * called under mdsc->mutex.
3235 */
3236static void check_new_map(struct ceph_mds_client *mdsc,
3237 struct ceph_mdsmap *newmap,
3238 struct ceph_mdsmap *oldmap)
3239{
3240 int i;
3241 int oldstate, newstate;
3242 struct ceph_mds_session *s;
3243
3244 dout("check_new_map new %u old %u\n",
3245 newmap->m_epoch, oldmap->m_epoch);
3246
Yan, Zheng76201b62017-03-28 17:04:13 +08003247 for (i = 0; i < oldmap->m_num_mds && i < mdsc->max_sessions; i++) {
Markus Elfringd37b1d92017-08-20 20:22:02 +02003248 if (!mdsc->sessions[i])
Sage Weil2f2dc052009-10-06 11:31:09 -07003249 continue;
3250 s = mdsc->sessions[i];
3251 oldstate = ceph_mdsmap_get_state(oldmap, i);
3252 newstate = ceph_mdsmap_get_state(newmap, i);
3253
Sage Weil0deb01c2010-06-17 14:19:01 -07003254 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07003255 i, ceph_mds_state_name(oldstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07003256 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
Sage Weil2f2dc052009-10-06 11:31:09 -07003257 ceph_mds_state_name(newstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07003258 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
John Spraya687eca2014-09-19 13:51:08 +01003259 ceph_session_state_name(s->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07003260
Yan, Zheng76201b62017-03-28 17:04:13 +08003261 if (i >= newmap->m_num_mds ||
Yan, Zheng3e8f43a2012-09-20 17:42:25 +08003262 memcmp(ceph_mdsmap_get_addr(oldmap, i),
Sage Weil2f2dc052009-10-06 11:31:09 -07003263 ceph_mdsmap_get_addr(newmap, i),
3264 sizeof(struct ceph_entity_addr))) {
3265 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
3266 /* the session never opened, just close it
3267 * out now */
Yan, Zheng0a07fc82017-03-29 15:30:24 +08003268 get_session(s);
Sage Weil2600d2d2010-02-22 15:12:16 -08003269 __unregister_session(mdsc, s);
Yan, Zheng0a07fc82017-03-29 15:30:24 +08003270 __wake_requests(mdsc, &s->s_waiting);
3271 ceph_put_mds_session(s);
Yan, Zheng28275282017-03-28 17:56:29 +08003272 } else if (i >= newmap->m_num_mds) {
3273 /* force close session for stopped mds */
3274 get_session(s);
3275 __unregister_session(mdsc, s);
3276 __wake_requests(mdsc, &s->s_waiting);
3277 kick_requests(mdsc, i);
3278 mutex_unlock(&mdsc->mutex);
3279
3280 mutex_lock(&s->s_mutex);
3281 cleanup_session_requests(mdsc, s);
3282 remove_session_caps(s);
3283 mutex_unlock(&s->s_mutex);
3284
3285 ceph_put_mds_session(s);
3286
3287 mutex_lock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003288 } else {
3289 /* just close it */
3290 mutex_unlock(&mdsc->mutex);
3291 mutex_lock(&s->s_mutex);
3292 mutex_lock(&mdsc->mutex);
3293 ceph_con_close(&s->s_con);
3294 mutex_unlock(&s->s_mutex);
3295 s->s_state = CEPH_MDS_SESSION_RESTARTING;
3296 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003297 } else if (oldstate == newstate) {
3298 continue; /* nothing new with this mds */
3299 }
3300
3301 /*
3302 * send reconnect?
3303 */
3304 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
Sage Weil34b6c852010-05-10 16:31:25 -07003305 newstate >= CEPH_MDS_STATE_RECONNECT) {
3306 mutex_unlock(&mdsc->mutex);
3307 send_mds_reconnect(mdsc, s);
3308 mutex_lock(&mdsc->mutex);
3309 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003310
3311 /*
Sage Weil29790f22010-03-18 14:45:05 -07003312 * kick request on any mds that has gone active.
Sage Weil2f2dc052009-10-06 11:31:09 -07003313 */
3314 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
3315 newstate >= CEPH_MDS_STATE_ACTIVE) {
Sage Weil29790f22010-03-18 14:45:05 -07003316 if (oldstate != CEPH_MDS_STATE_CREATING &&
3317 oldstate != CEPH_MDS_STATE_STARTING)
3318 pr_info("mds%d recovery completed\n", s->s_mds);
3319 kick_requests(mdsc, i);
Sage Weil2f2dc052009-10-06 11:31:09 -07003320 ceph_kick_flushing_caps(mdsc, s);
Sage Weil0dc25702009-11-20 13:43:45 -08003321 wake_up_session_caps(s, 1);
Sage Weil2f2dc052009-10-06 11:31:09 -07003322 }
3323 }
Sage Weilcb170a22010-06-21 13:38:35 -07003324
Yan, Zheng76201b62017-03-28 17:04:13 +08003325 for (i = 0; i < newmap->m_num_mds && i < mdsc->max_sessions; i++) {
Sage Weilcb170a22010-06-21 13:38:35 -07003326 s = mdsc->sessions[i];
3327 if (!s)
3328 continue;
3329 if (!ceph_mdsmap_is_laggy(newmap, i))
3330 continue;
3331 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3332 s->s_state == CEPH_MDS_SESSION_HUNG ||
3333 s->s_state == CEPH_MDS_SESSION_CLOSING) {
3334 dout(" connecting to export targets of laggy mds%d\n",
3335 i);
3336 __open_export_target_sessions(mdsc, s);
3337 }
3338 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003339}
3340
3341
3342
3343/*
3344 * leases
3345 */
3346
3347/*
3348 * caller must hold session s_mutex, dentry->d_lock
3349 */
3350void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
3351{
3352 struct ceph_dentry_info *di = ceph_dentry(dentry);
3353
3354 ceph_put_mds_session(di->lease_session);
3355 di->lease_session = NULL;
3356}
3357
Sage Weil2600d2d2010-02-22 15:12:16 -08003358static void handle_lease(struct ceph_mds_client *mdsc,
3359 struct ceph_mds_session *session,
3360 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07003361{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003362 struct super_block *sb = mdsc->fsc->sb;
Sage Weil2f2dc052009-10-06 11:31:09 -07003363 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07003364 struct dentry *parent, *dentry;
3365 struct ceph_dentry_info *di;
Sage Weil2600d2d2010-02-22 15:12:16 -08003366 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07003367 struct ceph_mds_lease *h = msg->front.iov_base;
Sage Weil1e5ea232010-06-04 10:05:40 -07003368 u32 seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003369 struct ceph_vino vino;
Sage Weil2f2dc052009-10-06 11:31:09 -07003370 struct qstr dname;
3371 int release = 0;
3372
Sage Weil2f2dc052009-10-06 11:31:09 -07003373 dout("handle_lease from mds%d\n", mds);
3374
3375 /* decode */
3376 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
3377 goto bad;
3378 vino.ino = le64_to_cpu(h->ino);
3379 vino.snap = CEPH_NOSNAP;
Sage Weil1e5ea232010-06-04 10:05:40 -07003380 seq = le32_to_cpu(h->seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003381 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
3382 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
3383 if (dname.len != get_unaligned_le32(h+1))
3384 goto bad;
3385
Sage Weil2f2dc052009-10-06 11:31:09 -07003386 /* lookup inode */
3387 inode = ceph_find_inode(sb, vino);
Sage Weil2f90b852011-07-26 11:28:25 -07003388 dout("handle_lease %s, ino %llx %p %.*s\n",
3389 ceph_lease_op_name(h->action), vino.ino, inode,
Sage Weil1e5ea232010-06-04 10:05:40 -07003390 dname.len, dname.name);
Yan, Zheng6cd3bca2014-09-17 07:45:12 +08003391
3392 mutex_lock(&session->s_mutex);
3393 session->s_seq++;
3394
Markus Elfringd37b1d92017-08-20 20:22:02 +02003395 if (!inode) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003396 dout("handle_lease no inode %llx\n", vino.ino);
3397 goto release;
3398 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003399
3400 /* dentry */
3401 parent = d_find_alias(inode);
3402 if (!parent) {
3403 dout("no parent dentry on inode %p\n", inode);
3404 WARN_ON(1);
3405 goto release; /* hrm... */
3406 }
Linus Torvalds8387ff22016-06-10 07:51:30 -07003407 dname.hash = full_name_hash(parent, dname.name, dname.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07003408 dentry = d_lookup(parent, &dname);
3409 dput(parent);
3410 if (!dentry)
3411 goto release;
3412
3413 spin_lock(&dentry->d_lock);
3414 di = ceph_dentry(dentry);
3415 switch (h->action) {
3416 case CEPH_MDS_LEASE_REVOKE:
Sage Weil3d8eb7a2011-11-11 09:48:53 -08003417 if (di->lease_session == session) {
Sage Weil1e5ea232010-06-04 10:05:40 -07003418 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
3419 h->seq = cpu_to_le32(di->lease_seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003420 __ceph_mdsc_drop_dentry_lease(dentry);
3421 }
3422 release = 1;
3423 break;
3424
3425 case CEPH_MDS_LEASE_RENEW:
Sage Weil3d8eb7a2011-11-11 09:48:53 -08003426 if (di->lease_session == session &&
Sage Weil2f2dc052009-10-06 11:31:09 -07003427 di->lease_gen == session->s_cap_gen &&
3428 di->lease_renew_from &&
3429 di->lease_renew_after == 0) {
3430 unsigned long duration =
Nicholas Mc Guire3563dbdd2015-02-06 06:52:17 -05003431 msecs_to_jiffies(le32_to_cpu(h->duration_ms));
Sage Weil2f2dc052009-10-06 11:31:09 -07003432
Sage Weil1e5ea232010-06-04 10:05:40 -07003433 di->lease_seq = seq;
Miklos Szeredi9b16f03c2016-06-22 16:35:04 +02003434 di->time = di->lease_renew_from + duration;
Sage Weil2f2dc052009-10-06 11:31:09 -07003435 di->lease_renew_after = di->lease_renew_from +
3436 (duration >> 1);
3437 di->lease_renew_from = 0;
3438 }
3439 break;
3440 }
3441 spin_unlock(&dentry->d_lock);
3442 dput(dentry);
3443
3444 if (!release)
3445 goto out;
3446
3447release:
3448 /* let's just reuse the same message */
3449 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
3450 ceph_msg_get(msg);
3451 ceph_con_send(&session->s_con, msg);
3452
3453out:
3454 iput(inode);
3455 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003456 return;
3457
3458bad:
3459 pr_err("corrupt lease message\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08003460 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003461}
3462
3463void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
3464 struct inode *inode,
3465 struct dentry *dentry, char action,
3466 u32 seq)
3467{
3468 struct ceph_msg *msg;
3469 struct ceph_mds_lease *lease;
3470 int len = sizeof(*lease) + sizeof(u32);
3471 int dnamelen = 0;
3472
3473 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
3474 inode, dentry, ceph_lease_op_name(action), session->s_mds);
3475 dnamelen = dentry->d_name.len;
3476 len += dnamelen;
3477
Sage Weilb61c2762011-08-09 15:03:46 -07003478 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07003479 if (!msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07003480 return;
3481 lease = msg->front.iov_base;
3482 lease->action = action;
Sage Weil2f2dc052009-10-06 11:31:09 -07003483 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
3484 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
3485 lease->seq = cpu_to_le32(seq);
3486 put_unaligned_le32(dnamelen, lease + 1);
3487 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
3488
3489 /*
3490 * if this is a preemptive lease RELEASE, no need to
3491 * flush request stream, since the actual request will
3492 * soon follow.
3493 */
3494 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
3495
3496 ceph_con_send(&session->s_con, msg);
3497}
3498
3499/*
Yan, Zheng7aac4532018-03-13 19:52:20 +08003500 * lock unlock sessions, to wait ongoing session activities
Sage Weil2f2dc052009-10-06 11:31:09 -07003501 */
Yan, Zheng7aac4532018-03-13 19:52:20 +08003502static void lock_unlock_sessions(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07003503{
3504 int i;
3505
Sage Weil2f2dc052009-10-06 11:31:09 -07003506 mutex_lock(&mdsc->mutex);
3507 for (i = 0; i < mdsc->max_sessions; i++) {
3508 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
3509 if (!s)
3510 continue;
3511 mutex_unlock(&mdsc->mutex);
3512 mutex_lock(&s->s_mutex);
3513 mutex_unlock(&s->s_mutex);
3514 ceph_put_mds_session(s);
3515 mutex_lock(&mdsc->mutex);
3516 }
3517 mutex_unlock(&mdsc->mutex);
3518}
3519
3520
3521
3522/*
3523 * delayed work -- periodically trim expired leases, renew caps with mds
3524 */
3525static void schedule_delayed(struct ceph_mds_client *mdsc)
3526{
3527 int delay = 5;
3528 unsigned hz = round_jiffies_relative(HZ * delay);
3529 schedule_delayed_work(&mdsc->delayed_work, hz);
3530}
3531
3532static void delayed_work(struct work_struct *work)
3533{
3534 int i;
3535 struct ceph_mds_client *mdsc =
3536 container_of(work, struct ceph_mds_client, delayed_work.work);
3537 int renew_interval;
3538 int renew_caps;
3539
3540 dout("mdsc delayed_work\n");
Sage Weilafcdaea2009-10-14 14:27:38 -07003541 ceph_check_delayed_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003542
3543 mutex_lock(&mdsc->mutex);
3544 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
3545 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
3546 mdsc->last_renew_caps);
3547 if (renew_caps)
3548 mdsc->last_renew_caps = jiffies;
3549
3550 for (i = 0; i < mdsc->max_sessions; i++) {
3551 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
Markus Elfringd37b1d92017-08-20 20:22:02 +02003552 if (!s)
Sage Weil2f2dc052009-10-06 11:31:09 -07003553 continue;
3554 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
3555 dout("resending session close request for mds%d\n",
3556 s->s_mds);
3557 request_close_session(mdsc, s);
3558 ceph_put_mds_session(s);
3559 continue;
3560 }
3561 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
3562 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
3563 s->s_state = CEPH_MDS_SESSION_HUNG;
3564 pr_info("mds%d hung\n", s->s_mds);
3565 }
3566 }
3567 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
3568 /* this mds is failed or recovering, just wait */
3569 ceph_put_mds_session(s);
3570 continue;
3571 }
3572 mutex_unlock(&mdsc->mutex);
3573
3574 mutex_lock(&s->s_mutex);
3575 if (renew_caps)
3576 send_renew_caps(mdsc, s);
3577 else
3578 ceph_con_keepalive(&s->s_con);
Sage Weilaab53dd2010-03-17 16:30:21 -07003579 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3580 s->s_state == CEPH_MDS_SESSION_HUNG)
Sage Weil3d7ded42010-06-09 16:47:10 -07003581 ceph_send_cap_releases(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07003582 mutex_unlock(&s->s_mutex);
3583 ceph_put_mds_session(s);
3584
3585 mutex_lock(&mdsc->mutex);
3586 }
3587 mutex_unlock(&mdsc->mutex);
3588
3589 schedule_delayed(mdsc);
3590}
3591
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003592int ceph_mdsc_init(struct ceph_fs_client *fsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07003593
Sage Weil2f2dc052009-10-06 11:31:09 -07003594{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003595 struct ceph_mds_client *mdsc;
3596
3597 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
3598 if (!mdsc)
3599 return -ENOMEM;
3600 mdsc->fsc = fsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07003601 mutex_init(&mdsc->mutex);
3602 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
Markus Elfringd37b1d92017-08-20 20:22:02 +02003603 if (!mdsc->mdsmap) {
majianpengfb3101b2013-06-25 14:48:19 +08003604 kfree(mdsc);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08003605 return -ENOMEM;
majianpengfb3101b2013-06-25 14:48:19 +08003606 }
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08003607
Chengguang Xu50c55ae2018-03-14 13:47:33 +08003608 fsc->mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07003609 init_completion(&mdsc->safe_umount_waiters);
Sage Weilf3c60c52010-08-11 14:51:23 -07003610 init_waitqueue_head(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003611 INIT_LIST_HEAD(&mdsc->waiting_for_map);
3612 mdsc->sessions = NULL;
Yan, Zheng86d8f672015-01-09 17:00:42 +08003613 atomic_set(&mdsc->num_sessions, 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07003614 mdsc->max_sessions = 0;
3615 mdsc->stopping = 0;
Luis Henriquesd557c482018-01-12 17:19:29 +00003616 atomic64_set(&mdsc->quotarealms_count, 0);
Yan, Zhengaffbc192015-05-05 21:22:13 +08003617 mdsc->last_snap_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003618 init_rwsem(&mdsc->snap_rwsem);
Sage Weila105f002010-02-15 14:37:55 -08003619 mdsc->snap_realms = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07003620 INIT_LIST_HEAD(&mdsc->snap_empty);
3621 spin_lock_init(&mdsc->snap_empty_lock);
3622 mdsc->last_tid = 0;
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08003623 mdsc->oldest_tid = 0;
Sage Weil44ca18f2010-02-15 12:08:46 -08003624 mdsc->request_tree = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07003625 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
3626 mdsc->last_renew_caps = jiffies;
3627 INIT_LIST_HEAD(&mdsc->cap_delay_list);
3628 spin_lock_init(&mdsc->cap_delay_lock);
3629 INIT_LIST_HEAD(&mdsc->snap_flush_list);
3630 spin_lock_init(&mdsc->snap_flush_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08003631 mdsc->last_cap_flush_tid = 1;
Yan, Zhenge4500b52016-07-06 11:12:56 +08003632 INIT_LIST_HEAD(&mdsc->cap_flush_list);
Sage Weil2f2dc052009-10-06 11:31:09 -07003633 INIT_LIST_HEAD(&mdsc->cap_dirty);
Sage Weildb354052011-05-24 11:46:31 -07003634 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
Sage Weil2f2dc052009-10-06 11:31:09 -07003635 mdsc->num_cap_flushing = 0;
3636 spin_lock_init(&mdsc->cap_dirty_lock);
3637 init_waitqueue_head(&mdsc->cap_flushing_wq);
3638 spin_lock_init(&mdsc->dentry_lru_lock);
3639 INIT_LIST_HEAD(&mdsc->dentry_lru);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08003640
Yehuda Sadeh37151662010-06-17 16:16:12 -07003641 ceph_caps_init(mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003642 ceph_adjust_min_caps(mdsc, fsc->min_caps);
Yehuda Sadeh37151662010-06-17 16:16:12 -07003643
Yan, Zheng10183a62015-04-27 15:33:28 +08003644 init_rwsem(&mdsc->pool_perm_rwsem);
3645 mdsc->pool_perm_tree = RB_ROOT;
3646
Yan, Zheng717e6f22017-09-11 12:10:08 +08003647 strncpy(mdsc->nodename, utsname()->nodename,
3648 sizeof(mdsc->nodename) - 1);
Sage Weil5f44f142009-11-18 14:52:18 -08003649 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003650}
3651
3652/*
3653 * Wait for safe replies on open mds requests. If we time out, drop
3654 * all requests from the tree to avoid dangling dentry refs.
3655 */
3656static void wait_requests(struct ceph_mds_client *mdsc)
3657{
Ilya Dryomova319bf52015-05-15 12:02:17 +03003658 struct ceph_options *opts = mdsc->fsc->client->options;
Sage Weil2f2dc052009-10-06 11:31:09 -07003659 struct ceph_mds_request *req;
Sage Weil2f2dc052009-10-06 11:31:09 -07003660
3661 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003662 if (__get_oldest_req(mdsc)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003663 mutex_unlock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003664
Sage Weil2f2dc052009-10-06 11:31:09 -07003665 dout("wait_requests waiting for requests\n");
3666 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
Ilya Dryomova319bf52015-05-15 12:02:17 +03003667 ceph_timeout_jiffies(opts->mount_timeout));
Sage Weil2f2dc052009-10-06 11:31:09 -07003668
3669 /* tear down remaining requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08003670 mutex_lock(&mdsc->mutex);
3671 while ((req = __get_oldest_req(mdsc))) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003672 dout("wait_requests timed out on tid %llu\n",
3673 req->r_tid);
Sage Weil44ca18f2010-02-15 12:08:46 -08003674 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07003675 }
3676 }
3677 mutex_unlock(&mdsc->mutex);
3678 dout("wait_requests done\n");
3679}
3680
3681/*
3682 * called before mount is ro, and before dentries are torn down.
3683 * (hmm, does this still race with new lookups?)
3684 */
3685void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
3686{
3687 dout("pre_umount\n");
3688 mdsc->stopping = 1;
3689
Yan, Zheng7aac4532018-03-13 19:52:20 +08003690 lock_unlock_sessions(mdsc);
Sage Weilafcdaea2009-10-14 14:27:38 -07003691 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003692 wait_requests(mdsc);
Sage Weil17c688c2010-06-21 16:12:26 -07003693
3694 /*
3695 * wait for reply handlers to drop their request refs and
3696 * their inode/dcache refs
3697 */
3698 ceph_msgr_flush();
Sage Weil2f2dc052009-10-06 11:31:09 -07003699}
3700
3701/*
3702 * wait for all write mds requests to flush.
3703 */
3704static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
3705{
Sage Weil80fc7312010-03-16 15:28:54 -07003706 struct ceph_mds_request *req = NULL, *nextreq;
Sage Weil44ca18f2010-02-15 12:08:46 -08003707 struct rb_node *n;
Sage Weil2f2dc052009-10-06 11:31:09 -07003708
3709 mutex_lock(&mdsc->mutex);
3710 dout("wait_unsafe_requests want %lld\n", want_tid);
Sage Weil80fc7312010-03-16 15:28:54 -07003711restart:
Sage Weil44ca18f2010-02-15 12:08:46 -08003712 req = __get_oldest_req(mdsc);
3713 while (req && req->r_tid <= want_tid) {
Sage Weil80fc7312010-03-16 15:28:54 -07003714 /* find next request */
3715 n = rb_next(&req->r_node);
3716 if (n)
3717 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
3718 else
3719 nextreq = NULL;
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08003720 if (req->r_op != CEPH_MDS_OP_SETFILELOCK &&
3721 (req->r_op & CEPH_MDS_OP_WRITE)) {
Sage Weil44ca18f2010-02-15 12:08:46 -08003722 /* write op */
3723 ceph_mdsc_get_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07003724 if (nextreq)
3725 ceph_mdsc_get_request(nextreq);
Sage Weil44ca18f2010-02-15 12:08:46 -08003726 mutex_unlock(&mdsc->mutex);
3727 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3728 req->r_tid, want_tid);
3729 wait_for_completion(&req->r_safe_completion);
3730 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003731 ceph_mdsc_put_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07003732 if (!nextreq)
3733 break; /* next dne before, so we're done! */
3734 if (RB_EMPTY_NODE(&nextreq->r_node)) {
3735 /* next request was removed from tree */
3736 ceph_mdsc_put_request(nextreq);
3737 goto restart;
3738 }
3739 ceph_mdsc_put_request(nextreq); /* won't go away */
Sage Weil44ca18f2010-02-15 12:08:46 -08003740 }
Sage Weil80fc7312010-03-16 15:28:54 -07003741 req = nextreq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003742 }
3743 mutex_unlock(&mdsc->mutex);
3744 dout("wait_unsafe_requests done\n");
3745}
3746
3747void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
3748{
Yan, Zheng0e294382016-07-04 18:06:41 +08003749 u64 want_tid, want_flush;
Sage Weil2f2dc052009-10-06 11:31:09 -07003750
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01003751 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
Sage Weil56b7cf92010-05-03 15:22:00 -07003752 return;
3753
Sage Weil2f2dc052009-10-06 11:31:09 -07003754 dout("sync\n");
3755 mutex_lock(&mdsc->mutex);
3756 want_tid = mdsc->last_tid;
Sage Weil2f2dc052009-10-06 11:31:09 -07003757 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003758
Sage Weilafcdaea2009-10-14 14:27:38 -07003759 ceph_flush_dirty_caps(mdsc);
Yan, Zhengd3383a82015-01-08 21:30:12 +08003760 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zheng8310b082015-06-09 17:20:12 +08003761 want_flush = mdsc->last_cap_flush_tid;
Yan, Zhengc8799fc2016-07-07 15:22:38 +08003762 if (!list_empty(&mdsc->cap_flush_list)) {
3763 struct ceph_cap_flush *cf =
3764 list_last_entry(&mdsc->cap_flush_list,
3765 struct ceph_cap_flush, g_list);
3766 cf->wake = true;
3767 }
Yan, Zhengd3383a82015-01-08 21:30:12 +08003768 spin_unlock(&mdsc->cap_dirty_lock);
3769
Yan, Zheng0e294382016-07-04 18:06:41 +08003770 dout("sync want tid %lld flush_seq %lld\n",
3771 want_tid, want_flush);
Sage Weil2f2dc052009-10-06 11:31:09 -07003772
3773 wait_unsafe_requests(mdsc, want_tid);
Yan, Zheng0e294382016-07-04 18:06:41 +08003774 wait_caps_flush(mdsc, want_flush);
Sage Weil2f2dc052009-10-06 11:31:09 -07003775}
3776
Sage Weilf3c60c52010-08-11 14:51:23 -07003777/*
3778 * true if all sessions are closed, or we force unmount
3779 */
Yan, Zhengfcff4152016-09-14 16:39:51 +08003780static bool done_closing_sessions(struct ceph_mds_client *mdsc, int skipped)
Sage Weilf3c60c52010-08-11 14:51:23 -07003781{
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01003782 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
Sage Weilf3c60c52010-08-11 14:51:23 -07003783 return true;
Yan, Zhengfcff4152016-09-14 16:39:51 +08003784 return atomic_read(&mdsc->num_sessions) <= skipped;
Sage Weilf3c60c52010-08-11 14:51:23 -07003785}
Sage Weil2f2dc052009-10-06 11:31:09 -07003786
3787/*
3788 * called after sb is ro.
3789 */
3790void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
3791{
Ilya Dryomova319bf52015-05-15 12:02:17 +03003792 struct ceph_options *opts = mdsc->fsc->client->options;
Sage Weil2f2dc052009-10-06 11:31:09 -07003793 struct ceph_mds_session *session;
3794 int i;
Yan, Zhengfcff4152016-09-14 16:39:51 +08003795 int skipped = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003796
3797 dout("close_sessions\n");
3798
Sage Weil2f2dc052009-10-06 11:31:09 -07003799 /* close sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07003800 mutex_lock(&mdsc->mutex);
3801 for (i = 0; i < mdsc->max_sessions; i++) {
3802 session = __ceph_lookup_mds_session(mdsc, i);
3803 if (!session)
3804 continue;
Sage Weil2f2dc052009-10-06 11:31:09 -07003805 mutex_unlock(&mdsc->mutex);
Sage Weilf3c60c52010-08-11 14:51:23 -07003806 mutex_lock(&session->s_mutex);
Yan, Zhengfcff4152016-09-14 16:39:51 +08003807 if (__close_session(mdsc, session) <= 0)
3808 skipped++;
Sage Weilf3c60c52010-08-11 14:51:23 -07003809 mutex_unlock(&session->s_mutex);
3810 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003811 mutex_lock(&mdsc->mutex);
3812 }
Sage Weilf3c60c52010-08-11 14:51:23 -07003813 mutex_unlock(&mdsc->mutex);
3814
3815 dout("waiting for sessions to close\n");
Yan, Zhengfcff4152016-09-14 16:39:51 +08003816 wait_event_timeout(mdsc->session_close_wq,
3817 done_closing_sessions(mdsc, skipped),
Ilya Dryomova319bf52015-05-15 12:02:17 +03003818 ceph_timeout_jiffies(opts->mount_timeout));
Sage Weil2f2dc052009-10-06 11:31:09 -07003819
3820 /* tear down remaining sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07003821 mutex_lock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003822 for (i = 0; i < mdsc->max_sessions; i++) {
3823 if (mdsc->sessions[i]) {
3824 session = get_session(mdsc->sessions[i]);
Sage Weil2600d2d2010-02-22 15:12:16 -08003825 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003826 mutex_unlock(&mdsc->mutex);
3827 mutex_lock(&session->s_mutex);
3828 remove_session_caps(session);
3829 mutex_unlock(&session->s_mutex);
3830 ceph_put_mds_session(session);
3831 mutex_lock(&mdsc->mutex);
3832 }
3833 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003834 WARN_ON(!list_empty(&mdsc->cap_delay_list));
Sage Weil2f2dc052009-10-06 11:31:09 -07003835 mutex_unlock(&mdsc->mutex);
3836
3837 ceph_cleanup_empty_realms(mdsc);
3838
3839 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3840
3841 dout("stopped\n");
3842}
3843
Yan, Zheng48fec5d2015-07-01 16:27:46 +08003844void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc)
3845{
3846 struct ceph_mds_session *session;
3847 int mds;
3848
3849 dout("force umount\n");
3850
3851 mutex_lock(&mdsc->mutex);
3852 for (mds = 0; mds < mdsc->max_sessions; mds++) {
3853 session = __ceph_lookup_mds_session(mdsc, mds);
3854 if (!session)
3855 continue;
3856 mutex_unlock(&mdsc->mutex);
3857 mutex_lock(&session->s_mutex);
3858 __close_session(mdsc, session);
3859 if (session->s_state == CEPH_MDS_SESSION_CLOSING) {
3860 cleanup_session_requests(mdsc, session);
3861 remove_session_caps(session);
3862 }
3863 mutex_unlock(&session->s_mutex);
3864 ceph_put_mds_session(session);
3865 mutex_lock(&mdsc->mutex);
3866 kick_requests(mdsc, mds);
3867 }
3868 __wake_requests(mdsc, &mdsc->waiting_for_map);
3869 mutex_unlock(&mdsc->mutex);
3870}
3871
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003872static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07003873{
3874 dout("stop\n");
3875 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3876 if (mdsc->mdsmap)
3877 ceph_mdsmap_destroy(mdsc->mdsmap);
3878 kfree(mdsc->sessions);
Yehuda Sadeh37151662010-06-17 16:16:12 -07003879 ceph_caps_finalize(mdsc);
Yan, Zheng10183a62015-04-27 15:33:28 +08003880 ceph_pool_perm_destroy(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003881}
3882
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003883void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
3884{
3885 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weilef550f62011-03-25 13:27:48 -07003886 dout("mdsc_destroy %p\n", mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07003887
Chengguang Xu50c55ae2018-03-14 13:47:33 +08003888 if (!mdsc)
3889 return;
3890
Sage Weilef550f62011-03-25 13:27:48 -07003891 /* flush out any connection work with references to us */
3892 ceph_msgr_flush();
3893
Yan, Zheng62a65f32017-06-22 16:26:34 +08003894 ceph_mdsc_stop(mdsc);
3895
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003896 fsc->mdsc = NULL;
3897 kfree(mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07003898 dout("mdsc_destroy %p done\n", mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003899}
3900
Yan, Zheng430afba2016-07-08 11:25:38 +08003901void ceph_mdsc_handle_fsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3902{
3903 struct ceph_fs_client *fsc = mdsc->fsc;
3904 const char *mds_namespace = fsc->mount_options->mds_namespace;
3905 void *p = msg->front.iov_base;
3906 void *end = p + msg->front.iov_len;
3907 u32 epoch;
3908 u32 map_len;
3909 u32 num_fs;
3910 u32 mount_fscid = (u32)-1;
3911 u8 struct_v, struct_cv;
3912 int err = -EINVAL;
3913
3914 ceph_decode_need(&p, end, sizeof(u32), bad);
3915 epoch = ceph_decode_32(&p);
3916
3917 dout("handle_fsmap epoch %u\n", epoch);
3918
3919 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
3920 struct_v = ceph_decode_8(&p);
3921 struct_cv = ceph_decode_8(&p);
3922 map_len = ceph_decode_32(&p);
3923
3924 ceph_decode_need(&p, end, sizeof(u32) * 3, bad);
3925 p += sizeof(u32) * 2; /* skip epoch and legacy_client_fscid */
3926
3927 num_fs = ceph_decode_32(&p);
3928 while (num_fs-- > 0) {
3929 void *info_p, *info_end;
3930 u32 info_len;
3931 u8 info_v, info_cv;
3932 u32 fscid, namelen;
3933
3934 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
3935 info_v = ceph_decode_8(&p);
3936 info_cv = ceph_decode_8(&p);
3937 info_len = ceph_decode_32(&p);
3938 ceph_decode_need(&p, end, info_len, bad);
3939 info_p = p;
3940 info_end = p + info_len;
3941 p = info_end;
3942
3943 ceph_decode_need(&info_p, info_end, sizeof(u32) * 2, bad);
3944 fscid = ceph_decode_32(&info_p);
3945 namelen = ceph_decode_32(&info_p);
3946 ceph_decode_need(&info_p, info_end, namelen, bad);
3947
3948 if (mds_namespace &&
3949 strlen(mds_namespace) == namelen &&
3950 !strncmp(mds_namespace, (char *)info_p, namelen)) {
3951 mount_fscid = fscid;
3952 break;
3953 }
3954 }
3955
3956 ceph_monc_got_map(&fsc->client->monc, CEPH_SUB_FSMAP, epoch);
3957 if (mount_fscid != (u32)-1) {
3958 fsc->client->monc.fs_cluster_id = mount_fscid;
3959 ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP,
3960 0, true);
3961 ceph_monc_renew_subs(&fsc->client->monc);
3962 } else {
3963 err = -ENOENT;
3964 goto err_out;
3965 }
3966 return;
Ilya Dryomov76bd6ec2017-10-16 10:32:50 +02003967
Yan, Zheng430afba2016-07-08 11:25:38 +08003968bad:
3969 pr_err("error decoding fsmap\n");
3970err_out:
3971 mutex_lock(&mdsc->mutex);
Ilya Dryomov76bd6ec2017-10-16 10:32:50 +02003972 mdsc->mdsmap_err = err;
Yan, Zheng430afba2016-07-08 11:25:38 +08003973 __wake_requests(mdsc, &mdsc->waiting_for_map);
3974 mutex_unlock(&mdsc->mutex);
Yan, Zheng430afba2016-07-08 11:25:38 +08003975}
Sage Weil2f2dc052009-10-06 11:31:09 -07003976
3977/*
3978 * handle mds map update.
3979 */
Yan, Zheng430afba2016-07-08 11:25:38 +08003980void ceph_mdsc_handle_mdsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07003981{
3982 u32 epoch;
3983 u32 maplen;
3984 void *p = msg->front.iov_base;
3985 void *end = p + msg->front.iov_len;
3986 struct ceph_mdsmap *newmap, *oldmap;
3987 struct ceph_fsid fsid;
3988 int err = -EINVAL;
3989
3990 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
3991 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003992 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
Sage Weil07433042009-11-18 16:50:41 -08003993 return;
Sage Weilc89136e2009-10-14 09:59:09 -07003994 epoch = ceph_decode_32(&p);
3995 maplen = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07003996 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
3997
3998 /* do we need it? */
Sage Weil2f2dc052009-10-06 11:31:09 -07003999 mutex_lock(&mdsc->mutex);
4000 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
4001 dout("handle_map epoch %u <= our %u\n",
4002 epoch, mdsc->mdsmap->m_epoch);
4003 mutex_unlock(&mdsc->mutex);
4004 return;
4005 }
4006
4007 newmap = ceph_mdsmap_decode(&p, end);
4008 if (IS_ERR(newmap)) {
4009 err = PTR_ERR(newmap);
4010 goto bad_unlock;
4011 }
4012
4013 /* swap into place */
4014 if (mdsc->mdsmap) {
4015 oldmap = mdsc->mdsmap;
4016 mdsc->mdsmap = newmap;
4017 check_new_map(mdsc, newmap, oldmap);
4018 ceph_mdsmap_destroy(oldmap);
4019 } else {
4020 mdsc->mdsmap = newmap; /* first mds map */
4021 }
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004022 mdsc->fsc->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
Sage Weil2f2dc052009-10-06 11:31:09 -07004023
4024 __wake_requests(mdsc, &mdsc->waiting_for_map);
Ilya Dryomov82dcaba2016-01-19 16:19:06 +01004025 ceph_monc_got_map(&mdsc->fsc->client->monc, CEPH_SUB_MDSMAP,
4026 mdsc->mdsmap->m_epoch);
Sage Weil2f2dc052009-10-06 11:31:09 -07004027
4028 mutex_unlock(&mdsc->mutex);
4029 schedule_delayed(mdsc);
4030 return;
4031
4032bad_unlock:
4033 mutex_unlock(&mdsc->mutex);
4034bad:
4035 pr_err("error decoding mdsmap %d\n", err);
4036 return;
4037}
4038
4039static struct ceph_connection *con_get(struct ceph_connection *con)
4040{
4041 struct ceph_mds_session *s = con->private;
4042
4043 if (get_session(s)) {
Elena Reshetova3997c012017-03-03 11:15:06 +02004044 dout("mdsc con_get %p ok (%d)\n", s, refcount_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -07004045 return con;
4046 }
4047 dout("mdsc con_get %p FAIL\n", s);
4048 return NULL;
4049}
4050
4051static void con_put(struct ceph_connection *con)
4052{
4053 struct ceph_mds_session *s = con->private;
4054
Elena Reshetova3997c012017-03-03 11:15:06 +02004055 dout("mdsc con_put %p (%d)\n", s, refcount_read(&s->s_ref) - 1);
Sage Weil2f2dc052009-10-06 11:31:09 -07004056 ceph_put_mds_session(s);
4057}
4058
4059/*
4060 * if the client is unresponsive for long enough, the mds will kill
4061 * the session entirely.
4062 */
4063static void peer_reset(struct ceph_connection *con)
4064{
4065 struct ceph_mds_session *s = con->private;
Sage Weil7e70f0e2010-03-18 13:59:12 -07004066 struct ceph_mds_client *mdsc = s->s_mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07004067
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07004068 pr_warn("mds%d closed our session\n", s->s_mds);
Sage Weil7e70f0e2010-03-18 13:59:12 -07004069 send_mds_reconnect(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07004070}
4071
4072static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
4073{
4074 struct ceph_mds_session *s = con->private;
4075 struct ceph_mds_client *mdsc = s->s_mdsc;
4076 int type = le16_to_cpu(msg->hdr.type);
4077
Sage Weil2600d2d2010-02-22 15:12:16 -08004078 mutex_lock(&mdsc->mutex);
4079 if (__verify_registered_session(mdsc, s) < 0) {
4080 mutex_unlock(&mdsc->mutex);
4081 goto out;
4082 }
4083 mutex_unlock(&mdsc->mutex);
4084
Sage Weil2f2dc052009-10-06 11:31:09 -07004085 switch (type) {
4086 case CEPH_MSG_MDS_MAP:
Yan, Zheng430afba2016-07-08 11:25:38 +08004087 ceph_mdsc_handle_mdsmap(mdsc, msg);
4088 break;
4089 case CEPH_MSG_FS_MAP_USER:
4090 ceph_mdsc_handle_fsmap(mdsc, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004091 break;
4092 case CEPH_MSG_CLIENT_SESSION:
4093 handle_session(s, msg);
4094 break;
4095 case CEPH_MSG_CLIENT_REPLY:
4096 handle_reply(s, msg);
4097 break;
4098 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
Sage Weil2600d2d2010-02-22 15:12:16 -08004099 handle_forward(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004100 break;
4101 case CEPH_MSG_CLIENT_CAPS:
4102 ceph_handle_caps(s, msg);
4103 break;
4104 case CEPH_MSG_CLIENT_SNAP:
Sage Weil2600d2d2010-02-22 15:12:16 -08004105 ceph_handle_snap(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004106 break;
4107 case CEPH_MSG_CLIENT_LEASE:
Sage Weil2600d2d2010-02-22 15:12:16 -08004108 handle_lease(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004109 break;
Luis Henriquesfb18a572018-01-05 10:47:18 +00004110 case CEPH_MSG_CLIENT_QUOTA:
4111 ceph_handle_quota(mdsc, s, msg);
4112 break;
Sage Weil2f2dc052009-10-06 11:31:09 -07004113
4114 default:
4115 pr_err("received unknown message type %d %s\n", type,
4116 ceph_msg_type_name(type));
4117 }
Sage Weil2600d2d2010-02-22 15:12:16 -08004118out:
Sage Weil2f2dc052009-10-06 11:31:09 -07004119 ceph_msg_put(msg);
4120}
4121
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004122/*
4123 * authentication
4124 */
Alex Eldera3530df2012-05-16 15:16:39 -05004125
4126/*
4127 * Note: returned pointer is the address of a structure that's
4128 * managed separately. Caller must *not* attempt to free it.
4129 */
4130static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05004131 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004132{
4133 struct ceph_mds_session *s = con->private;
4134 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004135 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05004136 struct ceph_auth_handshake *auth = &s->s_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004137
Alex Elder74f18692012-05-16 15:16:39 -05004138 if (force_new && auth->authorizer) {
Ilya Dryomov6c1ea262016-04-11 19:34:49 +02004139 ceph_auth_destroy_authorizer(auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05004140 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004141 }
Sage Weil27859f92013-03-25 10:26:14 -07004142 if (!auth->authorizer) {
4143 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4144 auth);
Sage Weil0bed9b52013-03-25 10:26:01 -07004145 if (ret)
4146 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07004147 } else {
4148 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4149 auth);
Alex Eldera2556512012-05-16 15:16:39 -05004150 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05004151 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004152 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004153 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05004154
Alex Eldera3530df2012-05-16 15:16:39 -05004155 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004156}
4157
4158
Ilya Dryomov0dde5842016-12-02 16:35:09 +01004159static int verify_authorizer_reply(struct ceph_connection *con)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004160{
4161 struct ceph_mds_session *s = con->private;
4162 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004163 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004164
Ilya Dryomov0dde5842016-12-02 16:35:09 +01004165 return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004166}
4167
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004168static int invalidate_authorizer(struct ceph_connection *con)
4169{
4170 struct ceph_mds_session *s = con->private;
4171 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004172 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004173
Sage Weil27859f92013-03-25 10:26:14 -07004174 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004175
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004176 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004177}
4178
Alex Elder53ded492013-03-01 18:00:14 -06004179static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
4180 struct ceph_msg_header *hdr, int *skip)
4181{
4182 struct ceph_msg *msg;
4183 int type = (int) le16_to_cpu(hdr->type);
4184 int front_len = (int) le32_to_cpu(hdr->front_len);
4185
4186 if (con->in_msg)
4187 return con->in_msg;
4188
4189 *skip = 0;
4190 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
4191 if (!msg) {
4192 pr_err("unable to allocate msg type %d len %d\n",
4193 type, front_len);
4194 return NULL;
4195 }
Alex Elder53ded492013-03-01 18:00:14 -06004196
4197 return msg;
4198}
4199
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004200static int mds_sign_message(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08004201{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004202 struct ceph_mds_session *s = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08004203 struct ceph_auth_handshake *auth = &s->s_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004204
Yan, Zheng33d07332014-11-04 16:33:37 +08004205 return ceph_auth_sign_message(auth, msg);
4206}
4207
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004208static int mds_check_message_signature(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08004209{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004210 struct ceph_mds_session *s = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08004211 struct ceph_auth_handshake *auth = &s->s_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004212
Yan, Zheng33d07332014-11-04 16:33:37 +08004213 return ceph_auth_check_message_signature(auth, msg);
4214}
4215
Tobias Klauser9e327892010-05-20 10:40:19 +02004216static const struct ceph_connection_operations mds_con_ops = {
Sage Weil2f2dc052009-10-06 11:31:09 -07004217 .get = con_get,
4218 .put = con_put,
4219 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004220 .get_authorizer = get_authorizer,
4221 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004222 .invalidate_authorizer = invalidate_authorizer,
Sage Weil2f2dc052009-10-06 11:31:09 -07004223 .peer_reset = peer_reset,
Alex Elder53ded492013-03-01 18:00:14 -06004224 .alloc_msg = mds_alloc_msg,
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004225 .sign_message = mds_sign_message,
4226 .check_message_signature = mds_check_message_signature,
Sage Weil2f2dc052009-10-06 11:31:09 -07004227};
4228
Sage Weil2f2dc052009-10-06 11:31:09 -07004229/* eof */