blob: 81eaf5212eeed9180253e542397390285716529e [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)) {
Sam Lang6e8575f2012-12-28 09:56:46 -0800519 info->has_create_ino = true;
Jeff Layton06a1ad42020-09-29 19:32:19 -0400520 /* struct_v, struct_compat, and len */
521 ceph_decode_skip_n(p, end, 2 + sizeof(u32), bad);
Jeff Layton1d3f8722019-09-26 16:05:11 -0400522 ceph_decode_64_safe(p, end, info->ino, bad);
Jeff Laytond4846482019-11-15 11:51:55 -0500523 ret = ceph_parse_deleg_inos(p, end, s);
524 if (ret)
525 return ret;
526 } else {
527 /* legacy */
528 ceph_decode_64_safe(p, end, info->ino, bad);
529 info->has_create_ino = true;
Sam Lang6e8575f2012-12-28 09:56:46 -0800530 }
Jeff Layton1d3f8722019-09-26 16:05:11 -0400531 } else {
532 if (*p != end)
533 goto bad;
Sam Lang6e8575f2012-12-28 09:56:46 -0800534 }
535
Jeff Layton1d3f8722019-09-26 16:05:11 -0400536 /* Skip over any unrecognized fields */
537 *p = end;
Sam Lang6e8575f2012-12-28 09:56:46 -0800538 return 0;
Sam Lang6e8575f2012-12-28 09:56:46 -0800539bad:
540 return -EIO;
541}
542
543/*
Herb Shiu25933ab2010-12-01 14:14:38 -0800544 * parse extra results
545 */
546static int parse_reply_info_extra(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800547 struct ceph_mds_reply_info_parsed *info,
Jeff Laytond4846482019-11-15 11:51:55 -0500548 u64 features, struct ceph_mds_session *s)
Herb Shiu25933ab2010-12-01 14:14:38 -0800549{
Jeff Layton6df8c9d2017-01-12 14:42:41 -0500550 u32 op = le32_to_cpu(info->head->op);
551
552 if (op == CEPH_MDS_OP_GETFILELOCK)
Sage Weil14303d22010-12-14 17:37:52 -0800553 return parse_reply_info_filelock(p, end, info, features);
Jeff Layton6df8c9d2017-01-12 14:42:41 -0500554 else if (op == CEPH_MDS_OP_READDIR || op == CEPH_MDS_OP_LSSNAP)
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800555 return parse_reply_info_readdir(p, end, info, features);
Jeff Layton6df8c9d2017-01-12 14:42:41 -0500556 else if (op == CEPH_MDS_OP_CREATE)
Jeff Laytond4846482019-11-15 11:51:55 -0500557 return parse_reply_info_create(p, end, info, features, s);
Sam Lang6e8575f2012-12-28 09:56:46 -0800558 else
559 return -EIO;
Herb Shiu25933ab2010-12-01 14:14:38 -0800560}
561
562/*
Sage Weil2f2dc052009-10-06 11:31:09 -0700563 * parse entire mds reply
564 */
Jeff Laytond4846482019-11-15 11:51:55 -0500565static int parse_reply_info(struct ceph_mds_session *s, struct ceph_msg *msg,
Sage Weil14303d22010-12-14 17:37:52 -0800566 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200567 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700568{
569 void *p, *end;
570 u32 len;
571 int err;
572
573 info->head = msg->front.iov_base;
574 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
575 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
576
577 /* trace */
578 ceph_decode_32_safe(&p, end, len, bad);
579 if (len > 0) {
Xi Wang32852a82012-01-14 22:20:59 -0500580 ceph_decode_need(&p, end, len, bad);
Sage Weil14303d22010-12-14 17:37:52 -0800581 err = parse_reply_info_trace(&p, p+len, info, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700582 if (err < 0)
583 goto out_bad;
584 }
585
Herb Shiu25933ab2010-12-01 14:14:38 -0800586 /* extra */
Sage Weil2f2dc052009-10-06 11:31:09 -0700587 ceph_decode_32_safe(&p, end, len, bad);
588 if (len > 0) {
Xi Wang32852a82012-01-14 22:20:59 -0500589 ceph_decode_need(&p, end, len, bad);
Jeff Laytond4846482019-11-15 11:51:55 -0500590 err = parse_reply_info_extra(&p, p+len, info, features, s);
Sage Weil2f2dc052009-10-06 11:31:09 -0700591 if (err < 0)
592 goto out_bad;
593 }
594
595 /* snap blob */
596 ceph_decode_32_safe(&p, end, len, bad);
597 info->snapblob_len = len;
598 info->snapblob = p;
599 p += len;
600
601 if (p != end)
602 goto bad;
603 return 0;
604
605bad:
606 err = -EIO;
607out_bad:
608 pr_err("mds parse_reply err %d\n", err);
609 return err;
610}
611
612static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
613{
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800614 if (!info->dir_entries)
Yan, Zheng54008392014-03-29 13:41:15 +0800615 return;
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800616 free_pages((unsigned long)info->dir_entries, get_order(info->dir_buf_size));
Sage Weil2f2dc052009-10-06 11:31:09 -0700617}
618
619
620/*
621 * sessions
622 */
John Spraya687eca2014-09-19 13:51:08 +0100623const char *ceph_session_state_name(int s)
Sage Weil2f2dc052009-10-06 11:31:09 -0700624{
625 switch (s) {
626 case CEPH_MDS_SESSION_NEW: return "new";
627 case CEPH_MDS_SESSION_OPENING: return "opening";
628 case CEPH_MDS_SESSION_OPEN: return "open";
629 case CEPH_MDS_SESSION_HUNG: return "hung";
630 case CEPH_MDS_SESSION_CLOSING: return "closing";
Xiubo Li4d681c22019-12-05 22:35:51 -0500631 case CEPH_MDS_SESSION_CLOSED: return "closed";
Sage Weil44ca18f2010-02-15 12:08:46 -0800632 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
Sage Weil2f2dc052009-10-06 11:31:09 -0700633 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
Yan, Zhengfcff4152016-09-14 16:39:51 +0800634 case CEPH_MDS_SESSION_REJECTED: return "rejected";
Sage Weil2f2dc052009-10-06 11:31:09 -0700635 default: return "???";
636 }
637}
638
Xiubo Li5b3248c2019-12-19 19:44:09 -0500639struct ceph_mds_session *ceph_get_mds_session(struct ceph_mds_session *s)
Sage Weil2f2dc052009-10-06 11:31:09 -0700640{
Elena Reshetova3997c012017-03-03 11:15:06 +0200641 if (refcount_inc_not_zero(&s->s_ref)) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700642 dout("mdsc get_session %p %d -> %d\n", s,
Elena Reshetova3997c012017-03-03 11:15:06 +0200643 refcount_read(&s->s_ref)-1, refcount_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -0700644 return s;
645 } else {
Chengguang Xu4c069a52018-01-30 16:29:17 +0800646 dout("mdsc get_session %p 0 -- FAIL\n", s);
Sage Weil2f2dc052009-10-06 11:31:09 -0700647 return NULL;
648 }
649}
650
651void ceph_put_mds_session(struct ceph_mds_session *s)
652{
653 dout("mdsc put_session %p %d -> %d\n", s,
Elena Reshetova3997c012017-03-03 11:15:06 +0200654 refcount_read(&s->s_ref), refcount_read(&s->s_ref)-1);
655 if (refcount_dec_and_test(&s->s_ref)) {
Alex Elder6c4a1912012-05-16 15:16:38 -0500656 if (s->s_auth.authorizer)
Ilya Dryomov6c1ea262016-04-11 19:34:49 +0200657 ceph_auth_destroy_authorizer(s->s_auth.authorizer);
Jeff Layton88828192020-03-20 17:07:36 -0400658 WARN_ON(mutex_is_locked(&s->s_mutex));
Jeff Laytond4846482019-11-15 11:51:55 -0500659 xa_destroy(&s->s_delegated_inos);
Sage Weil2f2dc052009-10-06 11:31:09 -0700660 kfree(s);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800661 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700662}
663
664/*
665 * called under mdsc->mutex
666 */
667struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
668 int mds)
669{
Markus Elfringd37b1d92017-08-20 20:22:02 +0200670 if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
Sage Weil2f2dc052009-10-06 11:31:09 -0700671 return NULL;
Xiubo Li5b3248c2019-12-19 19:44:09 -0500672 return ceph_get_mds_session(mdsc->sessions[mds]);
Sage Weil2f2dc052009-10-06 11:31:09 -0700673}
674
675static bool __have_session(struct ceph_mds_client *mdsc, int mds)
676{
Chengguang Xu98cfda82018-03-13 10:43:45 +0800677 if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
Sage Weil2f2dc052009-10-06 11:31:09 -0700678 return false;
Chengguang Xu98cfda82018-03-13 10:43:45 +0800679 else
680 return true;
Sage Weil2f2dc052009-10-06 11:31:09 -0700681}
682
Sage Weil2600d2d2010-02-22 15:12:16 -0800683static int __verify_registered_session(struct ceph_mds_client *mdsc,
684 struct ceph_mds_session *s)
685{
686 if (s->s_mds >= mdsc->max_sessions ||
687 mdsc->sessions[s->s_mds] != s)
688 return -ENOENT;
689 return 0;
690}
691
Sage Weil2f2dc052009-10-06 11:31:09 -0700692/*
693 * create+register a new session for given mds.
694 * called under mdsc->mutex.
695 */
696static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
697 int mds)
698{
699 struct ceph_mds_session *s;
700
Xiubo Lib38c9eb2019-12-04 06:57:39 -0500701 if (mds >= mdsc->mdsmap->possible_max_rank)
Nathaniel Yazdanic338c072013-08-04 21:04:30 -0700702 return ERR_PTR(-EINVAL);
703
Sage Weil2f2dc052009-10-06 11:31:09 -0700704 s = kzalloc(sizeof(*s), GFP_NOFS);
Dan Carpenter4736b002010-03-20 15:30:16 +0300705 if (!s)
706 return ERR_PTR(-ENOMEM);
Chengguang Xu47474d02018-03-13 23:01:07 +0800707
708 if (mds >= mdsc->max_sessions) {
709 int newmax = 1 << get_count_order(mds + 1);
710 struct ceph_mds_session **sa;
711
712 dout("%s: realloc to %d\n", __func__, newmax);
713 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
714 if (!sa)
715 goto fail_realloc;
716 if (mdsc->sessions) {
717 memcpy(sa, mdsc->sessions,
718 mdsc->max_sessions * sizeof(void *));
719 kfree(mdsc->sessions);
720 }
721 mdsc->sessions = sa;
722 mdsc->max_sessions = newmax;
723 }
724
725 dout("%s: mds%d\n", __func__, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -0700726 s->s_mdsc = mdsc;
727 s->s_mds = mds;
728 s->s_state = CEPH_MDS_SESSION_NEW;
729 s->s_ttl = 0;
730 s->s_seq = 0;
731 mutex_init(&s->s_mutex);
732
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700733 ceph_con_init(&s->s_con, s, &mds_con_ops, &mdsc->fsc->client->msgr);
Sage Weil2f2dc052009-10-06 11:31:09 -0700734
Alex Elderd8fb02a2012-01-12 17:48:10 -0800735 spin_lock_init(&s->s_gen_ttl_lock);
Yan, Zheng1e9c2eb2019-01-28 20:43:55 +0800736 s->s_cap_gen = 1;
Alex Elder1ce208a2012-01-12 17:48:11 -0800737 s->s_cap_ttl = jiffies - 1;
Alex Elderd8fb02a2012-01-12 17:48:10 -0800738
739 spin_lock_init(&s->s_cap_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700740 s->s_renew_requested = 0;
741 s->s_renew_seq = 0;
742 INIT_LIST_HEAD(&s->s_caps);
743 s->s_nr_caps = 0;
Elena Reshetova3997c012017-03-03 11:15:06 +0200744 refcount_set(&s->s_ref, 1);
Sage Weil2f2dc052009-10-06 11:31:09 -0700745 INIT_LIST_HEAD(&s->s_waiting);
746 INIT_LIST_HEAD(&s->s_unsafe);
Jeff Laytond4846482019-11-15 11:51:55 -0500747 xa_init(&s->s_delegated_inos);
Sage Weil2f2dc052009-10-06 11:31:09 -0700748 s->s_num_cap_releases = 0;
Yan, Zheng99a9c272013-09-22 11:08:14 +0800749 s->s_cap_reconnect = 0;
Sage Weil7c1332b2010-02-16 11:39:45 -0800750 s->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700751 INIT_LIST_HEAD(&s->s_cap_releases);
Yan, Zhenge3ec8d62019-01-14 17:21:19 +0800752 INIT_WORK(&s->s_cap_release_work, ceph_cap_release_work);
753
Jeff Layton1cf03a62020-04-01 17:07:52 -0400754 INIT_LIST_HEAD(&s->s_cap_dirty);
Sage Weil2f2dc052009-10-06 11:31:09 -0700755 INIT_LIST_HEAD(&s->s_cap_flushing);
Sage Weil2f2dc052009-10-06 11:31:09 -0700756
Sage Weil2f2dc052009-10-06 11:31:09 -0700757 mdsc->sessions[mds] = s;
Yan, Zheng86d8f672015-01-09 17:00:42 +0800758 atomic_inc(&mdsc->num_sessions);
Elena Reshetova3997c012017-03-03 11:15:06 +0200759 refcount_inc(&s->s_ref); /* one ref to sessions[], one to caller */
Sage Weil42ce56e2009-11-18 11:22:36 -0800760
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700761 ceph_con_open(&s->s_con, CEPH_ENTITY_TYPE_MDS, mds,
762 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
Sage Weil42ce56e2009-11-18 11:22:36 -0800763
Sage Weil2f2dc052009-10-06 11:31:09 -0700764 return s;
Sage Weil42ce56e2009-11-18 11:22:36 -0800765
766fail_realloc:
767 kfree(s);
768 return ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -0700769}
770
771/*
772 * called under mdsc->mutex
773 */
Sage Weil2600d2d2010-02-22 15:12:16 -0800774static void __unregister_session(struct ceph_mds_client *mdsc,
Sage Weil42ce56e2009-11-18 11:22:36 -0800775 struct ceph_mds_session *s)
Sage Weil2f2dc052009-10-06 11:31:09 -0700776{
Sage Weil2600d2d2010-02-22 15:12:16 -0800777 dout("__unregister_session mds%d %p\n", s->s_mds, s);
778 BUG_ON(mdsc->sessions[s->s_mds] != s);
Sage Weil42ce56e2009-11-18 11:22:36 -0800779 mdsc->sessions[s->s_mds] = NULL;
780 ceph_con_close(&s->s_con);
781 ceph_put_mds_session(s);
Yan, Zheng86d8f672015-01-09 17:00:42 +0800782 atomic_dec(&mdsc->num_sessions);
Sage Weil2f2dc052009-10-06 11:31:09 -0700783}
784
785/*
786 * drop session refs in request.
787 *
788 * should be last request ref, or hold mdsc->mutex
789 */
790static void put_request_session(struct ceph_mds_request *req)
791{
792 if (req->r_session) {
793 ceph_put_mds_session(req->r_session);
794 req->r_session = NULL;
795 }
796}
797
Sage Weil153c8e62009-12-07 12:31:09 -0800798void ceph_mdsc_release_request(struct kref *kref)
Sage Weil2f2dc052009-10-06 11:31:09 -0700799{
Sage Weil153c8e62009-12-07 12:31:09 -0800800 struct ceph_mds_request *req = container_of(kref,
801 struct ceph_mds_request,
802 r_kref);
Xiubo Lie64f44a2020-05-27 09:09:27 -0400803 ceph_mdsc_release_dir_caps_no_check(req);
Yan, Zheng54008392014-03-29 13:41:15 +0800804 destroy_reply_info(&req->r_reply_info);
Sage Weil153c8e62009-12-07 12:31:09 -0800805 if (req->r_request)
806 ceph_msg_put(req->r_request);
Yan, Zheng54008392014-03-29 13:41:15 +0800807 if (req->r_reply)
Sage Weil153c8e62009-12-07 12:31:09 -0800808 ceph_msg_put(req->r_reply);
Sage Weil153c8e62009-12-07 12:31:09 -0800809 if (req->r_inode) {
Sage Weil41b02e12011-07-26 11:31:14 -0700810 ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
Yan, Zheng3e1d0452019-05-18 20:39:55 +0800811 /* avoid calling iput_final() in mds dispatch threads */
812 ceph_async_iput(req->r_inode);
Sage Weil153c8e62009-12-07 12:31:09 -0800813 }
Jeff Layton9c1c2b32019-04-03 13:16:01 -0400814 if (req->r_parent) {
Jeff Layton3dd69aa2017-01-31 10:28:26 -0500815 ceph_put_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
Jeff Layton9c1c2b32019-04-03 13:16:01 -0400816 ceph_async_iput(req->r_parent);
817 }
Yan, Zheng3e1d0452019-05-18 20:39:55 +0800818 ceph_async_iput(req->r_target_inode);
Sage Weil153c8e62009-12-07 12:31:09 -0800819 if (req->r_dentry)
820 dput(req->r_dentry);
Sage Weil844d87c2013-02-05 13:40:09 -0800821 if (req->r_old_dentry)
822 dput(req->r_old_dentry);
823 if (req->r_old_dentry_dir) {
Sage Weil41b02e12011-07-26 11:31:14 -0700824 /*
825 * track (and drop pins for) r_old_dentry_dir
826 * separately, since r_old_dentry's d_parent may have
827 * changed between the dir mutex being dropped and
828 * this request being freed.
829 */
830 ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir),
831 CEPH_CAP_PIN);
Yan, Zheng3e1d0452019-05-18 20:39:55 +0800832 ceph_async_iput(req->r_old_dentry_dir);
Sage Weil153c8e62009-12-07 12:31:09 -0800833 }
834 kfree(req->r_path1);
835 kfree(req->r_path2);
Yan, Zheng25e6bae2014-09-16 19:15:28 +0800836 if (req->r_pagelist)
837 ceph_pagelist_release(req->r_pagelist);
Sage Weil153c8e62009-12-07 12:31:09 -0800838 put_request_session(req);
Yehuda Sadeh37151662010-06-17 16:16:12 -0700839 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
Yan, Zheng428138c2019-06-14 10:55:05 +0800840 WARN_ON_ONCE(!list_empty(&req->r_wait));
Jeff Layton058daab2020-02-17 18:38:37 -0500841 kmem_cache_free(ceph_mds_request_cachep, req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700842}
843
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200844DEFINE_RB_FUNCS(request, struct ceph_mds_request, r_tid, r_node)
845
Sage Weil2f2dc052009-10-06 11:31:09 -0700846/*
847 * lookup session, bump ref if found.
848 *
849 * called under mdsc->mutex.
850 */
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200851static struct ceph_mds_request *
852lookup_get_request(struct ceph_mds_client *mdsc, u64 tid)
Sage Weil2f2dc052009-10-06 11:31:09 -0700853{
854 struct ceph_mds_request *req;
Sage Weil44ca18f2010-02-15 12:08:46 -0800855
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200856 req = lookup_request(&mdsc->request_tree, tid);
857 if (req)
858 ceph_mdsc_get_request(req);
Sage Weil44ca18f2010-02-15 12:08:46 -0800859
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200860 return req;
Sage Weil2f2dc052009-10-06 11:31:09 -0700861}
862
863/*
864 * Register an in-flight request, and assign a tid. Link to directory
865 * are modifying (if any).
866 *
867 * Called under mdsc->mutex.
868 */
869static void __register_request(struct ceph_mds_client *mdsc,
870 struct ceph_mds_request *req,
871 struct inode *dir)
872{
Zhi Zhange30ee582018-01-24 21:24:33 +0800873 int ret = 0;
874
Sage Weil2f2dc052009-10-06 11:31:09 -0700875 req->r_tid = ++mdsc->last_tid;
Zhi Zhange30ee582018-01-24 21:24:33 +0800876 if (req->r_num_caps) {
877 ret = ceph_reserve_caps(mdsc, &req->r_caps_reservation,
878 req->r_num_caps);
879 if (ret < 0) {
880 pr_err("__register_request %p "
881 "failed to reserve caps: %d\n", req, ret);
882 /* set req->r_err to fail early from __do_request */
883 req->r_err = ret;
884 return;
885 }
886 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700887 dout("__register_request %p tid %lld\n", req, req->r_tid);
888 ceph_mdsc_get_request(req);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200889 insert_request(&mdsc->request_tree, req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700890
Sage Weilcb4276c2010-11-08 07:28:52 -0800891 req->r_uid = current_fsuid();
892 req->r_gid = current_fsgid();
893
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +0800894 if (mdsc->oldest_tid == 0 && req->r_op != CEPH_MDS_OP_SETFILELOCK)
895 mdsc->oldest_tid = req->r_tid;
896
Sage Weil2f2dc052009-10-06 11:31:09 -0700897 if (dir) {
Jeff Layton3db0a2f2019-04-04 08:05:38 -0400898 struct ceph_inode_info *ci = ceph_inode(dir);
899
Sage Weil3b663782011-05-18 16:12:12 -0700900 ihold(dir);
Sage Weil2f2dc052009-10-06 11:31:09 -0700901 req->r_unsafe_dir = dir;
Jeff Layton3db0a2f2019-04-04 08:05:38 -0400902 spin_lock(&ci->i_unsafe_lock);
903 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
904 spin_unlock(&ci->i_unsafe_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700905 }
906}
907
908static void __unregister_request(struct ceph_mds_client *mdsc,
909 struct ceph_mds_request *req)
910{
911 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +0800912
Jeff Laytondf963ea2017-02-14 10:09:40 -0500913 /* Never leave an unregistered request on an unsafe list! */
914 list_del_init(&req->r_unsafe_item);
915
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +0800916 if (req->r_tid == mdsc->oldest_tid) {
917 struct rb_node *p = rb_next(&req->r_node);
918 mdsc->oldest_tid = 0;
919 while (p) {
920 struct ceph_mds_request *next_req =
921 rb_entry(p, struct ceph_mds_request, r_node);
922 if (next_req->r_op != CEPH_MDS_OP_SETFILELOCK) {
923 mdsc->oldest_tid = next_req->r_tid;
924 break;
925 }
926 p = rb_next(p);
927 }
928 }
929
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200930 erase_request(&mdsc->request_tree, req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700931
Jeff Layton3db0a2f2019-04-04 08:05:38 -0400932 if (req->r_unsafe_dir) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700933 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
Sage Weil2f2dc052009-10-06 11:31:09 -0700934 spin_lock(&ci->i_unsafe_lock);
935 list_del_init(&req->r_unsafe_dir_item);
936 spin_unlock(&ci->i_unsafe_lock);
Yan, Zheng4c06ace2015-10-27 17:18:00 +0800937 }
Jeff Laytonbc2de10d2017-02-01 13:49:09 -0500938 if (req->r_target_inode &&
939 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Yan, Zheng68cd5b42015-10-27 18:36:06 +0800940 struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
941 spin_lock(&ci->i_unsafe_lock);
942 list_del_init(&req->r_unsafe_target_item);
943 spin_unlock(&ci->i_unsafe_lock);
944 }
Sage Weil3b663782011-05-18 16:12:12 -0700945
Yan, Zheng4c06ace2015-10-27 17:18:00 +0800946 if (req->r_unsafe_dir) {
Yan, Zheng3e1d0452019-05-18 20:39:55 +0800947 /* avoid calling iput_final() in mds dispatch threads */
948 ceph_async_iput(req->r_unsafe_dir);
Sage Weil3b663782011-05-18 16:12:12 -0700949 req->r_unsafe_dir = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700950 }
Sage Weil94aa8ae2010-03-28 21:22:50 -0700951
Yan, Zhengfc55d2c2013-10-31 09:10:47 +0800952 complete_all(&req->r_safe_completion);
953
Sage Weil94aa8ae2010-03-28 21:22:50 -0700954 ceph_mdsc_put_request(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700955}
956
957/*
Jeff Layton30c71232016-12-15 08:37:56 -0500958 * Walk back up the dentry tree until we hit a dentry representing a
959 * non-snapshot inode. We do this using the rcu_read_lock (which must be held
960 * when calling this) to ensure that the objects won't disappear while we're
961 * working with them. Once we hit a candidate dentry, we attempt to take a
962 * reference to it, and return that as the result.
963 */
Dan Carpenterf1075482017-02-23 13:39:59 +0300964static struct inode *get_nonsnap_parent(struct dentry *dentry)
965{
966 struct inode *inode = NULL;
Jeff Layton30c71232016-12-15 08:37:56 -0500967
968 while (dentry && !IS_ROOT(dentry)) {
969 inode = d_inode_rcu(dentry);
970 if (!inode || ceph_snap(inode) == CEPH_NOSNAP)
971 break;
972 dentry = dentry->d_parent;
973 }
974 if (inode)
975 inode = igrab(inode);
976 return inode;
977}
978
979/*
Sage Weil2f2dc052009-10-06 11:31:09 -0700980 * Choose mds to send request to next. If there is a hint set in the
981 * request (e.g., due to a prior forward hint from the mds), use that.
982 * Otherwise, consult frag tree and/or caps to identify the
983 * appropriate mds. If all else fails, choose randomly.
984 *
985 * Called under mdsc->mutex.
986 */
987static int __choose_mds(struct ceph_mds_client *mdsc,
Xiubo Lic4853e92019-12-09 07:47:15 -0500988 struct ceph_mds_request *req,
989 bool *random)
Sage Weil2f2dc052009-10-06 11:31:09 -0700990{
991 struct inode *inode;
992 struct ceph_inode_info *ci;
993 struct ceph_cap *cap;
994 int mode = req->r_direct_mode;
995 int mds = -1;
996 u32 hash = req->r_direct_hash;
Jeff Laytonbc2de10d2017-02-01 13:49:09 -0500997 bool is_hash = test_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags);
Sage Weil2f2dc052009-10-06 11:31:09 -0700998
Xiubo Lic4853e92019-12-09 07:47:15 -0500999 if (random)
1000 *random = false;
1001
Sage Weil2f2dc052009-10-06 11:31:09 -07001002 /*
1003 * is there a specific mds we should try? ignore hint if we have
1004 * no session and the mds is not up (active or recovering).
1005 */
1006 if (req->r_resend_mds >= 0 &&
1007 (__have_session(mdsc, req->r_resend_mds) ||
1008 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
Xiubo Li3c802092020-01-01 22:09:37 -05001009 dout("%s using resend_mds mds%d\n", __func__,
Sage Weil2f2dc052009-10-06 11:31:09 -07001010 req->r_resend_mds);
1011 return req->r_resend_mds;
1012 }
1013
1014 if (mode == USE_RANDOM_MDS)
1015 goto random;
1016
1017 inode = NULL;
1018 if (req->r_inode) {
Yan, Zheng5d37ca12017-07-26 12:48:08 +08001019 if (ceph_snap(req->r_inode) != CEPH_SNAPDIR) {
1020 inode = req->r_inode;
1021 ihold(inode);
1022 } else {
Yan, Zheng38f340c2017-09-22 11:41:06 +08001023 /* req->r_dentry is non-null for LSSNAP request */
1024 rcu_read_lock();
1025 inode = get_nonsnap_parent(req->r_dentry);
1026 rcu_read_unlock();
Xiubo Li3c802092020-01-01 22:09:37 -05001027 dout("%s using snapdir's parent %p\n", __func__, inode);
Yan, Zheng5d37ca12017-07-26 12:48:08 +08001028 }
Yan, Zheng38f340c2017-09-22 11:41:06 +08001029 } else if (req->r_dentry) {
Sage Weild79698d2011-07-26 11:31:26 -07001030 /* ignore race with rename; old or new d_parent is okay */
Jeff Layton30c71232016-12-15 08:37:56 -05001031 struct dentry *parent;
1032 struct inode *dir;
Sage Weileb6bb1c2010-08-16 09:21:27 -07001033
Jeff Layton30c71232016-12-15 08:37:56 -05001034 rcu_read_lock();
Yan, Zheng41883ba2019-05-23 10:22:55 +08001035 parent = READ_ONCE(req->r_dentry->d_parent);
Jeff Layton3dd69aa2017-01-31 10:28:26 -05001036 dir = req->r_parent ? : d_inode_rcu(parent);
Jeff Layton30c71232016-12-15 08:37:56 -05001037
1038 if (!dir || dir->i_sb != mdsc->fsc->sb) {
1039 /* not this fs or parent went negative */
David Howells2b0143b2015-03-17 22:25:59 +00001040 inode = d_inode(req->r_dentry);
Jeff Layton30c71232016-12-15 08:37:56 -05001041 if (inode)
1042 ihold(inode);
Sage Weileb6bb1c2010-08-16 09:21:27 -07001043 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
1044 /* direct snapped/virtual snapdir requests
1045 * based on parent dir inode */
Jeff Layton30c71232016-12-15 08:37:56 -05001046 inode = get_nonsnap_parent(parent);
Xiubo Li3c802092020-01-01 22:09:37 -05001047 dout("%s using nonsnap parent %p\n", __func__, inode);
Yan, Zhengca18bed2013-11-22 14:21:44 +08001048 } else {
Sage Weileb6bb1c2010-08-16 09:21:27 -07001049 /* dentry target */
David Howells2b0143b2015-03-17 22:25:59 +00001050 inode = d_inode(req->r_dentry);
Yan, Zhengca18bed2013-11-22 14:21:44 +08001051 if (!inode || mode == USE_AUTH_MDS) {
1052 /* dir + name */
Jeff Layton30c71232016-12-15 08:37:56 -05001053 inode = igrab(dir);
Yan, Zhengca18bed2013-11-22 14:21:44 +08001054 hash = ceph_dentry_hash(dir, req->r_dentry);
1055 is_hash = true;
Jeff Layton30c71232016-12-15 08:37:56 -05001056 } else {
1057 ihold(inode);
Yan, Zhengca18bed2013-11-22 14:21:44 +08001058 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001059 }
Jeff Layton30c71232016-12-15 08:37:56 -05001060 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -07001061 }
Sage Weileb6bb1c2010-08-16 09:21:27 -07001062
Xiubo Li3c802092020-01-01 22:09:37 -05001063 dout("%s %p is_hash=%d (0x%x) mode %d\n", __func__, inode, (int)is_hash,
1064 hash, mode);
Sage Weil2f2dc052009-10-06 11:31:09 -07001065 if (!inode)
1066 goto random;
1067 ci = ceph_inode(inode);
1068
1069 if (is_hash && S_ISDIR(inode->i_mode)) {
1070 struct ceph_inode_frag frag;
1071 int found;
1072
1073 ceph_choose_frag(ci, hash, &frag, &found);
1074 if (found) {
1075 if (mode == USE_ANY_MDS && frag.ndist > 0) {
1076 u8 r;
1077
1078 /* choose a random replica */
1079 get_random_bytes(&r, 1);
1080 r %= frag.ndist;
1081 mds = frag.dist[r];
Xiubo Li3c802092020-01-01 22:09:37 -05001082 dout("%s %p %llx.%llx frag %u mds%d (%d/%d)\n",
1083 __func__, inode, ceph_vinop(inode),
1084 frag.frag, mds, (int)r, frag.ndist);
Sage Weild66bbd42011-01-21 21:16:46 -08001085 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
Xiubo Li5d476482019-11-26 07:24:22 -05001086 CEPH_MDS_STATE_ACTIVE &&
1087 !ceph_mdsmap_is_laggy(mdsc->mdsmap, mds))
Jeff Layton30c71232016-12-15 08:37:56 -05001088 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07001089 }
1090
1091 /* since this file/dir wasn't known to be
1092 * replicated, then we want to look for the
1093 * authoritative mds. */
Sage Weil2f2dc052009-10-06 11:31:09 -07001094 if (frag.mds >= 0) {
1095 /* choose auth mds */
1096 mds = frag.mds;
Xiubo Li3c802092020-01-01 22:09:37 -05001097 dout("%s %p %llx.%llx frag %u mds%d (auth)\n",
1098 __func__, inode, ceph_vinop(inode),
1099 frag.frag, mds);
Sage Weild66bbd42011-01-21 21:16:46 -08001100 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
Xiubo Li5d476482019-11-26 07:24:22 -05001101 CEPH_MDS_STATE_ACTIVE) {
Yanhu Cao224c7b62020-07-31 16:25:13 +08001102 if (!ceph_mdsmap_is_laggy(mdsc->mdsmap,
Xiubo Li5d476482019-11-26 07:24:22 -05001103 mds))
1104 goto out;
1105 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001106 }
Xiubo Li5d476482019-11-26 07:24:22 -05001107 mode = USE_AUTH_MDS;
Sage Weil2f2dc052009-10-06 11:31:09 -07001108 }
1109 }
1110
Sage Weilbe655592011-11-30 09:47:09 -08001111 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001112 cap = NULL;
1113 if (mode == USE_AUTH_MDS)
1114 cap = ci->i_auth_cap;
1115 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
1116 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
1117 if (!cap) {
Sage Weilbe655592011-11-30 09:47:09 -08001118 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng3e1d0452019-05-18 20:39:55 +08001119 ceph_async_iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07001120 goto random;
1121 }
1122 mds = cap->session->s_mds;
Xiubo Li3c802092020-01-01 22:09:37 -05001123 dout("%s %p %llx.%llx mds%d (%scap %p)\n", __func__,
Sage Weil2f2dc052009-10-06 11:31:09 -07001124 inode, ceph_vinop(inode), mds,
1125 cap == ci->i_auth_cap ? "auth " : "", cap);
Sage Weilbe655592011-11-30 09:47:09 -08001126 spin_unlock(&ci->i_ceph_lock);
Jeff Layton30c71232016-12-15 08:37:56 -05001127out:
Yan, Zheng3e1d0452019-05-18 20:39:55 +08001128 /* avoid calling iput_final() while holding mdsc->mutex or
1129 * in mds dispatch threads */
1130 ceph_async_iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07001131 return mds;
1132
1133random:
Xiubo Lic4853e92019-12-09 07:47:15 -05001134 if (random)
1135 *random = true;
1136
Sage Weil2f2dc052009-10-06 11:31:09 -07001137 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
Xiubo Li3c802092020-01-01 22:09:37 -05001138 dout("%s chose random mds%d\n", __func__, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07001139 return mds;
1140}
1141
1142
1143/*
1144 * session messages
1145 */
1146static struct ceph_msg *create_session_msg(u32 op, u64 seq)
1147{
1148 struct ceph_msg *msg;
1149 struct ceph_mds_session_head *h;
1150
Sage Weilb61c2762011-08-09 15:03:46 -07001151 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
1152 false);
Sage Weila79832f2010-04-01 16:06:19 -07001153 if (!msg) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001154 pr_err("create_session_msg ENOMEM creating msg\n");
Sage Weila79832f2010-04-01 16:06:19 -07001155 return NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001156 }
1157 h = msg->front.iov_base;
1158 h->op = cpu_to_le32(op);
1159 h->seq = cpu_to_le64(seq);
John Spraydbd0c8b2014-09-09 19:26:01 +01001160
1161 return msg;
1162}
1163
Xiubo Li9ba1e222020-01-08 05:17:31 -05001164static const unsigned char feature_bits[] = CEPHFS_FEATURES_CLIENT_SUPPORTED;
1165#define FEATURE_BYTES(c) (DIV_ROUND_UP((size_t)feature_bits[c - 1] + 1, 64) * 8)
Xiubo Lib682c6d2020-06-30 03:52:18 -04001166static int encode_supported_features(void **p, void *end)
Yan, Zheng342ce182018-05-11 18:47:29 +08001167{
Xiubo Li9ba1e222020-01-08 05:17:31 -05001168 static const size_t count = ARRAY_SIZE(feature_bits);
Yan, Zheng342ce182018-05-11 18:47:29 +08001169
1170 if (count > 0) {
1171 size_t i;
Xiubo Li9ba1e222020-01-08 05:17:31 -05001172 size_t size = FEATURE_BYTES(count);
Yan, Zheng342ce182018-05-11 18:47:29 +08001173
Xiubo Lib682c6d2020-06-30 03:52:18 -04001174 if (WARN_ON_ONCE(*p + 4 + size > end))
1175 return -ERANGE;
1176
Yan, Zheng342ce182018-05-11 18:47:29 +08001177 ceph_encode_32(p, size);
1178 memset(*p, 0, size);
1179 for (i = 0; i < count; i++)
Xiubo Li9ba1e222020-01-08 05:17:31 -05001180 ((unsigned char*)(*p))[i / 8] |= BIT(feature_bits[i] % 8);
Yan, Zheng342ce182018-05-11 18:47:29 +08001181 *p += size;
1182 } else {
Xiubo Lib682c6d2020-06-30 03:52:18 -04001183 if (WARN_ON_ONCE(*p + 4 > end))
1184 return -ERANGE;
1185
Yan, Zheng342ce182018-05-11 18:47:29 +08001186 ceph_encode_32(p, 0);
1187 }
Xiubo Lib682c6d2020-06-30 03:52:18 -04001188
1189 return 0;
Yan, Zheng342ce182018-05-11 18:47:29 +08001190}
1191
Xiubo Li3b4168d2020-07-16 10:05:58 -04001192static const unsigned char metric_bits[] = CEPHFS_METRIC_SPEC_CLIENT_SUPPORTED;
1193#define METRIC_BYTES(cnt) (DIV_ROUND_UP((size_t)metric_bits[cnt - 1] + 1, 64) * 8)
1194static int encode_metric_spec(void **p, void *end)
1195{
1196 static const size_t count = ARRAY_SIZE(metric_bits);
1197
1198 /* header */
1199 if (WARN_ON_ONCE(*p + 2 > end))
1200 return -ERANGE;
1201
1202 ceph_encode_8(p, 1); /* version */
1203 ceph_encode_8(p, 1); /* compat */
1204
1205 if (count > 0) {
1206 size_t i;
1207 size_t size = METRIC_BYTES(count);
1208
1209 if (WARN_ON_ONCE(*p + 4 + 4 + size > end))
1210 return -ERANGE;
1211
1212 /* metric spec info length */
1213 ceph_encode_32(p, 4 + size);
1214
1215 /* metric spec */
1216 ceph_encode_32(p, size);
1217 memset(*p, 0, size);
1218 for (i = 0; i < count; i++)
1219 ((unsigned char *)(*p))[i / 8] |= BIT(metric_bits[i] % 8);
1220 *p += size;
1221 } else {
1222 if (WARN_ON_ONCE(*p + 4 + 4 > end))
1223 return -ERANGE;
1224
1225 /* metric spec info length */
1226 ceph_encode_32(p, 4);
1227 /* metric spec */
1228 ceph_encode_32(p, 0);
1229 }
1230
1231 return 0;
1232}
1233
John Spraydbd0c8b2014-09-09 19:26:01 +01001234/*
1235 * session message, specialization for CEPH_SESSION_REQUEST_OPEN
1236 * to include additional client metadata fields.
1237 */
1238static struct ceph_msg *create_session_open_msg(struct ceph_mds_client *mdsc, u64 seq)
1239{
1240 struct ceph_msg *msg;
1241 struct ceph_mds_session_head *h;
1242 int i = -1;
Yan, Zheng342ce182018-05-11 18:47:29 +08001243 int extra_bytes = 0;
John Spraydbd0c8b2014-09-09 19:26:01 +01001244 int metadata_key_count = 0;
1245 struct ceph_options *opt = mdsc->fsc->client->options;
Yan, Zheng3f384952016-04-21 11:09:55 +08001246 struct ceph_mount_options *fsopt = mdsc->fsc->mount_options;
Xiubo Li9ba1e222020-01-08 05:17:31 -05001247 size_t size, count;
Yan, Zheng342ce182018-05-11 18:47:29 +08001248 void *p, *end;
Xiubo Lib682c6d2020-06-30 03:52:18 -04001249 int ret;
John Spraydbd0c8b2014-09-09 19:26:01 +01001250
Yan, Zhenga6a5ce42015-01-16 10:54:43 +08001251 const char* metadata[][2] = {
Yan, Zheng717e6f22017-09-11 12:10:08 +08001252 {"hostname", mdsc->nodename},
1253 {"kernel_version", init_utsname()->release},
Yan, Zheng3f384952016-04-21 11:09:55 +08001254 {"entity_id", opt->name ? : ""},
1255 {"root", fsopt->server_path ? : "/"},
John Spraydbd0c8b2014-09-09 19:26:01 +01001256 {NULL, NULL}
1257 };
1258
1259 /* Calculate serialized length of metadata */
Yan, Zheng342ce182018-05-11 18:47:29 +08001260 extra_bytes = 4; /* map length */
Markus Elfringd37b1d92017-08-20 20:22:02 +02001261 for (i = 0; metadata[i][0]; ++i) {
Yan, Zheng342ce182018-05-11 18:47:29 +08001262 extra_bytes += 8 + strlen(metadata[i][0]) +
John Spraydbd0c8b2014-09-09 19:26:01 +01001263 strlen(metadata[i][1]);
1264 metadata_key_count++;
1265 }
Xiubo Li9ba1e222020-01-08 05:17:31 -05001266
Yan, Zheng342ce182018-05-11 18:47:29 +08001267 /* supported feature */
Xiubo Li9ba1e222020-01-08 05:17:31 -05001268 size = 0;
1269 count = ARRAY_SIZE(feature_bits);
1270 if (count > 0)
1271 size = FEATURE_BYTES(count);
1272 extra_bytes += 4 + size;
John Spraydbd0c8b2014-09-09 19:26:01 +01001273
Xiubo Li3b4168d2020-07-16 10:05:58 -04001274 /* metric spec */
1275 size = 0;
1276 count = ARRAY_SIZE(metric_bits);
1277 if (count > 0)
1278 size = METRIC_BYTES(count);
1279 extra_bytes += 2 + 4 + 4 + size;
1280
John Spraydbd0c8b2014-09-09 19:26:01 +01001281 /* Allocate the message */
Yan, Zheng342ce182018-05-11 18:47:29 +08001282 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + extra_bytes,
John Spraydbd0c8b2014-09-09 19:26:01 +01001283 GFP_NOFS, false);
1284 if (!msg) {
1285 pr_err("create_session_msg ENOMEM creating msg\n");
Xiubo Lib682c6d2020-06-30 03:52:18 -04001286 return ERR_PTR(-ENOMEM);
John Spraydbd0c8b2014-09-09 19:26:01 +01001287 }
Yan, Zheng342ce182018-05-11 18:47:29 +08001288 p = msg->front.iov_base;
1289 end = p + msg->front.iov_len;
1290
1291 h = p;
John Spraydbd0c8b2014-09-09 19:26:01 +01001292 h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN);
1293 h->seq = cpu_to_le64(seq);
1294
1295 /*
1296 * Serialize client metadata into waiting buffer space, using
1297 * the format that userspace expects for map<string, string>
John Spray7cfa0312014-10-30 17:15:26 +00001298 *
Xiubo Li3b4168d2020-07-16 10:05:58 -04001299 * ClientSession messages with metadata are v4
John Spraydbd0c8b2014-09-09 19:26:01 +01001300 */
Xiubo Li3b4168d2020-07-16 10:05:58 -04001301 msg->hdr.version = cpu_to_le16(4);
John Spray7cfa0312014-10-30 17:15:26 +00001302 msg->hdr.compat_version = cpu_to_le16(1);
John Spraydbd0c8b2014-09-09 19:26:01 +01001303
1304 /* The write pointer, following the session_head structure */
Yan, Zheng342ce182018-05-11 18:47:29 +08001305 p += sizeof(*h);
John Spraydbd0c8b2014-09-09 19:26:01 +01001306
1307 /* Number of entries in the map */
1308 ceph_encode_32(&p, metadata_key_count);
1309
1310 /* Two length-prefixed strings for each entry in the map */
Markus Elfringd37b1d92017-08-20 20:22:02 +02001311 for (i = 0; metadata[i][0]; ++i) {
John Spraydbd0c8b2014-09-09 19:26:01 +01001312 size_t const key_len = strlen(metadata[i][0]);
1313 size_t const val_len = strlen(metadata[i][1]);
1314
1315 ceph_encode_32(&p, key_len);
1316 memcpy(p, metadata[i][0], key_len);
1317 p += key_len;
1318 ceph_encode_32(&p, val_len);
1319 memcpy(p, metadata[i][1], val_len);
1320 p += val_len;
1321 }
1322
Xiubo Lib682c6d2020-06-30 03:52:18 -04001323 ret = encode_supported_features(&p, end);
1324 if (ret) {
1325 pr_err("encode_supported_features failed!\n");
1326 ceph_msg_put(msg);
1327 return ERR_PTR(ret);
1328 }
1329
Xiubo Li3b4168d2020-07-16 10:05:58 -04001330 ret = encode_metric_spec(&p, end);
1331 if (ret) {
1332 pr_err("encode_metric_spec failed!\n");
1333 ceph_msg_put(msg);
1334 return ERR_PTR(ret);
1335 }
1336
Yan, Zheng342ce182018-05-11 18:47:29 +08001337 msg->front.iov_len = p - msg->front.iov_base;
1338 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1339
Sage Weil2f2dc052009-10-06 11:31:09 -07001340 return msg;
1341}
1342
1343/*
1344 * send session open request.
1345 *
1346 * called under mdsc->mutex
1347 */
1348static int __open_session(struct ceph_mds_client *mdsc,
1349 struct ceph_mds_session *session)
1350{
1351 struct ceph_msg *msg;
1352 int mstate;
1353 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07001354
1355 /* wait for mds to go active? */
1356 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
1357 dout("open_session to mds%d (%s)\n", mds,
1358 ceph_mds_state_name(mstate));
1359 session->s_state = CEPH_MDS_SESSION_OPENING;
1360 session->s_renew_requested = jiffies;
1361
1362 /* send connect message */
John Spraydbd0c8b2014-09-09 19:26:01 +01001363 msg = create_session_open_msg(mdsc, session->s_seq);
Xiubo Lib682c6d2020-06-30 03:52:18 -04001364 if (IS_ERR(msg))
1365 return PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001366 ceph_con_send(&session->s_con, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001367 return 0;
1368}
1369
1370/*
Sage Weiled0552a2010-06-21 13:38:25 -07001371 * open sessions for any export targets for the given mds
1372 *
1373 * called under mdsc->mutex
1374 */
Yan, Zheng5d72d132013-11-24 14:33:01 +08001375static struct ceph_mds_session *
1376__open_export_target_session(struct ceph_mds_client *mdsc, int target)
1377{
1378 struct ceph_mds_session *session;
Xiubo Lib682c6d2020-06-30 03:52:18 -04001379 int ret;
Yan, Zheng5d72d132013-11-24 14:33:01 +08001380
1381 session = __ceph_lookup_mds_session(mdsc, target);
1382 if (!session) {
1383 session = register_session(mdsc, target);
1384 if (IS_ERR(session))
1385 return session;
1386 }
1387 if (session->s_state == CEPH_MDS_SESSION_NEW ||
Xiubo Lib682c6d2020-06-30 03:52:18 -04001388 session->s_state == CEPH_MDS_SESSION_CLOSING) {
1389 ret = __open_session(mdsc, session);
1390 if (ret)
1391 return ERR_PTR(ret);
1392 }
Yan, Zheng5d72d132013-11-24 14:33:01 +08001393
1394 return session;
1395}
1396
1397struct ceph_mds_session *
1398ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target)
1399{
1400 struct ceph_mds_session *session;
1401
1402 dout("open_export_target_session to mds%d\n", target);
1403
1404 mutex_lock(&mdsc->mutex);
1405 session = __open_export_target_session(mdsc, target);
1406 mutex_unlock(&mdsc->mutex);
1407
1408 return session;
1409}
1410
Sage Weiled0552a2010-06-21 13:38:25 -07001411static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
1412 struct ceph_mds_session *session)
1413{
1414 struct ceph_mds_info *mi;
1415 struct ceph_mds_session *ts;
1416 int i, mds = session->s_mds;
Sage Weiled0552a2010-06-21 13:38:25 -07001417
Xiubo Lib38c9eb2019-12-04 06:57:39 -05001418 if (mds >= mdsc->mdsmap->possible_max_rank)
Sage Weiled0552a2010-06-21 13:38:25 -07001419 return;
Yan, Zheng5d72d132013-11-24 14:33:01 +08001420
Sage Weiled0552a2010-06-21 13:38:25 -07001421 mi = &mdsc->mdsmap->m_info[mds];
1422 dout("open_export_target_sessions for mds%d (%d targets)\n",
1423 session->s_mds, mi->num_export_targets);
1424
1425 for (i = 0; i < mi->num_export_targets; i++) {
Yan, Zheng5d72d132013-11-24 14:33:01 +08001426 ts = __open_export_target_session(mdsc, mi->export_targets[i]);
1427 if (!IS_ERR(ts))
1428 ceph_put_mds_session(ts);
Sage Weiled0552a2010-06-21 13:38:25 -07001429 }
1430}
1431
Sage Weil154f42c2010-06-21 13:45:04 -07001432void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
1433 struct ceph_mds_session *session)
1434{
1435 mutex_lock(&mdsc->mutex);
1436 __open_export_target_sessions(mdsc, session);
1437 mutex_unlock(&mdsc->mutex);
1438}
1439
Sage Weiled0552a2010-06-21 13:38:25 -07001440/*
Sage Weil2f2dc052009-10-06 11:31:09 -07001441 * session caps
1442 */
1443
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001444static void detach_cap_releases(struct ceph_mds_session *session,
1445 struct list_head *target)
Sage Weil2f2dc052009-10-06 11:31:09 -07001446{
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001447 lockdep_assert_held(&session->s_cap_lock);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001448
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001449 list_splice_init(&session->s_cap_releases, target);
1450 session->s_num_cap_releases = 0;
1451 dout("dispose_cap_releases mds%d\n", session->s_mds);
1452}
1453
1454static void dispose_cap_releases(struct ceph_mds_client *mdsc,
1455 struct list_head *dispose)
1456{
1457 while (!list_empty(dispose)) {
Yan, Zheng745a8e32015-05-14 17:22:42 +08001458 struct ceph_cap *cap;
1459 /* zero out the in-progress message */
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001460 cap = list_first_entry(dispose, struct ceph_cap, session_caps);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001461 list_del(&cap->session_caps);
1462 ceph_put_cap(mdsc, cap);
1463 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001464}
1465
Yan, Zheng1c841a92015-03-24 20:15:36 +08001466static void cleanup_session_requests(struct ceph_mds_client *mdsc,
1467 struct ceph_mds_session *session)
1468{
1469 struct ceph_mds_request *req;
1470 struct rb_node *p;
Yan, Zhengf4b97862019-07-25 20:16:42 +08001471 struct ceph_inode_info *ci;
Yan, Zheng1c841a92015-03-24 20:15:36 +08001472
1473 dout("cleanup_session_requests mds%d\n", session->s_mds);
1474 mutex_lock(&mdsc->mutex);
1475 while (!list_empty(&session->s_unsafe)) {
1476 req = list_first_entry(&session->s_unsafe,
1477 struct ceph_mds_request, r_unsafe_item);
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001478 pr_warn_ratelimited(" dropping unsafe request %llu\n",
1479 req->r_tid);
Yan, Zhengf4b97862019-07-25 20:16:42 +08001480 if (req->r_target_inode) {
1481 /* dropping unsafe change of inode's attributes */
1482 ci = ceph_inode(req->r_target_inode);
1483 errseq_set(&ci->i_meta_err, -EIO);
1484 }
1485 if (req->r_unsafe_dir) {
1486 /* dropping unsafe directory operation */
1487 ci = ceph_inode(req->r_unsafe_dir);
1488 errseq_set(&ci->i_meta_err, -EIO);
1489 }
Yan, Zheng1c841a92015-03-24 20:15:36 +08001490 __unregister_request(mdsc, req);
1491 }
1492 /* zero r_attempts, so kick_requests() will re-send requests */
1493 p = rb_first(&mdsc->request_tree);
1494 while (p) {
1495 req = rb_entry(p, struct ceph_mds_request, r_node);
1496 p = rb_next(p);
1497 if (req->r_session &&
1498 req->r_session->s_mds == session->s_mds)
1499 req->r_attempts = 0;
1500 }
1501 mutex_unlock(&mdsc->mutex);
1502}
1503
Sage Weil2f2dc052009-10-06 11:31:09 -07001504/*
Sage Weilf818a732010-05-11 20:56:31 -07001505 * Helper to safely iterate over all caps associated with a session, with
1506 * special care taken to handle a racing __ceph_remove_cap().
Sage Weil2f2dc052009-10-06 11:31:09 -07001507 *
Sage Weilf818a732010-05-11 20:56:31 -07001508 * Caller must hold session s_mutex.
Sage Weil2f2dc052009-10-06 11:31:09 -07001509 */
Jeff Laytonf5d772692019-04-24 12:09:04 -04001510int ceph_iterate_session_caps(struct ceph_mds_session *session,
1511 int (*cb)(struct inode *, struct ceph_cap *,
1512 void *), void *arg)
Sage Weil2f2dc052009-10-06 11:31:09 -07001513{
Sage Weil7c1332b2010-02-16 11:39:45 -08001514 struct list_head *p;
1515 struct ceph_cap *cap;
1516 struct inode *inode, *last_inode = NULL;
1517 struct ceph_cap *old_cap = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001518 int ret;
1519
1520 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
1521 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001522 p = session->s_caps.next;
1523 while (p != &session->s_caps) {
1524 cap = list_entry(p, struct ceph_cap, session_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -07001525 inode = igrab(&cap->ci->vfs_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001526 if (!inode) {
1527 p = p->next;
Sage Weil2f2dc052009-10-06 11:31:09 -07001528 continue;
Sage Weil7c1332b2010-02-16 11:39:45 -08001529 }
1530 session->s_cap_iterator = cap;
Sage Weil2f2dc052009-10-06 11:31:09 -07001531 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001532
1533 if (last_inode) {
Yan, Zheng3e1d0452019-05-18 20:39:55 +08001534 /* avoid calling iput_final() while holding
1535 * s_mutex or in mds dispatch threads */
1536 ceph_async_iput(last_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001537 last_inode = NULL;
1538 }
1539 if (old_cap) {
Yehuda Sadeh37151662010-06-17 16:16:12 -07001540 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -08001541 old_cap = NULL;
1542 }
1543
Sage Weil2f2dc052009-10-06 11:31:09 -07001544 ret = cb(inode, cap, arg);
Sage Weil7c1332b2010-02-16 11:39:45 -08001545 last_inode = inode;
1546
Sage Weil2f2dc052009-10-06 11:31:09 -07001547 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001548 p = p->next;
Markus Elfringd37b1d92017-08-20 20:22:02 +02001549 if (!cap->ci) {
Sage Weil7c1332b2010-02-16 11:39:45 -08001550 dout("iterate_session_caps finishing cap %p removal\n",
1551 cap);
1552 BUG_ON(cap->session != session);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001553 cap->session = NULL;
Sage Weil7c1332b2010-02-16 11:39:45 -08001554 list_del_init(&cap->session_caps);
1555 session->s_nr_caps--;
Xiubo Li4f1d7562020-06-30 03:52:16 -04001556 atomic64_dec(&session->s_mdsc->metric.total_caps);
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08001557 if (cap->queue_release)
1558 __ceph_queue_cap_release(session, cap);
1559 else
Yan, Zheng745a8e32015-05-14 17:22:42 +08001560 old_cap = cap; /* put_cap it w/o locks held */
Sage Weil7c1332b2010-02-16 11:39:45 -08001561 }
Sage Weil5dacf092009-12-21 20:40:34 -08001562 if (ret < 0)
1563 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07001564 }
Sage Weil5dacf092009-12-21 20:40:34 -08001565 ret = 0;
1566out:
Sage Weil7c1332b2010-02-16 11:39:45 -08001567 session->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001568 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001569
Yan, Zheng3e1d0452019-05-18 20:39:55 +08001570 ceph_async_iput(last_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001571 if (old_cap)
Yehuda Sadeh37151662010-06-17 16:16:12 -07001572 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -08001573
Sage Weil5dacf092009-12-21 20:40:34 -08001574 return ret;
Sage Weil2f2dc052009-10-06 11:31:09 -07001575}
1576
1577static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
Sage Weil6c99f252010-05-10 16:12:25 -07001578 void *arg)
Sage Weil2f2dc052009-10-06 11:31:09 -07001579{
Yan, Zheng6c93df52016-04-15 13:56:12 +08001580 struct ceph_fs_client *fsc = (struct ceph_fs_client *)arg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001581 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001582 LIST_HEAD(to_remove);
Yan, Zhengf4b97862019-07-25 20:16:42 +08001583 bool dirty_dropped = false;
Yan, Zheng6c93df52016-04-15 13:56:12 +08001584 bool invalidate = false;
Sage Weil6c99f252010-05-10 16:12:25 -07001585
Sage Weil2f2dc052009-10-06 11:31:09 -07001586 dout("removing cap %p, ci is %p, inode is %p\n",
1587 cap, ci, &ci->vfs_inode);
Sage Weilbe655592011-11-30 09:47:09 -08001588 spin_lock(&ci->i_ceph_lock);
Yan, Zhenga096b092013-09-22 10:15:58 +08001589 __ceph_remove_cap(cap, false);
Yan, Zheng571ade32015-03-24 11:36:08 +08001590 if (!ci->i_auth_cap) {
Yan, Zheng553adfd2015-06-09 15:48:57 +08001591 struct ceph_cap_flush *cf;
Yan, Zheng6c93df52016-04-15 13:56:12 +08001592 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil6c99f252010-05-10 16:12:25 -07001593
Jeff Layton50c91322020-09-25 07:55:39 -04001594 if (READ_ONCE(fsc->mount_state) >= CEPH_MOUNT_SHUTDOWN) {
Yan, Zhengd468e722019-07-25 20:16:44 +08001595 if (inode->i_data.nrpages > 0)
1596 invalidate = true;
1597 if (ci->i_wrbuffer_ref > 0)
1598 mapping_set_error(&inode->i_data, -EIO);
1599 }
Yan, Zheng6c93df52016-04-15 13:56:12 +08001600
Yan, Zhenge4500b52016-07-06 11:12:56 +08001601 while (!list_empty(&ci->i_cap_flush_list)) {
1602 cf = list_first_entry(&ci->i_cap_flush_list,
1603 struct ceph_cap_flush, i_list);
Wei Yongjun8cdcc072016-08-13 09:10:28 +00001604 list_move(&cf->i_list, &to_remove);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001605 }
1606
Sage Weil6c99f252010-05-10 16:12:25 -07001607 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zheng8310b082015-06-09 17:20:12 +08001608
Yan, Zhenge4500b52016-07-06 11:12:56 +08001609 list_for_each_entry(cf, &to_remove, i_list)
1610 list_del(&cf->g_list);
Yan, Zheng8310b082015-06-09 17:20:12 +08001611
Sage Weil6c99f252010-05-10 16:12:25 -07001612 if (!list_empty(&ci->i_dirty_item)) {
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001613 pr_warn_ratelimited(
1614 " dropping dirty %s state for %p %lld\n",
Sage Weil6c99f252010-05-10 16:12:25 -07001615 ceph_cap_string(ci->i_dirty_caps),
1616 inode, ceph_ino(inode));
1617 ci->i_dirty_caps = 0;
1618 list_del_init(&ci->i_dirty_item);
Yan, Zhengf4b97862019-07-25 20:16:42 +08001619 dirty_dropped = true;
Sage Weil6c99f252010-05-10 16:12:25 -07001620 }
1621 if (!list_empty(&ci->i_flushing_item)) {
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001622 pr_warn_ratelimited(
1623 " dropping dirty+flushing %s state for %p %lld\n",
Sage Weil6c99f252010-05-10 16:12:25 -07001624 ceph_cap_string(ci->i_flushing_caps),
1625 inode, ceph_ino(inode));
1626 ci->i_flushing_caps = 0;
1627 list_del_init(&ci->i_flushing_item);
1628 mdsc->num_cap_flushing--;
Yan, Zhengf4b97862019-07-25 20:16:42 +08001629 dirty_dropped = true;
Sage Weil6c99f252010-05-10 16:12:25 -07001630 }
Sage Weil6c99f252010-05-10 16:12:25 -07001631 spin_unlock(&mdsc->cap_dirty_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001632
Yan, Zhengf4b97862019-07-25 20:16:42 +08001633 if (dirty_dropped) {
1634 errseq_set(&ci->i_meta_err, -EIO);
1635
1636 if (ci->i_wrbuffer_ref_head == 0 &&
1637 ci->i_wr_ref == 0 &&
1638 ci->i_dirty_caps == 0 &&
1639 ci->i_flushing_caps == 0) {
1640 ceph_put_snap_context(ci->i_head_snapc);
1641 ci->i_head_snapc = NULL;
1642 }
1643 }
1644
Yan, Zhengb3f8d682017-09-11 10:58:55 +08001645 if (atomic_read(&ci->i_filelock_ref) > 0) {
1646 /* make further file lock syscall return -EIO */
1647 ci->i_ceph_flags |= CEPH_I_ERROR_FILELOCK;
1648 pr_warn_ratelimited(" dropping file locks for %p %lld\n",
1649 inode, ceph_ino(inode));
1650 }
1651
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001652 if (!ci->i_dirty_caps && ci->i_prealloc_cap_flush) {
Yan, Zhenge4500b52016-07-06 11:12:56 +08001653 list_add(&ci->i_prealloc_cap_flush->i_list, &to_remove);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001654 ci->i_prealloc_cap_flush = NULL;
1655 }
Sage Weil6c99f252010-05-10 16:12:25 -07001656 }
Sage Weilbe655592011-11-30 09:47:09 -08001657 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001658 while (!list_empty(&to_remove)) {
1659 struct ceph_cap_flush *cf;
1660 cf = list_first_entry(&to_remove,
Yan, Zhenge4500b52016-07-06 11:12:56 +08001661 struct ceph_cap_flush, i_list);
1662 list_del(&cf->i_list);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001663 ceph_free_cap_flush(cf);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001664 }
Yan, Zheng77310322016-04-08 15:27:16 +08001665
1666 wake_up_all(&ci->i_cap_wq);
Yan, Zheng6c93df52016-04-15 13:56:12 +08001667 if (invalidate)
1668 ceph_queue_invalidate(inode);
Yan, Zhengf4b97862019-07-25 20:16:42 +08001669 if (dirty_dropped)
Sage Weil6c99f252010-05-10 16:12:25 -07001670 iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07001671 return 0;
1672}
1673
1674/*
1675 * caller must hold session s_mutex
1676 */
1677static void remove_session_caps(struct ceph_mds_session *session)
1678{
Yan, Zheng6c93df52016-04-15 13:56:12 +08001679 struct ceph_fs_client *fsc = session->s_mdsc->fsc;
1680 struct super_block *sb = fsc->sb;
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001681 LIST_HEAD(dispose);
1682
Sage Weil2f2dc052009-10-06 11:31:09 -07001683 dout("remove_session_caps on %p\n", session);
Jeff Laytonf5d772692019-04-24 12:09:04 -04001684 ceph_iterate_session_caps(session, remove_session_caps_cb, fsc);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001685
Yan, Zhengc8799fc2016-07-07 15:22:38 +08001686 wake_up_all(&fsc->mdsc->cap_flushing_wq);
1687
Yan, Zheng6f60f882013-07-24 12:22:11 +08001688 spin_lock(&session->s_cap_lock);
1689 if (session->s_nr_caps > 0) {
Yan, Zheng6f60f882013-07-24 12:22:11 +08001690 struct inode *inode;
1691 struct ceph_cap *cap, *prev = NULL;
1692 struct ceph_vino vino;
1693 /*
1694 * iterate_session_caps() skips inodes that are being
1695 * deleted, we need to wait until deletions are complete.
1696 * __wait_on_freeing_inode() is designed for the job,
1697 * but it is not exported, so use lookup inode function
1698 * to access it.
1699 */
1700 while (!list_empty(&session->s_caps)) {
1701 cap = list_entry(session->s_caps.next,
1702 struct ceph_cap, session_caps);
1703 if (cap == prev)
1704 break;
1705 prev = cap;
1706 vino = cap->ci->i_vino;
1707 spin_unlock(&session->s_cap_lock);
1708
Yan, Zhenged284c42013-09-02 15:19:53 +08001709 inode = ceph_find_inode(sb, vino);
Yan, Zheng3e1d0452019-05-18 20:39:55 +08001710 /* avoid calling iput_final() while holding s_mutex */
1711 ceph_async_iput(inode);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001712
1713 spin_lock(&session->s_cap_lock);
1714 }
1715 }
Yan, Zheng745a8e32015-05-14 17:22:42 +08001716
1717 // drop cap expires and unlock s_cap_lock
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001718 detach_cap_releases(session, &dispose);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001719
Sage Weil2f2dc052009-10-06 11:31:09 -07001720 BUG_ON(session->s_nr_caps > 0);
Sage Weil6c99f252010-05-10 16:12:25 -07001721 BUG_ON(!list_empty(&session->s_cap_flushing));
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001722 spin_unlock(&session->s_cap_lock);
1723 dispose_cap_releases(session->s_mdsc, &dispose);
Sage Weil2f2dc052009-10-06 11:31:09 -07001724}
1725
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001726enum {
1727 RECONNECT,
1728 RENEWCAPS,
1729 FORCE_RO,
1730};
1731
Sage Weil2f2dc052009-10-06 11:31:09 -07001732/*
1733 * wake up any threads waiting on this session's caps. if the cap is
1734 * old (didn't get renewed on the client reconnect), remove it now.
1735 *
1736 * caller must hold s_mutex.
1737 */
1738static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1739 void *arg)
1740{
Sage Weil0dc25702009-11-20 13:43:45 -08001741 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001742 unsigned long ev = (unsigned long)arg;
Sage Weil0dc25702009-11-20 13:43:45 -08001743
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001744 if (ev == RECONNECT) {
Sage Weilbe655592011-11-30 09:47:09 -08001745 spin_lock(&ci->i_ceph_lock);
Sage Weil0dc25702009-11-20 13:43:45 -08001746 ci->i_wanted_max_size = 0;
1747 ci->i_requested_max_size = 0;
Sage Weilbe655592011-11-30 09:47:09 -08001748 spin_unlock(&ci->i_ceph_lock);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001749 } else if (ev == RENEWCAPS) {
1750 if (cap->cap_gen < cap->session->s_cap_gen) {
1751 /* mds did not re-issue stale cap */
1752 spin_lock(&ci->i_ceph_lock);
1753 cap->issued = cap->implemented = CEPH_CAP_PIN;
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001754 spin_unlock(&ci->i_ceph_lock);
1755 }
1756 } else if (ev == FORCE_RO) {
Sage Weil0dc25702009-11-20 13:43:45 -08001757 }
Yan, Zhenge5360302016-05-19 19:15:19 +08001758 wake_up_all(&ci->i_cap_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07001759 return 0;
1760}
1761
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001762static void wake_up_session_caps(struct ceph_mds_session *session, int ev)
Sage Weil2f2dc052009-10-06 11:31:09 -07001763{
1764 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
Jeff Laytonf5d772692019-04-24 12:09:04 -04001765 ceph_iterate_session_caps(session, wake_up_session_cb,
1766 (void *)(unsigned long)ev);
Sage Weil2f2dc052009-10-06 11:31:09 -07001767}
1768
1769/*
1770 * Send periodic message to MDS renewing all currently held caps. The
1771 * ack will reset the expiration for all caps from this session.
1772 *
1773 * caller holds s_mutex
1774 */
1775static int send_renew_caps(struct ceph_mds_client *mdsc,
1776 struct ceph_mds_session *session)
1777{
1778 struct ceph_msg *msg;
1779 int state;
1780
1781 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1782 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1783 pr_info("mds%d caps stale\n", session->s_mds);
Sage Weile4cb4cb2010-03-18 13:43:09 -07001784 session->s_renew_requested = jiffies;
Sage Weil2f2dc052009-10-06 11:31:09 -07001785
1786 /* do not try to renew caps until a recovering mds has reconnected
1787 * with its clients. */
1788 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1789 if (state < CEPH_MDS_STATE_RECONNECT) {
1790 dout("send_renew_caps ignoring mds%d (%s)\n",
1791 session->s_mds, ceph_mds_state_name(state));
1792 return 0;
1793 }
1794
1795 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1796 ceph_mds_state_name(state));
Sage Weil2f2dc052009-10-06 11:31:09 -07001797 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1798 ++session->s_renew_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001799 if (!msg)
1800 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -07001801 ceph_con_send(&session->s_con, msg);
1802 return 0;
1803}
1804
Yan, Zheng186e4f72013-11-22 14:48:37 +08001805static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
1806 struct ceph_mds_session *session, u64 seq)
1807{
1808 struct ceph_msg *msg;
1809
1810 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
John Spraya687eca2014-09-19 13:51:08 +01001811 session->s_mds, ceph_session_state_name(session->s_state), seq);
Yan, Zheng186e4f72013-11-22 14:48:37 +08001812 msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
1813 if (!msg)
1814 return -ENOMEM;
1815 ceph_con_send(&session->s_con, msg);
1816 return 0;
1817}
1818
1819
Sage Weil2f2dc052009-10-06 11:31:09 -07001820/*
1821 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
Sage Weil0dc25702009-11-20 13:43:45 -08001822 *
1823 * Called under session->s_mutex
Sage Weil2f2dc052009-10-06 11:31:09 -07001824 */
1825static void renewed_caps(struct ceph_mds_client *mdsc,
1826 struct ceph_mds_session *session, int is_renew)
1827{
1828 int was_stale;
1829 int wake = 0;
1830
1831 spin_lock(&session->s_cap_lock);
Alex Elder1ce208a2012-01-12 17:48:11 -08001832 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
Sage Weil2f2dc052009-10-06 11:31:09 -07001833
1834 session->s_cap_ttl = session->s_renew_requested +
1835 mdsc->mdsmap->m_session_timeout*HZ;
1836
1837 if (was_stale) {
1838 if (time_before(jiffies, session->s_cap_ttl)) {
1839 pr_info("mds%d caps renewed\n", session->s_mds);
1840 wake = 1;
1841 } else {
1842 pr_info("mds%d caps still stale\n", session->s_mds);
1843 }
1844 }
1845 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1846 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1847 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1848 spin_unlock(&session->s_cap_lock);
1849
1850 if (wake)
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001851 wake_up_session_caps(session, RENEWCAPS);
Sage Weil2f2dc052009-10-06 11:31:09 -07001852}
1853
1854/*
1855 * send a session close request
1856 */
Xiubo Li3e699bd2020-06-30 03:52:15 -04001857static int request_close_session(struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001858{
1859 struct ceph_msg *msg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001860
1861 dout("request_close_session mds%d state %s seq %lld\n",
John Spraya687eca2014-09-19 13:51:08 +01001862 session->s_mds, ceph_session_state_name(session->s_state),
Sage Weil2f2dc052009-10-06 11:31:09 -07001863 session->s_seq);
1864 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001865 if (!msg)
1866 return -ENOMEM;
1867 ceph_con_send(&session->s_con, msg);
Yan, Zhengfcff4152016-09-14 16:39:51 +08001868 return 1;
Sage Weil2f2dc052009-10-06 11:31:09 -07001869}
1870
1871/*
1872 * Called with s_mutex held.
1873 */
1874static int __close_session(struct ceph_mds_client *mdsc,
1875 struct ceph_mds_session *session)
1876{
1877 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1878 return 0;
1879 session->s_state = CEPH_MDS_SESSION_CLOSING;
Xiubo Li3e699bd2020-06-30 03:52:15 -04001880 return request_close_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07001881}
1882
Yan, Zheng040d7862017-11-30 11:59:22 +08001883static bool drop_negative_children(struct dentry *dentry)
1884{
1885 struct dentry *child;
1886 bool all_negative = true;
1887
1888 if (!d_is_dir(dentry))
1889 goto out;
1890
1891 spin_lock(&dentry->d_lock);
1892 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
1893 if (d_really_is_positive(child)) {
1894 all_negative = false;
1895 break;
1896 }
1897 }
1898 spin_unlock(&dentry->d_lock);
1899
1900 if (all_negative)
1901 shrink_dcache_parent(dentry);
1902out:
1903 return all_negative;
1904}
1905
Sage Weil2f2dc052009-10-06 11:31:09 -07001906/*
1907 * Trim old(er) caps.
1908 *
1909 * Because we can't cache an inode without one or more caps, we do
1910 * this indirectly: if a cap is unused, we prune its aliases, at which
1911 * point the inode will hopefully get dropped to.
1912 *
1913 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1914 * memory pressure from the MDS, though, so it needn't be perfect.
1915 */
1916static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1917{
Jeff Layton533a2812019-07-19 15:22:28 -04001918 int *remaining = arg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001919 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng979abfd2013-11-22 13:56:24 +08001920 int used, wanted, oissued, mine;
Sage Weil2f2dc052009-10-06 11:31:09 -07001921
Jeff Layton533a2812019-07-19 15:22:28 -04001922 if (*remaining <= 0)
Sage Weil2f2dc052009-10-06 11:31:09 -07001923 return -1;
1924
Sage Weilbe655592011-11-30 09:47:09 -08001925 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001926 mine = cap->issued | cap->implemented;
1927 used = __ceph_caps_used(ci);
Yan, Zheng979abfd2013-11-22 13:56:24 +08001928 wanted = __ceph_caps_file_wanted(ci);
Sage Weil2f2dc052009-10-06 11:31:09 -07001929 oissued = __ceph_caps_issued_other(ci, cap);
1930
Yan, Zheng979abfd2013-11-22 13:56:24 +08001931 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001932 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
Yan, Zheng979abfd2013-11-22 13:56:24 +08001933 ceph_cap_string(used), ceph_cap_string(wanted));
1934 if (cap == ci->i_auth_cap) {
Yan, Zheng622f3e22015-05-07 10:59:47 +08001935 if (ci->i_dirty_caps || ci->i_flushing_caps ||
1936 !list_empty(&ci->i_cap_snaps))
Yan, Zheng979abfd2013-11-22 13:56:24 +08001937 goto out;
1938 if ((used | wanted) & CEPH_CAP_ANY_WR)
1939 goto out;
Yan, Zheng89aa5932017-09-08 15:23:18 +08001940 /* Note: it's possible that i_filelock_ref becomes non-zero
1941 * after dropping auth caps. It doesn't hurt because reply
1942 * of lock mds request will re-add auth caps. */
1943 if (atomic_read(&ci->i_filelock_ref) > 0)
1944 goto out;
Yan, Zheng979abfd2013-11-22 13:56:24 +08001945 }
Yan, Zheng5e804ac2015-10-26 16:08:43 +08001946 /* The inode has cached pages, but it's no longer used.
1947 * we can safely drop it */
Yan, Zheng525d15e2019-05-11 17:27:59 +08001948 if (S_ISREG(inode->i_mode) &&
1949 wanted == 0 && used == CEPH_CAP_FILE_CACHE &&
Yan, Zheng5e804ac2015-10-26 16:08:43 +08001950 !(oissued & CEPH_CAP_FILE_CACHE)) {
1951 used = 0;
1952 oissued = 0;
1953 }
Yan, Zheng979abfd2013-11-22 13:56:24 +08001954 if ((used | wanted) & ~oissued & mine)
Sage Weil2f2dc052009-10-06 11:31:09 -07001955 goto out; /* we need these caps */
1956
Sage Weil2f2dc052009-10-06 11:31:09 -07001957 if (oissued) {
1958 /* we aren't the only cap.. just remove us */
Yan, Zhenga096b092013-09-22 10:15:58 +08001959 __ceph_remove_cap(cap, true);
Jeff Layton533a2812019-07-19 15:22:28 -04001960 (*remaining)--;
Sage Weil2f2dc052009-10-06 11:31:09 -07001961 } else {
Yan, Zheng040d7862017-11-30 11:59:22 +08001962 struct dentry *dentry;
Yan, Zheng5e804ac2015-10-26 16:08:43 +08001963 /* try dropping referring dentries */
Sage Weilbe655592011-11-30 09:47:09 -08001964 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng040d7862017-11-30 11:59:22 +08001965 dentry = d_find_any_alias(inode);
1966 if (dentry && drop_negative_children(dentry)) {
1967 int count;
1968 dput(dentry);
1969 d_prune_aliases(inode);
1970 count = atomic_read(&inode->i_count);
1971 if (count == 1)
Jeff Layton533a2812019-07-19 15:22:28 -04001972 (*remaining)--;
Yan, Zheng040d7862017-11-30 11:59:22 +08001973 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1974 inode, cap, count);
1975 } else {
1976 dput(dentry);
1977 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001978 return 0;
1979 }
1980
1981out:
Sage Weilbe655592011-11-30 09:47:09 -08001982 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001983 return 0;
1984}
1985
1986/*
1987 * Trim session cap count down to some max number.
1988 */
Zhi Zhange30ee582018-01-24 21:24:33 +08001989int ceph_trim_caps(struct ceph_mds_client *mdsc,
1990 struct ceph_mds_session *session,
1991 int max_caps)
Sage Weil2f2dc052009-10-06 11:31:09 -07001992{
1993 int trim_caps = session->s_nr_caps - max_caps;
1994
1995 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1996 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1997 if (trim_caps > 0) {
Jeff Layton533a2812019-07-19 15:22:28 -04001998 int remaining = trim_caps;
1999
2000 ceph_iterate_session_caps(session, trim_caps_cb, &remaining);
Sage Weil2f2dc052009-10-06 11:31:09 -07002001 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
2002 session->s_mds, session->s_nr_caps, max_caps,
Jeff Layton533a2812019-07-19 15:22:28 -04002003 trim_caps - remaining);
Sage Weil2f2dc052009-10-06 11:31:09 -07002004 }
Yan, Zhenga56371d2014-04-01 20:34:56 +08002005
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08002006 ceph_flush_cap_releases(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002007 return 0;
2008}
2009
Yan, Zheng8310b082015-06-09 17:20:12 +08002010static int check_caps_flush(struct ceph_mds_client *mdsc,
2011 u64 want_flush_tid)
2012{
Yan, Zheng8310b082015-06-09 17:20:12 +08002013 int ret = 1;
2014
2015 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zhenge4500b52016-07-06 11:12:56 +08002016 if (!list_empty(&mdsc->cap_flush_list)) {
2017 struct ceph_cap_flush *cf =
2018 list_first_entry(&mdsc->cap_flush_list,
2019 struct ceph_cap_flush, g_list);
2020 if (cf->tid <= want_flush_tid) {
2021 dout("check_caps_flush still flushing tid "
2022 "%llu <= %llu\n", cf->tid, want_flush_tid);
2023 ret = 0;
2024 }
Yan, Zheng8310b082015-06-09 17:20:12 +08002025 }
2026 spin_unlock(&mdsc->cap_dirty_lock);
2027 return ret;
Yan, Zhengd3383a82015-01-08 21:30:12 +08002028}
2029
Sage Weil2f2dc052009-10-06 11:31:09 -07002030/*
2031 * flush all dirty inode data to disk.
2032 *
Yan, Zheng8310b082015-06-09 17:20:12 +08002033 * returns true if we've flushed through want_flush_tid
Sage Weil2f2dc052009-10-06 11:31:09 -07002034 */
Yan, Zhengaffbc192015-05-05 21:22:13 +08002035static void wait_caps_flush(struct ceph_mds_client *mdsc,
Yan, Zheng0e294382016-07-04 18:06:41 +08002036 u64 want_flush_tid)
Sage Weil2f2dc052009-10-06 11:31:09 -07002037{
Yan, Zheng0e294382016-07-04 18:06:41 +08002038 dout("check_caps_flush want %llu\n", want_flush_tid);
Yan, Zheng8310b082015-06-09 17:20:12 +08002039
2040 wait_event(mdsc->cap_flushing_wq,
2041 check_caps_flush(mdsc, want_flush_tid));
2042
2043 dout("check_caps_flush ok, flushed thru %llu\n", want_flush_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002044}
2045
2046/*
2047 * called under s_mutex
2048 */
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08002049static void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
2050 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07002051{
Yan, Zheng745a8e32015-05-14 17:22:42 +08002052 struct ceph_msg *msg = NULL;
2053 struct ceph_mds_cap_release *head;
2054 struct ceph_mds_cap_item *item;
Jeff Layton92475f02017-04-13 11:07:04 -04002055 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
Yan, Zheng745a8e32015-05-14 17:22:42 +08002056 struct ceph_cap *cap;
2057 LIST_HEAD(tmp_list);
2058 int num_cap_releases;
Jeff Layton92475f02017-04-13 11:07:04 -04002059 __le32 barrier, *cap_barrier;
2060
2061 down_read(&osdc->lock);
2062 barrier = cpu_to_le32(osdc->epoch_barrier);
2063 up_read(&osdc->lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002064
Sage Weil0f8605f2010-05-05 15:51:35 -07002065 spin_lock(&session->s_cap_lock);
Yan, Zheng745a8e32015-05-14 17:22:42 +08002066again:
2067 list_splice_init(&session->s_cap_releases, &tmp_list);
2068 num_cap_releases = session->s_num_cap_releases;
2069 session->s_num_cap_releases = 0;
2070 spin_unlock(&session->s_cap_lock);
2071
2072 while (!list_empty(&tmp_list)) {
2073 if (!msg) {
2074 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002075 PAGE_SIZE, GFP_NOFS, false);
Yan, Zheng745a8e32015-05-14 17:22:42 +08002076 if (!msg)
2077 goto out_err;
2078 head = msg->front.iov_base;
2079 head->num = cpu_to_le32(0);
2080 msg->front.iov_len = sizeof(*head);
Jeff Layton92475f02017-04-13 11:07:04 -04002081
2082 msg->hdr.version = cpu_to_le16(2);
2083 msg->hdr.compat_version = cpu_to_le16(1);
Yan, Zheng745a8e32015-05-14 17:22:42 +08002084 }
Jeff Layton92475f02017-04-13 11:07:04 -04002085
Yan, Zheng745a8e32015-05-14 17:22:42 +08002086 cap = list_first_entry(&tmp_list, struct ceph_cap,
2087 session_caps);
2088 list_del(&cap->session_caps);
2089 num_cap_releases--;
2090
2091 head = msg->front.iov_base;
Jeff Layton4198aba2019-05-02 08:06:50 -04002092 put_unaligned_le32(get_unaligned_le32(&head->num) + 1,
2093 &head->num);
Yan, Zheng745a8e32015-05-14 17:22:42 +08002094 item = msg->front.iov_base + msg->front.iov_len;
2095 item->ino = cpu_to_le64(cap->cap_ino);
2096 item->cap_id = cpu_to_le64(cap->cap_id);
2097 item->migrate_seq = cpu_to_le32(cap->mseq);
2098 item->seq = cpu_to_le32(cap->issue_seq);
2099 msg->front.iov_len += sizeof(*item);
2100
2101 ceph_put_cap(mdsc, cap);
2102
2103 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
Jeff Layton92475f02017-04-13 11:07:04 -04002104 // Append cap_barrier field
2105 cap_barrier = msg->front.iov_base + msg->front.iov_len;
2106 *cap_barrier = barrier;
2107 msg->front.iov_len += sizeof(*cap_barrier);
2108
Yan, Zheng745a8e32015-05-14 17:22:42 +08002109 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2110 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
2111 ceph_con_send(&session->s_con, msg);
2112 msg = NULL;
2113 }
2114 }
2115
2116 BUG_ON(num_cap_releases != 0);
2117
2118 spin_lock(&session->s_cap_lock);
2119 if (!list_empty(&session->s_cap_releases))
2120 goto again;
2121 spin_unlock(&session->s_cap_lock);
2122
2123 if (msg) {
Jeff Layton92475f02017-04-13 11:07:04 -04002124 // Append cap_barrier field
2125 cap_barrier = msg->front.iov_base + msg->front.iov_len;
2126 *cap_barrier = barrier;
2127 msg->front.iov_len += sizeof(*cap_barrier);
2128
Sage Weil2f2dc052009-10-06 11:31:09 -07002129 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2130 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
2131 ceph_con_send(&session->s_con, msg);
2132 }
Yan, Zheng745a8e32015-05-14 17:22:42 +08002133 return;
2134out_err:
2135 pr_err("send_cap_releases mds%d, failed to allocate message\n",
2136 session->s_mds);
2137 spin_lock(&session->s_cap_lock);
2138 list_splice(&tmp_list, &session->s_cap_releases);
2139 session->s_num_cap_releases += num_cap_releases;
Sage Weil2f2dc052009-10-06 11:31:09 -07002140 spin_unlock(&session->s_cap_lock);
2141}
2142
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08002143static void ceph_cap_release_work(struct work_struct *work)
2144{
2145 struct ceph_mds_session *session =
2146 container_of(work, struct ceph_mds_session, s_cap_release_work);
2147
2148 mutex_lock(&session->s_mutex);
2149 if (session->s_state == CEPH_MDS_SESSION_OPEN ||
2150 session->s_state == CEPH_MDS_SESSION_HUNG)
2151 ceph_send_cap_releases(session->s_mdsc, session);
2152 mutex_unlock(&session->s_mutex);
2153 ceph_put_mds_session(session);
2154}
2155
2156void ceph_flush_cap_releases(struct ceph_mds_client *mdsc,
2157 struct ceph_mds_session *session)
2158{
2159 if (mdsc->stopping)
2160 return;
2161
Xiubo Li5b3248c2019-12-19 19:44:09 -05002162 ceph_get_mds_session(session);
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08002163 if (queue_work(mdsc->fsc->cap_wq,
2164 &session->s_cap_release_work)) {
2165 dout("cap release work queued\n");
2166 } else {
2167 ceph_put_mds_session(session);
2168 dout("failed to queue cap release work\n");
2169 }
2170}
2171
2172/*
2173 * caller holds session->s_cap_lock
2174 */
2175void __ceph_queue_cap_release(struct ceph_mds_session *session,
2176 struct ceph_cap *cap)
2177{
2178 list_add_tail(&cap->session_caps, &session->s_cap_releases);
2179 session->s_num_cap_releases++;
2180
2181 if (!(session->s_num_cap_releases % CEPH_CAPS_PER_RELEASE))
2182 ceph_flush_cap_releases(session->s_mdsc, session);
2183}
2184
Yan, Zheng37c4efc2019-01-31 16:55:51 +08002185static void ceph_cap_reclaim_work(struct work_struct *work)
2186{
2187 struct ceph_mds_client *mdsc =
2188 container_of(work, struct ceph_mds_client, cap_reclaim_work);
2189 int ret = ceph_trim_dentries(mdsc);
2190 if (ret == -EAGAIN)
2191 ceph_queue_cap_reclaim_work(mdsc);
2192}
2193
2194void ceph_queue_cap_reclaim_work(struct ceph_mds_client *mdsc)
2195{
2196 if (mdsc->stopping)
2197 return;
2198
2199 if (queue_work(mdsc->fsc->cap_wq, &mdsc->cap_reclaim_work)) {
2200 dout("caps reclaim work queued\n");
2201 } else {
2202 dout("failed to queue caps release work\n");
2203 }
2204}
2205
Yan, Zhengfe330322019-02-01 14:57:15 +08002206void ceph_reclaim_caps_nr(struct ceph_mds_client *mdsc, int nr)
2207{
2208 int val;
2209 if (!nr)
2210 return;
2211 val = atomic_add_return(nr, &mdsc->cap_reclaim_pending);
Xiubo Libba15602019-11-26 07:32:22 -05002212 if ((val % CEPH_CAPS_PER_RELEASE) < nr) {
Yan, Zhengfe330322019-02-01 14:57:15 +08002213 atomic_set(&mdsc->cap_reclaim_pending, 0);
2214 ceph_queue_cap_reclaim_work(mdsc);
2215 }
2216}
2217
Sage Weil2f2dc052009-10-06 11:31:09 -07002218/*
2219 * requests
2220 */
2221
Yan, Zheng54008392014-03-29 13:41:15 +08002222int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
2223 struct inode *dir)
2224{
2225 struct ceph_inode_info *ci = ceph_inode(dir);
2226 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
2227 struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options;
Yan, Zheng2a5beea2016-04-28 09:37:39 +08002228 size_t size = sizeof(struct ceph_mds_reply_dir_entry);
Jeff Laytonad8c28a2019-09-09 15:58:55 -04002229 unsigned int num_entries;
2230 int order;
Yan, Zheng54008392014-03-29 13:41:15 +08002231
2232 spin_lock(&ci->i_ceph_lock);
2233 num_entries = ci->i_files + ci->i_subdirs;
2234 spin_unlock(&ci->i_ceph_lock);
Jeff Laytonad8c28a2019-09-09 15:58:55 -04002235 num_entries = max(num_entries, 1U);
Yan, Zheng54008392014-03-29 13:41:15 +08002236 num_entries = min(num_entries, opt->max_readdir);
2237
2238 order = get_order(size * num_entries);
2239 while (order >= 0) {
Yan, Zheng2a5beea2016-04-28 09:37:39 +08002240 rinfo->dir_entries = (void*)__get_free_pages(GFP_KERNEL |
2241 __GFP_NOWARN,
2242 order);
2243 if (rinfo->dir_entries)
Yan, Zheng54008392014-03-29 13:41:15 +08002244 break;
2245 order--;
2246 }
Yan, Zheng2a5beea2016-04-28 09:37:39 +08002247 if (!rinfo->dir_entries)
Yan, Zheng54008392014-03-29 13:41:15 +08002248 return -ENOMEM;
2249
2250 num_entries = (PAGE_SIZE << order) / size;
2251 num_entries = min(num_entries, opt->max_readdir);
2252
2253 rinfo->dir_buf_size = PAGE_SIZE << order;
2254 req->r_num_caps = num_entries + 1;
2255 req->r_args.readdir.max_entries = cpu_to_le32(num_entries);
2256 req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes);
2257 return 0;
2258}
2259
Sage Weil2f2dc052009-10-06 11:31:09 -07002260/*
2261 * Create an mds request.
2262 */
2263struct ceph_mds_request *
2264ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
2265{
Jeff Layton058daab2020-02-17 18:38:37 -05002266 struct ceph_mds_request *req;
Sage Weil2f2dc052009-10-06 11:31:09 -07002267
Jeff Layton058daab2020-02-17 18:38:37 -05002268 req = kmem_cache_zalloc(ceph_mds_request_cachep, GFP_NOFS);
Sage Weil2f2dc052009-10-06 11:31:09 -07002269 if (!req)
2270 return ERR_PTR(-ENOMEM);
2271
Sage Weilb4556392010-05-13 12:01:13 -07002272 mutex_init(&req->r_fill_mutex);
Yehuda Sadeh37151662010-06-17 16:16:12 -07002273 req->r_mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07002274 req->r_started = jiffies;
Xiubo Li70c94822020-03-19 23:45:02 -04002275 req->r_start_latency = ktime_get();
Sage Weil2f2dc052009-10-06 11:31:09 -07002276 req->r_resend_mds = -1;
2277 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002278 INIT_LIST_HEAD(&req->r_unsafe_target_item);
Sage Weil2f2dc052009-10-06 11:31:09 -07002279 req->r_fmode = -1;
Sage Weil153c8e62009-12-07 12:31:09 -08002280 kref_init(&req->r_kref);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02002281 RB_CLEAR_NODE(&req->r_node);
Sage Weil2f2dc052009-10-06 11:31:09 -07002282 INIT_LIST_HEAD(&req->r_wait);
2283 init_completion(&req->r_completion);
2284 init_completion(&req->r_safe_completion);
2285 INIT_LIST_HEAD(&req->r_unsafe_item);
2286
Deepa Dinamani668c9a62019-12-02 21:19:42 -08002287 ktime_get_coarse_real_ts64(&req->r_stamp);
Sage Weilb8e69062014-05-21 17:41:08 -07002288
Sage Weil2f2dc052009-10-06 11:31:09 -07002289 req->r_op = op;
2290 req->r_direct_mode = mode;
2291 return req;
2292}
2293
2294/*
Sage Weil44ca18f2010-02-15 12:08:46 -08002295 * return oldest (lowest) request, tid in request tree, 0 if none.
Sage Weil2f2dc052009-10-06 11:31:09 -07002296 *
2297 * called under mdsc->mutex.
2298 */
Sage Weil44ca18f2010-02-15 12:08:46 -08002299static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
2300{
2301 if (RB_EMPTY_ROOT(&mdsc->request_tree))
2302 return NULL;
2303 return rb_entry(rb_first(&mdsc->request_tree),
2304 struct ceph_mds_request, r_node);
2305}
2306
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08002307static inline u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07002308{
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08002309 return mdsc->oldest_tid;
Sage Weil2f2dc052009-10-06 11:31:09 -07002310}
2311
2312/*
2313 * Build a dentry's path. Allocate on heap; caller must kfree. Based
2314 * on build_path_from_dentry in fs/cifs/dir.c.
2315 *
2316 * If @stop_on_nosnap, generate path relative to the first non-snapped
2317 * inode.
2318 *
2319 * Encode hidden .snap dirs as a double /, i.e.
2320 * foo/.snap/bar -> foo//bar
2321 */
Jeff Layton69a10fb2019-04-26 13:33:39 -04002322char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase,
Sage Weil2f2dc052009-10-06 11:31:09 -07002323 int stop_on_nosnap)
2324{
2325 struct dentry *temp;
2326 char *path;
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002327 int pos;
Al Viro1b71fe22011-07-16 23:43:58 -04002328 unsigned seq;
Jeff Layton69a10fb2019-04-26 13:33:39 -04002329 u64 base;
Sage Weil2f2dc052009-10-06 11:31:09 -07002330
Markus Elfringd37b1d92017-08-20 20:22:02 +02002331 if (!dentry)
Sage Weil2f2dc052009-10-06 11:31:09 -07002332 return ERR_PTR(-EINVAL);
2333
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002334 path = __getname();
Markus Elfringd37b1d92017-08-20 20:22:02 +02002335 if (!path)
Sage Weil2f2dc052009-10-06 11:31:09 -07002336 return ERR_PTR(-ENOMEM);
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002337retry:
2338 pos = PATH_MAX - 1;
2339 path[pos] = '\0';
2340
2341 seq = read_seqbegin(&rename_lock);
Al Viro1b71fe22011-07-16 23:43:58 -04002342 rcu_read_lock();
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002343 temp = dentry;
2344 for (;;) {
Al Viro1b71fe22011-07-16 23:43:58 -04002345 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07002346
Al Viro1b71fe22011-07-16 23:43:58 -04002347 spin_lock(&temp->d_lock);
David Howells2b0143b2015-03-17 22:25:59 +00002348 inode = d_inode(temp);
Sage Weil2f2dc052009-10-06 11:31:09 -07002349 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
Sage Weil104648a2010-03-18 10:14:30 -07002350 dout("build_path path+%d: %p SNAPDIR\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002351 pos, temp);
Jeff Laytond6b8bd62019-05-09 07:58:38 -04002352 } else if (stop_on_nosnap && inode && dentry != temp &&
Sage Weil2f2dc052009-10-06 11:31:09 -07002353 ceph_snap(inode) == CEPH_NOSNAP) {
Yehuda Sadeh9d5a09e2011-12-13 09:57:44 -08002354 spin_unlock(&temp->d_lock);
Jeff Laytond6b8bd62019-05-09 07:58:38 -04002355 pos++; /* get rid of any prepended '/' */
Sage Weil2f2dc052009-10-06 11:31:09 -07002356 break;
2357 } else {
2358 pos -= temp->d_name.len;
Al Viro1b71fe22011-07-16 23:43:58 -04002359 if (pos < 0) {
2360 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002361 break;
Al Viro1b71fe22011-07-16 23:43:58 -04002362 }
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002363 memcpy(path + pos, temp->d_name.name, temp->d_name.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07002364 }
Al Viro1b71fe22011-07-16 23:43:58 -04002365 spin_unlock(&temp->d_lock);
Yan, Zheng41883ba2019-05-23 10:22:55 +08002366 temp = READ_ONCE(temp->d_parent);
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002367
2368 /* Are we at the root? */
2369 if (IS_ROOT(temp))
2370 break;
2371
2372 /* Are we out of buffer? */
2373 if (--pos < 0)
2374 break;
2375
2376 path[pos] = '/';
Sage Weil2f2dc052009-10-06 11:31:09 -07002377 }
Jeff Layton69a10fb2019-04-26 13:33:39 -04002378 base = ceph_ino(d_inode(temp));
Al Viro1b71fe22011-07-16 23:43:58 -04002379 rcu_read_unlock();
Jeff Laytonf5946bc2019-10-16 08:20:17 -04002380
2381 if (read_seqretry(&rename_lock, seq))
2382 goto retry;
2383
2384 if (pos < 0) {
2385 /*
2386 * A rename didn't occur, but somehow we didn't end up where
2387 * we thought we would. Throw a warning and try again.
2388 */
2389 pr_warn("build_path did not end path lookup where "
2390 "expected, pos is %d\n", pos);
Sage Weil2f2dc052009-10-06 11:31:09 -07002391 goto retry;
2392 }
2393
Jeff Layton69a10fb2019-04-26 13:33:39 -04002394 *pbase = base;
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002395 *plen = PATH_MAX - 1 - pos;
Sage Weil104648a2010-03-18 10:14:30 -07002396 dout("build_path on %p %d built %llx '%.*s'\n",
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002397 dentry, d_count(dentry), base, *plen, path + pos);
2398 return path + pos;
Sage Weil2f2dc052009-10-06 11:31:09 -07002399}
2400
Jeff Laytonfd36a712016-12-15 08:37:58 -05002401static int build_dentry_path(struct dentry *dentry, struct inode *dir,
Sage Weil2f2dc052009-10-06 11:31:09 -07002402 const char **ppath, int *ppathlen, u64 *pino,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002403 bool *pfreepath, bool parent_locked)
Sage Weil2f2dc052009-10-06 11:31:09 -07002404{
2405 char *path;
2406
Jeff Laytonc6b0b652016-12-15 08:37:57 -05002407 rcu_read_lock();
Jeff Laytonfd36a712016-12-15 08:37:58 -05002408 if (!dir)
2409 dir = d_inode_rcu(dentry->d_parent);
Jeff Layton964fff72019-04-29 11:51:02 -04002410 if (dir && parent_locked && ceph_snap(dir) == CEPH_NOSNAP) {
Jeff Laytonc6b0b652016-12-15 08:37:57 -05002411 *pino = ceph_ino(dir);
2412 rcu_read_unlock();
Jeff Layton964fff72019-04-29 11:51:02 -04002413 *ppath = dentry->d_name.name;
2414 *ppathlen = dentry->d_name.len;
Sage Weil2f2dc052009-10-06 11:31:09 -07002415 return 0;
2416 }
Jeff Laytonc6b0b652016-12-15 08:37:57 -05002417 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -07002418 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
2419 if (IS_ERR(path))
2420 return PTR_ERR(path);
2421 *ppath = path;
Jeff Layton1bcb3442019-04-15 12:00:42 -04002422 *pfreepath = true;
Sage Weil2f2dc052009-10-06 11:31:09 -07002423 return 0;
2424}
2425
2426static int build_inode_path(struct inode *inode,
2427 const char **ppath, int *ppathlen, u64 *pino,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002428 bool *pfreepath)
Sage Weil2f2dc052009-10-06 11:31:09 -07002429{
2430 struct dentry *dentry;
2431 char *path;
2432
2433 if (ceph_snap(inode) == CEPH_NOSNAP) {
2434 *pino = ceph_ino(inode);
2435 *ppathlen = 0;
2436 return 0;
2437 }
2438 dentry = d_find_alias(inode);
2439 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
2440 dput(dentry);
2441 if (IS_ERR(path))
2442 return PTR_ERR(path);
2443 *ppath = path;
Jeff Layton1bcb3442019-04-15 12:00:42 -04002444 *pfreepath = true;
Sage Weil2f2dc052009-10-06 11:31:09 -07002445 return 0;
2446}
2447
2448/*
2449 * request arguments may be specified via an inode *, a dentry *, or
2450 * an explicit ino+path.
2451 */
2452static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
Jeff Laytonfd36a712016-12-15 08:37:58 -05002453 struct inode *rdiri, const char *rpath,
2454 u64 rino, const char **ppath, int *pathlen,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002455 u64 *ino, bool *freepath, bool parent_locked)
Sage Weil2f2dc052009-10-06 11:31:09 -07002456{
2457 int r = 0;
2458
2459 if (rinode) {
2460 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
2461 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
2462 ceph_snap(rinode));
2463 } else if (rdentry) {
Jeff Laytonfd36a712016-12-15 08:37:58 -05002464 r = build_dentry_path(rdentry, rdiri, ppath, pathlen, ino,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002465 freepath, parent_locked);
Sage Weil2f2dc052009-10-06 11:31:09 -07002466 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
2467 *ppath);
Sage Weil795858d2011-08-15 13:02:37 -07002468 } else if (rpath || rino) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002469 *ino = rino;
2470 *ppath = rpath;
David Zafmanb0000562012-10-25 10:23:46 -07002471 *pathlen = rpath ? strlen(rpath) : 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002472 dout(" path %.*s\n", *pathlen, rpath);
2473 }
2474
2475 return r;
2476}
2477
2478/*
2479 * called under mdsc->mutex
2480 */
2481static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
2482 struct ceph_mds_request *req,
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002483 int mds, bool drop_cap_releases)
Sage Weil2f2dc052009-10-06 11:31:09 -07002484{
2485 struct ceph_msg *msg;
2486 struct ceph_mds_request_head *head;
2487 const char *path1 = NULL;
2488 const char *path2 = NULL;
2489 u64 ino1 = 0, ino2 = 0;
2490 int pathlen1 = 0, pathlen2 = 0;
Jeff Layton1bcb3442019-04-15 12:00:42 -04002491 bool freepath1 = false, freepath2 = false;
Sage Weil2f2dc052009-10-06 11:31:09 -07002492 int len;
2493 u16 releases;
2494 void *p, *end;
2495 int ret;
2496
2497 ret = set_request_path_attr(req->r_inode, req->r_dentry,
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002498 req->r_parent, req->r_path1, req->r_ino1.ino,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002499 &path1, &pathlen1, &ino1, &freepath1,
2500 test_bit(CEPH_MDS_R_PARENT_LOCKED,
2501 &req->r_req_flags));
Sage Weil2f2dc052009-10-06 11:31:09 -07002502 if (ret < 0) {
2503 msg = ERR_PTR(ret);
2504 goto out;
2505 }
2506
Jeff Layton1bcb3442019-04-15 12:00:42 -04002507 /* If r_old_dentry is set, then assume that its parent is locked */
Sage Weil2f2dc052009-10-06 11:31:09 -07002508 ret = set_request_path_attr(NULL, req->r_old_dentry,
Jeff Laytonfd36a712016-12-15 08:37:58 -05002509 req->r_old_dentry_dir,
Sage Weil2f2dc052009-10-06 11:31:09 -07002510 req->r_path2, req->r_ino2.ino,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002511 &path2, &pathlen2, &ino2, &freepath2, true);
Sage Weil2f2dc052009-10-06 11:31:09 -07002512 if (ret < 0) {
2513 msg = ERR_PTR(ret);
2514 goto out_free1;
2515 }
2516
2517 len = sizeof(*head) +
Sage Weilb8e69062014-05-21 17:41:08 -07002518 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) +
Arnd Bergmann777d7382015-09-30 15:04:42 +02002519 sizeof(struct ceph_timespec);
Sage Weil2f2dc052009-10-06 11:31:09 -07002520
2521 /* calculate (max) length for cap releases */
2522 len += sizeof(struct ceph_mds_request_release) *
2523 (!!req->r_inode_drop + !!req->r_dentry_drop +
2524 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
2525 if (req->r_dentry_drop)
Jeff Laytonc1dfc272019-04-17 14:23:17 -04002526 len += pathlen1;
Sage Weil2f2dc052009-10-06 11:31:09 -07002527 if (req->r_old_dentry_drop)
Jeff Laytonc1dfc272019-04-17 14:23:17 -04002528 len += pathlen2;
Sage Weil2f2dc052009-10-06 11:31:09 -07002529
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02002530 msg = ceph_msg_new2(CEPH_MSG_CLIENT_REQUEST, len, 1, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07002531 if (!msg) {
2532 msg = ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -07002533 goto out_free2;
Sage Weila79832f2010-04-01 16:06:19 -07002534 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002535
John Spray7cfa0312014-10-30 17:15:26 +00002536 msg->hdr.version = cpu_to_le16(2);
Sage Weil6df058c2009-12-22 11:24:33 -08002537 msg->hdr.tid = cpu_to_le64(req->r_tid);
2538
Sage Weil2f2dc052009-10-06 11:31:09 -07002539 head = msg->front.iov_base;
2540 p = msg->front.iov_base + sizeof(*head);
2541 end = msg->front.iov_base + msg->front.iov_len;
2542
2543 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
2544 head->op = cpu_to_le32(req->r_op);
Eric W. Biedermanff3d0042013-01-31 04:01:53 -08002545 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid));
2546 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid));
Jeff Layton6deb8002020-01-13 13:04:08 -05002547 head->ino = cpu_to_le64(req->r_deleg_ino);
Sage Weil2f2dc052009-10-06 11:31:09 -07002548 head->args = req->r_args;
2549
2550 ceph_encode_filepath(&p, end, ino1, path1);
2551 ceph_encode_filepath(&p, end, ino2, path2);
2552
Sage Weile979cf52010-07-15 14:58:39 -07002553 /* make note of release offset, in case we need to replay */
2554 req->r_request_release_offset = p - msg->front.iov_base;
2555
Sage Weil2f2dc052009-10-06 11:31:09 -07002556 /* cap releases */
2557 releases = 0;
2558 if (req->r_inode_drop)
2559 releases += ceph_encode_inode_release(&p,
David Howells2b0143b2015-03-17 22:25:59 +00002560 req->r_inode ? req->r_inode : d_inode(req->r_dentry),
Yan, Zheng719a2512020-03-05 20:21:00 +08002561 mds, req->r_inode_drop, req->r_inode_unless,
2562 req->r_op == CEPH_MDS_OP_READDIR);
Sage Weil2f2dc052009-10-06 11:31:09 -07002563 if (req->r_dentry_drop)
2564 releases += ceph_encode_dentry_release(&p, req->r_dentry,
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002565 req->r_parent, mds, req->r_dentry_drop,
Jeff Laytonca6c8ae2016-12-15 08:37:59 -05002566 req->r_dentry_unless);
Sage Weil2f2dc052009-10-06 11:31:09 -07002567 if (req->r_old_dentry_drop)
2568 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
Jeff Laytonca6c8ae2016-12-15 08:37:59 -05002569 req->r_old_dentry_dir, mds,
2570 req->r_old_dentry_drop,
2571 req->r_old_dentry_unless);
Sage Weil2f2dc052009-10-06 11:31:09 -07002572 if (req->r_old_inode_drop)
2573 releases += ceph_encode_inode_release(&p,
David Howells2b0143b2015-03-17 22:25:59 +00002574 d_inode(req->r_old_dentry),
Sage Weil2f2dc052009-10-06 11:31:09 -07002575 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002576
2577 if (drop_cap_releases) {
2578 releases = 0;
2579 p = msg->front.iov_base + req->r_request_release_offset;
2580 }
2581
Sage Weil2f2dc052009-10-06 11:31:09 -07002582 head->num_releases = cpu_to_le16(releases);
2583
Sage Weilb8e69062014-05-21 17:41:08 -07002584 /* time stamp */
Yan, Zheng1f041a82015-01-13 15:20:52 +08002585 {
2586 struct ceph_timespec ts;
Arnd Bergmann0ed1e902018-07-13 22:18:38 +02002587 ceph_encode_timespec64(&ts, &req->r_stamp);
Yan, Zheng1f041a82015-01-13 15:20:52 +08002588 ceph_encode_copy(&p, &ts, sizeof(ts));
2589 }
Sage Weilb8e69062014-05-21 17:41:08 -07002590
Xiubo Lib682c6d2020-06-30 03:52:18 -04002591 if (WARN_ON_ONCE(p > end)) {
2592 ceph_msg_put(msg);
2593 msg = ERR_PTR(-ERANGE);
2594 goto out_free2;
2595 }
2596
Sage Weil2f2dc052009-10-06 11:31:09 -07002597 msg->front.iov_len = p - msg->front.iov_base;
2598 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2599
Yan, Zheng25e6bae2014-09-16 19:15:28 +08002600 if (req->r_pagelist) {
2601 struct ceph_pagelist *pagelist = req->r_pagelist;
Yan, Zheng25e6bae2014-09-16 19:15:28 +08002602 ceph_msg_data_add_pagelist(msg, pagelist);
2603 msg->hdr.data_len = cpu_to_le32(pagelist->length);
2604 } else {
2605 msg->hdr.data_len = 0;
Alex Elderebf18f42013-03-04 22:29:57 -06002606 }
Alex Elder02afca62013-02-14 12:16:43 -06002607
Sage Weil2f2dc052009-10-06 11:31:09 -07002608 msg->hdr.data_off = cpu_to_le16(0);
2609
2610out_free2:
2611 if (freepath2)
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002612 ceph_mdsc_free_path((char *)path2, pathlen2);
Sage Weil2f2dc052009-10-06 11:31:09 -07002613out_free1:
2614 if (freepath1)
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002615 ceph_mdsc_free_path((char *)path1, pathlen1);
Sage Weil2f2dc052009-10-06 11:31:09 -07002616out:
2617 return msg;
2618}
2619
2620/*
2621 * called under mdsc->mutex if error, under no mutex if
2622 * success.
2623 */
2624static void complete_request(struct ceph_mds_client *mdsc,
2625 struct ceph_mds_request *req)
2626{
Xiubo Li70c94822020-03-19 23:45:02 -04002627 req->r_end_latency = ktime_get();
2628
Sage Weil2f2dc052009-10-06 11:31:09 -07002629 if (req->r_callback)
2630 req->r_callback(mdsc, req);
Jeff Layton111c7082019-04-02 09:43:18 -04002631 complete_all(&req->r_completion);
Sage Weil2f2dc052009-10-06 11:31:09 -07002632}
2633
2634/*
2635 * called under mdsc->mutex
2636 */
2637static int __prepare_send_request(struct ceph_mds_client *mdsc,
2638 struct ceph_mds_request *req,
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002639 int mds, bool drop_cap_releases)
Sage Weil2f2dc052009-10-06 11:31:09 -07002640{
2641 struct ceph_mds_request_head *rhead;
2642 struct ceph_msg *msg;
2643 int flags = 0;
2644
Sage Weil2f2dc052009-10-06 11:31:09 -07002645 req->r_attempts++;
Greg Farnume55b71f2010-06-22 15:58:01 -07002646 if (req->r_inode) {
2647 struct ceph_cap *cap =
2648 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
2649
2650 if (cap)
2651 req->r_sent_on_mseq = cap->mseq;
2652 else
2653 req->r_sent_on_mseq = -1;
2654 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002655 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
2656 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
2657
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002658 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002659 void *p;
Sage Weil01a92f12010-07-15 13:24:32 -07002660 /*
2661 * Replay. Do not regenerate message (and rebuild
2662 * paths, etc.); just use the original message.
2663 * Rebuilding paths will break for renames because
2664 * d_move mangles the src name.
2665 */
2666 msg = req->r_request;
2667 rhead = msg->front.iov_base;
2668
2669 flags = le32_to_cpu(rhead->flags);
2670 flags |= CEPH_MDS_FLAG_REPLAY;
2671 rhead->flags = cpu_to_le32(flags);
2672
2673 if (req->r_target_inode)
2674 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
2675
2676 rhead->num_retry = req->r_attempts - 1;
Sage Weile979cf52010-07-15 14:58:39 -07002677
2678 /* remove cap/dentry releases from message */
2679 rhead->num_releases = 0;
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002680
2681 /* time stamp */
2682 p = msg->front.iov_base + req->r_request_release_offset;
Yan, Zheng1f041a82015-01-13 15:20:52 +08002683 {
2684 struct ceph_timespec ts;
Arnd Bergmann0ed1e902018-07-13 22:18:38 +02002685 ceph_encode_timespec64(&ts, &req->r_stamp);
Yan, Zheng1f041a82015-01-13 15:20:52 +08002686 ceph_encode_copy(&p, &ts, sizeof(ts));
2687 }
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002688
2689 msg->front.iov_len = p - msg->front.iov_base;
2690 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
Sage Weil01a92f12010-07-15 13:24:32 -07002691 return 0;
2692 }
2693
Sage Weil2f2dc052009-10-06 11:31:09 -07002694 if (req->r_request) {
2695 ceph_msg_put(req->r_request);
2696 req->r_request = NULL;
2697 }
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002698 msg = create_request_message(mdsc, req, mds, drop_cap_releases);
Sage Weil2f2dc052009-10-06 11:31:09 -07002699 if (IS_ERR(msg)) {
Sage Weile1518c72010-05-13 11:19:06 -07002700 req->r_err = PTR_ERR(msg);
Sage Weila79832f2010-04-01 16:06:19 -07002701 return PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002702 }
2703 req->r_request = msg;
2704
2705 rhead = msg->front.iov_base;
Sage Weil2f2dc052009-10-06 11:31:09 -07002706 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002707 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Sage Weil2f2dc052009-10-06 11:31:09 -07002708 flags |= CEPH_MDS_FLAG_REPLAY;
Jeff Layton3bb48b42019-12-02 13:47:57 -05002709 if (test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags))
2710 flags |= CEPH_MDS_FLAG_ASYNC;
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002711 if (req->r_parent)
Sage Weil2f2dc052009-10-06 11:31:09 -07002712 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
2713 rhead->flags = cpu_to_le32(flags);
2714 rhead->num_fwd = req->r_num_fwd;
2715 rhead->num_retry = req->r_attempts - 1;
2716
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002717 dout(" r_parent = %p\n", req->r_parent);
Sage Weil2f2dc052009-10-06 11:31:09 -07002718 return 0;
2719}
2720
2721/*
Xiubo Li9cf54562019-12-05 20:50:21 -05002722 * called under mdsc->mutex
2723 */
2724static int __send_request(struct ceph_mds_client *mdsc,
2725 struct ceph_mds_session *session,
2726 struct ceph_mds_request *req,
2727 bool drop_cap_releases)
2728{
2729 int err;
2730
2731 err = __prepare_send_request(mdsc, req, session->s_mds,
2732 drop_cap_releases);
2733 if (!err) {
2734 ceph_msg_get(req->r_request);
2735 ceph_con_send(&session->s_con, req->r_request);
2736 }
2737
2738 return err;
2739}
2740
2741/*
Sage Weil2f2dc052009-10-06 11:31:09 -07002742 * send request, or put it on the appropriate wait list.
2743 */
Chengguang Xud5548492018-07-28 16:30:48 +08002744static void __do_request(struct ceph_mds_client *mdsc,
Sage Weil2f2dc052009-10-06 11:31:09 -07002745 struct ceph_mds_request *req)
2746{
2747 struct ceph_mds_session *session = NULL;
2748 int mds = -1;
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002749 int err = 0;
Xiubo Lic4853e92019-12-09 07:47:15 -05002750 bool random;
Sage Weil2f2dc052009-10-06 11:31:09 -07002751
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002752 if (req->r_err || test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
2753 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
Yan, Zhengeb1b8af2013-09-26 14:25:36 +08002754 __unregister_request(mdsc, req);
Chengguang Xud5548492018-07-28 16:30:48 +08002755 return;
Yan, Zhengeb1b8af2013-09-26 14:25:36 +08002756 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002757
2758 if (req->r_timeout &&
2759 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
2760 dout("do_request timed out\n");
Xiubo Li8ccf7fc2020-02-23 22:23:11 -05002761 err = -ETIMEDOUT;
Sage Weil2f2dc052009-10-06 11:31:09 -07002762 goto finish;
2763 }
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01002764 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002765 dout("do_request forced umount\n");
2766 err = -EIO;
2767 goto finish;
2768 }
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01002769 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_MOUNTING) {
Yan, Zhenge9e427f2016-11-10 16:02:06 +08002770 if (mdsc->mdsmap_err) {
2771 err = mdsc->mdsmap_err;
2772 dout("do_request mdsmap err %d\n", err);
2773 goto finish;
2774 }
Yan, Zhengcc8e8342017-01-04 16:21:58 +08002775 if (mdsc->mdsmap->m_epoch == 0) {
2776 dout("do_request no mdsmap, waiting for map\n");
2777 list_add(&req->r_wait, &mdsc->waiting_for_map);
Chengguang Xud5548492018-07-28 16:30:48 +08002778 return;
Yan, Zhengcc8e8342017-01-04 16:21:58 +08002779 }
Yan, Zhenge9e427f2016-11-10 16:02:06 +08002780 if (!(mdsc->fsc->mount_options->flags &
2781 CEPH_MOUNT_OPT_MOUNTWAIT) &&
2782 !ceph_mdsmap_is_cluster_available(mdsc->mdsmap)) {
Xiubo Li97820052019-12-10 20:29:40 -05002783 err = -EHOSTUNREACH;
Yan, Zhenge9e427f2016-11-10 16:02:06 +08002784 goto finish;
2785 }
2786 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002787
Sage Weildc69e2e2010-11-02 13:49:00 -07002788 put_request_session(req);
2789
Xiubo Lic4853e92019-12-09 07:47:15 -05002790 mds = __choose_mds(mdsc, req, &random);
Sage Weil2f2dc052009-10-06 11:31:09 -07002791 if (mds < 0 ||
2792 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
Jeff Layton3bb48b42019-12-02 13:47:57 -05002793 if (test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags)) {
2794 err = -EJUKEBOX;
2795 goto finish;
2796 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002797 dout("do_request no mds or not active, waiting for map\n");
2798 list_add(&req->r_wait, &mdsc->waiting_for_map);
Chengguang Xud5548492018-07-28 16:30:48 +08002799 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07002800 }
2801
2802 /* get, open session */
2803 session = __ceph_lookup_mds_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07002804 if (!session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002805 session = register_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07002806 if (IS_ERR(session)) {
2807 err = PTR_ERR(session);
2808 goto finish;
2809 }
2810 }
Xiubo Li5b3248c2019-12-19 19:44:09 -05002811 req->r_session = ceph_get_mds_session(session);
Sage Weildc69e2e2010-11-02 13:49:00 -07002812
Sage Weil2f2dc052009-10-06 11:31:09 -07002813 dout("do_request mds%d session %p state %s\n", mds, session,
John Spraya687eca2014-09-19 13:51:08 +01002814 ceph_session_state_name(session->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07002815 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
2816 session->s_state != CEPH_MDS_SESSION_HUNG) {
Jeff Layton3bb48b42019-12-02 13:47:57 -05002817 /*
2818 * We cannot queue async requests since the caps and delegated
2819 * inodes are bound to the session. Just return -EJUKEBOX and
2820 * let the caller retry a sync request in that case.
2821 */
2822 if (test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags)) {
2823 err = -EJUKEBOX;
2824 goto out_session;
2825 }
Jeff Layton4ae37132020-09-21 13:12:53 -04002826
2827 /*
2828 * If the session has been REJECTED, then return a hard error,
2829 * unless it's a CLEANRECOVER mount, in which case we'll queue
2830 * it to the mdsc queue.
2831 */
2832 if (session->s_state == CEPH_MDS_SESSION_REJECTED) {
2833 if (ceph_test_mount_opt(mdsc->fsc, CLEANRECOVER))
2834 list_add(&req->r_wait, &mdsc->waiting_for_map);
2835 else
2836 err = -EACCES;
2837 goto out_session;
2838 }
2839
Sage Weil2f2dc052009-10-06 11:31:09 -07002840 if (session->s_state == CEPH_MDS_SESSION_NEW ||
Xiubo Lic4853e92019-12-09 07:47:15 -05002841 session->s_state == CEPH_MDS_SESSION_CLOSING) {
Xiubo Lib682c6d2020-06-30 03:52:18 -04002842 err = __open_session(mdsc, session);
2843 if (err)
2844 goto out_session;
Xiubo Lic4853e92019-12-09 07:47:15 -05002845 /* retry the same mds later */
2846 if (random)
2847 req->r_resend_mds = mds;
2848 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002849 list_add(&req->r_wait, &session->s_waiting);
2850 goto out_session;
2851 }
2852
2853 /* send request */
Sage Weil2f2dc052009-10-06 11:31:09 -07002854 req->r_resend_mds = -1; /* forget any previous mds hint */
2855
2856 if (req->r_request_started == 0) /* note request start time */
2857 req->r_request_started = jiffies;
2858
Xiubo Li9cf54562019-12-05 20:50:21 -05002859 err = __send_request(mdsc, session, req, false);
Sage Weil2f2dc052009-10-06 11:31:09 -07002860
2861out_session:
2862 ceph_put_mds_session(session);
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002863finish:
2864 if (err) {
2865 dout("__do_request early error %d\n", err);
2866 req->r_err = err;
2867 complete_request(mdsc, req);
2868 __unregister_request(mdsc, req);
2869 }
Chengguang Xud5548492018-07-28 16:30:48 +08002870 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07002871}
2872
2873/*
2874 * called under mdsc->mutex
2875 */
2876static void __wake_requests(struct ceph_mds_client *mdsc,
2877 struct list_head *head)
2878{
Yan, Zhenged75ec22012-11-19 10:49:06 +08002879 struct ceph_mds_request *req;
2880 LIST_HEAD(tmp_list);
Sage Weil2f2dc052009-10-06 11:31:09 -07002881
Yan, Zhenged75ec22012-11-19 10:49:06 +08002882 list_splice_init(head, &tmp_list);
2883
2884 while (!list_empty(&tmp_list)) {
2885 req = list_entry(tmp_list.next,
2886 struct ceph_mds_request, r_wait);
Sage Weil2f2dc052009-10-06 11:31:09 -07002887 list_del_init(&req->r_wait);
Sage Weil7971bd92013-05-01 21:15:58 -07002888 dout(" wake request %p tid %llu\n", req, req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002889 __do_request(mdsc, req);
2890 }
2891}
2892
2893/*
2894 * Wake up threads with requests pending for @mds, so that they can
Sage Weil29790f22010-03-18 14:45:05 -07002895 * resubmit their requests to a possibly different mds.
Sage Weil2f2dc052009-10-06 11:31:09 -07002896 */
Sage Weil29790f22010-03-18 14:45:05 -07002897static void kick_requests(struct ceph_mds_client *mdsc, int mds)
Sage Weil2f2dc052009-10-06 11:31:09 -07002898{
Sage Weil44ca18f2010-02-15 12:08:46 -08002899 struct ceph_mds_request *req;
Yan, Zheng282c1052014-07-30 10:12:47 +08002900 struct rb_node *p = rb_first(&mdsc->request_tree);
Sage Weil2f2dc052009-10-06 11:31:09 -07002901
2902 dout("kick_requests mds%d\n", mds);
Yan, Zheng282c1052014-07-30 10:12:47 +08002903 while (p) {
Sage Weil44ca18f2010-02-15 12:08:46 -08002904 req = rb_entry(p, struct ceph_mds_request, r_node);
Yan, Zheng282c1052014-07-30 10:12:47 +08002905 p = rb_next(p);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002906 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Sage Weil44ca18f2010-02-15 12:08:46 -08002907 continue;
Yan, Zheng3de22be2015-02-04 14:26:22 +08002908 if (req->r_attempts > 0)
2909 continue; /* only new requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08002910 if (req->r_session &&
2911 req->r_session->s_mds == mds) {
2912 dout(" kicking tid %llu\n", req->r_tid);
Yan, Zheng03974e82014-09-11 14:28:56 +08002913 list_del_init(&req->r_wait);
Sage Weil44ca18f2010-02-15 12:08:46 -08002914 __do_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002915 }
2916 }
2917}
2918
Jeff Layton86bda532019-04-02 09:24:36 -04002919int ceph_mdsc_submit_request(struct ceph_mds_client *mdsc, struct inode *dir,
Sage Weil2f2dc052009-10-06 11:31:09 -07002920 struct ceph_mds_request *req)
2921{
Jeff Layton891f3f52020-01-14 15:06:40 -05002922 int err = 0;
Jeff Layton86bda532019-04-02 09:24:36 -04002923
2924 /* take CAP_PIN refs for r_inode, r_parent, r_old_dentry */
2925 if (req->r_inode)
2926 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
Jeff Layton9c1c2b32019-04-03 13:16:01 -04002927 if (req->r_parent) {
Yan, Zheng719a2512020-03-05 20:21:00 +08002928 struct ceph_inode_info *ci = ceph_inode(req->r_parent);
2929 int fmode = (req->r_op & CEPH_MDS_OP_WRITE) ?
2930 CEPH_FILE_MODE_WR : CEPH_FILE_MODE_RD;
2931 spin_lock(&ci->i_ceph_lock);
2932 ceph_take_cap_refs(ci, CEPH_CAP_PIN, false);
2933 __ceph_touch_fmode(ci, mdsc, fmode);
2934 spin_unlock(&ci->i_ceph_lock);
Jeff Layton9c1c2b32019-04-03 13:16:01 -04002935 ihold(req->r_parent);
2936 }
Jeff Layton86bda532019-04-02 09:24:36 -04002937 if (req->r_old_dentry_dir)
2938 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
2939 CEPH_CAP_PIN);
2940
Jeff Layton891f3f52020-01-14 15:06:40 -05002941 if (req->r_inode) {
2942 err = ceph_wait_on_async_create(req->r_inode);
2943 if (err) {
2944 dout("%s: wait for async create returned: %d\n",
2945 __func__, err);
2946 return err;
2947 }
2948 }
2949
2950 if (!err && req->r_old_inode) {
2951 err = ceph_wait_on_async_create(req->r_old_inode);
2952 if (err) {
2953 dout("%s: wait for async create returned: %d\n",
2954 __func__, err);
2955 return err;
2956 }
2957 }
2958
Jeff Layton86bda532019-04-02 09:24:36 -04002959 dout("submit_request on %p for inode %p\n", req, dir);
Sage Weil2f2dc052009-10-06 11:31:09 -07002960 mutex_lock(&mdsc->mutex);
Jeff Layton86bda532019-04-02 09:24:36 -04002961 __register_request(mdsc, req, dir);
Sage Weil2f2dc052009-10-06 11:31:09 -07002962 __do_request(mdsc, req);
Jeff Layton86bda532019-04-02 09:24:36 -04002963 err = req->r_err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002964 mutex_unlock(&mdsc->mutex);
Jeff Layton86bda532019-04-02 09:24:36 -04002965 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002966}
2967
Jeff Layton8340f222019-04-02 12:34:38 -04002968static int ceph_mdsc_wait_request(struct ceph_mds_client *mdsc,
2969 struct ceph_mds_request *req)
Sage Weil2f2dc052009-10-06 11:31:09 -07002970{
2971 int err;
2972
Sage Weile1518c72010-05-13 11:19:06 -07002973 /* wait */
Sage Weile1518c72010-05-13 11:19:06 -07002974 dout("do_request waiting\n");
Ilya Dryomov5be73032015-05-19 12:05:38 +03002975 if (!req->r_timeout && req->r_wait_for_completion) {
Yan, Zheng9280be22014-10-14 10:33:35 +08002976 err = req->r_wait_for_completion(mdsc, req);
Sage Weile1518c72010-05-13 11:19:06 -07002977 } else {
Ilya Dryomov5be73032015-05-19 12:05:38 +03002978 long timeleft = wait_for_completion_killable_timeout(
2979 &req->r_completion,
2980 ceph_timeout_jiffies(req->r_timeout));
2981 if (timeleft > 0)
2982 err = 0;
2983 else if (!timeleft)
Xiubo Li8ccf7fc2020-02-23 22:23:11 -05002984 err = -ETIMEDOUT; /* timed out */
Ilya Dryomov5be73032015-05-19 12:05:38 +03002985 else
2986 err = timeleft; /* killed */
Sage Weile1518c72010-05-13 11:19:06 -07002987 }
2988 dout("do_request waited, got %d\n", err);
2989 mutex_lock(&mdsc->mutex);
2990
2991 /* only abort if we didn't race with a real reply */
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002992 if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
Sage Weile1518c72010-05-13 11:19:06 -07002993 err = le32_to_cpu(req->r_reply_info.head->result);
2994 } else if (err < 0) {
2995 dout("aborted request %lld with %d\n", req->r_tid, err);
Sage Weilb4556392010-05-13 12:01:13 -07002996
2997 /*
2998 * ensure we aren't running concurrently with
2999 * ceph_fill_trace or ceph_readdir_prepopulate, which
3000 * rely on locks (dir mutex) held by our caller.
3001 */
3002 mutex_lock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07003003 req->r_err = err;
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003004 set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
Sage Weilb4556392010-05-13 12:01:13 -07003005 mutex_unlock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07003006
Jeff Layton3dd69aa2017-01-31 10:28:26 -05003007 if (req->r_parent &&
Sage Weil167c9e32010-05-14 10:02:57 -07003008 (req->r_op & CEPH_MDS_OP_WRITE))
3009 ceph_invalidate_dir_request(req);
Sage Weile1518c72010-05-13 11:19:06 -07003010 } else {
3011 err = req->r_err;
3012 }
3013
Sage Weile1518c72010-05-13 11:19:06 -07003014 mutex_unlock(&mdsc->mutex);
Jeff Layton8340f222019-04-02 12:34:38 -04003015 return err;
3016}
3017
3018/*
3019 * Synchrously perform an mds request. Take care of all of the
3020 * session setup, forwarding, retry details.
3021 */
3022int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
3023 struct inode *dir,
3024 struct ceph_mds_request *req)
3025{
3026 int err;
3027
3028 dout("do_request on %p\n", req);
3029
3030 /* issue */
3031 err = ceph_mdsc_submit_request(mdsc, dir, req);
3032 if (!err)
3033 err = ceph_mdsc_wait_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07003034 dout("do_request %p done, result %d\n", req, err);
3035 return err;
3036}
3037
3038/*
Yan, Zheng2f276c52013-03-13 19:44:32 +08003039 * Invalidate dir's completeness, dentry lease state on an aborted MDS
Sage Weil167c9e32010-05-14 10:02:57 -07003040 * namespace request.
3041 */
3042void ceph_invalidate_dir_request(struct ceph_mds_request *req)
3043{
Yan, Zheng8d8f3712017-11-24 11:51:32 +08003044 struct inode *dir = req->r_parent;
3045 struct inode *old_dir = req->r_old_dentry_dir;
Sage Weil167c9e32010-05-14 10:02:57 -07003046
Yan, Zheng8d8f3712017-11-24 11:51:32 +08003047 dout("invalidate_dir_request %p %p (complete, lease(s))\n", dir, old_dir);
Sage Weil167c9e32010-05-14 10:02:57 -07003048
Yan, Zheng8d8f3712017-11-24 11:51:32 +08003049 ceph_dir_clear_complete(dir);
3050 if (old_dir)
3051 ceph_dir_clear_complete(old_dir);
Sage Weil167c9e32010-05-14 10:02:57 -07003052 if (req->r_dentry)
3053 ceph_invalidate_dentry_lease(req->r_dentry);
3054 if (req->r_old_dentry)
3055 ceph_invalidate_dentry_lease(req->r_old_dentry);
3056}
3057
3058/*
Sage Weil2f2dc052009-10-06 11:31:09 -07003059 * Handle mds reply.
3060 *
3061 * We take the session mutex and parse and process the reply immediately.
3062 * This preserves the logical ordering of replies, capabilities, etc., sent
3063 * by the MDS as they are applied to our local cache.
3064 */
3065static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
3066{
3067 struct ceph_mds_client *mdsc = session->s_mdsc;
3068 struct ceph_mds_request *req;
3069 struct ceph_mds_reply_head *head = msg->front.iov_base;
3070 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
Yan, Zheng982d6012014-12-23 15:30:54 +08003071 struct ceph_snap_realm *realm;
Sage Weil2f2dc052009-10-06 11:31:09 -07003072 u64 tid;
3073 int err, result;
Sage Weil2600d2d2010-02-22 15:12:16 -08003074 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07003075
Sage Weil2f2dc052009-10-06 11:31:09 -07003076 if (msg->front.iov_len < sizeof(*head)) {
3077 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08003078 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003079 return;
3080 }
3081
3082 /* get request, session */
Sage Weil6df058c2009-12-22 11:24:33 -08003083 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07003084 mutex_lock(&mdsc->mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02003085 req = lookup_get_request(mdsc, tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07003086 if (!req) {
3087 dout("handle_reply on unknown tid %llu\n", tid);
3088 mutex_unlock(&mdsc->mutex);
3089 return;
3090 }
3091 dout("handle_reply %p\n", req);
Sage Weil2f2dc052009-10-06 11:31:09 -07003092
3093 /* correct session? */
Sage Weild96d6042010-03-20 20:50:58 -07003094 if (req->r_session != session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003095 pr_err("mdsc_handle_reply got %llu on session mds%d"
3096 " not mds%d\n", tid, session->s_mds,
3097 req->r_session ? req->r_session->s_mds : -1);
3098 mutex_unlock(&mdsc->mutex);
3099 goto out;
3100 }
3101
3102 /* dup? */
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003103 if ((test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags) && !head->safe) ||
3104 (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags) && head->safe)) {
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07003105 pr_warn("got a dup %s reply on %llu from mds%d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07003106 head->safe ? "safe" : "unsafe", tid, mds);
3107 mutex_unlock(&mdsc->mutex);
3108 goto out;
3109 }
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003110 if (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags)) {
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07003111 pr_warn("got unsafe after safe on %llu from mds%d\n",
Sage Weil85792d0d2010-05-13 09:06:02 -07003112 tid, mds);
3113 mutex_unlock(&mdsc->mutex);
3114 goto out;
3115 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003116
3117 result = le32_to_cpu(head->result);
3118
3119 /*
Greg Farnume55b71f2010-06-22 15:58:01 -07003120 * Handle an ESTALE
3121 * if we're not talking to the authority, send to them
3122 * if the authority has changed while we weren't looking,
3123 * send to new authority
3124 * Otherwise we just have to return an ESTALE
Sage Weil2f2dc052009-10-06 11:31:09 -07003125 */
3126 if (result == -ESTALE) {
Chengguang Xu4c069a52018-01-30 16:29:17 +08003127 dout("got ESTALE on request %llu\n", req->r_tid);
Yan, Zheng51da8e82014-07-14 10:48:29 +08003128 req->r_resend_mds = -1;
Yan, Zhengca18bed2013-11-22 14:21:44 +08003129 if (req->r_direct_mode != USE_AUTH_MDS) {
Chengguang Xu4c069a52018-01-30 16:29:17 +08003130 dout("not using auth, setting for that now\n");
Greg Farnume55b71f2010-06-22 15:58:01 -07003131 req->r_direct_mode = USE_AUTH_MDS;
Sage Weil2f2dc052009-10-06 11:31:09 -07003132 __do_request(mdsc, req);
3133 mutex_unlock(&mdsc->mutex);
3134 goto out;
Greg Farnume55b71f2010-06-22 15:58:01 -07003135 } else {
Xiubo Lic4853e92019-12-09 07:47:15 -05003136 int mds = __choose_mds(mdsc, req, NULL);
Yan, Zhengca18bed2013-11-22 14:21:44 +08003137 if (mds >= 0 && mds != req->r_session->s_mds) {
Chengguang Xu4c069a52018-01-30 16:29:17 +08003138 dout("but auth changed, so resending\n");
Greg Farnume55b71f2010-06-22 15:58:01 -07003139 __do_request(mdsc, req);
3140 mutex_unlock(&mdsc->mutex);
3141 goto out;
3142 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003143 }
Chengguang Xu4c069a52018-01-30 16:29:17 +08003144 dout("have to return ESTALE on request %llu\n", req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07003145 }
3146
Greg Farnume55b71f2010-06-22 15:58:01 -07003147
Sage Weil2f2dc052009-10-06 11:31:09 -07003148 if (head->safe) {
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003149 set_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags);
Sage Weil2f2dc052009-10-06 11:31:09 -07003150 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07003151
Xiubo Li07edc052019-12-04 01:27:18 -05003152 /* last request during umount? */
3153 if (mdsc->stopping && !__get_oldest_req(mdsc))
3154 complete_all(&mdsc->safe_umount_waiters);
3155
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003156 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003157 /*
3158 * We already handled the unsafe response, now do the
3159 * cleanup. No need to examine the response; the MDS
3160 * doesn't include any result info in the safe
3161 * response. And even if it did, there is nothing
3162 * useful we could do with a revised return value.
3163 */
3164 dout("got safe reply %llu, mds%d\n", tid, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003165
Sage Weil2f2dc052009-10-06 11:31:09 -07003166 mutex_unlock(&mdsc->mutex);
3167 goto out;
3168 }
Sage Weile1518c72010-05-13 11:19:06 -07003169 } else {
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003170 set_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags);
Sage Weil2f2dc052009-10-06 11:31:09 -07003171 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
3172 }
3173
3174 dout("handle_reply tid %lld result %d\n", tid, result);
3175 rinfo = &req->r_reply_info;
Yan, Zhengb37fe1f2019-01-09 10:10:17 +08003176 if (test_bit(CEPHFS_FEATURE_REPLY_ENCODING, &session->s_features))
Jeff Laytond4846482019-11-15 11:51:55 -05003177 err = parse_reply_info(session, msg, rinfo, (u64)-1);
Yan, Zhengb37fe1f2019-01-09 10:10:17 +08003178 else
Jeff Laytond4846482019-11-15 11:51:55 -05003179 err = parse_reply_info(session, msg, rinfo, session->s_con.peer_features);
Sage Weil2f2dc052009-10-06 11:31:09 -07003180 mutex_unlock(&mdsc->mutex);
3181
Jeff Laytonbca9fc12020-11-12 10:03:38 -05003182 /* Must find target inode outside of mutexes to avoid deadlocks */
3183 if ((err >= 0) && rinfo->head->is_target) {
3184 struct inode *in;
3185 struct ceph_vino tvino = {
3186 .ino = le64_to_cpu(rinfo->targeti.in->ino),
3187 .snap = le64_to_cpu(rinfo->targeti.in->snapid)
3188 };
3189
3190 in = ceph_get_inode(mdsc->fsc->sb, tvino);
3191 if (IS_ERR(in)) {
3192 err = PTR_ERR(in);
3193 mutex_lock(&session->s_mutex);
3194 goto out_err;
3195 }
3196 req->r_target_inode = in;
3197 }
3198
Sage Weil2f2dc052009-10-06 11:31:09 -07003199 mutex_lock(&session->s_mutex);
3200 if (err < 0) {
Herb Shiu25933ab2010-12-01 14:14:38 -08003201 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
Sage Weil9ec7cab2009-12-14 15:13:47 -08003202 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003203 goto out_err;
3204 }
3205
3206 /* snap trace */
Yan, Zheng982d6012014-12-23 15:30:54 +08003207 realm = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07003208 if (rinfo->snapblob_len) {
3209 down_write(&mdsc->snap_rwsem);
3210 ceph_update_snap_trace(mdsc, rinfo->snapblob,
Yan, Zheng982d6012014-12-23 15:30:54 +08003211 rinfo->snapblob + rinfo->snapblob_len,
3212 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP,
3213 &realm);
Sage Weil2f2dc052009-10-06 11:31:09 -07003214 downgrade_write(&mdsc->snap_rwsem);
3215 } else {
3216 down_read(&mdsc->snap_rwsem);
3217 }
3218
3219 /* insert trace into our cache */
Sage Weilb4556392010-05-13 12:01:13 -07003220 mutex_lock(&req->r_fill_mutex);
Yan, Zheng315f2402016-03-07 10:34:50 +08003221 current->journal_info = req;
Jeff Laytonf5a03b02017-01-31 11:06:13 -05003222 err = ceph_fill_trace(mdsc->fsc->sb, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07003223 if (err == 0) {
Sam Lang6e8575f2012-12-28 09:56:46 -08003224 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
Yan, Zheng81c6aea2013-09-18 09:44:13 +08003225 req->r_op == CEPH_MDS_OP_LSSNAP))
Sage Weil2f2dc052009-10-06 11:31:09 -07003226 ceph_readdir_prepopulate(req, req->r_session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003227 }
Yan, Zheng315f2402016-03-07 10:34:50 +08003228 current->journal_info = NULL;
Sage Weilb4556392010-05-13 12:01:13 -07003229 mutex_unlock(&req->r_fill_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003230
3231 up_read(&mdsc->snap_rwsem);
Yan, Zheng982d6012014-12-23 15:30:54 +08003232 if (realm)
3233 ceph_put_snap_realm(mdsc, realm);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08003234
Yan, Zhengfe330322019-02-01 14:57:15 +08003235 if (err == 0) {
3236 if (req->r_target_inode &&
3237 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
3238 struct ceph_inode_info *ci =
3239 ceph_inode(req->r_target_inode);
3240 spin_lock(&ci->i_unsafe_lock);
3241 list_add_tail(&req->r_unsafe_target_item,
3242 &ci->i_unsafe_iops);
3243 spin_unlock(&ci->i_unsafe_lock);
3244 }
3245
3246 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08003247 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003248out_err:
Sage Weile1518c72010-05-13 11:19:06 -07003249 mutex_lock(&mdsc->mutex);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003250 if (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
Sage Weile1518c72010-05-13 11:19:06 -07003251 if (err) {
3252 req->r_err = err;
3253 } else {
Jianpeng Ma5fdb1382015-08-18 10:30:38 +08003254 req->r_reply = ceph_msg_get(msg);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003255 set_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags);
Sage Weile1518c72010-05-13 11:19:06 -07003256 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003257 } else {
Sage Weile1518c72010-05-13 11:19:06 -07003258 dout("reply arrived after request %lld was aborted\n", tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07003259 }
Sage Weile1518c72010-05-13 11:19:06 -07003260 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003261
Sage Weil2f2dc052009-10-06 11:31:09 -07003262 mutex_unlock(&session->s_mutex);
3263
3264 /* kick calling process */
3265 complete_request(mdsc, req);
Xiubo Li70c94822020-03-19 23:45:02 -04003266
3267 ceph_update_metadata_latency(&mdsc->metric, req->r_start_latency,
3268 req->r_end_latency, err);
Sage Weil2f2dc052009-10-06 11:31:09 -07003269out:
3270 ceph_mdsc_put_request(req);
3271 return;
3272}
3273
3274
3275
3276/*
3277 * handle mds notification that our request has been forwarded.
3278 */
Sage Weil2600d2d2010-02-22 15:12:16 -08003279static void handle_forward(struct ceph_mds_client *mdsc,
3280 struct ceph_mds_session *session,
3281 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07003282{
3283 struct ceph_mds_request *req;
Sage Weila1ea7872010-02-23 14:02:44 -08003284 u64 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07003285 u32 next_mds;
3286 u32 fwd_seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003287 int err = -EINVAL;
3288 void *p = msg->front.iov_base;
3289 void *end = p + msg->front.iov_len;
Sage Weil2f2dc052009-10-06 11:31:09 -07003290
Sage Weila1ea7872010-02-23 14:02:44 -08003291 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07003292 next_mds = ceph_decode_32(&p);
3293 fwd_seq = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07003294
3295 mutex_lock(&mdsc->mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02003296 req = lookup_get_request(mdsc, tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07003297 if (!req) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07003298 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003299 goto out; /* dup reply? */
3300 }
3301
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003302 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07003303 dout("forward tid %llu aborted, unregistering\n", tid);
3304 __unregister_request(mdsc, req);
3305 } else if (fwd_seq <= req->r_num_fwd) {
3306 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07003307 tid, next_mds, req->r_num_fwd, fwd_seq);
3308 } else {
3309 /* resend. forward race not possible; mds would drop */
Sage Weil2a8e5e32010-05-28 16:43:16 -07003310 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
3311 BUG_ON(req->r_err);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003312 BUG_ON(test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags));
Yan, Zheng3de22be2015-02-04 14:26:22 +08003313 req->r_attempts = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003314 req->r_num_fwd = fwd_seq;
3315 req->r_resend_mds = next_mds;
3316 put_request_session(req);
3317 __do_request(mdsc, req);
3318 }
3319 ceph_mdsc_put_request(req);
3320out:
3321 mutex_unlock(&mdsc->mutex);
3322 return;
3323
3324bad:
3325 pr_err("mdsc_handle_forward decode error err=%d\n", err);
3326}
3327
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003328static int __decode_session_metadata(void **p, void *end,
Ilya Dryomov0b98acd2020-09-14 13:39:19 +02003329 bool *blocklisted)
Yan, Zheng84bf3952018-12-21 17:41:39 +08003330{
3331 /* map<string,string> */
3332 u32 n;
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003333 bool err_str;
Yan, Zheng84bf3952018-12-21 17:41:39 +08003334 ceph_decode_32_safe(p, end, n, bad);
3335 while (n-- > 0) {
3336 u32 len;
3337 ceph_decode_32_safe(p, end, len, bad);
3338 ceph_decode_need(p, end, len, bad);
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003339 err_str = !strncmp(*p, "error_string", len);
Yan, Zheng84bf3952018-12-21 17:41:39 +08003340 *p += len;
3341 ceph_decode_32_safe(p, end, len, bad);
3342 ceph_decode_need(p, end, len, bad);
Ilya Dryomov4bb926e2020-09-15 21:11:30 +02003343 /*
3344 * Match "blocklisted (blacklisted)" from newer MDSes,
3345 * or "blacklisted" from older MDSes.
3346 */
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003347 if (err_str && strnstr(*p, "blacklisted", len))
Ilya Dryomov0b98acd2020-09-14 13:39:19 +02003348 *blocklisted = true;
Yan, Zheng84bf3952018-12-21 17:41:39 +08003349 *p += len;
3350 }
3351 return 0;
3352bad:
3353 return -1;
3354}
3355
Sage Weil2f2dc052009-10-06 11:31:09 -07003356/*
3357 * handle a mds session control message
3358 */
3359static void handle_session(struct ceph_mds_session *session,
3360 struct ceph_msg *msg)
3361{
3362 struct ceph_mds_client *mdsc = session->s_mdsc;
Yan, Zheng84bf3952018-12-21 17:41:39 +08003363 int mds = session->s_mds;
3364 int msg_version = le16_to_cpu(msg->hdr.version);
3365 void *p = msg->front.iov_base;
3366 void *end = p + msg->front.iov_len;
3367 struct ceph_mds_session_head *h;
Sage Weil2f2dc052009-10-06 11:31:09 -07003368 u32 op;
Jeff Layton0fa82632020-04-28 08:10:22 -04003369 u64 seq, features = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003370 int wake = 0;
Ilya Dryomov0b98acd2020-09-14 13:39:19 +02003371 bool blocklisted = false;
Sage Weil2f2dc052009-10-06 11:31:09 -07003372
Sage Weil2f2dc052009-10-06 11:31:09 -07003373 /* decode */
Yan, Zheng84bf3952018-12-21 17:41:39 +08003374 ceph_decode_need(&p, end, sizeof(*h), bad);
3375 h = p;
3376 p += sizeof(*h);
3377
Sage Weil2f2dc052009-10-06 11:31:09 -07003378 op = le32_to_cpu(h->op);
3379 seq = le64_to_cpu(h->seq);
3380
Yan, Zheng84bf3952018-12-21 17:41:39 +08003381 if (msg_version >= 3) {
3382 u32 len;
3383 /* version >= 2, metadata */
Ilya Dryomov0b98acd2020-09-14 13:39:19 +02003384 if (__decode_session_metadata(&p, end, &blocklisted) < 0)
Yan, Zheng84bf3952018-12-21 17:41:39 +08003385 goto bad;
3386 /* version >= 3, feature bits */
3387 ceph_decode_32_safe(&p, end, len, bad);
Jeff Layton02e37572020-08-04 12:31:56 -04003388 if (len) {
3389 ceph_decode_64_safe(&p, end, features, bad);
3390 p += len - sizeof(features);
3391 }
Yan, Zheng84bf3952018-12-21 17:41:39 +08003392 }
3393
Sage Weil2f2dc052009-10-06 11:31:09 -07003394 mutex_lock(&mdsc->mutex);
Yan, Zheng0a07fc82017-03-29 15:30:24 +08003395 if (op == CEPH_SESSION_CLOSE) {
Xiubo Li5b3248c2019-12-19 19:44:09 -05003396 ceph_get_mds_session(session);
Sage Weil2600d2d2010-02-22 15:12:16 -08003397 __unregister_session(mdsc, session);
Yan, Zheng0a07fc82017-03-29 15:30:24 +08003398 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003399 /* FIXME: this ttl calculation is generous */
3400 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
3401 mutex_unlock(&mdsc->mutex);
3402
3403 mutex_lock(&session->s_mutex);
3404
3405 dout("handle_session mds%d %s %p state %s seq %llu\n",
3406 mds, ceph_session_op_name(op), session,
John Spraya687eca2014-09-19 13:51:08 +01003407 ceph_session_state_name(session->s_state), seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003408
3409 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
3410 session->s_state = CEPH_MDS_SESSION_OPEN;
3411 pr_info("mds%d came back\n", session->s_mds);
3412 }
3413
3414 switch (op) {
3415 case CEPH_SESSION_OPEN:
Sage Weil29790f22010-03-18 14:45:05 -07003416 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
3417 pr_info("mds%d reconnect success\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003418 session->s_state = CEPH_MDS_SESSION_OPEN;
Yan, Zheng84bf3952018-12-21 17:41:39 +08003419 session->s_features = features;
Sage Weil2f2dc052009-10-06 11:31:09 -07003420 renewed_caps(mdsc, session, 0);
Xiubo Li18f473b2020-07-16 10:05:57 -04003421 if (test_bit(CEPHFS_FEATURE_METRIC_COLLECT, &session->s_features))
3422 metric_schedule_delayed(&mdsc->metric);
Sage Weil2f2dc052009-10-06 11:31:09 -07003423 wake = 1;
3424 if (mdsc->stopping)
3425 __close_session(mdsc, session);
3426 break;
3427
3428 case CEPH_SESSION_RENEWCAPS:
3429 if (session->s_renew_seq == seq)
3430 renewed_caps(mdsc, session, 1);
3431 break;
3432
3433 case CEPH_SESSION_CLOSE:
Sage Weil29790f22010-03-18 14:45:05 -07003434 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
3435 pr_info("mds%d reconnect denied\n", session->s_mds);
Xiubo Li4d681c22019-12-05 22:35:51 -05003436 session->s_state = CEPH_MDS_SESSION_CLOSED;
Yan, Zheng1c841a92015-03-24 20:15:36 +08003437 cleanup_session_requests(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003438 remove_session_caps(session);
Yan, Zheng656e4382014-09-11 14:25:18 +08003439 wake = 2; /* for good measure */
Sage Weilf3c60c52010-08-11 14:51:23 -07003440 wake_up_all(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003441 break;
3442
3443 case CEPH_SESSION_STALE:
3444 pr_info("mds%d caps went stale, renewing\n",
3445 session->s_mds);
Alex Elderd8fb02a2012-01-12 17:48:10 -08003446 spin_lock(&session->s_gen_ttl_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07003447 session->s_cap_gen++;
Alex Elder1ce208a2012-01-12 17:48:11 -08003448 session->s_cap_ttl = jiffies - 1;
Alex Elderd8fb02a2012-01-12 17:48:10 -08003449 spin_unlock(&session->s_gen_ttl_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07003450 send_renew_caps(mdsc, session);
3451 break;
3452
3453 case CEPH_SESSION_RECALL_STATE:
Zhi Zhange30ee582018-01-24 21:24:33 +08003454 ceph_trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
Sage Weil2f2dc052009-10-06 11:31:09 -07003455 break;
3456
Yan, Zheng186e4f72013-11-22 14:48:37 +08003457 case CEPH_SESSION_FLUSHMSG:
3458 send_flushmsg_ack(mdsc, session, seq);
3459 break;
3460
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08003461 case CEPH_SESSION_FORCE_RO:
3462 dout("force_session_readonly %p\n", session);
3463 spin_lock(&session->s_cap_lock);
3464 session->s_readonly = true;
3465 spin_unlock(&session->s_cap_lock);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08003466 wake_up_session_caps(session, FORCE_RO);
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08003467 break;
3468
Yan, Zhengfcff4152016-09-14 16:39:51 +08003469 case CEPH_SESSION_REJECT:
3470 WARN_ON(session->s_state != CEPH_MDS_SESSION_OPENING);
3471 pr_info("mds%d rejected session\n", session->s_mds);
3472 session->s_state = CEPH_MDS_SESSION_REJECTED;
3473 cleanup_session_requests(mdsc, session);
3474 remove_session_caps(session);
Ilya Dryomov0b98acd2020-09-14 13:39:19 +02003475 if (blocklisted)
3476 mdsc->fsc->blocklisted = true;
Yan, Zhengfcff4152016-09-14 16:39:51 +08003477 wake = 2; /* for good measure */
3478 break;
3479
Sage Weil2f2dc052009-10-06 11:31:09 -07003480 default:
3481 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
3482 WARN_ON(1);
3483 }
3484
3485 mutex_unlock(&session->s_mutex);
3486 if (wake) {
3487 mutex_lock(&mdsc->mutex);
3488 __wake_requests(mdsc, &session->s_waiting);
Yan, Zheng656e4382014-09-11 14:25:18 +08003489 if (wake == 2)
3490 kick_requests(mdsc, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003491 mutex_unlock(&mdsc->mutex);
3492 }
Yan, Zheng0a07fc82017-03-29 15:30:24 +08003493 if (op == CEPH_SESSION_CLOSE)
3494 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003495 return;
3496
3497bad:
3498 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
3499 (int)msg->front.iov_len);
Sage Weil9ec7cab2009-12-14 15:13:47 -08003500 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003501 return;
3502}
3503
Jeff Laytona25949b2020-02-18 14:12:45 -05003504void ceph_mdsc_release_dir_caps(struct ceph_mds_request *req)
3505{
3506 int dcaps;
3507
3508 dcaps = xchg(&req->r_dir_caps, 0);
3509 if (dcaps) {
3510 dout("releasing r_dir_caps=%s\n", ceph_cap_string(dcaps));
3511 ceph_put_cap_refs(ceph_inode(req->r_parent), dcaps);
3512 }
3513}
3514
Xiubo Lie64f44a2020-05-27 09:09:27 -04003515void ceph_mdsc_release_dir_caps_no_check(struct ceph_mds_request *req)
3516{
3517 int dcaps;
3518
3519 dcaps = xchg(&req->r_dir_caps, 0);
3520 if (dcaps) {
3521 dout("releasing r_dir_caps=%s\n", ceph_cap_string(dcaps));
3522 ceph_put_cap_refs_no_check_caps(ceph_inode(req->r_parent),
3523 dcaps);
3524 }
3525}
3526
Sage Weil2f2dc052009-10-06 11:31:09 -07003527/*
3528 * called under session->mutex.
3529 */
3530static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
3531 struct ceph_mds_session *session)
3532{
3533 struct ceph_mds_request *req, *nreq;
Yan, Zheng3de22be2015-02-04 14:26:22 +08003534 struct rb_node *p;
Sage Weil2f2dc052009-10-06 11:31:09 -07003535
3536 dout("replay_unsafe_requests mds%d\n", session->s_mds);
3537
3538 mutex_lock(&mdsc->mutex);
Xiubo Li9cf54562019-12-05 20:50:21 -05003539 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item)
3540 __send_request(mdsc, session, req, true);
Yan, Zheng3de22be2015-02-04 14:26:22 +08003541
3542 /*
3543 * also re-send old requests when MDS enters reconnect stage. So that MDS
3544 * can process completed request in clientreplay stage.
3545 */
3546 p = rb_first(&mdsc->request_tree);
3547 while (p) {
3548 req = rb_entry(p, struct ceph_mds_request, r_node);
3549 p = rb_next(p);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003550 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Yan, Zheng3de22be2015-02-04 14:26:22 +08003551 continue;
3552 if (req->r_attempts == 0)
3553 continue; /* only old requests */
Jeff Laytona25949b2020-02-18 14:12:45 -05003554 if (!req->r_session)
3555 continue;
3556 if (req->r_session->s_mds != session->s_mds)
3557 continue;
3558
Xiubo Lie64f44a2020-05-27 09:09:27 -04003559 ceph_mdsc_release_dir_caps_no_check(req);
Jeff Laytona25949b2020-02-18 14:12:45 -05003560
3561 __send_request(mdsc, session, req, true);
Yan, Zheng3de22be2015-02-04 14:26:22 +08003562 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003563 mutex_unlock(&mdsc->mutex);
3564}
3565
Yan, Zheng81c5a142019-01-01 16:28:33 +08003566static int send_reconnect_partial(struct ceph_reconnect_state *recon_state)
3567{
3568 struct ceph_msg *reply;
3569 struct ceph_pagelist *_pagelist;
3570 struct page *page;
3571 __le32 *addr;
3572 int err = -ENOMEM;
3573
3574 if (!recon_state->allow_multi)
3575 return -ENOSPC;
3576
3577 /* can't handle message that contains both caps and realm */
3578 BUG_ON(!recon_state->nr_caps == !recon_state->nr_realms);
3579
3580 /* pre-allocate new pagelist */
3581 _pagelist = ceph_pagelist_alloc(GFP_NOFS);
3582 if (!_pagelist)
3583 return -ENOMEM;
3584
3585 reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false);
3586 if (!reply)
3587 goto fail_msg;
3588
3589 /* placeholder for nr_caps */
3590 err = ceph_pagelist_encode_32(_pagelist, 0);
3591 if (err < 0)
3592 goto fail;
3593
3594 if (recon_state->nr_caps) {
3595 /* currently encoding caps */
3596 err = ceph_pagelist_encode_32(recon_state->pagelist, 0);
3597 if (err)
3598 goto fail;
3599 } else {
3600 /* placeholder for nr_realms (currently encoding relams) */
3601 err = ceph_pagelist_encode_32(_pagelist, 0);
3602 if (err < 0)
3603 goto fail;
3604 }
3605
3606 err = ceph_pagelist_encode_8(recon_state->pagelist, 1);
3607 if (err)
3608 goto fail;
3609
3610 page = list_first_entry(&recon_state->pagelist->head, struct page, lru);
3611 addr = kmap_atomic(page);
3612 if (recon_state->nr_caps) {
3613 /* currently encoding caps */
3614 *addr = cpu_to_le32(recon_state->nr_caps);
3615 } else {
3616 /* currently encoding relams */
3617 *(addr + 1) = cpu_to_le32(recon_state->nr_realms);
3618 }
3619 kunmap_atomic(addr);
3620
3621 reply->hdr.version = cpu_to_le16(5);
3622 reply->hdr.compat_version = cpu_to_le16(4);
3623
3624 reply->hdr.data_len = cpu_to_le32(recon_state->pagelist->length);
3625 ceph_msg_data_add_pagelist(reply, recon_state->pagelist);
3626
3627 ceph_con_send(&recon_state->session->s_con, reply);
3628 ceph_pagelist_release(recon_state->pagelist);
3629
3630 recon_state->pagelist = _pagelist;
3631 recon_state->nr_caps = 0;
3632 recon_state->nr_realms = 0;
3633 recon_state->msg_version = 5;
3634 return 0;
3635fail:
3636 ceph_msg_put(reply);
3637fail_msg:
3638 ceph_pagelist_release(_pagelist);
3639 return err;
3640}
3641
Yan, Zhenga33f6432020-08-11 15:23:03 +08003642static struct dentry* d_find_primary(struct inode *inode)
3643{
3644 struct dentry *alias, *dn = NULL;
3645
3646 if (hlist_empty(&inode->i_dentry))
3647 return NULL;
3648
3649 spin_lock(&inode->i_lock);
3650 if (hlist_empty(&inode->i_dentry))
3651 goto out_unlock;
3652
3653 if (S_ISDIR(inode->i_mode)) {
3654 alias = hlist_entry(inode->i_dentry.first, struct dentry, d_u.d_alias);
3655 if (!IS_ROOT(alias))
3656 dn = dget(alias);
3657 goto out_unlock;
3658 }
3659
3660 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
3661 spin_lock(&alias->d_lock);
3662 if (!d_unhashed(alias) &&
3663 (ceph_dentry(alias)->flags & CEPH_DENTRY_PRIMARY_LINK)) {
3664 dn = dget_dlock(alias);
3665 }
3666 spin_unlock(&alias->d_lock);
3667 if (dn)
3668 break;
3669 }
3670out_unlock:
3671 spin_unlock(&inode->i_lock);
3672 return dn;
3673}
3674
Sage Weil2f2dc052009-10-06 11:31:09 -07003675/*
3676 * Encode information about a cap for a reconnect with the MDS.
3677 */
Jeff Laytona25949b2020-02-18 14:12:45 -05003678static int reconnect_caps_cb(struct inode *inode, struct ceph_cap *cap,
Sage Weil2f2dc052009-10-06 11:31:09 -07003679 void *arg)
3680{
Sage Weil20cb34a2010-05-12 15:21:32 -07003681 union {
3682 struct ceph_mds_cap_reconnect v2;
3683 struct ceph_mds_cap_reconnect_v1 v1;
3684 } rec;
Yan, Zhengb3f8d682017-09-11 10:58:55 +08003685 struct ceph_inode_info *ci = cap->ci;
Sage Weil20cb34a2010-05-12 15:21:32 -07003686 struct ceph_reconnect_state *recon_state = arg;
3687 struct ceph_pagelist *pagelist = recon_state->pagelist;
Yan, Zhenga33f6432020-08-11 15:23:03 +08003688 struct dentry *dentry;
3689 char *path;
3690 int pathlen, err;
3691 u64 pathbase;
Yan, Zheng3469ed02016-07-05 09:32:31 +08003692 u64 snap_follows;
Sage Weil2f2dc052009-10-06 11:31:09 -07003693
Sage Weil2f2dc052009-10-06 11:31:09 -07003694 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
3695 inode, ceph_vinop(inode), cap, cap->cap_id,
3696 ceph_cap_string(cap->issued));
Sage Weil2f2dc052009-10-06 11:31:09 -07003697
Yan, Zhenga33f6432020-08-11 15:23:03 +08003698 dentry = d_find_primary(inode);
3699 if (dentry) {
3700 /* set pathbase to parent dir when msg_version >= 2 */
3701 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase,
3702 recon_state->msg_version >= 2);
3703 dput(dentry);
3704 if (IS_ERR(path)) {
3705 err = PTR_ERR(path);
3706 goto out_err;
3707 }
3708 } else {
3709 path = NULL;
3710 pathlen = 0;
3711 pathbase = 0;
3712 }
3713
Sage Weilbe655592011-11-30 09:47:09 -08003714 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07003715 cap->seq = 0; /* reset cap seq */
3716 cap->issue_seq = 0; /* and issue_seq */
Yan, Zheng667ca052013-05-31 16:25:36 +08003717 cap->mseq = 0; /* and migrate_seq */
Yan, Zheng99a9c272013-09-22 11:08:14 +08003718 cap->cap_gen = cap->session->s_cap_gen;
Sage Weil20cb34a2010-05-12 15:21:32 -07003719
Jeff Laytona25949b2020-02-18 14:12:45 -05003720 /* These are lost when the session goes away */
Jeff Layton785892f2020-01-02 07:11:38 -05003721 if (S_ISDIR(inode->i_mode)) {
3722 if (cap->issued & CEPH_CAP_DIR_CREATE) {
3723 ceph_put_string(rcu_dereference_raw(ci->i_cached_layout.pool_ns));
3724 memset(&ci->i_cached_layout, 0, sizeof(ci->i_cached_layout));
3725 }
Jeff Laytona25949b2020-02-18 14:12:45 -05003726 cap->issued &= ~CEPH_CAP_ANY_DIR_OPS;
Jeff Layton785892f2020-01-02 07:11:38 -05003727 }
Jeff Laytona25949b2020-02-18 14:12:45 -05003728
Yan, Zheng121f22a2016-07-04 22:05:18 +08003729 if (recon_state->msg_version >= 2) {
Sage Weil20cb34a2010-05-12 15:21:32 -07003730 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
3731 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
3732 rec.v2.issued = cpu_to_le32(cap->issued);
3733 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
Yan, Zhenga33f6432020-08-11 15:23:03 +08003734 rec.v2.pathbase = cpu_to_le64(pathbase);
Jeff Laytonec1dff22017-10-31 15:51:14 -04003735 rec.v2.flock_len = (__force __le32)
3736 ((ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) ? 0 : 1);
Sage Weil20cb34a2010-05-12 15:21:32 -07003737 } else {
3738 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
3739 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
3740 rec.v1.issued = cpu_to_le32(cap->issued);
3741 rec.v1.size = cpu_to_le64(inode->i_size);
Arnd Bergmann9bbeab42018-07-13 22:18:36 +02003742 ceph_encode_timespec64(&rec.v1.mtime, &inode->i_mtime);
3743 ceph_encode_timespec64(&rec.v1.atime, &inode->i_atime);
Sage Weil20cb34a2010-05-12 15:21:32 -07003744 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
Yan, Zhenga33f6432020-08-11 15:23:03 +08003745 rec.v1.pathbase = cpu_to_le64(pathbase);
Sage Weil20cb34a2010-05-12 15:21:32 -07003746 }
Yan, Zheng3469ed02016-07-05 09:32:31 +08003747
3748 if (list_empty(&ci->i_cap_snaps)) {
Yan, Zheng92776fd2017-08-16 21:42:39 +08003749 snap_follows = ci->i_head_snapc ? ci->i_head_snapc->seq : 0;
Yan, Zheng3469ed02016-07-05 09:32:31 +08003750 } else {
3751 struct ceph_cap_snap *capsnap =
3752 list_first_entry(&ci->i_cap_snaps,
3753 struct ceph_cap_snap, ci_item);
3754 snap_follows = capsnap->follows;
Sage Weil93cea5b2009-12-23 12:21:51 -08003755 }
Sage Weilbe655592011-11-30 09:47:09 -08003756 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07003757
Yan, Zheng121f22a2016-07-04 22:05:18 +08003758 if (recon_state->msg_version >= 2) {
Greg Farnum40819f62010-08-02 15:34:23 -07003759 int num_fcntl_locks, num_flock_locks;
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003760 struct ceph_filelock *flocks = NULL;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003761 size_t struct_len, total_len = sizeof(u64);
Yan, Zheng121f22a2016-07-04 22:05:18 +08003762 u8 struct_v = 0;
Greg Farnum40819f62010-08-02 15:34:23 -07003763
Jim Schutt39be95e2013-05-15 13:03:35 -05003764encode_again:
Yan, Zhengb3f8d682017-09-11 10:58:55 +08003765 if (rec.v2.flock_len) {
3766 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
3767 } else {
3768 num_fcntl_locks = 0;
3769 num_flock_locks = 0;
3770 }
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003771 if (num_fcntl_locks + num_flock_locks > 0) {
Kees Cook6da2ec52018-06-12 13:55:00 -07003772 flocks = kmalloc_array(num_fcntl_locks + num_flock_locks,
3773 sizeof(struct ceph_filelock),
3774 GFP_NOFS);
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003775 if (!flocks) {
3776 err = -ENOMEM;
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003777 goto out_err;
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003778 }
3779 err = ceph_encode_locks_to_buffer(inode, flocks,
3780 num_fcntl_locks,
3781 num_flock_locks);
3782 if (err) {
3783 kfree(flocks);
3784 flocks = NULL;
3785 if (err == -ENOSPC)
3786 goto encode_again;
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003787 goto out_err;
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003788 }
3789 } else {
Jim Schutt39be95e2013-05-15 13:03:35 -05003790 kfree(flocks);
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003791 flocks = NULL;
Jim Schutt39be95e2013-05-15 13:03:35 -05003792 }
Yan, Zheng121f22a2016-07-04 22:05:18 +08003793
3794 if (recon_state->msg_version >= 3) {
3795 /* version, compat_version and struct_len */
Yan, Zheng81c5a142019-01-01 16:28:33 +08003796 total_len += 2 * sizeof(u8) + sizeof(u32);
Yan, Zheng3469ed02016-07-05 09:32:31 +08003797 struct_v = 2;
Yan, Zheng121f22a2016-07-04 22:05:18 +08003798 }
Jim Schutt39be95e2013-05-15 13:03:35 -05003799 /*
3800 * number of encoded locks is stable, so copy to pagelist
3801 */
Yan, Zheng121f22a2016-07-04 22:05:18 +08003802 struct_len = 2 * sizeof(u32) +
3803 (num_fcntl_locks + num_flock_locks) *
3804 sizeof(struct ceph_filelock);
3805 rec.v2.flock_len = cpu_to_le32(struct_len);
3806
Yan, Zhenga33f6432020-08-11 15:23:03 +08003807 struct_len += sizeof(u32) + pathlen + sizeof(rec.v2);
Yan, Zheng121f22a2016-07-04 22:05:18 +08003808
Yan, Zheng3469ed02016-07-05 09:32:31 +08003809 if (struct_v >= 2)
3810 struct_len += sizeof(u64); /* snap_follows */
3811
Yan, Zheng121f22a2016-07-04 22:05:18 +08003812 total_len += struct_len;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003813
3814 if (pagelist->length + total_len > RECONNECT_MAX_SIZE) {
3815 err = send_reconnect_partial(recon_state);
3816 if (err)
3817 goto out_freeflocks;
3818 pagelist = recon_state->pagelist;
Yan, Zheng121f22a2016-07-04 22:05:18 +08003819 }
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003820
Yan, Zheng81c5a142019-01-01 16:28:33 +08003821 err = ceph_pagelist_reserve(pagelist, total_len);
3822 if (err)
3823 goto out_freeflocks;
3824
3825 ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003826 if (recon_state->msg_version >= 3) {
3827 ceph_pagelist_encode_8(pagelist, struct_v);
3828 ceph_pagelist_encode_8(pagelist, 1);
3829 ceph_pagelist_encode_32(pagelist, struct_len);
3830 }
Yan, Zhenga33f6432020-08-11 15:23:03 +08003831 ceph_pagelist_encode_string(pagelist, path, pathlen);
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003832 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v2));
3833 ceph_locks_to_pagelist(flocks, pagelist,
3834 num_fcntl_locks, num_flock_locks);
3835 if (struct_v >= 2)
3836 ceph_pagelist_encode_64(pagelist, snap_follows);
Yan, Zheng81c5a142019-01-01 16:28:33 +08003837out_freeflocks:
Jim Schutt39be95e2013-05-15 13:03:35 -05003838 kfree(flocks);
Sage Weil3612abb2010-09-07 15:59:27 -07003839 } else {
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003840 err = ceph_pagelist_reserve(pagelist,
Yan, Zheng81c5a142019-01-01 16:28:33 +08003841 sizeof(u64) + sizeof(u32) +
3842 pathlen + sizeof(rec.v1));
Yan, Zhenga33f6432020-08-11 15:23:03 +08003843 if (err)
3844 goto out_err;
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003845
Yan, Zheng81c5a142019-01-01 16:28:33 +08003846 ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003847 ceph_pagelist_encode_string(pagelist, path, pathlen);
3848 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v1));
Greg Farnum40819f62010-08-02 15:34:23 -07003849 }
Yan, Zheng44c99752013-09-22 10:28:10 +08003850
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003851out_err:
Yan, Zhenga33f6432020-08-11 15:23:03 +08003852 ceph_mdsc_free_path(path, pathlen);
3853 if (!err)
Yan, Zheng81c5a142019-01-01 16:28:33 +08003854 recon_state->nr_caps++;
3855 return err;
3856}
3857
3858static int encode_snap_realms(struct ceph_mds_client *mdsc,
3859 struct ceph_reconnect_state *recon_state)
3860{
3861 struct rb_node *p;
3862 struct ceph_pagelist *pagelist = recon_state->pagelist;
3863 int err = 0;
3864
3865 if (recon_state->msg_version >= 4) {
3866 err = ceph_pagelist_encode_32(pagelist, mdsc->num_snap_realms);
3867 if (err < 0)
3868 goto fail;
3869 }
3870
3871 /*
3872 * snaprealms. we provide mds with the ino, seq (version), and
3873 * parent for all of our realms. If the mds has any newer info,
3874 * it will tell us.
3875 */
3876 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
3877 struct ceph_snap_realm *realm =
3878 rb_entry(p, struct ceph_snap_realm, node);
3879 struct ceph_mds_snaprealm_reconnect sr_rec;
3880
3881 if (recon_state->msg_version >= 4) {
3882 size_t need = sizeof(u8) * 2 + sizeof(u32) +
3883 sizeof(sr_rec);
3884
3885 if (pagelist->length + need > RECONNECT_MAX_SIZE) {
3886 err = send_reconnect_partial(recon_state);
3887 if (err)
3888 goto fail;
3889 pagelist = recon_state->pagelist;
3890 }
3891
3892 err = ceph_pagelist_reserve(pagelist, need);
3893 if (err)
3894 goto fail;
3895
3896 ceph_pagelist_encode_8(pagelist, 1);
3897 ceph_pagelist_encode_8(pagelist, 1);
3898 ceph_pagelist_encode_32(pagelist, sizeof(sr_rec));
3899 }
3900
3901 dout(" adding snap realm %llx seq %lld parent %llx\n",
3902 realm->ino, realm->seq, realm->parent_ino);
3903 sr_rec.ino = cpu_to_le64(realm->ino);
3904 sr_rec.seq = cpu_to_le64(realm->seq);
3905 sr_rec.parent = cpu_to_le64(realm->parent_ino);
3906
3907 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
3908 if (err)
3909 goto fail;
3910
3911 recon_state->nr_realms++;
3912 }
3913fail:
Sage Weil93cea5b2009-12-23 12:21:51 -08003914 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07003915}
3916
3917
3918/*
3919 * If an MDS fails and recovers, clients need to reconnect in order to
3920 * reestablish shared state. This includes all caps issued through
3921 * this session _and_ the snap_realm hierarchy. Because it's not
3922 * clear which snap realms the mds cares about, we send everything we
3923 * know about.. that ensures we'll then get any new info the
3924 * recovering MDS might have.
3925 *
3926 * This is a relatively heavyweight operation, but it's rare.
Sage Weil2f2dc052009-10-06 11:31:09 -07003927 */
Sage Weil34b6c852010-05-10 16:31:25 -07003928static void send_mds_reconnect(struct ceph_mds_client *mdsc,
3929 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07003930{
Sage Weil2f2dc052009-10-06 11:31:09 -07003931 struct ceph_msg *reply;
Sage Weil34b6c852010-05-10 16:31:25 -07003932 int mds = session->s_mds;
Sage Weil9abf82b2010-05-10 21:58:38 -07003933 int err = -ENOMEM;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003934 struct ceph_reconnect_state recon_state = {
3935 .session = session,
3936 };
Jeff Laytonc8a96a32017-10-19 08:53:58 -04003937 LIST_HEAD(dispose);
Sage Weil2f2dc052009-10-06 11:31:09 -07003938
Sage Weil34b6c852010-05-10 16:31:25 -07003939 pr_info("mds%d reconnect start\n", mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003940
Yan, Zheng81c5a142019-01-01 16:28:33 +08003941 recon_state.pagelist = ceph_pagelist_alloc(GFP_NOFS);
3942 if (!recon_state.pagelist)
Sage Weil93cea5b2009-12-23 12:21:51 -08003943 goto fail_nopagelist;
Sage Weil93cea5b2009-12-23 12:21:51 -08003944
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003945 reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07003946 if (!reply)
Sage Weil93cea5b2009-12-23 12:21:51 -08003947 goto fail_nomsg;
Sage Weil93cea5b2009-12-23 12:21:51 -08003948
Jeff Laytond4846482019-11-15 11:51:55 -05003949 xa_destroy(&session->s_delegated_inos);
3950
Sage Weil34b6c852010-05-10 16:31:25 -07003951 mutex_lock(&session->s_mutex);
3952 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
3953 session->s_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003954
Sage Weil2f2dc052009-10-06 11:31:09 -07003955 dout("session %p state %s\n", session,
John Spraya687eca2014-09-19 13:51:08 +01003956 ceph_session_state_name(session->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07003957
Yan, Zheng99a9c272013-09-22 11:08:14 +08003958 spin_lock(&session->s_gen_ttl_lock);
3959 session->s_cap_gen++;
3960 spin_unlock(&session->s_gen_ttl_lock);
3961
3962 spin_lock(&session->s_cap_lock);
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08003963 /* don't know if session is readonly */
3964 session->s_readonly = 0;
Yan, Zheng99a9c272013-09-22 11:08:14 +08003965 /*
3966 * notify __ceph_remove_cap() that we are composing cap reconnect.
3967 * If a cap get released before being added to the cap reconnect,
3968 * __ceph_remove_cap() should skip queuing cap release.
3969 */
3970 session->s_cap_reconnect = 1;
Sage Weile01a5942010-05-10 15:36:44 -07003971 /* drop old cap expires; we're about to reestablish that state */
Jeff Laytonc8a96a32017-10-19 08:53:58 -04003972 detach_cap_releases(session, &dispose);
3973 spin_unlock(&session->s_cap_lock);
3974 dispose_cap_releases(mdsc, &dispose);
Sage Weile01a5942010-05-10 15:36:44 -07003975
Yan, Zheng5d233712014-09-10 16:56:23 +08003976 /* trim unused caps to reduce MDS's cache rejoin time */
Yan, Zhengc0bd50e2015-04-07 15:51:08 +08003977 if (mdsc->fsc->sb->s_root)
3978 shrink_dcache_parent(mdsc->fsc->sb->s_root);
Yan, Zheng5d233712014-09-10 16:56:23 +08003979
3980 ceph_con_close(&session->s_con);
3981 ceph_con_open(&session->s_con,
3982 CEPH_ENTITY_TYPE_MDS, mds,
3983 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
3984
3985 /* replay unsafe requests */
3986 replay_unsafe_requests(mdsc, session);
3987
Yan, Zheng81c5a142019-01-01 16:28:33 +08003988 ceph_early_kick_flushing_caps(mdsc, session);
3989
Yan, Zheng5d233712014-09-10 16:56:23 +08003990 down_read(&mdsc->snap_rwsem);
3991
Yan, Zheng81c5a142019-01-01 16:28:33 +08003992 /* placeholder for nr_caps */
3993 err = ceph_pagelist_encode_32(recon_state.pagelist, 0);
Sage Weil93cea5b2009-12-23 12:21:51 -08003994 if (err)
3995 goto fail;
Sage Weil20cb34a2010-05-12 15:21:32 -07003996
Yan, Zheng81c5a142019-01-01 16:28:33 +08003997 if (test_bit(CEPHFS_FEATURE_MULTI_RECONNECT, &session->s_features)) {
Yan, Zheng121f22a2016-07-04 22:05:18 +08003998 recon_state.msg_version = 3;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003999 recon_state.allow_multi = true;
4000 } else if (session->s_con.peer_features & CEPH_FEATURE_MDSENC) {
4001 recon_state.msg_version = 3;
4002 } else {
Ilya Dryomov23c625c2018-11-08 14:55:21 +01004003 recon_state.msg_version = 2;
Yan, Zheng81c5a142019-01-01 16:28:33 +08004004 }
4005 /* trsaverse this session's caps */
Jeff Laytona25949b2020-02-18 14:12:45 -05004006 err = ceph_iterate_session_caps(session, reconnect_caps_cb, &recon_state);
Sage Weil2f2dc052009-10-06 11:31:09 -07004007
Yan, Zheng99a9c272013-09-22 11:08:14 +08004008 spin_lock(&session->s_cap_lock);
4009 session->s_cap_reconnect = 0;
4010 spin_unlock(&session->s_cap_lock);
4011
Yan, Zheng81c5a142019-01-01 16:28:33 +08004012 if (err < 0)
4013 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07004014
Yan, Zheng81c5a142019-01-01 16:28:33 +08004015 /* check if all realms can be encoded into current message */
4016 if (mdsc->num_snap_realms) {
4017 size_t total_len =
4018 recon_state.pagelist->length +
4019 mdsc->num_snap_realms *
4020 sizeof(struct ceph_mds_snaprealm_reconnect);
4021 if (recon_state.msg_version >= 4) {
4022 /* number of realms */
4023 total_len += sizeof(u32);
4024 /* version, compat_version and struct_len */
4025 total_len += mdsc->num_snap_realms *
4026 (2 * sizeof(u8) + sizeof(u32));
4027 }
4028 if (total_len > RECONNECT_MAX_SIZE) {
4029 if (!recon_state.allow_multi) {
4030 err = -ENOSPC;
4031 goto fail;
4032 }
4033 if (recon_state.nr_caps) {
4034 err = send_reconnect_partial(&recon_state);
4035 if (err)
4036 goto fail;
4037 }
4038 recon_state.msg_version = 5;
4039 }
4040 }
4041
4042 err = encode_snap_realms(mdsc, &recon_state);
4043 if (err < 0)
4044 goto fail;
4045
4046 if (recon_state.msg_version >= 5) {
4047 err = ceph_pagelist_encode_8(recon_state.pagelist, 0);
4048 if (err < 0)
Sage Weil93cea5b2009-12-23 12:21:51 -08004049 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07004050 }
Sage Weil2f2dc052009-10-06 11:31:09 -07004051
Yan, Zheng81c5a142019-01-01 16:28:33 +08004052 if (recon_state.nr_caps || recon_state.nr_realms) {
4053 struct page *page =
4054 list_first_entry(&recon_state.pagelist->head,
4055 struct page, lru);
Yan, Zheng44c99752013-09-22 10:28:10 +08004056 __le32 *addr = kmap_atomic(page);
Yan, Zheng81c5a142019-01-01 16:28:33 +08004057 if (recon_state.nr_caps) {
4058 WARN_ON(recon_state.nr_realms != mdsc->num_snap_realms);
4059 *addr = cpu_to_le32(recon_state.nr_caps);
4060 } else if (recon_state.msg_version >= 4) {
4061 *(addr + 1) = cpu_to_le32(recon_state.nr_realms);
4062 }
Yan, Zheng44c99752013-09-22 10:28:10 +08004063 kunmap_atomic(addr);
Alex Elderebf18f42013-03-04 22:29:57 -06004064 }
Yan, Zheng44c99752013-09-22 10:28:10 +08004065
Yan, Zheng81c5a142019-01-01 16:28:33 +08004066 reply->hdr.version = cpu_to_le16(recon_state.msg_version);
4067 if (recon_state.msg_version >= 4)
4068 reply->hdr.compat_version = cpu_to_le16(4);
Yan, Zhenge548e9b2015-06-10 15:17:56 +08004069
Yan, Zheng81c5a142019-01-01 16:28:33 +08004070 reply->hdr.data_len = cpu_to_le32(recon_state.pagelist->length);
4071 ceph_msg_data_add_pagelist(reply, recon_state.pagelist);
Yan, Zhenge548e9b2015-06-10 15:17:56 +08004072
Sage Weil2f2dc052009-10-06 11:31:09 -07004073 ceph_con_send(&session->s_con, reply);
4074
Sage Weil9abf82b2010-05-10 21:58:38 -07004075 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07004076
Sage Weil9abf82b2010-05-10 21:58:38 -07004077 mutex_lock(&mdsc->mutex);
4078 __wake_requests(mdsc, &session->s_waiting);
4079 mutex_unlock(&mdsc->mutex);
4080
Sage Weil2f2dc052009-10-06 11:31:09 -07004081 up_read(&mdsc->snap_rwsem);
Yan, Zheng81c5a142019-01-01 16:28:33 +08004082 ceph_pagelist_release(recon_state.pagelist);
Sage Weil2f2dc052009-10-06 11:31:09 -07004083 return;
4084
Sage Weil93cea5b2009-12-23 12:21:51 -08004085fail:
Sage Weil2f2dc052009-10-06 11:31:09 -07004086 ceph_msg_put(reply);
Sage Weil9abf82b2010-05-10 21:58:38 -07004087 up_read(&mdsc->snap_rwsem);
4088 mutex_unlock(&session->s_mutex);
Sage Weil93cea5b2009-12-23 12:21:51 -08004089fail_nomsg:
Yan, Zheng81c5a142019-01-01 16:28:33 +08004090 ceph_pagelist_release(recon_state.pagelist);
Sage Weil93cea5b2009-12-23 12:21:51 -08004091fail_nopagelist:
Sage Weil9abf82b2010-05-10 21:58:38 -07004092 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
Sage Weil9abf82b2010-05-10 21:58:38 -07004093 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07004094}
4095
4096
4097/*
4098 * compare old and new mdsmaps, kicking requests
4099 * and closing out old connections as necessary
4100 *
4101 * called under mdsc->mutex.
4102 */
4103static void check_new_map(struct ceph_mds_client *mdsc,
4104 struct ceph_mdsmap *newmap,
4105 struct ceph_mdsmap *oldmap)
4106{
4107 int i;
4108 int oldstate, newstate;
4109 struct ceph_mds_session *s;
4110
4111 dout("check_new_map new %u old %u\n",
4112 newmap->m_epoch, oldmap->m_epoch);
4113
Xiubo Lib38c9eb2019-12-04 06:57:39 -05004114 for (i = 0; i < oldmap->possible_max_rank && i < mdsc->max_sessions; i++) {
Markus Elfringd37b1d92017-08-20 20:22:02 +02004115 if (!mdsc->sessions[i])
Sage Weil2f2dc052009-10-06 11:31:09 -07004116 continue;
4117 s = mdsc->sessions[i];
4118 oldstate = ceph_mdsmap_get_state(oldmap, i);
4119 newstate = ceph_mdsmap_get_state(newmap, i);
4120
Sage Weil0deb01c2010-06-17 14:19:01 -07004121 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07004122 i, ceph_mds_state_name(oldstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07004123 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
Sage Weil2f2dc052009-10-06 11:31:09 -07004124 ceph_mds_state_name(newstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07004125 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
John Spraya687eca2014-09-19 13:51:08 +01004126 ceph_session_state_name(s->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07004127
Xiubo Lib38c9eb2019-12-04 06:57:39 -05004128 if (i >= newmap->possible_max_rank) {
Yan, Zheng6f0f5972019-06-10 15:45:09 +08004129 /* force close session for stopped mds */
Xiubo Li5b3248c2019-12-19 19:44:09 -05004130 ceph_get_mds_session(s);
Yan, Zheng6f0f5972019-06-10 15:45:09 +08004131 __unregister_session(mdsc, s);
4132 __wake_requests(mdsc, &s->s_waiting);
4133 mutex_unlock(&mdsc->mutex);
4134
4135 mutex_lock(&s->s_mutex);
4136 cleanup_session_requests(mdsc, s);
4137 remove_session_caps(s);
4138 mutex_unlock(&s->s_mutex);
4139
4140 ceph_put_mds_session(s);
4141
4142 mutex_lock(&mdsc->mutex);
4143 kick_requests(mdsc, i);
4144 continue;
4145 }
4146
4147 if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
Sage Weil2f2dc052009-10-06 11:31:09 -07004148 ceph_mdsmap_get_addr(newmap, i),
4149 sizeof(struct ceph_entity_addr))) {
Yan, Zheng6f0f5972019-06-10 15:45:09 +08004150 /* just close it */
4151 mutex_unlock(&mdsc->mutex);
4152 mutex_lock(&s->s_mutex);
4153 mutex_lock(&mdsc->mutex);
4154 ceph_con_close(&s->s_con);
4155 mutex_unlock(&s->s_mutex);
4156 s->s_state = CEPH_MDS_SESSION_RESTARTING;
Sage Weil2f2dc052009-10-06 11:31:09 -07004157 } else if (oldstate == newstate) {
4158 continue; /* nothing new with this mds */
4159 }
4160
4161 /*
4162 * send reconnect?
4163 */
4164 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
Sage Weil34b6c852010-05-10 16:31:25 -07004165 newstate >= CEPH_MDS_STATE_RECONNECT) {
4166 mutex_unlock(&mdsc->mutex);
4167 send_mds_reconnect(mdsc, s);
4168 mutex_lock(&mdsc->mutex);
4169 }
Sage Weil2f2dc052009-10-06 11:31:09 -07004170
4171 /*
Sage Weil29790f22010-03-18 14:45:05 -07004172 * kick request on any mds that has gone active.
Sage Weil2f2dc052009-10-06 11:31:09 -07004173 */
4174 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
4175 newstate >= CEPH_MDS_STATE_ACTIVE) {
Sage Weil29790f22010-03-18 14:45:05 -07004176 if (oldstate != CEPH_MDS_STATE_CREATING &&
4177 oldstate != CEPH_MDS_STATE_STARTING)
4178 pr_info("mds%d recovery completed\n", s->s_mds);
4179 kick_requests(mdsc, i);
Xiubo Liea8412b2020-05-20 03:51:19 -04004180 mutex_unlock(&mdsc->mutex);
Jeff Layton829ad4d2020-04-03 13:09:07 -04004181 mutex_lock(&s->s_mutex);
Xiubo Liea8412b2020-05-20 03:51:19 -04004182 mutex_lock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07004183 ceph_kick_flushing_caps(mdsc, s);
Jeff Layton829ad4d2020-04-03 13:09:07 -04004184 mutex_unlock(&s->s_mutex);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08004185 wake_up_session_caps(s, RECONNECT);
Sage Weil2f2dc052009-10-06 11:31:09 -07004186 }
4187 }
Sage Weilcb170a22010-06-21 13:38:35 -07004188
Xiubo Lib38c9eb2019-12-04 06:57:39 -05004189 for (i = 0; i < newmap->possible_max_rank && i < mdsc->max_sessions; i++) {
Sage Weilcb170a22010-06-21 13:38:35 -07004190 s = mdsc->sessions[i];
4191 if (!s)
4192 continue;
4193 if (!ceph_mdsmap_is_laggy(newmap, i))
4194 continue;
4195 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
4196 s->s_state == CEPH_MDS_SESSION_HUNG ||
4197 s->s_state == CEPH_MDS_SESSION_CLOSING) {
4198 dout(" connecting to export targets of laggy mds%d\n",
4199 i);
4200 __open_export_target_sessions(mdsc, s);
4201 }
4202 }
Sage Weil2f2dc052009-10-06 11:31:09 -07004203}
4204
4205
4206
4207/*
4208 * leases
4209 */
4210
4211/*
4212 * caller must hold session s_mutex, dentry->d_lock
4213 */
4214void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
4215{
4216 struct ceph_dentry_info *di = ceph_dentry(dentry);
4217
4218 ceph_put_mds_session(di->lease_session);
4219 di->lease_session = NULL;
4220}
4221
Sage Weil2600d2d2010-02-22 15:12:16 -08004222static void handle_lease(struct ceph_mds_client *mdsc,
4223 struct ceph_mds_session *session,
4224 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07004225{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004226 struct super_block *sb = mdsc->fsc->sb;
Sage Weil2f2dc052009-10-06 11:31:09 -07004227 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07004228 struct dentry *parent, *dentry;
4229 struct ceph_dentry_info *di;
Sage Weil2600d2d2010-02-22 15:12:16 -08004230 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07004231 struct ceph_mds_lease *h = msg->front.iov_base;
Sage Weil1e5ea232010-06-04 10:05:40 -07004232 u32 seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07004233 struct ceph_vino vino;
Sage Weil2f2dc052009-10-06 11:31:09 -07004234 struct qstr dname;
4235 int release = 0;
4236
Sage Weil2f2dc052009-10-06 11:31:09 -07004237 dout("handle_lease from mds%d\n", mds);
4238
4239 /* decode */
4240 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
4241 goto bad;
4242 vino.ino = le64_to_cpu(h->ino);
4243 vino.snap = CEPH_NOSNAP;
Sage Weil1e5ea232010-06-04 10:05:40 -07004244 seq = le32_to_cpu(h->seq);
Yan, Zheng0fcf6c02018-08-03 16:24:49 +08004245 dname.len = get_unaligned_le32(h + 1);
4246 if (msg->front.iov_len < sizeof(*h) + sizeof(u32) + dname.len)
Sage Weil2f2dc052009-10-06 11:31:09 -07004247 goto bad;
Yan, Zheng0fcf6c02018-08-03 16:24:49 +08004248 dname.name = (void *)(h + 1) + sizeof(u32);
Sage Weil2f2dc052009-10-06 11:31:09 -07004249
Sage Weil2f2dc052009-10-06 11:31:09 -07004250 /* lookup inode */
4251 inode = ceph_find_inode(sb, vino);
Sage Weil2f90b852011-07-26 11:28:25 -07004252 dout("handle_lease %s, ino %llx %p %.*s\n",
4253 ceph_lease_op_name(h->action), vino.ino, inode,
Sage Weil1e5ea232010-06-04 10:05:40 -07004254 dname.len, dname.name);
Yan, Zheng6cd3bca2014-09-17 07:45:12 +08004255
4256 mutex_lock(&session->s_mutex);
Jeff Layton62575e22020-10-12 09:39:06 -04004257 inc_session_sequence(session);
Yan, Zheng6cd3bca2014-09-17 07:45:12 +08004258
Markus Elfringd37b1d92017-08-20 20:22:02 +02004259 if (!inode) {
Sage Weil2f2dc052009-10-06 11:31:09 -07004260 dout("handle_lease no inode %llx\n", vino.ino);
4261 goto release;
4262 }
Sage Weil2f2dc052009-10-06 11:31:09 -07004263
4264 /* dentry */
4265 parent = d_find_alias(inode);
4266 if (!parent) {
4267 dout("no parent dentry on inode %p\n", inode);
4268 WARN_ON(1);
4269 goto release; /* hrm... */
4270 }
Linus Torvalds8387ff22016-06-10 07:51:30 -07004271 dname.hash = full_name_hash(parent, dname.name, dname.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07004272 dentry = d_lookup(parent, &dname);
4273 dput(parent);
4274 if (!dentry)
4275 goto release;
4276
4277 spin_lock(&dentry->d_lock);
4278 di = ceph_dentry(dentry);
4279 switch (h->action) {
4280 case CEPH_MDS_LEASE_REVOKE:
Sage Weil3d8eb7a2011-11-11 09:48:53 -08004281 if (di->lease_session == session) {
Sage Weil1e5ea232010-06-04 10:05:40 -07004282 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
4283 h->seq = cpu_to_le32(di->lease_seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07004284 __ceph_mdsc_drop_dentry_lease(dentry);
4285 }
4286 release = 1;
4287 break;
4288
4289 case CEPH_MDS_LEASE_RENEW:
Sage Weil3d8eb7a2011-11-11 09:48:53 -08004290 if (di->lease_session == session &&
Sage Weil2f2dc052009-10-06 11:31:09 -07004291 di->lease_gen == session->s_cap_gen &&
4292 di->lease_renew_from &&
4293 di->lease_renew_after == 0) {
4294 unsigned long duration =
Nicholas Mc Guire3563dbdd2015-02-06 06:52:17 -05004295 msecs_to_jiffies(le32_to_cpu(h->duration_ms));
Sage Weil2f2dc052009-10-06 11:31:09 -07004296
Sage Weil1e5ea232010-06-04 10:05:40 -07004297 di->lease_seq = seq;
Miklos Szeredi9b16f03c2016-06-22 16:35:04 +02004298 di->time = di->lease_renew_from + duration;
Sage Weil2f2dc052009-10-06 11:31:09 -07004299 di->lease_renew_after = di->lease_renew_from +
4300 (duration >> 1);
4301 di->lease_renew_from = 0;
4302 }
4303 break;
4304 }
4305 spin_unlock(&dentry->d_lock);
4306 dput(dentry);
4307
4308 if (!release)
4309 goto out;
4310
4311release:
4312 /* let's just reuse the same message */
4313 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
4314 ceph_msg_get(msg);
4315 ceph_con_send(&session->s_con, msg);
4316
4317out:
Sage Weil2f2dc052009-10-06 11:31:09 -07004318 mutex_unlock(&session->s_mutex);
Yan, Zheng3e1d0452019-05-18 20:39:55 +08004319 /* avoid calling iput_final() in mds dispatch threads */
4320 ceph_async_iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07004321 return;
4322
4323bad:
4324 pr_err("corrupt lease message\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08004325 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004326}
4327
4328void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
Sage Weil2f2dc052009-10-06 11:31:09 -07004329 struct dentry *dentry, char action,
4330 u32 seq)
4331{
4332 struct ceph_msg *msg;
4333 struct ceph_mds_lease *lease;
Yan, Zheng8f2a98ef2019-05-23 10:45:24 +08004334 struct inode *dir;
4335 int len = sizeof(*lease) + sizeof(u32) + NAME_MAX;
Sage Weil2f2dc052009-10-06 11:31:09 -07004336
Yan, Zheng8f2a98ef2019-05-23 10:45:24 +08004337 dout("lease_send_msg identry %p %s to mds%d\n",
4338 dentry, ceph_lease_op_name(action), session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07004339
Sage Weilb61c2762011-08-09 15:03:46 -07004340 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07004341 if (!msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07004342 return;
4343 lease = msg->front.iov_base;
4344 lease->action = action;
Sage Weil2f2dc052009-10-06 11:31:09 -07004345 lease->seq = cpu_to_le32(seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07004346
Yan, Zheng8f2a98ef2019-05-23 10:45:24 +08004347 spin_lock(&dentry->d_lock);
4348 dir = d_inode(dentry->d_parent);
4349 lease->ino = cpu_to_le64(ceph_ino(dir));
4350 lease->first = lease->last = cpu_to_le64(ceph_snap(dir));
4351
4352 put_unaligned_le32(dentry->d_name.len, lease + 1);
4353 memcpy((void *)(lease + 1) + 4,
4354 dentry->d_name.name, dentry->d_name.len);
4355 spin_unlock(&dentry->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07004356 /*
4357 * if this is a preemptive lease RELEASE, no need to
4358 * flush request stream, since the actual request will
4359 * soon follow.
4360 */
4361 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
4362
4363 ceph_con_send(&session->s_con, msg);
4364}
4365
4366/*
Yan, Zheng7aac4532018-03-13 19:52:20 +08004367 * lock unlock sessions, to wait ongoing session activities
Sage Weil2f2dc052009-10-06 11:31:09 -07004368 */
Yan, Zheng7aac4532018-03-13 19:52:20 +08004369static void lock_unlock_sessions(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07004370{
4371 int i;
4372
Sage Weil2f2dc052009-10-06 11:31:09 -07004373 mutex_lock(&mdsc->mutex);
4374 for (i = 0; i < mdsc->max_sessions; i++) {
4375 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
4376 if (!s)
4377 continue;
4378 mutex_unlock(&mdsc->mutex);
4379 mutex_lock(&s->s_mutex);
4380 mutex_unlock(&s->s_mutex);
4381 ceph_put_mds_session(s);
4382 mutex_lock(&mdsc->mutex);
4383 }
4384 mutex_unlock(&mdsc->mutex);
4385}
4386
Yan, Zheng131d7eb2019-07-25 20:16:47 +08004387static void maybe_recover_session(struct ceph_mds_client *mdsc)
4388{
4389 struct ceph_fs_client *fsc = mdsc->fsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07004390
Yan, Zheng131d7eb2019-07-25 20:16:47 +08004391 if (!ceph_test_mount_opt(fsc, CLEANRECOVER))
4392 return;
4393
4394 if (READ_ONCE(fsc->mount_state) != CEPH_MOUNT_MOUNTED)
4395 return;
4396
Ilya Dryomov0b98acd2020-09-14 13:39:19 +02004397 if (!READ_ONCE(fsc->blocklisted))
Yan, Zheng131d7eb2019-07-25 20:16:47 +08004398 return;
4399
Ilya Dryomov0b98acd2020-09-14 13:39:19 +02004400 pr_info("auto reconnect after blocklisted\n");
Yan, Zheng131d7eb2019-07-25 20:16:47 +08004401 ceph_force_reconnect(fsc->sb);
4402}
Sage Weil2f2dc052009-10-06 11:31:09 -07004403
Xiubo Li3e699bd2020-06-30 03:52:15 -04004404bool check_session_state(struct ceph_mds_session *s)
4405{
Jeff Layton62575e22020-10-12 09:39:06 -04004406 switch (s->s_state) {
4407 case CEPH_MDS_SESSION_OPEN:
4408 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
Xiubo Li3e699bd2020-06-30 03:52:15 -04004409 s->s_state = CEPH_MDS_SESSION_HUNG;
4410 pr_info("mds%d hung\n", s->s_mds);
4411 }
Jeff Layton62575e22020-10-12 09:39:06 -04004412 break;
4413 case CEPH_MDS_SESSION_CLOSING:
4414 /* Should never reach this when we're unmounting */
4415 WARN_ON_ONCE(true);
4416 fallthrough;
4417 case CEPH_MDS_SESSION_NEW:
4418 case CEPH_MDS_SESSION_RESTARTING:
4419 case CEPH_MDS_SESSION_CLOSED:
4420 case CEPH_MDS_SESSION_REJECTED:
Xiubo Li3e699bd2020-06-30 03:52:15 -04004421 return false;
Jeff Layton62575e22020-10-12 09:39:06 -04004422 }
Xiubo Li3e699bd2020-06-30 03:52:15 -04004423
4424 return true;
4425}
4426
Sage Weil2f2dc052009-10-06 11:31:09 -07004427/*
Jeff Layton62575e22020-10-12 09:39:06 -04004428 * If the sequence is incremented while we're waiting on a REQUEST_CLOSE reply,
4429 * then we need to retransmit that request.
4430 */
4431void inc_session_sequence(struct ceph_mds_session *s)
4432{
4433 lockdep_assert_held(&s->s_mutex);
4434
4435 s->s_seq++;
4436
4437 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
4438 int ret;
4439
4440 dout("resending session close request for mds%d\n", s->s_mds);
4441 ret = request_close_session(s);
4442 if (ret < 0)
4443 pr_err("unable to close session to mds%d: %d\n",
4444 s->s_mds, ret);
4445 }
4446}
4447
4448/*
Sage Weil2f2dc052009-10-06 11:31:09 -07004449 * delayed work -- periodically trim expired leases, renew caps with mds
4450 */
4451static void schedule_delayed(struct ceph_mds_client *mdsc)
4452{
4453 int delay = 5;
4454 unsigned hz = round_jiffies_relative(HZ * delay);
4455 schedule_delayed_work(&mdsc->delayed_work, hz);
4456}
4457
4458static void delayed_work(struct work_struct *work)
4459{
4460 int i;
4461 struct ceph_mds_client *mdsc =
4462 container_of(work, struct ceph_mds_client, delayed_work.work);
4463 int renew_interval;
4464 int renew_caps;
4465
4466 dout("mdsc delayed_work\n");
Yan, Zheng75c96272017-12-14 15:11:09 +08004467
Xiubo Lifa996772020-07-01 01:52:48 -04004468 if (mdsc->stopping)
4469 return;
4470
Sage Weil2f2dc052009-10-06 11:31:09 -07004471 mutex_lock(&mdsc->mutex);
4472 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
4473 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
4474 mdsc->last_renew_caps);
4475 if (renew_caps)
4476 mdsc->last_renew_caps = jiffies;
4477
4478 for (i = 0; i < mdsc->max_sessions; i++) {
4479 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
Markus Elfringd37b1d92017-08-20 20:22:02 +02004480 if (!s)
Sage Weil2f2dc052009-10-06 11:31:09 -07004481 continue;
Xiubo Li3e699bd2020-06-30 03:52:15 -04004482
4483 if (!check_session_state(s)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07004484 ceph_put_mds_session(s);
4485 continue;
4486 }
4487 mutex_unlock(&mdsc->mutex);
4488
4489 mutex_lock(&s->s_mutex);
4490 if (renew_caps)
4491 send_renew_caps(mdsc, s);
4492 else
4493 ceph_con_keepalive(&s->s_con);
Sage Weilaab53dd2010-03-17 16:30:21 -07004494 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
4495 s->s_state == CEPH_MDS_SESSION_HUNG)
Sage Weil3d7ded42010-06-09 16:47:10 -07004496 ceph_send_cap_releases(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07004497 mutex_unlock(&s->s_mutex);
4498 ceph_put_mds_session(s);
4499
4500 mutex_lock(&mdsc->mutex);
4501 }
4502 mutex_unlock(&mdsc->mutex);
4503
Yan, Zheng37c4efc2019-01-31 16:55:51 +08004504 ceph_check_delayed_caps(mdsc);
4505
4506 ceph_queue_cap_reclaim_work(mdsc);
4507
4508 ceph_trim_snapid_map(mdsc);
4509
Yan, Zheng131d7eb2019-07-25 20:16:47 +08004510 maybe_recover_session(mdsc);
4511
Sage Weil2f2dc052009-10-06 11:31:09 -07004512 schedule_delayed(mdsc);
4513}
4514
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004515int ceph_mdsc_init(struct ceph_fs_client *fsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07004516
Sage Weil2f2dc052009-10-06 11:31:09 -07004517{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004518 struct ceph_mds_client *mdsc;
Xiubo Lif9009ef2020-03-19 23:44:59 -04004519 int err;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004520
4521 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
4522 if (!mdsc)
4523 return -ENOMEM;
4524 mdsc->fsc = fsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07004525 mutex_init(&mdsc->mutex);
4526 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
Markus Elfringd37b1d92017-08-20 20:22:02 +02004527 if (!mdsc->mdsmap) {
Xiubo Lif9009ef2020-03-19 23:44:59 -04004528 err = -ENOMEM;
4529 goto err_mdsc;
majianpengfb3101b2013-06-25 14:48:19 +08004530 }
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08004531
Sage Weil2f2dc052009-10-06 11:31:09 -07004532 init_completion(&mdsc->safe_umount_waiters);
Sage Weilf3c60c52010-08-11 14:51:23 -07004533 init_waitqueue_head(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07004534 INIT_LIST_HEAD(&mdsc->waiting_for_map);
4535 mdsc->sessions = NULL;
Yan, Zheng86d8f672015-01-09 17:00:42 +08004536 atomic_set(&mdsc->num_sessions, 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07004537 mdsc->max_sessions = 0;
4538 mdsc->stopping = 0;
Luis Henriquesd557c482018-01-12 17:19:29 +00004539 atomic64_set(&mdsc->quotarealms_count, 0);
Luis Henriques0c44a8e2019-03-21 10:20:10 +00004540 mdsc->quotarealms_inodes = RB_ROOT;
4541 mutex_init(&mdsc->quotarealms_inodes_mutex);
Yan, Zhengaffbc192015-05-05 21:22:13 +08004542 mdsc->last_snap_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07004543 init_rwsem(&mdsc->snap_rwsem);
Sage Weila105f002010-02-15 14:37:55 -08004544 mdsc->snap_realms = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07004545 INIT_LIST_HEAD(&mdsc->snap_empty);
Yan, Zheng81c5a142019-01-01 16:28:33 +08004546 mdsc->num_snap_realms = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07004547 spin_lock_init(&mdsc->snap_empty_lock);
4548 mdsc->last_tid = 0;
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08004549 mdsc->oldest_tid = 0;
Sage Weil44ca18f2010-02-15 12:08:46 -08004550 mdsc->request_tree = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07004551 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
4552 mdsc->last_renew_caps = jiffies;
4553 INIT_LIST_HEAD(&mdsc->cap_delay_list);
Jeff Layton3a3430a2019-11-20 12:00:59 -05004554 INIT_LIST_HEAD(&mdsc->cap_wait_list);
Sage Weil2f2dc052009-10-06 11:31:09 -07004555 spin_lock_init(&mdsc->cap_delay_lock);
4556 INIT_LIST_HEAD(&mdsc->snap_flush_list);
4557 spin_lock_init(&mdsc->snap_flush_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08004558 mdsc->last_cap_flush_tid = 1;
Yan, Zhenge4500b52016-07-06 11:12:56 +08004559 INIT_LIST_HEAD(&mdsc->cap_flush_list);
Sage Weildb354052011-05-24 11:46:31 -07004560 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
Sage Weil2f2dc052009-10-06 11:31:09 -07004561 mdsc->num_cap_flushing = 0;
4562 spin_lock_init(&mdsc->cap_dirty_lock);
4563 init_waitqueue_head(&mdsc->cap_flushing_wq);
Yan, Zheng37c4efc2019-01-31 16:55:51 +08004564 INIT_WORK(&mdsc->cap_reclaim_work, ceph_cap_reclaim_work);
Yan, Zhengfe330322019-02-01 14:57:15 +08004565 atomic_set(&mdsc->cap_reclaim_pending, 0);
Xiubo Lif9009ef2020-03-19 23:44:59 -04004566 err = ceph_metric_init(&mdsc->metric);
4567 if (err)
4568 goto err_mdsmap;
Yan, Zheng37c4efc2019-01-31 16:55:51 +08004569
4570 spin_lock_init(&mdsc->dentry_list_lock);
4571 INIT_LIST_HEAD(&mdsc->dentry_leases);
4572 INIT_LIST_HEAD(&mdsc->dentry_dir_leases);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08004573
Yehuda Sadeh37151662010-06-17 16:16:12 -07004574 ceph_caps_init(mdsc);
Yan, Zhengfe330322019-02-01 14:57:15 +08004575 ceph_adjust_caps_max_min(mdsc, fsc->mount_options);
Yehuda Sadeh37151662010-06-17 16:16:12 -07004576
Yan, Zheng75c96272017-12-14 15:11:09 +08004577 spin_lock_init(&mdsc->snapid_map_lock);
4578 mdsc->snapid_map_tree = RB_ROOT;
4579 INIT_LIST_HEAD(&mdsc->snapid_map_lru);
4580
Yan, Zheng10183a62015-04-27 15:33:28 +08004581 init_rwsem(&mdsc->pool_perm_rwsem);
4582 mdsc->pool_perm_tree = RB_ROOT;
4583
Yan, Zhengdfeb84d2018-07-02 15:55:23 +08004584 strscpy(mdsc->nodename, utsname()->nodename,
4585 sizeof(mdsc->nodename));
Xiubo Lia7caa882020-07-23 15:32:25 +08004586
4587 fsc->mdsc = mdsc;
Sage Weil5f44f142009-11-18 14:52:18 -08004588 return 0;
Xiubo Lif9009ef2020-03-19 23:44:59 -04004589
4590err_mdsmap:
4591 kfree(mdsc->mdsmap);
4592err_mdsc:
4593 kfree(mdsc);
4594 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07004595}
4596
4597/*
4598 * Wait for safe replies on open mds requests. If we time out, drop
4599 * all requests from the tree to avoid dangling dentry refs.
4600 */
4601static void wait_requests(struct ceph_mds_client *mdsc)
4602{
Ilya Dryomova319bf52015-05-15 12:02:17 +03004603 struct ceph_options *opts = mdsc->fsc->client->options;
Sage Weil2f2dc052009-10-06 11:31:09 -07004604 struct ceph_mds_request *req;
Sage Weil2f2dc052009-10-06 11:31:09 -07004605
4606 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08004607 if (__get_oldest_req(mdsc)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07004608 mutex_unlock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08004609
Sage Weil2f2dc052009-10-06 11:31:09 -07004610 dout("wait_requests waiting for requests\n");
4611 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
Ilya Dryomova319bf52015-05-15 12:02:17 +03004612 ceph_timeout_jiffies(opts->mount_timeout));
Sage Weil2f2dc052009-10-06 11:31:09 -07004613
4614 /* tear down remaining requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08004615 mutex_lock(&mdsc->mutex);
4616 while ((req = __get_oldest_req(mdsc))) {
Sage Weil2f2dc052009-10-06 11:31:09 -07004617 dout("wait_requests timed out on tid %llu\n",
4618 req->r_tid);
Yan, Zheng428138c2019-06-14 10:55:05 +08004619 list_del_init(&req->r_wait);
Sage Weil44ca18f2010-02-15 12:08:46 -08004620 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07004621 }
4622 }
4623 mutex_unlock(&mdsc->mutex);
4624 dout("wait_requests done\n");
4625}
4626
4627/*
4628 * called before mount is ro, and before dentries are torn down.
4629 * (hmm, does this still race with new lookups?)
4630 */
4631void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
4632{
4633 dout("pre_umount\n");
4634 mdsc->stopping = 1;
4635
Yan, Zheng7aac4532018-03-13 19:52:20 +08004636 lock_unlock_sessions(mdsc);
Sage Weilafcdaea2009-10-14 14:27:38 -07004637 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07004638 wait_requests(mdsc);
Sage Weil17c688c2010-06-21 16:12:26 -07004639
4640 /*
4641 * wait for reply handlers to drop their request refs and
4642 * their inode/dcache refs
4643 */
4644 ceph_msgr_flush();
Luis Henriques0c44a8e2019-03-21 10:20:10 +00004645
4646 ceph_cleanup_quotarealms_inodes(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07004647}
4648
4649/*
4650 * wait for all write mds requests to flush.
4651 */
4652static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
4653{
Sage Weil80fc7312010-03-16 15:28:54 -07004654 struct ceph_mds_request *req = NULL, *nextreq;
Sage Weil44ca18f2010-02-15 12:08:46 -08004655 struct rb_node *n;
Sage Weil2f2dc052009-10-06 11:31:09 -07004656
4657 mutex_lock(&mdsc->mutex);
4658 dout("wait_unsafe_requests want %lld\n", want_tid);
Sage Weil80fc7312010-03-16 15:28:54 -07004659restart:
Sage Weil44ca18f2010-02-15 12:08:46 -08004660 req = __get_oldest_req(mdsc);
4661 while (req && req->r_tid <= want_tid) {
Sage Weil80fc7312010-03-16 15:28:54 -07004662 /* find next request */
4663 n = rb_next(&req->r_node);
4664 if (n)
4665 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
4666 else
4667 nextreq = NULL;
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08004668 if (req->r_op != CEPH_MDS_OP_SETFILELOCK &&
4669 (req->r_op & CEPH_MDS_OP_WRITE)) {
Sage Weil44ca18f2010-02-15 12:08:46 -08004670 /* write op */
4671 ceph_mdsc_get_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07004672 if (nextreq)
4673 ceph_mdsc_get_request(nextreq);
Sage Weil44ca18f2010-02-15 12:08:46 -08004674 mutex_unlock(&mdsc->mutex);
4675 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
4676 req->r_tid, want_tid);
4677 wait_for_completion(&req->r_safe_completion);
4678 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08004679 ceph_mdsc_put_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07004680 if (!nextreq)
4681 break; /* next dne before, so we're done! */
4682 if (RB_EMPTY_NODE(&nextreq->r_node)) {
4683 /* next request was removed from tree */
4684 ceph_mdsc_put_request(nextreq);
4685 goto restart;
4686 }
4687 ceph_mdsc_put_request(nextreq); /* won't go away */
Sage Weil44ca18f2010-02-15 12:08:46 -08004688 }
Sage Weil80fc7312010-03-16 15:28:54 -07004689 req = nextreq;
Sage Weil2f2dc052009-10-06 11:31:09 -07004690 }
4691 mutex_unlock(&mdsc->mutex);
4692 dout("wait_unsafe_requests done\n");
4693}
4694
4695void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
4696{
Yan, Zheng0e294382016-07-04 18:06:41 +08004697 u64 want_tid, want_flush;
Sage Weil2f2dc052009-10-06 11:31:09 -07004698
Jeff Layton50c91322020-09-25 07:55:39 -04004699 if (READ_ONCE(mdsc->fsc->mount_state) >= CEPH_MOUNT_SHUTDOWN)
Sage Weil56b7cf92010-05-03 15:22:00 -07004700 return;
4701
Sage Weil2f2dc052009-10-06 11:31:09 -07004702 dout("sync\n");
4703 mutex_lock(&mdsc->mutex);
4704 want_tid = mdsc->last_tid;
Sage Weil2f2dc052009-10-06 11:31:09 -07004705 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07004706
Sage Weilafcdaea2009-10-14 14:27:38 -07004707 ceph_flush_dirty_caps(mdsc);
Yan, Zhengd3383a82015-01-08 21:30:12 +08004708 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zheng8310b082015-06-09 17:20:12 +08004709 want_flush = mdsc->last_cap_flush_tid;
Yan, Zhengc8799fc2016-07-07 15:22:38 +08004710 if (!list_empty(&mdsc->cap_flush_list)) {
4711 struct ceph_cap_flush *cf =
4712 list_last_entry(&mdsc->cap_flush_list,
4713 struct ceph_cap_flush, g_list);
4714 cf->wake = true;
4715 }
Yan, Zhengd3383a82015-01-08 21:30:12 +08004716 spin_unlock(&mdsc->cap_dirty_lock);
4717
Yan, Zheng0e294382016-07-04 18:06:41 +08004718 dout("sync want tid %lld flush_seq %lld\n",
4719 want_tid, want_flush);
Sage Weil2f2dc052009-10-06 11:31:09 -07004720
4721 wait_unsafe_requests(mdsc, want_tid);
Yan, Zheng0e294382016-07-04 18:06:41 +08004722 wait_caps_flush(mdsc, want_flush);
Sage Weil2f2dc052009-10-06 11:31:09 -07004723}
4724
Sage Weilf3c60c52010-08-11 14:51:23 -07004725/*
4726 * true if all sessions are closed, or we force unmount
4727 */
Yan, Zhengfcff4152016-09-14 16:39:51 +08004728static bool done_closing_sessions(struct ceph_mds_client *mdsc, int skipped)
Sage Weilf3c60c52010-08-11 14:51:23 -07004729{
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01004730 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
Sage Weilf3c60c52010-08-11 14:51:23 -07004731 return true;
Yan, Zhengfcff4152016-09-14 16:39:51 +08004732 return atomic_read(&mdsc->num_sessions) <= skipped;
Sage Weilf3c60c52010-08-11 14:51:23 -07004733}
Sage Weil2f2dc052009-10-06 11:31:09 -07004734
4735/*
4736 * called after sb is ro.
4737 */
4738void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
4739{
Ilya Dryomova319bf52015-05-15 12:02:17 +03004740 struct ceph_options *opts = mdsc->fsc->client->options;
Sage Weil2f2dc052009-10-06 11:31:09 -07004741 struct ceph_mds_session *session;
4742 int i;
Yan, Zhengfcff4152016-09-14 16:39:51 +08004743 int skipped = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07004744
4745 dout("close_sessions\n");
4746
Sage Weil2f2dc052009-10-06 11:31:09 -07004747 /* close sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07004748 mutex_lock(&mdsc->mutex);
4749 for (i = 0; i < mdsc->max_sessions; i++) {
4750 session = __ceph_lookup_mds_session(mdsc, i);
4751 if (!session)
4752 continue;
Sage Weil2f2dc052009-10-06 11:31:09 -07004753 mutex_unlock(&mdsc->mutex);
Sage Weilf3c60c52010-08-11 14:51:23 -07004754 mutex_lock(&session->s_mutex);
Yan, Zhengfcff4152016-09-14 16:39:51 +08004755 if (__close_session(mdsc, session) <= 0)
4756 skipped++;
Sage Weilf3c60c52010-08-11 14:51:23 -07004757 mutex_unlock(&session->s_mutex);
4758 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07004759 mutex_lock(&mdsc->mutex);
4760 }
Sage Weilf3c60c52010-08-11 14:51:23 -07004761 mutex_unlock(&mdsc->mutex);
4762
4763 dout("waiting for sessions to close\n");
Yan, Zhengfcff4152016-09-14 16:39:51 +08004764 wait_event_timeout(mdsc->session_close_wq,
4765 done_closing_sessions(mdsc, skipped),
Ilya Dryomova319bf52015-05-15 12:02:17 +03004766 ceph_timeout_jiffies(opts->mount_timeout));
Sage Weil2f2dc052009-10-06 11:31:09 -07004767
4768 /* tear down remaining sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07004769 mutex_lock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07004770 for (i = 0; i < mdsc->max_sessions; i++) {
4771 if (mdsc->sessions[i]) {
Xiubo Li5b3248c2019-12-19 19:44:09 -05004772 session = ceph_get_mds_session(mdsc->sessions[i]);
Sage Weil2600d2d2010-02-22 15:12:16 -08004773 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07004774 mutex_unlock(&mdsc->mutex);
4775 mutex_lock(&session->s_mutex);
4776 remove_session_caps(session);
4777 mutex_unlock(&session->s_mutex);
4778 ceph_put_mds_session(session);
4779 mutex_lock(&mdsc->mutex);
4780 }
4781 }
Sage Weil2f2dc052009-10-06 11:31:09 -07004782 WARN_ON(!list_empty(&mdsc->cap_delay_list));
Sage Weil2f2dc052009-10-06 11:31:09 -07004783 mutex_unlock(&mdsc->mutex);
4784
Yan, Zheng75c96272017-12-14 15:11:09 +08004785 ceph_cleanup_snapid_map(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07004786 ceph_cleanup_empty_realms(mdsc);
4787
Yan, Zheng37c4efc2019-01-31 16:55:51 +08004788 cancel_work_sync(&mdsc->cap_reclaim_work);
Sage Weil2f2dc052009-10-06 11:31:09 -07004789 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
4790
4791 dout("stopped\n");
4792}
4793
Yan, Zheng48fec5d2015-07-01 16:27:46 +08004794void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc)
4795{
4796 struct ceph_mds_session *session;
4797 int mds;
4798
4799 dout("force umount\n");
4800
4801 mutex_lock(&mdsc->mutex);
4802 for (mds = 0; mds < mdsc->max_sessions; mds++) {
4803 session = __ceph_lookup_mds_session(mdsc, mds);
4804 if (!session)
4805 continue;
Yan, Zhengd468e722019-07-25 20:16:44 +08004806
4807 if (session->s_state == CEPH_MDS_SESSION_REJECTED)
4808 __unregister_session(mdsc, session);
4809 __wake_requests(mdsc, &session->s_waiting);
Yan, Zheng48fec5d2015-07-01 16:27:46 +08004810 mutex_unlock(&mdsc->mutex);
Yan, Zhengd468e722019-07-25 20:16:44 +08004811
Yan, Zheng48fec5d2015-07-01 16:27:46 +08004812 mutex_lock(&session->s_mutex);
4813 __close_session(mdsc, session);
4814 if (session->s_state == CEPH_MDS_SESSION_CLOSING) {
4815 cleanup_session_requests(mdsc, session);
4816 remove_session_caps(session);
4817 }
4818 mutex_unlock(&session->s_mutex);
4819 ceph_put_mds_session(session);
Yan, Zhengd468e722019-07-25 20:16:44 +08004820
Yan, Zheng48fec5d2015-07-01 16:27:46 +08004821 mutex_lock(&mdsc->mutex);
4822 kick_requests(mdsc, mds);
4823 }
4824 __wake_requests(mdsc, &mdsc->waiting_for_map);
4825 mutex_unlock(&mdsc->mutex);
4826}
4827
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004828static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07004829{
4830 dout("stop\n");
Xiubo Lifa996772020-07-01 01:52:48 -04004831 /*
4832 * Make sure the delayed work stopped before releasing
4833 * the resources.
4834 *
4835 * Because the cancel_delayed_work_sync() will only
4836 * guarantee that the work finishes executing. But the
4837 * delayed work will re-arm itself again after that.
4838 */
4839 flush_delayed_work(&mdsc->delayed_work);
4840
Sage Weil2f2dc052009-10-06 11:31:09 -07004841 if (mdsc->mdsmap)
4842 ceph_mdsmap_destroy(mdsc->mdsmap);
4843 kfree(mdsc->sessions);
Yehuda Sadeh37151662010-06-17 16:16:12 -07004844 ceph_caps_finalize(mdsc);
Yan, Zheng10183a62015-04-27 15:33:28 +08004845 ceph_pool_perm_destroy(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07004846}
4847
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004848void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
4849{
4850 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weilef550f62011-03-25 13:27:48 -07004851 dout("mdsc_destroy %p\n", mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07004852
Chengguang Xu50c55ae2018-03-14 13:47:33 +08004853 if (!mdsc)
4854 return;
4855
Sage Weilef550f62011-03-25 13:27:48 -07004856 /* flush out any connection work with references to us */
4857 ceph_msgr_flush();
4858
Yan, Zheng62a65f32017-06-22 16:26:34 +08004859 ceph_mdsc_stop(mdsc);
4860
Xiubo Lif9009ef2020-03-19 23:44:59 -04004861 ceph_metric_destroy(&mdsc->metric);
4862
Xiubo Li18f473b2020-07-16 10:05:57 -04004863 flush_delayed_work(&mdsc->metric.delayed_work);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004864 fsc->mdsc = NULL;
4865 kfree(mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07004866 dout("mdsc_destroy %p done\n", mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004867}
4868
Yan, Zheng430afba2016-07-08 11:25:38 +08004869void ceph_mdsc_handle_fsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
4870{
4871 struct ceph_fs_client *fsc = mdsc->fsc;
4872 const char *mds_namespace = fsc->mount_options->mds_namespace;
4873 void *p = msg->front.iov_base;
4874 void *end = p + msg->front.iov_len;
4875 u32 epoch;
Yan, Zheng430afba2016-07-08 11:25:38 +08004876 u32 num_fs;
4877 u32 mount_fscid = (u32)-1;
Yan, Zheng430afba2016-07-08 11:25:38 +08004878 int err = -EINVAL;
4879
4880 ceph_decode_need(&p, end, sizeof(u32), bad);
4881 epoch = ceph_decode_32(&p);
4882
4883 dout("handle_fsmap epoch %u\n", epoch);
4884
Jeff Layton06a1ad42020-09-29 19:32:19 -04004885 /* struct_v, struct_cv, map_len, epoch, legacy_client_fscid */
4886 ceph_decode_skip_n(&p, end, 2 + sizeof(u32) * 3, bad);
Yan, Zheng430afba2016-07-08 11:25:38 +08004887
Jeff Layton06a1ad42020-09-29 19:32:19 -04004888 ceph_decode_32_safe(&p, end, num_fs, bad);
Yan, Zheng430afba2016-07-08 11:25:38 +08004889 while (num_fs-- > 0) {
4890 void *info_p, *info_end;
4891 u32 info_len;
Yan, Zheng430afba2016-07-08 11:25:38 +08004892 u32 fscid, namelen;
4893
4894 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
Jeff Layton06a1ad42020-09-29 19:32:19 -04004895 p += 2; // info_v, info_cv
Yan, Zheng430afba2016-07-08 11:25:38 +08004896 info_len = ceph_decode_32(&p);
4897 ceph_decode_need(&p, end, info_len, bad);
4898 info_p = p;
4899 info_end = p + info_len;
4900 p = info_end;
4901
4902 ceph_decode_need(&info_p, info_end, sizeof(u32) * 2, bad);
4903 fscid = ceph_decode_32(&info_p);
4904 namelen = ceph_decode_32(&info_p);
4905 ceph_decode_need(&info_p, info_end, namelen, bad);
4906
4907 if (mds_namespace &&
4908 strlen(mds_namespace) == namelen &&
4909 !strncmp(mds_namespace, (char *)info_p, namelen)) {
4910 mount_fscid = fscid;
4911 break;
4912 }
4913 }
4914
4915 ceph_monc_got_map(&fsc->client->monc, CEPH_SUB_FSMAP, epoch);
4916 if (mount_fscid != (u32)-1) {
4917 fsc->client->monc.fs_cluster_id = mount_fscid;
4918 ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP,
4919 0, true);
4920 ceph_monc_renew_subs(&fsc->client->monc);
4921 } else {
4922 err = -ENOENT;
4923 goto err_out;
4924 }
4925 return;
Ilya Dryomov76bd6ec2017-10-16 10:32:50 +02004926
Yan, Zheng430afba2016-07-08 11:25:38 +08004927bad:
4928 pr_err("error decoding fsmap\n");
4929err_out:
4930 mutex_lock(&mdsc->mutex);
Ilya Dryomov76bd6ec2017-10-16 10:32:50 +02004931 mdsc->mdsmap_err = err;
Yan, Zheng430afba2016-07-08 11:25:38 +08004932 __wake_requests(mdsc, &mdsc->waiting_for_map);
4933 mutex_unlock(&mdsc->mutex);
Yan, Zheng430afba2016-07-08 11:25:38 +08004934}
Sage Weil2f2dc052009-10-06 11:31:09 -07004935
4936/*
4937 * handle mds map update.
4938 */
Yan, Zheng430afba2016-07-08 11:25:38 +08004939void ceph_mdsc_handle_mdsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07004940{
4941 u32 epoch;
4942 u32 maplen;
4943 void *p = msg->front.iov_base;
4944 void *end = p + msg->front.iov_len;
4945 struct ceph_mdsmap *newmap, *oldmap;
4946 struct ceph_fsid fsid;
4947 int err = -EINVAL;
4948
4949 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
4950 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004951 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
Sage Weil07433042009-11-18 16:50:41 -08004952 return;
Sage Weilc89136e2009-10-14 09:59:09 -07004953 epoch = ceph_decode_32(&p);
4954 maplen = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07004955 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
4956
4957 /* do we need it? */
Sage Weil2f2dc052009-10-06 11:31:09 -07004958 mutex_lock(&mdsc->mutex);
4959 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
4960 dout("handle_map epoch %u <= our %u\n",
4961 epoch, mdsc->mdsmap->m_epoch);
4962 mutex_unlock(&mdsc->mutex);
4963 return;
4964 }
4965
4966 newmap = ceph_mdsmap_decode(&p, end);
4967 if (IS_ERR(newmap)) {
4968 err = PTR_ERR(newmap);
4969 goto bad_unlock;
4970 }
4971
4972 /* swap into place */
4973 if (mdsc->mdsmap) {
4974 oldmap = mdsc->mdsmap;
4975 mdsc->mdsmap = newmap;
4976 check_new_map(mdsc, newmap, oldmap);
4977 ceph_mdsmap_destroy(oldmap);
4978 } else {
4979 mdsc->mdsmap = newmap; /* first mds map */
4980 }
Chengguang Xu719784b2018-07-19 22:15:24 +08004981 mdsc->fsc->max_file_size = min((loff_t)mdsc->mdsmap->m_max_file_size,
4982 MAX_LFS_FILESIZE);
Sage Weil2f2dc052009-10-06 11:31:09 -07004983
4984 __wake_requests(mdsc, &mdsc->waiting_for_map);
Ilya Dryomov82dcaba2016-01-19 16:19:06 +01004985 ceph_monc_got_map(&mdsc->fsc->client->monc, CEPH_SUB_MDSMAP,
4986 mdsc->mdsmap->m_epoch);
Sage Weil2f2dc052009-10-06 11:31:09 -07004987
4988 mutex_unlock(&mdsc->mutex);
4989 schedule_delayed(mdsc);
4990 return;
4991
4992bad_unlock:
4993 mutex_unlock(&mdsc->mutex);
4994bad:
4995 pr_err("error decoding mdsmap %d\n", err);
4996 return;
4997}
4998
4999static struct ceph_connection *con_get(struct ceph_connection *con)
5000{
5001 struct ceph_mds_session *s = con->private;
5002
Xiubo Li5b3248c2019-12-19 19:44:09 -05005003 if (ceph_get_mds_session(s))
Sage Weil2f2dc052009-10-06 11:31:09 -07005004 return con;
Sage Weil2f2dc052009-10-06 11:31:09 -07005005 return NULL;
5006}
5007
5008static void con_put(struct ceph_connection *con)
5009{
5010 struct ceph_mds_session *s = con->private;
5011
Sage Weil2f2dc052009-10-06 11:31:09 -07005012 ceph_put_mds_session(s);
5013}
5014
5015/*
5016 * if the client is unresponsive for long enough, the mds will kill
5017 * the session entirely.
5018 */
5019static void peer_reset(struct ceph_connection *con)
5020{
5021 struct ceph_mds_session *s = con->private;
Sage Weil7e70f0e2010-03-18 13:59:12 -07005022 struct ceph_mds_client *mdsc = s->s_mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07005023
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07005024 pr_warn("mds%d closed our session\n", s->s_mds);
Sage Weil7e70f0e2010-03-18 13:59:12 -07005025 send_mds_reconnect(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07005026}
5027
5028static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
5029{
5030 struct ceph_mds_session *s = con->private;
5031 struct ceph_mds_client *mdsc = s->s_mdsc;
5032 int type = le16_to_cpu(msg->hdr.type);
5033
Sage Weil2600d2d2010-02-22 15:12:16 -08005034 mutex_lock(&mdsc->mutex);
5035 if (__verify_registered_session(mdsc, s) < 0) {
5036 mutex_unlock(&mdsc->mutex);
5037 goto out;
5038 }
5039 mutex_unlock(&mdsc->mutex);
5040
Sage Weil2f2dc052009-10-06 11:31:09 -07005041 switch (type) {
5042 case CEPH_MSG_MDS_MAP:
Yan, Zheng430afba2016-07-08 11:25:38 +08005043 ceph_mdsc_handle_mdsmap(mdsc, msg);
5044 break;
5045 case CEPH_MSG_FS_MAP_USER:
5046 ceph_mdsc_handle_fsmap(mdsc, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07005047 break;
5048 case CEPH_MSG_CLIENT_SESSION:
5049 handle_session(s, msg);
5050 break;
5051 case CEPH_MSG_CLIENT_REPLY:
5052 handle_reply(s, msg);
5053 break;
5054 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
Sage Weil2600d2d2010-02-22 15:12:16 -08005055 handle_forward(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07005056 break;
5057 case CEPH_MSG_CLIENT_CAPS:
5058 ceph_handle_caps(s, msg);
5059 break;
5060 case CEPH_MSG_CLIENT_SNAP:
Sage Weil2600d2d2010-02-22 15:12:16 -08005061 ceph_handle_snap(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07005062 break;
5063 case CEPH_MSG_CLIENT_LEASE:
Sage Weil2600d2d2010-02-22 15:12:16 -08005064 handle_lease(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07005065 break;
Luis Henriquesfb18a572018-01-05 10:47:18 +00005066 case CEPH_MSG_CLIENT_QUOTA:
5067 ceph_handle_quota(mdsc, s, msg);
5068 break;
Sage Weil2f2dc052009-10-06 11:31:09 -07005069
5070 default:
5071 pr_err("received unknown message type %d %s\n", type,
5072 ceph_msg_type_name(type));
5073 }
Sage Weil2600d2d2010-02-22 15:12:16 -08005074out:
Sage Weil2f2dc052009-10-06 11:31:09 -07005075 ceph_msg_put(msg);
5076}
5077
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005078/*
5079 * authentication
5080 */
Alex Eldera3530df2012-05-16 15:16:39 -05005081
5082/*
5083 * Note: returned pointer is the address of a structure that's
5084 * managed separately. Caller must *not* attempt to free it.
5085 */
5086static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05005087 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005088{
5089 struct ceph_mds_session *s = con->private;
5090 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07005091 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05005092 struct ceph_auth_handshake *auth = &s->s_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005093
Alex Elder74f18692012-05-16 15:16:39 -05005094 if (force_new && auth->authorizer) {
Ilya Dryomov6c1ea262016-04-11 19:34:49 +02005095 ceph_auth_destroy_authorizer(auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05005096 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005097 }
Sage Weil27859f92013-03-25 10:26:14 -07005098 if (!auth->authorizer) {
5099 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
5100 auth);
Sage Weil0bed9b52013-03-25 10:26:01 -07005101 if (ret)
5102 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07005103 } else {
5104 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
5105 auth);
Alex Eldera2556512012-05-16 15:16:39 -05005106 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05005107 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005108 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005109 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05005110
Alex Eldera3530df2012-05-16 15:16:39 -05005111 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005112}
5113
Ilya Dryomov6daca132018-07-27 19:18:34 +02005114static int add_authorizer_challenge(struct ceph_connection *con,
5115 void *challenge_buf, int challenge_buf_len)
5116{
5117 struct ceph_mds_session *s = con->private;
5118 struct ceph_mds_client *mdsc = s->s_mdsc;
5119 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
5120
5121 return ceph_auth_add_authorizer_challenge(ac, s->s_auth.authorizer,
5122 challenge_buf, challenge_buf_len);
5123}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005124
Ilya Dryomov0dde5842016-12-02 16:35:09 +01005125static int verify_authorizer_reply(struct ceph_connection *con)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005126{
5127 struct ceph_mds_session *s = con->private;
5128 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07005129 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005130
Ilya Dryomov0dde5842016-12-02 16:35:09 +01005131 return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005132}
5133
Sage Weil9bd2e6f2010-02-02 16:21:06 -08005134static int invalidate_authorizer(struct ceph_connection *con)
5135{
5136 struct ceph_mds_session *s = con->private;
5137 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07005138 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil9bd2e6f2010-02-02 16:21:06 -08005139
Sage Weil27859f92013-03-25 10:26:14 -07005140 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08005141
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07005142 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08005143}
5144
Alex Elder53ded492013-03-01 18:00:14 -06005145static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
5146 struct ceph_msg_header *hdr, int *skip)
5147{
5148 struct ceph_msg *msg;
5149 int type = (int) le16_to_cpu(hdr->type);
5150 int front_len = (int) le32_to_cpu(hdr->front_len);
5151
5152 if (con->in_msg)
5153 return con->in_msg;
5154
5155 *skip = 0;
5156 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
5157 if (!msg) {
5158 pr_err("unable to allocate msg type %d len %d\n",
5159 type, front_len);
5160 return NULL;
5161 }
Alex Elder53ded492013-03-01 18:00:14 -06005162
5163 return msg;
5164}
5165
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005166static int mds_sign_message(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08005167{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005168 struct ceph_mds_session *s = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08005169 struct ceph_auth_handshake *auth = &s->s_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005170
Yan, Zheng33d07332014-11-04 16:33:37 +08005171 return ceph_auth_sign_message(auth, msg);
5172}
5173
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005174static int mds_check_message_signature(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08005175{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005176 struct ceph_mds_session *s = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08005177 struct ceph_auth_handshake *auth = &s->s_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005178
Yan, Zheng33d07332014-11-04 16:33:37 +08005179 return ceph_auth_check_message_signature(auth, msg);
5180}
5181
Tobias Klauser9e327892010-05-20 10:40:19 +02005182static const struct ceph_connection_operations mds_con_ops = {
Sage Weil2f2dc052009-10-06 11:31:09 -07005183 .get = con_get,
5184 .put = con_put,
5185 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005186 .get_authorizer = get_authorizer,
Ilya Dryomov6daca132018-07-27 19:18:34 +02005187 .add_authorizer_challenge = add_authorizer_challenge,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08005188 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08005189 .invalidate_authorizer = invalidate_authorizer,
Sage Weil2f2dc052009-10-06 11:31:09 -07005190 .peer_reset = peer_reset,
Alex Elder53ded492013-03-01 18:00:14 -06005191 .alloc_msg = mds_alloc_msg,
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01005192 .sign_message = mds_sign_message,
5193 .check_message_signature = mds_check_message_signature,
Sage Weil2f2dc052009-10-06 11:31:09 -07005194};
5195
Sage Weil2f2dc052009-10-06 11:31:09 -07005196/* eof */