blob: f3c71230df932ac49c0401d0a3533a42e176b424 [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>
Xiubo Li9ba1e222020-01-08 05:17:31 -050012#include <linux/bits.h>
Xiubo Li70c94822020-03-19 23:45:02 -040013#include <linux/ktime.h>
Sage Weil2f2dc052009-10-06 11:31:09 -070014
Sage Weil2f2dc052009-10-06 11:31:09 -070015#include "super.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070016#include "mds_client.h"
17
Sage Weil1fe60e52012-07-30 16:23:22 -070018#include <linux/ceph/ceph_features.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070019#include <linux/ceph/messenger.h>
20#include <linux/ceph/decode.h>
21#include <linux/ceph/pagelist.h>
22#include <linux/ceph/auth.h>
23#include <linux/ceph/debugfs.h>
Sage Weil2f2dc052009-10-06 11:31:09 -070024
Yan, Zheng81c5a142019-01-01 16:28:33 +080025#define RECONNECT_MAX_SIZE (INT_MAX - PAGE_SIZE)
26
Sage Weil2f2dc052009-10-06 11:31:09 -070027/*
28 * A cluster of MDS (metadata server) daemons is responsible for
29 * managing the file system namespace (the directory hierarchy and
30 * inodes) and for coordinating shared access to storage. Metadata is
31 * partitioning hierarchically across a number of servers, and that
32 * partition varies over time as the cluster adjusts the distribution
33 * in order to balance load.
34 *
35 * The MDS client is primarily responsible to managing synchronous
36 * metadata requests for operations like open, unlink, and so forth.
37 * If there is a MDS failure, we find out about it when we (possibly
38 * request and) receive a new MDS map, and can resubmit affected
39 * requests.
40 *
41 * For the most part, though, we take advantage of a lossless
42 * communications channel to the MDS, and do not need to worry about
43 * timing out or resubmitting requests.
44 *
45 * We maintain a stateful "session" with each MDS we interact with.
46 * Within each session, we sent periodic heartbeat messages to ensure
47 * any capabilities or leases we have been issues remain valid. If
48 * the session times out and goes stale, our leases and capabilities
49 * are no longer valid.
50 */
51
Sage Weil20cb34a2010-05-12 15:21:32 -070052struct ceph_reconnect_state {
Yan, Zheng81c5a142019-01-01 16:28:33 +080053 struct ceph_mds_session *session;
54 int nr_caps, nr_realms;
Sage Weil20cb34a2010-05-12 15:21:32 -070055 struct ceph_pagelist *pagelist;
Yan, Zheng121f22a2016-07-04 22:05:18 +080056 unsigned msg_version;
Yan, Zheng81c5a142019-01-01 16:28:33 +080057 bool allow_multi;
Sage Weil20cb34a2010-05-12 15:21:32 -070058};
59
Sage Weil2f2dc052009-10-06 11:31:09 -070060static void __wake_requests(struct ceph_mds_client *mdsc,
61 struct list_head *head);
Yan, Zhenge3ec8d62019-01-14 17:21:19 +080062static void ceph_cap_release_work(struct work_struct *work);
Yan, Zheng37c4efc2019-01-31 16:55:51 +080063static void ceph_cap_reclaim_work(struct work_struct *work);
Sage Weil2f2dc052009-10-06 11:31:09 -070064
Tobias Klauser9e327892010-05-20 10:40:19 +020065static const struct ceph_connection_operations mds_con_ops;
Sage Weil2f2dc052009-10-06 11:31:09 -070066
67
68/*
69 * mds reply parsing
70 */
71
Yan, Zhengb37fe1f2019-01-09 10:10:17 +080072static int parse_reply_info_quota(void **p, void *end,
73 struct ceph_mds_reply_info_in *info)
74{
75 u8 struct_v, struct_compat;
76 u32 struct_len;
77
78 ceph_decode_8_safe(p, end, struct_v, bad);
79 ceph_decode_8_safe(p, end, struct_compat, bad);
80 /* struct_v is expected to be >= 1. we only
81 * understand encoding with struct_compat == 1. */
82 if (!struct_v || struct_compat != 1)
83 goto bad;
84 ceph_decode_32_safe(p, end, struct_len, bad);
85 ceph_decode_need(p, end, struct_len, bad);
86 end = *p + struct_len;
87 ceph_decode_64_safe(p, end, info->max_bytes, bad);
88 ceph_decode_64_safe(p, end, info->max_files, bad);
89 *p = end;
90 return 0;
91bad:
92 return -EIO;
93}
94
Sage Weil2f2dc052009-10-06 11:31:09 -070095/*
96 * parse individual inode info
97 */
98static int parse_reply_info_in(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -080099 struct ceph_mds_reply_info_in *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200100 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700101{
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800102 int err = 0;
103 u8 struct_v = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -0700104
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800105 if (features == (u64)-1) {
106 u32 struct_len;
107 u8 struct_compat;
108 ceph_decode_8_safe(p, end, struct_v, bad);
109 ceph_decode_8_safe(p, end, struct_compat, bad);
110 /* struct_v is expected to be >= 1. we only understand
111 * encoding with struct_compat == 1. */
112 if (!struct_v || struct_compat != 1)
113 goto bad;
114 ceph_decode_32_safe(p, end, struct_len, bad);
115 ceph_decode_need(p, end, struct_len, bad);
116 end = *p + struct_len;
117 }
118
119 ceph_decode_need(p, end, sizeof(struct ceph_mds_reply_inode), bad);
Sage Weil2f2dc052009-10-06 11:31:09 -0700120 info->in = *p;
121 *p += sizeof(struct ceph_mds_reply_inode) +
122 sizeof(*info->in->fragtree.splits) *
123 le32_to_cpu(info->in->fragtree.nsplits);
124
125 ceph_decode_32_safe(p, end, info->symlink_len, bad);
126 ceph_decode_need(p, end, info->symlink_len, bad);
127 info->symlink = *p;
128 *p += info->symlink_len;
129
Ilya Dryomov23c625c2018-11-08 14:55:21 +0100130 ceph_decode_copy_safe(p, end, &info->dir_layout,
131 sizeof(info->dir_layout), bad);
Sage Weil2f2dc052009-10-06 11:31:09 -0700132 ceph_decode_32_safe(p, end, info->xattr_len, bad);
133 ceph_decode_need(p, end, info->xattr_len, bad);
134 info->xattr_data = *p;
135 *p += info->xattr_len;
Yan, Zhengfb01d1f2014-11-14 21:29:55 +0800136
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800137 if (features == (u64)-1) {
138 /* inline data */
Yan, Zhengfb01d1f2014-11-14 21:29:55 +0800139 ceph_decode_64_safe(p, end, info->inline_version, bad);
140 ceph_decode_32_safe(p, end, info->inline_len, bad);
141 ceph_decode_need(p, end, info->inline_len, bad);
142 info->inline_data = *p;
143 *p += info->inline_len;
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800144 /* quota */
145 err = parse_reply_info_quota(p, end, info);
146 if (err < 0)
147 goto out_bad;
148 /* pool namespace */
Yan, Zheng5ea5c5e2016-02-14 18:06:41 +0800149 ceph_decode_32_safe(p, end, info->pool_ns_len, bad);
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800150 if (info->pool_ns_len > 0) {
151 ceph_decode_need(p, end, info->pool_ns_len, bad);
152 info->pool_ns_data = *p;
153 *p += info->pool_ns_len;
154 }
Jeff Layton245ce992019-05-29 11:19:42 -0400155
156 /* btime */
157 ceph_decode_need(p, end, sizeof(info->btime), bad);
158 ceph_decode_copy(p, &info->btime, sizeof(info->btime));
159
160 /* change attribute */
Jeff Laytona35ead32019-06-06 07:29:23 -0400161 ceph_decode_64_safe(p, end, info->change_attr, bad);
Yan, Zheng5ea5c5e2016-02-14 18:06:41 +0800162
Yan, Zheng08796872019-01-09 11:07:02 +0800163 /* dir pin */
164 if (struct_v >= 2) {
165 ceph_decode_32_safe(p, end, info->dir_pin, bad);
166 } else {
167 info->dir_pin = -ENODATA;
168 }
169
David Disseldorp193e7b32019-04-18 14:15:46 +0200170 /* snapshot birth time, remains zero for v<=2 */
171 if (struct_v >= 3) {
172 ceph_decode_need(p, end, sizeof(info->snap_btime), bad);
173 ceph_decode_copy(p, &info->snap_btime,
174 sizeof(info->snap_btime));
175 } else {
176 memset(&info->snap_btime, 0, sizeof(info->snap_btime));
177 }
178
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800179 *p = end;
180 } else {
181 if (features & CEPH_FEATURE_MDS_INLINE_DATA) {
182 ceph_decode_64_safe(p, end, info->inline_version, bad);
183 ceph_decode_32_safe(p, end, info->inline_len, bad);
184 ceph_decode_need(p, end, info->inline_len, bad);
185 info->inline_data = *p;
186 *p += info->inline_len;
187 } else
188 info->inline_version = CEPH_INLINE_NONE;
189
190 if (features & CEPH_FEATURE_MDS_QUOTA) {
191 err = parse_reply_info_quota(p, end, info);
192 if (err < 0)
193 goto out_bad;
194 } else {
195 info->max_bytes = 0;
196 info->max_files = 0;
197 }
198
199 info->pool_ns_len = 0;
200 info->pool_ns_data = NULL;
201 if (features & CEPH_FEATURE_FS_FILE_LAYOUT_V2) {
202 ceph_decode_32_safe(p, end, info->pool_ns_len, bad);
203 if (info->pool_ns_len > 0) {
204 ceph_decode_need(p, end, info->pool_ns_len, bad);
205 info->pool_ns_data = *p;
206 *p += info->pool_ns_len;
207 }
208 }
Yan, Zheng08796872019-01-09 11:07:02 +0800209
Jeff Layton245ce992019-05-29 11:19:42 -0400210 if (features & CEPH_FEATURE_FS_BTIME) {
211 ceph_decode_need(p, end, sizeof(info->btime), bad);
212 ceph_decode_copy(p, &info->btime, sizeof(info->btime));
Jeff Laytona35ead32019-06-06 07:29:23 -0400213 ceph_decode_64_safe(p, end, info->change_attr, bad);
Jeff Layton245ce992019-05-29 11:19:42 -0400214 }
215
Yan, Zheng08796872019-01-09 11:07:02 +0800216 info->dir_pin = -ENODATA;
David Disseldorp193e7b32019-04-18 14:15:46 +0200217 /* info->snap_btime remains zero */
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800218 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700219 return 0;
220bad:
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800221 err = -EIO;
222out_bad:
Sage Weil2f2dc052009-10-06 11:31:09 -0700223 return err;
224}
225
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800226static int parse_reply_info_dir(void **p, void *end,
227 struct ceph_mds_reply_dirfrag **dirfrag,
228 u64 features)
229{
230 if (features == (u64)-1) {
231 u8 struct_v, struct_compat;
232 u32 struct_len;
233 ceph_decode_8_safe(p, end, struct_v, bad);
234 ceph_decode_8_safe(p, end, struct_compat, bad);
235 /* struct_v is expected to be >= 1. we only understand
236 * encoding whose struct_compat == 1. */
237 if (!struct_v || struct_compat != 1)
238 goto bad;
239 ceph_decode_32_safe(p, end, struct_len, bad);
240 ceph_decode_need(p, end, struct_len, bad);
241 end = *p + struct_len;
242 }
243
244 ceph_decode_need(p, end, sizeof(**dirfrag), bad);
245 *dirfrag = *p;
246 *p += sizeof(**dirfrag) + sizeof(u32) * le32_to_cpu((*dirfrag)->ndist);
247 if (unlikely(*p > end))
248 goto bad;
249 if (features == (u64)-1)
250 *p = end;
251 return 0;
252bad:
253 return -EIO;
254}
255
256static int parse_reply_info_lease(void **p, void *end,
257 struct ceph_mds_reply_lease **lease,
258 u64 features)
259{
260 if (features == (u64)-1) {
261 u8 struct_v, struct_compat;
262 u32 struct_len;
263 ceph_decode_8_safe(p, end, struct_v, bad);
264 ceph_decode_8_safe(p, end, struct_compat, bad);
265 /* struct_v is expected to be >= 1. we only understand
266 * encoding whose struct_compat == 1. */
267 if (!struct_v || struct_compat != 1)
268 goto bad;
269 ceph_decode_32_safe(p, end, struct_len, bad);
270 ceph_decode_need(p, end, struct_len, bad);
271 end = *p + struct_len;
272 }
273
274 ceph_decode_need(p, end, sizeof(**lease), bad);
275 *lease = *p;
276 *p += sizeof(**lease);
277 if (features == (u64)-1)
278 *p = end;
279 return 0;
280bad:
281 return -EIO;
282}
283
Sage Weil2f2dc052009-10-06 11:31:09 -0700284/*
285 * parse a normal reply, which may contain a (dir+)dentry and/or a
286 * target inode.
287 */
288static int parse_reply_info_trace(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800289 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200290 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700291{
292 int err;
293
294 if (info->head->is_dentry) {
Sage Weil14303d22010-12-14 17:37:52 -0800295 err = parse_reply_info_in(p, end, &info->diri, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700296 if (err < 0)
297 goto out_bad;
298
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800299 err = parse_reply_info_dir(p, end, &info->dirfrag, features);
300 if (err < 0)
301 goto out_bad;
Sage Weil2f2dc052009-10-06 11:31:09 -0700302
303 ceph_decode_32_safe(p, end, info->dname_len, bad);
304 ceph_decode_need(p, end, info->dname_len, bad);
305 info->dname = *p;
306 *p += info->dname_len;
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800307
308 err = parse_reply_info_lease(p, end, &info->dlease, features);
309 if (err < 0)
310 goto out_bad;
Sage Weil2f2dc052009-10-06 11:31:09 -0700311 }
312
313 if (info->head->is_target) {
Sage Weil14303d22010-12-14 17:37:52 -0800314 err = parse_reply_info_in(p, end, &info->targeti, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700315 if (err < 0)
316 goto out_bad;
317 }
318
319 if (unlikely(*p != end))
320 goto bad;
321 return 0;
322
323bad:
324 err = -EIO;
325out_bad:
326 pr_err("problem parsing mds trace %d\n", err);
327 return err;
328}
329
330/*
331 * parse readdir results
332 */
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800333static int parse_reply_info_readdir(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800334 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200335 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700336{
337 u32 num, i = 0;
338 int err;
339
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800340 err = parse_reply_info_dir(p, end, &info->dir_dir, features);
341 if (err < 0)
342 goto out_bad;
Sage Weil2f2dc052009-10-06 11:31:09 -0700343
344 ceph_decode_need(p, end, sizeof(num) + 2, bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700345 num = ceph_decode_32(p);
Yan, Zheng956d39d2016-04-27 17:48:30 +0800346 {
347 u16 flags = ceph_decode_16(p);
348 info->dir_end = !!(flags & CEPH_READDIR_FRAG_END);
349 info->dir_complete = !!(flags & CEPH_READDIR_FRAG_COMPLETE);
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800350 info->hash_order = !!(flags & CEPH_READDIR_HASH_ORDER);
Yan, Zheng79162542017-04-05 12:54:05 -0400351 info->offset_hash = !!(flags & CEPH_READDIR_OFFSET_HASH);
Yan, Zheng956d39d2016-04-27 17:48:30 +0800352 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700353 if (num == 0)
354 goto done;
355
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800356 BUG_ON(!info->dir_entries);
357 if ((unsigned long)(info->dir_entries + num) >
358 (unsigned long)info->dir_entries + info->dir_buf_size) {
Yan, Zheng54008392014-03-29 13:41:15 +0800359 pr_err("dir contents are larger than expected\n");
360 WARN_ON(1);
361 goto bad;
362 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700363
Yan, Zheng54008392014-03-29 13:41:15 +0800364 info->dir_nr = num;
Sage Weil2f2dc052009-10-06 11:31:09 -0700365 while (num) {
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800366 struct ceph_mds_reply_dir_entry *rde = info->dir_entries + i;
Sage Weil2f2dc052009-10-06 11:31:09 -0700367 /* dentry */
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800368 ceph_decode_32_safe(p, end, rde->name_len, bad);
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800369 ceph_decode_need(p, end, rde->name_len, bad);
370 rde->name = *p;
371 *p += rde->name_len;
372 dout("parsed dir dname '%.*s'\n", rde->name_len, rde->name);
Sage Weil2f2dc052009-10-06 11:31:09 -0700373
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800374 /* dentry lease */
375 err = parse_reply_info_lease(p, end, &rde->lease, features);
376 if (err)
377 goto out_bad;
Sage Weil2f2dc052009-10-06 11:31:09 -0700378 /* inode */
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800379 err = parse_reply_info_in(p, end, &rde->inode, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700380 if (err < 0)
381 goto out_bad;
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800382 /* ceph_readdir_prepopulate() will update it */
383 rde->offset = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -0700384 i++;
385 num--;
386 }
387
388done:
Jeff Layton1d3f8722019-09-26 16:05:11 -0400389 /* Skip over any unrecognized fields */
390 *p = end;
Sage Weil2f2dc052009-10-06 11:31:09 -0700391 return 0;
392
393bad:
394 err = -EIO;
395out_bad:
396 pr_err("problem parsing dir contents %d\n", err);
397 return err;
398}
399
400/*
Herb Shiu25933ab2010-12-01 14:14:38 -0800401 * parse fcntl F_GETLK results
402 */
403static int parse_reply_info_filelock(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800404 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200405 u64 features)
Herb Shiu25933ab2010-12-01 14:14:38 -0800406{
407 if (*p + sizeof(*info->filelock_reply) > end)
408 goto bad;
409
410 info->filelock_reply = *p;
Herb Shiu25933ab2010-12-01 14:14:38 -0800411
Jeff Layton1d3f8722019-09-26 16:05:11 -0400412 /* Skip over any unrecognized fields */
413 *p = end;
Herb Shiu25933ab2010-12-01 14:14:38 -0800414 return 0;
Herb Shiu25933ab2010-12-01 14:14:38 -0800415bad:
416 return -EIO;
417}
418
Jeff Laytond4846482019-11-15 11:51:55 -0500419
420#if BITS_PER_LONG == 64
421
422#define DELEGATED_INO_AVAILABLE xa_mk_value(1)
423
424static int ceph_parse_deleg_inos(void **p, void *end,
425 struct ceph_mds_session *s)
426{
427 u32 sets;
428
429 ceph_decode_32_safe(p, end, sets, bad);
430 dout("got %u sets of delegated inodes\n", sets);
431 while (sets--) {
432 u64 start, len, ino;
433
434 ceph_decode_64_safe(p, end, start, bad);
435 ceph_decode_64_safe(p, end, len, bad);
436 while (len--) {
437 int err = xa_insert(&s->s_delegated_inos, ino = start++,
438 DELEGATED_INO_AVAILABLE,
439 GFP_KERNEL);
440 if (!err) {
441 dout("added delegated inode 0x%llx\n",
442 start - 1);
443 } else if (err == -EBUSY) {
444 pr_warn("ceph: MDS delegated inode 0x%llx more than once.\n",
445 start - 1);
446 } else {
447 return err;
448 }
449 }
450 }
451 return 0;
452bad:
453 return -EIO;
454}
455
456u64 ceph_get_deleg_ino(struct ceph_mds_session *s)
457{
458 unsigned long ino;
459 void *val;
460
461 xa_for_each(&s->s_delegated_inos, ino, val) {
462 val = xa_erase(&s->s_delegated_inos, ino);
463 if (val == DELEGATED_INO_AVAILABLE)
464 return ino;
465 }
466 return 0;
467}
468
469int ceph_restore_deleg_ino(struct ceph_mds_session *s, u64 ino)
470{
471 return xa_insert(&s->s_delegated_inos, ino, DELEGATED_INO_AVAILABLE,
472 GFP_KERNEL);
473}
474#else /* BITS_PER_LONG == 64 */
475/*
476 * FIXME: xarrays can't handle 64-bit indexes on a 32-bit arch. For now, just
477 * ignore delegated_inos on 32 bit arch. Maybe eventually add xarrays for top
478 * and bottom words?
479 */
480static int ceph_parse_deleg_inos(void **p, void *end,
481 struct ceph_mds_session *s)
482{
483 u32 sets;
484
485 ceph_decode_32_safe(p, end, sets, bad);
486 if (sets)
487 ceph_decode_skip_n(p, end, sets * 2 * sizeof(__le64), bad);
488 return 0;
489bad:
490 return -EIO;
491}
492
493u64 ceph_get_deleg_ino(struct ceph_mds_session *s)
494{
495 return 0;
496}
497
498int ceph_restore_deleg_ino(struct ceph_mds_session *s, u64 ino)
499{
500 return 0;
501}
502#endif /* BITS_PER_LONG == 64 */
503
Herb Shiu25933ab2010-12-01 14:14:38 -0800504/*
Sam Lang6e8575f2012-12-28 09:56:46 -0800505 * parse create results
506 */
507static int parse_reply_info_create(void **p, void *end,
508 struct ceph_mds_reply_info_parsed *info,
Jeff Laytond4846482019-11-15 11:51:55 -0500509 u64 features, struct ceph_mds_session *s)
Sam Lang6e8575f2012-12-28 09:56:46 -0800510{
Jeff Laytond4846482019-11-15 11:51:55 -0500511 int ret;
512
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800513 if (features == (u64)-1 ||
514 (features & CEPH_FEATURE_REPLY_CREATE_INODE)) {
Sam Lang6e8575f2012-12-28 09:56:46 -0800515 if (*p == end) {
Jeff Laytond4846482019-11-15 11:51:55 -0500516 /* Malformed reply? */
Sam Lang6e8575f2012-12-28 09:56:46 -0800517 info->has_create_ino = false;
Jeff Laytond4846482019-11-15 11:51:55 -0500518 } else if (test_bit(CEPHFS_FEATURE_DELEG_INO, &s->s_features)) {
519 u8 struct_v, struct_compat;
520 u32 len;
521
Sam Lang6e8575f2012-12-28 09:56:46 -0800522 info->has_create_ino = true;
Jeff Laytond4846482019-11-15 11:51:55 -0500523 ceph_decode_8_safe(p, end, struct_v, bad);
524 ceph_decode_8_safe(p, end, struct_compat, bad);
525 ceph_decode_32_safe(p, end, len, bad);
Jeff Layton1d3f8722019-09-26 16:05:11 -0400526 ceph_decode_64_safe(p, end, info->ino, bad);
Jeff Laytond4846482019-11-15 11:51:55 -0500527 ret = ceph_parse_deleg_inos(p, end, s);
528 if (ret)
529 return ret;
530 } else {
531 /* legacy */
532 ceph_decode_64_safe(p, end, info->ino, bad);
533 info->has_create_ino = true;
Sam Lang6e8575f2012-12-28 09:56:46 -0800534 }
Jeff Layton1d3f8722019-09-26 16:05:11 -0400535 } else {
536 if (*p != end)
537 goto bad;
Sam Lang6e8575f2012-12-28 09:56:46 -0800538 }
539
Jeff Layton1d3f8722019-09-26 16:05:11 -0400540 /* Skip over any unrecognized fields */
541 *p = end;
Sam Lang6e8575f2012-12-28 09:56:46 -0800542 return 0;
Sam Lang6e8575f2012-12-28 09:56:46 -0800543bad:
544 return -EIO;
545}
546
547/*
Herb Shiu25933ab2010-12-01 14:14:38 -0800548 * parse extra results
549 */
550static int parse_reply_info_extra(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800551 struct ceph_mds_reply_info_parsed *info,
Jeff Laytond4846482019-11-15 11:51:55 -0500552 u64 features, struct ceph_mds_session *s)
Herb Shiu25933ab2010-12-01 14:14:38 -0800553{
Jeff Layton6df8c9d2017-01-12 14:42:41 -0500554 u32 op = le32_to_cpu(info->head->op);
555
556 if (op == CEPH_MDS_OP_GETFILELOCK)
Sage Weil14303d22010-12-14 17:37:52 -0800557 return parse_reply_info_filelock(p, end, info, features);
Jeff Layton6df8c9d2017-01-12 14:42:41 -0500558 else if (op == CEPH_MDS_OP_READDIR || op == CEPH_MDS_OP_LSSNAP)
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800559 return parse_reply_info_readdir(p, end, info, features);
Jeff Layton6df8c9d2017-01-12 14:42:41 -0500560 else if (op == CEPH_MDS_OP_CREATE)
Jeff Laytond4846482019-11-15 11:51:55 -0500561 return parse_reply_info_create(p, end, info, features, s);
Sam Lang6e8575f2012-12-28 09:56:46 -0800562 else
563 return -EIO;
Herb Shiu25933ab2010-12-01 14:14:38 -0800564}
565
566/*
Sage Weil2f2dc052009-10-06 11:31:09 -0700567 * parse entire mds reply
568 */
Jeff Laytond4846482019-11-15 11:51:55 -0500569static int parse_reply_info(struct ceph_mds_session *s, struct ceph_msg *msg,
Sage Weil14303d22010-12-14 17:37:52 -0800570 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200571 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700572{
573 void *p, *end;
574 u32 len;
575 int err;
576
577 info->head = msg->front.iov_base;
578 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
579 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
580
581 /* trace */
582 ceph_decode_32_safe(&p, end, len, bad);
583 if (len > 0) {
Xi Wang32852a82012-01-14 22:20:59 -0500584 ceph_decode_need(&p, end, len, bad);
Sage Weil14303d22010-12-14 17:37:52 -0800585 err = parse_reply_info_trace(&p, p+len, info, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700586 if (err < 0)
587 goto out_bad;
588 }
589
Herb Shiu25933ab2010-12-01 14:14:38 -0800590 /* extra */
Sage Weil2f2dc052009-10-06 11:31:09 -0700591 ceph_decode_32_safe(&p, end, len, bad);
592 if (len > 0) {
Xi Wang32852a82012-01-14 22:20:59 -0500593 ceph_decode_need(&p, end, len, bad);
Jeff Laytond4846482019-11-15 11:51:55 -0500594 err = parse_reply_info_extra(&p, p+len, info, features, s);
Sage Weil2f2dc052009-10-06 11:31:09 -0700595 if (err < 0)
596 goto out_bad;
597 }
598
599 /* snap blob */
600 ceph_decode_32_safe(&p, end, len, bad);
601 info->snapblob_len = len;
602 info->snapblob = p;
603 p += len;
604
605 if (p != end)
606 goto bad;
607 return 0;
608
609bad:
610 err = -EIO;
611out_bad:
612 pr_err("mds parse_reply err %d\n", err);
613 return err;
614}
615
616static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
617{
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800618 if (!info->dir_entries)
Yan, Zheng54008392014-03-29 13:41:15 +0800619 return;
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800620 free_pages((unsigned long)info->dir_entries, get_order(info->dir_buf_size));
Sage Weil2f2dc052009-10-06 11:31:09 -0700621}
622
623
624/*
625 * sessions
626 */
John Spraya687eca2014-09-19 13:51:08 +0100627const char *ceph_session_state_name(int s)
Sage Weil2f2dc052009-10-06 11:31:09 -0700628{
629 switch (s) {
630 case CEPH_MDS_SESSION_NEW: return "new";
631 case CEPH_MDS_SESSION_OPENING: return "opening";
632 case CEPH_MDS_SESSION_OPEN: return "open";
633 case CEPH_MDS_SESSION_HUNG: return "hung";
634 case CEPH_MDS_SESSION_CLOSING: return "closing";
Xiubo Li4d681c22019-12-05 22:35:51 -0500635 case CEPH_MDS_SESSION_CLOSED: return "closed";
Sage Weil44ca18f2010-02-15 12:08:46 -0800636 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
Sage Weil2f2dc052009-10-06 11:31:09 -0700637 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
Yan, Zhengfcff4152016-09-14 16:39:51 +0800638 case CEPH_MDS_SESSION_REJECTED: return "rejected";
Sage Weil2f2dc052009-10-06 11:31:09 -0700639 default: return "???";
640 }
641}
642
Xiubo Li5b3248c2019-12-19 19:44:09 -0500643struct ceph_mds_session *ceph_get_mds_session(struct ceph_mds_session *s)
Sage Weil2f2dc052009-10-06 11:31:09 -0700644{
Elena Reshetova3997c012017-03-03 11:15:06 +0200645 if (refcount_inc_not_zero(&s->s_ref)) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700646 dout("mdsc get_session %p %d -> %d\n", s,
Elena Reshetova3997c012017-03-03 11:15:06 +0200647 refcount_read(&s->s_ref)-1, refcount_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -0700648 return s;
649 } else {
Chengguang Xu4c069a52018-01-30 16:29:17 +0800650 dout("mdsc get_session %p 0 -- FAIL\n", s);
Sage Weil2f2dc052009-10-06 11:31:09 -0700651 return NULL;
652 }
653}
654
655void ceph_put_mds_session(struct ceph_mds_session *s)
656{
657 dout("mdsc put_session %p %d -> %d\n", s,
Elena Reshetova3997c012017-03-03 11:15:06 +0200658 refcount_read(&s->s_ref), refcount_read(&s->s_ref)-1);
659 if (refcount_dec_and_test(&s->s_ref)) {
Alex Elder6c4a1912012-05-16 15:16:38 -0500660 if (s->s_auth.authorizer)
Ilya Dryomov6c1ea262016-04-11 19:34:49 +0200661 ceph_auth_destroy_authorizer(s->s_auth.authorizer);
Jeff Layton88828192020-03-20 17:07:36 -0400662 WARN_ON(mutex_is_locked(&s->s_mutex));
Jeff Laytond4846482019-11-15 11:51:55 -0500663 xa_destroy(&s->s_delegated_inos);
Sage Weil2f2dc052009-10-06 11:31:09 -0700664 kfree(s);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800665 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700666}
667
668/*
669 * called under mdsc->mutex
670 */
671struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
672 int mds)
673{
Markus Elfringd37b1d92017-08-20 20:22:02 +0200674 if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
Sage Weil2f2dc052009-10-06 11:31:09 -0700675 return NULL;
Xiubo Li5b3248c2019-12-19 19:44:09 -0500676 return ceph_get_mds_session(mdsc->sessions[mds]);
Sage Weil2f2dc052009-10-06 11:31:09 -0700677}
678
679static bool __have_session(struct ceph_mds_client *mdsc, int mds)
680{
Chengguang Xu98cfda82018-03-13 10:43:45 +0800681 if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
Sage Weil2f2dc052009-10-06 11:31:09 -0700682 return false;
Chengguang Xu98cfda82018-03-13 10:43:45 +0800683 else
684 return true;
Sage Weil2f2dc052009-10-06 11:31:09 -0700685}
686
Sage Weil2600d2d2010-02-22 15:12:16 -0800687static int __verify_registered_session(struct ceph_mds_client *mdsc,
688 struct ceph_mds_session *s)
689{
690 if (s->s_mds >= mdsc->max_sessions ||
691 mdsc->sessions[s->s_mds] != s)
692 return -ENOENT;
693 return 0;
694}
695
Sage Weil2f2dc052009-10-06 11:31:09 -0700696/*
697 * create+register a new session for given mds.
698 * called under mdsc->mutex.
699 */
700static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
701 int mds)
702{
703 struct ceph_mds_session *s;
704
Xiubo Lib38c9eb2019-12-04 06:57:39 -0500705 if (mds >= mdsc->mdsmap->possible_max_rank)
Nathaniel Yazdanic338c072013-08-04 21:04:30 -0700706 return ERR_PTR(-EINVAL);
707
Sage Weil2f2dc052009-10-06 11:31:09 -0700708 s = kzalloc(sizeof(*s), GFP_NOFS);
Dan Carpenter4736b002010-03-20 15:30:16 +0300709 if (!s)
710 return ERR_PTR(-ENOMEM);
Chengguang Xu47474d02018-03-13 23:01:07 +0800711
712 if (mds >= mdsc->max_sessions) {
713 int newmax = 1 << get_count_order(mds + 1);
714 struct ceph_mds_session **sa;
715
716 dout("%s: realloc to %d\n", __func__, newmax);
717 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
718 if (!sa)
719 goto fail_realloc;
720 if (mdsc->sessions) {
721 memcpy(sa, mdsc->sessions,
722 mdsc->max_sessions * sizeof(void *));
723 kfree(mdsc->sessions);
724 }
725 mdsc->sessions = sa;
726 mdsc->max_sessions = newmax;
727 }
728
729 dout("%s: mds%d\n", __func__, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -0700730 s->s_mdsc = mdsc;
731 s->s_mds = mds;
732 s->s_state = CEPH_MDS_SESSION_NEW;
733 s->s_ttl = 0;
734 s->s_seq = 0;
735 mutex_init(&s->s_mutex);
736
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700737 ceph_con_init(&s->s_con, s, &mds_con_ops, &mdsc->fsc->client->msgr);
Sage Weil2f2dc052009-10-06 11:31:09 -0700738
Alex Elderd8fb02a2012-01-12 17:48:10 -0800739 spin_lock_init(&s->s_gen_ttl_lock);
Yan, Zheng1e9c2eb2019-01-28 20:43:55 +0800740 s->s_cap_gen = 1;
Alex Elder1ce208a2012-01-12 17:48:11 -0800741 s->s_cap_ttl = jiffies - 1;
Alex Elderd8fb02a2012-01-12 17:48:10 -0800742
743 spin_lock_init(&s->s_cap_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700744 s->s_renew_requested = 0;
745 s->s_renew_seq = 0;
746 INIT_LIST_HEAD(&s->s_caps);
747 s->s_nr_caps = 0;
Elena Reshetova3997c012017-03-03 11:15:06 +0200748 refcount_set(&s->s_ref, 1);
Sage Weil2f2dc052009-10-06 11:31:09 -0700749 INIT_LIST_HEAD(&s->s_waiting);
750 INIT_LIST_HEAD(&s->s_unsafe);
Jeff Laytond4846482019-11-15 11:51:55 -0500751 xa_init(&s->s_delegated_inos);
Sage Weil2f2dc052009-10-06 11:31:09 -0700752 s->s_num_cap_releases = 0;
Yan, Zheng99a9c272013-09-22 11:08:14 +0800753 s->s_cap_reconnect = 0;
Sage Weil7c1332b2010-02-16 11:39:45 -0800754 s->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700755 INIT_LIST_HEAD(&s->s_cap_releases);
Yan, Zhenge3ec8d62019-01-14 17:21:19 +0800756 INIT_WORK(&s->s_cap_release_work, ceph_cap_release_work);
757
Jeff Layton1cf03a62020-04-01 17:07:52 -0400758 INIT_LIST_HEAD(&s->s_cap_dirty);
Sage Weil2f2dc052009-10-06 11:31:09 -0700759 INIT_LIST_HEAD(&s->s_cap_flushing);
Sage Weil2f2dc052009-10-06 11:31:09 -0700760
Sage Weil2f2dc052009-10-06 11:31:09 -0700761 mdsc->sessions[mds] = s;
Yan, Zheng86d8f672015-01-09 17:00:42 +0800762 atomic_inc(&mdsc->num_sessions);
Elena Reshetova3997c012017-03-03 11:15:06 +0200763 refcount_inc(&s->s_ref); /* one ref to sessions[], one to caller */
Sage Weil42ce56e2009-11-18 11:22:36 -0800764
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700765 ceph_con_open(&s->s_con, CEPH_ENTITY_TYPE_MDS, mds,
766 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
Sage Weil42ce56e2009-11-18 11:22:36 -0800767
Sage Weil2f2dc052009-10-06 11:31:09 -0700768 return s;
Sage Weil42ce56e2009-11-18 11:22:36 -0800769
770fail_realloc:
771 kfree(s);
772 return ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -0700773}
774
775/*
776 * called under mdsc->mutex
777 */
Sage Weil2600d2d2010-02-22 15:12:16 -0800778static void __unregister_session(struct ceph_mds_client *mdsc,
Sage Weil42ce56e2009-11-18 11:22:36 -0800779 struct ceph_mds_session *s)
Sage Weil2f2dc052009-10-06 11:31:09 -0700780{
Sage Weil2600d2d2010-02-22 15:12:16 -0800781 dout("__unregister_session mds%d %p\n", s->s_mds, s);
782 BUG_ON(mdsc->sessions[s->s_mds] != s);
Sage Weil42ce56e2009-11-18 11:22:36 -0800783 mdsc->sessions[s->s_mds] = NULL;
784 ceph_con_close(&s->s_con);
785 ceph_put_mds_session(s);
Yan, Zheng86d8f672015-01-09 17:00:42 +0800786 atomic_dec(&mdsc->num_sessions);
Sage Weil2f2dc052009-10-06 11:31:09 -0700787}
788
789/*
790 * drop session refs in request.
791 *
792 * should be last request ref, or hold mdsc->mutex
793 */
794static void put_request_session(struct ceph_mds_request *req)
795{
796 if (req->r_session) {
797 ceph_put_mds_session(req->r_session);
798 req->r_session = NULL;
799 }
800}
801
Sage Weil153c8e62009-12-07 12:31:09 -0800802void ceph_mdsc_release_request(struct kref *kref)
Sage Weil2f2dc052009-10-06 11:31:09 -0700803{
Sage Weil153c8e62009-12-07 12:31:09 -0800804 struct ceph_mds_request *req = container_of(kref,
805 struct ceph_mds_request,
806 r_kref);
Xiubo Lie64f44a2020-05-27 09:09:27 -0400807 ceph_mdsc_release_dir_caps_no_check(req);
Yan, Zheng54008392014-03-29 13:41:15 +0800808 destroy_reply_info(&req->r_reply_info);
Sage Weil153c8e62009-12-07 12:31:09 -0800809 if (req->r_request)
810 ceph_msg_put(req->r_request);
Yan, Zheng54008392014-03-29 13:41:15 +0800811 if (req->r_reply)
Sage Weil153c8e62009-12-07 12:31:09 -0800812 ceph_msg_put(req->r_reply);
Sage Weil153c8e62009-12-07 12:31:09 -0800813 if (req->r_inode) {
Sage Weil41b02e12011-07-26 11:31:14 -0700814 ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
Yan, Zheng3e1d0452019-05-18 20:39:55 +0800815 /* avoid calling iput_final() in mds dispatch threads */
816 ceph_async_iput(req->r_inode);
Sage Weil153c8e62009-12-07 12:31:09 -0800817 }
Jeff Layton9c1c2b32019-04-03 13:16:01 -0400818 if (req->r_parent) {
Jeff Layton3dd69aa2017-01-31 10:28:26 -0500819 ceph_put_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
Jeff Layton9c1c2b32019-04-03 13:16:01 -0400820 ceph_async_iput(req->r_parent);
821 }
Yan, Zheng3e1d0452019-05-18 20:39:55 +0800822 ceph_async_iput(req->r_target_inode);
Sage Weil153c8e62009-12-07 12:31:09 -0800823 if (req->r_dentry)
824 dput(req->r_dentry);
Sage Weil844d87c2013-02-05 13:40:09 -0800825 if (req->r_old_dentry)
826 dput(req->r_old_dentry);
827 if (req->r_old_dentry_dir) {
Sage Weil41b02e12011-07-26 11:31:14 -0700828 /*
829 * track (and drop pins for) r_old_dentry_dir
830 * separately, since r_old_dentry's d_parent may have
831 * changed between the dir mutex being dropped and
832 * this request being freed.
833 */
834 ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir),
835 CEPH_CAP_PIN);
Yan, Zheng3e1d0452019-05-18 20:39:55 +0800836 ceph_async_iput(req->r_old_dentry_dir);
Sage Weil153c8e62009-12-07 12:31:09 -0800837 }
838 kfree(req->r_path1);
839 kfree(req->r_path2);
Yan, Zheng25e6bae2014-09-16 19:15:28 +0800840 if (req->r_pagelist)
841 ceph_pagelist_release(req->r_pagelist);
Sage Weil153c8e62009-12-07 12:31:09 -0800842 put_request_session(req);
Yehuda Sadeh37151662010-06-17 16:16:12 -0700843 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
Yan, Zheng428138c2019-06-14 10:55:05 +0800844 WARN_ON_ONCE(!list_empty(&req->r_wait));
Jeff Layton058daab2020-02-17 18:38:37 -0500845 kmem_cache_free(ceph_mds_request_cachep, req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700846}
847
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200848DEFINE_RB_FUNCS(request, struct ceph_mds_request, r_tid, r_node)
849
Sage Weil2f2dc052009-10-06 11:31:09 -0700850/*
851 * lookup session, bump ref if found.
852 *
853 * called under mdsc->mutex.
854 */
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200855static struct ceph_mds_request *
856lookup_get_request(struct ceph_mds_client *mdsc, u64 tid)
Sage Weil2f2dc052009-10-06 11:31:09 -0700857{
858 struct ceph_mds_request *req;
Sage Weil44ca18f2010-02-15 12:08:46 -0800859
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200860 req = lookup_request(&mdsc->request_tree, tid);
861 if (req)
862 ceph_mdsc_get_request(req);
Sage Weil44ca18f2010-02-15 12:08:46 -0800863
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200864 return req;
Sage Weil2f2dc052009-10-06 11:31:09 -0700865}
866
867/*
868 * Register an in-flight request, and assign a tid. Link to directory
869 * are modifying (if any).
870 *
871 * Called under mdsc->mutex.
872 */
873static void __register_request(struct ceph_mds_client *mdsc,
874 struct ceph_mds_request *req,
875 struct inode *dir)
876{
Zhi Zhange30ee582018-01-24 21:24:33 +0800877 int ret = 0;
878
Sage Weil2f2dc052009-10-06 11:31:09 -0700879 req->r_tid = ++mdsc->last_tid;
Zhi Zhange30ee582018-01-24 21:24:33 +0800880 if (req->r_num_caps) {
881 ret = ceph_reserve_caps(mdsc, &req->r_caps_reservation,
882 req->r_num_caps);
883 if (ret < 0) {
884 pr_err("__register_request %p "
885 "failed to reserve caps: %d\n", req, ret);
886 /* set req->r_err to fail early from __do_request */
887 req->r_err = ret;
888 return;
889 }
890 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700891 dout("__register_request %p tid %lld\n", req, req->r_tid);
892 ceph_mdsc_get_request(req);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200893 insert_request(&mdsc->request_tree, req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700894
Sage Weilcb4276c2010-11-08 07:28:52 -0800895 req->r_uid = current_fsuid();
896 req->r_gid = current_fsgid();
897
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +0800898 if (mdsc->oldest_tid == 0 && req->r_op != CEPH_MDS_OP_SETFILELOCK)
899 mdsc->oldest_tid = req->r_tid;
900
Sage Weil2f2dc052009-10-06 11:31:09 -0700901 if (dir) {
Jeff Layton3db0a2f2019-04-04 08:05:38 -0400902 struct ceph_inode_info *ci = ceph_inode(dir);
903
Sage Weil3b663782011-05-18 16:12:12 -0700904 ihold(dir);
Sage Weil2f2dc052009-10-06 11:31:09 -0700905 req->r_unsafe_dir = dir;
Jeff Layton3db0a2f2019-04-04 08:05:38 -0400906 spin_lock(&ci->i_unsafe_lock);
907 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
908 spin_unlock(&ci->i_unsafe_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700909 }
910}
911
912static void __unregister_request(struct ceph_mds_client *mdsc,
913 struct ceph_mds_request *req)
914{
915 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +0800916
Jeff Laytondf963ea2017-02-14 10:09:40 -0500917 /* Never leave an unregistered request on an unsafe list! */
918 list_del_init(&req->r_unsafe_item);
919
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +0800920 if (req->r_tid == mdsc->oldest_tid) {
921 struct rb_node *p = rb_next(&req->r_node);
922 mdsc->oldest_tid = 0;
923 while (p) {
924 struct ceph_mds_request *next_req =
925 rb_entry(p, struct ceph_mds_request, r_node);
926 if (next_req->r_op != CEPH_MDS_OP_SETFILELOCK) {
927 mdsc->oldest_tid = next_req->r_tid;
928 break;
929 }
930 p = rb_next(p);
931 }
932 }
933
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200934 erase_request(&mdsc->request_tree, req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700935
Jeff Layton3db0a2f2019-04-04 08:05:38 -0400936 if (req->r_unsafe_dir) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700937 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
Sage Weil2f2dc052009-10-06 11:31:09 -0700938 spin_lock(&ci->i_unsafe_lock);
939 list_del_init(&req->r_unsafe_dir_item);
940 spin_unlock(&ci->i_unsafe_lock);
Yan, Zheng4c06ace2015-10-27 17:18:00 +0800941 }
Jeff Laytonbc2de10d2017-02-01 13:49:09 -0500942 if (req->r_target_inode &&
943 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Yan, Zheng68cd5b42015-10-27 18:36:06 +0800944 struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
945 spin_lock(&ci->i_unsafe_lock);
946 list_del_init(&req->r_unsafe_target_item);
947 spin_unlock(&ci->i_unsafe_lock);
948 }
Sage Weil3b663782011-05-18 16:12:12 -0700949
Yan, Zheng4c06ace2015-10-27 17:18:00 +0800950 if (req->r_unsafe_dir) {
Yan, Zheng3e1d0452019-05-18 20:39:55 +0800951 /* avoid calling iput_final() in mds dispatch threads */
952 ceph_async_iput(req->r_unsafe_dir);
Sage Weil3b663782011-05-18 16:12:12 -0700953 req->r_unsafe_dir = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700954 }
Sage Weil94aa8ae2010-03-28 21:22:50 -0700955
Yan, Zhengfc55d2c2013-10-31 09:10:47 +0800956 complete_all(&req->r_safe_completion);
957
Sage Weil94aa8ae2010-03-28 21:22:50 -0700958 ceph_mdsc_put_request(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700959}
960
961/*
Jeff Layton30c71232016-12-15 08:37:56 -0500962 * Walk back up the dentry tree until we hit a dentry representing a
963 * non-snapshot inode. We do this using the rcu_read_lock (which must be held
964 * when calling this) to ensure that the objects won't disappear while we're
965 * working with them. Once we hit a candidate dentry, we attempt to take a
966 * reference to it, and return that as the result.
967 */
Dan Carpenterf1075482017-02-23 13:39:59 +0300968static struct inode *get_nonsnap_parent(struct dentry *dentry)
969{
970 struct inode *inode = NULL;
Jeff Layton30c71232016-12-15 08:37:56 -0500971
972 while (dentry && !IS_ROOT(dentry)) {
973 inode = d_inode_rcu(dentry);
974 if (!inode || ceph_snap(inode) == CEPH_NOSNAP)
975 break;
976 dentry = dentry->d_parent;
977 }
978 if (inode)
979 inode = igrab(inode);
980 return inode;
981}
982
983/*
Sage Weil2f2dc052009-10-06 11:31:09 -0700984 * Choose mds to send request to next. If there is a hint set in the
985 * request (e.g., due to a prior forward hint from the mds), use that.
986 * Otherwise, consult frag tree and/or caps to identify the
987 * appropriate mds. If all else fails, choose randomly.
988 *
989 * Called under mdsc->mutex.
990 */
991static int __choose_mds(struct ceph_mds_client *mdsc,
Xiubo Lic4853e92019-12-09 07:47:15 -0500992 struct ceph_mds_request *req,
993 bool *random)
Sage Weil2f2dc052009-10-06 11:31:09 -0700994{
995 struct inode *inode;
996 struct ceph_inode_info *ci;
997 struct ceph_cap *cap;
998 int mode = req->r_direct_mode;
999 int mds = -1;
1000 u32 hash = req->r_direct_hash;
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05001001 bool is_hash = test_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags);
Sage Weil2f2dc052009-10-06 11:31:09 -07001002
Xiubo Lic4853e92019-12-09 07:47:15 -05001003 if (random)
1004 *random = false;
1005
Sage Weil2f2dc052009-10-06 11:31:09 -07001006 /*
1007 * is there a specific mds we should try? ignore hint if we have
1008 * no session and the mds is not up (active or recovering).
1009 */
1010 if (req->r_resend_mds >= 0 &&
1011 (__have_session(mdsc, req->r_resend_mds) ||
1012 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
Xiubo Li3c802092020-01-01 22:09:37 -05001013 dout("%s using resend_mds mds%d\n", __func__,
Sage Weil2f2dc052009-10-06 11:31:09 -07001014 req->r_resend_mds);
1015 return req->r_resend_mds;
1016 }
1017
1018 if (mode == USE_RANDOM_MDS)
1019 goto random;
1020
1021 inode = NULL;
1022 if (req->r_inode) {
Yan, Zheng5d37ca12017-07-26 12:48:08 +08001023 if (ceph_snap(req->r_inode) != CEPH_SNAPDIR) {
1024 inode = req->r_inode;
1025 ihold(inode);
1026 } else {
Yan, Zheng38f340c2017-09-22 11:41:06 +08001027 /* req->r_dentry is non-null for LSSNAP request */
1028 rcu_read_lock();
1029 inode = get_nonsnap_parent(req->r_dentry);
1030 rcu_read_unlock();
Xiubo Li3c802092020-01-01 22:09:37 -05001031 dout("%s using snapdir's parent %p\n", __func__, inode);
Yan, Zheng5d37ca12017-07-26 12:48:08 +08001032 }
Yan, Zheng38f340c2017-09-22 11:41:06 +08001033 } else if (req->r_dentry) {
Sage Weild79698d2011-07-26 11:31:26 -07001034 /* ignore race with rename; old or new d_parent is okay */
Jeff Layton30c71232016-12-15 08:37:56 -05001035 struct dentry *parent;
1036 struct inode *dir;
Sage Weileb6bb1c2010-08-16 09:21:27 -07001037
Jeff Layton30c71232016-12-15 08:37:56 -05001038 rcu_read_lock();
Yan, Zheng41883ba2019-05-23 10:22:55 +08001039 parent = READ_ONCE(req->r_dentry->d_parent);
Jeff Layton3dd69aa2017-01-31 10:28:26 -05001040 dir = req->r_parent ? : d_inode_rcu(parent);
Jeff Layton30c71232016-12-15 08:37:56 -05001041
1042 if (!dir || dir->i_sb != mdsc->fsc->sb) {
1043 /* not this fs or parent went negative */
David Howells2b0143b2015-03-17 22:25:59 +00001044 inode = d_inode(req->r_dentry);
Jeff Layton30c71232016-12-15 08:37:56 -05001045 if (inode)
1046 ihold(inode);
Sage Weileb6bb1c2010-08-16 09:21:27 -07001047 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
1048 /* direct snapped/virtual snapdir requests
1049 * based on parent dir inode */
Jeff Layton30c71232016-12-15 08:37:56 -05001050 inode = get_nonsnap_parent(parent);
Xiubo Li3c802092020-01-01 22:09:37 -05001051 dout("%s using nonsnap parent %p\n", __func__, inode);
Yan, Zhengca18bed2013-11-22 14:21:44 +08001052 } else {
Sage Weileb6bb1c2010-08-16 09:21:27 -07001053 /* dentry target */
David Howells2b0143b2015-03-17 22:25:59 +00001054 inode = d_inode(req->r_dentry);
Yan, Zhengca18bed2013-11-22 14:21:44 +08001055 if (!inode || mode == USE_AUTH_MDS) {
1056 /* dir + name */
Jeff Layton30c71232016-12-15 08:37:56 -05001057 inode = igrab(dir);
Yan, Zhengca18bed2013-11-22 14:21:44 +08001058 hash = ceph_dentry_hash(dir, req->r_dentry);
1059 is_hash = true;
Jeff Layton30c71232016-12-15 08:37:56 -05001060 } else {
1061 ihold(inode);
Yan, Zhengca18bed2013-11-22 14:21:44 +08001062 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001063 }
Jeff Layton30c71232016-12-15 08:37:56 -05001064 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001065 }
Sage Weileb6bb1c2010-08-16 09:21:27 -07001066
Xiubo Li3c802092020-01-01 22:09:37 -05001067 dout("%s %p is_hash=%d (0x%x) mode %d\n", __func__, inode, (int)is_hash,
1068 hash, mode);
Sage Weil2f2dc052009-10-06 11:31:09 -07001069 if (!inode)
1070 goto random;
1071 ci = ceph_inode(inode);
1072
1073 if (is_hash && S_ISDIR(inode->i_mode)) {
1074 struct ceph_inode_frag frag;
1075 int found;
1076
1077 ceph_choose_frag(ci, hash, &frag, &found);
1078 if (found) {
1079 if (mode == USE_ANY_MDS && frag.ndist > 0) {
1080 u8 r;
1081
1082 /* choose a random replica */
1083 get_random_bytes(&r, 1);
1084 r %= frag.ndist;
1085 mds = frag.dist[r];
Xiubo Li3c802092020-01-01 22:09:37 -05001086 dout("%s %p %llx.%llx frag %u mds%d (%d/%d)\n",
1087 __func__, inode, ceph_vinop(inode),
1088 frag.frag, mds, (int)r, frag.ndist);
Sage Weild66bbd42011-01-21 21:16:46 -08001089 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
Xiubo Li5d476482019-11-26 07:24:22 -05001090 CEPH_MDS_STATE_ACTIVE &&
1091 !ceph_mdsmap_is_laggy(mdsc->mdsmap, mds))
Jeff Layton30c71232016-12-15 08:37:56 -05001092 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07001093 }
1094
1095 /* since this file/dir wasn't known to be
1096 * replicated, then we want to look for the
1097 * authoritative mds. */
Sage Weil2f2dc052009-10-06 11:31:09 -07001098 if (frag.mds >= 0) {
1099 /* choose auth mds */
1100 mds = frag.mds;
Xiubo Li3c802092020-01-01 22:09:37 -05001101 dout("%s %p %llx.%llx frag %u mds%d (auth)\n",
1102 __func__, inode, ceph_vinop(inode),
1103 frag.frag, mds);
Sage Weild66bbd42011-01-21 21:16:46 -08001104 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
Xiubo Li5d476482019-11-26 07:24:22 -05001105 CEPH_MDS_STATE_ACTIVE) {
1106 if (mode == USE_ANY_MDS &&
1107 !ceph_mdsmap_is_laggy(mdsc->mdsmap,
1108 mds))
1109 goto out;
1110 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001111 }
Xiubo Li5d476482019-11-26 07:24:22 -05001112 mode = USE_AUTH_MDS;
Sage Weil2f2dc052009-10-06 11:31:09 -07001113 }
1114 }
1115
Sage Weilbe655592011-11-30 09:47:09 -08001116 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001117 cap = NULL;
1118 if (mode == USE_AUTH_MDS)
1119 cap = ci->i_auth_cap;
1120 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
1121 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
1122 if (!cap) {
Sage Weilbe655592011-11-30 09:47:09 -08001123 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng3e1d0452019-05-18 20:39:55 +08001124 ceph_async_iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07001125 goto random;
1126 }
1127 mds = cap->session->s_mds;
Xiubo Li3c802092020-01-01 22:09:37 -05001128 dout("%s %p %llx.%llx mds%d (%scap %p)\n", __func__,
Sage Weil2f2dc052009-10-06 11:31:09 -07001129 inode, ceph_vinop(inode), mds,
1130 cap == ci->i_auth_cap ? "auth " : "", cap);
Sage Weilbe655592011-11-30 09:47:09 -08001131 spin_unlock(&ci->i_ceph_lock);
Jeff Layton30c71232016-12-15 08:37:56 -05001132out:
Yan, Zheng3e1d0452019-05-18 20:39:55 +08001133 /* avoid calling iput_final() while holding mdsc->mutex or
1134 * in mds dispatch threads */
1135 ceph_async_iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07001136 return mds;
1137
1138random:
Xiubo Lic4853e92019-12-09 07:47:15 -05001139 if (random)
1140 *random = true;
1141
Sage Weil2f2dc052009-10-06 11:31:09 -07001142 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
Xiubo Li3c802092020-01-01 22:09:37 -05001143 dout("%s chose random mds%d\n", __func__, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07001144 return mds;
1145}
1146
1147
1148/*
1149 * session messages
1150 */
1151static struct ceph_msg *create_session_msg(u32 op, u64 seq)
1152{
1153 struct ceph_msg *msg;
1154 struct ceph_mds_session_head *h;
1155
Sage Weilb61c2762011-08-09 15:03:46 -07001156 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
1157 false);
Sage Weila79832f2010-04-01 16:06:19 -07001158 if (!msg) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001159 pr_err("create_session_msg ENOMEM creating msg\n");
Sage Weila79832f2010-04-01 16:06:19 -07001160 return NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001161 }
1162 h = msg->front.iov_base;
1163 h->op = cpu_to_le32(op);
1164 h->seq = cpu_to_le64(seq);
John Spraydbd0c8b2014-09-09 19:26:01 +01001165
1166 return msg;
1167}
1168
Xiubo Li9ba1e222020-01-08 05:17:31 -05001169static const unsigned char feature_bits[] = CEPHFS_FEATURES_CLIENT_SUPPORTED;
1170#define FEATURE_BYTES(c) (DIV_ROUND_UP((size_t)feature_bits[c - 1] + 1, 64) * 8)
Yan, Zheng342ce182018-05-11 18:47:29 +08001171static void encode_supported_features(void **p, void *end)
1172{
Xiubo Li9ba1e222020-01-08 05:17:31 -05001173 static const size_t count = ARRAY_SIZE(feature_bits);
Yan, Zheng342ce182018-05-11 18:47:29 +08001174
1175 if (count > 0) {
1176 size_t i;
Xiubo Li9ba1e222020-01-08 05:17:31 -05001177 size_t size = FEATURE_BYTES(count);
Yan, Zheng342ce182018-05-11 18:47:29 +08001178
1179 BUG_ON(*p + 4 + size > end);
1180 ceph_encode_32(p, size);
1181 memset(*p, 0, size);
1182 for (i = 0; i < count; i++)
Xiubo Li9ba1e222020-01-08 05:17:31 -05001183 ((unsigned char*)(*p))[i / 8] |= BIT(feature_bits[i] % 8);
Yan, Zheng342ce182018-05-11 18:47:29 +08001184 *p += size;
1185 } else {
1186 BUG_ON(*p + 4 > end);
1187 ceph_encode_32(p, 0);
1188 }
1189}
1190
John Spraydbd0c8b2014-09-09 19:26:01 +01001191/*
1192 * session message, specialization for CEPH_SESSION_REQUEST_OPEN
1193 * to include additional client metadata fields.
1194 */
1195static struct ceph_msg *create_session_open_msg(struct ceph_mds_client *mdsc, u64 seq)
1196{
1197 struct ceph_msg *msg;
1198 struct ceph_mds_session_head *h;
1199 int i = -1;
Yan, Zheng342ce182018-05-11 18:47:29 +08001200 int extra_bytes = 0;
John Spraydbd0c8b2014-09-09 19:26:01 +01001201 int metadata_key_count = 0;
1202 struct ceph_options *opt = mdsc->fsc->client->options;
Yan, Zheng3f384952016-04-21 11:09:55 +08001203 struct ceph_mount_options *fsopt = mdsc->fsc->mount_options;
Xiubo Li9ba1e222020-01-08 05:17:31 -05001204 size_t size, count;
Yan, Zheng342ce182018-05-11 18:47:29 +08001205 void *p, *end;
John Spraydbd0c8b2014-09-09 19:26:01 +01001206
Yan, Zhenga6a5ce42015-01-16 10:54:43 +08001207 const char* metadata[][2] = {
Yan, Zheng717e6f22017-09-11 12:10:08 +08001208 {"hostname", mdsc->nodename},
1209 {"kernel_version", init_utsname()->release},
Yan, Zheng3f384952016-04-21 11:09:55 +08001210 {"entity_id", opt->name ? : ""},
1211 {"root", fsopt->server_path ? : "/"},
John Spraydbd0c8b2014-09-09 19:26:01 +01001212 {NULL, NULL}
1213 };
1214
1215 /* Calculate serialized length of metadata */
Yan, Zheng342ce182018-05-11 18:47:29 +08001216 extra_bytes = 4; /* map length */
Markus Elfringd37b1d92017-08-20 20:22:02 +02001217 for (i = 0; metadata[i][0]; ++i) {
Yan, Zheng342ce182018-05-11 18:47:29 +08001218 extra_bytes += 8 + strlen(metadata[i][0]) +
John Spraydbd0c8b2014-09-09 19:26:01 +01001219 strlen(metadata[i][1]);
1220 metadata_key_count++;
1221 }
Xiubo Li9ba1e222020-01-08 05:17:31 -05001222
Yan, Zheng342ce182018-05-11 18:47:29 +08001223 /* supported feature */
Xiubo Li9ba1e222020-01-08 05:17:31 -05001224 size = 0;
1225 count = ARRAY_SIZE(feature_bits);
1226 if (count > 0)
1227 size = FEATURE_BYTES(count);
1228 extra_bytes += 4 + size;
John Spraydbd0c8b2014-09-09 19:26:01 +01001229
1230 /* Allocate the message */
Yan, Zheng342ce182018-05-11 18:47:29 +08001231 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + extra_bytes,
John Spraydbd0c8b2014-09-09 19:26:01 +01001232 GFP_NOFS, false);
1233 if (!msg) {
1234 pr_err("create_session_msg ENOMEM creating msg\n");
1235 return NULL;
1236 }
Yan, Zheng342ce182018-05-11 18:47:29 +08001237 p = msg->front.iov_base;
1238 end = p + msg->front.iov_len;
1239
1240 h = p;
John Spraydbd0c8b2014-09-09 19:26:01 +01001241 h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN);
1242 h->seq = cpu_to_le64(seq);
1243
1244 /*
1245 * Serialize client metadata into waiting buffer space, using
1246 * the format that userspace expects for map<string, string>
John Spray7cfa0312014-10-30 17:15:26 +00001247 *
Xiubo Li9ba1e222020-01-08 05:17:31 -05001248 * ClientSession messages with metadata are v3
John Spraydbd0c8b2014-09-09 19:26:01 +01001249 */
Yan, Zheng342ce182018-05-11 18:47:29 +08001250 msg->hdr.version = cpu_to_le16(3);
John Spray7cfa0312014-10-30 17:15:26 +00001251 msg->hdr.compat_version = cpu_to_le16(1);
John Spraydbd0c8b2014-09-09 19:26:01 +01001252
1253 /* The write pointer, following the session_head structure */
Yan, Zheng342ce182018-05-11 18:47:29 +08001254 p += sizeof(*h);
John Spraydbd0c8b2014-09-09 19:26:01 +01001255
1256 /* Number of entries in the map */
1257 ceph_encode_32(&p, metadata_key_count);
1258
1259 /* Two length-prefixed strings for each entry in the map */
Markus Elfringd37b1d92017-08-20 20:22:02 +02001260 for (i = 0; metadata[i][0]; ++i) {
John Spraydbd0c8b2014-09-09 19:26:01 +01001261 size_t const key_len = strlen(metadata[i][0]);
1262 size_t const val_len = strlen(metadata[i][1]);
1263
1264 ceph_encode_32(&p, key_len);
1265 memcpy(p, metadata[i][0], key_len);
1266 p += key_len;
1267 ceph_encode_32(&p, val_len);
1268 memcpy(p, metadata[i][1], val_len);
1269 p += val_len;
1270 }
1271
Yan, Zheng342ce182018-05-11 18:47:29 +08001272 encode_supported_features(&p, end);
1273 msg->front.iov_len = p - msg->front.iov_base;
1274 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1275
Sage Weil2f2dc052009-10-06 11:31:09 -07001276 return msg;
1277}
1278
1279/*
1280 * send session open request.
1281 *
1282 * called under mdsc->mutex
1283 */
1284static int __open_session(struct ceph_mds_client *mdsc,
1285 struct ceph_mds_session *session)
1286{
1287 struct ceph_msg *msg;
1288 int mstate;
1289 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07001290
1291 /* wait for mds to go active? */
1292 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
1293 dout("open_session to mds%d (%s)\n", mds,
1294 ceph_mds_state_name(mstate));
1295 session->s_state = CEPH_MDS_SESSION_OPENING;
1296 session->s_renew_requested = jiffies;
1297
1298 /* send connect message */
John Spraydbd0c8b2014-09-09 19:26:01 +01001299 msg = create_session_open_msg(mdsc, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001300 if (!msg)
1301 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -07001302 ceph_con_send(&session->s_con, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001303 return 0;
1304}
1305
1306/*
Sage Weiled0552a2010-06-21 13:38:25 -07001307 * open sessions for any export targets for the given mds
1308 *
1309 * called under mdsc->mutex
1310 */
Yan, Zheng5d72d132013-11-24 14:33:01 +08001311static struct ceph_mds_session *
1312__open_export_target_session(struct ceph_mds_client *mdsc, int target)
1313{
1314 struct ceph_mds_session *session;
1315
1316 session = __ceph_lookup_mds_session(mdsc, target);
1317 if (!session) {
1318 session = register_session(mdsc, target);
1319 if (IS_ERR(session))
1320 return session;
1321 }
1322 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1323 session->s_state == CEPH_MDS_SESSION_CLOSING)
1324 __open_session(mdsc, session);
1325
1326 return session;
1327}
1328
1329struct ceph_mds_session *
1330ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target)
1331{
1332 struct ceph_mds_session *session;
1333
1334 dout("open_export_target_session to mds%d\n", target);
1335
1336 mutex_lock(&mdsc->mutex);
1337 session = __open_export_target_session(mdsc, target);
1338 mutex_unlock(&mdsc->mutex);
1339
1340 return session;
1341}
1342
Sage Weiled0552a2010-06-21 13:38:25 -07001343static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
1344 struct ceph_mds_session *session)
1345{
1346 struct ceph_mds_info *mi;
1347 struct ceph_mds_session *ts;
1348 int i, mds = session->s_mds;
Sage Weiled0552a2010-06-21 13:38:25 -07001349
Xiubo Lib38c9eb2019-12-04 06:57:39 -05001350 if (mds >= mdsc->mdsmap->possible_max_rank)
Sage Weiled0552a2010-06-21 13:38:25 -07001351 return;
Yan, Zheng5d72d132013-11-24 14:33:01 +08001352
Sage Weiled0552a2010-06-21 13:38:25 -07001353 mi = &mdsc->mdsmap->m_info[mds];
1354 dout("open_export_target_sessions for mds%d (%d targets)\n",
1355 session->s_mds, mi->num_export_targets);
1356
1357 for (i = 0; i < mi->num_export_targets; i++) {
Yan, Zheng5d72d132013-11-24 14:33:01 +08001358 ts = __open_export_target_session(mdsc, mi->export_targets[i]);
1359 if (!IS_ERR(ts))
1360 ceph_put_mds_session(ts);
Sage Weiled0552a2010-06-21 13:38:25 -07001361 }
1362}
1363
Sage Weil154f42c2010-06-21 13:45:04 -07001364void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
1365 struct ceph_mds_session *session)
1366{
1367 mutex_lock(&mdsc->mutex);
1368 __open_export_target_sessions(mdsc, session);
1369 mutex_unlock(&mdsc->mutex);
1370}
1371
Sage Weiled0552a2010-06-21 13:38:25 -07001372/*
Sage Weil2f2dc052009-10-06 11:31:09 -07001373 * session caps
1374 */
1375
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001376static void detach_cap_releases(struct ceph_mds_session *session,
1377 struct list_head *target)
Sage Weil2f2dc052009-10-06 11:31:09 -07001378{
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001379 lockdep_assert_held(&session->s_cap_lock);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001380
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001381 list_splice_init(&session->s_cap_releases, target);
1382 session->s_num_cap_releases = 0;
1383 dout("dispose_cap_releases mds%d\n", session->s_mds);
1384}
1385
1386static void dispose_cap_releases(struct ceph_mds_client *mdsc,
1387 struct list_head *dispose)
1388{
1389 while (!list_empty(dispose)) {
Yan, Zheng745a8e32015-05-14 17:22:42 +08001390 struct ceph_cap *cap;
1391 /* zero out the in-progress message */
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001392 cap = list_first_entry(dispose, struct ceph_cap, session_caps);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001393 list_del(&cap->session_caps);
1394 ceph_put_cap(mdsc, cap);
1395 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001396}
1397
Yan, Zheng1c841a92015-03-24 20:15:36 +08001398static void cleanup_session_requests(struct ceph_mds_client *mdsc,
1399 struct ceph_mds_session *session)
1400{
1401 struct ceph_mds_request *req;
1402 struct rb_node *p;
Yan, Zhengf4b97862019-07-25 20:16:42 +08001403 struct ceph_inode_info *ci;
Yan, Zheng1c841a92015-03-24 20:15:36 +08001404
1405 dout("cleanup_session_requests mds%d\n", session->s_mds);
1406 mutex_lock(&mdsc->mutex);
1407 while (!list_empty(&session->s_unsafe)) {
1408 req = list_first_entry(&session->s_unsafe,
1409 struct ceph_mds_request, r_unsafe_item);
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001410 pr_warn_ratelimited(" dropping unsafe request %llu\n",
1411 req->r_tid);
Yan, Zhengf4b97862019-07-25 20:16:42 +08001412 if (req->r_target_inode) {
1413 /* dropping unsafe change of inode's attributes */
1414 ci = ceph_inode(req->r_target_inode);
1415 errseq_set(&ci->i_meta_err, -EIO);
1416 }
1417 if (req->r_unsafe_dir) {
1418 /* dropping unsafe directory operation */
1419 ci = ceph_inode(req->r_unsafe_dir);
1420 errseq_set(&ci->i_meta_err, -EIO);
1421 }
Yan, Zheng1c841a92015-03-24 20:15:36 +08001422 __unregister_request(mdsc, req);
1423 }
1424 /* zero r_attempts, so kick_requests() will re-send requests */
1425 p = rb_first(&mdsc->request_tree);
1426 while (p) {
1427 req = rb_entry(p, struct ceph_mds_request, r_node);
1428 p = rb_next(p);
1429 if (req->r_session &&
1430 req->r_session->s_mds == session->s_mds)
1431 req->r_attempts = 0;
1432 }
1433 mutex_unlock(&mdsc->mutex);
1434}
1435
Sage Weil2f2dc052009-10-06 11:31:09 -07001436/*
Sage Weilf818a732010-05-11 20:56:31 -07001437 * Helper to safely iterate over all caps associated with a session, with
1438 * special care taken to handle a racing __ceph_remove_cap().
Sage Weil2f2dc052009-10-06 11:31:09 -07001439 *
Sage Weilf818a732010-05-11 20:56:31 -07001440 * Caller must hold session s_mutex.
Sage Weil2f2dc052009-10-06 11:31:09 -07001441 */
Jeff Laytonf5d772692019-04-24 12:09:04 -04001442int ceph_iterate_session_caps(struct ceph_mds_session *session,
1443 int (*cb)(struct inode *, struct ceph_cap *,
1444 void *), void *arg)
Sage Weil2f2dc052009-10-06 11:31:09 -07001445{
Sage Weil7c1332b2010-02-16 11:39:45 -08001446 struct list_head *p;
1447 struct ceph_cap *cap;
1448 struct inode *inode, *last_inode = NULL;
1449 struct ceph_cap *old_cap = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001450 int ret;
1451
1452 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
1453 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001454 p = session->s_caps.next;
1455 while (p != &session->s_caps) {
1456 cap = list_entry(p, struct ceph_cap, session_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -07001457 inode = igrab(&cap->ci->vfs_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001458 if (!inode) {
1459 p = p->next;
Sage Weil2f2dc052009-10-06 11:31:09 -07001460 continue;
Sage Weil7c1332b2010-02-16 11:39:45 -08001461 }
1462 session->s_cap_iterator = cap;
Sage Weil2f2dc052009-10-06 11:31:09 -07001463 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001464
1465 if (last_inode) {
Yan, Zheng3e1d0452019-05-18 20:39:55 +08001466 /* avoid calling iput_final() while holding
1467 * s_mutex or in mds dispatch threads */
1468 ceph_async_iput(last_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001469 last_inode = NULL;
1470 }
1471 if (old_cap) {
Yehuda Sadeh37151662010-06-17 16:16:12 -07001472 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -08001473 old_cap = NULL;
1474 }
1475
Sage Weil2f2dc052009-10-06 11:31:09 -07001476 ret = cb(inode, cap, arg);
Sage Weil7c1332b2010-02-16 11:39:45 -08001477 last_inode = inode;
1478
Sage Weil2f2dc052009-10-06 11:31:09 -07001479 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001480 p = p->next;
Markus Elfringd37b1d92017-08-20 20:22:02 +02001481 if (!cap->ci) {
Sage Weil7c1332b2010-02-16 11:39:45 -08001482 dout("iterate_session_caps finishing cap %p removal\n",
1483 cap);
1484 BUG_ON(cap->session != session);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001485 cap->session = NULL;
Sage Weil7c1332b2010-02-16 11:39:45 -08001486 list_del_init(&cap->session_caps);
1487 session->s_nr_caps--;
Xiubo Li4f1d7562020-06-30 03:52:16 -04001488 atomic64_dec(&session->s_mdsc->metric.total_caps);
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08001489 if (cap->queue_release)
1490 __ceph_queue_cap_release(session, cap);
1491 else
Yan, Zheng745a8e32015-05-14 17:22:42 +08001492 old_cap = cap; /* put_cap it w/o locks held */
Sage Weil7c1332b2010-02-16 11:39:45 -08001493 }
Sage Weil5dacf092009-12-21 20:40:34 -08001494 if (ret < 0)
1495 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07001496 }
Sage Weil5dacf092009-12-21 20:40:34 -08001497 ret = 0;
1498out:
Sage Weil7c1332b2010-02-16 11:39:45 -08001499 session->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001500 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001501
Yan, Zheng3e1d0452019-05-18 20:39:55 +08001502 ceph_async_iput(last_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001503 if (old_cap)
Yehuda Sadeh37151662010-06-17 16:16:12 -07001504 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -08001505
Sage Weil5dacf092009-12-21 20:40:34 -08001506 return ret;
Sage Weil2f2dc052009-10-06 11:31:09 -07001507}
1508
1509static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
Sage Weil6c99f252010-05-10 16:12:25 -07001510 void *arg)
Sage Weil2f2dc052009-10-06 11:31:09 -07001511{
Yan, Zheng6c93df52016-04-15 13:56:12 +08001512 struct ceph_fs_client *fsc = (struct ceph_fs_client *)arg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001513 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001514 LIST_HEAD(to_remove);
Yan, Zhengf4b97862019-07-25 20:16:42 +08001515 bool dirty_dropped = false;
Yan, Zheng6c93df52016-04-15 13:56:12 +08001516 bool invalidate = false;
Sage Weil6c99f252010-05-10 16:12:25 -07001517
Sage Weil2f2dc052009-10-06 11:31:09 -07001518 dout("removing cap %p, ci is %p, inode is %p\n",
1519 cap, ci, &ci->vfs_inode);
Sage Weilbe655592011-11-30 09:47:09 -08001520 spin_lock(&ci->i_ceph_lock);
Yan, Zhenga096b092013-09-22 10:15:58 +08001521 __ceph_remove_cap(cap, false);
Yan, Zheng571ade32015-03-24 11:36:08 +08001522 if (!ci->i_auth_cap) {
Yan, Zheng553adfd2015-06-09 15:48:57 +08001523 struct ceph_cap_flush *cf;
Yan, Zheng6c93df52016-04-15 13:56:12 +08001524 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil6c99f252010-05-10 16:12:25 -07001525
Yan, Zhengd468e722019-07-25 20:16:44 +08001526 if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
1527 if (inode->i_data.nrpages > 0)
1528 invalidate = true;
1529 if (ci->i_wrbuffer_ref > 0)
1530 mapping_set_error(&inode->i_data, -EIO);
1531 }
Yan, Zheng6c93df52016-04-15 13:56:12 +08001532
Yan, Zhenge4500b52016-07-06 11:12:56 +08001533 while (!list_empty(&ci->i_cap_flush_list)) {
1534 cf = list_first_entry(&ci->i_cap_flush_list,
1535 struct ceph_cap_flush, i_list);
Wei Yongjun8cdcc072016-08-13 09:10:28 +00001536 list_move(&cf->i_list, &to_remove);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001537 }
1538
Sage Weil6c99f252010-05-10 16:12:25 -07001539 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zheng8310b082015-06-09 17:20:12 +08001540
Yan, Zhenge4500b52016-07-06 11:12:56 +08001541 list_for_each_entry(cf, &to_remove, i_list)
1542 list_del(&cf->g_list);
Yan, Zheng8310b082015-06-09 17:20:12 +08001543
Sage Weil6c99f252010-05-10 16:12:25 -07001544 if (!list_empty(&ci->i_dirty_item)) {
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001545 pr_warn_ratelimited(
1546 " dropping dirty %s state for %p %lld\n",
Sage Weil6c99f252010-05-10 16:12:25 -07001547 ceph_cap_string(ci->i_dirty_caps),
1548 inode, ceph_ino(inode));
1549 ci->i_dirty_caps = 0;
1550 list_del_init(&ci->i_dirty_item);
Yan, Zhengf4b97862019-07-25 20:16:42 +08001551 dirty_dropped = true;
Sage Weil6c99f252010-05-10 16:12:25 -07001552 }
1553 if (!list_empty(&ci->i_flushing_item)) {
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001554 pr_warn_ratelimited(
1555 " dropping dirty+flushing %s state for %p %lld\n",
Sage Weil6c99f252010-05-10 16:12:25 -07001556 ceph_cap_string(ci->i_flushing_caps),
1557 inode, ceph_ino(inode));
1558 ci->i_flushing_caps = 0;
1559 list_del_init(&ci->i_flushing_item);
1560 mdsc->num_cap_flushing--;
Yan, Zhengf4b97862019-07-25 20:16:42 +08001561 dirty_dropped = true;
Sage Weil6c99f252010-05-10 16:12:25 -07001562 }
Sage Weil6c99f252010-05-10 16:12:25 -07001563 spin_unlock(&mdsc->cap_dirty_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001564
Yan, Zhengf4b97862019-07-25 20:16:42 +08001565 if (dirty_dropped) {
1566 errseq_set(&ci->i_meta_err, -EIO);
1567
1568 if (ci->i_wrbuffer_ref_head == 0 &&
1569 ci->i_wr_ref == 0 &&
1570 ci->i_dirty_caps == 0 &&
1571 ci->i_flushing_caps == 0) {
1572 ceph_put_snap_context(ci->i_head_snapc);
1573 ci->i_head_snapc = NULL;
1574 }
1575 }
1576
Yan, Zhengb3f8d682017-09-11 10:58:55 +08001577 if (atomic_read(&ci->i_filelock_ref) > 0) {
1578 /* make further file lock syscall return -EIO */
1579 ci->i_ceph_flags |= CEPH_I_ERROR_FILELOCK;
1580 pr_warn_ratelimited(" dropping file locks for %p %lld\n",
1581 inode, ceph_ino(inode));
1582 }
1583
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001584 if (!ci->i_dirty_caps && ci->i_prealloc_cap_flush) {
Yan, Zhenge4500b52016-07-06 11:12:56 +08001585 list_add(&ci->i_prealloc_cap_flush->i_list, &to_remove);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001586 ci->i_prealloc_cap_flush = NULL;
1587 }
Sage Weil6c99f252010-05-10 16:12:25 -07001588 }
Sage Weilbe655592011-11-30 09:47:09 -08001589 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001590 while (!list_empty(&to_remove)) {
1591 struct ceph_cap_flush *cf;
1592 cf = list_first_entry(&to_remove,
Yan, Zhenge4500b52016-07-06 11:12:56 +08001593 struct ceph_cap_flush, i_list);
1594 list_del(&cf->i_list);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001595 ceph_free_cap_flush(cf);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001596 }
Yan, Zheng77310322016-04-08 15:27:16 +08001597
1598 wake_up_all(&ci->i_cap_wq);
Yan, Zheng6c93df52016-04-15 13:56:12 +08001599 if (invalidate)
1600 ceph_queue_invalidate(inode);
Yan, Zhengf4b97862019-07-25 20:16:42 +08001601 if (dirty_dropped)
Sage Weil6c99f252010-05-10 16:12:25 -07001602 iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07001603 return 0;
1604}
1605
1606/*
1607 * caller must hold session s_mutex
1608 */
1609static void remove_session_caps(struct ceph_mds_session *session)
1610{
Yan, Zheng6c93df52016-04-15 13:56:12 +08001611 struct ceph_fs_client *fsc = session->s_mdsc->fsc;
1612 struct super_block *sb = fsc->sb;
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001613 LIST_HEAD(dispose);
1614
Sage Weil2f2dc052009-10-06 11:31:09 -07001615 dout("remove_session_caps on %p\n", session);
Jeff Laytonf5d772692019-04-24 12:09:04 -04001616 ceph_iterate_session_caps(session, remove_session_caps_cb, fsc);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001617
Yan, Zhengc8799fc2016-07-07 15:22:38 +08001618 wake_up_all(&fsc->mdsc->cap_flushing_wq);
1619
Yan, Zheng6f60f882013-07-24 12:22:11 +08001620 spin_lock(&session->s_cap_lock);
1621 if (session->s_nr_caps > 0) {
Yan, Zheng6f60f882013-07-24 12:22:11 +08001622 struct inode *inode;
1623 struct ceph_cap *cap, *prev = NULL;
1624 struct ceph_vino vino;
1625 /*
1626 * iterate_session_caps() skips inodes that are being
1627 * deleted, we need to wait until deletions are complete.
1628 * __wait_on_freeing_inode() is designed for the job,
1629 * but it is not exported, so use lookup inode function
1630 * to access it.
1631 */
1632 while (!list_empty(&session->s_caps)) {
1633 cap = list_entry(session->s_caps.next,
1634 struct ceph_cap, session_caps);
1635 if (cap == prev)
1636 break;
1637 prev = cap;
1638 vino = cap->ci->i_vino;
1639 spin_unlock(&session->s_cap_lock);
1640
Yan, Zhenged284c42013-09-02 15:19:53 +08001641 inode = ceph_find_inode(sb, vino);
Yan, Zheng3e1d0452019-05-18 20:39:55 +08001642 /* avoid calling iput_final() while holding s_mutex */
1643 ceph_async_iput(inode);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001644
1645 spin_lock(&session->s_cap_lock);
1646 }
1647 }
Yan, Zheng745a8e32015-05-14 17:22:42 +08001648
1649 // drop cap expires and unlock s_cap_lock
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001650 detach_cap_releases(session, &dispose);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001651
Sage Weil2f2dc052009-10-06 11:31:09 -07001652 BUG_ON(session->s_nr_caps > 0);
Sage Weil6c99f252010-05-10 16:12:25 -07001653 BUG_ON(!list_empty(&session->s_cap_flushing));
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001654 spin_unlock(&session->s_cap_lock);
1655 dispose_cap_releases(session->s_mdsc, &dispose);
Sage Weil2f2dc052009-10-06 11:31:09 -07001656}
1657
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001658enum {
1659 RECONNECT,
1660 RENEWCAPS,
1661 FORCE_RO,
1662};
1663
Sage Weil2f2dc052009-10-06 11:31:09 -07001664/*
1665 * wake up any threads waiting on this session's caps. if the cap is
1666 * old (didn't get renewed on the client reconnect), remove it now.
1667 *
1668 * caller must hold s_mutex.
1669 */
1670static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1671 void *arg)
1672{
Sage Weil0dc25702009-11-20 13:43:45 -08001673 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001674 unsigned long ev = (unsigned long)arg;
Sage Weil0dc25702009-11-20 13:43:45 -08001675
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001676 if (ev == RECONNECT) {
Sage Weilbe655592011-11-30 09:47:09 -08001677 spin_lock(&ci->i_ceph_lock);
Sage Weil0dc25702009-11-20 13:43:45 -08001678 ci->i_wanted_max_size = 0;
1679 ci->i_requested_max_size = 0;
Sage Weilbe655592011-11-30 09:47:09 -08001680 spin_unlock(&ci->i_ceph_lock);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001681 } else if (ev == RENEWCAPS) {
1682 if (cap->cap_gen < cap->session->s_cap_gen) {
1683 /* mds did not re-issue stale cap */
1684 spin_lock(&ci->i_ceph_lock);
1685 cap->issued = cap->implemented = CEPH_CAP_PIN;
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001686 spin_unlock(&ci->i_ceph_lock);
1687 }
1688 } else if (ev == FORCE_RO) {
Sage Weil0dc25702009-11-20 13:43:45 -08001689 }
Yan, Zhenge5360302016-05-19 19:15:19 +08001690 wake_up_all(&ci->i_cap_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07001691 return 0;
1692}
1693
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001694static void wake_up_session_caps(struct ceph_mds_session *session, int ev)
Sage Weil2f2dc052009-10-06 11:31:09 -07001695{
1696 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
Jeff Laytonf5d772692019-04-24 12:09:04 -04001697 ceph_iterate_session_caps(session, wake_up_session_cb,
1698 (void *)(unsigned long)ev);
Sage Weil2f2dc052009-10-06 11:31:09 -07001699}
1700
1701/*
1702 * Send periodic message to MDS renewing all currently held caps. The
1703 * ack will reset the expiration for all caps from this session.
1704 *
1705 * caller holds s_mutex
1706 */
1707static int send_renew_caps(struct ceph_mds_client *mdsc,
1708 struct ceph_mds_session *session)
1709{
1710 struct ceph_msg *msg;
1711 int state;
1712
1713 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1714 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1715 pr_info("mds%d caps stale\n", session->s_mds);
Sage Weile4cb4cb2010-03-18 13:43:09 -07001716 session->s_renew_requested = jiffies;
Sage Weil2f2dc052009-10-06 11:31:09 -07001717
1718 /* do not try to renew caps until a recovering mds has reconnected
1719 * with its clients. */
1720 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1721 if (state < CEPH_MDS_STATE_RECONNECT) {
1722 dout("send_renew_caps ignoring mds%d (%s)\n",
1723 session->s_mds, ceph_mds_state_name(state));
1724 return 0;
1725 }
1726
1727 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1728 ceph_mds_state_name(state));
Sage Weil2f2dc052009-10-06 11:31:09 -07001729 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1730 ++session->s_renew_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001731 if (!msg)
1732 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -07001733 ceph_con_send(&session->s_con, msg);
1734 return 0;
1735}
1736
Yan, Zheng186e4f72013-11-22 14:48:37 +08001737static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
1738 struct ceph_mds_session *session, u64 seq)
1739{
1740 struct ceph_msg *msg;
1741
1742 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
John Spraya687eca2014-09-19 13:51:08 +01001743 session->s_mds, ceph_session_state_name(session->s_state), seq);
Yan, Zheng186e4f72013-11-22 14:48:37 +08001744 msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
1745 if (!msg)
1746 return -ENOMEM;
1747 ceph_con_send(&session->s_con, msg);
1748 return 0;
1749}
1750
1751
Sage Weil2f2dc052009-10-06 11:31:09 -07001752/*
1753 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
Sage Weil0dc25702009-11-20 13:43:45 -08001754 *
1755 * Called under session->s_mutex
Sage Weil2f2dc052009-10-06 11:31:09 -07001756 */
1757static void renewed_caps(struct ceph_mds_client *mdsc,
1758 struct ceph_mds_session *session, int is_renew)
1759{
1760 int was_stale;
1761 int wake = 0;
1762
1763 spin_lock(&session->s_cap_lock);
Alex Elder1ce208a2012-01-12 17:48:11 -08001764 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
Sage Weil2f2dc052009-10-06 11:31:09 -07001765
1766 session->s_cap_ttl = session->s_renew_requested +
1767 mdsc->mdsmap->m_session_timeout*HZ;
1768
1769 if (was_stale) {
1770 if (time_before(jiffies, session->s_cap_ttl)) {
1771 pr_info("mds%d caps renewed\n", session->s_mds);
1772 wake = 1;
1773 } else {
1774 pr_info("mds%d caps still stale\n", session->s_mds);
1775 }
1776 }
1777 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1778 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1779 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1780 spin_unlock(&session->s_cap_lock);
1781
1782 if (wake)
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001783 wake_up_session_caps(session, RENEWCAPS);
Sage Weil2f2dc052009-10-06 11:31:09 -07001784}
1785
1786/*
1787 * send a session close request
1788 */
Xiubo Li3e699bd2020-06-30 03:52:15 -04001789static int request_close_session(struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001790{
1791 struct ceph_msg *msg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001792
1793 dout("request_close_session mds%d state %s seq %lld\n",
John Spraya687eca2014-09-19 13:51:08 +01001794 session->s_mds, ceph_session_state_name(session->s_state),
Sage Weil2f2dc052009-10-06 11:31:09 -07001795 session->s_seq);
1796 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001797 if (!msg)
1798 return -ENOMEM;
1799 ceph_con_send(&session->s_con, msg);
Yan, Zhengfcff4152016-09-14 16:39:51 +08001800 return 1;
Sage Weil2f2dc052009-10-06 11:31:09 -07001801}
1802
1803/*
1804 * Called with s_mutex held.
1805 */
1806static int __close_session(struct ceph_mds_client *mdsc,
1807 struct ceph_mds_session *session)
1808{
1809 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1810 return 0;
1811 session->s_state = CEPH_MDS_SESSION_CLOSING;
Xiubo Li3e699bd2020-06-30 03:52:15 -04001812 return request_close_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07001813}
1814
Yan, Zheng040d7862017-11-30 11:59:22 +08001815static bool drop_negative_children(struct dentry *dentry)
1816{
1817 struct dentry *child;
1818 bool all_negative = true;
1819
1820 if (!d_is_dir(dentry))
1821 goto out;
1822
1823 spin_lock(&dentry->d_lock);
1824 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
1825 if (d_really_is_positive(child)) {
1826 all_negative = false;
1827 break;
1828 }
1829 }
1830 spin_unlock(&dentry->d_lock);
1831
1832 if (all_negative)
1833 shrink_dcache_parent(dentry);
1834out:
1835 return all_negative;
1836}
1837
Sage Weil2f2dc052009-10-06 11:31:09 -07001838/*
1839 * Trim old(er) caps.
1840 *
1841 * Because we can't cache an inode without one or more caps, we do
1842 * this indirectly: if a cap is unused, we prune its aliases, at which
1843 * point the inode will hopefully get dropped to.
1844 *
1845 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1846 * memory pressure from the MDS, though, so it needn't be perfect.
1847 */
1848static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1849{
Jeff Layton533a2812019-07-19 15:22:28 -04001850 int *remaining = arg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001851 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng979abfd2013-11-22 13:56:24 +08001852 int used, wanted, oissued, mine;
Sage Weil2f2dc052009-10-06 11:31:09 -07001853
Jeff Layton533a2812019-07-19 15:22:28 -04001854 if (*remaining <= 0)
Sage Weil2f2dc052009-10-06 11:31:09 -07001855 return -1;
1856
Sage Weilbe655592011-11-30 09:47:09 -08001857 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001858 mine = cap->issued | cap->implemented;
1859 used = __ceph_caps_used(ci);
Yan, Zheng979abfd2013-11-22 13:56:24 +08001860 wanted = __ceph_caps_file_wanted(ci);
Sage Weil2f2dc052009-10-06 11:31:09 -07001861 oissued = __ceph_caps_issued_other(ci, cap);
1862
Yan, Zheng979abfd2013-11-22 13:56:24 +08001863 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001864 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
Yan, Zheng979abfd2013-11-22 13:56:24 +08001865 ceph_cap_string(used), ceph_cap_string(wanted));
1866 if (cap == ci->i_auth_cap) {
Yan, Zheng622f3e22015-05-07 10:59:47 +08001867 if (ci->i_dirty_caps || ci->i_flushing_caps ||
1868 !list_empty(&ci->i_cap_snaps))
Yan, Zheng979abfd2013-11-22 13:56:24 +08001869 goto out;
1870 if ((used | wanted) & CEPH_CAP_ANY_WR)
1871 goto out;
Yan, Zheng89aa5932017-09-08 15:23:18 +08001872 /* Note: it's possible that i_filelock_ref becomes non-zero
1873 * after dropping auth caps. It doesn't hurt because reply
1874 * of lock mds request will re-add auth caps. */
1875 if (atomic_read(&ci->i_filelock_ref) > 0)
1876 goto out;
Yan, Zheng979abfd2013-11-22 13:56:24 +08001877 }
Yan, Zheng5e804ac2015-10-26 16:08:43 +08001878 /* The inode has cached pages, but it's no longer used.
1879 * we can safely drop it */
Yan, Zheng525d15e2019-05-11 17:27:59 +08001880 if (S_ISREG(inode->i_mode) &&
1881 wanted == 0 && used == CEPH_CAP_FILE_CACHE &&
Yan, Zheng5e804ac2015-10-26 16:08:43 +08001882 !(oissued & CEPH_CAP_FILE_CACHE)) {
1883 used = 0;
1884 oissued = 0;
1885 }
Yan, Zheng979abfd2013-11-22 13:56:24 +08001886 if ((used | wanted) & ~oissued & mine)
Sage Weil2f2dc052009-10-06 11:31:09 -07001887 goto out; /* we need these caps */
1888
Sage Weil2f2dc052009-10-06 11:31:09 -07001889 if (oissued) {
1890 /* we aren't the only cap.. just remove us */
Yan, Zhenga096b092013-09-22 10:15:58 +08001891 __ceph_remove_cap(cap, true);
Jeff Layton533a2812019-07-19 15:22:28 -04001892 (*remaining)--;
Sage Weil2f2dc052009-10-06 11:31:09 -07001893 } else {
Yan, Zheng040d7862017-11-30 11:59:22 +08001894 struct dentry *dentry;
Yan, Zheng5e804ac2015-10-26 16:08:43 +08001895 /* try dropping referring dentries */
Sage Weilbe655592011-11-30 09:47:09 -08001896 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng040d7862017-11-30 11:59:22 +08001897 dentry = d_find_any_alias(inode);
1898 if (dentry && drop_negative_children(dentry)) {
1899 int count;
1900 dput(dentry);
1901 d_prune_aliases(inode);
1902 count = atomic_read(&inode->i_count);
1903 if (count == 1)
Jeff Layton533a2812019-07-19 15:22:28 -04001904 (*remaining)--;
Yan, Zheng040d7862017-11-30 11:59:22 +08001905 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1906 inode, cap, count);
1907 } else {
1908 dput(dentry);
1909 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001910 return 0;
1911 }
1912
1913out:
Sage Weilbe655592011-11-30 09:47:09 -08001914 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001915 return 0;
1916}
1917
1918/*
1919 * Trim session cap count down to some max number.
1920 */
Zhi Zhange30ee582018-01-24 21:24:33 +08001921int ceph_trim_caps(struct ceph_mds_client *mdsc,
1922 struct ceph_mds_session *session,
1923 int max_caps)
Sage Weil2f2dc052009-10-06 11:31:09 -07001924{
1925 int trim_caps = session->s_nr_caps - max_caps;
1926
1927 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1928 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1929 if (trim_caps > 0) {
Jeff Layton533a2812019-07-19 15:22:28 -04001930 int remaining = trim_caps;
1931
1932 ceph_iterate_session_caps(session, trim_caps_cb, &remaining);
Sage Weil2f2dc052009-10-06 11:31:09 -07001933 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1934 session->s_mds, session->s_nr_caps, max_caps,
Jeff Layton533a2812019-07-19 15:22:28 -04001935 trim_caps - remaining);
Sage Weil2f2dc052009-10-06 11:31:09 -07001936 }
Yan, Zhenga56371d2014-04-01 20:34:56 +08001937
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08001938 ceph_flush_cap_releases(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07001939 return 0;
1940}
1941
Yan, Zheng8310b082015-06-09 17:20:12 +08001942static int check_caps_flush(struct ceph_mds_client *mdsc,
1943 u64 want_flush_tid)
1944{
Yan, Zheng8310b082015-06-09 17:20:12 +08001945 int ret = 1;
1946
1947 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zhenge4500b52016-07-06 11:12:56 +08001948 if (!list_empty(&mdsc->cap_flush_list)) {
1949 struct ceph_cap_flush *cf =
1950 list_first_entry(&mdsc->cap_flush_list,
1951 struct ceph_cap_flush, g_list);
1952 if (cf->tid <= want_flush_tid) {
1953 dout("check_caps_flush still flushing tid "
1954 "%llu <= %llu\n", cf->tid, want_flush_tid);
1955 ret = 0;
1956 }
Yan, Zheng8310b082015-06-09 17:20:12 +08001957 }
1958 spin_unlock(&mdsc->cap_dirty_lock);
1959 return ret;
Yan, Zhengd3383a82015-01-08 21:30:12 +08001960}
1961
Sage Weil2f2dc052009-10-06 11:31:09 -07001962/*
1963 * flush all dirty inode data to disk.
1964 *
Yan, Zheng8310b082015-06-09 17:20:12 +08001965 * returns true if we've flushed through want_flush_tid
Sage Weil2f2dc052009-10-06 11:31:09 -07001966 */
Yan, Zhengaffbc192015-05-05 21:22:13 +08001967static void wait_caps_flush(struct ceph_mds_client *mdsc,
Yan, Zheng0e294382016-07-04 18:06:41 +08001968 u64 want_flush_tid)
Sage Weil2f2dc052009-10-06 11:31:09 -07001969{
Yan, Zheng0e294382016-07-04 18:06:41 +08001970 dout("check_caps_flush want %llu\n", want_flush_tid);
Yan, Zheng8310b082015-06-09 17:20:12 +08001971
1972 wait_event(mdsc->cap_flushing_wq,
1973 check_caps_flush(mdsc, want_flush_tid));
1974
1975 dout("check_caps_flush ok, flushed thru %llu\n", want_flush_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07001976}
1977
1978/*
1979 * called under s_mutex
1980 */
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08001981static void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1982 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001983{
Yan, Zheng745a8e32015-05-14 17:22:42 +08001984 struct ceph_msg *msg = NULL;
1985 struct ceph_mds_cap_release *head;
1986 struct ceph_mds_cap_item *item;
Jeff Layton92475f02017-04-13 11:07:04 -04001987 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
Yan, Zheng745a8e32015-05-14 17:22:42 +08001988 struct ceph_cap *cap;
1989 LIST_HEAD(tmp_list);
1990 int num_cap_releases;
Jeff Layton92475f02017-04-13 11:07:04 -04001991 __le32 barrier, *cap_barrier;
1992
1993 down_read(&osdc->lock);
1994 barrier = cpu_to_le32(osdc->epoch_barrier);
1995 up_read(&osdc->lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001996
Sage Weil0f8605f2010-05-05 15:51:35 -07001997 spin_lock(&session->s_cap_lock);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001998again:
1999 list_splice_init(&session->s_cap_releases, &tmp_list);
2000 num_cap_releases = session->s_num_cap_releases;
2001 session->s_num_cap_releases = 0;
2002 spin_unlock(&session->s_cap_lock);
2003
2004 while (!list_empty(&tmp_list)) {
2005 if (!msg) {
2006 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002007 PAGE_SIZE, GFP_NOFS, false);
Yan, Zheng745a8e32015-05-14 17:22:42 +08002008 if (!msg)
2009 goto out_err;
2010 head = msg->front.iov_base;
2011 head->num = cpu_to_le32(0);
2012 msg->front.iov_len = sizeof(*head);
Jeff Layton92475f02017-04-13 11:07:04 -04002013
2014 msg->hdr.version = cpu_to_le16(2);
2015 msg->hdr.compat_version = cpu_to_le16(1);
Yan, Zheng745a8e32015-05-14 17:22:42 +08002016 }
Jeff Layton92475f02017-04-13 11:07:04 -04002017
Yan, Zheng745a8e32015-05-14 17:22:42 +08002018 cap = list_first_entry(&tmp_list, struct ceph_cap,
2019 session_caps);
2020 list_del(&cap->session_caps);
2021 num_cap_releases--;
2022
2023 head = msg->front.iov_base;
Jeff Layton4198aba2019-05-02 08:06:50 -04002024 put_unaligned_le32(get_unaligned_le32(&head->num) + 1,
2025 &head->num);
Yan, Zheng745a8e32015-05-14 17:22:42 +08002026 item = msg->front.iov_base + msg->front.iov_len;
2027 item->ino = cpu_to_le64(cap->cap_ino);
2028 item->cap_id = cpu_to_le64(cap->cap_id);
2029 item->migrate_seq = cpu_to_le32(cap->mseq);
2030 item->seq = cpu_to_le32(cap->issue_seq);
2031 msg->front.iov_len += sizeof(*item);
2032
2033 ceph_put_cap(mdsc, cap);
2034
2035 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
Jeff Layton92475f02017-04-13 11:07:04 -04002036 // Append cap_barrier field
2037 cap_barrier = msg->front.iov_base + msg->front.iov_len;
2038 *cap_barrier = barrier;
2039 msg->front.iov_len += sizeof(*cap_barrier);
2040
Yan, Zheng745a8e32015-05-14 17:22:42 +08002041 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2042 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
2043 ceph_con_send(&session->s_con, msg);
2044 msg = NULL;
2045 }
2046 }
2047
2048 BUG_ON(num_cap_releases != 0);
2049
2050 spin_lock(&session->s_cap_lock);
2051 if (!list_empty(&session->s_cap_releases))
2052 goto again;
2053 spin_unlock(&session->s_cap_lock);
2054
2055 if (msg) {
Jeff Layton92475f02017-04-13 11:07:04 -04002056 // Append cap_barrier field
2057 cap_barrier = msg->front.iov_base + msg->front.iov_len;
2058 *cap_barrier = barrier;
2059 msg->front.iov_len += sizeof(*cap_barrier);
2060
Sage Weil2f2dc052009-10-06 11:31:09 -07002061 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2062 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
2063 ceph_con_send(&session->s_con, msg);
2064 }
Yan, Zheng745a8e32015-05-14 17:22:42 +08002065 return;
2066out_err:
2067 pr_err("send_cap_releases mds%d, failed to allocate message\n",
2068 session->s_mds);
2069 spin_lock(&session->s_cap_lock);
2070 list_splice(&tmp_list, &session->s_cap_releases);
2071 session->s_num_cap_releases += num_cap_releases;
Sage Weil2f2dc052009-10-06 11:31:09 -07002072 spin_unlock(&session->s_cap_lock);
2073}
2074
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08002075static void ceph_cap_release_work(struct work_struct *work)
2076{
2077 struct ceph_mds_session *session =
2078 container_of(work, struct ceph_mds_session, s_cap_release_work);
2079
2080 mutex_lock(&session->s_mutex);
2081 if (session->s_state == CEPH_MDS_SESSION_OPEN ||
2082 session->s_state == CEPH_MDS_SESSION_HUNG)
2083 ceph_send_cap_releases(session->s_mdsc, session);
2084 mutex_unlock(&session->s_mutex);
2085 ceph_put_mds_session(session);
2086}
2087
2088void ceph_flush_cap_releases(struct ceph_mds_client *mdsc,
2089 struct ceph_mds_session *session)
2090{
2091 if (mdsc->stopping)
2092 return;
2093
Xiubo Li5b3248c2019-12-19 19:44:09 -05002094 ceph_get_mds_session(session);
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08002095 if (queue_work(mdsc->fsc->cap_wq,
2096 &session->s_cap_release_work)) {
2097 dout("cap release work queued\n");
2098 } else {
2099 ceph_put_mds_session(session);
2100 dout("failed to queue cap release work\n");
2101 }
2102}
2103
2104/*
2105 * caller holds session->s_cap_lock
2106 */
2107void __ceph_queue_cap_release(struct ceph_mds_session *session,
2108 struct ceph_cap *cap)
2109{
2110 list_add_tail(&cap->session_caps, &session->s_cap_releases);
2111 session->s_num_cap_releases++;
2112
2113 if (!(session->s_num_cap_releases % CEPH_CAPS_PER_RELEASE))
2114 ceph_flush_cap_releases(session->s_mdsc, session);
2115}
2116
Yan, Zheng37c4efc2019-01-31 16:55:51 +08002117static void ceph_cap_reclaim_work(struct work_struct *work)
2118{
2119 struct ceph_mds_client *mdsc =
2120 container_of(work, struct ceph_mds_client, cap_reclaim_work);
2121 int ret = ceph_trim_dentries(mdsc);
2122 if (ret == -EAGAIN)
2123 ceph_queue_cap_reclaim_work(mdsc);
2124}
2125
2126void ceph_queue_cap_reclaim_work(struct ceph_mds_client *mdsc)
2127{
2128 if (mdsc->stopping)
2129 return;
2130
2131 if (queue_work(mdsc->fsc->cap_wq, &mdsc->cap_reclaim_work)) {
2132 dout("caps reclaim work queued\n");
2133 } else {
2134 dout("failed to queue caps release work\n");
2135 }
2136}
2137
Yan, Zhengfe330322019-02-01 14:57:15 +08002138void ceph_reclaim_caps_nr(struct ceph_mds_client *mdsc, int nr)
2139{
2140 int val;
2141 if (!nr)
2142 return;
2143 val = atomic_add_return(nr, &mdsc->cap_reclaim_pending);
Xiubo Libba15602019-11-26 07:32:22 -05002144 if ((val % CEPH_CAPS_PER_RELEASE) < nr) {
Yan, Zhengfe330322019-02-01 14:57:15 +08002145 atomic_set(&mdsc->cap_reclaim_pending, 0);
2146 ceph_queue_cap_reclaim_work(mdsc);
2147 }
2148}
2149
Sage Weil2f2dc052009-10-06 11:31:09 -07002150/*
2151 * requests
2152 */
2153
Yan, Zheng54008392014-03-29 13:41:15 +08002154int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
2155 struct inode *dir)
2156{
2157 struct ceph_inode_info *ci = ceph_inode(dir);
2158 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
2159 struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options;
Yan, Zheng2a5beea2016-04-28 09:37:39 +08002160 size_t size = sizeof(struct ceph_mds_reply_dir_entry);
Jeff Laytonad8c28a2019-09-09 15:58:55 -04002161 unsigned int num_entries;
2162 int order;
Yan, Zheng54008392014-03-29 13:41:15 +08002163
2164 spin_lock(&ci->i_ceph_lock);
2165 num_entries = ci->i_files + ci->i_subdirs;
2166 spin_unlock(&ci->i_ceph_lock);
Jeff Laytonad8c28a2019-09-09 15:58:55 -04002167 num_entries = max(num_entries, 1U);
Yan, Zheng54008392014-03-29 13:41:15 +08002168 num_entries = min(num_entries, opt->max_readdir);
2169
2170 order = get_order(size * num_entries);
2171 while (order >= 0) {
Yan, Zheng2a5beea2016-04-28 09:37:39 +08002172 rinfo->dir_entries = (void*)__get_free_pages(GFP_KERNEL |
2173 __GFP_NOWARN,
2174 order);
2175 if (rinfo->dir_entries)
Yan, Zheng54008392014-03-29 13:41:15 +08002176 break;
2177 order--;
2178 }
Yan, Zheng2a5beea2016-04-28 09:37:39 +08002179 if (!rinfo->dir_entries)
Yan, Zheng54008392014-03-29 13:41:15 +08002180 return -ENOMEM;
2181
2182 num_entries = (PAGE_SIZE << order) / size;
2183 num_entries = min(num_entries, opt->max_readdir);
2184
2185 rinfo->dir_buf_size = PAGE_SIZE << order;
2186 req->r_num_caps = num_entries + 1;
2187 req->r_args.readdir.max_entries = cpu_to_le32(num_entries);
2188 req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes);
2189 return 0;
2190}
2191
Sage Weil2f2dc052009-10-06 11:31:09 -07002192/*
2193 * Create an mds request.
2194 */
2195struct ceph_mds_request *
2196ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
2197{
Jeff Layton058daab2020-02-17 18:38:37 -05002198 struct ceph_mds_request *req;
Sage Weil2f2dc052009-10-06 11:31:09 -07002199
Jeff Layton058daab2020-02-17 18:38:37 -05002200 req = kmem_cache_zalloc(ceph_mds_request_cachep, GFP_NOFS);
Sage Weil2f2dc052009-10-06 11:31:09 -07002201 if (!req)
2202 return ERR_PTR(-ENOMEM);
2203
Sage Weilb4556392010-05-13 12:01:13 -07002204 mutex_init(&req->r_fill_mutex);
Yehuda Sadeh37151662010-06-17 16:16:12 -07002205 req->r_mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07002206 req->r_started = jiffies;
Xiubo Li70c94822020-03-19 23:45:02 -04002207 req->r_start_latency = ktime_get();
Sage Weil2f2dc052009-10-06 11:31:09 -07002208 req->r_resend_mds = -1;
2209 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002210 INIT_LIST_HEAD(&req->r_unsafe_target_item);
Sage Weil2f2dc052009-10-06 11:31:09 -07002211 req->r_fmode = -1;
Sage Weil153c8e62009-12-07 12:31:09 -08002212 kref_init(&req->r_kref);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02002213 RB_CLEAR_NODE(&req->r_node);
Sage Weil2f2dc052009-10-06 11:31:09 -07002214 INIT_LIST_HEAD(&req->r_wait);
2215 init_completion(&req->r_completion);
2216 init_completion(&req->r_safe_completion);
2217 INIT_LIST_HEAD(&req->r_unsafe_item);
2218
Deepa Dinamani668c9a62019-12-02 21:19:42 -08002219 ktime_get_coarse_real_ts64(&req->r_stamp);
Sage Weilb8e69062014-05-21 17:41:08 -07002220
Sage Weil2f2dc052009-10-06 11:31:09 -07002221 req->r_op = op;
2222 req->r_direct_mode = mode;
2223 return req;
2224}
2225
2226/*
Sage Weil44ca18f2010-02-15 12:08:46 -08002227 * return oldest (lowest) request, tid in request tree, 0 if none.
Sage Weil2f2dc052009-10-06 11:31:09 -07002228 *
2229 * called under mdsc->mutex.
2230 */
Sage Weil44ca18f2010-02-15 12:08:46 -08002231static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
2232{
2233 if (RB_EMPTY_ROOT(&mdsc->request_tree))
2234 return NULL;
2235 return rb_entry(rb_first(&mdsc->request_tree),
2236 struct ceph_mds_request, r_node);
2237}
2238
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08002239static inline u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07002240{
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08002241 return mdsc->oldest_tid;
Sage Weil2f2dc052009-10-06 11:31:09 -07002242}
2243
2244/*
2245 * Build a dentry's path. Allocate on heap; caller must kfree. Based
2246 * on build_path_from_dentry in fs/cifs/dir.c.
2247 *
2248 * If @stop_on_nosnap, generate path relative to the first non-snapped
2249 * inode.
2250 *
2251 * Encode hidden .snap dirs as a double /, i.e.
2252 * foo/.snap/bar -> foo//bar
2253 */
Jeff Layton69a10fb2019-04-26 13:33:39 -04002254char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase,
Sage Weil2f2dc052009-10-06 11:31:09 -07002255 int stop_on_nosnap)
2256{
2257 struct dentry *temp;
2258 char *path;
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002259 int pos;
Al Viro1b71fe22011-07-16 23:43:58 -04002260 unsigned seq;
Jeff Layton69a10fb2019-04-26 13:33:39 -04002261 u64 base;
Sage Weil2f2dc052009-10-06 11:31:09 -07002262
Markus Elfringd37b1d92017-08-20 20:22:02 +02002263 if (!dentry)
Sage Weil2f2dc052009-10-06 11:31:09 -07002264 return ERR_PTR(-EINVAL);
2265
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002266 path = __getname();
Markus Elfringd37b1d92017-08-20 20:22:02 +02002267 if (!path)
Sage Weil2f2dc052009-10-06 11:31:09 -07002268 return ERR_PTR(-ENOMEM);
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002269retry:
2270 pos = PATH_MAX - 1;
2271 path[pos] = '\0';
2272
2273 seq = read_seqbegin(&rename_lock);
Al Viro1b71fe22011-07-16 23:43:58 -04002274 rcu_read_lock();
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002275 temp = dentry;
2276 for (;;) {
Al Viro1b71fe22011-07-16 23:43:58 -04002277 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07002278
Al Viro1b71fe22011-07-16 23:43:58 -04002279 spin_lock(&temp->d_lock);
David Howells2b0143b2015-03-17 22:25:59 +00002280 inode = d_inode(temp);
Sage Weil2f2dc052009-10-06 11:31:09 -07002281 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
Sage Weil104648a2010-03-18 10:14:30 -07002282 dout("build_path path+%d: %p SNAPDIR\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002283 pos, temp);
Jeff Laytond6b8bd62019-05-09 07:58:38 -04002284 } else if (stop_on_nosnap && inode && dentry != temp &&
Sage Weil2f2dc052009-10-06 11:31:09 -07002285 ceph_snap(inode) == CEPH_NOSNAP) {
Yehuda Sadeh9d5a09e2011-12-13 09:57:44 -08002286 spin_unlock(&temp->d_lock);
Jeff Laytond6b8bd62019-05-09 07:58:38 -04002287 pos++; /* get rid of any prepended '/' */
Sage Weil2f2dc052009-10-06 11:31:09 -07002288 break;
2289 } else {
2290 pos -= temp->d_name.len;
Al Viro1b71fe22011-07-16 23:43:58 -04002291 if (pos < 0) {
2292 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002293 break;
Al Viro1b71fe22011-07-16 23:43:58 -04002294 }
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002295 memcpy(path + pos, temp->d_name.name, temp->d_name.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07002296 }
Al Viro1b71fe22011-07-16 23:43:58 -04002297 spin_unlock(&temp->d_lock);
Yan, Zheng41883ba2019-05-23 10:22:55 +08002298 temp = READ_ONCE(temp->d_parent);
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002299
2300 /* Are we at the root? */
2301 if (IS_ROOT(temp))
2302 break;
2303
2304 /* Are we out of buffer? */
2305 if (--pos < 0)
2306 break;
2307
2308 path[pos] = '/';
Sage Weil2f2dc052009-10-06 11:31:09 -07002309 }
Jeff Layton69a10fb2019-04-26 13:33:39 -04002310 base = ceph_ino(d_inode(temp));
Al Viro1b71fe22011-07-16 23:43:58 -04002311 rcu_read_unlock();
Jeff Laytonf5946bc2019-10-16 08:20:17 -04002312
2313 if (read_seqretry(&rename_lock, seq))
2314 goto retry;
2315
2316 if (pos < 0) {
2317 /*
2318 * A rename didn't occur, but somehow we didn't end up where
2319 * we thought we would. Throw a warning and try again.
2320 */
2321 pr_warn("build_path did not end path lookup where "
2322 "expected, pos is %d\n", pos);
Sage Weil2f2dc052009-10-06 11:31:09 -07002323 goto retry;
2324 }
2325
Jeff Layton69a10fb2019-04-26 13:33:39 -04002326 *pbase = base;
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002327 *plen = PATH_MAX - 1 - pos;
Sage Weil104648a2010-03-18 10:14:30 -07002328 dout("build_path on %p %d built %llx '%.*s'\n",
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002329 dentry, d_count(dentry), base, *plen, path + pos);
2330 return path + pos;
Sage Weil2f2dc052009-10-06 11:31:09 -07002331}
2332
Jeff Laytonfd36a712016-12-15 08:37:58 -05002333static int build_dentry_path(struct dentry *dentry, struct inode *dir,
Sage Weil2f2dc052009-10-06 11:31:09 -07002334 const char **ppath, int *ppathlen, u64 *pino,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002335 bool *pfreepath, bool parent_locked)
Sage Weil2f2dc052009-10-06 11:31:09 -07002336{
2337 char *path;
2338
Jeff Laytonc6b0b652016-12-15 08:37:57 -05002339 rcu_read_lock();
Jeff Laytonfd36a712016-12-15 08:37:58 -05002340 if (!dir)
2341 dir = d_inode_rcu(dentry->d_parent);
Jeff Layton964fff72019-04-29 11:51:02 -04002342 if (dir && parent_locked && ceph_snap(dir) == CEPH_NOSNAP) {
Jeff Laytonc6b0b652016-12-15 08:37:57 -05002343 *pino = ceph_ino(dir);
2344 rcu_read_unlock();
Jeff Layton964fff72019-04-29 11:51:02 -04002345 *ppath = dentry->d_name.name;
2346 *ppathlen = dentry->d_name.len;
Sage Weil2f2dc052009-10-06 11:31:09 -07002347 return 0;
2348 }
Jeff Laytonc6b0b652016-12-15 08:37:57 -05002349 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -07002350 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
2351 if (IS_ERR(path))
2352 return PTR_ERR(path);
2353 *ppath = path;
Jeff Layton1bcb3442019-04-15 12:00:42 -04002354 *pfreepath = true;
Sage Weil2f2dc052009-10-06 11:31:09 -07002355 return 0;
2356}
2357
2358static int build_inode_path(struct inode *inode,
2359 const char **ppath, int *ppathlen, u64 *pino,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002360 bool *pfreepath)
Sage Weil2f2dc052009-10-06 11:31:09 -07002361{
2362 struct dentry *dentry;
2363 char *path;
2364
2365 if (ceph_snap(inode) == CEPH_NOSNAP) {
2366 *pino = ceph_ino(inode);
2367 *ppathlen = 0;
2368 return 0;
2369 }
2370 dentry = d_find_alias(inode);
2371 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
2372 dput(dentry);
2373 if (IS_ERR(path))
2374 return PTR_ERR(path);
2375 *ppath = path;
Jeff Layton1bcb3442019-04-15 12:00:42 -04002376 *pfreepath = true;
Sage Weil2f2dc052009-10-06 11:31:09 -07002377 return 0;
2378}
2379
2380/*
2381 * request arguments may be specified via an inode *, a dentry *, or
2382 * an explicit ino+path.
2383 */
2384static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
Jeff Laytonfd36a712016-12-15 08:37:58 -05002385 struct inode *rdiri, const char *rpath,
2386 u64 rino, const char **ppath, int *pathlen,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002387 u64 *ino, bool *freepath, bool parent_locked)
Sage Weil2f2dc052009-10-06 11:31:09 -07002388{
2389 int r = 0;
2390
2391 if (rinode) {
2392 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
2393 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
2394 ceph_snap(rinode));
2395 } else if (rdentry) {
Jeff Laytonfd36a712016-12-15 08:37:58 -05002396 r = build_dentry_path(rdentry, rdiri, ppath, pathlen, ino,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002397 freepath, parent_locked);
Sage Weil2f2dc052009-10-06 11:31:09 -07002398 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
2399 *ppath);
Sage Weil795858d2011-08-15 13:02:37 -07002400 } else if (rpath || rino) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002401 *ino = rino;
2402 *ppath = rpath;
David Zafmanb0000562012-10-25 10:23:46 -07002403 *pathlen = rpath ? strlen(rpath) : 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002404 dout(" path %.*s\n", *pathlen, rpath);
2405 }
2406
2407 return r;
2408}
2409
2410/*
2411 * called under mdsc->mutex
2412 */
2413static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
2414 struct ceph_mds_request *req,
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002415 int mds, bool drop_cap_releases)
Sage Weil2f2dc052009-10-06 11:31:09 -07002416{
2417 struct ceph_msg *msg;
2418 struct ceph_mds_request_head *head;
2419 const char *path1 = NULL;
2420 const char *path2 = NULL;
2421 u64 ino1 = 0, ino2 = 0;
2422 int pathlen1 = 0, pathlen2 = 0;
Jeff Layton1bcb3442019-04-15 12:00:42 -04002423 bool freepath1 = false, freepath2 = false;
Sage Weil2f2dc052009-10-06 11:31:09 -07002424 int len;
2425 u16 releases;
2426 void *p, *end;
2427 int ret;
2428
2429 ret = set_request_path_attr(req->r_inode, req->r_dentry,
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002430 req->r_parent, req->r_path1, req->r_ino1.ino,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002431 &path1, &pathlen1, &ino1, &freepath1,
2432 test_bit(CEPH_MDS_R_PARENT_LOCKED,
2433 &req->r_req_flags));
Sage Weil2f2dc052009-10-06 11:31:09 -07002434 if (ret < 0) {
2435 msg = ERR_PTR(ret);
2436 goto out;
2437 }
2438
Jeff Layton1bcb3442019-04-15 12:00:42 -04002439 /* If r_old_dentry is set, then assume that its parent is locked */
Sage Weil2f2dc052009-10-06 11:31:09 -07002440 ret = set_request_path_attr(NULL, req->r_old_dentry,
Jeff Laytonfd36a712016-12-15 08:37:58 -05002441 req->r_old_dentry_dir,
Sage Weil2f2dc052009-10-06 11:31:09 -07002442 req->r_path2, req->r_ino2.ino,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002443 &path2, &pathlen2, &ino2, &freepath2, true);
Sage Weil2f2dc052009-10-06 11:31:09 -07002444 if (ret < 0) {
2445 msg = ERR_PTR(ret);
2446 goto out_free1;
2447 }
2448
2449 len = sizeof(*head) +
Sage Weilb8e69062014-05-21 17:41:08 -07002450 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) +
Arnd Bergmann777d7382015-09-30 15:04:42 +02002451 sizeof(struct ceph_timespec);
Sage Weil2f2dc052009-10-06 11:31:09 -07002452
2453 /* calculate (max) length for cap releases */
2454 len += sizeof(struct ceph_mds_request_release) *
2455 (!!req->r_inode_drop + !!req->r_dentry_drop +
2456 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
2457 if (req->r_dentry_drop)
Jeff Laytonc1dfc272019-04-17 14:23:17 -04002458 len += pathlen1;
Sage Weil2f2dc052009-10-06 11:31:09 -07002459 if (req->r_old_dentry_drop)
Jeff Laytonc1dfc272019-04-17 14:23:17 -04002460 len += pathlen2;
Sage Weil2f2dc052009-10-06 11:31:09 -07002461
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02002462 msg = ceph_msg_new2(CEPH_MSG_CLIENT_REQUEST, len, 1, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07002463 if (!msg) {
2464 msg = ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -07002465 goto out_free2;
Sage Weila79832f2010-04-01 16:06:19 -07002466 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002467
John Spray7cfa0312014-10-30 17:15:26 +00002468 msg->hdr.version = cpu_to_le16(2);
Sage Weil6df058c2009-12-22 11:24:33 -08002469 msg->hdr.tid = cpu_to_le64(req->r_tid);
2470
Sage Weil2f2dc052009-10-06 11:31:09 -07002471 head = msg->front.iov_base;
2472 p = msg->front.iov_base + sizeof(*head);
2473 end = msg->front.iov_base + msg->front.iov_len;
2474
2475 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
2476 head->op = cpu_to_le32(req->r_op);
Eric W. Biedermanff3d0042013-01-31 04:01:53 -08002477 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid));
2478 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid));
Jeff Layton6deb8002020-01-13 13:04:08 -05002479 head->ino = cpu_to_le64(req->r_deleg_ino);
Sage Weil2f2dc052009-10-06 11:31:09 -07002480 head->args = req->r_args;
2481
2482 ceph_encode_filepath(&p, end, ino1, path1);
2483 ceph_encode_filepath(&p, end, ino2, path2);
2484
Sage Weile979cf52010-07-15 14:58:39 -07002485 /* make note of release offset, in case we need to replay */
2486 req->r_request_release_offset = p - msg->front.iov_base;
2487
Sage Weil2f2dc052009-10-06 11:31:09 -07002488 /* cap releases */
2489 releases = 0;
2490 if (req->r_inode_drop)
2491 releases += ceph_encode_inode_release(&p,
David Howells2b0143b2015-03-17 22:25:59 +00002492 req->r_inode ? req->r_inode : d_inode(req->r_dentry),
Yan, Zheng719a2512020-03-05 20:21:00 +08002493 mds, req->r_inode_drop, req->r_inode_unless,
2494 req->r_op == CEPH_MDS_OP_READDIR);
Sage Weil2f2dc052009-10-06 11:31:09 -07002495 if (req->r_dentry_drop)
2496 releases += ceph_encode_dentry_release(&p, req->r_dentry,
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002497 req->r_parent, mds, req->r_dentry_drop,
Jeff Laytonca6c8ae2016-12-15 08:37:59 -05002498 req->r_dentry_unless);
Sage Weil2f2dc052009-10-06 11:31:09 -07002499 if (req->r_old_dentry_drop)
2500 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
Jeff Laytonca6c8ae2016-12-15 08:37:59 -05002501 req->r_old_dentry_dir, mds,
2502 req->r_old_dentry_drop,
2503 req->r_old_dentry_unless);
Sage Weil2f2dc052009-10-06 11:31:09 -07002504 if (req->r_old_inode_drop)
2505 releases += ceph_encode_inode_release(&p,
David Howells2b0143b2015-03-17 22:25:59 +00002506 d_inode(req->r_old_dentry),
Sage Weil2f2dc052009-10-06 11:31:09 -07002507 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002508
2509 if (drop_cap_releases) {
2510 releases = 0;
2511 p = msg->front.iov_base + req->r_request_release_offset;
2512 }
2513
Sage Weil2f2dc052009-10-06 11:31:09 -07002514 head->num_releases = cpu_to_le16(releases);
2515
Sage Weilb8e69062014-05-21 17:41:08 -07002516 /* time stamp */
Yan, Zheng1f041a82015-01-13 15:20:52 +08002517 {
2518 struct ceph_timespec ts;
Arnd Bergmann0ed1e902018-07-13 22:18:38 +02002519 ceph_encode_timespec64(&ts, &req->r_stamp);
Yan, Zheng1f041a82015-01-13 15:20:52 +08002520 ceph_encode_copy(&p, &ts, sizeof(ts));
2521 }
Sage Weilb8e69062014-05-21 17:41:08 -07002522
Sage Weil2f2dc052009-10-06 11:31:09 -07002523 BUG_ON(p > end);
2524 msg->front.iov_len = p - msg->front.iov_base;
2525 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2526
Yan, Zheng25e6bae2014-09-16 19:15:28 +08002527 if (req->r_pagelist) {
2528 struct ceph_pagelist *pagelist = req->r_pagelist;
Yan, Zheng25e6bae2014-09-16 19:15:28 +08002529 ceph_msg_data_add_pagelist(msg, pagelist);
2530 msg->hdr.data_len = cpu_to_le32(pagelist->length);
2531 } else {
2532 msg->hdr.data_len = 0;
Alex Elderebf18f42013-03-04 22:29:57 -06002533 }
Alex Elder02afca62013-02-14 12:16:43 -06002534
Sage Weil2f2dc052009-10-06 11:31:09 -07002535 msg->hdr.data_off = cpu_to_le16(0);
2536
2537out_free2:
2538 if (freepath2)
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002539 ceph_mdsc_free_path((char *)path2, pathlen2);
Sage Weil2f2dc052009-10-06 11:31:09 -07002540out_free1:
2541 if (freepath1)
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002542 ceph_mdsc_free_path((char *)path1, pathlen1);
Sage Weil2f2dc052009-10-06 11:31:09 -07002543out:
2544 return msg;
2545}
2546
2547/*
2548 * called under mdsc->mutex if error, under no mutex if
2549 * success.
2550 */
2551static void complete_request(struct ceph_mds_client *mdsc,
2552 struct ceph_mds_request *req)
2553{
Xiubo Li70c94822020-03-19 23:45:02 -04002554 req->r_end_latency = ktime_get();
2555
Sage Weil2f2dc052009-10-06 11:31:09 -07002556 if (req->r_callback)
2557 req->r_callback(mdsc, req);
Jeff Layton111c7082019-04-02 09:43:18 -04002558 complete_all(&req->r_completion);
Sage Weil2f2dc052009-10-06 11:31:09 -07002559}
2560
2561/*
2562 * called under mdsc->mutex
2563 */
2564static int __prepare_send_request(struct ceph_mds_client *mdsc,
2565 struct ceph_mds_request *req,
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002566 int mds, bool drop_cap_releases)
Sage Weil2f2dc052009-10-06 11:31:09 -07002567{
2568 struct ceph_mds_request_head *rhead;
2569 struct ceph_msg *msg;
2570 int flags = 0;
2571
Sage Weil2f2dc052009-10-06 11:31:09 -07002572 req->r_attempts++;
Greg Farnume55b71f2010-06-22 15:58:01 -07002573 if (req->r_inode) {
2574 struct ceph_cap *cap =
2575 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
2576
2577 if (cap)
2578 req->r_sent_on_mseq = cap->mseq;
2579 else
2580 req->r_sent_on_mseq = -1;
2581 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002582 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
2583 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
2584
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002585 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002586 void *p;
Sage Weil01a92f12010-07-15 13:24:32 -07002587 /*
2588 * Replay. Do not regenerate message (and rebuild
2589 * paths, etc.); just use the original message.
2590 * Rebuilding paths will break for renames because
2591 * d_move mangles the src name.
2592 */
2593 msg = req->r_request;
2594 rhead = msg->front.iov_base;
2595
2596 flags = le32_to_cpu(rhead->flags);
2597 flags |= CEPH_MDS_FLAG_REPLAY;
2598 rhead->flags = cpu_to_le32(flags);
2599
2600 if (req->r_target_inode)
2601 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
2602
2603 rhead->num_retry = req->r_attempts - 1;
Sage Weile979cf52010-07-15 14:58:39 -07002604
2605 /* remove cap/dentry releases from message */
2606 rhead->num_releases = 0;
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002607
2608 /* time stamp */
2609 p = msg->front.iov_base + req->r_request_release_offset;
Yan, Zheng1f041a82015-01-13 15:20:52 +08002610 {
2611 struct ceph_timespec ts;
Arnd Bergmann0ed1e902018-07-13 22:18:38 +02002612 ceph_encode_timespec64(&ts, &req->r_stamp);
Yan, Zheng1f041a82015-01-13 15:20:52 +08002613 ceph_encode_copy(&p, &ts, sizeof(ts));
2614 }
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002615
2616 msg->front.iov_len = p - msg->front.iov_base;
2617 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
Sage Weil01a92f12010-07-15 13:24:32 -07002618 return 0;
2619 }
2620
Sage Weil2f2dc052009-10-06 11:31:09 -07002621 if (req->r_request) {
2622 ceph_msg_put(req->r_request);
2623 req->r_request = NULL;
2624 }
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002625 msg = create_request_message(mdsc, req, mds, drop_cap_releases);
Sage Weil2f2dc052009-10-06 11:31:09 -07002626 if (IS_ERR(msg)) {
Sage Weile1518c72010-05-13 11:19:06 -07002627 req->r_err = PTR_ERR(msg);
Sage Weila79832f2010-04-01 16:06:19 -07002628 return PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002629 }
2630 req->r_request = msg;
2631
2632 rhead = msg->front.iov_base;
Sage Weil2f2dc052009-10-06 11:31:09 -07002633 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002634 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Sage Weil2f2dc052009-10-06 11:31:09 -07002635 flags |= CEPH_MDS_FLAG_REPLAY;
Jeff Layton3bb48b42019-12-02 13:47:57 -05002636 if (test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags))
2637 flags |= CEPH_MDS_FLAG_ASYNC;
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002638 if (req->r_parent)
Sage Weil2f2dc052009-10-06 11:31:09 -07002639 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
2640 rhead->flags = cpu_to_le32(flags);
2641 rhead->num_fwd = req->r_num_fwd;
2642 rhead->num_retry = req->r_attempts - 1;
2643
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002644 dout(" r_parent = %p\n", req->r_parent);
Sage Weil2f2dc052009-10-06 11:31:09 -07002645 return 0;
2646}
2647
2648/*
Xiubo Li9cf54562019-12-05 20:50:21 -05002649 * called under mdsc->mutex
2650 */
2651static int __send_request(struct ceph_mds_client *mdsc,
2652 struct ceph_mds_session *session,
2653 struct ceph_mds_request *req,
2654 bool drop_cap_releases)
2655{
2656 int err;
2657
2658 err = __prepare_send_request(mdsc, req, session->s_mds,
2659 drop_cap_releases);
2660 if (!err) {
2661 ceph_msg_get(req->r_request);
2662 ceph_con_send(&session->s_con, req->r_request);
2663 }
2664
2665 return err;
2666}
2667
2668/*
Sage Weil2f2dc052009-10-06 11:31:09 -07002669 * send request, or put it on the appropriate wait list.
2670 */
Chengguang Xud5548492018-07-28 16:30:48 +08002671static void __do_request(struct ceph_mds_client *mdsc,
Sage Weil2f2dc052009-10-06 11:31:09 -07002672 struct ceph_mds_request *req)
2673{
2674 struct ceph_mds_session *session = NULL;
2675 int mds = -1;
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002676 int err = 0;
Xiubo Lic4853e92019-12-09 07:47:15 -05002677 bool random;
Sage Weil2f2dc052009-10-06 11:31:09 -07002678
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002679 if (req->r_err || test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
2680 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
Yan, Zhengeb1b8af2013-09-26 14:25:36 +08002681 __unregister_request(mdsc, req);
Chengguang Xud5548492018-07-28 16:30:48 +08002682 return;
Yan, Zhengeb1b8af2013-09-26 14:25:36 +08002683 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002684
2685 if (req->r_timeout &&
2686 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
2687 dout("do_request timed out\n");
Xiubo Li8ccf7fc2020-02-23 22:23:11 -05002688 err = -ETIMEDOUT;
Sage Weil2f2dc052009-10-06 11:31:09 -07002689 goto finish;
2690 }
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01002691 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002692 dout("do_request forced umount\n");
2693 err = -EIO;
2694 goto finish;
2695 }
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01002696 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_MOUNTING) {
Yan, Zhenge9e427f2016-11-10 16:02:06 +08002697 if (mdsc->mdsmap_err) {
2698 err = mdsc->mdsmap_err;
2699 dout("do_request mdsmap err %d\n", err);
2700 goto finish;
2701 }
Yan, Zhengcc8e8342017-01-04 16:21:58 +08002702 if (mdsc->mdsmap->m_epoch == 0) {
2703 dout("do_request no mdsmap, waiting for map\n");
2704 list_add(&req->r_wait, &mdsc->waiting_for_map);
Chengguang Xud5548492018-07-28 16:30:48 +08002705 return;
Yan, Zhengcc8e8342017-01-04 16:21:58 +08002706 }
Yan, Zhenge9e427f2016-11-10 16:02:06 +08002707 if (!(mdsc->fsc->mount_options->flags &
2708 CEPH_MOUNT_OPT_MOUNTWAIT) &&
2709 !ceph_mdsmap_is_cluster_available(mdsc->mdsmap)) {
Xiubo Li97820052019-12-10 20:29:40 -05002710 err = -EHOSTUNREACH;
Yan, Zhenge9e427f2016-11-10 16:02:06 +08002711 goto finish;
2712 }
2713 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002714
Sage Weildc69e2e2010-11-02 13:49:00 -07002715 put_request_session(req);
2716
Xiubo Lic4853e92019-12-09 07:47:15 -05002717 mds = __choose_mds(mdsc, req, &random);
Sage Weil2f2dc052009-10-06 11:31:09 -07002718 if (mds < 0 ||
2719 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
Jeff Layton3bb48b42019-12-02 13:47:57 -05002720 if (test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags)) {
2721 err = -EJUKEBOX;
2722 goto finish;
2723 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002724 dout("do_request no mds or not active, waiting for map\n");
2725 list_add(&req->r_wait, &mdsc->waiting_for_map);
Chengguang Xud5548492018-07-28 16:30:48 +08002726 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07002727 }
2728
2729 /* get, open session */
2730 session = __ceph_lookup_mds_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07002731 if (!session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002732 session = register_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07002733 if (IS_ERR(session)) {
2734 err = PTR_ERR(session);
2735 goto finish;
2736 }
2737 }
Xiubo Li5b3248c2019-12-19 19:44:09 -05002738 req->r_session = ceph_get_mds_session(session);
Sage Weildc69e2e2010-11-02 13:49:00 -07002739
Sage Weil2f2dc052009-10-06 11:31:09 -07002740 dout("do_request mds%d session %p state %s\n", mds, session,
John Spraya687eca2014-09-19 13:51:08 +01002741 ceph_session_state_name(session->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07002742 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
2743 session->s_state != CEPH_MDS_SESSION_HUNG) {
Yan, Zhengfcff4152016-09-14 16:39:51 +08002744 if (session->s_state == CEPH_MDS_SESSION_REJECTED) {
2745 err = -EACCES;
2746 goto out_session;
2747 }
Jeff Layton3bb48b42019-12-02 13:47:57 -05002748 /*
2749 * We cannot queue async requests since the caps and delegated
2750 * inodes are bound to the session. Just return -EJUKEBOX and
2751 * let the caller retry a sync request in that case.
2752 */
2753 if (test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags)) {
2754 err = -EJUKEBOX;
2755 goto out_session;
2756 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002757 if (session->s_state == CEPH_MDS_SESSION_NEW ||
Xiubo Lic4853e92019-12-09 07:47:15 -05002758 session->s_state == CEPH_MDS_SESSION_CLOSING) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002759 __open_session(mdsc, session);
Xiubo Lic4853e92019-12-09 07:47:15 -05002760 /* retry the same mds later */
2761 if (random)
2762 req->r_resend_mds = mds;
2763 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002764 list_add(&req->r_wait, &session->s_waiting);
2765 goto out_session;
2766 }
2767
2768 /* send request */
Sage Weil2f2dc052009-10-06 11:31:09 -07002769 req->r_resend_mds = -1; /* forget any previous mds hint */
2770
2771 if (req->r_request_started == 0) /* note request start time */
2772 req->r_request_started = jiffies;
2773
Xiubo Li9cf54562019-12-05 20:50:21 -05002774 err = __send_request(mdsc, session, req, false);
Sage Weil2f2dc052009-10-06 11:31:09 -07002775
2776out_session:
2777 ceph_put_mds_session(session);
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002778finish:
2779 if (err) {
2780 dout("__do_request early error %d\n", err);
2781 req->r_err = err;
2782 complete_request(mdsc, req);
2783 __unregister_request(mdsc, req);
2784 }
Chengguang Xud5548492018-07-28 16:30:48 +08002785 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07002786}
2787
2788/*
2789 * called under mdsc->mutex
2790 */
2791static void __wake_requests(struct ceph_mds_client *mdsc,
2792 struct list_head *head)
2793{
Yan, Zhenged75ec22012-11-19 10:49:06 +08002794 struct ceph_mds_request *req;
2795 LIST_HEAD(tmp_list);
Sage Weil2f2dc052009-10-06 11:31:09 -07002796
Yan, Zhenged75ec22012-11-19 10:49:06 +08002797 list_splice_init(head, &tmp_list);
2798
2799 while (!list_empty(&tmp_list)) {
2800 req = list_entry(tmp_list.next,
2801 struct ceph_mds_request, r_wait);
Sage Weil2f2dc052009-10-06 11:31:09 -07002802 list_del_init(&req->r_wait);
Sage Weil7971bd92013-05-01 21:15:58 -07002803 dout(" wake request %p tid %llu\n", req, req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002804 __do_request(mdsc, req);
2805 }
2806}
2807
2808/*
2809 * Wake up threads with requests pending for @mds, so that they can
Sage Weil29790f22010-03-18 14:45:05 -07002810 * resubmit their requests to a possibly different mds.
Sage Weil2f2dc052009-10-06 11:31:09 -07002811 */
Sage Weil29790f22010-03-18 14:45:05 -07002812static void kick_requests(struct ceph_mds_client *mdsc, int mds)
Sage Weil2f2dc052009-10-06 11:31:09 -07002813{
Sage Weil44ca18f2010-02-15 12:08:46 -08002814 struct ceph_mds_request *req;
Yan, Zheng282c1052014-07-30 10:12:47 +08002815 struct rb_node *p = rb_first(&mdsc->request_tree);
Sage Weil2f2dc052009-10-06 11:31:09 -07002816
2817 dout("kick_requests mds%d\n", mds);
Yan, Zheng282c1052014-07-30 10:12:47 +08002818 while (p) {
Sage Weil44ca18f2010-02-15 12:08:46 -08002819 req = rb_entry(p, struct ceph_mds_request, r_node);
Yan, Zheng282c1052014-07-30 10:12:47 +08002820 p = rb_next(p);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002821 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Sage Weil44ca18f2010-02-15 12:08:46 -08002822 continue;
Yan, Zheng3de22be2015-02-04 14:26:22 +08002823 if (req->r_attempts > 0)
2824 continue; /* only new requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08002825 if (req->r_session &&
2826 req->r_session->s_mds == mds) {
2827 dout(" kicking tid %llu\n", req->r_tid);
Yan, Zheng03974e82014-09-11 14:28:56 +08002828 list_del_init(&req->r_wait);
Sage Weil44ca18f2010-02-15 12:08:46 -08002829 __do_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002830 }
2831 }
2832}
2833
Jeff Layton86bda532019-04-02 09:24:36 -04002834int ceph_mdsc_submit_request(struct ceph_mds_client *mdsc, struct inode *dir,
Sage Weil2f2dc052009-10-06 11:31:09 -07002835 struct ceph_mds_request *req)
2836{
Jeff Layton891f3f52020-01-14 15:06:40 -05002837 int err = 0;
Jeff Layton86bda532019-04-02 09:24:36 -04002838
2839 /* take CAP_PIN refs for r_inode, r_parent, r_old_dentry */
2840 if (req->r_inode)
2841 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
Jeff Layton9c1c2b32019-04-03 13:16:01 -04002842 if (req->r_parent) {
Yan, Zheng719a2512020-03-05 20:21:00 +08002843 struct ceph_inode_info *ci = ceph_inode(req->r_parent);
2844 int fmode = (req->r_op & CEPH_MDS_OP_WRITE) ?
2845 CEPH_FILE_MODE_WR : CEPH_FILE_MODE_RD;
2846 spin_lock(&ci->i_ceph_lock);
2847 ceph_take_cap_refs(ci, CEPH_CAP_PIN, false);
2848 __ceph_touch_fmode(ci, mdsc, fmode);
2849 spin_unlock(&ci->i_ceph_lock);
Jeff Layton9c1c2b32019-04-03 13:16:01 -04002850 ihold(req->r_parent);
2851 }
Jeff Layton86bda532019-04-02 09:24:36 -04002852 if (req->r_old_dentry_dir)
2853 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
2854 CEPH_CAP_PIN);
2855
Jeff Layton891f3f52020-01-14 15:06:40 -05002856 if (req->r_inode) {
2857 err = ceph_wait_on_async_create(req->r_inode);
2858 if (err) {
2859 dout("%s: wait for async create returned: %d\n",
2860 __func__, err);
2861 return err;
2862 }
2863 }
2864
2865 if (!err && req->r_old_inode) {
2866 err = ceph_wait_on_async_create(req->r_old_inode);
2867 if (err) {
2868 dout("%s: wait for async create returned: %d\n",
2869 __func__, err);
2870 return err;
2871 }
2872 }
2873
Jeff Layton86bda532019-04-02 09:24:36 -04002874 dout("submit_request on %p for inode %p\n", req, dir);
Sage Weil2f2dc052009-10-06 11:31:09 -07002875 mutex_lock(&mdsc->mutex);
Jeff Layton86bda532019-04-02 09:24:36 -04002876 __register_request(mdsc, req, dir);
Sage Weil2f2dc052009-10-06 11:31:09 -07002877 __do_request(mdsc, req);
Jeff Layton86bda532019-04-02 09:24:36 -04002878 err = req->r_err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002879 mutex_unlock(&mdsc->mutex);
Jeff Layton86bda532019-04-02 09:24:36 -04002880 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002881}
2882
Jeff Layton8340f222019-04-02 12:34:38 -04002883static int ceph_mdsc_wait_request(struct ceph_mds_client *mdsc,
2884 struct ceph_mds_request *req)
Sage Weil2f2dc052009-10-06 11:31:09 -07002885{
2886 int err;
2887
Sage Weile1518c72010-05-13 11:19:06 -07002888 /* wait */
Sage Weile1518c72010-05-13 11:19:06 -07002889 dout("do_request waiting\n");
Ilya Dryomov5be73032015-05-19 12:05:38 +03002890 if (!req->r_timeout && req->r_wait_for_completion) {
Yan, Zheng9280be22014-10-14 10:33:35 +08002891 err = req->r_wait_for_completion(mdsc, req);
Sage Weile1518c72010-05-13 11:19:06 -07002892 } else {
Ilya Dryomov5be73032015-05-19 12:05:38 +03002893 long timeleft = wait_for_completion_killable_timeout(
2894 &req->r_completion,
2895 ceph_timeout_jiffies(req->r_timeout));
2896 if (timeleft > 0)
2897 err = 0;
2898 else if (!timeleft)
Xiubo Li8ccf7fc2020-02-23 22:23:11 -05002899 err = -ETIMEDOUT; /* timed out */
Ilya Dryomov5be73032015-05-19 12:05:38 +03002900 else
2901 err = timeleft; /* killed */
Sage Weile1518c72010-05-13 11:19:06 -07002902 }
2903 dout("do_request waited, got %d\n", err);
2904 mutex_lock(&mdsc->mutex);
2905
2906 /* only abort if we didn't race with a real reply */
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002907 if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
Sage Weile1518c72010-05-13 11:19:06 -07002908 err = le32_to_cpu(req->r_reply_info.head->result);
2909 } else if (err < 0) {
2910 dout("aborted request %lld with %d\n", req->r_tid, err);
Sage Weilb4556392010-05-13 12:01:13 -07002911
2912 /*
2913 * ensure we aren't running concurrently with
2914 * ceph_fill_trace or ceph_readdir_prepopulate, which
2915 * rely on locks (dir mutex) held by our caller.
2916 */
2917 mutex_lock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07002918 req->r_err = err;
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002919 set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
Sage Weilb4556392010-05-13 12:01:13 -07002920 mutex_unlock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07002921
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002922 if (req->r_parent &&
Sage Weil167c9e32010-05-14 10:02:57 -07002923 (req->r_op & CEPH_MDS_OP_WRITE))
2924 ceph_invalidate_dir_request(req);
Sage Weile1518c72010-05-13 11:19:06 -07002925 } else {
2926 err = req->r_err;
2927 }
2928
Sage Weile1518c72010-05-13 11:19:06 -07002929 mutex_unlock(&mdsc->mutex);
Jeff Layton8340f222019-04-02 12:34:38 -04002930 return err;
2931}
2932
2933/*
2934 * Synchrously perform an mds request. Take care of all of the
2935 * session setup, forwarding, retry details.
2936 */
2937int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
2938 struct inode *dir,
2939 struct ceph_mds_request *req)
2940{
2941 int err;
2942
2943 dout("do_request on %p\n", req);
2944
2945 /* issue */
2946 err = ceph_mdsc_submit_request(mdsc, dir, req);
2947 if (!err)
2948 err = ceph_mdsc_wait_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002949 dout("do_request %p done, result %d\n", req, err);
2950 return err;
2951}
2952
2953/*
Yan, Zheng2f276c52013-03-13 19:44:32 +08002954 * Invalidate dir's completeness, dentry lease state on an aborted MDS
Sage Weil167c9e32010-05-14 10:02:57 -07002955 * namespace request.
2956 */
2957void ceph_invalidate_dir_request(struct ceph_mds_request *req)
2958{
Yan, Zheng8d8f3712017-11-24 11:51:32 +08002959 struct inode *dir = req->r_parent;
2960 struct inode *old_dir = req->r_old_dentry_dir;
Sage Weil167c9e32010-05-14 10:02:57 -07002961
Yan, Zheng8d8f3712017-11-24 11:51:32 +08002962 dout("invalidate_dir_request %p %p (complete, lease(s))\n", dir, old_dir);
Sage Weil167c9e32010-05-14 10:02:57 -07002963
Yan, Zheng8d8f3712017-11-24 11:51:32 +08002964 ceph_dir_clear_complete(dir);
2965 if (old_dir)
2966 ceph_dir_clear_complete(old_dir);
Sage Weil167c9e32010-05-14 10:02:57 -07002967 if (req->r_dentry)
2968 ceph_invalidate_dentry_lease(req->r_dentry);
2969 if (req->r_old_dentry)
2970 ceph_invalidate_dentry_lease(req->r_old_dentry);
2971}
2972
2973/*
Sage Weil2f2dc052009-10-06 11:31:09 -07002974 * Handle mds reply.
2975 *
2976 * We take the session mutex and parse and process the reply immediately.
2977 * This preserves the logical ordering of replies, capabilities, etc., sent
2978 * by the MDS as they are applied to our local cache.
2979 */
2980static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2981{
2982 struct ceph_mds_client *mdsc = session->s_mdsc;
2983 struct ceph_mds_request *req;
2984 struct ceph_mds_reply_head *head = msg->front.iov_base;
2985 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
Yan, Zheng982d6012014-12-23 15:30:54 +08002986 struct ceph_snap_realm *realm;
Sage Weil2f2dc052009-10-06 11:31:09 -07002987 u64 tid;
2988 int err, result;
Sage Weil2600d2d2010-02-22 15:12:16 -08002989 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002990
Sage Weil2f2dc052009-10-06 11:31:09 -07002991 if (msg->front.iov_len < sizeof(*head)) {
2992 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002993 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002994 return;
2995 }
2996
2997 /* get request, session */
Sage Weil6df058c2009-12-22 11:24:33 -08002998 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002999 mutex_lock(&mdsc->mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02003000 req = lookup_get_request(mdsc, tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07003001 if (!req) {
3002 dout("handle_reply on unknown tid %llu\n", tid);
3003 mutex_unlock(&mdsc->mutex);
3004 return;
3005 }
3006 dout("handle_reply %p\n", req);
Sage Weil2f2dc052009-10-06 11:31:09 -07003007
3008 /* correct session? */
Sage Weild96d6042010-03-20 20:50:58 -07003009 if (req->r_session != session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003010 pr_err("mdsc_handle_reply got %llu on session mds%d"
3011 " not mds%d\n", tid, session->s_mds,
3012 req->r_session ? req->r_session->s_mds : -1);
3013 mutex_unlock(&mdsc->mutex);
3014 goto out;
3015 }
3016
3017 /* dup? */
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003018 if ((test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags) && !head->safe) ||
3019 (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags) && head->safe)) {
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07003020 pr_warn("got a dup %s reply on %llu from mds%d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07003021 head->safe ? "safe" : "unsafe", tid, mds);
3022 mutex_unlock(&mdsc->mutex);
3023 goto out;
3024 }
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003025 if (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags)) {
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07003026 pr_warn("got unsafe after safe on %llu from mds%d\n",
Sage Weil85792d0d2010-05-13 09:06:02 -07003027 tid, mds);
3028 mutex_unlock(&mdsc->mutex);
3029 goto out;
3030 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003031
3032 result = le32_to_cpu(head->result);
3033
3034 /*
Greg Farnume55b71f2010-06-22 15:58:01 -07003035 * Handle an ESTALE
3036 * if we're not talking to the authority, send to them
3037 * if the authority has changed while we weren't looking,
3038 * send to new authority
3039 * Otherwise we just have to return an ESTALE
Sage Weil2f2dc052009-10-06 11:31:09 -07003040 */
3041 if (result == -ESTALE) {
Chengguang Xu4c069a52018-01-30 16:29:17 +08003042 dout("got ESTALE on request %llu\n", req->r_tid);
Yan, Zheng51da8e82014-07-14 10:48:29 +08003043 req->r_resend_mds = -1;
Yan, Zhengca18bed2013-11-22 14:21:44 +08003044 if (req->r_direct_mode != USE_AUTH_MDS) {
Chengguang Xu4c069a52018-01-30 16:29:17 +08003045 dout("not using auth, setting for that now\n");
Greg Farnume55b71f2010-06-22 15:58:01 -07003046 req->r_direct_mode = USE_AUTH_MDS;
Sage Weil2f2dc052009-10-06 11:31:09 -07003047 __do_request(mdsc, req);
3048 mutex_unlock(&mdsc->mutex);
3049 goto out;
Greg Farnume55b71f2010-06-22 15:58:01 -07003050 } else {
Xiubo Lic4853e92019-12-09 07:47:15 -05003051 int mds = __choose_mds(mdsc, req, NULL);
Yan, Zhengca18bed2013-11-22 14:21:44 +08003052 if (mds >= 0 && mds != req->r_session->s_mds) {
Chengguang Xu4c069a52018-01-30 16:29:17 +08003053 dout("but auth changed, so resending\n");
Greg Farnume55b71f2010-06-22 15:58:01 -07003054 __do_request(mdsc, req);
3055 mutex_unlock(&mdsc->mutex);
3056 goto out;
3057 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003058 }
Chengguang Xu4c069a52018-01-30 16:29:17 +08003059 dout("have to return ESTALE on request %llu\n", req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07003060 }
3061
Greg Farnume55b71f2010-06-22 15:58:01 -07003062
Sage Weil2f2dc052009-10-06 11:31:09 -07003063 if (head->safe) {
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003064 set_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags);
Sage Weil2f2dc052009-10-06 11:31:09 -07003065 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07003066
Xiubo Li07edc052019-12-04 01:27:18 -05003067 /* last request during umount? */
3068 if (mdsc->stopping && !__get_oldest_req(mdsc))
3069 complete_all(&mdsc->safe_umount_waiters);
3070
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003071 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003072 /*
3073 * We already handled the unsafe response, now do the
3074 * cleanup. No need to examine the response; the MDS
3075 * doesn't include any result info in the safe
3076 * response. And even if it did, there is nothing
3077 * useful we could do with a revised return value.
3078 */
3079 dout("got safe reply %llu, mds%d\n", tid, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003080
Sage Weil2f2dc052009-10-06 11:31:09 -07003081 mutex_unlock(&mdsc->mutex);
3082 goto out;
3083 }
Sage Weile1518c72010-05-13 11:19:06 -07003084 } else {
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003085 set_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags);
Sage Weil2f2dc052009-10-06 11:31:09 -07003086 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
3087 }
3088
3089 dout("handle_reply tid %lld result %d\n", tid, result);
3090 rinfo = &req->r_reply_info;
Yan, Zhengb37fe1f2019-01-09 10:10:17 +08003091 if (test_bit(CEPHFS_FEATURE_REPLY_ENCODING, &session->s_features))
Jeff Laytond4846482019-11-15 11:51:55 -05003092 err = parse_reply_info(session, msg, rinfo, (u64)-1);
Yan, Zhengb37fe1f2019-01-09 10:10:17 +08003093 else
Jeff Laytond4846482019-11-15 11:51:55 -05003094 err = parse_reply_info(session, msg, rinfo, session->s_con.peer_features);
Sage Weil2f2dc052009-10-06 11:31:09 -07003095 mutex_unlock(&mdsc->mutex);
3096
3097 mutex_lock(&session->s_mutex);
3098 if (err < 0) {
Herb Shiu25933ab2010-12-01 14:14:38 -08003099 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
Sage Weil9ec7cab2009-12-14 15:13:47 -08003100 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003101 goto out_err;
3102 }
3103
3104 /* snap trace */
Yan, Zheng982d6012014-12-23 15:30:54 +08003105 realm = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07003106 if (rinfo->snapblob_len) {
3107 down_write(&mdsc->snap_rwsem);
3108 ceph_update_snap_trace(mdsc, rinfo->snapblob,
Yan, Zheng982d6012014-12-23 15:30:54 +08003109 rinfo->snapblob + rinfo->snapblob_len,
3110 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP,
3111 &realm);
Sage Weil2f2dc052009-10-06 11:31:09 -07003112 downgrade_write(&mdsc->snap_rwsem);
3113 } else {
3114 down_read(&mdsc->snap_rwsem);
3115 }
3116
3117 /* insert trace into our cache */
Sage Weilb4556392010-05-13 12:01:13 -07003118 mutex_lock(&req->r_fill_mutex);
Yan, Zheng315f2402016-03-07 10:34:50 +08003119 current->journal_info = req;
Jeff Laytonf5a03b02017-01-31 11:06:13 -05003120 err = ceph_fill_trace(mdsc->fsc->sb, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07003121 if (err == 0) {
Sam Lang6e8575f2012-12-28 09:56:46 -08003122 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
Yan, Zheng81c6aea2013-09-18 09:44:13 +08003123 req->r_op == CEPH_MDS_OP_LSSNAP))
Sage Weil2f2dc052009-10-06 11:31:09 -07003124 ceph_readdir_prepopulate(req, req->r_session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003125 }
Yan, Zheng315f2402016-03-07 10:34:50 +08003126 current->journal_info = NULL;
Sage Weilb4556392010-05-13 12:01:13 -07003127 mutex_unlock(&req->r_fill_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003128
3129 up_read(&mdsc->snap_rwsem);
Yan, Zheng982d6012014-12-23 15:30:54 +08003130 if (realm)
3131 ceph_put_snap_realm(mdsc, realm);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08003132
Yan, Zhengfe330322019-02-01 14:57:15 +08003133 if (err == 0) {
3134 if (req->r_target_inode &&
3135 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
3136 struct ceph_inode_info *ci =
3137 ceph_inode(req->r_target_inode);
3138 spin_lock(&ci->i_unsafe_lock);
3139 list_add_tail(&req->r_unsafe_target_item,
3140 &ci->i_unsafe_iops);
3141 spin_unlock(&ci->i_unsafe_lock);
3142 }
3143
3144 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08003145 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003146out_err:
Sage Weile1518c72010-05-13 11:19:06 -07003147 mutex_lock(&mdsc->mutex);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003148 if (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
Sage Weile1518c72010-05-13 11:19:06 -07003149 if (err) {
3150 req->r_err = err;
3151 } else {
Jianpeng Ma5fdb1382015-08-18 10:30:38 +08003152 req->r_reply = ceph_msg_get(msg);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003153 set_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags);
Sage Weile1518c72010-05-13 11:19:06 -07003154 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003155 } else {
Sage Weile1518c72010-05-13 11:19:06 -07003156 dout("reply arrived after request %lld was aborted\n", tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07003157 }
Sage Weile1518c72010-05-13 11:19:06 -07003158 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003159
Sage Weil2f2dc052009-10-06 11:31:09 -07003160 mutex_unlock(&session->s_mutex);
3161
3162 /* kick calling process */
3163 complete_request(mdsc, req);
Xiubo Li70c94822020-03-19 23:45:02 -04003164
3165 ceph_update_metadata_latency(&mdsc->metric, req->r_start_latency,
3166 req->r_end_latency, err);
Sage Weil2f2dc052009-10-06 11:31:09 -07003167out:
3168 ceph_mdsc_put_request(req);
3169 return;
3170}
3171
3172
3173
3174/*
3175 * handle mds notification that our request has been forwarded.
3176 */
Sage Weil2600d2d2010-02-22 15:12:16 -08003177static void handle_forward(struct ceph_mds_client *mdsc,
3178 struct ceph_mds_session *session,
3179 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07003180{
3181 struct ceph_mds_request *req;
Sage Weila1ea7872010-02-23 14:02:44 -08003182 u64 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07003183 u32 next_mds;
3184 u32 fwd_seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003185 int err = -EINVAL;
3186 void *p = msg->front.iov_base;
3187 void *end = p + msg->front.iov_len;
Sage Weil2f2dc052009-10-06 11:31:09 -07003188
Sage Weila1ea7872010-02-23 14:02:44 -08003189 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07003190 next_mds = ceph_decode_32(&p);
3191 fwd_seq = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07003192
3193 mutex_lock(&mdsc->mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02003194 req = lookup_get_request(mdsc, tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07003195 if (!req) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07003196 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003197 goto out; /* dup reply? */
3198 }
3199
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003200 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07003201 dout("forward tid %llu aborted, unregistering\n", tid);
3202 __unregister_request(mdsc, req);
3203 } else if (fwd_seq <= req->r_num_fwd) {
3204 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07003205 tid, next_mds, req->r_num_fwd, fwd_seq);
3206 } else {
3207 /* resend. forward race not possible; mds would drop */
Sage Weil2a8e5e32010-05-28 16:43:16 -07003208 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
3209 BUG_ON(req->r_err);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003210 BUG_ON(test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags));
Yan, Zheng3de22be2015-02-04 14:26:22 +08003211 req->r_attempts = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003212 req->r_num_fwd = fwd_seq;
3213 req->r_resend_mds = next_mds;
3214 put_request_session(req);
3215 __do_request(mdsc, req);
3216 }
3217 ceph_mdsc_put_request(req);
3218out:
3219 mutex_unlock(&mdsc->mutex);
3220 return;
3221
3222bad:
3223 pr_err("mdsc_handle_forward decode error err=%d\n", err);
3224}
3225
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003226static int __decode_session_metadata(void **p, void *end,
3227 bool *blacklisted)
Yan, Zheng84bf3952018-12-21 17:41:39 +08003228{
3229 /* map<string,string> */
3230 u32 n;
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003231 bool err_str;
Yan, Zheng84bf3952018-12-21 17:41:39 +08003232 ceph_decode_32_safe(p, end, n, bad);
3233 while (n-- > 0) {
3234 u32 len;
3235 ceph_decode_32_safe(p, end, len, bad);
3236 ceph_decode_need(p, end, len, bad);
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003237 err_str = !strncmp(*p, "error_string", len);
Yan, Zheng84bf3952018-12-21 17:41:39 +08003238 *p += len;
3239 ceph_decode_32_safe(p, end, len, bad);
3240 ceph_decode_need(p, end, len, bad);
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003241 if (err_str && strnstr(*p, "blacklisted", len))
3242 *blacklisted = true;
Yan, Zheng84bf3952018-12-21 17:41:39 +08003243 *p += len;
3244 }
3245 return 0;
3246bad:
3247 return -1;
3248}
3249
Sage Weil2f2dc052009-10-06 11:31:09 -07003250/*
3251 * handle a mds session control message
3252 */
3253static void handle_session(struct ceph_mds_session *session,
3254 struct ceph_msg *msg)
3255{
3256 struct ceph_mds_client *mdsc = session->s_mdsc;
Yan, Zheng84bf3952018-12-21 17:41:39 +08003257 int mds = session->s_mds;
3258 int msg_version = le16_to_cpu(msg->hdr.version);
3259 void *p = msg->front.iov_base;
3260 void *end = p + msg->front.iov_len;
3261 struct ceph_mds_session_head *h;
Sage Weil2f2dc052009-10-06 11:31:09 -07003262 u32 op;
Jeff Layton0fa82632020-04-28 08:10:22 -04003263 u64 seq, features = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003264 int wake = 0;
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003265 bool blacklisted = false;
Sage Weil2f2dc052009-10-06 11:31:09 -07003266
Sage Weil2f2dc052009-10-06 11:31:09 -07003267 /* decode */
Yan, Zheng84bf3952018-12-21 17:41:39 +08003268 ceph_decode_need(&p, end, sizeof(*h), bad);
3269 h = p;
3270 p += sizeof(*h);
3271
Sage Weil2f2dc052009-10-06 11:31:09 -07003272 op = le32_to_cpu(h->op);
3273 seq = le64_to_cpu(h->seq);
3274
Yan, Zheng84bf3952018-12-21 17:41:39 +08003275 if (msg_version >= 3) {
3276 u32 len;
3277 /* version >= 2, metadata */
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003278 if (__decode_session_metadata(&p, end, &blacklisted) < 0)
Yan, Zheng84bf3952018-12-21 17:41:39 +08003279 goto bad;
3280 /* version >= 3, feature bits */
3281 ceph_decode_32_safe(&p, end, len, bad);
Jeff Layton0fa82632020-04-28 08:10:22 -04003282 ceph_decode_64_safe(&p, end, features, bad);
3283 p += len - sizeof(features);
Yan, Zheng84bf3952018-12-21 17:41:39 +08003284 }
3285
Sage Weil2f2dc052009-10-06 11:31:09 -07003286 mutex_lock(&mdsc->mutex);
Yan, Zheng0a07fc82017-03-29 15:30:24 +08003287 if (op == CEPH_SESSION_CLOSE) {
Xiubo Li5b3248c2019-12-19 19:44:09 -05003288 ceph_get_mds_session(session);
Sage Weil2600d2d2010-02-22 15:12:16 -08003289 __unregister_session(mdsc, session);
Yan, Zheng0a07fc82017-03-29 15:30:24 +08003290 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003291 /* FIXME: this ttl calculation is generous */
3292 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
3293 mutex_unlock(&mdsc->mutex);
3294
3295 mutex_lock(&session->s_mutex);
3296
3297 dout("handle_session mds%d %s %p state %s seq %llu\n",
3298 mds, ceph_session_op_name(op), session,
John Spraya687eca2014-09-19 13:51:08 +01003299 ceph_session_state_name(session->s_state), seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003300
3301 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
3302 session->s_state = CEPH_MDS_SESSION_OPEN;
3303 pr_info("mds%d came back\n", session->s_mds);
3304 }
3305
3306 switch (op) {
3307 case CEPH_SESSION_OPEN:
Sage Weil29790f22010-03-18 14:45:05 -07003308 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
3309 pr_info("mds%d reconnect success\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003310 session->s_state = CEPH_MDS_SESSION_OPEN;
Yan, Zheng84bf3952018-12-21 17:41:39 +08003311 session->s_features = features;
Sage Weil2f2dc052009-10-06 11:31:09 -07003312 renewed_caps(mdsc, session, 0);
3313 wake = 1;
3314 if (mdsc->stopping)
3315 __close_session(mdsc, session);
3316 break;
3317
3318 case CEPH_SESSION_RENEWCAPS:
3319 if (session->s_renew_seq == seq)
3320 renewed_caps(mdsc, session, 1);
3321 break;
3322
3323 case CEPH_SESSION_CLOSE:
Sage Weil29790f22010-03-18 14:45:05 -07003324 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
3325 pr_info("mds%d reconnect denied\n", session->s_mds);
Xiubo Li4d681c22019-12-05 22:35:51 -05003326 session->s_state = CEPH_MDS_SESSION_CLOSED;
Yan, Zheng1c841a92015-03-24 20:15:36 +08003327 cleanup_session_requests(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003328 remove_session_caps(session);
Yan, Zheng656e4382014-09-11 14:25:18 +08003329 wake = 2; /* for good measure */
Sage Weilf3c60c52010-08-11 14:51:23 -07003330 wake_up_all(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003331 break;
3332
3333 case CEPH_SESSION_STALE:
3334 pr_info("mds%d caps went stale, renewing\n",
3335 session->s_mds);
Alex Elderd8fb02a2012-01-12 17:48:10 -08003336 spin_lock(&session->s_gen_ttl_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07003337 session->s_cap_gen++;
Alex Elder1ce208a2012-01-12 17:48:11 -08003338 session->s_cap_ttl = jiffies - 1;
Alex Elderd8fb02a2012-01-12 17:48:10 -08003339 spin_unlock(&session->s_gen_ttl_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07003340 send_renew_caps(mdsc, session);
3341 break;
3342
3343 case CEPH_SESSION_RECALL_STATE:
Zhi Zhange30ee582018-01-24 21:24:33 +08003344 ceph_trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
Sage Weil2f2dc052009-10-06 11:31:09 -07003345 break;
3346
Yan, Zheng186e4f72013-11-22 14:48:37 +08003347 case CEPH_SESSION_FLUSHMSG:
3348 send_flushmsg_ack(mdsc, session, seq);
3349 break;
3350
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08003351 case CEPH_SESSION_FORCE_RO:
3352 dout("force_session_readonly %p\n", session);
3353 spin_lock(&session->s_cap_lock);
3354 session->s_readonly = true;
3355 spin_unlock(&session->s_cap_lock);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08003356 wake_up_session_caps(session, FORCE_RO);
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08003357 break;
3358
Yan, Zhengfcff4152016-09-14 16:39:51 +08003359 case CEPH_SESSION_REJECT:
3360 WARN_ON(session->s_state != CEPH_MDS_SESSION_OPENING);
3361 pr_info("mds%d rejected session\n", session->s_mds);
3362 session->s_state = CEPH_MDS_SESSION_REJECTED;
3363 cleanup_session_requests(mdsc, session);
3364 remove_session_caps(session);
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003365 if (blacklisted)
3366 mdsc->fsc->blacklisted = true;
Yan, Zhengfcff4152016-09-14 16:39:51 +08003367 wake = 2; /* for good measure */
3368 break;
3369
Sage Weil2f2dc052009-10-06 11:31:09 -07003370 default:
3371 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
3372 WARN_ON(1);
3373 }
3374
3375 mutex_unlock(&session->s_mutex);
3376 if (wake) {
3377 mutex_lock(&mdsc->mutex);
3378 __wake_requests(mdsc, &session->s_waiting);
Yan, Zheng656e4382014-09-11 14:25:18 +08003379 if (wake == 2)
3380 kick_requests(mdsc, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003381 mutex_unlock(&mdsc->mutex);
3382 }
Yan, Zheng0a07fc82017-03-29 15:30:24 +08003383 if (op == CEPH_SESSION_CLOSE)
3384 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003385 return;
3386
3387bad:
3388 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
3389 (int)msg->front.iov_len);
Sage Weil9ec7cab2009-12-14 15:13:47 -08003390 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003391 return;
3392}
3393
Jeff Laytona25949b2020-02-18 14:12:45 -05003394void ceph_mdsc_release_dir_caps(struct ceph_mds_request *req)
3395{
3396 int dcaps;
3397
3398 dcaps = xchg(&req->r_dir_caps, 0);
3399 if (dcaps) {
3400 dout("releasing r_dir_caps=%s\n", ceph_cap_string(dcaps));
3401 ceph_put_cap_refs(ceph_inode(req->r_parent), dcaps);
3402 }
3403}
3404
Xiubo Lie64f44a2020-05-27 09:09:27 -04003405void ceph_mdsc_release_dir_caps_no_check(struct ceph_mds_request *req)
3406{
3407 int dcaps;
3408
3409 dcaps = xchg(&req->r_dir_caps, 0);
3410 if (dcaps) {
3411 dout("releasing r_dir_caps=%s\n", ceph_cap_string(dcaps));
3412 ceph_put_cap_refs_no_check_caps(ceph_inode(req->r_parent),
3413 dcaps);
3414 }
3415}
3416
Sage Weil2f2dc052009-10-06 11:31:09 -07003417/*
3418 * called under session->mutex.
3419 */
3420static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
3421 struct ceph_mds_session *session)
3422{
3423 struct ceph_mds_request *req, *nreq;
Yan, Zheng3de22be2015-02-04 14:26:22 +08003424 struct rb_node *p;
Sage Weil2f2dc052009-10-06 11:31:09 -07003425
3426 dout("replay_unsafe_requests mds%d\n", session->s_mds);
3427
3428 mutex_lock(&mdsc->mutex);
Xiubo Li9cf54562019-12-05 20:50:21 -05003429 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item)
3430 __send_request(mdsc, session, req, true);
Yan, Zheng3de22be2015-02-04 14:26:22 +08003431
3432 /*
3433 * also re-send old requests when MDS enters reconnect stage. So that MDS
3434 * can process completed request in clientreplay stage.
3435 */
3436 p = rb_first(&mdsc->request_tree);
3437 while (p) {
3438 req = rb_entry(p, struct ceph_mds_request, r_node);
3439 p = rb_next(p);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003440 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Yan, Zheng3de22be2015-02-04 14:26:22 +08003441 continue;
3442 if (req->r_attempts == 0)
3443 continue; /* only old requests */
Jeff Laytona25949b2020-02-18 14:12:45 -05003444 if (!req->r_session)
3445 continue;
3446 if (req->r_session->s_mds != session->s_mds)
3447 continue;
3448
Xiubo Lie64f44a2020-05-27 09:09:27 -04003449 ceph_mdsc_release_dir_caps_no_check(req);
Jeff Laytona25949b2020-02-18 14:12:45 -05003450
3451 __send_request(mdsc, session, req, true);
Yan, Zheng3de22be2015-02-04 14:26:22 +08003452 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003453 mutex_unlock(&mdsc->mutex);
3454}
3455
Yan, Zheng81c5a142019-01-01 16:28:33 +08003456static int send_reconnect_partial(struct ceph_reconnect_state *recon_state)
3457{
3458 struct ceph_msg *reply;
3459 struct ceph_pagelist *_pagelist;
3460 struct page *page;
3461 __le32 *addr;
3462 int err = -ENOMEM;
3463
3464 if (!recon_state->allow_multi)
3465 return -ENOSPC;
3466
3467 /* can't handle message that contains both caps and realm */
3468 BUG_ON(!recon_state->nr_caps == !recon_state->nr_realms);
3469
3470 /* pre-allocate new pagelist */
3471 _pagelist = ceph_pagelist_alloc(GFP_NOFS);
3472 if (!_pagelist)
3473 return -ENOMEM;
3474
3475 reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false);
3476 if (!reply)
3477 goto fail_msg;
3478
3479 /* placeholder for nr_caps */
3480 err = ceph_pagelist_encode_32(_pagelist, 0);
3481 if (err < 0)
3482 goto fail;
3483
3484 if (recon_state->nr_caps) {
3485 /* currently encoding caps */
3486 err = ceph_pagelist_encode_32(recon_state->pagelist, 0);
3487 if (err)
3488 goto fail;
3489 } else {
3490 /* placeholder for nr_realms (currently encoding relams) */
3491 err = ceph_pagelist_encode_32(_pagelist, 0);
3492 if (err < 0)
3493 goto fail;
3494 }
3495
3496 err = ceph_pagelist_encode_8(recon_state->pagelist, 1);
3497 if (err)
3498 goto fail;
3499
3500 page = list_first_entry(&recon_state->pagelist->head, struct page, lru);
3501 addr = kmap_atomic(page);
3502 if (recon_state->nr_caps) {
3503 /* currently encoding caps */
3504 *addr = cpu_to_le32(recon_state->nr_caps);
3505 } else {
3506 /* currently encoding relams */
3507 *(addr + 1) = cpu_to_le32(recon_state->nr_realms);
3508 }
3509 kunmap_atomic(addr);
3510
3511 reply->hdr.version = cpu_to_le16(5);
3512 reply->hdr.compat_version = cpu_to_le16(4);
3513
3514 reply->hdr.data_len = cpu_to_le32(recon_state->pagelist->length);
3515 ceph_msg_data_add_pagelist(reply, recon_state->pagelist);
3516
3517 ceph_con_send(&recon_state->session->s_con, reply);
3518 ceph_pagelist_release(recon_state->pagelist);
3519
3520 recon_state->pagelist = _pagelist;
3521 recon_state->nr_caps = 0;
3522 recon_state->nr_realms = 0;
3523 recon_state->msg_version = 5;
3524 return 0;
3525fail:
3526 ceph_msg_put(reply);
3527fail_msg:
3528 ceph_pagelist_release(_pagelist);
3529 return err;
3530}
3531
Sage Weil2f2dc052009-10-06 11:31:09 -07003532/*
3533 * Encode information about a cap for a reconnect with the MDS.
3534 */
Jeff Laytona25949b2020-02-18 14:12:45 -05003535static int reconnect_caps_cb(struct inode *inode, struct ceph_cap *cap,
Sage Weil2f2dc052009-10-06 11:31:09 -07003536 void *arg)
3537{
Sage Weil20cb34a2010-05-12 15:21:32 -07003538 union {
3539 struct ceph_mds_cap_reconnect v2;
3540 struct ceph_mds_cap_reconnect_v1 v1;
3541 } rec;
Yan, Zhengb3f8d682017-09-11 10:58:55 +08003542 struct ceph_inode_info *ci = cap->ci;
Sage Weil20cb34a2010-05-12 15:21:32 -07003543 struct ceph_reconnect_state *recon_state = arg;
3544 struct ceph_pagelist *pagelist = recon_state->pagelist;
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003545 int err;
Yan, Zheng3469ed02016-07-05 09:32:31 +08003546 u64 snap_follows;
Sage Weil2f2dc052009-10-06 11:31:09 -07003547
Sage Weil2f2dc052009-10-06 11:31:09 -07003548 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
3549 inode, ceph_vinop(inode), cap, cap->cap_id,
3550 ceph_cap_string(cap->issued));
Sage Weil2f2dc052009-10-06 11:31:09 -07003551
Sage Weilbe655592011-11-30 09:47:09 -08003552 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07003553 cap->seq = 0; /* reset cap seq */
3554 cap->issue_seq = 0; /* and issue_seq */
Yan, Zheng667ca052013-05-31 16:25:36 +08003555 cap->mseq = 0; /* and migrate_seq */
Yan, Zheng99a9c272013-09-22 11:08:14 +08003556 cap->cap_gen = cap->session->s_cap_gen;
Sage Weil20cb34a2010-05-12 15:21:32 -07003557
Jeff Laytona25949b2020-02-18 14:12:45 -05003558 /* These are lost when the session goes away */
Jeff Layton785892f2020-01-02 07:11:38 -05003559 if (S_ISDIR(inode->i_mode)) {
3560 if (cap->issued & CEPH_CAP_DIR_CREATE) {
3561 ceph_put_string(rcu_dereference_raw(ci->i_cached_layout.pool_ns));
3562 memset(&ci->i_cached_layout, 0, sizeof(ci->i_cached_layout));
3563 }
Jeff Laytona25949b2020-02-18 14:12:45 -05003564 cap->issued &= ~CEPH_CAP_ANY_DIR_OPS;
Jeff Layton785892f2020-01-02 07:11:38 -05003565 }
Jeff Laytona25949b2020-02-18 14:12:45 -05003566
Yan, Zheng121f22a2016-07-04 22:05:18 +08003567 if (recon_state->msg_version >= 2) {
Sage Weil20cb34a2010-05-12 15:21:32 -07003568 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
3569 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
3570 rec.v2.issued = cpu_to_le32(cap->issued);
3571 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003572 rec.v2.pathbase = 0;
Jeff Laytonec1dff22017-10-31 15:51:14 -04003573 rec.v2.flock_len = (__force __le32)
3574 ((ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) ? 0 : 1);
Sage Weil20cb34a2010-05-12 15:21:32 -07003575 } else {
3576 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
3577 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
3578 rec.v1.issued = cpu_to_le32(cap->issued);
3579 rec.v1.size = cpu_to_le64(inode->i_size);
Arnd Bergmann9bbeab42018-07-13 22:18:36 +02003580 ceph_encode_timespec64(&rec.v1.mtime, &inode->i_mtime);
3581 ceph_encode_timespec64(&rec.v1.atime, &inode->i_atime);
Sage Weil20cb34a2010-05-12 15:21:32 -07003582 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003583 rec.v1.pathbase = 0;
Sage Weil20cb34a2010-05-12 15:21:32 -07003584 }
Yan, Zheng3469ed02016-07-05 09:32:31 +08003585
3586 if (list_empty(&ci->i_cap_snaps)) {
Yan, Zheng92776fd2017-08-16 21:42:39 +08003587 snap_follows = ci->i_head_snapc ? ci->i_head_snapc->seq : 0;
Yan, Zheng3469ed02016-07-05 09:32:31 +08003588 } else {
3589 struct ceph_cap_snap *capsnap =
3590 list_first_entry(&ci->i_cap_snaps,
3591 struct ceph_cap_snap, ci_item);
3592 snap_follows = capsnap->follows;
Sage Weil93cea5b2009-12-23 12:21:51 -08003593 }
Sage Weilbe655592011-11-30 09:47:09 -08003594 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07003595
Yan, Zheng121f22a2016-07-04 22:05:18 +08003596 if (recon_state->msg_version >= 2) {
Greg Farnum40819f62010-08-02 15:34:23 -07003597 int num_fcntl_locks, num_flock_locks;
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003598 struct ceph_filelock *flocks = NULL;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003599 size_t struct_len, total_len = sizeof(u64);
Yan, Zheng121f22a2016-07-04 22:05:18 +08003600 u8 struct_v = 0;
Greg Farnum40819f62010-08-02 15:34:23 -07003601
Jim Schutt39be95e2013-05-15 13:03:35 -05003602encode_again:
Yan, Zhengb3f8d682017-09-11 10:58:55 +08003603 if (rec.v2.flock_len) {
3604 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
3605 } else {
3606 num_fcntl_locks = 0;
3607 num_flock_locks = 0;
3608 }
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003609 if (num_fcntl_locks + num_flock_locks > 0) {
Kees Cook6da2ec52018-06-12 13:55:00 -07003610 flocks = kmalloc_array(num_fcntl_locks + num_flock_locks,
3611 sizeof(struct ceph_filelock),
3612 GFP_NOFS);
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003613 if (!flocks) {
3614 err = -ENOMEM;
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003615 goto out_err;
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003616 }
3617 err = ceph_encode_locks_to_buffer(inode, flocks,
3618 num_fcntl_locks,
3619 num_flock_locks);
3620 if (err) {
3621 kfree(flocks);
3622 flocks = NULL;
3623 if (err == -ENOSPC)
3624 goto encode_again;
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003625 goto out_err;
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003626 }
3627 } else {
Jim Schutt39be95e2013-05-15 13:03:35 -05003628 kfree(flocks);
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003629 flocks = NULL;
Jim Schutt39be95e2013-05-15 13:03:35 -05003630 }
Yan, Zheng121f22a2016-07-04 22:05:18 +08003631
3632 if (recon_state->msg_version >= 3) {
3633 /* version, compat_version and struct_len */
Yan, Zheng81c5a142019-01-01 16:28:33 +08003634 total_len += 2 * sizeof(u8) + sizeof(u32);
Yan, Zheng3469ed02016-07-05 09:32:31 +08003635 struct_v = 2;
Yan, Zheng121f22a2016-07-04 22:05:18 +08003636 }
Jim Schutt39be95e2013-05-15 13:03:35 -05003637 /*
3638 * number of encoded locks is stable, so copy to pagelist
3639 */
Yan, Zheng121f22a2016-07-04 22:05:18 +08003640 struct_len = 2 * sizeof(u32) +
3641 (num_fcntl_locks + num_flock_locks) *
3642 sizeof(struct ceph_filelock);
3643 rec.v2.flock_len = cpu_to_le32(struct_len);
3644
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003645 struct_len += sizeof(u32) + sizeof(rec.v2);
Yan, Zheng121f22a2016-07-04 22:05:18 +08003646
Yan, Zheng3469ed02016-07-05 09:32:31 +08003647 if (struct_v >= 2)
3648 struct_len += sizeof(u64); /* snap_follows */
3649
Yan, Zheng121f22a2016-07-04 22:05:18 +08003650 total_len += struct_len;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003651
3652 if (pagelist->length + total_len > RECONNECT_MAX_SIZE) {
3653 err = send_reconnect_partial(recon_state);
3654 if (err)
3655 goto out_freeflocks;
3656 pagelist = recon_state->pagelist;
Yan, Zheng121f22a2016-07-04 22:05:18 +08003657 }
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003658
Yan, Zheng81c5a142019-01-01 16:28:33 +08003659 err = ceph_pagelist_reserve(pagelist, total_len);
3660 if (err)
3661 goto out_freeflocks;
3662
3663 ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003664 if (recon_state->msg_version >= 3) {
3665 ceph_pagelist_encode_8(pagelist, struct_v);
3666 ceph_pagelist_encode_8(pagelist, 1);
3667 ceph_pagelist_encode_32(pagelist, struct_len);
3668 }
3669 ceph_pagelist_encode_string(pagelist, NULL, 0);
3670 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v2));
3671 ceph_locks_to_pagelist(flocks, pagelist,
3672 num_fcntl_locks, num_flock_locks);
3673 if (struct_v >= 2)
3674 ceph_pagelist_encode_64(pagelist, snap_follows);
Yan, Zheng81c5a142019-01-01 16:28:33 +08003675out_freeflocks:
Jim Schutt39be95e2013-05-15 13:03:35 -05003676 kfree(flocks);
Sage Weil3612abb2010-09-07 15:59:27 -07003677 } else {
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003678 u64 pathbase = 0;
3679 int pathlen = 0;
3680 char *path = NULL;
3681 struct dentry *dentry;
3682
3683 dentry = d_find_alias(inode);
3684 if (dentry) {
3685 path = ceph_mdsc_build_path(dentry,
3686 &pathlen, &pathbase, 0);
3687 dput(dentry);
3688 if (IS_ERR(path)) {
3689 err = PTR_ERR(path);
3690 goto out_err;
3691 }
3692 rec.v1.pathbase = cpu_to_le64(pathbase);
Yan, Zheng121f22a2016-07-04 22:05:18 +08003693 }
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003694
3695 err = ceph_pagelist_reserve(pagelist,
Yan, Zheng81c5a142019-01-01 16:28:33 +08003696 sizeof(u64) + sizeof(u32) +
3697 pathlen + sizeof(rec.v1));
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003698 if (err) {
Yan, Zheng81c5a142019-01-01 16:28:33 +08003699 goto out_freepath;
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003700 }
3701
Yan, Zheng81c5a142019-01-01 16:28:33 +08003702 ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003703 ceph_pagelist_encode_string(pagelist, path, pathlen);
3704 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v1));
Yan, Zheng81c5a142019-01-01 16:28:33 +08003705out_freepath:
Jeff Laytonf77f21b2019-04-29 12:13:14 -04003706 ceph_mdsc_free_path(path, pathlen);
Greg Farnum40819f62010-08-02 15:34:23 -07003707 }
Yan, Zheng44c99752013-09-22 10:28:10 +08003708
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003709out_err:
Yan, Zheng81c5a142019-01-01 16:28:33 +08003710 if (err >= 0)
3711 recon_state->nr_caps++;
3712 return err;
3713}
3714
3715static int encode_snap_realms(struct ceph_mds_client *mdsc,
3716 struct ceph_reconnect_state *recon_state)
3717{
3718 struct rb_node *p;
3719 struct ceph_pagelist *pagelist = recon_state->pagelist;
3720 int err = 0;
3721
3722 if (recon_state->msg_version >= 4) {
3723 err = ceph_pagelist_encode_32(pagelist, mdsc->num_snap_realms);
3724 if (err < 0)
3725 goto fail;
3726 }
3727
3728 /*
3729 * snaprealms. we provide mds with the ino, seq (version), and
3730 * parent for all of our realms. If the mds has any newer info,
3731 * it will tell us.
3732 */
3733 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
3734 struct ceph_snap_realm *realm =
3735 rb_entry(p, struct ceph_snap_realm, node);
3736 struct ceph_mds_snaprealm_reconnect sr_rec;
3737
3738 if (recon_state->msg_version >= 4) {
3739 size_t need = sizeof(u8) * 2 + sizeof(u32) +
3740 sizeof(sr_rec);
3741
3742 if (pagelist->length + need > RECONNECT_MAX_SIZE) {
3743 err = send_reconnect_partial(recon_state);
3744 if (err)
3745 goto fail;
3746 pagelist = recon_state->pagelist;
3747 }
3748
3749 err = ceph_pagelist_reserve(pagelist, need);
3750 if (err)
3751 goto fail;
3752
3753 ceph_pagelist_encode_8(pagelist, 1);
3754 ceph_pagelist_encode_8(pagelist, 1);
3755 ceph_pagelist_encode_32(pagelist, sizeof(sr_rec));
3756 }
3757
3758 dout(" adding snap realm %llx seq %lld parent %llx\n",
3759 realm->ino, realm->seq, realm->parent_ino);
3760 sr_rec.ino = cpu_to_le64(realm->ino);
3761 sr_rec.seq = cpu_to_le64(realm->seq);
3762 sr_rec.parent = cpu_to_le64(realm->parent_ino);
3763
3764 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
3765 if (err)
3766 goto fail;
3767
3768 recon_state->nr_realms++;
3769 }
3770fail:
Sage Weil93cea5b2009-12-23 12:21:51 -08003771 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07003772}
3773
3774
3775/*
3776 * If an MDS fails and recovers, clients need to reconnect in order to
3777 * reestablish shared state. This includes all caps issued through
3778 * this session _and_ the snap_realm hierarchy. Because it's not
3779 * clear which snap realms the mds cares about, we send everything we
3780 * know about.. that ensures we'll then get any new info the
3781 * recovering MDS might have.
3782 *
3783 * This is a relatively heavyweight operation, but it's rare.
Sage Weil2f2dc052009-10-06 11:31:09 -07003784 */
Sage Weil34b6c852010-05-10 16:31:25 -07003785static void send_mds_reconnect(struct ceph_mds_client *mdsc,
3786 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07003787{
Sage Weil2f2dc052009-10-06 11:31:09 -07003788 struct ceph_msg *reply;
Sage Weil34b6c852010-05-10 16:31:25 -07003789 int mds = session->s_mds;
Sage Weil9abf82b2010-05-10 21:58:38 -07003790 int err = -ENOMEM;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003791 struct ceph_reconnect_state recon_state = {
3792 .session = session,
3793 };
Jeff Laytonc8a96a32017-10-19 08:53:58 -04003794 LIST_HEAD(dispose);
Sage Weil2f2dc052009-10-06 11:31:09 -07003795
Sage Weil34b6c852010-05-10 16:31:25 -07003796 pr_info("mds%d reconnect start\n", mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003797
Yan, Zheng81c5a142019-01-01 16:28:33 +08003798 recon_state.pagelist = ceph_pagelist_alloc(GFP_NOFS);
3799 if (!recon_state.pagelist)
Sage Weil93cea5b2009-12-23 12:21:51 -08003800 goto fail_nopagelist;
Sage Weil93cea5b2009-12-23 12:21:51 -08003801
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003802 reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07003803 if (!reply)
Sage Weil93cea5b2009-12-23 12:21:51 -08003804 goto fail_nomsg;
Sage Weil93cea5b2009-12-23 12:21:51 -08003805
Jeff Laytond4846482019-11-15 11:51:55 -05003806 xa_destroy(&session->s_delegated_inos);
3807
Sage Weil34b6c852010-05-10 16:31:25 -07003808 mutex_lock(&session->s_mutex);
3809 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
3810 session->s_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003811
Sage Weil2f2dc052009-10-06 11:31:09 -07003812 dout("session %p state %s\n", session,
John Spraya687eca2014-09-19 13:51:08 +01003813 ceph_session_state_name(session->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07003814
Yan, Zheng99a9c272013-09-22 11:08:14 +08003815 spin_lock(&session->s_gen_ttl_lock);
3816 session->s_cap_gen++;
3817 spin_unlock(&session->s_gen_ttl_lock);
3818
3819 spin_lock(&session->s_cap_lock);
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08003820 /* don't know if session is readonly */
3821 session->s_readonly = 0;
Yan, Zheng99a9c272013-09-22 11:08:14 +08003822 /*
3823 * notify __ceph_remove_cap() that we are composing cap reconnect.
3824 * If a cap get released before being added to the cap reconnect,
3825 * __ceph_remove_cap() should skip queuing cap release.
3826 */
3827 session->s_cap_reconnect = 1;
Sage Weile01a5942010-05-10 15:36:44 -07003828 /* drop old cap expires; we're about to reestablish that state */
Jeff Laytonc8a96a32017-10-19 08:53:58 -04003829 detach_cap_releases(session, &dispose);
3830 spin_unlock(&session->s_cap_lock);
3831 dispose_cap_releases(mdsc, &dispose);
Sage Weile01a5942010-05-10 15:36:44 -07003832
Yan, Zheng5d233712014-09-10 16:56:23 +08003833 /* trim unused caps to reduce MDS's cache rejoin time */
Yan, Zhengc0bd50e2015-04-07 15:51:08 +08003834 if (mdsc->fsc->sb->s_root)
3835 shrink_dcache_parent(mdsc->fsc->sb->s_root);
Yan, Zheng5d233712014-09-10 16:56:23 +08003836
3837 ceph_con_close(&session->s_con);
3838 ceph_con_open(&session->s_con,
3839 CEPH_ENTITY_TYPE_MDS, mds,
3840 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
3841
3842 /* replay unsafe requests */
3843 replay_unsafe_requests(mdsc, session);
3844
Yan, Zheng81c5a142019-01-01 16:28:33 +08003845 ceph_early_kick_flushing_caps(mdsc, session);
3846
Yan, Zheng5d233712014-09-10 16:56:23 +08003847 down_read(&mdsc->snap_rwsem);
3848
Yan, Zheng81c5a142019-01-01 16:28:33 +08003849 /* placeholder for nr_caps */
3850 err = ceph_pagelist_encode_32(recon_state.pagelist, 0);
Sage Weil93cea5b2009-12-23 12:21:51 -08003851 if (err)
3852 goto fail;
Sage Weil20cb34a2010-05-12 15:21:32 -07003853
Yan, Zheng81c5a142019-01-01 16:28:33 +08003854 if (test_bit(CEPHFS_FEATURE_MULTI_RECONNECT, &session->s_features)) {
Yan, Zheng121f22a2016-07-04 22:05:18 +08003855 recon_state.msg_version = 3;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003856 recon_state.allow_multi = true;
3857 } else if (session->s_con.peer_features & CEPH_FEATURE_MDSENC) {
3858 recon_state.msg_version = 3;
3859 } else {
Ilya Dryomov23c625c2018-11-08 14:55:21 +01003860 recon_state.msg_version = 2;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003861 }
3862 /* trsaverse this session's caps */
Jeff Laytona25949b2020-02-18 14:12:45 -05003863 err = ceph_iterate_session_caps(session, reconnect_caps_cb, &recon_state);
Sage Weil2f2dc052009-10-06 11:31:09 -07003864
Yan, Zheng99a9c272013-09-22 11:08:14 +08003865 spin_lock(&session->s_cap_lock);
3866 session->s_cap_reconnect = 0;
3867 spin_unlock(&session->s_cap_lock);
3868
Yan, Zheng81c5a142019-01-01 16:28:33 +08003869 if (err < 0)
3870 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07003871
Yan, Zheng81c5a142019-01-01 16:28:33 +08003872 /* check if all realms can be encoded into current message */
3873 if (mdsc->num_snap_realms) {
3874 size_t total_len =
3875 recon_state.pagelist->length +
3876 mdsc->num_snap_realms *
3877 sizeof(struct ceph_mds_snaprealm_reconnect);
3878 if (recon_state.msg_version >= 4) {
3879 /* number of realms */
3880 total_len += sizeof(u32);
3881 /* version, compat_version and struct_len */
3882 total_len += mdsc->num_snap_realms *
3883 (2 * sizeof(u8) + sizeof(u32));
3884 }
3885 if (total_len > RECONNECT_MAX_SIZE) {
3886 if (!recon_state.allow_multi) {
3887 err = -ENOSPC;
3888 goto fail;
3889 }
3890 if (recon_state.nr_caps) {
3891 err = send_reconnect_partial(&recon_state);
3892 if (err)
3893 goto fail;
3894 }
3895 recon_state.msg_version = 5;
3896 }
3897 }
3898
3899 err = encode_snap_realms(mdsc, &recon_state);
3900 if (err < 0)
3901 goto fail;
3902
3903 if (recon_state.msg_version >= 5) {
3904 err = ceph_pagelist_encode_8(recon_state.pagelist, 0);
3905 if (err < 0)
Sage Weil93cea5b2009-12-23 12:21:51 -08003906 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07003907 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003908
Yan, Zheng81c5a142019-01-01 16:28:33 +08003909 if (recon_state.nr_caps || recon_state.nr_realms) {
3910 struct page *page =
3911 list_first_entry(&recon_state.pagelist->head,
3912 struct page, lru);
Yan, Zheng44c99752013-09-22 10:28:10 +08003913 __le32 *addr = kmap_atomic(page);
Yan, Zheng81c5a142019-01-01 16:28:33 +08003914 if (recon_state.nr_caps) {
3915 WARN_ON(recon_state.nr_realms != mdsc->num_snap_realms);
3916 *addr = cpu_to_le32(recon_state.nr_caps);
3917 } else if (recon_state.msg_version >= 4) {
3918 *(addr + 1) = cpu_to_le32(recon_state.nr_realms);
3919 }
Yan, Zheng44c99752013-09-22 10:28:10 +08003920 kunmap_atomic(addr);
Alex Elderebf18f42013-03-04 22:29:57 -06003921 }
Yan, Zheng44c99752013-09-22 10:28:10 +08003922
Yan, Zheng81c5a142019-01-01 16:28:33 +08003923 reply->hdr.version = cpu_to_le16(recon_state.msg_version);
3924 if (recon_state.msg_version >= 4)
3925 reply->hdr.compat_version = cpu_to_le16(4);
Yan, Zhenge548e9b2015-06-10 15:17:56 +08003926
Yan, Zheng81c5a142019-01-01 16:28:33 +08003927 reply->hdr.data_len = cpu_to_le32(recon_state.pagelist->length);
3928 ceph_msg_data_add_pagelist(reply, recon_state.pagelist);
Yan, Zhenge548e9b2015-06-10 15:17:56 +08003929
Sage Weil2f2dc052009-10-06 11:31:09 -07003930 ceph_con_send(&session->s_con, reply);
3931
Sage Weil9abf82b2010-05-10 21:58:38 -07003932 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003933
Sage Weil9abf82b2010-05-10 21:58:38 -07003934 mutex_lock(&mdsc->mutex);
3935 __wake_requests(mdsc, &session->s_waiting);
3936 mutex_unlock(&mdsc->mutex);
3937
Sage Weil2f2dc052009-10-06 11:31:09 -07003938 up_read(&mdsc->snap_rwsem);
Yan, Zheng81c5a142019-01-01 16:28:33 +08003939 ceph_pagelist_release(recon_state.pagelist);
Sage Weil2f2dc052009-10-06 11:31:09 -07003940 return;
3941
Sage Weil93cea5b2009-12-23 12:21:51 -08003942fail:
Sage Weil2f2dc052009-10-06 11:31:09 -07003943 ceph_msg_put(reply);
Sage Weil9abf82b2010-05-10 21:58:38 -07003944 up_read(&mdsc->snap_rwsem);
3945 mutex_unlock(&session->s_mutex);
Sage Weil93cea5b2009-12-23 12:21:51 -08003946fail_nomsg:
Yan, Zheng81c5a142019-01-01 16:28:33 +08003947 ceph_pagelist_release(recon_state.pagelist);
Sage Weil93cea5b2009-12-23 12:21:51 -08003948fail_nopagelist:
Sage Weil9abf82b2010-05-10 21:58:38 -07003949 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
Sage Weil9abf82b2010-05-10 21:58:38 -07003950 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07003951}
3952
3953
3954/*
3955 * compare old and new mdsmaps, kicking requests
3956 * and closing out old connections as necessary
3957 *
3958 * called under mdsc->mutex.
3959 */
3960static void check_new_map(struct ceph_mds_client *mdsc,
3961 struct ceph_mdsmap *newmap,
3962 struct ceph_mdsmap *oldmap)
3963{
3964 int i;
3965 int oldstate, newstate;
3966 struct ceph_mds_session *s;
3967
3968 dout("check_new_map new %u old %u\n",
3969 newmap->m_epoch, oldmap->m_epoch);
3970
Xiubo Lib38c9eb2019-12-04 06:57:39 -05003971 for (i = 0; i < oldmap->possible_max_rank && i < mdsc->max_sessions; i++) {
Markus Elfringd37b1d92017-08-20 20:22:02 +02003972 if (!mdsc->sessions[i])
Sage Weil2f2dc052009-10-06 11:31:09 -07003973 continue;
3974 s = mdsc->sessions[i];
3975 oldstate = ceph_mdsmap_get_state(oldmap, i);
3976 newstate = ceph_mdsmap_get_state(newmap, i);
3977
Sage Weil0deb01c2010-06-17 14:19:01 -07003978 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07003979 i, ceph_mds_state_name(oldstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07003980 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
Sage Weil2f2dc052009-10-06 11:31:09 -07003981 ceph_mds_state_name(newstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07003982 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
John Spraya687eca2014-09-19 13:51:08 +01003983 ceph_session_state_name(s->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07003984
Xiubo Lib38c9eb2019-12-04 06:57:39 -05003985 if (i >= newmap->possible_max_rank) {
Yan, Zheng6f0f5972019-06-10 15:45:09 +08003986 /* force close session for stopped mds */
Xiubo Li5b3248c2019-12-19 19:44:09 -05003987 ceph_get_mds_session(s);
Yan, Zheng6f0f5972019-06-10 15:45:09 +08003988 __unregister_session(mdsc, s);
3989 __wake_requests(mdsc, &s->s_waiting);
3990 mutex_unlock(&mdsc->mutex);
3991
3992 mutex_lock(&s->s_mutex);
3993 cleanup_session_requests(mdsc, s);
3994 remove_session_caps(s);
3995 mutex_unlock(&s->s_mutex);
3996
3997 ceph_put_mds_session(s);
3998
3999 mutex_lock(&mdsc->mutex);
4000 kick_requests(mdsc, i);
4001 continue;
4002 }
4003
4004 if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
Sage Weil2f2dc052009-10-06 11:31:09 -07004005 ceph_mdsmap_get_addr(newmap, i),
4006 sizeof(struct ceph_entity_addr))) {
Yan, Zheng6f0f5972019-06-10 15:45:09 +08004007 /* just close it */
4008 mutex_unlock(&mdsc->mutex);
4009 mutex_lock(&s->s_mutex);
4010 mutex_lock(&mdsc->mutex);
4011 ceph_con_close(&s->s_con);
4012 mutex_unlock(&s->s_mutex);
4013 s->s_state = CEPH_MDS_SESSION_RESTARTING;
Sage Weil2f2dc052009-10-06 11:31:09 -07004014 } else if (oldstate == newstate) {
4015 continue; /* nothing new with this mds */
4016 }
4017
4018 /*
4019 * send reconnect?
4020 */
4021 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
Sage Weil34b6c852010-05-10 16:31:25 -07004022 newstate >= CEPH_MDS_STATE_RECONNECT) {
4023 mutex_unlock(&mdsc->mutex);
4024 send_mds_reconnect(mdsc, s);
4025 mutex_lock(&mdsc->mutex);
4026 }
Sage Weil2f2dc052009-10-06 11:31:09 -07004027
4028 /*
Sage Weil29790f22010-03-18 14:45:05 -07004029 * kick request on any mds that has gone active.
Sage Weil2f2dc052009-10-06 11:31:09 -07004030 */
4031 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
4032 newstate >= CEPH_MDS_STATE_ACTIVE) {
Sage Weil29790f22010-03-18 14:45:05 -07004033 if (oldstate != CEPH_MDS_STATE_CREATING &&
4034 oldstate != CEPH_MDS_STATE_STARTING)
4035 pr_info("mds%d recovery completed\n", s->s_mds);
4036 kick_requests(mdsc, i);
Xiubo Liea8412b2020-05-20 03:51:19 -04004037 mutex_unlock(&mdsc->mutex);
Jeff Layton829ad4d2020-04-03 13:09:07 -04004038 mutex_lock(&s->s_mutex);
Xiubo Liea8412b2020-05-20 03:51:19 -04004039 mutex_lock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07004040 ceph_kick_flushing_caps(mdsc, s);
Jeff Layton829ad4d2020-04-03 13:09:07 -04004041 mutex_unlock(&s->s_mutex);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08004042 wake_up_session_caps(s, RECONNECT);
Sage Weil2f2dc052009-10-06 11:31:09 -07004043 }
4044 }
Sage Weilcb170a22010-06-21 13:38:35 -07004045
Xiubo Lib38c9eb2019-12-04 06:57:39 -05004046 for (i = 0; i < newmap->possible_max_rank && i < mdsc->max_sessions; i++) {
Sage Weilcb170a22010-06-21 13:38:35 -07004047 s = mdsc->sessions[i];
4048 if (!s)
4049 continue;
4050 if (!ceph_mdsmap_is_laggy(newmap, i))
4051 continue;
4052 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
4053 s->s_state == CEPH_MDS_SESSION_HUNG ||
4054 s->s_state == CEPH_MDS_SESSION_CLOSING) {
4055 dout(" connecting to export targets of laggy mds%d\n",
4056 i);
4057 __open_export_target_sessions(mdsc, s);
4058 }
4059 }
Sage Weil2f2dc052009-10-06 11:31:09 -07004060}
4061
4062
4063
4064/*
4065 * leases
4066 */
4067
4068/*
4069 * caller must hold session s_mutex, dentry->d_lock
4070 */
4071void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
4072{
4073 struct ceph_dentry_info *di = ceph_dentry(dentry);
4074
4075 ceph_put_mds_session(di->lease_session);
4076 di->lease_session = NULL;
4077}
4078
Sage Weil2600d2d2010-02-22 15:12:16 -08004079static void handle_lease(struct ceph_mds_client *mdsc,
4080 struct ceph_mds_session *session,
4081 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07004082{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004083 struct super_block *sb = mdsc->fsc->sb;
Sage Weil2f2dc052009-10-06 11:31:09 -07004084 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07004085 struct dentry *parent, *dentry;
4086 struct ceph_dentry_info *di;
Sage Weil2600d2d2010-02-22 15:12:16 -08004087 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07004088 struct ceph_mds_lease *h = msg->front.iov_base;
Sage Weil1e5ea232010-06-04 10:05:40 -07004089 u32 seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07004090 struct ceph_vino vino;
Sage Weil2f2dc052009-10-06 11:31:09 -07004091 struct qstr dname;
4092 int release = 0;
4093
Sage Weil2f2dc052009-10-06 11:31:09 -07004094 dout("handle_lease from mds%d\n", mds);
4095
4096 /* decode */
4097 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
4098 goto bad;
4099 vino.ino = le64_to_cpu(h->ino);
4100 vino.snap = CEPH_NOSNAP;
Sage Weil1e5ea232010-06-04 10:05:40 -07004101 seq = le32_to_cpu(h->seq);
Yan, Zheng0fcf6c02018-08-03 16:24:49 +08004102 dname.len = get_unaligned_le32(h + 1);
4103 if (msg->front.iov_len < sizeof(*h) + sizeof(u32) + dname.len)
Sage Weil2f2dc052009-10-06 11:31:09 -07004104 goto bad;
Yan, Zheng0fcf6c02018-08-03 16:24:49 +08004105 dname.name = (void *)(h + 1) + sizeof(u32);
Sage Weil2f2dc052009-10-06 11:31:09 -07004106
Sage Weil2f2dc052009-10-06 11:31:09 -07004107 /* lookup inode */
4108 inode = ceph_find_inode(sb, vino);
Sage Weil2f90b852011-07-26 11:28:25 -07004109 dout("handle_lease %s, ino %llx %p %.*s\n",
4110 ceph_lease_op_name(h->action), vino.ino, inode,
Sage Weil1e5ea232010-06-04 10:05:40 -07004111 dname.len, dname.name);
Yan, Zheng6cd3bca2014-09-17 07:45:12 +08004112
4113 mutex_lock(&session->s_mutex);
4114 session->s_seq++;
4115
Markus Elfringd37b1d92017-08-20 20:22:02 +02004116 if (!inode) {
Sage Weil2f2dc052009-10-06 11:31:09 -07004117 dout("handle_lease no inode %llx\n", vino.ino);
4118 goto release;
4119 }
Sage Weil2f2dc052009-10-06 11:31:09 -07004120
4121 /* dentry */
4122 parent = d_find_alias(inode);
4123 if (!parent) {
4124 dout("no parent dentry on inode %p\n", inode);
4125 WARN_ON(1);
4126 goto release; /* hrm... */
4127 }
Linus Torvalds8387ff22016-06-10 07:51:30 -07004128 dname.hash = full_name_hash(parent, dname.name, dname.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07004129 dentry = d_lookup(parent, &dname);
4130 dput(parent);
4131 if (!dentry)
4132 goto release;
4133
4134 spin_lock(&dentry->d_lock);
4135 di = ceph_dentry(dentry);
4136 switch (h->action) {
4137 case CEPH_MDS_LEASE_REVOKE:
Sage Weil3d8eb7a2011-11-11 09:48:53 -08004138 if (di->lease_session == session) {
Sage Weil1e5ea232010-06-04 10:05:40 -07004139 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
4140 h->seq = cpu_to_le32(di->lease_seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07004141 __ceph_mdsc_drop_dentry_lease(dentry);
4142 }
4143 release = 1;
4144 break;
4145
4146 case CEPH_MDS_LEASE_RENEW:
Sage Weil3d8eb7a2011-11-11 09:48:53 -08004147 if (di->lease_session == session &&
Sage Weil2f2dc052009-10-06 11:31:09 -07004148 di->lease_gen == session->s_cap_gen &&
4149 di->lease_renew_from &&
4150 di->lease_renew_after == 0) {
4151 unsigned long duration =
Nicholas Mc Guire3563dbdd2015-02-06 06:52:17 -05004152 msecs_to_jiffies(le32_to_cpu(h->duration_ms));
Sage Weil2f2dc052009-10-06 11:31:09 -07004153
Sage Weil1e5ea232010-06-04 10:05:40 -07004154 di->lease_seq = seq;
Miklos Szeredi9b16f03c2016-06-22 16:35:04 +02004155 di->time = di->lease_renew_from + duration;
Sage Weil2f2dc052009-10-06 11:31:09 -07004156 di->lease_renew_after = di->lease_renew_from +
4157 (duration >> 1);
4158 di->lease_renew_from = 0;
4159 }
4160 break;
4161 }
4162 spin_unlock(&dentry->d_lock);
4163 dput(dentry);
4164
4165 if (!release)
4166 goto out;
4167
4168release:
4169 /* let's just reuse the same message */
4170 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
4171 ceph_msg_get(msg);
4172 ceph_con_send(&session->s_con, msg);
4173
4174out:
Sage Weil2f2dc052009-10-06 11:31:09 -07004175 mutex_unlock(&session->s_mutex);
Yan, Zheng3e1d0452019-05-18 20:39:55 +08004176 /* avoid calling iput_final() in mds dispatch threads */
4177 ceph_async_iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07004178 return;
4179
4180bad:
4181 pr_err("corrupt lease message\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08004182 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004183}
4184
4185void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
Sage Weil2f2dc052009-10-06 11:31:09 -07004186 struct dentry *dentry, char action,
4187 u32 seq)
4188{
4189 struct ceph_msg *msg;
4190 struct ceph_mds_lease *lease;
Yan, Zheng8f2a98ef2019-05-23 10:45:24 +08004191 struct inode *dir;
4192 int len = sizeof(*lease) + sizeof(u32) + NAME_MAX;
Sage Weil2f2dc052009-10-06 11:31:09 -07004193
Yan, Zheng8f2a98ef2019-05-23 10:45:24 +08004194 dout("lease_send_msg identry %p %s to mds%d\n",
4195 dentry, ceph_lease_op_name(action), session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07004196
Sage Weilb61c2762011-08-09 15:03:46 -07004197 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07004198 if (!msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07004199 return;
4200 lease = msg->front.iov_base;
4201 lease->action = action;
Sage Weil2f2dc052009-10-06 11:31:09 -07004202 lease->seq = cpu_to_le32(seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07004203
Yan, Zheng8f2a98ef2019-05-23 10:45:24 +08004204 spin_lock(&dentry->d_lock);
4205 dir = d_inode(dentry->d_parent);
4206 lease->ino = cpu_to_le64(ceph_ino(dir));
4207 lease->first = lease->last = cpu_to_le64(ceph_snap(dir));
4208
4209 put_unaligned_le32(dentry->d_name.len, lease + 1);
4210 memcpy((void *)(lease + 1) + 4,
4211 dentry->d_name.name, dentry->d_name.len);
4212 spin_unlock(&dentry->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07004213 /*
4214 * if this is a preemptive lease RELEASE, no need to
4215 * flush request stream, since the actual request will
4216 * soon follow.
4217 */
4218 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
4219
4220 ceph_con_send(&session->s_con, msg);
4221}
4222
4223/*
Yan, Zheng7aac4532018-03-13 19:52:20 +08004224 * lock unlock sessions, to wait ongoing session activities
Sage Weil2f2dc052009-10-06 11:31:09 -07004225 */
Yan, Zheng7aac4532018-03-13 19:52:20 +08004226static void lock_unlock_sessions(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07004227{
4228 int i;
4229
Sage Weil2f2dc052009-10-06 11:31:09 -07004230 mutex_lock(&mdsc->mutex);
4231 for (i = 0; i < mdsc->max_sessions; i++) {
4232 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
4233 if (!s)
4234 continue;
4235 mutex_unlock(&mdsc->mutex);
4236 mutex_lock(&s->s_mutex);
4237 mutex_unlock(&s->s_mutex);
4238 ceph_put_mds_session(s);
4239 mutex_lock(&mdsc->mutex);
4240 }
4241 mutex_unlock(&mdsc->mutex);
4242}
4243
Yan, Zheng131d7eb2019-07-25 20:16:47 +08004244static void maybe_recover_session(struct ceph_mds_client *mdsc)
4245{
4246 struct ceph_fs_client *fsc = mdsc->fsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07004247
Yan, Zheng131d7eb2019-07-25 20:16:47 +08004248 if (!ceph_test_mount_opt(fsc, CLEANRECOVER))
4249 return;
4250
4251 if (READ_ONCE(fsc->mount_state) != CEPH_MOUNT_MOUNTED)
4252 return;
4253
4254 if (!READ_ONCE(fsc->blacklisted))
4255 return;
4256
4257 if (fsc->last_auto_reconnect &&
4258 time_before(jiffies, fsc->last_auto_reconnect + HZ * 60 * 30))
4259 return;
4260
4261 pr_info("auto reconnect after blacklisted\n");
4262 fsc->last_auto_reconnect = jiffies;
4263 ceph_force_reconnect(fsc->sb);
4264}
Sage Weil2f2dc052009-10-06 11:31:09 -07004265
Xiubo Li3e699bd2020-06-30 03:52:15 -04004266bool check_session_state(struct ceph_mds_session *s)
4267{
4268 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
4269 dout("resending session close request for mds%d\n",
4270 s->s_mds);
4271 request_close_session(s);
4272 return false;
4273 }
4274 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
4275 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
4276 s->s_state = CEPH_MDS_SESSION_HUNG;
4277 pr_info("mds%d hung\n", s->s_mds);
4278 }
4279 }
4280 if (s->s_state == CEPH_MDS_SESSION_NEW ||
4281 s->s_state == CEPH_MDS_SESSION_RESTARTING ||
4282 s->s_state == CEPH_MDS_SESSION_REJECTED)
4283 /* this mds is failed or recovering, just wait */
4284 return false;
4285
4286 return true;
4287}
4288
Sage Weil2f2dc052009-10-06 11:31:09 -07004289/*
4290 * delayed work -- periodically trim expired leases, renew caps with mds
4291 */
4292static void schedule_delayed(struct ceph_mds_client *mdsc)
4293{
4294 int delay = 5;
4295 unsigned hz = round_jiffies_relative(HZ * delay);
4296 schedule_delayed_work(&mdsc->delayed_work, hz);
4297}
4298
4299static void delayed_work(struct work_struct *work)
4300{
4301 int i;
4302 struct ceph_mds_client *mdsc =
4303 container_of(work, struct ceph_mds_client, delayed_work.work);
4304 int renew_interval;
4305 int renew_caps;
4306
4307 dout("mdsc delayed_work\n");
Yan, Zheng75c96272017-12-14 15:11:09 +08004308
Sage Weil2f2dc052009-10-06 11:31:09 -07004309 mutex_lock(&mdsc->mutex);
4310 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
4311 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
4312 mdsc->last_renew_caps);
4313 if (renew_caps)
4314 mdsc->last_renew_caps = jiffies;
4315
4316 for (i = 0; i < mdsc->max_sessions; i++) {
4317 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
Markus Elfringd37b1d92017-08-20 20:22:02 +02004318 if (!s)
Sage Weil2f2dc052009-10-06 11:31:09 -07004319 continue;
Xiubo Li3e699bd2020-06-30 03:52:15 -04004320
4321 if (!check_session_state(s)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07004322 ceph_put_mds_session(s);
4323 continue;
4324 }
4325 mutex_unlock(&mdsc->mutex);
4326
4327 mutex_lock(&s->s_mutex);
4328 if (renew_caps)
4329 send_renew_caps(mdsc, s);
4330 else
4331 ceph_con_keepalive(&s->s_con);
Sage Weilaab53dd2010-03-17 16:30:21 -07004332 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
4333 s->s_state == CEPH_MDS_SESSION_HUNG)
Sage Weil3d7ded42010-06-09 16:47:10 -07004334 ceph_send_cap_releases(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07004335 mutex_unlock(&s->s_mutex);
4336 ceph_put_mds_session(s);
4337
4338 mutex_lock(&mdsc->mutex);
4339 }
4340 mutex_unlock(&mdsc->mutex);
4341
Yan, Zheng37c4efc2019-01-31 16:55:51 +08004342 ceph_check_delayed_caps(mdsc);
4343
4344 ceph_queue_cap_reclaim_work(mdsc);
4345
4346 ceph_trim_snapid_map(mdsc);
4347
Yan, Zheng131d7eb2019-07-25 20:16:47 +08004348 maybe_recover_session(mdsc);
4349
Sage Weil2f2dc052009-10-06 11:31:09 -07004350 schedule_delayed(mdsc);
4351}
4352
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004353int ceph_mdsc_init(struct ceph_fs_client *fsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07004354
Sage Weil2f2dc052009-10-06 11:31:09 -07004355{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004356 struct ceph_mds_client *mdsc;
Xiubo Lif9009ef2020-03-19 23:44:59 -04004357 int err;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004358
4359 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
4360 if (!mdsc)
4361 return -ENOMEM;
4362 mdsc->fsc = fsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07004363 mutex_init(&mdsc->mutex);
4364 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
Markus Elfringd37b1d92017-08-20 20:22:02 +02004365 if (!mdsc->mdsmap) {
Xiubo Lif9009ef2020-03-19 23:44:59 -04004366 err = -ENOMEM;
4367 goto err_mdsc;
majianpengfb3101b2013-06-25 14:48:19 +08004368 }
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08004369
Chengguang Xu50c55ae2018-03-14 13:47:33 +08004370 fsc->mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07004371 init_completion(&mdsc->safe_umount_waiters);
Sage Weilf3c60c52010-08-11 14:51:23 -07004372 init_waitqueue_head(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07004373 INIT_LIST_HEAD(&mdsc->waiting_for_map);
4374 mdsc->sessions = NULL;
Yan, Zheng86d8f672015-01-09 17:00:42 +08004375 atomic_set(&mdsc->num_sessions, 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07004376 mdsc->max_sessions = 0;
4377 mdsc->stopping = 0;
Luis Henriquesd557c482018-01-12 17:19:29 +00004378 atomic64_set(&mdsc->quotarealms_count, 0);
Luis Henriques0c44a8e2019-03-21 10:20:10 +00004379 mdsc->quotarealms_inodes = RB_ROOT;
4380 mutex_init(&mdsc->quotarealms_inodes_mutex);
Yan, Zhengaffbc192015-05-05 21:22:13 +08004381 mdsc->last_snap_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07004382 init_rwsem(&mdsc->snap_rwsem);
Sage Weila105f002010-02-15 14:37:55 -08004383 mdsc->snap_realms = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07004384 INIT_LIST_HEAD(&mdsc->snap_empty);
Yan, Zheng81c5a142019-01-01 16:28:33 +08004385 mdsc->num_snap_realms = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07004386 spin_lock_init(&mdsc->snap_empty_lock);
4387 mdsc->last_tid = 0;
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08004388 mdsc->oldest_tid = 0;
Sage Weil44ca18f2010-02-15 12:08:46 -08004389 mdsc->request_tree = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07004390 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
4391 mdsc->last_renew_caps = jiffies;
4392 INIT_LIST_HEAD(&mdsc->cap_delay_list);
Jeff Layton3a3430a2019-11-20 12:00:59 -05004393 INIT_LIST_HEAD(&mdsc->cap_wait_list);
Sage Weil2f2dc052009-10-06 11:31:09 -07004394 spin_lock_init(&mdsc->cap_delay_lock);
4395 INIT_LIST_HEAD(&mdsc->snap_flush_list);
4396 spin_lock_init(&mdsc->snap_flush_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08004397 mdsc->last_cap_flush_tid = 1;
Yan, Zhenge4500b52016-07-06 11:12:56 +08004398 INIT_LIST_HEAD(&mdsc->cap_flush_list);
Sage Weildb354052011-05-24 11:46:31 -07004399 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
Sage Weil2f2dc052009-10-06 11:31:09 -07004400 mdsc->num_cap_flushing = 0;
4401 spin_lock_init(&mdsc->cap_dirty_lock);
4402 init_waitqueue_head(&mdsc->cap_flushing_wq);
Yan, Zheng37c4efc2019-01-31 16:55:51 +08004403 INIT_WORK(&mdsc->cap_reclaim_work, ceph_cap_reclaim_work);
Yan, Zhengfe330322019-02-01 14:57:15 +08004404 atomic_set(&mdsc->cap_reclaim_pending, 0);
Xiubo Lif9009ef2020-03-19 23:44:59 -04004405 err = ceph_metric_init(&mdsc->metric);
4406 if (err)
4407 goto err_mdsmap;
Yan, Zheng37c4efc2019-01-31 16:55:51 +08004408
4409 spin_lock_init(&mdsc->dentry_list_lock);
4410 INIT_LIST_HEAD(&mdsc->dentry_leases);
4411 INIT_LIST_HEAD(&mdsc->dentry_dir_leases);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08004412
Yehuda Sadeh37151662010-06-17 16:16:12 -07004413 ceph_caps_init(mdsc);
Yan, Zhengfe330322019-02-01 14:57:15 +08004414 ceph_adjust_caps_max_min(mdsc, fsc->mount_options);
Yehuda Sadeh37151662010-06-17 16:16:12 -07004415
Yan, Zheng75c96272017-12-14 15:11:09 +08004416 spin_lock_init(&mdsc->snapid_map_lock);
4417 mdsc->snapid_map_tree = RB_ROOT;
4418 INIT_LIST_HEAD(&mdsc->snapid_map_lru);
4419
Yan, Zheng10183a62015-04-27 15:33:28 +08004420 init_rwsem(&mdsc->pool_perm_rwsem);
4421 mdsc->pool_perm_tree = RB_ROOT;
4422
Yan, Zhengdfeb84d2018-07-02 15:55:23 +08004423 strscpy(mdsc->nodename, utsname()->nodename,
4424 sizeof(mdsc->nodename));
Sage Weil5f44f142009-11-18 14:52:18 -08004425 return 0;
Xiubo Lif9009ef2020-03-19 23:44:59 -04004426
4427err_mdsmap:
4428 kfree(mdsc->mdsmap);
4429err_mdsc:
4430 kfree(mdsc);
4431 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07004432}
4433
4434/*
4435 * Wait for safe replies on open mds requests. If we time out, drop
4436 * all requests from the tree to avoid dangling dentry refs.
4437 */
4438static void wait_requests(struct ceph_mds_client *mdsc)
4439{
Ilya Dryomova319bf52015-05-15 12:02:17 +03004440 struct ceph_options *opts = mdsc->fsc->client->options;
Sage Weil2f2dc052009-10-06 11:31:09 -07004441 struct ceph_mds_request *req;
Sage Weil2f2dc052009-10-06 11:31:09 -07004442
4443 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08004444 if (__get_oldest_req(mdsc)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07004445 mutex_unlock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08004446
Sage Weil2f2dc052009-10-06 11:31:09 -07004447 dout("wait_requests waiting for requests\n");
4448 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
Ilya Dryomova319bf52015-05-15 12:02:17 +03004449 ceph_timeout_jiffies(opts->mount_timeout));
Sage Weil2f2dc052009-10-06 11:31:09 -07004450
4451 /* tear down remaining requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08004452 mutex_lock(&mdsc->mutex);
4453 while ((req = __get_oldest_req(mdsc))) {
Sage Weil2f2dc052009-10-06 11:31:09 -07004454 dout("wait_requests timed out on tid %llu\n",
4455 req->r_tid);
Yan, Zheng428138c2019-06-14 10:55:05 +08004456 list_del_init(&req->r_wait);
Sage Weil44ca18f2010-02-15 12:08:46 -08004457 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07004458 }
4459 }
4460 mutex_unlock(&mdsc->mutex);
4461 dout("wait_requests done\n");
4462}
4463
4464/*
4465 * called before mount is ro, and before dentries are torn down.
4466 * (hmm, does this still race with new lookups?)
4467 */
4468void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
4469{
4470 dout("pre_umount\n");
4471 mdsc->stopping = 1;
4472
Yan, Zheng7aac4532018-03-13 19:52:20 +08004473 lock_unlock_sessions(mdsc);
Sage Weilafcdaea2009-10-14 14:27:38 -07004474 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07004475 wait_requests(mdsc);
Sage Weil17c688c2010-06-21 16:12:26 -07004476
4477 /*
4478 * wait for reply handlers to drop their request refs and
4479 * their inode/dcache refs
4480 */
4481 ceph_msgr_flush();
Luis Henriques0c44a8e2019-03-21 10:20:10 +00004482
4483 ceph_cleanup_quotarealms_inodes(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07004484}
4485
4486/*
4487 * wait for all write mds requests to flush.
4488 */
4489static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
4490{
Sage Weil80fc7312010-03-16 15:28:54 -07004491 struct ceph_mds_request *req = NULL, *nextreq;
Sage Weil44ca18f2010-02-15 12:08:46 -08004492 struct rb_node *n;
Sage Weil2f2dc052009-10-06 11:31:09 -07004493
4494 mutex_lock(&mdsc->mutex);
4495 dout("wait_unsafe_requests want %lld\n", want_tid);
Sage Weil80fc7312010-03-16 15:28:54 -07004496restart:
Sage Weil44ca18f2010-02-15 12:08:46 -08004497 req = __get_oldest_req(mdsc);
4498 while (req && req->r_tid <= want_tid) {
Sage Weil80fc7312010-03-16 15:28:54 -07004499 /* find next request */
4500 n = rb_next(&req->r_node);
4501 if (n)
4502 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
4503 else
4504 nextreq = NULL;
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08004505 if (req->r_op != CEPH_MDS_OP_SETFILELOCK &&
4506 (req->r_op & CEPH_MDS_OP_WRITE)) {
Sage Weil44ca18f2010-02-15 12:08:46 -08004507 /* write op */
4508 ceph_mdsc_get_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07004509 if (nextreq)
4510 ceph_mdsc_get_request(nextreq);
Sage Weil44ca18f2010-02-15 12:08:46 -08004511 mutex_unlock(&mdsc->mutex);
4512 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
4513 req->r_tid, want_tid);
4514 wait_for_completion(&req->r_safe_completion);
4515 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08004516 ceph_mdsc_put_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07004517 if (!nextreq)
4518 break; /* next dne before, so we're done! */
4519 if (RB_EMPTY_NODE(&nextreq->r_node)) {
4520 /* next request was removed from tree */
4521 ceph_mdsc_put_request(nextreq);
4522 goto restart;
4523 }
4524 ceph_mdsc_put_request(nextreq); /* won't go away */
Sage Weil44ca18f2010-02-15 12:08:46 -08004525 }
Sage Weil80fc7312010-03-16 15:28:54 -07004526 req = nextreq;
Sage Weil2f2dc052009-10-06 11:31:09 -07004527 }
4528 mutex_unlock(&mdsc->mutex);
4529 dout("wait_unsafe_requests done\n");
4530}
4531
4532void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
4533{
Yan, Zheng0e294382016-07-04 18:06:41 +08004534 u64 want_tid, want_flush;
Sage Weil2f2dc052009-10-06 11:31:09 -07004535
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01004536 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
Sage Weil56b7cf92010-05-03 15:22:00 -07004537 return;
4538
Sage Weil2f2dc052009-10-06 11:31:09 -07004539 dout("sync\n");
4540 mutex_lock(&mdsc->mutex);
4541 want_tid = mdsc->last_tid;
Sage Weil2f2dc052009-10-06 11:31:09 -07004542 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07004543
Sage Weilafcdaea2009-10-14 14:27:38 -07004544 ceph_flush_dirty_caps(mdsc);
Yan, Zhengd3383a82015-01-08 21:30:12 +08004545 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zheng8310b082015-06-09 17:20:12 +08004546 want_flush = mdsc->last_cap_flush_tid;
Yan, Zhengc8799fc2016-07-07 15:22:38 +08004547 if (!list_empty(&mdsc->cap_flush_list)) {
4548 struct ceph_cap_flush *cf =
4549 list_last_entry(&mdsc->cap_flush_list,
4550 struct ceph_cap_flush, g_list);
4551 cf->wake = true;
4552 }
Yan, Zhengd3383a82015-01-08 21:30:12 +08004553 spin_unlock(&mdsc->cap_dirty_lock);
4554
Yan, Zheng0e294382016-07-04 18:06:41 +08004555 dout("sync want tid %lld flush_seq %lld\n",
4556 want_tid, want_flush);
Sage Weil2f2dc052009-10-06 11:31:09 -07004557
4558 wait_unsafe_requests(mdsc, want_tid);
Yan, Zheng0e294382016-07-04 18:06:41 +08004559 wait_caps_flush(mdsc, want_flush);
Sage Weil2f2dc052009-10-06 11:31:09 -07004560}
4561
Sage Weilf3c60c52010-08-11 14:51:23 -07004562/*
4563 * true if all sessions are closed, or we force unmount
4564 */
Yan, Zhengfcff4152016-09-14 16:39:51 +08004565static bool done_closing_sessions(struct ceph_mds_client *mdsc, int skipped)
Sage Weilf3c60c52010-08-11 14:51:23 -07004566{
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01004567 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
Sage Weilf3c60c52010-08-11 14:51:23 -07004568 return true;
Yan, Zhengfcff4152016-09-14 16:39:51 +08004569 return atomic_read(&mdsc->num_sessions) <= skipped;
Sage Weilf3c60c52010-08-11 14:51:23 -07004570}
Sage Weil2f2dc052009-10-06 11:31:09 -07004571
4572/*
4573 * called after sb is ro.
4574 */
4575void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
4576{
Ilya Dryomova319bf52015-05-15 12:02:17 +03004577 struct ceph_options *opts = mdsc->fsc->client->options;
Sage Weil2f2dc052009-10-06 11:31:09 -07004578 struct ceph_mds_session *session;
4579 int i;
Yan, Zhengfcff4152016-09-14 16:39:51 +08004580 int skipped = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07004581
4582 dout("close_sessions\n");
4583
Sage Weil2f2dc052009-10-06 11:31:09 -07004584 /* close sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07004585 mutex_lock(&mdsc->mutex);
4586 for (i = 0; i < mdsc->max_sessions; i++) {
4587 session = __ceph_lookup_mds_session(mdsc, i);
4588 if (!session)
4589 continue;
Sage Weil2f2dc052009-10-06 11:31:09 -07004590 mutex_unlock(&mdsc->mutex);
Sage Weilf3c60c52010-08-11 14:51:23 -07004591 mutex_lock(&session->s_mutex);
Yan, Zhengfcff4152016-09-14 16:39:51 +08004592 if (__close_session(mdsc, session) <= 0)
4593 skipped++;
Sage Weilf3c60c52010-08-11 14:51:23 -07004594 mutex_unlock(&session->s_mutex);
4595 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07004596 mutex_lock(&mdsc->mutex);
4597 }
Sage Weilf3c60c52010-08-11 14:51:23 -07004598 mutex_unlock(&mdsc->mutex);
4599
4600 dout("waiting for sessions to close\n");
Yan, Zhengfcff4152016-09-14 16:39:51 +08004601 wait_event_timeout(mdsc->session_close_wq,
4602 done_closing_sessions(mdsc, skipped),
Ilya Dryomova319bf52015-05-15 12:02:17 +03004603 ceph_timeout_jiffies(opts->mount_timeout));
Sage Weil2f2dc052009-10-06 11:31:09 -07004604
4605 /* tear down remaining sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07004606 mutex_lock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07004607 for (i = 0; i < mdsc->max_sessions; i++) {
4608 if (mdsc->sessions[i]) {
Xiubo Li5b3248c2019-12-19 19:44:09 -05004609 session = ceph_get_mds_session(mdsc->sessions[i]);
Sage Weil2600d2d2010-02-22 15:12:16 -08004610 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07004611 mutex_unlock(&mdsc->mutex);
4612 mutex_lock(&session->s_mutex);
4613 remove_session_caps(session);
4614 mutex_unlock(&session->s_mutex);
4615 ceph_put_mds_session(session);
4616 mutex_lock(&mdsc->mutex);
4617 }
4618 }
Sage Weil2f2dc052009-10-06 11:31:09 -07004619 WARN_ON(!list_empty(&mdsc->cap_delay_list));
Sage Weil2f2dc052009-10-06 11:31:09 -07004620 mutex_unlock(&mdsc->mutex);
4621
Yan, Zheng75c96272017-12-14 15:11:09 +08004622 ceph_cleanup_snapid_map(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07004623 ceph_cleanup_empty_realms(mdsc);
4624
Yan, Zheng37c4efc2019-01-31 16:55:51 +08004625 cancel_work_sync(&mdsc->cap_reclaim_work);
Sage Weil2f2dc052009-10-06 11:31:09 -07004626 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
4627
4628 dout("stopped\n");
4629}
4630
Yan, Zheng48fec5d2015-07-01 16:27:46 +08004631void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc)
4632{
4633 struct ceph_mds_session *session;
4634 int mds;
4635
4636 dout("force umount\n");
4637
4638 mutex_lock(&mdsc->mutex);
4639 for (mds = 0; mds < mdsc->max_sessions; mds++) {
4640 session = __ceph_lookup_mds_session(mdsc, mds);
4641 if (!session)
4642 continue;
Yan, Zhengd468e722019-07-25 20:16:44 +08004643
4644 if (session->s_state == CEPH_MDS_SESSION_REJECTED)
4645 __unregister_session(mdsc, session);
4646 __wake_requests(mdsc, &session->s_waiting);
Yan, Zheng48fec5d2015-07-01 16:27:46 +08004647 mutex_unlock(&mdsc->mutex);
Yan, Zhengd468e722019-07-25 20:16:44 +08004648
Yan, Zheng48fec5d2015-07-01 16:27:46 +08004649 mutex_lock(&session->s_mutex);
4650 __close_session(mdsc, session);
4651 if (session->s_state == CEPH_MDS_SESSION_CLOSING) {
4652 cleanup_session_requests(mdsc, session);
4653 remove_session_caps(session);
4654 }
4655 mutex_unlock(&session->s_mutex);
4656 ceph_put_mds_session(session);
Yan, Zhengd468e722019-07-25 20:16:44 +08004657
Yan, Zheng48fec5d2015-07-01 16:27:46 +08004658 mutex_lock(&mdsc->mutex);
4659 kick_requests(mdsc, mds);
4660 }
4661 __wake_requests(mdsc, &mdsc->waiting_for_map);
4662 mutex_unlock(&mdsc->mutex);
4663}
4664
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004665static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07004666{
4667 dout("stop\n");
4668 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
4669 if (mdsc->mdsmap)
4670 ceph_mdsmap_destroy(mdsc->mdsmap);
4671 kfree(mdsc->sessions);
Yehuda Sadeh37151662010-06-17 16:16:12 -07004672 ceph_caps_finalize(mdsc);
Yan, Zheng10183a62015-04-27 15:33:28 +08004673 ceph_pool_perm_destroy(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07004674}
4675
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004676void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
4677{
4678 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weilef550f62011-03-25 13:27:48 -07004679 dout("mdsc_destroy %p\n", mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07004680
Chengguang Xu50c55ae2018-03-14 13:47:33 +08004681 if (!mdsc)
4682 return;
4683
Sage Weilef550f62011-03-25 13:27:48 -07004684 /* flush out any connection work with references to us */
4685 ceph_msgr_flush();
4686
Yan, Zheng62a65f32017-06-22 16:26:34 +08004687 ceph_mdsc_stop(mdsc);
4688
Xiubo Lif9009ef2020-03-19 23:44:59 -04004689 ceph_metric_destroy(&mdsc->metric);
4690
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004691 fsc->mdsc = NULL;
4692 kfree(mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07004693 dout("mdsc_destroy %p done\n", mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004694}
4695
Yan, Zheng430afba2016-07-08 11:25:38 +08004696void ceph_mdsc_handle_fsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
4697{
4698 struct ceph_fs_client *fsc = mdsc->fsc;
4699 const char *mds_namespace = fsc->mount_options->mds_namespace;
4700 void *p = msg->front.iov_base;
4701 void *end = p + msg->front.iov_len;
4702 u32 epoch;
4703 u32 map_len;
4704 u32 num_fs;
4705 u32 mount_fscid = (u32)-1;
4706 u8 struct_v, struct_cv;
4707 int err = -EINVAL;
4708
4709 ceph_decode_need(&p, end, sizeof(u32), bad);
4710 epoch = ceph_decode_32(&p);
4711
4712 dout("handle_fsmap epoch %u\n", epoch);
4713
4714 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
4715 struct_v = ceph_decode_8(&p);
4716 struct_cv = ceph_decode_8(&p);
4717 map_len = ceph_decode_32(&p);
4718
4719 ceph_decode_need(&p, end, sizeof(u32) * 3, bad);
4720 p += sizeof(u32) * 2; /* skip epoch and legacy_client_fscid */
4721
4722 num_fs = ceph_decode_32(&p);
4723 while (num_fs-- > 0) {
4724 void *info_p, *info_end;
4725 u32 info_len;
4726 u8 info_v, info_cv;
4727 u32 fscid, namelen;
4728
4729 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
4730 info_v = ceph_decode_8(&p);
4731 info_cv = ceph_decode_8(&p);
4732 info_len = ceph_decode_32(&p);
4733 ceph_decode_need(&p, end, info_len, bad);
4734 info_p = p;
4735 info_end = p + info_len;
4736 p = info_end;
4737
4738 ceph_decode_need(&info_p, info_end, sizeof(u32) * 2, bad);
4739 fscid = ceph_decode_32(&info_p);
4740 namelen = ceph_decode_32(&info_p);
4741 ceph_decode_need(&info_p, info_end, namelen, bad);
4742
4743 if (mds_namespace &&
4744 strlen(mds_namespace) == namelen &&
4745 !strncmp(mds_namespace, (char *)info_p, namelen)) {
4746 mount_fscid = fscid;
4747 break;
4748 }
4749 }
4750
4751 ceph_monc_got_map(&fsc->client->monc, CEPH_SUB_FSMAP, epoch);
4752 if (mount_fscid != (u32)-1) {
4753 fsc->client->monc.fs_cluster_id = mount_fscid;
4754 ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP,
4755 0, true);
4756 ceph_monc_renew_subs(&fsc->client->monc);
4757 } else {
4758 err = -ENOENT;
4759 goto err_out;
4760 }
4761 return;
Ilya Dryomov76bd6ec2017-10-16 10:32:50 +02004762
Yan, Zheng430afba2016-07-08 11:25:38 +08004763bad:
4764 pr_err("error decoding fsmap\n");
4765err_out:
4766 mutex_lock(&mdsc->mutex);
Ilya Dryomov76bd6ec2017-10-16 10:32:50 +02004767 mdsc->mdsmap_err = err;
Yan, Zheng430afba2016-07-08 11:25:38 +08004768 __wake_requests(mdsc, &mdsc->waiting_for_map);
4769 mutex_unlock(&mdsc->mutex);
Yan, Zheng430afba2016-07-08 11:25:38 +08004770}
Sage Weil2f2dc052009-10-06 11:31:09 -07004771
4772/*
4773 * handle mds map update.
4774 */
Yan, Zheng430afba2016-07-08 11:25:38 +08004775void ceph_mdsc_handle_mdsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07004776{
4777 u32 epoch;
4778 u32 maplen;
4779 void *p = msg->front.iov_base;
4780 void *end = p + msg->front.iov_len;
4781 struct ceph_mdsmap *newmap, *oldmap;
4782 struct ceph_fsid fsid;
4783 int err = -EINVAL;
4784
4785 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
4786 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004787 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
Sage Weil07433042009-11-18 16:50:41 -08004788 return;
Sage Weilc89136e2009-10-14 09:59:09 -07004789 epoch = ceph_decode_32(&p);
4790 maplen = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07004791 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
4792
4793 /* do we need it? */
Sage Weil2f2dc052009-10-06 11:31:09 -07004794 mutex_lock(&mdsc->mutex);
4795 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
4796 dout("handle_map epoch %u <= our %u\n",
4797 epoch, mdsc->mdsmap->m_epoch);
4798 mutex_unlock(&mdsc->mutex);
4799 return;
4800 }
4801
4802 newmap = ceph_mdsmap_decode(&p, end);
4803 if (IS_ERR(newmap)) {
4804 err = PTR_ERR(newmap);
4805 goto bad_unlock;
4806 }
4807
4808 /* swap into place */
4809 if (mdsc->mdsmap) {
4810 oldmap = mdsc->mdsmap;
4811 mdsc->mdsmap = newmap;
4812 check_new_map(mdsc, newmap, oldmap);
4813 ceph_mdsmap_destroy(oldmap);
4814 } else {
4815 mdsc->mdsmap = newmap; /* first mds map */
4816 }
Chengguang Xu719784b2018-07-19 22:15:24 +08004817 mdsc->fsc->max_file_size = min((loff_t)mdsc->mdsmap->m_max_file_size,
4818 MAX_LFS_FILESIZE);
Sage Weil2f2dc052009-10-06 11:31:09 -07004819
4820 __wake_requests(mdsc, &mdsc->waiting_for_map);
Ilya Dryomov82dcaba2016-01-19 16:19:06 +01004821 ceph_monc_got_map(&mdsc->fsc->client->monc, CEPH_SUB_MDSMAP,
4822 mdsc->mdsmap->m_epoch);
Sage Weil2f2dc052009-10-06 11:31:09 -07004823
4824 mutex_unlock(&mdsc->mutex);
4825 schedule_delayed(mdsc);
4826 return;
4827
4828bad_unlock:
4829 mutex_unlock(&mdsc->mutex);
4830bad:
4831 pr_err("error decoding mdsmap %d\n", err);
4832 return;
4833}
4834
4835static struct ceph_connection *con_get(struct ceph_connection *con)
4836{
4837 struct ceph_mds_session *s = con->private;
4838
Xiubo Li5b3248c2019-12-19 19:44:09 -05004839 if (ceph_get_mds_session(s))
Sage Weil2f2dc052009-10-06 11:31:09 -07004840 return con;
Sage Weil2f2dc052009-10-06 11:31:09 -07004841 return NULL;
4842}
4843
4844static void con_put(struct ceph_connection *con)
4845{
4846 struct ceph_mds_session *s = con->private;
4847
Sage Weil2f2dc052009-10-06 11:31:09 -07004848 ceph_put_mds_session(s);
4849}
4850
4851/*
4852 * if the client is unresponsive for long enough, the mds will kill
4853 * the session entirely.
4854 */
4855static void peer_reset(struct ceph_connection *con)
4856{
4857 struct ceph_mds_session *s = con->private;
Sage Weil7e70f0e2010-03-18 13:59:12 -07004858 struct ceph_mds_client *mdsc = s->s_mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07004859
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07004860 pr_warn("mds%d closed our session\n", s->s_mds);
Sage Weil7e70f0e2010-03-18 13:59:12 -07004861 send_mds_reconnect(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07004862}
4863
4864static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
4865{
4866 struct ceph_mds_session *s = con->private;
4867 struct ceph_mds_client *mdsc = s->s_mdsc;
4868 int type = le16_to_cpu(msg->hdr.type);
4869
Sage Weil2600d2d2010-02-22 15:12:16 -08004870 mutex_lock(&mdsc->mutex);
4871 if (__verify_registered_session(mdsc, s) < 0) {
4872 mutex_unlock(&mdsc->mutex);
4873 goto out;
4874 }
4875 mutex_unlock(&mdsc->mutex);
4876
Sage Weil2f2dc052009-10-06 11:31:09 -07004877 switch (type) {
4878 case CEPH_MSG_MDS_MAP:
Yan, Zheng430afba2016-07-08 11:25:38 +08004879 ceph_mdsc_handle_mdsmap(mdsc, msg);
4880 break;
4881 case CEPH_MSG_FS_MAP_USER:
4882 ceph_mdsc_handle_fsmap(mdsc, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004883 break;
4884 case CEPH_MSG_CLIENT_SESSION:
4885 handle_session(s, msg);
4886 break;
4887 case CEPH_MSG_CLIENT_REPLY:
4888 handle_reply(s, msg);
4889 break;
4890 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
Sage Weil2600d2d2010-02-22 15:12:16 -08004891 handle_forward(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004892 break;
4893 case CEPH_MSG_CLIENT_CAPS:
4894 ceph_handle_caps(s, msg);
4895 break;
4896 case CEPH_MSG_CLIENT_SNAP:
Sage Weil2600d2d2010-02-22 15:12:16 -08004897 ceph_handle_snap(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004898 break;
4899 case CEPH_MSG_CLIENT_LEASE:
Sage Weil2600d2d2010-02-22 15:12:16 -08004900 handle_lease(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004901 break;
Luis Henriquesfb18a572018-01-05 10:47:18 +00004902 case CEPH_MSG_CLIENT_QUOTA:
4903 ceph_handle_quota(mdsc, s, msg);
4904 break;
Sage Weil2f2dc052009-10-06 11:31:09 -07004905
4906 default:
4907 pr_err("received unknown message type %d %s\n", type,
4908 ceph_msg_type_name(type));
4909 }
Sage Weil2600d2d2010-02-22 15:12:16 -08004910out:
Sage Weil2f2dc052009-10-06 11:31:09 -07004911 ceph_msg_put(msg);
4912}
4913
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004914/*
4915 * authentication
4916 */
Alex Eldera3530df2012-05-16 15:16:39 -05004917
4918/*
4919 * Note: returned pointer is the address of a structure that's
4920 * managed separately. Caller must *not* attempt to free it.
4921 */
4922static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05004923 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004924{
4925 struct ceph_mds_session *s = con->private;
4926 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004927 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05004928 struct ceph_auth_handshake *auth = &s->s_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004929
Alex Elder74f18692012-05-16 15:16:39 -05004930 if (force_new && auth->authorizer) {
Ilya Dryomov6c1ea262016-04-11 19:34:49 +02004931 ceph_auth_destroy_authorizer(auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05004932 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004933 }
Sage Weil27859f92013-03-25 10:26:14 -07004934 if (!auth->authorizer) {
4935 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4936 auth);
Sage Weil0bed9b52013-03-25 10:26:01 -07004937 if (ret)
4938 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07004939 } else {
4940 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4941 auth);
Alex Eldera2556512012-05-16 15:16:39 -05004942 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05004943 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004944 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004945 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05004946
Alex Eldera3530df2012-05-16 15:16:39 -05004947 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004948}
4949
Ilya Dryomov6daca132018-07-27 19:18:34 +02004950static int add_authorizer_challenge(struct ceph_connection *con,
4951 void *challenge_buf, int challenge_buf_len)
4952{
4953 struct ceph_mds_session *s = con->private;
4954 struct ceph_mds_client *mdsc = s->s_mdsc;
4955 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
4956
4957 return ceph_auth_add_authorizer_challenge(ac, s->s_auth.authorizer,
4958 challenge_buf, challenge_buf_len);
4959}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004960
Ilya Dryomov0dde5842016-12-02 16:35:09 +01004961static int verify_authorizer_reply(struct ceph_connection *con)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004962{
4963 struct ceph_mds_session *s = con->private;
4964 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004965 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004966
Ilya Dryomov0dde5842016-12-02 16:35:09 +01004967 return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004968}
4969
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004970static int invalidate_authorizer(struct ceph_connection *con)
4971{
4972 struct ceph_mds_session *s = con->private;
4973 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004974 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004975
Sage Weil27859f92013-03-25 10:26:14 -07004976 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004977
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004978 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004979}
4980
Alex Elder53ded492013-03-01 18:00:14 -06004981static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
4982 struct ceph_msg_header *hdr, int *skip)
4983{
4984 struct ceph_msg *msg;
4985 int type = (int) le16_to_cpu(hdr->type);
4986 int front_len = (int) le32_to_cpu(hdr->front_len);
4987
4988 if (con->in_msg)
4989 return con->in_msg;
4990
4991 *skip = 0;
4992 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
4993 if (!msg) {
4994 pr_err("unable to allocate msg type %d len %d\n",
4995 type, front_len);
4996 return NULL;
4997 }
Alex Elder53ded492013-03-01 18:00:14 -06004998
4999 return msg;
5000}
5001
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005002static int mds_sign_message(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08005003{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005004 struct ceph_mds_session *s = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08005005 struct ceph_auth_handshake *auth = &s->s_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005006
Yan, Zheng33d07332014-11-04 16:33:37 +08005007 return ceph_auth_sign_message(auth, msg);
5008}
5009
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005010static int mds_check_message_signature(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08005011{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005012 struct ceph_mds_session *s = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08005013 struct ceph_auth_handshake *auth = &s->s_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005014
Yan, Zheng33d07332014-11-04 16:33:37 +08005015 return ceph_auth_check_message_signature(auth, msg);
5016}
5017
Tobias Klauser9e327892010-05-20 10:40:19 +02005018static const struct ceph_connection_operations mds_con_ops = {
Sage Weil2f2dc052009-10-06 11:31:09 -07005019 .get = con_get,
5020 .put = con_put,
5021 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005022 .get_authorizer = get_authorizer,
Ilya Dryomov6daca132018-07-27 19:18:34 +02005023 .add_authorizer_challenge = add_authorizer_challenge,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005024 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08005025 .invalidate_authorizer = invalidate_authorizer,
Sage Weil2f2dc052009-10-06 11:31:09 -07005026 .peer_reset = peer_reset,
Alex Elder53ded492013-03-01 18:00:14 -06005027 .alloc_msg = mds_alloc_msg,
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005028 .sign_message = mds_sign_message,
5029 .check_message_signature = mds_check_message_signature,
Sage Weil2f2dc052009-10-06 11:31:09 -07005030};
5031
Sage Weil2f2dc052009-10-06 11:31:09 -07005032/* eof */