blob: 1095802ad9bd7ad87a9793fe64ed7257f0b2e574 [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) {
Yanhu Cao224c7b62020-07-31 16:25:13 +08001106 if (!ceph_mdsmap_is_laggy(mdsc->mdsmap,
Xiubo Li5d476482019-11-26 07:24:22 -05001107 mds))
1108 goto out;
1109 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001110 }
Xiubo Li5d476482019-11-26 07:24:22 -05001111 mode = USE_AUTH_MDS;
Sage Weil2f2dc052009-10-06 11:31:09 -07001112 }
1113 }
1114
Sage Weilbe655592011-11-30 09:47:09 -08001115 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001116 cap = NULL;
1117 if (mode == USE_AUTH_MDS)
1118 cap = ci->i_auth_cap;
1119 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
1120 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
1121 if (!cap) {
Sage Weilbe655592011-11-30 09:47:09 -08001122 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng3e1d0452019-05-18 20:39:55 +08001123 ceph_async_iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07001124 goto random;
1125 }
1126 mds = cap->session->s_mds;
Xiubo Li3c802092020-01-01 22:09:37 -05001127 dout("%s %p %llx.%llx mds%d (%scap %p)\n", __func__,
Sage Weil2f2dc052009-10-06 11:31:09 -07001128 inode, ceph_vinop(inode), mds,
1129 cap == ci->i_auth_cap ? "auth " : "", cap);
Sage Weilbe655592011-11-30 09:47:09 -08001130 spin_unlock(&ci->i_ceph_lock);
Jeff Layton30c71232016-12-15 08:37:56 -05001131out:
Yan, Zheng3e1d0452019-05-18 20:39:55 +08001132 /* avoid calling iput_final() while holding mdsc->mutex or
1133 * in mds dispatch threads */
1134 ceph_async_iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07001135 return mds;
1136
1137random:
Xiubo Lic4853e92019-12-09 07:47:15 -05001138 if (random)
1139 *random = true;
1140
Sage Weil2f2dc052009-10-06 11:31:09 -07001141 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
Xiubo Li3c802092020-01-01 22:09:37 -05001142 dout("%s chose random mds%d\n", __func__, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07001143 return mds;
1144}
1145
1146
1147/*
1148 * session messages
1149 */
1150static struct ceph_msg *create_session_msg(u32 op, u64 seq)
1151{
1152 struct ceph_msg *msg;
1153 struct ceph_mds_session_head *h;
1154
Sage Weilb61c2762011-08-09 15:03:46 -07001155 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
1156 false);
Sage Weila79832f2010-04-01 16:06:19 -07001157 if (!msg) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001158 pr_err("create_session_msg ENOMEM creating msg\n");
Sage Weila79832f2010-04-01 16:06:19 -07001159 return NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001160 }
1161 h = msg->front.iov_base;
1162 h->op = cpu_to_le32(op);
1163 h->seq = cpu_to_le64(seq);
John Spraydbd0c8b2014-09-09 19:26:01 +01001164
1165 return msg;
1166}
1167
Xiubo Li9ba1e222020-01-08 05:17:31 -05001168static const unsigned char feature_bits[] = CEPHFS_FEATURES_CLIENT_SUPPORTED;
1169#define FEATURE_BYTES(c) (DIV_ROUND_UP((size_t)feature_bits[c - 1] + 1, 64) * 8)
Xiubo Lib682c6d2020-06-30 03:52:18 -04001170static int encode_supported_features(void **p, void *end)
Yan, Zheng342ce182018-05-11 18:47:29 +08001171{
Xiubo Li9ba1e222020-01-08 05:17:31 -05001172 static const size_t count = ARRAY_SIZE(feature_bits);
Yan, Zheng342ce182018-05-11 18:47:29 +08001173
1174 if (count > 0) {
1175 size_t i;
Xiubo Li9ba1e222020-01-08 05:17:31 -05001176 size_t size = FEATURE_BYTES(count);
Yan, Zheng342ce182018-05-11 18:47:29 +08001177
Xiubo Lib682c6d2020-06-30 03:52:18 -04001178 if (WARN_ON_ONCE(*p + 4 + size > end))
1179 return -ERANGE;
1180
Yan, Zheng342ce182018-05-11 18:47:29 +08001181 ceph_encode_32(p, size);
1182 memset(*p, 0, size);
1183 for (i = 0; i < count; i++)
Xiubo Li9ba1e222020-01-08 05:17:31 -05001184 ((unsigned char*)(*p))[i / 8] |= BIT(feature_bits[i] % 8);
Yan, Zheng342ce182018-05-11 18:47:29 +08001185 *p += size;
1186 } else {
Xiubo Lib682c6d2020-06-30 03:52:18 -04001187 if (WARN_ON_ONCE(*p + 4 > end))
1188 return -ERANGE;
1189
Yan, Zheng342ce182018-05-11 18:47:29 +08001190 ceph_encode_32(p, 0);
1191 }
Xiubo Lib682c6d2020-06-30 03:52:18 -04001192
1193 return 0;
Yan, Zheng342ce182018-05-11 18:47:29 +08001194}
1195
Xiubo Li3b4168d2020-07-16 10:05:58 -04001196static const unsigned char metric_bits[] = CEPHFS_METRIC_SPEC_CLIENT_SUPPORTED;
1197#define METRIC_BYTES(cnt) (DIV_ROUND_UP((size_t)metric_bits[cnt - 1] + 1, 64) * 8)
1198static int encode_metric_spec(void **p, void *end)
1199{
1200 static const size_t count = ARRAY_SIZE(metric_bits);
1201
1202 /* header */
1203 if (WARN_ON_ONCE(*p + 2 > end))
1204 return -ERANGE;
1205
1206 ceph_encode_8(p, 1); /* version */
1207 ceph_encode_8(p, 1); /* compat */
1208
1209 if (count > 0) {
1210 size_t i;
1211 size_t size = METRIC_BYTES(count);
1212
1213 if (WARN_ON_ONCE(*p + 4 + 4 + size > end))
1214 return -ERANGE;
1215
1216 /* metric spec info length */
1217 ceph_encode_32(p, 4 + size);
1218
1219 /* metric spec */
1220 ceph_encode_32(p, size);
1221 memset(*p, 0, size);
1222 for (i = 0; i < count; i++)
1223 ((unsigned char *)(*p))[i / 8] |= BIT(metric_bits[i] % 8);
1224 *p += size;
1225 } else {
1226 if (WARN_ON_ONCE(*p + 4 + 4 > end))
1227 return -ERANGE;
1228
1229 /* metric spec info length */
1230 ceph_encode_32(p, 4);
1231 /* metric spec */
1232 ceph_encode_32(p, 0);
1233 }
1234
1235 return 0;
1236}
1237
John Spraydbd0c8b2014-09-09 19:26:01 +01001238/*
1239 * session message, specialization for CEPH_SESSION_REQUEST_OPEN
1240 * to include additional client metadata fields.
1241 */
1242static struct ceph_msg *create_session_open_msg(struct ceph_mds_client *mdsc, u64 seq)
1243{
1244 struct ceph_msg *msg;
1245 struct ceph_mds_session_head *h;
1246 int i = -1;
Yan, Zheng342ce182018-05-11 18:47:29 +08001247 int extra_bytes = 0;
John Spraydbd0c8b2014-09-09 19:26:01 +01001248 int metadata_key_count = 0;
1249 struct ceph_options *opt = mdsc->fsc->client->options;
Yan, Zheng3f384952016-04-21 11:09:55 +08001250 struct ceph_mount_options *fsopt = mdsc->fsc->mount_options;
Xiubo Li9ba1e222020-01-08 05:17:31 -05001251 size_t size, count;
Yan, Zheng342ce182018-05-11 18:47:29 +08001252 void *p, *end;
Xiubo Lib682c6d2020-06-30 03:52:18 -04001253 int ret;
John Spraydbd0c8b2014-09-09 19:26:01 +01001254
Yan, Zhenga6a5ce42015-01-16 10:54:43 +08001255 const char* metadata[][2] = {
Yan, Zheng717e6f22017-09-11 12:10:08 +08001256 {"hostname", mdsc->nodename},
1257 {"kernel_version", init_utsname()->release},
Yan, Zheng3f384952016-04-21 11:09:55 +08001258 {"entity_id", opt->name ? : ""},
1259 {"root", fsopt->server_path ? : "/"},
John Spraydbd0c8b2014-09-09 19:26:01 +01001260 {NULL, NULL}
1261 };
1262
1263 /* Calculate serialized length of metadata */
Yan, Zheng342ce182018-05-11 18:47:29 +08001264 extra_bytes = 4; /* map length */
Markus Elfringd37b1d92017-08-20 20:22:02 +02001265 for (i = 0; metadata[i][0]; ++i) {
Yan, Zheng342ce182018-05-11 18:47:29 +08001266 extra_bytes += 8 + strlen(metadata[i][0]) +
John Spraydbd0c8b2014-09-09 19:26:01 +01001267 strlen(metadata[i][1]);
1268 metadata_key_count++;
1269 }
Xiubo Li9ba1e222020-01-08 05:17:31 -05001270
Yan, Zheng342ce182018-05-11 18:47:29 +08001271 /* supported feature */
Xiubo Li9ba1e222020-01-08 05:17:31 -05001272 size = 0;
1273 count = ARRAY_SIZE(feature_bits);
1274 if (count > 0)
1275 size = FEATURE_BYTES(count);
1276 extra_bytes += 4 + size;
John Spraydbd0c8b2014-09-09 19:26:01 +01001277
Xiubo Li3b4168d2020-07-16 10:05:58 -04001278 /* metric spec */
1279 size = 0;
1280 count = ARRAY_SIZE(metric_bits);
1281 if (count > 0)
1282 size = METRIC_BYTES(count);
1283 extra_bytes += 2 + 4 + 4 + size;
1284
John Spraydbd0c8b2014-09-09 19:26:01 +01001285 /* Allocate the message */
Yan, Zheng342ce182018-05-11 18:47:29 +08001286 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + extra_bytes,
John Spraydbd0c8b2014-09-09 19:26:01 +01001287 GFP_NOFS, false);
1288 if (!msg) {
1289 pr_err("create_session_msg ENOMEM creating msg\n");
Xiubo Lib682c6d2020-06-30 03:52:18 -04001290 return ERR_PTR(-ENOMEM);
John Spraydbd0c8b2014-09-09 19:26:01 +01001291 }
Yan, Zheng342ce182018-05-11 18:47:29 +08001292 p = msg->front.iov_base;
1293 end = p + msg->front.iov_len;
1294
1295 h = p;
John Spraydbd0c8b2014-09-09 19:26:01 +01001296 h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN);
1297 h->seq = cpu_to_le64(seq);
1298
1299 /*
1300 * Serialize client metadata into waiting buffer space, using
1301 * the format that userspace expects for map<string, string>
John Spray7cfa0312014-10-30 17:15:26 +00001302 *
Xiubo Li3b4168d2020-07-16 10:05:58 -04001303 * ClientSession messages with metadata are v4
John Spraydbd0c8b2014-09-09 19:26:01 +01001304 */
Xiubo Li3b4168d2020-07-16 10:05:58 -04001305 msg->hdr.version = cpu_to_le16(4);
John Spray7cfa0312014-10-30 17:15:26 +00001306 msg->hdr.compat_version = cpu_to_le16(1);
John Spraydbd0c8b2014-09-09 19:26:01 +01001307
1308 /* The write pointer, following the session_head structure */
Yan, Zheng342ce182018-05-11 18:47:29 +08001309 p += sizeof(*h);
John Spraydbd0c8b2014-09-09 19:26:01 +01001310
1311 /* Number of entries in the map */
1312 ceph_encode_32(&p, metadata_key_count);
1313
1314 /* Two length-prefixed strings for each entry in the map */
Markus Elfringd37b1d92017-08-20 20:22:02 +02001315 for (i = 0; metadata[i][0]; ++i) {
John Spraydbd0c8b2014-09-09 19:26:01 +01001316 size_t const key_len = strlen(metadata[i][0]);
1317 size_t const val_len = strlen(metadata[i][1]);
1318
1319 ceph_encode_32(&p, key_len);
1320 memcpy(p, metadata[i][0], key_len);
1321 p += key_len;
1322 ceph_encode_32(&p, val_len);
1323 memcpy(p, metadata[i][1], val_len);
1324 p += val_len;
1325 }
1326
Xiubo Lib682c6d2020-06-30 03:52:18 -04001327 ret = encode_supported_features(&p, end);
1328 if (ret) {
1329 pr_err("encode_supported_features failed!\n");
1330 ceph_msg_put(msg);
1331 return ERR_PTR(ret);
1332 }
1333
Xiubo Li3b4168d2020-07-16 10:05:58 -04001334 ret = encode_metric_spec(&p, end);
1335 if (ret) {
1336 pr_err("encode_metric_spec failed!\n");
1337 ceph_msg_put(msg);
1338 return ERR_PTR(ret);
1339 }
1340
Yan, Zheng342ce182018-05-11 18:47:29 +08001341 msg->front.iov_len = p - msg->front.iov_base;
1342 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1343
Sage Weil2f2dc052009-10-06 11:31:09 -07001344 return msg;
1345}
1346
1347/*
1348 * send session open request.
1349 *
1350 * called under mdsc->mutex
1351 */
1352static int __open_session(struct ceph_mds_client *mdsc,
1353 struct ceph_mds_session *session)
1354{
1355 struct ceph_msg *msg;
1356 int mstate;
1357 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07001358
1359 /* wait for mds to go active? */
1360 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
1361 dout("open_session to mds%d (%s)\n", mds,
1362 ceph_mds_state_name(mstate));
1363 session->s_state = CEPH_MDS_SESSION_OPENING;
1364 session->s_renew_requested = jiffies;
1365
1366 /* send connect message */
John Spraydbd0c8b2014-09-09 19:26:01 +01001367 msg = create_session_open_msg(mdsc, session->s_seq);
Xiubo Lib682c6d2020-06-30 03:52:18 -04001368 if (IS_ERR(msg))
1369 return PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001370 ceph_con_send(&session->s_con, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001371 return 0;
1372}
1373
1374/*
Sage Weiled0552a2010-06-21 13:38:25 -07001375 * open sessions for any export targets for the given mds
1376 *
1377 * called under mdsc->mutex
1378 */
Yan, Zheng5d72d132013-11-24 14:33:01 +08001379static struct ceph_mds_session *
1380__open_export_target_session(struct ceph_mds_client *mdsc, int target)
1381{
1382 struct ceph_mds_session *session;
Xiubo Lib682c6d2020-06-30 03:52:18 -04001383 int ret;
Yan, Zheng5d72d132013-11-24 14:33:01 +08001384
1385 session = __ceph_lookup_mds_session(mdsc, target);
1386 if (!session) {
1387 session = register_session(mdsc, target);
1388 if (IS_ERR(session))
1389 return session;
1390 }
1391 if (session->s_state == CEPH_MDS_SESSION_NEW ||
Xiubo Lib682c6d2020-06-30 03:52:18 -04001392 session->s_state == CEPH_MDS_SESSION_CLOSING) {
1393 ret = __open_session(mdsc, session);
1394 if (ret)
1395 return ERR_PTR(ret);
1396 }
Yan, Zheng5d72d132013-11-24 14:33:01 +08001397
1398 return session;
1399}
1400
1401struct ceph_mds_session *
1402ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target)
1403{
1404 struct ceph_mds_session *session;
1405
1406 dout("open_export_target_session to mds%d\n", target);
1407
1408 mutex_lock(&mdsc->mutex);
1409 session = __open_export_target_session(mdsc, target);
1410 mutex_unlock(&mdsc->mutex);
1411
1412 return session;
1413}
1414
Sage Weiled0552a2010-06-21 13:38:25 -07001415static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
1416 struct ceph_mds_session *session)
1417{
1418 struct ceph_mds_info *mi;
1419 struct ceph_mds_session *ts;
1420 int i, mds = session->s_mds;
Sage Weiled0552a2010-06-21 13:38:25 -07001421
Xiubo Lib38c9eb2019-12-04 06:57:39 -05001422 if (mds >= mdsc->mdsmap->possible_max_rank)
Sage Weiled0552a2010-06-21 13:38:25 -07001423 return;
Yan, Zheng5d72d132013-11-24 14:33:01 +08001424
Sage Weiled0552a2010-06-21 13:38:25 -07001425 mi = &mdsc->mdsmap->m_info[mds];
1426 dout("open_export_target_sessions for mds%d (%d targets)\n",
1427 session->s_mds, mi->num_export_targets);
1428
1429 for (i = 0; i < mi->num_export_targets; i++) {
Yan, Zheng5d72d132013-11-24 14:33:01 +08001430 ts = __open_export_target_session(mdsc, mi->export_targets[i]);
1431 if (!IS_ERR(ts))
1432 ceph_put_mds_session(ts);
Sage Weiled0552a2010-06-21 13:38:25 -07001433 }
1434}
1435
Sage Weil154f42c2010-06-21 13:45:04 -07001436void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
1437 struct ceph_mds_session *session)
1438{
1439 mutex_lock(&mdsc->mutex);
1440 __open_export_target_sessions(mdsc, session);
1441 mutex_unlock(&mdsc->mutex);
1442}
1443
Sage Weiled0552a2010-06-21 13:38:25 -07001444/*
Sage Weil2f2dc052009-10-06 11:31:09 -07001445 * session caps
1446 */
1447
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001448static void detach_cap_releases(struct ceph_mds_session *session,
1449 struct list_head *target)
Sage Weil2f2dc052009-10-06 11:31:09 -07001450{
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001451 lockdep_assert_held(&session->s_cap_lock);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001452
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001453 list_splice_init(&session->s_cap_releases, target);
1454 session->s_num_cap_releases = 0;
1455 dout("dispose_cap_releases mds%d\n", session->s_mds);
1456}
1457
1458static void dispose_cap_releases(struct ceph_mds_client *mdsc,
1459 struct list_head *dispose)
1460{
1461 while (!list_empty(dispose)) {
Yan, Zheng745a8e32015-05-14 17:22:42 +08001462 struct ceph_cap *cap;
1463 /* zero out the in-progress message */
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001464 cap = list_first_entry(dispose, struct ceph_cap, session_caps);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001465 list_del(&cap->session_caps);
1466 ceph_put_cap(mdsc, cap);
1467 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001468}
1469
Yan, Zheng1c841a92015-03-24 20:15:36 +08001470static void cleanup_session_requests(struct ceph_mds_client *mdsc,
1471 struct ceph_mds_session *session)
1472{
1473 struct ceph_mds_request *req;
1474 struct rb_node *p;
Yan, Zhengf4b97862019-07-25 20:16:42 +08001475 struct ceph_inode_info *ci;
Yan, Zheng1c841a92015-03-24 20:15:36 +08001476
1477 dout("cleanup_session_requests mds%d\n", session->s_mds);
1478 mutex_lock(&mdsc->mutex);
1479 while (!list_empty(&session->s_unsafe)) {
1480 req = list_first_entry(&session->s_unsafe,
1481 struct ceph_mds_request, r_unsafe_item);
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001482 pr_warn_ratelimited(" dropping unsafe request %llu\n",
1483 req->r_tid);
Yan, Zhengf4b97862019-07-25 20:16:42 +08001484 if (req->r_target_inode) {
1485 /* dropping unsafe change of inode's attributes */
1486 ci = ceph_inode(req->r_target_inode);
1487 errseq_set(&ci->i_meta_err, -EIO);
1488 }
1489 if (req->r_unsafe_dir) {
1490 /* dropping unsafe directory operation */
1491 ci = ceph_inode(req->r_unsafe_dir);
1492 errseq_set(&ci->i_meta_err, -EIO);
1493 }
Yan, Zheng1c841a92015-03-24 20:15:36 +08001494 __unregister_request(mdsc, req);
1495 }
1496 /* zero r_attempts, so kick_requests() will re-send requests */
1497 p = rb_first(&mdsc->request_tree);
1498 while (p) {
1499 req = rb_entry(p, struct ceph_mds_request, r_node);
1500 p = rb_next(p);
1501 if (req->r_session &&
1502 req->r_session->s_mds == session->s_mds)
1503 req->r_attempts = 0;
1504 }
1505 mutex_unlock(&mdsc->mutex);
1506}
1507
Sage Weil2f2dc052009-10-06 11:31:09 -07001508/*
Sage Weilf818a732010-05-11 20:56:31 -07001509 * Helper to safely iterate over all caps associated with a session, with
1510 * special care taken to handle a racing __ceph_remove_cap().
Sage Weil2f2dc052009-10-06 11:31:09 -07001511 *
Sage Weilf818a732010-05-11 20:56:31 -07001512 * Caller must hold session s_mutex.
Sage Weil2f2dc052009-10-06 11:31:09 -07001513 */
Jeff Laytonf5d772692019-04-24 12:09:04 -04001514int ceph_iterate_session_caps(struct ceph_mds_session *session,
1515 int (*cb)(struct inode *, struct ceph_cap *,
1516 void *), void *arg)
Sage Weil2f2dc052009-10-06 11:31:09 -07001517{
Sage Weil7c1332b2010-02-16 11:39:45 -08001518 struct list_head *p;
1519 struct ceph_cap *cap;
1520 struct inode *inode, *last_inode = NULL;
1521 struct ceph_cap *old_cap = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001522 int ret;
1523
1524 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
1525 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001526 p = session->s_caps.next;
1527 while (p != &session->s_caps) {
1528 cap = list_entry(p, struct ceph_cap, session_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -07001529 inode = igrab(&cap->ci->vfs_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001530 if (!inode) {
1531 p = p->next;
Sage Weil2f2dc052009-10-06 11:31:09 -07001532 continue;
Sage Weil7c1332b2010-02-16 11:39:45 -08001533 }
1534 session->s_cap_iterator = cap;
Sage Weil2f2dc052009-10-06 11:31:09 -07001535 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001536
1537 if (last_inode) {
Yan, Zheng3e1d0452019-05-18 20:39:55 +08001538 /* avoid calling iput_final() while holding
1539 * s_mutex or in mds dispatch threads */
1540 ceph_async_iput(last_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001541 last_inode = NULL;
1542 }
1543 if (old_cap) {
Yehuda Sadeh37151662010-06-17 16:16:12 -07001544 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -08001545 old_cap = NULL;
1546 }
1547
Sage Weil2f2dc052009-10-06 11:31:09 -07001548 ret = cb(inode, cap, arg);
Sage Weil7c1332b2010-02-16 11:39:45 -08001549 last_inode = inode;
1550
Sage Weil2f2dc052009-10-06 11:31:09 -07001551 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001552 p = p->next;
Markus Elfringd37b1d92017-08-20 20:22:02 +02001553 if (!cap->ci) {
Sage Weil7c1332b2010-02-16 11:39:45 -08001554 dout("iterate_session_caps finishing cap %p removal\n",
1555 cap);
1556 BUG_ON(cap->session != session);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001557 cap->session = NULL;
Sage Weil7c1332b2010-02-16 11:39:45 -08001558 list_del_init(&cap->session_caps);
1559 session->s_nr_caps--;
Xiubo Li4f1d7562020-06-30 03:52:16 -04001560 atomic64_dec(&session->s_mdsc->metric.total_caps);
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08001561 if (cap->queue_release)
1562 __ceph_queue_cap_release(session, cap);
1563 else
Yan, Zheng745a8e32015-05-14 17:22:42 +08001564 old_cap = cap; /* put_cap it w/o locks held */
Sage Weil7c1332b2010-02-16 11:39:45 -08001565 }
Sage Weil5dacf092009-12-21 20:40:34 -08001566 if (ret < 0)
1567 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07001568 }
Sage Weil5dacf092009-12-21 20:40:34 -08001569 ret = 0;
1570out:
Sage Weil7c1332b2010-02-16 11:39:45 -08001571 session->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001572 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001573
Yan, Zheng3e1d0452019-05-18 20:39:55 +08001574 ceph_async_iput(last_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001575 if (old_cap)
Yehuda Sadeh37151662010-06-17 16:16:12 -07001576 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -08001577
Sage Weil5dacf092009-12-21 20:40:34 -08001578 return ret;
Sage Weil2f2dc052009-10-06 11:31:09 -07001579}
1580
1581static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
Sage Weil6c99f252010-05-10 16:12:25 -07001582 void *arg)
Sage Weil2f2dc052009-10-06 11:31:09 -07001583{
Yan, Zheng6c93df52016-04-15 13:56:12 +08001584 struct ceph_fs_client *fsc = (struct ceph_fs_client *)arg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001585 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001586 LIST_HEAD(to_remove);
Yan, Zhengf4b97862019-07-25 20:16:42 +08001587 bool dirty_dropped = false;
Yan, Zheng6c93df52016-04-15 13:56:12 +08001588 bool invalidate = false;
Sage Weil6c99f252010-05-10 16:12:25 -07001589
Sage Weil2f2dc052009-10-06 11:31:09 -07001590 dout("removing cap %p, ci is %p, inode is %p\n",
1591 cap, ci, &ci->vfs_inode);
Sage Weilbe655592011-11-30 09:47:09 -08001592 spin_lock(&ci->i_ceph_lock);
Yan, Zhenga096b092013-09-22 10:15:58 +08001593 __ceph_remove_cap(cap, false);
Yan, Zheng571ade32015-03-24 11:36:08 +08001594 if (!ci->i_auth_cap) {
Yan, Zheng553adfd2015-06-09 15:48:57 +08001595 struct ceph_cap_flush *cf;
Yan, Zheng6c93df52016-04-15 13:56:12 +08001596 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil6c99f252010-05-10 16:12:25 -07001597
Yan, Zhengd468e722019-07-25 20:16:44 +08001598 if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
1599 if (inode->i_data.nrpages > 0)
1600 invalidate = true;
1601 if (ci->i_wrbuffer_ref > 0)
1602 mapping_set_error(&inode->i_data, -EIO);
1603 }
Yan, Zheng6c93df52016-04-15 13:56:12 +08001604
Yan, Zhenge4500b52016-07-06 11:12:56 +08001605 while (!list_empty(&ci->i_cap_flush_list)) {
1606 cf = list_first_entry(&ci->i_cap_flush_list,
1607 struct ceph_cap_flush, i_list);
Wei Yongjun8cdcc072016-08-13 09:10:28 +00001608 list_move(&cf->i_list, &to_remove);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001609 }
1610
Sage Weil6c99f252010-05-10 16:12:25 -07001611 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zheng8310b082015-06-09 17:20:12 +08001612
Yan, Zhenge4500b52016-07-06 11:12:56 +08001613 list_for_each_entry(cf, &to_remove, i_list)
1614 list_del(&cf->g_list);
Yan, Zheng8310b082015-06-09 17:20:12 +08001615
Sage Weil6c99f252010-05-10 16:12:25 -07001616 if (!list_empty(&ci->i_dirty_item)) {
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001617 pr_warn_ratelimited(
1618 " dropping dirty %s state for %p %lld\n",
Sage Weil6c99f252010-05-10 16:12:25 -07001619 ceph_cap_string(ci->i_dirty_caps),
1620 inode, ceph_ino(inode));
1621 ci->i_dirty_caps = 0;
1622 list_del_init(&ci->i_dirty_item);
Yan, Zhengf4b97862019-07-25 20:16:42 +08001623 dirty_dropped = true;
Sage Weil6c99f252010-05-10 16:12:25 -07001624 }
1625 if (!list_empty(&ci->i_flushing_item)) {
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001626 pr_warn_ratelimited(
1627 " dropping dirty+flushing %s state for %p %lld\n",
Sage Weil6c99f252010-05-10 16:12:25 -07001628 ceph_cap_string(ci->i_flushing_caps),
1629 inode, ceph_ino(inode));
1630 ci->i_flushing_caps = 0;
1631 list_del_init(&ci->i_flushing_item);
1632 mdsc->num_cap_flushing--;
Yan, Zhengf4b97862019-07-25 20:16:42 +08001633 dirty_dropped = true;
Sage Weil6c99f252010-05-10 16:12:25 -07001634 }
Sage Weil6c99f252010-05-10 16:12:25 -07001635 spin_unlock(&mdsc->cap_dirty_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001636
Yan, Zhengf4b97862019-07-25 20:16:42 +08001637 if (dirty_dropped) {
1638 errseq_set(&ci->i_meta_err, -EIO);
1639
1640 if (ci->i_wrbuffer_ref_head == 0 &&
1641 ci->i_wr_ref == 0 &&
1642 ci->i_dirty_caps == 0 &&
1643 ci->i_flushing_caps == 0) {
1644 ceph_put_snap_context(ci->i_head_snapc);
1645 ci->i_head_snapc = NULL;
1646 }
1647 }
1648
Yan, Zhengb3f8d682017-09-11 10:58:55 +08001649 if (atomic_read(&ci->i_filelock_ref) > 0) {
1650 /* make further file lock syscall return -EIO */
1651 ci->i_ceph_flags |= CEPH_I_ERROR_FILELOCK;
1652 pr_warn_ratelimited(" dropping file locks for %p %lld\n",
1653 inode, ceph_ino(inode));
1654 }
1655
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001656 if (!ci->i_dirty_caps && ci->i_prealloc_cap_flush) {
Yan, Zhenge4500b52016-07-06 11:12:56 +08001657 list_add(&ci->i_prealloc_cap_flush->i_list, &to_remove);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001658 ci->i_prealloc_cap_flush = NULL;
1659 }
Sage Weil6c99f252010-05-10 16:12:25 -07001660 }
Sage Weilbe655592011-11-30 09:47:09 -08001661 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001662 while (!list_empty(&to_remove)) {
1663 struct ceph_cap_flush *cf;
1664 cf = list_first_entry(&to_remove,
Yan, Zhenge4500b52016-07-06 11:12:56 +08001665 struct ceph_cap_flush, i_list);
1666 list_del(&cf->i_list);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001667 ceph_free_cap_flush(cf);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001668 }
Yan, Zheng77310322016-04-08 15:27:16 +08001669
1670 wake_up_all(&ci->i_cap_wq);
Yan, Zheng6c93df52016-04-15 13:56:12 +08001671 if (invalidate)
1672 ceph_queue_invalidate(inode);
Yan, Zhengf4b97862019-07-25 20:16:42 +08001673 if (dirty_dropped)
Sage Weil6c99f252010-05-10 16:12:25 -07001674 iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07001675 return 0;
1676}
1677
1678/*
1679 * caller must hold session s_mutex
1680 */
1681static void remove_session_caps(struct ceph_mds_session *session)
1682{
Yan, Zheng6c93df52016-04-15 13:56:12 +08001683 struct ceph_fs_client *fsc = session->s_mdsc->fsc;
1684 struct super_block *sb = fsc->sb;
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001685 LIST_HEAD(dispose);
1686
Sage Weil2f2dc052009-10-06 11:31:09 -07001687 dout("remove_session_caps on %p\n", session);
Jeff Laytonf5d772692019-04-24 12:09:04 -04001688 ceph_iterate_session_caps(session, remove_session_caps_cb, fsc);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001689
Yan, Zhengc8799fc2016-07-07 15:22:38 +08001690 wake_up_all(&fsc->mdsc->cap_flushing_wq);
1691
Yan, Zheng6f60f882013-07-24 12:22:11 +08001692 spin_lock(&session->s_cap_lock);
1693 if (session->s_nr_caps > 0) {
Yan, Zheng6f60f882013-07-24 12:22:11 +08001694 struct inode *inode;
1695 struct ceph_cap *cap, *prev = NULL;
1696 struct ceph_vino vino;
1697 /*
1698 * iterate_session_caps() skips inodes that are being
1699 * deleted, we need to wait until deletions are complete.
1700 * __wait_on_freeing_inode() is designed for the job,
1701 * but it is not exported, so use lookup inode function
1702 * to access it.
1703 */
1704 while (!list_empty(&session->s_caps)) {
1705 cap = list_entry(session->s_caps.next,
1706 struct ceph_cap, session_caps);
1707 if (cap == prev)
1708 break;
1709 prev = cap;
1710 vino = cap->ci->i_vino;
1711 spin_unlock(&session->s_cap_lock);
1712
Yan, Zhenged284c42013-09-02 15:19:53 +08001713 inode = ceph_find_inode(sb, vino);
Yan, Zheng3e1d0452019-05-18 20:39:55 +08001714 /* avoid calling iput_final() while holding s_mutex */
1715 ceph_async_iput(inode);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001716
1717 spin_lock(&session->s_cap_lock);
1718 }
1719 }
Yan, Zheng745a8e32015-05-14 17:22:42 +08001720
1721 // drop cap expires and unlock s_cap_lock
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001722 detach_cap_releases(session, &dispose);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001723
Sage Weil2f2dc052009-10-06 11:31:09 -07001724 BUG_ON(session->s_nr_caps > 0);
Sage Weil6c99f252010-05-10 16:12:25 -07001725 BUG_ON(!list_empty(&session->s_cap_flushing));
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001726 spin_unlock(&session->s_cap_lock);
1727 dispose_cap_releases(session->s_mdsc, &dispose);
Sage Weil2f2dc052009-10-06 11:31:09 -07001728}
1729
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001730enum {
1731 RECONNECT,
1732 RENEWCAPS,
1733 FORCE_RO,
1734};
1735
Sage Weil2f2dc052009-10-06 11:31:09 -07001736/*
1737 * wake up any threads waiting on this session's caps. if the cap is
1738 * old (didn't get renewed on the client reconnect), remove it now.
1739 *
1740 * caller must hold s_mutex.
1741 */
1742static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1743 void *arg)
1744{
Sage Weil0dc25702009-11-20 13:43:45 -08001745 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001746 unsigned long ev = (unsigned long)arg;
Sage Weil0dc25702009-11-20 13:43:45 -08001747
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001748 if (ev == RECONNECT) {
Sage Weilbe655592011-11-30 09:47:09 -08001749 spin_lock(&ci->i_ceph_lock);
Sage Weil0dc25702009-11-20 13:43:45 -08001750 ci->i_wanted_max_size = 0;
1751 ci->i_requested_max_size = 0;
Sage Weilbe655592011-11-30 09:47:09 -08001752 spin_unlock(&ci->i_ceph_lock);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001753 } else if (ev == RENEWCAPS) {
1754 if (cap->cap_gen < cap->session->s_cap_gen) {
1755 /* mds did not re-issue stale cap */
1756 spin_lock(&ci->i_ceph_lock);
1757 cap->issued = cap->implemented = CEPH_CAP_PIN;
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001758 spin_unlock(&ci->i_ceph_lock);
1759 }
1760 } else if (ev == FORCE_RO) {
Sage Weil0dc25702009-11-20 13:43:45 -08001761 }
Yan, Zhenge5360302016-05-19 19:15:19 +08001762 wake_up_all(&ci->i_cap_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07001763 return 0;
1764}
1765
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001766static void wake_up_session_caps(struct ceph_mds_session *session, int ev)
Sage Weil2f2dc052009-10-06 11:31:09 -07001767{
1768 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
Jeff Laytonf5d772692019-04-24 12:09:04 -04001769 ceph_iterate_session_caps(session, wake_up_session_cb,
1770 (void *)(unsigned long)ev);
Sage Weil2f2dc052009-10-06 11:31:09 -07001771}
1772
1773/*
1774 * Send periodic message to MDS renewing all currently held caps. The
1775 * ack will reset the expiration for all caps from this session.
1776 *
1777 * caller holds s_mutex
1778 */
1779static int send_renew_caps(struct ceph_mds_client *mdsc,
1780 struct ceph_mds_session *session)
1781{
1782 struct ceph_msg *msg;
1783 int state;
1784
1785 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1786 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1787 pr_info("mds%d caps stale\n", session->s_mds);
Sage Weile4cb4cb2010-03-18 13:43:09 -07001788 session->s_renew_requested = jiffies;
Sage Weil2f2dc052009-10-06 11:31:09 -07001789
1790 /* do not try to renew caps until a recovering mds has reconnected
1791 * with its clients. */
1792 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1793 if (state < CEPH_MDS_STATE_RECONNECT) {
1794 dout("send_renew_caps ignoring mds%d (%s)\n",
1795 session->s_mds, ceph_mds_state_name(state));
1796 return 0;
1797 }
1798
1799 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1800 ceph_mds_state_name(state));
Sage Weil2f2dc052009-10-06 11:31:09 -07001801 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1802 ++session->s_renew_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001803 if (!msg)
1804 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -07001805 ceph_con_send(&session->s_con, msg);
1806 return 0;
1807}
1808
Yan, Zheng186e4f72013-11-22 14:48:37 +08001809static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
1810 struct ceph_mds_session *session, u64 seq)
1811{
1812 struct ceph_msg *msg;
1813
1814 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
John Spraya687eca2014-09-19 13:51:08 +01001815 session->s_mds, ceph_session_state_name(session->s_state), seq);
Yan, Zheng186e4f72013-11-22 14:48:37 +08001816 msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
1817 if (!msg)
1818 return -ENOMEM;
1819 ceph_con_send(&session->s_con, msg);
1820 return 0;
1821}
1822
1823
Sage Weil2f2dc052009-10-06 11:31:09 -07001824/*
1825 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
Sage Weil0dc25702009-11-20 13:43:45 -08001826 *
1827 * Called under session->s_mutex
Sage Weil2f2dc052009-10-06 11:31:09 -07001828 */
1829static void renewed_caps(struct ceph_mds_client *mdsc,
1830 struct ceph_mds_session *session, int is_renew)
1831{
1832 int was_stale;
1833 int wake = 0;
1834
1835 spin_lock(&session->s_cap_lock);
Alex Elder1ce208a2012-01-12 17:48:11 -08001836 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
Sage Weil2f2dc052009-10-06 11:31:09 -07001837
1838 session->s_cap_ttl = session->s_renew_requested +
1839 mdsc->mdsmap->m_session_timeout*HZ;
1840
1841 if (was_stale) {
1842 if (time_before(jiffies, session->s_cap_ttl)) {
1843 pr_info("mds%d caps renewed\n", session->s_mds);
1844 wake = 1;
1845 } else {
1846 pr_info("mds%d caps still stale\n", session->s_mds);
1847 }
1848 }
1849 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1850 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1851 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1852 spin_unlock(&session->s_cap_lock);
1853
1854 if (wake)
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001855 wake_up_session_caps(session, RENEWCAPS);
Sage Weil2f2dc052009-10-06 11:31:09 -07001856}
1857
1858/*
1859 * send a session close request
1860 */
Xiubo Li3e699bd2020-06-30 03:52:15 -04001861static int request_close_session(struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001862{
1863 struct ceph_msg *msg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001864
1865 dout("request_close_session mds%d state %s seq %lld\n",
John Spraya687eca2014-09-19 13:51:08 +01001866 session->s_mds, ceph_session_state_name(session->s_state),
Sage Weil2f2dc052009-10-06 11:31:09 -07001867 session->s_seq);
1868 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001869 if (!msg)
1870 return -ENOMEM;
1871 ceph_con_send(&session->s_con, msg);
Yan, Zhengfcff4152016-09-14 16:39:51 +08001872 return 1;
Sage Weil2f2dc052009-10-06 11:31:09 -07001873}
1874
1875/*
1876 * Called with s_mutex held.
1877 */
1878static int __close_session(struct ceph_mds_client *mdsc,
1879 struct ceph_mds_session *session)
1880{
1881 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1882 return 0;
1883 session->s_state = CEPH_MDS_SESSION_CLOSING;
Xiubo Li3e699bd2020-06-30 03:52:15 -04001884 return request_close_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07001885}
1886
Yan, Zheng040d7862017-11-30 11:59:22 +08001887static bool drop_negative_children(struct dentry *dentry)
1888{
1889 struct dentry *child;
1890 bool all_negative = true;
1891
1892 if (!d_is_dir(dentry))
1893 goto out;
1894
1895 spin_lock(&dentry->d_lock);
1896 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
1897 if (d_really_is_positive(child)) {
1898 all_negative = false;
1899 break;
1900 }
1901 }
1902 spin_unlock(&dentry->d_lock);
1903
1904 if (all_negative)
1905 shrink_dcache_parent(dentry);
1906out:
1907 return all_negative;
1908}
1909
Sage Weil2f2dc052009-10-06 11:31:09 -07001910/*
1911 * Trim old(er) caps.
1912 *
1913 * Because we can't cache an inode without one or more caps, we do
1914 * this indirectly: if a cap is unused, we prune its aliases, at which
1915 * point the inode will hopefully get dropped to.
1916 *
1917 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1918 * memory pressure from the MDS, though, so it needn't be perfect.
1919 */
1920static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1921{
Jeff Layton533a2812019-07-19 15:22:28 -04001922 int *remaining = arg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001923 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng979abfd2013-11-22 13:56:24 +08001924 int used, wanted, oissued, mine;
Sage Weil2f2dc052009-10-06 11:31:09 -07001925
Jeff Layton533a2812019-07-19 15:22:28 -04001926 if (*remaining <= 0)
Sage Weil2f2dc052009-10-06 11:31:09 -07001927 return -1;
1928
Sage Weilbe655592011-11-30 09:47:09 -08001929 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001930 mine = cap->issued | cap->implemented;
1931 used = __ceph_caps_used(ci);
Yan, Zheng979abfd2013-11-22 13:56:24 +08001932 wanted = __ceph_caps_file_wanted(ci);
Sage Weil2f2dc052009-10-06 11:31:09 -07001933 oissued = __ceph_caps_issued_other(ci, cap);
1934
Yan, Zheng979abfd2013-11-22 13:56:24 +08001935 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001936 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
Yan, Zheng979abfd2013-11-22 13:56:24 +08001937 ceph_cap_string(used), ceph_cap_string(wanted));
1938 if (cap == ci->i_auth_cap) {
Yan, Zheng622f3e22015-05-07 10:59:47 +08001939 if (ci->i_dirty_caps || ci->i_flushing_caps ||
1940 !list_empty(&ci->i_cap_snaps))
Yan, Zheng979abfd2013-11-22 13:56:24 +08001941 goto out;
1942 if ((used | wanted) & CEPH_CAP_ANY_WR)
1943 goto out;
Yan, Zheng89aa5932017-09-08 15:23:18 +08001944 /* Note: it's possible that i_filelock_ref becomes non-zero
1945 * after dropping auth caps. It doesn't hurt because reply
1946 * of lock mds request will re-add auth caps. */
1947 if (atomic_read(&ci->i_filelock_ref) > 0)
1948 goto out;
Yan, Zheng979abfd2013-11-22 13:56:24 +08001949 }
Yan, Zheng5e804ac2015-10-26 16:08:43 +08001950 /* The inode has cached pages, but it's no longer used.
1951 * we can safely drop it */
Yan, Zheng525d15e2019-05-11 17:27:59 +08001952 if (S_ISREG(inode->i_mode) &&
1953 wanted == 0 && used == CEPH_CAP_FILE_CACHE &&
Yan, Zheng5e804ac2015-10-26 16:08:43 +08001954 !(oissued & CEPH_CAP_FILE_CACHE)) {
1955 used = 0;
1956 oissued = 0;
1957 }
Yan, Zheng979abfd2013-11-22 13:56:24 +08001958 if ((used | wanted) & ~oissued & mine)
Sage Weil2f2dc052009-10-06 11:31:09 -07001959 goto out; /* we need these caps */
1960
Sage Weil2f2dc052009-10-06 11:31:09 -07001961 if (oissued) {
1962 /* we aren't the only cap.. just remove us */
Yan, Zhenga096b092013-09-22 10:15:58 +08001963 __ceph_remove_cap(cap, true);
Jeff Layton533a2812019-07-19 15:22:28 -04001964 (*remaining)--;
Sage Weil2f2dc052009-10-06 11:31:09 -07001965 } else {
Yan, Zheng040d7862017-11-30 11:59:22 +08001966 struct dentry *dentry;
Yan, Zheng5e804ac2015-10-26 16:08:43 +08001967 /* try dropping referring dentries */
Sage Weilbe655592011-11-30 09:47:09 -08001968 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng040d7862017-11-30 11:59:22 +08001969 dentry = d_find_any_alias(inode);
1970 if (dentry && drop_negative_children(dentry)) {
1971 int count;
1972 dput(dentry);
1973 d_prune_aliases(inode);
1974 count = atomic_read(&inode->i_count);
1975 if (count == 1)
Jeff Layton533a2812019-07-19 15:22:28 -04001976 (*remaining)--;
Yan, Zheng040d7862017-11-30 11:59:22 +08001977 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1978 inode, cap, count);
1979 } else {
1980 dput(dentry);
1981 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001982 return 0;
1983 }
1984
1985out:
Sage Weilbe655592011-11-30 09:47:09 -08001986 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001987 return 0;
1988}
1989
1990/*
1991 * Trim session cap count down to some max number.
1992 */
Zhi Zhange30ee582018-01-24 21:24:33 +08001993int ceph_trim_caps(struct ceph_mds_client *mdsc,
1994 struct ceph_mds_session *session,
1995 int max_caps)
Sage Weil2f2dc052009-10-06 11:31:09 -07001996{
1997 int trim_caps = session->s_nr_caps - max_caps;
1998
1999 dout("trim_caps mds%d start: %d / %d, trim %d\n",
2000 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
2001 if (trim_caps > 0) {
Jeff Layton533a2812019-07-19 15:22:28 -04002002 int remaining = trim_caps;
2003
2004 ceph_iterate_session_caps(session, trim_caps_cb, &remaining);
Sage Weil2f2dc052009-10-06 11:31:09 -07002005 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
2006 session->s_mds, session->s_nr_caps, max_caps,
Jeff Layton533a2812019-07-19 15:22:28 -04002007 trim_caps - remaining);
Sage Weil2f2dc052009-10-06 11:31:09 -07002008 }
Yan, Zhenga56371d2014-04-01 20:34:56 +08002009
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08002010 ceph_flush_cap_releases(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002011 return 0;
2012}
2013
Yan, Zheng8310b082015-06-09 17:20:12 +08002014static int check_caps_flush(struct ceph_mds_client *mdsc,
2015 u64 want_flush_tid)
2016{
Yan, Zheng8310b082015-06-09 17:20:12 +08002017 int ret = 1;
2018
2019 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zhenge4500b52016-07-06 11:12:56 +08002020 if (!list_empty(&mdsc->cap_flush_list)) {
2021 struct ceph_cap_flush *cf =
2022 list_first_entry(&mdsc->cap_flush_list,
2023 struct ceph_cap_flush, g_list);
2024 if (cf->tid <= want_flush_tid) {
2025 dout("check_caps_flush still flushing tid "
2026 "%llu <= %llu\n", cf->tid, want_flush_tid);
2027 ret = 0;
2028 }
Yan, Zheng8310b082015-06-09 17:20:12 +08002029 }
2030 spin_unlock(&mdsc->cap_dirty_lock);
2031 return ret;
Yan, Zhengd3383a82015-01-08 21:30:12 +08002032}
2033
Sage Weil2f2dc052009-10-06 11:31:09 -07002034/*
2035 * flush all dirty inode data to disk.
2036 *
Yan, Zheng8310b082015-06-09 17:20:12 +08002037 * returns true if we've flushed through want_flush_tid
Sage Weil2f2dc052009-10-06 11:31:09 -07002038 */
Yan, Zhengaffbc192015-05-05 21:22:13 +08002039static void wait_caps_flush(struct ceph_mds_client *mdsc,
Yan, Zheng0e294382016-07-04 18:06:41 +08002040 u64 want_flush_tid)
Sage Weil2f2dc052009-10-06 11:31:09 -07002041{
Yan, Zheng0e294382016-07-04 18:06:41 +08002042 dout("check_caps_flush want %llu\n", want_flush_tid);
Yan, Zheng8310b082015-06-09 17:20:12 +08002043
2044 wait_event(mdsc->cap_flushing_wq,
2045 check_caps_flush(mdsc, want_flush_tid));
2046
2047 dout("check_caps_flush ok, flushed thru %llu\n", want_flush_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002048}
2049
2050/*
2051 * called under s_mutex
2052 */
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08002053static void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
2054 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07002055{
Yan, Zheng745a8e32015-05-14 17:22:42 +08002056 struct ceph_msg *msg = NULL;
2057 struct ceph_mds_cap_release *head;
2058 struct ceph_mds_cap_item *item;
Jeff Layton92475f02017-04-13 11:07:04 -04002059 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
Yan, Zheng745a8e32015-05-14 17:22:42 +08002060 struct ceph_cap *cap;
2061 LIST_HEAD(tmp_list);
2062 int num_cap_releases;
Jeff Layton92475f02017-04-13 11:07:04 -04002063 __le32 barrier, *cap_barrier;
2064
2065 down_read(&osdc->lock);
2066 barrier = cpu_to_le32(osdc->epoch_barrier);
2067 up_read(&osdc->lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002068
Sage Weil0f8605f2010-05-05 15:51:35 -07002069 spin_lock(&session->s_cap_lock);
Yan, Zheng745a8e32015-05-14 17:22:42 +08002070again:
2071 list_splice_init(&session->s_cap_releases, &tmp_list);
2072 num_cap_releases = session->s_num_cap_releases;
2073 session->s_num_cap_releases = 0;
2074 spin_unlock(&session->s_cap_lock);
2075
2076 while (!list_empty(&tmp_list)) {
2077 if (!msg) {
2078 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002079 PAGE_SIZE, GFP_NOFS, false);
Yan, Zheng745a8e32015-05-14 17:22:42 +08002080 if (!msg)
2081 goto out_err;
2082 head = msg->front.iov_base;
2083 head->num = cpu_to_le32(0);
2084 msg->front.iov_len = sizeof(*head);
Jeff Layton92475f02017-04-13 11:07:04 -04002085
2086 msg->hdr.version = cpu_to_le16(2);
2087 msg->hdr.compat_version = cpu_to_le16(1);
Yan, Zheng745a8e32015-05-14 17:22:42 +08002088 }
Jeff Layton92475f02017-04-13 11:07:04 -04002089
Yan, Zheng745a8e32015-05-14 17:22:42 +08002090 cap = list_first_entry(&tmp_list, struct ceph_cap,
2091 session_caps);
2092 list_del(&cap->session_caps);
2093 num_cap_releases--;
2094
2095 head = msg->front.iov_base;
Jeff Layton4198aba2019-05-02 08:06:50 -04002096 put_unaligned_le32(get_unaligned_le32(&head->num) + 1,
2097 &head->num);
Yan, Zheng745a8e32015-05-14 17:22:42 +08002098 item = msg->front.iov_base + msg->front.iov_len;
2099 item->ino = cpu_to_le64(cap->cap_ino);
2100 item->cap_id = cpu_to_le64(cap->cap_id);
2101 item->migrate_seq = cpu_to_le32(cap->mseq);
2102 item->seq = cpu_to_le32(cap->issue_seq);
2103 msg->front.iov_len += sizeof(*item);
2104
2105 ceph_put_cap(mdsc, cap);
2106
2107 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
Jeff Layton92475f02017-04-13 11:07:04 -04002108 // Append cap_barrier field
2109 cap_barrier = msg->front.iov_base + msg->front.iov_len;
2110 *cap_barrier = barrier;
2111 msg->front.iov_len += sizeof(*cap_barrier);
2112
Yan, Zheng745a8e32015-05-14 17:22:42 +08002113 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2114 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
2115 ceph_con_send(&session->s_con, msg);
2116 msg = NULL;
2117 }
2118 }
2119
2120 BUG_ON(num_cap_releases != 0);
2121
2122 spin_lock(&session->s_cap_lock);
2123 if (!list_empty(&session->s_cap_releases))
2124 goto again;
2125 spin_unlock(&session->s_cap_lock);
2126
2127 if (msg) {
Jeff Layton92475f02017-04-13 11:07:04 -04002128 // Append cap_barrier field
2129 cap_barrier = msg->front.iov_base + msg->front.iov_len;
2130 *cap_barrier = barrier;
2131 msg->front.iov_len += sizeof(*cap_barrier);
2132
Sage Weil2f2dc052009-10-06 11:31:09 -07002133 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2134 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
2135 ceph_con_send(&session->s_con, msg);
2136 }
Yan, Zheng745a8e32015-05-14 17:22:42 +08002137 return;
2138out_err:
2139 pr_err("send_cap_releases mds%d, failed to allocate message\n",
2140 session->s_mds);
2141 spin_lock(&session->s_cap_lock);
2142 list_splice(&tmp_list, &session->s_cap_releases);
2143 session->s_num_cap_releases += num_cap_releases;
Sage Weil2f2dc052009-10-06 11:31:09 -07002144 spin_unlock(&session->s_cap_lock);
2145}
2146
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08002147static void ceph_cap_release_work(struct work_struct *work)
2148{
2149 struct ceph_mds_session *session =
2150 container_of(work, struct ceph_mds_session, s_cap_release_work);
2151
2152 mutex_lock(&session->s_mutex);
2153 if (session->s_state == CEPH_MDS_SESSION_OPEN ||
2154 session->s_state == CEPH_MDS_SESSION_HUNG)
2155 ceph_send_cap_releases(session->s_mdsc, session);
2156 mutex_unlock(&session->s_mutex);
2157 ceph_put_mds_session(session);
2158}
2159
2160void ceph_flush_cap_releases(struct ceph_mds_client *mdsc,
2161 struct ceph_mds_session *session)
2162{
2163 if (mdsc->stopping)
2164 return;
2165
Xiubo Li5b3248c2019-12-19 19:44:09 -05002166 ceph_get_mds_session(session);
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08002167 if (queue_work(mdsc->fsc->cap_wq,
2168 &session->s_cap_release_work)) {
2169 dout("cap release work queued\n");
2170 } else {
2171 ceph_put_mds_session(session);
2172 dout("failed to queue cap release work\n");
2173 }
2174}
2175
2176/*
2177 * caller holds session->s_cap_lock
2178 */
2179void __ceph_queue_cap_release(struct ceph_mds_session *session,
2180 struct ceph_cap *cap)
2181{
2182 list_add_tail(&cap->session_caps, &session->s_cap_releases);
2183 session->s_num_cap_releases++;
2184
2185 if (!(session->s_num_cap_releases % CEPH_CAPS_PER_RELEASE))
2186 ceph_flush_cap_releases(session->s_mdsc, session);
2187}
2188
Yan, Zheng37c4efc2019-01-31 16:55:51 +08002189static void ceph_cap_reclaim_work(struct work_struct *work)
2190{
2191 struct ceph_mds_client *mdsc =
2192 container_of(work, struct ceph_mds_client, cap_reclaim_work);
2193 int ret = ceph_trim_dentries(mdsc);
2194 if (ret == -EAGAIN)
2195 ceph_queue_cap_reclaim_work(mdsc);
2196}
2197
2198void ceph_queue_cap_reclaim_work(struct ceph_mds_client *mdsc)
2199{
2200 if (mdsc->stopping)
2201 return;
2202
2203 if (queue_work(mdsc->fsc->cap_wq, &mdsc->cap_reclaim_work)) {
2204 dout("caps reclaim work queued\n");
2205 } else {
2206 dout("failed to queue caps release work\n");
2207 }
2208}
2209
Yan, Zhengfe330322019-02-01 14:57:15 +08002210void ceph_reclaim_caps_nr(struct ceph_mds_client *mdsc, int nr)
2211{
2212 int val;
2213 if (!nr)
2214 return;
2215 val = atomic_add_return(nr, &mdsc->cap_reclaim_pending);
Xiubo Libba15602019-11-26 07:32:22 -05002216 if ((val % CEPH_CAPS_PER_RELEASE) < nr) {
Yan, Zhengfe330322019-02-01 14:57:15 +08002217 atomic_set(&mdsc->cap_reclaim_pending, 0);
2218 ceph_queue_cap_reclaim_work(mdsc);
2219 }
2220}
2221
Sage Weil2f2dc052009-10-06 11:31:09 -07002222/*
2223 * requests
2224 */
2225
Yan, Zheng54008392014-03-29 13:41:15 +08002226int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
2227 struct inode *dir)
2228{
2229 struct ceph_inode_info *ci = ceph_inode(dir);
2230 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
2231 struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options;
Yan, Zheng2a5beea2016-04-28 09:37:39 +08002232 size_t size = sizeof(struct ceph_mds_reply_dir_entry);
Jeff Laytonad8c28a2019-09-09 15:58:55 -04002233 unsigned int num_entries;
2234 int order;
Yan, Zheng54008392014-03-29 13:41:15 +08002235
2236 spin_lock(&ci->i_ceph_lock);
2237 num_entries = ci->i_files + ci->i_subdirs;
2238 spin_unlock(&ci->i_ceph_lock);
Jeff Laytonad8c28a2019-09-09 15:58:55 -04002239 num_entries = max(num_entries, 1U);
Yan, Zheng54008392014-03-29 13:41:15 +08002240 num_entries = min(num_entries, opt->max_readdir);
2241
2242 order = get_order(size * num_entries);
2243 while (order >= 0) {
Yan, Zheng2a5beea2016-04-28 09:37:39 +08002244 rinfo->dir_entries = (void*)__get_free_pages(GFP_KERNEL |
2245 __GFP_NOWARN,
2246 order);
2247 if (rinfo->dir_entries)
Yan, Zheng54008392014-03-29 13:41:15 +08002248 break;
2249 order--;
2250 }
Yan, Zheng2a5beea2016-04-28 09:37:39 +08002251 if (!rinfo->dir_entries)
Yan, Zheng54008392014-03-29 13:41:15 +08002252 return -ENOMEM;
2253
2254 num_entries = (PAGE_SIZE << order) / size;
2255 num_entries = min(num_entries, opt->max_readdir);
2256
2257 rinfo->dir_buf_size = PAGE_SIZE << order;
2258 req->r_num_caps = num_entries + 1;
2259 req->r_args.readdir.max_entries = cpu_to_le32(num_entries);
2260 req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes);
2261 return 0;
2262}
2263
Sage Weil2f2dc052009-10-06 11:31:09 -07002264/*
2265 * Create an mds request.
2266 */
2267struct ceph_mds_request *
2268ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
2269{
Jeff Layton058daab2020-02-17 18:38:37 -05002270 struct ceph_mds_request *req;
Sage Weil2f2dc052009-10-06 11:31:09 -07002271
Jeff Layton058daab2020-02-17 18:38:37 -05002272 req = kmem_cache_zalloc(ceph_mds_request_cachep, GFP_NOFS);
Sage Weil2f2dc052009-10-06 11:31:09 -07002273 if (!req)
2274 return ERR_PTR(-ENOMEM);
2275
Sage Weilb4556392010-05-13 12:01:13 -07002276 mutex_init(&req->r_fill_mutex);
Yehuda Sadeh37151662010-06-17 16:16:12 -07002277 req->r_mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07002278 req->r_started = jiffies;
Xiubo Li70c94822020-03-19 23:45:02 -04002279 req->r_start_latency = ktime_get();
Sage Weil2f2dc052009-10-06 11:31:09 -07002280 req->r_resend_mds = -1;
2281 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002282 INIT_LIST_HEAD(&req->r_unsafe_target_item);
Sage Weil2f2dc052009-10-06 11:31:09 -07002283 req->r_fmode = -1;
Sage Weil153c8e62009-12-07 12:31:09 -08002284 kref_init(&req->r_kref);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02002285 RB_CLEAR_NODE(&req->r_node);
Sage Weil2f2dc052009-10-06 11:31:09 -07002286 INIT_LIST_HEAD(&req->r_wait);
2287 init_completion(&req->r_completion);
2288 init_completion(&req->r_safe_completion);
2289 INIT_LIST_HEAD(&req->r_unsafe_item);
2290
Deepa Dinamani668c9a62019-12-02 21:19:42 -08002291 ktime_get_coarse_real_ts64(&req->r_stamp);
Sage Weilb8e69062014-05-21 17:41:08 -07002292
Sage Weil2f2dc052009-10-06 11:31:09 -07002293 req->r_op = op;
2294 req->r_direct_mode = mode;
2295 return req;
2296}
2297
2298/*
Sage Weil44ca18f2010-02-15 12:08:46 -08002299 * return oldest (lowest) request, tid in request tree, 0 if none.
Sage Weil2f2dc052009-10-06 11:31:09 -07002300 *
2301 * called under mdsc->mutex.
2302 */
Sage Weil44ca18f2010-02-15 12:08:46 -08002303static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
2304{
2305 if (RB_EMPTY_ROOT(&mdsc->request_tree))
2306 return NULL;
2307 return rb_entry(rb_first(&mdsc->request_tree),
2308 struct ceph_mds_request, r_node);
2309}
2310
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08002311static inline u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07002312{
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08002313 return mdsc->oldest_tid;
Sage Weil2f2dc052009-10-06 11:31:09 -07002314}
2315
2316/*
2317 * Build a dentry's path. Allocate on heap; caller must kfree. Based
2318 * on build_path_from_dentry in fs/cifs/dir.c.
2319 *
2320 * If @stop_on_nosnap, generate path relative to the first non-snapped
2321 * inode.
2322 *
2323 * Encode hidden .snap dirs as a double /, i.e.
2324 * foo/.snap/bar -> foo//bar
2325 */
Jeff Layton69a10fb2019-04-26 13:33:39 -04002326char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase,
Sage Weil2f2dc052009-10-06 11:31:09 -07002327 int stop_on_nosnap)
2328{
2329 struct dentry *temp;
2330 char *path;
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002331 int pos;
Al Viro1b71fe22011-07-16 23:43:58 -04002332 unsigned seq;
Jeff Layton69a10fb2019-04-26 13:33:39 -04002333 u64 base;
Sage Weil2f2dc052009-10-06 11:31:09 -07002334
Markus Elfringd37b1d92017-08-20 20:22:02 +02002335 if (!dentry)
Sage Weil2f2dc052009-10-06 11:31:09 -07002336 return ERR_PTR(-EINVAL);
2337
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002338 path = __getname();
Markus Elfringd37b1d92017-08-20 20:22:02 +02002339 if (!path)
Sage Weil2f2dc052009-10-06 11:31:09 -07002340 return ERR_PTR(-ENOMEM);
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002341retry:
2342 pos = PATH_MAX - 1;
2343 path[pos] = '\0';
2344
2345 seq = read_seqbegin(&rename_lock);
Al Viro1b71fe22011-07-16 23:43:58 -04002346 rcu_read_lock();
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002347 temp = dentry;
2348 for (;;) {
Al Viro1b71fe22011-07-16 23:43:58 -04002349 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07002350
Al Viro1b71fe22011-07-16 23:43:58 -04002351 spin_lock(&temp->d_lock);
David Howells2b0143b2015-03-17 22:25:59 +00002352 inode = d_inode(temp);
Sage Weil2f2dc052009-10-06 11:31:09 -07002353 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
Sage Weil104648a2010-03-18 10:14:30 -07002354 dout("build_path path+%d: %p SNAPDIR\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002355 pos, temp);
Jeff Laytond6b8bd62019-05-09 07:58:38 -04002356 } else if (stop_on_nosnap && inode && dentry != temp &&
Sage Weil2f2dc052009-10-06 11:31:09 -07002357 ceph_snap(inode) == CEPH_NOSNAP) {
Yehuda Sadeh9d5a09e2011-12-13 09:57:44 -08002358 spin_unlock(&temp->d_lock);
Jeff Laytond6b8bd62019-05-09 07:58:38 -04002359 pos++; /* get rid of any prepended '/' */
Sage Weil2f2dc052009-10-06 11:31:09 -07002360 break;
2361 } else {
2362 pos -= temp->d_name.len;
Al Viro1b71fe22011-07-16 23:43:58 -04002363 if (pos < 0) {
2364 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002365 break;
Al Viro1b71fe22011-07-16 23:43:58 -04002366 }
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002367 memcpy(path + pos, temp->d_name.name, temp->d_name.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07002368 }
Al Viro1b71fe22011-07-16 23:43:58 -04002369 spin_unlock(&temp->d_lock);
Yan, Zheng41883ba2019-05-23 10:22:55 +08002370 temp = READ_ONCE(temp->d_parent);
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002371
2372 /* Are we at the root? */
2373 if (IS_ROOT(temp))
2374 break;
2375
2376 /* Are we out of buffer? */
2377 if (--pos < 0)
2378 break;
2379
2380 path[pos] = '/';
Sage Weil2f2dc052009-10-06 11:31:09 -07002381 }
Jeff Layton69a10fb2019-04-26 13:33:39 -04002382 base = ceph_ino(d_inode(temp));
Al Viro1b71fe22011-07-16 23:43:58 -04002383 rcu_read_unlock();
Jeff Laytonf5946bc2019-10-16 08:20:17 -04002384
2385 if (read_seqretry(&rename_lock, seq))
2386 goto retry;
2387
2388 if (pos < 0) {
2389 /*
2390 * A rename didn't occur, but somehow we didn't end up where
2391 * we thought we would. Throw a warning and try again.
2392 */
2393 pr_warn("build_path did not end path lookup where "
2394 "expected, pos is %d\n", pos);
Sage Weil2f2dc052009-10-06 11:31:09 -07002395 goto retry;
2396 }
2397
Jeff Layton69a10fb2019-04-26 13:33:39 -04002398 *pbase = base;
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002399 *plen = PATH_MAX - 1 - pos;
Sage Weil104648a2010-03-18 10:14:30 -07002400 dout("build_path on %p %d built %llx '%.*s'\n",
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002401 dentry, d_count(dentry), base, *plen, path + pos);
2402 return path + pos;
Sage Weil2f2dc052009-10-06 11:31:09 -07002403}
2404
Jeff Laytonfd36a712016-12-15 08:37:58 -05002405static int build_dentry_path(struct dentry *dentry, struct inode *dir,
Sage Weil2f2dc052009-10-06 11:31:09 -07002406 const char **ppath, int *ppathlen, u64 *pino,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002407 bool *pfreepath, bool parent_locked)
Sage Weil2f2dc052009-10-06 11:31:09 -07002408{
2409 char *path;
2410
Jeff Laytonc6b0b652016-12-15 08:37:57 -05002411 rcu_read_lock();
Jeff Laytonfd36a712016-12-15 08:37:58 -05002412 if (!dir)
2413 dir = d_inode_rcu(dentry->d_parent);
Jeff Layton964fff72019-04-29 11:51:02 -04002414 if (dir && parent_locked && ceph_snap(dir) == CEPH_NOSNAP) {
Jeff Laytonc6b0b652016-12-15 08:37:57 -05002415 *pino = ceph_ino(dir);
2416 rcu_read_unlock();
Jeff Layton964fff72019-04-29 11:51:02 -04002417 *ppath = dentry->d_name.name;
2418 *ppathlen = dentry->d_name.len;
Sage Weil2f2dc052009-10-06 11:31:09 -07002419 return 0;
2420 }
Jeff Laytonc6b0b652016-12-15 08:37:57 -05002421 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -07002422 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
2423 if (IS_ERR(path))
2424 return PTR_ERR(path);
2425 *ppath = path;
Jeff Layton1bcb3442019-04-15 12:00:42 -04002426 *pfreepath = true;
Sage Weil2f2dc052009-10-06 11:31:09 -07002427 return 0;
2428}
2429
2430static int build_inode_path(struct inode *inode,
2431 const char **ppath, int *ppathlen, u64 *pino,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002432 bool *pfreepath)
Sage Weil2f2dc052009-10-06 11:31:09 -07002433{
2434 struct dentry *dentry;
2435 char *path;
2436
2437 if (ceph_snap(inode) == CEPH_NOSNAP) {
2438 *pino = ceph_ino(inode);
2439 *ppathlen = 0;
2440 return 0;
2441 }
2442 dentry = d_find_alias(inode);
2443 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
2444 dput(dentry);
2445 if (IS_ERR(path))
2446 return PTR_ERR(path);
2447 *ppath = path;
Jeff Layton1bcb3442019-04-15 12:00:42 -04002448 *pfreepath = true;
Sage Weil2f2dc052009-10-06 11:31:09 -07002449 return 0;
2450}
2451
2452/*
2453 * request arguments may be specified via an inode *, a dentry *, or
2454 * an explicit ino+path.
2455 */
2456static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
Jeff Laytonfd36a712016-12-15 08:37:58 -05002457 struct inode *rdiri, const char *rpath,
2458 u64 rino, const char **ppath, int *pathlen,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002459 u64 *ino, bool *freepath, bool parent_locked)
Sage Weil2f2dc052009-10-06 11:31:09 -07002460{
2461 int r = 0;
2462
2463 if (rinode) {
2464 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
2465 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
2466 ceph_snap(rinode));
2467 } else if (rdentry) {
Jeff Laytonfd36a712016-12-15 08:37:58 -05002468 r = build_dentry_path(rdentry, rdiri, ppath, pathlen, ino,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002469 freepath, parent_locked);
Sage Weil2f2dc052009-10-06 11:31:09 -07002470 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
2471 *ppath);
Sage Weil795858d2011-08-15 13:02:37 -07002472 } else if (rpath || rino) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002473 *ino = rino;
2474 *ppath = rpath;
David Zafmanb0000562012-10-25 10:23:46 -07002475 *pathlen = rpath ? strlen(rpath) : 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002476 dout(" path %.*s\n", *pathlen, rpath);
2477 }
2478
2479 return r;
2480}
2481
2482/*
2483 * called under mdsc->mutex
2484 */
2485static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
2486 struct ceph_mds_request *req,
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002487 int mds, bool drop_cap_releases)
Sage Weil2f2dc052009-10-06 11:31:09 -07002488{
2489 struct ceph_msg *msg;
2490 struct ceph_mds_request_head *head;
2491 const char *path1 = NULL;
2492 const char *path2 = NULL;
2493 u64 ino1 = 0, ino2 = 0;
2494 int pathlen1 = 0, pathlen2 = 0;
Jeff Layton1bcb3442019-04-15 12:00:42 -04002495 bool freepath1 = false, freepath2 = false;
Sage Weil2f2dc052009-10-06 11:31:09 -07002496 int len;
2497 u16 releases;
2498 void *p, *end;
2499 int ret;
2500
2501 ret = set_request_path_attr(req->r_inode, req->r_dentry,
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002502 req->r_parent, req->r_path1, req->r_ino1.ino,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002503 &path1, &pathlen1, &ino1, &freepath1,
2504 test_bit(CEPH_MDS_R_PARENT_LOCKED,
2505 &req->r_req_flags));
Sage Weil2f2dc052009-10-06 11:31:09 -07002506 if (ret < 0) {
2507 msg = ERR_PTR(ret);
2508 goto out;
2509 }
2510
Jeff Layton1bcb3442019-04-15 12:00:42 -04002511 /* If r_old_dentry is set, then assume that its parent is locked */
Sage Weil2f2dc052009-10-06 11:31:09 -07002512 ret = set_request_path_attr(NULL, req->r_old_dentry,
Jeff Laytonfd36a712016-12-15 08:37:58 -05002513 req->r_old_dentry_dir,
Sage Weil2f2dc052009-10-06 11:31:09 -07002514 req->r_path2, req->r_ino2.ino,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002515 &path2, &pathlen2, &ino2, &freepath2, true);
Sage Weil2f2dc052009-10-06 11:31:09 -07002516 if (ret < 0) {
2517 msg = ERR_PTR(ret);
2518 goto out_free1;
2519 }
2520
2521 len = sizeof(*head) +
Sage Weilb8e69062014-05-21 17:41:08 -07002522 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) +
Arnd Bergmann777d7382015-09-30 15:04:42 +02002523 sizeof(struct ceph_timespec);
Sage Weil2f2dc052009-10-06 11:31:09 -07002524
2525 /* calculate (max) length for cap releases */
2526 len += sizeof(struct ceph_mds_request_release) *
2527 (!!req->r_inode_drop + !!req->r_dentry_drop +
2528 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
2529 if (req->r_dentry_drop)
Jeff Laytonc1dfc272019-04-17 14:23:17 -04002530 len += pathlen1;
Sage Weil2f2dc052009-10-06 11:31:09 -07002531 if (req->r_old_dentry_drop)
Jeff Laytonc1dfc272019-04-17 14:23:17 -04002532 len += pathlen2;
Sage Weil2f2dc052009-10-06 11:31:09 -07002533
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02002534 msg = ceph_msg_new2(CEPH_MSG_CLIENT_REQUEST, len, 1, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07002535 if (!msg) {
2536 msg = ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -07002537 goto out_free2;
Sage Weila79832f2010-04-01 16:06:19 -07002538 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002539
John Spray7cfa0312014-10-30 17:15:26 +00002540 msg->hdr.version = cpu_to_le16(2);
Sage Weil6df058c2009-12-22 11:24:33 -08002541 msg->hdr.tid = cpu_to_le64(req->r_tid);
2542
Sage Weil2f2dc052009-10-06 11:31:09 -07002543 head = msg->front.iov_base;
2544 p = msg->front.iov_base + sizeof(*head);
2545 end = msg->front.iov_base + msg->front.iov_len;
2546
2547 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
2548 head->op = cpu_to_le32(req->r_op);
Eric W. Biedermanff3d0042013-01-31 04:01:53 -08002549 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid));
2550 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid));
Jeff Layton6deb8002020-01-13 13:04:08 -05002551 head->ino = cpu_to_le64(req->r_deleg_ino);
Sage Weil2f2dc052009-10-06 11:31:09 -07002552 head->args = req->r_args;
2553
2554 ceph_encode_filepath(&p, end, ino1, path1);
2555 ceph_encode_filepath(&p, end, ino2, path2);
2556
Sage Weile979cf52010-07-15 14:58:39 -07002557 /* make note of release offset, in case we need to replay */
2558 req->r_request_release_offset = p - msg->front.iov_base;
2559
Sage Weil2f2dc052009-10-06 11:31:09 -07002560 /* cap releases */
2561 releases = 0;
2562 if (req->r_inode_drop)
2563 releases += ceph_encode_inode_release(&p,
David Howells2b0143b2015-03-17 22:25:59 +00002564 req->r_inode ? req->r_inode : d_inode(req->r_dentry),
Yan, Zheng719a2512020-03-05 20:21:00 +08002565 mds, req->r_inode_drop, req->r_inode_unless,
2566 req->r_op == CEPH_MDS_OP_READDIR);
Sage Weil2f2dc052009-10-06 11:31:09 -07002567 if (req->r_dentry_drop)
2568 releases += ceph_encode_dentry_release(&p, req->r_dentry,
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002569 req->r_parent, mds, req->r_dentry_drop,
Jeff Laytonca6c8ae2016-12-15 08:37:59 -05002570 req->r_dentry_unless);
Sage Weil2f2dc052009-10-06 11:31:09 -07002571 if (req->r_old_dentry_drop)
2572 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
Jeff Laytonca6c8ae2016-12-15 08:37:59 -05002573 req->r_old_dentry_dir, mds,
2574 req->r_old_dentry_drop,
2575 req->r_old_dentry_unless);
Sage Weil2f2dc052009-10-06 11:31:09 -07002576 if (req->r_old_inode_drop)
2577 releases += ceph_encode_inode_release(&p,
David Howells2b0143b2015-03-17 22:25:59 +00002578 d_inode(req->r_old_dentry),
Sage Weil2f2dc052009-10-06 11:31:09 -07002579 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002580
2581 if (drop_cap_releases) {
2582 releases = 0;
2583 p = msg->front.iov_base + req->r_request_release_offset;
2584 }
2585
Sage Weil2f2dc052009-10-06 11:31:09 -07002586 head->num_releases = cpu_to_le16(releases);
2587
Sage Weilb8e69062014-05-21 17:41:08 -07002588 /* time stamp */
Yan, Zheng1f041a82015-01-13 15:20:52 +08002589 {
2590 struct ceph_timespec ts;
Arnd Bergmann0ed1e902018-07-13 22:18:38 +02002591 ceph_encode_timespec64(&ts, &req->r_stamp);
Yan, Zheng1f041a82015-01-13 15:20:52 +08002592 ceph_encode_copy(&p, &ts, sizeof(ts));
2593 }
Sage Weilb8e69062014-05-21 17:41:08 -07002594
Xiubo Lib682c6d2020-06-30 03:52:18 -04002595 if (WARN_ON_ONCE(p > end)) {
2596 ceph_msg_put(msg);
2597 msg = ERR_PTR(-ERANGE);
2598 goto out_free2;
2599 }
2600
Sage Weil2f2dc052009-10-06 11:31:09 -07002601 msg->front.iov_len = p - msg->front.iov_base;
2602 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2603
Yan, Zheng25e6bae2014-09-16 19:15:28 +08002604 if (req->r_pagelist) {
2605 struct ceph_pagelist *pagelist = req->r_pagelist;
Yan, Zheng25e6bae2014-09-16 19:15:28 +08002606 ceph_msg_data_add_pagelist(msg, pagelist);
2607 msg->hdr.data_len = cpu_to_le32(pagelist->length);
2608 } else {
2609 msg->hdr.data_len = 0;
Alex Elderebf18f42013-03-04 22:29:57 -06002610 }
Alex Elder02afca62013-02-14 12:16:43 -06002611
Sage Weil2f2dc052009-10-06 11:31:09 -07002612 msg->hdr.data_off = cpu_to_le16(0);
2613
2614out_free2:
2615 if (freepath2)
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002616 ceph_mdsc_free_path((char *)path2, pathlen2);
Sage Weil2f2dc052009-10-06 11:31:09 -07002617out_free1:
2618 if (freepath1)
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002619 ceph_mdsc_free_path((char *)path1, pathlen1);
Sage Weil2f2dc052009-10-06 11:31:09 -07002620out:
2621 return msg;
2622}
2623
2624/*
2625 * called under mdsc->mutex if error, under no mutex if
2626 * success.
2627 */
2628static void complete_request(struct ceph_mds_client *mdsc,
2629 struct ceph_mds_request *req)
2630{
Xiubo Li70c94822020-03-19 23:45:02 -04002631 req->r_end_latency = ktime_get();
2632
Sage Weil2f2dc052009-10-06 11:31:09 -07002633 if (req->r_callback)
2634 req->r_callback(mdsc, req);
Jeff Layton111c7082019-04-02 09:43:18 -04002635 complete_all(&req->r_completion);
Sage Weil2f2dc052009-10-06 11:31:09 -07002636}
2637
2638/*
2639 * called under mdsc->mutex
2640 */
2641static int __prepare_send_request(struct ceph_mds_client *mdsc,
2642 struct ceph_mds_request *req,
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002643 int mds, bool drop_cap_releases)
Sage Weil2f2dc052009-10-06 11:31:09 -07002644{
2645 struct ceph_mds_request_head *rhead;
2646 struct ceph_msg *msg;
2647 int flags = 0;
2648
Sage Weil2f2dc052009-10-06 11:31:09 -07002649 req->r_attempts++;
Greg Farnume55b71f2010-06-22 15:58:01 -07002650 if (req->r_inode) {
2651 struct ceph_cap *cap =
2652 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
2653
2654 if (cap)
2655 req->r_sent_on_mseq = cap->mseq;
2656 else
2657 req->r_sent_on_mseq = -1;
2658 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002659 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
2660 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
2661
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002662 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002663 void *p;
Sage Weil01a92f12010-07-15 13:24:32 -07002664 /*
2665 * Replay. Do not regenerate message (and rebuild
2666 * paths, etc.); just use the original message.
2667 * Rebuilding paths will break for renames because
2668 * d_move mangles the src name.
2669 */
2670 msg = req->r_request;
2671 rhead = msg->front.iov_base;
2672
2673 flags = le32_to_cpu(rhead->flags);
2674 flags |= CEPH_MDS_FLAG_REPLAY;
2675 rhead->flags = cpu_to_le32(flags);
2676
2677 if (req->r_target_inode)
2678 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
2679
2680 rhead->num_retry = req->r_attempts - 1;
Sage Weile979cf52010-07-15 14:58:39 -07002681
2682 /* remove cap/dentry releases from message */
2683 rhead->num_releases = 0;
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002684
2685 /* time stamp */
2686 p = msg->front.iov_base + req->r_request_release_offset;
Yan, Zheng1f041a82015-01-13 15:20:52 +08002687 {
2688 struct ceph_timespec ts;
Arnd Bergmann0ed1e902018-07-13 22:18:38 +02002689 ceph_encode_timespec64(&ts, &req->r_stamp);
Yan, Zheng1f041a82015-01-13 15:20:52 +08002690 ceph_encode_copy(&p, &ts, sizeof(ts));
2691 }
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002692
2693 msg->front.iov_len = p - msg->front.iov_base;
2694 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
Sage Weil01a92f12010-07-15 13:24:32 -07002695 return 0;
2696 }
2697
Sage Weil2f2dc052009-10-06 11:31:09 -07002698 if (req->r_request) {
2699 ceph_msg_put(req->r_request);
2700 req->r_request = NULL;
2701 }
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002702 msg = create_request_message(mdsc, req, mds, drop_cap_releases);
Sage Weil2f2dc052009-10-06 11:31:09 -07002703 if (IS_ERR(msg)) {
Sage Weile1518c72010-05-13 11:19:06 -07002704 req->r_err = PTR_ERR(msg);
Sage Weila79832f2010-04-01 16:06:19 -07002705 return PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002706 }
2707 req->r_request = msg;
2708
2709 rhead = msg->front.iov_base;
Sage Weil2f2dc052009-10-06 11:31:09 -07002710 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002711 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Sage Weil2f2dc052009-10-06 11:31:09 -07002712 flags |= CEPH_MDS_FLAG_REPLAY;
Jeff Layton3bb48b42019-12-02 13:47:57 -05002713 if (test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags))
2714 flags |= CEPH_MDS_FLAG_ASYNC;
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002715 if (req->r_parent)
Sage Weil2f2dc052009-10-06 11:31:09 -07002716 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
2717 rhead->flags = cpu_to_le32(flags);
2718 rhead->num_fwd = req->r_num_fwd;
2719 rhead->num_retry = req->r_attempts - 1;
2720
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002721 dout(" r_parent = %p\n", req->r_parent);
Sage Weil2f2dc052009-10-06 11:31:09 -07002722 return 0;
2723}
2724
2725/*
Xiubo Li9cf54562019-12-05 20:50:21 -05002726 * called under mdsc->mutex
2727 */
2728static int __send_request(struct ceph_mds_client *mdsc,
2729 struct ceph_mds_session *session,
2730 struct ceph_mds_request *req,
2731 bool drop_cap_releases)
2732{
2733 int err;
2734
2735 err = __prepare_send_request(mdsc, req, session->s_mds,
2736 drop_cap_releases);
2737 if (!err) {
2738 ceph_msg_get(req->r_request);
2739 ceph_con_send(&session->s_con, req->r_request);
2740 }
2741
2742 return err;
2743}
2744
2745/*
Sage Weil2f2dc052009-10-06 11:31:09 -07002746 * send request, or put it on the appropriate wait list.
2747 */
Chengguang Xud5548492018-07-28 16:30:48 +08002748static void __do_request(struct ceph_mds_client *mdsc,
Sage Weil2f2dc052009-10-06 11:31:09 -07002749 struct ceph_mds_request *req)
2750{
2751 struct ceph_mds_session *session = NULL;
2752 int mds = -1;
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002753 int err = 0;
Xiubo Lic4853e92019-12-09 07:47:15 -05002754 bool random;
Sage Weil2f2dc052009-10-06 11:31:09 -07002755
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002756 if (req->r_err || test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
2757 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
Yan, Zhengeb1b8af2013-09-26 14:25:36 +08002758 __unregister_request(mdsc, req);
Chengguang Xud5548492018-07-28 16:30:48 +08002759 return;
Yan, Zhengeb1b8af2013-09-26 14:25:36 +08002760 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002761
2762 if (req->r_timeout &&
2763 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
2764 dout("do_request timed out\n");
Xiubo Li8ccf7fc2020-02-23 22:23:11 -05002765 err = -ETIMEDOUT;
Sage Weil2f2dc052009-10-06 11:31:09 -07002766 goto finish;
2767 }
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01002768 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002769 dout("do_request forced umount\n");
2770 err = -EIO;
2771 goto finish;
2772 }
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01002773 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_MOUNTING) {
Yan, Zhenge9e427f2016-11-10 16:02:06 +08002774 if (mdsc->mdsmap_err) {
2775 err = mdsc->mdsmap_err;
2776 dout("do_request mdsmap err %d\n", err);
2777 goto finish;
2778 }
Yan, Zhengcc8e8342017-01-04 16:21:58 +08002779 if (mdsc->mdsmap->m_epoch == 0) {
2780 dout("do_request no mdsmap, waiting for map\n");
2781 list_add(&req->r_wait, &mdsc->waiting_for_map);
Chengguang Xud5548492018-07-28 16:30:48 +08002782 return;
Yan, Zhengcc8e8342017-01-04 16:21:58 +08002783 }
Yan, Zhenge9e427f2016-11-10 16:02:06 +08002784 if (!(mdsc->fsc->mount_options->flags &
2785 CEPH_MOUNT_OPT_MOUNTWAIT) &&
2786 !ceph_mdsmap_is_cluster_available(mdsc->mdsmap)) {
Xiubo Li97820052019-12-10 20:29:40 -05002787 err = -EHOSTUNREACH;
Yan, Zhenge9e427f2016-11-10 16:02:06 +08002788 goto finish;
2789 }
2790 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002791
Sage Weildc69e2e2010-11-02 13:49:00 -07002792 put_request_session(req);
2793
Xiubo Lic4853e92019-12-09 07:47:15 -05002794 mds = __choose_mds(mdsc, req, &random);
Sage Weil2f2dc052009-10-06 11:31:09 -07002795 if (mds < 0 ||
2796 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
Jeff Layton3bb48b42019-12-02 13:47:57 -05002797 if (test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags)) {
2798 err = -EJUKEBOX;
2799 goto finish;
2800 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002801 dout("do_request no mds or not active, waiting for map\n");
2802 list_add(&req->r_wait, &mdsc->waiting_for_map);
Chengguang Xud5548492018-07-28 16:30:48 +08002803 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07002804 }
2805
2806 /* get, open session */
2807 session = __ceph_lookup_mds_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07002808 if (!session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002809 session = register_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07002810 if (IS_ERR(session)) {
2811 err = PTR_ERR(session);
2812 goto finish;
2813 }
2814 }
Xiubo Li5b3248c2019-12-19 19:44:09 -05002815 req->r_session = ceph_get_mds_session(session);
Sage Weildc69e2e2010-11-02 13:49:00 -07002816
Sage Weil2f2dc052009-10-06 11:31:09 -07002817 dout("do_request mds%d session %p state %s\n", mds, session,
John Spraya687eca2014-09-19 13:51:08 +01002818 ceph_session_state_name(session->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07002819 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
2820 session->s_state != CEPH_MDS_SESSION_HUNG) {
Yan, Zhengfcff4152016-09-14 16:39:51 +08002821 if (session->s_state == CEPH_MDS_SESSION_REJECTED) {
2822 err = -EACCES;
2823 goto out_session;
2824 }
Jeff Layton3bb48b42019-12-02 13:47:57 -05002825 /*
2826 * We cannot queue async requests since the caps and delegated
2827 * inodes are bound to the session. Just return -EJUKEBOX and
2828 * let the caller retry a sync request in that case.
2829 */
2830 if (test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags)) {
2831 err = -EJUKEBOX;
2832 goto out_session;
2833 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002834 if (session->s_state == CEPH_MDS_SESSION_NEW ||
Xiubo Lic4853e92019-12-09 07:47:15 -05002835 session->s_state == CEPH_MDS_SESSION_CLOSING) {
Xiubo Lib682c6d2020-06-30 03:52:18 -04002836 err = __open_session(mdsc, session);
2837 if (err)
2838 goto out_session;
Xiubo Lic4853e92019-12-09 07:47:15 -05002839 /* retry the same mds later */
2840 if (random)
2841 req->r_resend_mds = mds;
2842 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002843 list_add(&req->r_wait, &session->s_waiting);
2844 goto out_session;
2845 }
2846
2847 /* send request */
Sage Weil2f2dc052009-10-06 11:31:09 -07002848 req->r_resend_mds = -1; /* forget any previous mds hint */
2849
2850 if (req->r_request_started == 0) /* note request start time */
2851 req->r_request_started = jiffies;
2852
Xiubo Li9cf54562019-12-05 20:50:21 -05002853 err = __send_request(mdsc, session, req, false);
Sage Weil2f2dc052009-10-06 11:31:09 -07002854
2855out_session:
2856 ceph_put_mds_session(session);
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002857finish:
2858 if (err) {
2859 dout("__do_request early error %d\n", err);
2860 req->r_err = err;
2861 complete_request(mdsc, req);
2862 __unregister_request(mdsc, req);
2863 }
Chengguang Xud5548492018-07-28 16:30:48 +08002864 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07002865}
2866
2867/*
2868 * called under mdsc->mutex
2869 */
2870static void __wake_requests(struct ceph_mds_client *mdsc,
2871 struct list_head *head)
2872{
Yan, Zhenged75ec22012-11-19 10:49:06 +08002873 struct ceph_mds_request *req;
2874 LIST_HEAD(tmp_list);
Sage Weil2f2dc052009-10-06 11:31:09 -07002875
Yan, Zhenged75ec22012-11-19 10:49:06 +08002876 list_splice_init(head, &tmp_list);
2877
2878 while (!list_empty(&tmp_list)) {
2879 req = list_entry(tmp_list.next,
2880 struct ceph_mds_request, r_wait);
Sage Weil2f2dc052009-10-06 11:31:09 -07002881 list_del_init(&req->r_wait);
Sage Weil7971bd92013-05-01 21:15:58 -07002882 dout(" wake request %p tid %llu\n", req, req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002883 __do_request(mdsc, req);
2884 }
2885}
2886
2887/*
2888 * Wake up threads with requests pending for @mds, so that they can
Sage Weil29790f22010-03-18 14:45:05 -07002889 * resubmit their requests to a possibly different mds.
Sage Weil2f2dc052009-10-06 11:31:09 -07002890 */
Sage Weil29790f22010-03-18 14:45:05 -07002891static void kick_requests(struct ceph_mds_client *mdsc, int mds)
Sage Weil2f2dc052009-10-06 11:31:09 -07002892{
Sage Weil44ca18f2010-02-15 12:08:46 -08002893 struct ceph_mds_request *req;
Yan, Zheng282c1052014-07-30 10:12:47 +08002894 struct rb_node *p = rb_first(&mdsc->request_tree);
Sage Weil2f2dc052009-10-06 11:31:09 -07002895
2896 dout("kick_requests mds%d\n", mds);
Yan, Zheng282c1052014-07-30 10:12:47 +08002897 while (p) {
Sage Weil44ca18f2010-02-15 12:08:46 -08002898 req = rb_entry(p, struct ceph_mds_request, r_node);
Yan, Zheng282c1052014-07-30 10:12:47 +08002899 p = rb_next(p);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002900 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Sage Weil44ca18f2010-02-15 12:08:46 -08002901 continue;
Yan, Zheng3de22be2015-02-04 14:26:22 +08002902 if (req->r_attempts > 0)
2903 continue; /* only new requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08002904 if (req->r_session &&
2905 req->r_session->s_mds == mds) {
2906 dout(" kicking tid %llu\n", req->r_tid);
Yan, Zheng03974e82014-09-11 14:28:56 +08002907 list_del_init(&req->r_wait);
Sage Weil44ca18f2010-02-15 12:08:46 -08002908 __do_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002909 }
2910 }
2911}
2912
Jeff Layton86bda532019-04-02 09:24:36 -04002913int ceph_mdsc_submit_request(struct ceph_mds_client *mdsc, struct inode *dir,
Sage Weil2f2dc052009-10-06 11:31:09 -07002914 struct ceph_mds_request *req)
2915{
Jeff Layton891f3f52020-01-14 15:06:40 -05002916 int err = 0;
Jeff Layton86bda532019-04-02 09:24:36 -04002917
2918 /* take CAP_PIN refs for r_inode, r_parent, r_old_dentry */
2919 if (req->r_inode)
2920 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
Jeff Layton9c1c2b32019-04-03 13:16:01 -04002921 if (req->r_parent) {
Yan, Zheng719a2512020-03-05 20:21:00 +08002922 struct ceph_inode_info *ci = ceph_inode(req->r_parent);
2923 int fmode = (req->r_op & CEPH_MDS_OP_WRITE) ?
2924 CEPH_FILE_MODE_WR : CEPH_FILE_MODE_RD;
2925 spin_lock(&ci->i_ceph_lock);
2926 ceph_take_cap_refs(ci, CEPH_CAP_PIN, false);
2927 __ceph_touch_fmode(ci, mdsc, fmode);
2928 spin_unlock(&ci->i_ceph_lock);
Jeff Layton9c1c2b32019-04-03 13:16:01 -04002929 ihold(req->r_parent);
2930 }
Jeff Layton86bda532019-04-02 09:24:36 -04002931 if (req->r_old_dentry_dir)
2932 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
2933 CEPH_CAP_PIN);
2934
Jeff Layton891f3f52020-01-14 15:06:40 -05002935 if (req->r_inode) {
2936 err = ceph_wait_on_async_create(req->r_inode);
2937 if (err) {
2938 dout("%s: wait for async create returned: %d\n",
2939 __func__, err);
2940 return err;
2941 }
2942 }
2943
2944 if (!err && req->r_old_inode) {
2945 err = ceph_wait_on_async_create(req->r_old_inode);
2946 if (err) {
2947 dout("%s: wait for async create returned: %d\n",
2948 __func__, err);
2949 return err;
2950 }
2951 }
2952
Jeff Layton86bda532019-04-02 09:24:36 -04002953 dout("submit_request on %p for inode %p\n", req, dir);
Sage Weil2f2dc052009-10-06 11:31:09 -07002954 mutex_lock(&mdsc->mutex);
Jeff Layton86bda532019-04-02 09:24:36 -04002955 __register_request(mdsc, req, dir);
Sage Weil2f2dc052009-10-06 11:31:09 -07002956 __do_request(mdsc, req);
Jeff Layton86bda532019-04-02 09:24:36 -04002957 err = req->r_err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002958 mutex_unlock(&mdsc->mutex);
Jeff Layton86bda532019-04-02 09:24:36 -04002959 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002960}
2961
Jeff Layton8340f222019-04-02 12:34:38 -04002962static int ceph_mdsc_wait_request(struct ceph_mds_client *mdsc,
2963 struct ceph_mds_request *req)
Sage Weil2f2dc052009-10-06 11:31:09 -07002964{
2965 int err;
2966
Sage Weile1518c72010-05-13 11:19:06 -07002967 /* wait */
Sage Weile1518c72010-05-13 11:19:06 -07002968 dout("do_request waiting\n");
Ilya Dryomov5be73032015-05-19 12:05:38 +03002969 if (!req->r_timeout && req->r_wait_for_completion) {
Yan, Zheng9280be22014-10-14 10:33:35 +08002970 err = req->r_wait_for_completion(mdsc, req);
Sage Weile1518c72010-05-13 11:19:06 -07002971 } else {
Ilya Dryomov5be73032015-05-19 12:05:38 +03002972 long timeleft = wait_for_completion_killable_timeout(
2973 &req->r_completion,
2974 ceph_timeout_jiffies(req->r_timeout));
2975 if (timeleft > 0)
2976 err = 0;
2977 else if (!timeleft)
Xiubo Li8ccf7fc2020-02-23 22:23:11 -05002978 err = -ETIMEDOUT; /* timed out */
Ilya Dryomov5be73032015-05-19 12:05:38 +03002979 else
2980 err = timeleft; /* killed */
Sage Weile1518c72010-05-13 11:19:06 -07002981 }
2982 dout("do_request waited, got %d\n", err);
2983 mutex_lock(&mdsc->mutex);
2984
2985 /* only abort if we didn't race with a real reply */
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002986 if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
Sage Weile1518c72010-05-13 11:19:06 -07002987 err = le32_to_cpu(req->r_reply_info.head->result);
2988 } else if (err < 0) {
2989 dout("aborted request %lld with %d\n", req->r_tid, err);
Sage Weilb4556392010-05-13 12:01:13 -07002990
2991 /*
2992 * ensure we aren't running concurrently with
2993 * ceph_fill_trace or ceph_readdir_prepopulate, which
2994 * rely on locks (dir mutex) held by our caller.
2995 */
2996 mutex_lock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07002997 req->r_err = err;
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002998 set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
Sage Weilb4556392010-05-13 12:01:13 -07002999 mutex_unlock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07003000
Jeff Layton3dd69aa2017-01-31 10:28:26 -05003001 if (req->r_parent &&
Sage Weil167c9e32010-05-14 10:02:57 -07003002 (req->r_op & CEPH_MDS_OP_WRITE))
3003 ceph_invalidate_dir_request(req);
Sage Weile1518c72010-05-13 11:19:06 -07003004 } else {
3005 err = req->r_err;
3006 }
3007
Sage Weile1518c72010-05-13 11:19:06 -07003008 mutex_unlock(&mdsc->mutex);
Jeff Layton8340f222019-04-02 12:34:38 -04003009 return err;
3010}
3011
3012/*
3013 * Synchrously perform an mds request. Take care of all of the
3014 * session setup, forwarding, retry details.
3015 */
3016int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
3017 struct inode *dir,
3018 struct ceph_mds_request *req)
3019{
3020 int err;
3021
3022 dout("do_request on %p\n", req);
3023
3024 /* issue */
3025 err = ceph_mdsc_submit_request(mdsc, dir, req);
3026 if (!err)
3027 err = ceph_mdsc_wait_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07003028 dout("do_request %p done, result %d\n", req, err);
3029 return err;
3030}
3031
3032/*
Yan, Zheng2f276c52013-03-13 19:44:32 +08003033 * Invalidate dir's completeness, dentry lease state on an aborted MDS
Sage Weil167c9e32010-05-14 10:02:57 -07003034 * namespace request.
3035 */
3036void ceph_invalidate_dir_request(struct ceph_mds_request *req)
3037{
Yan, Zheng8d8f3712017-11-24 11:51:32 +08003038 struct inode *dir = req->r_parent;
3039 struct inode *old_dir = req->r_old_dentry_dir;
Sage Weil167c9e32010-05-14 10:02:57 -07003040
Yan, Zheng8d8f3712017-11-24 11:51:32 +08003041 dout("invalidate_dir_request %p %p (complete, lease(s))\n", dir, old_dir);
Sage Weil167c9e32010-05-14 10:02:57 -07003042
Yan, Zheng8d8f3712017-11-24 11:51:32 +08003043 ceph_dir_clear_complete(dir);
3044 if (old_dir)
3045 ceph_dir_clear_complete(old_dir);
Sage Weil167c9e32010-05-14 10:02:57 -07003046 if (req->r_dentry)
3047 ceph_invalidate_dentry_lease(req->r_dentry);
3048 if (req->r_old_dentry)
3049 ceph_invalidate_dentry_lease(req->r_old_dentry);
3050}
3051
3052/*
Sage Weil2f2dc052009-10-06 11:31:09 -07003053 * Handle mds reply.
3054 *
3055 * We take the session mutex and parse and process the reply immediately.
3056 * This preserves the logical ordering of replies, capabilities, etc., sent
3057 * by the MDS as they are applied to our local cache.
3058 */
3059static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
3060{
3061 struct ceph_mds_client *mdsc = session->s_mdsc;
3062 struct ceph_mds_request *req;
3063 struct ceph_mds_reply_head *head = msg->front.iov_base;
3064 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
Yan, Zheng982d6012014-12-23 15:30:54 +08003065 struct ceph_snap_realm *realm;
Sage Weil2f2dc052009-10-06 11:31:09 -07003066 u64 tid;
3067 int err, result;
Sage Weil2600d2d2010-02-22 15:12:16 -08003068 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07003069
Sage Weil2f2dc052009-10-06 11:31:09 -07003070 if (msg->front.iov_len < sizeof(*head)) {
3071 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08003072 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003073 return;
3074 }
3075
3076 /* get request, session */
Sage Weil6df058c2009-12-22 11:24:33 -08003077 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07003078 mutex_lock(&mdsc->mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02003079 req = lookup_get_request(mdsc, tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07003080 if (!req) {
3081 dout("handle_reply on unknown tid %llu\n", tid);
3082 mutex_unlock(&mdsc->mutex);
3083 return;
3084 }
3085 dout("handle_reply %p\n", req);
Sage Weil2f2dc052009-10-06 11:31:09 -07003086
3087 /* correct session? */
Sage Weild96d6042010-03-20 20:50:58 -07003088 if (req->r_session != session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003089 pr_err("mdsc_handle_reply got %llu on session mds%d"
3090 " not mds%d\n", tid, session->s_mds,
3091 req->r_session ? req->r_session->s_mds : -1);
3092 mutex_unlock(&mdsc->mutex);
3093 goto out;
3094 }
3095
3096 /* dup? */
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003097 if ((test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags) && !head->safe) ||
3098 (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags) && head->safe)) {
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07003099 pr_warn("got a dup %s reply on %llu from mds%d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07003100 head->safe ? "safe" : "unsafe", tid, mds);
3101 mutex_unlock(&mdsc->mutex);
3102 goto out;
3103 }
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003104 if (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags)) {
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07003105 pr_warn("got unsafe after safe on %llu from mds%d\n",
Sage Weil85792d0d2010-05-13 09:06:02 -07003106 tid, mds);
3107 mutex_unlock(&mdsc->mutex);
3108 goto out;
3109 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003110
3111 result = le32_to_cpu(head->result);
3112
3113 /*
Greg Farnume55b71f2010-06-22 15:58:01 -07003114 * Handle an ESTALE
3115 * if we're not talking to the authority, send to them
3116 * if the authority has changed while we weren't looking,
3117 * send to new authority
3118 * Otherwise we just have to return an ESTALE
Sage Weil2f2dc052009-10-06 11:31:09 -07003119 */
3120 if (result == -ESTALE) {
Chengguang Xu4c069a52018-01-30 16:29:17 +08003121 dout("got ESTALE on request %llu\n", req->r_tid);
Yan, Zheng51da8e82014-07-14 10:48:29 +08003122 req->r_resend_mds = -1;
Yan, Zhengca18bed2013-11-22 14:21:44 +08003123 if (req->r_direct_mode != USE_AUTH_MDS) {
Chengguang Xu4c069a52018-01-30 16:29:17 +08003124 dout("not using auth, setting for that now\n");
Greg Farnume55b71f2010-06-22 15:58:01 -07003125 req->r_direct_mode = USE_AUTH_MDS;
Sage Weil2f2dc052009-10-06 11:31:09 -07003126 __do_request(mdsc, req);
3127 mutex_unlock(&mdsc->mutex);
3128 goto out;
Greg Farnume55b71f2010-06-22 15:58:01 -07003129 } else {
Xiubo Lic4853e92019-12-09 07:47:15 -05003130 int mds = __choose_mds(mdsc, req, NULL);
Yan, Zhengca18bed2013-11-22 14:21:44 +08003131 if (mds >= 0 && mds != req->r_session->s_mds) {
Chengguang Xu4c069a52018-01-30 16:29:17 +08003132 dout("but auth changed, so resending\n");
Greg Farnume55b71f2010-06-22 15:58:01 -07003133 __do_request(mdsc, req);
3134 mutex_unlock(&mdsc->mutex);
3135 goto out;
3136 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003137 }
Chengguang Xu4c069a52018-01-30 16:29:17 +08003138 dout("have to return ESTALE on request %llu\n", req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07003139 }
3140
Greg Farnume55b71f2010-06-22 15:58:01 -07003141
Sage Weil2f2dc052009-10-06 11:31:09 -07003142 if (head->safe) {
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003143 set_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags);
Sage Weil2f2dc052009-10-06 11:31:09 -07003144 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07003145
Xiubo Li07edc052019-12-04 01:27:18 -05003146 /* last request during umount? */
3147 if (mdsc->stopping && !__get_oldest_req(mdsc))
3148 complete_all(&mdsc->safe_umount_waiters);
3149
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003150 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003151 /*
3152 * We already handled the unsafe response, now do the
3153 * cleanup. No need to examine the response; the MDS
3154 * doesn't include any result info in the safe
3155 * response. And even if it did, there is nothing
3156 * useful we could do with a revised return value.
3157 */
3158 dout("got safe reply %llu, mds%d\n", tid, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003159
Sage Weil2f2dc052009-10-06 11:31:09 -07003160 mutex_unlock(&mdsc->mutex);
3161 goto out;
3162 }
Sage Weile1518c72010-05-13 11:19:06 -07003163 } else {
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003164 set_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags);
Sage Weil2f2dc052009-10-06 11:31:09 -07003165 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
3166 }
3167
3168 dout("handle_reply tid %lld result %d\n", tid, result);
3169 rinfo = &req->r_reply_info;
Yan, Zhengb37fe1f2019-01-09 10:10:17 +08003170 if (test_bit(CEPHFS_FEATURE_REPLY_ENCODING, &session->s_features))
Jeff Laytond4846482019-11-15 11:51:55 -05003171 err = parse_reply_info(session, msg, rinfo, (u64)-1);
Yan, Zhengb37fe1f2019-01-09 10:10:17 +08003172 else
Jeff Laytond4846482019-11-15 11:51:55 -05003173 err = parse_reply_info(session, msg, rinfo, session->s_con.peer_features);
Sage Weil2f2dc052009-10-06 11:31:09 -07003174 mutex_unlock(&mdsc->mutex);
3175
3176 mutex_lock(&session->s_mutex);
3177 if (err < 0) {
Herb Shiu25933ab2010-12-01 14:14:38 -08003178 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
Sage Weil9ec7cab2009-12-14 15:13:47 -08003179 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003180 goto out_err;
3181 }
3182
3183 /* snap trace */
Yan, Zheng982d6012014-12-23 15:30:54 +08003184 realm = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07003185 if (rinfo->snapblob_len) {
3186 down_write(&mdsc->snap_rwsem);
3187 ceph_update_snap_trace(mdsc, rinfo->snapblob,
Yan, Zheng982d6012014-12-23 15:30:54 +08003188 rinfo->snapblob + rinfo->snapblob_len,
3189 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP,
3190 &realm);
Sage Weil2f2dc052009-10-06 11:31:09 -07003191 downgrade_write(&mdsc->snap_rwsem);
3192 } else {
3193 down_read(&mdsc->snap_rwsem);
3194 }
3195
3196 /* insert trace into our cache */
Sage Weilb4556392010-05-13 12:01:13 -07003197 mutex_lock(&req->r_fill_mutex);
Yan, Zheng315f2402016-03-07 10:34:50 +08003198 current->journal_info = req;
Jeff Laytonf5a03b02017-01-31 11:06:13 -05003199 err = ceph_fill_trace(mdsc->fsc->sb, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07003200 if (err == 0) {
Sam Lang6e8575f2012-12-28 09:56:46 -08003201 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
Yan, Zheng81c6aea2013-09-18 09:44:13 +08003202 req->r_op == CEPH_MDS_OP_LSSNAP))
Sage Weil2f2dc052009-10-06 11:31:09 -07003203 ceph_readdir_prepopulate(req, req->r_session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003204 }
Yan, Zheng315f2402016-03-07 10:34:50 +08003205 current->journal_info = NULL;
Sage Weilb4556392010-05-13 12:01:13 -07003206 mutex_unlock(&req->r_fill_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003207
3208 up_read(&mdsc->snap_rwsem);
Yan, Zheng982d6012014-12-23 15:30:54 +08003209 if (realm)
3210 ceph_put_snap_realm(mdsc, realm);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08003211
Yan, Zhengfe330322019-02-01 14:57:15 +08003212 if (err == 0) {
3213 if (req->r_target_inode &&
3214 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
3215 struct ceph_inode_info *ci =
3216 ceph_inode(req->r_target_inode);
3217 spin_lock(&ci->i_unsafe_lock);
3218 list_add_tail(&req->r_unsafe_target_item,
3219 &ci->i_unsafe_iops);
3220 spin_unlock(&ci->i_unsafe_lock);
3221 }
3222
3223 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08003224 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003225out_err:
Sage Weile1518c72010-05-13 11:19:06 -07003226 mutex_lock(&mdsc->mutex);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003227 if (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
Sage Weile1518c72010-05-13 11:19:06 -07003228 if (err) {
3229 req->r_err = err;
3230 } else {
Jianpeng Ma5fdb1382015-08-18 10:30:38 +08003231 req->r_reply = ceph_msg_get(msg);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003232 set_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags);
Sage Weile1518c72010-05-13 11:19:06 -07003233 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003234 } else {
Sage Weile1518c72010-05-13 11:19:06 -07003235 dout("reply arrived after request %lld was aborted\n", tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07003236 }
Sage Weile1518c72010-05-13 11:19:06 -07003237 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003238
Sage Weil2f2dc052009-10-06 11:31:09 -07003239 mutex_unlock(&session->s_mutex);
3240
3241 /* kick calling process */
3242 complete_request(mdsc, req);
Xiubo Li70c94822020-03-19 23:45:02 -04003243
3244 ceph_update_metadata_latency(&mdsc->metric, req->r_start_latency,
3245 req->r_end_latency, err);
Sage Weil2f2dc052009-10-06 11:31:09 -07003246out:
3247 ceph_mdsc_put_request(req);
3248 return;
3249}
3250
3251
3252
3253/*
3254 * handle mds notification that our request has been forwarded.
3255 */
Sage Weil2600d2d2010-02-22 15:12:16 -08003256static void handle_forward(struct ceph_mds_client *mdsc,
3257 struct ceph_mds_session *session,
3258 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07003259{
3260 struct ceph_mds_request *req;
Sage Weila1ea7872010-02-23 14:02:44 -08003261 u64 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07003262 u32 next_mds;
3263 u32 fwd_seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003264 int err = -EINVAL;
3265 void *p = msg->front.iov_base;
3266 void *end = p + msg->front.iov_len;
Sage Weil2f2dc052009-10-06 11:31:09 -07003267
Sage Weila1ea7872010-02-23 14:02:44 -08003268 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07003269 next_mds = ceph_decode_32(&p);
3270 fwd_seq = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07003271
3272 mutex_lock(&mdsc->mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02003273 req = lookup_get_request(mdsc, tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07003274 if (!req) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07003275 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003276 goto out; /* dup reply? */
3277 }
3278
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003279 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07003280 dout("forward tid %llu aborted, unregistering\n", tid);
3281 __unregister_request(mdsc, req);
3282 } else if (fwd_seq <= req->r_num_fwd) {
3283 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07003284 tid, next_mds, req->r_num_fwd, fwd_seq);
3285 } else {
3286 /* resend. forward race not possible; mds would drop */
Sage Weil2a8e5e32010-05-28 16:43:16 -07003287 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
3288 BUG_ON(req->r_err);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003289 BUG_ON(test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags));
Yan, Zheng3de22be2015-02-04 14:26:22 +08003290 req->r_attempts = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003291 req->r_num_fwd = fwd_seq;
3292 req->r_resend_mds = next_mds;
3293 put_request_session(req);
3294 __do_request(mdsc, req);
3295 }
3296 ceph_mdsc_put_request(req);
3297out:
3298 mutex_unlock(&mdsc->mutex);
3299 return;
3300
3301bad:
3302 pr_err("mdsc_handle_forward decode error err=%d\n", err);
3303}
3304
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003305static int __decode_session_metadata(void **p, void *end,
3306 bool *blacklisted)
Yan, Zheng84bf3952018-12-21 17:41:39 +08003307{
3308 /* map<string,string> */
3309 u32 n;
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003310 bool err_str;
Yan, Zheng84bf3952018-12-21 17:41:39 +08003311 ceph_decode_32_safe(p, end, n, bad);
3312 while (n-- > 0) {
3313 u32 len;
3314 ceph_decode_32_safe(p, end, len, bad);
3315 ceph_decode_need(p, end, len, bad);
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003316 err_str = !strncmp(*p, "error_string", len);
Yan, Zheng84bf3952018-12-21 17:41:39 +08003317 *p += len;
3318 ceph_decode_32_safe(p, end, len, bad);
3319 ceph_decode_need(p, end, len, bad);
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003320 if (err_str && strnstr(*p, "blacklisted", len))
3321 *blacklisted = true;
Yan, Zheng84bf3952018-12-21 17:41:39 +08003322 *p += len;
3323 }
3324 return 0;
3325bad:
3326 return -1;
3327}
3328
Sage Weil2f2dc052009-10-06 11:31:09 -07003329/*
3330 * handle a mds session control message
3331 */
3332static void handle_session(struct ceph_mds_session *session,
3333 struct ceph_msg *msg)
3334{
3335 struct ceph_mds_client *mdsc = session->s_mdsc;
Yan, Zheng84bf3952018-12-21 17:41:39 +08003336 int mds = session->s_mds;
3337 int msg_version = le16_to_cpu(msg->hdr.version);
3338 void *p = msg->front.iov_base;
3339 void *end = p + msg->front.iov_len;
3340 struct ceph_mds_session_head *h;
Sage Weil2f2dc052009-10-06 11:31:09 -07003341 u32 op;
Jeff Layton0fa82632020-04-28 08:10:22 -04003342 u64 seq, features = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003343 int wake = 0;
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003344 bool blacklisted = false;
Sage Weil2f2dc052009-10-06 11:31:09 -07003345
Sage Weil2f2dc052009-10-06 11:31:09 -07003346 /* decode */
Yan, Zheng84bf3952018-12-21 17:41:39 +08003347 ceph_decode_need(&p, end, sizeof(*h), bad);
3348 h = p;
3349 p += sizeof(*h);
3350
Sage Weil2f2dc052009-10-06 11:31:09 -07003351 op = le32_to_cpu(h->op);
3352 seq = le64_to_cpu(h->seq);
3353
Yan, Zheng84bf3952018-12-21 17:41:39 +08003354 if (msg_version >= 3) {
3355 u32 len;
3356 /* version >= 2, metadata */
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003357 if (__decode_session_metadata(&p, end, &blacklisted) < 0)
Yan, Zheng84bf3952018-12-21 17:41:39 +08003358 goto bad;
3359 /* version >= 3, feature bits */
3360 ceph_decode_32_safe(&p, end, len, bad);
Jeff Layton0fa82632020-04-28 08:10:22 -04003361 ceph_decode_64_safe(&p, end, features, bad);
3362 p += len - sizeof(features);
Yan, Zheng84bf3952018-12-21 17:41:39 +08003363 }
3364
Sage Weil2f2dc052009-10-06 11:31:09 -07003365 mutex_lock(&mdsc->mutex);
Yan, Zheng0a07fc82017-03-29 15:30:24 +08003366 if (op == CEPH_SESSION_CLOSE) {
Xiubo Li5b3248c2019-12-19 19:44:09 -05003367 ceph_get_mds_session(session);
Sage Weil2600d2d2010-02-22 15:12:16 -08003368 __unregister_session(mdsc, session);
Yan, Zheng0a07fc82017-03-29 15:30:24 +08003369 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003370 /* FIXME: this ttl calculation is generous */
3371 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
3372 mutex_unlock(&mdsc->mutex);
3373
3374 mutex_lock(&session->s_mutex);
3375
3376 dout("handle_session mds%d %s %p state %s seq %llu\n",
3377 mds, ceph_session_op_name(op), session,
John Spraya687eca2014-09-19 13:51:08 +01003378 ceph_session_state_name(session->s_state), seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003379
3380 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
3381 session->s_state = CEPH_MDS_SESSION_OPEN;
3382 pr_info("mds%d came back\n", session->s_mds);
3383 }
3384
3385 switch (op) {
3386 case CEPH_SESSION_OPEN:
Sage Weil29790f22010-03-18 14:45:05 -07003387 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
3388 pr_info("mds%d reconnect success\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003389 session->s_state = CEPH_MDS_SESSION_OPEN;
Yan, Zheng84bf3952018-12-21 17:41:39 +08003390 session->s_features = features;
Sage Weil2f2dc052009-10-06 11:31:09 -07003391 renewed_caps(mdsc, session, 0);
Xiubo Li18f473b2020-07-16 10:05:57 -04003392 if (test_bit(CEPHFS_FEATURE_METRIC_COLLECT, &session->s_features))
3393 metric_schedule_delayed(&mdsc->metric);
Sage Weil2f2dc052009-10-06 11:31:09 -07003394 wake = 1;
3395 if (mdsc->stopping)
3396 __close_session(mdsc, session);
3397 break;
3398
3399 case CEPH_SESSION_RENEWCAPS:
3400 if (session->s_renew_seq == seq)
3401 renewed_caps(mdsc, session, 1);
3402 break;
3403
3404 case CEPH_SESSION_CLOSE:
Sage Weil29790f22010-03-18 14:45:05 -07003405 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
3406 pr_info("mds%d reconnect denied\n", session->s_mds);
Xiubo Li4d681c22019-12-05 22:35:51 -05003407 session->s_state = CEPH_MDS_SESSION_CLOSED;
Yan, Zheng1c841a92015-03-24 20:15:36 +08003408 cleanup_session_requests(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003409 remove_session_caps(session);
Yan, Zheng656e4382014-09-11 14:25:18 +08003410 wake = 2; /* for good measure */
Sage Weilf3c60c52010-08-11 14:51:23 -07003411 wake_up_all(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003412 break;
3413
3414 case CEPH_SESSION_STALE:
3415 pr_info("mds%d caps went stale, renewing\n",
3416 session->s_mds);
Alex Elderd8fb02a2012-01-12 17:48:10 -08003417 spin_lock(&session->s_gen_ttl_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07003418 session->s_cap_gen++;
Alex Elder1ce208a2012-01-12 17:48:11 -08003419 session->s_cap_ttl = jiffies - 1;
Alex Elderd8fb02a2012-01-12 17:48:10 -08003420 spin_unlock(&session->s_gen_ttl_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07003421 send_renew_caps(mdsc, session);
3422 break;
3423
3424 case CEPH_SESSION_RECALL_STATE:
Zhi Zhange30ee582018-01-24 21:24:33 +08003425 ceph_trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
Sage Weil2f2dc052009-10-06 11:31:09 -07003426 break;
3427
Yan, Zheng186e4f72013-11-22 14:48:37 +08003428 case CEPH_SESSION_FLUSHMSG:
3429 send_flushmsg_ack(mdsc, session, seq);
3430 break;
3431
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08003432 case CEPH_SESSION_FORCE_RO:
3433 dout("force_session_readonly %p\n", session);
3434 spin_lock(&session->s_cap_lock);
3435 session->s_readonly = true;
3436 spin_unlock(&session->s_cap_lock);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08003437 wake_up_session_caps(session, FORCE_RO);
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08003438 break;
3439
Yan, Zhengfcff4152016-09-14 16:39:51 +08003440 case CEPH_SESSION_REJECT:
3441 WARN_ON(session->s_state != CEPH_MDS_SESSION_OPENING);
3442 pr_info("mds%d rejected session\n", session->s_mds);
3443 session->s_state = CEPH_MDS_SESSION_REJECTED;
3444 cleanup_session_requests(mdsc, session);
3445 remove_session_caps(session);
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003446 if (blacklisted)
3447 mdsc->fsc->blacklisted = true;
Yan, Zhengfcff4152016-09-14 16:39:51 +08003448 wake = 2; /* for good measure */
3449 break;
3450
Sage Weil2f2dc052009-10-06 11:31:09 -07003451 default:
3452 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
3453 WARN_ON(1);
3454 }
3455
3456 mutex_unlock(&session->s_mutex);
3457 if (wake) {
3458 mutex_lock(&mdsc->mutex);
3459 __wake_requests(mdsc, &session->s_waiting);
Yan, Zheng656e4382014-09-11 14:25:18 +08003460 if (wake == 2)
3461 kick_requests(mdsc, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003462 mutex_unlock(&mdsc->mutex);
3463 }
Yan, Zheng0a07fc82017-03-29 15:30:24 +08003464 if (op == CEPH_SESSION_CLOSE)
3465 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003466 return;
3467
3468bad:
3469 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
3470 (int)msg->front.iov_len);
Sage Weil9ec7cab2009-12-14 15:13:47 -08003471 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003472 return;
3473}
3474
Jeff Laytona25949b2020-02-18 14:12:45 -05003475void ceph_mdsc_release_dir_caps(struct ceph_mds_request *req)
3476{
3477 int dcaps;
3478
3479 dcaps = xchg(&req->r_dir_caps, 0);
3480 if (dcaps) {
3481 dout("releasing r_dir_caps=%s\n", ceph_cap_string(dcaps));
3482 ceph_put_cap_refs(ceph_inode(req->r_parent), dcaps);
3483 }
3484}
3485
Xiubo Lie64f44a2020-05-27 09:09:27 -04003486void ceph_mdsc_release_dir_caps_no_check(struct ceph_mds_request *req)
3487{
3488 int dcaps;
3489
3490 dcaps = xchg(&req->r_dir_caps, 0);
3491 if (dcaps) {
3492 dout("releasing r_dir_caps=%s\n", ceph_cap_string(dcaps));
3493 ceph_put_cap_refs_no_check_caps(ceph_inode(req->r_parent),
3494 dcaps);
3495 }
3496}
3497
Sage Weil2f2dc052009-10-06 11:31:09 -07003498/*
3499 * called under session->mutex.
3500 */
3501static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
3502 struct ceph_mds_session *session)
3503{
3504 struct ceph_mds_request *req, *nreq;
Yan, Zheng3de22be2015-02-04 14:26:22 +08003505 struct rb_node *p;
Sage Weil2f2dc052009-10-06 11:31:09 -07003506
3507 dout("replay_unsafe_requests mds%d\n", session->s_mds);
3508
3509 mutex_lock(&mdsc->mutex);
Xiubo Li9cf54562019-12-05 20:50:21 -05003510 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item)
3511 __send_request(mdsc, session, req, true);
Yan, Zheng3de22be2015-02-04 14:26:22 +08003512
3513 /*
3514 * also re-send old requests when MDS enters reconnect stage. So that MDS
3515 * can process completed request in clientreplay stage.
3516 */
3517 p = rb_first(&mdsc->request_tree);
3518 while (p) {
3519 req = rb_entry(p, struct ceph_mds_request, r_node);
3520 p = rb_next(p);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003521 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Yan, Zheng3de22be2015-02-04 14:26:22 +08003522 continue;
3523 if (req->r_attempts == 0)
3524 continue; /* only old requests */
Jeff Laytona25949b2020-02-18 14:12:45 -05003525 if (!req->r_session)
3526 continue;
3527 if (req->r_session->s_mds != session->s_mds)
3528 continue;
3529
Xiubo Lie64f44a2020-05-27 09:09:27 -04003530 ceph_mdsc_release_dir_caps_no_check(req);
Jeff Laytona25949b2020-02-18 14:12:45 -05003531
3532 __send_request(mdsc, session, req, true);
Yan, Zheng3de22be2015-02-04 14:26:22 +08003533 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003534 mutex_unlock(&mdsc->mutex);
3535}
3536
Yan, Zheng81c5a142019-01-01 16:28:33 +08003537static int send_reconnect_partial(struct ceph_reconnect_state *recon_state)
3538{
3539 struct ceph_msg *reply;
3540 struct ceph_pagelist *_pagelist;
3541 struct page *page;
3542 __le32 *addr;
3543 int err = -ENOMEM;
3544
3545 if (!recon_state->allow_multi)
3546 return -ENOSPC;
3547
3548 /* can't handle message that contains both caps and realm */
3549 BUG_ON(!recon_state->nr_caps == !recon_state->nr_realms);
3550
3551 /* pre-allocate new pagelist */
3552 _pagelist = ceph_pagelist_alloc(GFP_NOFS);
3553 if (!_pagelist)
3554 return -ENOMEM;
3555
3556 reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false);
3557 if (!reply)
3558 goto fail_msg;
3559
3560 /* placeholder for nr_caps */
3561 err = ceph_pagelist_encode_32(_pagelist, 0);
3562 if (err < 0)
3563 goto fail;
3564
3565 if (recon_state->nr_caps) {
3566 /* currently encoding caps */
3567 err = ceph_pagelist_encode_32(recon_state->pagelist, 0);
3568 if (err)
3569 goto fail;
3570 } else {
3571 /* placeholder for nr_realms (currently encoding relams) */
3572 err = ceph_pagelist_encode_32(_pagelist, 0);
3573 if (err < 0)
3574 goto fail;
3575 }
3576
3577 err = ceph_pagelist_encode_8(recon_state->pagelist, 1);
3578 if (err)
3579 goto fail;
3580
3581 page = list_first_entry(&recon_state->pagelist->head, struct page, lru);
3582 addr = kmap_atomic(page);
3583 if (recon_state->nr_caps) {
3584 /* currently encoding caps */
3585 *addr = cpu_to_le32(recon_state->nr_caps);
3586 } else {
3587 /* currently encoding relams */
3588 *(addr + 1) = cpu_to_le32(recon_state->nr_realms);
3589 }
3590 kunmap_atomic(addr);
3591
3592 reply->hdr.version = cpu_to_le16(5);
3593 reply->hdr.compat_version = cpu_to_le16(4);
3594
3595 reply->hdr.data_len = cpu_to_le32(recon_state->pagelist->length);
3596 ceph_msg_data_add_pagelist(reply, recon_state->pagelist);
3597
3598 ceph_con_send(&recon_state->session->s_con, reply);
3599 ceph_pagelist_release(recon_state->pagelist);
3600
3601 recon_state->pagelist = _pagelist;
3602 recon_state->nr_caps = 0;
3603 recon_state->nr_realms = 0;
3604 recon_state->msg_version = 5;
3605 return 0;
3606fail:
3607 ceph_msg_put(reply);
3608fail_msg:
3609 ceph_pagelist_release(_pagelist);
3610 return err;
3611}
3612
Sage Weil2f2dc052009-10-06 11:31:09 -07003613/*
3614 * Encode information about a cap for a reconnect with the MDS.
3615 */
Jeff Laytona25949b2020-02-18 14:12:45 -05003616static int reconnect_caps_cb(struct inode *inode, struct ceph_cap *cap,
Sage Weil2f2dc052009-10-06 11:31:09 -07003617 void *arg)
3618{
Sage Weil20cb34a2010-05-12 15:21:32 -07003619 union {
3620 struct ceph_mds_cap_reconnect v2;
3621 struct ceph_mds_cap_reconnect_v1 v1;
3622 } rec;
Yan, Zhengb3f8d682017-09-11 10:58:55 +08003623 struct ceph_inode_info *ci = cap->ci;
Sage Weil20cb34a2010-05-12 15:21:32 -07003624 struct ceph_reconnect_state *recon_state = arg;
3625 struct ceph_pagelist *pagelist = recon_state->pagelist;
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003626 int err;
Yan, Zheng3469ed02016-07-05 09:32:31 +08003627 u64 snap_follows;
Sage Weil2f2dc052009-10-06 11:31:09 -07003628
Sage Weil2f2dc052009-10-06 11:31:09 -07003629 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
3630 inode, ceph_vinop(inode), cap, cap->cap_id,
3631 ceph_cap_string(cap->issued));
Sage Weil2f2dc052009-10-06 11:31:09 -07003632
Sage Weilbe655592011-11-30 09:47:09 -08003633 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07003634 cap->seq = 0; /* reset cap seq */
3635 cap->issue_seq = 0; /* and issue_seq */
Yan, Zheng667ca052013-05-31 16:25:36 +08003636 cap->mseq = 0; /* and migrate_seq */
Yan, Zheng99a9c272013-09-22 11:08:14 +08003637 cap->cap_gen = cap->session->s_cap_gen;
Sage Weil20cb34a2010-05-12 15:21:32 -07003638
Jeff Laytona25949b2020-02-18 14:12:45 -05003639 /* These are lost when the session goes away */
Jeff Layton785892f2020-01-02 07:11:38 -05003640 if (S_ISDIR(inode->i_mode)) {
3641 if (cap->issued & CEPH_CAP_DIR_CREATE) {
3642 ceph_put_string(rcu_dereference_raw(ci->i_cached_layout.pool_ns));
3643 memset(&ci->i_cached_layout, 0, sizeof(ci->i_cached_layout));
3644 }
Jeff Laytona25949b2020-02-18 14:12:45 -05003645 cap->issued &= ~CEPH_CAP_ANY_DIR_OPS;
Jeff Layton785892f2020-01-02 07:11:38 -05003646 }
Jeff Laytona25949b2020-02-18 14:12:45 -05003647
Yan, Zheng121f22a2016-07-04 22:05:18 +08003648 if (recon_state->msg_version >= 2) {
Sage Weil20cb34a2010-05-12 15:21:32 -07003649 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
3650 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
3651 rec.v2.issued = cpu_to_le32(cap->issued);
3652 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003653 rec.v2.pathbase = 0;
Jeff Laytonec1dff22017-10-31 15:51:14 -04003654 rec.v2.flock_len = (__force __le32)
3655 ((ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) ? 0 : 1);
Sage Weil20cb34a2010-05-12 15:21:32 -07003656 } else {
3657 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
3658 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
3659 rec.v1.issued = cpu_to_le32(cap->issued);
3660 rec.v1.size = cpu_to_le64(inode->i_size);
Arnd Bergmann9bbeab42018-07-13 22:18:36 +02003661 ceph_encode_timespec64(&rec.v1.mtime, &inode->i_mtime);
3662 ceph_encode_timespec64(&rec.v1.atime, &inode->i_atime);
Sage Weil20cb34a2010-05-12 15:21:32 -07003663 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003664 rec.v1.pathbase = 0;
Sage Weil20cb34a2010-05-12 15:21:32 -07003665 }
Yan, Zheng3469ed02016-07-05 09:32:31 +08003666
3667 if (list_empty(&ci->i_cap_snaps)) {
Yan, Zheng92776fd2017-08-16 21:42:39 +08003668 snap_follows = ci->i_head_snapc ? ci->i_head_snapc->seq : 0;
Yan, Zheng3469ed02016-07-05 09:32:31 +08003669 } else {
3670 struct ceph_cap_snap *capsnap =
3671 list_first_entry(&ci->i_cap_snaps,
3672 struct ceph_cap_snap, ci_item);
3673 snap_follows = capsnap->follows;
Sage Weil93cea5b2009-12-23 12:21:51 -08003674 }
Sage Weilbe655592011-11-30 09:47:09 -08003675 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07003676
Yan, Zheng121f22a2016-07-04 22:05:18 +08003677 if (recon_state->msg_version >= 2) {
Greg Farnum40819f62010-08-02 15:34:23 -07003678 int num_fcntl_locks, num_flock_locks;
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003679 struct ceph_filelock *flocks = NULL;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003680 size_t struct_len, total_len = sizeof(u64);
Yan, Zheng121f22a2016-07-04 22:05:18 +08003681 u8 struct_v = 0;
Greg Farnum40819f62010-08-02 15:34:23 -07003682
Jim Schutt39be95e2013-05-15 13:03:35 -05003683encode_again:
Yan, Zhengb3f8d682017-09-11 10:58:55 +08003684 if (rec.v2.flock_len) {
3685 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
3686 } else {
3687 num_fcntl_locks = 0;
3688 num_flock_locks = 0;
3689 }
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003690 if (num_fcntl_locks + num_flock_locks > 0) {
Kees Cook6da2ec52018-06-12 13:55:00 -07003691 flocks = kmalloc_array(num_fcntl_locks + num_flock_locks,
3692 sizeof(struct ceph_filelock),
3693 GFP_NOFS);
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003694 if (!flocks) {
3695 err = -ENOMEM;
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003696 goto out_err;
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003697 }
3698 err = ceph_encode_locks_to_buffer(inode, flocks,
3699 num_fcntl_locks,
3700 num_flock_locks);
3701 if (err) {
3702 kfree(flocks);
3703 flocks = NULL;
3704 if (err == -ENOSPC)
3705 goto encode_again;
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003706 goto out_err;
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003707 }
3708 } else {
Jim Schutt39be95e2013-05-15 13:03:35 -05003709 kfree(flocks);
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003710 flocks = NULL;
Jim Schutt39be95e2013-05-15 13:03:35 -05003711 }
Yan, Zheng121f22a2016-07-04 22:05:18 +08003712
3713 if (recon_state->msg_version >= 3) {
3714 /* version, compat_version and struct_len */
Yan, Zheng81c5a142019-01-01 16:28:33 +08003715 total_len += 2 * sizeof(u8) + sizeof(u32);
Yan, Zheng3469ed02016-07-05 09:32:31 +08003716 struct_v = 2;
Yan, Zheng121f22a2016-07-04 22:05:18 +08003717 }
Jim Schutt39be95e2013-05-15 13:03:35 -05003718 /*
3719 * number of encoded locks is stable, so copy to pagelist
3720 */
Yan, Zheng121f22a2016-07-04 22:05:18 +08003721 struct_len = 2 * sizeof(u32) +
3722 (num_fcntl_locks + num_flock_locks) *
3723 sizeof(struct ceph_filelock);
3724 rec.v2.flock_len = cpu_to_le32(struct_len);
3725
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003726 struct_len += sizeof(u32) + sizeof(rec.v2);
Yan, Zheng121f22a2016-07-04 22:05:18 +08003727
Yan, Zheng3469ed02016-07-05 09:32:31 +08003728 if (struct_v >= 2)
3729 struct_len += sizeof(u64); /* snap_follows */
3730
Yan, Zheng121f22a2016-07-04 22:05:18 +08003731 total_len += struct_len;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003732
3733 if (pagelist->length + total_len > RECONNECT_MAX_SIZE) {
3734 err = send_reconnect_partial(recon_state);
3735 if (err)
3736 goto out_freeflocks;
3737 pagelist = recon_state->pagelist;
Yan, Zheng121f22a2016-07-04 22:05:18 +08003738 }
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003739
Yan, Zheng81c5a142019-01-01 16:28:33 +08003740 err = ceph_pagelist_reserve(pagelist, total_len);
3741 if (err)
3742 goto out_freeflocks;
3743
3744 ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003745 if (recon_state->msg_version >= 3) {
3746 ceph_pagelist_encode_8(pagelist, struct_v);
3747 ceph_pagelist_encode_8(pagelist, 1);
3748 ceph_pagelist_encode_32(pagelist, struct_len);
3749 }
3750 ceph_pagelist_encode_string(pagelist, NULL, 0);
3751 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v2));
3752 ceph_locks_to_pagelist(flocks, pagelist,
3753 num_fcntl_locks, num_flock_locks);
3754 if (struct_v >= 2)
3755 ceph_pagelist_encode_64(pagelist, snap_follows);
Yan, Zheng81c5a142019-01-01 16:28:33 +08003756out_freeflocks:
Jim Schutt39be95e2013-05-15 13:03:35 -05003757 kfree(flocks);
Sage Weil3612abb2010-09-07 15:59:27 -07003758 } else {
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003759 u64 pathbase = 0;
3760 int pathlen = 0;
3761 char *path = NULL;
3762 struct dentry *dentry;
3763
3764 dentry = d_find_alias(inode);
3765 if (dentry) {
3766 path = ceph_mdsc_build_path(dentry,
3767 &pathlen, &pathbase, 0);
3768 dput(dentry);
3769 if (IS_ERR(path)) {
3770 err = PTR_ERR(path);
3771 goto out_err;
3772 }
3773 rec.v1.pathbase = cpu_to_le64(pathbase);
Yan, Zheng121f22a2016-07-04 22:05:18 +08003774 }
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003775
3776 err = ceph_pagelist_reserve(pagelist,
Yan, Zheng81c5a142019-01-01 16:28:33 +08003777 sizeof(u64) + sizeof(u32) +
3778 pathlen + sizeof(rec.v1));
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003779 if (err) {
Yan, Zheng81c5a142019-01-01 16:28:33 +08003780 goto out_freepath;
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003781 }
3782
Yan, Zheng81c5a142019-01-01 16:28:33 +08003783 ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003784 ceph_pagelist_encode_string(pagelist, path, pathlen);
3785 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v1));
Yan, Zheng81c5a142019-01-01 16:28:33 +08003786out_freepath:
Jeff Laytonf77f21b2019-04-29 12:13:14 -04003787 ceph_mdsc_free_path(path, pathlen);
Greg Farnum40819f62010-08-02 15:34:23 -07003788 }
Yan, Zheng44c99752013-09-22 10:28:10 +08003789
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003790out_err:
Yan, Zheng81c5a142019-01-01 16:28:33 +08003791 if (err >= 0)
3792 recon_state->nr_caps++;
3793 return err;
3794}
3795
3796static int encode_snap_realms(struct ceph_mds_client *mdsc,
3797 struct ceph_reconnect_state *recon_state)
3798{
3799 struct rb_node *p;
3800 struct ceph_pagelist *pagelist = recon_state->pagelist;
3801 int err = 0;
3802
3803 if (recon_state->msg_version >= 4) {
3804 err = ceph_pagelist_encode_32(pagelist, mdsc->num_snap_realms);
3805 if (err < 0)
3806 goto fail;
3807 }
3808
3809 /*
3810 * snaprealms. we provide mds with the ino, seq (version), and
3811 * parent for all of our realms. If the mds has any newer info,
3812 * it will tell us.
3813 */
3814 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
3815 struct ceph_snap_realm *realm =
3816 rb_entry(p, struct ceph_snap_realm, node);
3817 struct ceph_mds_snaprealm_reconnect sr_rec;
3818
3819 if (recon_state->msg_version >= 4) {
3820 size_t need = sizeof(u8) * 2 + sizeof(u32) +
3821 sizeof(sr_rec);
3822
3823 if (pagelist->length + need > RECONNECT_MAX_SIZE) {
3824 err = send_reconnect_partial(recon_state);
3825 if (err)
3826 goto fail;
3827 pagelist = recon_state->pagelist;
3828 }
3829
3830 err = ceph_pagelist_reserve(pagelist, need);
3831 if (err)
3832 goto fail;
3833
3834 ceph_pagelist_encode_8(pagelist, 1);
3835 ceph_pagelist_encode_8(pagelist, 1);
3836 ceph_pagelist_encode_32(pagelist, sizeof(sr_rec));
3837 }
3838
3839 dout(" adding snap realm %llx seq %lld parent %llx\n",
3840 realm->ino, realm->seq, realm->parent_ino);
3841 sr_rec.ino = cpu_to_le64(realm->ino);
3842 sr_rec.seq = cpu_to_le64(realm->seq);
3843 sr_rec.parent = cpu_to_le64(realm->parent_ino);
3844
3845 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
3846 if (err)
3847 goto fail;
3848
3849 recon_state->nr_realms++;
3850 }
3851fail:
Sage Weil93cea5b2009-12-23 12:21:51 -08003852 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07003853}
3854
3855
3856/*
3857 * If an MDS fails and recovers, clients need to reconnect in order to
3858 * reestablish shared state. This includes all caps issued through
3859 * this session _and_ the snap_realm hierarchy. Because it's not
3860 * clear which snap realms the mds cares about, we send everything we
3861 * know about.. that ensures we'll then get any new info the
3862 * recovering MDS might have.
3863 *
3864 * This is a relatively heavyweight operation, but it's rare.
Sage Weil2f2dc052009-10-06 11:31:09 -07003865 */
Sage Weil34b6c852010-05-10 16:31:25 -07003866static void send_mds_reconnect(struct ceph_mds_client *mdsc,
3867 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07003868{
Sage Weil2f2dc052009-10-06 11:31:09 -07003869 struct ceph_msg *reply;
Sage Weil34b6c852010-05-10 16:31:25 -07003870 int mds = session->s_mds;
Sage Weil9abf82b2010-05-10 21:58:38 -07003871 int err = -ENOMEM;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003872 struct ceph_reconnect_state recon_state = {
3873 .session = session,
3874 };
Jeff Laytonc8a96a32017-10-19 08:53:58 -04003875 LIST_HEAD(dispose);
Sage Weil2f2dc052009-10-06 11:31:09 -07003876
Sage Weil34b6c852010-05-10 16:31:25 -07003877 pr_info("mds%d reconnect start\n", mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003878
Yan, Zheng81c5a142019-01-01 16:28:33 +08003879 recon_state.pagelist = ceph_pagelist_alloc(GFP_NOFS);
3880 if (!recon_state.pagelist)
Sage Weil93cea5b2009-12-23 12:21:51 -08003881 goto fail_nopagelist;
Sage Weil93cea5b2009-12-23 12:21:51 -08003882
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003883 reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07003884 if (!reply)
Sage Weil93cea5b2009-12-23 12:21:51 -08003885 goto fail_nomsg;
Sage Weil93cea5b2009-12-23 12:21:51 -08003886
Jeff Laytond4846482019-11-15 11:51:55 -05003887 xa_destroy(&session->s_delegated_inos);
3888
Sage Weil34b6c852010-05-10 16:31:25 -07003889 mutex_lock(&session->s_mutex);
3890 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
3891 session->s_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003892
Sage Weil2f2dc052009-10-06 11:31:09 -07003893 dout("session %p state %s\n", session,
John Spraya687eca2014-09-19 13:51:08 +01003894 ceph_session_state_name(session->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07003895
Yan, Zheng99a9c272013-09-22 11:08:14 +08003896 spin_lock(&session->s_gen_ttl_lock);
3897 session->s_cap_gen++;
3898 spin_unlock(&session->s_gen_ttl_lock);
3899
3900 spin_lock(&session->s_cap_lock);
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08003901 /* don't know if session is readonly */
3902 session->s_readonly = 0;
Yan, Zheng99a9c272013-09-22 11:08:14 +08003903 /*
3904 * notify __ceph_remove_cap() that we are composing cap reconnect.
3905 * If a cap get released before being added to the cap reconnect,
3906 * __ceph_remove_cap() should skip queuing cap release.
3907 */
3908 session->s_cap_reconnect = 1;
Sage Weile01a5942010-05-10 15:36:44 -07003909 /* drop old cap expires; we're about to reestablish that state */
Jeff Laytonc8a96a32017-10-19 08:53:58 -04003910 detach_cap_releases(session, &dispose);
3911 spin_unlock(&session->s_cap_lock);
3912 dispose_cap_releases(mdsc, &dispose);
Sage Weile01a5942010-05-10 15:36:44 -07003913
Yan, Zheng5d233712014-09-10 16:56:23 +08003914 /* trim unused caps to reduce MDS's cache rejoin time */
Yan, Zhengc0bd50e2015-04-07 15:51:08 +08003915 if (mdsc->fsc->sb->s_root)
3916 shrink_dcache_parent(mdsc->fsc->sb->s_root);
Yan, Zheng5d233712014-09-10 16:56:23 +08003917
3918 ceph_con_close(&session->s_con);
3919 ceph_con_open(&session->s_con,
3920 CEPH_ENTITY_TYPE_MDS, mds,
3921 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
3922
3923 /* replay unsafe requests */
3924 replay_unsafe_requests(mdsc, session);
3925
Yan, Zheng81c5a142019-01-01 16:28:33 +08003926 ceph_early_kick_flushing_caps(mdsc, session);
3927
Yan, Zheng5d233712014-09-10 16:56:23 +08003928 down_read(&mdsc->snap_rwsem);
3929
Yan, Zheng81c5a142019-01-01 16:28:33 +08003930 /* placeholder for nr_caps */
3931 err = ceph_pagelist_encode_32(recon_state.pagelist, 0);
Sage Weil93cea5b2009-12-23 12:21:51 -08003932 if (err)
3933 goto fail;
Sage Weil20cb34a2010-05-12 15:21:32 -07003934
Yan, Zheng81c5a142019-01-01 16:28:33 +08003935 if (test_bit(CEPHFS_FEATURE_MULTI_RECONNECT, &session->s_features)) {
Yan, Zheng121f22a2016-07-04 22:05:18 +08003936 recon_state.msg_version = 3;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003937 recon_state.allow_multi = true;
3938 } else if (session->s_con.peer_features & CEPH_FEATURE_MDSENC) {
3939 recon_state.msg_version = 3;
3940 } else {
Ilya Dryomov23c625c2018-11-08 14:55:21 +01003941 recon_state.msg_version = 2;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003942 }
3943 /* trsaverse this session's caps */
Jeff Laytona25949b2020-02-18 14:12:45 -05003944 err = ceph_iterate_session_caps(session, reconnect_caps_cb, &recon_state);
Sage Weil2f2dc052009-10-06 11:31:09 -07003945
Yan, Zheng99a9c272013-09-22 11:08:14 +08003946 spin_lock(&session->s_cap_lock);
3947 session->s_cap_reconnect = 0;
3948 spin_unlock(&session->s_cap_lock);
3949
Yan, Zheng81c5a142019-01-01 16:28:33 +08003950 if (err < 0)
3951 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07003952
Yan, Zheng81c5a142019-01-01 16:28:33 +08003953 /* check if all realms can be encoded into current message */
3954 if (mdsc->num_snap_realms) {
3955 size_t total_len =
3956 recon_state.pagelist->length +
3957 mdsc->num_snap_realms *
3958 sizeof(struct ceph_mds_snaprealm_reconnect);
3959 if (recon_state.msg_version >= 4) {
3960 /* number of realms */
3961 total_len += sizeof(u32);
3962 /* version, compat_version and struct_len */
3963 total_len += mdsc->num_snap_realms *
3964 (2 * sizeof(u8) + sizeof(u32));
3965 }
3966 if (total_len > RECONNECT_MAX_SIZE) {
3967 if (!recon_state.allow_multi) {
3968 err = -ENOSPC;
3969 goto fail;
3970 }
3971 if (recon_state.nr_caps) {
3972 err = send_reconnect_partial(&recon_state);
3973 if (err)
3974 goto fail;
3975 }
3976 recon_state.msg_version = 5;
3977 }
3978 }
3979
3980 err = encode_snap_realms(mdsc, &recon_state);
3981 if (err < 0)
3982 goto fail;
3983
3984 if (recon_state.msg_version >= 5) {
3985 err = ceph_pagelist_encode_8(recon_state.pagelist, 0);
3986 if (err < 0)
Sage Weil93cea5b2009-12-23 12:21:51 -08003987 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07003988 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003989
Yan, Zheng81c5a142019-01-01 16:28:33 +08003990 if (recon_state.nr_caps || recon_state.nr_realms) {
3991 struct page *page =
3992 list_first_entry(&recon_state.pagelist->head,
3993 struct page, lru);
Yan, Zheng44c99752013-09-22 10:28:10 +08003994 __le32 *addr = kmap_atomic(page);
Yan, Zheng81c5a142019-01-01 16:28:33 +08003995 if (recon_state.nr_caps) {
3996 WARN_ON(recon_state.nr_realms != mdsc->num_snap_realms);
3997 *addr = cpu_to_le32(recon_state.nr_caps);
3998 } else if (recon_state.msg_version >= 4) {
3999 *(addr + 1) = cpu_to_le32(recon_state.nr_realms);
4000 }
Yan, Zheng44c99752013-09-22 10:28:10 +08004001 kunmap_atomic(addr);
Alex Elderebf18f42013-03-04 22:29:57 -06004002 }
Yan, Zheng44c99752013-09-22 10:28:10 +08004003
Yan, Zheng81c5a142019-01-01 16:28:33 +08004004 reply->hdr.version = cpu_to_le16(recon_state.msg_version);
4005 if (recon_state.msg_version >= 4)
4006 reply->hdr.compat_version = cpu_to_le16(4);
Yan, Zhenge548e9b2015-06-10 15:17:56 +08004007
Yan, Zheng81c5a142019-01-01 16:28:33 +08004008 reply->hdr.data_len = cpu_to_le32(recon_state.pagelist->length);
4009 ceph_msg_data_add_pagelist(reply, recon_state.pagelist);
Yan, Zhenge548e9b2015-06-10 15:17:56 +08004010
Sage Weil2f2dc052009-10-06 11:31:09 -07004011 ceph_con_send(&session->s_con, reply);
4012
Sage Weil9abf82b2010-05-10 21:58:38 -07004013 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07004014
Sage Weil9abf82b2010-05-10 21:58:38 -07004015 mutex_lock(&mdsc->mutex);
4016 __wake_requests(mdsc, &session->s_waiting);
4017 mutex_unlock(&mdsc->mutex);
4018
Sage Weil2f2dc052009-10-06 11:31:09 -07004019 up_read(&mdsc->snap_rwsem);
Yan, Zheng81c5a142019-01-01 16:28:33 +08004020 ceph_pagelist_release(recon_state.pagelist);
Sage Weil2f2dc052009-10-06 11:31:09 -07004021 return;
4022
Sage Weil93cea5b2009-12-23 12:21:51 -08004023fail:
Sage Weil2f2dc052009-10-06 11:31:09 -07004024 ceph_msg_put(reply);
Sage Weil9abf82b2010-05-10 21:58:38 -07004025 up_read(&mdsc->snap_rwsem);
4026 mutex_unlock(&session->s_mutex);
Sage Weil93cea5b2009-12-23 12:21:51 -08004027fail_nomsg:
Yan, Zheng81c5a142019-01-01 16:28:33 +08004028 ceph_pagelist_release(recon_state.pagelist);
Sage Weil93cea5b2009-12-23 12:21:51 -08004029fail_nopagelist:
Sage Weil9abf82b2010-05-10 21:58:38 -07004030 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
Sage Weil9abf82b2010-05-10 21:58:38 -07004031 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07004032}
4033
4034
4035/*
4036 * compare old and new mdsmaps, kicking requests
4037 * and closing out old connections as necessary
4038 *
4039 * called under mdsc->mutex.
4040 */
4041static void check_new_map(struct ceph_mds_client *mdsc,
4042 struct ceph_mdsmap *newmap,
4043 struct ceph_mdsmap *oldmap)
4044{
4045 int i;
4046 int oldstate, newstate;
4047 struct ceph_mds_session *s;
4048
4049 dout("check_new_map new %u old %u\n",
4050 newmap->m_epoch, oldmap->m_epoch);
4051
Xiubo Lib38c9eb2019-12-04 06:57:39 -05004052 for (i = 0; i < oldmap->possible_max_rank && i < mdsc->max_sessions; i++) {
Markus Elfringd37b1d92017-08-20 20:22:02 +02004053 if (!mdsc->sessions[i])
Sage Weil2f2dc052009-10-06 11:31:09 -07004054 continue;
4055 s = mdsc->sessions[i];
4056 oldstate = ceph_mdsmap_get_state(oldmap, i);
4057 newstate = ceph_mdsmap_get_state(newmap, i);
4058
Sage Weil0deb01c2010-06-17 14:19:01 -07004059 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07004060 i, ceph_mds_state_name(oldstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07004061 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
Sage Weil2f2dc052009-10-06 11:31:09 -07004062 ceph_mds_state_name(newstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07004063 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
John Spraya687eca2014-09-19 13:51:08 +01004064 ceph_session_state_name(s->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07004065
Xiubo Lib38c9eb2019-12-04 06:57:39 -05004066 if (i >= newmap->possible_max_rank) {
Yan, Zheng6f0f5972019-06-10 15:45:09 +08004067 /* force close session for stopped mds */
Xiubo Li5b3248c2019-12-19 19:44:09 -05004068 ceph_get_mds_session(s);
Yan, Zheng6f0f5972019-06-10 15:45:09 +08004069 __unregister_session(mdsc, s);
4070 __wake_requests(mdsc, &s->s_waiting);
4071 mutex_unlock(&mdsc->mutex);
4072
4073 mutex_lock(&s->s_mutex);
4074 cleanup_session_requests(mdsc, s);
4075 remove_session_caps(s);
4076 mutex_unlock(&s->s_mutex);
4077
4078 ceph_put_mds_session(s);
4079
4080 mutex_lock(&mdsc->mutex);
4081 kick_requests(mdsc, i);
4082 continue;
4083 }
4084
4085 if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
Sage Weil2f2dc052009-10-06 11:31:09 -07004086 ceph_mdsmap_get_addr(newmap, i),
4087 sizeof(struct ceph_entity_addr))) {
Yan, Zheng6f0f5972019-06-10 15:45:09 +08004088 /* just close it */
4089 mutex_unlock(&mdsc->mutex);
4090 mutex_lock(&s->s_mutex);
4091 mutex_lock(&mdsc->mutex);
4092 ceph_con_close(&s->s_con);
4093 mutex_unlock(&s->s_mutex);
4094 s->s_state = CEPH_MDS_SESSION_RESTARTING;
Sage Weil2f2dc052009-10-06 11:31:09 -07004095 } else if (oldstate == newstate) {
4096 continue; /* nothing new with this mds */
4097 }
4098
4099 /*
4100 * send reconnect?
4101 */
4102 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
Sage Weil34b6c852010-05-10 16:31:25 -07004103 newstate >= CEPH_MDS_STATE_RECONNECT) {
4104 mutex_unlock(&mdsc->mutex);
4105 send_mds_reconnect(mdsc, s);
4106 mutex_lock(&mdsc->mutex);
4107 }
Sage Weil2f2dc052009-10-06 11:31:09 -07004108
4109 /*
Sage Weil29790f22010-03-18 14:45:05 -07004110 * kick request on any mds that has gone active.
Sage Weil2f2dc052009-10-06 11:31:09 -07004111 */
4112 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
4113 newstate >= CEPH_MDS_STATE_ACTIVE) {
Sage Weil29790f22010-03-18 14:45:05 -07004114 if (oldstate != CEPH_MDS_STATE_CREATING &&
4115 oldstate != CEPH_MDS_STATE_STARTING)
4116 pr_info("mds%d recovery completed\n", s->s_mds);
4117 kick_requests(mdsc, i);
Xiubo Liea8412b2020-05-20 03:51:19 -04004118 mutex_unlock(&mdsc->mutex);
Jeff Layton829ad4d2020-04-03 13:09:07 -04004119 mutex_lock(&s->s_mutex);
Xiubo Liea8412b2020-05-20 03:51:19 -04004120 mutex_lock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07004121 ceph_kick_flushing_caps(mdsc, s);
Jeff Layton829ad4d2020-04-03 13:09:07 -04004122 mutex_unlock(&s->s_mutex);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08004123 wake_up_session_caps(s, RECONNECT);
Sage Weil2f2dc052009-10-06 11:31:09 -07004124 }
4125 }
Sage Weilcb170a22010-06-21 13:38:35 -07004126
Xiubo Lib38c9eb2019-12-04 06:57:39 -05004127 for (i = 0; i < newmap->possible_max_rank && i < mdsc->max_sessions; i++) {
Sage Weilcb170a22010-06-21 13:38:35 -07004128 s = mdsc->sessions[i];
4129 if (!s)
4130 continue;
4131 if (!ceph_mdsmap_is_laggy(newmap, i))
4132 continue;
4133 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
4134 s->s_state == CEPH_MDS_SESSION_HUNG ||
4135 s->s_state == CEPH_MDS_SESSION_CLOSING) {
4136 dout(" connecting to export targets of laggy mds%d\n",
4137 i);
4138 __open_export_target_sessions(mdsc, s);
4139 }
4140 }
Sage Weil2f2dc052009-10-06 11:31:09 -07004141}
4142
4143
4144
4145/*
4146 * leases
4147 */
4148
4149/*
4150 * caller must hold session s_mutex, dentry->d_lock
4151 */
4152void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
4153{
4154 struct ceph_dentry_info *di = ceph_dentry(dentry);
4155
4156 ceph_put_mds_session(di->lease_session);
4157 di->lease_session = NULL;
4158}
4159
Sage Weil2600d2d2010-02-22 15:12:16 -08004160static void handle_lease(struct ceph_mds_client *mdsc,
4161 struct ceph_mds_session *session,
4162 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07004163{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004164 struct super_block *sb = mdsc->fsc->sb;
Sage Weil2f2dc052009-10-06 11:31:09 -07004165 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07004166 struct dentry *parent, *dentry;
4167 struct ceph_dentry_info *di;
Sage Weil2600d2d2010-02-22 15:12:16 -08004168 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07004169 struct ceph_mds_lease *h = msg->front.iov_base;
Sage Weil1e5ea232010-06-04 10:05:40 -07004170 u32 seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07004171 struct ceph_vino vino;
Sage Weil2f2dc052009-10-06 11:31:09 -07004172 struct qstr dname;
4173 int release = 0;
4174
Sage Weil2f2dc052009-10-06 11:31:09 -07004175 dout("handle_lease from mds%d\n", mds);
4176
4177 /* decode */
4178 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
4179 goto bad;
4180 vino.ino = le64_to_cpu(h->ino);
4181 vino.snap = CEPH_NOSNAP;
Sage Weil1e5ea232010-06-04 10:05:40 -07004182 seq = le32_to_cpu(h->seq);
Yan, Zheng0fcf6c02018-08-03 16:24:49 +08004183 dname.len = get_unaligned_le32(h + 1);
4184 if (msg->front.iov_len < sizeof(*h) + sizeof(u32) + dname.len)
Sage Weil2f2dc052009-10-06 11:31:09 -07004185 goto bad;
Yan, Zheng0fcf6c02018-08-03 16:24:49 +08004186 dname.name = (void *)(h + 1) + sizeof(u32);
Sage Weil2f2dc052009-10-06 11:31:09 -07004187
Sage Weil2f2dc052009-10-06 11:31:09 -07004188 /* lookup inode */
4189 inode = ceph_find_inode(sb, vino);
Sage Weil2f90b852011-07-26 11:28:25 -07004190 dout("handle_lease %s, ino %llx %p %.*s\n",
4191 ceph_lease_op_name(h->action), vino.ino, inode,
Sage Weil1e5ea232010-06-04 10:05:40 -07004192 dname.len, dname.name);
Yan, Zheng6cd3bca2014-09-17 07:45:12 +08004193
4194 mutex_lock(&session->s_mutex);
4195 session->s_seq++;
4196
Markus Elfringd37b1d92017-08-20 20:22:02 +02004197 if (!inode) {
Sage Weil2f2dc052009-10-06 11:31:09 -07004198 dout("handle_lease no inode %llx\n", vino.ino);
4199 goto release;
4200 }
Sage Weil2f2dc052009-10-06 11:31:09 -07004201
4202 /* dentry */
4203 parent = d_find_alias(inode);
4204 if (!parent) {
4205 dout("no parent dentry on inode %p\n", inode);
4206 WARN_ON(1);
4207 goto release; /* hrm... */
4208 }
Linus Torvalds8387ff22016-06-10 07:51:30 -07004209 dname.hash = full_name_hash(parent, dname.name, dname.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07004210 dentry = d_lookup(parent, &dname);
4211 dput(parent);
4212 if (!dentry)
4213 goto release;
4214
4215 spin_lock(&dentry->d_lock);
4216 di = ceph_dentry(dentry);
4217 switch (h->action) {
4218 case CEPH_MDS_LEASE_REVOKE:
Sage Weil3d8eb7a2011-11-11 09:48:53 -08004219 if (di->lease_session == session) {
Sage Weil1e5ea232010-06-04 10:05:40 -07004220 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
4221 h->seq = cpu_to_le32(di->lease_seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07004222 __ceph_mdsc_drop_dentry_lease(dentry);
4223 }
4224 release = 1;
4225 break;
4226
4227 case CEPH_MDS_LEASE_RENEW:
Sage Weil3d8eb7a2011-11-11 09:48:53 -08004228 if (di->lease_session == session &&
Sage Weil2f2dc052009-10-06 11:31:09 -07004229 di->lease_gen == session->s_cap_gen &&
4230 di->lease_renew_from &&
4231 di->lease_renew_after == 0) {
4232 unsigned long duration =
Nicholas Mc Guire3563dbdd2015-02-06 06:52:17 -05004233 msecs_to_jiffies(le32_to_cpu(h->duration_ms));
Sage Weil2f2dc052009-10-06 11:31:09 -07004234
Sage Weil1e5ea232010-06-04 10:05:40 -07004235 di->lease_seq = seq;
Miklos Szeredi9b16f03c2016-06-22 16:35:04 +02004236 di->time = di->lease_renew_from + duration;
Sage Weil2f2dc052009-10-06 11:31:09 -07004237 di->lease_renew_after = di->lease_renew_from +
4238 (duration >> 1);
4239 di->lease_renew_from = 0;
4240 }
4241 break;
4242 }
4243 spin_unlock(&dentry->d_lock);
4244 dput(dentry);
4245
4246 if (!release)
4247 goto out;
4248
4249release:
4250 /* let's just reuse the same message */
4251 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
4252 ceph_msg_get(msg);
4253 ceph_con_send(&session->s_con, msg);
4254
4255out:
Sage Weil2f2dc052009-10-06 11:31:09 -07004256 mutex_unlock(&session->s_mutex);
Yan, Zheng3e1d0452019-05-18 20:39:55 +08004257 /* avoid calling iput_final() in mds dispatch threads */
4258 ceph_async_iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07004259 return;
4260
4261bad:
4262 pr_err("corrupt lease message\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08004263 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004264}
4265
4266void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
Sage Weil2f2dc052009-10-06 11:31:09 -07004267 struct dentry *dentry, char action,
4268 u32 seq)
4269{
4270 struct ceph_msg *msg;
4271 struct ceph_mds_lease *lease;
Yan, Zheng8f2a98ef2019-05-23 10:45:24 +08004272 struct inode *dir;
4273 int len = sizeof(*lease) + sizeof(u32) + NAME_MAX;
Sage Weil2f2dc052009-10-06 11:31:09 -07004274
Yan, Zheng8f2a98ef2019-05-23 10:45:24 +08004275 dout("lease_send_msg identry %p %s to mds%d\n",
4276 dentry, ceph_lease_op_name(action), session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07004277
Sage Weilb61c2762011-08-09 15:03:46 -07004278 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07004279 if (!msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07004280 return;
4281 lease = msg->front.iov_base;
4282 lease->action = action;
Sage Weil2f2dc052009-10-06 11:31:09 -07004283 lease->seq = cpu_to_le32(seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07004284
Yan, Zheng8f2a98ef2019-05-23 10:45:24 +08004285 spin_lock(&dentry->d_lock);
4286 dir = d_inode(dentry->d_parent);
4287 lease->ino = cpu_to_le64(ceph_ino(dir));
4288 lease->first = lease->last = cpu_to_le64(ceph_snap(dir));
4289
4290 put_unaligned_le32(dentry->d_name.len, lease + 1);
4291 memcpy((void *)(lease + 1) + 4,
4292 dentry->d_name.name, dentry->d_name.len);
4293 spin_unlock(&dentry->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07004294 /*
4295 * if this is a preemptive lease RELEASE, no need to
4296 * flush request stream, since the actual request will
4297 * soon follow.
4298 */
4299 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
4300
4301 ceph_con_send(&session->s_con, msg);
4302}
4303
4304/*
Yan, Zheng7aac4532018-03-13 19:52:20 +08004305 * lock unlock sessions, to wait ongoing session activities
Sage Weil2f2dc052009-10-06 11:31:09 -07004306 */
Yan, Zheng7aac4532018-03-13 19:52:20 +08004307static void lock_unlock_sessions(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07004308{
4309 int i;
4310
Sage Weil2f2dc052009-10-06 11:31:09 -07004311 mutex_lock(&mdsc->mutex);
4312 for (i = 0; i < mdsc->max_sessions; i++) {
4313 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
4314 if (!s)
4315 continue;
4316 mutex_unlock(&mdsc->mutex);
4317 mutex_lock(&s->s_mutex);
4318 mutex_unlock(&s->s_mutex);
4319 ceph_put_mds_session(s);
4320 mutex_lock(&mdsc->mutex);
4321 }
4322 mutex_unlock(&mdsc->mutex);
4323}
4324
Yan, Zheng131d7eb2019-07-25 20:16:47 +08004325static void maybe_recover_session(struct ceph_mds_client *mdsc)
4326{
4327 struct ceph_fs_client *fsc = mdsc->fsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07004328
Yan, Zheng131d7eb2019-07-25 20:16:47 +08004329 if (!ceph_test_mount_opt(fsc, CLEANRECOVER))
4330 return;
4331
4332 if (READ_ONCE(fsc->mount_state) != CEPH_MOUNT_MOUNTED)
4333 return;
4334
4335 if (!READ_ONCE(fsc->blacklisted))
4336 return;
4337
4338 if (fsc->last_auto_reconnect &&
4339 time_before(jiffies, fsc->last_auto_reconnect + HZ * 60 * 30))
4340 return;
4341
4342 pr_info("auto reconnect after blacklisted\n");
4343 fsc->last_auto_reconnect = jiffies;
4344 ceph_force_reconnect(fsc->sb);
4345}
Sage Weil2f2dc052009-10-06 11:31:09 -07004346
Xiubo Li3e699bd2020-06-30 03:52:15 -04004347bool check_session_state(struct ceph_mds_session *s)
4348{
4349 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
4350 dout("resending session close request for mds%d\n",
4351 s->s_mds);
4352 request_close_session(s);
4353 return false;
4354 }
4355 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
4356 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
4357 s->s_state = CEPH_MDS_SESSION_HUNG;
4358 pr_info("mds%d hung\n", s->s_mds);
4359 }
4360 }
4361 if (s->s_state == CEPH_MDS_SESSION_NEW ||
4362 s->s_state == CEPH_MDS_SESSION_RESTARTING ||
Xiubo Liaaf5a472020-07-17 09:25:13 -04004363 s->s_state == CEPH_MDS_SESSION_CLOSED ||
Xiubo Li3e699bd2020-06-30 03:52:15 -04004364 s->s_state == CEPH_MDS_SESSION_REJECTED)
4365 /* this mds is failed or recovering, just wait */
4366 return false;
4367
4368 return true;
4369}
4370
Sage Weil2f2dc052009-10-06 11:31:09 -07004371/*
4372 * delayed work -- periodically trim expired leases, renew caps with mds
4373 */
4374static void schedule_delayed(struct ceph_mds_client *mdsc)
4375{
4376 int delay = 5;
4377 unsigned hz = round_jiffies_relative(HZ * delay);
4378 schedule_delayed_work(&mdsc->delayed_work, hz);
4379}
4380
4381static void delayed_work(struct work_struct *work)
4382{
4383 int i;
4384 struct ceph_mds_client *mdsc =
4385 container_of(work, struct ceph_mds_client, delayed_work.work);
4386 int renew_interval;
4387 int renew_caps;
4388
4389 dout("mdsc delayed_work\n");
Yan, Zheng75c96272017-12-14 15:11:09 +08004390
Xiubo Lifa996772020-07-01 01:52:48 -04004391 if (mdsc->stopping)
4392 return;
4393
Sage Weil2f2dc052009-10-06 11:31:09 -07004394 mutex_lock(&mdsc->mutex);
4395 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
4396 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
4397 mdsc->last_renew_caps);
4398 if (renew_caps)
4399 mdsc->last_renew_caps = jiffies;
4400
4401 for (i = 0; i < mdsc->max_sessions; i++) {
4402 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
Markus Elfringd37b1d92017-08-20 20:22:02 +02004403 if (!s)
Sage Weil2f2dc052009-10-06 11:31:09 -07004404 continue;
Xiubo Li3e699bd2020-06-30 03:52:15 -04004405
4406 if (!check_session_state(s)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07004407 ceph_put_mds_session(s);
4408 continue;
4409 }
4410 mutex_unlock(&mdsc->mutex);
4411
4412 mutex_lock(&s->s_mutex);
4413 if (renew_caps)
4414 send_renew_caps(mdsc, s);
4415 else
4416 ceph_con_keepalive(&s->s_con);
Sage Weilaab53dd2010-03-17 16:30:21 -07004417 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
4418 s->s_state == CEPH_MDS_SESSION_HUNG)
Sage Weil3d7ded42010-06-09 16:47:10 -07004419 ceph_send_cap_releases(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07004420 mutex_unlock(&s->s_mutex);
4421 ceph_put_mds_session(s);
4422
4423 mutex_lock(&mdsc->mutex);
4424 }
4425 mutex_unlock(&mdsc->mutex);
4426
Yan, Zheng37c4efc2019-01-31 16:55:51 +08004427 ceph_check_delayed_caps(mdsc);
4428
4429 ceph_queue_cap_reclaim_work(mdsc);
4430
4431 ceph_trim_snapid_map(mdsc);
4432
Yan, Zheng131d7eb2019-07-25 20:16:47 +08004433 maybe_recover_session(mdsc);
4434
Sage Weil2f2dc052009-10-06 11:31:09 -07004435 schedule_delayed(mdsc);
4436}
4437
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004438int ceph_mdsc_init(struct ceph_fs_client *fsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07004439
Sage Weil2f2dc052009-10-06 11:31:09 -07004440{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004441 struct ceph_mds_client *mdsc;
Xiubo Lif9009ef2020-03-19 23:44:59 -04004442 int err;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004443
4444 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
4445 if (!mdsc)
4446 return -ENOMEM;
4447 mdsc->fsc = fsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07004448 mutex_init(&mdsc->mutex);
4449 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
Markus Elfringd37b1d92017-08-20 20:22:02 +02004450 if (!mdsc->mdsmap) {
Xiubo Lif9009ef2020-03-19 23:44:59 -04004451 err = -ENOMEM;
4452 goto err_mdsc;
majianpengfb3101b2013-06-25 14:48:19 +08004453 }
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08004454
Sage Weil2f2dc052009-10-06 11:31:09 -07004455 init_completion(&mdsc->safe_umount_waiters);
Sage Weilf3c60c52010-08-11 14:51:23 -07004456 init_waitqueue_head(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07004457 INIT_LIST_HEAD(&mdsc->waiting_for_map);
4458 mdsc->sessions = NULL;
Yan, Zheng86d8f672015-01-09 17:00:42 +08004459 atomic_set(&mdsc->num_sessions, 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07004460 mdsc->max_sessions = 0;
4461 mdsc->stopping = 0;
Luis Henriquesd557c482018-01-12 17:19:29 +00004462 atomic64_set(&mdsc->quotarealms_count, 0);
Luis Henriques0c44a8e2019-03-21 10:20:10 +00004463 mdsc->quotarealms_inodes = RB_ROOT;
4464 mutex_init(&mdsc->quotarealms_inodes_mutex);
Yan, Zhengaffbc192015-05-05 21:22:13 +08004465 mdsc->last_snap_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07004466 init_rwsem(&mdsc->snap_rwsem);
Sage Weila105f002010-02-15 14:37:55 -08004467 mdsc->snap_realms = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07004468 INIT_LIST_HEAD(&mdsc->snap_empty);
Yan, Zheng81c5a142019-01-01 16:28:33 +08004469 mdsc->num_snap_realms = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07004470 spin_lock_init(&mdsc->snap_empty_lock);
4471 mdsc->last_tid = 0;
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08004472 mdsc->oldest_tid = 0;
Sage Weil44ca18f2010-02-15 12:08:46 -08004473 mdsc->request_tree = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07004474 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
4475 mdsc->last_renew_caps = jiffies;
4476 INIT_LIST_HEAD(&mdsc->cap_delay_list);
Jeff Layton3a3430a2019-11-20 12:00:59 -05004477 INIT_LIST_HEAD(&mdsc->cap_wait_list);
Sage Weil2f2dc052009-10-06 11:31:09 -07004478 spin_lock_init(&mdsc->cap_delay_lock);
4479 INIT_LIST_HEAD(&mdsc->snap_flush_list);
4480 spin_lock_init(&mdsc->snap_flush_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08004481 mdsc->last_cap_flush_tid = 1;
Yan, Zhenge4500b52016-07-06 11:12:56 +08004482 INIT_LIST_HEAD(&mdsc->cap_flush_list);
Sage Weildb354052011-05-24 11:46:31 -07004483 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
Sage Weil2f2dc052009-10-06 11:31:09 -07004484 mdsc->num_cap_flushing = 0;
4485 spin_lock_init(&mdsc->cap_dirty_lock);
4486 init_waitqueue_head(&mdsc->cap_flushing_wq);
Yan, Zheng37c4efc2019-01-31 16:55:51 +08004487 INIT_WORK(&mdsc->cap_reclaim_work, ceph_cap_reclaim_work);
Yan, Zhengfe330322019-02-01 14:57:15 +08004488 atomic_set(&mdsc->cap_reclaim_pending, 0);
Xiubo Lif9009ef2020-03-19 23:44:59 -04004489 err = ceph_metric_init(&mdsc->metric);
4490 if (err)
4491 goto err_mdsmap;
Yan, Zheng37c4efc2019-01-31 16:55:51 +08004492
4493 spin_lock_init(&mdsc->dentry_list_lock);
4494 INIT_LIST_HEAD(&mdsc->dentry_leases);
4495 INIT_LIST_HEAD(&mdsc->dentry_dir_leases);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08004496
Yehuda Sadeh37151662010-06-17 16:16:12 -07004497 ceph_caps_init(mdsc);
Yan, Zhengfe330322019-02-01 14:57:15 +08004498 ceph_adjust_caps_max_min(mdsc, fsc->mount_options);
Yehuda Sadeh37151662010-06-17 16:16:12 -07004499
Yan, Zheng75c96272017-12-14 15:11:09 +08004500 spin_lock_init(&mdsc->snapid_map_lock);
4501 mdsc->snapid_map_tree = RB_ROOT;
4502 INIT_LIST_HEAD(&mdsc->snapid_map_lru);
4503
Yan, Zheng10183a62015-04-27 15:33:28 +08004504 init_rwsem(&mdsc->pool_perm_rwsem);
4505 mdsc->pool_perm_tree = RB_ROOT;
4506
Yan, Zhengdfeb84d2018-07-02 15:55:23 +08004507 strscpy(mdsc->nodename, utsname()->nodename,
4508 sizeof(mdsc->nodename));
Xiubo Lia7caa882020-07-23 15:32:25 +08004509
4510 fsc->mdsc = mdsc;
Sage Weil5f44f142009-11-18 14:52:18 -08004511 return 0;
Xiubo Lif9009ef2020-03-19 23:44:59 -04004512
4513err_mdsmap:
4514 kfree(mdsc->mdsmap);
4515err_mdsc:
4516 kfree(mdsc);
4517 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07004518}
4519
4520/*
4521 * Wait for safe replies on open mds requests. If we time out, drop
4522 * all requests from the tree to avoid dangling dentry refs.
4523 */
4524static void wait_requests(struct ceph_mds_client *mdsc)
4525{
Ilya Dryomova319bf52015-05-15 12:02:17 +03004526 struct ceph_options *opts = mdsc->fsc->client->options;
Sage Weil2f2dc052009-10-06 11:31:09 -07004527 struct ceph_mds_request *req;
Sage Weil2f2dc052009-10-06 11:31:09 -07004528
4529 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08004530 if (__get_oldest_req(mdsc)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07004531 mutex_unlock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08004532
Sage Weil2f2dc052009-10-06 11:31:09 -07004533 dout("wait_requests waiting for requests\n");
4534 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
Ilya Dryomova319bf52015-05-15 12:02:17 +03004535 ceph_timeout_jiffies(opts->mount_timeout));
Sage Weil2f2dc052009-10-06 11:31:09 -07004536
4537 /* tear down remaining requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08004538 mutex_lock(&mdsc->mutex);
4539 while ((req = __get_oldest_req(mdsc))) {
Sage Weil2f2dc052009-10-06 11:31:09 -07004540 dout("wait_requests timed out on tid %llu\n",
4541 req->r_tid);
Yan, Zheng428138c2019-06-14 10:55:05 +08004542 list_del_init(&req->r_wait);
Sage Weil44ca18f2010-02-15 12:08:46 -08004543 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07004544 }
4545 }
4546 mutex_unlock(&mdsc->mutex);
4547 dout("wait_requests done\n");
4548}
4549
4550/*
4551 * called before mount is ro, and before dentries are torn down.
4552 * (hmm, does this still race with new lookups?)
4553 */
4554void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
4555{
4556 dout("pre_umount\n");
4557 mdsc->stopping = 1;
4558
Yan, Zheng7aac4532018-03-13 19:52:20 +08004559 lock_unlock_sessions(mdsc);
Sage Weilafcdaea2009-10-14 14:27:38 -07004560 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07004561 wait_requests(mdsc);
Sage Weil17c688c2010-06-21 16:12:26 -07004562
4563 /*
4564 * wait for reply handlers to drop their request refs and
4565 * their inode/dcache refs
4566 */
4567 ceph_msgr_flush();
Luis Henriques0c44a8e2019-03-21 10:20:10 +00004568
4569 ceph_cleanup_quotarealms_inodes(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07004570}
4571
4572/*
4573 * wait for all write mds requests to flush.
4574 */
4575static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
4576{
Sage Weil80fc7312010-03-16 15:28:54 -07004577 struct ceph_mds_request *req = NULL, *nextreq;
Sage Weil44ca18f2010-02-15 12:08:46 -08004578 struct rb_node *n;
Sage Weil2f2dc052009-10-06 11:31:09 -07004579
4580 mutex_lock(&mdsc->mutex);
4581 dout("wait_unsafe_requests want %lld\n", want_tid);
Sage Weil80fc7312010-03-16 15:28:54 -07004582restart:
Sage Weil44ca18f2010-02-15 12:08:46 -08004583 req = __get_oldest_req(mdsc);
4584 while (req && req->r_tid <= want_tid) {
Sage Weil80fc7312010-03-16 15:28:54 -07004585 /* find next request */
4586 n = rb_next(&req->r_node);
4587 if (n)
4588 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
4589 else
4590 nextreq = NULL;
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08004591 if (req->r_op != CEPH_MDS_OP_SETFILELOCK &&
4592 (req->r_op & CEPH_MDS_OP_WRITE)) {
Sage Weil44ca18f2010-02-15 12:08:46 -08004593 /* write op */
4594 ceph_mdsc_get_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07004595 if (nextreq)
4596 ceph_mdsc_get_request(nextreq);
Sage Weil44ca18f2010-02-15 12:08:46 -08004597 mutex_unlock(&mdsc->mutex);
4598 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
4599 req->r_tid, want_tid);
4600 wait_for_completion(&req->r_safe_completion);
4601 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08004602 ceph_mdsc_put_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07004603 if (!nextreq)
4604 break; /* next dne before, so we're done! */
4605 if (RB_EMPTY_NODE(&nextreq->r_node)) {
4606 /* next request was removed from tree */
4607 ceph_mdsc_put_request(nextreq);
4608 goto restart;
4609 }
4610 ceph_mdsc_put_request(nextreq); /* won't go away */
Sage Weil44ca18f2010-02-15 12:08:46 -08004611 }
Sage Weil80fc7312010-03-16 15:28:54 -07004612 req = nextreq;
Sage Weil2f2dc052009-10-06 11:31:09 -07004613 }
4614 mutex_unlock(&mdsc->mutex);
4615 dout("wait_unsafe_requests done\n");
4616}
4617
4618void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
4619{
Yan, Zheng0e294382016-07-04 18:06:41 +08004620 u64 want_tid, want_flush;
Sage Weil2f2dc052009-10-06 11:31:09 -07004621
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01004622 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
Sage Weil56b7cf92010-05-03 15:22:00 -07004623 return;
4624
Sage Weil2f2dc052009-10-06 11:31:09 -07004625 dout("sync\n");
4626 mutex_lock(&mdsc->mutex);
4627 want_tid = mdsc->last_tid;
Sage Weil2f2dc052009-10-06 11:31:09 -07004628 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07004629
Sage Weilafcdaea2009-10-14 14:27:38 -07004630 ceph_flush_dirty_caps(mdsc);
Yan, Zhengd3383a82015-01-08 21:30:12 +08004631 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zheng8310b082015-06-09 17:20:12 +08004632 want_flush = mdsc->last_cap_flush_tid;
Yan, Zhengc8799fc2016-07-07 15:22:38 +08004633 if (!list_empty(&mdsc->cap_flush_list)) {
4634 struct ceph_cap_flush *cf =
4635 list_last_entry(&mdsc->cap_flush_list,
4636 struct ceph_cap_flush, g_list);
4637 cf->wake = true;
4638 }
Yan, Zhengd3383a82015-01-08 21:30:12 +08004639 spin_unlock(&mdsc->cap_dirty_lock);
4640
Yan, Zheng0e294382016-07-04 18:06:41 +08004641 dout("sync want tid %lld flush_seq %lld\n",
4642 want_tid, want_flush);
Sage Weil2f2dc052009-10-06 11:31:09 -07004643
4644 wait_unsafe_requests(mdsc, want_tid);
Yan, Zheng0e294382016-07-04 18:06:41 +08004645 wait_caps_flush(mdsc, want_flush);
Sage Weil2f2dc052009-10-06 11:31:09 -07004646}
4647
Sage Weilf3c60c52010-08-11 14:51:23 -07004648/*
4649 * true if all sessions are closed, or we force unmount
4650 */
Yan, Zhengfcff4152016-09-14 16:39:51 +08004651static bool done_closing_sessions(struct ceph_mds_client *mdsc, int skipped)
Sage Weilf3c60c52010-08-11 14:51:23 -07004652{
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01004653 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
Sage Weilf3c60c52010-08-11 14:51:23 -07004654 return true;
Yan, Zhengfcff4152016-09-14 16:39:51 +08004655 return atomic_read(&mdsc->num_sessions) <= skipped;
Sage Weilf3c60c52010-08-11 14:51:23 -07004656}
Sage Weil2f2dc052009-10-06 11:31:09 -07004657
4658/*
4659 * called after sb is ro.
4660 */
4661void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
4662{
Ilya Dryomova319bf52015-05-15 12:02:17 +03004663 struct ceph_options *opts = mdsc->fsc->client->options;
Sage Weil2f2dc052009-10-06 11:31:09 -07004664 struct ceph_mds_session *session;
4665 int i;
Yan, Zhengfcff4152016-09-14 16:39:51 +08004666 int skipped = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07004667
4668 dout("close_sessions\n");
4669
Sage Weil2f2dc052009-10-06 11:31:09 -07004670 /* close sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07004671 mutex_lock(&mdsc->mutex);
4672 for (i = 0; i < mdsc->max_sessions; i++) {
4673 session = __ceph_lookup_mds_session(mdsc, i);
4674 if (!session)
4675 continue;
Sage Weil2f2dc052009-10-06 11:31:09 -07004676 mutex_unlock(&mdsc->mutex);
Sage Weilf3c60c52010-08-11 14:51:23 -07004677 mutex_lock(&session->s_mutex);
Yan, Zhengfcff4152016-09-14 16:39:51 +08004678 if (__close_session(mdsc, session) <= 0)
4679 skipped++;
Sage Weilf3c60c52010-08-11 14:51:23 -07004680 mutex_unlock(&session->s_mutex);
4681 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07004682 mutex_lock(&mdsc->mutex);
4683 }
Sage Weilf3c60c52010-08-11 14:51:23 -07004684 mutex_unlock(&mdsc->mutex);
4685
4686 dout("waiting for sessions to close\n");
Yan, Zhengfcff4152016-09-14 16:39:51 +08004687 wait_event_timeout(mdsc->session_close_wq,
4688 done_closing_sessions(mdsc, skipped),
Ilya Dryomova319bf52015-05-15 12:02:17 +03004689 ceph_timeout_jiffies(opts->mount_timeout));
Sage Weil2f2dc052009-10-06 11:31:09 -07004690
4691 /* tear down remaining sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07004692 mutex_lock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07004693 for (i = 0; i < mdsc->max_sessions; i++) {
4694 if (mdsc->sessions[i]) {
Xiubo Li5b3248c2019-12-19 19:44:09 -05004695 session = ceph_get_mds_session(mdsc->sessions[i]);
Sage Weil2600d2d2010-02-22 15:12:16 -08004696 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07004697 mutex_unlock(&mdsc->mutex);
4698 mutex_lock(&session->s_mutex);
4699 remove_session_caps(session);
4700 mutex_unlock(&session->s_mutex);
4701 ceph_put_mds_session(session);
4702 mutex_lock(&mdsc->mutex);
4703 }
4704 }
Sage Weil2f2dc052009-10-06 11:31:09 -07004705 WARN_ON(!list_empty(&mdsc->cap_delay_list));
Sage Weil2f2dc052009-10-06 11:31:09 -07004706 mutex_unlock(&mdsc->mutex);
4707
Yan, Zheng75c96272017-12-14 15:11:09 +08004708 ceph_cleanup_snapid_map(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07004709 ceph_cleanup_empty_realms(mdsc);
4710
Yan, Zheng37c4efc2019-01-31 16:55:51 +08004711 cancel_work_sync(&mdsc->cap_reclaim_work);
Sage Weil2f2dc052009-10-06 11:31:09 -07004712 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
4713
4714 dout("stopped\n");
4715}
4716
Yan, Zheng48fec5d2015-07-01 16:27:46 +08004717void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc)
4718{
4719 struct ceph_mds_session *session;
4720 int mds;
4721
4722 dout("force umount\n");
4723
4724 mutex_lock(&mdsc->mutex);
4725 for (mds = 0; mds < mdsc->max_sessions; mds++) {
4726 session = __ceph_lookup_mds_session(mdsc, mds);
4727 if (!session)
4728 continue;
Yan, Zhengd468e722019-07-25 20:16:44 +08004729
4730 if (session->s_state == CEPH_MDS_SESSION_REJECTED)
4731 __unregister_session(mdsc, session);
4732 __wake_requests(mdsc, &session->s_waiting);
Yan, Zheng48fec5d2015-07-01 16:27:46 +08004733 mutex_unlock(&mdsc->mutex);
Yan, Zhengd468e722019-07-25 20:16:44 +08004734
Yan, Zheng48fec5d2015-07-01 16:27:46 +08004735 mutex_lock(&session->s_mutex);
4736 __close_session(mdsc, session);
4737 if (session->s_state == CEPH_MDS_SESSION_CLOSING) {
4738 cleanup_session_requests(mdsc, session);
4739 remove_session_caps(session);
4740 }
4741 mutex_unlock(&session->s_mutex);
4742 ceph_put_mds_session(session);
Yan, Zhengd468e722019-07-25 20:16:44 +08004743
Yan, Zheng48fec5d2015-07-01 16:27:46 +08004744 mutex_lock(&mdsc->mutex);
4745 kick_requests(mdsc, mds);
4746 }
4747 __wake_requests(mdsc, &mdsc->waiting_for_map);
4748 mutex_unlock(&mdsc->mutex);
4749}
4750
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004751static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07004752{
4753 dout("stop\n");
Xiubo Lifa996772020-07-01 01:52:48 -04004754 /*
4755 * Make sure the delayed work stopped before releasing
4756 * the resources.
4757 *
4758 * Because the cancel_delayed_work_sync() will only
4759 * guarantee that the work finishes executing. But the
4760 * delayed work will re-arm itself again after that.
4761 */
4762 flush_delayed_work(&mdsc->delayed_work);
4763
Sage Weil2f2dc052009-10-06 11:31:09 -07004764 if (mdsc->mdsmap)
4765 ceph_mdsmap_destroy(mdsc->mdsmap);
4766 kfree(mdsc->sessions);
Yehuda Sadeh37151662010-06-17 16:16:12 -07004767 ceph_caps_finalize(mdsc);
Yan, Zheng10183a62015-04-27 15:33:28 +08004768 ceph_pool_perm_destroy(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07004769}
4770
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004771void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
4772{
4773 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weilef550f62011-03-25 13:27:48 -07004774 dout("mdsc_destroy %p\n", mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07004775
Chengguang Xu50c55ae2018-03-14 13:47:33 +08004776 if (!mdsc)
4777 return;
4778
Sage Weilef550f62011-03-25 13:27:48 -07004779 /* flush out any connection work with references to us */
4780 ceph_msgr_flush();
4781
Yan, Zheng62a65f32017-06-22 16:26:34 +08004782 ceph_mdsc_stop(mdsc);
4783
Xiubo Lif9009ef2020-03-19 23:44:59 -04004784 ceph_metric_destroy(&mdsc->metric);
4785
Xiubo Li18f473b2020-07-16 10:05:57 -04004786 flush_delayed_work(&mdsc->metric.delayed_work);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004787 fsc->mdsc = NULL;
4788 kfree(mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07004789 dout("mdsc_destroy %p done\n", mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004790}
4791
Yan, Zheng430afba2016-07-08 11:25:38 +08004792void ceph_mdsc_handle_fsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
4793{
4794 struct ceph_fs_client *fsc = mdsc->fsc;
4795 const char *mds_namespace = fsc->mount_options->mds_namespace;
4796 void *p = msg->front.iov_base;
4797 void *end = p + msg->front.iov_len;
4798 u32 epoch;
4799 u32 map_len;
4800 u32 num_fs;
4801 u32 mount_fscid = (u32)-1;
4802 u8 struct_v, struct_cv;
4803 int err = -EINVAL;
4804
4805 ceph_decode_need(&p, end, sizeof(u32), bad);
4806 epoch = ceph_decode_32(&p);
4807
4808 dout("handle_fsmap epoch %u\n", epoch);
4809
4810 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
4811 struct_v = ceph_decode_8(&p);
4812 struct_cv = ceph_decode_8(&p);
4813 map_len = ceph_decode_32(&p);
4814
4815 ceph_decode_need(&p, end, sizeof(u32) * 3, bad);
4816 p += sizeof(u32) * 2; /* skip epoch and legacy_client_fscid */
4817
4818 num_fs = ceph_decode_32(&p);
4819 while (num_fs-- > 0) {
4820 void *info_p, *info_end;
4821 u32 info_len;
4822 u8 info_v, info_cv;
4823 u32 fscid, namelen;
4824
4825 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
4826 info_v = ceph_decode_8(&p);
4827 info_cv = ceph_decode_8(&p);
4828 info_len = ceph_decode_32(&p);
4829 ceph_decode_need(&p, end, info_len, bad);
4830 info_p = p;
4831 info_end = p + info_len;
4832 p = info_end;
4833
4834 ceph_decode_need(&info_p, info_end, sizeof(u32) * 2, bad);
4835 fscid = ceph_decode_32(&info_p);
4836 namelen = ceph_decode_32(&info_p);
4837 ceph_decode_need(&info_p, info_end, namelen, bad);
4838
4839 if (mds_namespace &&
4840 strlen(mds_namespace) == namelen &&
4841 !strncmp(mds_namespace, (char *)info_p, namelen)) {
4842 mount_fscid = fscid;
4843 break;
4844 }
4845 }
4846
4847 ceph_monc_got_map(&fsc->client->monc, CEPH_SUB_FSMAP, epoch);
4848 if (mount_fscid != (u32)-1) {
4849 fsc->client->monc.fs_cluster_id = mount_fscid;
4850 ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP,
4851 0, true);
4852 ceph_monc_renew_subs(&fsc->client->monc);
4853 } else {
4854 err = -ENOENT;
4855 goto err_out;
4856 }
4857 return;
Ilya Dryomov76bd6ec2017-10-16 10:32:50 +02004858
Yan, Zheng430afba2016-07-08 11:25:38 +08004859bad:
4860 pr_err("error decoding fsmap\n");
4861err_out:
4862 mutex_lock(&mdsc->mutex);
Ilya Dryomov76bd6ec2017-10-16 10:32:50 +02004863 mdsc->mdsmap_err = err;
Yan, Zheng430afba2016-07-08 11:25:38 +08004864 __wake_requests(mdsc, &mdsc->waiting_for_map);
4865 mutex_unlock(&mdsc->mutex);
Yan, Zheng430afba2016-07-08 11:25:38 +08004866}
Sage Weil2f2dc052009-10-06 11:31:09 -07004867
4868/*
4869 * handle mds map update.
4870 */
Yan, Zheng430afba2016-07-08 11:25:38 +08004871void ceph_mdsc_handle_mdsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07004872{
4873 u32 epoch;
4874 u32 maplen;
4875 void *p = msg->front.iov_base;
4876 void *end = p + msg->front.iov_len;
4877 struct ceph_mdsmap *newmap, *oldmap;
4878 struct ceph_fsid fsid;
4879 int err = -EINVAL;
4880
4881 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
4882 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004883 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
Sage Weil07433042009-11-18 16:50:41 -08004884 return;
Sage Weilc89136e2009-10-14 09:59:09 -07004885 epoch = ceph_decode_32(&p);
4886 maplen = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07004887 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
4888
4889 /* do we need it? */
Sage Weil2f2dc052009-10-06 11:31:09 -07004890 mutex_lock(&mdsc->mutex);
4891 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
4892 dout("handle_map epoch %u <= our %u\n",
4893 epoch, mdsc->mdsmap->m_epoch);
4894 mutex_unlock(&mdsc->mutex);
4895 return;
4896 }
4897
4898 newmap = ceph_mdsmap_decode(&p, end);
4899 if (IS_ERR(newmap)) {
4900 err = PTR_ERR(newmap);
4901 goto bad_unlock;
4902 }
4903
4904 /* swap into place */
4905 if (mdsc->mdsmap) {
4906 oldmap = mdsc->mdsmap;
4907 mdsc->mdsmap = newmap;
4908 check_new_map(mdsc, newmap, oldmap);
4909 ceph_mdsmap_destroy(oldmap);
4910 } else {
4911 mdsc->mdsmap = newmap; /* first mds map */
4912 }
Chengguang Xu719784b2018-07-19 22:15:24 +08004913 mdsc->fsc->max_file_size = min((loff_t)mdsc->mdsmap->m_max_file_size,
4914 MAX_LFS_FILESIZE);
Sage Weil2f2dc052009-10-06 11:31:09 -07004915
4916 __wake_requests(mdsc, &mdsc->waiting_for_map);
Ilya Dryomov82dcaba2016-01-19 16:19:06 +01004917 ceph_monc_got_map(&mdsc->fsc->client->monc, CEPH_SUB_MDSMAP,
4918 mdsc->mdsmap->m_epoch);
Sage Weil2f2dc052009-10-06 11:31:09 -07004919
4920 mutex_unlock(&mdsc->mutex);
4921 schedule_delayed(mdsc);
4922 return;
4923
4924bad_unlock:
4925 mutex_unlock(&mdsc->mutex);
4926bad:
4927 pr_err("error decoding mdsmap %d\n", err);
4928 return;
4929}
4930
4931static struct ceph_connection *con_get(struct ceph_connection *con)
4932{
4933 struct ceph_mds_session *s = con->private;
4934
Xiubo Li5b3248c2019-12-19 19:44:09 -05004935 if (ceph_get_mds_session(s))
Sage Weil2f2dc052009-10-06 11:31:09 -07004936 return con;
Sage Weil2f2dc052009-10-06 11:31:09 -07004937 return NULL;
4938}
4939
4940static void con_put(struct ceph_connection *con)
4941{
4942 struct ceph_mds_session *s = con->private;
4943
Sage Weil2f2dc052009-10-06 11:31:09 -07004944 ceph_put_mds_session(s);
4945}
4946
4947/*
4948 * if the client is unresponsive for long enough, the mds will kill
4949 * the session entirely.
4950 */
4951static void peer_reset(struct ceph_connection *con)
4952{
4953 struct ceph_mds_session *s = con->private;
Sage Weil7e70f0e2010-03-18 13:59:12 -07004954 struct ceph_mds_client *mdsc = s->s_mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07004955
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07004956 pr_warn("mds%d closed our session\n", s->s_mds);
Sage Weil7e70f0e2010-03-18 13:59:12 -07004957 send_mds_reconnect(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07004958}
4959
4960static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
4961{
4962 struct ceph_mds_session *s = con->private;
4963 struct ceph_mds_client *mdsc = s->s_mdsc;
4964 int type = le16_to_cpu(msg->hdr.type);
4965
Sage Weil2600d2d2010-02-22 15:12:16 -08004966 mutex_lock(&mdsc->mutex);
4967 if (__verify_registered_session(mdsc, s) < 0) {
4968 mutex_unlock(&mdsc->mutex);
4969 goto out;
4970 }
4971 mutex_unlock(&mdsc->mutex);
4972
Sage Weil2f2dc052009-10-06 11:31:09 -07004973 switch (type) {
4974 case CEPH_MSG_MDS_MAP:
Yan, Zheng430afba2016-07-08 11:25:38 +08004975 ceph_mdsc_handle_mdsmap(mdsc, msg);
4976 break;
4977 case CEPH_MSG_FS_MAP_USER:
4978 ceph_mdsc_handle_fsmap(mdsc, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004979 break;
4980 case CEPH_MSG_CLIENT_SESSION:
4981 handle_session(s, msg);
4982 break;
4983 case CEPH_MSG_CLIENT_REPLY:
4984 handle_reply(s, msg);
4985 break;
4986 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
Sage Weil2600d2d2010-02-22 15:12:16 -08004987 handle_forward(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004988 break;
4989 case CEPH_MSG_CLIENT_CAPS:
4990 ceph_handle_caps(s, msg);
4991 break;
4992 case CEPH_MSG_CLIENT_SNAP:
Sage Weil2600d2d2010-02-22 15:12:16 -08004993 ceph_handle_snap(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004994 break;
4995 case CEPH_MSG_CLIENT_LEASE:
Sage Weil2600d2d2010-02-22 15:12:16 -08004996 handle_lease(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004997 break;
Luis Henriquesfb18a572018-01-05 10:47:18 +00004998 case CEPH_MSG_CLIENT_QUOTA:
4999 ceph_handle_quota(mdsc, s, msg);
5000 break;
Sage Weil2f2dc052009-10-06 11:31:09 -07005001
5002 default:
5003 pr_err("received unknown message type %d %s\n", type,
5004 ceph_msg_type_name(type));
5005 }
Sage Weil2600d2d2010-02-22 15:12:16 -08005006out:
Sage Weil2f2dc052009-10-06 11:31:09 -07005007 ceph_msg_put(msg);
5008}
5009
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005010/*
5011 * authentication
5012 */
Alex Eldera3530df2012-05-16 15:16:39 -05005013
5014/*
5015 * Note: returned pointer is the address of a structure that's
5016 * managed separately. Caller must *not* attempt to free it.
5017 */
5018static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05005019 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005020{
5021 struct ceph_mds_session *s = con->private;
5022 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07005023 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05005024 struct ceph_auth_handshake *auth = &s->s_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005025
Alex Elder74f18692012-05-16 15:16:39 -05005026 if (force_new && auth->authorizer) {
Ilya Dryomov6c1ea262016-04-11 19:34:49 +02005027 ceph_auth_destroy_authorizer(auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05005028 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005029 }
Sage Weil27859f92013-03-25 10:26:14 -07005030 if (!auth->authorizer) {
5031 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
5032 auth);
Sage Weil0bed9b52013-03-25 10:26:01 -07005033 if (ret)
5034 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07005035 } else {
5036 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
5037 auth);
Alex Eldera2556512012-05-16 15:16:39 -05005038 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05005039 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005040 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005041 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05005042
Alex Eldera3530df2012-05-16 15:16:39 -05005043 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005044}
5045
Ilya Dryomov6daca132018-07-27 19:18:34 +02005046static int add_authorizer_challenge(struct ceph_connection *con,
5047 void *challenge_buf, int challenge_buf_len)
5048{
5049 struct ceph_mds_session *s = con->private;
5050 struct ceph_mds_client *mdsc = s->s_mdsc;
5051 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
5052
5053 return ceph_auth_add_authorizer_challenge(ac, s->s_auth.authorizer,
5054 challenge_buf, challenge_buf_len);
5055}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005056
Ilya Dryomov0dde5842016-12-02 16:35:09 +01005057static int verify_authorizer_reply(struct ceph_connection *con)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005058{
5059 struct ceph_mds_session *s = con->private;
5060 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07005061 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005062
Ilya Dryomov0dde5842016-12-02 16:35:09 +01005063 return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005064}
5065
Sage Weil9bd2e6f2010-02-02 16:21:06 -08005066static int invalidate_authorizer(struct ceph_connection *con)
5067{
5068 struct ceph_mds_session *s = con->private;
5069 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07005070 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil9bd2e6f2010-02-02 16:21:06 -08005071
Sage Weil27859f92013-03-25 10:26:14 -07005072 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08005073
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07005074 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08005075}
5076
Alex Elder53ded492013-03-01 18:00:14 -06005077static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
5078 struct ceph_msg_header *hdr, int *skip)
5079{
5080 struct ceph_msg *msg;
5081 int type = (int) le16_to_cpu(hdr->type);
5082 int front_len = (int) le32_to_cpu(hdr->front_len);
5083
5084 if (con->in_msg)
5085 return con->in_msg;
5086
5087 *skip = 0;
5088 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
5089 if (!msg) {
5090 pr_err("unable to allocate msg type %d len %d\n",
5091 type, front_len);
5092 return NULL;
5093 }
Alex Elder53ded492013-03-01 18:00:14 -06005094
5095 return msg;
5096}
5097
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005098static int mds_sign_message(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08005099{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005100 struct ceph_mds_session *s = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08005101 struct ceph_auth_handshake *auth = &s->s_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005102
Yan, Zheng33d07332014-11-04 16:33:37 +08005103 return ceph_auth_sign_message(auth, msg);
5104}
5105
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005106static int mds_check_message_signature(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08005107{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005108 struct ceph_mds_session *s = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08005109 struct ceph_auth_handshake *auth = &s->s_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005110
Yan, Zheng33d07332014-11-04 16:33:37 +08005111 return ceph_auth_check_message_signature(auth, msg);
5112}
5113
Tobias Klauser9e327892010-05-20 10:40:19 +02005114static const struct ceph_connection_operations mds_con_ops = {
Sage Weil2f2dc052009-10-06 11:31:09 -07005115 .get = con_get,
5116 .put = con_put,
5117 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005118 .get_authorizer = get_authorizer,
Ilya Dryomov6daca132018-07-27 19:18:34 +02005119 .add_authorizer_challenge = add_authorizer_challenge,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005120 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08005121 .invalidate_authorizer = invalidate_authorizer,
Sage Weil2f2dc052009-10-06 11:31:09 -07005122 .peer_reset = peer_reset,
Alex Elder53ded492013-03-01 18:00:14 -06005123 .alloc_msg = mds_alloc_msg,
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005124 .sign_message = mds_sign_message,
5125 .check_message_signature = mds_check_message_signature,
Sage Weil2f2dc052009-10-06 11:31:09 -07005126};
5127
Sage Weil2f2dc052009-10-06 11:31:09 -07005128/* eof */