blob: f2f57775d2d586c0d5783e5c0c821446bdfa87f6 [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
Yan, Zheng81c5a142019-01-01 16:28:33 +080023#define RECONNECT_MAX_SIZE (INT_MAX - PAGE_SIZE)
24
Sage Weil2f2dc052009-10-06 11:31:09 -070025/*
26 * A cluster of MDS (metadata server) daemons is responsible for
27 * managing the file system namespace (the directory hierarchy and
28 * inodes) and for coordinating shared access to storage. Metadata is
29 * partitioning hierarchically across a number of servers, and that
30 * partition varies over time as the cluster adjusts the distribution
31 * in order to balance load.
32 *
33 * The MDS client is primarily responsible to managing synchronous
34 * metadata requests for operations like open, unlink, and so forth.
35 * If there is a MDS failure, we find out about it when we (possibly
36 * request and) receive a new MDS map, and can resubmit affected
37 * requests.
38 *
39 * For the most part, though, we take advantage of a lossless
40 * communications channel to the MDS, and do not need to worry about
41 * timing out or resubmitting requests.
42 *
43 * We maintain a stateful "session" with each MDS we interact with.
44 * Within each session, we sent periodic heartbeat messages to ensure
45 * any capabilities or leases we have been issues remain valid. If
46 * the session times out and goes stale, our leases and capabilities
47 * are no longer valid.
48 */
49
Sage Weil20cb34a2010-05-12 15:21:32 -070050struct ceph_reconnect_state {
Yan, Zheng81c5a142019-01-01 16:28:33 +080051 struct ceph_mds_session *session;
52 int nr_caps, nr_realms;
Sage Weil20cb34a2010-05-12 15:21:32 -070053 struct ceph_pagelist *pagelist;
Yan, Zheng121f22a2016-07-04 22:05:18 +080054 unsigned msg_version;
Yan, Zheng81c5a142019-01-01 16:28:33 +080055 bool allow_multi;
Sage Weil20cb34a2010-05-12 15:21:32 -070056};
57
Sage Weil2f2dc052009-10-06 11:31:09 -070058static void __wake_requests(struct ceph_mds_client *mdsc,
59 struct list_head *head);
60
Tobias Klauser9e327892010-05-20 10:40:19 +020061static const struct ceph_connection_operations mds_con_ops;
Sage Weil2f2dc052009-10-06 11:31:09 -070062
63
64/*
65 * mds reply parsing
66 */
67
68/*
69 * parse individual inode info
70 */
71static int parse_reply_info_in(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -080072 struct ceph_mds_reply_info_in *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +020073 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -070074{
75 int err = -EIO;
76
77 info->in = *p;
78 *p += sizeof(struct ceph_mds_reply_inode) +
79 sizeof(*info->in->fragtree.splits) *
80 le32_to_cpu(info->in->fragtree.nsplits);
81
82 ceph_decode_32_safe(p, end, info->symlink_len, bad);
83 ceph_decode_need(p, end, info->symlink_len, bad);
84 info->symlink = *p;
85 *p += info->symlink_len;
86
Ilya Dryomov23c625c2018-11-08 14:55:21 +010087 ceph_decode_copy_safe(p, end, &info->dir_layout,
88 sizeof(info->dir_layout), bad);
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, Zheng68cd5b42015-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
Yan, Zheng342ce182018-05-11 18:47:29 +0800905static void encode_supported_features(void **p, void *end)
906{
907 static const unsigned char bits[] = CEPHFS_FEATURES_CLIENT_SUPPORTED;
908 static const size_t count = ARRAY_SIZE(bits);
909
910 if (count > 0) {
911 size_t i;
912 size_t size = ((size_t)bits[count - 1] + 64) / 64 * 8;
913
914 BUG_ON(*p + 4 + size > end);
915 ceph_encode_32(p, size);
916 memset(*p, 0, size);
917 for (i = 0; i < count; i++)
918 ((unsigned char*)(*p))[i / 8] |= 1 << (bits[i] % 8);
919 *p += size;
920 } else {
921 BUG_ON(*p + 4 > end);
922 ceph_encode_32(p, 0);
923 }
924}
925
John Spraydbd0c8b2014-09-09 19:26:01 +0100926/*
927 * session message, specialization for CEPH_SESSION_REQUEST_OPEN
928 * to include additional client metadata fields.
929 */
930static struct ceph_msg *create_session_open_msg(struct ceph_mds_client *mdsc, u64 seq)
931{
932 struct ceph_msg *msg;
933 struct ceph_mds_session_head *h;
934 int i = -1;
Yan, Zheng342ce182018-05-11 18:47:29 +0800935 int extra_bytes = 0;
John Spraydbd0c8b2014-09-09 19:26:01 +0100936 int metadata_key_count = 0;
937 struct ceph_options *opt = mdsc->fsc->client->options;
Yan, Zheng3f384952016-04-21 11:09:55 +0800938 struct ceph_mount_options *fsopt = mdsc->fsc->mount_options;
Yan, Zheng342ce182018-05-11 18:47:29 +0800939 void *p, *end;
John Spraydbd0c8b2014-09-09 19:26:01 +0100940
Yan, Zhenga6a5ce42015-01-16 10:54:43 +0800941 const char* metadata[][2] = {
Yan, Zheng717e6f22017-09-11 12:10:08 +0800942 {"hostname", mdsc->nodename},
943 {"kernel_version", init_utsname()->release},
Yan, Zheng3f384952016-04-21 11:09:55 +0800944 {"entity_id", opt->name ? : ""},
945 {"root", fsopt->server_path ? : "/"},
John Spraydbd0c8b2014-09-09 19:26:01 +0100946 {NULL, NULL}
947 };
948
949 /* Calculate serialized length of metadata */
Yan, Zheng342ce182018-05-11 18:47:29 +0800950 extra_bytes = 4; /* map length */
Markus Elfringd37b1d92017-08-20 20:22:02 +0200951 for (i = 0; metadata[i][0]; ++i) {
Yan, Zheng342ce182018-05-11 18:47:29 +0800952 extra_bytes += 8 + strlen(metadata[i][0]) +
John Spraydbd0c8b2014-09-09 19:26:01 +0100953 strlen(metadata[i][1]);
954 metadata_key_count++;
955 }
Yan, Zheng342ce182018-05-11 18:47:29 +0800956 /* supported feature */
957 extra_bytes += 4 + 8;
John Spraydbd0c8b2014-09-09 19:26:01 +0100958
959 /* Allocate the message */
Yan, Zheng342ce182018-05-11 18:47:29 +0800960 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + extra_bytes,
John Spraydbd0c8b2014-09-09 19:26:01 +0100961 GFP_NOFS, false);
962 if (!msg) {
963 pr_err("create_session_msg ENOMEM creating msg\n");
964 return NULL;
965 }
Yan, Zheng342ce182018-05-11 18:47:29 +0800966 p = msg->front.iov_base;
967 end = p + msg->front.iov_len;
968
969 h = p;
John Spraydbd0c8b2014-09-09 19:26:01 +0100970 h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN);
971 h->seq = cpu_to_le64(seq);
972
973 /*
974 * Serialize client metadata into waiting buffer space, using
975 * the format that userspace expects for map<string, string>
John Spray7cfa0312014-10-30 17:15:26 +0000976 *
977 * ClientSession messages with metadata are v2
John Spraydbd0c8b2014-09-09 19:26:01 +0100978 */
Yan, Zheng342ce182018-05-11 18:47:29 +0800979 msg->hdr.version = cpu_to_le16(3);
John Spray7cfa0312014-10-30 17:15:26 +0000980 msg->hdr.compat_version = cpu_to_le16(1);
John Spraydbd0c8b2014-09-09 19:26:01 +0100981
982 /* The write pointer, following the session_head structure */
Yan, Zheng342ce182018-05-11 18:47:29 +0800983 p += sizeof(*h);
John Spraydbd0c8b2014-09-09 19:26:01 +0100984
985 /* Number of entries in the map */
986 ceph_encode_32(&p, metadata_key_count);
987
988 /* Two length-prefixed strings for each entry in the map */
Markus Elfringd37b1d92017-08-20 20:22:02 +0200989 for (i = 0; metadata[i][0]; ++i) {
John Spraydbd0c8b2014-09-09 19:26:01 +0100990 size_t const key_len = strlen(metadata[i][0]);
991 size_t const val_len = strlen(metadata[i][1]);
992
993 ceph_encode_32(&p, key_len);
994 memcpy(p, metadata[i][0], key_len);
995 p += key_len;
996 ceph_encode_32(&p, val_len);
997 memcpy(p, metadata[i][1], val_len);
998 p += val_len;
999 }
1000
Yan, Zheng342ce182018-05-11 18:47:29 +08001001 encode_supported_features(&p, end);
1002 msg->front.iov_len = p - msg->front.iov_base;
1003 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1004
Sage Weil2f2dc052009-10-06 11:31:09 -07001005 return msg;
1006}
1007
1008/*
1009 * send session open request.
1010 *
1011 * called under mdsc->mutex
1012 */
1013static int __open_session(struct ceph_mds_client *mdsc,
1014 struct ceph_mds_session *session)
1015{
1016 struct ceph_msg *msg;
1017 int mstate;
1018 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07001019
1020 /* wait for mds to go active? */
1021 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
1022 dout("open_session to mds%d (%s)\n", mds,
1023 ceph_mds_state_name(mstate));
1024 session->s_state = CEPH_MDS_SESSION_OPENING;
1025 session->s_renew_requested = jiffies;
1026
1027 /* send connect message */
John Spraydbd0c8b2014-09-09 19:26:01 +01001028 msg = create_session_open_msg(mdsc, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001029 if (!msg)
1030 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -07001031 ceph_con_send(&session->s_con, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001032 return 0;
1033}
1034
1035/*
Sage Weiled0552a2010-06-21 13:38:25 -07001036 * open sessions for any export targets for the given mds
1037 *
1038 * called under mdsc->mutex
1039 */
Yan, Zheng5d72d132013-11-24 14:33:01 +08001040static struct ceph_mds_session *
1041__open_export_target_session(struct ceph_mds_client *mdsc, int target)
1042{
1043 struct ceph_mds_session *session;
1044
1045 session = __ceph_lookup_mds_session(mdsc, target);
1046 if (!session) {
1047 session = register_session(mdsc, target);
1048 if (IS_ERR(session))
1049 return session;
1050 }
1051 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1052 session->s_state == CEPH_MDS_SESSION_CLOSING)
1053 __open_session(mdsc, session);
1054
1055 return session;
1056}
1057
1058struct ceph_mds_session *
1059ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target)
1060{
1061 struct ceph_mds_session *session;
1062
1063 dout("open_export_target_session to mds%d\n", target);
1064
1065 mutex_lock(&mdsc->mutex);
1066 session = __open_export_target_session(mdsc, target);
1067 mutex_unlock(&mdsc->mutex);
1068
1069 return session;
1070}
1071
Sage Weiled0552a2010-06-21 13:38:25 -07001072static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
1073 struct ceph_mds_session *session)
1074{
1075 struct ceph_mds_info *mi;
1076 struct ceph_mds_session *ts;
1077 int i, mds = session->s_mds;
Sage Weiled0552a2010-06-21 13:38:25 -07001078
Yan, Zheng76201b62017-03-28 17:04:13 +08001079 if (mds >= mdsc->mdsmap->m_num_mds)
Sage Weiled0552a2010-06-21 13:38:25 -07001080 return;
Yan, Zheng5d72d132013-11-24 14:33:01 +08001081
Sage Weiled0552a2010-06-21 13:38:25 -07001082 mi = &mdsc->mdsmap->m_info[mds];
1083 dout("open_export_target_sessions for mds%d (%d targets)\n",
1084 session->s_mds, mi->num_export_targets);
1085
1086 for (i = 0; i < mi->num_export_targets; i++) {
Yan, Zheng5d72d132013-11-24 14:33:01 +08001087 ts = __open_export_target_session(mdsc, mi->export_targets[i]);
1088 if (!IS_ERR(ts))
1089 ceph_put_mds_session(ts);
Sage Weiled0552a2010-06-21 13:38:25 -07001090 }
1091}
1092
Sage Weil154f42c2010-06-21 13:45:04 -07001093void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
1094 struct ceph_mds_session *session)
1095{
1096 mutex_lock(&mdsc->mutex);
1097 __open_export_target_sessions(mdsc, session);
1098 mutex_unlock(&mdsc->mutex);
1099}
1100
Sage Weiled0552a2010-06-21 13:38:25 -07001101/*
Sage Weil2f2dc052009-10-06 11:31:09 -07001102 * session caps
1103 */
1104
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001105static void detach_cap_releases(struct ceph_mds_session *session,
1106 struct list_head *target)
Sage Weil2f2dc052009-10-06 11:31:09 -07001107{
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001108 lockdep_assert_held(&session->s_cap_lock);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001109
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001110 list_splice_init(&session->s_cap_releases, target);
1111 session->s_num_cap_releases = 0;
1112 dout("dispose_cap_releases mds%d\n", session->s_mds);
1113}
1114
1115static void dispose_cap_releases(struct ceph_mds_client *mdsc,
1116 struct list_head *dispose)
1117{
1118 while (!list_empty(dispose)) {
Yan, Zheng745a8e32015-05-14 17:22:42 +08001119 struct ceph_cap *cap;
1120 /* zero out the in-progress message */
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001121 cap = list_first_entry(dispose, struct ceph_cap, session_caps);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001122 list_del(&cap->session_caps);
1123 ceph_put_cap(mdsc, cap);
1124 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001125}
1126
Yan, Zheng1c841a92015-03-24 20:15:36 +08001127static void cleanup_session_requests(struct ceph_mds_client *mdsc,
1128 struct ceph_mds_session *session)
1129{
1130 struct ceph_mds_request *req;
1131 struct rb_node *p;
1132
1133 dout("cleanup_session_requests mds%d\n", session->s_mds);
1134 mutex_lock(&mdsc->mutex);
1135 while (!list_empty(&session->s_unsafe)) {
1136 req = list_first_entry(&session->s_unsafe,
1137 struct ceph_mds_request, r_unsafe_item);
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001138 pr_warn_ratelimited(" dropping unsafe request %llu\n",
1139 req->r_tid);
Yan, Zheng1c841a92015-03-24 20:15:36 +08001140 __unregister_request(mdsc, req);
1141 }
1142 /* zero r_attempts, so kick_requests() will re-send requests */
1143 p = rb_first(&mdsc->request_tree);
1144 while (p) {
1145 req = rb_entry(p, struct ceph_mds_request, r_node);
1146 p = rb_next(p);
1147 if (req->r_session &&
1148 req->r_session->s_mds == session->s_mds)
1149 req->r_attempts = 0;
1150 }
1151 mutex_unlock(&mdsc->mutex);
1152}
1153
Sage Weil2f2dc052009-10-06 11:31:09 -07001154/*
Sage Weilf818a732010-05-11 20:56:31 -07001155 * Helper to safely iterate over all caps associated with a session, with
1156 * special care taken to handle a racing __ceph_remove_cap().
Sage Weil2f2dc052009-10-06 11:31:09 -07001157 *
Sage Weilf818a732010-05-11 20:56:31 -07001158 * Caller must hold session s_mutex.
Sage Weil2f2dc052009-10-06 11:31:09 -07001159 */
1160static int iterate_session_caps(struct ceph_mds_session *session,
1161 int (*cb)(struct inode *, struct ceph_cap *,
1162 void *), void *arg)
1163{
Sage Weil7c1332b2010-02-16 11:39:45 -08001164 struct list_head *p;
1165 struct ceph_cap *cap;
1166 struct inode *inode, *last_inode = NULL;
1167 struct ceph_cap *old_cap = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001168 int ret;
1169
1170 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
1171 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001172 p = session->s_caps.next;
1173 while (p != &session->s_caps) {
1174 cap = list_entry(p, struct ceph_cap, session_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -07001175 inode = igrab(&cap->ci->vfs_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001176 if (!inode) {
1177 p = p->next;
Sage Weil2f2dc052009-10-06 11:31:09 -07001178 continue;
Sage Weil7c1332b2010-02-16 11:39:45 -08001179 }
1180 session->s_cap_iterator = cap;
Sage Weil2f2dc052009-10-06 11:31:09 -07001181 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001182
1183 if (last_inode) {
1184 iput(last_inode);
1185 last_inode = NULL;
1186 }
1187 if (old_cap) {
Yehuda Sadeh37151662010-06-17 16:16:12 -07001188 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -08001189 old_cap = NULL;
1190 }
1191
Sage Weil2f2dc052009-10-06 11:31:09 -07001192 ret = cb(inode, cap, arg);
Sage Weil7c1332b2010-02-16 11:39:45 -08001193 last_inode = inode;
1194
Sage Weil2f2dc052009-10-06 11:31:09 -07001195 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001196 p = p->next;
Markus Elfringd37b1d92017-08-20 20:22:02 +02001197 if (!cap->ci) {
Sage Weil7c1332b2010-02-16 11:39:45 -08001198 dout("iterate_session_caps finishing cap %p removal\n",
1199 cap);
1200 BUG_ON(cap->session != session);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001201 cap->session = NULL;
Sage Weil7c1332b2010-02-16 11:39:45 -08001202 list_del_init(&cap->session_caps);
1203 session->s_nr_caps--;
Yan, Zheng745a8e32015-05-14 17:22:42 +08001204 if (cap->queue_release) {
1205 list_add_tail(&cap->session_caps,
1206 &session->s_cap_releases);
1207 session->s_num_cap_releases++;
1208 } else {
1209 old_cap = cap; /* put_cap it w/o locks held */
1210 }
Sage Weil7c1332b2010-02-16 11:39:45 -08001211 }
Sage Weil5dacf092009-12-21 20:40:34 -08001212 if (ret < 0)
1213 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07001214 }
Sage Weil5dacf092009-12-21 20:40:34 -08001215 ret = 0;
1216out:
Sage Weil7c1332b2010-02-16 11:39:45 -08001217 session->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001218 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001219
SF Markus Elfringe96a6502014-11-02 15:20:59 +01001220 iput(last_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001221 if (old_cap)
Yehuda Sadeh37151662010-06-17 16:16:12 -07001222 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -08001223
Sage Weil5dacf092009-12-21 20:40:34 -08001224 return ret;
Sage Weil2f2dc052009-10-06 11:31:09 -07001225}
1226
1227static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
Sage Weil6c99f252010-05-10 16:12:25 -07001228 void *arg)
Sage Weil2f2dc052009-10-06 11:31:09 -07001229{
Yan, Zheng6c93df52016-04-15 13:56:12 +08001230 struct ceph_fs_client *fsc = (struct ceph_fs_client *)arg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001231 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001232 LIST_HEAD(to_remove);
Yan, Zheng6c93df52016-04-15 13:56:12 +08001233 bool drop = false;
1234 bool invalidate = false;
Sage Weil6c99f252010-05-10 16:12:25 -07001235
Sage Weil2f2dc052009-10-06 11:31:09 -07001236 dout("removing cap %p, ci is %p, inode is %p\n",
1237 cap, ci, &ci->vfs_inode);
Sage Weilbe655592011-11-30 09:47:09 -08001238 spin_lock(&ci->i_ceph_lock);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001239 if (cap->mds_wanted | cap->issued)
1240 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
Yan, Zhenga096b092013-09-22 10:15:58 +08001241 __ceph_remove_cap(cap, false);
Yan, Zheng571ade32015-03-24 11:36:08 +08001242 if (!ci->i_auth_cap) {
Yan, Zheng553adfd2015-06-09 15:48:57 +08001243 struct ceph_cap_flush *cf;
Yan, Zheng6c93df52016-04-15 13:56:12 +08001244 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil6c99f252010-05-10 16:12:25 -07001245
Yan, Zheng6c93df52016-04-15 13:56:12 +08001246 if (ci->i_wrbuffer_ref > 0 &&
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01001247 READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
Yan, Zheng6c93df52016-04-15 13:56:12 +08001248 invalidate = true;
1249
Yan, Zhenge4500b52016-07-06 11:12:56 +08001250 while (!list_empty(&ci->i_cap_flush_list)) {
1251 cf = list_first_entry(&ci->i_cap_flush_list,
1252 struct ceph_cap_flush, i_list);
Wei Yongjun8cdcc072016-08-13 09:10:28 +00001253 list_move(&cf->i_list, &to_remove);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001254 }
1255
Sage Weil6c99f252010-05-10 16:12:25 -07001256 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zheng8310b082015-06-09 17:20:12 +08001257
Yan, Zhenge4500b52016-07-06 11:12:56 +08001258 list_for_each_entry(cf, &to_remove, i_list)
1259 list_del(&cf->g_list);
Yan, Zheng8310b082015-06-09 17:20:12 +08001260
Sage Weil6c99f252010-05-10 16:12:25 -07001261 if (!list_empty(&ci->i_dirty_item)) {
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001262 pr_warn_ratelimited(
1263 " dropping dirty %s state for %p %lld\n",
Sage Weil6c99f252010-05-10 16:12:25 -07001264 ceph_cap_string(ci->i_dirty_caps),
1265 inode, ceph_ino(inode));
1266 ci->i_dirty_caps = 0;
1267 list_del_init(&ci->i_dirty_item);
Yan, Zheng6c93df52016-04-15 13:56:12 +08001268 drop = true;
Sage Weil6c99f252010-05-10 16:12:25 -07001269 }
1270 if (!list_empty(&ci->i_flushing_item)) {
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001271 pr_warn_ratelimited(
1272 " dropping dirty+flushing %s state for %p %lld\n",
Sage Weil6c99f252010-05-10 16:12:25 -07001273 ceph_cap_string(ci->i_flushing_caps),
1274 inode, ceph_ino(inode));
1275 ci->i_flushing_caps = 0;
1276 list_del_init(&ci->i_flushing_item);
1277 mdsc->num_cap_flushing--;
Yan, Zheng6c93df52016-04-15 13:56:12 +08001278 drop = true;
Sage Weil6c99f252010-05-10 16:12:25 -07001279 }
Sage Weil6c99f252010-05-10 16:12:25 -07001280 spin_unlock(&mdsc->cap_dirty_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001281
Yan, Zhengb3f8d682017-09-11 10:58:55 +08001282 if (atomic_read(&ci->i_filelock_ref) > 0) {
1283 /* make further file lock syscall return -EIO */
1284 ci->i_ceph_flags |= CEPH_I_ERROR_FILELOCK;
1285 pr_warn_ratelimited(" dropping file locks for %p %lld\n",
1286 inode, ceph_ino(inode));
1287 }
1288
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001289 if (!ci->i_dirty_caps && ci->i_prealloc_cap_flush) {
Yan, Zhenge4500b52016-07-06 11:12:56 +08001290 list_add(&ci->i_prealloc_cap_flush->i_list, &to_remove);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001291 ci->i_prealloc_cap_flush = NULL;
1292 }
Sage Weil6c99f252010-05-10 16:12:25 -07001293 }
Sage Weilbe655592011-11-30 09:47:09 -08001294 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001295 while (!list_empty(&to_remove)) {
1296 struct ceph_cap_flush *cf;
1297 cf = list_first_entry(&to_remove,
Yan, Zhenge4500b52016-07-06 11:12:56 +08001298 struct ceph_cap_flush, i_list);
1299 list_del(&cf->i_list);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001300 ceph_free_cap_flush(cf);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001301 }
Yan, Zheng77310322016-04-08 15:27:16 +08001302
1303 wake_up_all(&ci->i_cap_wq);
Yan, Zheng6c93df52016-04-15 13:56:12 +08001304 if (invalidate)
1305 ceph_queue_invalidate(inode);
Yan, Zheng77310322016-04-08 15:27:16 +08001306 if (drop)
Sage Weil6c99f252010-05-10 16:12:25 -07001307 iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07001308 return 0;
1309}
1310
1311/*
1312 * caller must hold session s_mutex
1313 */
1314static void remove_session_caps(struct ceph_mds_session *session)
1315{
Yan, Zheng6c93df52016-04-15 13:56:12 +08001316 struct ceph_fs_client *fsc = session->s_mdsc->fsc;
1317 struct super_block *sb = fsc->sb;
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001318 LIST_HEAD(dispose);
1319
Sage Weil2f2dc052009-10-06 11:31:09 -07001320 dout("remove_session_caps on %p\n", session);
Yan, Zheng6c93df52016-04-15 13:56:12 +08001321 iterate_session_caps(session, remove_session_caps_cb, fsc);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001322
Yan, Zhengc8799fc2016-07-07 15:22:38 +08001323 wake_up_all(&fsc->mdsc->cap_flushing_wq);
1324
Yan, Zheng6f60f882013-07-24 12:22:11 +08001325 spin_lock(&session->s_cap_lock);
1326 if (session->s_nr_caps > 0) {
Yan, Zheng6f60f882013-07-24 12:22:11 +08001327 struct inode *inode;
1328 struct ceph_cap *cap, *prev = NULL;
1329 struct ceph_vino vino;
1330 /*
1331 * iterate_session_caps() skips inodes that are being
1332 * deleted, we need to wait until deletions are complete.
1333 * __wait_on_freeing_inode() is designed for the job,
1334 * but it is not exported, so use lookup inode function
1335 * to access it.
1336 */
1337 while (!list_empty(&session->s_caps)) {
1338 cap = list_entry(session->s_caps.next,
1339 struct ceph_cap, session_caps);
1340 if (cap == prev)
1341 break;
1342 prev = cap;
1343 vino = cap->ci->i_vino;
1344 spin_unlock(&session->s_cap_lock);
1345
Yan, Zhenged284c42013-09-02 15:19:53 +08001346 inode = ceph_find_inode(sb, vino);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001347 iput(inode);
1348
1349 spin_lock(&session->s_cap_lock);
1350 }
1351 }
Yan, Zheng745a8e32015-05-14 17:22:42 +08001352
1353 // drop cap expires and unlock s_cap_lock
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001354 detach_cap_releases(session, &dispose);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001355
Sage Weil2f2dc052009-10-06 11:31:09 -07001356 BUG_ON(session->s_nr_caps > 0);
Sage Weil6c99f252010-05-10 16:12:25 -07001357 BUG_ON(!list_empty(&session->s_cap_flushing));
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001358 spin_unlock(&session->s_cap_lock);
1359 dispose_cap_releases(session->s_mdsc, &dispose);
Sage Weil2f2dc052009-10-06 11:31:09 -07001360}
1361
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001362enum {
1363 RECONNECT,
1364 RENEWCAPS,
1365 FORCE_RO,
1366};
1367
Sage Weil2f2dc052009-10-06 11:31:09 -07001368/*
1369 * wake up any threads waiting on this session's caps. if the cap is
1370 * old (didn't get renewed on the client reconnect), remove it now.
1371 *
1372 * caller must hold s_mutex.
1373 */
1374static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1375 void *arg)
1376{
Sage Weil0dc25702009-11-20 13:43:45 -08001377 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001378 unsigned long ev = (unsigned long)arg;
Sage Weil0dc25702009-11-20 13:43:45 -08001379
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001380 if (ev == RECONNECT) {
Sage Weilbe655592011-11-30 09:47:09 -08001381 spin_lock(&ci->i_ceph_lock);
Sage Weil0dc25702009-11-20 13:43:45 -08001382 ci->i_wanted_max_size = 0;
1383 ci->i_requested_max_size = 0;
Sage Weilbe655592011-11-30 09:47:09 -08001384 spin_unlock(&ci->i_ceph_lock);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001385 } else if (ev == RENEWCAPS) {
1386 if (cap->cap_gen < cap->session->s_cap_gen) {
1387 /* mds did not re-issue stale cap */
1388 spin_lock(&ci->i_ceph_lock);
1389 cap->issued = cap->implemented = CEPH_CAP_PIN;
1390 /* make sure mds knows what we want */
1391 if (__ceph_caps_file_wanted(ci) & ~cap->mds_wanted)
1392 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
1393 spin_unlock(&ci->i_ceph_lock);
1394 }
1395 } else if (ev == FORCE_RO) {
Sage Weil0dc25702009-11-20 13:43:45 -08001396 }
Yan, Zhenge5360302016-05-19 19:15:19 +08001397 wake_up_all(&ci->i_cap_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07001398 return 0;
1399}
1400
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001401static void wake_up_session_caps(struct ceph_mds_session *session, int ev)
Sage Weil2f2dc052009-10-06 11:31:09 -07001402{
1403 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
Sage Weil0dc25702009-11-20 13:43:45 -08001404 iterate_session_caps(session, wake_up_session_cb,
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001405 (void *)(unsigned long)ev);
Sage Weil2f2dc052009-10-06 11:31:09 -07001406}
1407
1408/*
1409 * Send periodic message to MDS renewing all currently held caps. The
1410 * ack will reset the expiration for all caps from this session.
1411 *
1412 * caller holds s_mutex
1413 */
1414static int send_renew_caps(struct ceph_mds_client *mdsc,
1415 struct ceph_mds_session *session)
1416{
1417 struct ceph_msg *msg;
1418 int state;
1419
1420 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1421 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1422 pr_info("mds%d caps stale\n", session->s_mds);
Sage Weile4cb4cb2010-03-18 13:43:09 -07001423 session->s_renew_requested = jiffies;
Sage Weil2f2dc052009-10-06 11:31:09 -07001424
1425 /* do not try to renew caps until a recovering mds has reconnected
1426 * with its clients. */
1427 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1428 if (state < CEPH_MDS_STATE_RECONNECT) {
1429 dout("send_renew_caps ignoring mds%d (%s)\n",
1430 session->s_mds, ceph_mds_state_name(state));
1431 return 0;
1432 }
1433
1434 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1435 ceph_mds_state_name(state));
Sage Weil2f2dc052009-10-06 11:31:09 -07001436 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1437 ++session->s_renew_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001438 if (!msg)
1439 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -07001440 ceph_con_send(&session->s_con, msg);
1441 return 0;
1442}
1443
Yan, Zheng186e4f72013-11-22 14:48:37 +08001444static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
1445 struct ceph_mds_session *session, u64 seq)
1446{
1447 struct ceph_msg *msg;
1448
1449 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
John Spraya687eca2014-09-19 13:51:08 +01001450 session->s_mds, ceph_session_state_name(session->s_state), seq);
Yan, Zheng186e4f72013-11-22 14:48:37 +08001451 msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
1452 if (!msg)
1453 return -ENOMEM;
1454 ceph_con_send(&session->s_con, msg);
1455 return 0;
1456}
1457
1458
Sage Weil2f2dc052009-10-06 11:31:09 -07001459/*
1460 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
Sage Weil0dc25702009-11-20 13:43:45 -08001461 *
1462 * Called under session->s_mutex
Sage Weil2f2dc052009-10-06 11:31:09 -07001463 */
1464static void renewed_caps(struct ceph_mds_client *mdsc,
1465 struct ceph_mds_session *session, int is_renew)
1466{
1467 int was_stale;
1468 int wake = 0;
1469
1470 spin_lock(&session->s_cap_lock);
Alex Elder1ce208a2012-01-12 17:48:11 -08001471 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
Sage Weil2f2dc052009-10-06 11:31:09 -07001472
1473 session->s_cap_ttl = session->s_renew_requested +
1474 mdsc->mdsmap->m_session_timeout*HZ;
1475
1476 if (was_stale) {
1477 if (time_before(jiffies, session->s_cap_ttl)) {
1478 pr_info("mds%d caps renewed\n", session->s_mds);
1479 wake = 1;
1480 } else {
1481 pr_info("mds%d caps still stale\n", session->s_mds);
1482 }
1483 }
1484 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1485 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1486 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1487 spin_unlock(&session->s_cap_lock);
1488
1489 if (wake)
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001490 wake_up_session_caps(session, RENEWCAPS);
Sage Weil2f2dc052009-10-06 11:31:09 -07001491}
1492
1493/*
1494 * send a session close request
1495 */
1496static int request_close_session(struct ceph_mds_client *mdsc,
1497 struct ceph_mds_session *session)
1498{
1499 struct ceph_msg *msg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001500
1501 dout("request_close_session mds%d state %s seq %lld\n",
John Spraya687eca2014-09-19 13:51:08 +01001502 session->s_mds, ceph_session_state_name(session->s_state),
Sage Weil2f2dc052009-10-06 11:31:09 -07001503 session->s_seq);
1504 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001505 if (!msg)
1506 return -ENOMEM;
1507 ceph_con_send(&session->s_con, msg);
Yan, Zhengfcff4152016-09-14 16:39:51 +08001508 return 1;
Sage Weil2f2dc052009-10-06 11:31:09 -07001509}
1510
1511/*
1512 * Called with s_mutex held.
1513 */
1514static int __close_session(struct ceph_mds_client *mdsc,
1515 struct ceph_mds_session *session)
1516{
1517 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1518 return 0;
1519 session->s_state = CEPH_MDS_SESSION_CLOSING;
1520 return request_close_session(mdsc, session);
1521}
1522
Yan, Zheng040d7862017-11-30 11:59:22 +08001523static bool drop_negative_children(struct dentry *dentry)
1524{
1525 struct dentry *child;
1526 bool all_negative = true;
1527
1528 if (!d_is_dir(dentry))
1529 goto out;
1530
1531 spin_lock(&dentry->d_lock);
1532 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
1533 if (d_really_is_positive(child)) {
1534 all_negative = false;
1535 break;
1536 }
1537 }
1538 spin_unlock(&dentry->d_lock);
1539
1540 if (all_negative)
1541 shrink_dcache_parent(dentry);
1542out:
1543 return all_negative;
1544}
1545
Sage Weil2f2dc052009-10-06 11:31:09 -07001546/*
1547 * Trim old(er) caps.
1548 *
1549 * Because we can't cache an inode without one or more caps, we do
1550 * this indirectly: if a cap is unused, we prune its aliases, at which
1551 * point the inode will hopefully get dropped to.
1552 *
1553 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1554 * memory pressure from the MDS, though, so it needn't be perfect.
1555 */
1556static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1557{
1558 struct ceph_mds_session *session = arg;
1559 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng979abfd2013-11-22 13:56:24 +08001560 int used, wanted, oissued, mine;
Sage Weil2f2dc052009-10-06 11:31:09 -07001561
1562 if (session->s_trim_caps <= 0)
1563 return -1;
1564
Sage Weilbe655592011-11-30 09:47:09 -08001565 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001566 mine = cap->issued | cap->implemented;
1567 used = __ceph_caps_used(ci);
Yan, Zheng979abfd2013-11-22 13:56:24 +08001568 wanted = __ceph_caps_file_wanted(ci);
Sage Weil2f2dc052009-10-06 11:31:09 -07001569 oissued = __ceph_caps_issued_other(ci, cap);
1570
Yan, Zheng979abfd2013-11-22 13:56:24 +08001571 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001572 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
Yan, Zheng979abfd2013-11-22 13:56:24 +08001573 ceph_cap_string(used), ceph_cap_string(wanted));
1574 if (cap == ci->i_auth_cap) {
Yan, Zheng622f3e22015-05-07 10:59:47 +08001575 if (ci->i_dirty_caps || ci->i_flushing_caps ||
1576 !list_empty(&ci->i_cap_snaps))
Yan, Zheng979abfd2013-11-22 13:56:24 +08001577 goto out;
1578 if ((used | wanted) & CEPH_CAP_ANY_WR)
1579 goto out;
Yan, Zheng89aa5932017-09-08 15:23:18 +08001580 /* Note: it's possible that i_filelock_ref becomes non-zero
1581 * after dropping auth caps. It doesn't hurt because reply
1582 * of lock mds request will re-add auth caps. */
1583 if (atomic_read(&ci->i_filelock_ref) > 0)
1584 goto out;
Yan, Zheng979abfd2013-11-22 13:56:24 +08001585 }
Yan, Zheng5e804ac2015-10-26 16:08:43 +08001586 /* The inode has cached pages, but it's no longer used.
1587 * we can safely drop it */
1588 if (wanted == 0 && used == CEPH_CAP_FILE_CACHE &&
1589 !(oissued & CEPH_CAP_FILE_CACHE)) {
1590 used = 0;
1591 oissued = 0;
1592 }
Yan, Zheng979abfd2013-11-22 13:56:24 +08001593 if ((used | wanted) & ~oissued & mine)
Sage Weil2f2dc052009-10-06 11:31:09 -07001594 goto out; /* we need these caps */
1595
Sage Weil2f2dc052009-10-06 11:31:09 -07001596 if (oissued) {
1597 /* we aren't the only cap.. just remove us */
Yan, Zhenga096b092013-09-22 10:15:58 +08001598 __ceph_remove_cap(cap, true);
Yan, Zheng040d7862017-11-30 11:59:22 +08001599 session->s_trim_caps--;
Sage Weil2f2dc052009-10-06 11:31:09 -07001600 } else {
Yan, Zheng040d7862017-11-30 11:59:22 +08001601 struct dentry *dentry;
Yan, Zheng5e804ac2015-10-26 16:08:43 +08001602 /* try dropping referring dentries */
Sage Weilbe655592011-11-30 09:47:09 -08001603 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng040d7862017-11-30 11:59:22 +08001604 dentry = d_find_any_alias(inode);
1605 if (dentry && drop_negative_children(dentry)) {
1606 int count;
1607 dput(dentry);
1608 d_prune_aliases(inode);
1609 count = atomic_read(&inode->i_count);
1610 if (count == 1)
1611 session->s_trim_caps--;
1612 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1613 inode, cap, count);
1614 } else {
1615 dput(dentry);
1616 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001617 return 0;
1618 }
1619
1620out:
Sage Weilbe655592011-11-30 09:47:09 -08001621 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001622 return 0;
1623}
1624
1625/*
1626 * Trim session cap count down to some max number.
1627 */
Zhi Zhange30ee582018-01-24 21:24:33 +08001628int ceph_trim_caps(struct ceph_mds_client *mdsc,
1629 struct ceph_mds_session *session,
1630 int max_caps)
Sage Weil2f2dc052009-10-06 11:31:09 -07001631{
1632 int trim_caps = session->s_nr_caps - max_caps;
1633
1634 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1635 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1636 if (trim_caps > 0) {
1637 session->s_trim_caps = trim_caps;
1638 iterate_session_caps(session, trim_caps_cb, session);
1639 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1640 session->s_mds, session->s_nr_caps, max_caps,
1641 trim_caps - session->s_trim_caps);
Sage Weil5dacf092009-12-21 20:40:34 -08001642 session->s_trim_caps = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001643 }
Yan, Zhenga56371d2014-04-01 20:34:56 +08001644
Yan, Zhenga56371d2014-04-01 20:34:56 +08001645 ceph_send_cap_releases(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07001646 return 0;
1647}
1648
Yan, Zheng8310b082015-06-09 17:20:12 +08001649static int check_caps_flush(struct ceph_mds_client *mdsc,
1650 u64 want_flush_tid)
1651{
Yan, Zheng8310b082015-06-09 17:20:12 +08001652 int ret = 1;
1653
1654 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zhenge4500b52016-07-06 11:12:56 +08001655 if (!list_empty(&mdsc->cap_flush_list)) {
1656 struct ceph_cap_flush *cf =
1657 list_first_entry(&mdsc->cap_flush_list,
1658 struct ceph_cap_flush, g_list);
1659 if (cf->tid <= want_flush_tid) {
1660 dout("check_caps_flush still flushing tid "
1661 "%llu <= %llu\n", cf->tid, want_flush_tid);
1662 ret = 0;
1663 }
Yan, Zheng8310b082015-06-09 17:20:12 +08001664 }
1665 spin_unlock(&mdsc->cap_dirty_lock);
1666 return ret;
Yan, Zhengd3383a82015-01-08 21:30:12 +08001667}
1668
Sage Weil2f2dc052009-10-06 11:31:09 -07001669/*
1670 * flush all dirty inode data to disk.
1671 *
Yan, Zheng8310b082015-06-09 17:20:12 +08001672 * returns true if we've flushed through want_flush_tid
Sage Weil2f2dc052009-10-06 11:31:09 -07001673 */
Yan, Zhengaffbc192015-05-05 21:22:13 +08001674static void wait_caps_flush(struct ceph_mds_client *mdsc,
Yan, Zheng0e294382016-07-04 18:06:41 +08001675 u64 want_flush_tid)
Sage Weil2f2dc052009-10-06 11:31:09 -07001676{
Yan, Zheng0e294382016-07-04 18:06:41 +08001677 dout("check_caps_flush want %llu\n", want_flush_tid);
Yan, Zheng8310b082015-06-09 17:20:12 +08001678
1679 wait_event(mdsc->cap_flushing_wq,
1680 check_caps_flush(mdsc, want_flush_tid));
1681
1682 dout("check_caps_flush ok, flushed thru %llu\n", want_flush_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07001683}
1684
1685/*
1686 * called under s_mutex
1687 */
Sage Weil3d7ded42010-06-09 16:47:10 -07001688void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1689 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001690{
Yan, Zheng745a8e32015-05-14 17:22:42 +08001691 struct ceph_msg *msg = NULL;
1692 struct ceph_mds_cap_release *head;
1693 struct ceph_mds_cap_item *item;
Jeff Layton92475f02017-04-13 11:07:04 -04001694 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
Yan, Zheng745a8e32015-05-14 17:22:42 +08001695 struct ceph_cap *cap;
1696 LIST_HEAD(tmp_list);
1697 int num_cap_releases;
Jeff Layton92475f02017-04-13 11:07:04 -04001698 __le32 barrier, *cap_barrier;
1699
1700 down_read(&osdc->lock);
1701 barrier = cpu_to_le32(osdc->epoch_barrier);
1702 up_read(&osdc->lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001703
Sage Weil0f8605f2010-05-05 15:51:35 -07001704 spin_lock(&session->s_cap_lock);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001705again:
1706 list_splice_init(&session->s_cap_releases, &tmp_list);
1707 num_cap_releases = session->s_num_cap_releases;
1708 session->s_num_cap_releases = 0;
1709 spin_unlock(&session->s_cap_lock);
1710
1711 while (!list_empty(&tmp_list)) {
1712 if (!msg) {
1713 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001714 PAGE_SIZE, GFP_NOFS, false);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001715 if (!msg)
1716 goto out_err;
1717 head = msg->front.iov_base;
1718 head->num = cpu_to_le32(0);
1719 msg->front.iov_len = sizeof(*head);
Jeff Layton92475f02017-04-13 11:07:04 -04001720
1721 msg->hdr.version = cpu_to_le16(2);
1722 msg->hdr.compat_version = cpu_to_le16(1);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001723 }
Jeff Layton92475f02017-04-13 11:07:04 -04001724
Yan, Zheng745a8e32015-05-14 17:22:42 +08001725 cap = list_first_entry(&tmp_list, struct ceph_cap,
1726 session_caps);
1727 list_del(&cap->session_caps);
1728 num_cap_releases--;
1729
1730 head = msg->front.iov_base;
1731 le32_add_cpu(&head->num, 1);
1732 item = msg->front.iov_base + msg->front.iov_len;
1733 item->ino = cpu_to_le64(cap->cap_ino);
1734 item->cap_id = cpu_to_le64(cap->cap_id);
1735 item->migrate_seq = cpu_to_le32(cap->mseq);
1736 item->seq = cpu_to_le32(cap->issue_seq);
1737 msg->front.iov_len += sizeof(*item);
1738
1739 ceph_put_cap(mdsc, cap);
1740
1741 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
Jeff Layton92475f02017-04-13 11:07:04 -04001742 // Append cap_barrier field
1743 cap_barrier = msg->front.iov_base + msg->front.iov_len;
1744 *cap_barrier = barrier;
1745 msg->front.iov_len += sizeof(*cap_barrier);
1746
Yan, Zheng745a8e32015-05-14 17:22:42 +08001747 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1748 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1749 ceph_con_send(&session->s_con, msg);
1750 msg = NULL;
1751 }
1752 }
1753
1754 BUG_ON(num_cap_releases != 0);
1755
1756 spin_lock(&session->s_cap_lock);
1757 if (!list_empty(&session->s_cap_releases))
1758 goto again;
1759 spin_unlock(&session->s_cap_lock);
1760
1761 if (msg) {
Jeff Layton92475f02017-04-13 11:07:04 -04001762 // Append cap_barrier field
1763 cap_barrier = msg->front.iov_base + msg->front.iov_len;
1764 *cap_barrier = barrier;
1765 msg->front.iov_len += sizeof(*cap_barrier);
1766
Sage Weil2f2dc052009-10-06 11:31:09 -07001767 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1768 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1769 ceph_con_send(&session->s_con, msg);
1770 }
Yan, Zheng745a8e32015-05-14 17:22:42 +08001771 return;
1772out_err:
1773 pr_err("send_cap_releases mds%d, failed to allocate message\n",
1774 session->s_mds);
1775 spin_lock(&session->s_cap_lock);
1776 list_splice(&tmp_list, &session->s_cap_releases);
1777 session->s_num_cap_releases += num_cap_releases;
Sage Weil2f2dc052009-10-06 11:31:09 -07001778 spin_unlock(&session->s_cap_lock);
1779}
1780
1781/*
1782 * requests
1783 */
1784
Yan, Zheng54008392014-03-29 13:41:15 +08001785int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
1786 struct inode *dir)
1787{
1788 struct ceph_inode_info *ci = ceph_inode(dir);
1789 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1790 struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options;
Yan, Zheng2a5beea2016-04-28 09:37:39 +08001791 size_t size = sizeof(struct ceph_mds_reply_dir_entry);
Yan, Zheng54008392014-03-29 13:41:15 +08001792 int order, num_entries;
1793
1794 spin_lock(&ci->i_ceph_lock);
1795 num_entries = ci->i_files + ci->i_subdirs;
1796 spin_unlock(&ci->i_ceph_lock);
1797 num_entries = max(num_entries, 1);
1798 num_entries = min(num_entries, opt->max_readdir);
1799
1800 order = get_order(size * num_entries);
1801 while (order >= 0) {
Yan, Zheng2a5beea2016-04-28 09:37:39 +08001802 rinfo->dir_entries = (void*)__get_free_pages(GFP_KERNEL |
1803 __GFP_NOWARN,
1804 order);
1805 if (rinfo->dir_entries)
Yan, Zheng54008392014-03-29 13:41:15 +08001806 break;
1807 order--;
1808 }
Yan, Zheng2a5beea2016-04-28 09:37:39 +08001809 if (!rinfo->dir_entries)
Yan, Zheng54008392014-03-29 13:41:15 +08001810 return -ENOMEM;
1811
1812 num_entries = (PAGE_SIZE << order) / size;
1813 num_entries = min(num_entries, opt->max_readdir);
1814
1815 rinfo->dir_buf_size = PAGE_SIZE << order;
1816 req->r_num_caps = num_entries + 1;
1817 req->r_args.readdir.max_entries = cpu_to_le32(num_entries);
1818 req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes);
1819 return 0;
1820}
1821
Sage Weil2f2dc052009-10-06 11:31:09 -07001822/*
1823 * Create an mds request.
1824 */
1825struct ceph_mds_request *
1826ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1827{
1828 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
Arnd Bergmann63ecae72018-07-13 22:18:35 +02001829 struct timespec64 ts;
Sage Weil2f2dc052009-10-06 11:31:09 -07001830
1831 if (!req)
1832 return ERR_PTR(-ENOMEM);
1833
Sage Weilb4556392010-05-13 12:01:13 -07001834 mutex_init(&req->r_fill_mutex);
Yehuda Sadeh37151662010-06-17 16:16:12 -07001835 req->r_mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07001836 req->r_started = jiffies;
1837 req->r_resend_mds = -1;
1838 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08001839 INIT_LIST_HEAD(&req->r_unsafe_target_item);
Sage Weil2f2dc052009-10-06 11:31:09 -07001840 req->r_fmode = -1;
Sage Weil153c8e62009-12-07 12:31:09 -08001841 kref_init(&req->r_kref);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02001842 RB_CLEAR_NODE(&req->r_node);
Sage Weil2f2dc052009-10-06 11:31:09 -07001843 INIT_LIST_HEAD(&req->r_wait);
1844 init_completion(&req->r_completion);
1845 init_completion(&req->r_safe_completion);
1846 INIT_LIST_HEAD(&req->r_unsafe_item);
1847
Arnd Bergmann63ecae72018-07-13 22:18:35 +02001848 ktime_get_coarse_real_ts64(&ts);
Arnd Bergmann0ed1e902018-07-13 22:18:38 +02001849 req->r_stamp = timespec64_trunc(ts, mdsc->fsc->sb->s_time_gran);
Sage Weilb8e69062014-05-21 17:41:08 -07001850
Sage Weil2f2dc052009-10-06 11:31:09 -07001851 req->r_op = op;
1852 req->r_direct_mode = mode;
1853 return req;
1854}
1855
1856/*
Sage Weil44ca18f2010-02-15 12:08:46 -08001857 * return oldest (lowest) request, tid in request tree, 0 if none.
Sage Weil2f2dc052009-10-06 11:31:09 -07001858 *
1859 * called under mdsc->mutex.
1860 */
Sage Weil44ca18f2010-02-15 12:08:46 -08001861static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1862{
1863 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1864 return NULL;
1865 return rb_entry(rb_first(&mdsc->request_tree),
1866 struct ceph_mds_request, r_node);
1867}
1868
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08001869static inline u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07001870{
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08001871 return mdsc->oldest_tid;
Sage Weil2f2dc052009-10-06 11:31:09 -07001872}
1873
1874/*
1875 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1876 * on build_path_from_dentry in fs/cifs/dir.c.
1877 *
1878 * If @stop_on_nosnap, generate path relative to the first non-snapped
1879 * inode.
1880 *
1881 * Encode hidden .snap dirs as a double /, i.e.
1882 * foo/.snap/bar -> foo//bar
1883 */
1884char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1885 int stop_on_nosnap)
1886{
1887 struct dentry *temp;
1888 char *path;
1889 int len, pos;
Al Viro1b71fe22011-07-16 23:43:58 -04001890 unsigned seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07001891
Markus Elfringd37b1d92017-08-20 20:22:02 +02001892 if (!dentry)
Sage Weil2f2dc052009-10-06 11:31:09 -07001893 return ERR_PTR(-EINVAL);
1894
1895retry:
1896 len = 0;
Al Viro1b71fe22011-07-16 23:43:58 -04001897 seq = read_seqbegin(&rename_lock);
1898 rcu_read_lock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001899 for (temp = dentry; !IS_ROOT(temp);) {
David Howells2b0143b2015-03-17 22:25:59 +00001900 struct inode *inode = d_inode(temp);
Sage Weil2f2dc052009-10-06 11:31:09 -07001901 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1902 len++; /* slash only */
1903 else if (stop_on_nosnap && inode &&
1904 ceph_snap(inode) == CEPH_NOSNAP)
1905 break;
1906 else
1907 len += 1 + temp->d_name.len;
1908 temp = temp->d_parent;
Sage Weil2f2dc052009-10-06 11:31:09 -07001909 }
Al Viro1b71fe22011-07-16 23:43:58 -04001910 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001911 if (len)
1912 len--; /* no leading '/' */
1913
1914 path = kmalloc(len+1, GFP_NOFS);
Markus Elfringd37b1d92017-08-20 20:22:02 +02001915 if (!path)
Sage Weil2f2dc052009-10-06 11:31:09 -07001916 return ERR_PTR(-ENOMEM);
1917 pos = len;
1918 path[pos] = 0; /* trailing null */
Al Viro1b71fe22011-07-16 23:43:58 -04001919 rcu_read_lock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001920 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
Al Viro1b71fe22011-07-16 23:43:58 -04001921 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07001922
Al Viro1b71fe22011-07-16 23:43:58 -04001923 spin_lock(&temp->d_lock);
David Howells2b0143b2015-03-17 22:25:59 +00001924 inode = d_inode(temp);
Sage Weil2f2dc052009-10-06 11:31:09 -07001925 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
Sage Weil104648a2010-03-18 10:14:30 -07001926 dout("build_path path+%d: %p SNAPDIR\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001927 pos, temp);
1928 } else if (stop_on_nosnap && inode &&
1929 ceph_snap(inode) == CEPH_NOSNAP) {
Yehuda Sadeh9d5a09e2011-12-13 09:57:44 -08001930 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001931 break;
1932 } else {
1933 pos -= temp->d_name.len;
Al Viro1b71fe22011-07-16 23:43:58 -04001934 if (pos < 0) {
1935 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001936 break;
Al Viro1b71fe22011-07-16 23:43:58 -04001937 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001938 strncpy(path + pos, temp->d_name.name,
1939 temp->d_name.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07001940 }
Al Viro1b71fe22011-07-16 23:43:58 -04001941 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001942 if (pos)
1943 path[--pos] = '/';
1944 temp = temp->d_parent;
Sage Weil2f2dc052009-10-06 11:31:09 -07001945 }
Al Viro1b71fe22011-07-16 23:43:58 -04001946 rcu_read_unlock();
1947 if (pos != 0 || read_seqretry(&rename_lock, seq)) {
Sage Weil104648a2010-03-18 10:14:30 -07001948 pr_err("build_path did not end path lookup where "
Sage Weil2f2dc052009-10-06 11:31:09 -07001949 "expected, namelen is %d, pos is %d\n", len, pos);
1950 /* presumably this is only possible if racing with a
1951 rename of one of the parent directories (we can not
1952 lock the dentries above us to prevent this, but
1953 retrying should be harmless) */
1954 kfree(path);
1955 goto retry;
1956 }
1957
David Howells2b0143b2015-03-17 22:25:59 +00001958 *base = ceph_ino(d_inode(temp));
Sage Weil2f2dc052009-10-06 11:31:09 -07001959 *plen = len;
Sage Weil104648a2010-03-18 10:14:30 -07001960 dout("build_path on %p %d built %llx '%.*s'\n",
Al Viro84d08fa2013-07-05 18:59:33 +04001961 dentry, d_count(dentry), *base, len, path);
Sage Weil2f2dc052009-10-06 11:31:09 -07001962 return path;
1963}
1964
Jeff Laytonfd36a712016-12-15 08:37:58 -05001965static int build_dentry_path(struct dentry *dentry, struct inode *dir,
Sage Weil2f2dc052009-10-06 11:31:09 -07001966 const char **ppath, int *ppathlen, u64 *pino,
1967 int *pfreepath)
1968{
1969 char *path;
1970
Jeff Laytonc6b0b652016-12-15 08:37:57 -05001971 rcu_read_lock();
Jeff Laytonfd36a712016-12-15 08:37:58 -05001972 if (!dir)
1973 dir = d_inode_rcu(dentry->d_parent);
Jeff Laytonc6b0b652016-12-15 08:37:57 -05001974 if (dir && ceph_snap(dir) == CEPH_NOSNAP) {
1975 *pino = ceph_ino(dir);
1976 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001977 *ppath = dentry->d_name.name;
1978 *ppathlen = dentry->d_name.len;
1979 return 0;
1980 }
Jeff Laytonc6b0b652016-12-15 08:37:57 -05001981 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001982 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1983 if (IS_ERR(path))
1984 return PTR_ERR(path);
1985 *ppath = path;
1986 *pfreepath = 1;
1987 return 0;
1988}
1989
1990static int build_inode_path(struct inode *inode,
1991 const char **ppath, int *ppathlen, u64 *pino,
1992 int *pfreepath)
1993{
1994 struct dentry *dentry;
1995 char *path;
1996
1997 if (ceph_snap(inode) == CEPH_NOSNAP) {
1998 *pino = ceph_ino(inode);
1999 *ppathlen = 0;
2000 return 0;
2001 }
2002 dentry = d_find_alias(inode);
2003 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
2004 dput(dentry);
2005 if (IS_ERR(path))
2006 return PTR_ERR(path);
2007 *ppath = path;
2008 *pfreepath = 1;
2009 return 0;
2010}
2011
2012/*
2013 * request arguments may be specified via an inode *, a dentry *, or
2014 * an explicit ino+path.
2015 */
2016static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
Jeff Laytonfd36a712016-12-15 08:37:58 -05002017 struct inode *rdiri, const char *rpath,
2018 u64 rino, const char **ppath, int *pathlen,
Sage Weil2f2dc052009-10-06 11:31:09 -07002019 u64 *ino, int *freepath)
2020{
2021 int r = 0;
2022
2023 if (rinode) {
2024 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
2025 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
2026 ceph_snap(rinode));
2027 } else if (rdentry) {
Jeff Laytonfd36a712016-12-15 08:37:58 -05002028 r = build_dentry_path(rdentry, rdiri, ppath, pathlen, ino,
2029 freepath);
Sage Weil2f2dc052009-10-06 11:31:09 -07002030 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
2031 *ppath);
Sage Weil795858d2011-08-15 13:02:37 -07002032 } else if (rpath || rino) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002033 *ino = rino;
2034 *ppath = rpath;
David Zafmanb0000562012-10-25 10:23:46 -07002035 *pathlen = rpath ? strlen(rpath) : 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002036 dout(" path %.*s\n", *pathlen, rpath);
2037 }
2038
2039 return r;
2040}
2041
2042/*
2043 * called under mdsc->mutex
2044 */
2045static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
2046 struct ceph_mds_request *req,
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002047 int mds, bool drop_cap_releases)
Sage Weil2f2dc052009-10-06 11:31:09 -07002048{
2049 struct ceph_msg *msg;
2050 struct ceph_mds_request_head *head;
2051 const char *path1 = NULL;
2052 const char *path2 = NULL;
2053 u64 ino1 = 0, ino2 = 0;
2054 int pathlen1 = 0, pathlen2 = 0;
2055 int freepath1 = 0, freepath2 = 0;
2056 int len;
2057 u16 releases;
2058 void *p, *end;
2059 int ret;
2060
2061 ret = set_request_path_attr(req->r_inode, req->r_dentry,
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002062 req->r_parent, req->r_path1, req->r_ino1.ino,
Sage Weil2f2dc052009-10-06 11:31:09 -07002063 &path1, &pathlen1, &ino1, &freepath1);
2064 if (ret < 0) {
2065 msg = ERR_PTR(ret);
2066 goto out;
2067 }
2068
2069 ret = set_request_path_attr(NULL, req->r_old_dentry,
Jeff Laytonfd36a712016-12-15 08:37:58 -05002070 req->r_old_dentry_dir,
Sage Weil2f2dc052009-10-06 11:31:09 -07002071 req->r_path2, req->r_ino2.ino,
2072 &path2, &pathlen2, &ino2, &freepath2);
2073 if (ret < 0) {
2074 msg = ERR_PTR(ret);
2075 goto out_free1;
2076 }
2077
2078 len = sizeof(*head) +
Sage Weilb8e69062014-05-21 17:41:08 -07002079 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) +
Arnd Bergmann777d7382015-09-30 15:04:42 +02002080 sizeof(struct ceph_timespec);
Sage Weil2f2dc052009-10-06 11:31:09 -07002081
2082 /* calculate (max) length for cap releases */
2083 len += sizeof(struct ceph_mds_request_release) *
2084 (!!req->r_inode_drop + !!req->r_dentry_drop +
2085 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
2086 if (req->r_dentry_drop)
2087 len += req->r_dentry->d_name.len;
2088 if (req->r_old_dentry_drop)
2089 len += req->r_old_dentry->d_name.len;
2090
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02002091 msg = ceph_msg_new2(CEPH_MSG_CLIENT_REQUEST, len, 1, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07002092 if (!msg) {
2093 msg = ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -07002094 goto out_free2;
Sage Weila79832f2010-04-01 16:06:19 -07002095 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002096
John Spray7cfa0312014-10-30 17:15:26 +00002097 msg->hdr.version = cpu_to_le16(2);
Sage Weil6df058c2009-12-22 11:24:33 -08002098 msg->hdr.tid = cpu_to_le64(req->r_tid);
2099
Sage Weil2f2dc052009-10-06 11:31:09 -07002100 head = msg->front.iov_base;
2101 p = msg->front.iov_base + sizeof(*head);
2102 end = msg->front.iov_base + msg->front.iov_len;
2103
2104 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
2105 head->op = cpu_to_le32(req->r_op);
Eric W. Biedermanff3d0042013-01-31 04:01:53 -08002106 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid));
2107 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid));
Sage Weil2f2dc052009-10-06 11:31:09 -07002108 head->args = req->r_args;
2109
2110 ceph_encode_filepath(&p, end, ino1, path1);
2111 ceph_encode_filepath(&p, end, ino2, path2);
2112
Sage Weile979cf52010-07-15 14:58:39 -07002113 /* make note of release offset, in case we need to replay */
2114 req->r_request_release_offset = p - msg->front.iov_base;
2115
Sage Weil2f2dc052009-10-06 11:31:09 -07002116 /* cap releases */
2117 releases = 0;
2118 if (req->r_inode_drop)
2119 releases += ceph_encode_inode_release(&p,
David Howells2b0143b2015-03-17 22:25:59 +00002120 req->r_inode ? req->r_inode : d_inode(req->r_dentry),
Sage Weil2f2dc052009-10-06 11:31:09 -07002121 mds, req->r_inode_drop, req->r_inode_unless, 0);
2122 if (req->r_dentry_drop)
2123 releases += ceph_encode_dentry_release(&p, req->r_dentry,
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002124 req->r_parent, mds, req->r_dentry_drop,
Jeff Laytonca6c8ae2016-12-15 08:37:59 -05002125 req->r_dentry_unless);
Sage Weil2f2dc052009-10-06 11:31:09 -07002126 if (req->r_old_dentry_drop)
2127 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
Jeff Laytonca6c8ae2016-12-15 08:37:59 -05002128 req->r_old_dentry_dir, mds,
2129 req->r_old_dentry_drop,
2130 req->r_old_dentry_unless);
Sage Weil2f2dc052009-10-06 11:31:09 -07002131 if (req->r_old_inode_drop)
2132 releases += ceph_encode_inode_release(&p,
David Howells2b0143b2015-03-17 22:25:59 +00002133 d_inode(req->r_old_dentry),
Sage Weil2f2dc052009-10-06 11:31:09 -07002134 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002135
2136 if (drop_cap_releases) {
2137 releases = 0;
2138 p = msg->front.iov_base + req->r_request_release_offset;
2139 }
2140
Sage Weil2f2dc052009-10-06 11:31:09 -07002141 head->num_releases = cpu_to_le16(releases);
2142
Sage Weilb8e69062014-05-21 17:41:08 -07002143 /* time stamp */
Yan, Zheng1f041a82015-01-13 15:20:52 +08002144 {
2145 struct ceph_timespec ts;
Arnd Bergmann0ed1e902018-07-13 22:18:38 +02002146 ceph_encode_timespec64(&ts, &req->r_stamp);
Yan, Zheng1f041a82015-01-13 15:20:52 +08002147 ceph_encode_copy(&p, &ts, sizeof(ts));
2148 }
Sage Weilb8e69062014-05-21 17:41:08 -07002149
Sage Weil2f2dc052009-10-06 11:31:09 -07002150 BUG_ON(p > end);
2151 msg->front.iov_len = p - msg->front.iov_base;
2152 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2153
Yan, Zheng25e6bae2014-09-16 19:15:28 +08002154 if (req->r_pagelist) {
2155 struct ceph_pagelist *pagelist = req->r_pagelist;
Yan, Zheng25e6bae2014-09-16 19:15:28 +08002156 ceph_msg_data_add_pagelist(msg, pagelist);
2157 msg->hdr.data_len = cpu_to_le32(pagelist->length);
2158 } else {
2159 msg->hdr.data_len = 0;
Alex Elderebf18f42013-03-04 22:29:57 -06002160 }
Alex Elder02afca62013-02-14 12:16:43 -06002161
Sage Weil2f2dc052009-10-06 11:31:09 -07002162 msg->hdr.data_off = cpu_to_le16(0);
2163
2164out_free2:
2165 if (freepath2)
2166 kfree((char *)path2);
2167out_free1:
2168 if (freepath1)
2169 kfree((char *)path1);
2170out:
2171 return msg;
2172}
2173
2174/*
2175 * called under mdsc->mutex if error, under no mutex if
2176 * success.
2177 */
2178static void complete_request(struct ceph_mds_client *mdsc,
2179 struct ceph_mds_request *req)
2180{
2181 if (req->r_callback)
2182 req->r_callback(mdsc, req);
2183 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002184 complete_all(&req->r_completion);
Sage Weil2f2dc052009-10-06 11:31:09 -07002185}
2186
2187/*
2188 * called under mdsc->mutex
2189 */
2190static int __prepare_send_request(struct ceph_mds_client *mdsc,
2191 struct ceph_mds_request *req,
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002192 int mds, bool drop_cap_releases)
Sage Weil2f2dc052009-10-06 11:31:09 -07002193{
2194 struct ceph_mds_request_head *rhead;
2195 struct ceph_msg *msg;
2196 int flags = 0;
2197
Sage Weil2f2dc052009-10-06 11:31:09 -07002198 req->r_attempts++;
Greg Farnume55b71f2010-06-22 15:58:01 -07002199 if (req->r_inode) {
2200 struct ceph_cap *cap =
2201 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
2202
2203 if (cap)
2204 req->r_sent_on_mseq = cap->mseq;
2205 else
2206 req->r_sent_on_mseq = -1;
2207 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002208 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
2209 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
2210
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002211 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002212 void *p;
Sage Weil01a92f12010-07-15 13:24:32 -07002213 /*
2214 * Replay. Do not regenerate message (and rebuild
2215 * paths, etc.); just use the original message.
2216 * Rebuilding paths will break for renames because
2217 * d_move mangles the src name.
2218 */
2219 msg = req->r_request;
2220 rhead = msg->front.iov_base;
2221
2222 flags = le32_to_cpu(rhead->flags);
2223 flags |= CEPH_MDS_FLAG_REPLAY;
2224 rhead->flags = cpu_to_le32(flags);
2225
2226 if (req->r_target_inode)
2227 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
2228
2229 rhead->num_retry = req->r_attempts - 1;
Sage Weile979cf52010-07-15 14:58:39 -07002230
2231 /* remove cap/dentry releases from message */
2232 rhead->num_releases = 0;
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002233
2234 /* time stamp */
2235 p = msg->front.iov_base + req->r_request_release_offset;
Yan, Zheng1f041a82015-01-13 15:20:52 +08002236 {
2237 struct ceph_timespec ts;
Arnd Bergmann0ed1e902018-07-13 22:18:38 +02002238 ceph_encode_timespec64(&ts, &req->r_stamp);
Yan, Zheng1f041a82015-01-13 15:20:52 +08002239 ceph_encode_copy(&p, &ts, sizeof(ts));
2240 }
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002241
2242 msg->front.iov_len = p - msg->front.iov_base;
2243 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
Sage Weil01a92f12010-07-15 13:24:32 -07002244 return 0;
2245 }
2246
Sage Weil2f2dc052009-10-06 11:31:09 -07002247 if (req->r_request) {
2248 ceph_msg_put(req->r_request);
2249 req->r_request = NULL;
2250 }
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002251 msg = create_request_message(mdsc, req, mds, drop_cap_releases);
Sage Weil2f2dc052009-10-06 11:31:09 -07002252 if (IS_ERR(msg)) {
Sage Weile1518c72010-05-13 11:19:06 -07002253 req->r_err = PTR_ERR(msg);
Sage Weila79832f2010-04-01 16:06:19 -07002254 return PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002255 }
2256 req->r_request = msg;
2257
2258 rhead = msg->front.iov_base;
Sage Weil2f2dc052009-10-06 11:31:09 -07002259 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002260 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Sage Weil2f2dc052009-10-06 11:31:09 -07002261 flags |= CEPH_MDS_FLAG_REPLAY;
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002262 if (req->r_parent)
Sage Weil2f2dc052009-10-06 11:31:09 -07002263 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
2264 rhead->flags = cpu_to_le32(flags);
2265 rhead->num_fwd = req->r_num_fwd;
2266 rhead->num_retry = req->r_attempts - 1;
Sage Weil01a92f12010-07-15 13:24:32 -07002267 rhead->ino = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002268
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002269 dout(" r_parent = %p\n", req->r_parent);
Sage Weil2f2dc052009-10-06 11:31:09 -07002270 return 0;
2271}
2272
2273/*
2274 * send request, or put it on the appropriate wait list.
2275 */
Chengguang Xud5548492018-07-28 16:30:48 +08002276static void __do_request(struct ceph_mds_client *mdsc,
Sage Weil2f2dc052009-10-06 11:31:09 -07002277 struct ceph_mds_request *req)
2278{
2279 struct ceph_mds_session *session = NULL;
2280 int mds = -1;
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002281 int err = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002282
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002283 if (req->r_err || test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
2284 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
Yan, Zhengeb1b8af2013-09-26 14:25:36 +08002285 __unregister_request(mdsc, req);
Chengguang Xud5548492018-07-28 16:30:48 +08002286 return;
Yan, Zhengeb1b8af2013-09-26 14:25:36 +08002287 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002288
2289 if (req->r_timeout &&
2290 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
2291 dout("do_request timed out\n");
2292 err = -EIO;
2293 goto finish;
2294 }
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01002295 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002296 dout("do_request forced umount\n");
2297 err = -EIO;
2298 goto finish;
2299 }
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01002300 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_MOUNTING) {
Yan, Zhenge9e427f2016-11-10 16:02:06 +08002301 if (mdsc->mdsmap_err) {
2302 err = mdsc->mdsmap_err;
2303 dout("do_request mdsmap err %d\n", err);
2304 goto finish;
2305 }
Yan, Zhengcc8e8342017-01-04 16:21:58 +08002306 if (mdsc->mdsmap->m_epoch == 0) {
2307 dout("do_request no mdsmap, waiting for map\n");
2308 list_add(&req->r_wait, &mdsc->waiting_for_map);
Chengguang Xud5548492018-07-28 16:30:48 +08002309 return;
Yan, Zhengcc8e8342017-01-04 16:21:58 +08002310 }
Yan, Zhenge9e427f2016-11-10 16:02:06 +08002311 if (!(mdsc->fsc->mount_options->flags &
2312 CEPH_MOUNT_OPT_MOUNTWAIT) &&
2313 !ceph_mdsmap_is_cluster_available(mdsc->mdsmap)) {
2314 err = -ENOENT;
2315 pr_info("probably no mds server is up\n");
2316 goto finish;
2317 }
2318 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002319
Sage Weildc69e2e2010-11-02 13:49:00 -07002320 put_request_session(req);
2321
Sage Weil2f2dc052009-10-06 11:31:09 -07002322 mds = __choose_mds(mdsc, req);
2323 if (mds < 0 ||
2324 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
2325 dout("do_request no mds or not active, waiting for map\n");
2326 list_add(&req->r_wait, &mdsc->waiting_for_map);
Chengguang Xud5548492018-07-28 16:30:48 +08002327 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07002328 }
2329
2330 /* get, open session */
2331 session = __ceph_lookup_mds_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07002332 if (!session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002333 session = register_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07002334 if (IS_ERR(session)) {
2335 err = PTR_ERR(session);
2336 goto finish;
2337 }
2338 }
Sage Weildc69e2e2010-11-02 13:49:00 -07002339 req->r_session = get_session(session);
2340
Sage Weil2f2dc052009-10-06 11:31:09 -07002341 dout("do_request mds%d session %p state %s\n", mds, session,
John Spraya687eca2014-09-19 13:51:08 +01002342 ceph_session_state_name(session->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07002343 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
2344 session->s_state != CEPH_MDS_SESSION_HUNG) {
Yan, Zhengfcff4152016-09-14 16:39:51 +08002345 if (session->s_state == CEPH_MDS_SESSION_REJECTED) {
2346 err = -EACCES;
2347 goto out_session;
2348 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002349 if (session->s_state == CEPH_MDS_SESSION_NEW ||
2350 session->s_state == CEPH_MDS_SESSION_CLOSING)
2351 __open_session(mdsc, session);
2352 list_add(&req->r_wait, &session->s_waiting);
2353 goto out_session;
2354 }
2355
2356 /* send request */
Sage Weil2f2dc052009-10-06 11:31:09 -07002357 req->r_resend_mds = -1; /* forget any previous mds hint */
2358
2359 if (req->r_request_started == 0) /* note request start time */
2360 req->r_request_started = jiffies;
2361
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002362 err = __prepare_send_request(mdsc, req, mds, false);
Sage Weil2f2dc052009-10-06 11:31:09 -07002363 if (!err) {
2364 ceph_msg_get(req->r_request);
2365 ceph_con_send(&session->s_con, req->r_request);
2366 }
2367
2368out_session:
2369 ceph_put_mds_session(session);
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002370finish:
2371 if (err) {
2372 dout("__do_request early error %d\n", err);
2373 req->r_err = err;
2374 complete_request(mdsc, req);
2375 __unregister_request(mdsc, req);
2376 }
Chengguang Xud5548492018-07-28 16:30:48 +08002377 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07002378}
2379
2380/*
2381 * called under mdsc->mutex
2382 */
2383static void __wake_requests(struct ceph_mds_client *mdsc,
2384 struct list_head *head)
2385{
Yan, Zhenged75ec22012-11-19 10:49:06 +08002386 struct ceph_mds_request *req;
2387 LIST_HEAD(tmp_list);
Sage Weil2f2dc052009-10-06 11:31:09 -07002388
Yan, Zhenged75ec22012-11-19 10:49:06 +08002389 list_splice_init(head, &tmp_list);
2390
2391 while (!list_empty(&tmp_list)) {
2392 req = list_entry(tmp_list.next,
2393 struct ceph_mds_request, r_wait);
Sage Weil2f2dc052009-10-06 11:31:09 -07002394 list_del_init(&req->r_wait);
Sage Weil7971bd92013-05-01 21:15:58 -07002395 dout(" wake request %p tid %llu\n", req, req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002396 __do_request(mdsc, req);
2397 }
2398}
2399
2400/*
2401 * Wake up threads with requests pending for @mds, so that they can
Sage Weil29790f22010-03-18 14:45:05 -07002402 * resubmit their requests to a possibly different mds.
Sage Weil2f2dc052009-10-06 11:31:09 -07002403 */
Sage Weil29790f22010-03-18 14:45:05 -07002404static void kick_requests(struct ceph_mds_client *mdsc, int mds)
Sage Weil2f2dc052009-10-06 11:31:09 -07002405{
Sage Weil44ca18f2010-02-15 12:08:46 -08002406 struct ceph_mds_request *req;
Yan, Zheng282c1052014-07-30 10:12:47 +08002407 struct rb_node *p = rb_first(&mdsc->request_tree);
Sage Weil2f2dc052009-10-06 11:31:09 -07002408
2409 dout("kick_requests mds%d\n", mds);
Yan, Zheng282c1052014-07-30 10:12:47 +08002410 while (p) {
Sage Weil44ca18f2010-02-15 12:08:46 -08002411 req = rb_entry(p, struct ceph_mds_request, r_node);
Yan, Zheng282c1052014-07-30 10:12:47 +08002412 p = rb_next(p);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002413 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Sage Weil44ca18f2010-02-15 12:08:46 -08002414 continue;
Yan, Zheng3de22be2015-02-04 14:26:22 +08002415 if (req->r_attempts > 0)
2416 continue; /* only new requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08002417 if (req->r_session &&
2418 req->r_session->s_mds == mds) {
2419 dout(" kicking tid %llu\n", req->r_tid);
Yan, Zheng03974e82014-09-11 14:28:56 +08002420 list_del_init(&req->r_wait);
Sage Weil44ca18f2010-02-15 12:08:46 -08002421 __do_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002422 }
2423 }
2424}
2425
2426void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
2427 struct ceph_mds_request *req)
2428{
2429 dout("submit_request on %p\n", req);
2430 mutex_lock(&mdsc->mutex);
2431 __register_request(mdsc, req, NULL);
2432 __do_request(mdsc, req);
2433 mutex_unlock(&mdsc->mutex);
2434}
2435
2436/*
2437 * Synchrously perform an mds request. Take care of all of the
2438 * session setup, forwarding, retry details.
2439 */
2440int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
2441 struct inode *dir,
2442 struct ceph_mds_request *req)
2443{
2444 int err;
2445
2446 dout("do_request on %p\n", req);
2447
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002448 /* take CAP_PIN refs for r_inode, r_parent, r_old_dentry */
Sage Weil2f2dc052009-10-06 11:31:09 -07002449 if (req->r_inode)
2450 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002451 if (req->r_parent)
2452 ceph_get_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
Sage Weil844d87c2013-02-05 13:40:09 -08002453 if (req->r_old_dentry_dir)
Sage Weil41b02e12011-07-26 11:31:14 -07002454 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
2455 CEPH_CAP_PIN);
Sage Weil2f2dc052009-10-06 11:31:09 -07002456
2457 /* issue */
2458 mutex_lock(&mdsc->mutex);
2459 __register_request(mdsc, req, dir);
2460 __do_request(mdsc, req);
2461
Sage Weile1518c72010-05-13 11:19:06 -07002462 if (req->r_err) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002463 err = req->r_err;
Sage Weile1518c72010-05-13 11:19:06 -07002464 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07002465 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002466
Sage Weile1518c72010-05-13 11:19:06 -07002467 /* wait */
2468 mutex_unlock(&mdsc->mutex);
2469 dout("do_request waiting\n");
Ilya Dryomov5be73032015-05-19 12:05:38 +03002470 if (!req->r_timeout && req->r_wait_for_completion) {
Yan, Zheng9280be22014-10-14 10:33:35 +08002471 err = req->r_wait_for_completion(mdsc, req);
Sage Weile1518c72010-05-13 11:19:06 -07002472 } else {
Ilya Dryomov5be73032015-05-19 12:05:38 +03002473 long timeleft = wait_for_completion_killable_timeout(
2474 &req->r_completion,
2475 ceph_timeout_jiffies(req->r_timeout));
2476 if (timeleft > 0)
2477 err = 0;
2478 else if (!timeleft)
2479 err = -EIO; /* timed out */
2480 else
2481 err = timeleft; /* killed */
Sage Weile1518c72010-05-13 11:19:06 -07002482 }
2483 dout("do_request waited, got %d\n", err);
2484 mutex_lock(&mdsc->mutex);
2485
2486 /* only abort if we didn't race with a real reply */
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002487 if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
Sage Weile1518c72010-05-13 11:19:06 -07002488 err = le32_to_cpu(req->r_reply_info.head->result);
2489 } else if (err < 0) {
2490 dout("aborted request %lld with %d\n", req->r_tid, err);
Sage Weilb4556392010-05-13 12:01:13 -07002491
2492 /*
2493 * ensure we aren't running concurrently with
2494 * ceph_fill_trace or ceph_readdir_prepopulate, which
2495 * rely on locks (dir mutex) held by our caller.
2496 */
2497 mutex_lock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07002498 req->r_err = err;
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002499 set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
Sage Weilb4556392010-05-13 12:01:13 -07002500 mutex_unlock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07002501
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002502 if (req->r_parent &&
Sage Weil167c9e32010-05-14 10:02:57 -07002503 (req->r_op & CEPH_MDS_OP_WRITE))
2504 ceph_invalidate_dir_request(req);
Sage Weile1518c72010-05-13 11:19:06 -07002505 } else {
2506 err = req->r_err;
2507 }
2508
2509out:
2510 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002511 dout("do_request %p done, result %d\n", req, err);
2512 return err;
2513}
2514
2515/*
Yan, Zheng2f276c52013-03-13 19:44:32 +08002516 * Invalidate dir's completeness, dentry lease state on an aborted MDS
Sage Weil167c9e32010-05-14 10:02:57 -07002517 * namespace request.
2518 */
2519void ceph_invalidate_dir_request(struct ceph_mds_request *req)
2520{
Yan, Zheng8d8f3712017-11-24 11:51:32 +08002521 struct inode *dir = req->r_parent;
2522 struct inode *old_dir = req->r_old_dentry_dir;
Sage Weil167c9e32010-05-14 10:02:57 -07002523
Yan, Zheng8d8f3712017-11-24 11:51:32 +08002524 dout("invalidate_dir_request %p %p (complete, lease(s))\n", dir, old_dir);
Sage Weil167c9e32010-05-14 10:02:57 -07002525
Yan, Zheng8d8f3712017-11-24 11:51:32 +08002526 ceph_dir_clear_complete(dir);
2527 if (old_dir)
2528 ceph_dir_clear_complete(old_dir);
Sage Weil167c9e32010-05-14 10:02:57 -07002529 if (req->r_dentry)
2530 ceph_invalidate_dentry_lease(req->r_dentry);
2531 if (req->r_old_dentry)
2532 ceph_invalidate_dentry_lease(req->r_old_dentry);
2533}
2534
2535/*
Sage Weil2f2dc052009-10-06 11:31:09 -07002536 * Handle mds reply.
2537 *
2538 * We take the session mutex and parse and process the reply immediately.
2539 * This preserves the logical ordering of replies, capabilities, etc., sent
2540 * by the MDS as they are applied to our local cache.
2541 */
2542static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2543{
2544 struct ceph_mds_client *mdsc = session->s_mdsc;
2545 struct ceph_mds_request *req;
2546 struct ceph_mds_reply_head *head = msg->front.iov_base;
2547 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
Yan, Zheng982d6012014-12-23 15:30:54 +08002548 struct ceph_snap_realm *realm;
Sage Weil2f2dc052009-10-06 11:31:09 -07002549 u64 tid;
2550 int err, result;
Sage Weil2600d2d2010-02-22 15:12:16 -08002551 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002552
Sage Weil2f2dc052009-10-06 11:31:09 -07002553 if (msg->front.iov_len < sizeof(*head)) {
2554 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002555 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002556 return;
2557 }
2558
2559 /* get request, session */
Sage Weil6df058c2009-12-22 11:24:33 -08002560 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002561 mutex_lock(&mdsc->mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02002562 req = lookup_get_request(mdsc, tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002563 if (!req) {
2564 dout("handle_reply on unknown tid %llu\n", tid);
2565 mutex_unlock(&mdsc->mutex);
2566 return;
2567 }
2568 dout("handle_reply %p\n", req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002569
2570 /* correct session? */
Sage Weild96d6042010-03-20 20:50:58 -07002571 if (req->r_session != session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002572 pr_err("mdsc_handle_reply got %llu on session mds%d"
2573 " not mds%d\n", tid, session->s_mds,
2574 req->r_session ? req->r_session->s_mds : -1);
2575 mutex_unlock(&mdsc->mutex);
2576 goto out;
2577 }
2578
2579 /* dup? */
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002580 if ((test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags) && !head->safe) ||
2581 (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags) && head->safe)) {
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07002582 pr_warn("got a dup %s reply on %llu from mds%d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002583 head->safe ? "safe" : "unsafe", tid, mds);
2584 mutex_unlock(&mdsc->mutex);
2585 goto out;
2586 }
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002587 if (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags)) {
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07002588 pr_warn("got unsafe after safe on %llu from mds%d\n",
Sage Weil85792d0d2010-05-13 09:06:02 -07002589 tid, mds);
2590 mutex_unlock(&mdsc->mutex);
2591 goto out;
2592 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002593
2594 result = le32_to_cpu(head->result);
2595
2596 /*
Greg Farnume55b71f2010-06-22 15:58:01 -07002597 * Handle an ESTALE
2598 * if we're not talking to the authority, send to them
2599 * if the authority has changed while we weren't looking,
2600 * send to new authority
2601 * Otherwise we just have to return an ESTALE
Sage Weil2f2dc052009-10-06 11:31:09 -07002602 */
2603 if (result == -ESTALE) {
Chengguang Xu4c069a52018-01-30 16:29:17 +08002604 dout("got ESTALE on request %llu\n", req->r_tid);
Yan, Zheng51da8e82014-07-14 10:48:29 +08002605 req->r_resend_mds = -1;
Yan, Zhengca18bed2013-11-22 14:21:44 +08002606 if (req->r_direct_mode != USE_AUTH_MDS) {
Chengguang Xu4c069a52018-01-30 16:29:17 +08002607 dout("not using auth, setting for that now\n");
Greg Farnume55b71f2010-06-22 15:58:01 -07002608 req->r_direct_mode = USE_AUTH_MDS;
Sage Weil2f2dc052009-10-06 11:31:09 -07002609 __do_request(mdsc, req);
2610 mutex_unlock(&mdsc->mutex);
2611 goto out;
Greg Farnume55b71f2010-06-22 15:58:01 -07002612 } else {
Yan, Zhengca18bed2013-11-22 14:21:44 +08002613 int mds = __choose_mds(mdsc, req);
2614 if (mds >= 0 && mds != req->r_session->s_mds) {
Chengguang Xu4c069a52018-01-30 16:29:17 +08002615 dout("but auth changed, so resending\n");
Greg Farnume55b71f2010-06-22 15:58:01 -07002616 __do_request(mdsc, req);
2617 mutex_unlock(&mdsc->mutex);
2618 goto out;
2619 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002620 }
Chengguang Xu4c069a52018-01-30 16:29:17 +08002621 dout("have to return ESTALE on request %llu\n", req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002622 }
2623
Greg Farnume55b71f2010-06-22 15:58:01 -07002624
Sage Weil2f2dc052009-10-06 11:31:09 -07002625 if (head->safe) {
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002626 set_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags);
Sage Weil2f2dc052009-10-06 11:31:09 -07002627 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002628
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002629 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002630 /*
2631 * We already handled the unsafe response, now do the
2632 * cleanup. No need to examine the response; the MDS
2633 * doesn't include any result info in the safe
2634 * response. And even if it did, there is nothing
2635 * useful we could do with a revised return value.
2636 */
2637 dout("got safe reply %llu, mds%d\n", tid, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002638
2639 /* last unsafe request during umount? */
Sage Weil44ca18f2010-02-15 12:08:46 -08002640 if (mdsc->stopping && !__get_oldest_req(mdsc))
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002641 complete_all(&mdsc->safe_umount_waiters);
Sage Weil2f2dc052009-10-06 11:31:09 -07002642 mutex_unlock(&mdsc->mutex);
2643 goto out;
2644 }
Sage Weile1518c72010-05-13 11:19:06 -07002645 } else {
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002646 set_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags);
Sage Weil2f2dc052009-10-06 11:31:09 -07002647 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
Yan, Zheng4c06ace2015-10-27 17:18:00 +08002648 if (req->r_unsafe_dir) {
2649 struct ceph_inode_info *ci =
2650 ceph_inode(req->r_unsafe_dir);
2651 spin_lock(&ci->i_unsafe_lock);
2652 list_add_tail(&req->r_unsafe_dir_item,
2653 &ci->i_unsafe_dirops);
2654 spin_unlock(&ci->i_unsafe_lock);
2655 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002656 }
2657
2658 dout("handle_reply tid %lld result %d\n", tid, result);
2659 rinfo = &req->r_reply_info;
Sage Weil14303d22010-12-14 17:37:52 -08002660 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
Sage Weil2f2dc052009-10-06 11:31:09 -07002661 mutex_unlock(&mdsc->mutex);
2662
2663 mutex_lock(&session->s_mutex);
2664 if (err < 0) {
Herb Shiu25933ab2010-12-01 14:14:38 -08002665 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002666 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002667 goto out_err;
2668 }
2669
2670 /* snap trace */
Yan, Zheng982d6012014-12-23 15:30:54 +08002671 realm = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07002672 if (rinfo->snapblob_len) {
2673 down_write(&mdsc->snap_rwsem);
2674 ceph_update_snap_trace(mdsc, rinfo->snapblob,
Yan, Zheng982d6012014-12-23 15:30:54 +08002675 rinfo->snapblob + rinfo->snapblob_len,
2676 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP,
2677 &realm);
Sage Weil2f2dc052009-10-06 11:31:09 -07002678 downgrade_write(&mdsc->snap_rwsem);
2679 } else {
2680 down_read(&mdsc->snap_rwsem);
2681 }
2682
2683 /* insert trace into our cache */
Sage Weilb4556392010-05-13 12:01:13 -07002684 mutex_lock(&req->r_fill_mutex);
Yan, Zheng315f2402016-03-07 10:34:50 +08002685 current->journal_info = req;
Jeff Laytonf5a03b02017-01-31 11:06:13 -05002686 err = ceph_fill_trace(mdsc->fsc->sb, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002687 if (err == 0) {
Sam Lang6e8575f2012-12-28 09:56:46 -08002688 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
Yan, Zheng81c6aea2013-09-18 09:44:13 +08002689 req->r_op == CEPH_MDS_OP_LSSNAP))
Sage Weil2f2dc052009-10-06 11:31:09 -07002690 ceph_readdir_prepopulate(req, req->r_session);
Yehuda Sadeh37151662010-06-17 16:16:12 -07002691 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
Sage Weil2f2dc052009-10-06 11:31:09 -07002692 }
Yan, Zheng315f2402016-03-07 10:34:50 +08002693 current->journal_info = NULL;
Sage Weilb4556392010-05-13 12:01:13 -07002694 mutex_unlock(&req->r_fill_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002695
2696 up_read(&mdsc->snap_rwsem);
Yan, Zheng982d6012014-12-23 15:30:54 +08002697 if (realm)
2698 ceph_put_snap_realm(mdsc, realm);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002699
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002700 if (err == 0 && req->r_target_inode &&
2701 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002702 struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
2703 spin_lock(&ci->i_unsafe_lock);
2704 list_add_tail(&req->r_unsafe_target_item, &ci->i_unsafe_iops);
2705 spin_unlock(&ci->i_unsafe_lock);
2706 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002707out_err:
Sage Weile1518c72010-05-13 11:19:06 -07002708 mutex_lock(&mdsc->mutex);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002709 if (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
Sage Weile1518c72010-05-13 11:19:06 -07002710 if (err) {
2711 req->r_err = err;
2712 } else {
Jianpeng Ma5fdb1382015-08-18 10:30:38 +08002713 req->r_reply = ceph_msg_get(msg);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002714 set_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags);
Sage Weile1518c72010-05-13 11:19:06 -07002715 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002716 } else {
Sage Weile1518c72010-05-13 11:19:06 -07002717 dout("reply arrived after request %lld was aborted\n", tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002718 }
Sage Weile1518c72010-05-13 11:19:06 -07002719 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002720
Sage Weil2f2dc052009-10-06 11:31:09 -07002721 mutex_unlock(&session->s_mutex);
2722
2723 /* kick calling process */
2724 complete_request(mdsc, req);
2725out:
2726 ceph_mdsc_put_request(req);
2727 return;
2728}
2729
2730
2731
2732/*
2733 * handle mds notification that our request has been forwarded.
2734 */
Sage Weil2600d2d2010-02-22 15:12:16 -08002735static void handle_forward(struct ceph_mds_client *mdsc,
2736 struct ceph_mds_session *session,
2737 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002738{
2739 struct ceph_mds_request *req;
Sage Weila1ea7872010-02-23 14:02:44 -08002740 u64 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002741 u32 next_mds;
2742 u32 fwd_seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002743 int err = -EINVAL;
2744 void *p = msg->front.iov_base;
2745 void *end = p + msg->front.iov_len;
Sage Weil2f2dc052009-10-06 11:31:09 -07002746
Sage Weila1ea7872010-02-23 14:02:44 -08002747 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002748 next_mds = ceph_decode_32(&p);
2749 fwd_seq = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07002750
2751 mutex_lock(&mdsc->mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02002752 req = lookup_get_request(mdsc, tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002753 if (!req) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07002754 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002755 goto out; /* dup reply? */
2756 }
2757
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002758 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07002759 dout("forward tid %llu aborted, unregistering\n", tid);
2760 __unregister_request(mdsc, req);
2761 } else if (fwd_seq <= req->r_num_fwd) {
2762 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002763 tid, next_mds, req->r_num_fwd, fwd_seq);
2764 } else {
2765 /* resend. forward race not possible; mds would drop */
Sage Weil2a8e5e32010-05-28 16:43:16 -07002766 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2767 BUG_ON(req->r_err);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002768 BUG_ON(test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags));
Yan, Zheng3de22be2015-02-04 14:26:22 +08002769 req->r_attempts = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002770 req->r_num_fwd = fwd_seq;
2771 req->r_resend_mds = next_mds;
2772 put_request_session(req);
2773 __do_request(mdsc, req);
2774 }
2775 ceph_mdsc_put_request(req);
2776out:
2777 mutex_unlock(&mdsc->mutex);
2778 return;
2779
2780bad:
2781 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2782}
2783
Yan, Zheng84bf3952018-12-21 17:41:39 +08002784static int __decode_and_drop_session_metadata(void **p, void *end)
2785{
2786 /* map<string,string> */
2787 u32 n;
2788 ceph_decode_32_safe(p, end, n, bad);
2789 while (n-- > 0) {
2790 u32 len;
2791 ceph_decode_32_safe(p, end, len, bad);
2792 ceph_decode_need(p, end, len, bad);
2793 *p += len;
2794 ceph_decode_32_safe(p, end, len, bad);
2795 ceph_decode_need(p, end, len, bad);
2796 *p += len;
2797 }
2798 return 0;
2799bad:
2800 return -1;
2801}
2802
Sage Weil2f2dc052009-10-06 11:31:09 -07002803/*
2804 * handle a mds session control message
2805 */
2806static void handle_session(struct ceph_mds_session *session,
2807 struct ceph_msg *msg)
2808{
2809 struct ceph_mds_client *mdsc = session->s_mdsc;
Yan, Zheng84bf3952018-12-21 17:41:39 +08002810 int mds = session->s_mds;
2811 int msg_version = le16_to_cpu(msg->hdr.version);
2812 void *p = msg->front.iov_base;
2813 void *end = p + msg->front.iov_len;
2814 struct ceph_mds_session_head *h;
Sage Weil2f2dc052009-10-06 11:31:09 -07002815 u32 op;
2816 u64 seq;
Yan, Zheng84bf3952018-12-21 17:41:39 +08002817 unsigned long features = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002818 int wake = 0;
2819
Sage Weil2f2dc052009-10-06 11:31:09 -07002820 /* decode */
Yan, Zheng84bf3952018-12-21 17:41:39 +08002821 ceph_decode_need(&p, end, sizeof(*h), bad);
2822 h = p;
2823 p += sizeof(*h);
2824
Sage Weil2f2dc052009-10-06 11:31:09 -07002825 op = le32_to_cpu(h->op);
2826 seq = le64_to_cpu(h->seq);
2827
Yan, Zheng84bf3952018-12-21 17:41:39 +08002828 if (msg_version >= 3) {
2829 u32 len;
2830 /* version >= 2, metadata */
2831 if (__decode_and_drop_session_metadata(&p, end) < 0)
2832 goto bad;
2833 /* version >= 3, feature bits */
2834 ceph_decode_32_safe(&p, end, len, bad);
2835 ceph_decode_need(&p, end, len, bad);
2836 memcpy(&features, p, min_t(size_t, len, sizeof(features)));
2837 p += len;
2838 }
2839
Sage Weil2f2dc052009-10-06 11:31:09 -07002840 mutex_lock(&mdsc->mutex);
Yan, Zheng0a07fc82017-03-29 15:30:24 +08002841 if (op == CEPH_SESSION_CLOSE) {
2842 get_session(session);
Sage Weil2600d2d2010-02-22 15:12:16 -08002843 __unregister_session(mdsc, session);
Yan, Zheng0a07fc82017-03-29 15:30:24 +08002844 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002845 /* FIXME: this ttl calculation is generous */
2846 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2847 mutex_unlock(&mdsc->mutex);
2848
2849 mutex_lock(&session->s_mutex);
2850
2851 dout("handle_session mds%d %s %p state %s seq %llu\n",
2852 mds, ceph_session_op_name(op), session,
John Spraya687eca2014-09-19 13:51:08 +01002853 ceph_session_state_name(session->s_state), seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002854
2855 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2856 session->s_state = CEPH_MDS_SESSION_OPEN;
2857 pr_info("mds%d came back\n", session->s_mds);
2858 }
2859
2860 switch (op) {
2861 case CEPH_SESSION_OPEN:
Sage Weil29790f22010-03-18 14:45:05 -07002862 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2863 pr_info("mds%d reconnect success\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002864 session->s_state = CEPH_MDS_SESSION_OPEN;
Yan, Zheng84bf3952018-12-21 17:41:39 +08002865 session->s_features = features;
Sage Weil2f2dc052009-10-06 11:31:09 -07002866 renewed_caps(mdsc, session, 0);
2867 wake = 1;
2868 if (mdsc->stopping)
2869 __close_session(mdsc, session);
2870 break;
2871
2872 case CEPH_SESSION_RENEWCAPS:
2873 if (session->s_renew_seq == seq)
2874 renewed_caps(mdsc, session, 1);
2875 break;
2876
2877 case CEPH_SESSION_CLOSE:
Sage Weil29790f22010-03-18 14:45:05 -07002878 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2879 pr_info("mds%d reconnect denied\n", session->s_mds);
Yan, Zheng1c841a92015-03-24 20:15:36 +08002880 cleanup_session_requests(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002881 remove_session_caps(session);
Yan, Zheng656e4382014-09-11 14:25:18 +08002882 wake = 2; /* for good measure */
Sage Weilf3c60c52010-08-11 14:51:23 -07002883 wake_up_all(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07002884 break;
2885
2886 case CEPH_SESSION_STALE:
2887 pr_info("mds%d caps went stale, renewing\n",
2888 session->s_mds);
Alex Elderd8fb02a2012-01-12 17:48:10 -08002889 spin_lock(&session->s_gen_ttl_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002890 session->s_cap_gen++;
Alex Elder1ce208a2012-01-12 17:48:11 -08002891 session->s_cap_ttl = jiffies - 1;
Alex Elderd8fb02a2012-01-12 17:48:10 -08002892 spin_unlock(&session->s_gen_ttl_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002893 send_renew_caps(mdsc, session);
2894 break;
2895
2896 case CEPH_SESSION_RECALL_STATE:
Zhi Zhange30ee582018-01-24 21:24:33 +08002897 ceph_trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
Sage Weil2f2dc052009-10-06 11:31:09 -07002898 break;
2899
Yan, Zheng186e4f72013-11-22 14:48:37 +08002900 case CEPH_SESSION_FLUSHMSG:
2901 send_flushmsg_ack(mdsc, session, seq);
2902 break;
2903
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08002904 case CEPH_SESSION_FORCE_RO:
2905 dout("force_session_readonly %p\n", session);
2906 spin_lock(&session->s_cap_lock);
2907 session->s_readonly = true;
2908 spin_unlock(&session->s_cap_lock);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08002909 wake_up_session_caps(session, FORCE_RO);
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08002910 break;
2911
Yan, Zhengfcff4152016-09-14 16:39:51 +08002912 case CEPH_SESSION_REJECT:
2913 WARN_ON(session->s_state != CEPH_MDS_SESSION_OPENING);
2914 pr_info("mds%d rejected session\n", session->s_mds);
2915 session->s_state = CEPH_MDS_SESSION_REJECTED;
2916 cleanup_session_requests(mdsc, session);
2917 remove_session_caps(session);
2918 wake = 2; /* for good measure */
2919 break;
2920
Sage Weil2f2dc052009-10-06 11:31:09 -07002921 default:
2922 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2923 WARN_ON(1);
2924 }
2925
2926 mutex_unlock(&session->s_mutex);
2927 if (wake) {
2928 mutex_lock(&mdsc->mutex);
2929 __wake_requests(mdsc, &session->s_waiting);
Yan, Zheng656e4382014-09-11 14:25:18 +08002930 if (wake == 2)
2931 kick_requests(mdsc, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002932 mutex_unlock(&mdsc->mutex);
2933 }
Yan, Zheng0a07fc82017-03-29 15:30:24 +08002934 if (op == CEPH_SESSION_CLOSE)
2935 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002936 return;
2937
2938bad:
2939 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2940 (int)msg->front.iov_len);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002941 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002942 return;
2943}
2944
2945
2946/*
2947 * called under session->mutex.
2948 */
2949static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2950 struct ceph_mds_session *session)
2951{
2952 struct ceph_mds_request *req, *nreq;
Yan, Zheng3de22be2015-02-04 14:26:22 +08002953 struct rb_node *p;
Sage Weil2f2dc052009-10-06 11:31:09 -07002954 int err;
2955
2956 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2957
2958 mutex_lock(&mdsc->mutex);
2959 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002960 err = __prepare_send_request(mdsc, req, session->s_mds, true);
Sage Weil2f2dc052009-10-06 11:31:09 -07002961 if (!err) {
2962 ceph_msg_get(req->r_request);
2963 ceph_con_send(&session->s_con, req->r_request);
2964 }
2965 }
Yan, Zheng3de22be2015-02-04 14:26:22 +08002966
2967 /*
2968 * also re-send old requests when MDS enters reconnect stage. So that MDS
2969 * can process completed request in clientreplay stage.
2970 */
2971 p = rb_first(&mdsc->request_tree);
2972 while (p) {
2973 req = rb_entry(p, struct ceph_mds_request, r_node);
2974 p = rb_next(p);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002975 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Yan, Zheng3de22be2015-02-04 14:26:22 +08002976 continue;
2977 if (req->r_attempts == 0)
2978 continue; /* only old requests */
2979 if (req->r_session &&
2980 req->r_session->s_mds == session->s_mds) {
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002981 err = __prepare_send_request(mdsc, req,
2982 session->s_mds, true);
Yan, Zheng3de22be2015-02-04 14:26:22 +08002983 if (!err) {
2984 ceph_msg_get(req->r_request);
2985 ceph_con_send(&session->s_con, req->r_request);
2986 }
2987 }
2988 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002989 mutex_unlock(&mdsc->mutex);
2990}
2991
Yan, Zheng81c5a142019-01-01 16:28:33 +08002992static int send_reconnect_partial(struct ceph_reconnect_state *recon_state)
2993{
2994 struct ceph_msg *reply;
2995 struct ceph_pagelist *_pagelist;
2996 struct page *page;
2997 __le32 *addr;
2998 int err = -ENOMEM;
2999
3000 if (!recon_state->allow_multi)
3001 return -ENOSPC;
3002
3003 /* can't handle message that contains both caps and realm */
3004 BUG_ON(!recon_state->nr_caps == !recon_state->nr_realms);
3005
3006 /* pre-allocate new pagelist */
3007 _pagelist = ceph_pagelist_alloc(GFP_NOFS);
3008 if (!_pagelist)
3009 return -ENOMEM;
3010
3011 reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false);
3012 if (!reply)
3013 goto fail_msg;
3014
3015 /* placeholder for nr_caps */
3016 err = ceph_pagelist_encode_32(_pagelist, 0);
3017 if (err < 0)
3018 goto fail;
3019
3020 if (recon_state->nr_caps) {
3021 /* currently encoding caps */
3022 err = ceph_pagelist_encode_32(recon_state->pagelist, 0);
3023 if (err)
3024 goto fail;
3025 } else {
3026 /* placeholder for nr_realms (currently encoding relams) */
3027 err = ceph_pagelist_encode_32(_pagelist, 0);
3028 if (err < 0)
3029 goto fail;
3030 }
3031
3032 err = ceph_pagelist_encode_8(recon_state->pagelist, 1);
3033 if (err)
3034 goto fail;
3035
3036 page = list_first_entry(&recon_state->pagelist->head, struct page, lru);
3037 addr = kmap_atomic(page);
3038 if (recon_state->nr_caps) {
3039 /* currently encoding caps */
3040 *addr = cpu_to_le32(recon_state->nr_caps);
3041 } else {
3042 /* currently encoding relams */
3043 *(addr + 1) = cpu_to_le32(recon_state->nr_realms);
3044 }
3045 kunmap_atomic(addr);
3046
3047 reply->hdr.version = cpu_to_le16(5);
3048 reply->hdr.compat_version = cpu_to_le16(4);
3049
3050 reply->hdr.data_len = cpu_to_le32(recon_state->pagelist->length);
3051 ceph_msg_data_add_pagelist(reply, recon_state->pagelist);
3052
3053 ceph_con_send(&recon_state->session->s_con, reply);
3054 ceph_pagelist_release(recon_state->pagelist);
3055
3056 recon_state->pagelist = _pagelist;
3057 recon_state->nr_caps = 0;
3058 recon_state->nr_realms = 0;
3059 recon_state->msg_version = 5;
3060 return 0;
3061fail:
3062 ceph_msg_put(reply);
3063fail_msg:
3064 ceph_pagelist_release(_pagelist);
3065 return err;
3066}
3067
Sage Weil2f2dc052009-10-06 11:31:09 -07003068/*
3069 * Encode information about a cap for a reconnect with the MDS.
3070 */
Sage Weil2f2dc052009-10-06 11:31:09 -07003071static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
3072 void *arg)
3073{
Sage Weil20cb34a2010-05-12 15:21:32 -07003074 union {
3075 struct ceph_mds_cap_reconnect v2;
3076 struct ceph_mds_cap_reconnect_v1 v1;
3077 } rec;
Yan, Zhengb3f8d682017-09-11 10:58:55 +08003078 struct ceph_inode_info *ci = cap->ci;
Sage Weil20cb34a2010-05-12 15:21:32 -07003079 struct ceph_reconnect_state *recon_state = arg;
3080 struct ceph_pagelist *pagelist = recon_state->pagelist;
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003081 int err;
Yan, Zheng3469ed02016-07-05 09:32:31 +08003082 u64 snap_follows;
Sage Weil2f2dc052009-10-06 11:31:09 -07003083
Sage Weil2f2dc052009-10-06 11:31:09 -07003084 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
3085 inode, ceph_vinop(inode), cap, cap->cap_id,
3086 ceph_cap_string(cap->issued));
Sage Weil2f2dc052009-10-06 11:31:09 -07003087
Sage Weilbe655592011-11-30 09:47:09 -08003088 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07003089 cap->seq = 0; /* reset cap seq */
3090 cap->issue_seq = 0; /* and issue_seq */
Yan, Zheng667ca052013-05-31 16:25:36 +08003091 cap->mseq = 0; /* and migrate_seq */
Yan, Zheng99a9c272013-09-22 11:08:14 +08003092 cap->cap_gen = cap->session->s_cap_gen;
Sage Weil20cb34a2010-05-12 15:21:32 -07003093
Yan, Zheng121f22a2016-07-04 22:05:18 +08003094 if (recon_state->msg_version >= 2) {
Sage Weil20cb34a2010-05-12 15:21:32 -07003095 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
3096 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
3097 rec.v2.issued = cpu_to_le32(cap->issued);
3098 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003099 rec.v2.pathbase = 0;
Jeff Laytonec1dff22017-10-31 15:51:14 -04003100 rec.v2.flock_len = (__force __le32)
3101 ((ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) ? 0 : 1);
Sage Weil20cb34a2010-05-12 15:21:32 -07003102 } else {
3103 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
3104 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
3105 rec.v1.issued = cpu_to_le32(cap->issued);
3106 rec.v1.size = cpu_to_le64(inode->i_size);
Arnd Bergmann9bbeab42018-07-13 22:18:36 +02003107 ceph_encode_timespec64(&rec.v1.mtime, &inode->i_mtime);
3108 ceph_encode_timespec64(&rec.v1.atime, &inode->i_atime);
Sage Weil20cb34a2010-05-12 15:21:32 -07003109 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003110 rec.v1.pathbase = 0;
Sage Weil20cb34a2010-05-12 15:21:32 -07003111 }
Yan, Zheng3469ed02016-07-05 09:32:31 +08003112
3113 if (list_empty(&ci->i_cap_snaps)) {
Yan, Zheng92776fd2017-08-16 21:42:39 +08003114 snap_follows = ci->i_head_snapc ? ci->i_head_snapc->seq : 0;
Yan, Zheng3469ed02016-07-05 09:32:31 +08003115 } else {
3116 struct ceph_cap_snap *capsnap =
3117 list_first_entry(&ci->i_cap_snaps,
3118 struct ceph_cap_snap, ci_item);
3119 snap_follows = capsnap->follows;
Sage Weil93cea5b2009-12-23 12:21:51 -08003120 }
Sage Weilbe655592011-11-30 09:47:09 -08003121 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07003122
Yan, Zheng121f22a2016-07-04 22:05:18 +08003123 if (recon_state->msg_version >= 2) {
Greg Farnum40819f62010-08-02 15:34:23 -07003124 int num_fcntl_locks, num_flock_locks;
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003125 struct ceph_filelock *flocks = NULL;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003126 size_t struct_len, total_len = sizeof(u64);
Yan, Zheng121f22a2016-07-04 22:05:18 +08003127 u8 struct_v = 0;
Greg Farnum40819f62010-08-02 15:34:23 -07003128
Jim Schutt39be95e2013-05-15 13:03:35 -05003129encode_again:
Yan, Zhengb3f8d682017-09-11 10:58:55 +08003130 if (rec.v2.flock_len) {
3131 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
3132 } else {
3133 num_fcntl_locks = 0;
3134 num_flock_locks = 0;
3135 }
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003136 if (num_fcntl_locks + num_flock_locks > 0) {
Kees Cook6da2ec52018-06-12 13:55:00 -07003137 flocks = kmalloc_array(num_fcntl_locks + num_flock_locks,
3138 sizeof(struct ceph_filelock),
3139 GFP_NOFS);
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003140 if (!flocks) {
3141 err = -ENOMEM;
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003142 goto out_err;
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003143 }
3144 err = ceph_encode_locks_to_buffer(inode, flocks,
3145 num_fcntl_locks,
3146 num_flock_locks);
3147 if (err) {
3148 kfree(flocks);
3149 flocks = NULL;
3150 if (err == -ENOSPC)
3151 goto encode_again;
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003152 goto out_err;
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003153 }
3154 } else {
Jim Schutt39be95e2013-05-15 13:03:35 -05003155 kfree(flocks);
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003156 flocks = NULL;
Jim Schutt39be95e2013-05-15 13:03:35 -05003157 }
Yan, Zheng121f22a2016-07-04 22:05:18 +08003158
3159 if (recon_state->msg_version >= 3) {
3160 /* version, compat_version and struct_len */
Yan, Zheng81c5a142019-01-01 16:28:33 +08003161 total_len += 2 * sizeof(u8) + sizeof(u32);
Yan, Zheng3469ed02016-07-05 09:32:31 +08003162 struct_v = 2;
Yan, Zheng121f22a2016-07-04 22:05:18 +08003163 }
Jim Schutt39be95e2013-05-15 13:03:35 -05003164 /*
3165 * number of encoded locks is stable, so copy to pagelist
3166 */
Yan, Zheng121f22a2016-07-04 22:05:18 +08003167 struct_len = 2 * sizeof(u32) +
3168 (num_fcntl_locks + num_flock_locks) *
3169 sizeof(struct ceph_filelock);
3170 rec.v2.flock_len = cpu_to_le32(struct_len);
3171
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003172 struct_len += sizeof(u32) + sizeof(rec.v2);
Yan, Zheng121f22a2016-07-04 22:05:18 +08003173
Yan, Zheng3469ed02016-07-05 09:32:31 +08003174 if (struct_v >= 2)
3175 struct_len += sizeof(u64); /* snap_follows */
3176
Yan, Zheng121f22a2016-07-04 22:05:18 +08003177 total_len += struct_len;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003178
3179 if (pagelist->length + total_len > RECONNECT_MAX_SIZE) {
3180 err = send_reconnect_partial(recon_state);
3181 if (err)
3182 goto out_freeflocks;
3183 pagelist = recon_state->pagelist;
Yan, Zheng121f22a2016-07-04 22:05:18 +08003184 }
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003185
Yan, Zheng81c5a142019-01-01 16:28:33 +08003186 err = ceph_pagelist_reserve(pagelist, total_len);
3187 if (err)
3188 goto out_freeflocks;
3189
3190 ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003191 if (recon_state->msg_version >= 3) {
3192 ceph_pagelist_encode_8(pagelist, struct_v);
3193 ceph_pagelist_encode_8(pagelist, 1);
3194 ceph_pagelist_encode_32(pagelist, struct_len);
3195 }
3196 ceph_pagelist_encode_string(pagelist, NULL, 0);
3197 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v2));
3198 ceph_locks_to_pagelist(flocks, pagelist,
3199 num_fcntl_locks, num_flock_locks);
3200 if (struct_v >= 2)
3201 ceph_pagelist_encode_64(pagelist, snap_follows);
Yan, Zheng81c5a142019-01-01 16:28:33 +08003202out_freeflocks:
Jim Schutt39be95e2013-05-15 13:03:35 -05003203 kfree(flocks);
Sage Weil3612abb2010-09-07 15:59:27 -07003204 } else {
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003205 u64 pathbase = 0;
3206 int pathlen = 0;
3207 char *path = NULL;
3208 struct dentry *dentry;
3209
3210 dentry = d_find_alias(inode);
3211 if (dentry) {
3212 path = ceph_mdsc_build_path(dentry,
3213 &pathlen, &pathbase, 0);
3214 dput(dentry);
3215 if (IS_ERR(path)) {
3216 err = PTR_ERR(path);
3217 goto out_err;
3218 }
3219 rec.v1.pathbase = cpu_to_le64(pathbase);
Yan, Zheng121f22a2016-07-04 22:05:18 +08003220 }
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003221
3222 err = ceph_pagelist_reserve(pagelist,
Yan, Zheng81c5a142019-01-01 16:28:33 +08003223 sizeof(u64) + sizeof(u32) +
3224 pathlen + sizeof(rec.v1));
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003225 if (err) {
Yan, Zheng81c5a142019-01-01 16:28:33 +08003226 goto out_freepath;
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003227 }
3228
Yan, Zheng81c5a142019-01-01 16:28:33 +08003229 ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003230 ceph_pagelist_encode_string(pagelist, path, pathlen);
3231 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v1));
Yan, Zheng81c5a142019-01-01 16:28:33 +08003232out_freepath:
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003233 kfree(path);
Greg Farnum40819f62010-08-02 15:34:23 -07003234 }
Yan, Zheng44c99752013-09-22 10:28:10 +08003235
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003236out_err:
Yan, Zheng81c5a142019-01-01 16:28:33 +08003237 if (err >= 0)
3238 recon_state->nr_caps++;
3239 return err;
3240}
3241
3242static int encode_snap_realms(struct ceph_mds_client *mdsc,
3243 struct ceph_reconnect_state *recon_state)
3244{
3245 struct rb_node *p;
3246 struct ceph_pagelist *pagelist = recon_state->pagelist;
3247 int err = 0;
3248
3249 if (recon_state->msg_version >= 4) {
3250 err = ceph_pagelist_encode_32(pagelist, mdsc->num_snap_realms);
3251 if (err < 0)
3252 goto fail;
3253 }
3254
3255 /*
3256 * snaprealms. we provide mds with the ino, seq (version), and
3257 * parent for all of our realms. If the mds has any newer info,
3258 * it will tell us.
3259 */
3260 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
3261 struct ceph_snap_realm *realm =
3262 rb_entry(p, struct ceph_snap_realm, node);
3263 struct ceph_mds_snaprealm_reconnect sr_rec;
3264
3265 if (recon_state->msg_version >= 4) {
3266 size_t need = sizeof(u8) * 2 + sizeof(u32) +
3267 sizeof(sr_rec);
3268
3269 if (pagelist->length + need > RECONNECT_MAX_SIZE) {
3270 err = send_reconnect_partial(recon_state);
3271 if (err)
3272 goto fail;
3273 pagelist = recon_state->pagelist;
3274 }
3275
3276 err = ceph_pagelist_reserve(pagelist, need);
3277 if (err)
3278 goto fail;
3279
3280 ceph_pagelist_encode_8(pagelist, 1);
3281 ceph_pagelist_encode_8(pagelist, 1);
3282 ceph_pagelist_encode_32(pagelist, sizeof(sr_rec));
3283 }
3284
3285 dout(" adding snap realm %llx seq %lld parent %llx\n",
3286 realm->ino, realm->seq, realm->parent_ino);
3287 sr_rec.ino = cpu_to_le64(realm->ino);
3288 sr_rec.seq = cpu_to_le64(realm->seq);
3289 sr_rec.parent = cpu_to_le64(realm->parent_ino);
3290
3291 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
3292 if (err)
3293 goto fail;
3294
3295 recon_state->nr_realms++;
3296 }
3297fail:
Sage Weil93cea5b2009-12-23 12:21:51 -08003298 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07003299}
3300
3301
3302/*
3303 * If an MDS fails and recovers, clients need to reconnect in order to
3304 * reestablish shared state. This includes all caps issued through
3305 * this session _and_ the snap_realm hierarchy. Because it's not
3306 * clear which snap realms the mds cares about, we send everything we
3307 * know about.. that ensures we'll then get any new info the
3308 * recovering MDS might have.
3309 *
3310 * This is a relatively heavyweight operation, but it's rare.
3311 *
3312 * called with mdsc->mutex held.
3313 */
Sage Weil34b6c852010-05-10 16:31:25 -07003314static void send_mds_reconnect(struct ceph_mds_client *mdsc,
3315 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07003316{
Sage Weil2f2dc052009-10-06 11:31:09 -07003317 struct ceph_msg *reply;
Sage Weil34b6c852010-05-10 16:31:25 -07003318 int mds = session->s_mds;
Sage Weil9abf82b2010-05-10 21:58:38 -07003319 int err = -ENOMEM;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003320 struct ceph_reconnect_state recon_state = {
3321 .session = session,
3322 };
Jeff Laytonc8a96a32017-10-19 08:53:58 -04003323 LIST_HEAD(dispose);
Sage Weil2f2dc052009-10-06 11:31:09 -07003324
Sage Weil34b6c852010-05-10 16:31:25 -07003325 pr_info("mds%d reconnect start\n", mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003326
Yan, Zheng81c5a142019-01-01 16:28:33 +08003327 recon_state.pagelist = ceph_pagelist_alloc(GFP_NOFS);
3328 if (!recon_state.pagelist)
Sage Weil93cea5b2009-12-23 12:21:51 -08003329 goto fail_nopagelist;
Sage Weil93cea5b2009-12-23 12:21:51 -08003330
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003331 reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07003332 if (!reply)
Sage Weil93cea5b2009-12-23 12:21:51 -08003333 goto fail_nomsg;
Sage Weil93cea5b2009-12-23 12:21:51 -08003334
Sage Weil34b6c852010-05-10 16:31:25 -07003335 mutex_lock(&session->s_mutex);
3336 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
3337 session->s_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003338
Sage Weil2f2dc052009-10-06 11:31:09 -07003339 dout("session %p state %s\n", session,
John Spraya687eca2014-09-19 13:51:08 +01003340 ceph_session_state_name(session->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07003341
Yan, Zheng99a9c272013-09-22 11:08:14 +08003342 spin_lock(&session->s_gen_ttl_lock);
3343 session->s_cap_gen++;
3344 spin_unlock(&session->s_gen_ttl_lock);
3345
3346 spin_lock(&session->s_cap_lock);
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08003347 /* don't know if session is readonly */
3348 session->s_readonly = 0;
Yan, Zheng99a9c272013-09-22 11:08:14 +08003349 /*
3350 * notify __ceph_remove_cap() that we are composing cap reconnect.
3351 * If a cap get released before being added to the cap reconnect,
3352 * __ceph_remove_cap() should skip queuing cap release.
3353 */
3354 session->s_cap_reconnect = 1;
Sage Weile01a5942010-05-10 15:36:44 -07003355 /* drop old cap expires; we're about to reestablish that state */
Jeff Laytonc8a96a32017-10-19 08:53:58 -04003356 detach_cap_releases(session, &dispose);
3357 spin_unlock(&session->s_cap_lock);
3358 dispose_cap_releases(mdsc, &dispose);
Sage Weile01a5942010-05-10 15:36:44 -07003359
Yan, Zheng5d233712014-09-10 16:56:23 +08003360 /* trim unused caps to reduce MDS's cache rejoin time */
Yan, Zhengc0bd50e2015-04-07 15:51:08 +08003361 if (mdsc->fsc->sb->s_root)
3362 shrink_dcache_parent(mdsc->fsc->sb->s_root);
Yan, Zheng5d233712014-09-10 16:56:23 +08003363
3364 ceph_con_close(&session->s_con);
3365 ceph_con_open(&session->s_con,
3366 CEPH_ENTITY_TYPE_MDS, mds,
3367 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
3368
3369 /* replay unsafe requests */
3370 replay_unsafe_requests(mdsc, session);
3371
Yan, Zheng81c5a142019-01-01 16:28:33 +08003372 ceph_early_kick_flushing_caps(mdsc, session);
3373
Yan, Zheng5d233712014-09-10 16:56:23 +08003374 down_read(&mdsc->snap_rwsem);
3375
Yan, Zheng81c5a142019-01-01 16:28:33 +08003376 /* placeholder for nr_caps */
3377 err = ceph_pagelist_encode_32(recon_state.pagelist, 0);
Sage Weil93cea5b2009-12-23 12:21:51 -08003378 if (err)
3379 goto fail;
Sage Weil20cb34a2010-05-12 15:21:32 -07003380
Yan, Zheng81c5a142019-01-01 16:28:33 +08003381 if (test_bit(CEPHFS_FEATURE_MULTI_RECONNECT, &session->s_features)) {
Yan, Zheng121f22a2016-07-04 22:05:18 +08003382 recon_state.msg_version = 3;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003383 recon_state.allow_multi = true;
3384 } else if (session->s_con.peer_features & CEPH_FEATURE_MDSENC) {
3385 recon_state.msg_version = 3;
3386 } else {
Ilya Dryomov23c625c2018-11-08 14:55:21 +01003387 recon_state.msg_version = 2;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003388 }
3389 /* trsaverse this session's caps */
Sage Weil20cb34a2010-05-12 15:21:32 -07003390 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
Sage Weil2f2dc052009-10-06 11:31:09 -07003391
Yan, Zheng99a9c272013-09-22 11:08:14 +08003392 spin_lock(&session->s_cap_lock);
3393 session->s_cap_reconnect = 0;
3394 spin_unlock(&session->s_cap_lock);
3395
Yan, Zheng81c5a142019-01-01 16:28:33 +08003396 if (err < 0)
3397 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07003398
Yan, Zheng81c5a142019-01-01 16:28:33 +08003399 /* check if all realms can be encoded into current message */
3400 if (mdsc->num_snap_realms) {
3401 size_t total_len =
3402 recon_state.pagelist->length +
3403 mdsc->num_snap_realms *
3404 sizeof(struct ceph_mds_snaprealm_reconnect);
3405 if (recon_state.msg_version >= 4) {
3406 /* number of realms */
3407 total_len += sizeof(u32);
3408 /* version, compat_version and struct_len */
3409 total_len += mdsc->num_snap_realms *
3410 (2 * sizeof(u8) + sizeof(u32));
3411 }
3412 if (total_len > RECONNECT_MAX_SIZE) {
3413 if (!recon_state.allow_multi) {
3414 err = -ENOSPC;
3415 goto fail;
3416 }
3417 if (recon_state.nr_caps) {
3418 err = send_reconnect_partial(&recon_state);
3419 if (err)
3420 goto fail;
3421 }
3422 recon_state.msg_version = 5;
3423 }
3424 }
3425
3426 err = encode_snap_realms(mdsc, &recon_state);
3427 if (err < 0)
3428 goto fail;
3429
3430 if (recon_state.msg_version >= 5) {
3431 err = ceph_pagelist_encode_8(recon_state.pagelist, 0);
3432 if (err < 0)
Sage Weil93cea5b2009-12-23 12:21:51 -08003433 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07003434 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003435
Yan, Zheng81c5a142019-01-01 16:28:33 +08003436 if (recon_state.nr_caps || recon_state.nr_realms) {
3437 struct page *page =
3438 list_first_entry(&recon_state.pagelist->head,
3439 struct page, lru);
Yan, Zheng44c99752013-09-22 10:28:10 +08003440 __le32 *addr = kmap_atomic(page);
Yan, Zheng81c5a142019-01-01 16:28:33 +08003441 if (recon_state.nr_caps) {
3442 WARN_ON(recon_state.nr_realms != mdsc->num_snap_realms);
3443 *addr = cpu_to_le32(recon_state.nr_caps);
3444 } else if (recon_state.msg_version >= 4) {
3445 *(addr + 1) = cpu_to_le32(recon_state.nr_realms);
3446 }
Yan, Zheng44c99752013-09-22 10:28:10 +08003447 kunmap_atomic(addr);
Alex Elderebf18f42013-03-04 22:29:57 -06003448 }
Yan, Zheng44c99752013-09-22 10:28:10 +08003449
Yan, Zheng81c5a142019-01-01 16:28:33 +08003450 reply->hdr.version = cpu_to_le16(recon_state.msg_version);
3451 if (recon_state.msg_version >= 4)
3452 reply->hdr.compat_version = cpu_to_le16(4);
Yan, Zhenge548e9b2015-06-10 15:17:56 +08003453
Yan, Zheng81c5a142019-01-01 16:28:33 +08003454 reply->hdr.data_len = cpu_to_le32(recon_state.pagelist->length);
3455 ceph_msg_data_add_pagelist(reply, recon_state.pagelist);
Yan, Zhenge548e9b2015-06-10 15:17:56 +08003456
Sage Weil2f2dc052009-10-06 11:31:09 -07003457 ceph_con_send(&session->s_con, reply);
3458
Sage Weil9abf82b2010-05-10 21:58:38 -07003459 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003460
Sage Weil9abf82b2010-05-10 21:58:38 -07003461 mutex_lock(&mdsc->mutex);
3462 __wake_requests(mdsc, &session->s_waiting);
3463 mutex_unlock(&mdsc->mutex);
3464
Sage Weil2f2dc052009-10-06 11:31:09 -07003465 up_read(&mdsc->snap_rwsem);
Yan, Zheng81c5a142019-01-01 16:28:33 +08003466 ceph_pagelist_release(recon_state.pagelist);
Sage Weil2f2dc052009-10-06 11:31:09 -07003467 return;
3468
Sage Weil93cea5b2009-12-23 12:21:51 -08003469fail:
Sage Weil2f2dc052009-10-06 11:31:09 -07003470 ceph_msg_put(reply);
Sage Weil9abf82b2010-05-10 21:58:38 -07003471 up_read(&mdsc->snap_rwsem);
3472 mutex_unlock(&session->s_mutex);
Sage Weil93cea5b2009-12-23 12:21:51 -08003473fail_nomsg:
Yan, Zheng81c5a142019-01-01 16:28:33 +08003474 ceph_pagelist_release(recon_state.pagelist);
Sage Weil93cea5b2009-12-23 12:21:51 -08003475fail_nopagelist:
Sage Weil9abf82b2010-05-10 21:58:38 -07003476 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
Sage Weil9abf82b2010-05-10 21:58:38 -07003477 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07003478}
3479
3480
3481/*
3482 * compare old and new mdsmaps, kicking requests
3483 * and closing out old connections as necessary
3484 *
3485 * called under mdsc->mutex.
3486 */
3487static void check_new_map(struct ceph_mds_client *mdsc,
3488 struct ceph_mdsmap *newmap,
3489 struct ceph_mdsmap *oldmap)
3490{
3491 int i;
3492 int oldstate, newstate;
3493 struct ceph_mds_session *s;
3494
3495 dout("check_new_map new %u old %u\n",
3496 newmap->m_epoch, oldmap->m_epoch);
3497
Yan, Zheng76201b62017-03-28 17:04:13 +08003498 for (i = 0; i < oldmap->m_num_mds && i < mdsc->max_sessions; i++) {
Markus Elfringd37b1d92017-08-20 20:22:02 +02003499 if (!mdsc->sessions[i])
Sage Weil2f2dc052009-10-06 11:31:09 -07003500 continue;
3501 s = mdsc->sessions[i];
3502 oldstate = ceph_mdsmap_get_state(oldmap, i);
3503 newstate = ceph_mdsmap_get_state(newmap, i);
3504
Sage Weil0deb01c2010-06-17 14:19:01 -07003505 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07003506 i, ceph_mds_state_name(oldstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07003507 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
Sage Weil2f2dc052009-10-06 11:31:09 -07003508 ceph_mds_state_name(newstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07003509 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
John Spraya687eca2014-09-19 13:51:08 +01003510 ceph_session_state_name(s->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07003511
Yan, Zheng76201b62017-03-28 17:04:13 +08003512 if (i >= newmap->m_num_mds ||
Yan, Zheng3e8f43a2012-09-20 17:42:25 +08003513 memcmp(ceph_mdsmap_get_addr(oldmap, i),
Sage Weil2f2dc052009-10-06 11:31:09 -07003514 ceph_mdsmap_get_addr(newmap, i),
3515 sizeof(struct ceph_entity_addr))) {
3516 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
3517 /* the session never opened, just close it
3518 * out now */
Yan, Zheng0a07fc82017-03-29 15:30:24 +08003519 get_session(s);
Sage Weil2600d2d2010-02-22 15:12:16 -08003520 __unregister_session(mdsc, s);
Yan, Zheng0a07fc82017-03-29 15:30:24 +08003521 __wake_requests(mdsc, &s->s_waiting);
3522 ceph_put_mds_session(s);
Yan, Zheng28275282017-03-28 17:56:29 +08003523 } else if (i >= newmap->m_num_mds) {
3524 /* force close session for stopped mds */
3525 get_session(s);
3526 __unregister_session(mdsc, s);
3527 __wake_requests(mdsc, &s->s_waiting);
3528 kick_requests(mdsc, i);
3529 mutex_unlock(&mdsc->mutex);
3530
3531 mutex_lock(&s->s_mutex);
3532 cleanup_session_requests(mdsc, s);
3533 remove_session_caps(s);
3534 mutex_unlock(&s->s_mutex);
3535
3536 ceph_put_mds_session(s);
3537
3538 mutex_lock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003539 } else {
3540 /* just close it */
3541 mutex_unlock(&mdsc->mutex);
3542 mutex_lock(&s->s_mutex);
3543 mutex_lock(&mdsc->mutex);
3544 ceph_con_close(&s->s_con);
3545 mutex_unlock(&s->s_mutex);
3546 s->s_state = CEPH_MDS_SESSION_RESTARTING;
3547 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003548 } else if (oldstate == newstate) {
3549 continue; /* nothing new with this mds */
3550 }
3551
3552 /*
3553 * send reconnect?
3554 */
3555 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
Sage Weil34b6c852010-05-10 16:31:25 -07003556 newstate >= CEPH_MDS_STATE_RECONNECT) {
3557 mutex_unlock(&mdsc->mutex);
3558 send_mds_reconnect(mdsc, s);
3559 mutex_lock(&mdsc->mutex);
3560 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003561
3562 /*
Sage Weil29790f22010-03-18 14:45:05 -07003563 * kick request on any mds that has gone active.
Sage Weil2f2dc052009-10-06 11:31:09 -07003564 */
3565 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
3566 newstate >= CEPH_MDS_STATE_ACTIVE) {
Sage Weil29790f22010-03-18 14:45:05 -07003567 if (oldstate != CEPH_MDS_STATE_CREATING &&
3568 oldstate != CEPH_MDS_STATE_STARTING)
3569 pr_info("mds%d recovery completed\n", s->s_mds);
3570 kick_requests(mdsc, i);
Sage Weil2f2dc052009-10-06 11:31:09 -07003571 ceph_kick_flushing_caps(mdsc, s);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08003572 wake_up_session_caps(s, RECONNECT);
Sage Weil2f2dc052009-10-06 11:31:09 -07003573 }
3574 }
Sage Weilcb170a22010-06-21 13:38:35 -07003575
Yan, Zheng76201b62017-03-28 17:04:13 +08003576 for (i = 0; i < newmap->m_num_mds && i < mdsc->max_sessions; i++) {
Sage Weilcb170a22010-06-21 13:38:35 -07003577 s = mdsc->sessions[i];
3578 if (!s)
3579 continue;
3580 if (!ceph_mdsmap_is_laggy(newmap, i))
3581 continue;
3582 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3583 s->s_state == CEPH_MDS_SESSION_HUNG ||
3584 s->s_state == CEPH_MDS_SESSION_CLOSING) {
3585 dout(" connecting to export targets of laggy mds%d\n",
3586 i);
3587 __open_export_target_sessions(mdsc, s);
3588 }
3589 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003590}
3591
3592
3593
3594/*
3595 * leases
3596 */
3597
3598/*
3599 * caller must hold session s_mutex, dentry->d_lock
3600 */
3601void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
3602{
3603 struct ceph_dentry_info *di = ceph_dentry(dentry);
3604
3605 ceph_put_mds_session(di->lease_session);
3606 di->lease_session = NULL;
3607}
3608
Sage Weil2600d2d2010-02-22 15:12:16 -08003609static void handle_lease(struct ceph_mds_client *mdsc,
3610 struct ceph_mds_session *session,
3611 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07003612{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003613 struct super_block *sb = mdsc->fsc->sb;
Sage Weil2f2dc052009-10-06 11:31:09 -07003614 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07003615 struct dentry *parent, *dentry;
3616 struct ceph_dentry_info *di;
Sage Weil2600d2d2010-02-22 15:12:16 -08003617 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07003618 struct ceph_mds_lease *h = msg->front.iov_base;
Sage Weil1e5ea232010-06-04 10:05:40 -07003619 u32 seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003620 struct ceph_vino vino;
Sage Weil2f2dc052009-10-06 11:31:09 -07003621 struct qstr dname;
3622 int release = 0;
3623
Sage Weil2f2dc052009-10-06 11:31:09 -07003624 dout("handle_lease from mds%d\n", mds);
3625
3626 /* decode */
3627 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
3628 goto bad;
3629 vino.ino = le64_to_cpu(h->ino);
3630 vino.snap = CEPH_NOSNAP;
Sage Weil1e5ea232010-06-04 10:05:40 -07003631 seq = le32_to_cpu(h->seq);
Yan, Zheng0fcf6c02018-08-03 16:24:49 +08003632 dname.len = get_unaligned_le32(h + 1);
3633 if (msg->front.iov_len < sizeof(*h) + sizeof(u32) + dname.len)
Sage Weil2f2dc052009-10-06 11:31:09 -07003634 goto bad;
Yan, Zheng0fcf6c02018-08-03 16:24:49 +08003635 dname.name = (void *)(h + 1) + sizeof(u32);
Sage Weil2f2dc052009-10-06 11:31:09 -07003636
Sage Weil2f2dc052009-10-06 11:31:09 -07003637 /* lookup inode */
3638 inode = ceph_find_inode(sb, vino);
Sage Weil2f90b852011-07-26 11:28:25 -07003639 dout("handle_lease %s, ino %llx %p %.*s\n",
3640 ceph_lease_op_name(h->action), vino.ino, inode,
Sage Weil1e5ea232010-06-04 10:05:40 -07003641 dname.len, dname.name);
Yan, Zheng6cd3bca2014-09-17 07:45:12 +08003642
3643 mutex_lock(&session->s_mutex);
3644 session->s_seq++;
3645
Markus Elfringd37b1d92017-08-20 20:22:02 +02003646 if (!inode) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003647 dout("handle_lease no inode %llx\n", vino.ino);
3648 goto release;
3649 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003650
3651 /* dentry */
3652 parent = d_find_alias(inode);
3653 if (!parent) {
3654 dout("no parent dentry on inode %p\n", inode);
3655 WARN_ON(1);
3656 goto release; /* hrm... */
3657 }
Linus Torvalds8387ff22016-06-10 07:51:30 -07003658 dname.hash = full_name_hash(parent, dname.name, dname.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07003659 dentry = d_lookup(parent, &dname);
3660 dput(parent);
3661 if (!dentry)
3662 goto release;
3663
3664 spin_lock(&dentry->d_lock);
3665 di = ceph_dentry(dentry);
3666 switch (h->action) {
3667 case CEPH_MDS_LEASE_REVOKE:
Sage Weil3d8eb7a2011-11-11 09:48:53 -08003668 if (di->lease_session == session) {
Sage Weil1e5ea232010-06-04 10:05:40 -07003669 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
3670 h->seq = cpu_to_le32(di->lease_seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003671 __ceph_mdsc_drop_dentry_lease(dentry);
3672 }
3673 release = 1;
3674 break;
3675
3676 case CEPH_MDS_LEASE_RENEW:
Sage Weil3d8eb7a2011-11-11 09:48:53 -08003677 if (di->lease_session == session &&
Sage Weil2f2dc052009-10-06 11:31:09 -07003678 di->lease_gen == session->s_cap_gen &&
3679 di->lease_renew_from &&
3680 di->lease_renew_after == 0) {
3681 unsigned long duration =
Nicholas Mc Guire3563dbdd2015-02-06 06:52:17 -05003682 msecs_to_jiffies(le32_to_cpu(h->duration_ms));
Sage Weil2f2dc052009-10-06 11:31:09 -07003683
Sage Weil1e5ea232010-06-04 10:05:40 -07003684 di->lease_seq = seq;
Miklos Szeredi9b16f03c2016-06-22 16:35:04 +02003685 di->time = di->lease_renew_from + duration;
Sage Weil2f2dc052009-10-06 11:31:09 -07003686 di->lease_renew_after = di->lease_renew_from +
3687 (duration >> 1);
3688 di->lease_renew_from = 0;
3689 }
3690 break;
3691 }
3692 spin_unlock(&dentry->d_lock);
3693 dput(dentry);
3694
3695 if (!release)
3696 goto out;
3697
3698release:
3699 /* let's just reuse the same message */
3700 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
3701 ceph_msg_get(msg);
3702 ceph_con_send(&session->s_con, msg);
3703
3704out:
3705 iput(inode);
3706 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003707 return;
3708
3709bad:
3710 pr_err("corrupt lease message\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08003711 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003712}
3713
3714void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
3715 struct inode *inode,
3716 struct dentry *dentry, char action,
3717 u32 seq)
3718{
3719 struct ceph_msg *msg;
3720 struct ceph_mds_lease *lease;
3721 int len = sizeof(*lease) + sizeof(u32);
3722 int dnamelen = 0;
3723
3724 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
3725 inode, dentry, ceph_lease_op_name(action), session->s_mds);
3726 dnamelen = dentry->d_name.len;
3727 len += dnamelen;
3728
Sage Weilb61c2762011-08-09 15:03:46 -07003729 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07003730 if (!msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07003731 return;
3732 lease = msg->front.iov_base;
3733 lease->action = action;
Sage Weil2f2dc052009-10-06 11:31:09 -07003734 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
3735 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
3736 lease->seq = cpu_to_le32(seq);
3737 put_unaligned_le32(dnamelen, lease + 1);
3738 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
3739
3740 /*
3741 * if this is a preemptive lease RELEASE, no need to
3742 * flush request stream, since the actual request will
3743 * soon follow.
3744 */
3745 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
3746
3747 ceph_con_send(&session->s_con, msg);
3748}
3749
3750/*
Yan, Zheng7aac4532018-03-13 19:52:20 +08003751 * lock unlock sessions, to wait ongoing session activities
Sage Weil2f2dc052009-10-06 11:31:09 -07003752 */
Yan, Zheng7aac4532018-03-13 19:52:20 +08003753static void lock_unlock_sessions(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07003754{
3755 int i;
3756
Sage Weil2f2dc052009-10-06 11:31:09 -07003757 mutex_lock(&mdsc->mutex);
3758 for (i = 0; i < mdsc->max_sessions; i++) {
3759 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
3760 if (!s)
3761 continue;
3762 mutex_unlock(&mdsc->mutex);
3763 mutex_lock(&s->s_mutex);
3764 mutex_unlock(&s->s_mutex);
3765 ceph_put_mds_session(s);
3766 mutex_lock(&mdsc->mutex);
3767 }
3768 mutex_unlock(&mdsc->mutex);
3769}
3770
3771
3772
3773/*
3774 * delayed work -- periodically trim expired leases, renew caps with mds
3775 */
3776static void schedule_delayed(struct ceph_mds_client *mdsc)
3777{
3778 int delay = 5;
3779 unsigned hz = round_jiffies_relative(HZ * delay);
3780 schedule_delayed_work(&mdsc->delayed_work, hz);
3781}
3782
3783static void delayed_work(struct work_struct *work)
3784{
3785 int i;
3786 struct ceph_mds_client *mdsc =
3787 container_of(work, struct ceph_mds_client, delayed_work.work);
3788 int renew_interval;
3789 int renew_caps;
3790
3791 dout("mdsc delayed_work\n");
Sage Weilafcdaea2009-10-14 14:27:38 -07003792 ceph_check_delayed_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003793
Yan, Zheng75c96272017-12-14 15:11:09 +08003794 ceph_trim_snapid_map(mdsc);
3795
Sage Weil2f2dc052009-10-06 11:31:09 -07003796 mutex_lock(&mdsc->mutex);
3797 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
3798 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
3799 mdsc->last_renew_caps);
3800 if (renew_caps)
3801 mdsc->last_renew_caps = jiffies;
3802
3803 for (i = 0; i < mdsc->max_sessions; i++) {
3804 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
Markus Elfringd37b1d92017-08-20 20:22:02 +02003805 if (!s)
Sage Weil2f2dc052009-10-06 11:31:09 -07003806 continue;
3807 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
3808 dout("resending session close request for mds%d\n",
3809 s->s_mds);
3810 request_close_session(mdsc, s);
3811 ceph_put_mds_session(s);
3812 continue;
3813 }
3814 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
3815 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
3816 s->s_state = CEPH_MDS_SESSION_HUNG;
3817 pr_info("mds%d hung\n", s->s_mds);
3818 }
3819 }
3820 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
3821 /* this mds is failed or recovering, just wait */
3822 ceph_put_mds_session(s);
3823 continue;
3824 }
3825 mutex_unlock(&mdsc->mutex);
3826
3827 mutex_lock(&s->s_mutex);
3828 if (renew_caps)
3829 send_renew_caps(mdsc, s);
3830 else
3831 ceph_con_keepalive(&s->s_con);
Sage Weilaab53dd2010-03-17 16:30:21 -07003832 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3833 s->s_state == CEPH_MDS_SESSION_HUNG)
Sage Weil3d7ded42010-06-09 16:47:10 -07003834 ceph_send_cap_releases(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07003835 mutex_unlock(&s->s_mutex);
3836 ceph_put_mds_session(s);
3837
3838 mutex_lock(&mdsc->mutex);
3839 }
3840 mutex_unlock(&mdsc->mutex);
3841
3842 schedule_delayed(mdsc);
3843}
3844
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003845int ceph_mdsc_init(struct ceph_fs_client *fsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07003846
Sage Weil2f2dc052009-10-06 11:31:09 -07003847{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003848 struct ceph_mds_client *mdsc;
3849
3850 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
3851 if (!mdsc)
3852 return -ENOMEM;
3853 mdsc->fsc = fsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07003854 mutex_init(&mdsc->mutex);
3855 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
Markus Elfringd37b1d92017-08-20 20:22:02 +02003856 if (!mdsc->mdsmap) {
majianpengfb3101b2013-06-25 14:48:19 +08003857 kfree(mdsc);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08003858 return -ENOMEM;
majianpengfb3101b2013-06-25 14:48:19 +08003859 }
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08003860
Chengguang Xu50c55ae2018-03-14 13:47:33 +08003861 fsc->mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07003862 init_completion(&mdsc->safe_umount_waiters);
Sage Weilf3c60c52010-08-11 14:51:23 -07003863 init_waitqueue_head(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003864 INIT_LIST_HEAD(&mdsc->waiting_for_map);
3865 mdsc->sessions = NULL;
Yan, Zheng86d8f672015-01-09 17:00:42 +08003866 atomic_set(&mdsc->num_sessions, 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07003867 mdsc->max_sessions = 0;
3868 mdsc->stopping = 0;
Luis Henriquesd557c482018-01-12 17:19:29 +00003869 atomic64_set(&mdsc->quotarealms_count, 0);
Yan, Zhengaffbc192015-05-05 21:22:13 +08003870 mdsc->last_snap_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003871 init_rwsem(&mdsc->snap_rwsem);
Sage Weila105f002010-02-15 14:37:55 -08003872 mdsc->snap_realms = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07003873 INIT_LIST_HEAD(&mdsc->snap_empty);
Yan, Zheng81c5a142019-01-01 16:28:33 +08003874 mdsc->num_snap_realms = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003875 spin_lock_init(&mdsc->snap_empty_lock);
3876 mdsc->last_tid = 0;
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08003877 mdsc->oldest_tid = 0;
Sage Weil44ca18f2010-02-15 12:08:46 -08003878 mdsc->request_tree = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07003879 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
3880 mdsc->last_renew_caps = jiffies;
3881 INIT_LIST_HEAD(&mdsc->cap_delay_list);
3882 spin_lock_init(&mdsc->cap_delay_lock);
3883 INIT_LIST_HEAD(&mdsc->snap_flush_list);
3884 spin_lock_init(&mdsc->snap_flush_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08003885 mdsc->last_cap_flush_tid = 1;
Yan, Zhenge4500b52016-07-06 11:12:56 +08003886 INIT_LIST_HEAD(&mdsc->cap_flush_list);
Sage Weil2f2dc052009-10-06 11:31:09 -07003887 INIT_LIST_HEAD(&mdsc->cap_dirty);
Sage Weildb354052011-05-24 11:46:31 -07003888 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
Sage Weil2f2dc052009-10-06 11:31:09 -07003889 mdsc->num_cap_flushing = 0;
3890 spin_lock_init(&mdsc->cap_dirty_lock);
3891 init_waitqueue_head(&mdsc->cap_flushing_wq);
3892 spin_lock_init(&mdsc->dentry_lru_lock);
3893 INIT_LIST_HEAD(&mdsc->dentry_lru);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08003894
Yehuda Sadeh37151662010-06-17 16:16:12 -07003895 ceph_caps_init(mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003896 ceph_adjust_min_caps(mdsc, fsc->min_caps);
Yehuda Sadeh37151662010-06-17 16:16:12 -07003897
Yan, Zheng75c96272017-12-14 15:11:09 +08003898 spin_lock_init(&mdsc->snapid_map_lock);
3899 mdsc->snapid_map_tree = RB_ROOT;
3900 INIT_LIST_HEAD(&mdsc->snapid_map_lru);
3901
Yan, Zheng10183a62015-04-27 15:33:28 +08003902 init_rwsem(&mdsc->pool_perm_rwsem);
3903 mdsc->pool_perm_tree = RB_ROOT;
3904
Yan, Zhengdfeb84d2018-07-02 15:55:23 +08003905 strscpy(mdsc->nodename, utsname()->nodename,
3906 sizeof(mdsc->nodename));
Sage Weil5f44f142009-11-18 14:52:18 -08003907 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003908}
3909
3910/*
3911 * Wait for safe replies on open mds requests. If we time out, drop
3912 * all requests from the tree to avoid dangling dentry refs.
3913 */
3914static void wait_requests(struct ceph_mds_client *mdsc)
3915{
Ilya Dryomova319bf52015-05-15 12:02:17 +03003916 struct ceph_options *opts = mdsc->fsc->client->options;
Sage Weil2f2dc052009-10-06 11:31:09 -07003917 struct ceph_mds_request *req;
Sage Weil2f2dc052009-10-06 11:31:09 -07003918
3919 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003920 if (__get_oldest_req(mdsc)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003921 mutex_unlock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003922
Sage Weil2f2dc052009-10-06 11:31:09 -07003923 dout("wait_requests waiting for requests\n");
3924 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
Ilya Dryomova319bf52015-05-15 12:02:17 +03003925 ceph_timeout_jiffies(opts->mount_timeout));
Sage Weil2f2dc052009-10-06 11:31:09 -07003926
3927 /* tear down remaining requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08003928 mutex_lock(&mdsc->mutex);
3929 while ((req = __get_oldest_req(mdsc))) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003930 dout("wait_requests timed out on tid %llu\n",
3931 req->r_tid);
Sage Weil44ca18f2010-02-15 12:08:46 -08003932 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07003933 }
3934 }
3935 mutex_unlock(&mdsc->mutex);
3936 dout("wait_requests done\n");
3937}
3938
3939/*
3940 * called before mount is ro, and before dentries are torn down.
3941 * (hmm, does this still race with new lookups?)
3942 */
3943void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
3944{
3945 dout("pre_umount\n");
3946 mdsc->stopping = 1;
3947
Yan, Zheng7aac4532018-03-13 19:52:20 +08003948 lock_unlock_sessions(mdsc);
Sage Weilafcdaea2009-10-14 14:27:38 -07003949 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07003950 wait_requests(mdsc);
Sage Weil17c688c2010-06-21 16:12:26 -07003951
3952 /*
3953 * wait for reply handlers to drop their request refs and
3954 * their inode/dcache refs
3955 */
3956 ceph_msgr_flush();
Sage Weil2f2dc052009-10-06 11:31:09 -07003957}
3958
3959/*
3960 * wait for all write mds requests to flush.
3961 */
3962static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
3963{
Sage Weil80fc7312010-03-16 15:28:54 -07003964 struct ceph_mds_request *req = NULL, *nextreq;
Sage Weil44ca18f2010-02-15 12:08:46 -08003965 struct rb_node *n;
Sage Weil2f2dc052009-10-06 11:31:09 -07003966
3967 mutex_lock(&mdsc->mutex);
3968 dout("wait_unsafe_requests want %lld\n", want_tid);
Sage Weil80fc7312010-03-16 15:28:54 -07003969restart:
Sage Weil44ca18f2010-02-15 12:08:46 -08003970 req = __get_oldest_req(mdsc);
3971 while (req && req->r_tid <= want_tid) {
Sage Weil80fc7312010-03-16 15:28:54 -07003972 /* find next request */
3973 n = rb_next(&req->r_node);
3974 if (n)
3975 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
3976 else
3977 nextreq = NULL;
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08003978 if (req->r_op != CEPH_MDS_OP_SETFILELOCK &&
3979 (req->r_op & CEPH_MDS_OP_WRITE)) {
Sage Weil44ca18f2010-02-15 12:08:46 -08003980 /* write op */
3981 ceph_mdsc_get_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07003982 if (nextreq)
3983 ceph_mdsc_get_request(nextreq);
Sage Weil44ca18f2010-02-15 12:08:46 -08003984 mutex_unlock(&mdsc->mutex);
3985 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3986 req->r_tid, want_tid);
3987 wait_for_completion(&req->r_safe_completion);
3988 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08003989 ceph_mdsc_put_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07003990 if (!nextreq)
3991 break; /* next dne before, so we're done! */
3992 if (RB_EMPTY_NODE(&nextreq->r_node)) {
3993 /* next request was removed from tree */
3994 ceph_mdsc_put_request(nextreq);
3995 goto restart;
3996 }
3997 ceph_mdsc_put_request(nextreq); /* won't go away */
Sage Weil44ca18f2010-02-15 12:08:46 -08003998 }
Sage Weil80fc7312010-03-16 15:28:54 -07003999 req = nextreq;
Sage Weil2f2dc052009-10-06 11:31:09 -07004000 }
4001 mutex_unlock(&mdsc->mutex);
4002 dout("wait_unsafe_requests done\n");
4003}
4004
4005void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
4006{
Yan, Zheng0e294382016-07-04 18:06:41 +08004007 u64 want_tid, want_flush;
Sage Weil2f2dc052009-10-06 11:31:09 -07004008
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01004009 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
Sage Weil56b7cf92010-05-03 15:22:00 -07004010 return;
4011
Sage Weil2f2dc052009-10-06 11:31:09 -07004012 dout("sync\n");
4013 mutex_lock(&mdsc->mutex);
4014 want_tid = mdsc->last_tid;
Sage Weil2f2dc052009-10-06 11:31:09 -07004015 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07004016
Sage Weilafcdaea2009-10-14 14:27:38 -07004017 ceph_flush_dirty_caps(mdsc);
Yan, Zhengd3383a82015-01-08 21:30:12 +08004018 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zheng8310b082015-06-09 17:20:12 +08004019 want_flush = mdsc->last_cap_flush_tid;
Yan, Zhengc8799fc2016-07-07 15:22:38 +08004020 if (!list_empty(&mdsc->cap_flush_list)) {
4021 struct ceph_cap_flush *cf =
4022 list_last_entry(&mdsc->cap_flush_list,
4023 struct ceph_cap_flush, g_list);
4024 cf->wake = true;
4025 }
Yan, Zhengd3383a82015-01-08 21:30:12 +08004026 spin_unlock(&mdsc->cap_dirty_lock);
4027
Yan, Zheng0e294382016-07-04 18:06:41 +08004028 dout("sync want tid %lld flush_seq %lld\n",
4029 want_tid, want_flush);
Sage Weil2f2dc052009-10-06 11:31:09 -07004030
4031 wait_unsafe_requests(mdsc, want_tid);
Yan, Zheng0e294382016-07-04 18:06:41 +08004032 wait_caps_flush(mdsc, want_flush);
Sage Weil2f2dc052009-10-06 11:31:09 -07004033}
4034
Sage Weilf3c60c52010-08-11 14:51:23 -07004035/*
4036 * true if all sessions are closed, or we force unmount
4037 */
Yan, Zhengfcff4152016-09-14 16:39:51 +08004038static bool done_closing_sessions(struct ceph_mds_client *mdsc, int skipped)
Sage Weilf3c60c52010-08-11 14:51:23 -07004039{
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01004040 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
Sage Weilf3c60c52010-08-11 14:51:23 -07004041 return true;
Yan, Zhengfcff4152016-09-14 16:39:51 +08004042 return atomic_read(&mdsc->num_sessions) <= skipped;
Sage Weilf3c60c52010-08-11 14:51:23 -07004043}
Sage Weil2f2dc052009-10-06 11:31:09 -07004044
4045/*
4046 * called after sb is ro.
4047 */
4048void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
4049{
Ilya Dryomova319bf52015-05-15 12:02:17 +03004050 struct ceph_options *opts = mdsc->fsc->client->options;
Sage Weil2f2dc052009-10-06 11:31:09 -07004051 struct ceph_mds_session *session;
4052 int i;
Yan, Zhengfcff4152016-09-14 16:39:51 +08004053 int skipped = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07004054
4055 dout("close_sessions\n");
4056
Sage Weil2f2dc052009-10-06 11:31:09 -07004057 /* close sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07004058 mutex_lock(&mdsc->mutex);
4059 for (i = 0; i < mdsc->max_sessions; i++) {
4060 session = __ceph_lookup_mds_session(mdsc, i);
4061 if (!session)
4062 continue;
Sage Weil2f2dc052009-10-06 11:31:09 -07004063 mutex_unlock(&mdsc->mutex);
Sage Weilf3c60c52010-08-11 14:51:23 -07004064 mutex_lock(&session->s_mutex);
Yan, Zhengfcff4152016-09-14 16:39:51 +08004065 if (__close_session(mdsc, session) <= 0)
4066 skipped++;
Sage Weilf3c60c52010-08-11 14:51:23 -07004067 mutex_unlock(&session->s_mutex);
4068 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07004069 mutex_lock(&mdsc->mutex);
4070 }
Sage Weilf3c60c52010-08-11 14:51:23 -07004071 mutex_unlock(&mdsc->mutex);
4072
4073 dout("waiting for sessions to close\n");
Yan, Zhengfcff4152016-09-14 16:39:51 +08004074 wait_event_timeout(mdsc->session_close_wq,
4075 done_closing_sessions(mdsc, skipped),
Ilya Dryomova319bf52015-05-15 12:02:17 +03004076 ceph_timeout_jiffies(opts->mount_timeout));
Sage Weil2f2dc052009-10-06 11:31:09 -07004077
4078 /* tear down remaining sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07004079 mutex_lock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07004080 for (i = 0; i < mdsc->max_sessions; i++) {
4081 if (mdsc->sessions[i]) {
4082 session = get_session(mdsc->sessions[i]);
Sage Weil2600d2d2010-02-22 15:12:16 -08004083 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07004084 mutex_unlock(&mdsc->mutex);
4085 mutex_lock(&session->s_mutex);
4086 remove_session_caps(session);
4087 mutex_unlock(&session->s_mutex);
4088 ceph_put_mds_session(session);
4089 mutex_lock(&mdsc->mutex);
4090 }
4091 }
Sage Weil2f2dc052009-10-06 11:31:09 -07004092 WARN_ON(!list_empty(&mdsc->cap_delay_list));
Sage Weil2f2dc052009-10-06 11:31:09 -07004093 mutex_unlock(&mdsc->mutex);
4094
Yan, Zheng75c96272017-12-14 15:11:09 +08004095 ceph_cleanup_snapid_map(mdsc);
4096
Sage Weil2f2dc052009-10-06 11:31:09 -07004097 ceph_cleanup_empty_realms(mdsc);
4098
4099 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
4100
4101 dout("stopped\n");
4102}
4103
Yan, Zheng48fec5d2015-07-01 16:27:46 +08004104void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc)
4105{
4106 struct ceph_mds_session *session;
4107 int mds;
4108
4109 dout("force umount\n");
4110
4111 mutex_lock(&mdsc->mutex);
4112 for (mds = 0; mds < mdsc->max_sessions; mds++) {
4113 session = __ceph_lookup_mds_session(mdsc, mds);
4114 if (!session)
4115 continue;
4116 mutex_unlock(&mdsc->mutex);
4117 mutex_lock(&session->s_mutex);
4118 __close_session(mdsc, session);
4119 if (session->s_state == CEPH_MDS_SESSION_CLOSING) {
4120 cleanup_session_requests(mdsc, session);
4121 remove_session_caps(session);
4122 }
4123 mutex_unlock(&session->s_mutex);
4124 ceph_put_mds_session(session);
4125 mutex_lock(&mdsc->mutex);
4126 kick_requests(mdsc, mds);
4127 }
4128 __wake_requests(mdsc, &mdsc->waiting_for_map);
4129 mutex_unlock(&mdsc->mutex);
4130}
4131
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004132static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07004133{
4134 dout("stop\n");
4135 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
4136 if (mdsc->mdsmap)
4137 ceph_mdsmap_destroy(mdsc->mdsmap);
4138 kfree(mdsc->sessions);
Yehuda Sadeh37151662010-06-17 16:16:12 -07004139 ceph_caps_finalize(mdsc);
Yan, Zheng10183a62015-04-27 15:33:28 +08004140 ceph_pool_perm_destroy(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07004141}
4142
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004143void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
4144{
4145 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weilef550f62011-03-25 13:27:48 -07004146 dout("mdsc_destroy %p\n", mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07004147
Chengguang Xu50c55ae2018-03-14 13:47:33 +08004148 if (!mdsc)
4149 return;
4150
Sage Weilef550f62011-03-25 13:27:48 -07004151 /* flush out any connection work with references to us */
4152 ceph_msgr_flush();
4153
Yan, Zheng62a65f32017-06-22 16:26:34 +08004154 ceph_mdsc_stop(mdsc);
4155
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004156 fsc->mdsc = NULL;
4157 kfree(mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07004158 dout("mdsc_destroy %p done\n", mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004159}
4160
Yan, Zheng430afba2016-07-08 11:25:38 +08004161void ceph_mdsc_handle_fsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
4162{
4163 struct ceph_fs_client *fsc = mdsc->fsc;
4164 const char *mds_namespace = fsc->mount_options->mds_namespace;
4165 void *p = msg->front.iov_base;
4166 void *end = p + msg->front.iov_len;
4167 u32 epoch;
4168 u32 map_len;
4169 u32 num_fs;
4170 u32 mount_fscid = (u32)-1;
4171 u8 struct_v, struct_cv;
4172 int err = -EINVAL;
4173
4174 ceph_decode_need(&p, end, sizeof(u32), bad);
4175 epoch = ceph_decode_32(&p);
4176
4177 dout("handle_fsmap epoch %u\n", epoch);
4178
4179 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
4180 struct_v = ceph_decode_8(&p);
4181 struct_cv = ceph_decode_8(&p);
4182 map_len = ceph_decode_32(&p);
4183
4184 ceph_decode_need(&p, end, sizeof(u32) * 3, bad);
4185 p += sizeof(u32) * 2; /* skip epoch and legacy_client_fscid */
4186
4187 num_fs = ceph_decode_32(&p);
4188 while (num_fs-- > 0) {
4189 void *info_p, *info_end;
4190 u32 info_len;
4191 u8 info_v, info_cv;
4192 u32 fscid, namelen;
4193
4194 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
4195 info_v = ceph_decode_8(&p);
4196 info_cv = ceph_decode_8(&p);
4197 info_len = ceph_decode_32(&p);
4198 ceph_decode_need(&p, end, info_len, bad);
4199 info_p = p;
4200 info_end = p + info_len;
4201 p = info_end;
4202
4203 ceph_decode_need(&info_p, info_end, sizeof(u32) * 2, bad);
4204 fscid = ceph_decode_32(&info_p);
4205 namelen = ceph_decode_32(&info_p);
4206 ceph_decode_need(&info_p, info_end, namelen, bad);
4207
4208 if (mds_namespace &&
4209 strlen(mds_namespace) == namelen &&
4210 !strncmp(mds_namespace, (char *)info_p, namelen)) {
4211 mount_fscid = fscid;
4212 break;
4213 }
4214 }
4215
4216 ceph_monc_got_map(&fsc->client->monc, CEPH_SUB_FSMAP, epoch);
4217 if (mount_fscid != (u32)-1) {
4218 fsc->client->monc.fs_cluster_id = mount_fscid;
4219 ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP,
4220 0, true);
4221 ceph_monc_renew_subs(&fsc->client->monc);
4222 } else {
4223 err = -ENOENT;
4224 goto err_out;
4225 }
4226 return;
Ilya Dryomov76bd6ec2017-10-16 10:32:50 +02004227
Yan, Zheng430afba2016-07-08 11:25:38 +08004228bad:
4229 pr_err("error decoding fsmap\n");
4230err_out:
4231 mutex_lock(&mdsc->mutex);
Ilya Dryomov76bd6ec2017-10-16 10:32:50 +02004232 mdsc->mdsmap_err = err;
Yan, Zheng430afba2016-07-08 11:25:38 +08004233 __wake_requests(mdsc, &mdsc->waiting_for_map);
4234 mutex_unlock(&mdsc->mutex);
Yan, Zheng430afba2016-07-08 11:25:38 +08004235}
Sage Weil2f2dc052009-10-06 11:31:09 -07004236
4237/*
4238 * handle mds map update.
4239 */
Yan, Zheng430afba2016-07-08 11:25:38 +08004240void ceph_mdsc_handle_mdsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07004241{
4242 u32 epoch;
4243 u32 maplen;
4244 void *p = msg->front.iov_base;
4245 void *end = p + msg->front.iov_len;
4246 struct ceph_mdsmap *newmap, *oldmap;
4247 struct ceph_fsid fsid;
4248 int err = -EINVAL;
4249
4250 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
4251 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004252 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
Sage Weil07433042009-11-18 16:50:41 -08004253 return;
Sage Weilc89136e2009-10-14 09:59:09 -07004254 epoch = ceph_decode_32(&p);
4255 maplen = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07004256 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
4257
4258 /* do we need it? */
Sage Weil2f2dc052009-10-06 11:31:09 -07004259 mutex_lock(&mdsc->mutex);
4260 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
4261 dout("handle_map epoch %u <= our %u\n",
4262 epoch, mdsc->mdsmap->m_epoch);
4263 mutex_unlock(&mdsc->mutex);
4264 return;
4265 }
4266
4267 newmap = ceph_mdsmap_decode(&p, end);
4268 if (IS_ERR(newmap)) {
4269 err = PTR_ERR(newmap);
4270 goto bad_unlock;
4271 }
4272
4273 /* swap into place */
4274 if (mdsc->mdsmap) {
4275 oldmap = mdsc->mdsmap;
4276 mdsc->mdsmap = newmap;
4277 check_new_map(mdsc, newmap, oldmap);
4278 ceph_mdsmap_destroy(oldmap);
4279 } else {
4280 mdsc->mdsmap = newmap; /* first mds map */
4281 }
Chengguang Xu719784b2018-07-19 22:15:24 +08004282 mdsc->fsc->max_file_size = min((loff_t)mdsc->mdsmap->m_max_file_size,
4283 MAX_LFS_FILESIZE);
Sage Weil2f2dc052009-10-06 11:31:09 -07004284
4285 __wake_requests(mdsc, &mdsc->waiting_for_map);
Ilya Dryomov82dcaba2016-01-19 16:19:06 +01004286 ceph_monc_got_map(&mdsc->fsc->client->monc, CEPH_SUB_MDSMAP,
4287 mdsc->mdsmap->m_epoch);
Sage Weil2f2dc052009-10-06 11:31:09 -07004288
4289 mutex_unlock(&mdsc->mutex);
4290 schedule_delayed(mdsc);
4291 return;
4292
4293bad_unlock:
4294 mutex_unlock(&mdsc->mutex);
4295bad:
4296 pr_err("error decoding mdsmap %d\n", err);
4297 return;
4298}
4299
4300static struct ceph_connection *con_get(struct ceph_connection *con)
4301{
4302 struct ceph_mds_session *s = con->private;
4303
4304 if (get_session(s)) {
Elena Reshetova3997c012017-03-03 11:15:06 +02004305 dout("mdsc con_get %p ok (%d)\n", s, refcount_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -07004306 return con;
4307 }
4308 dout("mdsc con_get %p FAIL\n", s);
4309 return NULL;
4310}
4311
4312static void con_put(struct ceph_connection *con)
4313{
4314 struct ceph_mds_session *s = con->private;
4315
Elena Reshetova3997c012017-03-03 11:15:06 +02004316 dout("mdsc con_put %p (%d)\n", s, refcount_read(&s->s_ref) - 1);
Sage Weil2f2dc052009-10-06 11:31:09 -07004317 ceph_put_mds_session(s);
4318}
4319
4320/*
4321 * if the client is unresponsive for long enough, the mds will kill
4322 * the session entirely.
4323 */
4324static void peer_reset(struct ceph_connection *con)
4325{
4326 struct ceph_mds_session *s = con->private;
Sage Weil7e70f0e2010-03-18 13:59:12 -07004327 struct ceph_mds_client *mdsc = s->s_mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07004328
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07004329 pr_warn("mds%d closed our session\n", s->s_mds);
Sage Weil7e70f0e2010-03-18 13:59:12 -07004330 send_mds_reconnect(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07004331}
4332
4333static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
4334{
4335 struct ceph_mds_session *s = con->private;
4336 struct ceph_mds_client *mdsc = s->s_mdsc;
4337 int type = le16_to_cpu(msg->hdr.type);
4338
Sage Weil2600d2d2010-02-22 15:12:16 -08004339 mutex_lock(&mdsc->mutex);
4340 if (__verify_registered_session(mdsc, s) < 0) {
4341 mutex_unlock(&mdsc->mutex);
4342 goto out;
4343 }
4344 mutex_unlock(&mdsc->mutex);
4345
Sage Weil2f2dc052009-10-06 11:31:09 -07004346 switch (type) {
4347 case CEPH_MSG_MDS_MAP:
Yan, Zheng430afba2016-07-08 11:25:38 +08004348 ceph_mdsc_handle_mdsmap(mdsc, msg);
4349 break;
4350 case CEPH_MSG_FS_MAP_USER:
4351 ceph_mdsc_handle_fsmap(mdsc, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004352 break;
4353 case CEPH_MSG_CLIENT_SESSION:
4354 handle_session(s, msg);
4355 break;
4356 case CEPH_MSG_CLIENT_REPLY:
4357 handle_reply(s, msg);
4358 break;
4359 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
Sage Weil2600d2d2010-02-22 15:12:16 -08004360 handle_forward(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004361 break;
4362 case CEPH_MSG_CLIENT_CAPS:
4363 ceph_handle_caps(s, msg);
4364 break;
4365 case CEPH_MSG_CLIENT_SNAP:
Sage Weil2600d2d2010-02-22 15:12:16 -08004366 ceph_handle_snap(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004367 break;
4368 case CEPH_MSG_CLIENT_LEASE:
Sage Weil2600d2d2010-02-22 15:12:16 -08004369 handle_lease(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004370 break;
Luis Henriquesfb18a572018-01-05 10:47:18 +00004371 case CEPH_MSG_CLIENT_QUOTA:
4372 ceph_handle_quota(mdsc, s, msg);
4373 break;
Sage Weil2f2dc052009-10-06 11:31:09 -07004374
4375 default:
4376 pr_err("received unknown message type %d %s\n", type,
4377 ceph_msg_type_name(type));
4378 }
Sage Weil2600d2d2010-02-22 15:12:16 -08004379out:
Sage Weil2f2dc052009-10-06 11:31:09 -07004380 ceph_msg_put(msg);
4381}
4382
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004383/*
4384 * authentication
4385 */
Alex Eldera3530df2012-05-16 15:16:39 -05004386
4387/*
4388 * Note: returned pointer is the address of a structure that's
4389 * managed separately. Caller must *not* attempt to free it.
4390 */
4391static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05004392 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004393{
4394 struct ceph_mds_session *s = con->private;
4395 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004396 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05004397 struct ceph_auth_handshake *auth = &s->s_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004398
Alex Elder74f18692012-05-16 15:16:39 -05004399 if (force_new && auth->authorizer) {
Ilya Dryomov6c1ea262016-04-11 19:34:49 +02004400 ceph_auth_destroy_authorizer(auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05004401 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004402 }
Sage Weil27859f92013-03-25 10:26:14 -07004403 if (!auth->authorizer) {
4404 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4405 auth);
Sage Weil0bed9b52013-03-25 10:26:01 -07004406 if (ret)
4407 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07004408 } else {
4409 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4410 auth);
Alex Eldera2556512012-05-16 15:16:39 -05004411 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05004412 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004413 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004414 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05004415
Alex Eldera3530df2012-05-16 15:16:39 -05004416 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004417}
4418
Ilya Dryomov6daca132018-07-27 19:18:34 +02004419static int add_authorizer_challenge(struct ceph_connection *con,
4420 void *challenge_buf, int challenge_buf_len)
4421{
4422 struct ceph_mds_session *s = con->private;
4423 struct ceph_mds_client *mdsc = s->s_mdsc;
4424 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
4425
4426 return ceph_auth_add_authorizer_challenge(ac, s->s_auth.authorizer,
4427 challenge_buf, challenge_buf_len);
4428}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004429
Ilya Dryomov0dde5842016-12-02 16:35:09 +01004430static int verify_authorizer_reply(struct ceph_connection *con)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004431{
4432 struct ceph_mds_session *s = con->private;
4433 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004434 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004435
Ilya Dryomov0dde5842016-12-02 16:35:09 +01004436 return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004437}
4438
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004439static int invalidate_authorizer(struct ceph_connection *con)
4440{
4441 struct ceph_mds_session *s = con->private;
4442 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004443 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004444
Sage Weil27859f92013-03-25 10:26:14 -07004445 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004446
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004447 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004448}
4449
Alex Elder53ded492013-03-01 18:00:14 -06004450static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
4451 struct ceph_msg_header *hdr, int *skip)
4452{
4453 struct ceph_msg *msg;
4454 int type = (int) le16_to_cpu(hdr->type);
4455 int front_len = (int) le32_to_cpu(hdr->front_len);
4456
4457 if (con->in_msg)
4458 return con->in_msg;
4459
4460 *skip = 0;
4461 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
4462 if (!msg) {
4463 pr_err("unable to allocate msg type %d len %d\n",
4464 type, front_len);
4465 return NULL;
4466 }
Alex Elder53ded492013-03-01 18:00:14 -06004467
4468 return msg;
4469}
4470
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004471static int mds_sign_message(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08004472{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004473 struct ceph_mds_session *s = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08004474 struct ceph_auth_handshake *auth = &s->s_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004475
Yan, Zheng33d07332014-11-04 16:33:37 +08004476 return ceph_auth_sign_message(auth, msg);
4477}
4478
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004479static int mds_check_message_signature(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08004480{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004481 struct ceph_mds_session *s = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08004482 struct ceph_auth_handshake *auth = &s->s_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004483
Yan, Zheng33d07332014-11-04 16:33:37 +08004484 return ceph_auth_check_message_signature(auth, msg);
4485}
4486
Tobias Klauser9e327892010-05-20 10:40:19 +02004487static const struct ceph_connection_operations mds_con_ops = {
Sage Weil2f2dc052009-10-06 11:31:09 -07004488 .get = con_get,
4489 .put = con_put,
4490 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004491 .get_authorizer = get_authorizer,
Ilya Dryomov6daca132018-07-27 19:18:34 +02004492 .add_authorizer_challenge = add_authorizer_challenge,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004493 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004494 .invalidate_authorizer = invalidate_authorizer,
Sage Weil2f2dc052009-10-06 11:31:09 -07004495 .peer_reset = peer_reset,
Alex Elder53ded492013-03-01 18:00:14 -06004496 .alloc_msg = mds_alloc_msg,
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004497 .sign_message = mds_sign_message,
4498 .check_message_signature = mds_check_message_signature,
Sage Weil2f2dc052009-10-06 11:31:09 -07004499};
4500
Sage Weil2f2dc052009-10-06 11:31:09 -07004501/* eof */