blob: c839664f86c649cb6b2e49f7a2478304ca5ce7cd [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));
Sage Weil153c8e62009-12-07 12:31:09 -0800739 kfree(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) {
Sage Weil3b663782011-05-18 16:12:12 -0700796 ihold(dir);
Sage Weil2f2dc052009-10-06 11:31:09 -0700797 req->r_unsafe_dir = dir;
Sage Weil2f2dc052009-10-06 11:31:09 -0700798 }
799}
800
801static void __unregister_request(struct ceph_mds_client *mdsc,
802 struct ceph_mds_request *req)
803{
804 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +0800805
Jeff Laytondf963ea2017-02-14 10:09:40 -0500806 /* Never leave an unregistered request on an unsafe list! */
807 list_del_init(&req->r_unsafe_item);
808
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +0800809 if (req->r_tid == mdsc->oldest_tid) {
810 struct rb_node *p = rb_next(&req->r_node);
811 mdsc->oldest_tid = 0;
812 while (p) {
813 struct ceph_mds_request *next_req =
814 rb_entry(p, struct ceph_mds_request, r_node);
815 if (next_req->r_op != CEPH_MDS_OP_SETFILELOCK) {
816 mdsc->oldest_tid = next_req->r_tid;
817 break;
818 }
819 p = rb_next(p);
820 }
821 }
822
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200823 erase_request(&mdsc->request_tree, req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700824
Jeff Laytonbc2de10d2017-02-01 13:49:09 -0500825 if (req->r_unsafe_dir &&
826 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700827 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
Sage Weil2f2dc052009-10-06 11:31:09 -0700828 spin_lock(&ci->i_unsafe_lock);
829 list_del_init(&req->r_unsafe_dir_item);
830 spin_unlock(&ci->i_unsafe_lock);
Yan, Zheng4c06ace2015-10-27 17:18:00 +0800831 }
Jeff Laytonbc2de10d2017-02-01 13:49:09 -0500832 if (req->r_target_inode &&
833 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Yan, Zheng68cd5b42015-10-27 18:36:06 +0800834 struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
835 spin_lock(&ci->i_unsafe_lock);
836 list_del_init(&req->r_unsafe_target_item);
837 spin_unlock(&ci->i_unsafe_lock);
838 }
Sage Weil3b663782011-05-18 16:12:12 -0700839
Yan, Zheng4c06ace2015-10-27 17:18:00 +0800840 if (req->r_unsafe_dir) {
Yan, Zheng3e1d0452019-05-18 20:39:55 +0800841 /* avoid calling iput_final() in mds dispatch threads */
842 ceph_async_iput(req->r_unsafe_dir);
Sage Weil3b663782011-05-18 16:12:12 -0700843 req->r_unsafe_dir = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700844 }
Sage Weil94aa8ae2010-03-28 21:22:50 -0700845
Yan, Zhengfc55d2c2013-10-31 09:10:47 +0800846 complete_all(&req->r_safe_completion);
847
Sage Weil94aa8ae2010-03-28 21:22:50 -0700848 ceph_mdsc_put_request(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700849}
850
851/*
Jeff Layton30c71232016-12-15 08:37:56 -0500852 * Walk back up the dentry tree until we hit a dentry representing a
853 * non-snapshot inode. We do this using the rcu_read_lock (which must be held
854 * when calling this) to ensure that the objects won't disappear while we're
855 * working with them. Once we hit a candidate dentry, we attempt to take a
856 * reference to it, and return that as the result.
857 */
Dan Carpenterf1075482017-02-23 13:39:59 +0300858static struct inode *get_nonsnap_parent(struct dentry *dentry)
859{
860 struct inode *inode = NULL;
Jeff Layton30c71232016-12-15 08:37:56 -0500861
862 while (dentry && !IS_ROOT(dentry)) {
863 inode = d_inode_rcu(dentry);
864 if (!inode || ceph_snap(inode) == CEPH_NOSNAP)
865 break;
866 dentry = dentry->d_parent;
867 }
868 if (inode)
869 inode = igrab(inode);
870 return inode;
871}
872
873/*
Sage Weil2f2dc052009-10-06 11:31:09 -0700874 * Choose mds to send request to next. If there is a hint set in the
875 * request (e.g., due to a prior forward hint from the mds), use that.
876 * Otherwise, consult frag tree and/or caps to identify the
877 * appropriate mds. If all else fails, choose randomly.
878 *
879 * Called under mdsc->mutex.
880 */
881static int __choose_mds(struct ceph_mds_client *mdsc,
Xiubo Lic4853e92019-12-09 07:47:15 -0500882 struct ceph_mds_request *req,
883 bool *random)
Sage Weil2f2dc052009-10-06 11:31:09 -0700884{
885 struct inode *inode;
886 struct ceph_inode_info *ci;
887 struct ceph_cap *cap;
888 int mode = req->r_direct_mode;
889 int mds = -1;
890 u32 hash = req->r_direct_hash;
Jeff Laytonbc2de10d2017-02-01 13:49:09 -0500891 bool is_hash = test_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags);
Sage Weil2f2dc052009-10-06 11:31:09 -0700892
Xiubo Lic4853e92019-12-09 07:47:15 -0500893 if (random)
894 *random = false;
895
Sage Weil2f2dc052009-10-06 11:31:09 -0700896 /*
897 * is there a specific mds we should try? ignore hint if we have
898 * no session and the mds is not up (active or recovering).
899 */
900 if (req->r_resend_mds >= 0 &&
901 (__have_session(mdsc, req->r_resend_mds) ||
902 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
903 dout("choose_mds using resend_mds mds%d\n",
904 req->r_resend_mds);
905 return req->r_resend_mds;
906 }
907
908 if (mode == USE_RANDOM_MDS)
909 goto random;
910
911 inode = NULL;
912 if (req->r_inode) {
Yan, Zheng5d37ca12017-07-26 12:48:08 +0800913 if (ceph_snap(req->r_inode) != CEPH_SNAPDIR) {
914 inode = req->r_inode;
915 ihold(inode);
916 } else {
Yan, Zheng38f340c2017-09-22 11:41:06 +0800917 /* req->r_dentry is non-null for LSSNAP request */
918 rcu_read_lock();
919 inode = get_nonsnap_parent(req->r_dentry);
920 rcu_read_unlock();
921 dout("__choose_mds using snapdir's parent %p\n", inode);
Yan, Zheng5d37ca12017-07-26 12:48:08 +0800922 }
Yan, Zheng38f340c2017-09-22 11:41:06 +0800923 } else if (req->r_dentry) {
Sage Weild79698d2011-07-26 11:31:26 -0700924 /* ignore race with rename; old or new d_parent is okay */
Jeff Layton30c71232016-12-15 08:37:56 -0500925 struct dentry *parent;
926 struct inode *dir;
Sage Weileb6bb1c2010-08-16 09:21:27 -0700927
Jeff Layton30c71232016-12-15 08:37:56 -0500928 rcu_read_lock();
Yan, Zheng41883ba2019-05-23 10:22:55 +0800929 parent = READ_ONCE(req->r_dentry->d_parent);
Jeff Layton3dd69aa2017-01-31 10:28:26 -0500930 dir = req->r_parent ? : d_inode_rcu(parent);
Jeff Layton30c71232016-12-15 08:37:56 -0500931
932 if (!dir || dir->i_sb != mdsc->fsc->sb) {
933 /* not this fs or parent went negative */
David Howells2b0143b2015-03-17 22:25:59 +0000934 inode = d_inode(req->r_dentry);
Jeff Layton30c71232016-12-15 08:37:56 -0500935 if (inode)
936 ihold(inode);
Sage Weileb6bb1c2010-08-16 09:21:27 -0700937 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
938 /* direct snapped/virtual snapdir requests
939 * based on parent dir inode */
Jeff Layton30c71232016-12-15 08:37:56 -0500940 inode = get_nonsnap_parent(parent);
Sage Weileb6bb1c2010-08-16 09:21:27 -0700941 dout("__choose_mds using nonsnap parent %p\n", inode);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800942 } else {
Sage Weileb6bb1c2010-08-16 09:21:27 -0700943 /* dentry target */
David Howells2b0143b2015-03-17 22:25:59 +0000944 inode = d_inode(req->r_dentry);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800945 if (!inode || mode == USE_AUTH_MDS) {
946 /* dir + name */
Jeff Layton30c71232016-12-15 08:37:56 -0500947 inode = igrab(dir);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800948 hash = ceph_dentry_hash(dir, req->r_dentry);
949 is_hash = true;
Jeff Layton30c71232016-12-15 08:37:56 -0500950 } else {
951 ihold(inode);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800952 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700953 }
Jeff Layton30c71232016-12-15 08:37:56 -0500954 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -0700955 }
Sage Weileb6bb1c2010-08-16 09:21:27 -0700956
Sage Weil2f2dc052009-10-06 11:31:09 -0700957 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
958 (int)hash, mode);
959 if (!inode)
960 goto random;
961 ci = ceph_inode(inode);
962
963 if (is_hash && S_ISDIR(inode->i_mode)) {
964 struct ceph_inode_frag frag;
965 int found;
966
967 ceph_choose_frag(ci, hash, &frag, &found);
968 if (found) {
969 if (mode == USE_ANY_MDS && frag.ndist > 0) {
970 u8 r;
971
972 /* choose a random replica */
973 get_random_bytes(&r, 1);
974 r %= frag.ndist;
975 mds = frag.dist[r];
976 dout("choose_mds %p %llx.%llx "
977 "frag %u mds%d (%d/%d)\n",
978 inode, ceph_vinop(inode),
Sage Weild66bbd42011-01-21 21:16:46 -0800979 frag.frag, mds,
Sage Weil2f2dc052009-10-06 11:31:09 -0700980 (int)r, frag.ndist);
Sage Weild66bbd42011-01-21 21:16:46 -0800981 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
Xiubo Li5d476482019-11-26 07:24:22 -0500982 CEPH_MDS_STATE_ACTIVE &&
983 !ceph_mdsmap_is_laggy(mdsc->mdsmap, mds))
Jeff Layton30c71232016-12-15 08:37:56 -0500984 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -0700985 }
986
987 /* since this file/dir wasn't known to be
988 * replicated, then we want to look for the
989 * authoritative mds. */
Sage Weil2f2dc052009-10-06 11:31:09 -0700990 if (frag.mds >= 0) {
991 /* choose auth mds */
992 mds = frag.mds;
993 dout("choose_mds %p %llx.%llx "
994 "frag %u mds%d (auth)\n",
995 inode, ceph_vinop(inode), frag.frag, mds);
Sage Weild66bbd42011-01-21 21:16:46 -0800996 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
Xiubo Li5d476482019-11-26 07:24:22 -0500997 CEPH_MDS_STATE_ACTIVE) {
998 if (mode == USE_ANY_MDS &&
999 !ceph_mdsmap_is_laggy(mdsc->mdsmap,
1000 mds))
1001 goto out;
1002 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001003 }
Xiubo Li5d476482019-11-26 07:24:22 -05001004 mode = USE_AUTH_MDS;
Sage Weil2f2dc052009-10-06 11:31:09 -07001005 }
1006 }
1007
Sage Weilbe655592011-11-30 09:47:09 -08001008 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001009 cap = NULL;
1010 if (mode == USE_AUTH_MDS)
1011 cap = ci->i_auth_cap;
1012 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
1013 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
1014 if (!cap) {
Sage Weilbe655592011-11-30 09:47:09 -08001015 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng3e1d0452019-05-18 20:39:55 +08001016 ceph_async_iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07001017 goto random;
1018 }
1019 mds = cap->session->s_mds;
1020 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
1021 inode, ceph_vinop(inode), mds,
1022 cap == ci->i_auth_cap ? "auth " : "", cap);
Sage Weilbe655592011-11-30 09:47:09 -08001023 spin_unlock(&ci->i_ceph_lock);
Jeff Layton30c71232016-12-15 08:37:56 -05001024out:
Yan, Zheng3e1d0452019-05-18 20:39:55 +08001025 /* avoid calling iput_final() while holding mdsc->mutex or
1026 * in mds dispatch threads */
1027 ceph_async_iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07001028 return mds;
1029
1030random:
Xiubo Lic4853e92019-12-09 07:47:15 -05001031 if (random)
1032 *random = true;
1033
Sage Weil2f2dc052009-10-06 11:31:09 -07001034 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
1035 dout("choose_mds chose random mds%d\n", mds);
1036 return mds;
1037}
1038
1039
1040/*
1041 * session messages
1042 */
1043static struct ceph_msg *create_session_msg(u32 op, u64 seq)
1044{
1045 struct ceph_msg *msg;
1046 struct ceph_mds_session_head *h;
1047
Sage Weilb61c2762011-08-09 15:03:46 -07001048 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
1049 false);
Sage Weila79832f2010-04-01 16:06:19 -07001050 if (!msg) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001051 pr_err("create_session_msg ENOMEM creating msg\n");
Sage Weila79832f2010-04-01 16:06:19 -07001052 return NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001053 }
1054 h = msg->front.iov_base;
1055 h->op = cpu_to_le32(op);
1056 h->seq = cpu_to_le64(seq);
John Spraydbd0c8b2014-09-09 19:26:01 +01001057
1058 return msg;
1059}
1060
Xiubo Li9ba1e222020-01-08 05:17:31 -05001061static const unsigned char feature_bits[] = CEPHFS_FEATURES_CLIENT_SUPPORTED;
1062#define FEATURE_BYTES(c) (DIV_ROUND_UP((size_t)feature_bits[c - 1] + 1, 64) * 8)
Yan, Zheng342ce182018-05-11 18:47:29 +08001063static void encode_supported_features(void **p, void *end)
1064{
Xiubo Li9ba1e222020-01-08 05:17:31 -05001065 static const size_t count = ARRAY_SIZE(feature_bits);
Yan, Zheng342ce182018-05-11 18:47:29 +08001066
1067 if (count > 0) {
1068 size_t i;
Xiubo Li9ba1e222020-01-08 05:17:31 -05001069 size_t size = FEATURE_BYTES(count);
Yan, Zheng342ce182018-05-11 18:47:29 +08001070
1071 BUG_ON(*p + 4 + size > end);
1072 ceph_encode_32(p, size);
1073 memset(*p, 0, size);
1074 for (i = 0; i < count; i++)
Xiubo Li9ba1e222020-01-08 05:17:31 -05001075 ((unsigned char*)(*p))[i / 8] |= BIT(feature_bits[i] % 8);
Yan, Zheng342ce182018-05-11 18:47:29 +08001076 *p += size;
1077 } else {
1078 BUG_ON(*p + 4 > end);
1079 ceph_encode_32(p, 0);
1080 }
1081}
1082
John Spraydbd0c8b2014-09-09 19:26:01 +01001083/*
1084 * session message, specialization for CEPH_SESSION_REQUEST_OPEN
1085 * to include additional client metadata fields.
1086 */
1087static struct ceph_msg *create_session_open_msg(struct ceph_mds_client *mdsc, u64 seq)
1088{
1089 struct ceph_msg *msg;
1090 struct ceph_mds_session_head *h;
1091 int i = -1;
Yan, Zheng342ce182018-05-11 18:47:29 +08001092 int extra_bytes = 0;
John Spraydbd0c8b2014-09-09 19:26:01 +01001093 int metadata_key_count = 0;
1094 struct ceph_options *opt = mdsc->fsc->client->options;
Yan, Zheng3f384952016-04-21 11:09:55 +08001095 struct ceph_mount_options *fsopt = mdsc->fsc->mount_options;
Xiubo Li9ba1e222020-01-08 05:17:31 -05001096 size_t size, count;
Yan, Zheng342ce182018-05-11 18:47:29 +08001097 void *p, *end;
John Spraydbd0c8b2014-09-09 19:26:01 +01001098
Yan, Zhenga6a5ce42015-01-16 10:54:43 +08001099 const char* metadata[][2] = {
Yan, Zheng717e6f22017-09-11 12:10:08 +08001100 {"hostname", mdsc->nodename},
1101 {"kernel_version", init_utsname()->release},
Yan, Zheng3f384952016-04-21 11:09:55 +08001102 {"entity_id", opt->name ? : ""},
1103 {"root", fsopt->server_path ? : "/"},
John Spraydbd0c8b2014-09-09 19:26:01 +01001104 {NULL, NULL}
1105 };
1106
1107 /* Calculate serialized length of metadata */
Yan, Zheng342ce182018-05-11 18:47:29 +08001108 extra_bytes = 4; /* map length */
Markus Elfringd37b1d92017-08-20 20:22:02 +02001109 for (i = 0; metadata[i][0]; ++i) {
Yan, Zheng342ce182018-05-11 18:47:29 +08001110 extra_bytes += 8 + strlen(metadata[i][0]) +
John Spraydbd0c8b2014-09-09 19:26:01 +01001111 strlen(metadata[i][1]);
1112 metadata_key_count++;
1113 }
Xiubo Li9ba1e222020-01-08 05:17:31 -05001114
Yan, Zheng342ce182018-05-11 18:47:29 +08001115 /* supported feature */
Xiubo Li9ba1e222020-01-08 05:17:31 -05001116 size = 0;
1117 count = ARRAY_SIZE(feature_bits);
1118 if (count > 0)
1119 size = FEATURE_BYTES(count);
1120 extra_bytes += 4 + size;
John Spraydbd0c8b2014-09-09 19:26:01 +01001121
1122 /* Allocate the message */
Yan, Zheng342ce182018-05-11 18:47:29 +08001123 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + extra_bytes,
John Spraydbd0c8b2014-09-09 19:26:01 +01001124 GFP_NOFS, false);
1125 if (!msg) {
1126 pr_err("create_session_msg ENOMEM creating msg\n");
1127 return NULL;
1128 }
Yan, Zheng342ce182018-05-11 18:47:29 +08001129 p = msg->front.iov_base;
1130 end = p + msg->front.iov_len;
1131
1132 h = p;
John Spraydbd0c8b2014-09-09 19:26:01 +01001133 h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN);
1134 h->seq = cpu_to_le64(seq);
1135
1136 /*
1137 * Serialize client metadata into waiting buffer space, using
1138 * the format that userspace expects for map<string, string>
John Spray7cfa0312014-10-30 17:15:26 +00001139 *
Xiubo Li9ba1e222020-01-08 05:17:31 -05001140 * ClientSession messages with metadata are v3
John Spraydbd0c8b2014-09-09 19:26:01 +01001141 */
Yan, Zheng342ce182018-05-11 18:47:29 +08001142 msg->hdr.version = cpu_to_le16(3);
John Spray7cfa0312014-10-30 17:15:26 +00001143 msg->hdr.compat_version = cpu_to_le16(1);
John Spraydbd0c8b2014-09-09 19:26:01 +01001144
1145 /* The write pointer, following the session_head structure */
Yan, Zheng342ce182018-05-11 18:47:29 +08001146 p += sizeof(*h);
John Spraydbd0c8b2014-09-09 19:26:01 +01001147
1148 /* Number of entries in the map */
1149 ceph_encode_32(&p, metadata_key_count);
1150
1151 /* Two length-prefixed strings for each entry in the map */
Markus Elfringd37b1d92017-08-20 20:22:02 +02001152 for (i = 0; metadata[i][0]; ++i) {
John Spraydbd0c8b2014-09-09 19:26:01 +01001153 size_t const key_len = strlen(metadata[i][0]);
1154 size_t const val_len = strlen(metadata[i][1]);
1155
1156 ceph_encode_32(&p, key_len);
1157 memcpy(p, metadata[i][0], key_len);
1158 p += key_len;
1159 ceph_encode_32(&p, val_len);
1160 memcpy(p, metadata[i][1], val_len);
1161 p += val_len;
1162 }
1163
Yan, Zheng342ce182018-05-11 18:47:29 +08001164 encode_supported_features(&p, end);
1165 msg->front.iov_len = p - msg->front.iov_base;
1166 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1167
Sage Weil2f2dc052009-10-06 11:31:09 -07001168 return msg;
1169}
1170
1171/*
1172 * send session open request.
1173 *
1174 * called under mdsc->mutex
1175 */
1176static int __open_session(struct ceph_mds_client *mdsc,
1177 struct ceph_mds_session *session)
1178{
1179 struct ceph_msg *msg;
1180 int mstate;
1181 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07001182
1183 /* wait for mds to go active? */
1184 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
1185 dout("open_session to mds%d (%s)\n", mds,
1186 ceph_mds_state_name(mstate));
1187 session->s_state = CEPH_MDS_SESSION_OPENING;
1188 session->s_renew_requested = jiffies;
1189
1190 /* send connect message */
John Spraydbd0c8b2014-09-09 19:26:01 +01001191 msg = create_session_open_msg(mdsc, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001192 if (!msg)
1193 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -07001194 ceph_con_send(&session->s_con, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001195 return 0;
1196}
1197
1198/*
Sage Weiled0552a2010-06-21 13:38:25 -07001199 * open sessions for any export targets for the given mds
1200 *
1201 * called under mdsc->mutex
1202 */
Yan, Zheng5d72d132013-11-24 14:33:01 +08001203static struct ceph_mds_session *
1204__open_export_target_session(struct ceph_mds_client *mdsc, int target)
1205{
1206 struct ceph_mds_session *session;
1207
1208 session = __ceph_lookup_mds_session(mdsc, target);
1209 if (!session) {
1210 session = register_session(mdsc, target);
1211 if (IS_ERR(session))
1212 return session;
1213 }
1214 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1215 session->s_state == CEPH_MDS_SESSION_CLOSING)
1216 __open_session(mdsc, session);
1217
1218 return session;
1219}
1220
1221struct ceph_mds_session *
1222ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target)
1223{
1224 struct ceph_mds_session *session;
1225
1226 dout("open_export_target_session to mds%d\n", target);
1227
1228 mutex_lock(&mdsc->mutex);
1229 session = __open_export_target_session(mdsc, target);
1230 mutex_unlock(&mdsc->mutex);
1231
1232 return session;
1233}
1234
Sage Weiled0552a2010-06-21 13:38:25 -07001235static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
1236 struct ceph_mds_session *session)
1237{
1238 struct ceph_mds_info *mi;
1239 struct ceph_mds_session *ts;
1240 int i, mds = session->s_mds;
Sage Weiled0552a2010-06-21 13:38:25 -07001241
Xiubo Lib38c9eb2019-12-04 06:57:39 -05001242 if (mds >= mdsc->mdsmap->possible_max_rank)
Sage Weiled0552a2010-06-21 13:38:25 -07001243 return;
Yan, Zheng5d72d132013-11-24 14:33:01 +08001244
Sage Weiled0552a2010-06-21 13:38:25 -07001245 mi = &mdsc->mdsmap->m_info[mds];
1246 dout("open_export_target_sessions for mds%d (%d targets)\n",
1247 session->s_mds, mi->num_export_targets);
1248
1249 for (i = 0; i < mi->num_export_targets; i++) {
Yan, Zheng5d72d132013-11-24 14:33:01 +08001250 ts = __open_export_target_session(mdsc, mi->export_targets[i]);
1251 if (!IS_ERR(ts))
1252 ceph_put_mds_session(ts);
Sage Weiled0552a2010-06-21 13:38:25 -07001253 }
1254}
1255
Sage Weil154f42c2010-06-21 13:45:04 -07001256void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
1257 struct ceph_mds_session *session)
1258{
1259 mutex_lock(&mdsc->mutex);
1260 __open_export_target_sessions(mdsc, session);
1261 mutex_unlock(&mdsc->mutex);
1262}
1263
Sage Weiled0552a2010-06-21 13:38:25 -07001264/*
Sage Weil2f2dc052009-10-06 11:31:09 -07001265 * session caps
1266 */
1267
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001268static void detach_cap_releases(struct ceph_mds_session *session,
1269 struct list_head *target)
Sage Weil2f2dc052009-10-06 11:31:09 -07001270{
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001271 lockdep_assert_held(&session->s_cap_lock);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001272
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001273 list_splice_init(&session->s_cap_releases, target);
1274 session->s_num_cap_releases = 0;
1275 dout("dispose_cap_releases mds%d\n", session->s_mds);
1276}
1277
1278static void dispose_cap_releases(struct ceph_mds_client *mdsc,
1279 struct list_head *dispose)
1280{
1281 while (!list_empty(dispose)) {
Yan, Zheng745a8e32015-05-14 17:22:42 +08001282 struct ceph_cap *cap;
1283 /* zero out the in-progress message */
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001284 cap = list_first_entry(dispose, struct ceph_cap, session_caps);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001285 list_del(&cap->session_caps);
1286 ceph_put_cap(mdsc, cap);
1287 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001288}
1289
Yan, Zheng1c841a92015-03-24 20:15:36 +08001290static void cleanup_session_requests(struct ceph_mds_client *mdsc,
1291 struct ceph_mds_session *session)
1292{
1293 struct ceph_mds_request *req;
1294 struct rb_node *p;
Yan, Zhengf4b97862019-07-25 20:16:42 +08001295 struct ceph_inode_info *ci;
Yan, Zheng1c841a92015-03-24 20:15:36 +08001296
1297 dout("cleanup_session_requests mds%d\n", session->s_mds);
1298 mutex_lock(&mdsc->mutex);
1299 while (!list_empty(&session->s_unsafe)) {
1300 req = list_first_entry(&session->s_unsafe,
1301 struct ceph_mds_request, r_unsafe_item);
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001302 pr_warn_ratelimited(" dropping unsafe request %llu\n",
1303 req->r_tid);
Yan, Zhengf4b97862019-07-25 20:16:42 +08001304 if (req->r_target_inode) {
1305 /* dropping unsafe change of inode's attributes */
1306 ci = ceph_inode(req->r_target_inode);
1307 errseq_set(&ci->i_meta_err, -EIO);
1308 }
1309 if (req->r_unsafe_dir) {
1310 /* dropping unsafe directory operation */
1311 ci = ceph_inode(req->r_unsafe_dir);
1312 errseq_set(&ci->i_meta_err, -EIO);
1313 }
Yan, Zheng1c841a92015-03-24 20:15:36 +08001314 __unregister_request(mdsc, req);
1315 }
1316 /* zero r_attempts, so kick_requests() will re-send requests */
1317 p = rb_first(&mdsc->request_tree);
1318 while (p) {
1319 req = rb_entry(p, struct ceph_mds_request, r_node);
1320 p = rb_next(p);
1321 if (req->r_session &&
1322 req->r_session->s_mds == session->s_mds)
1323 req->r_attempts = 0;
1324 }
1325 mutex_unlock(&mdsc->mutex);
1326}
1327
Sage Weil2f2dc052009-10-06 11:31:09 -07001328/*
Sage Weilf818a732010-05-11 20:56:31 -07001329 * Helper to safely iterate over all caps associated with a session, with
1330 * special care taken to handle a racing __ceph_remove_cap().
Sage Weil2f2dc052009-10-06 11:31:09 -07001331 *
Sage Weilf818a732010-05-11 20:56:31 -07001332 * Caller must hold session s_mutex.
Sage Weil2f2dc052009-10-06 11:31:09 -07001333 */
Jeff Laytonf5d772692019-04-24 12:09:04 -04001334int ceph_iterate_session_caps(struct ceph_mds_session *session,
1335 int (*cb)(struct inode *, struct ceph_cap *,
1336 void *), void *arg)
Sage Weil2f2dc052009-10-06 11:31:09 -07001337{
Sage Weil7c1332b2010-02-16 11:39:45 -08001338 struct list_head *p;
1339 struct ceph_cap *cap;
1340 struct inode *inode, *last_inode = NULL;
1341 struct ceph_cap *old_cap = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001342 int ret;
1343
1344 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
1345 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001346 p = session->s_caps.next;
1347 while (p != &session->s_caps) {
1348 cap = list_entry(p, struct ceph_cap, session_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -07001349 inode = igrab(&cap->ci->vfs_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001350 if (!inode) {
1351 p = p->next;
Sage Weil2f2dc052009-10-06 11:31:09 -07001352 continue;
Sage Weil7c1332b2010-02-16 11:39:45 -08001353 }
1354 session->s_cap_iterator = cap;
Sage Weil2f2dc052009-10-06 11:31:09 -07001355 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001356
1357 if (last_inode) {
Yan, Zheng3e1d0452019-05-18 20:39:55 +08001358 /* avoid calling iput_final() while holding
1359 * s_mutex or in mds dispatch threads */
1360 ceph_async_iput(last_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001361 last_inode = NULL;
1362 }
1363 if (old_cap) {
Yehuda Sadeh37151662010-06-17 16:16:12 -07001364 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -08001365 old_cap = NULL;
1366 }
1367
Sage Weil2f2dc052009-10-06 11:31:09 -07001368 ret = cb(inode, cap, arg);
Sage Weil7c1332b2010-02-16 11:39:45 -08001369 last_inode = inode;
1370
Sage Weil2f2dc052009-10-06 11:31:09 -07001371 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001372 p = p->next;
Markus Elfringd37b1d92017-08-20 20:22:02 +02001373 if (!cap->ci) {
Sage Weil7c1332b2010-02-16 11:39:45 -08001374 dout("iterate_session_caps finishing cap %p removal\n",
1375 cap);
1376 BUG_ON(cap->session != session);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001377 cap->session = NULL;
Sage Weil7c1332b2010-02-16 11:39:45 -08001378 list_del_init(&cap->session_caps);
1379 session->s_nr_caps--;
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08001380 if (cap->queue_release)
1381 __ceph_queue_cap_release(session, cap);
1382 else
Yan, Zheng745a8e32015-05-14 17:22:42 +08001383 old_cap = cap; /* put_cap it w/o locks held */
Sage Weil7c1332b2010-02-16 11:39:45 -08001384 }
Sage Weil5dacf092009-12-21 20:40:34 -08001385 if (ret < 0)
1386 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07001387 }
Sage Weil5dacf092009-12-21 20:40:34 -08001388 ret = 0;
1389out:
Sage Weil7c1332b2010-02-16 11:39:45 -08001390 session->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001391 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001392
Yan, Zheng3e1d0452019-05-18 20:39:55 +08001393 ceph_async_iput(last_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001394 if (old_cap)
Yehuda Sadeh37151662010-06-17 16:16:12 -07001395 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -08001396
Sage Weil5dacf092009-12-21 20:40:34 -08001397 return ret;
Sage Weil2f2dc052009-10-06 11:31:09 -07001398}
1399
1400static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
Sage Weil6c99f252010-05-10 16:12:25 -07001401 void *arg)
Sage Weil2f2dc052009-10-06 11:31:09 -07001402{
Yan, Zheng6c93df52016-04-15 13:56:12 +08001403 struct ceph_fs_client *fsc = (struct ceph_fs_client *)arg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001404 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001405 LIST_HEAD(to_remove);
Yan, Zhengf4b97862019-07-25 20:16:42 +08001406 bool dirty_dropped = false;
Yan, Zheng6c93df52016-04-15 13:56:12 +08001407 bool invalidate = false;
Sage Weil6c99f252010-05-10 16:12:25 -07001408
Sage Weil2f2dc052009-10-06 11:31:09 -07001409 dout("removing cap %p, ci is %p, inode is %p\n",
1410 cap, ci, &ci->vfs_inode);
Sage Weilbe655592011-11-30 09:47:09 -08001411 spin_lock(&ci->i_ceph_lock);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001412 if (cap->mds_wanted | cap->issued)
1413 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
Yan, Zhenga096b092013-09-22 10:15:58 +08001414 __ceph_remove_cap(cap, false);
Yan, Zheng571ade32015-03-24 11:36:08 +08001415 if (!ci->i_auth_cap) {
Yan, Zheng553adfd2015-06-09 15:48:57 +08001416 struct ceph_cap_flush *cf;
Yan, Zheng6c93df52016-04-15 13:56:12 +08001417 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil6c99f252010-05-10 16:12:25 -07001418
Yan, Zhengd468e722019-07-25 20:16:44 +08001419 if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
1420 if (inode->i_data.nrpages > 0)
1421 invalidate = true;
1422 if (ci->i_wrbuffer_ref > 0)
1423 mapping_set_error(&inode->i_data, -EIO);
1424 }
Yan, Zheng6c93df52016-04-15 13:56:12 +08001425
Yan, Zhenge4500b52016-07-06 11:12:56 +08001426 while (!list_empty(&ci->i_cap_flush_list)) {
1427 cf = list_first_entry(&ci->i_cap_flush_list,
1428 struct ceph_cap_flush, i_list);
Wei Yongjun8cdcc072016-08-13 09:10:28 +00001429 list_move(&cf->i_list, &to_remove);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001430 }
1431
Sage Weil6c99f252010-05-10 16:12:25 -07001432 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zheng8310b082015-06-09 17:20:12 +08001433
Yan, Zhenge4500b52016-07-06 11:12:56 +08001434 list_for_each_entry(cf, &to_remove, i_list)
1435 list_del(&cf->g_list);
Yan, Zheng8310b082015-06-09 17:20:12 +08001436
Sage Weil6c99f252010-05-10 16:12:25 -07001437 if (!list_empty(&ci->i_dirty_item)) {
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001438 pr_warn_ratelimited(
1439 " dropping dirty %s state for %p %lld\n",
Sage Weil6c99f252010-05-10 16:12:25 -07001440 ceph_cap_string(ci->i_dirty_caps),
1441 inode, ceph_ino(inode));
1442 ci->i_dirty_caps = 0;
1443 list_del_init(&ci->i_dirty_item);
Yan, Zhengf4b97862019-07-25 20:16:42 +08001444 dirty_dropped = true;
Sage Weil6c99f252010-05-10 16:12:25 -07001445 }
1446 if (!list_empty(&ci->i_flushing_item)) {
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001447 pr_warn_ratelimited(
1448 " dropping dirty+flushing %s state for %p %lld\n",
Sage Weil6c99f252010-05-10 16:12:25 -07001449 ceph_cap_string(ci->i_flushing_caps),
1450 inode, ceph_ino(inode));
1451 ci->i_flushing_caps = 0;
1452 list_del_init(&ci->i_flushing_item);
1453 mdsc->num_cap_flushing--;
Yan, Zhengf4b97862019-07-25 20:16:42 +08001454 dirty_dropped = true;
Sage Weil6c99f252010-05-10 16:12:25 -07001455 }
Sage Weil6c99f252010-05-10 16:12:25 -07001456 spin_unlock(&mdsc->cap_dirty_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001457
Yan, Zhengf4b97862019-07-25 20:16:42 +08001458 if (dirty_dropped) {
1459 errseq_set(&ci->i_meta_err, -EIO);
1460
1461 if (ci->i_wrbuffer_ref_head == 0 &&
1462 ci->i_wr_ref == 0 &&
1463 ci->i_dirty_caps == 0 &&
1464 ci->i_flushing_caps == 0) {
1465 ceph_put_snap_context(ci->i_head_snapc);
1466 ci->i_head_snapc = NULL;
1467 }
1468 }
1469
Yan, Zhengb3f8d682017-09-11 10:58:55 +08001470 if (atomic_read(&ci->i_filelock_ref) > 0) {
1471 /* make further file lock syscall return -EIO */
1472 ci->i_ceph_flags |= CEPH_I_ERROR_FILELOCK;
1473 pr_warn_ratelimited(" dropping file locks for %p %lld\n",
1474 inode, ceph_ino(inode));
1475 }
1476
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001477 if (!ci->i_dirty_caps && ci->i_prealloc_cap_flush) {
Yan, Zhenge4500b52016-07-06 11:12:56 +08001478 list_add(&ci->i_prealloc_cap_flush->i_list, &to_remove);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001479 ci->i_prealloc_cap_flush = NULL;
1480 }
Sage Weil6c99f252010-05-10 16:12:25 -07001481 }
Sage Weilbe655592011-11-30 09:47:09 -08001482 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001483 while (!list_empty(&to_remove)) {
1484 struct ceph_cap_flush *cf;
1485 cf = list_first_entry(&to_remove,
Yan, Zhenge4500b52016-07-06 11:12:56 +08001486 struct ceph_cap_flush, i_list);
1487 list_del(&cf->i_list);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001488 ceph_free_cap_flush(cf);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001489 }
Yan, Zheng77310322016-04-08 15:27:16 +08001490
1491 wake_up_all(&ci->i_cap_wq);
Yan, Zheng6c93df52016-04-15 13:56:12 +08001492 if (invalidate)
1493 ceph_queue_invalidate(inode);
Yan, Zhengf4b97862019-07-25 20:16:42 +08001494 if (dirty_dropped)
Sage Weil6c99f252010-05-10 16:12:25 -07001495 iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07001496 return 0;
1497}
1498
1499/*
1500 * caller must hold session s_mutex
1501 */
1502static void remove_session_caps(struct ceph_mds_session *session)
1503{
Yan, Zheng6c93df52016-04-15 13:56:12 +08001504 struct ceph_fs_client *fsc = session->s_mdsc->fsc;
1505 struct super_block *sb = fsc->sb;
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001506 LIST_HEAD(dispose);
1507
Sage Weil2f2dc052009-10-06 11:31:09 -07001508 dout("remove_session_caps on %p\n", session);
Jeff Laytonf5d772692019-04-24 12:09:04 -04001509 ceph_iterate_session_caps(session, remove_session_caps_cb, fsc);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001510
Yan, Zhengc8799fc2016-07-07 15:22:38 +08001511 wake_up_all(&fsc->mdsc->cap_flushing_wq);
1512
Yan, Zheng6f60f882013-07-24 12:22:11 +08001513 spin_lock(&session->s_cap_lock);
1514 if (session->s_nr_caps > 0) {
Yan, Zheng6f60f882013-07-24 12:22:11 +08001515 struct inode *inode;
1516 struct ceph_cap *cap, *prev = NULL;
1517 struct ceph_vino vino;
1518 /*
1519 * iterate_session_caps() skips inodes that are being
1520 * deleted, we need to wait until deletions are complete.
1521 * __wait_on_freeing_inode() is designed for the job,
1522 * but it is not exported, so use lookup inode function
1523 * to access it.
1524 */
1525 while (!list_empty(&session->s_caps)) {
1526 cap = list_entry(session->s_caps.next,
1527 struct ceph_cap, session_caps);
1528 if (cap == prev)
1529 break;
1530 prev = cap;
1531 vino = cap->ci->i_vino;
1532 spin_unlock(&session->s_cap_lock);
1533
Yan, Zhenged284c42013-09-02 15:19:53 +08001534 inode = ceph_find_inode(sb, vino);
Yan, Zheng3e1d0452019-05-18 20:39:55 +08001535 /* avoid calling iput_final() while holding s_mutex */
1536 ceph_async_iput(inode);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001537
1538 spin_lock(&session->s_cap_lock);
1539 }
1540 }
Yan, Zheng745a8e32015-05-14 17:22:42 +08001541
1542 // drop cap expires and unlock s_cap_lock
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001543 detach_cap_releases(session, &dispose);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001544
Sage Weil2f2dc052009-10-06 11:31:09 -07001545 BUG_ON(session->s_nr_caps > 0);
Sage Weil6c99f252010-05-10 16:12:25 -07001546 BUG_ON(!list_empty(&session->s_cap_flushing));
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001547 spin_unlock(&session->s_cap_lock);
1548 dispose_cap_releases(session->s_mdsc, &dispose);
Sage Weil2f2dc052009-10-06 11:31:09 -07001549}
1550
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001551enum {
1552 RECONNECT,
1553 RENEWCAPS,
1554 FORCE_RO,
1555};
1556
Sage Weil2f2dc052009-10-06 11:31:09 -07001557/*
1558 * wake up any threads waiting on this session's caps. if the cap is
1559 * old (didn't get renewed on the client reconnect), remove it now.
1560 *
1561 * caller must hold s_mutex.
1562 */
1563static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1564 void *arg)
1565{
Sage Weil0dc25702009-11-20 13:43:45 -08001566 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001567 unsigned long ev = (unsigned long)arg;
Sage Weil0dc25702009-11-20 13:43:45 -08001568
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001569 if (ev == RECONNECT) {
Sage Weilbe655592011-11-30 09:47:09 -08001570 spin_lock(&ci->i_ceph_lock);
Sage Weil0dc25702009-11-20 13:43:45 -08001571 ci->i_wanted_max_size = 0;
1572 ci->i_requested_max_size = 0;
Sage Weilbe655592011-11-30 09:47:09 -08001573 spin_unlock(&ci->i_ceph_lock);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001574 } else if (ev == RENEWCAPS) {
1575 if (cap->cap_gen < cap->session->s_cap_gen) {
1576 /* mds did not re-issue stale cap */
1577 spin_lock(&ci->i_ceph_lock);
1578 cap->issued = cap->implemented = CEPH_CAP_PIN;
1579 /* make sure mds knows what we want */
1580 if (__ceph_caps_file_wanted(ci) & ~cap->mds_wanted)
1581 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
1582 spin_unlock(&ci->i_ceph_lock);
1583 }
1584 } else if (ev == FORCE_RO) {
Sage Weil0dc25702009-11-20 13:43:45 -08001585 }
Yan, Zhenge5360302016-05-19 19:15:19 +08001586 wake_up_all(&ci->i_cap_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07001587 return 0;
1588}
1589
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001590static void wake_up_session_caps(struct ceph_mds_session *session, int ev)
Sage Weil2f2dc052009-10-06 11:31:09 -07001591{
1592 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
Jeff Laytonf5d772692019-04-24 12:09:04 -04001593 ceph_iterate_session_caps(session, wake_up_session_cb,
1594 (void *)(unsigned long)ev);
Sage Weil2f2dc052009-10-06 11:31:09 -07001595}
1596
1597/*
1598 * Send periodic message to MDS renewing all currently held caps. The
1599 * ack will reset the expiration for all caps from this session.
1600 *
1601 * caller holds s_mutex
1602 */
1603static int send_renew_caps(struct ceph_mds_client *mdsc,
1604 struct ceph_mds_session *session)
1605{
1606 struct ceph_msg *msg;
1607 int state;
1608
1609 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1610 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1611 pr_info("mds%d caps stale\n", session->s_mds);
Sage Weile4cb4cb2010-03-18 13:43:09 -07001612 session->s_renew_requested = jiffies;
Sage Weil2f2dc052009-10-06 11:31:09 -07001613
1614 /* do not try to renew caps until a recovering mds has reconnected
1615 * with its clients. */
1616 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1617 if (state < CEPH_MDS_STATE_RECONNECT) {
1618 dout("send_renew_caps ignoring mds%d (%s)\n",
1619 session->s_mds, ceph_mds_state_name(state));
1620 return 0;
1621 }
1622
1623 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1624 ceph_mds_state_name(state));
Sage Weil2f2dc052009-10-06 11:31:09 -07001625 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1626 ++session->s_renew_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001627 if (!msg)
1628 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -07001629 ceph_con_send(&session->s_con, msg);
1630 return 0;
1631}
1632
Yan, Zheng186e4f72013-11-22 14:48:37 +08001633static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
1634 struct ceph_mds_session *session, u64 seq)
1635{
1636 struct ceph_msg *msg;
1637
1638 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
John Spraya687eca2014-09-19 13:51:08 +01001639 session->s_mds, ceph_session_state_name(session->s_state), seq);
Yan, Zheng186e4f72013-11-22 14:48:37 +08001640 msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
1641 if (!msg)
1642 return -ENOMEM;
1643 ceph_con_send(&session->s_con, msg);
1644 return 0;
1645}
1646
1647
Sage Weil2f2dc052009-10-06 11:31:09 -07001648/*
1649 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
Sage Weil0dc25702009-11-20 13:43:45 -08001650 *
1651 * Called under session->s_mutex
Sage Weil2f2dc052009-10-06 11:31:09 -07001652 */
1653static void renewed_caps(struct ceph_mds_client *mdsc,
1654 struct ceph_mds_session *session, int is_renew)
1655{
1656 int was_stale;
1657 int wake = 0;
1658
1659 spin_lock(&session->s_cap_lock);
Alex Elder1ce208a2012-01-12 17:48:11 -08001660 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
Sage Weil2f2dc052009-10-06 11:31:09 -07001661
1662 session->s_cap_ttl = session->s_renew_requested +
1663 mdsc->mdsmap->m_session_timeout*HZ;
1664
1665 if (was_stale) {
1666 if (time_before(jiffies, session->s_cap_ttl)) {
1667 pr_info("mds%d caps renewed\n", session->s_mds);
1668 wake = 1;
1669 } else {
1670 pr_info("mds%d caps still stale\n", session->s_mds);
1671 }
1672 }
1673 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1674 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1675 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1676 spin_unlock(&session->s_cap_lock);
1677
1678 if (wake)
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001679 wake_up_session_caps(session, RENEWCAPS);
Sage Weil2f2dc052009-10-06 11:31:09 -07001680}
1681
1682/*
1683 * send a session close request
1684 */
1685static int request_close_session(struct ceph_mds_client *mdsc,
1686 struct ceph_mds_session *session)
1687{
1688 struct ceph_msg *msg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001689
1690 dout("request_close_session mds%d state %s seq %lld\n",
John Spraya687eca2014-09-19 13:51:08 +01001691 session->s_mds, ceph_session_state_name(session->s_state),
Sage Weil2f2dc052009-10-06 11:31:09 -07001692 session->s_seq);
1693 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001694 if (!msg)
1695 return -ENOMEM;
1696 ceph_con_send(&session->s_con, msg);
Yan, Zhengfcff4152016-09-14 16:39:51 +08001697 return 1;
Sage Weil2f2dc052009-10-06 11:31:09 -07001698}
1699
1700/*
1701 * Called with s_mutex held.
1702 */
1703static int __close_session(struct ceph_mds_client *mdsc,
1704 struct ceph_mds_session *session)
1705{
1706 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1707 return 0;
1708 session->s_state = CEPH_MDS_SESSION_CLOSING;
1709 return request_close_session(mdsc, session);
1710}
1711
Yan, Zheng040d7862017-11-30 11:59:22 +08001712static bool drop_negative_children(struct dentry *dentry)
1713{
1714 struct dentry *child;
1715 bool all_negative = true;
1716
1717 if (!d_is_dir(dentry))
1718 goto out;
1719
1720 spin_lock(&dentry->d_lock);
1721 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
1722 if (d_really_is_positive(child)) {
1723 all_negative = false;
1724 break;
1725 }
1726 }
1727 spin_unlock(&dentry->d_lock);
1728
1729 if (all_negative)
1730 shrink_dcache_parent(dentry);
1731out:
1732 return all_negative;
1733}
1734
Sage Weil2f2dc052009-10-06 11:31:09 -07001735/*
1736 * Trim old(er) caps.
1737 *
1738 * Because we can't cache an inode without one or more caps, we do
1739 * this indirectly: if a cap is unused, we prune its aliases, at which
1740 * point the inode will hopefully get dropped to.
1741 *
1742 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1743 * memory pressure from the MDS, though, so it needn't be perfect.
1744 */
1745static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1746{
Jeff Layton533a2812019-07-19 15:22:28 -04001747 int *remaining = arg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001748 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng979abfd2013-11-22 13:56:24 +08001749 int used, wanted, oissued, mine;
Sage Weil2f2dc052009-10-06 11:31:09 -07001750
Jeff Layton533a2812019-07-19 15:22:28 -04001751 if (*remaining <= 0)
Sage Weil2f2dc052009-10-06 11:31:09 -07001752 return -1;
1753
Sage Weilbe655592011-11-30 09:47:09 -08001754 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001755 mine = cap->issued | cap->implemented;
1756 used = __ceph_caps_used(ci);
Yan, Zheng979abfd2013-11-22 13:56:24 +08001757 wanted = __ceph_caps_file_wanted(ci);
Sage Weil2f2dc052009-10-06 11:31:09 -07001758 oissued = __ceph_caps_issued_other(ci, cap);
1759
Yan, Zheng979abfd2013-11-22 13:56:24 +08001760 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001761 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
Yan, Zheng979abfd2013-11-22 13:56:24 +08001762 ceph_cap_string(used), ceph_cap_string(wanted));
1763 if (cap == ci->i_auth_cap) {
Yan, Zheng622f3e22015-05-07 10:59:47 +08001764 if (ci->i_dirty_caps || ci->i_flushing_caps ||
1765 !list_empty(&ci->i_cap_snaps))
Yan, Zheng979abfd2013-11-22 13:56:24 +08001766 goto out;
1767 if ((used | wanted) & CEPH_CAP_ANY_WR)
1768 goto out;
Yan, Zheng89aa5932017-09-08 15:23:18 +08001769 /* Note: it's possible that i_filelock_ref becomes non-zero
1770 * after dropping auth caps. It doesn't hurt because reply
1771 * of lock mds request will re-add auth caps. */
1772 if (atomic_read(&ci->i_filelock_ref) > 0)
1773 goto out;
Yan, Zheng979abfd2013-11-22 13:56:24 +08001774 }
Yan, Zheng5e804ac2015-10-26 16:08:43 +08001775 /* The inode has cached pages, but it's no longer used.
1776 * we can safely drop it */
1777 if (wanted == 0 && used == CEPH_CAP_FILE_CACHE &&
1778 !(oissued & CEPH_CAP_FILE_CACHE)) {
1779 used = 0;
1780 oissued = 0;
1781 }
Yan, Zheng979abfd2013-11-22 13:56:24 +08001782 if ((used | wanted) & ~oissued & mine)
Sage Weil2f2dc052009-10-06 11:31:09 -07001783 goto out; /* we need these caps */
1784
Sage Weil2f2dc052009-10-06 11:31:09 -07001785 if (oissued) {
1786 /* we aren't the only cap.. just remove us */
Yan, Zhenga096b092013-09-22 10:15:58 +08001787 __ceph_remove_cap(cap, true);
Jeff Layton533a2812019-07-19 15:22:28 -04001788 (*remaining)--;
Sage Weil2f2dc052009-10-06 11:31:09 -07001789 } else {
Yan, Zheng040d7862017-11-30 11:59:22 +08001790 struct dentry *dentry;
Yan, Zheng5e804ac2015-10-26 16:08:43 +08001791 /* try dropping referring dentries */
Sage Weilbe655592011-11-30 09:47:09 -08001792 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng040d7862017-11-30 11:59:22 +08001793 dentry = d_find_any_alias(inode);
1794 if (dentry && drop_negative_children(dentry)) {
1795 int count;
1796 dput(dentry);
1797 d_prune_aliases(inode);
1798 count = atomic_read(&inode->i_count);
1799 if (count == 1)
Jeff Layton533a2812019-07-19 15:22:28 -04001800 (*remaining)--;
Yan, Zheng040d7862017-11-30 11:59:22 +08001801 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1802 inode, cap, count);
1803 } else {
1804 dput(dentry);
1805 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001806 return 0;
1807 }
1808
1809out:
Sage Weilbe655592011-11-30 09:47:09 -08001810 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001811 return 0;
1812}
1813
1814/*
1815 * Trim session cap count down to some max number.
1816 */
Zhi Zhange30ee582018-01-24 21:24:33 +08001817int ceph_trim_caps(struct ceph_mds_client *mdsc,
1818 struct ceph_mds_session *session,
1819 int max_caps)
Sage Weil2f2dc052009-10-06 11:31:09 -07001820{
1821 int trim_caps = session->s_nr_caps - max_caps;
1822
1823 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1824 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1825 if (trim_caps > 0) {
Jeff Layton533a2812019-07-19 15:22:28 -04001826 int remaining = trim_caps;
1827
1828 ceph_iterate_session_caps(session, trim_caps_cb, &remaining);
Sage Weil2f2dc052009-10-06 11:31:09 -07001829 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1830 session->s_mds, session->s_nr_caps, max_caps,
Jeff Layton533a2812019-07-19 15:22:28 -04001831 trim_caps - remaining);
Sage Weil2f2dc052009-10-06 11:31:09 -07001832 }
Yan, Zhenga56371d2014-04-01 20:34:56 +08001833
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08001834 ceph_flush_cap_releases(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07001835 return 0;
1836}
1837
Yan, Zheng8310b082015-06-09 17:20:12 +08001838static int check_caps_flush(struct ceph_mds_client *mdsc,
1839 u64 want_flush_tid)
1840{
Yan, Zheng8310b082015-06-09 17:20:12 +08001841 int ret = 1;
1842
1843 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zhenge4500b52016-07-06 11:12:56 +08001844 if (!list_empty(&mdsc->cap_flush_list)) {
1845 struct ceph_cap_flush *cf =
1846 list_first_entry(&mdsc->cap_flush_list,
1847 struct ceph_cap_flush, g_list);
1848 if (cf->tid <= want_flush_tid) {
1849 dout("check_caps_flush still flushing tid "
1850 "%llu <= %llu\n", cf->tid, want_flush_tid);
1851 ret = 0;
1852 }
Yan, Zheng8310b082015-06-09 17:20:12 +08001853 }
1854 spin_unlock(&mdsc->cap_dirty_lock);
1855 return ret;
Yan, Zhengd3383a82015-01-08 21:30:12 +08001856}
1857
Sage Weil2f2dc052009-10-06 11:31:09 -07001858/*
1859 * flush all dirty inode data to disk.
1860 *
Yan, Zheng8310b082015-06-09 17:20:12 +08001861 * returns true if we've flushed through want_flush_tid
Sage Weil2f2dc052009-10-06 11:31:09 -07001862 */
Yan, Zhengaffbc192015-05-05 21:22:13 +08001863static void wait_caps_flush(struct ceph_mds_client *mdsc,
Yan, Zheng0e294382016-07-04 18:06:41 +08001864 u64 want_flush_tid)
Sage Weil2f2dc052009-10-06 11:31:09 -07001865{
Yan, Zheng0e294382016-07-04 18:06:41 +08001866 dout("check_caps_flush want %llu\n", want_flush_tid);
Yan, Zheng8310b082015-06-09 17:20:12 +08001867
1868 wait_event(mdsc->cap_flushing_wq,
1869 check_caps_flush(mdsc, want_flush_tid));
1870
1871 dout("check_caps_flush ok, flushed thru %llu\n", want_flush_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07001872}
1873
1874/*
1875 * called under s_mutex
1876 */
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08001877static void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1878 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001879{
Yan, Zheng745a8e32015-05-14 17:22:42 +08001880 struct ceph_msg *msg = NULL;
1881 struct ceph_mds_cap_release *head;
1882 struct ceph_mds_cap_item *item;
Jeff Layton92475f02017-04-13 11:07:04 -04001883 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
Yan, Zheng745a8e32015-05-14 17:22:42 +08001884 struct ceph_cap *cap;
1885 LIST_HEAD(tmp_list);
1886 int num_cap_releases;
Jeff Layton92475f02017-04-13 11:07:04 -04001887 __le32 barrier, *cap_barrier;
1888
1889 down_read(&osdc->lock);
1890 barrier = cpu_to_le32(osdc->epoch_barrier);
1891 up_read(&osdc->lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001892
Sage Weil0f8605f2010-05-05 15:51:35 -07001893 spin_lock(&session->s_cap_lock);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001894again:
1895 list_splice_init(&session->s_cap_releases, &tmp_list);
1896 num_cap_releases = session->s_num_cap_releases;
1897 session->s_num_cap_releases = 0;
1898 spin_unlock(&session->s_cap_lock);
1899
1900 while (!list_empty(&tmp_list)) {
1901 if (!msg) {
1902 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001903 PAGE_SIZE, GFP_NOFS, false);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001904 if (!msg)
1905 goto out_err;
1906 head = msg->front.iov_base;
1907 head->num = cpu_to_le32(0);
1908 msg->front.iov_len = sizeof(*head);
Jeff Layton92475f02017-04-13 11:07:04 -04001909
1910 msg->hdr.version = cpu_to_le16(2);
1911 msg->hdr.compat_version = cpu_to_le16(1);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001912 }
Jeff Layton92475f02017-04-13 11:07:04 -04001913
Yan, Zheng745a8e32015-05-14 17:22:42 +08001914 cap = list_first_entry(&tmp_list, struct ceph_cap,
1915 session_caps);
1916 list_del(&cap->session_caps);
1917 num_cap_releases--;
1918
1919 head = msg->front.iov_base;
Jeff Layton4198aba2019-05-02 08:06:50 -04001920 put_unaligned_le32(get_unaligned_le32(&head->num) + 1,
1921 &head->num);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001922 item = msg->front.iov_base + msg->front.iov_len;
1923 item->ino = cpu_to_le64(cap->cap_ino);
1924 item->cap_id = cpu_to_le64(cap->cap_id);
1925 item->migrate_seq = cpu_to_le32(cap->mseq);
1926 item->seq = cpu_to_le32(cap->issue_seq);
1927 msg->front.iov_len += sizeof(*item);
1928
1929 ceph_put_cap(mdsc, cap);
1930
1931 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
Jeff Layton92475f02017-04-13 11:07:04 -04001932 // Append cap_barrier field
1933 cap_barrier = msg->front.iov_base + msg->front.iov_len;
1934 *cap_barrier = barrier;
1935 msg->front.iov_len += sizeof(*cap_barrier);
1936
Yan, Zheng745a8e32015-05-14 17:22:42 +08001937 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1938 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1939 ceph_con_send(&session->s_con, msg);
1940 msg = NULL;
1941 }
1942 }
1943
1944 BUG_ON(num_cap_releases != 0);
1945
1946 spin_lock(&session->s_cap_lock);
1947 if (!list_empty(&session->s_cap_releases))
1948 goto again;
1949 spin_unlock(&session->s_cap_lock);
1950
1951 if (msg) {
Jeff Layton92475f02017-04-13 11:07:04 -04001952 // Append cap_barrier field
1953 cap_barrier = msg->front.iov_base + msg->front.iov_len;
1954 *cap_barrier = barrier;
1955 msg->front.iov_len += sizeof(*cap_barrier);
1956
Sage Weil2f2dc052009-10-06 11:31:09 -07001957 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1958 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1959 ceph_con_send(&session->s_con, msg);
1960 }
Yan, Zheng745a8e32015-05-14 17:22:42 +08001961 return;
1962out_err:
1963 pr_err("send_cap_releases mds%d, failed to allocate message\n",
1964 session->s_mds);
1965 spin_lock(&session->s_cap_lock);
1966 list_splice(&tmp_list, &session->s_cap_releases);
1967 session->s_num_cap_releases += num_cap_releases;
Sage Weil2f2dc052009-10-06 11:31:09 -07001968 spin_unlock(&session->s_cap_lock);
1969}
1970
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08001971static void ceph_cap_release_work(struct work_struct *work)
1972{
1973 struct ceph_mds_session *session =
1974 container_of(work, struct ceph_mds_session, s_cap_release_work);
1975
1976 mutex_lock(&session->s_mutex);
1977 if (session->s_state == CEPH_MDS_SESSION_OPEN ||
1978 session->s_state == CEPH_MDS_SESSION_HUNG)
1979 ceph_send_cap_releases(session->s_mdsc, session);
1980 mutex_unlock(&session->s_mutex);
1981 ceph_put_mds_session(session);
1982}
1983
1984void ceph_flush_cap_releases(struct ceph_mds_client *mdsc,
1985 struct ceph_mds_session *session)
1986{
1987 if (mdsc->stopping)
1988 return;
1989
Xiubo Li5b3248c2019-12-19 19:44:09 -05001990 ceph_get_mds_session(session);
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08001991 if (queue_work(mdsc->fsc->cap_wq,
1992 &session->s_cap_release_work)) {
1993 dout("cap release work queued\n");
1994 } else {
1995 ceph_put_mds_session(session);
1996 dout("failed to queue cap release work\n");
1997 }
1998}
1999
2000/*
2001 * caller holds session->s_cap_lock
2002 */
2003void __ceph_queue_cap_release(struct ceph_mds_session *session,
2004 struct ceph_cap *cap)
2005{
2006 list_add_tail(&cap->session_caps, &session->s_cap_releases);
2007 session->s_num_cap_releases++;
2008
2009 if (!(session->s_num_cap_releases % CEPH_CAPS_PER_RELEASE))
2010 ceph_flush_cap_releases(session->s_mdsc, session);
2011}
2012
Yan, Zheng37c4efc2019-01-31 16:55:51 +08002013static void ceph_cap_reclaim_work(struct work_struct *work)
2014{
2015 struct ceph_mds_client *mdsc =
2016 container_of(work, struct ceph_mds_client, cap_reclaim_work);
2017 int ret = ceph_trim_dentries(mdsc);
2018 if (ret == -EAGAIN)
2019 ceph_queue_cap_reclaim_work(mdsc);
2020}
2021
2022void ceph_queue_cap_reclaim_work(struct ceph_mds_client *mdsc)
2023{
2024 if (mdsc->stopping)
2025 return;
2026
2027 if (queue_work(mdsc->fsc->cap_wq, &mdsc->cap_reclaim_work)) {
2028 dout("caps reclaim work queued\n");
2029 } else {
2030 dout("failed to queue caps release work\n");
2031 }
2032}
2033
Yan, Zhengfe330322019-02-01 14:57:15 +08002034void ceph_reclaim_caps_nr(struct ceph_mds_client *mdsc, int nr)
2035{
2036 int val;
2037 if (!nr)
2038 return;
2039 val = atomic_add_return(nr, &mdsc->cap_reclaim_pending);
Xiubo Libba15602019-11-26 07:32:22 -05002040 if ((val % CEPH_CAPS_PER_RELEASE) < nr) {
Yan, Zhengfe330322019-02-01 14:57:15 +08002041 atomic_set(&mdsc->cap_reclaim_pending, 0);
2042 ceph_queue_cap_reclaim_work(mdsc);
2043 }
2044}
2045
Sage Weil2f2dc052009-10-06 11:31:09 -07002046/*
2047 * requests
2048 */
2049
Yan, Zheng54008392014-03-29 13:41:15 +08002050int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
2051 struct inode *dir)
2052{
2053 struct ceph_inode_info *ci = ceph_inode(dir);
2054 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
2055 struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options;
Yan, Zheng2a5beea2016-04-28 09:37:39 +08002056 size_t size = sizeof(struct ceph_mds_reply_dir_entry);
Jeff Laytonad8c28a2019-09-09 15:58:55 -04002057 unsigned int num_entries;
2058 int order;
Yan, Zheng54008392014-03-29 13:41:15 +08002059
2060 spin_lock(&ci->i_ceph_lock);
2061 num_entries = ci->i_files + ci->i_subdirs;
2062 spin_unlock(&ci->i_ceph_lock);
Jeff Laytonad8c28a2019-09-09 15:58:55 -04002063 num_entries = max(num_entries, 1U);
Yan, Zheng54008392014-03-29 13:41:15 +08002064 num_entries = min(num_entries, opt->max_readdir);
2065
2066 order = get_order(size * num_entries);
2067 while (order >= 0) {
Yan, Zheng2a5beea2016-04-28 09:37:39 +08002068 rinfo->dir_entries = (void*)__get_free_pages(GFP_KERNEL |
2069 __GFP_NOWARN,
2070 order);
2071 if (rinfo->dir_entries)
Yan, Zheng54008392014-03-29 13:41:15 +08002072 break;
2073 order--;
2074 }
Yan, Zheng2a5beea2016-04-28 09:37:39 +08002075 if (!rinfo->dir_entries)
Yan, Zheng54008392014-03-29 13:41:15 +08002076 return -ENOMEM;
2077
2078 num_entries = (PAGE_SIZE << order) / size;
2079 num_entries = min(num_entries, opt->max_readdir);
2080
2081 rinfo->dir_buf_size = PAGE_SIZE << order;
2082 req->r_num_caps = num_entries + 1;
2083 req->r_args.readdir.max_entries = cpu_to_le32(num_entries);
2084 req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes);
2085 return 0;
2086}
2087
Sage Weil2f2dc052009-10-06 11:31:09 -07002088/*
2089 * Create an mds request.
2090 */
2091struct ceph_mds_request *
2092ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
2093{
2094 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
Arnd Bergmann63ecae72018-07-13 22:18:35 +02002095 struct timespec64 ts;
Sage Weil2f2dc052009-10-06 11:31:09 -07002096
2097 if (!req)
2098 return ERR_PTR(-ENOMEM);
2099
Sage Weilb4556392010-05-13 12:01:13 -07002100 mutex_init(&req->r_fill_mutex);
Yehuda Sadeh37151662010-06-17 16:16:12 -07002101 req->r_mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07002102 req->r_started = jiffies;
2103 req->r_resend_mds = -1;
2104 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002105 INIT_LIST_HEAD(&req->r_unsafe_target_item);
Sage Weil2f2dc052009-10-06 11:31:09 -07002106 req->r_fmode = -1;
Sage Weil153c8e62009-12-07 12:31:09 -08002107 kref_init(&req->r_kref);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02002108 RB_CLEAR_NODE(&req->r_node);
Sage Weil2f2dc052009-10-06 11:31:09 -07002109 INIT_LIST_HEAD(&req->r_wait);
2110 init_completion(&req->r_completion);
2111 init_completion(&req->r_safe_completion);
2112 INIT_LIST_HEAD(&req->r_unsafe_item);
2113
Arnd Bergmann63ecae72018-07-13 22:18:35 +02002114 ktime_get_coarse_real_ts64(&ts);
Arnd Bergmann0ed1e902018-07-13 22:18:38 +02002115 req->r_stamp = timespec64_trunc(ts, mdsc->fsc->sb->s_time_gran);
Sage Weilb8e69062014-05-21 17:41:08 -07002116
Sage Weil2f2dc052009-10-06 11:31:09 -07002117 req->r_op = op;
2118 req->r_direct_mode = mode;
2119 return req;
2120}
2121
2122/*
Sage Weil44ca18f2010-02-15 12:08:46 -08002123 * return oldest (lowest) request, tid in request tree, 0 if none.
Sage Weil2f2dc052009-10-06 11:31:09 -07002124 *
2125 * called under mdsc->mutex.
2126 */
Sage Weil44ca18f2010-02-15 12:08:46 -08002127static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
2128{
2129 if (RB_EMPTY_ROOT(&mdsc->request_tree))
2130 return NULL;
2131 return rb_entry(rb_first(&mdsc->request_tree),
2132 struct ceph_mds_request, r_node);
2133}
2134
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08002135static inline u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07002136{
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08002137 return mdsc->oldest_tid;
Sage Weil2f2dc052009-10-06 11:31:09 -07002138}
2139
2140/*
2141 * Build a dentry's path. Allocate on heap; caller must kfree. Based
2142 * on build_path_from_dentry in fs/cifs/dir.c.
2143 *
2144 * If @stop_on_nosnap, generate path relative to the first non-snapped
2145 * inode.
2146 *
2147 * Encode hidden .snap dirs as a double /, i.e.
2148 * foo/.snap/bar -> foo//bar
2149 */
Jeff Layton69a10fb2019-04-26 13:33:39 -04002150char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase,
Sage Weil2f2dc052009-10-06 11:31:09 -07002151 int stop_on_nosnap)
2152{
2153 struct dentry *temp;
2154 char *path;
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002155 int pos;
Al Viro1b71fe22011-07-16 23:43:58 -04002156 unsigned seq;
Jeff Layton69a10fb2019-04-26 13:33:39 -04002157 u64 base;
Sage Weil2f2dc052009-10-06 11:31:09 -07002158
Markus Elfringd37b1d92017-08-20 20:22:02 +02002159 if (!dentry)
Sage Weil2f2dc052009-10-06 11:31:09 -07002160 return ERR_PTR(-EINVAL);
2161
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002162 path = __getname();
Markus Elfringd37b1d92017-08-20 20:22:02 +02002163 if (!path)
Sage Weil2f2dc052009-10-06 11:31:09 -07002164 return ERR_PTR(-ENOMEM);
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002165retry:
2166 pos = PATH_MAX - 1;
2167 path[pos] = '\0';
2168
2169 seq = read_seqbegin(&rename_lock);
Al Viro1b71fe22011-07-16 23:43:58 -04002170 rcu_read_lock();
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002171 temp = dentry;
2172 for (;;) {
Al Viro1b71fe22011-07-16 23:43:58 -04002173 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07002174
Al Viro1b71fe22011-07-16 23:43:58 -04002175 spin_lock(&temp->d_lock);
David Howells2b0143b2015-03-17 22:25:59 +00002176 inode = d_inode(temp);
Sage Weil2f2dc052009-10-06 11:31:09 -07002177 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
Sage Weil104648a2010-03-18 10:14:30 -07002178 dout("build_path path+%d: %p SNAPDIR\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002179 pos, temp);
Jeff Laytond6b8bd62019-05-09 07:58:38 -04002180 } else if (stop_on_nosnap && inode && dentry != temp &&
Sage Weil2f2dc052009-10-06 11:31:09 -07002181 ceph_snap(inode) == CEPH_NOSNAP) {
Yehuda Sadeh9d5a09e2011-12-13 09:57:44 -08002182 spin_unlock(&temp->d_lock);
Jeff Laytond6b8bd62019-05-09 07:58:38 -04002183 pos++; /* get rid of any prepended '/' */
Sage Weil2f2dc052009-10-06 11:31:09 -07002184 break;
2185 } else {
2186 pos -= temp->d_name.len;
Al Viro1b71fe22011-07-16 23:43:58 -04002187 if (pos < 0) {
2188 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002189 break;
Al Viro1b71fe22011-07-16 23:43:58 -04002190 }
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002191 memcpy(path + pos, temp->d_name.name, temp->d_name.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07002192 }
Al Viro1b71fe22011-07-16 23:43:58 -04002193 spin_unlock(&temp->d_lock);
Yan, Zheng41883ba2019-05-23 10:22:55 +08002194 temp = READ_ONCE(temp->d_parent);
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002195
2196 /* Are we at the root? */
2197 if (IS_ROOT(temp))
2198 break;
2199
2200 /* Are we out of buffer? */
2201 if (--pos < 0)
2202 break;
2203
2204 path[pos] = '/';
Sage Weil2f2dc052009-10-06 11:31:09 -07002205 }
Jeff Layton69a10fb2019-04-26 13:33:39 -04002206 base = ceph_ino(d_inode(temp));
Al Viro1b71fe22011-07-16 23:43:58 -04002207 rcu_read_unlock();
Jeff Laytonf5946bc2019-10-16 08:20:17 -04002208
2209 if (read_seqretry(&rename_lock, seq))
2210 goto retry;
2211
2212 if (pos < 0) {
2213 /*
2214 * A rename didn't occur, but somehow we didn't end up where
2215 * we thought we would. Throw a warning and try again.
2216 */
2217 pr_warn("build_path did not end path lookup where "
2218 "expected, pos is %d\n", pos);
Sage Weil2f2dc052009-10-06 11:31:09 -07002219 goto retry;
2220 }
2221
Jeff Layton69a10fb2019-04-26 13:33:39 -04002222 *pbase = base;
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002223 *plen = PATH_MAX - 1 - pos;
Sage Weil104648a2010-03-18 10:14:30 -07002224 dout("build_path on %p %d built %llx '%.*s'\n",
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002225 dentry, d_count(dentry), base, *plen, path + pos);
2226 return path + pos;
Sage Weil2f2dc052009-10-06 11:31:09 -07002227}
2228
Jeff Laytonfd36a712016-12-15 08:37:58 -05002229static int build_dentry_path(struct dentry *dentry, struct inode *dir,
Sage Weil2f2dc052009-10-06 11:31:09 -07002230 const char **ppath, int *ppathlen, u64 *pino,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002231 bool *pfreepath, bool parent_locked)
Sage Weil2f2dc052009-10-06 11:31:09 -07002232{
2233 char *path;
2234
Jeff Laytonc6b0b652016-12-15 08:37:57 -05002235 rcu_read_lock();
Jeff Laytonfd36a712016-12-15 08:37:58 -05002236 if (!dir)
2237 dir = d_inode_rcu(dentry->d_parent);
Jeff Layton964fff72019-04-29 11:51:02 -04002238 if (dir && parent_locked && ceph_snap(dir) == CEPH_NOSNAP) {
Jeff Laytonc6b0b652016-12-15 08:37:57 -05002239 *pino = ceph_ino(dir);
2240 rcu_read_unlock();
Jeff Layton964fff72019-04-29 11:51:02 -04002241 *ppath = dentry->d_name.name;
2242 *ppathlen = dentry->d_name.len;
Sage Weil2f2dc052009-10-06 11:31:09 -07002243 return 0;
2244 }
Jeff Laytonc6b0b652016-12-15 08:37:57 -05002245 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -07002246 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
2247 if (IS_ERR(path))
2248 return PTR_ERR(path);
2249 *ppath = path;
Jeff Layton1bcb3442019-04-15 12:00:42 -04002250 *pfreepath = true;
Sage Weil2f2dc052009-10-06 11:31:09 -07002251 return 0;
2252}
2253
2254static int build_inode_path(struct inode *inode,
2255 const char **ppath, int *ppathlen, u64 *pino,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002256 bool *pfreepath)
Sage Weil2f2dc052009-10-06 11:31:09 -07002257{
2258 struct dentry *dentry;
2259 char *path;
2260
2261 if (ceph_snap(inode) == CEPH_NOSNAP) {
2262 *pino = ceph_ino(inode);
2263 *ppathlen = 0;
2264 return 0;
2265 }
2266 dentry = d_find_alias(inode);
2267 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
2268 dput(dentry);
2269 if (IS_ERR(path))
2270 return PTR_ERR(path);
2271 *ppath = path;
Jeff Layton1bcb3442019-04-15 12:00:42 -04002272 *pfreepath = true;
Sage Weil2f2dc052009-10-06 11:31:09 -07002273 return 0;
2274}
2275
2276/*
2277 * request arguments may be specified via an inode *, a dentry *, or
2278 * an explicit ino+path.
2279 */
2280static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
Jeff Laytonfd36a712016-12-15 08:37:58 -05002281 struct inode *rdiri, const char *rpath,
2282 u64 rino, const char **ppath, int *pathlen,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002283 u64 *ino, bool *freepath, bool parent_locked)
Sage Weil2f2dc052009-10-06 11:31:09 -07002284{
2285 int r = 0;
2286
2287 if (rinode) {
2288 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
2289 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
2290 ceph_snap(rinode));
2291 } else if (rdentry) {
Jeff Laytonfd36a712016-12-15 08:37:58 -05002292 r = build_dentry_path(rdentry, rdiri, ppath, pathlen, ino,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002293 freepath, parent_locked);
Sage Weil2f2dc052009-10-06 11:31:09 -07002294 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
2295 *ppath);
Sage Weil795858d2011-08-15 13:02:37 -07002296 } else if (rpath || rino) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002297 *ino = rino;
2298 *ppath = rpath;
David Zafmanb0000562012-10-25 10:23:46 -07002299 *pathlen = rpath ? strlen(rpath) : 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002300 dout(" path %.*s\n", *pathlen, rpath);
2301 }
2302
2303 return r;
2304}
2305
2306/*
2307 * called under mdsc->mutex
2308 */
2309static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
2310 struct ceph_mds_request *req,
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002311 int mds, bool drop_cap_releases)
Sage Weil2f2dc052009-10-06 11:31:09 -07002312{
2313 struct ceph_msg *msg;
2314 struct ceph_mds_request_head *head;
2315 const char *path1 = NULL;
2316 const char *path2 = NULL;
2317 u64 ino1 = 0, ino2 = 0;
2318 int pathlen1 = 0, pathlen2 = 0;
Jeff Layton1bcb3442019-04-15 12:00:42 -04002319 bool freepath1 = false, freepath2 = false;
Sage Weil2f2dc052009-10-06 11:31:09 -07002320 int len;
2321 u16 releases;
2322 void *p, *end;
2323 int ret;
2324
2325 ret = set_request_path_attr(req->r_inode, req->r_dentry,
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002326 req->r_parent, req->r_path1, req->r_ino1.ino,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002327 &path1, &pathlen1, &ino1, &freepath1,
2328 test_bit(CEPH_MDS_R_PARENT_LOCKED,
2329 &req->r_req_flags));
Sage Weil2f2dc052009-10-06 11:31:09 -07002330 if (ret < 0) {
2331 msg = ERR_PTR(ret);
2332 goto out;
2333 }
2334
Jeff Layton1bcb3442019-04-15 12:00:42 -04002335 /* If r_old_dentry is set, then assume that its parent is locked */
Sage Weil2f2dc052009-10-06 11:31:09 -07002336 ret = set_request_path_attr(NULL, req->r_old_dentry,
Jeff Laytonfd36a712016-12-15 08:37:58 -05002337 req->r_old_dentry_dir,
Sage Weil2f2dc052009-10-06 11:31:09 -07002338 req->r_path2, req->r_ino2.ino,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002339 &path2, &pathlen2, &ino2, &freepath2, true);
Sage Weil2f2dc052009-10-06 11:31:09 -07002340 if (ret < 0) {
2341 msg = ERR_PTR(ret);
2342 goto out_free1;
2343 }
2344
2345 len = sizeof(*head) +
Sage Weilb8e69062014-05-21 17:41:08 -07002346 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) +
Arnd Bergmann777d7382015-09-30 15:04:42 +02002347 sizeof(struct ceph_timespec);
Sage Weil2f2dc052009-10-06 11:31:09 -07002348
2349 /* calculate (max) length for cap releases */
2350 len += sizeof(struct ceph_mds_request_release) *
2351 (!!req->r_inode_drop + !!req->r_dentry_drop +
2352 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
2353 if (req->r_dentry_drop)
Jeff Laytonc1dfc272019-04-17 14:23:17 -04002354 len += pathlen1;
Sage Weil2f2dc052009-10-06 11:31:09 -07002355 if (req->r_old_dentry_drop)
Jeff Laytonc1dfc272019-04-17 14:23:17 -04002356 len += pathlen2;
Sage Weil2f2dc052009-10-06 11:31:09 -07002357
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02002358 msg = ceph_msg_new2(CEPH_MSG_CLIENT_REQUEST, len, 1, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07002359 if (!msg) {
2360 msg = ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -07002361 goto out_free2;
Sage Weila79832f2010-04-01 16:06:19 -07002362 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002363
John Spray7cfa0312014-10-30 17:15:26 +00002364 msg->hdr.version = cpu_to_le16(2);
Sage Weil6df058c2009-12-22 11:24:33 -08002365 msg->hdr.tid = cpu_to_le64(req->r_tid);
2366
Sage Weil2f2dc052009-10-06 11:31:09 -07002367 head = msg->front.iov_base;
2368 p = msg->front.iov_base + sizeof(*head);
2369 end = msg->front.iov_base + msg->front.iov_len;
2370
2371 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
2372 head->op = cpu_to_le32(req->r_op);
Eric W. Biedermanff3d0042013-01-31 04:01:53 -08002373 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid));
2374 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid));
Jeff Layton2def8652019-10-14 15:41:54 -04002375 head->ino = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002376 head->args = req->r_args;
2377
2378 ceph_encode_filepath(&p, end, ino1, path1);
2379 ceph_encode_filepath(&p, end, ino2, path2);
2380
Sage Weile979cf52010-07-15 14:58:39 -07002381 /* make note of release offset, in case we need to replay */
2382 req->r_request_release_offset = p - msg->front.iov_base;
2383
Sage Weil2f2dc052009-10-06 11:31:09 -07002384 /* cap releases */
2385 releases = 0;
2386 if (req->r_inode_drop)
2387 releases += ceph_encode_inode_release(&p,
David Howells2b0143b2015-03-17 22:25:59 +00002388 req->r_inode ? req->r_inode : d_inode(req->r_dentry),
Sage Weil2f2dc052009-10-06 11:31:09 -07002389 mds, req->r_inode_drop, req->r_inode_unless, 0);
2390 if (req->r_dentry_drop)
2391 releases += ceph_encode_dentry_release(&p, req->r_dentry,
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002392 req->r_parent, mds, req->r_dentry_drop,
Jeff Laytonca6c8ae2016-12-15 08:37:59 -05002393 req->r_dentry_unless);
Sage Weil2f2dc052009-10-06 11:31:09 -07002394 if (req->r_old_dentry_drop)
2395 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
Jeff Laytonca6c8ae2016-12-15 08:37:59 -05002396 req->r_old_dentry_dir, mds,
2397 req->r_old_dentry_drop,
2398 req->r_old_dentry_unless);
Sage Weil2f2dc052009-10-06 11:31:09 -07002399 if (req->r_old_inode_drop)
2400 releases += ceph_encode_inode_release(&p,
David Howells2b0143b2015-03-17 22:25:59 +00002401 d_inode(req->r_old_dentry),
Sage Weil2f2dc052009-10-06 11:31:09 -07002402 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002403
2404 if (drop_cap_releases) {
2405 releases = 0;
2406 p = msg->front.iov_base + req->r_request_release_offset;
2407 }
2408
Sage Weil2f2dc052009-10-06 11:31:09 -07002409 head->num_releases = cpu_to_le16(releases);
2410
Sage Weilb8e69062014-05-21 17:41:08 -07002411 /* time stamp */
Yan, Zheng1f041a82015-01-13 15:20:52 +08002412 {
2413 struct ceph_timespec ts;
Arnd Bergmann0ed1e902018-07-13 22:18:38 +02002414 ceph_encode_timespec64(&ts, &req->r_stamp);
Yan, Zheng1f041a82015-01-13 15:20:52 +08002415 ceph_encode_copy(&p, &ts, sizeof(ts));
2416 }
Sage Weilb8e69062014-05-21 17:41:08 -07002417
Sage Weil2f2dc052009-10-06 11:31:09 -07002418 BUG_ON(p > end);
2419 msg->front.iov_len = p - msg->front.iov_base;
2420 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2421
Yan, Zheng25e6bae2014-09-16 19:15:28 +08002422 if (req->r_pagelist) {
2423 struct ceph_pagelist *pagelist = req->r_pagelist;
Yan, Zheng25e6bae2014-09-16 19:15:28 +08002424 ceph_msg_data_add_pagelist(msg, pagelist);
2425 msg->hdr.data_len = cpu_to_le32(pagelist->length);
2426 } else {
2427 msg->hdr.data_len = 0;
Alex Elderebf18f42013-03-04 22:29:57 -06002428 }
Alex Elder02afca62013-02-14 12:16:43 -06002429
Sage Weil2f2dc052009-10-06 11:31:09 -07002430 msg->hdr.data_off = cpu_to_le16(0);
2431
2432out_free2:
2433 if (freepath2)
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002434 ceph_mdsc_free_path((char *)path2, pathlen2);
Sage Weil2f2dc052009-10-06 11:31:09 -07002435out_free1:
2436 if (freepath1)
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002437 ceph_mdsc_free_path((char *)path1, pathlen1);
Sage Weil2f2dc052009-10-06 11:31:09 -07002438out:
2439 return msg;
2440}
2441
2442/*
2443 * called under mdsc->mutex if error, under no mutex if
2444 * success.
2445 */
2446static void complete_request(struct ceph_mds_client *mdsc,
2447 struct ceph_mds_request *req)
2448{
2449 if (req->r_callback)
2450 req->r_callback(mdsc, req);
Jeff Layton111c7082019-04-02 09:43:18 -04002451 complete_all(&req->r_completion);
Sage Weil2f2dc052009-10-06 11:31:09 -07002452}
2453
2454/*
2455 * called under mdsc->mutex
2456 */
2457static int __prepare_send_request(struct ceph_mds_client *mdsc,
2458 struct ceph_mds_request *req,
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002459 int mds, bool drop_cap_releases)
Sage Weil2f2dc052009-10-06 11:31:09 -07002460{
2461 struct ceph_mds_request_head *rhead;
2462 struct ceph_msg *msg;
2463 int flags = 0;
2464
Sage Weil2f2dc052009-10-06 11:31:09 -07002465 req->r_attempts++;
Greg Farnume55b71f2010-06-22 15:58:01 -07002466 if (req->r_inode) {
2467 struct ceph_cap *cap =
2468 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
2469
2470 if (cap)
2471 req->r_sent_on_mseq = cap->mseq;
2472 else
2473 req->r_sent_on_mseq = -1;
2474 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002475 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
2476 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
2477
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002478 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002479 void *p;
Sage Weil01a92f12010-07-15 13:24:32 -07002480 /*
2481 * Replay. Do not regenerate message (and rebuild
2482 * paths, etc.); just use the original message.
2483 * Rebuilding paths will break for renames because
2484 * d_move mangles the src name.
2485 */
2486 msg = req->r_request;
2487 rhead = msg->front.iov_base;
2488
2489 flags = le32_to_cpu(rhead->flags);
2490 flags |= CEPH_MDS_FLAG_REPLAY;
2491 rhead->flags = cpu_to_le32(flags);
2492
2493 if (req->r_target_inode)
2494 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
2495
2496 rhead->num_retry = req->r_attempts - 1;
Sage Weile979cf52010-07-15 14:58:39 -07002497
2498 /* remove cap/dentry releases from message */
2499 rhead->num_releases = 0;
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002500
2501 /* time stamp */
2502 p = msg->front.iov_base + req->r_request_release_offset;
Yan, Zheng1f041a82015-01-13 15:20:52 +08002503 {
2504 struct ceph_timespec ts;
Arnd Bergmann0ed1e902018-07-13 22:18:38 +02002505 ceph_encode_timespec64(&ts, &req->r_stamp);
Yan, Zheng1f041a82015-01-13 15:20:52 +08002506 ceph_encode_copy(&p, &ts, sizeof(ts));
2507 }
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002508
2509 msg->front.iov_len = p - msg->front.iov_base;
2510 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
Sage Weil01a92f12010-07-15 13:24:32 -07002511 return 0;
2512 }
2513
Sage Weil2f2dc052009-10-06 11:31:09 -07002514 if (req->r_request) {
2515 ceph_msg_put(req->r_request);
2516 req->r_request = NULL;
2517 }
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002518 msg = create_request_message(mdsc, req, mds, drop_cap_releases);
Sage Weil2f2dc052009-10-06 11:31:09 -07002519 if (IS_ERR(msg)) {
Sage Weile1518c72010-05-13 11:19:06 -07002520 req->r_err = PTR_ERR(msg);
Sage Weila79832f2010-04-01 16:06:19 -07002521 return PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002522 }
2523 req->r_request = msg;
2524
2525 rhead = msg->front.iov_base;
Sage Weil2f2dc052009-10-06 11:31:09 -07002526 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002527 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Sage Weil2f2dc052009-10-06 11:31:09 -07002528 flags |= CEPH_MDS_FLAG_REPLAY;
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002529 if (req->r_parent)
Sage Weil2f2dc052009-10-06 11:31:09 -07002530 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
2531 rhead->flags = cpu_to_le32(flags);
2532 rhead->num_fwd = req->r_num_fwd;
2533 rhead->num_retry = req->r_attempts - 1;
Sage Weil01a92f12010-07-15 13:24:32 -07002534 rhead->ino = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002535
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002536 dout(" r_parent = %p\n", req->r_parent);
Sage Weil2f2dc052009-10-06 11:31:09 -07002537 return 0;
2538}
2539
2540/*
Xiubo Li9cf54562019-12-05 20:50:21 -05002541 * called under mdsc->mutex
2542 */
2543static int __send_request(struct ceph_mds_client *mdsc,
2544 struct ceph_mds_session *session,
2545 struct ceph_mds_request *req,
2546 bool drop_cap_releases)
2547{
2548 int err;
2549
2550 err = __prepare_send_request(mdsc, req, session->s_mds,
2551 drop_cap_releases);
2552 if (!err) {
2553 ceph_msg_get(req->r_request);
2554 ceph_con_send(&session->s_con, req->r_request);
2555 }
2556
2557 return err;
2558}
2559
2560/*
Sage Weil2f2dc052009-10-06 11:31:09 -07002561 * send request, or put it on the appropriate wait list.
2562 */
Chengguang Xud5548492018-07-28 16:30:48 +08002563static void __do_request(struct ceph_mds_client *mdsc,
Sage Weil2f2dc052009-10-06 11:31:09 -07002564 struct ceph_mds_request *req)
2565{
2566 struct ceph_mds_session *session = NULL;
2567 int mds = -1;
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002568 int err = 0;
Xiubo Lic4853e92019-12-09 07:47:15 -05002569 bool random;
Sage Weil2f2dc052009-10-06 11:31:09 -07002570
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002571 if (req->r_err || test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
2572 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
Yan, Zhengeb1b8af2013-09-26 14:25:36 +08002573 __unregister_request(mdsc, req);
Chengguang Xud5548492018-07-28 16:30:48 +08002574 return;
Yan, Zhengeb1b8af2013-09-26 14:25:36 +08002575 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002576
2577 if (req->r_timeout &&
2578 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
2579 dout("do_request timed out\n");
2580 err = -EIO;
2581 goto finish;
2582 }
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01002583 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002584 dout("do_request forced umount\n");
2585 err = -EIO;
2586 goto finish;
2587 }
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01002588 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_MOUNTING) {
Yan, Zhenge9e427f2016-11-10 16:02:06 +08002589 if (mdsc->mdsmap_err) {
2590 err = mdsc->mdsmap_err;
2591 dout("do_request mdsmap err %d\n", err);
2592 goto finish;
2593 }
Yan, Zhengcc8e8342017-01-04 16:21:58 +08002594 if (mdsc->mdsmap->m_epoch == 0) {
2595 dout("do_request no mdsmap, waiting for map\n");
2596 list_add(&req->r_wait, &mdsc->waiting_for_map);
Chengguang Xud5548492018-07-28 16:30:48 +08002597 return;
Yan, Zhengcc8e8342017-01-04 16:21:58 +08002598 }
Yan, Zhenge9e427f2016-11-10 16:02:06 +08002599 if (!(mdsc->fsc->mount_options->flags &
2600 CEPH_MOUNT_OPT_MOUNTWAIT) &&
2601 !ceph_mdsmap_is_cluster_available(mdsc->mdsmap)) {
Xiubo Li97820052019-12-10 20:29:40 -05002602 err = -EHOSTUNREACH;
Yan, Zhenge9e427f2016-11-10 16:02:06 +08002603 goto finish;
2604 }
2605 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002606
Sage Weildc69e2e2010-11-02 13:49:00 -07002607 put_request_session(req);
2608
Xiubo Lic4853e92019-12-09 07:47:15 -05002609 mds = __choose_mds(mdsc, req, &random);
Sage Weil2f2dc052009-10-06 11:31:09 -07002610 if (mds < 0 ||
2611 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
2612 dout("do_request no mds or not active, waiting for map\n");
2613 list_add(&req->r_wait, &mdsc->waiting_for_map);
Chengguang Xud5548492018-07-28 16:30:48 +08002614 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07002615 }
2616
2617 /* get, open session */
2618 session = __ceph_lookup_mds_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07002619 if (!session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002620 session = register_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07002621 if (IS_ERR(session)) {
2622 err = PTR_ERR(session);
2623 goto finish;
2624 }
2625 }
Xiubo Li5b3248c2019-12-19 19:44:09 -05002626 req->r_session = ceph_get_mds_session(session);
Sage Weildc69e2e2010-11-02 13:49:00 -07002627
Sage Weil2f2dc052009-10-06 11:31:09 -07002628 dout("do_request mds%d session %p state %s\n", mds, session,
John Spraya687eca2014-09-19 13:51:08 +01002629 ceph_session_state_name(session->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07002630 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
2631 session->s_state != CEPH_MDS_SESSION_HUNG) {
Yan, Zhengfcff4152016-09-14 16:39:51 +08002632 if (session->s_state == CEPH_MDS_SESSION_REJECTED) {
2633 err = -EACCES;
2634 goto out_session;
2635 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002636 if (session->s_state == CEPH_MDS_SESSION_NEW ||
Xiubo Lic4853e92019-12-09 07:47:15 -05002637 session->s_state == CEPH_MDS_SESSION_CLOSING) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002638 __open_session(mdsc, session);
Xiubo Lic4853e92019-12-09 07:47:15 -05002639 /* retry the same mds later */
2640 if (random)
2641 req->r_resend_mds = mds;
2642 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002643 list_add(&req->r_wait, &session->s_waiting);
2644 goto out_session;
2645 }
2646
2647 /* send request */
Sage Weil2f2dc052009-10-06 11:31:09 -07002648 req->r_resend_mds = -1; /* forget any previous mds hint */
2649
2650 if (req->r_request_started == 0) /* note request start time */
2651 req->r_request_started = jiffies;
2652
Xiubo Li9cf54562019-12-05 20:50:21 -05002653 err = __send_request(mdsc, session, req, false);
Sage Weil2f2dc052009-10-06 11:31:09 -07002654
2655out_session:
2656 ceph_put_mds_session(session);
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002657finish:
2658 if (err) {
2659 dout("__do_request early error %d\n", err);
2660 req->r_err = err;
2661 complete_request(mdsc, req);
2662 __unregister_request(mdsc, req);
2663 }
Chengguang Xud5548492018-07-28 16:30:48 +08002664 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07002665}
2666
2667/*
2668 * called under mdsc->mutex
2669 */
2670static void __wake_requests(struct ceph_mds_client *mdsc,
2671 struct list_head *head)
2672{
Yan, Zhenged75ec22012-11-19 10:49:06 +08002673 struct ceph_mds_request *req;
2674 LIST_HEAD(tmp_list);
Sage Weil2f2dc052009-10-06 11:31:09 -07002675
Yan, Zhenged75ec22012-11-19 10:49:06 +08002676 list_splice_init(head, &tmp_list);
2677
2678 while (!list_empty(&tmp_list)) {
2679 req = list_entry(tmp_list.next,
2680 struct ceph_mds_request, r_wait);
Sage Weil2f2dc052009-10-06 11:31:09 -07002681 list_del_init(&req->r_wait);
Sage Weil7971bd92013-05-01 21:15:58 -07002682 dout(" wake request %p tid %llu\n", req, req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002683 __do_request(mdsc, req);
2684 }
2685}
2686
2687/*
2688 * Wake up threads with requests pending for @mds, so that they can
Sage Weil29790f22010-03-18 14:45:05 -07002689 * resubmit their requests to a possibly different mds.
Sage Weil2f2dc052009-10-06 11:31:09 -07002690 */
Sage Weil29790f22010-03-18 14:45:05 -07002691static void kick_requests(struct ceph_mds_client *mdsc, int mds)
Sage Weil2f2dc052009-10-06 11:31:09 -07002692{
Sage Weil44ca18f2010-02-15 12:08:46 -08002693 struct ceph_mds_request *req;
Yan, Zheng282c1052014-07-30 10:12:47 +08002694 struct rb_node *p = rb_first(&mdsc->request_tree);
Sage Weil2f2dc052009-10-06 11:31:09 -07002695
2696 dout("kick_requests mds%d\n", mds);
Yan, Zheng282c1052014-07-30 10:12:47 +08002697 while (p) {
Sage Weil44ca18f2010-02-15 12:08:46 -08002698 req = rb_entry(p, struct ceph_mds_request, r_node);
Yan, Zheng282c1052014-07-30 10:12:47 +08002699 p = rb_next(p);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002700 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Sage Weil44ca18f2010-02-15 12:08:46 -08002701 continue;
Yan, Zheng3de22be2015-02-04 14:26:22 +08002702 if (req->r_attempts > 0)
2703 continue; /* only new requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08002704 if (req->r_session &&
2705 req->r_session->s_mds == mds) {
2706 dout(" kicking tid %llu\n", req->r_tid);
Yan, Zheng03974e82014-09-11 14:28:56 +08002707 list_del_init(&req->r_wait);
Sage Weil44ca18f2010-02-15 12:08:46 -08002708 __do_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002709 }
2710 }
2711}
2712
Jeff Layton86bda532019-04-02 09:24:36 -04002713int ceph_mdsc_submit_request(struct ceph_mds_client *mdsc, struct inode *dir,
Sage Weil2f2dc052009-10-06 11:31:09 -07002714 struct ceph_mds_request *req)
2715{
Jeff Layton86bda532019-04-02 09:24:36 -04002716 int err;
2717
2718 /* take CAP_PIN refs for r_inode, r_parent, r_old_dentry */
2719 if (req->r_inode)
2720 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
Jeff Layton9c1c2b32019-04-03 13:16:01 -04002721 if (req->r_parent) {
Jeff Layton86bda532019-04-02 09:24:36 -04002722 ceph_get_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
Jeff Layton9c1c2b32019-04-03 13:16:01 -04002723 ihold(req->r_parent);
2724 }
Jeff Layton86bda532019-04-02 09:24:36 -04002725 if (req->r_old_dentry_dir)
2726 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
2727 CEPH_CAP_PIN);
2728
2729 dout("submit_request on %p for inode %p\n", req, dir);
Sage Weil2f2dc052009-10-06 11:31:09 -07002730 mutex_lock(&mdsc->mutex);
Jeff Layton86bda532019-04-02 09:24:36 -04002731 __register_request(mdsc, req, dir);
Sage Weil2f2dc052009-10-06 11:31:09 -07002732 __do_request(mdsc, req);
Jeff Layton86bda532019-04-02 09:24:36 -04002733 err = req->r_err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002734 mutex_unlock(&mdsc->mutex);
Jeff Layton86bda532019-04-02 09:24:36 -04002735 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002736}
2737
Jeff Layton8340f222019-04-02 12:34:38 -04002738static int ceph_mdsc_wait_request(struct ceph_mds_client *mdsc,
2739 struct ceph_mds_request *req)
Sage Weil2f2dc052009-10-06 11:31:09 -07002740{
2741 int err;
2742
Sage Weile1518c72010-05-13 11:19:06 -07002743 /* wait */
Sage Weile1518c72010-05-13 11:19:06 -07002744 dout("do_request waiting\n");
Ilya Dryomov5be73032015-05-19 12:05:38 +03002745 if (!req->r_timeout && req->r_wait_for_completion) {
Yan, Zheng9280be22014-10-14 10:33:35 +08002746 err = req->r_wait_for_completion(mdsc, req);
Sage Weile1518c72010-05-13 11:19:06 -07002747 } else {
Ilya Dryomov5be73032015-05-19 12:05:38 +03002748 long timeleft = wait_for_completion_killable_timeout(
2749 &req->r_completion,
2750 ceph_timeout_jiffies(req->r_timeout));
2751 if (timeleft > 0)
2752 err = 0;
2753 else if (!timeleft)
2754 err = -EIO; /* timed out */
2755 else
2756 err = timeleft; /* killed */
Sage Weile1518c72010-05-13 11:19:06 -07002757 }
2758 dout("do_request waited, got %d\n", err);
2759 mutex_lock(&mdsc->mutex);
2760
2761 /* only abort if we didn't race with a real reply */
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002762 if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
Sage Weile1518c72010-05-13 11:19:06 -07002763 err = le32_to_cpu(req->r_reply_info.head->result);
2764 } else if (err < 0) {
2765 dout("aborted request %lld with %d\n", req->r_tid, err);
Sage Weilb4556392010-05-13 12:01:13 -07002766
2767 /*
2768 * ensure we aren't running concurrently with
2769 * ceph_fill_trace or ceph_readdir_prepopulate, which
2770 * rely on locks (dir mutex) held by our caller.
2771 */
2772 mutex_lock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07002773 req->r_err = err;
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002774 set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
Sage Weilb4556392010-05-13 12:01:13 -07002775 mutex_unlock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07002776
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002777 if (req->r_parent &&
Sage Weil167c9e32010-05-14 10:02:57 -07002778 (req->r_op & CEPH_MDS_OP_WRITE))
2779 ceph_invalidate_dir_request(req);
Sage Weile1518c72010-05-13 11:19:06 -07002780 } else {
2781 err = req->r_err;
2782 }
2783
Sage Weile1518c72010-05-13 11:19:06 -07002784 mutex_unlock(&mdsc->mutex);
Jeff Layton8340f222019-04-02 12:34:38 -04002785 return err;
2786}
2787
2788/*
2789 * Synchrously perform an mds request. Take care of all of the
2790 * session setup, forwarding, retry details.
2791 */
2792int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
2793 struct inode *dir,
2794 struct ceph_mds_request *req)
2795{
2796 int err;
2797
2798 dout("do_request on %p\n", req);
2799
2800 /* issue */
2801 err = ceph_mdsc_submit_request(mdsc, dir, req);
2802 if (!err)
2803 err = ceph_mdsc_wait_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002804 dout("do_request %p done, result %d\n", req, err);
2805 return err;
2806}
2807
2808/*
Yan, Zheng2f276c52013-03-13 19:44:32 +08002809 * Invalidate dir's completeness, dentry lease state on an aborted MDS
Sage Weil167c9e32010-05-14 10:02:57 -07002810 * namespace request.
2811 */
2812void ceph_invalidate_dir_request(struct ceph_mds_request *req)
2813{
Yan, Zheng8d8f3712017-11-24 11:51:32 +08002814 struct inode *dir = req->r_parent;
2815 struct inode *old_dir = req->r_old_dentry_dir;
Sage Weil167c9e32010-05-14 10:02:57 -07002816
Yan, Zheng8d8f3712017-11-24 11:51:32 +08002817 dout("invalidate_dir_request %p %p (complete, lease(s))\n", dir, old_dir);
Sage Weil167c9e32010-05-14 10:02:57 -07002818
Yan, Zheng8d8f3712017-11-24 11:51:32 +08002819 ceph_dir_clear_complete(dir);
2820 if (old_dir)
2821 ceph_dir_clear_complete(old_dir);
Sage Weil167c9e32010-05-14 10:02:57 -07002822 if (req->r_dentry)
2823 ceph_invalidate_dentry_lease(req->r_dentry);
2824 if (req->r_old_dentry)
2825 ceph_invalidate_dentry_lease(req->r_old_dentry);
2826}
2827
2828/*
Sage Weil2f2dc052009-10-06 11:31:09 -07002829 * Handle mds reply.
2830 *
2831 * We take the session mutex and parse and process the reply immediately.
2832 * This preserves the logical ordering of replies, capabilities, etc., sent
2833 * by the MDS as they are applied to our local cache.
2834 */
2835static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2836{
2837 struct ceph_mds_client *mdsc = session->s_mdsc;
2838 struct ceph_mds_request *req;
2839 struct ceph_mds_reply_head *head = msg->front.iov_base;
2840 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
Yan, Zheng982d6012014-12-23 15:30:54 +08002841 struct ceph_snap_realm *realm;
Sage Weil2f2dc052009-10-06 11:31:09 -07002842 u64 tid;
2843 int err, result;
Sage Weil2600d2d2010-02-22 15:12:16 -08002844 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002845
Sage Weil2f2dc052009-10-06 11:31:09 -07002846 if (msg->front.iov_len < sizeof(*head)) {
2847 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002848 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002849 return;
2850 }
2851
2852 /* get request, session */
Sage Weil6df058c2009-12-22 11:24:33 -08002853 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002854 mutex_lock(&mdsc->mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02002855 req = lookup_get_request(mdsc, tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002856 if (!req) {
2857 dout("handle_reply on unknown tid %llu\n", tid);
2858 mutex_unlock(&mdsc->mutex);
2859 return;
2860 }
2861 dout("handle_reply %p\n", req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002862
2863 /* correct session? */
Sage Weild96d6042010-03-20 20:50:58 -07002864 if (req->r_session != session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002865 pr_err("mdsc_handle_reply got %llu on session mds%d"
2866 " not mds%d\n", tid, session->s_mds,
2867 req->r_session ? req->r_session->s_mds : -1);
2868 mutex_unlock(&mdsc->mutex);
2869 goto out;
2870 }
2871
2872 /* dup? */
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002873 if ((test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags) && !head->safe) ||
2874 (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags) && head->safe)) {
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07002875 pr_warn("got a dup %s reply on %llu from mds%d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002876 head->safe ? "safe" : "unsafe", tid, mds);
2877 mutex_unlock(&mdsc->mutex);
2878 goto out;
2879 }
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002880 if (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags)) {
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07002881 pr_warn("got unsafe after safe on %llu from mds%d\n",
Sage Weil85792d0d2010-05-13 09:06:02 -07002882 tid, mds);
2883 mutex_unlock(&mdsc->mutex);
2884 goto out;
2885 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002886
2887 result = le32_to_cpu(head->result);
2888
2889 /*
Greg Farnume55b71f2010-06-22 15:58:01 -07002890 * Handle an ESTALE
2891 * if we're not talking to the authority, send to them
2892 * if the authority has changed while we weren't looking,
2893 * send to new authority
2894 * Otherwise we just have to return an ESTALE
Sage Weil2f2dc052009-10-06 11:31:09 -07002895 */
2896 if (result == -ESTALE) {
Chengguang Xu4c069a52018-01-30 16:29:17 +08002897 dout("got ESTALE on request %llu\n", req->r_tid);
Yan, Zheng51da8e82014-07-14 10:48:29 +08002898 req->r_resend_mds = -1;
Yan, Zhengca18bed2013-11-22 14:21:44 +08002899 if (req->r_direct_mode != USE_AUTH_MDS) {
Chengguang Xu4c069a52018-01-30 16:29:17 +08002900 dout("not using auth, setting for that now\n");
Greg Farnume55b71f2010-06-22 15:58:01 -07002901 req->r_direct_mode = USE_AUTH_MDS;
Sage Weil2f2dc052009-10-06 11:31:09 -07002902 __do_request(mdsc, req);
2903 mutex_unlock(&mdsc->mutex);
2904 goto out;
Greg Farnume55b71f2010-06-22 15:58:01 -07002905 } else {
Xiubo Lic4853e92019-12-09 07:47:15 -05002906 int mds = __choose_mds(mdsc, req, NULL);
Yan, Zhengca18bed2013-11-22 14:21:44 +08002907 if (mds >= 0 && mds != req->r_session->s_mds) {
Chengguang Xu4c069a52018-01-30 16:29:17 +08002908 dout("but auth changed, so resending\n");
Greg Farnume55b71f2010-06-22 15:58:01 -07002909 __do_request(mdsc, req);
2910 mutex_unlock(&mdsc->mutex);
2911 goto out;
2912 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002913 }
Chengguang Xu4c069a52018-01-30 16:29:17 +08002914 dout("have to return ESTALE on request %llu\n", req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002915 }
2916
Greg Farnume55b71f2010-06-22 15:58:01 -07002917
Sage Weil2f2dc052009-10-06 11:31:09 -07002918 if (head->safe) {
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002919 set_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags);
Sage Weil2f2dc052009-10-06 11:31:09 -07002920 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002921
Xiubo Li07edc052019-12-04 01:27:18 -05002922 /* last request during umount? */
2923 if (mdsc->stopping && !__get_oldest_req(mdsc))
2924 complete_all(&mdsc->safe_umount_waiters);
2925
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002926 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002927 /*
2928 * We already handled the unsafe response, now do the
2929 * cleanup. No need to examine the response; the MDS
2930 * doesn't include any result info in the safe
2931 * response. And even if it did, there is nothing
2932 * useful we could do with a revised return value.
2933 */
2934 dout("got safe reply %llu, mds%d\n", tid, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002935
Sage Weil2f2dc052009-10-06 11:31:09 -07002936 mutex_unlock(&mdsc->mutex);
2937 goto out;
2938 }
Sage Weile1518c72010-05-13 11:19:06 -07002939 } else {
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002940 set_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags);
Sage Weil2f2dc052009-10-06 11:31:09 -07002941 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
Yan, Zheng4c06ace2015-10-27 17:18:00 +08002942 if (req->r_unsafe_dir) {
2943 struct ceph_inode_info *ci =
2944 ceph_inode(req->r_unsafe_dir);
2945 spin_lock(&ci->i_unsafe_lock);
2946 list_add_tail(&req->r_unsafe_dir_item,
2947 &ci->i_unsafe_dirops);
2948 spin_unlock(&ci->i_unsafe_lock);
2949 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002950 }
2951
2952 dout("handle_reply tid %lld result %d\n", tid, result);
2953 rinfo = &req->r_reply_info;
Yan, Zhengb37fe1f2019-01-09 10:10:17 +08002954 if (test_bit(CEPHFS_FEATURE_REPLY_ENCODING, &session->s_features))
2955 err = parse_reply_info(msg, rinfo, (u64)-1);
2956 else
2957 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
Sage Weil2f2dc052009-10-06 11:31:09 -07002958 mutex_unlock(&mdsc->mutex);
2959
2960 mutex_lock(&session->s_mutex);
2961 if (err < 0) {
Herb Shiu25933ab2010-12-01 14:14:38 -08002962 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002963 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002964 goto out_err;
2965 }
2966
2967 /* snap trace */
Yan, Zheng982d6012014-12-23 15:30:54 +08002968 realm = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07002969 if (rinfo->snapblob_len) {
2970 down_write(&mdsc->snap_rwsem);
2971 ceph_update_snap_trace(mdsc, rinfo->snapblob,
Yan, Zheng982d6012014-12-23 15:30:54 +08002972 rinfo->snapblob + rinfo->snapblob_len,
2973 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP,
2974 &realm);
Sage Weil2f2dc052009-10-06 11:31:09 -07002975 downgrade_write(&mdsc->snap_rwsem);
2976 } else {
2977 down_read(&mdsc->snap_rwsem);
2978 }
2979
2980 /* insert trace into our cache */
Sage Weilb4556392010-05-13 12:01:13 -07002981 mutex_lock(&req->r_fill_mutex);
Yan, Zheng315f2402016-03-07 10:34:50 +08002982 current->journal_info = req;
Jeff Laytonf5a03b02017-01-31 11:06:13 -05002983 err = ceph_fill_trace(mdsc->fsc->sb, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002984 if (err == 0) {
Sam Lang6e8575f2012-12-28 09:56:46 -08002985 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
Yan, Zheng81c6aea2013-09-18 09:44:13 +08002986 req->r_op == CEPH_MDS_OP_LSSNAP))
Sage Weil2f2dc052009-10-06 11:31:09 -07002987 ceph_readdir_prepopulate(req, req->r_session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002988 }
Yan, Zheng315f2402016-03-07 10:34:50 +08002989 current->journal_info = NULL;
Sage Weilb4556392010-05-13 12:01:13 -07002990 mutex_unlock(&req->r_fill_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002991
2992 up_read(&mdsc->snap_rwsem);
Yan, Zheng982d6012014-12-23 15:30:54 +08002993 if (realm)
2994 ceph_put_snap_realm(mdsc, realm);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002995
Yan, Zhengfe330322019-02-01 14:57:15 +08002996 if (err == 0) {
2997 if (req->r_target_inode &&
2998 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
2999 struct ceph_inode_info *ci =
3000 ceph_inode(req->r_target_inode);
3001 spin_lock(&ci->i_unsafe_lock);
3002 list_add_tail(&req->r_unsafe_target_item,
3003 &ci->i_unsafe_iops);
3004 spin_unlock(&ci->i_unsafe_lock);
3005 }
3006
3007 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08003008 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003009out_err:
Sage Weile1518c72010-05-13 11:19:06 -07003010 mutex_lock(&mdsc->mutex);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003011 if (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
Sage Weile1518c72010-05-13 11:19:06 -07003012 if (err) {
3013 req->r_err = err;
3014 } else {
Jianpeng Ma5fdb1382015-08-18 10:30:38 +08003015 req->r_reply = ceph_msg_get(msg);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003016 set_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags);
Sage Weile1518c72010-05-13 11:19:06 -07003017 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003018 } else {
Sage Weile1518c72010-05-13 11:19:06 -07003019 dout("reply arrived after request %lld was aborted\n", tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07003020 }
Sage Weile1518c72010-05-13 11:19:06 -07003021 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003022
Sage Weil2f2dc052009-10-06 11:31:09 -07003023 mutex_unlock(&session->s_mutex);
3024
3025 /* kick calling process */
3026 complete_request(mdsc, req);
3027out:
3028 ceph_mdsc_put_request(req);
3029 return;
3030}
3031
3032
3033
3034/*
3035 * handle mds notification that our request has been forwarded.
3036 */
Sage Weil2600d2d2010-02-22 15:12:16 -08003037static void handle_forward(struct ceph_mds_client *mdsc,
3038 struct ceph_mds_session *session,
3039 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07003040{
3041 struct ceph_mds_request *req;
Sage Weila1ea7872010-02-23 14:02:44 -08003042 u64 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07003043 u32 next_mds;
3044 u32 fwd_seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003045 int err = -EINVAL;
3046 void *p = msg->front.iov_base;
3047 void *end = p + msg->front.iov_len;
Sage Weil2f2dc052009-10-06 11:31:09 -07003048
Sage Weila1ea7872010-02-23 14:02:44 -08003049 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07003050 next_mds = ceph_decode_32(&p);
3051 fwd_seq = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07003052
3053 mutex_lock(&mdsc->mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02003054 req = lookup_get_request(mdsc, tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07003055 if (!req) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07003056 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003057 goto out; /* dup reply? */
3058 }
3059
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003060 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07003061 dout("forward tid %llu aborted, unregistering\n", tid);
3062 __unregister_request(mdsc, req);
3063 } else if (fwd_seq <= req->r_num_fwd) {
3064 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07003065 tid, next_mds, req->r_num_fwd, fwd_seq);
3066 } else {
3067 /* resend. forward race not possible; mds would drop */
Sage Weil2a8e5e32010-05-28 16:43:16 -07003068 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
3069 BUG_ON(req->r_err);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003070 BUG_ON(test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags));
Yan, Zheng3de22be2015-02-04 14:26:22 +08003071 req->r_attempts = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003072 req->r_num_fwd = fwd_seq;
3073 req->r_resend_mds = next_mds;
3074 put_request_session(req);
3075 __do_request(mdsc, req);
3076 }
3077 ceph_mdsc_put_request(req);
3078out:
3079 mutex_unlock(&mdsc->mutex);
3080 return;
3081
3082bad:
3083 pr_err("mdsc_handle_forward decode error err=%d\n", err);
3084}
3085
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003086static int __decode_session_metadata(void **p, void *end,
3087 bool *blacklisted)
Yan, Zheng84bf3952018-12-21 17:41:39 +08003088{
3089 /* map<string,string> */
3090 u32 n;
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003091 bool err_str;
Yan, Zheng84bf3952018-12-21 17:41:39 +08003092 ceph_decode_32_safe(p, end, n, bad);
3093 while (n-- > 0) {
3094 u32 len;
3095 ceph_decode_32_safe(p, end, len, bad);
3096 ceph_decode_need(p, end, len, bad);
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003097 err_str = !strncmp(*p, "error_string", len);
Yan, Zheng84bf3952018-12-21 17:41:39 +08003098 *p += len;
3099 ceph_decode_32_safe(p, end, len, bad);
3100 ceph_decode_need(p, end, len, bad);
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003101 if (err_str && strnstr(*p, "blacklisted", len))
3102 *blacklisted = true;
Yan, Zheng84bf3952018-12-21 17:41:39 +08003103 *p += len;
3104 }
3105 return 0;
3106bad:
3107 return -1;
3108}
3109
Sage Weil2f2dc052009-10-06 11:31:09 -07003110/*
3111 * handle a mds session control message
3112 */
3113static void handle_session(struct ceph_mds_session *session,
3114 struct ceph_msg *msg)
3115{
3116 struct ceph_mds_client *mdsc = session->s_mdsc;
Yan, Zheng84bf3952018-12-21 17:41:39 +08003117 int mds = session->s_mds;
3118 int msg_version = le16_to_cpu(msg->hdr.version);
3119 void *p = msg->front.iov_base;
3120 void *end = p + msg->front.iov_len;
3121 struct ceph_mds_session_head *h;
Sage Weil2f2dc052009-10-06 11:31:09 -07003122 u32 op;
3123 u64 seq;
Yan, Zheng84bf3952018-12-21 17:41:39 +08003124 unsigned long features = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003125 int wake = 0;
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003126 bool blacklisted = false;
Sage Weil2f2dc052009-10-06 11:31:09 -07003127
Sage Weil2f2dc052009-10-06 11:31:09 -07003128 /* decode */
Yan, Zheng84bf3952018-12-21 17:41:39 +08003129 ceph_decode_need(&p, end, sizeof(*h), bad);
3130 h = p;
3131 p += sizeof(*h);
3132
Sage Weil2f2dc052009-10-06 11:31:09 -07003133 op = le32_to_cpu(h->op);
3134 seq = le64_to_cpu(h->seq);
3135
Yan, Zheng84bf3952018-12-21 17:41:39 +08003136 if (msg_version >= 3) {
3137 u32 len;
3138 /* version >= 2, metadata */
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003139 if (__decode_session_metadata(&p, end, &blacklisted) < 0)
Yan, Zheng84bf3952018-12-21 17:41:39 +08003140 goto bad;
3141 /* version >= 3, feature bits */
3142 ceph_decode_32_safe(&p, end, len, bad);
3143 ceph_decode_need(&p, end, len, bad);
3144 memcpy(&features, p, min_t(size_t, len, sizeof(features)));
3145 p += len;
3146 }
3147
Sage Weil2f2dc052009-10-06 11:31:09 -07003148 mutex_lock(&mdsc->mutex);
Yan, Zheng0a07fc82017-03-29 15:30:24 +08003149 if (op == CEPH_SESSION_CLOSE) {
Xiubo Li5b3248c2019-12-19 19:44:09 -05003150 ceph_get_mds_session(session);
Sage Weil2600d2d2010-02-22 15:12:16 -08003151 __unregister_session(mdsc, session);
Yan, Zheng0a07fc82017-03-29 15:30:24 +08003152 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003153 /* FIXME: this ttl calculation is generous */
3154 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
3155 mutex_unlock(&mdsc->mutex);
3156
3157 mutex_lock(&session->s_mutex);
3158
3159 dout("handle_session mds%d %s %p state %s seq %llu\n",
3160 mds, ceph_session_op_name(op), session,
John Spraya687eca2014-09-19 13:51:08 +01003161 ceph_session_state_name(session->s_state), seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003162
3163 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
3164 session->s_state = CEPH_MDS_SESSION_OPEN;
3165 pr_info("mds%d came back\n", session->s_mds);
3166 }
3167
3168 switch (op) {
3169 case CEPH_SESSION_OPEN:
Sage Weil29790f22010-03-18 14:45:05 -07003170 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
3171 pr_info("mds%d reconnect success\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003172 session->s_state = CEPH_MDS_SESSION_OPEN;
Yan, Zheng84bf3952018-12-21 17:41:39 +08003173 session->s_features = features;
Sage Weil2f2dc052009-10-06 11:31:09 -07003174 renewed_caps(mdsc, session, 0);
3175 wake = 1;
3176 if (mdsc->stopping)
3177 __close_session(mdsc, session);
3178 break;
3179
3180 case CEPH_SESSION_RENEWCAPS:
3181 if (session->s_renew_seq == seq)
3182 renewed_caps(mdsc, session, 1);
3183 break;
3184
3185 case CEPH_SESSION_CLOSE:
Sage Weil29790f22010-03-18 14:45:05 -07003186 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
3187 pr_info("mds%d reconnect denied\n", session->s_mds);
Xiubo Li4d681c22019-12-05 22:35:51 -05003188 session->s_state = CEPH_MDS_SESSION_CLOSED;
Yan, Zheng1c841a92015-03-24 20:15:36 +08003189 cleanup_session_requests(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003190 remove_session_caps(session);
Yan, Zheng656e4382014-09-11 14:25:18 +08003191 wake = 2; /* for good measure */
Sage Weilf3c60c52010-08-11 14:51:23 -07003192 wake_up_all(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003193 break;
3194
3195 case CEPH_SESSION_STALE:
3196 pr_info("mds%d caps went stale, renewing\n",
3197 session->s_mds);
Alex Elderd8fb02a2012-01-12 17:48:10 -08003198 spin_lock(&session->s_gen_ttl_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07003199 session->s_cap_gen++;
Alex Elder1ce208a2012-01-12 17:48:11 -08003200 session->s_cap_ttl = jiffies - 1;
Alex Elderd8fb02a2012-01-12 17:48:10 -08003201 spin_unlock(&session->s_gen_ttl_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07003202 send_renew_caps(mdsc, session);
3203 break;
3204
3205 case CEPH_SESSION_RECALL_STATE:
Zhi Zhange30ee582018-01-24 21:24:33 +08003206 ceph_trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
Sage Weil2f2dc052009-10-06 11:31:09 -07003207 break;
3208
Yan, Zheng186e4f72013-11-22 14:48:37 +08003209 case CEPH_SESSION_FLUSHMSG:
3210 send_flushmsg_ack(mdsc, session, seq);
3211 break;
3212
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08003213 case CEPH_SESSION_FORCE_RO:
3214 dout("force_session_readonly %p\n", session);
3215 spin_lock(&session->s_cap_lock);
3216 session->s_readonly = true;
3217 spin_unlock(&session->s_cap_lock);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08003218 wake_up_session_caps(session, FORCE_RO);
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08003219 break;
3220
Yan, Zhengfcff4152016-09-14 16:39:51 +08003221 case CEPH_SESSION_REJECT:
3222 WARN_ON(session->s_state != CEPH_MDS_SESSION_OPENING);
3223 pr_info("mds%d rejected session\n", session->s_mds);
3224 session->s_state = CEPH_MDS_SESSION_REJECTED;
3225 cleanup_session_requests(mdsc, session);
3226 remove_session_caps(session);
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003227 if (blacklisted)
3228 mdsc->fsc->blacklisted = true;
Yan, Zhengfcff4152016-09-14 16:39:51 +08003229 wake = 2; /* for good measure */
3230 break;
3231
Sage Weil2f2dc052009-10-06 11:31:09 -07003232 default:
3233 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
3234 WARN_ON(1);
3235 }
3236
3237 mutex_unlock(&session->s_mutex);
3238 if (wake) {
3239 mutex_lock(&mdsc->mutex);
3240 __wake_requests(mdsc, &session->s_waiting);
Yan, Zheng656e4382014-09-11 14:25:18 +08003241 if (wake == 2)
3242 kick_requests(mdsc, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003243 mutex_unlock(&mdsc->mutex);
3244 }
Yan, Zheng0a07fc82017-03-29 15:30:24 +08003245 if (op == CEPH_SESSION_CLOSE)
3246 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003247 return;
3248
3249bad:
3250 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
3251 (int)msg->front.iov_len);
Sage Weil9ec7cab2009-12-14 15:13:47 -08003252 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003253 return;
3254}
3255
Sage Weil2f2dc052009-10-06 11:31:09 -07003256/*
3257 * called under session->mutex.
3258 */
3259static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
3260 struct ceph_mds_session *session)
3261{
3262 struct ceph_mds_request *req, *nreq;
Yan, Zheng3de22be2015-02-04 14:26:22 +08003263 struct rb_node *p;
Sage Weil2f2dc052009-10-06 11:31:09 -07003264
3265 dout("replay_unsafe_requests mds%d\n", session->s_mds);
3266
3267 mutex_lock(&mdsc->mutex);
Xiubo Li9cf54562019-12-05 20:50:21 -05003268 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item)
3269 __send_request(mdsc, session, req, true);
Yan, Zheng3de22be2015-02-04 14:26:22 +08003270
3271 /*
3272 * also re-send old requests when MDS enters reconnect stage. So that MDS
3273 * can process completed request in clientreplay stage.
3274 */
3275 p = rb_first(&mdsc->request_tree);
3276 while (p) {
3277 req = rb_entry(p, struct ceph_mds_request, r_node);
3278 p = rb_next(p);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003279 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Yan, Zheng3de22be2015-02-04 14:26:22 +08003280 continue;
3281 if (req->r_attempts == 0)
3282 continue; /* only old requests */
3283 if (req->r_session &&
Xiubo Li9cf54562019-12-05 20:50:21 -05003284 req->r_session->s_mds == session->s_mds)
3285 __send_request(mdsc, session, req, true);
Yan, Zheng3de22be2015-02-04 14:26:22 +08003286 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003287 mutex_unlock(&mdsc->mutex);
3288}
3289
Yan, Zheng81c5a142019-01-01 16:28:33 +08003290static int send_reconnect_partial(struct ceph_reconnect_state *recon_state)
3291{
3292 struct ceph_msg *reply;
3293 struct ceph_pagelist *_pagelist;
3294 struct page *page;
3295 __le32 *addr;
3296 int err = -ENOMEM;
3297
3298 if (!recon_state->allow_multi)
3299 return -ENOSPC;
3300
3301 /* can't handle message that contains both caps and realm */
3302 BUG_ON(!recon_state->nr_caps == !recon_state->nr_realms);
3303
3304 /* pre-allocate new pagelist */
3305 _pagelist = ceph_pagelist_alloc(GFP_NOFS);
3306 if (!_pagelist)
3307 return -ENOMEM;
3308
3309 reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false);
3310 if (!reply)
3311 goto fail_msg;
3312
3313 /* placeholder for nr_caps */
3314 err = ceph_pagelist_encode_32(_pagelist, 0);
3315 if (err < 0)
3316 goto fail;
3317
3318 if (recon_state->nr_caps) {
3319 /* currently encoding caps */
3320 err = ceph_pagelist_encode_32(recon_state->pagelist, 0);
3321 if (err)
3322 goto fail;
3323 } else {
3324 /* placeholder for nr_realms (currently encoding relams) */
3325 err = ceph_pagelist_encode_32(_pagelist, 0);
3326 if (err < 0)
3327 goto fail;
3328 }
3329
3330 err = ceph_pagelist_encode_8(recon_state->pagelist, 1);
3331 if (err)
3332 goto fail;
3333
3334 page = list_first_entry(&recon_state->pagelist->head, struct page, lru);
3335 addr = kmap_atomic(page);
3336 if (recon_state->nr_caps) {
3337 /* currently encoding caps */
3338 *addr = cpu_to_le32(recon_state->nr_caps);
3339 } else {
3340 /* currently encoding relams */
3341 *(addr + 1) = cpu_to_le32(recon_state->nr_realms);
3342 }
3343 kunmap_atomic(addr);
3344
3345 reply->hdr.version = cpu_to_le16(5);
3346 reply->hdr.compat_version = cpu_to_le16(4);
3347
3348 reply->hdr.data_len = cpu_to_le32(recon_state->pagelist->length);
3349 ceph_msg_data_add_pagelist(reply, recon_state->pagelist);
3350
3351 ceph_con_send(&recon_state->session->s_con, reply);
3352 ceph_pagelist_release(recon_state->pagelist);
3353
3354 recon_state->pagelist = _pagelist;
3355 recon_state->nr_caps = 0;
3356 recon_state->nr_realms = 0;
3357 recon_state->msg_version = 5;
3358 return 0;
3359fail:
3360 ceph_msg_put(reply);
3361fail_msg:
3362 ceph_pagelist_release(_pagelist);
3363 return err;
3364}
3365
Sage Weil2f2dc052009-10-06 11:31:09 -07003366/*
3367 * Encode information about a cap for a reconnect with the MDS.
3368 */
Sage Weil2f2dc052009-10-06 11:31:09 -07003369static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
3370 void *arg)
3371{
Sage Weil20cb34a2010-05-12 15:21:32 -07003372 union {
3373 struct ceph_mds_cap_reconnect v2;
3374 struct ceph_mds_cap_reconnect_v1 v1;
3375 } rec;
Yan, Zhengb3f8d682017-09-11 10:58:55 +08003376 struct ceph_inode_info *ci = cap->ci;
Sage Weil20cb34a2010-05-12 15:21:32 -07003377 struct ceph_reconnect_state *recon_state = arg;
3378 struct ceph_pagelist *pagelist = recon_state->pagelist;
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003379 int err;
Yan, Zheng3469ed02016-07-05 09:32:31 +08003380 u64 snap_follows;
Sage Weil2f2dc052009-10-06 11:31:09 -07003381
Sage Weil2f2dc052009-10-06 11:31:09 -07003382 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
3383 inode, ceph_vinop(inode), cap, cap->cap_id,
3384 ceph_cap_string(cap->issued));
Sage Weil2f2dc052009-10-06 11:31:09 -07003385
Sage Weilbe655592011-11-30 09:47:09 -08003386 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07003387 cap->seq = 0; /* reset cap seq */
3388 cap->issue_seq = 0; /* and issue_seq */
Yan, Zheng667ca052013-05-31 16:25:36 +08003389 cap->mseq = 0; /* and migrate_seq */
Yan, Zheng99a9c272013-09-22 11:08:14 +08003390 cap->cap_gen = cap->session->s_cap_gen;
Sage Weil20cb34a2010-05-12 15:21:32 -07003391
Yan, Zheng121f22a2016-07-04 22:05:18 +08003392 if (recon_state->msg_version >= 2) {
Sage Weil20cb34a2010-05-12 15:21:32 -07003393 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
3394 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
3395 rec.v2.issued = cpu_to_le32(cap->issued);
3396 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003397 rec.v2.pathbase = 0;
Jeff Laytonec1dff22017-10-31 15:51:14 -04003398 rec.v2.flock_len = (__force __le32)
3399 ((ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) ? 0 : 1);
Sage Weil20cb34a2010-05-12 15:21:32 -07003400 } else {
3401 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
3402 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
3403 rec.v1.issued = cpu_to_le32(cap->issued);
3404 rec.v1.size = cpu_to_le64(inode->i_size);
Arnd Bergmann9bbeab42018-07-13 22:18:36 +02003405 ceph_encode_timespec64(&rec.v1.mtime, &inode->i_mtime);
3406 ceph_encode_timespec64(&rec.v1.atime, &inode->i_atime);
Sage Weil20cb34a2010-05-12 15:21:32 -07003407 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003408 rec.v1.pathbase = 0;
Sage Weil20cb34a2010-05-12 15:21:32 -07003409 }
Yan, Zheng3469ed02016-07-05 09:32:31 +08003410
3411 if (list_empty(&ci->i_cap_snaps)) {
Yan, Zheng92776fd2017-08-16 21:42:39 +08003412 snap_follows = ci->i_head_snapc ? ci->i_head_snapc->seq : 0;
Yan, Zheng3469ed02016-07-05 09:32:31 +08003413 } else {
3414 struct ceph_cap_snap *capsnap =
3415 list_first_entry(&ci->i_cap_snaps,
3416 struct ceph_cap_snap, ci_item);
3417 snap_follows = capsnap->follows;
Sage Weil93cea5b2009-12-23 12:21:51 -08003418 }
Sage Weilbe655592011-11-30 09:47:09 -08003419 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07003420
Yan, Zheng121f22a2016-07-04 22:05:18 +08003421 if (recon_state->msg_version >= 2) {
Greg Farnum40819f62010-08-02 15:34:23 -07003422 int num_fcntl_locks, num_flock_locks;
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003423 struct ceph_filelock *flocks = NULL;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003424 size_t struct_len, total_len = sizeof(u64);
Yan, Zheng121f22a2016-07-04 22:05:18 +08003425 u8 struct_v = 0;
Greg Farnum40819f62010-08-02 15:34:23 -07003426
Jim Schutt39be95e2013-05-15 13:03:35 -05003427encode_again:
Yan, Zhengb3f8d682017-09-11 10:58:55 +08003428 if (rec.v2.flock_len) {
3429 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
3430 } else {
3431 num_fcntl_locks = 0;
3432 num_flock_locks = 0;
3433 }
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003434 if (num_fcntl_locks + num_flock_locks > 0) {
Kees Cook6da2ec52018-06-12 13:55:00 -07003435 flocks = kmalloc_array(num_fcntl_locks + num_flock_locks,
3436 sizeof(struct ceph_filelock),
3437 GFP_NOFS);
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003438 if (!flocks) {
3439 err = -ENOMEM;
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003440 goto out_err;
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003441 }
3442 err = ceph_encode_locks_to_buffer(inode, flocks,
3443 num_fcntl_locks,
3444 num_flock_locks);
3445 if (err) {
3446 kfree(flocks);
3447 flocks = NULL;
3448 if (err == -ENOSPC)
3449 goto encode_again;
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003450 goto out_err;
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003451 }
3452 } else {
Jim Schutt39be95e2013-05-15 13:03:35 -05003453 kfree(flocks);
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003454 flocks = NULL;
Jim Schutt39be95e2013-05-15 13:03:35 -05003455 }
Yan, Zheng121f22a2016-07-04 22:05:18 +08003456
3457 if (recon_state->msg_version >= 3) {
3458 /* version, compat_version and struct_len */
Yan, Zheng81c5a142019-01-01 16:28:33 +08003459 total_len += 2 * sizeof(u8) + sizeof(u32);
Yan, Zheng3469ed02016-07-05 09:32:31 +08003460 struct_v = 2;
Yan, Zheng121f22a2016-07-04 22:05:18 +08003461 }
Jim Schutt39be95e2013-05-15 13:03:35 -05003462 /*
3463 * number of encoded locks is stable, so copy to pagelist
3464 */
Yan, Zheng121f22a2016-07-04 22:05:18 +08003465 struct_len = 2 * sizeof(u32) +
3466 (num_fcntl_locks + num_flock_locks) *
3467 sizeof(struct ceph_filelock);
3468 rec.v2.flock_len = cpu_to_le32(struct_len);
3469
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003470 struct_len += sizeof(u32) + sizeof(rec.v2);
Yan, Zheng121f22a2016-07-04 22:05:18 +08003471
Yan, Zheng3469ed02016-07-05 09:32:31 +08003472 if (struct_v >= 2)
3473 struct_len += sizeof(u64); /* snap_follows */
3474
Yan, Zheng121f22a2016-07-04 22:05:18 +08003475 total_len += struct_len;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003476
3477 if (pagelist->length + total_len > RECONNECT_MAX_SIZE) {
3478 err = send_reconnect_partial(recon_state);
3479 if (err)
3480 goto out_freeflocks;
3481 pagelist = recon_state->pagelist;
Yan, Zheng121f22a2016-07-04 22:05:18 +08003482 }
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003483
Yan, Zheng81c5a142019-01-01 16:28:33 +08003484 err = ceph_pagelist_reserve(pagelist, total_len);
3485 if (err)
3486 goto out_freeflocks;
3487
3488 ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003489 if (recon_state->msg_version >= 3) {
3490 ceph_pagelist_encode_8(pagelist, struct_v);
3491 ceph_pagelist_encode_8(pagelist, 1);
3492 ceph_pagelist_encode_32(pagelist, struct_len);
3493 }
3494 ceph_pagelist_encode_string(pagelist, NULL, 0);
3495 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v2));
3496 ceph_locks_to_pagelist(flocks, pagelist,
3497 num_fcntl_locks, num_flock_locks);
3498 if (struct_v >= 2)
3499 ceph_pagelist_encode_64(pagelist, snap_follows);
Yan, Zheng81c5a142019-01-01 16:28:33 +08003500out_freeflocks:
Jim Schutt39be95e2013-05-15 13:03:35 -05003501 kfree(flocks);
Sage Weil3612abb2010-09-07 15:59:27 -07003502 } else {
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003503 u64 pathbase = 0;
3504 int pathlen = 0;
3505 char *path = NULL;
3506 struct dentry *dentry;
3507
3508 dentry = d_find_alias(inode);
3509 if (dentry) {
3510 path = ceph_mdsc_build_path(dentry,
3511 &pathlen, &pathbase, 0);
3512 dput(dentry);
3513 if (IS_ERR(path)) {
3514 err = PTR_ERR(path);
3515 goto out_err;
3516 }
3517 rec.v1.pathbase = cpu_to_le64(pathbase);
Yan, Zheng121f22a2016-07-04 22:05:18 +08003518 }
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003519
3520 err = ceph_pagelist_reserve(pagelist,
Yan, Zheng81c5a142019-01-01 16:28:33 +08003521 sizeof(u64) + sizeof(u32) +
3522 pathlen + sizeof(rec.v1));
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003523 if (err) {
Yan, Zheng81c5a142019-01-01 16:28:33 +08003524 goto out_freepath;
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003525 }
3526
Yan, Zheng81c5a142019-01-01 16:28:33 +08003527 ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003528 ceph_pagelist_encode_string(pagelist, path, pathlen);
3529 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v1));
Yan, Zheng81c5a142019-01-01 16:28:33 +08003530out_freepath:
Jeff Laytonf77f21b2019-04-29 12:13:14 -04003531 ceph_mdsc_free_path(path, pathlen);
Greg Farnum40819f62010-08-02 15:34:23 -07003532 }
Yan, Zheng44c99752013-09-22 10:28:10 +08003533
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003534out_err:
Yan, Zheng81c5a142019-01-01 16:28:33 +08003535 if (err >= 0)
3536 recon_state->nr_caps++;
3537 return err;
3538}
3539
3540static int encode_snap_realms(struct ceph_mds_client *mdsc,
3541 struct ceph_reconnect_state *recon_state)
3542{
3543 struct rb_node *p;
3544 struct ceph_pagelist *pagelist = recon_state->pagelist;
3545 int err = 0;
3546
3547 if (recon_state->msg_version >= 4) {
3548 err = ceph_pagelist_encode_32(pagelist, mdsc->num_snap_realms);
3549 if (err < 0)
3550 goto fail;
3551 }
3552
3553 /*
3554 * snaprealms. we provide mds with the ino, seq (version), and
3555 * parent for all of our realms. If the mds has any newer info,
3556 * it will tell us.
3557 */
3558 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
3559 struct ceph_snap_realm *realm =
3560 rb_entry(p, struct ceph_snap_realm, node);
3561 struct ceph_mds_snaprealm_reconnect sr_rec;
3562
3563 if (recon_state->msg_version >= 4) {
3564 size_t need = sizeof(u8) * 2 + sizeof(u32) +
3565 sizeof(sr_rec);
3566
3567 if (pagelist->length + need > RECONNECT_MAX_SIZE) {
3568 err = send_reconnect_partial(recon_state);
3569 if (err)
3570 goto fail;
3571 pagelist = recon_state->pagelist;
3572 }
3573
3574 err = ceph_pagelist_reserve(pagelist, need);
3575 if (err)
3576 goto fail;
3577
3578 ceph_pagelist_encode_8(pagelist, 1);
3579 ceph_pagelist_encode_8(pagelist, 1);
3580 ceph_pagelist_encode_32(pagelist, sizeof(sr_rec));
3581 }
3582
3583 dout(" adding snap realm %llx seq %lld parent %llx\n",
3584 realm->ino, realm->seq, realm->parent_ino);
3585 sr_rec.ino = cpu_to_le64(realm->ino);
3586 sr_rec.seq = cpu_to_le64(realm->seq);
3587 sr_rec.parent = cpu_to_le64(realm->parent_ino);
3588
3589 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
3590 if (err)
3591 goto fail;
3592
3593 recon_state->nr_realms++;
3594 }
3595fail:
Sage Weil93cea5b2009-12-23 12:21:51 -08003596 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07003597}
3598
3599
3600/*
3601 * If an MDS fails and recovers, clients need to reconnect in order to
3602 * reestablish shared state. This includes all caps issued through
3603 * this session _and_ the snap_realm hierarchy. Because it's not
3604 * clear which snap realms the mds cares about, we send everything we
3605 * know about.. that ensures we'll then get any new info the
3606 * recovering MDS might have.
3607 *
3608 * This is a relatively heavyweight operation, but it's rare.
3609 *
3610 * called with mdsc->mutex held.
3611 */
Sage Weil34b6c852010-05-10 16:31:25 -07003612static void send_mds_reconnect(struct ceph_mds_client *mdsc,
3613 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07003614{
Sage Weil2f2dc052009-10-06 11:31:09 -07003615 struct ceph_msg *reply;
Sage Weil34b6c852010-05-10 16:31:25 -07003616 int mds = session->s_mds;
Sage Weil9abf82b2010-05-10 21:58:38 -07003617 int err = -ENOMEM;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003618 struct ceph_reconnect_state recon_state = {
3619 .session = session,
3620 };
Jeff Laytonc8a96a32017-10-19 08:53:58 -04003621 LIST_HEAD(dispose);
Sage Weil2f2dc052009-10-06 11:31:09 -07003622
Sage Weil34b6c852010-05-10 16:31:25 -07003623 pr_info("mds%d reconnect start\n", mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003624
Yan, Zheng81c5a142019-01-01 16:28:33 +08003625 recon_state.pagelist = ceph_pagelist_alloc(GFP_NOFS);
3626 if (!recon_state.pagelist)
Sage Weil93cea5b2009-12-23 12:21:51 -08003627 goto fail_nopagelist;
Sage Weil93cea5b2009-12-23 12:21:51 -08003628
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003629 reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07003630 if (!reply)
Sage Weil93cea5b2009-12-23 12:21:51 -08003631 goto fail_nomsg;
Sage Weil93cea5b2009-12-23 12:21:51 -08003632
Sage Weil34b6c852010-05-10 16:31:25 -07003633 mutex_lock(&session->s_mutex);
3634 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
3635 session->s_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003636
Sage Weil2f2dc052009-10-06 11:31:09 -07003637 dout("session %p state %s\n", session,
John Spraya687eca2014-09-19 13:51:08 +01003638 ceph_session_state_name(session->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07003639
Yan, Zheng99a9c272013-09-22 11:08:14 +08003640 spin_lock(&session->s_gen_ttl_lock);
3641 session->s_cap_gen++;
3642 spin_unlock(&session->s_gen_ttl_lock);
3643
3644 spin_lock(&session->s_cap_lock);
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08003645 /* don't know if session is readonly */
3646 session->s_readonly = 0;
Yan, Zheng99a9c272013-09-22 11:08:14 +08003647 /*
3648 * notify __ceph_remove_cap() that we are composing cap reconnect.
3649 * If a cap get released before being added to the cap reconnect,
3650 * __ceph_remove_cap() should skip queuing cap release.
3651 */
3652 session->s_cap_reconnect = 1;
Sage Weile01a5942010-05-10 15:36:44 -07003653 /* drop old cap expires; we're about to reestablish that state */
Jeff Laytonc8a96a32017-10-19 08:53:58 -04003654 detach_cap_releases(session, &dispose);
3655 spin_unlock(&session->s_cap_lock);
3656 dispose_cap_releases(mdsc, &dispose);
Sage Weile01a5942010-05-10 15:36:44 -07003657
Yan, Zheng5d233712014-09-10 16:56:23 +08003658 /* trim unused caps to reduce MDS's cache rejoin time */
Yan, Zhengc0bd50e2015-04-07 15:51:08 +08003659 if (mdsc->fsc->sb->s_root)
3660 shrink_dcache_parent(mdsc->fsc->sb->s_root);
Yan, Zheng5d233712014-09-10 16:56:23 +08003661
3662 ceph_con_close(&session->s_con);
3663 ceph_con_open(&session->s_con,
3664 CEPH_ENTITY_TYPE_MDS, mds,
3665 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
3666
3667 /* replay unsafe requests */
3668 replay_unsafe_requests(mdsc, session);
3669
Yan, Zheng81c5a142019-01-01 16:28:33 +08003670 ceph_early_kick_flushing_caps(mdsc, session);
3671
Yan, Zheng5d233712014-09-10 16:56:23 +08003672 down_read(&mdsc->snap_rwsem);
3673
Yan, Zheng81c5a142019-01-01 16:28:33 +08003674 /* placeholder for nr_caps */
3675 err = ceph_pagelist_encode_32(recon_state.pagelist, 0);
Sage Weil93cea5b2009-12-23 12:21:51 -08003676 if (err)
3677 goto fail;
Sage Weil20cb34a2010-05-12 15:21:32 -07003678
Yan, Zheng81c5a142019-01-01 16:28:33 +08003679 if (test_bit(CEPHFS_FEATURE_MULTI_RECONNECT, &session->s_features)) {
Yan, Zheng121f22a2016-07-04 22:05:18 +08003680 recon_state.msg_version = 3;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003681 recon_state.allow_multi = true;
3682 } else if (session->s_con.peer_features & CEPH_FEATURE_MDSENC) {
3683 recon_state.msg_version = 3;
3684 } else {
Ilya Dryomov23c625c2018-11-08 14:55:21 +01003685 recon_state.msg_version = 2;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003686 }
3687 /* trsaverse this session's caps */
Jeff Laytonf5d772692019-04-24 12:09:04 -04003688 err = ceph_iterate_session_caps(session, encode_caps_cb, &recon_state);
Sage Weil2f2dc052009-10-06 11:31:09 -07003689
Yan, Zheng99a9c272013-09-22 11:08:14 +08003690 spin_lock(&session->s_cap_lock);
3691 session->s_cap_reconnect = 0;
3692 spin_unlock(&session->s_cap_lock);
3693
Yan, Zheng81c5a142019-01-01 16:28:33 +08003694 if (err < 0)
3695 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07003696
Yan, Zheng81c5a142019-01-01 16:28:33 +08003697 /* check if all realms can be encoded into current message */
3698 if (mdsc->num_snap_realms) {
3699 size_t total_len =
3700 recon_state.pagelist->length +
3701 mdsc->num_snap_realms *
3702 sizeof(struct ceph_mds_snaprealm_reconnect);
3703 if (recon_state.msg_version >= 4) {
3704 /* number of realms */
3705 total_len += sizeof(u32);
3706 /* version, compat_version and struct_len */
3707 total_len += mdsc->num_snap_realms *
3708 (2 * sizeof(u8) + sizeof(u32));
3709 }
3710 if (total_len > RECONNECT_MAX_SIZE) {
3711 if (!recon_state.allow_multi) {
3712 err = -ENOSPC;
3713 goto fail;
3714 }
3715 if (recon_state.nr_caps) {
3716 err = send_reconnect_partial(&recon_state);
3717 if (err)
3718 goto fail;
3719 }
3720 recon_state.msg_version = 5;
3721 }
3722 }
3723
3724 err = encode_snap_realms(mdsc, &recon_state);
3725 if (err < 0)
3726 goto fail;
3727
3728 if (recon_state.msg_version >= 5) {
3729 err = ceph_pagelist_encode_8(recon_state.pagelist, 0);
3730 if (err < 0)
Sage Weil93cea5b2009-12-23 12:21:51 -08003731 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07003732 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003733
Yan, Zheng81c5a142019-01-01 16:28:33 +08003734 if (recon_state.nr_caps || recon_state.nr_realms) {
3735 struct page *page =
3736 list_first_entry(&recon_state.pagelist->head,
3737 struct page, lru);
Yan, Zheng44c99752013-09-22 10:28:10 +08003738 __le32 *addr = kmap_atomic(page);
Yan, Zheng81c5a142019-01-01 16:28:33 +08003739 if (recon_state.nr_caps) {
3740 WARN_ON(recon_state.nr_realms != mdsc->num_snap_realms);
3741 *addr = cpu_to_le32(recon_state.nr_caps);
3742 } else if (recon_state.msg_version >= 4) {
3743 *(addr + 1) = cpu_to_le32(recon_state.nr_realms);
3744 }
Yan, Zheng44c99752013-09-22 10:28:10 +08003745 kunmap_atomic(addr);
Alex Elderebf18f42013-03-04 22:29:57 -06003746 }
Yan, Zheng44c99752013-09-22 10:28:10 +08003747
Yan, Zheng81c5a142019-01-01 16:28:33 +08003748 reply->hdr.version = cpu_to_le16(recon_state.msg_version);
3749 if (recon_state.msg_version >= 4)
3750 reply->hdr.compat_version = cpu_to_le16(4);
Yan, Zhenge548e9b2015-06-10 15:17:56 +08003751
Yan, Zheng81c5a142019-01-01 16:28:33 +08003752 reply->hdr.data_len = cpu_to_le32(recon_state.pagelist->length);
3753 ceph_msg_data_add_pagelist(reply, recon_state.pagelist);
Yan, Zhenge548e9b2015-06-10 15:17:56 +08003754
Sage Weil2f2dc052009-10-06 11:31:09 -07003755 ceph_con_send(&session->s_con, reply);
3756
Sage Weil9abf82b2010-05-10 21:58:38 -07003757 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003758
Sage Weil9abf82b2010-05-10 21:58:38 -07003759 mutex_lock(&mdsc->mutex);
3760 __wake_requests(mdsc, &session->s_waiting);
3761 mutex_unlock(&mdsc->mutex);
3762
Sage Weil2f2dc052009-10-06 11:31:09 -07003763 up_read(&mdsc->snap_rwsem);
Yan, Zheng81c5a142019-01-01 16:28:33 +08003764 ceph_pagelist_release(recon_state.pagelist);
Sage Weil2f2dc052009-10-06 11:31:09 -07003765 return;
3766
Sage Weil93cea5b2009-12-23 12:21:51 -08003767fail:
Sage Weil2f2dc052009-10-06 11:31:09 -07003768 ceph_msg_put(reply);
Sage Weil9abf82b2010-05-10 21:58:38 -07003769 up_read(&mdsc->snap_rwsem);
3770 mutex_unlock(&session->s_mutex);
Sage Weil93cea5b2009-12-23 12:21:51 -08003771fail_nomsg:
Yan, Zheng81c5a142019-01-01 16:28:33 +08003772 ceph_pagelist_release(recon_state.pagelist);
Sage Weil93cea5b2009-12-23 12:21:51 -08003773fail_nopagelist:
Sage Weil9abf82b2010-05-10 21:58:38 -07003774 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
Sage Weil9abf82b2010-05-10 21:58:38 -07003775 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07003776}
3777
3778
3779/*
3780 * compare old and new mdsmaps, kicking requests
3781 * and closing out old connections as necessary
3782 *
3783 * called under mdsc->mutex.
3784 */
3785static void check_new_map(struct ceph_mds_client *mdsc,
3786 struct ceph_mdsmap *newmap,
3787 struct ceph_mdsmap *oldmap)
3788{
3789 int i;
3790 int oldstate, newstate;
3791 struct ceph_mds_session *s;
3792
3793 dout("check_new_map new %u old %u\n",
3794 newmap->m_epoch, oldmap->m_epoch);
3795
Xiubo Lib38c9eb2019-12-04 06:57:39 -05003796 for (i = 0; i < oldmap->possible_max_rank && i < mdsc->max_sessions; i++) {
Markus Elfringd37b1d92017-08-20 20:22:02 +02003797 if (!mdsc->sessions[i])
Sage Weil2f2dc052009-10-06 11:31:09 -07003798 continue;
3799 s = mdsc->sessions[i];
3800 oldstate = ceph_mdsmap_get_state(oldmap, i);
3801 newstate = ceph_mdsmap_get_state(newmap, i);
3802
Sage Weil0deb01c2010-06-17 14:19:01 -07003803 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07003804 i, ceph_mds_state_name(oldstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07003805 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
Sage Weil2f2dc052009-10-06 11:31:09 -07003806 ceph_mds_state_name(newstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07003807 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
John Spraya687eca2014-09-19 13:51:08 +01003808 ceph_session_state_name(s->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07003809
Xiubo Lib38c9eb2019-12-04 06:57:39 -05003810 if (i >= newmap->possible_max_rank) {
Yan, Zheng6f0f5972019-06-10 15:45:09 +08003811 /* force close session for stopped mds */
Xiubo Li5b3248c2019-12-19 19:44:09 -05003812 ceph_get_mds_session(s);
Yan, Zheng6f0f5972019-06-10 15:45:09 +08003813 __unregister_session(mdsc, s);
3814 __wake_requests(mdsc, &s->s_waiting);
3815 mutex_unlock(&mdsc->mutex);
3816
3817 mutex_lock(&s->s_mutex);
3818 cleanup_session_requests(mdsc, s);
3819 remove_session_caps(s);
3820 mutex_unlock(&s->s_mutex);
3821
3822 ceph_put_mds_session(s);
3823
3824 mutex_lock(&mdsc->mutex);
3825 kick_requests(mdsc, i);
3826 continue;
3827 }
3828
3829 if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
Sage Weil2f2dc052009-10-06 11:31:09 -07003830 ceph_mdsmap_get_addr(newmap, i),
3831 sizeof(struct ceph_entity_addr))) {
Yan, Zheng6f0f5972019-06-10 15:45:09 +08003832 /* just close it */
3833 mutex_unlock(&mdsc->mutex);
3834 mutex_lock(&s->s_mutex);
3835 mutex_lock(&mdsc->mutex);
3836 ceph_con_close(&s->s_con);
3837 mutex_unlock(&s->s_mutex);
3838 s->s_state = CEPH_MDS_SESSION_RESTARTING;
Sage Weil2f2dc052009-10-06 11:31:09 -07003839 } else if (oldstate == newstate) {
3840 continue; /* nothing new with this mds */
3841 }
3842
3843 /*
3844 * send reconnect?
3845 */
3846 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
Sage Weil34b6c852010-05-10 16:31:25 -07003847 newstate >= CEPH_MDS_STATE_RECONNECT) {
3848 mutex_unlock(&mdsc->mutex);
3849 send_mds_reconnect(mdsc, s);
3850 mutex_lock(&mdsc->mutex);
3851 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003852
3853 /*
Sage Weil29790f22010-03-18 14:45:05 -07003854 * kick request on any mds that has gone active.
Sage Weil2f2dc052009-10-06 11:31:09 -07003855 */
3856 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
3857 newstate >= CEPH_MDS_STATE_ACTIVE) {
Sage Weil29790f22010-03-18 14:45:05 -07003858 if (oldstate != CEPH_MDS_STATE_CREATING &&
3859 oldstate != CEPH_MDS_STATE_STARTING)
3860 pr_info("mds%d recovery completed\n", s->s_mds);
3861 kick_requests(mdsc, i);
Sage Weil2f2dc052009-10-06 11:31:09 -07003862 ceph_kick_flushing_caps(mdsc, s);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08003863 wake_up_session_caps(s, RECONNECT);
Sage Weil2f2dc052009-10-06 11:31:09 -07003864 }
3865 }
Sage Weilcb170a22010-06-21 13:38:35 -07003866
Xiubo Lib38c9eb2019-12-04 06:57:39 -05003867 for (i = 0; i < newmap->possible_max_rank && i < mdsc->max_sessions; i++) {
Sage Weilcb170a22010-06-21 13:38:35 -07003868 s = mdsc->sessions[i];
3869 if (!s)
3870 continue;
3871 if (!ceph_mdsmap_is_laggy(newmap, i))
3872 continue;
3873 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3874 s->s_state == CEPH_MDS_SESSION_HUNG ||
3875 s->s_state == CEPH_MDS_SESSION_CLOSING) {
3876 dout(" connecting to export targets of laggy mds%d\n",
3877 i);
3878 __open_export_target_sessions(mdsc, s);
3879 }
3880 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003881}
3882
3883
3884
3885/*
3886 * leases
3887 */
3888
3889/*
3890 * caller must hold session s_mutex, dentry->d_lock
3891 */
3892void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
3893{
3894 struct ceph_dentry_info *di = ceph_dentry(dentry);
3895
3896 ceph_put_mds_session(di->lease_session);
3897 di->lease_session = NULL;
3898}
3899
Sage Weil2600d2d2010-02-22 15:12:16 -08003900static void handle_lease(struct ceph_mds_client *mdsc,
3901 struct ceph_mds_session *session,
3902 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07003903{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003904 struct super_block *sb = mdsc->fsc->sb;
Sage Weil2f2dc052009-10-06 11:31:09 -07003905 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07003906 struct dentry *parent, *dentry;
3907 struct ceph_dentry_info *di;
Sage Weil2600d2d2010-02-22 15:12:16 -08003908 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07003909 struct ceph_mds_lease *h = msg->front.iov_base;
Sage Weil1e5ea232010-06-04 10:05:40 -07003910 u32 seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003911 struct ceph_vino vino;
Sage Weil2f2dc052009-10-06 11:31:09 -07003912 struct qstr dname;
3913 int release = 0;
3914
Sage Weil2f2dc052009-10-06 11:31:09 -07003915 dout("handle_lease from mds%d\n", mds);
3916
3917 /* decode */
3918 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
3919 goto bad;
3920 vino.ino = le64_to_cpu(h->ino);
3921 vino.snap = CEPH_NOSNAP;
Sage Weil1e5ea232010-06-04 10:05:40 -07003922 seq = le32_to_cpu(h->seq);
Yan, Zheng0fcf6c02018-08-03 16:24:49 +08003923 dname.len = get_unaligned_le32(h + 1);
3924 if (msg->front.iov_len < sizeof(*h) + sizeof(u32) + dname.len)
Sage Weil2f2dc052009-10-06 11:31:09 -07003925 goto bad;
Yan, Zheng0fcf6c02018-08-03 16:24:49 +08003926 dname.name = (void *)(h + 1) + sizeof(u32);
Sage Weil2f2dc052009-10-06 11:31:09 -07003927
Sage Weil2f2dc052009-10-06 11:31:09 -07003928 /* lookup inode */
3929 inode = ceph_find_inode(sb, vino);
Sage Weil2f90b852011-07-26 11:28:25 -07003930 dout("handle_lease %s, ino %llx %p %.*s\n",
3931 ceph_lease_op_name(h->action), vino.ino, inode,
Sage Weil1e5ea232010-06-04 10:05:40 -07003932 dname.len, dname.name);
Yan, Zheng6cd3bca2014-09-17 07:45:12 +08003933
3934 mutex_lock(&session->s_mutex);
3935 session->s_seq++;
3936
Markus Elfringd37b1d92017-08-20 20:22:02 +02003937 if (!inode) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003938 dout("handle_lease no inode %llx\n", vino.ino);
3939 goto release;
3940 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003941
3942 /* dentry */
3943 parent = d_find_alias(inode);
3944 if (!parent) {
3945 dout("no parent dentry on inode %p\n", inode);
3946 WARN_ON(1);
3947 goto release; /* hrm... */
3948 }
Linus Torvalds8387ff22016-06-10 07:51:30 -07003949 dname.hash = full_name_hash(parent, dname.name, dname.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07003950 dentry = d_lookup(parent, &dname);
3951 dput(parent);
3952 if (!dentry)
3953 goto release;
3954
3955 spin_lock(&dentry->d_lock);
3956 di = ceph_dentry(dentry);
3957 switch (h->action) {
3958 case CEPH_MDS_LEASE_REVOKE:
Sage Weil3d8eb7a2011-11-11 09:48:53 -08003959 if (di->lease_session == session) {
Sage Weil1e5ea232010-06-04 10:05:40 -07003960 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
3961 h->seq = cpu_to_le32(di->lease_seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003962 __ceph_mdsc_drop_dentry_lease(dentry);
3963 }
3964 release = 1;
3965 break;
3966
3967 case CEPH_MDS_LEASE_RENEW:
Sage Weil3d8eb7a2011-11-11 09:48:53 -08003968 if (di->lease_session == session &&
Sage Weil2f2dc052009-10-06 11:31:09 -07003969 di->lease_gen == session->s_cap_gen &&
3970 di->lease_renew_from &&
3971 di->lease_renew_after == 0) {
3972 unsigned long duration =
Nicholas Mc Guire3563dbdd2015-02-06 06:52:17 -05003973 msecs_to_jiffies(le32_to_cpu(h->duration_ms));
Sage Weil2f2dc052009-10-06 11:31:09 -07003974
Sage Weil1e5ea232010-06-04 10:05:40 -07003975 di->lease_seq = seq;
Miklos Szeredi9b16f03c2016-06-22 16:35:04 +02003976 di->time = di->lease_renew_from + duration;
Sage Weil2f2dc052009-10-06 11:31:09 -07003977 di->lease_renew_after = di->lease_renew_from +
3978 (duration >> 1);
3979 di->lease_renew_from = 0;
3980 }
3981 break;
3982 }
3983 spin_unlock(&dentry->d_lock);
3984 dput(dentry);
3985
3986 if (!release)
3987 goto out;
3988
3989release:
3990 /* let's just reuse the same message */
3991 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
3992 ceph_msg_get(msg);
3993 ceph_con_send(&session->s_con, msg);
3994
3995out:
Sage Weil2f2dc052009-10-06 11:31:09 -07003996 mutex_unlock(&session->s_mutex);
Yan, Zheng3e1d0452019-05-18 20:39:55 +08003997 /* avoid calling iput_final() in mds dispatch threads */
3998 ceph_async_iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07003999 return;
4000
4001bad:
4002 pr_err("corrupt lease message\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08004003 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004004}
4005
4006void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
Sage Weil2f2dc052009-10-06 11:31:09 -07004007 struct dentry *dentry, char action,
4008 u32 seq)
4009{
4010 struct ceph_msg *msg;
4011 struct ceph_mds_lease *lease;
Yan, Zheng8f2a98ef2019-05-23 10:45:24 +08004012 struct inode *dir;
4013 int len = sizeof(*lease) + sizeof(u32) + NAME_MAX;
Sage Weil2f2dc052009-10-06 11:31:09 -07004014
Yan, Zheng8f2a98ef2019-05-23 10:45:24 +08004015 dout("lease_send_msg identry %p %s to mds%d\n",
4016 dentry, ceph_lease_op_name(action), session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07004017
Sage Weilb61c2762011-08-09 15:03:46 -07004018 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07004019 if (!msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07004020 return;
4021 lease = msg->front.iov_base;
4022 lease->action = action;
Sage Weil2f2dc052009-10-06 11:31:09 -07004023 lease->seq = cpu_to_le32(seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07004024
Yan, Zheng8f2a98ef2019-05-23 10:45:24 +08004025 spin_lock(&dentry->d_lock);
4026 dir = d_inode(dentry->d_parent);
4027 lease->ino = cpu_to_le64(ceph_ino(dir));
4028 lease->first = lease->last = cpu_to_le64(ceph_snap(dir));
4029
4030 put_unaligned_le32(dentry->d_name.len, lease + 1);
4031 memcpy((void *)(lease + 1) + 4,
4032 dentry->d_name.name, dentry->d_name.len);
4033 spin_unlock(&dentry->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07004034 /*
4035 * if this is a preemptive lease RELEASE, no need to
4036 * flush request stream, since the actual request will
4037 * soon follow.
4038 */
4039 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
4040
4041 ceph_con_send(&session->s_con, msg);
4042}
4043
4044/*
Yan, Zheng7aac4532018-03-13 19:52:20 +08004045 * lock unlock sessions, to wait ongoing session activities
Sage Weil2f2dc052009-10-06 11:31:09 -07004046 */
Yan, Zheng7aac4532018-03-13 19:52:20 +08004047static void lock_unlock_sessions(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07004048{
4049 int i;
4050
Sage Weil2f2dc052009-10-06 11:31:09 -07004051 mutex_lock(&mdsc->mutex);
4052 for (i = 0; i < mdsc->max_sessions; i++) {
4053 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
4054 if (!s)
4055 continue;
4056 mutex_unlock(&mdsc->mutex);
4057 mutex_lock(&s->s_mutex);
4058 mutex_unlock(&s->s_mutex);
4059 ceph_put_mds_session(s);
4060 mutex_lock(&mdsc->mutex);
4061 }
4062 mutex_unlock(&mdsc->mutex);
4063}
4064
Yan, Zheng131d7eb2019-07-25 20:16:47 +08004065static void maybe_recover_session(struct ceph_mds_client *mdsc)
4066{
4067 struct ceph_fs_client *fsc = mdsc->fsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07004068
Yan, Zheng131d7eb2019-07-25 20:16:47 +08004069 if (!ceph_test_mount_opt(fsc, CLEANRECOVER))
4070 return;
4071
4072 if (READ_ONCE(fsc->mount_state) != CEPH_MOUNT_MOUNTED)
4073 return;
4074
4075 if (!READ_ONCE(fsc->blacklisted))
4076 return;
4077
4078 if (fsc->last_auto_reconnect &&
4079 time_before(jiffies, fsc->last_auto_reconnect + HZ * 60 * 30))
4080 return;
4081
4082 pr_info("auto reconnect after blacklisted\n");
4083 fsc->last_auto_reconnect = jiffies;
4084 ceph_force_reconnect(fsc->sb);
4085}
Sage Weil2f2dc052009-10-06 11:31:09 -07004086
4087/*
4088 * delayed work -- periodically trim expired leases, renew caps with mds
4089 */
4090static void schedule_delayed(struct ceph_mds_client *mdsc)
4091{
4092 int delay = 5;
4093 unsigned hz = round_jiffies_relative(HZ * delay);
4094 schedule_delayed_work(&mdsc->delayed_work, hz);
4095}
4096
4097static void delayed_work(struct work_struct *work)
4098{
4099 int i;
4100 struct ceph_mds_client *mdsc =
4101 container_of(work, struct ceph_mds_client, delayed_work.work);
4102 int renew_interval;
4103 int renew_caps;
4104
4105 dout("mdsc delayed_work\n");
Yan, Zheng75c96272017-12-14 15:11:09 +08004106
Sage Weil2f2dc052009-10-06 11:31:09 -07004107 mutex_lock(&mdsc->mutex);
4108 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
4109 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
4110 mdsc->last_renew_caps);
4111 if (renew_caps)
4112 mdsc->last_renew_caps = jiffies;
4113
4114 for (i = 0; i < mdsc->max_sessions; i++) {
4115 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
Markus Elfringd37b1d92017-08-20 20:22:02 +02004116 if (!s)
Sage Weil2f2dc052009-10-06 11:31:09 -07004117 continue;
4118 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
4119 dout("resending session close request for mds%d\n",
4120 s->s_mds);
4121 request_close_session(mdsc, s);
4122 ceph_put_mds_session(s);
4123 continue;
4124 }
4125 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
4126 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
4127 s->s_state = CEPH_MDS_SESSION_HUNG;
4128 pr_info("mds%d hung\n", s->s_mds);
4129 }
4130 }
Erqi Chen71a228b2019-08-28 21:22:45 +08004131 if (s->s_state == CEPH_MDS_SESSION_NEW ||
4132 s->s_state == CEPH_MDS_SESSION_RESTARTING ||
4133 s->s_state == CEPH_MDS_SESSION_REJECTED) {
Sage Weil2f2dc052009-10-06 11:31:09 -07004134 /* this mds is failed or recovering, just wait */
4135 ceph_put_mds_session(s);
4136 continue;
4137 }
4138 mutex_unlock(&mdsc->mutex);
4139
4140 mutex_lock(&s->s_mutex);
4141 if (renew_caps)
4142 send_renew_caps(mdsc, s);
4143 else
4144 ceph_con_keepalive(&s->s_con);
Sage Weilaab53dd2010-03-17 16:30:21 -07004145 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
4146 s->s_state == CEPH_MDS_SESSION_HUNG)
Sage Weil3d7ded42010-06-09 16:47:10 -07004147 ceph_send_cap_releases(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07004148 mutex_unlock(&s->s_mutex);
4149 ceph_put_mds_session(s);
4150
4151 mutex_lock(&mdsc->mutex);
4152 }
4153 mutex_unlock(&mdsc->mutex);
4154
Yan, Zheng37c4efc2019-01-31 16:55:51 +08004155 ceph_check_delayed_caps(mdsc);
4156
4157 ceph_queue_cap_reclaim_work(mdsc);
4158
4159 ceph_trim_snapid_map(mdsc);
4160
Yan, Zheng131d7eb2019-07-25 20:16:47 +08004161 maybe_recover_session(mdsc);
4162
Sage Weil2f2dc052009-10-06 11:31:09 -07004163 schedule_delayed(mdsc);
4164}
4165
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004166int ceph_mdsc_init(struct ceph_fs_client *fsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07004167
Sage Weil2f2dc052009-10-06 11:31:09 -07004168{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004169 struct ceph_mds_client *mdsc;
4170
4171 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
4172 if (!mdsc)
4173 return -ENOMEM;
4174 mdsc->fsc = fsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07004175 mutex_init(&mdsc->mutex);
4176 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
Markus Elfringd37b1d92017-08-20 20:22:02 +02004177 if (!mdsc->mdsmap) {
majianpengfb3101b2013-06-25 14:48:19 +08004178 kfree(mdsc);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08004179 return -ENOMEM;
majianpengfb3101b2013-06-25 14:48:19 +08004180 }
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08004181
Chengguang Xu50c55ae2018-03-14 13:47:33 +08004182 fsc->mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07004183 init_completion(&mdsc->safe_umount_waiters);
Sage Weilf3c60c52010-08-11 14:51:23 -07004184 init_waitqueue_head(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07004185 INIT_LIST_HEAD(&mdsc->waiting_for_map);
4186 mdsc->sessions = NULL;
Yan, Zheng86d8f672015-01-09 17:00:42 +08004187 atomic_set(&mdsc->num_sessions, 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07004188 mdsc->max_sessions = 0;
4189 mdsc->stopping = 0;
Luis Henriquesd557c482018-01-12 17:19:29 +00004190 atomic64_set(&mdsc->quotarealms_count, 0);
Luis Henriques0c44a8e2019-03-21 10:20:10 +00004191 mdsc->quotarealms_inodes = RB_ROOT;
4192 mutex_init(&mdsc->quotarealms_inodes_mutex);
Yan, Zhengaffbc192015-05-05 21:22:13 +08004193 mdsc->last_snap_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07004194 init_rwsem(&mdsc->snap_rwsem);
Sage Weila105f002010-02-15 14:37:55 -08004195 mdsc->snap_realms = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07004196 INIT_LIST_HEAD(&mdsc->snap_empty);
Yan, Zheng81c5a142019-01-01 16:28:33 +08004197 mdsc->num_snap_realms = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07004198 spin_lock_init(&mdsc->snap_empty_lock);
4199 mdsc->last_tid = 0;
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08004200 mdsc->oldest_tid = 0;
Sage Weil44ca18f2010-02-15 12:08:46 -08004201 mdsc->request_tree = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07004202 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
4203 mdsc->last_renew_caps = jiffies;
4204 INIT_LIST_HEAD(&mdsc->cap_delay_list);
Jeff Layton3a3430a2019-11-20 12:00:59 -05004205 INIT_LIST_HEAD(&mdsc->cap_wait_list);
Sage Weil2f2dc052009-10-06 11:31:09 -07004206 spin_lock_init(&mdsc->cap_delay_lock);
4207 INIT_LIST_HEAD(&mdsc->snap_flush_list);
4208 spin_lock_init(&mdsc->snap_flush_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08004209 mdsc->last_cap_flush_tid = 1;
Yan, Zhenge4500b52016-07-06 11:12:56 +08004210 INIT_LIST_HEAD(&mdsc->cap_flush_list);
Sage Weil2f2dc052009-10-06 11:31:09 -07004211 INIT_LIST_HEAD(&mdsc->cap_dirty);
Sage Weildb354052011-05-24 11:46:31 -07004212 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
Sage Weil2f2dc052009-10-06 11:31:09 -07004213 mdsc->num_cap_flushing = 0;
4214 spin_lock_init(&mdsc->cap_dirty_lock);
4215 init_waitqueue_head(&mdsc->cap_flushing_wq);
Yan, Zheng37c4efc2019-01-31 16:55:51 +08004216 INIT_WORK(&mdsc->cap_reclaim_work, ceph_cap_reclaim_work);
Yan, Zhengfe330322019-02-01 14:57:15 +08004217 atomic_set(&mdsc->cap_reclaim_pending, 0);
Yan, Zheng37c4efc2019-01-31 16:55:51 +08004218
4219 spin_lock_init(&mdsc->dentry_list_lock);
4220 INIT_LIST_HEAD(&mdsc->dentry_leases);
4221 INIT_LIST_HEAD(&mdsc->dentry_dir_leases);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08004222
Yehuda Sadeh37151662010-06-17 16:16:12 -07004223 ceph_caps_init(mdsc);
Yan, Zhengfe330322019-02-01 14:57:15 +08004224 ceph_adjust_caps_max_min(mdsc, fsc->mount_options);
Yehuda Sadeh37151662010-06-17 16:16:12 -07004225
Yan, Zheng75c96272017-12-14 15:11:09 +08004226 spin_lock_init(&mdsc->snapid_map_lock);
4227 mdsc->snapid_map_tree = RB_ROOT;
4228 INIT_LIST_HEAD(&mdsc->snapid_map_lru);
4229
Yan, Zheng10183a62015-04-27 15:33:28 +08004230 init_rwsem(&mdsc->pool_perm_rwsem);
4231 mdsc->pool_perm_tree = RB_ROOT;
4232
Yan, Zhengdfeb84d2018-07-02 15:55:23 +08004233 strscpy(mdsc->nodename, utsname()->nodename,
4234 sizeof(mdsc->nodename));
Sage Weil5f44f142009-11-18 14:52:18 -08004235 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07004236}
4237
4238/*
4239 * Wait for safe replies on open mds requests. If we time out, drop
4240 * all requests from the tree to avoid dangling dentry refs.
4241 */
4242static void wait_requests(struct ceph_mds_client *mdsc)
4243{
Ilya Dryomova319bf52015-05-15 12:02:17 +03004244 struct ceph_options *opts = mdsc->fsc->client->options;
Sage Weil2f2dc052009-10-06 11:31:09 -07004245 struct ceph_mds_request *req;
Sage Weil2f2dc052009-10-06 11:31:09 -07004246
4247 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08004248 if (__get_oldest_req(mdsc)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07004249 mutex_unlock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08004250
Sage Weil2f2dc052009-10-06 11:31:09 -07004251 dout("wait_requests waiting for requests\n");
4252 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
Ilya Dryomova319bf52015-05-15 12:02:17 +03004253 ceph_timeout_jiffies(opts->mount_timeout));
Sage Weil2f2dc052009-10-06 11:31:09 -07004254
4255 /* tear down remaining requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08004256 mutex_lock(&mdsc->mutex);
4257 while ((req = __get_oldest_req(mdsc))) {
Sage Weil2f2dc052009-10-06 11:31:09 -07004258 dout("wait_requests timed out on tid %llu\n",
4259 req->r_tid);
Yan, Zheng428138c2019-06-14 10:55:05 +08004260 list_del_init(&req->r_wait);
Sage Weil44ca18f2010-02-15 12:08:46 -08004261 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07004262 }
4263 }
4264 mutex_unlock(&mdsc->mutex);
4265 dout("wait_requests done\n");
4266}
4267
4268/*
4269 * called before mount is ro, and before dentries are torn down.
4270 * (hmm, does this still race with new lookups?)
4271 */
4272void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
4273{
4274 dout("pre_umount\n");
4275 mdsc->stopping = 1;
4276
Yan, Zheng7aac4532018-03-13 19:52:20 +08004277 lock_unlock_sessions(mdsc);
Sage Weilafcdaea2009-10-14 14:27:38 -07004278 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07004279 wait_requests(mdsc);
Sage Weil17c688c2010-06-21 16:12:26 -07004280
4281 /*
4282 * wait for reply handlers to drop their request refs and
4283 * their inode/dcache refs
4284 */
4285 ceph_msgr_flush();
Luis Henriques0c44a8e2019-03-21 10:20:10 +00004286
4287 ceph_cleanup_quotarealms_inodes(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07004288}
4289
4290/*
4291 * wait for all write mds requests to flush.
4292 */
4293static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
4294{
Sage Weil80fc7312010-03-16 15:28:54 -07004295 struct ceph_mds_request *req = NULL, *nextreq;
Sage Weil44ca18f2010-02-15 12:08:46 -08004296 struct rb_node *n;
Sage Weil2f2dc052009-10-06 11:31:09 -07004297
4298 mutex_lock(&mdsc->mutex);
4299 dout("wait_unsafe_requests want %lld\n", want_tid);
Sage Weil80fc7312010-03-16 15:28:54 -07004300restart:
Sage Weil44ca18f2010-02-15 12:08:46 -08004301 req = __get_oldest_req(mdsc);
4302 while (req && req->r_tid <= want_tid) {
Sage Weil80fc7312010-03-16 15:28:54 -07004303 /* find next request */
4304 n = rb_next(&req->r_node);
4305 if (n)
4306 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
4307 else
4308 nextreq = NULL;
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08004309 if (req->r_op != CEPH_MDS_OP_SETFILELOCK &&
4310 (req->r_op & CEPH_MDS_OP_WRITE)) {
Sage Weil44ca18f2010-02-15 12:08:46 -08004311 /* write op */
4312 ceph_mdsc_get_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07004313 if (nextreq)
4314 ceph_mdsc_get_request(nextreq);
Sage Weil44ca18f2010-02-15 12:08:46 -08004315 mutex_unlock(&mdsc->mutex);
4316 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
4317 req->r_tid, want_tid);
4318 wait_for_completion(&req->r_safe_completion);
4319 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08004320 ceph_mdsc_put_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07004321 if (!nextreq)
4322 break; /* next dne before, so we're done! */
4323 if (RB_EMPTY_NODE(&nextreq->r_node)) {
4324 /* next request was removed from tree */
4325 ceph_mdsc_put_request(nextreq);
4326 goto restart;
4327 }
4328 ceph_mdsc_put_request(nextreq); /* won't go away */
Sage Weil44ca18f2010-02-15 12:08:46 -08004329 }
Sage Weil80fc7312010-03-16 15:28:54 -07004330 req = nextreq;
Sage Weil2f2dc052009-10-06 11:31:09 -07004331 }
4332 mutex_unlock(&mdsc->mutex);
4333 dout("wait_unsafe_requests done\n");
4334}
4335
4336void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
4337{
Yan, Zheng0e294382016-07-04 18:06:41 +08004338 u64 want_tid, want_flush;
Sage Weil2f2dc052009-10-06 11:31:09 -07004339
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01004340 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
Sage Weil56b7cf92010-05-03 15:22:00 -07004341 return;
4342
Sage Weil2f2dc052009-10-06 11:31:09 -07004343 dout("sync\n");
4344 mutex_lock(&mdsc->mutex);
4345 want_tid = mdsc->last_tid;
Sage Weil2f2dc052009-10-06 11:31:09 -07004346 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07004347
Sage Weilafcdaea2009-10-14 14:27:38 -07004348 ceph_flush_dirty_caps(mdsc);
Yan, Zhengd3383a82015-01-08 21:30:12 +08004349 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zheng8310b082015-06-09 17:20:12 +08004350 want_flush = mdsc->last_cap_flush_tid;
Yan, Zhengc8799fc2016-07-07 15:22:38 +08004351 if (!list_empty(&mdsc->cap_flush_list)) {
4352 struct ceph_cap_flush *cf =
4353 list_last_entry(&mdsc->cap_flush_list,
4354 struct ceph_cap_flush, g_list);
4355 cf->wake = true;
4356 }
Yan, Zhengd3383a82015-01-08 21:30:12 +08004357 spin_unlock(&mdsc->cap_dirty_lock);
4358
Yan, Zheng0e294382016-07-04 18:06:41 +08004359 dout("sync want tid %lld flush_seq %lld\n",
4360 want_tid, want_flush);
Sage Weil2f2dc052009-10-06 11:31:09 -07004361
4362 wait_unsafe_requests(mdsc, want_tid);
Yan, Zheng0e294382016-07-04 18:06:41 +08004363 wait_caps_flush(mdsc, want_flush);
Sage Weil2f2dc052009-10-06 11:31:09 -07004364}
4365
Sage Weilf3c60c52010-08-11 14:51:23 -07004366/*
4367 * true if all sessions are closed, or we force unmount
4368 */
Yan, Zhengfcff4152016-09-14 16:39:51 +08004369static bool done_closing_sessions(struct ceph_mds_client *mdsc, int skipped)
Sage Weilf3c60c52010-08-11 14:51:23 -07004370{
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01004371 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
Sage Weilf3c60c52010-08-11 14:51:23 -07004372 return true;
Yan, Zhengfcff4152016-09-14 16:39:51 +08004373 return atomic_read(&mdsc->num_sessions) <= skipped;
Sage Weilf3c60c52010-08-11 14:51:23 -07004374}
Sage Weil2f2dc052009-10-06 11:31:09 -07004375
4376/*
4377 * called after sb is ro.
4378 */
4379void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
4380{
Ilya Dryomova319bf52015-05-15 12:02:17 +03004381 struct ceph_options *opts = mdsc->fsc->client->options;
Sage Weil2f2dc052009-10-06 11:31:09 -07004382 struct ceph_mds_session *session;
4383 int i;
Yan, Zhengfcff4152016-09-14 16:39:51 +08004384 int skipped = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07004385
4386 dout("close_sessions\n");
4387
Sage Weil2f2dc052009-10-06 11:31:09 -07004388 /* close sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07004389 mutex_lock(&mdsc->mutex);
4390 for (i = 0; i < mdsc->max_sessions; i++) {
4391 session = __ceph_lookup_mds_session(mdsc, i);
4392 if (!session)
4393 continue;
Sage Weil2f2dc052009-10-06 11:31:09 -07004394 mutex_unlock(&mdsc->mutex);
Sage Weilf3c60c52010-08-11 14:51:23 -07004395 mutex_lock(&session->s_mutex);
Yan, Zhengfcff4152016-09-14 16:39:51 +08004396 if (__close_session(mdsc, session) <= 0)
4397 skipped++;
Sage Weilf3c60c52010-08-11 14:51:23 -07004398 mutex_unlock(&session->s_mutex);
4399 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07004400 mutex_lock(&mdsc->mutex);
4401 }
Sage Weilf3c60c52010-08-11 14:51:23 -07004402 mutex_unlock(&mdsc->mutex);
4403
4404 dout("waiting for sessions to close\n");
Yan, Zhengfcff4152016-09-14 16:39:51 +08004405 wait_event_timeout(mdsc->session_close_wq,
4406 done_closing_sessions(mdsc, skipped),
Ilya Dryomova319bf52015-05-15 12:02:17 +03004407 ceph_timeout_jiffies(opts->mount_timeout));
Sage Weil2f2dc052009-10-06 11:31:09 -07004408
4409 /* tear down remaining sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07004410 mutex_lock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07004411 for (i = 0; i < mdsc->max_sessions; i++) {
4412 if (mdsc->sessions[i]) {
Xiubo Li5b3248c2019-12-19 19:44:09 -05004413 session = ceph_get_mds_session(mdsc->sessions[i]);
Sage Weil2600d2d2010-02-22 15:12:16 -08004414 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07004415 mutex_unlock(&mdsc->mutex);
4416 mutex_lock(&session->s_mutex);
4417 remove_session_caps(session);
4418 mutex_unlock(&session->s_mutex);
4419 ceph_put_mds_session(session);
4420 mutex_lock(&mdsc->mutex);
4421 }
4422 }
Sage Weil2f2dc052009-10-06 11:31:09 -07004423 WARN_ON(!list_empty(&mdsc->cap_delay_list));
Sage Weil2f2dc052009-10-06 11:31:09 -07004424 mutex_unlock(&mdsc->mutex);
4425
Yan, Zheng75c96272017-12-14 15:11:09 +08004426 ceph_cleanup_snapid_map(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07004427 ceph_cleanup_empty_realms(mdsc);
4428
Yan, Zheng37c4efc2019-01-31 16:55:51 +08004429 cancel_work_sync(&mdsc->cap_reclaim_work);
Sage Weil2f2dc052009-10-06 11:31:09 -07004430 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
4431
4432 dout("stopped\n");
4433}
4434
Yan, Zheng48fec5d2015-07-01 16:27:46 +08004435void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc)
4436{
4437 struct ceph_mds_session *session;
4438 int mds;
4439
4440 dout("force umount\n");
4441
4442 mutex_lock(&mdsc->mutex);
4443 for (mds = 0; mds < mdsc->max_sessions; mds++) {
4444 session = __ceph_lookup_mds_session(mdsc, mds);
4445 if (!session)
4446 continue;
Yan, Zhengd468e722019-07-25 20:16:44 +08004447
4448 if (session->s_state == CEPH_MDS_SESSION_REJECTED)
4449 __unregister_session(mdsc, session);
4450 __wake_requests(mdsc, &session->s_waiting);
Yan, Zheng48fec5d2015-07-01 16:27:46 +08004451 mutex_unlock(&mdsc->mutex);
Yan, Zhengd468e722019-07-25 20:16:44 +08004452
Yan, Zheng48fec5d2015-07-01 16:27:46 +08004453 mutex_lock(&session->s_mutex);
4454 __close_session(mdsc, session);
4455 if (session->s_state == CEPH_MDS_SESSION_CLOSING) {
4456 cleanup_session_requests(mdsc, session);
4457 remove_session_caps(session);
4458 }
4459 mutex_unlock(&session->s_mutex);
4460 ceph_put_mds_session(session);
Yan, Zhengd468e722019-07-25 20:16:44 +08004461
Yan, Zheng48fec5d2015-07-01 16:27:46 +08004462 mutex_lock(&mdsc->mutex);
4463 kick_requests(mdsc, mds);
4464 }
4465 __wake_requests(mdsc, &mdsc->waiting_for_map);
4466 mutex_unlock(&mdsc->mutex);
4467}
4468
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004469static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07004470{
4471 dout("stop\n");
4472 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
4473 if (mdsc->mdsmap)
4474 ceph_mdsmap_destroy(mdsc->mdsmap);
4475 kfree(mdsc->sessions);
Yehuda Sadeh37151662010-06-17 16:16:12 -07004476 ceph_caps_finalize(mdsc);
Yan, Zheng10183a62015-04-27 15:33:28 +08004477 ceph_pool_perm_destroy(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07004478}
4479
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004480void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
4481{
4482 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weilef550f62011-03-25 13:27:48 -07004483 dout("mdsc_destroy %p\n", mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07004484
Chengguang Xu50c55ae2018-03-14 13:47:33 +08004485 if (!mdsc)
4486 return;
4487
Sage Weilef550f62011-03-25 13:27:48 -07004488 /* flush out any connection work with references to us */
4489 ceph_msgr_flush();
4490
Yan, Zheng62a65f32017-06-22 16:26:34 +08004491 ceph_mdsc_stop(mdsc);
4492
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004493 fsc->mdsc = NULL;
4494 kfree(mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07004495 dout("mdsc_destroy %p done\n", mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004496}
4497
Yan, Zheng430afba2016-07-08 11:25:38 +08004498void ceph_mdsc_handle_fsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
4499{
4500 struct ceph_fs_client *fsc = mdsc->fsc;
4501 const char *mds_namespace = fsc->mount_options->mds_namespace;
4502 void *p = msg->front.iov_base;
4503 void *end = p + msg->front.iov_len;
4504 u32 epoch;
4505 u32 map_len;
4506 u32 num_fs;
4507 u32 mount_fscid = (u32)-1;
4508 u8 struct_v, struct_cv;
4509 int err = -EINVAL;
4510
4511 ceph_decode_need(&p, end, sizeof(u32), bad);
4512 epoch = ceph_decode_32(&p);
4513
4514 dout("handle_fsmap epoch %u\n", epoch);
4515
4516 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
4517 struct_v = ceph_decode_8(&p);
4518 struct_cv = ceph_decode_8(&p);
4519 map_len = ceph_decode_32(&p);
4520
4521 ceph_decode_need(&p, end, sizeof(u32) * 3, bad);
4522 p += sizeof(u32) * 2; /* skip epoch and legacy_client_fscid */
4523
4524 num_fs = ceph_decode_32(&p);
4525 while (num_fs-- > 0) {
4526 void *info_p, *info_end;
4527 u32 info_len;
4528 u8 info_v, info_cv;
4529 u32 fscid, namelen;
4530
4531 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
4532 info_v = ceph_decode_8(&p);
4533 info_cv = ceph_decode_8(&p);
4534 info_len = ceph_decode_32(&p);
4535 ceph_decode_need(&p, end, info_len, bad);
4536 info_p = p;
4537 info_end = p + info_len;
4538 p = info_end;
4539
4540 ceph_decode_need(&info_p, info_end, sizeof(u32) * 2, bad);
4541 fscid = ceph_decode_32(&info_p);
4542 namelen = ceph_decode_32(&info_p);
4543 ceph_decode_need(&info_p, info_end, namelen, bad);
4544
4545 if (mds_namespace &&
4546 strlen(mds_namespace) == namelen &&
4547 !strncmp(mds_namespace, (char *)info_p, namelen)) {
4548 mount_fscid = fscid;
4549 break;
4550 }
4551 }
4552
4553 ceph_monc_got_map(&fsc->client->monc, CEPH_SUB_FSMAP, epoch);
4554 if (mount_fscid != (u32)-1) {
4555 fsc->client->monc.fs_cluster_id = mount_fscid;
4556 ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP,
4557 0, true);
4558 ceph_monc_renew_subs(&fsc->client->monc);
4559 } else {
4560 err = -ENOENT;
4561 goto err_out;
4562 }
4563 return;
Ilya Dryomov76bd6ec2017-10-16 10:32:50 +02004564
Yan, Zheng430afba2016-07-08 11:25:38 +08004565bad:
4566 pr_err("error decoding fsmap\n");
4567err_out:
4568 mutex_lock(&mdsc->mutex);
Ilya Dryomov76bd6ec2017-10-16 10:32:50 +02004569 mdsc->mdsmap_err = err;
Yan, Zheng430afba2016-07-08 11:25:38 +08004570 __wake_requests(mdsc, &mdsc->waiting_for_map);
4571 mutex_unlock(&mdsc->mutex);
Yan, Zheng430afba2016-07-08 11:25:38 +08004572}
Sage Weil2f2dc052009-10-06 11:31:09 -07004573
4574/*
4575 * handle mds map update.
4576 */
Yan, Zheng430afba2016-07-08 11:25:38 +08004577void ceph_mdsc_handle_mdsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07004578{
4579 u32 epoch;
4580 u32 maplen;
4581 void *p = msg->front.iov_base;
4582 void *end = p + msg->front.iov_len;
4583 struct ceph_mdsmap *newmap, *oldmap;
4584 struct ceph_fsid fsid;
4585 int err = -EINVAL;
4586
4587 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
4588 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004589 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
Sage Weil07433042009-11-18 16:50:41 -08004590 return;
Sage Weilc89136e2009-10-14 09:59:09 -07004591 epoch = ceph_decode_32(&p);
4592 maplen = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07004593 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
4594
4595 /* do we need it? */
Sage Weil2f2dc052009-10-06 11:31:09 -07004596 mutex_lock(&mdsc->mutex);
4597 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
4598 dout("handle_map epoch %u <= our %u\n",
4599 epoch, mdsc->mdsmap->m_epoch);
4600 mutex_unlock(&mdsc->mutex);
4601 return;
4602 }
4603
4604 newmap = ceph_mdsmap_decode(&p, end);
4605 if (IS_ERR(newmap)) {
4606 err = PTR_ERR(newmap);
4607 goto bad_unlock;
4608 }
4609
4610 /* swap into place */
4611 if (mdsc->mdsmap) {
4612 oldmap = mdsc->mdsmap;
4613 mdsc->mdsmap = newmap;
4614 check_new_map(mdsc, newmap, oldmap);
4615 ceph_mdsmap_destroy(oldmap);
4616 } else {
4617 mdsc->mdsmap = newmap; /* first mds map */
4618 }
Chengguang Xu719784b2018-07-19 22:15:24 +08004619 mdsc->fsc->max_file_size = min((loff_t)mdsc->mdsmap->m_max_file_size,
4620 MAX_LFS_FILESIZE);
Sage Weil2f2dc052009-10-06 11:31:09 -07004621
4622 __wake_requests(mdsc, &mdsc->waiting_for_map);
Ilya Dryomov82dcaba2016-01-19 16:19:06 +01004623 ceph_monc_got_map(&mdsc->fsc->client->monc, CEPH_SUB_MDSMAP,
4624 mdsc->mdsmap->m_epoch);
Sage Weil2f2dc052009-10-06 11:31:09 -07004625
4626 mutex_unlock(&mdsc->mutex);
4627 schedule_delayed(mdsc);
4628 return;
4629
4630bad_unlock:
4631 mutex_unlock(&mdsc->mutex);
4632bad:
4633 pr_err("error decoding mdsmap %d\n", err);
4634 return;
4635}
4636
4637static struct ceph_connection *con_get(struct ceph_connection *con)
4638{
4639 struct ceph_mds_session *s = con->private;
4640
Xiubo Li5b3248c2019-12-19 19:44:09 -05004641 if (ceph_get_mds_session(s))
Sage Weil2f2dc052009-10-06 11:31:09 -07004642 return con;
Sage Weil2f2dc052009-10-06 11:31:09 -07004643 return NULL;
4644}
4645
4646static void con_put(struct ceph_connection *con)
4647{
4648 struct ceph_mds_session *s = con->private;
4649
Sage Weil2f2dc052009-10-06 11:31:09 -07004650 ceph_put_mds_session(s);
4651}
4652
4653/*
4654 * if the client is unresponsive for long enough, the mds will kill
4655 * the session entirely.
4656 */
4657static void peer_reset(struct ceph_connection *con)
4658{
4659 struct ceph_mds_session *s = con->private;
Sage Weil7e70f0e2010-03-18 13:59:12 -07004660 struct ceph_mds_client *mdsc = s->s_mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07004661
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07004662 pr_warn("mds%d closed our session\n", s->s_mds);
Sage Weil7e70f0e2010-03-18 13:59:12 -07004663 send_mds_reconnect(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07004664}
4665
4666static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
4667{
4668 struct ceph_mds_session *s = con->private;
4669 struct ceph_mds_client *mdsc = s->s_mdsc;
4670 int type = le16_to_cpu(msg->hdr.type);
4671
Sage Weil2600d2d2010-02-22 15:12:16 -08004672 mutex_lock(&mdsc->mutex);
4673 if (__verify_registered_session(mdsc, s) < 0) {
4674 mutex_unlock(&mdsc->mutex);
4675 goto out;
4676 }
4677 mutex_unlock(&mdsc->mutex);
4678
Sage Weil2f2dc052009-10-06 11:31:09 -07004679 switch (type) {
4680 case CEPH_MSG_MDS_MAP:
Yan, Zheng430afba2016-07-08 11:25:38 +08004681 ceph_mdsc_handle_mdsmap(mdsc, msg);
4682 break;
4683 case CEPH_MSG_FS_MAP_USER:
4684 ceph_mdsc_handle_fsmap(mdsc, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004685 break;
4686 case CEPH_MSG_CLIENT_SESSION:
4687 handle_session(s, msg);
4688 break;
4689 case CEPH_MSG_CLIENT_REPLY:
4690 handle_reply(s, msg);
4691 break;
4692 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
Sage Weil2600d2d2010-02-22 15:12:16 -08004693 handle_forward(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004694 break;
4695 case CEPH_MSG_CLIENT_CAPS:
4696 ceph_handle_caps(s, msg);
4697 break;
4698 case CEPH_MSG_CLIENT_SNAP:
Sage Weil2600d2d2010-02-22 15:12:16 -08004699 ceph_handle_snap(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004700 break;
4701 case CEPH_MSG_CLIENT_LEASE:
Sage Weil2600d2d2010-02-22 15:12:16 -08004702 handle_lease(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004703 break;
Luis Henriquesfb18a572018-01-05 10:47:18 +00004704 case CEPH_MSG_CLIENT_QUOTA:
4705 ceph_handle_quota(mdsc, s, msg);
4706 break;
Sage Weil2f2dc052009-10-06 11:31:09 -07004707
4708 default:
4709 pr_err("received unknown message type %d %s\n", type,
4710 ceph_msg_type_name(type));
4711 }
Sage Weil2600d2d2010-02-22 15:12:16 -08004712out:
Sage Weil2f2dc052009-10-06 11:31:09 -07004713 ceph_msg_put(msg);
4714}
4715
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004716/*
4717 * authentication
4718 */
Alex Eldera3530df2012-05-16 15:16:39 -05004719
4720/*
4721 * Note: returned pointer is the address of a structure that's
4722 * managed separately. Caller must *not* attempt to free it.
4723 */
4724static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05004725 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004726{
4727 struct ceph_mds_session *s = con->private;
4728 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004729 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05004730 struct ceph_auth_handshake *auth = &s->s_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004731
Alex Elder74f18692012-05-16 15:16:39 -05004732 if (force_new && auth->authorizer) {
Ilya Dryomov6c1ea262016-04-11 19:34:49 +02004733 ceph_auth_destroy_authorizer(auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05004734 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004735 }
Sage Weil27859f92013-03-25 10:26:14 -07004736 if (!auth->authorizer) {
4737 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4738 auth);
Sage Weil0bed9b52013-03-25 10:26:01 -07004739 if (ret)
4740 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07004741 } else {
4742 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4743 auth);
Alex Eldera2556512012-05-16 15:16:39 -05004744 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05004745 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004746 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004747 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05004748
Alex Eldera3530df2012-05-16 15:16:39 -05004749 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004750}
4751
Ilya Dryomov6daca132018-07-27 19:18:34 +02004752static int add_authorizer_challenge(struct ceph_connection *con,
4753 void *challenge_buf, int challenge_buf_len)
4754{
4755 struct ceph_mds_session *s = con->private;
4756 struct ceph_mds_client *mdsc = s->s_mdsc;
4757 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
4758
4759 return ceph_auth_add_authorizer_challenge(ac, s->s_auth.authorizer,
4760 challenge_buf, challenge_buf_len);
4761}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004762
Ilya Dryomov0dde5842016-12-02 16:35:09 +01004763static int verify_authorizer_reply(struct ceph_connection *con)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004764{
4765 struct ceph_mds_session *s = con->private;
4766 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004767 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004768
Ilya Dryomov0dde5842016-12-02 16:35:09 +01004769 return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004770}
4771
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004772static int invalidate_authorizer(struct ceph_connection *con)
4773{
4774 struct ceph_mds_session *s = con->private;
4775 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004776 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004777
Sage Weil27859f92013-03-25 10:26:14 -07004778 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004779
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004780 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004781}
4782
Alex Elder53ded492013-03-01 18:00:14 -06004783static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
4784 struct ceph_msg_header *hdr, int *skip)
4785{
4786 struct ceph_msg *msg;
4787 int type = (int) le16_to_cpu(hdr->type);
4788 int front_len = (int) le32_to_cpu(hdr->front_len);
4789
4790 if (con->in_msg)
4791 return con->in_msg;
4792
4793 *skip = 0;
4794 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
4795 if (!msg) {
4796 pr_err("unable to allocate msg type %d len %d\n",
4797 type, front_len);
4798 return NULL;
4799 }
Alex Elder53ded492013-03-01 18:00:14 -06004800
4801 return msg;
4802}
4803
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004804static int mds_sign_message(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08004805{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004806 struct ceph_mds_session *s = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08004807 struct ceph_auth_handshake *auth = &s->s_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004808
Yan, Zheng33d07332014-11-04 16:33:37 +08004809 return ceph_auth_sign_message(auth, msg);
4810}
4811
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004812static int mds_check_message_signature(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08004813{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004814 struct ceph_mds_session *s = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08004815 struct ceph_auth_handshake *auth = &s->s_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004816
Yan, Zheng33d07332014-11-04 16:33:37 +08004817 return ceph_auth_check_message_signature(auth, msg);
4818}
4819
Tobias Klauser9e327892010-05-20 10:40:19 +02004820static const struct ceph_connection_operations mds_con_ops = {
Sage Weil2f2dc052009-10-06 11:31:09 -07004821 .get = con_get,
4822 .put = con_put,
4823 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004824 .get_authorizer = get_authorizer,
Ilya Dryomov6daca132018-07-27 19:18:34 +02004825 .add_authorizer_challenge = add_authorizer_challenge,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004826 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004827 .invalidate_authorizer = invalidate_authorizer,
Sage Weil2f2dc052009-10-06 11:31:09 -07004828 .peer_reset = peer_reset,
Alex Elder53ded492013-03-01 18:00:14 -06004829 .alloc_msg = mds_alloc_msg,
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004830 .sign_message = mds_sign_message,
4831 .check_message_signature = mds_check_message_signature,
Sage Weil2f2dc052009-10-06 11:31:09 -07004832};
4833
Sage Weil2f2dc052009-10-06 11:31:09 -07004834/* eof */