blob: 2da98b6cc064737264c5973b0a3f9239a73384be [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>
Sage Weil2f2dc052009-10-06 11:31:09 -070013
Sage Weil2f2dc052009-10-06 11:31:09 -070014#include "super.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070015#include "mds_client.h"
16
Sage Weil1fe60e52012-07-30 16:23:22 -070017#include <linux/ceph/ceph_features.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070018#include <linux/ceph/messenger.h>
19#include <linux/ceph/decode.h>
20#include <linux/ceph/pagelist.h>
21#include <linux/ceph/auth.h>
22#include <linux/ceph/debugfs.h>
Sage Weil2f2dc052009-10-06 11:31:09 -070023
Yan, Zheng81c5a142019-01-01 16:28:33 +080024#define RECONNECT_MAX_SIZE (INT_MAX - PAGE_SIZE)
25
Sage Weil2f2dc052009-10-06 11:31:09 -070026/*
27 * A cluster of MDS (metadata server) daemons is responsible for
28 * managing the file system namespace (the directory hierarchy and
29 * inodes) and for coordinating shared access to storage. Metadata is
30 * partitioning hierarchically across a number of servers, and that
31 * partition varies over time as the cluster adjusts the distribution
32 * in order to balance load.
33 *
34 * The MDS client is primarily responsible to managing synchronous
35 * metadata requests for operations like open, unlink, and so forth.
36 * If there is a MDS failure, we find out about it when we (possibly
37 * request and) receive a new MDS map, and can resubmit affected
38 * requests.
39 *
40 * For the most part, though, we take advantage of a lossless
41 * communications channel to the MDS, and do not need to worry about
42 * timing out or resubmitting requests.
43 *
44 * We maintain a stateful "session" with each MDS we interact with.
45 * Within each session, we sent periodic heartbeat messages to ensure
46 * any capabilities or leases we have been issues remain valid. If
47 * the session times out and goes stale, our leases and capabilities
48 * are no longer valid.
49 */
50
Sage Weil20cb34a2010-05-12 15:21:32 -070051struct ceph_reconnect_state {
Yan, Zheng81c5a142019-01-01 16:28:33 +080052 struct ceph_mds_session *session;
53 int nr_caps, nr_realms;
Sage Weil20cb34a2010-05-12 15:21:32 -070054 struct ceph_pagelist *pagelist;
Yan, Zheng121f22a2016-07-04 22:05:18 +080055 unsigned msg_version;
Yan, Zheng81c5a142019-01-01 16:28:33 +080056 bool allow_multi;
Sage Weil20cb34a2010-05-12 15:21:32 -070057};
58
Sage Weil2f2dc052009-10-06 11:31:09 -070059static void __wake_requests(struct ceph_mds_client *mdsc,
60 struct list_head *head);
Yan, Zhenge3ec8d62019-01-14 17:21:19 +080061static void ceph_cap_release_work(struct work_struct *work);
Yan, Zheng37c4efc2019-01-31 16:55:51 +080062static void ceph_cap_reclaim_work(struct work_struct *work);
Sage Weil2f2dc052009-10-06 11:31:09 -070063
Tobias Klauser9e327892010-05-20 10:40:19 +020064static const struct ceph_connection_operations mds_con_ops;
Sage Weil2f2dc052009-10-06 11:31:09 -070065
66
67/*
68 * mds reply parsing
69 */
70
Yan, Zhengb37fe1f2019-01-09 10:10:17 +080071static int parse_reply_info_quota(void **p, void *end,
72 struct ceph_mds_reply_info_in *info)
73{
74 u8 struct_v, struct_compat;
75 u32 struct_len;
76
77 ceph_decode_8_safe(p, end, struct_v, bad);
78 ceph_decode_8_safe(p, end, struct_compat, bad);
79 /* struct_v is expected to be >= 1. we only
80 * understand encoding with struct_compat == 1. */
81 if (!struct_v || struct_compat != 1)
82 goto bad;
83 ceph_decode_32_safe(p, end, struct_len, bad);
84 ceph_decode_need(p, end, struct_len, bad);
85 end = *p + struct_len;
86 ceph_decode_64_safe(p, end, info->max_bytes, bad);
87 ceph_decode_64_safe(p, end, info->max_files, bad);
88 *p = end;
89 return 0;
90bad:
91 return -EIO;
92}
93
Sage Weil2f2dc052009-10-06 11:31:09 -070094/*
95 * parse individual inode info
96 */
97static int parse_reply_info_in(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -080098 struct ceph_mds_reply_info_in *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +020099 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700100{
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800101 int err = 0;
102 u8 struct_v = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -0700103
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800104 if (features == (u64)-1) {
105 u32 struct_len;
106 u8 struct_compat;
107 ceph_decode_8_safe(p, end, struct_v, bad);
108 ceph_decode_8_safe(p, end, struct_compat, bad);
109 /* struct_v is expected to be >= 1. we only understand
110 * encoding with struct_compat == 1. */
111 if (!struct_v || struct_compat != 1)
112 goto bad;
113 ceph_decode_32_safe(p, end, struct_len, bad);
114 ceph_decode_need(p, end, struct_len, bad);
115 end = *p + struct_len;
116 }
117
118 ceph_decode_need(p, end, sizeof(struct ceph_mds_reply_inode), bad);
Sage Weil2f2dc052009-10-06 11:31:09 -0700119 info->in = *p;
120 *p += sizeof(struct ceph_mds_reply_inode) +
121 sizeof(*info->in->fragtree.splits) *
122 le32_to_cpu(info->in->fragtree.nsplits);
123
124 ceph_decode_32_safe(p, end, info->symlink_len, bad);
125 ceph_decode_need(p, end, info->symlink_len, bad);
126 info->symlink = *p;
127 *p += info->symlink_len;
128
Ilya Dryomov23c625c2018-11-08 14:55:21 +0100129 ceph_decode_copy_safe(p, end, &info->dir_layout,
130 sizeof(info->dir_layout), bad);
Sage Weil2f2dc052009-10-06 11:31:09 -0700131 ceph_decode_32_safe(p, end, info->xattr_len, bad);
132 ceph_decode_need(p, end, info->xattr_len, bad);
133 info->xattr_data = *p;
134 *p += info->xattr_len;
Yan, Zhengfb01d1f2014-11-14 21:29:55 +0800135
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800136 if (features == (u64)-1) {
137 /* inline data */
Yan, Zhengfb01d1f2014-11-14 21:29:55 +0800138 ceph_decode_64_safe(p, end, info->inline_version, bad);
139 ceph_decode_32_safe(p, end, info->inline_len, bad);
140 ceph_decode_need(p, end, info->inline_len, bad);
141 info->inline_data = *p;
142 *p += info->inline_len;
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800143 /* quota */
144 err = parse_reply_info_quota(p, end, info);
145 if (err < 0)
146 goto out_bad;
147 /* pool namespace */
Yan, Zheng5ea5c5e2016-02-14 18:06:41 +0800148 ceph_decode_32_safe(p, end, info->pool_ns_len, bad);
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800149 if (info->pool_ns_len > 0) {
150 ceph_decode_need(p, end, info->pool_ns_len, bad);
151 info->pool_ns_data = *p;
152 *p += info->pool_ns_len;
153 }
Jeff Layton245ce992019-05-29 11:19:42 -0400154
155 /* btime */
156 ceph_decode_need(p, end, sizeof(info->btime), bad);
157 ceph_decode_copy(p, &info->btime, sizeof(info->btime));
158
159 /* change attribute */
Jeff Laytona35ead32019-06-06 07:29:23 -0400160 ceph_decode_64_safe(p, end, info->change_attr, bad);
Yan, Zheng5ea5c5e2016-02-14 18:06:41 +0800161
Yan, Zheng08796872019-01-09 11:07:02 +0800162 /* dir pin */
163 if (struct_v >= 2) {
164 ceph_decode_32_safe(p, end, info->dir_pin, bad);
165 } else {
166 info->dir_pin = -ENODATA;
167 }
168
David Disseldorp193e7b32019-04-18 14:15:46 +0200169 /* snapshot birth time, remains zero for v<=2 */
170 if (struct_v >= 3) {
171 ceph_decode_need(p, end, sizeof(info->snap_btime), bad);
172 ceph_decode_copy(p, &info->snap_btime,
173 sizeof(info->snap_btime));
174 } else {
175 memset(&info->snap_btime, 0, sizeof(info->snap_btime));
176 }
177
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800178 *p = end;
179 } else {
180 if (features & CEPH_FEATURE_MDS_INLINE_DATA) {
181 ceph_decode_64_safe(p, end, info->inline_version, bad);
182 ceph_decode_32_safe(p, end, info->inline_len, bad);
183 ceph_decode_need(p, end, info->inline_len, bad);
184 info->inline_data = *p;
185 *p += info->inline_len;
186 } else
187 info->inline_version = CEPH_INLINE_NONE;
188
189 if (features & CEPH_FEATURE_MDS_QUOTA) {
190 err = parse_reply_info_quota(p, end, info);
191 if (err < 0)
192 goto out_bad;
193 } else {
194 info->max_bytes = 0;
195 info->max_files = 0;
196 }
197
198 info->pool_ns_len = 0;
199 info->pool_ns_data = NULL;
200 if (features & CEPH_FEATURE_FS_FILE_LAYOUT_V2) {
201 ceph_decode_32_safe(p, end, info->pool_ns_len, bad);
202 if (info->pool_ns_len > 0) {
203 ceph_decode_need(p, end, info->pool_ns_len, bad);
204 info->pool_ns_data = *p;
205 *p += info->pool_ns_len;
206 }
207 }
Yan, Zheng08796872019-01-09 11:07:02 +0800208
Jeff Layton245ce992019-05-29 11:19:42 -0400209 if (features & CEPH_FEATURE_FS_BTIME) {
210 ceph_decode_need(p, end, sizeof(info->btime), bad);
211 ceph_decode_copy(p, &info->btime, sizeof(info->btime));
Jeff Laytona35ead32019-06-06 07:29:23 -0400212 ceph_decode_64_safe(p, end, info->change_attr, bad);
Jeff Layton245ce992019-05-29 11:19:42 -0400213 }
214
Yan, Zheng08796872019-01-09 11:07:02 +0800215 info->dir_pin = -ENODATA;
David Disseldorp193e7b32019-04-18 14:15:46 +0200216 /* info->snap_btime remains zero */
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800217 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700218 return 0;
219bad:
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800220 err = -EIO;
221out_bad:
Sage Weil2f2dc052009-10-06 11:31:09 -0700222 return err;
223}
224
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800225static int parse_reply_info_dir(void **p, void *end,
226 struct ceph_mds_reply_dirfrag **dirfrag,
227 u64 features)
228{
229 if (features == (u64)-1) {
230 u8 struct_v, struct_compat;
231 u32 struct_len;
232 ceph_decode_8_safe(p, end, struct_v, bad);
233 ceph_decode_8_safe(p, end, struct_compat, bad);
234 /* struct_v is expected to be >= 1. we only understand
235 * encoding whose struct_compat == 1. */
236 if (!struct_v || struct_compat != 1)
237 goto bad;
238 ceph_decode_32_safe(p, end, struct_len, bad);
239 ceph_decode_need(p, end, struct_len, bad);
240 end = *p + struct_len;
241 }
242
243 ceph_decode_need(p, end, sizeof(**dirfrag), bad);
244 *dirfrag = *p;
245 *p += sizeof(**dirfrag) + sizeof(u32) * le32_to_cpu((*dirfrag)->ndist);
246 if (unlikely(*p > end))
247 goto bad;
248 if (features == (u64)-1)
249 *p = end;
250 return 0;
251bad:
252 return -EIO;
253}
254
255static int parse_reply_info_lease(void **p, void *end,
256 struct ceph_mds_reply_lease **lease,
257 u64 features)
258{
259 if (features == (u64)-1) {
260 u8 struct_v, struct_compat;
261 u32 struct_len;
262 ceph_decode_8_safe(p, end, struct_v, bad);
263 ceph_decode_8_safe(p, end, struct_compat, bad);
264 /* struct_v is expected to be >= 1. we only understand
265 * encoding whose struct_compat == 1. */
266 if (!struct_v || struct_compat != 1)
267 goto bad;
268 ceph_decode_32_safe(p, end, struct_len, bad);
269 ceph_decode_need(p, end, struct_len, bad);
270 end = *p + struct_len;
271 }
272
273 ceph_decode_need(p, end, sizeof(**lease), bad);
274 *lease = *p;
275 *p += sizeof(**lease);
276 if (features == (u64)-1)
277 *p = end;
278 return 0;
279bad:
280 return -EIO;
281}
282
Sage Weil2f2dc052009-10-06 11:31:09 -0700283/*
284 * parse a normal reply, which may contain a (dir+)dentry and/or a
285 * target inode.
286 */
287static int parse_reply_info_trace(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800288 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200289 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700290{
291 int err;
292
293 if (info->head->is_dentry) {
Sage Weil14303d22010-12-14 17:37:52 -0800294 err = parse_reply_info_in(p, end, &info->diri, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700295 if (err < 0)
296 goto out_bad;
297
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800298 err = parse_reply_info_dir(p, end, &info->dirfrag, features);
299 if (err < 0)
300 goto out_bad;
Sage Weil2f2dc052009-10-06 11:31:09 -0700301
302 ceph_decode_32_safe(p, end, info->dname_len, bad);
303 ceph_decode_need(p, end, info->dname_len, bad);
304 info->dname = *p;
305 *p += info->dname_len;
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800306
307 err = parse_reply_info_lease(p, end, &info->dlease, features);
308 if (err < 0)
309 goto out_bad;
Sage Weil2f2dc052009-10-06 11:31:09 -0700310 }
311
312 if (info->head->is_target) {
Sage Weil14303d22010-12-14 17:37:52 -0800313 err = parse_reply_info_in(p, end, &info->targeti, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700314 if (err < 0)
315 goto out_bad;
316 }
317
318 if (unlikely(*p != end))
319 goto bad;
320 return 0;
321
322bad:
323 err = -EIO;
324out_bad:
325 pr_err("problem parsing mds trace %d\n", err);
326 return err;
327}
328
329/*
330 * parse readdir results
331 */
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800332static int parse_reply_info_readdir(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800333 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200334 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700335{
336 u32 num, i = 0;
337 int err;
338
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800339 err = parse_reply_info_dir(p, end, &info->dir_dir, features);
340 if (err < 0)
341 goto out_bad;
Sage Weil2f2dc052009-10-06 11:31:09 -0700342
343 ceph_decode_need(p, end, sizeof(num) + 2, bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700344 num = ceph_decode_32(p);
Yan, Zheng956d39d2016-04-27 17:48:30 +0800345 {
346 u16 flags = ceph_decode_16(p);
347 info->dir_end = !!(flags & CEPH_READDIR_FRAG_END);
348 info->dir_complete = !!(flags & CEPH_READDIR_FRAG_COMPLETE);
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800349 info->hash_order = !!(flags & CEPH_READDIR_HASH_ORDER);
Yan, Zheng79162542017-04-05 12:54:05 -0400350 info->offset_hash = !!(flags & CEPH_READDIR_OFFSET_HASH);
Yan, Zheng956d39d2016-04-27 17:48:30 +0800351 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700352 if (num == 0)
353 goto done;
354
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800355 BUG_ON(!info->dir_entries);
356 if ((unsigned long)(info->dir_entries + num) >
357 (unsigned long)info->dir_entries + info->dir_buf_size) {
Yan, Zheng54008392014-03-29 13:41:15 +0800358 pr_err("dir contents are larger than expected\n");
359 WARN_ON(1);
360 goto bad;
361 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700362
Yan, Zheng54008392014-03-29 13:41:15 +0800363 info->dir_nr = num;
Sage Weil2f2dc052009-10-06 11:31:09 -0700364 while (num) {
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800365 struct ceph_mds_reply_dir_entry *rde = info->dir_entries + i;
Sage Weil2f2dc052009-10-06 11:31:09 -0700366 /* dentry */
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800367 ceph_decode_32_safe(p, end, rde->name_len, bad);
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800368 ceph_decode_need(p, end, rde->name_len, bad);
369 rde->name = *p;
370 *p += rde->name_len;
371 dout("parsed dir dname '%.*s'\n", rde->name_len, rde->name);
Sage Weil2f2dc052009-10-06 11:31:09 -0700372
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800373 /* dentry lease */
374 err = parse_reply_info_lease(p, end, &rde->lease, features);
375 if (err)
376 goto out_bad;
Sage Weil2f2dc052009-10-06 11:31:09 -0700377 /* inode */
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800378 err = parse_reply_info_in(p, end, &rde->inode, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700379 if (err < 0)
380 goto out_bad;
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800381 /* ceph_readdir_prepopulate() will update it */
382 rde->offset = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -0700383 i++;
384 num--;
385 }
386
387done:
Jeff Layton1d3f8722019-09-26 16:05:11 -0400388 /* Skip over any unrecognized fields */
389 *p = end;
Sage Weil2f2dc052009-10-06 11:31:09 -0700390 return 0;
391
392bad:
393 err = -EIO;
394out_bad:
395 pr_err("problem parsing dir contents %d\n", err);
396 return err;
397}
398
399/*
Herb Shiu25933ab2010-12-01 14:14:38 -0800400 * parse fcntl F_GETLK results
401 */
402static int parse_reply_info_filelock(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800403 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200404 u64 features)
Herb Shiu25933ab2010-12-01 14:14:38 -0800405{
406 if (*p + sizeof(*info->filelock_reply) > end)
407 goto bad;
408
409 info->filelock_reply = *p;
Herb Shiu25933ab2010-12-01 14:14:38 -0800410
Jeff Layton1d3f8722019-09-26 16:05:11 -0400411 /* Skip over any unrecognized fields */
412 *p = end;
Herb Shiu25933ab2010-12-01 14:14:38 -0800413 return 0;
Herb Shiu25933ab2010-12-01 14:14:38 -0800414bad:
415 return -EIO;
416}
417
418/*
Sam Lang6e8575f2012-12-28 09:56:46 -0800419 * parse create results
420 */
421static int parse_reply_info_create(void **p, void *end,
422 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200423 u64 features)
Sam Lang6e8575f2012-12-28 09:56:46 -0800424{
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800425 if (features == (u64)-1 ||
426 (features & CEPH_FEATURE_REPLY_CREATE_INODE)) {
Jeff Layton1d3f8722019-09-26 16:05:11 -0400427 /* Malformed reply? */
Sam Lang6e8575f2012-12-28 09:56:46 -0800428 if (*p == end) {
429 info->has_create_ino = false;
430 } else {
431 info->has_create_ino = true;
Jeff Layton1d3f8722019-09-26 16:05:11 -0400432 ceph_decode_64_safe(p, end, info->ino, bad);
Sam Lang6e8575f2012-12-28 09:56:46 -0800433 }
Jeff Layton1d3f8722019-09-26 16:05:11 -0400434 } else {
435 if (*p != end)
436 goto bad;
Sam Lang6e8575f2012-12-28 09:56:46 -0800437 }
438
Jeff Layton1d3f8722019-09-26 16:05:11 -0400439 /* Skip over any unrecognized fields */
440 *p = end;
Sam Lang6e8575f2012-12-28 09:56:46 -0800441 return 0;
Sam Lang6e8575f2012-12-28 09:56:46 -0800442bad:
443 return -EIO;
444}
445
446/*
Herb Shiu25933ab2010-12-01 14:14:38 -0800447 * parse extra results
448 */
449static int parse_reply_info_extra(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800450 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200451 u64 features)
Herb Shiu25933ab2010-12-01 14:14:38 -0800452{
Jeff Layton6df8c9d2017-01-12 14:42:41 -0500453 u32 op = le32_to_cpu(info->head->op);
454
455 if (op == CEPH_MDS_OP_GETFILELOCK)
Sage Weil14303d22010-12-14 17:37:52 -0800456 return parse_reply_info_filelock(p, end, info, features);
Jeff Layton6df8c9d2017-01-12 14:42:41 -0500457 else if (op == CEPH_MDS_OP_READDIR || op == CEPH_MDS_OP_LSSNAP)
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800458 return parse_reply_info_readdir(p, end, info, features);
Jeff Layton6df8c9d2017-01-12 14:42:41 -0500459 else if (op == CEPH_MDS_OP_CREATE)
Sam Lang6e8575f2012-12-28 09:56:46 -0800460 return parse_reply_info_create(p, end, info, features);
461 else
462 return -EIO;
Herb Shiu25933ab2010-12-01 14:14:38 -0800463}
464
465/*
Sage Weil2f2dc052009-10-06 11:31:09 -0700466 * parse entire mds reply
467 */
468static int parse_reply_info(struct ceph_msg *msg,
Sage Weil14303d22010-12-14 17:37:52 -0800469 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200470 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700471{
472 void *p, *end;
473 u32 len;
474 int err;
475
476 info->head = msg->front.iov_base;
477 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
478 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
479
480 /* trace */
481 ceph_decode_32_safe(&p, end, len, bad);
482 if (len > 0) {
Xi Wang32852a82012-01-14 22:20:59 -0500483 ceph_decode_need(&p, end, len, bad);
Sage Weil14303d22010-12-14 17:37:52 -0800484 err = parse_reply_info_trace(&p, p+len, info, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700485 if (err < 0)
486 goto out_bad;
487 }
488
Herb Shiu25933ab2010-12-01 14:14:38 -0800489 /* extra */
Sage Weil2f2dc052009-10-06 11:31:09 -0700490 ceph_decode_32_safe(&p, end, len, bad);
491 if (len > 0) {
Xi Wang32852a82012-01-14 22:20:59 -0500492 ceph_decode_need(&p, end, len, bad);
Sage Weil14303d22010-12-14 17:37:52 -0800493 err = parse_reply_info_extra(&p, p+len, info, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700494 if (err < 0)
495 goto out_bad;
496 }
497
498 /* snap blob */
499 ceph_decode_32_safe(&p, end, len, bad);
500 info->snapblob_len = len;
501 info->snapblob = p;
502 p += len;
503
504 if (p != end)
505 goto bad;
506 return 0;
507
508bad:
509 err = -EIO;
510out_bad:
511 pr_err("mds parse_reply err %d\n", err);
512 return err;
513}
514
515static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
516{
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800517 if (!info->dir_entries)
Yan, Zheng54008392014-03-29 13:41:15 +0800518 return;
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800519 free_pages((unsigned long)info->dir_entries, get_order(info->dir_buf_size));
Sage Weil2f2dc052009-10-06 11:31:09 -0700520}
521
522
523/*
524 * sessions
525 */
John Spraya687eca2014-09-19 13:51:08 +0100526const char *ceph_session_state_name(int s)
Sage Weil2f2dc052009-10-06 11:31:09 -0700527{
528 switch (s) {
529 case CEPH_MDS_SESSION_NEW: return "new";
530 case CEPH_MDS_SESSION_OPENING: return "opening";
531 case CEPH_MDS_SESSION_OPEN: return "open";
532 case CEPH_MDS_SESSION_HUNG: return "hung";
533 case CEPH_MDS_SESSION_CLOSING: return "closing";
Xiubo Li4d681c22019-12-05 22:35:51 -0500534 case CEPH_MDS_SESSION_CLOSED: return "closed";
Sage Weil44ca18f2010-02-15 12:08:46 -0800535 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
Sage Weil2f2dc052009-10-06 11:31:09 -0700536 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
Yan, Zhengfcff4152016-09-14 16:39:51 +0800537 case CEPH_MDS_SESSION_REJECTED: return "rejected";
Sage Weil2f2dc052009-10-06 11:31:09 -0700538 default: return "???";
539 }
540}
541
Xiubo Li5b3248c2019-12-19 19:44:09 -0500542struct ceph_mds_session *ceph_get_mds_session(struct ceph_mds_session *s)
Sage Weil2f2dc052009-10-06 11:31:09 -0700543{
Elena Reshetova3997c012017-03-03 11:15:06 +0200544 if (refcount_inc_not_zero(&s->s_ref)) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700545 dout("mdsc get_session %p %d -> %d\n", s,
Elena Reshetova3997c012017-03-03 11:15:06 +0200546 refcount_read(&s->s_ref)-1, refcount_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -0700547 return s;
548 } else {
Chengguang Xu4c069a52018-01-30 16:29:17 +0800549 dout("mdsc get_session %p 0 -- FAIL\n", s);
Sage Weil2f2dc052009-10-06 11:31:09 -0700550 return NULL;
551 }
552}
553
554void ceph_put_mds_session(struct ceph_mds_session *s)
555{
556 dout("mdsc put_session %p %d -> %d\n", s,
Elena Reshetova3997c012017-03-03 11:15:06 +0200557 refcount_read(&s->s_ref), refcount_read(&s->s_ref)-1);
558 if (refcount_dec_and_test(&s->s_ref)) {
Alex Elder6c4a1912012-05-16 15:16:38 -0500559 if (s->s_auth.authorizer)
Ilya Dryomov6c1ea262016-04-11 19:34:49 +0200560 ceph_auth_destroy_authorizer(s->s_auth.authorizer);
Sage Weil2f2dc052009-10-06 11:31:09 -0700561 kfree(s);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800562 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700563}
564
565/*
566 * called under mdsc->mutex
567 */
568struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
569 int mds)
570{
Markus Elfringd37b1d92017-08-20 20:22:02 +0200571 if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
Sage Weil2f2dc052009-10-06 11:31:09 -0700572 return NULL;
Xiubo Li5b3248c2019-12-19 19:44:09 -0500573 return ceph_get_mds_session(mdsc->sessions[mds]);
Sage Weil2f2dc052009-10-06 11:31:09 -0700574}
575
576static bool __have_session(struct ceph_mds_client *mdsc, int mds)
577{
Chengguang Xu98cfda82018-03-13 10:43:45 +0800578 if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
Sage Weil2f2dc052009-10-06 11:31:09 -0700579 return false;
Chengguang Xu98cfda82018-03-13 10:43:45 +0800580 else
581 return true;
Sage Weil2f2dc052009-10-06 11:31:09 -0700582}
583
Sage Weil2600d2d2010-02-22 15:12:16 -0800584static int __verify_registered_session(struct ceph_mds_client *mdsc,
585 struct ceph_mds_session *s)
586{
587 if (s->s_mds >= mdsc->max_sessions ||
588 mdsc->sessions[s->s_mds] != s)
589 return -ENOENT;
590 return 0;
591}
592
Sage Weil2f2dc052009-10-06 11:31:09 -0700593/*
594 * create+register a new session for given mds.
595 * called under mdsc->mutex.
596 */
597static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
598 int mds)
599{
600 struct ceph_mds_session *s;
601
Xiubo Lib38c9eb2019-12-04 06:57:39 -0500602 if (mds >= mdsc->mdsmap->possible_max_rank)
Nathaniel Yazdanic338c072013-08-04 21:04:30 -0700603 return ERR_PTR(-EINVAL);
604
Sage Weil2f2dc052009-10-06 11:31:09 -0700605 s = kzalloc(sizeof(*s), GFP_NOFS);
Dan Carpenter4736b002010-03-20 15:30:16 +0300606 if (!s)
607 return ERR_PTR(-ENOMEM);
Chengguang Xu47474d02018-03-13 23:01:07 +0800608
609 if (mds >= mdsc->max_sessions) {
610 int newmax = 1 << get_count_order(mds + 1);
611 struct ceph_mds_session **sa;
612
613 dout("%s: realloc to %d\n", __func__, newmax);
614 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
615 if (!sa)
616 goto fail_realloc;
617 if (mdsc->sessions) {
618 memcpy(sa, mdsc->sessions,
619 mdsc->max_sessions * sizeof(void *));
620 kfree(mdsc->sessions);
621 }
622 mdsc->sessions = sa;
623 mdsc->max_sessions = newmax;
624 }
625
626 dout("%s: mds%d\n", __func__, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -0700627 s->s_mdsc = mdsc;
628 s->s_mds = mds;
629 s->s_state = CEPH_MDS_SESSION_NEW;
630 s->s_ttl = 0;
631 s->s_seq = 0;
632 mutex_init(&s->s_mutex);
633
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700634 ceph_con_init(&s->s_con, s, &mds_con_ops, &mdsc->fsc->client->msgr);
Sage Weil2f2dc052009-10-06 11:31:09 -0700635
Alex Elderd8fb02a2012-01-12 17:48:10 -0800636 spin_lock_init(&s->s_gen_ttl_lock);
Yan, Zheng1e9c2eb2019-01-28 20:43:55 +0800637 s->s_cap_gen = 1;
Alex Elder1ce208a2012-01-12 17:48:11 -0800638 s->s_cap_ttl = jiffies - 1;
Alex Elderd8fb02a2012-01-12 17:48:10 -0800639
640 spin_lock_init(&s->s_cap_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700641 s->s_renew_requested = 0;
642 s->s_renew_seq = 0;
643 INIT_LIST_HEAD(&s->s_caps);
644 s->s_nr_caps = 0;
Elena Reshetova3997c012017-03-03 11:15:06 +0200645 refcount_set(&s->s_ref, 1);
Sage Weil2f2dc052009-10-06 11:31:09 -0700646 INIT_LIST_HEAD(&s->s_waiting);
647 INIT_LIST_HEAD(&s->s_unsafe);
648 s->s_num_cap_releases = 0;
Yan, Zheng99a9c272013-09-22 11:08:14 +0800649 s->s_cap_reconnect = 0;
Sage Weil7c1332b2010-02-16 11:39:45 -0800650 s->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700651 INIT_LIST_HEAD(&s->s_cap_releases);
Yan, Zhenge3ec8d62019-01-14 17:21:19 +0800652 INIT_WORK(&s->s_cap_release_work, ceph_cap_release_work);
653
Sage Weil2f2dc052009-10-06 11:31:09 -0700654 INIT_LIST_HEAD(&s->s_cap_flushing);
Sage Weil2f2dc052009-10-06 11:31:09 -0700655
Sage Weil2f2dc052009-10-06 11:31:09 -0700656 mdsc->sessions[mds] = s;
Yan, Zheng86d8f672015-01-09 17:00:42 +0800657 atomic_inc(&mdsc->num_sessions);
Elena Reshetova3997c012017-03-03 11:15:06 +0200658 refcount_inc(&s->s_ref); /* one ref to sessions[], one to caller */
Sage Weil42ce56e2009-11-18 11:22:36 -0800659
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700660 ceph_con_open(&s->s_con, CEPH_ENTITY_TYPE_MDS, mds,
661 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
Sage Weil42ce56e2009-11-18 11:22:36 -0800662
Sage Weil2f2dc052009-10-06 11:31:09 -0700663 return s;
Sage Weil42ce56e2009-11-18 11:22:36 -0800664
665fail_realloc:
666 kfree(s);
667 return ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -0700668}
669
670/*
671 * called under mdsc->mutex
672 */
Sage Weil2600d2d2010-02-22 15:12:16 -0800673static void __unregister_session(struct ceph_mds_client *mdsc,
Sage Weil42ce56e2009-11-18 11:22:36 -0800674 struct ceph_mds_session *s)
Sage Weil2f2dc052009-10-06 11:31:09 -0700675{
Sage Weil2600d2d2010-02-22 15:12:16 -0800676 dout("__unregister_session mds%d %p\n", s->s_mds, s);
677 BUG_ON(mdsc->sessions[s->s_mds] != s);
Sage Weil42ce56e2009-11-18 11:22:36 -0800678 mdsc->sessions[s->s_mds] = NULL;
679 ceph_con_close(&s->s_con);
680 ceph_put_mds_session(s);
Yan, Zheng86d8f672015-01-09 17:00:42 +0800681 atomic_dec(&mdsc->num_sessions);
Sage Weil2f2dc052009-10-06 11:31:09 -0700682}
683
684/*
685 * drop session refs in request.
686 *
687 * should be last request ref, or hold mdsc->mutex
688 */
689static void put_request_session(struct ceph_mds_request *req)
690{
691 if (req->r_session) {
692 ceph_put_mds_session(req->r_session);
693 req->r_session = NULL;
694 }
695}
696
Sage Weil153c8e62009-12-07 12:31:09 -0800697void ceph_mdsc_release_request(struct kref *kref)
Sage Weil2f2dc052009-10-06 11:31:09 -0700698{
Sage Weil153c8e62009-12-07 12:31:09 -0800699 struct ceph_mds_request *req = container_of(kref,
700 struct ceph_mds_request,
701 r_kref);
Yan, Zheng54008392014-03-29 13:41:15 +0800702 destroy_reply_info(&req->r_reply_info);
Sage Weil153c8e62009-12-07 12:31:09 -0800703 if (req->r_request)
704 ceph_msg_put(req->r_request);
Yan, Zheng54008392014-03-29 13:41:15 +0800705 if (req->r_reply)
Sage Weil153c8e62009-12-07 12:31:09 -0800706 ceph_msg_put(req->r_reply);
Sage Weil153c8e62009-12-07 12:31:09 -0800707 if (req->r_inode) {
Sage Weil41b02e12011-07-26 11:31:14 -0700708 ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
Yan, Zheng3e1d0452019-05-18 20:39:55 +0800709 /* avoid calling iput_final() in mds dispatch threads */
710 ceph_async_iput(req->r_inode);
Sage Weil153c8e62009-12-07 12:31:09 -0800711 }
Jeff Layton9c1c2b32019-04-03 13:16:01 -0400712 if (req->r_parent) {
Jeff Layton3dd69aa2017-01-31 10:28:26 -0500713 ceph_put_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
Jeff Layton9c1c2b32019-04-03 13:16:01 -0400714 ceph_async_iput(req->r_parent);
715 }
Yan, Zheng3e1d0452019-05-18 20:39:55 +0800716 ceph_async_iput(req->r_target_inode);
Sage Weil153c8e62009-12-07 12:31:09 -0800717 if (req->r_dentry)
718 dput(req->r_dentry);
Sage Weil844d87c2013-02-05 13:40:09 -0800719 if (req->r_old_dentry)
720 dput(req->r_old_dentry);
721 if (req->r_old_dentry_dir) {
Sage Weil41b02e12011-07-26 11:31:14 -0700722 /*
723 * track (and drop pins for) r_old_dentry_dir
724 * separately, since r_old_dentry's d_parent may have
725 * changed between the dir mutex being dropped and
726 * this request being freed.
727 */
728 ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir),
729 CEPH_CAP_PIN);
Yan, Zheng3e1d0452019-05-18 20:39:55 +0800730 ceph_async_iput(req->r_old_dentry_dir);
Sage Weil153c8e62009-12-07 12:31:09 -0800731 }
732 kfree(req->r_path1);
733 kfree(req->r_path2);
Yan, Zheng25e6bae2014-09-16 19:15:28 +0800734 if (req->r_pagelist)
735 ceph_pagelist_release(req->r_pagelist);
Sage Weil153c8e62009-12-07 12:31:09 -0800736 put_request_session(req);
Yehuda Sadeh37151662010-06-17 16:16:12 -0700737 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
Yan, Zheng428138c2019-06-14 10:55:05 +0800738 WARN_ON_ONCE(!list_empty(&req->r_wait));
Jeff Layton058daab2020-02-17 18:38:37 -0500739 kmem_cache_free(ceph_mds_request_cachep, req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700740}
741
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200742DEFINE_RB_FUNCS(request, struct ceph_mds_request, r_tid, r_node)
743
Sage Weil2f2dc052009-10-06 11:31:09 -0700744/*
745 * lookup session, bump ref if found.
746 *
747 * called under mdsc->mutex.
748 */
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200749static struct ceph_mds_request *
750lookup_get_request(struct ceph_mds_client *mdsc, u64 tid)
Sage Weil2f2dc052009-10-06 11:31:09 -0700751{
752 struct ceph_mds_request *req;
Sage Weil44ca18f2010-02-15 12:08:46 -0800753
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200754 req = lookup_request(&mdsc->request_tree, tid);
755 if (req)
756 ceph_mdsc_get_request(req);
Sage Weil44ca18f2010-02-15 12:08:46 -0800757
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200758 return req;
Sage Weil2f2dc052009-10-06 11:31:09 -0700759}
760
761/*
762 * Register an in-flight request, and assign a tid. Link to directory
763 * are modifying (if any).
764 *
765 * Called under mdsc->mutex.
766 */
767static void __register_request(struct ceph_mds_client *mdsc,
768 struct ceph_mds_request *req,
769 struct inode *dir)
770{
Zhi Zhange30ee582018-01-24 21:24:33 +0800771 int ret = 0;
772
Sage Weil2f2dc052009-10-06 11:31:09 -0700773 req->r_tid = ++mdsc->last_tid;
Zhi Zhange30ee582018-01-24 21:24:33 +0800774 if (req->r_num_caps) {
775 ret = ceph_reserve_caps(mdsc, &req->r_caps_reservation,
776 req->r_num_caps);
777 if (ret < 0) {
778 pr_err("__register_request %p "
779 "failed to reserve caps: %d\n", req, ret);
780 /* set req->r_err to fail early from __do_request */
781 req->r_err = ret;
782 return;
783 }
784 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700785 dout("__register_request %p tid %lld\n", req, req->r_tid);
786 ceph_mdsc_get_request(req);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200787 insert_request(&mdsc->request_tree, req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700788
Sage Weilcb4276c2010-11-08 07:28:52 -0800789 req->r_uid = current_fsuid();
790 req->r_gid = current_fsgid();
791
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +0800792 if (mdsc->oldest_tid == 0 && req->r_op != CEPH_MDS_OP_SETFILELOCK)
793 mdsc->oldest_tid = req->r_tid;
794
Sage Weil2f2dc052009-10-06 11:31:09 -0700795 if (dir) {
Jeff Layton3db0a2f2019-04-04 08:05:38 -0400796 struct ceph_inode_info *ci = ceph_inode(dir);
797
Sage Weil3b663782011-05-18 16:12:12 -0700798 ihold(dir);
Sage Weil2f2dc052009-10-06 11:31:09 -0700799 req->r_unsafe_dir = dir;
Jeff Layton3db0a2f2019-04-04 08:05:38 -0400800 spin_lock(&ci->i_unsafe_lock);
801 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
802 spin_unlock(&ci->i_unsafe_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700803 }
804}
805
806static void __unregister_request(struct ceph_mds_client *mdsc,
807 struct ceph_mds_request *req)
808{
809 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +0800810
Jeff Laytondf963ea2017-02-14 10:09:40 -0500811 /* Never leave an unregistered request on an unsafe list! */
812 list_del_init(&req->r_unsafe_item);
813
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +0800814 if (req->r_tid == mdsc->oldest_tid) {
815 struct rb_node *p = rb_next(&req->r_node);
816 mdsc->oldest_tid = 0;
817 while (p) {
818 struct ceph_mds_request *next_req =
819 rb_entry(p, struct ceph_mds_request, r_node);
820 if (next_req->r_op != CEPH_MDS_OP_SETFILELOCK) {
821 mdsc->oldest_tid = next_req->r_tid;
822 break;
823 }
824 p = rb_next(p);
825 }
826 }
827
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200828 erase_request(&mdsc->request_tree, req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700829
Jeff Layton3db0a2f2019-04-04 08:05:38 -0400830 if (req->r_unsafe_dir) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700831 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
Sage Weil2f2dc052009-10-06 11:31:09 -0700832 spin_lock(&ci->i_unsafe_lock);
833 list_del_init(&req->r_unsafe_dir_item);
834 spin_unlock(&ci->i_unsafe_lock);
Yan, Zheng4c06ace2015-10-27 17:18:00 +0800835 }
Jeff Laytonbc2de10d2017-02-01 13:49:09 -0500836 if (req->r_target_inode &&
837 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Yan, Zheng68cd5b42015-10-27 18:36:06 +0800838 struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
839 spin_lock(&ci->i_unsafe_lock);
840 list_del_init(&req->r_unsafe_target_item);
841 spin_unlock(&ci->i_unsafe_lock);
842 }
Sage Weil3b663782011-05-18 16:12:12 -0700843
Yan, Zheng4c06ace2015-10-27 17:18:00 +0800844 if (req->r_unsafe_dir) {
Yan, Zheng3e1d0452019-05-18 20:39:55 +0800845 /* avoid calling iput_final() in mds dispatch threads */
846 ceph_async_iput(req->r_unsafe_dir);
Sage Weil3b663782011-05-18 16:12:12 -0700847 req->r_unsafe_dir = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700848 }
Sage Weil94aa8ae2010-03-28 21:22:50 -0700849
Yan, Zhengfc55d2c2013-10-31 09:10:47 +0800850 complete_all(&req->r_safe_completion);
851
Sage Weil94aa8ae2010-03-28 21:22:50 -0700852 ceph_mdsc_put_request(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700853}
854
855/*
Jeff Layton30c71232016-12-15 08:37:56 -0500856 * Walk back up the dentry tree until we hit a dentry representing a
857 * non-snapshot inode. We do this using the rcu_read_lock (which must be held
858 * when calling this) to ensure that the objects won't disappear while we're
859 * working with them. Once we hit a candidate dentry, we attempt to take a
860 * reference to it, and return that as the result.
861 */
Dan Carpenterf1075482017-02-23 13:39:59 +0300862static struct inode *get_nonsnap_parent(struct dentry *dentry)
863{
864 struct inode *inode = NULL;
Jeff Layton30c71232016-12-15 08:37:56 -0500865
866 while (dentry && !IS_ROOT(dentry)) {
867 inode = d_inode_rcu(dentry);
868 if (!inode || ceph_snap(inode) == CEPH_NOSNAP)
869 break;
870 dentry = dentry->d_parent;
871 }
872 if (inode)
873 inode = igrab(inode);
874 return inode;
875}
876
877/*
Sage Weil2f2dc052009-10-06 11:31:09 -0700878 * Choose mds to send request to next. If there is a hint set in the
879 * request (e.g., due to a prior forward hint from the mds), use that.
880 * Otherwise, consult frag tree and/or caps to identify the
881 * appropriate mds. If all else fails, choose randomly.
882 *
883 * Called under mdsc->mutex.
884 */
885static int __choose_mds(struct ceph_mds_client *mdsc,
Xiubo Lic4853e92019-12-09 07:47:15 -0500886 struct ceph_mds_request *req,
887 bool *random)
Sage Weil2f2dc052009-10-06 11:31:09 -0700888{
889 struct inode *inode;
890 struct ceph_inode_info *ci;
891 struct ceph_cap *cap;
892 int mode = req->r_direct_mode;
893 int mds = -1;
894 u32 hash = req->r_direct_hash;
Jeff Laytonbc2de10d2017-02-01 13:49:09 -0500895 bool is_hash = test_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags);
Sage Weil2f2dc052009-10-06 11:31:09 -0700896
Xiubo Lic4853e92019-12-09 07:47:15 -0500897 if (random)
898 *random = false;
899
Sage Weil2f2dc052009-10-06 11:31:09 -0700900 /*
901 * is there a specific mds we should try? ignore hint if we have
902 * no session and the mds is not up (active or recovering).
903 */
904 if (req->r_resend_mds >= 0 &&
905 (__have_session(mdsc, req->r_resend_mds) ||
906 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
Xiubo Li3c802092020-01-01 22:09:37 -0500907 dout("%s using resend_mds mds%d\n", __func__,
Sage Weil2f2dc052009-10-06 11:31:09 -0700908 req->r_resend_mds);
909 return req->r_resend_mds;
910 }
911
912 if (mode == USE_RANDOM_MDS)
913 goto random;
914
915 inode = NULL;
916 if (req->r_inode) {
Yan, Zheng5d37ca12017-07-26 12:48:08 +0800917 if (ceph_snap(req->r_inode) != CEPH_SNAPDIR) {
918 inode = req->r_inode;
919 ihold(inode);
920 } else {
Yan, Zheng38f340c2017-09-22 11:41:06 +0800921 /* req->r_dentry is non-null for LSSNAP request */
922 rcu_read_lock();
923 inode = get_nonsnap_parent(req->r_dentry);
924 rcu_read_unlock();
Xiubo Li3c802092020-01-01 22:09:37 -0500925 dout("%s using snapdir's parent %p\n", __func__, inode);
Yan, Zheng5d37ca12017-07-26 12:48:08 +0800926 }
Yan, Zheng38f340c2017-09-22 11:41:06 +0800927 } else if (req->r_dentry) {
Sage Weild79698d2011-07-26 11:31:26 -0700928 /* ignore race with rename; old or new d_parent is okay */
Jeff Layton30c71232016-12-15 08:37:56 -0500929 struct dentry *parent;
930 struct inode *dir;
Sage Weileb6bb1c2010-08-16 09:21:27 -0700931
Jeff Layton30c71232016-12-15 08:37:56 -0500932 rcu_read_lock();
Yan, Zheng41883ba2019-05-23 10:22:55 +0800933 parent = READ_ONCE(req->r_dentry->d_parent);
Jeff Layton3dd69aa2017-01-31 10:28:26 -0500934 dir = req->r_parent ? : d_inode_rcu(parent);
Jeff Layton30c71232016-12-15 08:37:56 -0500935
936 if (!dir || dir->i_sb != mdsc->fsc->sb) {
937 /* not this fs or parent went negative */
David Howells2b0143b2015-03-17 22:25:59 +0000938 inode = d_inode(req->r_dentry);
Jeff Layton30c71232016-12-15 08:37:56 -0500939 if (inode)
940 ihold(inode);
Sage Weileb6bb1c2010-08-16 09:21:27 -0700941 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
942 /* direct snapped/virtual snapdir requests
943 * based on parent dir inode */
Jeff Layton30c71232016-12-15 08:37:56 -0500944 inode = get_nonsnap_parent(parent);
Xiubo Li3c802092020-01-01 22:09:37 -0500945 dout("%s using nonsnap parent %p\n", __func__, inode);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800946 } else {
Sage Weileb6bb1c2010-08-16 09:21:27 -0700947 /* dentry target */
David Howells2b0143b2015-03-17 22:25:59 +0000948 inode = d_inode(req->r_dentry);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800949 if (!inode || mode == USE_AUTH_MDS) {
950 /* dir + name */
Jeff Layton30c71232016-12-15 08:37:56 -0500951 inode = igrab(dir);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800952 hash = ceph_dentry_hash(dir, req->r_dentry);
953 is_hash = true;
Jeff Layton30c71232016-12-15 08:37:56 -0500954 } else {
955 ihold(inode);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800956 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700957 }
Jeff Layton30c71232016-12-15 08:37:56 -0500958 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -0700959 }
Sage Weileb6bb1c2010-08-16 09:21:27 -0700960
Xiubo Li3c802092020-01-01 22:09:37 -0500961 dout("%s %p is_hash=%d (0x%x) mode %d\n", __func__, inode, (int)is_hash,
962 hash, mode);
Sage Weil2f2dc052009-10-06 11:31:09 -0700963 if (!inode)
964 goto random;
965 ci = ceph_inode(inode);
966
967 if (is_hash && S_ISDIR(inode->i_mode)) {
968 struct ceph_inode_frag frag;
969 int found;
970
971 ceph_choose_frag(ci, hash, &frag, &found);
972 if (found) {
973 if (mode == USE_ANY_MDS && frag.ndist > 0) {
974 u8 r;
975
976 /* choose a random replica */
977 get_random_bytes(&r, 1);
978 r %= frag.ndist;
979 mds = frag.dist[r];
Xiubo Li3c802092020-01-01 22:09:37 -0500980 dout("%s %p %llx.%llx frag %u mds%d (%d/%d)\n",
981 __func__, inode, ceph_vinop(inode),
982 frag.frag, mds, (int)r, frag.ndist);
Sage Weild66bbd42011-01-21 21:16:46 -0800983 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
Xiubo Li5d476482019-11-26 07:24:22 -0500984 CEPH_MDS_STATE_ACTIVE &&
985 !ceph_mdsmap_is_laggy(mdsc->mdsmap, mds))
Jeff Layton30c71232016-12-15 08:37:56 -0500986 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -0700987 }
988
989 /* since this file/dir wasn't known to be
990 * replicated, then we want to look for the
991 * authoritative mds. */
Sage Weil2f2dc052009-10-06 11:31:09 -0700992 if (frag.mds >= 0) {
993 /* choose auth mds */
994 mds = frag.mds;
Xiubo Li3c802092020-01-01 22:09:37 -0500995 dout("%s %p %llx.%llx frag %u mds%d (auth)\n",
996 __func__, inode, ceph_vinop(inode),
997 frag.frag, mds);
Sage Weild66bbd42011-01-21 21:16:46 -0800998 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
Xiubo Li5d476482019-11-26 07:24:22 -0500999 CEPH_MDS_STATE_ACTIVE) {
1000 if (mode == USE_ANY_MDS &&
1001 !ceph_mdsmap_is_laggy(mdsc->mdsmap,
1002 mds))
1003 goto out;
1004 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001005 }
Xiubo Li5d476482019-11-26 07:24:22 -05001006 mode = USE_AUTH_MDS;
Sage Weil2f2dc052009-10-06 11:31:09 -07001007 }
1008 }
1009
Sage Weilbe655592011-11-30 09:47:09 -08001010 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001011 cap = NULL;
1012 if (mode == USE_AUTH_MDS)
1013 cap = ci->i_auth_cap;
1014 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
1015 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
1016 if (!cap) {
Sage Weilbe655592011-11-30 09:47:09 -08001017 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng3e1d0452019-05-18 20:39:55 +08001018 ceph_async_iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07001019 goto random;
1020 }
1021 mds = cap->session->s_mds;
Xiubo Li3c802092020-01-01 22:09:37 -05001022 dout("%s %p %llx.%llx mds%d (%scap %p)\n", __func__,
Sage Weil2f2dc052009-10-06 11:31:09 -07001023 inode, ceph_vinop(inode), mds,
1024 cap == ci->i_auth_cap ? "auth " : "", cap);
Sage Weilbe655592011-11-30 09:47:09 -08001025 spin_unlock(&ci->i_ceph_lock);
Jeff Layton30c71232016-12-15 08:37:56 -05001026out:
Yan, Zheng3e1d0452019-05-18 20:39:55 +08001027 /* avoid calling iput_final() while holding mdsc->mutex or
1028 * in mds dispatch threads */
1029 ceph_async_iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07001030 return mds;
1031
1032random:
Xiubo Lic4853e92019-12-09 07:47:15 -05001033 if (random)
1034 *random = true;
1035
Sage Weil2f2dc052009-10-06 11:31:09 -07001036 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
Xiubo Li3c802092020-01-01 22:09:37 -05001037 dout("%s chose random mds%d\n", __func__, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07001038 return mds;
1039}
1040
1041
1042/*
1043 * session messages
1044 */
1045static struct ceph_msg *create_session_msg(u32 op, u64 seq)
1046{
1047 struct ceph_msg *msg;
1048 struct ceph_mds_session_head *h;
1049
Sage Weilb61c2762011-08-09 15:03:46 -07001050 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
1051 false);
Sage Weila79832f2010-04-01 16:06:19 -07001052 if (!msg) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001053 pr_err("create_session_msg ENOMEM creating msg\n");
Sage Weila79832f2010-04-01 16:06:19 -07001054 return NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001055 }
1056 h = msg->front.iov_base;
1057 h->op = cpu_to_le32(op);
1058 h->seq = cpu_to_le64(seq);
John Spraydbd0c8b2014-09-09 19:26:01 +01001059
1060 return msg;
1061}
1062
Xiubo Li9ba1e222020-01-08 05:17:31 -05001063static const unsigned char feature_bits[] = CEPHFS_FEATURES_CLIENT_SUPPORTED;
1064#define FEATURE_BYTES(c) (DIV_ROUND_UP((size_t)feature_bits[c - 1] + 1, 64) * 8)
Yan, Zheng342ce182018-05-11 18:47:29 +08001065static void encode_supported_features(void **p, void *end)
1066{
Xiubo Li9ba1e222020-01-08 05:17:31 -05001067 static const size_t count = ARRAY_SIZE(feature_bits);
Yan, Zheng342ce182018-05-11 18:47:29 +08001068
1069 if (count > 0) {
1070 size_t i;
Xiubo Li9ba1e222020-01-08 05:17:31 -05001071 size_t size = FEATURE_BYTES(count);
Yan, Zheng342ce182018-05-11 18:47:29 +08001072
1073 BUG_ON(*p + 4 + size > end);
1074 ceph_encode_32(p, size);
1075 memset(*p, 0, size);
1076 for (i = 0; i < count; i++)
Xiubo Li9ba1e222020-01-08 05:17:31 -05001077 ((unsigned char*)(*p))[i / 8] |= BIT(feature_bits[i] % 8);
Yan, Zheng342ce182018-05-11 18:47:29 +08001078 *p += size;
1079 } else {
1080 BUG_ON(*p + 4 > end);
1081 ceph_encode_32(p, 0);
1082 }
1083}
1084
John Spraydbd0c8b2014-09-09 19:26:01 +01001085/*
1086 * session message, specialization for CEPH_SESSION_REQUEST_OPEN
1087 * to include additional client metadata fields.
1088 */
1089static struct ceph_msg *create_session_open_msg(struct ceph_mds_client *mdsc, u64 seq)
1090{
1091 struct ceph_msg *msg;
1092 struct ceph_mds_session_head *h;
1093 int i = -1;
Yan, Zheng342ce182018-05-11 18:47:29 +08001094 int extra_bytes = 0;
John Spraydbd0c8b2014-09-09 19:26:01 +01001095 int metadata_key_count = 0;
1096 struct ceph_options *opt = mdsc->fsc->client->options;
Yan, Zheng3f384952016-04-21 11:09:55 +08001097 struct ceph_mount_options *fsopt = mdsc->fsc->mount_options;
Xiubo Li9ba1e222020-01-08 05:17:31 -05001098 size_t size, count;
Yan, Zheng342ce182018-05-11 18:47:29 +08001099 void *p, *end;
John Spraydbd0c8b2014-09-09 19:26:01 +01001100
Yan, Zhenga6a5ce42015-01-16 10:54:43 +08001101 const char* metadata[][2] = {
Yan, Zheng717e6f22017-09-11 12:10:08 +08001102 {"hostname", mdsc->nodename},
1103 {"kernel_version", init_utsname()->release},
Yan, Zheng3f384952016-04-21 11:09:55 +08001104 {"entity_id", opt->name ? : ""},
1105 {"root", fsopt->server_path ? : "/"},
John Spraydbd0c8b2014-09-09 19:26:01 +01001106 {NULL, NULL}
1107 };
1108
1109 /* Calculate serialized length of metadata */
Yan, Zheng342ce182018-05-11 18:47:29 +08001110 extra_bytes = 4; /* map length */
Markus Elfringd37b1d92017-08-20 20:22:02 +02001111 for (i = 0; metadata[i][0]; ++i) {
Yan, Zheng342ce182018-05-11 18:47:29 +08001112 extra_bytes += 8 + strlen(metadata[i][0]) +
John Spraydbd0c8b2014-09-09 19:26:01 +01001113 strlen(metadata[i][1]);
1114 metadata_key_count++;
1115 }
Xiubo Li9ba1e222020-01-08 05:17:31 -05001116
Yan, Zheng342ce182018-05-11 18:47:29 +08001117 /* supported feature */
Xiubo Li9ba1e222020-01-08 05:17:31 -05001118 size = 0;
1119 count = ARRAY_SIZE(feature_bits);
1120 if (count > 0)
1121 size = FEATURE_BYTES(count);
1122 extra_bytes += 4 + size;
John Spraydbd0c8b2014-09-09 19:26:01 +01001123
1124 /* Allocate the message */
Yan, Zheng342ce182018-05-11 18:47:29 +08001125 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + extra_bytes,
John Spraydbd0c8b2014-09-09 19:26:01 +01001126 GFP_NOFS, false);
1127 if (!msg) {
1128 pr_err("create_session_msg ENOMEM creating msg\n");
1129 return NULL;
1130 }
Yan, Zheng342ce182018-05-11 18:47:29 +08001131 p = msg->front.iov_base;
1132 end = p + msg->front.iov_len;
1133
1134 h = p;
John Spraydbd0c8b2014-09-09 19:26:01 +01001135 h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN);
1136 h->seq = cpu_to_le64(seq);
1137
1138 /*
1139 * Serialize client metadata into waiting buffer space, using
1140 * the format that userspace expects for map<string, string>
John Spray7cfa0312014-10-30 17:15:26 +00001141 *
Xiubo Li9ba1e222020-01-08 05:17:31 -05001142 * ClientSession messages with metadata are v3
John Spraydbd0c8b2014-09-09 19:26:01 +01001143 */
Yan, Zheng342ce182018-05-11 18:47:29 +08001144 msg->hdr.version = cpu_to_le16(3);
John Spray7cfa0312014-10-30 17:15:26 +00001145 msg->hdr.compat_version = cpu_to_le16(1);
John Spraydbd0c8b2014-09-09 19:26:01 +01001146
1147 /* The write pointer, following the session_head structure */
Yan, Zheng342ce182018-05-11 18:47:29 +08001148 p += sizeof(*h);
John Spraydbd0c8b2014-09-09 19:26:01 +01001149
1150 /* Number of entries in the map */
1151 ceph_encode_32(&p, metadata_key_count);
1152
1153 /* Two length-prefixed strings for each entry in the map */
Markus Elfringd37b1d92017-08-20 20:22:02 +02001154 for (i = 0; metadata[i][0]; ++i) {
John Spraydbd0c8b2014-09-09 19:26:01 +01001155 size_t const key_len = strlen(metadata[i][0]);
1156 size_t const val_len = strlen(metadata[i][1]);
1157
1158 ceph_encode_32(&p, key_len);
1159 memcpy(p, metadata[i][0], key_len);
1160 p += key_len;
1161 ceph_encode_32(&p, val_len);
1162 memcpy(p, metadata[i][1], val_len);
1163 p += val_len;
1164 }
1165
Yan, Zheng342ce182018-05-11 18:47:29 +08001166 encode_supported_features(&p, end);
1167 msg->front.iov_len = p - msg->front.iov_base;
1168 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1169
Sage Weil2f2dc052009-10-06 11:31:09 -07001170 return msg;
1171}
1172
1173/*
1174 * send session open request.
1175 *
1176 * called under mdsc->mutex
1177 */
1178static int __open_session(struct ceph_mds_client *mdsc,
1179 struct ceph_mds_session *session)
1180{
1181 struct ceph_msg *msg;
1182 int mstate;
1183 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07001184
1185 /* wait for mds to go active? */
1186 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
1187 dout("open_session to mds%d (%s)\n", mds,
1188 ceph_mds_state_name(mstate));
1189 session->s_state = CEPH_MDS_SESSION_OPENING;
1190 session->s_renew_requested = jiffies;
1191
1192 /* send connect message */
John Spraydbd0c8b2014-09-09 19:26:01 +01001193 msg = create_session_open_msg(mdsc, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001194 if (!msg)
1195 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -07001196 ceph_con_send(&session->s_con, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001197 return 0;
1198}
1199
1200/*
Sage Weiled0552a2010-06-21 13:38:25 -07001201 * open sessions for any export targets for the given mds
1202 *
1203 * called under mdsc->mutex
1204 */
Yan, Zheng5d72d132013-11-24 14:33:01 +08001205static struct ceph_mds_session *
1206__open_export_target_session(struct ceph_mds_client *mdsc, int target)
1207{
1208 struct ceph_mds_session *session;
1209
1210 session = __ceph_lookup_mds_session(mdsc, target);
1211 if (!session) {
1212 session = register_session(mdsc, target);
1213 if (IS_ERR(session))
1214 return session;
1215 }
1216 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1217 session->s_state == CEPH_MDS_SESSION_CLOSING)
1218 __open_session(mdsc, session);
1219
1220 return session;
1221}
1222
1223struct ceph_mds_session *
1224ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target)
1225{
1226 struct ceph_mds_session *session;
1227
1228 dout("open_export_target_session to mds%d\n", target);
1229
1230 mutex_lock(&mdsc->mutex);
1231 session = __open_export_target_session(mdsc, target);
1232 mutex_unlock(&mdsc->mutex);
1233
1234 return session;
1235}
1236
Sage Weiled0552a2010-06-21 13:38:25 -07001237static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
1238 struct ceph_mds_session *session)
1239{
1240 struct ceph_mds_info *mi;
1241 struct ceph_mds_session *ts;
1242 int i, mds = session->s_mds;
Sage Weiled0552a2010-06-21 13:38:25 -07001243
Xiubo Lib38c9eb2019-12-04 06:57:39 -05001244 if (mds >= mdsc->mdsmap->possible_max_rank)
Sage Weiled0552a2010-06-21 13:38:25 -07001245 return;
Yan, Zheng5d72d132013-11-24 14:33:01 +08001246
Sage Weiled0552a2010-06-21 13:38:25 -07001247 mi = &mdsc->mdsmap->m_info[mds];
1248 dout("open_export_target_sessions for mds%d (%d targets)\n",
1249 session->s_mds, mi->num_export_targets);
1250
1251 for (i = 0; i < mi->num_export_targets; i++) {
Yan, Zheng5d72d132013-11-24 14:33:01 +08001252 ts = __open_export_target_session(mdsc, mi->export_targets[i]);
1253 if (!IS_ERR(ts))
1254 ceph_put_mds_session(ts);
Sage Weiled0552a2010-06-21 13:38:25 -07001255 }
1256}
1257
Sage Weil154f42c2010-06-21 13:45:04 -07001258void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
1259 struct ceph_mds_session *session)
1260{
1261 mutex_lock(&mdsc->mutex);
1262 __open_export_target_sessions(mdsc, session);
1263 mutex_unlock(&mdsc->mutex);
1264}
1265
Sage Weiled0552a2010-06-21 13:38:25 -07001266/*
Sage Weil2f2dc052009-10-06 11:31:09 -07001267 * session caps
1268 */
1269
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001270static void detach_cap_releases(struct ceph_mds_session *session,
1271 struct list_head *target)
Sage Weil2f2dc052009-10-06 11:31:09 -07001272{
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001273 lockdep_assert_held(&session->s_cap_lock);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001274
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001275 list_splice_init(&session->s_cap_releases, target);
1276 session->s_num_cap_releases = 0;
1277 dout("dispose_cap_releases mds%d\n", session->s_mds);
1278}
1279
1280static void dispose_cap_releases(struct ceph_mds_client *mdsc,
1281 struct list_head *dispose)
1282{
1283 while (!list_empty(dispose)) {
Yan, Zheng745a8e32015-05-14 17:22:42 +08001284 struct ceph_cap *cap;
1285 /* zero out the in-progress message */
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001286 cap = list_first_entry(dispose, struct ceph_cap, session_caps);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001287 list_del(&cap->session_caps);
1288 ceph_put_cap(mdsc, cap);
1289 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001290}
1291
Yan, Zheng1c841a92015-03-24 20:15:36 +08001292static void cleanup_session_requests(struct ceph_mds_client *mdsc,
1293 struct ceph_mds_session *session)
1294{
1295 struct ceph_mds_request *req;
1296 struct rb_node *p;
Yan, Zhengf4b97862019-07-25 20:16:42 +08001297 struct ceph_inode_info *ci;
Yan, Zheng1c841a92015-03-24 20:15:36 +08001298
1299 dout("cleanup_session_requests mds%d\n", session->s_mds);
1300 mutex_lock(&mdsc->mutex);
1301 while (!list_empty(&session->s_unsafe)) {
1302 req = list_first_entry(&session->s_unsafe,
1303 struct ceph_mds_request, r_unsafe_item);
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001304 pr_warn_ratelimited(" dropping unsafe request %llu\n",
1305 req->r_tid);
Yan, Zhengf4b97862019-07-25 20:16:42 +08001306 if (req->r_target_inode) {
1307 /* dropping unsafe change of inode's attributes */
1308 ci = ceph_inode(req->r_target_inode);
1309 errseq_set(&ci->i_meta_err, -EIO);
1310 }
1311 if (req->r_unsafe_dir) {
1312 /* dropping unsafe directory operation */
1313 ci = ceph_inode(req->r_unsafe_dir);
1314 errseq_set(&ci->i_meta_err, -EIO);
1315 }
Yan, Zheng1c841a92015-03-24 20:15:36 +08001316 __unregister_request(mdsc, req);
1317 }
1318 /* zero r_attempts, so kick_requests() will re-send requests */
1319 p = rb_first(&mdsc->request_tree);
1320 while (p) {
1321 req = rb_entry(p, struct ceph_mds_request, r_node);
1322 p = rb_next(p);
1323 if (req->r_session &&
1324 req->r_session->s_mds == session->s_mds)
1325 req->r_attempts = 0;
1326 }
1327 mutex_unlock(&mdsc->mutex);
1328}
1329
Sage Weil2f2dc052009-10-06 11:31:09 -07001330/*
Sage Weilf818a732010-05-11 20:56:31 -07001331 * Helper to safely iterate over all caps associated with a session, with
1332 * special care taken to handle a racing __ceph_remove_cap().
Sage Weil2f2dc052009-10-06 11:31:09 -07001333 *
Sage Weilf818a732010-05-11 20:56:31 -07001334 * Caller must hold session s_mutex.
Sage Weil2f2dc052009-10-06 11:31:09 -07001335 */
Jeff Laytonf5d772692019-04-24 12:09:04 -04001336int ceph_iterate_session_caps(struct ceph_mds_session *session,
1337 int (*cb)(struct inode *, struct ceph_cap *,
1338 void *), void *arg)
Sage Weil2f2dc052009-10-06 11:31:09 -07001339{
Sage Weil7c1332b2010-02-16 11:39:45 -08001340 struct list_head *p;
1341 struct ceph_cap *cap;
1342 struct inode *inode, *last_inode = NULL;
1343 struct ceph_cap *old_cap = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001344 int ret;
1345
1346 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
1347 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001348 p = session->s_caps.next;
1349 while (p != &session->s_caps) {
1350 cap = list_entry(p, struct ceph_cap, session_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -07001351 inode = igrab(&cap->ci->vfs_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001352 if (!inode) {
1353 p = p->next;
Sage Weil2f2dc052009-10-06 11:31:09 -07001354 continue;
Sage Weil7c1332b2010-02-16 11:39:45 -08001355 }
1356 session->s_cap_iterator = cap;
Sage Weil2f2dc052009-10-06 11:31:09 -07001357 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001358
1359 if (last_inode) {
Yan, Zheng3e1d0452019-05-18 20:39:55 +08001360 /* avoid calling iput_final() while holding
1361 * s_mutex or in mds dispatch threads */
1362 ceph_async_iput(last_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001363 last_inode = NULL;
1364 }
1365 if (old_cap) {
Yehuda Sadeh37151662010-06-17 16:16:12 -07001366 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -08001367 old_cap = NULL;
1368 }
1369
Sage Weil2f2dc052009-10-06 11:31:09 -07001370 ret = cb(inode, cap, arg);
Sage Weil7c1332b2010-02-16 11:39:45 -08001371 last_inode = inode;
1372
Sage Weil2f2dc052009-10-06 11:31:09 -07001373 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001374 p = p->next;
Markus Elfringd37b1d92017-08-20 20:22:02 +02001375 if (!cap->ci) {
Sage Weil7c1332b2010-02-16 11:39:45 -08001376 dout("iterate_session_caps finishing cap %p removal\n",
1377 cap);
1378 BUG_ON(cap->session != session);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001379 cap->session = NULL;
Sage Weil7c1332b2010-02-16 11:39:45 -08001380 list_del_init(&cap->session_caps);
1381 session->s_nr_caps--;
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08001382 if (cap->queue_release)
1383 __ceph_queue_cap_release(session, cap);
1384 else
Yan, Zheng745a8e32015-05-14 17:22:42 +08001385 old_cap = cap; /* put_cap it w/o locks held */
Sage Weil7c1332b2010-02-16 11:39:45 -08001386 }
Sage Weil5dacf092009-12-21 20:40:34 -08001387 if (ret < 0)
1388 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07001389 }
Sage Weil5dacf092009-12-21 20:40:34 -08001390 ret = 0;
1391out:
Sage Weil7c1332b2010-02-16 11:39:45 -08001392 session->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001393 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001394
Yan, Zheng3e1d0452019-05-18 20:39:55 +08001395 ceph_async_iput(last_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001396 if (old_cap)
Yehuda Sadeh37151662010-06-17 16:16:12 -07001397 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -08001398
Sage Weil5dacf092009-12-21 20:40:34 -08001399 return ret;
Sage Weil2f2dc052009-10-06 11:31:09 -07001400}
1401
1402static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
Sage Weil6c99f252010-05-10 16:12:25 -07001403 void *arg)
Sage Weil2f2dc052009-10-06 11:31:09 -07001404{
Yan, Zheng6c93df52016-04-15 13:56:12 +08001405 struct ceph_fs_client *fsc = (struct ceph_fs_client *)arg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001406 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001407 LIST_HEAD(to_remove);
Yan, Zhengf4b97862019-07-25 20:16:42 +08001408 bool dirty_dropped = false;
Yan, Zheng6c93df52016-04-15 13:56:12 +08001409 bool invalidate = false;
Sage Weil6c99f252010-05-10 16:12:25 -07001410
Sage Weil2f2dc052009-10-06 11:31:09 -07001411 dout("removing cap %p, ci is %p, inode is %p\n",
1412 cap, ci, &ci->vfs_inode);
Sage Weilbe655592011-11-30 09:47:09 -08001413 spin_lock(&ci->i_ceph_lock);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001414 if (cap->mds_wanted | cap->issued)
1415 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
Yan, Zhenga096b092013-09-22 10:15:58 +08001416 __ceph_remove_cap(cap, false);
Yan, Zheng571ade32015-03-24 11:36:08 +08001417 if (!ci->i_auth_cap) {
Yan, Zheng553adfd2015-06-09 15:48:57 +08001418 struct ceph_cap_flush *cf;
Yan, Zheng6c93df52016-04-15 13:56:12 +08001419 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil6c99f252010-05-10 16:12:25 -07001420
Yan, Zhengd468e722019-07-25 20:16:44 +08001421 if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
1422 if (inode->i_data.nrpages > 0)
1423 invalidate = true;
1424 if (ci->i_wrbuffer_ref > 0)
1425 mapping_set_error(&inode->i_data, -EIO);
1426 }
Yan, Zheng6c93df52016-04-15 13:56:12 +08001427
Yan, Zhenge4500b52016-07-06 11:12:56 +08001428 while (!list_empty(&ci->i_cap_flush_list)) {
1429 cf = list_first_entry(&ci->i_cap_flush_list,
1430 struct ceph_cap_flush, i_list);
Wei Yongjun8cdcc072016-08-13 09:10:28 +00001431 list_move(&cf->i_list, &to_remove);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001432 }
1433
Sage Weil6c99f252010-05-10 16:12:25 -07001434 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zheng8310b082015-06-09 17:20:12 +08001435
Yan, Zhenge4500b52016-07-06 11:12:56 +08001436 list_for_each_entry(cf, &to_remove, i_list)
1437 list_del(&cf->g_list);
Yan, Zheng8310b082015-06-09 17:20:12 +08001438
Sage Weil6c99f252010-05-10 16:12:25 -07001439 if (!list_empty(&ci->i_dirty_item)) {
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001440 pr_warn_ratelimited(
1441 " dropping dirty %s state for %p %lld\n",
Sage Weil6c99f252010-05-10 16:12:25 -07001442 ceph_cap_string(ci->i_dirty_caps),
1443 inode, ceph_ino(inode));
1444 ci->i_dirty_caps = 0;
1445 list_del_init(&ci->i_dirty_item);
Yan, Zhengf4b97862019-07-25 20:16:42 +08001446 dirty_dropped = true;
Sage Weil6c99f252010-05-10 16:12:25 -07001447 }
1448 if (!list_empty(&ci->i_flushing_item)) {
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001449 pr_warn_ratelimited(
1450 " dropping dirty+flushing %s state for %p %lld\n",
Sage Weil6c99f252010-05-10 16:12:25 -07001451 ceph_cap_string(ci->i_flushing_caps),
1452 inode, ceph_ino(inode));
1453 ci->i_flushing_caps = 0;
1454 list_del_init(&ci->i_flushing_item);
1455 mdsc->num_cap_flushing--;
Yan, Zhengf4b97862019-07-25 20:16:42 +08001456 dirty_dropped = true;
Sage Weil6c99f252010-05-10 16:12:25 -07001457 }
Sage Weil6c99f252010-05-10 16:12:25 -07001458 spin_unlock(&mdsc->cap_dirty_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001459
Yan, Zhengf4b97862019-07-25 20:16:42 +08001460 if (dirty_dropped) {
1461 errseq_set(&ci->i_meta_err, -EIO);
1462
1463 if (ci->i_wrbuffer_ref_head == 0 &&
1464 ci->i_wr_ref == 0 &&
1465 ci->i_dirty_caps == 0 &&
1466 ci->i_flushing_caps == 0) {
1467 ceph_put_snap_context(ci->i_head_snapc);
1468 ci->i_head_snapc = NULL;
1469 }
1470 }
1471
Yan, Zhengb3f8d682017-09-11 10:58:55 +08001472 if (atomic_read(&ci->i_filelock_ref) > 0) {
1473 /* make further file lock syscall return -EIO */
1474 ci->i_ceph_flags |= CEPH_I_ERROR_FILELOCK;
1475 pr_warn_ratelimited(" dropping file locks for %p %lld\n",
1476 inode, ceph_ino(inode));
1477 }
1478
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001479 if (!ci->i_dirty_caps && ci->i_prealloc_cap_flush) {
Yan, Zhenge4500b52016-07-06 11:12:56 +08001480 list_add(&ci->i_prealloc_cap_flush->i_list, &to_remove);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001481 ci->i_prealloc_cap_flush = NULL;
1482 }
Sage Weil6c99f252010-05-10 16:12:25 -07001483 }
Sage Weilbe655592011-11-30 09:47:09 -08001484 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001485 while (!list_empty(&to_remove)) {
1486 struct ceph_cap_flush *cf;
1487 cf = list_first_entry(&to_remove,
Yan, Zhenge4500b52016-07-06 11:12:56 +08001488 struct ceph_cap_flush, i_list);
1489 list_del(&cf->i_list);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001490 ceph_free_cap_flush(cf);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001491 }
Yan, Zheng77310322016-04-08 15:27:16 +08001492
1493 wake_up_all(&ci->i_cap_wq);
Yan, Zheng6c93df52016-04-15 13:56:12 +08001494 if (invalidate)
1495 ceph_queue_invalidate(inode);
Yan, Zhengf4b97862019-07-25 20:16:42 +08001496 if (dirty_dropped)
Sage Weil6c99f252010-05-10 16:12:25 -07001497 iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07001498 return 0;
1499}
1500
1501/*
1502 * caller must hold session s_mutex
1503 */
1504static void remove_session_caps(struct ceph_mds_session *session)
1505{
Yan, Zheng6c93df52016-04-15 13:56:12 +08001506 struct ceph_fs_client *fsc = session->s_mdsc->fsc;
1507 struct super_block *sb = fsc->sb;
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001508 LIST_HEAD(dispose);
1509
Sage Weil2f2dc052009-10-06 11:31:09 -07001510 dout("remove_session_caps on %p\n", session);
Jeff Laytonf5d772692019-04-24 12:09:04 -04001511 ceph_iterate_session_caps(session, remove_session_caps_cb, fsc);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001512
Yan, Zhengc8799fc2016-07-07 15:22:38 +08001513 wake_up_all(&fsc->mdsc->cap_flushing_wq);
1514
Yan, Zheng6f60f882013-07-24 12:22:11 +08001515 spin_lock(&session->s_cap_lock);
1516 if (session->s_nr_caps > 0) {
Yan, Zheng6f60f882013-07-24 12:22:11 +08001517 struct inode *inode;
1518 struct ceph_cap *cap, *prev = NULL;
1519 struct ceph_vino vino;
1520 /*
1521 * iterate_session_caps() skips inodes that are being
1522 * deleted, we need to wait until deletions are complete.
1523 * __wait_on_freeing_inode() is designed for the job,
1524 * but it is not exported, so use lookup inode function
1525 * to access it.
1526 */
1527 while (!list_empty(&session->s_caps)) {
1528 cap = list_entry(session->s_caps.next,
1529 struct ceph_cap, session_caps);
1530 if (cap == prev)
1531 break;
1532 prev = cap;
1533 vino = cap->ci->i_vino;
1534 spin_unlock(&session->s_cap_lock);
1535
Yan, Zhenged284c42013-09-02 15:19:53 +08001536 inode = ceph_find_inode(sb, vino);
Yan, Zheng3e1d0452019-05-18 20:39:55 +08001537 /* avoid calling iput_final() while holding s_mutex */
1538 ceph_async_iput(inode);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001539
1540 spin_lock(&session->s_cap_lock);
1541 }
1542 }
Yan, Zheng745a8e32015-05-14 17:22:42 +08001543
1544 // drop cap expires and unlock s_cap_lock
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001545 detach_cap_releases(session, &dispose);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001546
Sage Weil2f2dc052009-10-06 11:31:09 -07001547 BUG_ON(session->s_nr_caps > 0);
Sage Weil6c99f252010-05-10 16:12:25 -07001548 BUG_ON(!list_empty(&session->s_cap_flushing));
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001549 spin_unlock(&session->s_cap_lock);
1550 dispose_cap_releases(session->s_mdsc, &dispose);
Sage Weil2f2dc052009-10-06 11:31:09 -07001551}
1552
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001553enum {
1554 RECONNECT,
1555 RENEWCAPS,
1556 FORCE_RO,
1557};
1558
Sage Weil2f2dc052009-10-06 11:31:09 -07001559/*
1560 * wake up any threads waiting on this session's caps. if the cap is
1561 * old (didn't get renewed on the client reconnect), remove it now.
1562 *
1563 * caller must hold s_mutex.
1564 */
1565static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1566 void *arg)
1567{
Sage Weil0dc25702009-11-20 13:43:45 -08001568 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001569 unsigned long ev = (unsigned long)arg;
Sage Weil0dc25702009-11-20 13:43:45 -08001570
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001571 if (ev == RECONNECT) {
Sage Weilbe655592011-11-30 09:47:09 -08001572 spin_lock(&ci->i_ceph_lock);
Sage Weil0dc25702009-11-20 13:43:45 -08001573 ci->i_wanted_max_size = 0;
1574 ci->i_requested_max_size = 0;
Sage Weilbe655592011-11-30 09:47:09 -08001575 spin_unlock(&ci->i_ceph_lock);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001576 } else if (ev == RENEWCAPS) {
1577 if (cap->cap_gen < cap->session->s_cap_gen) {
1578 /* mds did not re-issue stale cap */
1579 spin_lock(&ci->i_ceph_lock);
1580 cap->issued = cap->implemented = CEPH_CAP_PIN;
1581 /* make sure mds knows what we want */
1582 if (__ceph_caps_file_wanted(ci) & ~cap->mds_wanted)
1583 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
1584 spin_unlock(&ci->i_ceph_lock);
1585 }
1586 } else if (ev == FORCE_RO) {
Sage Weil0dc25702009-11-20 13:43:45 -08001587 }
Yan, Zhenge5360302016-05-19 19:15:19 +08001588 wake_up_all(&ci->i_cap_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07001589 return 0;
1590}
1591
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001592static void wake_up_session_caps(struct ceph_mds_session *session, int ev)
Sage Weil2f2dc052009-10-06 11:31:09 -07001593{
1594 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
Jeff Laytonf5d772692019-04-24 12:09:04 -04001595 ceph_iterate_session_caps(session, wake_up_session_cb,
1596 (void *)(unsigned long)ev);
Sage Weil2f2dc052009-10-06 11:31:09 -07001597}
1598
1599/*
1600 * Send periodic message to MDS renewing all currently held caps. The
1601 * ack will reset the expiration for all caps from this session.
1602 *
1603 * caller holds s_mutex
1604 */
1605static int send_renew_caps(struct ceph_mds_client *mdsc,
1606 struct ceph_mds_session *session)
1607{
1608 struct ceph_msg *msg;
1609 int state;
1610
1611 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1612 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1613 pr_info("mds%d caps stale\n", session->s_mds);
Sage Weile4cb4cb2010-03-18 13:43:09 -07001614 session->s_renew_requested = jiffies;
Sage Weil2f2dc052009-10-06 11:31:09 -07001615
1616 /* do not try to renew caps until a recovering mds has reconnected
1617 * with its clients. */
1618 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1619 if (state < CEPH_MDS_STATE_RECONNECT) {
1620 dout("send_renew_caps ignoring mds%d (%s)\n",
1621 session->s_mds, ceph_mds_state_name(state));
1622 return 0;
1623 }
1624
1625 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1626 ceph_mds_state_name(state));
Sage Weil2f2dc052009-10-06 11:31:09 -07001627 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1628 ++session->s_renew_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001629 if (!msg)
1630 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -07001631 ceph_con_send(&session->s_con, msg);
1632 return 0;
1633}
1634
Yan, Zheng186e4f72013-11-22 14:48:37 +08001635static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
1636 struct ceph_mds_session *session, u64 seq)
1637{
1638 struct ceph_msg *msg;
1639
1640 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
John Spraya687eca2014-09-19 13:51:08 +01001641 session->s_mds, ceph_session_state_name(session->s_state), seq);
Yan, Zheng186e4f72013-11-22 14:48:37 +08001642 msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
1643 if (!msg)
1644 return -ENOMEM;
1645 ceph_con_send(&session->s_con, msg);
1646 return 0;
1647}
1648
1649
Sage Weil2f2dc052009-10-06 11:31:09 -07001650/*
1651 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
Sage Weil0dc25702009-11-20 13:43:45 -08001652 *
1653 * Called under session->s_mutex
Sage Weil2f2dc052009-10-06 11:31:09 -07001654 */
1655static void renewed_caps(struct ceph_mds_client *mdsc,
1656 struct ceph_mds_session *session, int is_renew)
1657{
1658 int was_stale;
1659 int wake = 0;
1660
1661 spin_lock(&session->s_cap_lock);
Alex Elder1ce208a2012-01-12 17:48:11 -08001662 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
Sage Weil2f2dc052009-10-06 11:31:09 -07001663
1664 session->s_cap_ttl = session->s_renew_requested +
1665 mdsc->mdsmap->m_session_timeout*HZ;
1666
1667 if (was_stale) {
1668 if (time_before(jiffies, session->s_cap_ttl)) {
1669 pr_info("mds%d caps renewed\n", session->s_mds);
1670 wake = 1;
1671 } else {
1672 pr_info("mds%d caps still stale\n", session->s_mds);
1673 }
1674 }
1675 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1676 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1677 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1678 spin_unlock(&session->s_cap_lock);
1679
1680 if (wake)
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001681 wake_up_session_caps(session, RENEWCAPS);
Sage Weil2f2dc052009-10-06 11:31:09 -07001682}
1683
1684/*
1685 * send a session close request
1686 */
1687static int request_close_session(struct ceph_mds_client *mdsc,
1688 struct ceph_mds_session *session)
1689{
1690 struct ceph_msg *msg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001691
1692 dout("request_close_session mds%d state %s seq %lld\n",
John Spraya687eca2014-09-19 13:51:08 +01001693 session->s_mds, ceph_session_state_name(session->s_state),
Sage Weil2f2dc052009-10-06 11:31:09 -07001694 session->s_seq);
1695 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001696 if (!msg)
1697 return -ENOMEM;
1698 ceph_con_send(&session->s_con, msg);
Yan, Zhengfcff4152016-09-14 16:39:51 +08001699 return 1;
Sage Weil2f2dc052009-10-06 11:31:09 -07001700}
1701
1702/*
1703 * Called with s_mutex held.
1704 */
1705static int __close_session(struct ceph_mds_client *mdsc,
1706 struct ceph_mds_session *session)
1707{
1708 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1709 return 0;
1710 session->s_state = CEPH_MDS_SESSION_CLOSING;
1711 return request_close_session(mdsc, session);
1712}
1713
Yan, Zheng040d7862017-11-30 11:59:22 +08001714static bool drop_negative_children(struct dentry *dentry)
1715{
1716 struct dentry *child;
1717 bool all_negative = true;
1718
1719 if (!d_is_dir(dentry))
1720 goto out;
1721
1722 spin_lock(&dentry->d_lock);
1723 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
1724 if (d_really_is_positive(child)) {
1725 all_negative = false;
1726 break;
1727 }
1728 }
1729 spin_unlock(&dentry->d_lock);
1730
1731 if (all_negative)
1732 shrink_dcache_parent(dentry);
1733out:
1734 return all_negative;
1735}
1736
Sage Weil2f2dc052009-10-06 11:31:09 -07001737/*
1738 * Trim old(er) caps.
1739 *
1740 * Because we can't cache an inode without one or more caps, we do
1741 * this indirectly: if a cap is unused, we prune its aliases, at which
1742 * point the inode will hopefully get dropped to.
1743 *
1744 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1745 * memory pressure from the MDS, though, so it needn't be perfect.
1746 */
1747static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1748{
Jeff Layton533a2812019-07-19 15:22:28 -04001749 int *remaining = arg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001750 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng979abfd2013-11-22 13:56:24 +08001751 int used, wanted, oissued, mine;
Sage Weil2f2dc052009-10-06 11:31:09 -07001752
Jeff Layton533a2812019-07-19 15:22:28 -04001753 if (*remaining <= 0)
Sage Weil2f2dc052009-10-06 11:31:09 -07001754 return -1;
1755
Sage Weilbe655592011-11-30 09:47:09 -08001756 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001757 mine = cap->issued | cap->implemented;
1758 used = __ceph_caps_used(ci);
Yan, Zheng979abfd2013-11-22 13:56:24 +08001759 wanted = __ceph_caps_file_wanted(ci);
Sage Weil2f2dc052009-10-06 11:31:09 -07001760 oissued = __ceph_caps_issued_other(ci, cap);
1761
Yan, Zheng979abfd2013-11-22 13:56:24 +08001762 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001763 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
Yan, Zheng979abfd2013-11-22 13:56:24 +08001764 ceph_cap_string(used), ceph_cap_string(wanted));
1765 if (cap == ci->i_auth_cap) {
Yan, Zheng622f3e22015-05-07 10:59:47 +08001766 if (ci->i_dirty_caps || ci->i_flushing_caps ||
1767 !list_empty(&ci->i_cap_snaps))
Yan, Zheng979abfd2013-11-22 13:56:24 +08001768 goto out;
1769 if ((used | wanted) & CEPH_CAP_ANY_WR)
1770 goto out;
Yan, Zheng89aa5932017-09-08 15:23:18 +08001771 /* Note: it's possible that i_filelock_ref becomes non-zero
1772 * after dropping auth caps. It doesn't hurt because reply
1773 * of lock mds request will re-add auth caps. */
1774 if (atomic_read(&ci->i_filelock_ref) > 0)
1775 goto out;
Yan, Zheng979abfd2013-11-22 13:56:24 +08001776 }
Yan, Zheng5e804ac2015-10-26 16:08:43 +08001777 /* The inode has cached pages, but it's no longer used.
1778 * we can safely drop it */
Yan, Zheng525d15e2019-05-11 17:27:59 +08001779 if (S_ISREG(inode->i_mode) &&
1780 wanted == 0 && used == CEPH_CAP_FILE_CACHE &&
Yan, Zheng5e804ac2015-10-26 16:08:43 +08001781 !(oissued & CEPH_CAP_FILE_CACHE)) {
1782 used = 0;
1783 oissued = 0;
1784 }
Yan, Zheng979abfd2013-11-22 13:56:24 +08001785 if ((used | wanted) & ~oissued & mine)
Sage Weil2f2dc052009-10-06 11:31:09 -07001786 goto out; /* we need these caps */
1787
Sage Weil2f2dc052009-10-06 11:31:09 -07001788 if (oissued) {
1789 /* we aren't the only cap.. just remove us */
Yan, Zhenga096b092013-09-22 10:15:58 +08001790 __ceph_remove_cap(cap, true);
Jeff Layton533a2812019-07-19 15:22:28 -04001791 (*remaining)--;
Sage Weil2f2dc052009-10-06 11:31:09 -07001792 } else {
Yan, Zheng040d7862017-11-30 11:59:22 +08001793 struct dentry *dentry;
Yan, Zheng5e804ac2015-10-26 16:08:43 +08001794 /* try dropping referring dentries */
Sage Weilbe655592011-11-30 09:47:09 -08001795 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng040d7862017-11-30 11:59:22 +08001796 dentry = d_find_any_alias(inode);
1797 if (dentry && drop_negative_children(dentry)) {
1798 int count;
1799 dput(dentry);
1800 d_prune_aliases(inode);
1801 count = atomic_read(&inode->i_count);
1802 if (count == 1)
Jeff Layton533a2812019-07-19 15:22:28 -04001803 (*remaining)--;
Yan, Zheng040d7862017-11-30 11:59:22 +08001804 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1805 inode, cap, count);
1806 } else {
1807 dput(dentry);
1808 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001809 return 0;
1810 }
1811
1812out:
Sage Weilbe655592011-11-30 09:47:09 -08001813 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001814 return 0;
1815}
1816
1817/*
1818 * Trim session cap count down to some max number.
1819 */
Zhi Zhange30ee582018-01-24 21:24:33 +08001820int ceph_trim_caps(struct ceph_mds_client *mdsc,
1821 struct ceph_mds_session *session,
1822 int max_caps)
Sage Weil2f2dc052009-10-06 11:31:09 -07001823{
1824 int trim_caps = session->s_nr_caps - max_caps;
1825
1826 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1827 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1828 if (trim_caps > 0) {
Jeff Layton533a2812019-07-19 15:22:28 -04001829 int remaining = trim_caps;
1830
1831 ceph_iterate_session_caps(session, trim_caps_cb, &remaining);
Sage Weil2f2dc052009-10-06 11:31:09 -07001832 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1833 session->s_mds, session->s_nr_caps, max_caps,
Jeff Layton533a2812019-07-19 15:22:28 -04001834 trim_caps - remaining);
Sage Weil2f2dc052009-10-06 11:31:09 -07001835 }
Yan, Zhenga56371d2014-04-01 20:34:56 +08001836
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08001837 ceph_flush_cap_releases(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07001838 return 0;
1839}
1840
Yan, Zheng8310b082015-06-09 17:20:12 +08001841static int check_caps_flush(struct ceph_mds_client *mdsc,
1842 u64 want_flush_tid)
1843{
Yan, Zheng8310b082015-06-09 17:20:12 +08001844 int ret = 1;
1845
1846 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zhenge4500b52016-07-06 11:12:56 +08001847 if (!list_empty(&mdsc->cap_flush_list)) {
1848 struct ceph_cap_flush *cf =
1849 list_first_entry(&mdsc->cap_flush_list,
1850 struct ceph_cap_flush, g_list);
1851 if (cf->tid <= want_flush_tid) {
1852 dout("check_caps_flush still flushing tid "
1853 "%llu <= %llu\n", cf->tid, want_flush_tid);
1854 ret = 0;
1855 }
Yan, Zheng8310b082015-06-09 17:20:12 +08001856 }
1857 spin_unlock(&mdsc->cap_dirty_lock);
1858 return ret;
Yan, Zhengd3383a82015-01-08 21:30:12 +08001859}
1860
Sage Weil2f2dc052009-10-06 11:31:09 -07001861/*
1862 * flush all dirty inode data to disk.
1863 *
Yan, Zheng8310b082015-06-09 17:20:12 +08001864 * returns true if we've flushed through want_flush_tid
Sage Weil2f2dc052009-10-06 11:31:09 -07001865 */
Yan, Zhengaffbc192015-05-05 21:22:13 +08001866static void wait_caps_flush(struct ceph_mds_client *mdsc,
Yan, Zheng0e294382016-07-04 18:06:41 +08001867 u64 want_flush_tid)
Sage Weil2f2dc052009-10-06 11:31:09 -07001868{
Yan, Zheng0e294382016-07-04 18:06:41 +08001869 dout("check_caps_flush want %llu\n", want_flush_tid);
Yan, Zheng8310b082015-06-09 17:20:12 +08001870
1871 wait_event(mdsc->cap_flushing_wq,
1872 check_caps_flush(mdsc, want_flush_tid));
1873
1874 dout("check_caps_flush ok, flushed thru %llu\n", want_flush_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07001875}
1876
1877/*
1878 * called under s_mutex
1879 */
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08001880static void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1881 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001882{
Yan, Zheng745a8e32015-05-14 17:22:42 +08001883 struct ceph_msg *msg = NULL;
1884 struct ceph_mds_cap_release *head;
1885 struct ceph_mds_cap_item *item;
Jeff Layton92475f02017-04-13 11:07:04 -04001886 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
Yan, Zheng745a8e32015-05-14 17:22:42 +08001887 struct ceph_cap *cap;
1888 LIST_HEAD(tmp_list);
1889 int num_cap_releases;
Jeff Layton92475f02017-04-13 11:07:04 -04001890 __le32 barrier, *cap_barrier;
1891
1892 down_read(&osdc->lock);
1893 barrier = cpu_to_le32(osdc->epoch_barrier);
1894 up_read(&osdc->lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001895
Sage Weil0f8605f2010-05-05 15:51:35 -07001896 spin_lock(&session->s_cap_lock);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001897again:
1898 list_splice_init(&session->s_cap_releases, &tmp_list);
1899 num_cap_releases = session->s_num_cap_releases;
1900 session->s_num_cap_releases = 0;
1901 spin_unlock(&session->s_cap_lock);
1902
1903 while (!list_empty(&tmp_list)) {
1904 if (!msg) {
1905 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001906 PAGE_SIZE, GFP_NOFS, false);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001907 if (!msg)
1908 goto out_err;
1909 head = msg->front.iov_base;
1910 head->num = cpu_to_le32(0);
1911 msg->front.iov_len = sizeof(*head);
Jeff Layton92475f02017-04-13 11:07:04 -04001912
1913 msg->hdr.version = cpu_to_le16(2);
1914 msg->hdr.compat_version = cpu_to_le16(1);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001915 }
Jeff Layton92475f02017-04-13 11:07:04 -04001916
Yan, Zheng745a8e32015-05-14 17:22:42 +08001917 cap = list_first_entry(&tmp_list, struct ceph_cap,
1918 session_caps);
1919 list_del(&cap->session_caps);
1920 num_cap_releases--;
1921
1922 head = msg->front.iov_base;
Jeff Layton4198aba2019-05-02 08:06:50 -04001923 put_unaligned_le32(get_unaligned_le32(&head->num) + 1,
1924 &head->num);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001925 item = msg->front.iov_base + msg->front.iov_len;
1926 item->ino = cpu_to_le64(cap->cap_ino);
1927 item->cap_id = cpu_to_le64(cap->cap_id);
1928 item->migrate_seq = cpu_to_le32(cap->mseq);
1929 item->seq = cpu_to_le32(cap->issue_seq);
1930 msg->front.iov_len += sizeof(*item);
1931
1932 ceph_put_cap(mdsc, cap);
1933
1934 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
Jeff Layton92475f02017-04-13 11:07:04 -04001935 // Append cap_barrier field
1936 cap_barrier = msg->front.iov_base + msg->front.iov_len;
1937 *cap_barrier = barrier;
1938 msg->front.iov_len += sizeof(*cap_barrier);
1939
Yan, Zheng745a8e32015-05-14 17:22:42 +08001940 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1941 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1942 ceph_con_send(&session->s_con, msg);
1943 msg = NULL;
1944 }
1945 }
1946
1947 BUG_ON(num_cap_releases != 0);
1948
1949 spin_lock(&session->s_cap_lock);
1950 if (!list_empty(&session->s_cap_releases))
1951 goto again;
1952 spin_unlock(&session->s_cap_lock);
1953
1954 if (msg) {
Jeff Layton92475f02017-04-13 11:07:04 -04001955 // Append cap_barrier field
1956 cap_barrier = msg->front.iov_base + msg->front.iov_len;
1957 *cap_barrier = barrier;
1958 msg->front.iov_len += sizeof(*cap_barrier);
1959
Sage Weil2f2dc052009-10-06 11:31:09 -07001960 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1961 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1962 ceph_con_send(&session->s_con, msg);
1963 }
Yan, Zheng745a8e32015-05-14 17:22:42 +08001964 return;
1965out_err:
1966 pr_err("send_cap_releases mds%d, failed to allocate message\n",
1967 session->s_mds);
1968 spin_lock(&session->s_cap_lock);
1969 list_splice(&tmp_list, &session->s_cap_releases);
1970 session->s_num_cap_releases += num_cap_releases;
Sage Weil2f2dc052009-10-06 11:31:09 -07001971 spin_unlock(&session->s_cap_lock);
1972}
1973
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08001974static void ceph_cap_release_work(struct work_struct *work)
1975{
1976 struct ceph_mds_session *session =
1977 container_of(work, struct ceph_mds_session, s_cap_release_work);
1978
1979 mutex_lock(&session->s_mutex);
1980 if (session->s_state == CEPH_MDS_SESSION_OPEN ||
1981 session->s_state == CEPH_MDS_SESSION_HUNG)
1982 ceph_send_cap_releases(session->s_mdsc, session);
1983 mutex_unlock(&session->s_mutex);
1984 ceph_put_mds_session(session);
1985}
1986
1987void ceph_flush_cap_releases(struct ceph_mds_client *mdsc,
1988 struct ceph_mds_session *session)
1989{
1990 if (mdsc->stopping)
1991 return;
1992
Xiubo Li5b3248c2019-12-19 19:44:09 -05001993 ceph_get_mds_session(session);
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08001994 if (queue_work(mdsc->fsc->cap_wq,
1995 &session->s_cap_release_work)) {
1996 dout("cap release work queued\n");
1997 } else {
1998 ceph_put_mds_session(session);
1999 dout("failed to queue cap release work\n");
2000 }
2001}
2002
2003/*
2004 * caller holds session->s_cap_lock
2005 */
2006void __ceph_queue_cap_release(struct ceph_mds_session *session,
2007 struct ceph_cap *cap)
2008{
2009 list_add_tail(&cap->session_caps, &session->s_cap_releases);
2010 session->s_num_cap_releases++;
2011
2012 if (!(session->s_num_cap_releases % CEPH_CAPS_PER_RELEASE))
2013 ceph_flush_cap_releases(session->s_mdsc, session);
2014}
2015
Yan, Zheng37c4efc2019-01-31 16:55:51 +08002016static void ceph_cap_reclaim_work(struct work_struct *work)
2017{
2018 struct ceph_mds_client *mdsc =
2019 container_of(work, struct ceph_mds_client, cap_reclaim_work);
2020 int ret = ceph_trim_dentries(mdsc);
2021 if (ret == -EAGAIN)
2022 ceph_queue_cap_reclaim_work(mdsc);
2023}
2024
2025void ceph_queue_cap_reclaim_work(struct ceph_mds_client *mdsc)
2026{
2027 if (mdsc->stopping)
2028 return;
2029
2030 if (queue_work(mdsc->fsc->cap_wq, &mdsc->cap_reclaim_work)) {
2031 dout("caps reclaim work queued\n");
2032 } else {
2033 dout("failed to queue caps release work\n");
2034 }
2035}
2036
Yan, Zhengfe330322019-02-01 14:57:15 +08002037void ceph_reclaim_caps_nr(struct ceph_mds_client *mdsc, int nr)
2038{
2039 int val;
2040 if (!nr)
2041 return;
2042 val = atomic_add_return(nr, &mdsc->cap_reclaim_pending);
Xiubo Libba15602019-11-26 07:32:22 -05002043 if ((val % CEPH_CAPS_PER_RELEASE) < nr) {
Yan, Zhengfe330322019-02-01 14:57:15 +08002044 atomic_set(&mdsc->cap_reclaim_pending, 0);
2045 ceph_queue_cap_reclaim_work(mdsc);
2046 }
2047}
2048
Sage Weil2f2dc052009-10-06 11:31:09 -07002049/*
2050 * requests
2051 */
2052
Yan, Zheng54008392014-03-29 13:41:15 +08002053int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
2054 struct inode *dir)
2055{
2056 struct ceph_inode_info *ci = ceph_inode(dir);
2057 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
2058 struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options;
Yan, Zheng2a5beea2016-04-28 09:37:39 +08002059 size_t size = sizeof(struct ceph_mds_reply_dir_entry);
Jeff Laytonad8c28a2019-09-09 15:58:55 -04002060 unsigned int num_entries;
2061 int order;
Yan, Zheng54008392014-03-29 13:41:15 +08002062
2063 spin_lock(&ci->i_ceph_lock);
2064 num_entries = ci->i_files + ci->i_subdirs;
2065 spin_unlock(&ci->i_ceph_lock);
Jeff Laytonad8c28a2019-09-09 15:58:55 -04002066 num_entries = max(num_entries, 1U);
Yan, Zheng54008392014-03-29 13:41:15 +08002067 num_entries = min(num_entries, opt->max_readdir);
2068
2069 order = get_order(size * num_entries);
2070 while (order >= 0) {
Yan, Zheng2a5beea2016-04-28 09:37:39 +08002071 rinfo->dir_entries = (void*)__get_free_pages(GFP_KERNEL |
2072 __GFP_NOWARN,
2073 order);
2074 if (rinfo->dir_entries)
Yan, Zheng54008392014-03-29 13:41:15 +08002075 break;
2076 order--;
2077 }
Yan, Zheng2a5beea2016-04-28 09:37:39 +08002078 if (!rinfo->dir_entries)
Yan, Zheng54008392014-03-29 13:41:15 +08002079 return -ENOMEM;
2080
2081 num_entries = (PAGE_SIZE << order) / size;
2082 num_entries = min(num_entries, opt->max_readdir);
2083
2084 rinfo->dir_buf_size = PAGE_SIZE << order;
2085 req->r_num_caps = num_entries + 1;
2086 req->r_args.readdir.max_entries = cpu_to_le32(num_entries);
2087 req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes);
2088 return 0;
2089}
2090
Sage Weil2f2dc052009-10-06 11:31:09 -07002091/*
2092 * Create an mds request.
2093 */
2094struct ceph_mds_request *
2095ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
2096{
Jeff Layton058daab2020-02-17 18:38:37 -05002097 struct ceph_mds_request *req;
Sage Weil2f2dc052009-10-06 11:31:09 -07002098
Jeff Layton058daab2020-02-17 18:38:37 -05002099 req = kmem_cache_zalloc(ceph_mds_request_cachep, GFP_NOFS);
Sage Weil2f2dc052009-10-06 11:31:09 -07002100 if (!req)
2101 return ERR_PTR(-ENOMEM);
2102
Sage Weilb4556392010-05-13 12:01:13 -07002103 mutex_init(&req->r_fill_mutex);
Yehuda Sadeh37151662010-06-17 16:16:12 -07002104 req->r_mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07002105 req->r_started = jiffies;
2106 req->r_resend_mds = -1;
2107 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002108 INIT_LIST_HEAD(&req->r_unsafe_target_item);
Sage Weil2f2dc052009-10-06 11:31:09 -07002109 req->r_fmode = -1;
Sage Weil153c8e62009-12-07 12:31:09 -08002110 kref_init(&req->r_kref);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02002111 RB_CLEAR_NODE(&req->r_node);
Sage Weil2f2dc052009-10-06 11:31:09 -07002112 INIT_LIST_HEAD(&req->r_wait);
2113 init_completion(&req->r_completion);
2114 init_completion(&req->r_safe_completion);
2115 INIT_LIST_HEAD(&req->r_unsafe_item);
2116
Deepa Dinamani668c9a62019-12-02 21:19:42 -08002117 ktime_get_coarse_real_ts64(&req->r_stamp);
Sage Weilb8e69062014-05-21 17:41:08 -07002118
Sage Weil2f2dc052009-10-06 11:31:09 -07002119 req->r_op = op;
2120 req->r_direct_mode = mode;
2121 return req;
2122}
2123
2124/*
Sage Weil44ca18f2010-02-15 12:08:46 -08002125 * return oldest (lowest) request, tid in request tree, 0 if none.
Sage Weil2f2dc052009-10-06 11:31:09 -07002126 *
2127 * called under mdsc->mutex.
2128 */
Sage Weil44ca18f2010-02-15 12:08:46 -08002129static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
2130{
2131 if (RB_EMPTY_ROOT(&mdsc->request_tree))
2132 return NULL;
2133 return rb_entry(rb_first(&mdsc->request_tree),
2134 struct ceph_mds_request, r_node);
2135}
2136
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08002137static inline u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07002138{
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08002139 return mdsc->oldest_tid;
Sage Weil2f2dc052009-10-06 11:31:09 -07002140}
2141
2142/*
2143 * Build a dentry's path. Allocate on heap; caller must kfree. Based
2144 * on build_path_from_dentry in fs/cifs/dir.c.
2145 *
2146 * If @stop_on_nosnap, generate path relative to the first non-snapped
2147 * inode.
2148 *
2149 * Encode hidden .snap dirs as a double /, i.e.
2150 * foo/.snap/bar -> foo//bar
2151 */
Jeff Layton69a10fb2019-04-26 13:33:39 -04002152char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase,
Sage Weil2f2dc052009-10-06 11:31:09 -07002153 int stop_on_nosnap)
2154{
2155 struct dentry *temp;
2156 char *path;
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002157 int pos;
Al Viro1b71fe22011-07-16 23:43:58 -04002158 unsigned seq;
Jeff Layton69a10fb2019-04-26 13:33:39 -04002159 u64 base;
Sage Weil2f2dc052009-10-06 11:31:09 -07002160
Markus Elfringd37b1d92017-08-20 20:22:02 +02002161 if (!dentry)
Sage Weil2f2dc052009-10-06 11:31:09 -07002162 return ERR_PTR(-EINVAL);
2163
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002164 path = __getname();
Markus Elfringd37b1d92017-08-20 20:22:02 +02002165 if (!path)
Sage Weil2f2dc052009-10-06 11:31:09 -07002166 return ERR_PTR(-ENOMEM);
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002167retry:
2168 pos = PATH_MAX - 1;
2169 path[pos] = '\0';
2170
2171 seq = read_seqbegin(&rename_lock);
Al Viro1b71fe22011-07-16 23:43:58 -04002172 rcu_read_lock();
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002173 temp = dentry;
2174 for (;;) {
Al Viro1b71fe22011-07-16 23:43:58 -04002175 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07002176
Al Viro1b71fe22011-07-16 23:43:58 -04002177 spin_lock(&temp->d_lock);
David Howells2b0143b2015-03-17 22:25:59 +00002178 inode = d_inode(temp);
Sage Weil2f2dc052009-10-06 11:31:09 -07002179 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
Sage Weil104648a2010-03-18 10:14:30 -07002180 dout("build_path path+%d: %p SNAPDIR\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002181 pos, temp);
Jeff Laytond6b8bd62019-05-09 07:58:38 -04002182 } else if (stop_on_nosnap && inode && dentry != temp &&
Sage Weil2f2dc052009-10-06 11:31:09 -07002183 ceph_snap(inode) == CEPH_NOSNAP) {
Yehuda Sadeh9d5a09e2011-12-13 09:57:44 -08002184 spin_unlock(&temp->d_lock);
Jeff Laytond6b8bd62019-05-09 07:58:38 -04002185 pos++; /* get rid of any prepended '/' */
Sage Weil2f2dc052009-10-06 11:31:09 -07002186 break;
2187 } else {
2188 pos -= temp->d_name.len;
Al Viro1b71fe22011-07-16 23:43:58 -04002189 if (pos < 0) {
2190 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002191 break;
Al Viro1b71fe22011-07-16 23:43:58 -04002192 }
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002193 memcpy(path + pos, temp->d_name.name, temp->d_name.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07002194 }
Al Viro1b71fe22011-07-16 23:43:58 -04002195 spin_unlock(&temp->d_lock);
Yan, Zheng41883ba2019-05-23 10:22:55 +08002196 temp = READ_ONCE(temp->d_parent);
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002197
2198 /* Are we at the root? */
2199 if (IS_ROOT(temp))
2200 break;
2201
2202 /* Are we out of buffer? */
2203 if (--pos < 0)
2204 break;
2205
2206 path[pos] = '/';
Sage Weil2f2dc052009-10-06 11:31:09 -07002207 }
Jeff Layton69a10fb2019-04-26 13:33:39 -04002208 base = ceph_ino(d_inode(temp));
Al Viro1b71fe22011-07-16 23:43:58 -04002209 rcu_read_unlock();
Jeff Laytonf5946bc2019-10-16 08:20:17 -04002210
2211 if (read_seqretry(&rename_lock, seq))
2212 goto retry;
2213
2214 if (pos < 0) {
2215 /*
2216 * A rename didn't occur, but somehow we didn't end up where
2217 * we thought we would. Throw a warning and try again.
2218 */
2219 pr_warn("build_path did not end path lookup where "
2220 "expected, pos is %d\n", pos);
Sage Weil2f2dc052009-10-06 11:31:09 -07002221 goto retry;
2222 }
2223
Jeff Layton69a10fb2019-04-26 13:33:39 -04002224 *pbase = base;
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002225 *plen = PATH_MAX - 1 - pos;
Sage Weil104648a2010-03-18 10:14:30 -07002226 dout("build_path on %p %d built %llx '%.*s'\n",
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002227 dentry, d_count(dentry), base, *plen, path + pos);
2228 return path + pos;
Sage Weil2f2dc052009-10-06 11:31:09 -07002229}
2230
Jeff Laytonfd36a712016-12-15 08:37:58 -05002231static int build_dentry_path(struct dentry *dentry, struct inode *dir,
Sage Weil2f2dc052009-10-06 11:31:09 -07002232 const char **ppath, int *ppathlen, u64 *pino,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002233 bool *pfreepath, bool parent_locked)
Sage Weil2f2dc052009-10-06 11:31:09 -07002234{
2235 char *path;
2236
Jeff Laytonc6b0b652016-12-15 08:37:57 -05002237 rcu_read_lock();
Jeff Laytonfd36a712016-12-15 08:37:58 -05002238 if (!dir)
2239 dir = d_inode_rcu(dentry->d_parent);
Jeff Layton964fff72019-04-29 11:51:02 -04002240 if (dir && parent_locked && ceph_snap(dir) == CEPH_NOSNAP) {
Jeff Laytonc6b0b652016-12-15 08:37:57 -05002241 *pino = ceph_ino(dir);
2242 rcu_read_unlock();
Jeff Layton964fff72019-04-29 11:51:02 -04002243 *ppath = dentry->d_name.name;
2244 *ppathlen = dentry->d_name.len;
Sage Weil2f2dc052009-10-06 11:31:09 -07002245 return 0;
2246 }
Jeff Laytonc6b0b652016-12-15 08:37:57 -05002247 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -07002248 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
2249 if (IS_ERR(path))
2250 return PTR_ERR(path);
2251 *ppath = path;
Jeff Layton1bcb3442019-04-15 12:00:42 -04002252 *pfreepath = true;
Sage Weil2f2dc052009-10-06 11:31:09 -07002253 return 0;
2254}
2255
2256static int build_inode_path(struct inode *inode,
2257 const char **ppath, int *ppathlen, u64 *pino,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002258 bool *pfreepath)
Sage Weil2f2dc052009-10-06 11:31:09 -07002259{
2260 struct dentry *dentry;
2261 char *path;
2262
2263 if (ceph_snap(inode) == CEPH_NOSNAP) {
2264 *pino = ceph_ino(inode);
2265 *ppathlen = 0;
2266 return 0;
2267 }
2268 dentry = d_find_alias(inode);
2269 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
2270 dput(dentry);
2271 if (IS_ERR(path))
2272 return PTR_ERR(path);
2273 *ppath = path;
Jeff Layton1bcb3442019-04-15 12:00:42 -04002274 *pfreepath = true;
Sage Weil2f2dc052009-10-06 11:31:09 -07002275 return 0;
2276}
2277
2278/*
2279 * request arguments may be specified via an inode *, a dentry *, or
2280 * an explicit ino+path.
2281 */
2282static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
Jeff Laytonfd36a712016-12-15 08:37:58 -05002283 struct inode *rdiri, const char *rpath,
2284 u64 rino, const char **ppath, int *pathlen,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002285 u64 *ino, bool *freepath, bool parent_locked)
Sage Weil2f2dc052009-10-06 11:31:09 -07002286{
2287 int r = 0;
2288
2289 if (rinode) {
2290 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
2291 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
2292 ceph_snap(rinode));
2293 } else if (rdentry) {
Jeff Laytonfd36a712016-12-15 08:37:58 -05002294 r = build_dentry_path(rdentry, rdiri, ppath, pathlen, ino,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002295 freepath, parent_locked);
Sage Weil2f2dc052009-10-06 11:31:09 -07002296 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
2297 *ppath);
Sage Weil795858d2011-08-15 13:02:37 -07002298 } else if (rpath || rino) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002299 *ino = rino;
2300 *ppath = rpath;
David Zafmanb0000562012-10-25 10:23:46 -07002301 *pathlen = rpath ? strlen(rpath) : 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002302 dout(" path %.*s\n", *pathlen, rpath);
2303 }
2304
2305 return r;
2306}
2307
2308/*
2309 * called under mdsc->mutex
2310 */
2311static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
2312 struct ceph_mds_request *req,
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002313 int mds, bool drop_cap_releases)
Sage Weil2f2dc052009-10-06 11:31:09 -07002314{
2315 struct ceph_msg *msg;
2316 struct ceph_mds_request_head *head;
2317 const char *path1 = NULL;
2318 const char *path2 = NULL;
2319 u64 ino1 = 0, ino2 = 0;
2320 int pathlen1 = 0, pathlen2 = 0;
Jeff Layton1bcb3442019-04-15 12:00:42 -04002321 bool freepath1 = false, freepath2 = false;
Sage Weil2f2dc052009-10-06 11:31:09 -07002322 int len;
2323 u16 releases;
2324 void *p, *end;
2325 int ret;
2326
2327 ret = set_request_path_attr(req->r_inode, req->r_dentry,
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002328 req->r_parent, req->r_path1, req->r_ino1.ino,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002329 &path1, &pathlen1, &ino1, &freepath1,
2330 test_bit(CEPH_MDS_R_PARENT_LOCKED,
2331 &req->r_req_flags));
Sage Weil2f2dc052009-10-06 11:31:09 -07002332 if (ret < 0) {
2333 msg = ERR_PTR(ret);
2334 goto out;
2335 }
2336
Jeff Layton1bcb3442019-04-15 12:00:42 -04002337 /* If r_old_dentry is set, then assume that its parent is locked */
Sage Weil2f2dc052009-10-06 11:31:09 -07002338 ret = set_request_path_attr(NULL, req->r_old_dentry,
Jeff Laytonfd36a712016-12-15 08:37:58 -05002339 req->r_old_dentry_dir,
Sage Weil2f2dc052009-10-06 11:31:09 -07002340 req->r_path2, req->r_ino2.ino,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002341 &path2, &pathlen2, &ino2, &freepath2, true);
Sage Weil2f2dc052009-10-06 11:31:09 -07002342 if (ret < 0) {
2343 msg = ERR_PTR(ret);
2344 goto out_free1;
2345 }
2346
2347 len = sizeof(*head) +
Sage Weilb8e69062014-05-21 17:41:08 -07002348 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) +
Arnd Bergmann777d7382015-09-30 15:04:42 +02002349 sizeof(struct ceph_timespec);
Sage Weil2f2dc052009-10-06 11:31:09 -07002350
2351 /* calculate (max) length for cap releases */
2352 len += sizeof(struct ceph_mds_request_release) *
2353 (!!req->r_inode_drop + !!req->r_dentry_drop +
2354 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
2355 if (req->r_dentry_drop)
Jeff Laytonc1dfc272019-04-17 14:23:17 -04002356 len += pathlen1;
Sage Weil2f2dc052009-10-06 11:31:09 -07002357 if (req->r_old_dentry_drop)
Jeff Laytonc1dfc272019-04-17 14:23:17 -04002358 len += pathlen2;
Sage Weil2f2dc052009-10-06 11:31:09 -07002359
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02002360 msg = ceph_msg_new2(CEPH_MSG_CLIENT_REQUEST, len, 1, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07002361 if (!msg) {
2362 msg = ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -07002363 goto out_free2;
Sage Weila79832f2010-04-01 16:06:19 -07002364 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002365
John Spray7cfa0312014-10-30 17:15:26 +00002366 msg->hdr.version = cpu_to_le16(2);
Sage Weil6df058c2009-12-22 11:24:33 -08002367 msg->hdr.tid = cpu_to_le64(req->r_tid);
2368
Sage Weil2f2dc052009-10-06 11:31:09 -07002369 head = msg->front.iov_base;
2370 p = msg->front.iov_base + sizeof(*head);
2371 end = msg->front.iov_base + msg->front.iov_len;
2372
2373 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
2374 head->op = cpu_to_le32(req->r_op);
Eric W. Biedermanff3d0042013-01-31 04:01:53 -08002375 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid));
2376 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid));
Jeff Layton2def8652019-10-14 15:41:54 -04002377 head->ino = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002378 head->args = req->r_args;
2379
2380 ceph_encode_filepath(&p, end, ino1, path1);
2381 ceph_encode_filepath(&p, end, ino2, path2);
2382
Sage Weile979cf52010-07-15 14:58:39 -07002383 /* make note of release offset, in case we need to replay */
2384 req->r_request_release_offset = p - msg->front.iov_base;
2385
Sage Weil2f2dc052009-10-06 11:31:09 -07002386 /* cap releases */
2387 releases = 0;
2388 if (req->r_inode_drop)
2389 releases += ceph_encode_inode_release(&p,
David Howells2b0143b2015-03-17 22:25:59 +00002390 req->r_inode ? req->r_inode : d_inode(req->r_dentry),
Sage Weil2f2dc052009-10-06 11:31:09 -07002391 mds, req->r_inode_drop, req->r_inode_unless, 0);
2392 if (req->r_dentry_drop)
2393 releases += ceph_encode_dentry_release(&p, req->r_dentry,
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002394 req->r_parent, mds, req->r_dentry_drop,
Jeff Laytonca6c8ae2016-12-15 08:37:59 -05002395 req->r_dentry_unless);
Sage Weil2f2dc052009-10-06 11:31:09 -07002396 if (req->r_old_dentry_drop)
2397 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
Jeff Laytonca6c8ae2016-12-15 08:37:59 -05002398 req->r_old_dentry_dir, mds,
2399 req->r_old_dentry_drop,
2400 req->r_old_dentry_unless);
Sage Weil2f2dc052009-10-06 11:31:09 -07002401 if (req->r_old_inode_drop)
2402 releases += ceph_encode_inode_release(&p,
David Howells2b0143b2015-03-17 22:25:59 +00002403 d_inode(req->r_old_dentry),
Sage Weil2f2dc052009-10-06 11:31:09 -07002404 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002405
2406 if (drop_cap_releases) {
2407 releases = 0;
2408 p = msg->front.iov_base + req->r_request_release_offset;
2409 }
2410
Sage Weil2f2dc052009-10-06 11:31:09 -07002411 head->num_releases = cpu_to_le16(releases);
2412
Sage Weilb8e69062014-05-21 17:41:08 -07002413 /* time stamp */
Yan, Zheng1f041a82015-01-13 15:20:52 +08002414 {
2415 struct ceph_timespec ts;
Arnd Bergmann0ed1e902018-07-13 22:18:38 +02002416 ceph_encode_timespec64(&ts, &req->r_stamp);
Yan, Zheng1f041a82015-01-13 15:20:52 +08002417 ceph_encode_copy(&p, &ts, sizeof(ts));
2418 }
Sage Weilb8e69062014-05-21 17:41:08 -07002419
Sage Weil2f2dc052009-10-06 11:31:09 -07002420 BUG_ON(p > end);
2421 msg->front.iov_len = p - msg->front.iov_base;
2422 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2423
Yan, Zheng25e6bae2014-09-16 19:15:28 +08002424 if (req->r_pagelist) {
2425 struct ceph_pagelist *pagelist = req->r_pagelist;
Yan, Zheng25e6bae2014-09-16 19:15:28 +08002426 ceph_msg_data_add_pagelist(msg, pagelist);
2427 msg->hdr.data_len = cpu_to_le32(pagelist->length);
2428 } else {
2429 msg->hdr.data_len = 0;
Alex Elderebf18f42013-03-04 22:29:57 -06002430 }
Alex Elder02afca62013-02-14 12:16:43 -06002431
Sage Weil2f2dc052009-10-06 11:31:09 -07002432 msg->hdr.data_off = cpu_to_le16(0);
2433
2434out_free2:
2435 if (freepath2)
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002436 ceph_mdsc_free_path((char *)path2, pathlen2);
Sage Weil2f2dc052009-10-06 11:31:09 -07002437out_free1:
2438 if (freepath1)
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002439 ceph_mdsc_free_path((char *)path1, pathlen1);
Sage Weil2f2dc052009-10-06 11:31:09 -07002440out:
2441 return msg;
2442}
2443
2444/*
2445 * called under mdsc->mutex if error, under no mutex if
2446 * success.
2447 */
2448static void complete_request(struct ceph_mds_client *mdsc,
2449 struct ceph_mds_request *req)
2450{
2451 if (req->r_callback)
2452 req->r_callback(mdsc, req);
Jeff Layton111c7082019-04-02 09:43:18 -04002453 complete_all(&req->r_completion);
Sage Weil2f2dc052009-10-06 11:31:09 -07002454}
2455
2456/*
2457 * called under mdsc->mutex
2458 */
2459static int __prepare_send_request(struct ceph_mds_client *mdsc,
2460 struct ceph_mds_request *req,
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002461 int mds, bool drop_cap_releases)
Sage Weil2f2dc052009-10-06 11:31:09 -07002462{
2463 struct ceph_mds_request_head *rhead;
2464 struct ceph_msg *msg;
2465 int flags = 0;
2466
Sage Weil2f2dc052009-10-06 11:31:09 -07002467 req->r_attempts++;
Greg Farnume55b71f2010-06-22 15:58:01 -07002468 if (req->r_inode) {
2469 struct ceph_cap *cap =
2470 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
2471
2472 if (cap)
2473 req->r_sent_on_mseq = cap->mseq;
2474 else
2475 req->r_sent_on_mseq = -1;
2476 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002477 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
2478 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
2479
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002480 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002481 void *p;
Sage Weil01a92f12010-07-15 13:24:32 -07002482 /*
2483 * Replay. Do not regenerate message (and rebuild
2484 * paths, etc.); just use the original message.
2485 * Rebuilding paths will break for renames because
2486 * d_move mangles the src name.
2487 */
2488 msg = req->r_request;
2489 rhead = msg->front.iov_base;
2490
2491 flags = le32_to_cpu(rhead->flags);
2492 flags |= CEPH_MDS_FLAG_REPLAY;
2493 rhead->flags = cpu_to_le32(flags);
2494
2495 if (req->r_target_inode)
2496 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
2497
2498 rhead->num_retry = req->r_attempts - 1;
Sage Weile979cf52010-07-15 14:58:39 -07002499
2500 /* remove cap/dentry releases from message */
2501 rhead->num_releases = 0;
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002502
2503 /* time stamp */
2504 p = msg->front.iov_base + req->r_request_release_offset;
Yan, Zheng1f041a82015-01-13 15:20:52 +08002505 {
2506 struct ceph_timespec ts;
Arnd Bergmann0ed1e902018-07-13 22:18:38 +02002507 ceph_encode_timespec64(&ts, &req->r_stamp);
Yan, Zheng1f041a82015-01-13 15:20:52 +08002508 ceph_encode_copy(&p, &ts, sizeof(ts));
2509 }
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002510
2511 msg->front.iov_len = p - msg->front.iov_base;
2512 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
Sage Weil01a92f12010-07-15 13:24:32 -07002513 return 0;
2514 }
2515
Sage Weil2f2dc052009-10-06 11:31:09 -07002516 if (req->r_request) {
2517 ceph_msg_put(req->r_request);
2518 req->r_request = NULL;
2519 }
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002520 msg = create_request_message(mdsc, req, mds, drop_cap_releases);
Sage Weil2f2dc052009-10-06 11:31:09 -07002521 if (IS_ERR(msg)) {
Sage Weile1518c72010-05-13 11:19:06 -07002522 req->r_err = PTR_ERR(msg);
Sage Weila79832f2010-04-01 16:06:19 -07002523 return PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002524 }
2525 req->r_request = msg;
2526
2527 rhead = msg->front.iov_base;
Sage Weil2f2dc052009-10-06 11:31:09 -07002528 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002529 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Sage Weil2f2dc052009-10-06 11:31:09 -07002530 flags |= CEPH_MDS_FLAG_REPLAY;
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002531 if (req->r_parent)
Sage Weil2f2dc052009-10-06 11:31:09 -07002532 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
2533 rhead->flags = cpu_to_le32(flags);
2534 rhead->num_fwd = req->r_num_fwd;
2535 rhead->num_retry = req->r_attempts - 1;
Sage Weil01a92f12010-07-15 13:24:32 -07002536 rhead->ino = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002537
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002538 dout(" r_parent = %p\n", req->r_parent);
Sage Weil2f2dc052009-10-06 11:31:09 -07002539 return 0;
2540}
2541
2542/*
Xiubo Li9cf54562019-12-05 20:50:21 -05002543 * called under mdsc->mutex
2544 */
2545static int __send_request(struct ceph_mds_client *mdsc,
2546 struct ceph_mds_session *session,
2547 struct ceph_mds_request *req,
2548 bool drop_cap_releases)
2549{
2550 int err;
2551
2552 err = __prepare_send_request(mdsc, req, session->s_mds,
2553 drop_cap_releases);
2554 if (!err) {
2555 ceph_msg_get(req->r_request);
2556 ceph_con_send(&session->s_con, req->r_request);
2557 }
2558
2559 return err;
2560}
2561
2562/*
Sage Weil2f2dc052009-10-06 11:31:09 -07002563 * send request, or put it on the appropriate wait list.
2564 */
Chengguang Xud5548492018-07-28 16:30:48 +08002565static void __do_request(struct ceph_mds_client *mdsc,
Sage Weil2f2dc052009-10-06 11:31:09 -07002566 struct ceph_mds_request *req)
2567{
2568 struct ceph_mds_session *session = NULL;
2569 int mds = -1;
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002570 int err = 0;
Xiubo Lic4853e92019-12-09 07:47:15 -05002571 bool random;
Sage Weil2f2dc052009-10-06 11:31:09 -07002572
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002573 if (req->r_err || test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
2574 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
Yan, Zhengeb1b8af2013-09-26 14:25:36 +08002575 __unregister_request(mdsc, req);
Chengguang Xud5548492018-07-28 16:30:48 +08002576 return;
Yan, Zhengeb1b8af2013-09-26 14:25:36 +08002577 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002578
2579 if (req->r_timeout &&
2580 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
2581 dout("do_request timed out\n");
Xiubo Li8ccf7fc2020-02-23 22:23:11 -05002582 err = -ETIMEDOUT;
Sage Weil2f2dc052009-10-06 11:31:09 -07002583 goto finish;
2584 }
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01002585 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002586 dout("do_request forced umount\n");
2587 err = -EIO;
2588 goto finish;
2589 }
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01002590 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_MOUNTING) {
Yan, Zhenge9e427f2016-11-10 16:02:06 +08002591 if (mdsc->mdsmap_err) {
2592 err = mdsc->mdsmap_err;
2593 dout("do_request mdsmap err %d\n", err);
2594 goto finish;
2595 }
Yan, Zhengcc8e8342017-01-04 16:21:58 +08002596 if (mdsc->mdsmap->m_epoch == 0) {
2597 dout("do_request no mdsmap, waiting for map\n");
2598 list_add(&req->r_wait, &mdsc->waiting_for_map);
Chengguang Xud5548492018-07-28 16:30:48 +08002599 return;
Yan, Zhengcc8e8342017-01-04 16:21:58 +08002600 }
Yan, Zhenge9e427f2016-11-10 16:02:06 +08002601 if (!(mdsc->fsc->mount_options->flags &
2602 CEPH_MOUNT_OPT_MOUNTWAIT) &&
2603 !ceph_mdsmap_is_cluster_available(mdsc->mdsmap)) {
Xiubo Li97820052019-12-10 20:29:40 -05002604 err = -EHOSTUNREACH;
Yan, Zhenge9e427f2016-11-10 16:02:06 +08002605 goto finish;
2606 }
2607 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002608
Sage Weildc69e2e2010-11-02 13:49:00 -07002609 put_request_session(req);
2610
Xiubo Lic4853e92019-12-09 07:47:15 -05002611 mds = __choose_mds(mdsc, req, &random);
Sage Weil2f2dc052009-10-06 11:31:09 -07002612 if (mds < 0 ||
2613 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
2614 dout("do_request no mds or not active, waiting for map\n");
2615 list_add(&req->r_wait, &mdsc->waiting_for_map);
Chengguang Xud5548492018-07-28 16:30:48 +08002616 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07002617 }
2618
2619 /* get, open session */
2620 session = __ceph_lookup_mds_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07002621 if (!session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002622 session = register_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07002623 if (IS_ERR(session)) {
2624 err = PTR_ERR(session);
2625 goto finish;
2626 }
2627 }
Xiubo Li5b3248c2019-12-19 19:44:09 -05002628 req->r_session = ceph_get_mds_session(session);
Sage Weildc69e2e2010-11-02 13:49:00 -07002629
Sage Weil2f2dc052009-10-06 11:31:09 -07002630 dout("do_request mds%d session %p state %s\n", mds, session,
John Spraya687eca2014-09-19 13:51:08 +01002631 ceph_session_state_name(session->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07002632 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
2633 session->s_state != CEPH_MDS_SESSION_HUNG) {
Yan, Zhengfcff4152016-09-14 16:39:51 +08002634 if (session->s_state == CEPH_MDS_SESSION_REJECTED) {
2635 err = -EACCES;
2636 goto out_session;
2637 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002638 if (session->s_state == CEPH_MDS_SESSION_NEW ||
Xiubo Lic4853e92019-12-09 07:47:15 -05002639 session->s_state == CEPH_MDS_SESSION_CLOSING) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002640 __open_session(mdsc, session);
Xiubo Lic4853e92019-12-09 07:47:15 -05002641 /* retry the same mds later */
2642 if (random)
2643 req->r_resend_mds = mds;
2644 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002645 list_add(&req->r_wait, &session->s_waiting);
2646 goto out_session;
2647 }
2648
2649 /* send request */
Sage Weil2f2dc052009-10-06 11:31:09 -07002650 req->r_resend_mds = -1; /* forget any previous mds hint */
2651
2652 if (req->r_request_started == 0) /* note request start time */
2653 req->r_request_started = jiffies;
2654
Xiubo Li9cf54562019-12-05 20:50:21 -05002655 err = __send_request(mdsc, session, req, false);
Sage Weil2f2dc052009-10-06 11:31:09 -07002656
2657out_session:
2658 ceph_put_mds_session(session);
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002659finish:
2660 if (err) {
2661 dout("__do_request early error %d\n", err);
2662 req->r_err = err;
2663 complete_request(mdsc, req);
2664 __unregister_request(mdsc, req);
2665 }
Chengguang Xud5548492018-07-28 16:30:48 +08002666 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07002667}
2668
2669/*
2670 * called under mdsc->mutex
2671 */
2672static void __wake_requests(struct ceph_mds_client *mdsc,
2673 struct list_head *head)
2674{
Yan, Zhenged75ec22012-11-19 10:49:06 +08002675 struct ceph_mds_request *req;
2676 LIST_HEAD(tmp_list);
Sage Weil2f2dc052009-10-06 11:31:09 -07002677
Yan, Zhenged75ec22012-11-19 10:49:06 +08002678 list_splice_init(head, &tmp_list);
2679
2680 while (!list_empty(&tmp_list)) {
2681 req = list_entry(tmp_list.next,
2682 struct ceph_mds_request, r_wait);
Sage Weil2f2dc052009-10-06 11:31:09 -07002683 list_del_init(&req->r_wait);
Sage Weil7971bd92013-05-01 21:15:58 -07002684 dout(" wake request %p tid %llu\n", req, req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002685 __do_request(mdsc, req);
2686 }
2687}
2688
2689/*
2690 * Wake up threads with requests pending for @mds, so that they can
Sage Weil29790f22010-03-18 14:45:05 -07002691 * resubmit their requests to a possibly different mds.
Sage Weil2f2dc052009-10-06 11:31:09 -07002692 */
Sage Weil29790f22010-03-18 14:45:05 -07002693static void kick_requests(struct ceph_mds_client *mdsc, int mds)
Sage Weil2f2dc052009-10-06 11:31:09 -07002694{
Sage Weil44ca18f2010-02-15 12:08:46 -08002695 struct ceph_mds_request *req;
Yan, Zheng282c1052014-07-30 10:12:47 +08002696 struct rb_node *p = rb_first(&mdsc->request_tree);
Sage Weil2f2dc052009-10-06 11:31:09 -07002697
2698 dout("kick_requests mds%d\n", mds);
Yan, Zheng282c1052014-07-30 10:12:47 +08002699 while (p) {
Sage Weil44ca18f2010-02-15 12:08:46 -08002700 req = rb_entry(p, struct ceph_mds_request, r_node);
Yan, Zheng282c1052014-07-30 10:12:47 +08002701 p = rb_next(p);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002702 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Sage Weil44ca18f2010-02-15 12:08:46 -08002703 continue;
Yan, Zheng3de22be2015-02-04 14:26:22 +08002704 if (req->r_attempts > 0)
2705 continue; /* only new requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08002706 if (req->r_session &&
2707 req->r_session->s_mds == mds) {
2708 dout(" kicking tid %llu\n", req->r_tid);
Yan, Zheng03974e82014-09-11 14:28:56 +08002709 list_del_init(&req->r_wait);
Sage Weil44ca18f2010-02-15 12:08:46 -08002710 __do_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002711 }
2712 }
2713}
2714
Jeff Layton86bda532019-04-02 09:24:36 -04002715int ceph_mdsc_submit_request(struct ceph_mds_client *mdsc, struct inode *dir,
Sage Weil2f2dc052009-10-06 11:31:09 -07002716 struct ceph_mds_request *req)
2717{
Jeff Layton86bda532019-04-02 09:24:36 -04002718 int err;
2719
2720 /* take CAP_PIN refs for r_inode, r_parent, r_old_dentry */
2721 if (req->r_inode)
2722 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
Jeff Layton9c1c2b32019-04-03 13:16:01 -04002723 if (req->r_parent) {
Jeff Layton86bda532019-04-02 09:24:36 -04002724 ceph_get_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
Jeff Layton9c1c2b32019-04-03 13:16:01 -04002725 ihold(req->r_parent);
2726 }
Jeff Layton86bda532019-04-02 09:24:36 -04002727 if (req->r_old_dentry_dir)
2728 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
2729 CEPH_CAP_PIN);
2730
2731 dout("submit_request on %p for inode %p\n", req, dir);
Sage Weil2f2dc052009-10-06 11:31:09 -07002732 mutex_lock(&mdsc->mutex);
Jeff Layton86bda532019-04-02 09:24:36 -04002733 __register_request(mdsc, req, dir);
Sage Weil2f2dc052009-10-06 11:31:09 -07002734 __do_request(mdsc, req);
Jeff Layton86bda532019-04-02 09:24:36 -04002735 err = req->r_err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002736 mutex_unlock(&mdsc->mutex);
Jeff Layton86bda532019-04-02 09:24:36 -04002737 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002738}
2739
Jeff Layton8340f222019-04-02 12:34:38 -04002740static int ceph_mdsc_wait_request(struct ceph_mds_client *mdsc,
2741 struct ceph_mds_request *req)
Sage Weil2f2dc052009-10-06 11:31:09 -07002742{
2743 int err;
2744
Sage Weile1518c72010-05-13 11:19:06 -07002745 /* wait */
Sage Weile1518c72010-05-13 11:19:06 -07002746 dout("do_request waiting\n");
Ilya Dryomov5be73032015-05-19 12:05:38 +03002747 if (!req->r_timeout && req->r_wait_for_completion) {
Yan, Zheng9280be22014-10-14 10:33:35 +08002748 err = req->r_wait_for_completion(mdsc, req);
Sage Weile1518c72010-05-13 11:19:06 -07002749 } else {
Ilya Dryomov5be73032015-05-19 12:05:38 +03002750 long timeleft = wait_for_completion_killable_timeout(
2751 &req->r_completion,
2752 ceph_timeout_jiffies(req->r_timeout));
2753 if (timeleft > 0)
2754 err = 0;
2755 else if (!timeleft)
Xiubo Li8ccf7fc2020-02-23 22:23:11 -05002756 err = -ETIMEDOUT; /* timed out */
Ilya Dryomov5be73032015-05-19 12:05:38 +03002757 else
2758 err = timeleft; /* killed */
Sage Weile1518c72010-05-13 11:19:06 -07002759 }
2760 dout("do_request waited, got %d\n", err);
2761 mutex_lock(&mdsc->mutex);
2762
2763 /* only abort if we didn't race with a real reply */
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002764 if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
Sage Weile1518c72010-05-13 11:19:06 -07002765 err = le32_to_cpu(req->r_reply_info.head->result);
2766 } else if (err < 0) {
2767 dout("aborted request %lld with %d\n", req->r_tid, err);
Sage Weilb4556392010-05-13 12:01:13 -07002768
2769 /*
2770 * ensure we aren't running concurrently with
2771 * ceph_fill_trace or ceph_readdir_prepopulate, which
2772 * rely on locks (dir mutex) held by our caller.
2773 */
2774 mutex_lock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07002775 req->r_err = err;
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002776 set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
Sage Weilb4556392010-05-13 12:01:13 -07002777 mutex_unlock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07002778
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002779 if (req->r_parent &&
Sage Weil167c9e32010-05-14 10:02:57 -07002780 (req->r_op & CEPH_MDS_OP_WRITE))
2781 ceph_invalidate_dir_request(req);
Sage Weile1518c72010-05-13 11:19:06 -07002782 } else {
2783 err = req->r_err;
2784 }
2785
Sage Weile1518c72010-05-13 11:19:06 -07002786 mutex_unlock(&mdsc->mutex);
Jeff Layton8340f222019-04-02 12:34:38 -04002787 return err;
2788}
2789
2790/*
2791 * Synchrously perform an mds request. Take care of all of the
2792 * session setup, forwarding, retry details.
2793 */
2794int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
2795 struct inode *dir,
2796 struct ceph_mds_request *req)
2797{
2798 int err;
2799
2800 dout("do_request on %p\n", req);
2801
2802 /* issue */
2803 err = ceph_mdsc_submit_request(mdsc, dir, req);
2804 if (!err)
2805 err = ceph_mdsc_wait_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002806 dout("do_request %p done, result %d\n", req, err);
2807 return err;
2808}
2809
2810/*
Yan, Zheng2f276c52013-03-13 19:44:32 +08002811 * Invalidate dir's completeness, dentry lease state on an aborted MDS
Sage Weil167c9e32010-05-14 10:02:57 -07002812 * namespace request.
2813 */
2814void ceph_invalidate_dir_request(struct ceph_mds_request *req)
2815{
Yan, Zheng8d8f3712017-11-24 11:51:32 +08002816 struct inode *dir = req->r_parent;
2817 struct inode *old_dir = req->r_old_dentry_dir;
Sage Weil167c9e32010-05-14 10:02:57 -07002818
Yan, Zheng8d8f3712017-11-24 11:51:32 +08002819 dout("invalidate_dir_request %p %p (complete, lease(s))\n", dir, old_dir);
Sage Weil167c9e32010-05-14 10:02:57 -07002820
Yan, Zheng8d8f3712017-11-24 11:51:32 +08002821 ceph_dir_clear_complete(dir);
2822 if (old_dir)
2823 ceph_dir_clear_complete(old_dir);
Sage Weil167c9e32010-05-14 10:02:57 -07002824 if (req->r_dentry)
2825 ceph_invalidate_dentry_lease(req->r_dentry);
2826 if (req->r_old_dentry)
2827 ceph_invalidate_dentry_lease(req->r_old_dentry);
2828}
2829
2830/*
Sage Weil2f2dc052009-10-06 11:31:09 -07002831 * Handle mds reply.
2832 *
2833 * We take the session mutex and parse and process the reply immediately.
2834 * This preserves the logical ordering of replies, capabilities, etc., sent
2835 * by the MDS as they are applied to our local cache.
2836 */
2837static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2838{
2839 struct ceph_mds_client *mdsc = session->s_mdsc;
2840 struct ceph_mds_request *req;
2841 struct ceph_mds_reply_head *head = msg->front.iov_base;
2842 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
Yan, Zheng982d6012014-12-23 15:30:54 +08002843 struct ceph_snap_realm *realm;
Sage Weil2f2dc052009-10-06 11:31:09 -07002844 u64 tid;
2845 int err, result;
Sage Weil2600d2d2010-02-22 15:12:16 -08002846 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002847
Sage Weil2f2dc052009-10-06 11:31:09 -07002848 if (msg->front.iov_len < sizeof(*head)) {
2849 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002850 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002851 return;
2852 }
2853
2854 /* get request, session */
Sage Weil6df058c2009-12-22 11:24:33 -08002855 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002856 mutex_lock(&mdsc->mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02002857 req = lookup_get_request(mdsc, tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002858 if (!req) {
2859 dout("handle_reply on unknown tid %llu\n", tid);
2860 mutex_unlock(&mdsc->mutex);
2861 return;
2862 }
2863 dout("handle_reply %p\n", req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002864
2865 /* correct session? */
Sage Weild96d6042010-03-20 20:50:58 -07002866 if (req->r_session != session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002867 pr_err("mdsc_handle_reply got %llu on session mds%d"
2868 " not mds%d\n", tid, session->s_mds,
2869 req->r_session ? req->r_session->s_mds : -1);
2870 mutex_unlock(&mdsc->mutex);
2871 goto out;
2872 }
2873
2874 /* dup? */
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002875 if ((test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags) && !head->safe) ||
2876 (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags) && head->safe)) {
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07002877 pr_warn("got a dup %s reply on %llu from mds%d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002878 head->safe ? "safe" : "unsafe", tid, mds);
2879 mutex_unlock(&mdsc->mutex);
2880 goto out;
2881 }
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002882 if (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags)) {
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07002883 pr_warn("got unsafe after safe on %llu from mds%d\n",
Sage Weil85792d0d2010-05-13 09:06:02 -07002884 tid, mds);
2885 mutex_unlock(&mdsc->mutex);
2886 goto out;
2887 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002888
2889 result = le32_to_cpu(head->result);
2890
2891 /*
Greg Farnume55b71f2010-06-22 15:58:01 -07002892 * Handle an ESTALE
2893 * if we're not talking to the authority, send to them
2894 * if the authority has changed while we weren't looking,
2895 * send to new authority
2896 * Otherwise we just have to return an ESTALE
Sage Weil2f2dc052009-10-06 11:31:09 -07002897 */
2898 if (result == -ESTALE) {
Chengguang Xu4c069a52018-01-30 16:29:17 +08002899 dout("got ESTALE on request %llu\n", req->r_tid);
Yan, Zheng51da8e82014-07-14 10:48:29 +08002900 req->r_resend_mds = -1;
Yan, Zhengca18bed2013-11-22 14:21:44 +08002901 if (req->r_direct_mode != USE_AUTH_MDS) {
Chengguang Xu4c069a52018-01-30 16:29:17 +08002902 dout("not using auth, setting for that now\n");
Greg Farnume55b71f2010-06-22 15:58:01 -07002903 req->r_direct_mode = USE_AUTH_MDS;
Sage Weil2f2dc052009-10-06 11:31:09 -07002904 __do_request(mdsc, req);
2905 mutex_unlock(&mdsc->mutex);
2906 goto out;
Greg Farnume55b71f2010-06-22 15:58:01 -07002907 } else {
Xiubo Lic4853e92019-12-09 07:47:15 -05002908 int mds = __choose_mds(mdsc, req, NULL);
Yan, Zhengca18bed2013-11-22 14:21:44 +08002909 if (mds >= 0 && mds != req->r_session->s_mds) {
Chengguang Xu4c069a52018-01-30 16:29:17 +08002910 dout("but auth changed, so resending\n");
Greg Farnume55b71f2010-06-22 15:58:01 -07002911 __do_request(mdsc, req);
2912 mutex_unlock(&mdsc->mutex);
2913 goto out;
2914 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002915 }
Chengguang Xu4c069a52018-01-30 16:29:17 +08002916 dout("have to return ESTALE on request %llu\n", req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002917 }
2918
Greg Farnume55b71f2010-06-22 15:58:01 -07002919
Sage Weil2f2dc052009-10-06 11:31:09 -07002920 if (head->safe) {
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002921 set_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags);
Sage Weil2f2dc052009-10-06 11:31:09 -07002922 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002923
Xiubo Li07edc052019-12-04 01:27:18 -05002924 /* last request during umount? */
2925 if (mdsc->stopping && !__get_oldest_req(mdsc))
2926 complete_all(&mdsc->safe_umount_waiters);
2927
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002928 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002929 /*
2930 * We already handled the unsafe response, now do the
2931 * cleanup. No need to examine the response; the MDS
2932 * doesn't include any result info in the safe
2933 * response. And even if it did, there is nothing
2934 * useful we could do with a revised return value.
2935 */
2936 dout("got safe reply %llu, mds%d\n", tid, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002937
Sage Weil2f2dc052009-10-06 11:31:09 -07002938 mutex_unlock(&mdsc->mutex);
2939 goto out;
2940 }
Sage Weile1518c72010-05-13 11:19:06 -07002941 } else {
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002942 set_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags);
Sage Weil2f2dc052009-10-06 11:31:09 -07002943 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
2944 }
2945
2946 dout("handle_reply tid %lld result %d\n", tid, result);
2947 rinfo = &req->r_reply_info;
Yan, Zhengb37fe1f2019-01-09 10:10:17 +08002948 if (test_bit(CEPHFS_FEATURE_REPLY_ENCODING, &session->s_features))
2949 err = parse_reply_info(msg, rinfo, (u64)-1);
2950 else
2951 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
Sage Weil2f2dc052009-10-06 11:31:09 -07002952 mutex_unlock(&mdsc->mutex);
2953
2954 mutex_lock(&session->s_mutex);
2955 if (err < 0) {
Herb Shiu25933ab2010-12-01 14:14:38 -08002956 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002957 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002958 goto out_err;
2959 }
2960
2961 /* snap trace */
Yan, Zheng982d6012014-12-23 15:30:54 +08002962 realm = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07002963 if (rinfo->snapblob_len) {
2964 down_write(&mdsc->snap_rwsem);
2965 ceph_update_snap_trace(mdsc, rinfo->snapblob,
Yan, Zheng982d6012014-12-23 15:30:54 +08002966 rinfo->snapblob + rinfo->snapblob_len,
2967 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP,
2968 &realm);
Sage Weil2f2dc052009-10-06 11:31:09 -07002969 downgrade_write(&mdsc->snap_rwsem);
2970 } else {
2971 down_read(&mdsc->snap_rwsem);
2972 }
2973
2974 /* insert trace into our cache */
Sage Weilb4556392010-05-13 12:01:13 -07002975 mutex_lock(&req->r_fill_mutex);
Yan, Zheng315f2402016-03-07 10:34:50 +08002976 current->journal_info = req;
Jeff Laytonf5a03b02017-01-31 11:06:13 -05002977 err = ceph_fill_trace(mdsc->fsc->sb, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002978 if (err == 0) {
Sam Lang6e8575f2012-12-28 09:56:46 -08002979 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
Yan, Zheng81c6aea2013-09-18 09:44:13 +08002980 req->r_op == CEPH_MDS_OP_LSSNAP))
Sage Weil2f2dc052009-10-06 11:31:09 -07002981 ceph_readdir_prepopulate(req, req->r_session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002982 }
Yan, Zheng315f2402016-03-07 10:34:50 +08002983 current->journal_info = NULL;
Sage Weilb4556392010-05-13 12:01:13 -07002984 mutex_unlock(&req->r_fill_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002985
2986 up_read(&mdsc->snap_rwsem);
Yan, Zheng982d6012014-12-23 15:30:54 +08002987 if (realm)
2988 ceph_put_snap_realm(mdsc, realm);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002989
Yan, Zhengfe330322019-02-01 14:57:15 +08002990 if (err == 0) {
2991 if (req->r_target_inode &&
2992 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
2993 struct ceph_inode_info *ci =
2994 ceph_inode(req->r_target_inode);
2995 spin_lock(&ci->i_unsafe_lock);
2996 list_add_tail(&req->r_unsafe_target_item,
2997 &ci->i_unsafe_iops);
2998 spin_unlock(&ci->i_unsafe_lock);
2999 }
3000
3001 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08003002 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003003out_err:
Sage Weile1518c72010-05-13 11:19:06 -07003004 mutex_lock(&mdsc->mutex);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003005 if (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
Sage Weile1518c72010-05-13 11:19:06 -07003006 if (err) {
3007 req->r_err = err;
3008 } else {
Jianpeng Ma5fdb1382015-08-18 10:30:38 +08003009 req->r_reply = ceph_msg_get(msg);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003010 set_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags);
Sage Weile1518c72010-05-13 11:19:06 -07003011 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003012 } else {
Sage Weile1518c72010-05-13 11:19:06 -07003013 dout("reply arrived after request %lld was aborted\n", tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07003014 }
Sage Weile1518c72010-05-13 11:19:06 -07003015 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003016
Sage Weil2f2dc052009-10-06 11:31:09 -07003017 mutex_unlock(&session->s_mutex);
3018
3019 /* kick calling process */
3020 complete_request(mdsc, req);
3021out:
3022 ceph_mdsc_put_request(req);
3023 return;
3024}
3025
3026
3027
3028/*
3029 * handle mds notification that our request has been forwarded.
3030 */
Sage Weil2600d2d2010-02-22 15:12:16 -08003031static void handle_forward(struct ceph_mds_client *mdsc,
3032 struct ceph_mds_session *session,
3033 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07003034{
3035 struct ceph_mds_request *req;
Sage Weila1ea7872010-02-23 14:02:44 -08003036 u64 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07003037 u32 next_mds;
3038 u32 fwd_seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003039 int err = -EINVAL;
3040 void *p = msg->front.iov_base;
3041 void *end = p + msg->front.iov_len;
Sage Weil2f2dc052009-10-06 11:31:09 -07003042
Sage Weila1ea7872010-02-23 14:02:44 -08003043 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07003044 next_mds = ceph_decode_32(&p);
3045 fwd_seq = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07003046
3047 mutex_lock(&mdsc->mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02003048 req = lookup_get_request(mdsc, tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07003049 if (!req) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07003050 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003051 goto out; /* dup reply? */
3052 }
3053
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003054 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07003055 dout("forward tid %llu aborted, unregistering\n", tid);
3056 __unregister_request(mdsc, req);
3057 } else if (fwd_seq <= req->r_num_fwd) {
3058 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07003059 tid, next_mds, req->r_num_fwd, fwd_seq);
3060 } else {
3061 /* resend. forward race not possible; mds would drop */
Sage Weil2a8e5e32010-05-28 16:43:16 -07003062 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
3063 BUG_ON(req->r_err);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003064 BUG_ON(test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags));
Yan, Zheng3de22be2015-02-04 14:26:22 +08003065 req->r_attempts = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003066 req->r_num_fwd = fwd_seq;
3067 req->r_resend_mds = next_mds;
3068 put_request_session(req);
3069 __do_request(mdsc, req);
3070 }
3071 ceph_mdsc_put_request(req);
3072out:
3073 mutex_unlock(&mdsc->mutex);
3074 return;
3075
3076bad:
3077 pr_err("mdsc_handle_forward decode error err=%d\n", err);
3078}
3079
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003080static int __decode_session_metadata(void **p, void *end,
3081 bool *blacklisted)
Yan, Zheng84bf3952018-12-21 17:41:39 +08003082{
3083 /* map<string,string> */
3084 u32 n;
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003085 bool err_str;
Yan, Zheng84bf3952018-12-21 17:41:39 +08003086 ceph_decode_32_safe(p, end, n, bad);
3087 while (n-- > 0) {
3088 u32 len;
3089 ceph_decode_32_safe(p, end, len, bad);
3090 ceph_decode_need(p, end, len, bad);
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003091 err_str = !strncmp(*p, "error_string", len);
Yan, Zheng84bf3952018-12-21 17:41:39 +08003092 *p += len;
3093 ceph_decode_32_safe(p, end, len, bad);
3094 ceph_decode_need(p, end, len, bad);
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003095 if (err_str && strnstr(*p, "blacklisted", len))
3096 *blacklisted = true;
Yan, Zheng84bf3952018-12-21 17:41:39 +08003097 *p += len;
3098 }
3099 return 0;
3100bad:
3101 return -1;
3102}
3103
Sage Weil2f2dc052009-10-06 11:31:09 -07003104/*
3105 * handle a mds session control message
3106 */
3107static void handle_session(struct ceph_mds_session *session,
3108 struct ceph_msg *msg)
3109{
3110 struct ceph_mds_client *mdsc = session->s_mdsc;
Yan, Zheng84bf3952018-12-21 17:41:39 +08003111 int mds = session->s_mds;
3112 int msg_version = le16_to_cpu(msg->hdr.version);
3113 void *p = msg->front.iov_base;
3114 void *end = p + msg->front.iov_len;
3115 struct ceph_mds_session_head *h;
Sage Weil2f2dc052009-10-06 11:31:09 -07003116 u32 op;
3117 u64 seq;
Yan, Zheng84bf3952018-12-21 17:41:39 +08003118 unsigned long features = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003119 int wake = 0;
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003120 bool blacklisted = false;
Sage Weil2f2dc052009-10-06 11:31:09 -07003121
Sage Weil2f2dc052009-10-06 11:31:09 -07003122 /* decode */
Yan, Zheng84bf3952018-12-21 17:41:39 +08003123 ceph_decode_need(&p, end, sizeof(*h), bad);
3124 h = p;
3125 p += sizeof(*h);
3126
Sage Weil2f2dc052009-10-06 11:31:09 -07003127 op = le32_to_cpu(h->op);
3128 seq = le64_to_cpu(h->seq);
3129
Yan, Zheng84bf3952018-12-21 17:41:39 +08003130 if (msg_version >= 3) {
3131 u32 len;
3132 /* version >= 2, metadata */
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003133 if (__decode_session_metadata(&p, end, &blacklisted) < 0)
Yan, Zheng84bf3952018-12-21 17:41:39 +08003134 goto bad;
3135 /* version >= 3, feature bits */
3136 ceph_decode_32_safe(&p, end, len, bad);
3137 ceph_decode_need(&p, end, len, bad);
3138 memcpy(&features, p, min_t(size_t, len, sizeof(features)));
3139 p += len;
3140 }
3141
Sage Weil2f2dc052009-10-06 11:31:09 -07003142 mutex_lock(&mdsc->mutex);
Yan, Zheng0a07fc82017-03-29 15:30:24 +08003143 if (op == CEPH_SESSION_CLOSE) {
Xiubo Li5b3248c2019-12-19 19:44:09 -05003144 ceph_get_mds_session(session);
Sage Weil2600d2d2010-02-22 15:12:16 -08003145 __unregister_session(mdsc, session);
Yan, Zheng0a07fc82017-03-29 15:30:24 +08003146 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003147 /* FIXME: this ttl calculation is generous */
3148 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
3149 mutex_unlock(&mdsc->mutex);
3150
3151 mutex_lock(&session->s_mutex);
3152
3153 dout("handle_session mds%d %s %p state %s seq %llu\n",
3154 mds, ceph_session_op_name(op), session,
John Spraya687eca2014-09-19 13:51:08 +01003155 ceph_session_state_name(session->s_state), seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003156
3157 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
3158 session->s_state = CEPH_MDS_SESSION_OPEN;
3159 pr_info("mds%d came back\n", session->s_mds);
3160 }
3161
3162 switch (op) {
3163 case CEPH_SESSION_OPEN:
Sage Weil29790f22010-03-18 14:45:05 -07003164 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
3165 pr_info("mds%d reconnect success\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003166 session->s_state = CEPH_MDS_SESSION_OPEN;
Yan, Zheng84bf3952018-12-21 17:41:39 +08003167 session->s_features = features;
Sage Weil2f2dc052009-10-06 11:31:09 -07003168 renewed_caps(mdsc, session, 0);
3169 wake = 1;
3170 if (mdsc->stopping)
3171 __close_session(mdsc, session);
3172 break;
3173
3174 case CEPH_SESSION_RENEWCAPS:
3175 if (session->s_renew_seq == seq)
3176 renewed_caps(mdsc, session, 1);
3177 break;
3178
3179 case CEPH_SESSION_CLOSE:
Sage Weil29790f22010-03-18 14:45:05 -07003180 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
3181 pr_info("mds%d reconnect denied\n", session->s_mds);
Xiubo Li4d681c22019-12-05 22:35:51 -05003182 session->s_state = CEPH_MDS_SESSION_CLOSED;
Yan, Zheng1c841a92015-03-24 20:15:36 +08003183 cleanup_session_requests(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003184 remove_session_caps(session);
Yan, Zheng656e4382014-09-11 14:25:18 +08003185 wake = 2; /* for good measure */
Sage Weilf3c60c52010-08-11 14:51:23 -07003186 wake_up_all(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003187 break;
3188
3189 case CEPH_SESSION_STALE:
3190 pr_info("mds%d caps went stale, renewing\n",
3191 session->s_mds);
Alex Elderd8fb02a2012-01-12 17:48:10 -08003192 spin_lock(&session->s_gen_ttl_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07003193 session->s_cap_gen++;
Alex Elder1ce208a2012-01-12 17:48:11 -08003194 session->s_cap_ttl = jiffies - 1;
Alex Elderd8fb02a2012-01-12 17:48:10 -08003195 spin_unlock(&session->s_gen_ttl_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07003196 send_renew_caps(mdsc, session);
3197 break;
3198
3199 case CEPH_SESSION_RECALL_STATE:
Zhi Zhange30ee582018-01-24 21:24:33 +08003200 ceph_trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
Sage Weil2f2dc052009-10-06 11:31:09 -07003201 break;
3202
Yan, Zheng186e4f72013-11-22 14:48:37 +08003203 case CEPH_SESSION_FLUSHMSG:
3204 send_flushmsg_ack(mdsc, session, seq);
3205 break;
3206
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08003207 case CEPH_SESSION_FORCE_RO:
3208 dout("force_session_readonly %p\n", session);
3209 spin_lock(&session->s_cap_lock);
3210 session->s_readonly = true;
3211 spin_unlock(&session->s_cap_lock);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08003212 wake_up_session_caps(session, FORCE_RO);
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08003213 break;
3214
Yan, Zhengfcff4152016-09-14 16:39:51 +08003215 case CEPH_SESSION_REJECT:
3216 WARN_ON(session->s_state != CEPH_MDS_SESSION_OPENING);
3217 pr_info("mds%d rejected session\n", session->s_mds);
3218 session->s_state = CEPH_MDS_SESSION_REJECTED;
3219 cleanup_session_requests(mdsc, session);
3220 remove_session_caps(session);
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003221 if (blacklisted)
3222 mdsc->fsc->blacklisted = true;
Yan, Zhengfcff4152016-09-14 16:39:51 +08003223 wake = 2; /* for good measure */
3224 break;
3225
Sage Weil2f2dc052009-10-06 11:31:09 -07003226 default:
3227 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
3228 WARN_ON(1);
3229 }
3230
3231 mutex_unlock(&session->s_mutex);
3232 if (wake) {
3233 mutex_lock(&mdsc->mutex);
3234 __wake_requests(mdsc, &session->s_waiting);
Yan, Zheng656e4382014-09-11 14:25:18 +08003235 if (wake == 2)
3236 kick_requests(mdsc, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003237 mutex_unlock(&mdsc->mutex);
3238 }
Yan, Zheng0a07fc82017-03-29 15:30:24 +08003239 if (op == CEPH_SESSION_CLOSE)
3240 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003241 return;
3242
3243bad:
3244 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
3245 (int)msg->front.iov_len);
Sage Weil9ec7cab2009-12-14 15:13:47 -08003246 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003247 return;
3248}
3249
Sage Weil2f2dc052009-10-06 11:31:09 -07003250/*
3251 * called under session->mutex.
3252 */
3253static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
3254 struct ceph_mds_session *session)
3255{
3256 struct ceph_mds_request *req, *nreq;
Yan, Zheng3de22be2015-02-04 14:26:22 +08003257 struct rb_node *p;
Sage Weil2f2dc052009-10-06 11:31:09 -07003258
3259 dout("replay_unsafe_requests mds%d\n", session->s_mds);
3260
3261 mutex_lock(&mdsc->mutex);
Xiubo Li9cf54562019-12-05 20:50:21 -05003262 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item)
3263 __send_request(mdsc, session, req, true);
Yan, Zheng3de22be2015-02-04 14:26:22 +08003264
3265 /*
3266 * also re-send old requests when MDS enters reconnect stage. So that MDS
3267 * can process completed request in clientreplay stage.
3268 */
3269 p = rb_first(&mdsc->request_tree);
3270 while (p) {
3271 req = rb_entry(p, struct ceph_mds_request, r_node);
3272 p = rb_next(p);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003273 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Yan, Zheng3de22be2015-02-04 14:26:22 +08003274 continue;
3275 if (req->r_attempts == 0)
3276 continue; /* only old requests */
3277 if (req->r_session &&
Xiubo Li9cf54562019-12-05 20:50:21 -05003278 req->r_session->s_mds == session->s_mds)
3279 __send_request(mdsc, session, req, true);
Yan, Zheng3de22be2015-02-04 14:26:22 +08003280 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003281 mutex_unlock(&mdsc->mutex);
3282}
3283
Yan, Zheng81c5a142019-01-01 16:28:33 +08003284static int send_reconnect_partial(struct ceph_reconnect_state *recon_state)
3285{
3286 struct ceph_msg *reply;
3287 struct ceph_pagelist *_pagelist;
3288 struct page *page;
3289 __le32 *addr;
3290 int err = -ENOMEM;
3291
3292 if (!recon_state->allow_multi)
3293 return -ENOSPC;
3294
3295 /* can't handle message that contains both caps and realm */
3296 BUG_ON(!recon_state->nr_caps == !recon_state->nr_realms);
3297
3298 /* pre-allocate new pagelist */
3299 _pagelist = ceph_pagelist_alloc(GFP_NOFS);
3300 if (!_pagelist)
3301 return -ENOMEM;
3302
3303 reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false);
3304 if (!reply)
3305 goto fail_msg;
3306
3307 /* placeholder for nr_caps */
3308 err = ceph_pagelist_encode_32(_pagelist, 0);
3309 if (err < 0)
3310 goto fail;
3311
3312 if (recon_state->nr_caps) {
3313 /* currently encoding caps */
3314 err = ceph_pagelist_encode_32(recon_state->pagelist, 0);
3315 if (err)
3316 goto fail;
3317 } else {
3318 /* placeholder for nr_realms (currently encoding relams) */
3319 err = ceph_pagelist_encode_32(_pagelist, 0);
3320 if (err < 0)
3321 goto fail;
3322 }
3323
3324 err = ceph_pagelist_encode_8(recon_state->pagelist, 1);
3325 if (err)
3326 goto fail;
3327
3328 page = list_first_entry(&recon_state->pagelist->head, struct page, lru);
3329 addr = kmap_atomic(page);
3330 if (recon_state->nr_caps) {
3331 /* currently encoding caps */
3332 *addr = cpu_to_le32(recon_state->nr_caps);
3333 } else {
3334 /* currently encoding relams */
3335 *(addr + 1) = cpu_to_le32(recon_state->nr_realms);
3336 }
3337 kunmap_atomic(addr);
3338
3339 reply->hdr.version = cpu_to_le16(5);
3340 reply->hdr.compat_version = cpu_to_le16(4);
3341
3342 reply->hdr.data_len = cpu_to_le32(recon_state->pagelist->length);
3343 ceph_msg_data_add_pagelist(reply, recon_state->pagelist);
3344
3345 ceph_con_send(&recon_state->session->s_con, reply);
3346 ceph_pagelist_release(recon_state->pagelist);
3347
3348 recon_state->pagelist = _pagelist;
3349 recon_state->nr_caps = 0;
3350 recon_state->nr_realms = 0;
3351 recon_state->msg_version = 5;
3352 return 0;
3353fail:
3354 ceph_msg_put(reply);
3355fail_msg:
3356 ceph_pagelist_release(_pagelist);
3357 return err;
3358}
3359
Sage Weil2f2dc052009-10-06 11:31:09 -07003360/*
3361 * Encode information about a cap for a reconnect with the MDS.
3362 */
Sage Weil2f2dc052009-10-06 11:31:09 -07003363static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
3364 void *arg)
3365{
Sage Weil20cb34a2010-05-12 15:21:32 -07003366 union {
3367 struct ceph_mds_cap_reconnect v2;
3368 struct ceph_mds_cap_reconnect_v1 v1;
3369 } rec;
Yan, Zhengb3f8d682017-09-11 10:58:55 +08003370 struct ceph_inode_info *ci = cap->ci;
Sage Weil20cb34a2010-05-12 15:21:32 -07003371 struct ceph_reconnect_state *recon_state = arg;
3372 struct ceph_pagelist *pagelist = recon_state->pagelist;
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003373 int err;
Yan, Zheng3469ed02016-07-05 09:32:31 +08003374 u64 snap_follows;
Sage Weil2f2dc052009-10-06 11:31:09 -07003375
Sage Weil2f2dc052009-10-06 11:31:09 -07003376 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
3377 inode, ceph_vinop(inode), cap, cap->cap_id,
3378 ceph_cap_string(cap->issued));
Sage Weil2f2dc052009-10-06 11:31:09 -07003379
Sage Weilbe655592011-11-30 09:47:09 -08003380 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07003381 cap->seq = 0; /* reset cap seq */
3382 cap->issue_seq = 0; /* and issue_seq */
Yan, Zheng667ca052013-05-31 16:25:36 +08003383 cap->mseq = 0; /* and migrate_seq */
Yan, Zheng99a9c272013-09-22 11:08:14 +08003384 cap->cap_gen = cap->session->s_cap_gen;
Sage Weil20cb34a2010-05-12 15:21:32 -07003385
Yan, Zheng121f22a2016-07-04 22:05:18 +08003386 if (recon_state->msg_version >= 2) {
Sage Weil20cb34a2010-05-12 15:21:32 -07003387 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
3388 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
3389 rec.v2.issued = cpu_to_le32(cap->issued);
3390 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003391 rec.v2.pathbase = 0;
Jeff Laytonec1dff22017-10-31 15:51:14 -04003392 rec.v2.flock_len = (__force __le32)
3393 ((ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) ? 0 : 1);
Sage Weil20cb34a2010-05-12 15:21:32 -07003394 } else {
3395 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
3396 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
3397 rec.v1.issued = cpu_to_le32(cap->issued);
3398 rec.v1.size = cpu_to_le64(inode->i_size);
Arnd Bergmann9bbeab42018-07-13 22:18:36 +02003399 ceph_encode_timespec64(&rec.v1.mtime, &inode->i_mtime);
3400 ceph_encode_timespec64(&rec.v1.atime, &inode->i_atime);
Sage Weil20cb34a2010-05-12 15:21:32 -07003401 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003402 rec.v1.pathbase = 0;
Sage Weil20cb34a2010-05-12 15:21:32 -07003403 }
Yan, Zheng3469ed02016-07-05 09:32:31 +08003404
3405 if (list_empty(&ci->i_cap_snaps)) {
Yan, Zheng92776fd2017-08-16 21:42:39 +08003406 snap_follows = ci->i_head_snapc ? ci->i_head_snapc->seq : 0;
Yan, Zheng3469ed02016-07-05 09:32:31 +08003407 } else {
3408 struct ceph_cap_snap *capsnap =
3409 list_first_entry(&ci->i_cap_snaps,
3410 struct ceph_cap_snap, ci_item);
3411 snap_follows = capsnap->follows;
Sage Weil93cea5b2009-12-23 12:21:51 -08003412 }
Sage Weilbe655592011-11-30 09:47:09 -08003413 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07003414
Yan, Zheng121f22a2016-07-04 22:05:18 +08003415 if (recon_state->msg_version >= 2) {
Greg Farnum40819f62010-08-02 15:34:23 -07003416 int num_fcntl_locks, num_flock_locks;
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003417 struct ceph_filelock *flocks = NULL;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003418 size_t struct_len, total_len = sizeof(u64);
Yan, Zheng121f22a2016-07-04 22:05:18 +08003419 u8 struct_v = 0;
Greg Farnum40819f62010-08-02 15:34:23 -07003420
Jim Schutt39be95e2013-05-15 13:03:35 -05003421encode_again:
Yan, Zhengb3f8d682017-09-11 10:58:55 +08003422 if (rec.v2.flock_len) {
3423 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
3424 } else {
3425 num_fcntl_locks = 0;
3426 num_flock_locks = 0;
3427 }
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003428 if (num_fcntl_locks + num_flock_locks > 0) {
Kees Cook6da2ec52018-06-12 13:55:00 -07003429 flocks = kmalloc_array(num_fcntl_locks + num_flock_locks,
3430 sizeof(struct ceph_filelock),
3431 GFP_NOFS);
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003432 if (!flocks) {
3433 err = -ENOMEM;
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003434 goto out_err;
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003435 }
3436 err = ceph_encode_locks_to_buffer(inode, flocks,
3437 num_fcntl_locks,
3438 num_flock_locks);
3439 if (err) {
3440 kfree(flocks);
3441 flocks = NULL;
3442 if (err == -ENOSPC)
3443 goto encode_again;
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003444 goto out_err;
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003445 }
3446 } else {
Jim Schutt39be95e2013-05-15 13:03:35 -05003447 kfree(flocks);
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003448 flocks = NULL;
Jim Schutt39be95e2013-05-15 13:03:35 -05003449 }
Yan, Zheng121f22a2016-07-04 22:05:18 +08003450
3451 if (recon_state->msg_version >= 3) {
3452 /* version, compat_version and struct_len */
Yan, Zheng81c5a142019-01-01 16:28:33 +08003453 total_len += 2 * sizeof(u8) + sizeof(u32);
Yan, Zheng3469ed02016-07-05 09:32:31 +08003454 struct_v = 2;
Yan, Zheng121f22a2016-07-04 22:05:18 +08003455 }
Jim Schutt39be95e2013-05-15 13:03:35 -05003456 /*
3457 * number of encoded locks is stable, so copy to pagelist
3458 */
Yan, Zheng121f22a2016-07-04 22:05:18 +08003459 struct_len = 2 * sizeof(u32) +
3460 (num_fcntl_locks + num_flock_locks) *
3461 sizeof(struct ceph_filelock);
3462 rec.v2.flock_len = cpu_to_le32(struct_len);
3463
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003464 struct_len += sizeof(u32) + sizeof(rec.v2);
Yan, Zheng121f22a2016-07-04 22:05:18 +08003465
Yan, Zheng3469ed02016-07-05 09:32:31 +08003466 if (struct_v >= 2)
3467 struct_len += sizeof(u64); /* snap_follows */
3468
Yan, Zheng121f22a2016-07-04 22:05:18 +08003469 total_len += struct_len;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003470
3471 if (pagelist->length + total_len > RECONNECT_MAX_SIZE) {
3472 err = send_reconnect_partial(recon_state);
3473 if (err)
3474 goto out_freeflocks;
3475 pagelist = recon_state->pagelist;
Yan, Zheng121f22a2016-07-04 22:05:18 +08003476 }
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003477
Yan, Zheng81c5a142019-01-01 16:28:33 +08003478 err = ceph_pagelist_reserve(pagelist, total_len);
3479 if (err)
3480 goto out_freeflocks;
3481
3482 ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003483 if (recon_state->msg_version >= 3) {
3484 ceph_pagelist_encode_8(pagelist, struct_v);
3485 ceph_pagelist_encode_8(pagelist, 1);
3486 ceph_pagelist_encode_32(pagelist, struct_len);
3487 }
3488 ceph_pagelist_encode_string(pagelist, NULL, 0);
3489 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v2));
3490 ceph_locks_to_pagelist(flocks, pagelist,
3491 num_fcntl_locks, num_flock_locks);
3492 if (struct_v >= 2)
3493 ceph_pagelist_encode_64(pagelist, snap_follows);
Yan, Zheng81c5a142019-01-01 16:28:33 +08003494out_freeflocks:
Jim Schutt39be95e2013-05-15 13:03:35 -05003495 kfree(flocks);
Sage Weil3612abb2010-09-07 15:59:27 -07003496 } else {
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003497 u64 pathbase = 0;
3498 int pathlen = 0;
3499 char *path = NULL;
3500 struct dentry *dentry;
3501
3502 dentry = d_find_alias(inode);
3503 if (dentry) {
3504 path = ceph_mdsc_build_path(dentry,
3505 &pathlen, &pathbase, 0);
3506 dput(dentry);
3507 if (IS_ERR(path)) {
3508 err = PTR_ERR(path);
3509 goto out_err;
3510 }
3511 rec.v1.pathbase = cpu_to_le64(pathbase);
Yan, Zheng121f22a2016-07-04 22:05:18 +08003512 }
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003513
3514 err = ceph_pagelist_reserve(pagelist,
Yan, Zheng81c5a142019-01-01 16:28:33 +08003515 sizeof(u64) + sizeof(u32) +
3516 pathlen + sizeof(rec.v1));
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003517 if (err) {
Yan, Zheng81c5a142019-01-01 16:28:33 +08003518 goto out_freepath;
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003519 }
3520
Yan, Zheng81c5a142019-01-01 16:28:33 +08003521 ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003522 ceph_pagelist_encode_string(pagelist, path, pathlen);
3523 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v1));
Yan, Zheng81c5a142019-01-01 16:28:33 +08003524out_freepath:
Jeff Laytonf77f21b2019-04-29 12:13:14 -04003525 ceph_mdsc_free_path(path, pathlen);
Greg Farnum40819f62010-08-02 15:34:23 -07003526 }
Yan, Zheng44c99752013-09-22 10:28:10 +08003527
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003528out_err:
Yan, Zheng81c5a142019-01-01 16:28:33 +08003529 if (err >= 0)
3530 recon_state->nr_caps++;
3531 return err;
3532}
3533
3534static int encode_snap_realms(struct ceph_mds_client *mdsc,
3535 struct ceph_reconnect_state *recon_state)
3536{
3537 struct rb_node *p;
3538 struct ceph_pagelist *pagelist = recon_state->pagelist;
3539 int err = 0;
3540
3541 if (recon_state->msg_version >= 4) {
3542 err = ceph_pagelist_encode_32(pagelist, mdsc->num_snap_realms);
3543 if (err < 0)
3544 goto fail;
3545 }
3546
3547 /*
3548 * snaprealms. we provide mds with the ino, seq (version), and
3549 * parent for all of our realms. If the mds has any newer info,
3550 * it will tell us.
3551 */
3552 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
3553 struct ceph_snap_realm *realm =
3554 rb_entry(p, struct ceph_snap_realm, node);
3555 struct ceph_mds_snaprealm_reconnect sr_rec;
3556
3557 if (recon_state->msg_version >= 4) {
3558 size_t need = sizeof(u8) * 2 + sizeof(u32) +
3559 sizeof(sr_rec);
3560
3561 if (pagelist->length + need > RECONNECT_MAX_SIZE) {
3562 err = send_reconnect_partial(recon_state);
3563 if (err)
3564 goto fail;
3565 pagelist = recon_state->pagelist;
3566 }
3567
3568 err = ceph_pagelist_reserve(pagelist, need);
3569 if (err)
3570 goto fail;
3571
3572 ceph_pagelist_encode_8(pagelist, 1);
3573 ceph_pagelist_encode_8(pagelist, 1);
3574 ceph_pagelist_encode_32(pagelist, sizeof(sr_rec));
3575 }
3576
3577 dout(" adding snap realm %llx seq %lld parent %llx\n",
3578 realm->ino, realm->seq, realm->parent_ino);
3579 sr_rec.ino = cpu_to_le64(realm->ino);
3580 sr_rec.seq = cpu_to_le64(realm->seq);
3581 sr_rec.parent = cpu_to_le64(realm->parent_ino);
3582
3583 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
3584 if (err)
3585 goto fail;
3586
3587 recon_state->nr_realms++;
3588 }
3589fail:
Sage Weil93cea5b2009-12-23 12:21:51 -08003590 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07003591}
3592
3593
3594/*
3595 * If an MDS fails and recovers, clients need to reconnect in order to
3596 * reestablish shared state. This includes all caps issued through
3597 * this session _and_ the snap_realm hierarchy. Because it's not
3598 * clear which snap realms the mds cares about, we send everything we
3599 * know about.. that ensures we'll then get any new info the
3600 * recovering MDS might have.
3601 *
3602 * This is a relatively heavyweight operation, but it's rare.
3603 *
3604 * called with mdsc->mutex held.
3605 */
Sage Weil34b6c852010-05-10 16:31:25 -07003606static void send_mds_reconnect(struct ceph_mds_client *mdsc,
3607 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07003608{
Sage Weil2f2dc052009-10-06 11:31:09 -07003609 struct ceph_msg *reply;
Sage Weil34b6c852010-05-10 16:31:25 -07003610 int mds = session->s_mds;
Sage Weil9abf82b2010-05-10 21:58:38 -07003611 int err = -ENOMEM;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003612 struct ceph_reconnect_state recon_state = {
3613 .session = session,
3614 };
Jeff Laytonc8a96a32017-10-19 08:53:58 -04003615 LIST_HEAD(dispose);
Sage Weil2f2dc052009-10-06 11:31:09 -07003616
Sage Weil34b6c852010-05-10 16:31:25 -07003617 pr_info("mds%d reconnect start\n", mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003618
Yan, Zheng81c5a142019-01-01 16:28:33 +08003619 recon_state.pagelist = ceph_pagelist_alloc(GFP_NOFS);
3620 if (!recon_state.pagelist)
Sage Weil93cea5b2009-12-23 12:21:51 -08003621 goto fail_nopagelist;
Sage Weil93cea5b2009-12-23 12:21:51 -08003622
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003623 reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07003624 if (!reply)
Sage Weil93cea5b2009-12-23 12:21:51 -08003625 goto fail_nomsg;
Sage Weil93cea5b2009-12-23 12:21:51 -08003626
Sage Weil34b6c852010-05-10 16:31:25 -07003627 mutex_lock(&session->s_mutex);
3628 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
3629 session->s_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003630
Sage Weil2f2dc052009-10-06 11:31:09 -07003631 dout("session %p state %s\n", session,
John Spraya687eca2014-09-19 13:51:08 +01003632 ceph_session_state_name(session->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07003633
Yan, Zheng99a9c272013-09-22 11:08:14 +08003634 spin_lock(&session->s_gen_ttl_lock);
3635 session->s_cap_gen++;
3636 spin_unlock(&session->s_gen_ttl_lock);
3637
3638 spin_lock(&session->s_cap_lock);
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08003639 /* don't know if session is readonly */
3640 session->s_readonly = 0;
Yan, Zheng99a9c272013-09-22 11:08:14 +08003641 /*
3642 * notify __ceph_remove_cap() that we are composing cap reconnect.
3643 * If a cap get released before being added to the cap reconnect,
3644 * __ceph_remove_cap() should skip queuing cap release.
3645 */
3646 session->s_cap_reconnect = 1;
Sage Weile01a5942010-05-10 15:36:44 -07003647 /* drop old cap expires; we're about to reestablish that state */
Jeff Laytonc8a96a32017-10-19 08:53:58 -04003648 detach_cap_releases(session, &dispose);
3649 spin_unlock(&session->s_cap_lock);
3650 dispose_cap_releases(mdsc, &dispose);
Sage Weile01a5942010-05-10 15:36:44 -07003651
Yan, Zheng5d233712014-09-10 16:56:23 +08003652 /* trim unused caps to reduce MDS's cache rejoin time */
Yan, Zhengc0bd50e2015-04-07 15:51:08 +08003653 if (mdsc->fsc->sb->s_root)
3654 shrink_dcache_parent(mdsc->fsc->sb->s_root);
Yan, Zheng5d233712014-09-10 16:56:23 +08003655
3656 ceph_con_close(&session->s_con);
3657 ceph_con_open(&session->s_con,
3658 CEPH_ENTITY_TYPE_MDS, mds,
3659 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
3660
3661 /* replay unsafe requests */
3662 replay_unsafe_requests(mdsc, session);
3663
Yan, Zheng81c5a142019-01-01 16:28:33 +08003664 ceph_early_kick_flushing_caps(mdsc, session);
3665
Yan, Zheng5d233712014-09-10 16:56:23 +08003666 down_read(&mdsc->snap_rwsem);
3667
Yan, Zheng81c5a142019-01-01 16:28:33 +08003668 /* placeholder for nr_caps */
3669 err = ceph_pagelist_encode_32(recon_state.pagelist, 0);
Sage Weil93cea5b2009-12-23 12:21:51 -08003670 if (err)
3671 goto fail;
Sage Weil20cb34a2010-05-12 15:21:32 -07003672
Yan, Zheng81c5a142019-01-01 16:28:33 +08003673 if (test_bit(CEPHFS_FEATURE_MULTI_RECONNECT, &session->s_features)) {
Yan, Zheng121f22a2016-07-04 22:05:18 +08003674 recon_state.msg_version = 3;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003675 recon_state.allow_multi = true;
3676 } else if (session->s_con.peer_features & CEPH_FEATURE_MDSENC) {
3677 recon_state.msg_version = 3;
3678 } else {
Ilya Dryomov23c625c2018-11-08 14:55:21 +01003679 recon_state.msg_version = 2;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003680 }
3681 /* trsaverse this session's caps */
Jeff Laytonf5d772692019-04-24 12:09:04 -04003682 err = ceph_iterate_session_caps(session, encode_caps_cb, &recon_state);
Sage Weil2f2dc052009-10-06 11:31:09 -07003683
Yan, Zheng99a9c272013-09-22 11:08:14 +08003684 spin_lock(&session->s_cap_lock);
3685 session->s_cap_reconnect = 0;
3686 spin_unlock(&session->s_cap_lock);
3687
Yan, Zheng81c5a142019-01-01 16:28:33 +08003688 if (err < 0)
3689 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07003690
Yan, Zheng81c5a142019-01-01 16:28:33 +08003691 /* check if all realms can be encoded into current message */
3692 if (mdsc->num_snap_realms) {
3693 size_t total_len =
3694 recon_state.pagelist->length +
3695 mdsc->num_snap_realms *
3696 sizeof(struct ceph_mds_snaprealm_reconnect);
3697 if (recon_state.msg_version >= 4) {
3698 /* number of realms */
3699 total_len += sizeof(u32);
3700 /* version, compat_version and struct_len */
3701 total_len += mdsc->num_snap_realms *
3702 (2 * sizeof(u8) + sizeof(u32));
3703 }
3704 if (total_len > RECONNECT_MAX_SIZE) {
3705 if (!recon_state.allow_multi) {
3706 err = -ENOSPC;
3707 goto fail;
3708 }
3709 if (recon_state.nr_caps) {
3710 err = send_reconnect_partial(&recon_state);
3711 if (err)
3712 goto fail;
3713 }
3714 recon_state.msg_version = 5;
3715 }
3716 }
3717
3718 err = encode_snap_realms(mdsc, &recon_state);
3719 if (err < 0)
3720 goto fail;
3721
3722 if (recon_state.msg_version >= 5) {
3723 err = ceph_pagelist_encode_8(recon_state.pagelist, 0);
3724 if (err < 0)
Sage Weil93cea5b2009-12-23 12:21:51 -08003725 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07003726 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003727
Yan, Zheng81c5a142019-01-01 16:28:33 +08003728 if (recon_state.nr_caps || recon_state.nr_realms) {
3729 struct page *page =
3730 list_first_entry(&recon_state.pagelist->head,
3731 struct page, lru);
Yan, Zheng44c99752013-09-22 10:28:10 +08003732 __le32 *addr = kmap_atomic(page);
Yan, Zheng81c5a142019-01-01 16:28:33 +08003733 if (recon_state.nr_caps) {
3734 WARN_ON(recon_state.nr_realms != mdsc->num_snap_realms);
3735 *addr = cpu_to_le32(recon_state.nr_caps);
3736 } else if (recon_state.msg_version >= 4) {
3737 *(addr + 1) = cpu_to_le32(recon_state.nr_realms);
3738 }
Yan, Zheng44c99752013-09-22 10:28:10 +08003739 kunmap_atomic(addr);
Alex Elderebf18f42013-03-04 22:29:57 -06003740 }
Yan, Zheng44c99752013-09-22 10:28:10 +08003741
Yan, Zheng81c5a142019-01-01 16:28:33 +08003742 reply->hdr.version = cpu_to_le16(recon_state.msg_version);
3743 if (recon_state.msg_version >= 4)
3744 reply->hdr.compat_version = cpu_to_le16(4);
Yan, Zhenge548e9b2015-06-10 15:17:56 +08003745
Yan, Zheng81c5a142019-01-01 16:28:33 +08003746 reply->hdr.data_len = cpu_to_le32(recon_state.pagelist->length);
3747 ceph_msg_data_add_pagelist(reply, recon_state.pagelist);
Yan, Zhenge548e9b2015-06-10 15:17:56 +08003748
Sage Weil2f2dc052009-10-06 11:31:09 -07003749 ceph_con_send(&session->s_con, reply);
3750
Sage Weil9abf82b2010-05-10 21:58:38 -07003751 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003752
Sage Weil9abf82b2010-05-10 21:58:38 -07003753 mutex_lock(&mdsc->mutex);
3754 __wake_requests(mdsc, &session->s_waiting);
3755 mutex_unlock(&mdsc->mutex);
3756
Sage Weil2f2dc052009-10-06 11:31:09 -07003757 up_read(&mdsc->snap_rwsem);
Yan, Zheng81c5a142019-01-01 16:28:33 +08003758 ceph_pagelist_release(recon_state.pagelist);
Sage Weil2f2dc052009-10-06 11:31:09 -07003759 return;
3760
Sage Weil93cea5b2009-12-23 12:21:51 -08003761fail:
Sage Weil2f2dc052009-10-06 11:31:09 -07003762 ceph_msg_put(reply);
Sage Weil9abf82b2010-05-10 21:58:38 -07003763 up_read(&mdsc->snap_rwsem);
3764 mutex_unlock(&session->s_mutex);
Sage Weil93cea5b2009-12-23 12:21:51 -08003765fail_nomsg:
Yan, Zheng81c5a142019-01-01 16:28:33 +08003766 ceph_pagelist_release(recon_state.pagelist);
Sage Weil93cea5b2009-12-23 12:21:51 -08003767fail_nopagelist:
Sage Weil9abf82b2010-05-10 21:58:38 -07003768 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
Sage Weil9abf82b2010-05-10 21:58:38 -07003769 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07003770}
3771
3772
3773/*
3774 * compare old and new mdsmaps, kicking requests
3775 * and closing out old connections as necessary
3776 *
3777 * called under mdsc->mutex.
3778 */
3779static void check_new_map(struct ceph_mds_client *mdsc,
3780 struct ceph_mdsmap *newmap,
3781 struct ceph_mdsmap *oldmap)
3782{
3783 int i;
3784 int oldstate, newstate;
3785 struct ceph_mds_session *s;
3786
3787 dout("check_new_map new %u old %u\n",
3788 newmap->m_epoch, oldmap->m_epoch);
3789
Xiubo Lib38c9eb2019-12-04 06:57:39 -05003790 for (i = 0; i < oldmap->possible_max_rank && i < mdsc->max_sessions; i++) {
Markus Elfringd37b1d92017-08-20 20:22:02 +02003791 if (!mdsc->sessions[i])
Sage Weil2f2dc052009-10-06 11:31:09 -07003792 continue;
3793 s = mdsc->sessions[i];
3794 oldstate = ceph_mdsmap_get_state(oldmap, i);
3795 newstate = ceph_mdsmap_get_state(newmap, i);
3796
Sage Weil0deb01c2010-06-17 14:19:01 -07003797 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07003798 i, ceph_mds_state_name(oldstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07003799 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
Sage Weil2f2dc052009-10-06 11:31:09 -07003800 ceph_mds_state_name(newstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07003801 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
John Spraya687eca2014-09-19 13:51:08 +01003802 ceph_session_state_name(s->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07003803
Xiubo Lib38c9eb2019-12-04 06:57:39 -05003804 if (i >= newmap->possible_max_rank) {
Yan, Zheng6f0f5972019-06-10 15:45:09 +08003805 /* force close session for stopped mds */
Xiubo Li5b3248c2019-12-19 19:44:09 -05003806 ceph_get_mds_session(s);
Yan, Zheng6f0f5972019-06-10 15:45:09 +08003807 __unregister_session(mdsc, s);
3808 __wake_requests(mdsc, &s->s_waiting);
3809 mutex_unlock(&mdsc->mutex);
3810
3811 mutex_lock(&s->s_mutex);
3812 cleanup_session_requests(mdsc, s);
3813 remove_session_caps(s);
3814 mutex_unlock(&s->s_mutex);
3815
3816 ceph_put_mds_session(s);
3817
3818 mutex_lock(&mdsc->mutex);
3819 kick_requests(mdsc, i);
3820 continue;
3821 }
3822
3823 if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
Sage Weil2f2dc052009-10-06 11:31:09 -07003824 ceph_mdsmap_get_addr(newmap, i),
3825 sizeof(struct ceph_entity_addr))) {
Yan, Zheng6f0f5972019-06-10 15:45:09 +08003826 /* just close it */
3827 mutex_unlock(&mdsc->mutex);
3828 mutex_lock(&s->s_mutex);
3829 mutex_lock(&mdsc->mutex);
3830 ceph_con_close(&s->s_con);
3831 mutex_unlock(&s->s_mutex);
3832 s->s_state = CEPH_MDS_SESSION_RESTARTING;
Sage Weil2f2dc052009-10-06 11:31:09 -07003833 } else if (oldstate == newstate) {
3834 continue; /* nothing new with this mds */
3835 }
3836
3837 /*
3838 * send reconnect?
3839 */
3840 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
Sage Weil34b6c852010-05-10 16:31:25 -07003841 newstate >= CEPH_MDS_STATE_RECONNECT) {
3842 mutex_unlock(&mdsc->mutex);
3843 send_mds_reconnect(mdsc, s);
3844 mutex_lock(&mdsc->mutex);
3845 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003846
3847 /*
Sage Weil29790f22010-03-18 14:45:05 -07003848 * kick request on any mds that has gone active.
Sage Weil2f2dc052009-10-06 11:31:09 -07003849 */
3850 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
3851 newstate >= CEPH_MDS_STATE_ACTIVE) {
Sage Weil29790f22010-03-18 14:45:05 -07003852 if (oldstate != CEPH_MDS_STATE_CREATING &&
3853 oldstate != CEPH_MDS_STATE_STARTING)
3854 pr_info("mds%d recovery completed\n", s->s_mds);
3855 kick_requests(mdsc, i);
Sage Weil2f2dc052009-10-06 11:31:09 -07003856 ceph_kick_flushing_caps(mdsc, s);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08003857 wake_up_session_caps(s, RECONNECT);
Sage Weil2f2dc052009-10-06 11:31:09 -07003858 }
3859 }
Sage Weilcb170a22010-06-21 13:38:35 -07003860
Xiubo Lib38c9eb2019-12-04 06:57:39 -05003861 for (i = 0; i < newmap->possible_max_rank && i < mdsc->max_sessions; i++) {
Sage Weilcb170a22010-06-21 13:38:35 -07003862 s = mdsc->sessions[i];
3863 if (!s)
3864 continue;
3865 if (!ceph_mdsmap_is_laggy(newmap, i))
3866 continue;
3867 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3868 s->s_state == CEPH_MDS_SESSION_HUNG ||
3869 s->s_state == CEPH_MDS_SESSION_CLOSING) {
3870 dout(" connecting to export targets of laggy mds%d\n",
3871 i);
3872 __open_export_target_sessions(mdsc, s);
3873 }
3874 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003875}
3876
3877
3878
3879/*
3880 * leases
3881 */
3882
3883/*
3884 * caller must hold session s_mutex, dentry->d_lock
3885 */
3886void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
3887{
3888 struct ceph_dentry_info *di = ceph_dentry(dentry);
3889
3890 ceph_put_mds_session(di->lease_session);
3891 di->lease_session = NULL;
3892}
3893
Sage Weil2600d2d2010-02-22 15:12:16 -08003894static void handle_lease(struct ceph_mds_client *mdsc,
3895 struct ceph_mds_session *session,
3896 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07003897{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003898 struct super_block *sb = mdsc->fsc->sb;
Sage Weil2f2dc052009-10-06 11:31:09 -07003899 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07003900 struct dentry *parent, *dentry;
3901 struct ceph_dentry_info *di;
Sage Weil2600d2d2010-02-22 15:12:16 -08003902 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07003903 struct ceph_mds_lease *h = msg->front.iov_base;
Sage Weil1e5ea232010-06-04 10:05:40 -07003904 u32 seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003905 struct ceph_vino vino;
Sage Weil2f2dc052009-10-06 11:31:09 -07003906 struct qstr dname;
3907 int release = 0;
3908
Sage Weil2f2dc052009-10-06 11:31:09 -07003909 dout("handle_lease from mds%d\n", mds);
3910
3911 /* decode */
3912 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
3913 goto bad;
3914 vino.ino = le64_to_cpu(h->ino);
3915 vino.snap = CEPH_NOSNAP;
Sage Weil1e5ea232010-06-04 10:05:40 -07003916 seq = le32_to_cpu(h->seq);
Yan, Zheng0fcf6c02018-08-03 16:24:49 +08003917 dname.len = get_unaligned_le32(h + 1);
3918 if (msg->front.iov_len < sizeof(*h) + sizeof(u32) + dname.len)
Sage Weil2f2dc052009-10-06 11:31:09 -07003919 goto bad;
Yan, Zheng0fcf6c02018-08-03 16:24:49 +08003920 dname.name = (void *)(h + 1) + sizeof(u32);
Sage Weil2f2dc052009-10-06 11:31:09 -07003921
Sage Weil2f2dc052009-10-06 11:31:09 -07003922 /* lookup inode */
3923 inode = ceph_find_inode(sb, vino);
Sage Weil2f90b852011-07-26 11:28:25 -07003924 dout("handle_lease %s, ino %llx %p %.*s\n",
3925 ceph_lease_op_name(h->action), vino.ino, inode,
Sage Weil1e5ea232010-06-04 10:05:40 -07003926 dname.len, dname.name);
Yan, Zheng6cd3bca2014-09-17 07:45:12 +08003927
3928 mutex_lock(&session->s_mutex);
3929 session->s_seq++;
3930
Markus Elfringd37b1d92017-08-20 20:22:02 +02003931 if (!inode) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003932 dout("handle_lease no inode %llx\n", vino.ino);
3933 goto release;
3934 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003935
3936 /* dentry */
3937 parent = d_find_alias(inode);
3938 if (!parent) {
3939 dout("no parent dentry on inode %p\n", inode);
3940 WARN_ON(1);
3941 goto release; /* hrm... */
3942 }
Linus Torvalds8387ff22016-06-10 07:51:30 -07003943 dname.hash = full_name_hash(parent, dname.name, dname.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07003944 dentry = d_lookup(parent, &dname);
3945 dput(parent);
3946 if (!dentry)
3947 goto release;
3948
3949 spin_lock(&dentry->d_lock);
3950 di = ceph_dentry(dentry);
3951 switch (h->action) {
3952 case CEPH_MDS_LEASE_REVOKE:
Sage Weil3d8eb7a2011-11-11 09:48:53 -08003953 if (di->lease_session == session) {
Sage Weil1e5ea232010-06-04 10:05:40 -07003954 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
3955 h->seq = cpu_to_le32(di->lease_seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003956 __ceph_mdsc_drop_dentry_lease(dentry);
3957 }
3958 release = 1;
3959 break;
3960
3961 case CEPH_MDS_LEASE_RENEW:
Sage Weil3d8eb7a2011-11-11 09:48:53 -08003962 if (di->lease_session == session &&
Sage Weil2f2dc052009-10-06 11:31:09 -07003963 di->lease_gen == session->s_cap_gen &&
3964 di->lease_renew_from &&
3965 di->lease_renew_after == 0) {
3966 unsigned long duration =
Nicholas Mc Guire3563dbdd2015-02-06 06:52:17 -05003967 msecs_to_jiffies(le32_to_cpu(h->duration_ms));
Sage Weil2f2dc052009-10-06 11:31:09 -07003968
Sage Weil1e5ea232010-06-04 10:05:40 -07003969 di->lease_seq = seq;
Miklos Szeredi9b16f03c2016-06-22 16:35:04 +02003970 di->time = di->lease_renew_from + duration;
Sage Weil2f2dc052009-10-06 11:31:09 -07003971 di->lease_renew_after = di->lease_renew_from +
3972 (duration >> 1);
3973 di->lease_renew_from = 0;
3974 }
3975 break;
3976 }
3977 spin_unlock(&dentry->d_lock);
3978 dput(dentry);
3979
3980 if (!release)
3981 goto out;
3982
3983release:
3984 /* let's just reuse the same message */
3985 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
3986 ceph_msg_get(msg);
3987 ceph_con_send(&session->s_con, msg);
3988
3989out:
Sage Weil2f2dc052009-10-06 11:31:09 -07003990 mutex_unlock(&session->s_mutex);
Yan, Zheng3e1d0452019-05-18 20:39:55 +08003991 /* avoid calling iput_final() in mds dispatch threads */
3992 ceph_async_iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07003993 return;
3994
3995bad:
3996 pr_err("corrupt lease message\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08003997 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003998}
3999
4000void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
Sage Weil2f2dc052009-10-06 11:31:09 -07004001 struct dentry *dentry, char action,
4002 u32 seq)
4003{
4004 struct ceph_msg *msg;
4005 struct ceph_mds_lease *lease;
Yan, Zheng8f2a98ef2019-05-23 10:45:24 +08004006 struct inode *dir;
4007 int len = sizeof(*lease) + sizeof(u32) + NAME_MAX;
Sage Weil2f2dc052009-10-06 11:31:09 -07004008
Yan, Zheng8f2a98ef2019-05-23 10:45:24 +08004009 dout("lease_send_msg identry %p %s to mds%d\n",
4010 dentry, ceph_lease_op_name(action), session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07004011
Sage Weilb61c2762011-08-09 15:03:46 -07004012 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07004013 if (!msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07004014 return;
4015 lease = msg->front.iov_base;
4016 lease->action = action;
Sage Weil2f2dc052009-10-06 11:31:09 -07004017 lease->seq = cpu_to_le32(seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07004018
Yan, Zheng8f2a98ef2019-05-23 10:45:24 +08004019 spin_lock(&dentry->d_lock);
4020 dir = d_inode(dentry->d_parent);
4021 lease->ino = cpu_to_le64(ceph_ino(dir));
4022 lease->first = lease->last = cpu_to_le64(ceph_snap(dir));
4023
4024 put_unaligned_le32(dentry->d_name.len, lease + 1);
4025 memcpy((void *)(lease + 1) + 4,
4026 dentry->d_name.name, dentry->d_name.len);
4027 spin_unlock(&dentry->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07004028 /*
4029 * if this is a preemptive lease RELEASE, no need to
4030 * flush request stream, since the actual request will
4031 * soon follow.
4032 */
4033 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
4034
4035 ceph_con_send(&session->s_con, msg);
4036}
4037
4038/*
Yan, Zheng7aac4532018-03-13 19:52:20 +08004039 * lock unlock sessions, to wait ongoing session activities
Sage Weil2f2dc052009-10-06 11:31:09 -07004040 */
Yan, Zheng7aac4532018-03-13 19:52:20 +08004041static void lock_unlock_sessions(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07004042{
4043 int i;
4044
Sage Weil2f2dc052009-10-06 11:31:09 -07004045 mutex_lock(&mdsc->mutex);
4046 for (i = 0; i < mdsc->max_sessions; i++) {
4047 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
4048 if (!s)
4049 continue;
4050 mutex_unlock(&mdsc->mutex);
4051 mutex_lock(&s->s_mutex);
4052 mutex_unlock(&s->s_mutex);
4053 ceph_put_mds_session(s);
4054 mutex_lock(&mdsc->mutex);
4055 }
4056 mutex_unlock(&mdsc->mutex);
4057}
4058
Yan, Zheng131d7eb2019-07-25 20:16:47 +08004059static void maybe_recover_session(struct ceph_mds_client *mdsc)
4060{
4061 struct ceph_fs_client *fsc = mdsc->fsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07004062
Yan, Zheng131d7eb2019-07-25 20:16:47 +08004063 if (!ceph_test_mount_opt(fsc, CLEANRECOVER))
4064 return;
4065
4066 if (READ_ONCE(fsc->mount_state) != CEPH_MOUNT_MOUNTED)
4067 return;
4068
4069 if (!READ_ONCE(fsc->blacklisted))
4070 return;
4071
4072 if (fsc->last_auto_reconnect &&
4073 time_before(jiffies, fsc->last_auto_reconnect + HZ * 60 * 30))
4074 return;
4075
4076 pr_info("auto reconnect after blacklisted\n");
4077 fsc->last_auto_reconnect = jiffies;
4078 ceph_force_reconnect(fsc->sb);
4079}
Sage Weil2f2dc052009-10-06 11:31:09 -07004080
4081/*
4082 * delayed work -- periodically trim expired leases, renew caps with mds
4083 */
4084static void schedule_delayed(struct ceph_mds_client *mdsc)
4085{
4086 int delay = 5;
4087 unsigned hz = round_jiffies_relative(HZ * delay);
4088 schedule_delayed_work(&mdsc->delayed_work, hz);
4089}
4090
4091static void delayed_work(struct work_struct *work)
4092{
4093 int i;
4094 struct ceph_mds_client *mdsc =
4095 container_of(work, struct ceph_mds_client, delayed_work.work);
4096 int renew_interval;
4097 int renew_caps;
4098
4099 dout("mdsc delayed_work\n");
Yan, Zheng75c96272017-12-14 15:11:09 +08004100
Sage Weil2f2dc052009-10-06 11:31:09 -07004101 mutex_lock(&mdsc->mutex);
4102 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
4103 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
4104 mdsc->last_renew_caps);
4105 if (renew_caps)
4106 mdsc->last_renew_caps = jiffies;
4107
4108 for (i = 0; i < mdsc->max_sessions; i++) {
4109 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
Markus Elfringd37b1d92017-08-20 20:22:02 +02004110 if (!s)
Sage Weil2f2dc052009-10-06 11:31:09 -07004111 continue;
4112 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
4113 dout("resending session close request for mds%d\n",
4114 s->s_mds);
4115 request_close_session(mdsc, s);
4116 ceph_put_mds_session(s);
4117 continue;
4118 }
4119 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
4120 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
4121 s->s_state = CEPH_MDS_SESSION_HUNG;
4122 pr_info("mds%d hung\n", s->s_mds);
4123 }
4124 }
Erqi Chen71a228b2019-08-28 21:22:45 +08004125 if (s->s_state == CEPH_MDS_SESSION_NEW ||
4126 s->s_state == CEPH_MDS_SESSION_RESTARTING ||
4127 s->s_state == CEPH_MDS_SESSION_REJECTED) {
Sage Weil2f2dc052009-10-06 11:31:09 -07004128 /* this mds is failed or recovering, just wait */
4129 ceph_put_mds_session(s);
4130 continue;
4131 }
4132 mutex_unlock(&mdsc->mutex);
4133
4134 mutex_lock(&s->s_mutex);
4135 if (renew_caps)
4136 send_renew_caps(mdsc, s);
4137 else
4138 ceph_con_keepalive(&s->s_con);
Sage Weilaab53dd2010-03-17 16:30:21 -07004139 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
4140 s->s_state == CEPH_MDS_SESSION_HUNG)
Sage Weil3d7ded42010-06-09 16:47:10 -07004141 ceph_send_cap_releases(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07004142 mutex_unlock(&s->s_mutex);
4143 ceph_put_mds_session(s);
4144
4145 mutex_lock(&mdsc->mutex);
4146 }
4147 mutex_unlock(&mdsc->mutex);
4148
Yan, Zheng37c4efc2019-01-31 16:55:51 +08004149 ceph_check_delayed_caps(mdsc);
4150
4151 ceph_queue_cap_reclaim_work(mdsc);
4152
4153 ceph_trim_snapid_map(mdsc);
4154
Yan, Zheng131d7eb2019-07-25 20:16:47 +08004155 maybe_recover_session(mdsc);
4156
Sage Weil2f2dc052009-10-06 11:31:09 -07004157 schedule_delayed(mdsc);
4158}
4159
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004160int ceph_mdsc_init(struct ceph_fs_client *fsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07004161
Sage Weil2f2dc052009-10-06 11:31:09 -07004162{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004163 struct ceph_mds_client *mdsc;
4164
4165 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
4166 if (!mdsc)
4167 return -ENOMEM;
4168 mdsc->fsc = fsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07004169 mutex_init(&mdsc->mutex);
4170 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
Markus Elfringd37b1d92017-08-20 20:22:02 +02004171 if (!mdsc->mdsmap) {
majianpengfb3101b2013-06-25 14:48:19 +08004172 kfree(mdsc);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08004173 return -ENOMEM;
majianpengfb3101b2013-06-25 14:48:19 +08004174 }
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08004175
Chengguang Xu50c55ae2018-03-14 13:47:33 +08004176 fsc->mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07004177 init_completion(&mdsc->safe_umount_waiters);
Sage Weilf3c60c52010-08-11 14:51:23 -07004178 init_waitqueue_head(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07004179 INIT_LIST_HEAD(&mdsc->waiting_for_map);
4180 mdsc->sessions = NULL;
Yan, Zheng86d8f672015-01-09 17:00:42 +08004181 atomic_set(&mdsc->num_sessions, 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07004182 mdsc->max_sessions = 0;
4183 mdsc->stopping = 0;
Luis Henriquesd557c482018-01-12 17:19:29 +00004184 atomic64_set(&mdsc->quotarealms_count, 0);
Luis Henriques0c44a8e2019-03-21 10:20:10 +00004185 mdsc->quotarealms_inodes = RB_ROOT;
4186 mutex_init(&mdsc->quotarealms_inodes_mutex);
Yan, Zhengaffbc192015-05-05 21:22:13 +08004187 mdsc->last_snap_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07004188 init_rwsem(&mdsc->snap_rwsem);
Sage Weila105f002010-02-15 14:37:55 -08004189 mdsc->snap_realms = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07004190 INIT_LIST_HEAD(&mdsc->snap_empty);
Yan, Zheng81c5a142019-01-01 16:28:33 +08004191 mdsc->num_snap_realms = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07004192 spin_lock_init(&mdsc->snap_empty_lock);
4193 mdsc->last_tid = 0;
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08004194 mdsc->oldest_tid = 0;
Sage Weil44ca18f2010-02-15 12:08:46 -08004195 mdsc->request_tree = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07004196 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
4197 mdsc->last_renew_caps = jiffies;
4198 INIT_LIST_HEAD(&mdsc->cap_delay_list);
Jeff Layton3a3430a2019-11-20 12:00:59 -05004199 INIT_LIST_HEAD(&mdsc->cap_wait_list);
Sage Weil2f2dc052009-10-06 11:31:09 -07004200 spin_lock_init(&mdsc->cap_delay_lock);
4201 INIT_LIST_HEAD(&mdsc->snap_flush_list);
4202 spin_lock_init(&mdsc->snap_flush_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08004203 mdsc->last_cap_flush_tid = 1;
Yan, Zhenge4500b52016-07-06 11:12:56 +08004204 INIT_LIST_HEAD(&mdsc->cap_flush_list);
Sage Weil2f2dc052009-10-06 11:31:09 -07004205 INIT_LIST_HEAD(&mdsc->cap_dirty);
Sage Weildb354052011-05-24 11:46:31 -07004206 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
Sage Weil2f2dc052009-10-06 11:31:09 -07004207 mdsc->num_cap_flushing = 0;
4208 spin_lock_init(&mdsc->cap_dirty_lock);
4209 init_waitqueue_head(&mdsc->cap_flushing_wq);
Yan, Zheng37c4efc2019-01-31 16:55:51 +08004210 INIT_WORK(&mdsc->cap_reclaim_work, ceph_cap_reclaim_work);
Yan, Zhengfe330322019-02-01 14:57:15 +08004211 atomic_set(&mdsc->cap_reclaim_pending, 0);
Yan, Zheng37c4efc2019-01-31 16:55:51 +08004212
4213 spin_lock_init(&mdsc->dentry_list_lock);
4214 INIT_LIST_HEAD(&mdsc->dentry_leases);
4215 INIT_LIST_HEAD(&mdsc->dentry_dir_leases);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08004216
Yehuda Sadeh37151662010-06-17 16:16:12 -07004217 ceph_caps_init(mdsc);
Yan, Zhengfe330322019-02-01 14:57:15 +08004218 ceph_adjust_caps_max_min(mdsc, fsc->mount_options);
Yehuda Sadeh37151662010-06-17 16:16:12 -07004219
Yan, Zheng75c96272017-12-14 15:11:09 +08004220 spin_lock_init(&mdsc->snapid_map_lock);
4221 mdsc->snapid_map_tree = RB_ROOT;
4222 INIT_LIST_HEAD(&mdsc->snapid_map_lru);
4223
Yan, Zheng10183a62015-04-27 15:33:28 +08004224 init_rwsem(&mdsc->pool_perm_rwsem);
4225 mdsc->pool_perm_tree = RB_ROOT;
4226
Yan, Zhengdfeb84d2018-07-02 15:55:23 +08004227 strscpy(mdsc->nodename, utsname()->nodename,
4228 sizeof(mdsc->nodename));
Sage Weil5f44f142009-11-18 14:52:18 -08004229 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07004230}
4231
4232/*
4233 * Wait for safe replies on open mds requests. If we time out, drop
4234 * all requests from the tree to avoid dangling dentry refs.
4235 */
4236static void wait_requests(struct ceph_mds_client *mdsc)
4237{
Ilya Dryomova319bf52015-05-15 12:02:17 +03004238 struct ceph_options *opts = mdsc->fsc->client->options;
Sage Weil2f2dc052009-10-06 11:31:09 -07004239 struct ceph_mds_request *req;
Sage Weil2f2dc052009-10-06 11:31:09 -07004240
4241 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08004242 if (__get_oldest_req(mdsc)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07004243 mutex_unlock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08004244
Sage Weil2f2dc052009-10-06 11:31:09 -07004245 dout("wait_requests waiting for requests\n");
4246 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
Ilya Dryomova319bf52015-05-15 12:02:17 +03004247 ceph_timeout_jiffies(opts->mount_timeout));
Sage Weil2f2dc052009-10-06 11:31:09 -07004248
4249 /* tear down remaining requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08004250 mutex_lock(&mdsc->mutex);
4251 while ((req = __get_oldest_req(mdsc))) {
Sage Weil2f2dc052009-10-06 11:31:09 -07004252 dout("wait_requests timed out on tid %llu\n",
4253 req->r_tid);
Yan, Zheng428138c2019-06-14 10:55:05 +08004254 list_del_init(&req->r_wait);
Sage Weil44ca18f2010-02-15 12:08:46 -08004255 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07004256 }
4257 }
4258 mutex_unlock(&mdsc->mutex);
4259 dout("wait_requests done\n");
4260}
4261
4262/*
4263 * called before mount is ro, and before dentries are torn down.
4264 * (hmm, does this still race with new lookups?)
4265 */
4266void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
4267{
4268 dout("pre_umount\n");
4269 mdsc->stopping = 1;
4270
Yan, Zheng7aac4532018-03-13 19:52:20 +08004271 lock_unlock_sessions(mdsc);
Sage Weilafcdaea2009-10-14 14:27:38 -07004272 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07004273 wait_requests(mdsc);
Sage Weil17c688c2010-06-21 16:12:26 -07004274
4275 /*
4276 * wait for reply handlers to drop their request refs and
4277 * their inode/dcache refs
4278 */
4279 ceph_msgr_flush();
Luis Henriques0c44a8e2019-03-21 10:20:10 +00004280
4281 ceph_cleanup_quotarealms_inodes(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07004282}
4283
4284/*
4285 * wait for all write mds requests to flush.
4286 */
4287static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
4288{
Sage Weil80fc7312010-03-16 15:28:54 -07004289 struct ceph_mds_request *req = NULL, *nextreq;
Sage Weil44ca18f2010-02-15 12:08:46 -08004290 struct rb_node *n;
Sage Weil2f2dc052009-10-06 11:31:09 -07004291
4292 mutex_lock(&mdsc->mutex);
4293 dout("wait_unsafe_requests want %lld\n", want_tid);
Sage Weil80fc7312010-03-16 15:28:54 -07004294restart:
Sage Weil44ca18f2010-02-15 12:08:46 -08004295 req = __get_oldest_req(mdsc);
4296 while (req && req->r_tid <= want_tid) {
Sage Weil80fc7312010-03-16 15:28:54 -07004297 /* find next request */
4298 n = rb_next(&req->r_node);
4299 if (n)
4300 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
4301 else
4302 nextreq = NULL;
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08004303 if (req->r_op != CEPH_MDS_OP_SETFILELOCK &&
4304 (req->r_op & CEPH_MDS_OP_WRITE)) {
Sage Weil44ca18f2010-02-15 12:08:46 -08004305 /* write op */
4306 ceph_mdsc_get_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07004307 if (nextreq)
4308 ceph_mdsc_get_request(nextreq);
Sage Weil44ca18f2010-02-15 12:08:46 -08004309 mutex_unlock(&mdsc->mutex);
4310 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
4311 req->r_tid, want_tid);
4312 wait_for_completion(&req->r_safe_completion);
4313 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08004314 ceph_mdsc_put_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07004315 if (!nextreq)
4316 break; /* next dne before, so we're done! */
4317 if (RB_EMPTY_NODE(&nextreq->r_node)) {
4318 /* next request was removed from tree */
4319 ceph_mdsc_put_request(nextreq);
4320 goto restart;
4321 }
4322 ceph_mdsc_put_request(nextreq); /* won't go away */
Sage Weil44ca18f2010-02-15 12:08:46 -08004323 }
Sage Weil80fc7312010-03-16 15:28:54 -07004324 req = nextreq;
Sage Weil2f2dc052009-10-06 11:31:09 -07004325 }
4326 mutex_unlock(&mdsc->mutex);
4327 dout("wait_unsafe_requests done\n");
4328}
4329
4330void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
4331{
Yan, Zheng0e294382016-07-04 18:06:41 +08004332 u64 want_tid, want_flush;
Sage Weil2f2dc052009-10-06 11:31:09 -07004333
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01004334 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
Sage Weil56b7cf92010-05-03 15:22:00 -07004335 return;
4336
Sage Weil2f2dc052009-10-06 11:31:09 -07004337 dout("sync\n");
4338 mutex_lock(&mdsc->mutex);
4339 want_tid = mdsc->last_tid;
Sage Weil2f2dc052009-10-06 11:31:09 -07004340 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07004341
Sage Weilafcdaea2009-10-14 14:27:38 -07004342 ceph_flush_dirty_caps(mdsc);
Yan, Zhengd3383a82015-01-08 21:30:12 +08004343 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zheng8310b082015-06-09 17:20:12 +08004344 want_flush = mdsc->last_cap_flush_tid;
Yan, Zhengc8799fc2016-07-07 15:22:38 +08004345 if (!list_empty(&mdsc->cap_flush_list)) {
4346 struct ceph_cap_flush *cf =
4347 list_last_entry(&mdsc->cap_flush_list,
4348 struct ceph_cap_flush, g_list);
4349 cf->wake = true;
4350 }
Yan, Zhengd3383a82015-01-08 21:30:12 +08004351 spin_unlock(&mdsc->cap_dirty_lock);
4352
Yan, Zheng0e294382016-07-04 18:06:41 +08004353 dout("sync want tid %lld flush_seq %lld\n",
4354 want_tid, want_flush);
Sage Weil2f2dc052009-10-06 11:31:09 -07004355
4356 wait_unsafe_requests(mdsc, want_tid);
Yan, Zheng0e294382016-07-04 18:06:41 +08004357 wait_caps_flush(mdsc, want_flush);
Sage Weil2f2dc052009-10-06 11:31:09 -07004358}
4359
Sage Weilf3c60c52010-08-11 14:51:23 -07004360/*
4361 * true if all sessions are closed, or we force unmount
4362 */
Yan, Zhengfcff4152016-09-14 16:39:51 +08004363static bool done_closing_sessions(struct ceph_mds_client *mdsc, int skipped)
Sage Weilf3c60c52010-08-11 14:51:23 -07004364{
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01004365 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
Sage Weilf3c60c52010-08-11 14:51:23 -07004366 return true;
Yan, Zhengfcff4152016-09-14 16:39:51 +08004367 return atomic_read(&mdsc->num_sessions) <= skipped;
Sage Weilf3c60c52010-08-11 14:51:23 -07004368}
Sage Weil2f2dc052009-10-06 11:31:09 -07004369
4370/*
4371 * called after sb is ro.
4372 */
4373void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
4374{
Ilya Dryomova319bf52015-05-15 12:02:17 +03004375 struct ceph_options *opts = mdsc->fsc->client->options;
Sage Weil2f2dc052009-10-06 11:31:09 -07004376 struct ceph_mds_session *session;
4377 int i;
Yan, Zhengfcff4152016-09-14 16:39:51 +08004378 int skipped = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07004379
4380 dout("close_sessions\n");
4381
Sage Weil2f2dc052009-10-06 11:31:09 -07004382 /* close sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07004383 mutex_lock(&mdsc->mutex);
4384 for (i = 0; i < mdsc->max_sessions; i++) {
4385 session = __ceph_lookup_mds_session(mdsc, i);
4386 if (!session)
4387 continue;
Sage Weil2f2dc052009-10-06 11:31:09 -07004388 mutex_unlock(&mdsc->mutex);
Sage Weilf3c60c52010-08-11 14:51:23 -07004389 mutex_lock(&session->s_mutex);
Yan, Zhengfcff4152016-09-14 16:39:51 +08004390 if (__close_session(mdsc, session) <= 0)
4391 skipped++;
Sage Weilf3c60c52010-08-11 14:51:23 -07004392 mutex_unlock(&session->s_mutex);
4393 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07004394 mutex_lock(&mdsc->mutex);
4395 }
Sage Weilf3c60c52010-08-11 14:51:23 -07004396 mutex_unlock(&mdsc->mutex);
4397
4398 dout("waiting for sessions to close\n");
Yan, Zhengfcff4152016-09-14 16:39:51 +08004399 wait_event_timeout(mdsc->session_close_wq,
4400 done_closing_sessions(mdsc, skipped),
Ilya Dryomova319bf52015-05-15 12:02:17 +03004401 ceph_timeout_jiffies(opts->mount_timeout));
Sage Weil2f2dc052009-10-06 11:31:09 -07004402
4403 /* tear down remaining sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07004404 mutex_lock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07004405 for (i = 0; i < mdsc->max_sessions; i++) {
4406 if (mdsc->sessions[i]) {
Xiubo Li5b3248c2019-12-19 19:44:09 -05004407 session = ceph_get_mds_session(mdsc->sessions[i]);
Sage Weil2600d2d2010-02-22 15:12:16 -08004408 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07004409 mutex_unlock(&mdsc->mutex);
4410 mutex_lock(&session->s_mutex);
4411 remove_session_caps(session);
4412 mutex_unlock(&session->s_mutex);
4413 ceph_put_mds_session(session);
4414 mutex_lock(&mdsc->mutex);
4415 }
4416 }
Sage Weil2f2dc052009-10-06 11:31:09 -07004417 WARN_ON(!list_empty(&mdsc->cap_delay_list));
Sage Weil2f2dc052009-10-06 11:31:09 -07004418 mutex_unlock(&mdsc->mutex);
4419
Yan, Zheng75c96272017-12-14 15:11:09 +08004420 ceph_cleanup_snapid_map(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07004421 ceph_cleanup_empty_realms(mdsc);
4422
Yan, Zheng37c4efc2019-01-31 16:55:51 +08004423 cancel_work_sync(&mdsc->cap_reclaim_work);
Sage Weil2f2dc052009-10-06 11:31:09 -07004424 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
4425
4426 dout("stopped\n");
4427}
4428
Yan, Zheng48fec5d2015-07-01 16:27:46 +08004429void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc)
4430{
4431 struct ceph_mds_session *session;
4432 int mds;
4433
4434 dout("force umount\n");
4435
4436 mutex_lock(&mdsc->mutex);
4437 for (mds = 0; mds < mdsc->max_sessions; mds++) {
4438 session = __ceph_lookup_mds_session(mdsc, mds);
4439 if (!session)
4440 continue;
Yan, Zhengd468e722019-07-25 20:16:44 +08004441
4442 if (session->s_state == CEPH_MDS_SESSION_REJECTED)
4443 __unregister_session(mdsc, session);
4444 __wake_requests(mdsc, &session->s_waiting);
Yan, Zheng48fec5d2015-07-01 16:27:46 +08004445 mutex_unlock(&mdsc->mutex);
Yan, Zhengd468e722019-07-25 20:16:44 +08004446
Yan, Zheng48fec5d2015-07-01 16:27:46 +08004447 mutex_lock(&session->s_mutex);
4448 __close_session(mdsc, session);
4449 if (session->s_state == CEPH_MDS_SESSION_CLOSING) {
4450 cleanup_session_requests(mdsc, session);
4451 remove_session_caps(session);
4452 }
4453 mutex_unlock(&session->s_mutex);
4454 ceph_put_mds_session(session);
Yan, Zhengd468e722019-07-25 20:16:44 +08004455
Yan, Zheng48fec5d2015-07-01 16:27:46 +08004456 mutex_lock(&mdsc->mutex);
4457 kick_requests(mdsc, mds);
4458 }
4459 __wake_requests(mdsc, &mdsc->waiting_for_map);
4460 mutex_unlock(&mdsc->mutex);
4461}
4462
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004463static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07004464{
4465 dout("stop\n");
4466 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
4467 if (mdsc->mdsmap)
4468 ceph_mdsmap_destroy(mdsc->mdsmap);
4469 kfree(mdsc->sessions);
Yehuda Sadeh37151662010-06-17 16:16:12 -07004470 ceph_caps_finalize(mdsc);
Yan, Zheng10183a62015-04-27 15:33:28 +08004471 ceph_pool_perm_destroy(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07004472}
4473
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004474void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
4475{
4476 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weilef550f62011-03-25 13:27:48 -07004477 dout("mdsc_destroy %p\n", mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07004478
Chengguang Xu50c55ae2018-03-14 13:47:33 +08004479 if (!mdsc)
4480 return;
4481
Sage Weilef550f62011-03-25 13:27:48 -07004482 /* flush out any connection work with references to us */
4483 ceph_msgr_flush();
4484
Yan, Zheng62a65f32017-06-22 16:26:34 +08004485 ceph_mdsc_stop(mdsc);
4486
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004487 fsc->mdsc = NULL;
4488 kfree(mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07004489 dout("mdsc_destroy %p done\n", mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004490}
4491
Yan, Zheng430afba2016-07-08 11:25:38 +08004492void ceph_mdsc_handle_fsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
4493{
4494 struct ceph_fs_client *fsc = mdsc->fsc;
4495 const char *mds_namespace = fsc->mount_options->mds_namespace;
4496 void *p = msg->front.iov_base;
4497 void *end = p + msg->front.iov_len;
4498 u32 epoch;
4499 u32 map_len;
4500 u32 num_fs;
4501 u32 mount_fscid = (u32)-1;
4502 u8 struct_v, struct_cv;
4503 int err = -EINVAL;
4504
4505 ceph_decode_need(&p, end, sizeof(u32), bad);
4506 epoch = ceph_decode_32(&p);
4507
4508 dout("handle_fsmap epoch %u\n", epoch);
4509
4510 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
4511 struct_v = ceph_decode_8(&p);
4512 struct_cv = ceph_decode_8(&p);
4513 map_len = ceph_decode_32(&p);
4514
4515 ceph_decode_need(&p, end, sizeof(u32) * 3, bad);
4516 p += sizeof(u32) * 2; /* skip epoch and legacy_client_fscid */
4517
4518 num_fs = ceph_decode_32(&p);
4519 while (num_fs-- > 0) {
4520 void *info_p, *info_end;
4521 u32 info_len;
4522 u8 info_v, info_cv;
4523 u32 fscid, namelen;
4524
4525 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
4526 info_v = ceph_decode_8(&p);
4527 info_cv = ceph_decode_8(&p);
4528 info_len = ceph_decode_32(&p);
4529 ceph_decode_need(&p, end, info_len, bad);
4530 info_p = p;
4531 info_end = p + info_len;
4532 p = info_end;
4533
4534 ceph_decode_need(&info_p, info_end, sizeof(u32) * 2, bad);
4535 fscid = ceph_decode_32(&info_p);
4536 namelen = ceph_decode_32(&info_p);
4537 ceph_decode_need(&info_p, info_end, namelen, bad);
4538
4539 if (mds_namespace &&
4540 strlen(mds_namespace) == namelen &&
4541 !strncmp(mds_namespace, (char *)info_p, namelen)) {
4542 mount_fscid = fscid;
4543 break;
4544 }
4545 }
4546
4547 ceph_monc_got_map(&fsc->client->monc, CEPH_SUB_FSMAP, epoch);
4548 if (mount_fscid != (u32)-1) {
4549 fsc->client->monc.fs_cluster_id = mount_fscid;
4550 ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP,
4551 0, true);
4552 ceph_monc_renew_subs(&fsc->client->monc);
4553 } else {
4554 err = -ENOENT;
4555 goto err_out;
4556 }
4557 return;
Ilya Dryomov76bd6ec2017-10-16 10:32:50 +02004558
Yan, Zheng430afba2016-07-08 11:25:38 +08004559bad:
4560 pr_err("error decoding fsmap\n");
4561err_out:
4562 mutex_lock(&mdsc->mutex);
Ilya Dryomov76bd6ec2017-10-16 10:32:50 +02004563 mdsc->mdsmap_err = err;
Yan, Zheng430afba2016-07-08 11:25:38 +08004564 __wake_requests(mdsc, &mdsc->waiting_for_map);
4565 mutex_unlock(&mdsc->mutex);
Yan, Zheng430afba2016-07-08 11:25:38 +08004566}
Sage Weil2f2dc052009-10-06 11:31:09 -07004567
4568/*
4569 * handle mds map update.
4570 */
Yan, Zheng430afba2016-07-08 11:25:38 +08004571void ceph_mdsc_handle_mdsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07004572{
4573 u32 epoch;
4574 u32 maplen;
4575 void *p = msg->front.iov_base;
4576 void *end = p + msg->front.iov_len;
4577 struct ceph_mdsmap *newmap, *oldmap;
4578 struct ceph_fsid fsid;
4579 int err = -EINVAL;
4580
4581 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
4582 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004583 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
Sage Weil07433042009-11-18 16:50:41 -08004584 return;
Sage Weilc89136e2009-10-14 09:59:09 -07004585 epoch = ceph_decode_32(&p);
4586 maplen = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07004587 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
4588
4589 /* do we need it? */
Sage Weil2f2dc052009-10-06 11:31:09 -07004590 mutex_lock(&mdsc->mutex);
4591 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
4592 dout("handle_map epoch %u <= our %u\n",
4593 epoch, mdsc->mdsmap->m_epoch);
4594 mutex_unlock(&mdsc->mutex);
4595 return;
4596 }
4597
4598 newmap = ceph_mdsmap_decode(&p, end);
4599 if (IS_ERR(newmap)) {
4600 err = PTR_ERR(newmap);
4601 goto bad_unlock;
4602 }
4603
4604 /* swap into place */
4605 if (mdsc->mdsmap) {
4606 oldmap = mdsc->mdsmap;
4607 mdsc->mdsmap = newmap;
4608 check_new_map(mdsc, newmap, oldmap);
4609 ceph_mdsmap_destroy(oldmap);
4610 } else {
4611 mdsc->mdsmap = newmap; /* first mds map */
4612 }
Chengguang Xu719784b2018-07-19 22:15:24 +08004613 mdsc->fsc->max_file_size = min((loff_t)mdsc->mdsmap->m_max_file_size,
4614 MAX_LFS_FILESIZE);
Sage Weil2f2dc052009-10-06 11:31:09 -07004615
4616 __wake_requests(mdsc, &mdsc->waiting_for_map);
Ilya Dryomov82dcaba2016-01-19 16:19:06 +01004617 ceph_monc_got_map(&mdsc->fsc->client->monc, CEPH_SUB_MDSMAP,
4618 mdsc->mdsmap->m_epoch);
Sage Weil2f2dc052009-10-06 11:31:09 -07004619
4620 mutex_unlock(&mdsc->mutex);
4621 schedule_delayed(mdsc);
4622 return;
4623
4624bad_unlock:
4625 mutex_unlock(&mdsc->mutex);
4626bad:
4627 pr_err("error decoding mdsmap %d\n", err);
4628 return;
4629}
4630
4631static struct ceph_connection *con_get(struct ceph_connection *con)
4632{
4633 struct ceph_mds_session *s = con->private;
4634
Xiubo Li5b3248c2019-12-19 19:44:09 -05004635 if (ceph_get_mds_session(s))
Sage Weil2f2dc052009-10-06 11:31:09 -07004636 return con;
Sage Weil2f2dc052009-10-06 11:31:09 -07004637 return NULL;
4638}
4639
4640static void con_put(struct ceph_connection *con)
4641{
4642 struct ceph_mds_session *s = con->private;
4643
Sage Weil2f2dc052009-10-06 11:31:09 -07004644 ceph_put_mds_session(s);
4645}
4646
4647/*
4648 * if the client is unresponsive for long enough, the mds will kill
4649 * the session entirely.
4650 */
4651static void peer_reset(struct ceph_connection *con)
4652{
4653 struct ceph_mds_session *s = con->private;
Sage Weil7e70f0e2010-03-18 13:59:12 -07004654 struct ceph_mds_client *mdsc = s->s_mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07004655
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07004656 pr_warn("mds%d closed our session\n", s->s_mds);
Sage Weil7e70f0e2010-03-18 13:59:12 -07004657 send_mds_reconnect(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07004658}
4659
4660static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
4661{
4662 struct ceph_mds_session *s = con->private;
4663 struct ceph_mds_client *mdsc = s->s_mdsc;
4664 int type = le16_to_cpu(msg->hdr.type);
4665
Sage Weil2600d2d2010-02-22 15:12:16 -08004666 mutex_lock(&mdsc->mutex);
4667 if (__verify_registered_session(mdsc, s) < 0) {
4668 mutex_unlock(&mdsc->mutex);
4669 goto out;
4670 }
4671 mutex_unlock(&mdsc->mutex);
4672
Sage Weil2f2dc052009-10-06 11:31:09 -07004673 switch (type) {
4674 case CEPH_MSG_MDS_MAP:
Yan, Zheng430afba2016-07-08 11:25:38 +08004675 ceph_mdsc_handle_mdsmap(mdsc, msg);
4676 break;
4677 case CEPH_MSG_FS_MAP_USER:
4678 ceph_mdsc_handle_fsmap(mdsc, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004679 break;
4680 case CEPH_MSG_CLIENT_SESSION:
4681 handle_session(s, msg);
4682 break;
4683 case CEPH_MSG_CLIENT_REPLY:
4684 handle_reply(s, msg);
4685 break;
4686 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
Sage Weil2600d2d2010-02-22 15:12:16 -08004687 handle_forward(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004688 break;
4689 case CEPH_MSG_CLIENT_CAPS:
4690 ceph_handle_caps(s, msg);
4691 break;
4692 case CEPH_MSG_CLIENT_SNAP:
Sage Weil2600d2d2010-02-22 15:12:16 -08004693 ceph_handle_snap(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004694 break;
4695 case CEPH_MSG_CLIENT_LEASE:
Sage Weil2600d2d2010-02-22 15:12:16 -08004696 handle_lease(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004697 break;
Luis Henriquesfb18a572018-01-05 10:47:18 +00004698 case CEPH_MSG_CLIENT_QUOTA:
4699 ceph_handle_quota(mdsc, s, msg);
4700 break;
Sage Weil2f2dc052009-10-06 11:31:09 -07004701
4702 default:
4703 pr_err("received unknown message type %d %s\n", type,
4704 ceph_msg_type_name(type));
4705 }
Sage Weil2600d2d2010-02-22 15:12:16 -08004706out:
Sage Weil2f2dc052009-10-06 11:31:09 -07004707 ceph_msg_put(msg);
4708}
4709
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004710/*
4711 * authentication
4712 */
Alex Eldera3530df2012-05-16 15:16:39 -05004713
4714/*
4715 * Note: returned pointer is the address of a structure that's
4716 * managed separately. Caller must *not* attempt to free it.
4717 */
4718static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05004719 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004720{
4721 struct ceph_mds_session *s = con->private;
4722 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004723 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05004724 struct ceph_auth_handshake *auth = &s->s_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004725
Alex Elder74f18692012-05-16 15:16:39 -05004726 if (force_new && auth->authorizer) {
Ilya Dryomov6c1ea262016-04-11 19:34:49 +02004727 ceph_auth_destroy_authorizer(auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05004728 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004729 }
Sage Weil27859f92013-03-25 10:26:14 -07004730 if (!auth->authorizer) {
4731 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4732 auth);
Sage Weil0bed9b52013-03-25 10:26:01 -07004733 if (ret)
4734 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07004735 } else {
4736 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4737 auth);
Alex Eldera2556512012-05-16 15:16:39 -05004738 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05004739 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004740 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004741 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05004742
Alex Eldera3530df2012-05-16 15:16:39 -05004743 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004744}
4745
Ilya Dryomov6daca132018-07-27 19:18:34 +02004746static int add_authorizer_challenge(struct ceph_connection *con,
4747 void *challenge_buf, int challenge_buf_len)
4748{
4749 struct ceph_mds_session *s = con->private;
4750 struct ceph_mds_client *mdsc = s->s_mdsc;
4751 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
4752
4753 return ceph_auth_add_authorizer_challenge(ac, s->s_auth.authorizer,
4754 challenge_buf, challenge_buf_len);
4755}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004756
Ilya Dryomov0dde5842016-12-02 16:35:09 +01004757static int verify_authorizer_reply(struct ceph_connection *con)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004758{
4759 struct ceph_mds_session *s = con->private;
4760 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004761 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004762
Ilya Dryomov0dde5842016-12-02 16:35:09 +01004763 return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004764}
4765
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004766static int invalidate_authorizer(struct ceph_connection *con)
4767{
4768 struct ceph_mds_session *s = con->private;
4769 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004770 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004771
Sage Weil27859f92013-03-25 10:26:14 -07004772 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004773
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004774 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004775}
4776
Alex Elder53ded492013-03-01 18:00:14 -06004777static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
4778 struct ceph_msg_header *hdr, int *skip)
4779{
4780 struct ceph_msg *msg;
4781 int type = (int) le16_to_cpu(hdr->type);
4782 int front_len = (int) le32_to_cpu(hdr->front_len);
4783
4784 if (con->in_msg)
4785 return con->in_msg;
4786
4787 *skip = 0;
4788 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
4789 if (!msg) {
4790 pr_err("unable to allocate msg type %d len %d\n",
4791 type, front_len);
4792 return NULL;
4793 }
Alex Elder53ded492013-03-01 18:00:14 -06004794
4795 return msg;
4796}
4797
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004798static int mds_sign_message(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08004799{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004800 struct ceph_mds_session *s = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08004801 struct ceph_auth_handshake *auth = &s->s_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004802
Yan, Zheng33d07332014-11-04 16:33:37 +08004803 return ceph_auth_sign_message(auth, msg);
4804}
4805
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004806static int mds_check_message_signature(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08004807{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004808 struct ceph_mds_session *s = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08004809 struct ceph_auth_handshake *auth = &s->s_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004810
Yan, Zheng33d07332014-11-04 16:33:37 +08004811 return ceph_auth_check_message_signature(auth, msg);
4812}
4813
Tobias Klauser9e327892010-05-20 10:40:19 +02004814static const struct ceph_connection_operations mds_con_ops = {
Sage Weil2f2dc052009-10-06 11:31:09 -07004815 .get = con_get,
4816 .put = con_put,
4817 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004818 .get_authorizer = get_authorizer,
Ilya Dryomov6daca132018-07-27 19:18:34 +02004819 .add_authorizer_challenge = add_authorizer_challenge,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004820 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004821 .invalidate_authorizer = invalidate_authorizer,
Sage Weil2f2dc052009-10-06 11:31:09 -07004822 .peer_reset = peer_reset,
Alex Elder53ded492013-03-01 18:00:14 -06004823 .alloc_msg = mds_alloc_msg,
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004824 .sign_message = mds_sign_message,
4825 .check_message_signature = mds_check_message_signature,
Sage Weil2f2dc052009-10-06 11:31:09 -07004826};
4827
Sage Weil2f2dc052009-10-06 11:31:09 -07004828/* eof */