blob: 1b53aceb54bd91a467c06baeec6ad26534f7d9d7 [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>
Sage Weil2f2dc052009-10-06 11:31:09 -070012
Sage Weil2f2dc052009-10-06 11:31:09 -070013#include "super.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070014#include "mds_client.h"
15
Sage Weil1fe60e52012-07-30 16:23:22 -070016#include <linux/ceph/ceph_features.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070017#include <linux/ceph/messenger.h>
18#include <linux/ceph/decode.h>
19#include <linux/ceph/pagelist.h>
20#include <linux/ceph/auth.h>
21#include <linux/ceph/debugfs.h>
Sage Weil2f2dc052009-10-06 11:31:09 -070022
Yan, Zheng81c5a142019-01-01 16:28:33 +080023#define RECONNECT_MAX_SIZE (INT_MAX - PAGE_SIZE)
24
Sage Weil2f2dc052009-10-06 11:31:09 -070025/*
26 * A cluster of MDS (metadata server) daemons is responsible for
27 * managing the file system namespace (the directory hierarchy and
28 * inodes) and for coordinating shared access to storage. Metadata is
29 * partitioning hierarchically across a number of servers, and that
30 * partition varies over time as the cluster adjusts the distribution
31 * in order to balance load.
32 *
33 * The MDS client is primarily responsible to managing synchronous
34 * metadata requests for operations like open, unlink, and so forth.
35 * If there is a MDS failure, we find out about it when we (possibly
36 * request and) receive a new MDS map, and can resubmit affected
37 * requests.
38 *
39 * For the most part, though, we take advantage of a lossless
40 * communications channel to the MDS, and do not need to worry about
41 * timing out or resubmitting requests.
42 *
43 * We maintain a stateful "session" with each MDS we interact with.
44 * Within each session, we sent periodic heartbeat messages to ensure
45 * any capabilities or leases we have been issues remain valid. If
46 * the session times out and goes stale, our leases and capabilities
47 * are no longer valid.
48 */
49
Sage Weil20cb34a2010-05-12 15:21:32 -070050struct ceph_reconnect_state {
Yan, Zheng81c5a142019-01-01 16:28:33 +080051 struct ceph_mds_session *session;
52 int nr_caps, nr_realms;
Sage Weil20cb34a2010-05-12 15:21:32 -070053 struct ceph_pagelist *pagelist;
Yan, Zheng121f22a2016-07-04 22:05:18 +080054 unsigned msg_version;
Yan, Zheng81c5a142019-01-01 16:28:33 +080055 bool allow_multi;
Sage Weil20cb34a2010-05-12 15:21:32 -070056};
57
Sage Weil2f2dc052009-10-06 11:31:09 -070058static void __wake_requests(struct ceph_mds_client *mdsc,
59 struct list_head *head);
Yan, Zhenge3ec8d62019-01-14 17:21:19 +080060static void ceph_cap_release_work(struct work_struct *work);
Yan, Zheng37c4efc2019-01-31 16:55:51 +080061static void ceph_cap_reclaim_work(struct work_struct *work);
Sage Weil2f2dc052009-10-06 11:31:09 -070062
Tobias Klauser9e327892010-05-20 10:40:19 +020063static const struct ceph_connection_operations mds_con_ops;
Sage Weil2f2dc052009-10-06 11:31:09 -070064
65
66/*
67 * mds reply parsing
68 */
69
Yan, Zhengb37fe1f2019-01-09 10:10:17 +080070static int parse_reply_info_quota(void **p, void *end,
71 struct ceph_mds_reply_info_in *info)
72{
73 u8 struct_v, struct_compat;
74 u32 struct_len;
75
76 ceph_decode_8_safe(p, end, struct_v, bad);
77 ceph_decode_8_safe(p, end, struct_compat, bad);
78 /* struct_v is expected to be >= 1. we only
79 * understand encoding with struct_compat == 1. */
80 if (!struct_v || struct_compat != 1)
81 goto bad;
82 ceph_decode_32_safe(p, end, struct_len, bad);
83 ceph_decode_need(p, end, struct_len, bad);
84 end = *p + struct_len;
85 ceph_decode_64_safe(p, end, info->max_bytes, bad);
86 ceph_decode_64_safe(p, end, info->max_files, bad);
87 *p = end;
88 return 0;
89bad:
90 return -EIO;
91}
92
Sage Weil2f2dc052009-10-06 11:31:09 -070093/*
94 * parse individual inode info
95 */
96static int parse_reply_info_in(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -080097 struct ceph_mds_reply_info_in *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +020098 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -070099{
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800100 int err = 0;
101 u8 struct_v = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -0700102
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800103 if (features == (u64)-1) {
104 u32 struct_len;
105 u8 struct_compat;
106 ceph_decode_8_safe(p, end, struct_v, bad);
107 ceph_decode_8_safe(p, end, struct_compat, bad);
108 /* struct_v is expected to be >= 1. we only understand
109 * encoding with struct_compat == 1. */
110 if (!struct_v || struct_compat != 1)
111 goto bad;
112 ceph_decode_32_safe(p, end, struct_len, bad);
113 ceph_decode_need(p, end, struct_len, bad);
114 end = *p + struct_len;
115 }
116
117 ceph_decode_need(p, end, sizeof(struct ceph_mds_reply_inode), bad);
Sage Weil2f2dc052009-10-06 11:31:09 -0700118 info->in = *p;
119 *p += sizeof(struct ceph_mds_reply_inode) +
120 sizeof(*info->in->fragtree.splits) *
121 le32_to_cpu(info->in->fragtree.nsplits);
122
123 ceph_decode_32_safe(p, end, info->symlink_len, bad);
124 ceph_decode_need(p, end, info->symlink_len, bad);
125 info->symlink = *p;
126 *p += info->symlink_len;
127
Ilya Dryomov23c625c2018-11-08 14:55:21 +0100128 ceph_decode_copy_safe(p, end, &info->dir_layout,
129 sizeof(info->dir_layout), bad);
Sage Weil2f2dc052009-10-06 11:31:09 -0700130 ceph_decode_32_safe(p, end, info->xattr_len, bad);
131 ceph_decode_need(p, end, info->xattr_len, bad);
132 info->xattr_data = *p;
133 *p += info->xattr_len;
Yan, Zhengfb01d1f2014-11-14 21:29:55 +0800134
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800135 if (features == (u64)-1) {
136 /* inline data */
Yan, Zhengfb01d1f2014-11-14 21:29:55 +0800137 ceph_decode_64_safe(p, end, info->inline_version, bad);
138 ceph_decode_32_safe(p, end, info->inline_len, bad);
139 ceph_decode_need(p, end, info->inline_len, bad);
140 info->inline_data = *p;
141 *p += info->inline_len;
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800142 /* quota */
143 err = parse_reply_info_quota(p, end, info);
144 if (err < 0)
145 goto out_bad;
146 /* pool namespace */
Yan, Zheng5ea5c5e2016-02-14 18:06:41 +0800147 ceph_decode_32_safe(p, end, info->pool_ns_len, bad);
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800148 if (info->pool_ns_len > 0) {
149 ceph_decode_need(p, end, info->pool_ns_len, bad);
150 info->pool_ns_data = *p;
151 *p += info->pool_ns_len;
152 }
Jeff Layton245ce992019-05-29 11:19:42 -0400153
154 /* btime */
155 ceph_decode_need(p, end, sizeof(info->btime), bad);
156 ceph_decode_copy(p, &info->btime, sizeof(info->btime));
157
158 /* change attribute */
Jeff Laytona35ead32019-06-06 07:29:23 -0400159 ceph_decode_64_safe(p, end, info->change_attr, bad);
Yan, Zheng5ea5c5e2016-02-14 18:06:41 +0800160
Yan, Zheng08796872019-01-09 11:07:02 +0800161 /* dir pin */
162 if (struct_v >= 2) {
163 ceph_decode_32_safe(p, end, info->dir_pin, bad);
164 } else {
165 info->dir_pin = -ENODATA;
166 }
167
David Disseldorp193e7b32019-04-18 14:15:46 +0200168 /* snapshot birth time, remains zero for v<=2 */
169 if (struct_v >= 3) {
170 ceph_decode_need(p, end, sizeof(info->snap_btime), bad);
171 ceph_decode_copy(p, &info->snap_btime,
172 sizeof(info->snap_btime));
173 } else {
174 memset(&info->snap_btime, 0, sizeof(info->snap_btime));
175 }
176
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800177 *p = end;
178 } else {
179 if (features & CEPH_FEATURE_MDS_INLINE_DATA) {
180 ceph_decode_64_safe(p, end, info->inline_version, bad);
181 ceph_decode_32_safe(p, end, info->inline_len, bad);
182 ceph_decode_need(p, end, info->inline_len, bad);
183 info->inline_data = *p;
184 *p += info->inline_len;
185 } else
186 info->inline_version = CEPH_INLINE_NONE;
187
188 if (features & CEPH_FEATURE_MDS_QUOTA) {
189 err = parse_reply_info_quota(p, end, info);
190 if (err < 0)
191 goto out_bad;
192 } else {
193 info->max_bytes = 0;
194 info->max_files = 0;
195 }
196
197 info->pool_ns_len = 0;
198 info->pool_ns_data = NULL;
199 if (features & CEPH_FEATURE_FS_FILE_LAYOUT_V2) {
200 ceph_decode_32_safe(p, end, info->pool_ns_len, bad);
201 if (info->pool_ns_len > 0) {
202 ceph_decode_need(p, end, info->pool_ns_len, bad);
203 info->pool_ns_data = *p;
204 *p += info->pool_ns_len;
205 }
206 }
Yan, Zheng08796872019-01-09 11:07:02 +0800207
Jeff Layton245ce992019-05-29 11:19:42 -0400208 if (features & CEPH_FEATURE_FS_BTIME) {
209 ceph_decode_need(p, end, sizeof(info->btime), bad);
210 ceph_decode_copy(p, &info->btime, sizeof(info->btime));
Jeff Laytona35ead32019-06-06 07:29:23 -0400211 ceph_decode_64_safe(p, end, info->change_attr, bad);
Jeff Layton245ce992019-05-29 11:19:42 -0400212 }
213
Yan, Zheng08796872019-01-09 11:07:02 +0800214 info->dir_pin = -ENODATA;
David Disseldorp193e7b32019-04-18 14:15:46 +0200215 /* info->snap_btime remains zero */
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800216 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700217 return 0;
218bad:
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800219 err = -EIO;
220out_bad:
Sage Weil2f2dc052009-10-06 11:31:09 -0700221 return err;
222}
223
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800224static int parse_reply_info_dir(void **p, void *end,
225 struct ceph_mds_reply_dirfrag **dirfrag,
226 u64 features)
227{
228 if (features == (u64)-1) {
229 u8 struct_v, struct_compat;
230 u32 struct_len;
231 ceph_decode_8_safe(p, end, struct_v, bad);
232 ceph_decode_8_safe(p, end, struct_compat, bad);
233 /* struct_v is expected to be >= 1. we only understand
234 * encoding whose struct_compat == 1. */
235 if (!struct_v || struct_compat != 1)
236 goto bad;
237 ceph_decode_32_safe(p, end, struct_len, bad);
238 ceph_decode_need(p, end, struct_len, bad);
239 end = *p + struct_len;
240 }
241
242 ceph_decode_need(p, end, sizeof(**dirfrag), bad);
243 *dirfrag = *p;
244 *p += sizeof(**dirfrag) + sizeof(u32) * le32_to_cpu((*dirfrag)->ndist);
245 if (unlikely(*p > end))
246 goto bad;
247 if (features == (u64)-1)
248 *p = end;
249 return 0;
250bad:
251 return -EIO;
252}
253
254static int parse_reply_info_lease(void **p, void *end,
255 struct ceph_mds_reply_lease **lease,
256 u64 features)
257{
258 if (features == (u64)-1) {
259 u8 struct_v, struct_compat;
260 u32 struct_len;
261 ceph_decode_8_safe(p, end, struct_v, bad);
262 ceph_decode_8_safe(p, end, struct_compat, bad);
263 /* struct_v is expected to be >= 1. we only understand
264 * encoding whose struct_compat == 1. */
265 if (!struct_v || struct_compat != 1)
266 goto bad;
267 ceph_decode_32_safe(p, end, struct_len, bad);
268 ceph_decode_need(p, end, struct_len, bad);
269 end = *p + struct_len;
270 }
271
272 ceph_decode_need(p, end, sizeof(**lease), bad);
273 *lease = *p;
274 *p += sizeof(**lease);
275 if (features == (u64)-1)
276 *p = end;
277 return 0;
278bad:
279 return -EIO;
280}
281
Sage Weil2f2dc052009-10-06 11:31:09 -0700282/*
283 * parse a normal reply, which may contain a (dir+)dentry and/or a
284 * target inode.
285 */
286static int parse_reply_info_trace(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800287 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200288 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700289{
290 int err;
291
292 if (info->head->is_dentry) {
Sage Weil14303d22010-12-14 17:37:52 -0800293 err = parse_reply_info_in(p, end, &info->diri, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700294 if (err < 0)
295 goto out_bad;
296
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800297 err = parse_reply_info_dir(p, end, &info->dirfrag, features);
298 if (err < 0)
299 goto out_bad;
Sage Weil2f2dc052009-10-06 11:31:09 -0700300
301 ceph_decode_32_safe(p, end, info->dname_len, bad);
302 ceph_decode_need(p, end, info->dname_len, bad);
303 info->dname = *p;
304 *p += info->dname_len;
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800305
306 err = parse_reply_info_lease(p, end, &info->dlease, features);
307 if (err < 0)
308 goto out_bad;
Sage Weil2f2dc052009-10-06 11:31:09 -0700309 }
310
311 if (info->head->is_target) {
Sage Weil14303d22010-12-14 17:37:52 -0800312 err = parse_reply_info_in(p, end, &info->targeti, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700313 if (err < 0)
314 goto out_bad;
315 }
316
317 if (unlikely(*p != end))
318 goto bad;
319 return 0;
320
321bad:
322 err = -EIO;
323out_bad:
324 pr_err("problem parsing mds trace %d\n", err);
325 return err;
326}
327
328/*
329 * parse readdir results
330 */
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800331static int parse_reply_info_readdir(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800332 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200333 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700334{
335 u32 num, i = 0;
336 int err;
337
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800338 err = parse_reply_info_dir(p, end, &info->dir_dir, features);
339 if (err < 0)
340 goto out_bad;
Sage Weil2f2dc052009-10-06 11:31:09 -0700341
342 ceph_decode_need(p, end, sizeof(num) + 2, bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700343 num = ceph_decode_32(p);
Yan, Zheng956d39d2016-04-27 17:48:30 +0800344 {
345 u16 flags = ceph_decode_16(p);
346 info->dir_end = !!(flags & CEPH_READDIR_FRAG_END);
347 info->dir_complete = !!(flags & CEPH_READDIR_FRAG_COMPLETE);
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800348 info->hash_order = !!(flags & CEPH_READDIR_HASH_ORDER);
Yan, Zheng79162542017-04-05 12:54:05 -0400349 info->offset_hash = !!(flags & CEPH_READDIR_OFFSET_HASH);
Yan, Zheng956d39d2016-04-27 17:48:30 +0800350 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700351 if (num == 0)
352 goto done;
353
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800354 BUG_ON(!info->dir_entries);
355 if ((unsigned long)(info->dir_entries + num) >
356 (unsigned long)info->dir_entries + info->dir_buf_size) {
Yan, Zheng54008392014-03-29 13:41:15 +0800357 pr_err("dir contents are larger than expected\n");
358 WARN_ON(1);
359 goto bad;
360 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700361
Yan, Zheng54008392014-03-29 13:41:15 +0800362 info->dir_nr = num;
Sage Weil2f2dc052009-10-06 11:31:09 -0700363 while (num) {
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800364 struct ceph_mds_reply_dir_entry *rde = info->dir_entries + i;
Sage Weil2f2dc052009-10-06 11:31:09 -0700365 /* dentry */
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800366 ceph_decode_32_safe(p, end, rde->name_len, bad);
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800367 ceph_decode_need(p, end, rde->name_len, bad);
368 rde->name = *p;
369 *p += rde->name_len;
370 dout("parsed dir dname '%.*s'\n", rde->name_len, rde->name);
Sage Weil2f2dc052009-10-06 11:31:09 -0700371
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800372 /* dentry lease */
373 err = parse_reply_info_lease(p, end, &rde->lease, features);
374 if (err)
375 goto out_bad;
Sage Weil2f2dc052009-10-06 11:31:09 -0700376 /* inode */
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800377 err = parse_reply_info_in(p, end, &rde->inode, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700378 if (err < 0)
379 goto out_bad;
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800380 /* ceph_readdir_prepopulate() will update it */
381 rde->offset = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -0700382 i++;
383 num--;
384 }
385
386done:
Jeff Layton1d3f8722019-09-26 16:05:11 -0400387 /* Skip over any unrecognized fields */
388 *p = end;
Sage Weil2f2dc052009-10-06 11:31:09 -0700389 return 0;
390
391bad:
392 err = -EIO;
393out_bad:
394 pr_err("problem parsing dir contents %d\n", err);
395 return err;
396}
397
398/*
Herb Shiu25933ab2010-12-01 14:14:38 -0800399 * parse fcntl F_GETLK results
400 */
401static int parse_reply_info_filelock(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800402 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200403 u64 features)
Herb Shiu25933ab2010-12-01 14:14:38 -0800404{
405 if (*p + sizeof(*info->filelock_reply) > end)
406 goto bad;
407
408 info->filelock_reply = *p;
Herb Shiu25933ab2010-12-01 14:14:38 -0800409
Jeff Layton1d3f8722019-09-26 16:05:11 -0400410 /* Skip over any unrecognized fields */
411 *p = end;
Herb Shiu25933ab2010-12-01 14:14:38 -0800412 return 0;
Herb Shiu25933ab2010-12-01 14:14:38 -0800413bad:
414 return -EIO;
415}
416
417/*
Sam Lang6e8575f2012-12-28 09:56:46 -0800418 * parse create results
419 */
420static int parse_reply_info_create(void **p, void *end,
421 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200422 u64 features)
Sam Lang6e8575f2012-12-28 09:56:46 -0800423{
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800424 if (features == (u64)-1 ||
425 (features & CEPH_FEATURE_REPLY_CREATE_INODE)) {
Jeff Layton1d3f8722019-09-26 16:05:11 -0400426 /* Malformed reply? */
Sam Lang6e8575f2012-12-28 09:56:46 -0800427 if (*p == end) {
428 info->has_create_ino = false;
429 } else {
430 info->has_create_ino = true;
Jeff Layton1d3f8722019-09-26 16:05:11 -0400431 ceph_decode_64_safe(p, end, info->ino, bad);
Sam Lang6e8575f2012-12-28 09:56:46 -0800432 }
Jeff Layton1d3f8722019-09-26 16:05:11 -0400433 } else {
434 if (*p != end)
435 goto bad;
Sam Lang6e8575f2012-12-28 09:56:46 -0800436 }
437
Jeff Layton1d3f8722019-09-26 16:05:11 -0400438 /* Skip over any unrecognized fields */
439 *p = end;
Sam Lang6e8575f2012-12-28 09:56:46 -0800440 return 0;
Sam Lang6e8575f2012-12-28 09:56:46 -0800441bad:
442 return -EIO;
443}
444
445/*
Herb Shiu25933ab2010-12-01 14:14:38 -0800446 * parse extra results
447 */
448static int parse_reply_info_extra(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800449 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200450 u64 features)
Herb Shiu25933ab2010-12-01 14:14:38 -0800451{
Jeff Layton6df8c9d2017-01-12 14:42:41 -0500452 u32 op = le32_to_cpu(info->head->op);
453
454 if (op == CEPH_MDS_OP_GETFILELOCK)
Sage Weil14303d22010-12-14 17:37:52 -0800455 return parse_reply_info_filelock(p, end, info, features);
Jeff Layton6df8c9d2017-01-12 14:42:41 -0500456 else if (op == CEPH_MDS_OP_READDIR || op == CEPH_MDS_OP_LSSNAP)
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800457 return parse_reply_info_readdir(p, end, info, features);
Jeff Layton6df8c9d2017-01-12 14:42:41 -0500458 else if (op == CEPH_MDS_OP_CREATE)
Sam Lang6e8575f2012-12-28 09:56:46 -0800459 return parse_reply_info_create(p, end, info, features);
460 else
461 return -EIO;
Herb Shiu25933ab2010-12-01 14:14:38 -0800462}
463
464/*
Sage Weil2f2dc052009-10-06 11:31:09 -0700465 * parse entire mds reply
466 */
467static int parse_reply_info(struct ceph_msg *msg,
Sage Weil14303d22010-12-14 17:37:52 -0800468 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200469 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700470{
471 void *p, *end;
472 u32 len;
473 int err;
474
475 info->head = msg->front.iov_base;
476 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
477 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
478
479 /* trace */
480 ceph_decode_32_safe(&p, end, len, bad);
481 if (len > 0) {
Xi Wang32852a82012-01-14 22:20:59 -0500482 ceph_decode_need(&p, end, len, bad);
Sage Weil14303d22010-12-14 17:37:52 -0800483 err = parse_reply_info_trace(&p, p+len, info, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700484 if (err < 0)
485 goto out_bad;
486 }
487
Herb Shiu25933ab2010-12-01 14:14:38 -0800488 /* extra */
Sage Weil2f2dc052009-10-06 11:31:09 -0700489 ceph_decode_32_safe(&p, end, len, bad);
490 if (len > 0) {
Xi Wang32852a82012-01-14 22:20:59 -0500491 ceph_decode_need(&p, end, len, bad);
Sage Weil14303d22010-12-14 17:37:52 -0800492 err = parse_reply_info_extra(&p, p+len, info, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700493 if (err < 0)
494 goto out_bad;
495 }
496
497 /* snap blob */
498 ceph_decode_32_safe(&p, end, len, bad);
499 info->snapblob_len = len;
500 info->snapblob = p;
501 p += len;
502
503 if (p != end)
504 goto bad;
505 return 0;
506
507bad:
508 err = -EIO;
509out_bad:
510 pr_err("mds parse_reply err %d\n", err);
511 return err;
512}
513
514static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
515{
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800516 if (!info->dir_entries)
Yan, Zheng54008392014-03-29 13:41:15 +0800517 return;
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800518 free_pages((unsigned long)info->dir_entries, get_order(info->dir_buf_size));
Sage Weil2f2dc052009-10-06 11:31:09 -0700519}
520
521
522/*
523 * sessions
524 */
John Spraya687eca2014-09-19 13:51:08 +0100525const char *ceph_session_state_name(int s)
Sage Weil2f2dc052009-10-06 11:31:09 -0700526{
527 switch (s) {
528 case CEPH_MDS_SESSION_NEW: return "new";
529 case CEPH_MDS_SESSION_OPENING: return "opening";
530 case CEPH_MDS_SESSION_OPEN: return "open";
531 case CEPH_MDS_SESSION_HUNG: return "hung";
532 case CEPH_MDS_SESSION_CLOSING: return "closing";
Sage Weil44ca18f2010-02-15 12:08:46 -0800533 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
Sage Weil2f2dc052009-10-06 11:31:09 -0700534 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
Yan, Zhengfcff4152016-09-14 16:39:51 +0800535 case CEPH_MDS_SESSION_REJECTED: return "rejected";
Sage Weil2f2dc052009-10-06 11:31:09 -0700536 default: return "???";
537 }
538}
539
540static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
541{
Elena Reshetova3997c012017-03-03 11:15:06 +0200542 if (refcount_inc_not_zero(&s->s_ref)) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700543 dout("mdsc get_session %p %d -> %d\n", s,
Elena Reshetova3997c012017-03-03 11:15:06 +0200544 refcount_read(&s->s_ref)-1, refcount_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -0700545 return s;
546 } else {
Chengguang Xu4c069a52018-01-30 16:29:17 +0800547 dout("mdsc get_session %p 0 -- FAIL\n", s);
Sage Weil2f2dc052009-10-06 11:31:09 -0700548 return NULL;
549 }
550}
551
552void ceph_put_mds_session(struct ceph_mds_session *s)
553{
554 dout("mdsc put_session %p %d -> %d\n", s,
Elena Reshetova3997c012017-03-03 11:15:06 +0200555 refcount_read(&s->s_ref), refcount_read(&s->s_ref)-1);
556 if (refcount_dec_and_test(&s->s_ref)) {
Alex Elder6c4a1912012-05-16 15:16:38 -0500557 if (s->s_auth.authorizer)
Ilya Dryomov6c1ea262016-04-11 19:34:49 +0200558 ceph_auth_destroy_authorizer(s->s_auth.authorizer);
Sage Weil2f2dc052009-10-06 11:31:09 -0700559 kfree(s);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800560 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700561}
562
563/*
564 * called under mdsc->mutex
565 */
566struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
567 int mds)
568{
Markus Elfringd37b1d92017-08-20 20:22:02 +0200569 if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
Sage Weil2f2dc052009-10-06 11:31:09 -0700570 return NULL;
Jeff Layton488f5282019-04-24 10:05:39 -0400571 return get_session(mdsc->sessions[mds]);
Sage Weil2f2dc052009-10-06 11:31:09 -0700572}
573
574static bool __have_session(struct ceph_mds_client *mdsc, int mds)
575{
Chengguang Xu98cfda82018-03-13 10:43:45 +0800576 if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
Sage Weil2f2dc052009-10-06 11:31:09 -0700577 return false;
Chengguang Xu98cfda82018-03-13 10:43:45 +0800578 else
579 return true;
Sage Weil2f2dc052009-10-06 11:31:09 -0700580}
581
Sage Weil2600d2d2010-02-22 15:12:16 -0800582static int __verify_registered_session(struct ceph_mds_client *mdsc,
583 struct ceph_mds_session *s)
584{
585 if (s->s_mds >= mdsc->max_sessions ||
586 mdsc->sessions[s->s_mds] != s)
587 return -ENOENT;
588 return 0;
589}
590
Sage Weil2f2dc052009-10-06 11:31:09 -0700591/*
592 * create+register a new session for given mds.
593 * called under mdsc->mutex.
594 */
595static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
596 int mds)
597{
598 struct ceph_mds_session *s;
599
Yan, Zheng76201b62017-03-28 17:04:13 +0800600 if (mds >= mdsc->mdsmap->m_num_mds)
Nathaniel Yazdanic338c072013-08-04 21:04:30 -0700601 return ERR_PTR(-EINVAL);
602
Sage Weil2f2dc052009-10-06 11:31:09 -0700603 s = kzalloc(sizeof(*s), GFP_NOFS);
Dan Carpenter4736b002010-03-20 15:30:16 +0300604 if (!s)
605 return ERR_PTR(-ENOMEM);
Chengguang Xu47474d02018-03-13 23:01:07 +0800606
607 if (mds >= mdsc->max_sessions) {
608 int newmax = 1 << get_count_order(mds + 1);
609 struct ceph_mds_session **sa;
610
611 dout("%s: realloc to %d\n", __func__, newmax);
612 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
613 if (!sa)
614 goto fail_realloc;
615 if (mdsc->sessions) {
616 memcpy(sa, mdsc->sessions,
617 mdsc->max_sessions * sizeof(void *));
618 kfree(mdsc->sessions);
619 }
620 mdsc->sessions = sa;
621 mdsc->max_sessions = newmax;
622 }
623
624 dout("%s: mds%d\n", __func__, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -0700625 s->s_mdsc = mdsc;
626 s->s_mds = mds;
627 s->s_state = CEPH_MDS_SESSION_NEW;
628 s->s_ttl = 0;
629 s->s_seq = 0;
630 mutex_init(&s->s_mutex);
631
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700632 ceph_con_init(&s->s_con, s, &mds_con_ops, &mdsc->fsc->client->msgr);
Sage Weil2f2dc052009-10-06 11:31:09 -0700633
Alex Elderd8fb02a2012-01-12 17:48:10 -0800634 spin_lock_init(&s->s_gen_ttl_lock);
Yan, Zheng1e9c2eb2019-01-28 20:43:55 +0800635 s->s_cap_gen = 1;
Alex Elder1ce208a2012-01-12 17:48:11 -0800636 s->s_cap_ttl = jiffies - 1;
Alex Elderd8fb02a2012-01-12 17:48:10 -0800637
638 spin_lock_init(&s->s_cap_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700639 s->s_renew_requested = 0;
640 s->s_renew_seq = 0;
641 INIT_LIST_HEAD(&s->s_caps);
642 s->s_nr_caps = 0;
Elena Reshetova3997c012017-03-03 11:15:06 +0200643 refcount_set(&s->s_ref, 1);
Sage Weil2f2dc052009-10-06 11:31:09 -0700644 INIT_LIST_HEAD(&s->s_waiting);
645 INIT_LIST_HEAD(&s->s_unsafe);
646 s->s_num_cap_releases = 0;
Yan, Zheng99a9c272013-09-22 11:08:14 +0800647 s->s_cap_reconnect = 0;
Sage Weil7c1332b2010-02-16 11:39:45 -0800648 s->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700649 INIT_LIST_HEAD(&s->s_cap_releases);
Yan, Zhenge3ec8d62019-01-14 17:21:19 +0800650 INIT_WORK(&s->s_cap_release_work, ceph_cap_release_work);
651
Sage Weil2f2dc052009-10-06 11:31:09 -0700652 INIT_LIST_HEAD(&s->s_cap_flushing);
Sage Weil2f2dc052009-10-06 11:31:09 -0700653
Sage Weil2f2dc052009-10-06 11:31:09 -0700654 mdsc->sessions[mds] = s;
Yan, Zheng86d8f672015-01-09 17:00:42 +0800655 atomic_inc(&mdsc->num_sessions);
Elena Reshetova3997c012017-03-03 11:15:06 +0200656 refcount_inc(&s->s_ref); /* one ref to sessions[], one to caller */
Sage Weil42ce56e2009-11-18 11:22:36 -0800657
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700658 ceph_con_open(&s->s_con, CEPH_ENTITY_TYPE_MDS, mds,
659 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
Sage Weil42ce56e2009-11-18 11:22:36 -0800660
Sage Weil2f2dc052009-10-06 11:31:09 -0700661 return s;
Sage Weil42ce56e2009-11-18 11:22:36 -0800662
663fail_realloc:
664 kfree(s);
665 return ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -0700666}
667
668/*
669 * called under mdsc->mutex
670 */
Sage Weil2600d2d2010-02-22 15:12:16 -0800671static void __unregister_session(struct ceph_mds_client *mdsc,
Sage Weil42ce56e2009-11-18 11:22:36 -0800672 struct ceph_mds_session *s)
Sage Weil2f2dc052009-10-06 11:31:09 -0700673{
Sage Weil2600d2d2010-02-22 15:12:16 -0800674 dout("__unregister_session mds%d %p\n", s->s_mds, s);
675 BUG_ON(mdsc->sessions[s->s_mds] != s);
Sage Weil42ce56e2009-11-18 11:22:36 -0800676 mdsc->sessions[s->s_mds] = NULL;
Yan, Zhenge3ec8d62019-01-14 17:21:19 +0800677 s->s_state = 0;
Sage Weil42ce56e2009-11-18 11:22:36 -0800678 ceph_con_close(&s->s_con);
679 ceph_put_mds_session(s);
Yan, Zheng86d8f672015-01-09 17:00:42 +0800680 atomic_dec(&mdsc->num_sessions);
Sage Weil2f2dc052009-10-06 11:31:09 -0700681}
682
683/*
684 * drop session refs in request.
685 *
686 * should be last request ref, or hold mdsc->mutex
687 */
688static void put_request_session(struct ceph_mds_request *req)
689{
690 if (req->r_session) {
691 ceph_put_mds_session(req->r_session);
692 req->r_session = NULL;
693 }
694}
695
Sage Weil153c8e62009-12-07 12:31:09 -0800696void ceph_mdsc_release_request(struct kref *kref)
Sage Weil2f2dc052009-10-06 11:31:09 -0700697{
Sage Weil153c8e62009-12-07 12:31:09 -0800698 struct ceph_mds_request *req = container_of(kref,
699 struct ceph_mds_request,
700 r_kref);
Yan, Zheng54008392014-03-29 13:41:15 +0800701 destroy_reply_info(&req->r_reply_info);
Sage Weil153c8e62009-12-07 12:31:09 -0800702 if (req->r_request)
703 ceph_msg_put(req->r_request);
Yan, Zheng54008392014-03-29 13:41:15 +0800704 if (req->r_reply)
Sage Weil153c8e62009-12-07 12:31:09 -0800705 ceph_msg_put(req->r_reply);
Sage Weil153c8e62009-12-07 12:31:09 -0800706 if (req->r_inode) {
Sage Weil41b02e12011-07-26 11:31:14 -0700707 ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
Yan, Zheng3e1d0452019-05-18 20:39:55 +0800708 /* avoid calling iput_final() in mds dispatch threads */
709 ceph_async_iput(req->r_inode);
Sage Weil153c8e62009-12-07 12:31:09 -0800710 }
Jeff Layton9c1c2b32019-04-03 13:16:01 -0400711 if (req->r_parent) {
Jeff Layton3dd69aa2017-01-31 10:28:26 -0500712 ceph_put_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
Jeff Layton9c1c2b32019-04-03 13:16:01 -0400713 ceph_async_iput(req->r_parent);
714 }
Yan, Zheng3e1d0452019-05-18 20:39:55 +0800715 ceph_async_iput(req->r_target_inode);
Sage Weil153c8e62009-12-07 12:31:09 -0800716 if (req->r_dentry)
717 dput(req->r_dentry);
Sage Weil844d87c2013-02-05 13:40:09 -0800718 if (req->r_old_dentry)
719 dput(req->r_old_dentry);
720 if (req->r_old_dentry_dir) {
Sage Weil41b02e12011-07-26 11:31:14 -0700721 /*
722 * track (and drop pins for) r_old_dentry_dir
723 * separately, since r_old_dentry's d_parent may have
724 * changed between the dir mutex being dropped and
725 * this request being freed.
726 */
727 ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir),
728 CEPH_CAP_PIN);
Yan, Zheng3e1d0452019-05-18 20:39:55 +0800729 ceph_async_iput(req->r_old_dentry_dir);
Sage Weil153c8e62009-12-07 12:31:09 -0800730 }
731 kfree(req->r_path1);
732 kfree(req->r_path2);
Yan, Zheng25e6bae2014-09-16 19:15:28 +0800733 if (req->r_pagelist)
734 ceph_pagelist_release(req->r_pagelist);
Sage Weil153c8e62009-12-07 12:31:09 -0800735 put_request_session(req);
Yehuda Sadeh37151662010-06-17 16:16:12 -0700736 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
Yan, Zheng428138c2019-06-14 10:55:05 +0800737 WARN_ON_ONCE(!list_empty(&req->r_wait));
Sage Weil153c8e62009-12-07 12:31:09 -0800738 kfree(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700739}
740
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200741DEFINE_RB_FUNCS(request, struct ceph_mds_request, r_tid, r_node)
742
Sage Weil2f2dc052009-10-06 11:31:09 -0700743/*
744 * lookup session, bump ref if found.
745 *
746 * called under mdsc->mutex.
747 */
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200748static struct ceph_mds_request *
749lookup_get_request(struct ceph_mds_client *mdsc, u64 tid)
Sage Weil2f2dc052009-10-06 11:31:09 -0700750{
751 struct ceph_mds_request *req;
Sage Weil44ca18f2010-02-15 12:08:46 -0800752
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200753 req = lookup_request(&mdsc->request_tree, tid);
754 if (req)
755 ceph_mdsc_get_request(req);
Sage Weil44ca18f2010-02-15 12:08:46 -0800756
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200757 return req;
Sage Weil2f2dc052009-10-06 11:31:09 -0700758}
759
760/*
761 * Register an in-flight request, and assign a tid. Link to directory
762 * are modifying (if any).
763 *
764 * Called under mdsc->mutex.
765 */
766static void __register_request(struct ceph_mds_client *mdsc,
767 struct ceph_mds_request *req,
768 struct inode *dir)
769{
Zhi Zhange30ee582018-01-24 21:24:33 +0800770 int ret = 0;
771
Sage Weil2f2dc052009-10-06 11:31:09 -0700772 req->r_tid = ++mdsc->last_tid;
Zhi Zhange30ee582018-01-24 21:24:33 +0800773 if (req->r_num_caps) {
774 ret = ceph_reserve_caps(mdsc, &req->r_caps_reservation,
775 req->r_num_caps);
776 if (ret < 0) {
777 pr_err("__register_request %p "
778 "failed to reserve caps: %d\n", req, ret);
779 /* set req->r_err to fail early from __do_request */
780 req->r_err = ret;
781 return;
782 }
783 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700784 dout("__register_request %p tid %lld\n", req, req->r_tid);
785 ceph_mdsc_get_request(req);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200786 insert_request(&mdsc->request_tree, req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700787
Sage Weilcb4276c2010-11-08 07:28:52 -0800788 req->r_uid = current_fsuid();
789 req->r_gid = current_fsgid();
790
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +0800791 if (mdsc->oldest_tid == 0 && req->r_op != CEPH_MDS_OP_SETFILELOCK)
792 mdsc->oldest_tid = req->r_tid;
793
Sage Weil2f2dc052009-10-06 11:31:09 -0700794 if (dir) {
Sage Weil3b663782011-05-18 16:12:12 -0700795 ihold(dir);
Sage Weil2f2dc052009-10-06 11:31:09 -0700796 req->r_unsafe_dir = dir;
Sage Weil2f2dc052009-10-06 11:31:09 -0700797 }
798}
799
800static void __unregister_request(struct ceph_mds_client *mdsc,
801 struct ceph_mds_request *req)
802{
803 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +0800804
Jeff Laytondf963ea2017-02-14 10:09:40 -0500805 /* Never leave an unregistered request on an unsafe list! */
806 list_del_init(&req->r_unsafe_item);
807
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +0800808 if (req->r_tid == mdsc->oldest_tid) {
809 struct rb_node *p = rb_next(&req->r_node);
810 mdsc->oldest_tid = 0;
811 while (p) {
812 struct ceph_mds_request *next_req =
813 rb_entry(p, struct ceph_mds_request, r_node);
814 if (next_req->r_op != CEPH_MDS_OP_SETFILELOCK) {
815 mdsc->oldest_tid = next_req->r_tid;
816 break;
817 }
818 p = rb_next(p);
819 }
820 }
821
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200822 erase_request(&mdsc->request_tree, req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700823
Jeff Laytonbc2de10d2017-02-01 13:49:09 -0500824 if (req->r_unsafe_dir &&
825 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700826 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
Sage Weil2f2dc052009-10-06 11:31:09 -0700827 spin_lock(&ci->i_unsafe_lock);
828 list_del_init(&req->r_unsafe_dir_item);
829 spin_unlock(&ci->i_unsafe_lock);
Yan, Zheng4c06ace2015-10-27 17:18:00 +0800830 }
Jeff Laytonbc2de10d2017-02-01 13:49:09 -0500831 if (req->r_target_inode &&
832 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Yan, Zheng68cd5b42015-10-27 18:36:06 +0800833 struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
834 spin_lock(&ci->i_unsafe_lock);
835 list_del_init(&req->r_unsafe_target_item);
836 spin_unlock(&ci->i_unsafe_lock);
837 }
Sage Weil3b663782011-05-18 16:12:12 -0700838
Yan, Zheng4c06ace2015-10-27 17:18:00 +0800839 if (req->r_unsafe_dir) {
Yan, Zheng3e1d0452019-05-18 20:39:55 +0800840 /* avoid calling iput_final() in mds dispatch threads */
841 ceph_async_iput(req->r_unsafe_dir);
Sage Weil3b663782011-05-18 16:12:12 -0700842 req->r_unsafe_dir = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700843 }
Sage Weil94aa8ae2010-03-28 21:22:50 -0700844
Yan, Zhengfc55d2c2013-10-31 09:10:47 +0800845 complete_all(&req->r_safe_completion);
846
Sage Weil94aa8ae2010-03-28 21:22:50 -0700847 ceph_mdsc_put_request(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700848}
849
850/*
Jeff Layton30c71232016-12-15 08:37:56 -0500851 * Walk back up the dentry tree until we hit a dentry representing a
852 * non-snapshot inode. We do this using the rcu_read_lock (which must be held
853 * when calling this) to ensure that the objects won't disappear while we're
854 * working with them. Once we hit a candidate dentry, we attempt to take a
855 * reference to it, and return that as the result.
856 */
Dan Carpenterf1075482017-02-23 13:39:59 +0300857static struct inode *get_nonsnap_parent(struct dentry *dentry)
858{
859 struct inode *inode = NULL;
Jeff Layton30c71232016-12-15 08:37:56 -0500860
861 while (dentry && !IS_ROOT(dentry)) {
862 inode = d_inode_rcu(dentry);
863 if (!inode || ceph_snap(inode) == CEPH_NOSNAP)
864 break;
865 dentry = dentry->d_parent;
866 }
867 if (inode)
868 inode = igrab(inode);
869 return inode;
870}
871
872/*
Sage Weil2f2dc052009-10-06 11:31:09 -0700873 * Choose mds to send request to next. If there is a hint set in the
874 * request (e.g., due to a prior forward hint from the mds), use that.
875 * Otherwise, consult frag tree and/or caps to identify the
876 * appropriate mds. If all else fails, choose randomly.
877 *
878 * Called under mdsc->mutex.
879 */
880static int __choose_mds(struct ceph_mds_client *mdsc,
881 struct ceph_mds_request *req)
882{
883 struct inode *inode;
884 struct ceph_inode_info *ci;
885 struct ceph_cap *cap;
886 int mode = req->r_direct_mode;
887 int mds = -1;
888 u32 hash = req->r_direct_hash;
Jeff Laytonbc2de10d2017-02-01 13:49:09 -0500889 bool is_hash = test_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags);
Sage Weil2f2dc052009-10-06 11:31:09 -0700890
891 /*
892 * is there a specific mds we should try? ignore hint if we have
893 * no session and the mds is not up (active or recovering).
894 */
895 if (req->r_resend_mds >= 0 &&
896 (__have_session(mdsc, req->r_resend_mds) ||
897 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
898 dout("choose_mds using resend_mds mds%d\n",
899 req->r_resend_mds);
900 return req->r_resend_mds;
901 }
902
903 if (mode == USE_RANDOM_MDS)
904 goto random;
905
906 inode = NULL;
907 if (req->r_inode) {
Yan, Zheng5d37ca12017-07-26 12:48:08 +0800908 if (ceph_snap(req->r_inode) != CEPH_SNAPDIR) {
909 inode = req->r_inode;
910 ihold(inode);
911 } else {
Yan, Zheng38f340c2017-09-22 11:41:06 +0800912 /* req->r_dentry is non-null for LSSNAP request */
913 rcu_read_lock();
914 inode = get_nonsnap_parent(req->r_dentry);
915 rcu_read_unlock();
916 dout("__choose_mds using snapdir's parent %p\n", inode);
Yan, Zheng5d37ca12017-07-26 12:48:08 +0800917 }
Yan, Zheng38f340c2017-09-22 11:41:06 +0800918 } else if (req->r_dentry) {
Sage Weild79698d2011-07-26 11:31:26 -0700919 /* ignore race with rename; old or new d_parent is okay */
Jeff Layton30c71232016-12-15 08:37:56 -0500920 struct dentry *parent;
921 struct inode *dir;
Sage Weileb6bb1c2010-08-16 09:21:27 -0700922
Jeff Layton30c71232016-12-15 08:37:56 -0500923 rcu_read_lock();
Yan, Zheng41883ba2019-05-23 10:22:55 +0800924 parent = READ_ONCE(req->r_dentry->d_parent);
Jeff Layton3dd69aa2017-01-31 10:28:26 -0500925 dir = req->r_parent ? : d_inode_rcu(parent);
Jeff Layton30c71232016-12-15 08:37:56 -0500926
927 if (!dir || dir->i_sb != mdsc->fsc->sb) {
928 /* not this fs or parent went negative */
David Howells2b0143b2015-03-17 22:25:59 +0000929 inode = d_inode(req->r_dentry);
Jeff Layton30c71232016-12-15 08:37:56 -0500930 if (inode)
931 ihold(inode);
Sage Weileb6bb1c2010-08-16 09:21:27 -0700932 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
933 /* direct snapped/virtual snapdir requests
934 * based on parent dir inode */
Jeff Layton30c71232016-12-15 08:37:56 -0500935 inode = get_nonsnap_parent(parent);
Sage Weileb6bb1c2010-08-16 09:21:27 -0700936 dout("__choose_mds using nonsnap parent %p\n", inode);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800937 } else {
Sage Weileb6bb1c2010-08-16 09:21:27 -0700938 /* dentry target */
David Howells2b0143b2015-03-17 22:25:59 +0000939 inode = d_inode(req->r_dentry);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800940 if (!inode || mode == USE_AUTH_MDS) {
941 /* dir + name */
Jeff Layton30c71232016-12-15 08:37:56 -0500942 inode = igrab(dir);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800943 hash = ceph_dentry_hash(dir, req->r_dentry);
944 is_hash = true;
Jeff Layton30c71232016-12-15 08:37:56 -0500945 } else {
946 ihold(inode);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800947 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700948 }
Jeff Layton30c71232016-12-15 08:37:56 -0500949 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -0700950 }
Sage Weileb6bb1c2010-08-16 09:21:27 -0700951
Sage Weil2f2dc052009-10-06 11:31:09 -0700952 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
953 (int)hash, mode);
954 if (!inode)
955 goto random;
956 ci = ceph_inode(inode);
957
958 if (is_hash && S_ISDIR(inode->i_mode)) {
959 struct ceph_inode_frag frag;
960 int found;
961
962 ceph_choose_frag(ci, hash, &frag, &found);
963 if (found) {
964 if (mode == USE_ANY_MDS && frag.ndist > 0) {
965 u8 r;
966
967 /* choose a random replica */
968 get_random_bytes(&r, 1);
969 r %= frag.ndist;
970 mds = frag.dist[r];
971 dout("choose_mds %p %llx.%llx "
972 "frag %u mds%d (%d/%d)\n",
973 inode, ceph_vinop(inode),
Sage Weild66bbd42011-01-21 21:16:46 -0800974 frag.frag, mds,
Sage Weil2f2dc052009-10-06 11:31:09 -0700975 (int)r, frag.ndist);
Sage Weild66bbd42011-01-21 21:16:46 -0800976 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
Xiubo Li5d476482019-11-26 07:24:22 -0500977 CEPH_MDS_STATE_ACTIVE &&
978 !ceph_mdsmap_is_laggy(mdsc->mdsmap, mds))
Jeff Layton30c71232016-12-15 08:37:56 -0500979 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -0700980 }
981
982 /* since this file/dir wasn't known to be
983 * replicated, then we want to look for the
984 * authoritative mds. */
Sage Weil2f2dc052009-10-06 11:31:09 -0700985 if (frag.mds >= 0) {
986 /* choose auth mds */
987 mds = frag.mds;
988 dout("choose_mds %p %llx.%llx "
989 "frag %u mds%d (auth)\n",
990 inode, ceph_vinop(inode), frag.frag, mds);
Sage Weild66bbd42011-01-21 21:16:46 -0800991 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
Xiubo Li5d476482019-11-26 07:24:22 -0500992 CEPH_MDS_STATE_ACTIVE) {
993 if (mode == USE_ANY_MDS &&
994 !ceph_mdsmap_is_laggy(mdsc->mdsmap,
995 mds))
996 goto out;
997 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700998 }
Xiubo Li5d476482019-11-26 07:24:22 -0500999 mode = USE_AUTH_MDS;
Sage Weil2f2dc052009-10-06 11:31:09 -07001000 }
1001 }
1002
Sage Weilbe655592011-11-30 09:47:09 -08001003 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001004 cap = NULL;
1005 if (mode == USE_AUTH_MDS)
1006 cap = ci->i_auth_cap;
1007 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
1008 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
1009 if (!cap) {
Sage Weilbe655592011-11-30 09:47:09 -08001010 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng3e1d0452019-05-18 20:39:55 +08001011 ceph_async_iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07001012 goto random;
1013 }
1014 mds = cap->session->s_mds;
1015 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
1016 inode, ceph_vinop(inode), mds,
1017 cap == ci->i_auth_cap ? "auth " : "", cap);
Sage Weilbe655592011-11-30 09:47:09 -08001018 spin_unlock(&ci->i_ceph_lock);
Jeff Layton30c71232016-12-15 08:37:56 -05001019out:
Yan, Zheng3e1d0452019-05-18 20:39:55 +08001020 /* avoid calling iput_final() while holding mdsc->mutex or
1021 * in mds dispatch threads */
1022 ceph_async_iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07001023 return mds;
1024
1025random:
1026 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
1027 dout("choose_mds chose random mds%d\n", mds);
1028 return mds;
1029}
1030
1031
1032/*
1033 * session messages
1034 */
1035static struct ceph_msg *create_session_msg(u32 op, u64 seq)
1036{
1037 struct ceph_msg *msg;
1038 struct ceph_mds_session_head *h;
1039
Sage Weilb61c2762011-08-09 15:03:46 -07001040 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
1041 false);
Sage Weila79832f2010-04-01 16:06:19 -07001042 if (!msg) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001043 pr_err("create_session_msg ENOMEM creating msg\n");
Sage Weila79832f2010-04-01 16:06:19 -07001044 return NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001045 }
1046 h = msg->front.iov_base;
1047 h->op = cpu_to_le32(op);
1048 h->seq = cpu_to_le64(seq);
John Spraydbd0c8b2014-09-09 19:26:01 +01001049
1050 return msg;
1051}
1052
Yan, Zheng342ce182018-05-11 18:47:29 +08001053static void encode_supported_features(void **p, void *end)
1054{
1055 static const unsigned char bits[] = CEPHFS_FEATURES_CLIENT_SUPPORTED;
1056 static const size_t count = ARRAY_SIZE(bits);
1057
1058 if (count > 0) {
1059 size_t i;
1060 size_t size = ((size_t)bits[count - 1] + 64) / 64 * 8;
1061
1062 BUG_ON(*p + 4 + size > end);
1063 ceph_encode_32(p, size);
1064 memset(*p, 0, size);
1065 for (i = 0; i < count; i++)
1066 ((unsigned char*)(*p))[i / 8] |= 1 << (bits[i] % 8);
1067 *p += size;
1068 } else {
1069 BUG_ON(*p + 4 > end);
1070 ceph_encode_32(p, 0);
1071 }
1072}
1073
John Spraydbd0c8b2014-09-09 19:26:01 +01001074/*
1075 * session message, specialization for CEPH_SESSION_REQUEST_OPEN
1076 * to include additional client metadata fields.
1077 */
1078static struct ceph_msg *create_session_open_msg(struct ceph_mds_client *mdsc, u64 seq)
1079{
1080 struct ceph_msg *msg;
1081 struct ceph_mds_session_head *h;
1082 int i = -1;
Yan, Zheng342ce182018-05-11 18:47:29 +08001083 int extra_bytes = 0;
John Spraydbd0c8b2014-09-09 19:26:01 +01001084 int metadata_key_count = 0;
1085 struct ceph_options *opt = mdsc->fsc->client->options;
Yan, Zheng3f384952016-04-21 11:09:55 +08001086 struct ceph_mount_options *fsopt = mdsc->fsc->mount_options;
Yan, Zheng342ce182018-05-11 18:47:29 +08001087 void *p, *end;
John Spraydbd0c8b2014-09-09 19:26:01 +01001088
Yan, Zhenga6a5ce42015-01-16 10:54:43 +08001089 const char* metadata[][2] = {
Yan, Zheng717e6f22017-09-11 12:10:08 +08001090 {"hostname", mdsc->nodename},
1091 {"kernel_version", init_utsname()->release},
Yan, Zheng3f384952016-04-21 11:09:55 +08001092 {"entity_id", opt->name ? : ""},
1093 {"root", fsopt->server_path ? : "/"},
John Spraydbd0c8b2014-09-09 19:26:01 +01001094 {NULL, NULL}
1095 };
1096
1097 /* Calculate serialized length of metadata */
Yan, Zheng342ce182018-05-11 18:47:29 +08001098 extra_bytes = 4; /* map length */
Markus Elfringd37b1d92017-08-20 20:22:02 +02001099 for (i = 0; metadata[i][0]; ++i) {
Yan, Zheng342ce182018-05-11 18:47:29 +08001100 extra_bytes += 8 + strlen(metadata[i][0]) +
John Spraydbd0c8b2014-09-09 19:26:01 +01001101 strlen(metadata[i][1]);
1102 metadata_key_count++;
1103 }
Yan, Zheng342ce182018-05-11 18:47:29 +08001104 /* supported feature */
1105 extra_bytes += 4 + 8;
John Spraydbd0c8b2014-09-09 19:26:01 +01001106
1107 /* Allocate the message */
Yan, Zheng342ce182018-05-11 18:47:29 +08001108 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + extra_bytes,
John Spraydbd0c8b2014-09-09 19:26:01 +01001109 GFP_NOFS, false);
1110 if (!msg) {
1111 pr_err("create_session_msg ENOMEM creating msg\n");
1112 return NULL;
1113 }
Yan, Zheng342ce182018-05-11 18:47:29 +08001114 p = msg->front.iov_base;
1115 end = p + msg->front.iov_len;
1116
1117 h = p;
John Spraydbd0c8b2014-09-09 19:26:01 +01001118 h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN);
1119 h->seq = cpu_to_le64(seq);
1120
1121 /*
1122 * Serialize client metadata into waiting buffer space, using
1123 * the format that userspace expects for map<string, string>
John Spray7cfa0312014-10-30 17:15:26 +00001124 *
1125 * ClientSession messages with metadata are v2
John Spraydbd0c8b2014-09-09 19:26:01 +01001126 */
Yan, Zheng342ce182018-05-11 18:47:29 +08001127 msg->hdr.version = cpu_to_le16(3);
John Spray7cfa0312014-10-30 17:15:26 +00001128 msg->hdr.compat_version = cpu_to_le16(1);
John Spraydbd0c8b2014-09-09 19:26:01 +01001129
1130 /* The write pointer, following the session_head structure */
Yan, Zheng342ce182018-05-11 18:47:29 +08001131 p += sizeof(*h);
John Spraydbd0c8b2014-09-09 19:26:01 +01001132
1133 /* Number of entries in the map */
1134 ceph_encode_32(&p, metadata_key_count);
1135
1136 /* Two length-prefixed strings for each entry in the map */
Markus Elfringd37b1d92017-08-20 20:22:02 +02001137 for (i = 0; metadata[i][0]; ++i) {
John Spraydbd0c8b2014-09-09 19:26:01 +01001138 size_t const key_len = strlen(metadata[i][0]);
1139 size_t const val_len = strlen(metadata[i][1]);
1140
1141 ceph_encode_32(&p, key_len);
1142 memcpy(p, metadata[i][0], key_len);
1143 p += key_len;
1144 ceph_encode_32(&p, val_len);
1145 memcpy(p, metadata[i][1], val_len);
1146 p += val_len;
1147 }
1148
Yan, Zheng342ce182018-05-11 18:47:29 +08001149 encode_supported_features(&p, end);
1150 msg->front.iov_len = p - msg->front.iov_base;
1151 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1152
Sage Weil2f2dc052009-10-06 11:31:09 -07001153 return msg;
1154}
1155
1156/*
1157 * send session open request.
1158 *
1159 * called under mdsc->mutex
1160 */
1161static int __open_session(struct ceph_mds_client *mdsc,
1162 struct ceph_mds_session *session)
1163{
1164 struct ceph_msg *msg;
1165 int mstate;
1166 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07001167
1168 /* wait for mds to go active? */
1169 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
1170 dout("open_session to mds%d (%s)\n", mds,
1171 ceph_mds_state_name(mstate));
1172 session->s_state = CEPH_MDS_SESSION_OPENING;
1173 session->s_renew_requested = jiffies;
1174
1175 /* send connect message */
John Spraydbd0c8b2014-09-09 19:26:01 +01001176 msg = create_session_open_msg(mdsc, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001177 if (!msg)
1178 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -07001179 ceph_con_send(&session->s_con, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001180 return 0;
1181}
1182
1183/*
Sage Weiled0552a2010-06-21 13:38:25 -07001184 * open sessions for any export targets for the given mds
1185 *
1186 * called under mdsc->mutex
1187 */
Yan, Zheng5d72d132013-11-24 14:33:01 +08001188static struct ceph_mds_session *
1189__open_export_target_session(struct ceph_mds_client *mdsc, int target)
1190{
1191 struct ceph_mds_session *session;
1192
1193 session = __ceph_lookup_mds_session(mdsc, target);
1194 if (!session) {
1195 session = register_session(mdsc, target);
1196 if (IS_ERR(session))
1197 return session;
1198 }
1199 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1200 session->s_state == CEPH_MDS_SESSION_CLOSING)
1201 __open_session(mdsc, session);
1202
1203 return session;
1204}
1205
1206struct ceph_mds_session *
1207ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target)
1208{
1209 struct ceph_mds_session *session;
1210
1211 dout("open_export_target_session to mds%d\n", target);
1212
1213 mutex_lock(&mdsc->mutex);
1214 session = __open_export_target_session(mdsc, target);
1215 mutex_unlock(&mdsc->mutex);
1216
1217 return session;
1218}
1219
Sage Weiled0552a2010-06-21 13:38:25 -07001220static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
1221 struct ceph_mds_session *session)
1222{
1223 struct ceph_mds_info *mi;
1224 struct ceph_mds_session *ts;
1225 int i, mds = session->s_mds;
Sage Weiled0552a2010-06-21 13:38:25 -07001226
Yan, Zheng76201b62017-03-28 17:04:13 +08001227 if (mds >= mdsc->mdsmap->m_num_mds)
Sage Weiled0552a2010-06-21 13:38:25 -07001228 return;
Yan, Zheng5d72d132013-11-24 14:33:01 +08001229
Sage Weiled0552a2010-06-21 13:38:25 -07001230 mi = &mdsc->mdsmap->m_info[mds];
1231 dout("open_export_target_sessions for mds%d (%d targets)\n",
1232 session->s_mds, mi->num_export_targets);
1233
1234 for (i = 0; i < mi->num_export_targets; i++) {
Yan, Zheng5d72d132013-11-24 14:33:01 +08001235 ts = __open_export_target_session(mdsc, mi->export_targets[i]);
1236 if (!IS_ERR(ts))
1237 ceph_put_mds_session(ts);
Sage Weiled0552a2010-06-21 13:38:25 -07001238 }
1239}
1240
Sage Weil154f42c2010-06-21 13:45:04 -07001241void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
1242 struct ceph_mds_session *session)
1243{
1244 mutex_lock(&mdsc->mutex);
1245 __open_export_target_sessions(mdsc, session);
1246 mutex_unlock(&mdsc->mutex);
1247}
1248
Sage Weiled0552a2010-06-21 13:38:25 -07001249/*
Sage Weil2f2dc052009-10-06 11:31:09 -07001250 * session caps
1251 */
1252
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001253static void detach_cap_releases(struct ceph_mds_session *session,
1254 struct list_head *target)
Sage Weil2f2dc052009-10-06 11:31:09 -07001255{
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001256 lockdep_assert_held(&session->s_cap_lock);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001257
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001258 list_splice_init(&session->s_cap_releases, target);
1259 session->s_num_cap_releases = 0;
1260 dout("dispose_cap_releases mds%d\n", session->s_mds);
1261}
1262
1263static void dispose_cap_releases(struct ceph_mds_client *mdsc,
1264 struct list_head *dispose)
1265{
1266 while (!list_empty(dispose)) {
Yan, Zheng745a8e32015-05-14 17:22:42 +08001267 struct ceph_cap *cap;
1268 /* zero out the in-progress message */
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001269 cap = list_first_entry(dispose, struct ceph_cap, session_caps);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001270 list_del(&cap->session_caps);
1271 ceph_put_cap(mdsc, cap);
1272 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001273}
1274
Yan, Zheng1c841a92015-03-24 20:15:36 +08001275static void cleanup_session_requests(struct ceph_mds_client *mdsc,
1276 struct ceph_mds_session *session)
1277{
1278 struct ceph_mds_request *req;
1279 struct rb_node *p;
Yan, Zhengf4b97862019-07-25 20:16:42 +08001280 struct ceph_inode_info *ci;
Yan, Zheng1c841a92015-03-24 20:15:36 +08001281
1282 dout("cleanup_session_requests mds%d\n", session->s_mds);
1283 mutex_lock(&mdsc->mutex);
1284 while (!list_empty(&session->s_unsafe)) {
1285 req = list_first_entry(&session->s_unsafe,
1286 struct ceph_mds_request, r_unsafe_item);
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001287 pr_warn_ratelimited(" dropping unsafe request %llu\n",
1288 req->r_tid);
Yan, Zhengf4b97862019-07-25 20:16:42 +08001289 if (req->r_target_inode) {
1290 /* dropping unsafe change of inode's attributes */
1291 ci = ceph_inode(req->r_target_inode);
1292 errseq_set(&ci->i_meta_err, -EIO);
1293 }
1294 if (req->r_unsafe_dir) {
1295 /* dropping unsafe directory operation */
1296 ci = ceph_inode(req->r_unsafe_dir);
1297 errseq_set(&ci->i_meta_err, -EIO);
1298 }
Yan, Zheng1c841a92015-03-24 20:15:36 +08001299 __unregister_request(mdsc, req);
1300 }
1301 /* zero r_attempts, so kick_requests() will re-send requests */
1302 p = rb_first(&mdsc->request_tree);
1303 while (p) {
1304 req = rb_entry(p, struct ceph_mds_request, r_node);
1305 p = rb_next(p);
1306 if (req->r_session &&
1307 req->r_session->s_mds == session->s_mds)
1308 req->r_attempts = 0;
1309 }
1310 mutex_unlock(&mdsc->mutex);
1311}
1312
Sage Weil2f2dc052009-10-06 11:31:09 -07001313/*
Sage Weilf818a732010-05-11 20:56:31 -07001314 * Helper to safely iterate over all caps associated with a session, with
1315 * special care taken to handle a racing __ceph_remove_cap().
Sage Weil2f2dc052009-10-06 11:31:09 -07001316 *
Sage Weilf818a732010-05-11 20:56:31 -07001317 * Caller must hold session s_mutex.
Sage Weil2f2dc052009-10-06 11:31:09 -07001318 */
Jeff Laytonf5d772692019-04-24 12:09:04 -04001319int ceph_iterate_session_caps(struct ceph_mds_session *session,
1320 int (*cb)(struct inode *, struct ceph_cap *,
1321 void *), void *arg)
Sage Weil2f2dc052009-10-06 11:31:09 -07001322{
Sage Weil7c1332b2010-02-16 11:39:45 -08001323 struct list_head *p;
1324 struct ceph_cap *cap;
1325 struct inode *inode, *last_inode = NULL;
1326 struct ceph_cap *old_cap = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001327 int ret;
1328
1329 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
1330 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001331 p = session->s_caps.next;
1332 while (p != &session->s_caps) {
1333 cap = list_entry(p, struct ceph_cap, session_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -07001334 inode = igrab(&cap->ci->vfs_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001335 if (!inode) {
1336 p = p->next;
Sage Weil2f2dc052009-10-06 11:31:09 -07001337 continue;
Sage Weil7c1332b2010-02-16 11:39:45 -08001338 }
1339 session->s_cap_iterator = cap;
Sage Weil2f2dc052009-10-06 11:31:09 -07001340 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001341
1342 if (last_inode) {
Yan, Zheng3e1d0452019-05-18 20:39:55 +08001343 /* avoid calling iput_final() while holding
1344 * s_mutex or in mds dispatch threads */
1345 ceph_async_iput(last_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001346 last_inode = NULL;
1347 }
1348 if (old_cap) {
Yehuda Sadeh37151662010-06-17 16:16:12 -07001349 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -08001350 old_cap = NULL;
1351 }
1352
Sage Weil2f2dc052009-10-06 11:31:09 -07001353 ret = cb(inode, cap, arg);
Sage Weil7c1332b2010-02-16 11:39:45 -08001354 last_inode = inode;
1355
Sage Weil2f2dc052009-10-06 11:31:09 -07001356 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001357 p = p->next;
Markus Elfringd37b1d92017-08-20 20:22:02 +02001358 if (!cap->ci) {
Sage Weil7c1332b2010-02-16 11:39:45 -08001359 dout("iterate_session_caps finishing cap %p removal\n",
1360 cap);
1361 BUG_ON(cap->session != session);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001362 cap->session = NULL;
Sage Weil7c1332b2010-02-16 11:39:45 -08001363 list_del_init(&cap->session_caps);
1364 session->s_nr_caps--;
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08001365 if (cap->queue_release)
1366 __ceph_queue_cap_release(session, cap);
1367 else
Yan, Zheng745a8e32015-05-14 17:22:42 +08001368 old_cap = cap; /* put_cap it w/o locks held */
Sage Weil7c1332b2010-02-16 11:39:45 -08001369 }
Sage Weil5dacf092009-12-21 20:40:34 -08001370 if (ret < 0)
1371 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07001372 }
Sage Weil5dacf092009-12-21 20:40:34 -08001373 ret = 0;
1374out:
Sage Weil7c1332b2010-02-16 11:39:45 -08001375 session->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001376 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001377
Yan, Zheng3e1d0452019-05-18 20:39:55 +08001378 ceph_async_iput(last_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001379 if (old_cap)
Yehuda Sadeh37151662010-06-17 16:16:12 -07001380 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -08001381
Sage Weil5dacf092009-12-21 20:40:34 -08001382 return ret;
Sage Weil2f2dc052009-10-06 11:31:09 -07001383}
1384
1385static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
Sage Weil6c99f252010-05-10 16:12:25 -07001386 void *arg)
Sage Weil2f2dc052009-10-06 11:31:09 -07001387{
Yan, Zheng6c93df52016-04-15 13:56:12 +08001388 struct ceph_fs_client *fsc = (struct ceph_fs_client *)arg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001389 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001390 LIST_HEAD(to_remove);
Yan, Zhengf4b97862019-07-25 20:16:42 +08001391 bool dirty_dropped = false;
Yan, Zheng6c93df52016-04-15 13:56:12 +08001392 bool invalidate = false;
Sage Weil6c99f252010-05-10 16:12:25 -07001393
Sage Weil2f2dc052009-10-06 11:31:09 -07001394 dout("removing cap %p, ci is %p, inode is %p\n",
1395 cap, ci, &ci->vfs_inode);
Sage Weilbe655592011-11-30 09:47:09 -08001396 spin_lock(&ci->i_ceph_lock);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001397 if (cap->mds_wanted | cap->issued)
1398 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
Yan, Zhenga096b092013-09-22 10:15:58 +08001399 __ceph_remove_cap(cap, false);
Yan, Zheng571ade32015-03-24 11:36:08 +08001400 if (!ci->i_auth_cap) {
Yan, Zheng553adfd2015-06-09 15:48:57 +08001401 struct ceph_cap_flush *cf;
Yan, Zheng6c93df52016-04-15 13:56:12 +08001402 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil6c99f252010-05-10 16:12:25 -07001403
Yan, Zhengd468e722019-07-25 20:16:44 +08001404 if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
1405 if (inode->i_data.nrpages > 0)
1406 invalidate = true;
1407 if (ci->i_wrbuffer_ref > 0)
1408 mapping_set_error(&inode->i_data, -EIO);
1409 }
Yan, Zheng6c93df52016-04-15 13:56:12 +08001410
Yan, Zhenge4500b52016-07-06 11:12:56 +08001411 while (!list_empty(&ci->i_cap_flush_list)) {
1412 cf = list_first_entry(&ci->i_cap_flush_list,
1413 struct ceph_cap_flush, i_list);
Wei Yongjun8cdcc072016-08-13 09:10:28 +00001414 list_move(&cf->i_list, &to_remove);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001415 }
1416
Sage Weil6c99f252010-05-10 16:12:25 -07001417 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zheng8310b082015-06-09 17:20:12 +08001418
Yan, Zhenge4500b52016-07-06 11:12:56 +08001419 list_for_each_entry(cf, &to_remove, i_list)
1420 list_del(&cf->g_list);
Yan, Zheng8310b082015-06-09 17:20:12 +08001421
Sage Weil6c99f252010-05-10 16:12:25 -07001422 if (!list_empty(&ci->i_dirty_item)) {
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001423 pr_warn_ratelimited(
1424 " dropping dirty %s state for %p %lld\n",
Sage Weil6c99f252010-05-10 16:12:25 -07001425 ceph_cap_string(ci->i_dirty_caps),
1426 inode, ceph_ino(inode));
1427 ci->i_dirty_caps = 0;
1428 list_del_init(&ci->i_dirty_item);
Yan, Zhengf4b97862019-07-25 20:16:42 +08001429 dirty_dropped = true;
Sage Weil6c99f252010-05-10 16:12:25 -07001430 }
1431 if (!list_empty(&ci->i_flushing_item)) {
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001432 pr_warn_ratelimited(
1433 " dropping dirty+flushing %s state for %p %lld\n",
Sage Weil6c99f252010-05-10 16:12:25 -07001434 ceph_cap_string(ci->i_flushing_caps),
1435 inode, ceph_ino(inode));
1436 ci->i_flushing_caps = 0;
1437 list_del_init(&ci->i_flushing_item);
1438 mdsc->num_cap_flushing--;
Yan, Zhengf4b97862019-07-25 20:16:42 +08001439 dirty_dropped = true;
Sage Weil6c99f252010-05-10 16:12:25 -07001440 }
Sage Weil6c99f252010-05-10 16:12:25 -07001441 spin_unlock(&mdsc->cap_dirty_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001442
Yan, Zhengf4b97862019-07-25 20:16:42 +08001443 if (dirty_dropped) {
1444 errseq_set(&ci->i_meta_err, -EIO);
1445
1446 if (ci->i_wrbuffer_ref_head == 0 &&
1447 ci->i_wr_ref == 0 &&
1448 ci->i_dirty_caps == 0 &&
1449 ci->i_flushing_caps == 0) {
1450 ceph_put_snap_context(ci->i_head_snapc);
1451 ci->i_head_snapc = NULL;
1452 }
1453 }
1454
Yan, Zhengb3f8d682017-09-11 10:58:55 +08001455 if (atomic_read(&ci->i_filelock_ref) > 0) {
1456 /* make further file lock syscall return -EIO */
1457 ci->i_ceph_flags |= CEPH_I_ERROR_FILELOCK;
1458 pr_warn_ratelimited(" dropping file locks for %p %lld\n",
1459 inode, ceph_ino(inode));
1460 }
1461
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001462 if (!ci->i_dirty_caps && ci->i_prealloc_cap_flush) {
Yan, Zhenge4500b52016-07-06 11:12:56 +08001463 list_add(&ci->i_prealloc_cap_flush->i_list, &to_remove);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001464 ci->i_prealloc_cap_flush = NULL;
1465 }
Sage Weil6c99f252010-05-10 16:12:25 -07001466 }
Sage Weilbe655592011-11-30 09:47:09 -08001467 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001468 while (!list_empty(&to_remove)) {
1469 struct ceph_cap_flush *cf;
1470 cf = list_first_entry(&to_remove,
Yan, Zhenge4500b52016-07-06 11:12:56 +08001471 struct ceph_cap_flush, i_list);
1472 list_del(&cf->i_list);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001473 ceph_free_cap_flush(cf);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001474 }
Yan, Zheng77310322016-04-08 15:27:16 +08001475
1476 wake_up_all(&ci->i_cap_wq);
Yan, Zheng6c93df52016-04-15 13:56:12 +08001477 if (invalidate)
1478 ceph_queue_invalidate(inode);
Yan, Zhengf4b97862019-07-25 20:16:42 +08001479 if (dirty_dropped)
Sage Weil6c99f252010-05-10 16:12:25 -07001480 iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07001481 return 0;
1482}
1483
1484/*
1485 * caller must hold session s_mutex
1486 */
1487static void remove_session_caps(struct ceph_mds_session *session)
1488{
Yan, Zheng6c93df52016-04-15 13:56:12 +08001489 struct ceph_fs_client *fsc = session->s_mdsc->fsc;
1490 struct super_block *sb = fsc->sb;
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001491 LIST_HEAD(dispose);
1492
Sage Weil2f2dc052009-10-06 11:31:09 -07001493 dout("remove_session_caps on %p\n", session);
Jeff Laytonf5d772692019-04-24 12:09:04 -04001494 ceph_iterate_session_caps(session, remove_session_caps_cb, fsc);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001495
Yan, Zhengc8799fc2016-07-07 15:22:38 +08001496 wake_up_all(&fsc->mdsc->cap_flushing_wq);
1497
Yan, Zheng6f60f882013-07-24 12:22:11 +08001498 spin_lock(&session->s_cap_lock);
1499 if (session->s_nr_caps > 0) {
Yan, Zheng6f60f882013-07-24 12:22:11 +08001500 struct inode *inode;
1501 struct ceph_cap *cap, *prev = NULL;
1502 struct ceph_vino vino;
1503 /*
1504 * iterate_session_caps() skips inodes that are being
1505 * deleted, we need to wait until deletions are complete.
1506 * __wait_on_freeing_inode() is designed for the job,
1507 * but it is not exported, so use lookup inode function
1508 * to access it.
1509 */
1510 while (!list_empty(&session->s_caps)) {
1511 cap = list_entry(session->s_caps.next,
1512 struct ceph_cap, session_caps);
1513 if (cap == prev)
1514 break;
1515 prev = cap;
1516 vino = cap->ci->i_vino;
1517 spin_unlock(&session->s_cap_lock);
1518
Yan, Zhenged284c42013-09-02 15:19:53 +08001519 inode = ceph_find_inode(sb, vino);
Yan, Zheng3e1d0452019-05-18 20:39:55 +08001520 /* avoid calling iput_final() while holding s_mutex */
1521 ceph_async_iput(inode);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001522
1523 spin_lock(&session->s_cap_lock);
1524 }
1525 }
Yan, Zheng745a8e32015-05-14 17:22:42 +08001526
1527 // drop cap expires and unlock s_cap_lock
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001528 detach_cap_releases(session, &dispose);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001529
Sage Weil2f2dc052009-10-06 11:31:09 -07001530 BUG_ON(session->s_nr_caps > 0);
Sage Weil6c99f252010-05-10 16:12:25 -07001531 BUG_ON(!list_empty(&session->s_cap_flushing));
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001532 spin_unlock(&session->s_cap_lock);
1533 dispose_cap_releases(session->s_mdsc, &dispose);
Sage Weil2f2dc052009-10-06 11:31:09 -07001534}
1535
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001536enum {
1537 RECONNECT,
1538 RENEWCAPS,
1539 FORCE_RO,
1540};
1541
Sage Weil2f2dc052009-10-06 11:31:09 -07001542/*
1543 * wake up any threads waiting on this session's caps. if the cap is
1544 * old (didn't get renewed on the client reconnect), remove it now.
1545 *
1546 * caller must hold s_mutex.
1547 */
1548static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1549 void *arg)
1550{
Sage Weil0dc25702009-11-20 13:43:45 -08001551 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001552 unsigned long ev = (unsigned long)arg;
Sage Weil0dc25702009-11-20 13:43:45 -08001553
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001554 if (ev == RECONNECT) {
Sage Weilbe655592011-11-30 09:47:09 -08001555 spin_lock(&ci->i_ceph_lock);
Sage Weil0dc25702009-11-20 13:43:45 -08001556 ci->i_wanted_max_size = 0;
1557 ci->i_requested_max_size = 0;
Sage Weilbe655592011-11-30 09:47:09 -08001558 spin_unlock(&ci->i_ceph_lock);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001559 } else if (ev == RENEWCAPS) {
1560 if (cap->cap_gen < cap->session->s_cap_gen) {
1561 /* mds did not re-issue stale cap */
1562 spin_lock(&ci->i_ceph_lock);
1563 cap->issued = cap->implemented = CEPH_CAP_PIN;
1564 /* make sure mds knows what we want */
1565 if (__ceph_caps_file_wanted(ci) & ~cap->mds_wanted)
1566 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
1567 spin_unlock(&ci->i_ceph_lock);
1568 }
1569 } else if (ev == FORCE_RO) {
Sage Weil0dc25702009-11-20 13:43:45 -08001570 }
Yan, Zhenge5360302016-05-19 19:15:19 +08001571 wake_up_all(&ci->i_cap_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07001572 return 0;
1573}
1574
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001575static void wake_up_session_caps(struct ceph_mds_session *session, int ev)
Sage Weil2f2dc052009-10-06 11:31:09 -07001576{
1577 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
Jeff Laytonf5d772692019-04-24 12:09:04 -04001578 ceph_iterate_session_caps(session, wake_up_session_cb,
1579 (void *)(unsigned long)ev);
Sage Weil2f2dc052009-10-06 11:31:09 -07001580}
1581
1582/*
1583 * Send periodic message to MDS renewing all currently held caps. The
1584 * ack will reset the expiration for all caps from this session.
1585 *
1586 * caller holds s_mutex
1587 */
1588static int send_renew_caps(struct ceph_mds_client *mdsc,
1589 struct ceph_mds_session *session)
1590{
1591 struct ceph_msg *msg;
1592 int state;
1593
1594 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1595 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1596 pr_info("mds%d caps stale\n", session->s_mds);
Sage Weile4cb4cb2010-03-18 13:43:09 -07001597 session->s_renew_requested = jiffies;
Sage Weil2f2dc052009-10-06 11:31:09 -07001598
1599 /* do not try to renew caps until a recovering mds has reconnected
1600 * with its clients. */
1601 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1602 if (state < CEPH_MDS_STATE_RECONNECT) {
1603 dout("send_renew_caps ignoring mds%d (%s)\n",
1604 session->s_mds, ceph_mds_state_name(state));
1605 return 0;
1606 }
1607
1608 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1609 ceph_mds_state_name(state));
Sage Weil2f2dc052009-10-06 11:31:09 -07001610 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1611 ++session->s_renew_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001612 if (!msg)
1613 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -07001614 ceph_con_send(&session->s_con, msg);
1615 return 0;
1616}
1617
Yan, Zheng186e4f72013-11-22 14:48:37 +08001618static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
1619 struct ceph_mds_session *session, u64 seq)
1620{
1621 struct ceph_msg *msg;
1622
1623 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
John Spraya687eca2014-09-19 13:51:08 +01001624 session->s_mds, ceph_session_state_name(session->s_state), seq);
Yan, Zheng186e4f72013-11-22 14:48:37 +08001625 msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
1626 if (!msg)
1627 return -ENOMEM;
1628 ceph_con_send(&session->s_con, msg);
1629 return 0;
1630}
1631
1632
Sage Weil2f2dc052009-10-06 11:31:09 -07001633/*
1634 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
Sage Weil0dc25702009-11-20 13:43:45 -08001635 *
1636 * Called under session->s_mutex
Sage Weil2f2dc052009-10-06 11:31:09 -07001637 */
1638static void renewed_caps(struct ceph_mds_client *mdsc,
1639 struct ceph_mds_session *session, int is_renew)
1640{
1641 int was_stale;
1642 int wake = 0;
1643
1644 spin_lock(&session->s_cap_lock);
Alex Elder1ce208a2012-01-12 17:48:11 -08001645 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
Sage Weil2f2dc052009-10-06 11:31:09 -07001646
1647 session->s_cap_ttl = session->s_renew_requested +
1648 mdsc->mdsmap->m_session_timeout*HZ;
1649
1650 if (was_stale) {
1651 if (time_before(jiffies, session->s_cap_ttl)) {
1652 pr_info("mds%d caps renewed\n", session->s_mds);
1653 wake = 1;
1654 } else {
1655 pr_info("mds%d caps still stale\n", session->s_mds);
1656 }
1657 }
1658 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1659 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1660 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1661 spin_unlock(&session->s_cap_lock);
1662
1663 if (wake)
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001664 wake_up_session_caps(session, RENEWCAPS);
Sage Weil2f2dc052009-10-06 11:31:09 -07001665}
1666
1667/*
1668 * send a session close request
1669 */
1670static int request_close_session(struct ceph_mds_client *mdsc,
1671 struct ceph_mds_session *session)
1672{
1673 struct ceph_msg *msg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001674
1675 dout("request_close_session mds%d state %s seq %lld\n",
John Spraya687eca2014-09-19 13:51:08 +01001676 session->s_mds, ceph_session_state_name(session->s_state),
Sage Weil2f2dc052009-10-06 11:31:09 -07001677 session->s_seq);
1678 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001679 if (!msg)
1680 return -ENOMEM;
1681 ceph_con_send(&session->s_con, msg);
Yan, Zhengfcff4152016-09-14 16:39:51 +08001682 return 1;
Sage Weil2f2dc052009-10-06 11:31:09 -07001683}
1684
1685/*
1686 * Called with s_mutex held.
1687 */
1688static int __close_session(struct ceph_mds_client *mdsc,
1689 struct ceph_mds_session *session)
1690{
1691 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1692 return 0;
1693 session->s_state = CEPH_MDS_SESSION_CLOSING;
1694 return request_close_session(mdsc, session);
1695}
1696
Yan, Zheng040d7862017-11-30 11:59:22 +08001697static bool drop_negative_children(struct dentry *dentry)
1698{
1699 struct dentry *child;
1700 bool all_negative = true;
1701
1702 if (!d_is_dir(dentry))
1703 goto out;
1704
1705 spin_lock(&dentry->d_lock);
1706 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
1707 if (d_really_is_positive(child)) {
1708 all_negative = false;
1709 break;
1710 }
1711 }
1712 spin_unlock(&dentry->d_lock);
1713
1714 if (all_negative)
1715 shrink_dcache_parent(dentry);
1716out:
1717 return all_negative;
1718}
1719
Sage Weil2f2dc052009-10-06 11:31:09 -07001720/*
1721 * Trim old(er) caps.
1722 *
1723 * Because we can't cache an inode without one or more caps, we do
1724 * this indirectly: if a cap is unused, we prune its aliases, at which
1725 * point the inode will hopefully get dropped to.
1726 *
1727 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1728 * memory pressure from the MDS, though, so it needn't be perfect.
1729 */
1730static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1731{
Jeff Layton533a2812019-07-19 15:22:28 -04001732 int *remaining = arg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001733 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng979abfd2013-11-22 13:56:24 +08001734 int used, wanted, oissued, mine;
Sage Weil2f2dc052009-10-06 11:31:09 -07001735
Jeff Layton533a2812019-07-19 15:22:28 -04001736 if (*remaining <= 0)
Sage Weil2f2dc052009-10-06 11:31:09 -07001737 return -1;
1738
Sage Weilbe655592011-11-30 09:47:09 -08001739 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001740 mine = cap->issued | cap->implemented;
1741 used = __ceph_caps_used(ci);
Yan, Zheng979abfd2013-11-22 13:56:24 +08001742 wanted = __ceph_caps_file_wanted(ci);
Sage Weil2f2dc052009-10-06 11:31:09 -07001743 oissued = __ceph_caps_issued_other(ci, cap);
1744
Yan, Zheng979abfd2013-11-22 13:56:24 +08001745 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001746 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
Yan, Zheng979abfd2013-11-22 13:56:24 +08001747 ceph_cap_string(used), ceph_cap_string(wanted));
1748 if (cap == ci->i_auth_cap) {
Yan, Zheng622f3e22015-05-07 10:59:47 +08001749 if (ci->i_dirty_caps || ci->i_flushing_caps ||
1750 !list_empty(&ci->i_cap_snaps))
Yan, Zheng979abfd2013-11-22 13:56:24 +08001751 goto out;
1752 if ((used | wanted) & CEPH_CAP_ANY_WR)
1753 goto out;
Yan, Zheng89aa5932017-09-08 15:23:18 +08001754 /* Note: it's possible that i_filelock_ref becomes non-zero
1755 * after dropping auth caps. It doesn't hurt because reply
1756 * of lock mds request will re-add auth caps. */
1757 if (atomic_read(&ci->i_filelock_ref) > 0)
1758 goto out;
Yan, Zheng979abfd2013-11-22 13:56:24 +08001759 }
Yan, Zheng5e804ac2015-10-26 16:08:43 +08001760 /* The inode has cached pages, but it's no longer used.
1761 * we can safely drop it */
1762 if (wanted == 0 && used == CEPH_CAP_FILE_CACHE &&
1763 !(oissued & CEPH_CAP_FILE_CACHE)) {
1764 used = 0;
1765 oissued = 0;
1766 }
Yan, Zheng979abfd2013-11-22 13:56:24 +08001767 if ((used | wanted) & ~oissued & mine)
Sage Weil2f2dc052009-10-06 11:31:09 -07001768 goto out; /* we need these caps */
1769
Sage Weil2f2dc052009-10-06 11:31:09 -07001770 if (oissued) {
1771 /* we aren't the only cap.. just remove us */
Yan, Zhenga096b092013-09-22 10:15:58 +08001772 __ceph_remove_cap(cap, true);
Jeff Layton533a2812019-07-19 15:22:28 -04001773 (*remaining)--;
Sage Weil2f2dc052009-10-06 11:31:09 -07001774 } else {
Yan, Zheng040d7862017-11-30 11:59:22 +08001775 struct dentry *dentry;
Yan, Zheng5e804ac2015-10-26 16:08:43 +08001776 /* try dropping referring dentries */
Sage Weilbe655592011-11-30 09:47:09 -08001777 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng040d7862017-11-30 11:59:22 +08001778 dentry = d_find_any_alias(inode);
1779 if (dentry && drop_negative_children(dentry)) {
1780 int count;
1781 dput(dentry);
1782 d_prune_aliases(inode);
1783 count = atomic_read(&inode->i_count);
1784 if (count == 1)
Jeff Layton533a2812019-07-19 15:22:28 -04001785 (*remaining)--;
Yan, Zheng040d7862017-11-30 11:59:22 +08001786 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1787 inode, cap, count);
1788 } else {
1789 dput(dentry);
1790 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001791 return 0;
1792 }
1793
1794out:
Sage Weilbe655592011-11-30 09:47:09 -08001795 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001796 return 0;
1797}
1798
1799/*
1800 * Trim session cap count down to some max number.
1801 */
Zhi Zhange30ee582018-01-24 21:24:33 +08001802int ceph_trim_caps(struct ceph_mds_client *mdsc,
1803 struct ceph_mds_session *session,
1804 int max_caps)
Sage Weil2f2dc052009-10-06 11:31:09 -07001805{
1806 int trim_caps = session->s_nr_caps - max_caps;
1807
1808 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1809 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1810 if (trim_caps > 0) {
Jeff Layton533a2812019-07-19 15:22:28 -04001811 int remaining = trim_caps;
1812
1813 ceph_iterate_session_caps(session, trim_caps_cb, &remaining);
Sage Weil2f2dc052009-10-06 11:31:09 -07001814 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1815 session->s_mds, session->s_nr_caps, max_caps,
Jeff Layton533a2812019-07-19 15:22:28 -04001816 trim_caps - remaining);
Sage Weil2f2dc052009-10-06 11:31:09 -07001817 }
Yan, Zhenga56371d2014-04-01 20:34:56 +08001818
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08001819 ceph_flush_cap_releases(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07001820 return 0;
1821}
1822
Yan, Zheng8310b082015-06-09 17:20:12 +08001823static int check_caps_flush(struct ceph_mds_client *mdsc,
1824 u64 want_flush_tid)
1825{
Yan, Zheng8310b082015-06-09 17:20:12 +08001826 int ret = 1;
1827
1828 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zhenge4500b52016-07-06 11:12:56 +08001829 if (!list_empty(&mdsc->cap_flush_list)) {
1830 struct ceph_cap_flush *cf =
1831 list_first_entry(&mdsc->cap_flush_list,
1832 struct ceph_cap_flush, g_list);
1833 if (cf->tid <= want_flush_tid) {
1834 dout("check_caps_flush still flushing tid "
1835 "%llu <= %llu\n", cf->tid, want_flush_tid);
1836 ret = 0;
1837 }
Yan, Zheng8310b082015-06-09 17:20:12 +08001838 }
1839 spin_unlock(&mdsc->cap_dirty_lock);
1840 return ret;
Yan, Zhengd3383a82015-01-08 21:30:12 +08001841}
1842
Sage Weil2f2dc052009-10-06 11:31:09 -07001843/*
1844 * flush all dirty inode data to disk.
1845 *
Yan, Zheng8310b082015-06-09 17:20:12 +08001846 * returns true if we've flushed through want_flush_tid
Sage Weil2f2dc052009-10-06 11:31:09 -07001847 */
Yan, Zhengaffbc192015-05-05 21:22:13 +08001848static void wait_caps_flush(struct ceph_mds_client *mdsc,
Yan, Zheng0e294382016-07-04 18:06:41 +08001849 u64 want_flush_tid)
Sage Weil2f2dc052009-10-06 11:31:09 -07001850{
Yan, Zheng0e294382016-07-04 18:06:41 +08001851 dout("check_caps_flush want %llu\n", want_flush_tid);
Yan, Zheng8310b082015-06-09 17:20:12 +08001852
1853 wait_event(mdsc->cap_flushing_wq,
1854 check_caps_flush(mdsc, want_flush_tid));
1855
1856 dout("check_caps_flush ok, flushed thru %llu\n", want_flush_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07001857}
1858
1859/*
1860 * called under s_mutex
1861 */
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08001862static void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1863 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001864{
Yan, Zheng745a8e32015-05-14 17:22:42 +08001865 struct ceph_msg *msg = NULL;
1866 struct ceph_mds_cap_release *head;
1867 struct ceph_mds_cap_item *item;
Jeff Layton92475f02017-04-13 11:07:04 -04001868 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
Yan, Zheng745a8e32015-05-14 17:22:42 +08001869 struct ceph_cap *cap;
1870 LIST_HEAD(tmp_list);
1871 int num_cap_releases;
Jeff Layton92475f02017-04-13 11:07:04 -04001872 __le32 barrier, *cap_barrier;
1873
1874 down_read(&osdc->lock);
1875 barrier = cpu_to_le32(osdc->epoch_barrier);
1876 up_read(&osdc->lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001877
Sage Weil0f8605f2010-05-05 15:51:35 -07001878 spin_lock(&session->s_cap_lock);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001879again:
1880 list_splice_init(&session->s_cap_releases, &tmp_list);
1881 num_cap_releases = session->s_num_cap_releases;
1882 session->s_num_cap_releases = 0;
1883 spin_unlock(&session->s_cap_lock);
1884
1885 while (!list_empty(&tmp_list)) {
1886 if (!msg) {
1887 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001888 PAGE_SIZE, GFP_NOFS, false);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001889 if (!msg)
1890 goto out_err;
1891 head = msg->front.iov_base;
1892 head->num = cpu_to_le32(0);
1893 msg->front.iov_len = sizeof(*head);
Jeff Layton92475f02017-04-13 11:07:04 -04001894
1895 msg->hdr.version = cpu_to_le16(2);
1896 msg->hdr.compat_version = cpu_to_le16(1);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001897 }
Jeff Layton92475f02017-04-13 11:07:04 -04001898
Yan, Zheng745a8e32015-05-14 17:22:42 +08001899 cap = list_first_entry(&tmp_list, struct ceph_cap,
1900 session_caps);
1901 list_del(&cap->session_caps);
1902 num_cap_releases--;
1903
1904 head = msg->front.iov_base;
Jeff Layton4198aba2019-05-02 08:06:50 -04001905 put_unaligned_le32(get_unaligned_le32(&head->num) + 1,
1906 &head->num);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001907 item = msg->front.iov_base + msg->front.iov_len;
1908 item->ino = cpu_to_le64(cap->cap_ino);
1909 item->cap_id = cpu_to_le64(cap->cap_id);
1910 item->migrate_seq = cpu_to_le32(cap->mseq);
1911 item->seq = cpu_to_le32(cap->issue_seq);
1912 msg->front.iov_len += sizeof(*item);
1913
1914 ceph_put_cap(mdsc, cap);
1915
1916 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
Jeff Layton92475f02017-04-13 11:07:04 -04001917 // Append cap_barrier field
1918 cap_barrier = msg->front.iov_base + msg->front.iov_len;
1919 *cap_barrier = barrier;
1920 msg->front.iov_len += sizeof(*cap_barrier);
1921
Yan, Zheng745a8e32015-05-14 17:22:42 +08001922 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1923 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1924 ceph_con_send(&session->s_con, msg);
1925 msg = NULL;
1926 }
1927 }
1928
1929 BUG_ON(num_cap_releases != 0);
1930
1931 spin_lock(&session->s_cap_lock);
1932 if (!list_empty(&session->s_cap_releases))
1933 goto again;
1934 spin_unlock(&session->s_cap_lock);
1935
1936 if (msg) {
Jeff Layton92475f02017-04-13 11:07:04 -04001937 // Append cap_barrier field
1938 cap_barrier = msg->front.iov_base + msg->front.iov_len;
1939 *cap_barrier = barrier;
1940 msg->front.iov_len += sizeof(*cap_barrier);
1941
Sage Weil2f2dc052009-10-06 11:31:09 -07001942 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1943 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1944 ceph_con_send(&session->s_con, msg);
1945 }
Yan, Zheng745a8e32015-05-14 17:22:42 +08001946 return;
1947out_err:
1948 pr_err("send_cap_releases mds%d, failed to allocate message\n",
1949 session->s_mds);
1950 spin_lock(&session->s_cap_lock);
1951 list_splice(&tmp_list, &session->s_cap_releases);
1952 session->s_num_cap_releases += num_cap_releases;
Sage Weil2f2dc052009-10-06 11:31:09 -07001953 spin_unlock(&session->s_cap_lock);
1954}
1955
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08001956static void ceph_cap_release_work(struct work_struct *work)
1957{
1958 struct ceph_mds_session *session =
1959 container_of(work, struct ceph_mds_session, s_cap_release_work);
1960
1961 mutex_lock(&session->s_mutex);
1962 if (session->s_state == CEPH_MDS_SESSION_OPEN ||
1963 session->s_state == CEPH_MDS_SESSION_HUNG)
1964 ceph_send_cap_releases(session->s_mdsc, session);
1965 mutex_unlock(&session->s_mutex);
1966 ceph_put_mds_session(session);
1967}
1968
1969void ceph_flush_cap_releases(struct ceph_mds_client *mdsc,
1970 struct ceph_mds_session *session)
1971{
1972 if (mdsc->stopping)
1973 return;
1974
1975 get_session(session);
1976 if (queue_work(mdsc->fsc->cap_wq,
1977 &session->s_cap_release_work)) {
1978 dout("cap release work queued\n");
1979 } else {
1980 ceph_put_mds_session(session);
1981 dout("failed to queue cap release work\n");
1982 }
1983}
1984
1985/*
1986 * caller holds session->s_cap_lock
1987 */
1988void __ceph_queue_cap_release(struct ceph_mds_session *session,
1989 struct ceph_cap *cap)
1990{
1991 list_add_tail(&cap->session_caps, &session->s_cap_releases);
1992 session->s_num_cap_releases++;
1993
1994 if (!(session->s_num_cap_releases % CEPH_CAPS_PER_RELEASE))
1995 ceph_flush_cap_releases(session->s_mdsc, session);
1996}
1997
Yan, Zheng37c4efc2019-01-31 16:55:51 +08001998static void ceph_cap_reclaim_work(struct work_struct *work)
1999{
2000 struct ceph_mds_client *mdsc =
2001 container_of(work, struct ceph_mds_client, cap_reclaim_work);
2002 int ret = ceph_trim_dentries(mdsc);
2003 if (ret == -EAGAIN)
2004 ceph_queue_cap_reclaim_work(mdsc);
2005}
2006
2007void ceph_queue_cap_reclaim_work(struct ceph_mds_client *mdsc)
2008{
2009 if (mdsc->stopping)
2010 return;
2011
2012 if (queue_work(mdsc->fsc->cap_wq, &mdsc->cap_reclaim_work)) {
2013 dout("caps reclaim work queued\n");
2014 } else {
2015 dout("failed to queue caps release work\n");
2016 }
2017}
2018
Yan, Zhengfe330322019-02-01 14:57:15 +08002019void ceph_reclaim_caps_nr(struct ceph_mds_client *mdsc, int nr)
2020{
2021 int val;
2022 if (!nr)
2023 return;
2024 val = atomic_add_return(nr, &mdsc->cap_reclaim_pending);
Xiubo Libba15602019-11-26 07:32:22 -05002025 if ((val % CEPH_CAPS_PER_RELEASE) < nr) {
Yan, Zhengfe330322019-02-01 14:57:15 +08002026 atomic_set(&mdsc->cap_reclaim_pending, 0);
2027 ceph_queue_cap_reclaim_work(mdsc);
2028 }
2029}
2030
Sage Weil2f2dc052009-10-06 11:31:09 -07002031/*
2032 * requests
2033 */
2034
Yan, Zheng54008392014-03-29 13:41:15 +08002035int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
2036 struct inode *dir)
2037{
2038 struct ceph_inode_info *ci = ceph_inode(dir);
2039 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
2040 struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options;
Yan, Zheng2a5beea2016-04-28 09:37:39 +08002041 size_t size = sizeof(struct ceph_mds_reply_dir_entry);
Jeff Laytonad8c28a2019-09-09 15:58:55 -04002042 unsigned int num_entries;
2043 int order;
Yan, Zheng54008392014-03-29 13:41:15 +08002044
2045 spin_lock(&ci->i_ceph_lock);
2046 num_entries = ci->i_files + ci->i_subdirs;
2047 spin_unlock(&ci->i_ceph_lock);
Jeff Laytonad8c28a2019-09-09 15:58:55 -04002048 num_entries = max(num_entries, 1U);
Yan, Zheng54008392014-03-29 13:41:15 +08002049 num_entries = min(num_entries, opt->max_readdir);
2050
2051 order = get_order(size * num_entries);
2052 while (order >= 0) {
Yan, Zheng2a5beea2016-04-28 09:37:39 +08002053 rinfo->dir_entries = (void*)__get_free_pages(GFP_KERNEL |
2054 __GFP_NOWARN,
2055 order);
2056 if (rinfo->dir_entries)
Yan, Zheng54008392014-03-29 13:41:15 +08002057 break;
2058 order--;
2059 }
Yan, Zheng2a5beea2016-04-28 09:37:39 +08002060 if (!rinfo->dir_entries)
Yan, Zheng54008392014-03-29 13:41:15 +08002061 return -ENOMEM;
2062
2063 num_entries = (PAGE_SIZE << order) / size;
2064 num_entries = min(num_entries, opt->max_readdir);
2065
2066 rinfo->dir_buf_size = PAGE_SIZE << order;
2067 req->r_num_caps = num_entries + 1;
2068 req->r_args.readdir.max_entries = cpu_to_le32(num_entries);
2069 req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes);
2070 return 0;
2071}
2072
Sage Weil2f2dc052009-10-06 11:31:09 -07002073/*
2074 * Create an mds request.
2075 */
2076struct ceph_mds_request *
2077ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
2078{
2079 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
Arnd Bergmann63ecae72018-07-13 22:18:35 +02002080 struct timespec64 ts;
Sage Weil2f2dc052009-10-06 11:31:09 -07002081
2082 if (!req)
2083 return ERR_PTR(-ENOMEM);
2084
Sage Weilb4556392010-05-13 12:01:13 -07002085 mutex_init(&req->r_fill_mutex);
Yehuda Sadeh37151662010-06-17 16:16:12 -07002086 req->r_mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07002087 req->r_started = jiffies;
2088 req->r_resend_mds = -1;
2089 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002090 INIT_LIST_HEAD(&req->r_unsafe_target_item);
Sage Weil2f2dc052009-10-06 11:31:09 -07002091 req->r_fmode = -1;
Sage Weil153c8e62009-12-07 12:31:09 -08002092 kref_init(&req->r_kref);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02002093 RB_CLEAR_NODE(&req->r_node);
Sage Weil2f2dc052009-10-06 11:31:09 -07002094 INIT_LIST_HEAD(&req->r_wait);
2095 init_completion(&req->r_completion);
2096 init_completion(&req->r_safe_completion);
2097 INIT_LIST_HEAD(&req->r_unsafe_item);
2098
Arnd Bergmann63ecae72018-07-13 22:18:35 +02002099 ktime_get_coarse_real_ts64(&ts);
Arnd Bergmann0ed1e902018-07-13 22:18:38 +02002100 req->r_stamp = timespec64_trunc(ts, mdsc->fsc->sb->s_time_gran);
Sage Weilb8e69062014-05-21 17:41:08 -07002101
Sage Weil2f2dc052009-10-06 11:31:09 -07002102 req->r_op = op;
2103 req->r_direct_mode = mode;
2104 return req;
2105}
2106
2107/*
Sage Weil44ca18f2010-02-15 12:08:46 -08002108 * return oldest (lowest) request, tid in request tree, 0 if none.
Sage Weil2f2dc052009-10-06 11:31:09 -07002109 *
2110 * called under mdsc->mutex.
2111 */
Sage Weil44ca18f2010-02-15 12:08:46 -08002112static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
2113{
2114 if (RB_EMPTY_ROOT(&mdsc->request_tree))
2115 return NULL;
2116 return rb_entry(rb_first(&mdsc->request_tree),
2117 struct ceph_mds_request, r_node);
2118}
2119
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08002120static inline u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07002121{
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08002122 return mdsc->oldest_tid;
Sage Weil2f2dc052009-10-06 11:31:09 -07002123}
2124
2125/*
2126 * Build a dentry's path. Allocate on heap; caller must kfree. Based
2127 * on build_path_from_dentry in fs/cifs/dir.c.
2128 *
2129 * If @stop_on_nosnap, generate path relative to the first non-snapped
2130 * inode.
2131 *
2132 * Encode hidden .snap dirs as a double /, i.e.
2133 * foo/.snap/bar -> foo//bar
2134 */
Jeff Layton69a10fb2019-04-26 13:33:39 -04002135char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase,
Sage Weil2f2dc052009-10-06 11:31:09 -07002136 int stop_on_nosnap)
2137{
2138 struct dentry *temp;
2139 char *path;
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002140 int pos;
Al Viro1b71fe22011-07-16 23:43:58 -04002141 unsigned seq;
Jeff Layton69a10fb2019-04-26 13:33:39 -04002142 u64 base;
Sage Weil2f2dc052009-10-06 11:31:09 -07002143
Markus Elfringd37b1d92017-08-20 20:22:02 +02002144 if (!dentry)
Sage Weil2f2dc052009-10-06 11:31:09 -07002145 return ERR_PTR(-EINVAL);
2146
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002147 path = __getname();
Markus Elfringd37b1d92017-08-20 20:22:02 +02002148 if (!path)
Sage Weil2f2dc052009-10-06 11:31:09 -07002149 return ERR_PTR(-ENOMEM);
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002150retry:
2151 pos = PATH_MAX - 1;
2152 path[pos] = '\0';
2153
2154 seq = read_seqbegin(&rename_lock);
Al Viro1b71fe22011-07-16 23:43:58 -04002155 rcu_read_lock();
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002156 temp = dentry;
2157 for (;;) {
Al Viro1b71fe22011-07-16 23:43:58 -04002158 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07002159
Al Viro1b71fe22011-07-16 23:43:58 -04002160 spin_lock(&temp->d_lock);
David Howells2b0143b2015-03-17 22:25:59 +00002161 inode = d_inode(temp);
Sage Weil2f2dc052009-10-06 11:31:09 -07002162 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
Sage Weil104648a2010-03-18 10:14:30 -07002163 dout("build_path path+%d: %p SNAPDIR\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002164 pos, temp);
Jeff Laytond6b8bd62019-05-09 07:58:38 -04002165 } else if (stop_on_nosnap && inode && dentry != temp &&
Sage Weil2f2dc052009-10-06 11:31:09 -07002166 ceph_snap(inode) == CEPH_NOSNAP) {
Yehuda Sadeh9d5a09e2011-12-13 09:57:44 -08002167 spin_unlock(&temp->d_lock);
Jeff Laytond6b8bd62019-05-09 07:58:38 -04002168 pos++; /* get rid of any prepended '/' */
Sage Weil2f2dc052009-10-06 11:31:09 -07002169 break;
2170 } else {
2171 pos -= temp->d_name.len;
Al Viro1b71fe22011-07-16 23:43:58 -04002172 if (pos < 0) {
2173 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002174 break;
Al Viro1b71fe22011-07-16 23:43:58 -04002175 }
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002176 memcpy(path + pos, temp->d_name.name, temp->d_name.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07002177 }
Al Viro1b71fe22011-07-16 23:43:58 -04002178 spin_unlock(&temp->d_lock);
Yan, Zheng41883ba2019-05-23 10:22:55 +08002179 temp = READ_ONCE(temp->d_parent);
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002180
2181 /* Are we at the root? */
2182 if (IS_ROOT(temp))
2183 break;
2184
2185 /* Are we out of buffer? */
2186 if (--pos < 0)
2187 break;
2188
2189 path[pos] = '/';
Sage Weil2f2dc052009-10-06 11:31:09 -07002190 }
Jeff Layton69a10fb2019-04-26 13:33:39 -04002191 base = ceph_ino(d_inode(temp));
Al Viro1b71fe22011-07-16 23:43:58 -04002192 rcu_read_unlock();
Jeff Laytonf5946bc2019-10-16 08:20:17 -04002193
2194 if (read_seqretry(&rename_lock, seq))
2195 goto retry;
2196
2197 if (pos < 0) {
2198 /*
2199 * A rename didn't occur, but somehow we didn't end up where
2200 * we thought we would. Throw a warning and try again.
2201 */
2202 pr_warn("build_path did not end path lookup where "
2203 "expected, pos is %d\n", pos);
Sage Weil2f2dc052009-10-06 11:31:09 -07002204 goto retry;
2205 }
2206
Jeff Layton69a10fb2019-04-26 13:33:39 -04002207 *pbase = base;
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002208 *plen = PATH_MAX - 1 - pos;
Sage Weil104648a2010-03-18 10:14:30 -07002209 dout("build_path on %p %d built %llx '%.*s'\n",
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002210 dentry, d_count(dentry), base, *plen, path + pos);
2211 return path + pos;
Sage Weil2f2dc052009-10-06 11:31:09 -07002212}
2213
Jeff Laytonfd36a712016-12-15 08:37:58 -05002214static int build_dentry_path(struct dentry *dentry, struct inode *dir,
Sage Weil2f2dc052009-10-06 11:31:09 -07002215 const char **ppath, int *ppathlen, u64 *pino,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002216 bool *pfreepath, bool parent_locked)
Sage Weil2f2dc052009-10-06 11:31:09 -07002217{
2218 char *path;
2219
Jeff Laytonc6b0b652016-12-15 08:37:57 -05002220 rcu_read_lock();
Jeff Laytonfd36a712016-12-15 08:37:58 -05002221 if (!dir)
2222 dir = d_inode_rcu(dentry->d_parent);
Jeff Layton964fff72019-04-29 11:51:02 -04002223 if (dir && parent_locked && ceph_snap(dir) == CEPH_NOSNAP) {
Jeff Laytonc6b0b652016-12-15 08:37:57 -05002224 *pino = ceph_ino(dir);
2225 rcu_read_unlock();
Jeff Layton964fff72019-04-29 11:51:02 -04002226 *ppath = dentry->d_name.name;
2227 *ppathlen = dentry->d_name.len;
Sage Weil2f2dc052009-10-06 11:31:09 -07002228 return 0;
2229 }
Jeff Laytonc6b0b652016-12-15 08:37:57 -05002230 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -07002231 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
2232 if (IS_ERR(path))
2233 return PTR_ERR(path);
2234 *ppath = path;
Jeff Layton1bcb3442019-04-15 12:00:42 -04002235 *pfreepath = true;
Sage Weil2f2dc052009-10-06 11:31:09 -07002236 return 0;
2237}
2238
2239static int build_inode_path(struct inode *inode,
2240 const char **ppath, int *ppathlen, u64 *pino,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002241 bool *pfreepath)
Sage Weil2f2dc052009-10-06 11:31:09 -07002242{
2243 struct dentry *dentry;
2244 char *path;
2245
2246 if (ceph_snap(inode) == CEPH_NOSNAP) {
2247 *pino = ceph_ino(inode);
2248 *ppathlen = 0;
2249 return 0;
2250 }
2251 dentry = d_find_alias(inode);
2252 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
2253 dput(dentry);
2254 if (IS_ERR(path))
2255 return PTR_ERR(path);
2256 *ppath = path;
Jeff Layton1bcb3442019-04-15 12:00:42 -04002257 *pfreepath = true;
Sage Weil2f2dc052009-10-06 11:31:09 -07002258 return 0;
2259}
2260
2261/*
2262 * request arguments may be specified via an inode *, a dentry *, or
2263 * an explicit ino+path.
2264 */
2265static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
Jeff Laytonfd36a712016-12-15 08:37:58 -05002266 struct inode *rdiri, const char *rpath,
2267 u64 rino, const char **ppath, int *pathlen,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002268 u64 *ino, bool *freepath, bool parent_locked)
Sage Weil2f2dc052009-10-06 11:31:09 -07002269{
2270 int r = 0;
2271
2272 if (rinode) {
2273 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
2274 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
2275 ceph_snap(rinode));
2276 } else if (rdentry) {
Jeff Laytonfd36a712016-12-15 08:37:58 -05002277 r = build_dentry_path(rdentry, rdiri, ppath, pathlen, ino,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002278 freepath, parent_locked);
Sage Weil2f2dc052009-10-06 11:31:09 -07002279 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
2280 *ppath);
Sage Weil795858d2011-08-15 13:02:37 -07002281 } else if (rpath || rino) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002282 *ino = rino;
2283 *ppath = rpath;
David Zafmanb0000562012-10-25 10:23:46 -07002284 *pathlen = rpath ? strlen(rpath) : 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002285 dout(" path %.*s\n", *pathlen, rpath);
2286 }
2287
2288 return r;
2289}
2290
2291/*
2292 * called under mdsc->mutex
2293 */
2294static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
2295 struct ceph_mds_request *req,
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002296 int mds, bool drop_cap_releases)
Sage Weil2f2dc052009-10-06 11:31:09 -07002297{
2298 struct ceph_msg *msg;
2299 struct ceph_mds_request_head *head;
2300 const char *path1 = NULL;
2301 const char *path2 = NULL;
2302 u64 ino1 = 0, ino2 = 0;
2303 int pathlen1 = 0, pathlen2 = 0;
Jeff Layton1bcb3442019-04-15 12:00:42 -04002304 bool freepath1 = false, freepath2 = false;
Sage Weil2f2dc052009-10-06 11:31:09 -07002305 int len;
2306 u16 releases;
2307 void *p, *end;
2308 int ret;
2309
2310 ret = set_request_path_attr(req->r_inode, req->r_dentry,
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002311 req->r_parent, req->r_path1, req->r_ino1.ino,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002312 &path1, &pathlen1, &ino1, &freepath1,
2313 test_bit(CEPH_MDS_R_PARENT_LOCKED,
2314 &req->r_req_flags));
Sage Weil2f2dc052009-10-06 11:31:09 -07002315 if (ret < 0) {
2316 msg = ERR_PTR(ret);
2317 goto out;
2318 }
2319
Jeff Layton1bcb3442019-04-15 12:00:42 -04002320 /* If r_old_dentry is set, then assume that its parent is locked */
Sage Weil2f2dc052009-10-06 11:31:09 -07002321 ret = set_request_path_attr(NULL, req->r_old_dentry,
Jeff Laytonfd36a712016-12-15 08:37:58 -05002322 req->r_old_dentry_dir,
Sage Weil2f2dc052009-10-06 11:31:09 -07002323 req->r_path2, req->r_ino2.ino,
Jeff Layton1bcb3442019-04-15 12:00:42 -04002324 &path2, &pathlen2, &ino2, &freepath2, true);
Sage Weil2f2dc052009-10-06 11:31:09 -07002325 if (ret < 0) {
2326 msg = ERR_PTR(ret);
2327 goto out_free1;
2328 }
2329
2330 len = sizeof(*head) +
Sage Weilb8e69062014-05-21 17:41:08 -07002331 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) +
Arnd Bergmann777d7382015-09-30 15:04:42 +02002332 sizeof(struct ceph_timespec);
Sage Weil2f2dc052009-10-06 11:31:09 -07002333
2334 /* calculate (max) length for cap releases */
2335 len += sizeof(struct ceph_mds_request_release) *
2336 (!!req->r_inode_drop + !!req->r_dentry_drop +
2337 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
2338 if (req->r_dentry_drop)
Jeff Laytonc1dfc272019-04-17 14:23:17 -04002339 len += pathlen1;
Sage Weil2f2dc052009-10-06 11:31:09 -07002340 if (req->r_old_dentry_drop)
Jeff Laytonc1dfc272019-04-17 14:23:17 -04002341 len += pathlen2;
Sage Weil2f2dc052009-10-06 11:31:09 -07002342
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02002343 msg = ceph_msg_new2(CEPH_MSG_CLIENT_REQUEST, len, 1, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07002344 if (!msg) {
2345 msg = ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -07002346 goto out_free2;
Sage Weila79832f2010-04-01 16:06:19 -07002347 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002348
John Spray7cfa0312014-10-30 17:15:26 +00002349 msg->hdr.version = cpu_to_le16(2);
Sage Weil6df058c2009-12-22 11:24:33 -08002350 msg->hdr.tid = cpu_to_le64(req->r_tid);
2351
Sage Weil2f2dc052009-10-06 11:31:09 -07002352 head = msg->front.iov_base;
2353 p = msg->front.iov_base + sizeof(*head);
2354 end = msg->front.iov_base + msg->front.iov_len;
2355
2356 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
2357 head->op = cpu_to_le32(req->r_op);
Eric W. Biedermanff3d0042013-01-31 04:01:53 -08002358 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid));
2359 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid));
Jeff Layton2def8652019-10-14 15:41:54 -04002360 head->ino = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002361 head->args = req->r_args;
2362
2363 ceph_encode_filepath(&p, end, ino1, path1);
2364 ceph_encode_filepath(&p, end, ino2, path2);
2365
Sage Weile979cf52010-07-15 14:58:39 -07002366 /* make note of release offset, in case we need to replay */
2367 req->r_request_release_offset = p - msg->front.iov_base;
2368
Sage Weil2f2dc052009-10-06 11:31:09 -07002369 /* cap releases */
2370 releases = 0;
2371 if (req->r_inode_drop)
2372 releases += ceph_encode_inode_release(&p,
David Howells2b0143b2015-03-17 22:25:59 +00002373 req->r_inode ? req->r_inode : d_inode(req->r_dentry),
Sage Weil2f2dc052009-10-06 11:31:09 -07002374 mds, req->r_inode_drop, req->r_inode_unless, 0);
2375 if (req->r_dentry_drop)
2376 releases += ceph_encode_dentry_release(&p, req->r_dentry,
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002377 req->r_parent, mds, req->r_dentry_drop,
Jeff Laytonca6c8ae2016-12-15 08:37:59 -05002378 req->r_dentry_unless);
Sage Weil2f2dc052009-10-06 11:31:09 -07002379 if (req->r_old_dentry_drop)
2380 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
Jeff Laytonca6c8ae2016-12-15 08:37:59 -05002381 req->r_old_dentry_dir, mds,
2382 req->r_old_dentry_drop,
2383 req->r_old_dentry_unless);
Sage Weil2f2dc052009-10-06 11:31:09 -07002384 if (req->r_old_inode_drop)
2385 releases += ceph_encode_inode_release(&p,
David Howells2b0143b2015-03-17 22:25:59 +00002386 d_inode(req->r_old_dentry),
Sage Weil2f2dc052009-10-06 11:31:09 -07002387 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002388
2389 if (drop_cap_releases) {
2390 releases = 0;
2391 p = msg->front.iov_base + req->r_request_release_offset;
2392 }
2393
Sage Weil2f2dc052009-10-06 11:31:09 -07002394 head->num_releases = cpu_to_le16(releases);
2395
Sage Weilb8e69062014-05-21 17:41:08 -07002396 /* time stamp */
Yan, Zheng1f041a82015-01-13 15:20:52 +08002397 {
2398 struct ceph_timespec ts;
Arnd Bergmann0ed1e902018-07-13 22:18:38 +02002399 ceph_encode_timespec64(&ts, &req->r_stamp);
Yan, Zheng1f041a82015-01-13 15:20:52 +08002400 ceph_encode_copy(&p, &ts, sizeof(ts));
2401 }
Sage Weilb8e69062014-05-21 17:41:08 -07002402
Sage Weil2f2dc052009-10-06 11:31:09 -07002403 BUG_ON(p > end);
2404 msg->front.iov_len = p - msg->front.iov_base;
2405 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2406
Yan, Zheng25e6bae2014-09-16 19:15:28 +08002407 if (req->r_pagelist) {
2408 struct ceph_pagelist *pagelist = req->r_pagelist;
Yan, Zheng25e6bae2014-09-16 19:15:28 +08002409 ceph_msg_data_add_pagelist(msg, pagelist);
2410 msg->hdr.data_len = cpu_to_le32(pagelist->length);
2411 } else {
2412 msg->hdr.data_len = 0;
Alex Elderebf18f42013-03-04 22:29:57 -06002413 }
Alex Elder02afca62013-02-14 12:16:43 -06002414
Sage Weil2f2dc052009-10-06 11:31:09 -07002415 msg->hdr.data_off = cpu_to_le16(0);
2416
2417out_free2:
2418 if (freepath2)
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002419 ceph_mdsc_free_path((char *)path2, pathlen2);
Sage Weil2f2dc052009-10-06 11:31:09 -07002420out_free1:
2421 if (freepath1)
Jeff Laytonf77f21b2019-04-29 12:13:14 -04002422 ceph_mdsc_free_path((char *)path1, pathlen1);
Sage Weil2f2dc052009-10-06 11:31:09 -07002423out:
2424 return msg;
2425}
2426
2427/*
2428 * called under mdsc->mutex if error, under no mutex if
2429 * success.
2430 */
2431static void complete_request(struct ceph_mds_client *mdsc,
2432 struct ceph_mds_request *req)
2433{
2434 if (req->r_callback)
2435 req->r_callback(mdsc, req);
Jeff Layton111c7082019-04-02 09:43:18 -04002436 complete_all(&req->r_completion);
Sage Weil2f2dc052009-10-06 11:31:09 -07002437}
2438
2439/*
2440 * called under mdsc->mutex
2441 */
2442static int __prepare_send_request(struct ceph_mds_client *mdsc,
2443 struct ceph_mds_request *req,
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002444 int mds, bool drop_cap_releases)
Sage Weil2f2dc052009-10-06 11:31:09 -07002445{
2446 struct ceph_mds_request_head *rhead;
2447 struct ceph_msg *msg;
2448 int flags = 0;
2449
Sage Weil2f2dc052009-10-06 11:31:09 -07002450 req->r_attempts++;
Greg Farnume55b71f2010-06-22 15:58:01 -07002451 if (req->r_inode) {
2452 struct ceph_cap *cap =
2453 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
2454
2455 if (cap)
2456 req->r_sent_on_mseq = cap->mseq;
2457 else
2458 req->r_sent_on_mseq = -1;
2459 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002460 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
2461 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
2462
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002463 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002464 void *p;
Sage Weil01a92f12010-07-15 13:24:32 -07002465 /*
2466 * Replay. Do not regenerate message (and rebuild
2467 * paths, etc.); just use the original message.
2468 * Rebuilding paths will break for renames because
2469 * d_move mangles the src name.
2470 */
2471 msg = req->r_request;
2472 rhead = msg->front.iov_base;
2473
2474 flags = le32_to_cpu(rhead->flags);
2475 flags |= CEPH_MDS_FLAG_REPLAY;
2476 rhead->flags = cpu_to_le32(flags);
2477
2478 if (req->r_target_inode)
2479 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
2480
2481 rhead->num_retry = req->r_attempts - 1;
Sage Weile979cf52010-07-15 14:58:39 -07002482
2483 /* remove cap/dentry releases from message */
2484 rhead->num_releases = 0;
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002485
2486 /* time stamp */
2487 p = msg->front.iov_base + req->r_request_release_offset;
Yan, Zheng1f041a82015-01-13 15:20:52 +08002488 {
2489 struct ceph_timespec ts;
Arnd Bergmann0ed1e902018-07-13 22:18:38 +02002490 ceph_encode_timespec64(&ts, &req->r_stamp);
Yan, Zheng1f041a82015-01-13 15:20:52 +08002491 ceph_encode_copy(&p, &ts, sizeof(ts));
2492 }
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002493
2494 msg->front.iov_len = p - msg->front.iov_base;
2495 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
Sage Weil01a92f12010-07-15 13:24:32 -07002496 return 0;
2497 }
2498
Sage Weil2f2dc052009-10-06 11:31:09 -07002499 if (req->r_request) {
2500 ceph_msg_put(req->r_request);
2501 req->r_request = NULL;
2502 }
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002503 msg = create_request_message(mdsc, req, mds, drop_cap_releases);
Sage Weil2f2dc052009-10-06 11:31:09 -07002504 if (IS_ERR(msg)) {
Sage Weile1518c72010-05-13 11:19:06 -07002505 req->r_err = PTR_ERR(msg);
Sage Weila79832f2010-04-01 16:06:19 -07002506 return PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002507 }
2508 req->r_request = msg;
2509
2510 rhead = msg->front.iov_base;
Sage Weil2f2dc052009-10-06 11:31:09 -07002511 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002512 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Sage Weil2f2dc052009-10-06 11:31:09 -07002513 flags |= CEPH_MDS_FLAG_REPLAY;
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002514 if (req->r_parent)
Sage Weil2f2dc052009-10-06 11:31:09 -07002515 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
2516 rhead->flags = cpu_to_le32(flags);
2517 rhead->num_fwd = req->r_num_fwd;
2518 rhead->num_retry = req->r_attempts - 1;
Sage Weil01a92f12010-07-15 13:24:32 -07002519 rhead->ino = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002520
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002521 dout(" r_parent = %p\n", req->r_parent);
Sage Weil2f2dc052009-10-06 11:31:09 -07002522 return 0;
2523}
2524
2525/*
2526 * send request, or put it on the appropriate wait list.
2527 */
Chengguang Xud5548492018-07-28 16:30:48 +08002528static void __do_request(struct ceph_mds_client *mdsc,
Sage Weil2f2dc052009-10-06 11:31:09 -07002529 struct ceph_mds_request *req)
2530{
2531 struct ceph_mds_session *session = NULL;
2532 int mds = -1;
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002533 int err = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002534
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002535 if (req->r_err || test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
2536 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
Yan, Zhengeb1b8af2013-09-26 14:25:36 +08002537 __unregister_request(mdsc, req);
Chengguang Xud5548492018-07-28 16:30:48 +08002538 return;
Yan, Zhengeb1b8af2013-09-26 14:25:36 +08002539 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002540
2541 if (req->r_timeout &&
2542 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
2543 dout("do_request timed out\n");
2544 err = -EIO;
2545 goto finish;
2546 }
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01002547 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002548 dout("do_request forced umount\n");
2549 err = -EIO;
2550 goto finish;
2551 }
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01002552 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_MOUNTING) {
Yan, Zhenge9e427f2016-11-10 16:02:06 +08002553 if (mdsc->mdsmap_err) {
2554 err = mdsc->mdsmap_err;
2555 dout("do_request mdsmap err %d\n", err);
2556 goto finish;
2557 }
Yan, Zhengcc8e8342017-01-04 16:21:58 +08002558 if (mdsc->mdsmap->m_epoch == 0) {
2559 dout("do_request no mdsmap, waiting for map\n");
2560 list_add(&req->r_wait, &mdsc->waiting_for_map);
Chengguang Xud5548492018-07-28 16:30:48 +08002561 return;
Yan, Zhengcc8e8342017-01-04 16:21:58 +08002562 }
Yan, Zhenge9e427f2016-11-10 16:02:06 +08002563 if (!(mdsc->fsc->mount_options->flags &
2564 CEPH_MOUNT_OPT_MOUNTWAIT) &&
2565 !ceph_mdsmap_is_cluster_available(mdsc->mdsmap)) {
2566 err = -ENOENT;
2567 pr_info("probably no mds server is up\n");
2568 goto finish;
2569 }
2570 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002571
Sage Weildc69e2e2010-11-02 13:49:00 -07002572 put_request_session(req);
2573
Sage Weil2f2dc052009-10-06 11:31:09 -07002574 mds = __choose_mds(mdsc, req);
2575 if (mds < 0 ||
2576 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
2577 dout("do_request no mds or not active, waiting for map\n");
2578 list_add(&req->r_wait, &mdsc->waiting_for_map);
Chengguang Xud5548492018-07-28 16:30:48 +08002579 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07002580 }
2581
2582 /* get, open session */
2583 session = __ceph_lookup_mds_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07002584 if (!session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002585 session = register_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07002586 if (IS_ERR(session)) {
2587 err = PTR_ERR(session);
2588 goto finish;
2589 }
2590 }
Sage Weildc69e2e2010-11-02 13:49:00 -07002591 req->r_session = get_session(session);
2592
Sage Weil2f2dc052009-10-06 11:31:09 -07002593 dout("do_request mds%d session %p state %s\n", mds, session,
John Spraya687eca2014-09-19 13:51:08 +01002594 ceph_session_state_name(session->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07002595 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
2596 session->s_state != CEPH_MDS_SESSION_HUNG) {
Yan, Zhengfcff4152016-09-14 16:39:51 +08002597 if (session->s_state == CEPH_MDS_SESSION_REJECTED) {
2598 err = -EACCES;
2599 goto out_session;
2600 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002601 if (session->s_state == CEPH_MDS_SESSION_NEW ||
2602 session->s_state == CEPH_MDS_SESSION_CLOSING)
2603 __open_session(mdsc, session);
2604 list_add(&req->r_wait, &session->s_waiting);
2605 goto out_session;
2606 }
2607
2608 /* send request */
Sage Weil2f2dc052009-10-06 11:31:09 -07002609 req->r_resend_mds = -1; /* forget any previous mds hint */
2610
2611 if (req->r_request_started == 0) /* note request start time */
2612 req->r_request_started = jiffies;
2613
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002614 err = __prepare_send_request(mdsc, req, mds, false);
Sage Weil2f2dc052009-10-06 11:31:09 -07002615 if (!err) {
2616 ceph_msg_get(req->r_request);
2617 ceph_con_send(&session->s_con, req->r_request);
2618 }
2619
2620out_session:
2621 ceph_put_mds_session(session);
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002622finish:
2623 if (err) {
2624 dout("__do_request early error %d\n", err);
2625 req->r_err = err;
2626 complete_request(mdsc, req);
2627 __unregister_request(mdsc, req);
2628 }
Chengguang Xud5548492018-07-28 16:30:48 +08002629 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07002630}
2631
2632/*
2633 * called under mdsc->mutex
2634 */
2635static void __wake_requests(struct ceph_mds_client *mdsc,
2636 struct list_head *head)
2637{
Yan, Zhenged75ec22012-11-19 10:49:06 +08002638 struct ceph_mds_request *req;
2639 LIST_HEAD(tmp_list);
Sage Weil2f2dc052009-10-06 11:31:09 -07002640
Yan, Zhenged75ec22012-11-19 10:49:06 +08002641 list_splice_init(head, &tmp_list);
2642
2643 while (!list_empty(&tmp_list)) {
2644 req = list_entry(tmp_list.next,
2645 struct ceph_mds_request, r_wait);
Sage Weil2f2dc052009-10-06 11:31:09 -07002646 list_del_init(&req->r_wait);
Sage Weil7971bd92013-05-01 21:15:58 -07002647 dout(" wake request %p tid %llu\n", req, req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002648 __do_request(mdsc, req);
2649 }
2650}
2651
2652/*
2653 * Wake up threads with requests pending for @mds, so that they can
Sage Weil29790f22010-03-18 14:45:05 -07002654 * resubmit their requests to a possibly different mds.
Sage Weil2f2dc052009-10-06 11:31:09 -07002655 */
Sage Weil29790f22010-03-18 14:45:05 -07002656static void kick_requests(struct ceph_mds_client *mdsc, int mds)
Sage Weil2f2dc052009-10-06 11:31:09 -07002657{
Sage Weil44ca18f2010-02-15 12:08:46 -08002658 struct ceph_mds_request *req;
Yan, Zheng282c1052014-07-30 10:12:47 +08002659 struct rb_node *p = rb_first(&mdsc->request_tree);
Sage Weil2f2dc052009-10-06 11:31:09 -07002660
2661 dout("kick_requests mds%d\n", mds);
Yan, Zheng282c1052014-07-30 10:12:47 +08002662 while (p) {
Sage Weil44ca18f2010-02-15 12:08:46 -08002663 req = rb_entry(p, struct ceph_mds_request, r_node);
Yan, Zheng282c1052014-07-30 10:12:47 +08002664 p = rb_next(p);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002665 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Sage Weil44ca18f2010-02-15 12:08:46 -08002666 continue;
Yan, Zheng3de22be2015-02-04 14:26:22 +08002667 if (req->r_attempts > 0)
2668 continue; /* only new requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08002669 if (req->r_session &&
2670 req->r_session->s_mds == mds) {
2671 dout(" kicking tid %llu\n", req->r_tid);
Yan, Zheng03974e82014-09-11 14:28:56 +08002672 list_del_init(&req->r_wait);
Sage Weil44ca18f2010-02-15 12:08:46 -08002673 __do_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002674 }
2675 }
2676}
2677
Jeff Layton86bda532019-04-02 09:24:36 -04002678int ceph_mdsc_submit_request(struct ceph_mds_client *mdsc, struct inode *dir,
Sage Weil2f2dc052009-10-06 11:31:09 -07002679 struct ceph_mds_request *req)
2680{
Jeff Layton86bda532019-04-02 09:24:36 -04002681 int err;
2682
2683 /* take CAP_PIN refs for r_inode, r_parent, r_old_dentry */
2684 if (req->r_inode)
2685 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
Jeff Layton9c1c2b32019-04-03 13:16:01 -04002686 if (req->r_parent) {
Jeff Layton86bda532019-04-02 09:24:36 -04002687 ceph_get_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
Jeff Layton9c1c2b32019-04-03 13:16:01 -04002688 ihold(req->r_parent);
2689 }
Jeff Layton86bda532019-04-02 09:24:36 -04002690 if (req->r_old_dentry_dir)
2691 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
2692 CEPH_CAP_PIN);
2693
2694 dout("submit_request on %p for inode %p\n", req, dir);
Sage Weil2f2dc052009-10-06 11:31:09 -07002695 mutex_lock(&mdsc->mutex);
Jeff Layton86bda532019-04-02 09:24:36 -04002696 __register_request(mdsc, req, dir);
Sage Weil2f2dc052009-10-06 11:31:09 -07002697 __do_request(mdsc, req);
Jeff Layton86bda532019-04-02 09:24:36 -04002698 err = req->r_err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002699 mutex_unlock(&mdsc->mutex);
Jeff Layton86bda532019-04-02 09:24:36 -04002700 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07002701}
2702
Jeff Layton8340f222019-04-02 12:34:38 -04002703static int ceph_mdsc_wait_request(struct ceph_mds_client *mdsc,
2704 struct ceph_mds_request *req)
Sage Weil2f2dc052009-10-06 11:31:09 -07002705{
2706 int err;
2707
Sage Weile1518c72010-05-13 11:19:06 -07002708 /* wait */
Sage Weile1518c72010-05-13 11:19:06 -07002709 dout("do_request waiting\n");
Ilya Dryomov5be73032015-05-19 12:05:38 +03002710 if (!req->r_timeout && req->r_wait_for_completion) {
Yan, Zheng9280be22014-10-14 10:33:35 +08002711 err = req->r_wait_for_completion(mdsc, req);
Sage Weile1518c72010-05-13 11:19:06 -07002712 } else {
Ilya Dryomov5be73032015-05-19 12:05:38 +03002713 long timeleft = wait_for_completion_killable_timeout(
2714 &req->r_completion,
2715 ceph_timeout_jiffies(req->r_timeout));
2716 if (timeleft > 0)
2717 err = 0;
2718 else if (!timeleft)
2719 err = -EIO; /* timed out */
2720 else
2721 err = timeleft; /* killed */
Sage Weile1518c72010-05-13 11:19:06 -07002722 }
2723 dout("do_request waited, got %d\n", err);
2724 mutex_lock(&mdsc->mutex);
2725
2726 /* only abort if we didn't race with a real reply */
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002727 if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
Sage Weile1518c72010-05-13 11:19:06 -07002728 err = le32_to_cpu(req->r_reply_info.head->result);
2729 } else if (err < 0) {
2730 dout("aborted request %lld with %d\n", req->r_tid, err);
Sage Weilb4556392010-05-13 12:01:13 -07002731
2732 /*
2733 * ensure we aren't running concurrently with
2734 * ceph_fill_trace or ceph_readdir_prepopulate, which
2735 * rely on locks (dir mutex) held by our caller.
2736 */
2737 mutex_lock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07002738 req->r_err = err;
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002739 set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
Sage Weilb4556392010-05-13 12:01:13 -07002740 mutex_unlock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07002741
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002742 if (req->r_parent &&
Sage Weil167c9e32010-05-14 10:02:57 -07002743 (req->r_op & CEPH_MDS_OP_WRITE))
2744 ceph_invalidate_dir_request(req);
Sage Weile1518c72010-05-13 11:19:06 -07002745 } else {
2746 err = req->r_err;
2747 }
2748
Sage Weile1518c72010-05-13 11:19:06 -07002749 mutex_unlock(&mdsc->mutex);
Jeff Layton8340f222019-04-02 12:34:38 -04002750 return err;
2751}
2752
2753/*
2754 * Synchrously perform an mds request. Take care of all of the
2755 * session setup, forwarding, retry details.
2756 */
2757int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
2758 struct inode *dir,
2759 struct ceph_mds_request *req)
2760{
2761 int err;
2762
2763 dout("do_request on %p\n", req);
2764
2765 /* issue */
2766 err = ceph_mdsc_submit_request(mdsc, dir, req);
2767 if (!err)
2768 err = ceph_mdsc_wait_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002769 dout("do_request %p done, result %d\n", req, err);
2770 return err;
2771}
2772
2773/*
Yan, Zheng2f276c52013-03-13 19:44:32 +08002774 * Invalidate dir's completeness, dentry lease state on an aborted MDS
Sage Weil167c9e32010-05-14 10:02:57 -07002775 * namespace request.
2776 */
2777void ceph_invalidate_dir_request(struct ceph_mds_request *req)
2778{
Yan, Zheng8d8f3712017-11-24 11:51:32 +08002779 struct inode *dir = req->r_parent;
2780 struct inode *old_dir = req->r_old_dentry_dir;
Sage Weil167c9e32010-05-14 10:02:57 -07002781
Yan, Zheng8d8f3712017-11-24 11:51:32 +08002782 dout("invalidate_dir_request %p %p (complete, lease(s))\n", dir, old_dir);
Sage Weil167c9e32010-05-14 10:02:57 -07002783
Yan, Zheng8d8f3712017-11-24 11:51:32 +08002784 ceph_dir_clear_complete(dir);
2785 if (old_dir)
2786 ceph_dir_clear_complete(old_dir);
Sage Weil167c9e32010-05-14 10:02:57 -07002787 if (req->r_dentry)
2788 ceph_invalidate_dentry_lease(req->r_dentry);
2789 if (req->r_old_dentry)
2790 ceph_invalidate_dentry_lease(req->r_old_dentry);
2791}
2792
2793/*
Sage Weil2f2dc052009-10-06 11:31:09 -07002794 * Handle mds reply.
2795 *
2796 * We take the session mutex and parse and process the reply immediately.
2797 * This preserves the logical ordering of replies, capabilities, etc., sent
2798 * by the MDS as they are applied to our local cache.
2799 */
2800static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2801{
2802 struct ceph_mds_client *mdsc = session->s_mdsc;
2803 struct ceph_mds_request *req;
2804 struct ceph_mds_reply_head *head = msg->front.iov_base;
2805 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
Yan, Zheng982d6012014-12-23 15:30:54 +08002806 struct ceph_snap_realm *realm;
Sage Weil2f2dc052009-10-06 11:31:09 -07002807 u64 tid;
2808 int err, result;
Sage Weil2600d2d2010-02-22 15:12:16 -08002809 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002810
Sage Weil2f2dc052009-10-06 11:31:09 -07002811 if (msg->front.iov_len < sizeof(*head)) {
2812 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002813 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002814 return;
2815 }
2816
2817 /* get request, session */
Sage Weil6df058c2009-12-22 11:24:33 -08002818 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002819 mutex_lock(&mdsc->mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02002820 req = lookup_get_request(mdsc, tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002821 if (!req) {
2822 dout("handle_reply on unknown tid %llu\n", tid);
2823 mutex_unlock(&mdsc->mutex);
2824 return;
2825 }
2826 dout("handle_reply %p\n", req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002827
2828 /* correct session? */
Sage Weild96d6042010-03-20 20:50:58 -07002829 if (req->r_session != session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002830 pr_err("mdsc_handle_reply got %llu on session mds%d"
2831 " not mds%d\n", tid, session->s_mds,
2832 req->r_session ? req->r_session->s_mds : -1);
2833 mutex_unlock(&mdsc->mutex);
2834 goto out;
2835 }
2836
2837 /* dup? */
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002838 if ((test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags) && !head->safe) ||
2839 (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags) && head->safe)) {
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07002840 pr_warn("got a dup %s reply on %llu from mds%d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002841 head->safe ? "safe" : "unsafe", tid, mds);
2842 mutex_unlock(&mdsc->mutex);
2843 goto out;
2844 }
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002845 if (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags)) {
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07002846 pr_warn("got unsafe after safe on %llu from mds%d\n",
Sage Weil85792d0d2010-05-13 09:06:02 -07002847 tid, mds);
2848 mutex_unlock(&mdsc->mutex);
2849 goto out;
2850 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002851
2852 result = le32_to_cpu(head->result);
2853
2854 /*
Greg Farnume55b71f2010-06-22 15:58:01 -07002855 * Handle an ESTALE
2856 * if we're not talking to the authority, send to them
2857 * if the authority has changed while we weren't looking,
2858 * send to new authority
2859 * Otherwise we just have to return an ESTALE
Sage Weil2f2dc052009-10-06 11:31:09 -07002860 */
2861 if (result == -ESTALE) {
Chengguang Xu4c069a52018-01-30 16:29:17 +08002862 dout("got ESTALE on request %llu\n", req->r_tid);
Yan, Zheng51da8e82014-07-14 10:48:29 +08002863 req->r_resend_mds = -1;
Yan, Zhengca18bed2013-11-22 14:21:44 +08002864 if (req->r_direct_mode != USE_AUTH_MDS) {
Chengguang Xu4c069a52018-01-30 16:29:17 +08002865 dout("not using auth, setting for that now\n");
Greg Farnume55b71f2010-06-22 15:58:01 -07002866 req->r_direct_mode = USE_AUTH_MDS;
Sage Weil2f2dc052009-10-06 11:31:09 -07002867 __do_request(mdsc, req);
2868 mutex_unlock(&mdsc->mutex);
2869 goto out;
Greg Farnume55b71f2010-06-22 15:58:01 -07002870 } else {
Yan, Zhengca18bed2013-11-22 14:21:44 +08002871 int mds = __choose_mds(mdsc, req);
2872 if (mds >= 0 && mds != req->r_session->s_mds) {
Chengguang Xu4c069a52018-01-30 16:29:17 +08002873 dout("but auth changed, so resending\n");
Greg Farnume55b71f2010-06-22 15:58:01 -07002874 __do_request(mdsc, req);
2875 mutex_unlock(&mdsc->mutex);
2876 goto out;
2877 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002878 }
Chengguang Xu4c069a52018-01-30 16:29:17 +08002879 dout("have to return ESTALE on request %llu\n", req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002880 }
2881
Greg Farnume55b71f2010-06-22 15:58:01 -07002882
Sage Weil2f2dc052009-10-06 11:31:09 -07002883 if (head->safe) {
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002884 set_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags);
Sage Weil2f2dc052009-10-06 11:31:09 -07002885 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002886
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002887 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002888 /*
2889 * We already handled the unsafe response, now do the
2890 * cleanup. No need to examine the response; the MDS
2891 * doesn't include any result info in the safe
2892 * response. And even if it did, there is nothing
2893 * useful we could do with a revised return value.
2894 */
2895 dout("got safe reply %llu, mds%d\n", tid, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002896
2897 /* last unsafe request during umount? */
Sage Weil44ca18f2010-02-15 12:08:46 -08002898 if (mdsc->stopping && !__get_oldest_req(mdsc))
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002899 complete_all(&mdsc->safe_umount_waiters);
Sage Weil2f2dc052009-10-06 11:31:09 -07002900 mutex_unlock(&mdsc->mutex);
2901 goto out;
2902 }
Sage Weile1518c72010-05-13 11:19:06 -07002903 } else {
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002904 set_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags);
Sage Weil2f2dc052009-10-06 11:31:09 -07002905 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
Yan, Zheng4c06ace2015-10-27 17:18:00 +08002906 if (req->r_unsafe_dir) {
2907 struct ceph_inode_info *ci =
2908 ceph_inode(req->r_unsafe_dir);
2909 spin_lock(&ci->i_unsafe_lock);
2910 list_add_tail(&req->r_unsafe_dir_item,
2911 &ci->i_unsafe_dirops);
2912 spin_unlock(&ci->i_unsafe_lock);
2913 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002914 }
2915
2916 dout("handle_reply tid %lld result %d\n", tid, result);
2917 rinfo = &req->r_reply_info;
Yan, Zhengb37fe1f2019-01-09 10:10:17 +08002918 if (test_bit(CEPHFS_FEATURE_REPLY_ENCODING, &session->s_features))
2919 err = parse_reply_info(msg, rinfo, (u64)-1);
2920 else
2921 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
Sage Weil2f2dc052009-10-06 11:31:09 -07002922 mutex_unlock(&mdsc->mutex);
2923
2924 mutex_lock(&session->s_mutex);
2925 if (err < 0) {
Herb Shiu25933ab2010-12-01 14:14:38 -08002926 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002927 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002928 goto out_err;
2929 }
2930
2931 /* snap trace */
Yan, Zheng982d6012014-12-23 15:30:54 +08002932 realm = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07002933 if (rinfo->snapblob_len) {
2934 down_write(&mdsc->snap_rwsem);
2935 ceph_update_snap_trace(mdsc, rinfo->snapblob,
Yan, Zheng982d6012014-12-23 15:30:54 +08002936 rinfo->snapblob + rinfo->snapblob_len,
2937 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP,
2938 &realm);
Sage Weil2f2dc052009-10-06 11:31:09 -07002939 downgrade_write(&mdsc->snap_rwsem);
2940 } else {
2941 down_read(&mdsc->snap_rwsem);
2942 }
2943
2944 /* insert trace into our cache */
Sage Weilb4556392010-05-13 12:01:13 -07002945 mutex_lock(&req->r_fill_mutex);
Yan, Zheng315f2402016-03-07 10:34:50 +08002946 current->journal_info = req;
Jeff Laytonf5a03b02017-01-31 11:06:13 -05002947 err = ceph_fill_trace(mdsc->fsc->sb, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002948 if (err == 0) {
Sam Lang6e8575f2012-12-28 09:56:46 -08002949 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
Yan, Zheng81c6aea2013-09-18 09:44:13 +08002950 req->r_op == CEPH_MDS_OP_LSSNAP))
Sage Weil2f2dc052009-10-06 11:31:09 -07002951 ceph_readdir_prepopulate(req, req->r_session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002952 }
Yan, Zheng315f2402016-03-07 10:34:50 +08002953 current->journal_info = NULL;
Sage Weilb4556392010-05-13 12:01:13 -07002954 mutex_unlock(&req->r_fill_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002955
2956 up_read(&mdsc->snap_rwsem);
Yan, Zheng982d6012014-12-23 15:30:54 +08002957 if (realm)
2958 ceph_put_snap_realm(mdsc, realm);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002959
Yan, Zhengfe330322019-02-01 14:57:15 +08002960 if (err == 0) {
2961 if (req->r_target_inode &&
2962 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
2963 struct ceph_inode_info *ci =
2964 ceph_inode(req->r_target_inode);
2965 spin_lock(&ci->i_unsafe_lock);
2966 list_add_tail(&req->r_unsafe_target_item,
2967 &ci->i_unsafe_iops);
2968 spin_unlock(&ci->i_unsafe_lock);
2969 }
2970
2971 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002972 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002973out_err:
Sage Weile1518c72010-05-13 11:19:06 -07002974 mutex_lock(&mdsc->mutex);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002975 if (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
Sage Weile1518c72010-05-13 11:19:06 -07002976 if (err) {
2977 req->r_err = err;
2978 } else {
Jianpeng Ma5fdb1382015-08-18 10:30:38 +08002979 req->r_reply = ceph_msg_get(msg);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002980 set_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags);
Sage Weile1518c72010-05-13 11:19:06 -07002981 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002982 } else {
Sage Weile1518c72010-05-13 11:19:06 -07002983 dout("reply arrived after request %lld was aborted\n", tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002984 }
Sage Weile1518c72010-05-13 11:19:06 -07002985 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002986
Sage Weil2f2dc052009-10-06 11:31:09 -07002987 mutex_unlock(&session->s_mutex);
2988
2989 /* kick calling process */
2990 complete_request(mdsc, req);
2991out:
2992 ceph_mdsc_put_request(req);
2993 return;
2994}
2995
2996
2997
2998/*
2999 * handle mds notification that our request has been forwarded.
3000 */
Sage Weil2600d2d2010-02-22 15:12:16 -08003001static void handle_forward(struct ceph_mds_client *mdsc,
3002 struct ceph_mds_session *session,
3003 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07003004{
3005 struct ceph_mds_request *req;
Sage Weila1ea7872010-02-23 14:02:44 -08003006 u64 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07003007 u32 next_mds;
3008 u32 fwd_seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003009 int err = -EINVAL;
3010 void *p = msg->front.iov_base;
3011 void *end = p + msg->front.iov_len;
Sage Weil2f2dc052009-10-06 11:31:09 -07003012
Sage Weila1ea7872010-02-23 14:02:44 -08003013 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07003014 next_mds = ceph_decode_32(&p);
3015 fwd_seq = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07003016
3017 mutex_lock(&mdsc->mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02003018 req = lookup_get_request(mdsc, tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07003019 if (!req) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07003020 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003021 goto out; /* dup reply? */
3022 }
3023
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003024 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07003025 dout("forward tid %llu aborted, unregistering\n", tid);
3026 __unregister_request(mdsc, req);
3027 } else if (fwd_seq <= req->r_num_fwd) {
3028 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07003029 tid, next_mds, req->r_num_fwd, fwd_seq);
3030 } else {
3031 /* resend. forward race not possible; mds would drop */
Sage Weil2a8e5e32010-05-28 16:43:16 -07003032 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
3033 BUG_ON(req->r_err);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003034 BUG_ON(test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags));
Yan, Zheng3de22be2015-02-04 14:26:22 +08003035 req->r_attempts = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003036 req->r_num_fwd = fwd_seq;
3037 req->r_resend_mds = next_mds;
3038 put_request_session(req);
3039 __do_request(mdsc, req);
3040 }
3041 ceph_mdsc_put_request(req);
3042out:
3043 mutex_unlock(&mdsc->mutex);
3044 return;
3045
3046bad:
3047 pr_err("mdsc_handle_forward decode error err=%d\n", err);
3048}
3049
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003050static int __decode_session_metadata(void **p, void *end,
3051 bool *blacklisted)
Yan, Zheng84bf3952018-12-21 17:41:39 +08003052{
3053 /* map<string,string> */
3054 u32 n;
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003055 bool err_str;
Yan, Zheng84bf3952018-12-21 17:41:39 +08003056 ceph_decode_32_safe(p, end, n, bad);
3057 while (n-- > 0) {
3058 u32 len;
3059 ceph_decode_32_safe(p, end, len, bad);
3060 ceph_decode_need(p, end, len, bad);
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003061 err_str = !strncmp(*p, "error_string", len);
Yan, Zheng84bf3952018-12-21 17:41:39 +08003062 *p += len;
3063 ceph_decode_32_safe(p, end, len, bad);
3064 ceph_decode_need(p, end, len, bad);
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003065 if (err_str && strnstr(*p, "blacklisted", len))
3066 *blacklisted = true;
Yan, Zheng84bf3952018-12-21 17:41:39 +08003067 *p += len;
3068 }
3069 return 0;
3070bad:
3071 return -1;
3072}
3073
Sage Weil2f2dc052009-10-06 11:31:09 -07003074/*
3075 * handle a mds session control message
3076 */
3077static void handle_session(struct ceph_mds_session *session,
3078 struct ceph_msg *msg)
3079{
3080 struct ceph_mds_client *mdsc = session->s_mdsc;
Yan, Zheng84bf3952018-12-21 17:41:39 +08003081 int mds = session->s_mds;
3082 int msg_version = le16_to_cpu(msg->hdr.version);
3083 void *p = msg->front.iov_base;
3084 void *end = p + msg->front.iov_len;
3085 struct ceph_mds_session_head *h;
Sage Weil2f2dc052009-10-06 11:31:09 -07003086 u32 op;
3087 u64 seq;
Yan, Zheng84bf3952018-12-21 17:41:39 +08003088 unsigned long features = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003089 int wake = 0;
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003090 bool blacklisted = false;
Sage Weil2f2dc052009-10-06 11:31:09 -07003091
Sage Weil2f2dc052009-10-06 11:31:09 -07003092 /* decode */
Yan, Zheng84bf3952018-12-21 17:41:39 +08003093 ceph_decode_need(&p, end, sizeof(*h), bad);
3094 h = p;
3095 p += sizeof(*h);
3096
Sage Weil2f2dc052009-10-06 11:31:09 -07003097 op = le32_to_cpu(h->op);
3098 seq = le64_to_cpu(h->seq);
3099
Yan, Zheng84bf3952018-12-21 17:41:39 +08003100 if (msg_version >= 3) {
3101 u32 len;
3102 /* version >= 2, metadata */
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003103 if (__decode_session_metadata(&p, end, &blacklisted) < 0)
Yan, Zheng84bf3952018-12-21 17:41:39 +08003104 goto bad;
3105 /* version >= 3, feature bits */
3106 ceph_decode_32_safe(&p, end, len, bad);
3107 ceph_decode_need(&p, end, len, bad);
3108 memcpy(&features, p, min_t(size_t, len, sizeof(features)));
3109 p += len;
3110 }
3111
Sage Weil2f2dc052009-10-06 11:31:09 -07003112 mutex_lock(&mdsc->mutex);
Yan, Zheng0a07fc82017-03-29 15:30:24 +08003113 if (op == CEPH_SESSION_CLOSE) {
3114 get_session(session);
Sage Weil2600d2d2010-02-22 15:12:16 -08003115 __unregister_session(mdsc, session);
Yan, Zheng0a07fc82017-03-29 15:30:24 +08003116 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003117 /* FIXME: this ttl calculation is generous */
3118 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
3119 mutex_unlock(&mdsc->mutex);
3120
3121 mutex_lock(&session->s_mutex);
3122
3123 dout("handle_session mds%d %s %p state %s seq %llu\n",
3124 mds, ceph_session_op_name(op), session,
John Spraya687eca2014-09-19 13:51:08 +01003125 ceph_session_state_name(session->s_state), seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003126
3127 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
3128 session->s_state = CEPH_MDS_SESSION_OPEN;
3129 pr_info("mds%d came back\n", session->s_mds);
3130 }
3131
3132 switch (op) {
3133 case CEPH_SESSION_OPEN:
Sage Weil29790f22010-03-18 14:45:05 -07003134 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
3135 pr_info("mds%d reconnect success\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003136 session->s_state = CEPH_MDS_SESSION_OPEN;
Yan, Zheng84bf3952018-12-21 17:41:39 +08003137 session->s_features = features;
Sage Weil2f2dc052009-10-06 11:31:09 -07003138 renewed_caps(mdsc, session, 0);
3139 wake = 1;
3140 if (mdsc->stopping)
3141 __close_session(mdsc, session);
3142 break;
3143
3144 case CEPH_SESSION_RENEWCAPS:
3145 if (session->s_renew_seq == seq)
3146 renewed_caps(mdsc, session, 1);
3147 break;
3148
3149 case CEPH_SESSION_CLOSE:
Sage Weil29790f22010-03-18 14:45:05 -07003150 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
3151 pr_info("mds%d reconnect denied\n", session->s_mds);
Yan, Zheng1c841a92015-03-24 20:15:36 +08003152 cleanup_session_requests(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003153 remove_session_caps(session);
Yan, Zheng656e4382014-09-11 14:25:18 +08003154 wake = 2; /* for good measure */
Sage Weilf3c60c52010-08-11 14:51:23 -07003155 wake_up_all(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003156 break;
3157
3158 case CEPH_SESSION_STALE:
3159 pr_info("mds%d caps went stale, renewing\n",
3160 session->s_mds);
Alex Elderd8fb02a2012-01-12 17:48:10 -08003161 spin_lock(&session->s_gen_ttl_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07003162 session->s_cap_gen++;
Alex Elder1ce208a2012-01-12 17:48:11 -08003163 session->s_cap_ttl = jiffies - 1;
Alex Elderd8fb02a2012-01-12 17:48:10 -08003164 spin_unlock(&session->s_gen_ttl_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07003165 send_renew_caps(mdsc, session);
3166 break;
3167
3168 case CEPH_SESSION_RECALL_STATE:
Zhi Zhange30ee582018-01-24 21:24:33 +08003169 ceph_trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
Sage Weil2f2dc052009-10-06 11:31:09 -07003170 break;
3171
Yan, Zheng186e4f72013-11-22 14:48:37 +08003172 case CEPH_SESSION_FLUSHMSG:
3173 send_flushmsg_ack(mdsc, session, seq);
3174 break;
3175
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08003176 case CEPH_SESSION_FORCE_RO:
3177 dout("force_session_readonly %p\n", session);
3178 spin_lock(&session->s_cap_lock);
3179 session->s_readonly = true;
3180 spin_unlock(&session->s_cap_lock);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08003181 wake_up_session_caps(session, FORCE_RO);
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08003182 break;
3183
Yan, Zhengfcff4152016-09-14 16:39:51 +08003184 case CEPH_SESSION_REJECT:
3185 WARN_ON(session->s_state != CEPH_MDS_SESSION_OPENING);
3186 pr_info("mds%d rejected session\n", session->s_mds);
3187 session->s_state = CEPH_MDS_SESSION_REJECTED;
3188 cleanup_session_requests(mdsc, session);
3189 remove_session_caps(session);
Yan, Zheng131d7eb2019-07-25 20:16:47 +08003190 if (blacklisted)
3191 mdsc->fsc->blacklisted = true;
Yan, Zhengfcff4152016-09-14 16:39:51 +08003192 wake = 2; /* for good measure */
3193 break;
3194
Sage Weil2f2dc052009-10-06 11:31:09 -07003195 default:
3196 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
3197 WARN_ON(1);
3198 }
3199
3200 mutex_unlock(&session->s_mutex);
3201 if (wake) {
3202 mutex_lock(&mdsc->mutex);
3203 __wake_requests(mdsc, &session->s_waiting);
Yan, Zheng656e4382014-09-11 14:25:18 +08003204 if (wake == 2)
3205 kick_requests(mdsc, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003206 mutex_unlock(&mdsc->mutex);
3207 }
Yan, Zheng0a07fc82017-03-29 15:30:24 +08003208 if (op == CEPH_SESSION_CLOSE)
3209 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003210 return;
3211
3212bad:
3213 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
3214 (int)msg->front.iov_len);
Sage Weil9ec7cab2009-12-14 15:13:47 -08003215 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003216 return;
3217}
3218
3219
3220/*
3221 * called under session->mutex.
3222 */
3223static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
3224 struct ceph_mds_session *session)
3225{
3226 struct ceph_mds_request *req, *nreq;
Yan, Zheng3de22be2015-02-04 14:26:22 +08003227 struct rb_node *p;
Sage Weil2f2dc052009-10-06 11:31:09 -07003228 int err;
3229
3230 dout("replay_unsafe_requests mds%d\n", session->s_mds);
3231
3232 mutex_lock(&mdsc->mutex);
3233 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
Yan, Zheng6e6f0922015-02-27 08:54:08 +08003234 err = __prepare_send_request(mdsc, req, session->s_mds, true);
Sage Weil2f2dc052009-10-06 11:31:09 -07003235 if (!err) {
3236 ceph_msg_get(req->r_request);
3237 ceph_con_send(&session->s_con, req->r_request);
3238 }
3239 }
Yan, Zheng3de22be2015-02-04 14:26:22 +08003240
3241 /*
3242 * also re-send old requests when MDS enters reconnect stage. So that MDS
3243 * can process completed request in clientreplay stage.
3244 */
3245 p = rb_first(&mdsc->request_tree);
3246 while (p) {
3247 req = rb_entry(p, struct ceph_mds_request, r_node);
3248 p = rb_next(p);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003249 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Yan, Zheng3de22be2015-02-04 14:26:22 +08003250 continue;
3251 if (req->r_attempts == 0)
3252 continue; /* only old requests */
3253 if (req->r_session &&
3254 req->r_session->s_mds == session->s_mds) {
Yan, Zheng6e6f0922015-02-27 08:54:08 +08003255 err = __prepare_send_request(mdsc, req,
3256 session->s_mds, true);
Yan, Zheng3de22be2015-02-04 14:26:22 +08003257 if (!err) {
3258 ceph_msg_get(req->r_request);
3259 ceph_con_send(&session->s_con, req->r_request);
3260 }
3261 }
3262 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003263 mutex_unlock(&mdsc->mutex);
3264}
3265
Yan, Zheng81c5a142019-01-01 16:28:33 +08003266static int send_reconnect_partial(struct ceph_reconnect_state *recon_state)
3267{
3268 struct ceph_msg *reply;
3269 struct ceph_pagelist *_pagelist;
3270 struct page *page;
3271 __le32 *addr;
3272 int err = -ENOMEM;
3273
3274 if (!recon_state->allow_multi)
3275 return -ENOSPC;
3276
3277 /* can't handle message that contains both caps and realm */
3278 BUG_ON(!recon_state->nr_caps == !recon_state->nr_realms);
3279
3280 /* pre-allocate new pagelist */
3281 _pagelist = ceph_pagelist_alloc(GFP_NOFS);
3282 if (!_pagelist)
3283 return -ENOMEM;
3284
3285 reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false);
3286 if (!reply)
3287 goto fail_msg;
3288
3289 /* placeholder for nr_caps */
3290 err = ceph_pagelist_encode_32(_pagelist, 0);
3291 if (err < 0)
3292 goto fail;
3293
3294 if (recon_state->nr_caps) {
3295 /* currently encoding caps */
3296 err = ceph_pagelist_encode_32(recon_state->pagelist, 0);
3297 if (err)
3298 goto fail;
3299 } else {
3300 /* placeholder for nr_realms (currently encoding relams) */
3301 err = ceph_pagelist_encode_32(_pagelist, 0);
3302 if (err < 0)
3303 goto fail;
3304 }
3305
3306 err = ceph_pagelist_encode_8(recon_state->pagelist, 1);
3307 if (err)
3308 goto fail;
3309
3310 page = list_first_entry(&recon_state->pagelist->head, struct page, lru);
3311 addr = kmap_atomic(page);
3312 if (recon_state->nr_caps) {
3313 /* currently encoding caps */
3314 *addr = cpu_to_le32(recon_state->nr_caps);
3315 } else {
3316 /* currently encoding relams */
3317 *(addr + 1) = cpu_to_le32(recon_state->nr_realms);
3318 }
3319 kunmap_atomic(addr);
3320
3321 reply->hdr.version = cpu_to_le16(5);
3322 reply->hdr.compat_version = cpu_to_le16(4);
3323
3324 reply->hdr.data_len = cpu_to_le32(recon_state->pagelist->length);
3325 ceph_msg_data_add_pagelist(reply, recon_state->pagelist);
3326
3327 ceph_con_send(&recon_state->session->s_con, reply);
3328 ceph_pagelist_release(recon_state->pagelist);
3329
3330 recon_state->pagelist = _pagelist;
3331 recon_state->nr_caps = 0;
3332 recon_state->nr_realms = 0;
3333 recon_state->msg_version = 5;
3334 return 0;
3335fail:
3336 ceph_msg_put(reply);
3337fail_msg:
3338 ceph_pagelist_release(_pagelist);
3339 return err;
3340}
3341
Sage Weil2f2dc052009-10-06 11:31:09 -07003342/*
3343 * Encode information about a cap for a reconnect with the MDS.
3344 */
Sage Weil2f2dc052009-10-06 11:31:09 -07003345static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
3346 void *arg)
3347{
Sage Weil20cb34a2010-05-12 15:21:32 -07003348 union {
3349 struct ceph_mds_cap_reconnect v2;
3350 struct ceph_mds_cap_reconnect_v1 v1;
3351 } rec;
Yan, Zhengb3f8d682017-09-11 10:58:55 +08003352 struct ceph_inode_info *ci = cap->ci;
Sage Weil20cb34a2010-05-12 15:21:32 -07003353 struct ceph_reconnect_state *recon_state = arg;
3354 struct ceph_pagelist *pagelist = recon_state->pagelist;
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003355 int err;
Yan, Zheng3469ed02016-07-05 09:32:31 +08003356 u64 snap_follows;
Sage Weil2f2dc052009-10-06 11:31:09 -07003357
Sage Weil2f2dc052009-10-06 11:31:09 -07003358 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
3359 inode, ceph_vinop(inode), cap, cap->cap_id,
3360 ceph_cap_string(cap->issued));
Sage Weil2f2dc052009-10-06 11:31:09 -07003361
Sage Weilbe655592011-11-30 09:47:09 -08003362 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07003363 cap->seq = 0; /* reset cap seq */
3364 cap->issue_seq = 0; /* and issue_seq */
Yan, Zheng667ca052013-05-31 16:25:36 +08003365 cap->mseq = 0; /* and migrate_seq */
Yan, Zheng99a9c272013-09-22 11:08:14 +08003366 cap->cap_gen = cap->session->s_cap_gen;
Sage Weil20cb34a2010-05-12 15:21:32 -07003367
Yan, Zheng121f22a2016-07-04 22:05:18 +08003368 if (recon_state->msg_version >= 2) {
Sage Weil20cb34a2010-05-12 15:21:32 -07003369 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
3370 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
3371 rec.v2.issued = cpu_to_le32(cap->issued);
3372 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003373 rec.v2.pathbase = 0;
Jeff Laytonec1dff22017-10-31 15:51:14 -04003374 rec.v2.flock_len = (__force __le32)
3375 ((ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) ? 0 : 1);
Sage Weil20cb34a2010-05-12 15:21:32 -07003376 } else {
3377 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
3378 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
3379 rec.v1.issued = cpu_to_le32(cap->issued);
3380 rec.v1.size = cpu_to_le64(inode->i_size);
Arnd Bergmann9bbeab42018-07-13 22:18:36 +02003381 ceph_encode_timespec64(&rec.v1.mtime, &inode->i_mtime);
3382 ceph_encode_timespec64(&rec.v1.atime, &inode->i_atime);
Sage Weil20cb34a2010-05-12 15:21:32 -07003383 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003384 rec.v1.pathbase = 0;
Sage Weil20cb34a2010-05-12 15:21:32 -07003385 }
Yan, Zheng3469ed02016-07-05 09:32:31 +08003386
3387 if (list_empty(&ci->i_cap_snaps)) {
Yan, Zheng92776fd2017-08-16 21:42:39 +08003388 snap_follows = ci->i_head_snapc ? ci->i_head_snapc->seq : 0;
Yan, Zheng3469ed02016-07-05 09:32:31 +08003389 } else {
3390 struct ceph_cap_snap *capsnap =
3391 list_first_entry(&ci->i_cap_snaps,
3392 struct ceph_cap_snap, ci_item);
3393 snap_follows = capsnap->follows;
Sage Weil93cea5b2009-12-23 12:21:51 -08003394 }
Sage Weilbe655592011-11-30 09:47:09 -08003395 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07003396
Yan, Zheng121f22a2016-07-04 22:05:18 +08003397 if (recon_state->msg_version >= 2) {
Greg Farnum40819f62010-08-02 15:34:23 -07003398 int num_fcntl_locks, num_flock_locks;
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003399 struct ceph_filelock *flocks = NULL;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003400 size_t struct_len, total_len = sizeof(u64);
Yan, Zheng121f22a2016-07-04 22:05:18 +08003401 u8 struct_v = 0;
Greg Farnum40819f62010-08-02 15:34:23 -07003402
Jim Schutt39be95e2013-05-15 13:03:35 -05003403encode_again:
Yan, Zhengb3f8d682017-09-11 10:58:55 +08003404 if (rec.v2.flock_len) {
3405 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
3406 } else {
3407 num_fcntl_locks = 0;
3408 num_flock_locks = 0;
3409 }
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003410 if (num_fcntl_locks + num_flock_locks > 0) {
Kees Cook6da2ec52018-06-12 13:55:00 -07003411 flocks = kmalloc_array(num_fcntl_locks + num_flock_locks,
3412 sizeof(struct ceph_filelock),
3413 GFP_NOFS);
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003414 if (!flocks) {
3415 err = -ENOMEM;
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003416 goto out_err;
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003417 }
3418 err = ceph_encode_locks_to_buffer(inode, flocks,
3419 num_fcntl_locks,
3420 num_flock_locks);
3421 if (err) {
3422 kfree(flocks);
3423 flocks = NULL;
3424 if (err == -ENOSPC)
3425 goto encode_again;
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003426 goto out_err;
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003427 }
3428 } else {
Jim Schutt39be95e2013-05-15 13:03:35 -05003429 kfree(flocks);
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003430 flocks = NULL;
Jim Schutt39be95e2013-05-15 13:03:35 -05003431 }
Yan, Zheng121f22a2016-07-04 22:05:18 +08003432
3433 if (recon_state->msg_version >= 3) {
3434 /* version, compat_version and struct_len */
Yan, Zheng81c5a142019-01-01 16:28:33 +08003435 total_len += 2 * sizeof(u8) + sizeof(u32);
Yan, Zheng3469ed02016-07-05 09:32:31 +08003436 struct_v = 2;
Yan, Zheng121f22a2016-07-04 22:05:18 +08003437 }
Jim Schutt39be95e2013-05-15 13:03:35 -05003438 /*
3439 * number of encoded locks is stable, so copy to pagelist
3440 */
Yan, Zheng121f22a2016-07-04 22:05:18 +08003441 struct_len = 2 * sizeof(u32) +
3442 (num_fcntl_locks + num_flock_locks) *
3443 sizeof(struct ceph_filelock);
3444 rec.v2.flock_len = cpu_to_le32(struct_len);
3445
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003446 struct_len += sizeof(u32) + sizeof(rec.v2);
Yan, Zheng121f22a2016-07-04 22:05:18 +08003447
Yan, Zheng3469ed02016-07-05 09:32:31 +08003448 if (struct_v >= 2)
3449 struct_len += sizeof(u64); /* snap_follows */
3450
Yan, Zheng121f22a2016-07-04 22:05:18 +08003451 total_len += struct_len;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003452
3453 if (pagelist->length + total_len > RECONNECT_MAX_SIZE) {
3454 err = send_reconnect_partial(recon_state);
3455 if (err)
3456 goto out_freeflocks;
3457 pagelist = recon_state->pagelist;
Yan, Zheng121f22a2016-07-04 22:05:18 +08003458 }
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003459
Yan, Zheng81c5a142019-01-01 16:28:33 +08003460 err = ceph_pagelist_reserve(pagelist, total_len);
3461 if (err)
3462 goto out_freeflocks;
3463
3464 ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003465 if (recon_state->msg_version >= 3) {
3466 ceph_pagelist_encode_8(pagelist, struct_v);
3467 ceph_pagelist_encode_8(pagelist, 1);
3468 ceph_pagelist_encode_32(pagelist, struct_len);
3469 }
3470 ceph_pagelist_encode_string(pagelist, NULL, 0);
3471 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v2));
3472 ceph_locks_to_pagelist(flocks, pagelist,
3473 num_fcntl_locks, num_flock_locks);
3474 if (struct_v >= 2)
3475 ceph_pagelist_encode_64(pagelist, snap_follows);
Yan, Zheng81c5a142019-01-01 16:28:33 +08003476out_freeflocks:
Jim Schutt39be95e2013-05-15 13:03:35 -05003477 kfree(flocks);
Sage Weil3612abb2010-09-07 15:59:27 -07003478 } else {
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003479 u64 pathbase = 0;
3480 int pathlen = 0;
3481 char *path = NULL;
3482 struct dentry *dentry;
3483
3484 dentry = d_find_alias(inode);
3485 if (dentry) {
3486 path = ceph_mdsc_build_path(dentry,
3487 &pathlen, &pathbase, 0);
3488 dput(dentry);
3489 if (IS_ERR(path)) {
3490 err = PTR_ERR(path);
3491 goto out_err;
3492 }
3493 rec.v1.pathbase = cpu_to_le64(pathbase);
Yan, Zheng121f22a2016-07-04 22:05:18 +08003494 }
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003495
3496 err = ceph_pagelist_reserve(pagelist,
Yan, Zheng81c5a142019-01-01 16:28:33 +08003497 sizeof(u64) + sizeof(u32) +
3498 pathlen + sizeof(rec.v1));
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003499 if (err) {
Yan, Zheng81c5a142019-01-01 16:28:33 +08003500 goto out_freepath;
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003501 }
3502
Yan, Zheng81c5a142019-01-01 16:28:33 +08003503 ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003504 ceph_pagelist_encode_string(pagelist, path, pathlen);
3505 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v1));
Yan, Zheng81c5a142019-01-01 16:28:33 +08003506out_freepath:
Jeff Laytonf77f21b2019-04-29 12:13:14 -04003507 ceph_mdsc_free_path(path, pathlen);
Greg Farnum40819f62010-08-02 15:34:23 -07003508 }
Yan, Zheng44c99752013-09-22 10:28:10 +08003509
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003510out_err:
Yan, Zheng81c5a142019-01-01 16:28:33 +08003511 if (err >= 0)
3512 recon_state->nr_caps++;
3513 return err;
3514}
3515
3516static int encode_snap_realms(struct ceph_mds_client *mdsc,
3517 struct ceph_reconnect_state *recon_state)
3518{
3519 struct rb_node *p;
3520 struct ceph_pagelist *pagelist = recon_state->pagelist;
3521 int err = 0;
3522
3523 if (recon_state->msg_version >= 4) {
3524 err = ceph_pagelist_encode_32(pagelist, mdsc->num_snap_realms);
3525 if (err < 0)
3526 goto fail;
3527 }
3528
3529 /*
3530 * snaprealms. we provide mds with the ino, seq (version), and
3531 * parent for all of our realms. If the mds has any newer info,
3532 * it will tell us.
3533 */
3534 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
3535 struct ceph_snap_realm *realm =
3536 rb_entry(p, struct ceph_snap_realm, node);
3537 struct ceph_mds_snaprealm_reconnect sr_rec;
3538
3539 if (recon_state->msg_version >= 4) {
3540 size_t need = sizeof(u8) * 2 + sizeof(u32) +
3541 sizeof(sr_rec);
3542
3543 if (pagelist->length + need > RECONNECT_MAX_SIZE) {
3544 err = send_reconnect_partial(recon_state);
3545 if (err)
3546 goto fail;
3547 pagelist = recon_state->pagelist;
3548 }
3549
3550 err = ceph_pagelist_reserve(pagelist, need);
3551 if (err)
3552 goto fail;
3553
3554 ceph_pagelist_encode_8(pagelist, 1);
3555 ceph_pagelist_encode_8(pagelist, 1);
3556 ceph_pagelist_encode_32(pagelist, sizeof(sr_rec));
3557 }
3558
3559 dout(" adding snap realm %llx seq %lld parent %llx\n",
3560 realm->ino, realm->seq, realm->parent_ino);
3561 sr_rec.ino = cpu_to_le64(realm->ino);
3562 sr_rec.seq = cpu_to_le64(realm->seq);
3563 sr_rec.parent = cpu_to_le64(realm->parent_ino);
3564
3565 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
3566 if (err)
3567 goto fail;
3568
3569 recon_state->nr_realms++;
3570 }
3571fail:
Sage Weil93cea5b2009-12-23 12:21:51 -08003572 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07003573}
3574
3575
3576/*
3577 * If an MDS fails and recovers, clients need to reconnect in order to
3578 * reestablish shared state. This includes all caps issued through
3579 * this session _and_ the snap_realm hierarchy. Because it's not
3580 * clear which snap realms the mds cares about, we send everything we
3581 * know about.. that ensures we'll then get any new info the
3582 * recovering MDS might have.
3583 *
3584 * This is a relatively heavyweight operation, but it's rare.
3585 *
3586 * called with mdsc->mutex held.
3587 */
Sage Weil34b6c852010-05-10 16:31:25 -07003588static void send_mds_reconnect(struct ceph_mds_client *mdsc,
3589 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07003590{
Sage Weil2f2dc052009-10-06 11:31:09 -07003591 struct ceph_msg *reply;
Sage Weil34b6c852010-05-10 16:31:25 -07003592 int mds = session->s_mds;
Sage Weil9abf82b2010-05-10 21:58:38 -07003593 int err = -ENOMEM;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003594 struct ceph_reconnect_state recon_state = {
3595 .session = session,
3596 };
Jeff Laytonc8a96a32017-10-19 08:53:58 -04003597 LIST_HEAD(dispose);
Sage Weil2f2dc052009-10-06 11:31:09 -07003598
Sage Weil34b6c852010-05-10 16:31:25 -07003599 pr_info("mds%d reconnect start\n", mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003600
Yan, Zheng81c5a142019-01-01 16:28:33 +08003601 recon_state.pagelist = ceph_pagelist_alloc(GFP_NOFS);
3602 if (!recon_state.pagelist)
Sage Weil93cea5b2009-12-23 12:21:51 -08003603 goto fail_nopagelist;
Sage Weil93cea5b2009-12-23 12:21:51 -08003604
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003605 reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07003606 if (!reply)
Sage Weil93cea5b2009-12-23 12:21:51 -08003607 goto fail_nomsg;
Sage Weil93cea5b2009-12-23 12:21:51 -08003608
Sage Weil34b6c852010-05-10 16:31:25 -07003609 mutex_lock(&session->s_mutex);
3610 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
3611 session->s_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003612
Sage Weil2f2dc052009-10-06 11:31:09 -07003613 dout("session %p state %s\n", session,
John Spraya687eca2014-09-19 13:51:08 +01003614 ceph_session_state_name(session->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07003615
Yan, Zheng99a9c272013-09-22 11:08:14 +08003616 spin_lock(&session->s_gen_ttl_lock);
3617 session->s_cap_gen++;
3618 spin_unlock(&session->s_gen_ttl_lock);
3619
3620 spin_lock(&session->s_cap_lock);
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08003621 /* don't know if session is readonly */
3622 session->s_readonly = 0;
Yan, Zheng99a9c272013-09-22 11:08:14 +08003623 /*
3624 * notify __ceph_remove_cap() that we are composing cap reconnect.
3625 * If a cap get released before being added to the cap reconnect,
3626 * __ceph_remove_cap() should skip queuing cap release.
3627 */
3628 session->s_cap_reconnect = 1;
Sage Weile01a5942010-05-10 15:36:44 -07003629 /* drop old cap expires; we're about to reestablish that state */
Jeff Laytonc8a96a32017-10-19 08:53:58 -04003630 detach_cap_releases(session, &dispose);
3631 spin_unlock(&session->s_cap_lock);
3632 dispose_cap_releases(mdsc, &dispose);
Sage Weile01a5942010-05-10 15:36:44 -07003633
Yan, Zheng5d233712014-09-10 16:56:23 +08003634 /* trim unused caps to reduce MDS's cache rejoin time */
Yan, Zhengc0bd50e2015-04-07 15:51:08 +08003635 if (mdsc->fsc->sb->s_root)
3636 shrink_dcache_parent(mdsc->fsc->sb->s_root);
Yan, Zheng5d233712014-09-10 16:56:23 +08003637
3638 ceph_con_close(&session->s_con);
3639 ceph_con_open(&session->s_con,
3640 CEPH_ENTITY_TYPE_MDS, mds,
3641 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
3642
3643 /* replay unsafe requests */
3644 replay_unsafe_requests(mdsc, session);
3645
Yan, Zheng81c5a142019-01-01 16:28:33 +08003646 ceph_early_kick_flushing_caps(mdsc, session);
3647
Yan, Zheng5d233712014-09-10 16:56:23 +08003648 down_read(&mdsc->snap_rwsem);
3649
Yan, Zheng81c5a142019-01-01 16:28:33 +08003650 /* placeholder for nr_caps */
3651 err = ceph_pagelist_encode_32(recon_state.pagelist, 0);
Sage Weil93cea5b2009-12-23 12:21:51 -08003652 if (err)
3653 goto fail;
Sage Weil20cb34a2010-05-12 15:21:32 -07003654
Yan, Zheng81c5a142019-01-01 16:28:33 +08003655 if (test_bit(CEPHFS_FEATURE_MULTI_RECONNECT, &session->s_features)) {
Yan, Zheng121f22a2016-07-04 22:05:18 +08003656 recon_state.msg_version = 3;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003657 recon_state.allow_multi = true;
3658 } else if (session->s_con.peer_features & CEPH_FEATURE_MDSENC) {
3659 recon_state.msg_version = 3;
3660 } else {
Ilya Dryomov23c625c2018-11-08 14:55:21 +01003661 recon_state.msg_version = 2;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003662 }
3663 /* trsaverse this session's caps */
Jeff Laytonf5d772692019-04-24 12:09:04 -04003664 err = ceph_iterate_session_caps(session, encode_caps_cb, &recon_state);
Sage Weil2f2dc052009-10-06 11:31:09 -07003665
Yan, Zheng99a9c272013-09-22 11:08:14 +08003666 spin_lock(&session->s_cap_lock);
3667 session->s_cap_reconnect = 0;
3668 spin_unlock(&session->s_cap_lock);
3669
Yan, Zheng81c5a142019-01-01 16:28:33 +08003670 if (err < 0)
3671 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07003672
Yan, Zheng81c5a142019-01-01 16:28:33 +08003673 /* check if all realms can be encoded into current message */
3674 if (mdsc->num_snap_realms) {
3675 size_t total_len =
3676 recon_state.pagelist->length +
3677 mdsc->num_snap_realms *
3678 sizeof(struct ceph_mds_snaprealm_reconnect);
3679 if (recon_state.msg_version >= 4) {
3680 /* number of realms */
3681 total_len += sizeof(u32);
3682 /* version, compat_version and struct_len */
3683 total_len += mdsc->num_snap_realms *
3684 (2 * sizeof(u8) + sizeof(u32));
3685 }
3686 if (total_len > RECONNECT_MAX_SIZE) {
3687 if (!recon_state.allow_multi) {
3688 err = -ENOSPC;
3689 goto fail;
3690 }
3691 if (recon_state.nr_caps) {
3692 err = send_reconnect_partial(&recon_state);
3693 if (err)
3694 goto fail;
3695 }
3696 recon_state.msg_version = 5;
3697 }
3698 }
3699
3700 err = encode_snap_realms(mdsc, &recon_state);
3701 if (err < 0)
3702 goto fail;
3703
3704 if (recon_state.msg_version >= 5) {
3705 err = ceph_pagelist_encode_8(recon_state.pagelist, 0);
3706 if (err < 0)
Sage Weil93cea5b2009-12-23 12:21:51 -08003707 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07003708 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003709
Yan, Zheng81c5a142019-01-01 16:28:33 +08003710 if (recon_state.nr_caps || recon_state.nr_realms) {
3711 struct page *page =
3712 list_first_entry(&recon_state.pagelist->head,
3713 struct page, lru);
Yan, Zheng44c99752013-09-22 10:28:10 +08003714 __le32 *addr = kmap_atomic(page);
Yan, Zheng81c5a142019-01-01 16:28:33 +08003715 if (recon_state.nr_caps) {
3716 WARN_ON(recon_state.nr_realms != mdsc->num_snap_realms);
3717 *addr = cpu_to_le32(recon_state.nr_caps);
3718 } else if (recon_state.msg_version >= 4) {
3719 *(addr + 1) = cpu_to_le32(recon_state.nr_realms);
3720 }
Yan, Zheng44c99752013-09-22 10:28:10 +08003721 kunmap_atomic(addr);
Alex Elderebf18f42013-03-04 22:29:57 -06003722 }
Yan, Zheng44c99752013-09-22 10:28:10 +08003723
Yan, Zheng81c5a142019-01-01 16:28:33 +08003724 reply->hdr.version = cpu_to_le16(recon_state.msg_version);
3725 if (recon_state.msg_version >= 4)
3726 reply->hdr.compat_version = cpu_to_le16(4);
Yan, Zhenge548e9b2015-06-10 15:17:56 +08003727
Yan, Zheng81c5a142019-01-01 16:28:33 +08003728 reply->hdr.data_len = cpu_to_le32(recon_state.pagelist->length);
3729 ceph_msg_data_add_pagelist(reply, recon_state.pagelist);
Yan, Zhenge548e9b2015-06-10 15:17:56 +08003730
Sage Weil2f2dc052009-10-06 11:31:09 -07003731 ceph_con_send(&session->s_con, reply);
3732
Sage Weil9abf82b2010-05-10 21:58:38 -07003733 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003734
Sage Weil9abf82b2010-05-10 21:58:38 -07003735 mutex_lock(&mdsc->mutex);
3736 __wake_requests(mdsc, &session->s_waiting);
3737 mutex_unlock(&mdsc->mutex);
3738
Sage Weil2f2dc052009-10-06 11:31:09 -07003739 up_read(&mdsc->snap_rwsem);
Yan, Zheng81c5a142019-01-01 16:28:33 +08003740 ceph_pagelist_release(recon_state.pagelist);
Sage Weil2f2dc052009-10-06 11:31:09 -07003741 return;
3742
Sage Weil93cea5b2009-12-23 12:21:51 -08003743fail:
Sage Weil2f2dc052009-10-06 11:31:09 -07003744 ceph_msg_put(reply);
Sage Weil9abf82b2010-05-10 21:58:38 -07003745 up_read(&mdsc->snap_rwsem);
3746 mutex_unlock(&session->s_mutex);
Sage Weil93cea5b2009-12-23 12:21:51 -08003747fail_nomsg:
Yan, Zheng81c5a142019-01-01 16:28:33 +08003748 ceph_pagelist_release(recon_state.pagelist);
Sage Weil93cea5b2009-12-23 12:21:51 -08003749fail_nopagelist:
Sage Weil9abf82b2010-05-10 21:58:38 -07003750 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
Sage Weil9abf82b2010-05-10 21:58:38 -07003751 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07003752}
3753
3754
3755/*
3756 * compare old and new mdsmaps, kicking requests
3757 * and closing out old connections as necessary
3758 *
3759 * called under mdsc->mutex.
3760 */
3761static void check_new_map(struct ceph_mds_client *mdsc,
3762 struct ceph_mdsmap *newmap,
3763 struct ceph_mdsmap *oldmap)
3764{
3765 int i;
3766 int oldstate, newstate;
3767 struct ceph_mds_session *s;
3768
3769 dout("check_new_map new %u old %u\n",
3770 newmap->m_epoch, oldmap->m_epoch);
3771
Yan, Zheng76201b62017-03-28 17:04:13 +08003772 for (i = 0; i < oldmap->m_num_mds && i < mdsc->max_sessions; i++) {
Markus Elfringd37b1d92017-08-20 20:22:02 +02003773 if (!mdsc->sessions[i])
Sage Weil2f2dc052009-10-06 11:31:09 -07003774 continue;
3775 s = mdsc->sessions[i];
3776 oldstate = ceph_mdsmap_get_state(oldmap, i);
3777 newstate = ceph_mdsmap_get_state(newmap, i);
3778
Sage Weil0deb01c2010-06-17 14:19:01 -07003779 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07003780 i, ceph_mds_state_name(oldstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07003781 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
Sage Weil2f2dc052009-10-06 11:31:09 -07003782 ceph_mds_state_name(newstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07003783 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
John Spraya687eca2014-09-19 13:51:08 +01003784 ceph_session_state_name(s->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07003785
Yan, Zheng6f0f5972019-06-10 15:45:09 +08003786 if (i >= newmap->m_num_mds) {
3787 /* force close session for stopped mds */
3788 get_session(s);
3789 __unregister_session(mdsc, s);
3790 __wake_requests(mdsc, &s->s_waiting);
3791 mutex_unlock(&mdsc->mutex);
3792
3793 mutex_lock(&s->s_mutex);
3794 cleanup_session_requests(mdsc, s);
3795 remove_session_caps(s);
3796 mutex_unlock(&s->s_mutex);
3797
3798 ceph_put_mds_session(s);
3799
3800 mutex_lock(&mdsc->mutex);
3801 kick_requests(mdsc, i);
3802 continue;
3803 }
3804
3805 if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
Sage Weil2f2dc052009-10-06 11:31:09 -07003806 ceph_mdsmap_get_addr(newmap, i),
3807 sizeof(struct ceph_entity_addr))) {
Yan, Zheng6f0f5972019-06-10 15:45:09 +08003808 /* just close it */
3809 mutex_unlock(&mdsc->mutex);
3810 mutex_lock(&s->s_mutex);
3811 mutex_lock(&mdsc->mutex);
3812 ceph_con_close(&s->s_con);
3813 mutex_unlock(&s->s_mutex);
3814 s->s_state = CEPH_MDS_SESSION_RESTARTING;
Sage Weil2f2dc052009-10-06 11:31:09 -07003815 } else if (oldstate == newstate) {
3816 continue; /* nothing new with this mds */
3817 }
3818
3819 /*
3820 * send reconnect?
3821 */
3822 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
Sage Weil34b6c852010-05-10 16:31:25 -07003823 newstate >= CEPH_MDS_STATE_RECONNECT) {
3824 mutex_unlock(&mdsc->mutex);
3825 send_mds_reconnect(mdsc, s);
3826 mutex_lock(&mdsc->mutex);
3827 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003828
3829 /*
Sage Weil29790f22010-03-18 14:45:05 -07003830 * kick request on any mds that has gone active.
Sage Weil2f2dc052009-10-06 11:31:09 -07003831 */
3832 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
3833 newstate >= CEPH_MDS_STATE_ACTIVE) {
Sage Weil29790f22010-03-18 14:45:05 -07003834 if (oldstate != CEPH_MDS_STATE_CREATING &&
3835 oldstate != CEPH_MDS_STATE_STARTING)
3836 pr_info("mds%d recovery completed\n", s->s_mds);
3837 kick_requests(mdsc, i);
Sage Weil2f2dc052009-10-06 11:31:09 -07003838 ceph_kick_flushing_caps(mdsc, s);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08003839 wake_up_session_caps(s, RECONNECT);
Sage Weil2f2dc052009-10-06 11:31:09 -07003840 }
3841 }
Sage Weilcb170a22010-06-21 13:38:35 -07003842
Yan, Zheng76201b62017-03-28 17:04:13 +08003843 for (i = 0; i < newmap->m_num_mds && i < mdsc->max_sessions; i++) {
Sage Weilcb170a22010-06-21 13:38:35 -07003844 s = mdsc->sessions[i];
3845 if (!s)
3846 continue;
3847 if (!ceph_mdsmap_is_laggy(newmap, i))
3848 continue;
3849 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3850 s->s_state == CEPH_MDS_SESSION_HUNG ||
3851 s->s_state == CEPH_MDS_SESSION_CLOSING) {
3852 dout(" connecting to export targets of laggy mds%d\n",
3853 i);
3854 __open_export_target_sessions(mdsc, s);
3855 }
3856 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003857}
3858
3859
3860
3861/*
3862 * leases
3863 */
3864
3865/*
3866 * caller must hold session s_mutex, dentry->d_lock
3867 */
3868void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
3869{
3870 struct ceph_dentry_info *di = ceph_dentry(dentry);
3871
3872 ceph_put_mds_session(di->lease_session);
3873 di->lease_session = NULL;
3874}
3875
Sage Weil2600d2d2010-02-22 15:12:16 -08003876static void handle_lease(struct ceph_mds_client *mdsc,
3877 struct ceph_mds_session *session,
3878 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07003879{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003880 struct super_block *sb = mdsc->fsc->sb;
Sage Weil2f2dc052009-10-06 11:31:09 -07003881 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07003882 struct dentry *parent, *dentry;
3883 struct ceph_dentry_info *di;
Sage Weil2600d2d2010-02-22 15:12:16 -08003884 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07003885 struct ceph_mds_lease *h = msg->front.iov_base;
Sage Weil1e5ea232010-06-04 10:05:40 -07003886 u32 seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003887 struct ceph_vino vino;
Sage Weil2f2dc052009-10-06 11:31:09 -07003888 struct qstr dname;
3889 int release = 0;
3890
Sage Weil2f2dc052009-10-06 11:31:09 -07003891 dout("handle_lease from mds%d\n", mds);
3892
3893 /* decode */
3894 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
3895 goto bad;
3896 vino.ino = le64_to_cpu(h->ino);
3897 vino.snap = CEPH_NOSNAP;
Sage Weil1e5ea232010-06-04 10:05:40 -07003898 seq = le32_to_cpu(h->seq);
Yan, Zheng0fcf6c02018-08-03 16:24:49 +08003899 dname.len = get_unaligned_le32(h + 1);
3900 if (msg->front.iov_len < sizeof(*h) + sizeof(u32) + dname.len)
Sage Weil2f2dc052009-10-06 11:31:09 -07003901 goto bad;
Yan, Zheng0fcf6c02018-08-03 16:24:49 +08003902 dname.name = (void *)(h + 1) + sizeof(u32);
Sage Weil2f2dc052009-10-06 11:31:09 -07003903
Sage Weil2f2dc052009-10-06 11:31:09 -07003904 /* lookup inode */
3905 inode = ceph_find_inode(sb, vino);
Sage Weil2f90b852011-07-26 11:28:25 -07003906 dout("handle_lease %s, ino %llx %p %.*s\n",
3907 ceph_lease_op_name(h->action), vino.ino, inode,
Sage Weil1e5ea232010-06-04 10:05:40 -07003908 dname.len, dname.name);
Yan, Zheng6cd3bca2014-09-17 07:45:12 +08003909
3910 mutex_lock(&session->s_mutex);
3911 session->s_seq++;
3912
Markus Elfringd37b1d92017-08-20 20:22:02 +02003913 if (!inode) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003914 dout("handle_lease no inode %llx\n", vino.ino);
3915 goto release;
3916 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003917
3918 /* dentry */
3919 parent = d_find_alias(inode);
3920 if (!parent) {
3921 dout("no parent dentry on inode %p\n", inode);
3922 WARN_ON(1);
3923 goto release; /* hrm... */
3924 }
Linus Torvalds8387ff22016-06-10 07:51:30 -07003925 dname.hash = full_name_hash(parent, dname.name, dname.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07003926 dentry = d_lookup(parent, &dname);
3927 dput(parent);
3928 if (!dentry)
3929 goto release;
3930
3931 spin_lock(&dentry->d_lock);
3932 di = ceph_dentry(dentry);
3933 switch (h->action) {
3934 case CEPH_MDS_LEASE_REVOKE:
Sage Weil3d8eb7a2011-11-11 09:48:53 -08003935 if (di->lease_session == session) {
Sage Weil1e5ea232010-06-04 10:05:40 -07003936 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
3937 h->seq = cpu_to_le32(di->lease_seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003938 __ceph_mdsc_drop_dentry_lease(dentry);
3939 }
3940 release = 1;
3941 break;
3942
3943 case CEPH_MDS_LEASE_RENEW:
Sage Weil3d8eb7a2011-11-11 09:48:53 -08003944 if (di->lease_session == session &&
Sage Weil2f2dc052009-10-06 11:31:09 -07003945 di->lease_gen == session->s_cap_gen &&
3946 di->lease_renew_from &&
3947 di->lease_renew_after == 0) {
3948 unsigned long duration =
Nicholas Mc Guire3563dbdd2015-02-06 06:52:17 -05003949 msecs_to_jiffies(le32_to_cpu(h->duration_ms));
Sage Weil2f2dc052009-10-06 11:31:09 -07003950
Sage Weil1e5ea232010-06-04 10:05:40 -07003951 di->lease_seq = seq;
Miklos Szeredi9b16f03c2016-06-22 16:35:04 +02003952 di->time = di->lease_renew_from + duration;
Sage Weil2f2dc052009-10-06 11:31:09 -07003953 di->lease_renew_after = di->lease_renew_from +
3954 (duration >> 1);
3955 di->lease_renew_from = 0;
3956 }
3957 break;
3958 }
3959 spin_unlock(&dentry->d_lock);
3960 dput(dentry);
3961
3962 if (!release)
3963 goto out;
3964
3965release:
3966 /* let's just reuse the same message */
3967 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
3968 ceph_msg_get(msg);
3969 ceph_con_send(&session->s_con, msg);
3970
3971out:
Sage Weil2f2dc052009-10-06 11:31:09 -07003972 mutex_unlock(&session->s_mutex);
Yan, Zheng3e1d0452019-05-18 20:39:55 +08003973 /* avoid calling iput_final() in mds dispatch threads */
3974 ceph_async_iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07003975 return;
3976
3977bad:
3978 pr_err("corrupt lease message\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08003979 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003980}
3981
3982void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
Sage Weil2f2dc052009-10-06 11:31:09 -07003983 struct dentry *dentry, char action,
3984 u32 seq)
3985{
3986 struct ceph_msg *msg;
3987 struct ceph_mds_lease *lease;
Yan, Zheng8f2a98ef2019-05-23 10:45:24 +08003988 struct inode *dir;
3989 int len = sizeof(*lease) + sizeof(u32) + NAME_MAX;
Sage Weil2f2dc052009-10-06 11:31:09 -07003990
Yan, Zheng8f2a98ef2019-05-23 10:45:24 +08003991 dout("lease_send_msg identry %p %s to mds%d\n",
3992 dentry, ceph_lease_op_name(action), session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003993
Sage Weilb61c2762011-08-09 15:03:46 -07003994 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07003995 if (!msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07003996 return;
3997 lease = msg->front.iov_base;
3998 lease->action = action;
Sage Weil2f2dc052009-10-06 11:31:09 -07003999 lease->seq = cpu_to_le32(seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07004000
Yan, Zheng8f2a98ef2019-05-23 10:45:24 +08004001 spin_lock(&dentry->d_lock);
4002 dir = d_inode(dentry->d_parent);
4003 lease->ino = cpu_to_le64(ceph_ino(dir));
4004 lease->first = lease->last = cpu_to_le64(ceph_snap(dir));
4005
4006 put_unaligned_le32(dentry->d_name.len, lease + 1);
4007 memcpy((void *)(lease + 1) + 4,
4008 dentry->d_name.name, dentry->d_name.len);
4009 spin_unlock(&dentry->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07004010 /*
4011 * if this is a preemptive lease RELEASE, no need to
4012 * flush request stream, since the actual request will
4013 * soon follow.
4014 */
4015 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
4016
4017 ceph_con_send(&session->s_con, msg);
4018}
4019
4020/*
Yan, Zheng7aac4532018-03-13 19:52:20 +08004021 * lock unlock sessions, to wait ongoing session activities
Sage Weil2f2dc052009-10-06 11:31:09 -07004022 */
Yan, Zheng7aac4532018-03-13 19:52:20 +08004023static void lock_unlock_sessions(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07004024{
4025 int i;
4026
Sage Weil2f2dc052009-10-06 11:31:09 -07004027 mutex_lock(&mdsc->mutex);
4028 for (i = 0; i < mdsc->max_sessions; i++) {
4029 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
4030 if (!s)
4031 continue;
4032 mutex_unlock(&mdsc->mutex);
4033 mutex_lock(&s->s_mutex);
4034 mutex_unlock(&s->s_mutex);
4035 ceph_put_mds_session(s);
4036 mutex_lock(&mdsc->mutex);
4037 }
4038 mutex_unlock(&mdsc->mutex);
4039}
4040
Yan, Zheng131d7eb2019-07-25 20:16:47 +08004041static void maybe_recover_session(struct ceph_mds_client *mdsc)
4042{
4043 struct ceph_fs_client *fsc = mdsc->fsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07004044
Yan, Zheng131d7eb2019-07-25 20:16:47 +08004045 if (!ceph_test_mount_opt(fsc, CLEANRECOVER))
4046 return;
4047
4048 if (READ_ONCE(fsc->mount_state) != CEPH_MOUNT_MOUNTED)
4049 return;
4050
4051 if (!READ_ONCE(fsc->blacklisted))
4052 return;
4053
4054 if (fsc->last_auto_reconnect &&
4055 time_before(jiffies, fsc->last_auto_reconnect + HZ * 60 * 30))
4056 return;
4057
4058 pr_info("auto reconnect after blacklisted\n");
4059 fsc->last_auto_reconnect = jiffies;
4060 ceph_force_reconnect(fsc->sb);
4061}
Sage Weil2f2dc052009-10-06 11:31:09 -07004062
4063/*
4064 * delayed work -- periodically trim expired leases, renew caps with mds
4065 */
4066static void schedule_delayed(struct ceph_mds_client *mdsc)
4067{
4068 int delay = 5;
4069 unsigned hz = round_jiffies_relative(HZ * delay);
4070 schedule_delayed_work(&mdsc->delayed_work, hz);
4071}
4072
4073static void delayed_work(struct work_struct *work)
4074{
4075 int i;
4076 struct ceph_mds_client *mdsc =
4077 container_of(work, struct ceph_mds_client, delayed_work.work);
4078 int renew_interval;
4079 int renew_caps;
4080
4081 dout("mdsc delayed_work\n");
Yan, Zheng75c96272017-12-14 15:11:09 +08004082
Sage Weil2f2dc052009-10-06 11:31:09 -07004083 mutex_lock(&mdsc->mutex);
4084 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
4085 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
4086 mdsc->last_renew_caps);
4087 if (renew_caps)
4088 mdsc->last_renew_caps = jiffies;
4089
4090 for (i = 0; i < mdsc->max_sessions; i++) {
4091 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
Markus Elfringd37b1d92017-08-20 20:22:02 +02004092 if (!s)
Sage Weil2f2dc052009-10-06 11:31:09 -07004093 continue;
4094 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
4095 dout("resending session close request for mds%d\n",
4096 s->s_mds);
4097 request_close_session(mdsc, s);
4098 ceph_put_mds_session(s);
4099 continue;
4100 }
4101 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
4102 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
4103 s->s_state = CEPH_MDS_SESSION_HUNG;
4104 pr_info("mds%d hung\n", s->s_mds);
4105 }
4106 }
Erqi Chen71a228b2019-08-28 21:22:45 +08004107 if (s->s_state == CEPH_MDS_SESSION_NEW ||
4108 s->s_state == CEPH_MDS_SESSION_RESTARTING ||
4109 s->s_state == CEPH_MDS_SESSION_REJECTED) {
Sage Weil2f2dc052009-10-06 11:31:09 -07004110 /* this mds is failed or recovering, just wait */
4111 ceph_put_mds_session(s);
4112 continue;
4113 }
4114 mutex_unlock(&mdsc->mutex);
4115
4116 mutex_lock(&s->s_mutex);
4117 if (renew_caps)
4118 send_renew_caps(mdsc, s);
4119 else
4120 ceph_con_keepalive(&s->s_con);
Sage Weilaab53dd2010-03-17 16:30:21 -07004121 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
4122 s->s_state == CEPH_MDS_SESSION_HUNG)
Sage Weil3d7ded42010-06-09 16:47:10 -07004123 ceph_send_cap_releases(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07004124 mutex_unlock(&s->s_mutex);
4125 ceph_put_mds_session(s);
4126
4127 mutex_lock(&mdsc->mutex);
4128 }
4129 mutex_unlock(&mdsc->mutex);
4130
Yan, Zheng37c4efc2019-01-31 16:55:51 +08004131 ceph_check_delayed_caps(mdsc);
4132
4133 ceph_queue_cap_reclaim_work(mdsc);
4134
4135 ceph_trim_snapid_map(mdsc);
4136
Yan, Zheng131d7eb2019-07-25 20:16:47 +08004137 maybe_recover_session(mdsc);
4138
Sage Weil2f2dc052009-10-06 11:31:09 -07004139 schedule_delayed(mdsc);
4140}
4141
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004142int ceph_mdsc_init(struct ceph_fs_client *fsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07004143
Sage Weil2f2dc052009-10-06 11:31:09 -07004144{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004145 struct ceph_mds_client *mdsc;
4146
4147 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
4148 if (!mdsc)
4149 return -ENOMEM;
4150 mdsc->fsc = fsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07004151 mutex_init(&mdsc->mutex);
4152 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
Markus Elfringd37b1d92017-08-20 20:22:02 +02004153 if (!mdsc->mdsmap) {
majianpengfb3101b2013-06-25 14:48:19 +08004154 kfree(mdsc);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08004155 return -ENOMEM;
majianpengfb3101b2013-06-25 14:48:19 +08004156 }
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08004157
Chengguang Xu50c55ae2018-03-14 13:47:33 +08004158 fsc->mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07004159 init_completion(&mdsc->safe_umount_waiters);
Sage Weilf3c60c52010-08-11 14:51:23 -07004160 init_waitqueue_head(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07004161 INIT_LIST_HEAD(&mdsc->waiting_for_map);
4162 mdsc->sessions = NULL;
Yan, Zheng86d8f672015-01-09 17:00:42 +08004163 atomic_set(&mdsc->num_sessions, 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07004164 mdsc->max_sessions = 0;
4165 mdsc->stopping = 0;
Luis Henriquesd557c482018-01-12 17:19:29 +00004166 atomic64_set(&mdsc->quotarealms_count, 0);
Luis Henriques0c44a8e2019-03-21 10:20:10 +00004167 mdsc->quotarealms_inodes = RB_ROOT;
4168 mutex_init(&mdsc->quotarealms_inodes_mutex);
Yan, Zhengaffbc192015-05-05 21:22:13 +08004169 mdsc->last_snap_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07004170 init_rwsem(&mdsc->snap_rwsem);
Sage Weila105f002010-02-15 14:37:55 -08004171 mdsc->snap_realms = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07004172 INIT_LIST_HEAD(&mdsc->snap_empty);
Yan, Zheng81c5a142019-01-01 16:28:33 +08004173 mdsc->num_snap_realms = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07004174 spin_lock_init(&mdsc->snap_empty_lock);
4175 mdsc->last_tid = 0;
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08004176 mdsc->oldest_tid = 0;
Sage Weil44ca18f2010-02-15 12:08:46 -08004177 mdsc->request_tree = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07004178 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
4179 mdsc->last_renew_caps = jiffies;
4180 INIT_LIST_HEAD(&mdsc->cap_delay_list);
Jeff Layton3a3430a2019-11-20 12:00:59 -05004181 INIT_LIST_HEAD(&mdsc->cap_wait_list);
Sage Weil2f2dc052009-10-06 11:31:09 -07004182 spin_lock_init(&mdsc->cap_delay_lock);
4183 INIT_LIST_HEAD(&mdsc->snap_flush_list);
4184 spin_lock_init(&mdsc->snap_flush_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08004185 mdsc->last_cap_flush_tid = 1;
Yan, Zhenge4500b52016-07-06 11:12:56 +08004186 INIT_LIST_HEAD(&mdsc->cap_flush_list);
Sage Weil2f2dc052009-10-06 11:31:09 -07004187 INIT_LIST_HEAD(&mdsc->cap_dirty);
Sage Weildb354052011-05-24 11:46:31 -07004188 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
Sage Weil2f2dc052009-10-06 11:31:09 -07004189 mdsc->num_cap_flushing = 0;
4190 spin_lock_init(&mdsc->cap_dirty_lock);
4191 init_waitqueue_head(&mdsc->cap_flushing_wq);
Yan, Zheng37c4efc2019-01-31 16:55:51 +08004192 INIT_WORK(&mdsc->cap_reclaim_work, ceph_cap_reclaim_work);
Yan, Zhengfe330322019-02-01 14:57:15 +08004193 atomic_set(&mdsc->cap_reclaim_pending, 0);
Yan, Zheng37c4efc2019-01-31 16:55:51 +08004194
4195 spin_lock_init(&mdsc->dentry_list_lock);
4196 INIT_LIST_HEAD(&mdsc->dentry_leases);
4197 INIT_LIST_HEAD(&mdsc->dentry_dir_leases);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08004198
Yehuda Sadeh37151662010-06-17 16:16:12 -07004199 ceph_caps_init(mdsc);
Yan, Zhengfe330322019-02-01 14:57:15 +08004200 ceph_adjust_caps_max_min(mdsc, fsc->mount_options);
Yehuda Sadeh37151662010-06-17 16:16:12 -07004201
Yan, Zheng75c96272017-12-14 15:11:09 +08004202 spin_lock_init(&mdsc->snapid_map_lock);
4203 mdsc->snapid_map_tree = RB_ROOT;
4204 INIT_LIST_HEAD(&mdsc->snapid_map_lru);
4205
Yan, Zheng10183a62015-04-27 15:33:28 +08004206 init_rwsem(&mdsc->pool_perm_rwsem);
4207 mdsc->pool_perm_tree = RB_ROOT;
4208
Yan, Zhengdfeb84d2018-07-02 15:55:23 +08004209 strscpy(mdsc->nodename, utsname()->nodename,
4210 sizeof(mdsc->nodename));
Sage Weil5f44f142009-11-18 14:52:18 -08004211 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07004212}
4213
4214/*
4215 * Wait for safe replies on open mds requests. If we time out, drop
4216 * all requests from the tree to avoid dangling dentry refs.
4217 */
4218static void wait_requests(struct ceph_mds_client *mdsc)
4219{
Ilya Dryomova319bf52015-05-15 12:02:17 +03004220 struct ceph_options *opts = mdsc->fsc->client->options;
Sage Weil2f2dc052009-10-06 11:31:09 -07004221 struct ceph_mds_request *req;
Sage Weil2f2dc052009-10-06 11:31:09 -07004222
4223 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08004224 if (__get_oldest_req(mdsc)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07004225 mutex_unlock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08004226
Sage Weil2f2dc052009-10-06 11:31:09 -07004227 dout("wait_requests waiting for requests\n");
4228 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
Ilya Dryomova319bf52015-05-15 12:02:17 +03004229 ceph_timeout_jiffies(opts->mount_timeout));
Sage Weil2f2dc052009-10-06 11:31:09 -07004230
4231 /* tear down remaining requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08004232 mutex_lock(&mdsc->mutex);
4233 while ((req = __get_oldest_req(mdsc))) {
Sage Weil2f2dc052009-10-06 11:31:09 -07004234 dout("wait_requests timed out on tid %llu\n",
4235 req->r_tid);
Yan, Zheng428138c2019-06-14 10:55:05 +08004236 list_del_init(&req->r_wait);
Sage Weil44ca18f2010-02-15 12:08:46 -08004237 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07004238 }
4239 }
4240 mutex_unlock(&mdsc->mutex);
4241 dout("wait_requests done\n");
4242}
4243
4244/*
4245 * called before mount is ro, and before dentries are torn down.
4246 * (hmm, does this still race with new lookups?)
4247 */
4248void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
4249{
4250 dout("pre_umount\n");
4251 mdsc->stopping = 1;
4252
Yan, Zheng7aac4532018-03-13 19:52:20 +08004253 lock_unlock_sessions(mdsc);
Sage Weilafcdaea2009-10-14 14:27:38 -07004254 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07004255 wait_requests(mdsc);
Sage Weil17c688c2010-06-21 16:12:26 -07004256
4257 /*
4258 * wait for reply handlers to drop their request refs and
4259 * their inode/dcache refs
4260 */
4261 ceph_msgr_flush();
Luis Henriques0c44a8e2019-03-21 10:20:10 +00004262
4263 ceph_cleanup_quotarealms_inodes(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07004264}
4265
4266/*
4267 * wait for all write mds requests to flush.
4268 */
4269static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
4270{
Sage Weil80fc7312010-03-16 15:28:54 -07004271 struct ceph_mds_request *req = NULL, *nextreq;
Sage Weil44ca18f2010-02-15 12:08:46 -08004272 struct rb_node *n;
Sage Weil2f2dc052009-10-06 11:31:09 -07004273
4274 mutex_lock(&mdsc->mutex);
4275 dout("wait_unsafe_requests want %lld\n", want_tid);
Sage Weil80fc7312010-03-16 15:28:54 -07004276restart:
Sage Weil44ca18f2010-02-15 12:08:46 -08004277 req = __get_oldest_req(mdsc);
4278 while (req && req->r_tid <= want_tid) {
Sage Weil80fc7312010-03-16 15:28:54 -07004279 /* find next request */
4280 n = rb_next(&req->r_node);
4281 if (n)
4282 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
4283 else
4284 nextreq = NULL;
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08004285 if (req->r_op != CEPH_MDS_OP_SETFILELOCK &&
4286 (req->r_op & CEPH_MDS_OP_WRITE)) {
Sage Weil44ca18f2010-02-15 12:08:46 -08004287 /* write op */
4288 ceph_mdsc_get_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07004289 if (nextreq)
4290 ceph_mdsc_get_request(nextreq);
Sage Weil44ca18f2010-02-15 12:08:46 -08004291 mutex_unlock(&mdsc->mutex);
4292 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
4293 req->r_tid, want_tid);
4294 wait_for_completion(&req->r_safe_completion);
4295 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08004296 ceph_mdsc_put_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07004297 if (!nextreq)
4298 break; /* next dne before, so we're done! */
4299 if (RB_EMPTY_NODE(&nextreq->r_node)) {
4300 /* next request was removed from tree */
4301 ceph_mdsc_put_request(nextreq);
4302 goto restart;
4303 }
4304 ceph_mdsc_put_request(nextreq); /* won't go away */
Sage Weil44ca18f2010-02-15 12:08:46 -08004305 }
Sage Weil80fc7312010-03-16 15:28:54 -07004306 req = nextreq;
Sage Weil2f2dc052009-10-06 11:31:09 -07004307 }
4308 mutex_unlock(&mdsc->mutex);
4309 dout("wait_unsafe_requests done\n");
4310}
4311
4312void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
4313{
Yan, Zheng0e294382016-07-04 18:06:41 +08004314 u64 want_tid, want_flush;
Sage Weil2f2dc052009-10-06 11:31:09 -07004315
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01004316 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
Sage Weil56b7cf92010-05-03 15:22:00 -07004317 return;
4318
Sage Weil2f2dc052009-10-06 11:31:09 -07004319 dout("sync\n");
4320 mutex_lock(&mdsc->mutex);
4321 want_tid = mdsc->last_tid;
Sage Weil2f2dc052009-10-06 11:31:09 -07004322 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07004323
Sage Weilafcdaea2009-10-14 14:27:38 -07004324 ceph_flush_dirty_caps(mdsc);
Yan, Zhengd3383a82015-01-08 21:30:12 +08004325 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zheng8310b082015-06-09 17:20:12 +08004326 want_flush = mdsc->last_cap_flush_tid;
Yan, Zhengc8799fc2016-07-07 15:22:38 +08004327 if (!list_empty(&mdsc->cap_flush_list)) {
4328 struct ceph_cap_flush *cf =
4329 list_last_entry(&mdsc->cap_flush_list,
4330 struct ceph_cap_flush, g_list);
4331 cf->wake = true;
4332 }
Yan, Zhengd3383a82015-01-08 21:30:12 +08004333 spin_unlock(&mdsc->cap_dirty_lock);
4334
Yan, Zheng0e294382016-07-04 18:06:41 +08004335 dout("sync want tid %lld flush_seq %lld\n",
4336 want_tid, want_flush);
Sage Weil2f2dc052009-10-06 11:31:09 -07004337
4338 wait_unsafe_requests(mdsc, want_tid);
Yan, Zheng0e294382016-07-04 18:06:41 +08004339 wait_caps_flush(mdsc, want_flush);
Sage Weil2f2dc052009-10-06 11:31:09 -07004340}
4341
Sage Weilf3c60c52010-08-11 14:51:23 -07004342/*
4343 * true if all sessions are closed, or we force unmount
4344 */
Yan, Zhengfcff4152016-09-14 16:39:51 +08004345static bool done_closing_sessions(struct ceph_mds_client *mdsc, int skipped)
Sage Weilf3c60c52010-08-11 14:51:23 -07004346{
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01004347 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
Sage Weilf3c60c52010-08-11 14:51:23 -07004348 return true;
Yan, Zhengfcff4152016-09-14 16:39:51 +08004349 return atomic_read(&mdsc->num_sessions) <= skipped;
Sage Weilf3c60c52010-08-11 14:51:23 -07004350}
Sage Weil2f2dc052009-10-06 11:31:09 -07004351
4352/*
4353 * called after sb is ro.
4354 */
4355void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
4356{
Ilya Dryomova319bf52015-05-15 12:02:17 +03004357 struct ceph_options *opts = mdsc->fsc->client->options;
Sage Weil2f2dc052009-10-06 11:31:09 -07004358 struct ceph_mds_session *session;
4359 int i;
Yan, Zhengfcff4152016-09-14 16:39:51 +08004360 int skipped = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07004361
4362 dout("close_sessions\n");
4363
Sage Weil2f2dc052009-10-06 11:31:09 -07004364 /* close sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07004365 mutex_lock(&mdsc->mutex);
4366 for (i = 0; i < mdsc->max_sessions; i++) {
4367 session = __ceph_lookup_mds_session(mdsc, i);
4368 if (!session)
4369 continue;
Sage Weil2f2dc052009-10-06 11:31:09 -07004370 mutex_unlock(&mdsc->mutex);
Sage Weilf3c60c52010-08-11 14:51:23 -07004371 mutex_lock(&session->s_mutex);
Yan, Zhengfcff4152016-09-14 16:39:51 +08004372 if (__close_session(mdsc, session) <= 0)
4373 skipped++;
Sage Weilf3c60c52010-08-11 14:51:23 -07004374 mutex_unlock(&session->s_mutex);
4375 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07004376 mutex_lock(&mdsc->mutex);
4377 }
Sage Weilf3c60c52010-08-11 14:51:23 -07004378 mutex_unlock(&mdsc->mutex);
4379
4380 dout("waiting for sessions to close\n");
Yan, Zhengfcff4152016-09-14 16:39:51 +08004381 wait_event_timeout(mdsc->session_close_wq,
4382 done_closing_sessions(mdsc, skipped),
Ilya Dryomova319bf52015-05-15 12:02:17 +03004383 ceph_timeout_jiffies(opts->mount_timeout));
Sage Weil2f2dc052009-10-06 11:31:09 -07004384
4385 /* tear down remaining sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07004386 mutex_lock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07004387 for (i = 0; i < mdsc->max_sessions; i++) {
4388 if (mdsc->sessions[i]) {
4389 session = get_session(mdsc->sessions[i]);
Sage Weil2600d2d2010-02-22 15:12:16 -08004390 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07004391 mutex_unlock(&mdsc->mutex);
4392 mutex_lock(&session->s_mutex);
4393 remove_session_caps(session);
4394 mutex_unlock(&session->s_mutex);
4395 ceph_put_mds_session(session);
4396 mutex_lock(&mdsc->mutex);
4397 }
4398 }
Sage Weil2f2dc052009-10-06 11:31:09 -07004399 WARN_ON(!list_empty(&mdsc->cap_delay_list));
Sage Weil2f2dc052009-10-06 11:31:09 -07004400 mutex_unlock(&mdsc->mutex);
4401
Yan, Zheng75c96272017-12-14 15:11:09 +08004402 ceph_cleanup_snapid_map(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07004403 ceph_cleanup_empty_realms(mdsc);
4404
Yan, Zheng37c4efc2019-01-31 16:55:51 +08004405 cancel_work_sync(&mdsc->cap_reclaim_work);
Sage Weil2f2dc052009-10-06 11:31:09 -07004406 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
4407
4408 dout("stopped\n");
4409}
4410
Yan, Zheng48fec5d2015-07-01 16:27:46 +08004411void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc)
4412{
4413 struct ceph_mds_session *session;
4414 int mds;
4415
4416 dout("force umount\n");
4417
4418 mutex_lock(&mdsc->mutex);
4419 for (mds = 0; mds < mdsc->max_sessions; mds++) {
4420 session = __ceph_lookup_mds_session(mdsc, mds);
4421 if (!session)
4422 continue;
Yan, Zhengd468e722019-07-25 20:16:44 +08004423
4424 if (session->s_state == CEPH_MDS_SESSION_REJECTED)
4425 __unregister_session(mdsc, session);
4426 __wake_requests(mdsc, &session->s_waiting);
Yan, Zheng48fec5d2015-07-01 16:27:46 +08004427 mutex_unlock(&mdsc->mutex);
Yan, Zhengd468e722019-07-25 20:16:44 +08004428
Yan, Zheng48fec5d2015-07-01 16:27:46 +08004429 mutex_lock(&session->s_mutex);
4430 __close_session(mdsc, session);
4431 if (session->s_state == CEPH_MDS_SESSION_CLOSING) {
4432 cleanup_session_requests(mdsc, session);
4433 remove_session_caps(session);
4434 }
4435 mutex_unlock(&session->s_mutex);
4436 ceph_put_mds_session(session);
Yan, Zhengd468e722019-07-25 20:16:44 +08004437
Yan, Zheng48fec5d2015-07-01 16:27:46 +08004438 mutex_lock(&mdsc->mutex);
4439 kick_requests(mdsc, mds);
4440 }
4441 __wake_requests(mdsc, &mdsc->waiting_for_map);
4442 mutex_unlock(&mdsc->mutex);
4443}
4444
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004445static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07004446{
4447 dout("stop\n");
4448 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
4449 if (mdsc->mdsmap)
4450 ceph_mdsmap_destroy(mdsc->mdsmap);
4451 kfree(mdsc->sessions);
Yehuda Sadeh37151662010-06-17 16:16:12 -07004452 ceph_caps_finalize(mdsc);
Yan, Zheng10183a62015-04-27 15:33:28 +08004453 ceph_pool_perm_destroy(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07004454}
4455
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004456void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
4457{
4458 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weilef550f62011-03-25 13:27:48 -07004459 dout("mdsc_destroy %p\n", mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07004460
Chengguang Xu50c55ae2018-03-14 13:47:33 +08004461 if (!mdsc)
4462 return;
4463
Sage Weilef550f62011-03-25 13:27:48 -07004464 /* flush out any connection work with references to us */
4465 ceph_msgr_flush();
4466
Yan, Zheng62a65f32017-06-22 16:26:34 +08004467 ceph_mdsc_stop(mdsc);
4468
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004469 fsc->mdsc = NULL;
4470 kfree(mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07004471 dout("mdsc_destroy %p done\n", mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004472}
4473
Yan, Zheng430afba2016-07-08 11:25:38 +08004474void ceph_mdsc_handle_fsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
4475{
4476 struct ceph_fs_client *fsc = mdsc->fsc;
4477 const char *mds_namespace = fsc->mount_options->mds_namespace;
4478 void *p = msg->front.iov_base;
4479 void *end = p + msg->front.iov_len;
4480 u32 epoch;
4481 u32 map_len;
4482 u32 num_fs;
4483 u32 mount_fscid = (u32)-1;
4484 u8 struct_v, struct_cv;
4485 int err = -EINVAL;
4486
4487 ceph_decode_need(&p, end, sizeof(u32), bad);
4488 epoch = ceph_decode_32(&p);
4489
4490 dout("handle_fsmap epoch %u\n", epoch);
4491
4492 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
4493 struct_v = ceph_decode_8(&p);
4494 struct_cv = ceph_decode_8(&p);
4495 map_len = ceph_decode_32(&p);
4496
4497 ceph_decode_need(&p, end, sizeof(u32) * 3, bad);
4498 p += sizeof(u32) * 2; /* skip epoch and legacy_client_fscid */
4499
4500 num_fs = ceph_decode_32(&p);
4501 while (num_fs-- > 0) {
4502 void *info_p, *info_end;
4503 u32 info_len;
4504 u8 info_v, info_cv;
4505 u32 fscid, namelen;
4506
4507 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
4508 info_v = ceph_decode_8(&p);
4509 info_cv = ceph_decode_8(&p);
4510 info_len = ceph_decode_32(&p);
4511 ceph_decode_need(&p, end, info_len, bad);
4512 info_p = p;
4513 info_end = p + info_len;
4514 p = info_end;
4515
4516 ceph_decode_need(&info_p, info_end, sizeof(u32) * 2, bad);
4517 fscid = ceph_decode_32(&info_p);
4518 namelen = ceph_decode_32(&info_p);
4519 ceph_decode_need(&info_p, info_end, namelen, bad);
4520
4521 if (mds_namespace &&
4522 strlen(mds_namespace) == namelen &&
4523 !strncmp(mds_namespace, (char *)info_p, namelen)) {
4524 mount_fscid = fscid;
4525 break;
4526 }
4527 }
4528
4529 ceph_monc_got_map(&fsc->client->monc, CEPH_SUB_FSMAP, epoch);
4530 if (mount_fscid != (u32)-1) {
4531 fsc->client->monc.fs_cluster_id = mount_fscid;
4532 ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP,
4533 0, true);
4534 ceph_monc_renew_subs(&fsc->client->monc);
4535 } else {
4536 err = -ENOENT;
4537 goto err_out;
4538 }
4539 return;
Ilya Dryomov76bd6ec2017-10-16 10:32:50 +02004540
Yan, Zheng430afba2016-07-08 11:25:38 +08004541bad:
4542 pr_err("error decoding fsmap\n");
4543err_out:
4544 mutex_lock(&mdsc->mutex);
Ilya Dryomov76bd6ec2017-10-16 10:32:50 +02004545 mdsc->mdsmap_err = err;
Yan, Zheng430afba2016-07-08 11:25:38 +08004546 __wake_requests(mdsc, &mdsc->waiting_for_map);
4547 mutex_unlock(&mdsc->mutex);
Yan, Zheng430afba2016-07-08 11:25:38 +08004548}
Sage Weil2f2dc052009-10-06 11:31:09 -07004549
4550/*
4551 * handle mds map update.
4552 */
Yan, Zheng430afba2016-07-08 11:25:38 +08004553void ceph_mdsc_handle_mdsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07004554{
4555 u32 epoch;
4556 u32 maplen;
4557 void *p = msg->front.iov_base;
4558 void *end = p + msg->front.iov_len;
4559 struct ceph_mdsmap *newmap, *oldmap;
4560 struct ceph_fsid fsid;
4561 int err = -EINVAL;
4562
4563 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
4564 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004565 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
Sage Weil07433042009-11-18 16:50:41 -08004566 return;
Sage Weilc89136e2009-10-14 09:59:09 -07004567 epoch = ceph_decode_32(&p);
4568 maplen = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07004569 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
4570
4571 /* do we need it? */
Sage Weil2f2dc052009-10-06 11:31:09 -07004572 mutex_lock(&mdsc->mutex);
4573 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
4574 dout("handle_map epoch %u <= our %u\n",
4575 epoch, mdsc->mdsmap->m_epoch);
4576 mutex_unlock(&mdsc->mutex);
4577 return;
4578 }
4579
4580 newmap = ceph_mdsmap_decode(&p, end);
4581 if (IS_ERR(newmap)) {
4582 err = PTR_ERR(newmap);
4583 goto bad_unlock;
4584 }
4585
4586 /* swap into place */
4587 if (mdsc->mdsmap) {
4588 oldmap = mdsc->mdsmap;
4589 mdsc->mdsmap = newmap;
4590 check_new_map(mdsc, newmap, oldmap);
4591 ceph_mdsmap_destroy(oldmap);
4592 } else {
4593 mdsc->mdsmap = newmap; /* first mds map */
4594 }
Chengguang Xu719784b2018-07-19 22:15:24 +08004595 mdsc->fsc->max_file_size = min((loff_t)mdsc->mdsmap->m_max_file_size,
4596 MAX_LFS_FILESIZE);
Sage Weil2f2dc052009-10-06 11:31:09 -07004597
4598 __wake_requests(mdsc, &mdsc->waiting_for_map);
Ilya Dryomov82dcaba2016-01-19 16:19:06 +01004599 ceph_monc_got_map(&mdsc->fsc->client->monc, CEPH_SUB_MDSMAP,
4600 mdsc->mdsmap->m_epoch);
Sage Weil2f2dc052009-10-06 11:31:09 -07004601
4602 mutex_unlock(&mdsc->mutex);
4603 schedule_delayed(mdsc);
4604 return;
4605
4606bad_unlock:
4607 mutex_unlock(&mdsc->mutex);
4608bad:
4609 pr_err("error decoding mdsmap %d\n", err);
4610 return;
4611}
4612
4613static struct ceph_connection *con_get(struct ceph_connection *con)
4614{
4615 struct ceph_mds_session *s = con->private;
4616
Chengguang Xu8f5ac172018-07-12 16:45:08 +08004617 if (get_session(s))
Sage Weil2f2dc052009-10-06 11:31:09 -07004618 return con;
Sage Weil2f2dc052009-10-06 11:31:09 -07004619 return NULL;
4620}
4621
4622static void con_put(struct ceph_connection *con)
4623{
4624 struct ceph_mds_session *s = con->private;
4625
Sage Weil2f2dc052009-10-06 11:31:09 -07004626 ceph_put_mds_session(s);
4627}
4628
4629/*
4630 * if the client is unresponsive for long enough, the mds will kill
4631 * the session entirely.
4632 */
4633static void peer_reset(struct ceph_connection *con)
4634{
4635 struct ceph_mds_session *s = con->private;
Sage Weil7e70f0e2010-03-18 13:59:12 -07004636 struct ceph_mds_client *mdsc = s->s_mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07004637
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07004638 pr_warn("mds%d closed our session\n", s->s_mds);
Sage Weil7e70f0e2010-03-18 13:59:12 -07004639 send_mds_reconnect(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07004640}
4641
4642static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
4643{
4644 struct ceph_mds_session *s = con->private;
4645 struct ceph_mds_client *mdsc = s->s_mdsc;
4646 int type = le16_to_cpu(msg->hdr.type);
4647
Sage Weil2600d2d2010-02-22 15:12:16 -08004648 mutex_lock(&mdsc->mutex);
4649 if (__verify_registered_session(mdsc, s) < 0) {
4650 mutex_unlock(&mdsc->mutex);
4651 goto out;
4652 }
4653 mutex_unlock(&mdsc->mutex);
4654
Sage Weil2f2dc052009-10-06 11:31:09 -07004655 switch (type) {
4656 case CEPH_MSG_MDS_MAP:
Yan, Zheng430afba2016-07-08 11:25:38 +08004657 ceph_mdsc_handle_mdsmap(mdsc, msg);
4658 break;
4659 case CEPH_MSG_FS_MAP_USER:
4660 ceph_mdsc_handle_fsmap(mdsc, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004661 break;
4662 case CEPH_MSG_CLIENT_SESSION:
4663 handle_session(s, msg);
4664 break;
4665 case CEPH_MSG_CLIENT_REPLY:
4666 handle_reply(s, msg);
4667 break;
4668 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
Sage Weil2600d2d2010-02-22 15:12:16 -08004669 handle_forward(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004670 break;
4671 case CEPH_MSG_CLIENT_CAPS:
4672 ceph_handle_caps(s, msg);
4673 break;
4674 case CEPH_MSG_CLIENT_SNAP:
Sage Weil2600d2d2010-02-22 15:12:16 -08004675 ceph_handle_snap(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004676 break;
4677 case CEPH_MSG_CLIENT_LEASE:
Sage Weil2600d2d2010-02-22 15:12:16 -08004678 handle_lease(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004679 break;
Luis Henriquesfb18a572018-01-05 10:47:18 +00004680 case CEPH_MSG_CLIENT_QUOTA:
4681 ceph_handle_quota(mdsc, s, msg);
4682 break;
Sage Weil2f2dc052009-10-06 11:31:09 -07004683
4684 default:
4685 pr_err("received unknown message type %d %s\n", type,
4686 ceph_msg_type_name(type));
4687 }
Sage Weil2600d2d2010-02-22 15:12:16 -08004688out:
Sage Weil2f2dc052009-10-06 11:31:09 -07004689 ceph_msg_put(msg);
4690}
4691
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004692/*
4693 * authentication
4694 */
Alex Eldera3530df2012-05-16 15:16:39 -05004695
4696/*
4697 * Note: returned pointer is the address of a structure that's
4698 * managed separately. Caller must *not* attempt to free it.
4699 */
4700static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05004701 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004702{
4703 struct ceph_mds_session *s = con->private;
4704 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004705 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05004706 struct ceph_auth_handshake *auth = &s->s_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004707
Alex Elder74f18692012-05-16 15:16:39 -05004708 if (force_new && auth->authorizer) {
Ilya Dryomov6c1ea262016-04-11 19:34:49 +02004709 ceph_auth_destroy_authorizer(auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05004710 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004711 }
Sage Weil27859f92013-03-25 10:26:14 -07004712 if (!auth->authorizer) {
4713 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4714 auth);
Sage Weil0bed9b52013-03-25 10:26:01 -07004715 if (ret)
4716 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07004717 } else {
4718 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4719 auth);
Alex Eldera2556512012-05-16 15:16:39 -05004720 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05004721 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004722 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004723 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05004724
Alex Eldera3530df2012-05-16 15:16:39 -05004725 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004726}
4727
Ilya Dryomov6daca132018-07-27 19:18:34 +02004728static int add_authorizer_challenge(struct ceph_connection *con,
4729 void *challenge_buf, int challenge_buf_len)
4730{
4731 struct ceph_mds_session *s = con->private;
4732 struct ceph_mds_client *mdsc = s->s_mdsc;
4733 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
4734
4735 return ceph_auth_add_authorizer_challenge(ac, s->s_auth.authorizer,
4736 challenge_buf, challenge_buf_len);
4737}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004738
Ilya Dryomov0dde5842016-12-02 16:35:09 +01004739static int verify_authorizer_reply(struct ceph_connection *con)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004740{
4741 struct ceph_mds_session *s = con->private;
4742 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004743 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004744
Ilya Dryomov0dde5842016-12-02 16:35:09 +01004745 return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004746}
4747
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004748static int invalidate_authorizer(struct ceph_connection *con)
4749{
4750 struct ceph_mds_session *s = con->private;
4751 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004752 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004753
Sage Weil27859f92013-03-25 10:26:14 -07004754 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004755
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004756 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004757}
4758
Alex Elder53ded492013-03-01 18:00:14 -06004759static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
4760 struct ceph_msg_header *hdr, int *skip)
4761{
4762 struct ceph_msg *msg;
4763 int type = (int) le16_to_cpu(hdr->type);
4764 int front_len = (int) le32_to_cpu(hdr->front_len);
4765
4766 if (con->in_msg)
4767 return con->in_msg;
4768
4769 *skip = 0;
4770 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
4771 if (!msg) {
4772 pr_err("unable to allocate msg type %d len %d\n",
4773 type, front_len);
4774 return NULL;
4775 }
Alex Elder53ded492013-03-01 18:00:14 -06004776
4777 return msg;
4778}
4779
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004780static int mds_sign_message(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08004781{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004782 struct ceph_mds_session *s = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08004783 struct ceph_auth_handshake *auth = &s->s_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004784
Yan, Zheng33d07332014-11-04 16:33:37 +08004785 return ceph_auth_sign_message(auth, msg);
4786}
4787
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004788static int mds_check_message_signature(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08004789{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004790 struct ceph_mds_session *s = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08004791 struct ceph_auth_handshake *auth = &s->s_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004792
Yan, Zheng33d07332014-11-04 16:33:37 +08004793 return ceph_auth_check_message_signature(auth, msg);
4794}
4795
Tobias Klauser9e327892010-05-20 10:40:19 +02004796static const struct ceph_connection_operations mds_con_ops = {
Sage Weil2f2dc052009-10-06 11:31:09 -07004797 .get = con_get,
4798 .put = con_put,
4799 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004800 .get_authorizer = get_authorizer,
Ilya Dryomov6daca132018-07-27 19:18:34 +02004801 .add_authorizer_challenge = add_authorizer_challenge,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004802 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004803 .invalidate_authorizer = invalidate_authorizer,
Sage Weil2f2dc052009-10-06 11:31:09 -07004804 .peer_reset = peer_reset,
Alex Elder53ded492013-03-01 18:00:14 -06004805 .alloc_msg = mds_alloc_msg,
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004806 .sign_message = mds_sign_message,
4807 .check_message_signature = mds_check_message_signature,
Sage Weil2f2dc052009-10-06 11:31:09 -07004808};
4809
Sage Weil2f2dc052009-10-06 11:31:09 -07004810/* eof */