blob: 21c33ed048ed7095aa2347159ab472d8550b9721 [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 }
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800153 /* btime, change_attr */
154 {
155 struct ceph_timespec btime;
156 u64 change_attr;
157 ceph_decode_need(p, end, sizeof(btime), bad);
158 ceph_decode_copy(p, &btime, sizeof(btime));
159 ceph_decode_64_safe(p, end, change_attr, bad);
160 }
Yan, Zheng5ea5c5e2016-02-14 18:06:41 +0800161
Yan, Zheng08796872019-01-09 11:07:02 +0800162 /* dir pin */
163 if (struct_v >= 2) {
164 ceph_decode_32_safe(p, end, info->dir_pin, bad);
165 } else {
166 info->dir_pin = -ENODATA;
167 }
168
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800169 *p = end;
170 } else {
171 if (features & CEPH_FEATURE_MDS_INLINE_DATA) {
172 ceph_decode_64_safe(p, end, info->inline_version, bad);
173 ceph_decode_32_safe(p, end, info->inline_len, bad);
174 ceph_decode_need(p, end, info->inline_len, bad);
175 info->inline_data = *p;
176 *p += info->inline_len;
177 } else
178 info->inline_version = CEPH_INLINE_NONE;
179
180 if (features & CEPH_FEATURE_MDS_QUOTA) {
181 err = parse_reply_info_quota(p, end, info);
182 if (err < 0)
183 goto out_bad;
184 } else {
185 info->max_bytes = 0;
186 info->max_files = 0;
187 }
188
189 info->pool_ns_len = 0;
190 info->pool_ns_data = NULL;
191 if (features & CEPH_FEATURE_FS_FILE_LAYOUT_V2) {
192 ceph_decode_32_safe(p, end, info->pool_ns_len, bad);
193 if (info->pool_ns_len > 0) {
194 ceph_decode_need(p, end, info->pool_ns_len, bad);
195 info->pool_ns_data = *p;
196 *p += info->pool_ns_len;
197 }
198 }
Yan, Zheng08796872019-01-09 11:07:02 +0800199
200 info->dir_pin = -ENODATA;
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800201 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700202 return 0;
203bad:
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800204 err = -EIO;
205out_bad:
Sage Weil2f2dc052009-10-06 11:31:09 -0700206 return err;
207}
208
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800209static int parse_reply_info_dir(void **p, void *end,
210 struct ceph_mds_reply_dirfrag **dirfrag,
211 u64 features)
212{
213 if (features == (u64)-1) {
214 u8 struct_v, struct_compat;
215 u32 struct_len;
216 ceph_decode_8_safe(p, end, struct_v, bad);
217 ceph_decode_8_safe(p, end, struct_compat, bad);
218 /* struct_v is expected to be >= 1. we only understand
219 * encoding whose struct_compat == 1. */
220 if (!struct_v || struct_compat != 1)
221 goto bad;
222 ceph_decode_32_safe(p, end, struct_len, bad);
223 ceph_decode_need(p, end, struct_len, bad);
224 end = *p + struct_len;
225 }
226
227 ceph_decode_need(p, end, sizeof(**dirfrag), bad);
228 *dirfrag = *p;
229 *p += sizeof(**dirfrag) + sizeof(u32) * le32_to_cpu((*dirfrag)->ndist);
230 if (unlikely(*p > end))
231 goto bad;
232 if (features == (u64)-1)
233 *p = end;
234 return 0;
235bad:
236 return -EIO;
237}
238
239static int parse_reply_info_lease(void **p, void *end,
240 struct ceph_mds_reply_lease **lease,
241 u64 features)
242{
243 if (features == (u64)-1) {
244 u8 struct_v, struct_compat;
245 u32 struct_len;
246 ceph_decode_8_safe(p, end, struct_v, bad);
247 ceph_decode_8_safe(p, end, struct_compat, bad);
248 /* struct_v is expected to be >= 1. we only understand
249 * encoding whose struct_compat == 1. */
250 if (!struct_v || struct_compat != 1)
251 goto bad;
252 ceph_decode_32_safe(p, end, struct_len, bad);
253 ceph_decode_need(p, end, struct_len, bad);
254 end = *p + struct_len;
255 }
256
257 ceph_decode_need(p, end, sizeof(**lease), bad);
258 *lease = *p;
259 *p += sizeof(**lease);
260 if (features == (u64)-1)
261 *p = end;
262 return 0;
263bad:
264 return -EIO;
265}
266
Sage Weil2f2dc052009-10-06 11:31:09 -0700267/*
268 * parse a normal reply, which may contain a (dir+)dentry and/or a
269 * target inode.
270 */
271static int parse_reply_info_trace(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800272 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200273 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700274{
275 int err;
276
277 if (info->head->is_dentry) {
Sage Weil14303d22010-12-14 17:37:52 -0800278 err = parse_reply_info_in(p, end, &info->diri, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700279 if (err < 0)
280 goto out_bad;
281
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800282 err = parse_reply_info_dir(p, end, &info->dirfrag, features);
283 if (err < 0)
284 goto out_bad;
Sage Weil2f2dc052009-10-06 11:31:09 -0700285
286 ceph_decode_32_safe(p, end, info->dname_len, bad);
287 ceph_decode_need(p, end, info->dname_len, bad);
288 info->dname = *p;
289 *p += info->dname_len;
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800290
291 err = parse_reply_info_lease(p, end, &info->dlease, features);
292 if (err < 0)
293 goto out_bad;
Sage Weil2f2dc052009-10-06 11:31:09 -0700294 }
295
296 if (info->head->is_target) {
Sage Weil14303d22010-12-14 17:37:52 -0800297 err = parse_reply_info_in(p, end, &info->targeti, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700298 if (err < 0)
299 goto out_bad;
300 }
301
302 if (unlikely(*p != end))
303 goto bad;
304 return 0;
305
306bad:
307 err = -EIO;
308out_bad:
309 pr_err("problem parsing mds trace %d\n", err);
310 return err;
311}
312
313/*
314 * parse readdir results
315 */
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800316static int parse_reply_info_readdir(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800317 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200318 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700319{
320 u32 num, i = 0;
321 int err;
322
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800323 err = parse_reply_info_dir(p, end, &info->dir_dir, features);
324 if (err < 0)
325 goto out_bad;
Sage Weil2f2dc052009-10-06 11:31:09 -0700326
327 ceph_decode_need(p, end, sizeof(num) + 2, bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700328 num = ceph_decode_32(p);
Yan, Zheng956d39d2016-04-27 17:48:30 +0800329 {
330 u16 flags = ceph_decode_16(p);
331 info->dir_end = !!(flags & CEPH_READDIR_FRAG_END);
332 info->dir_complete = !!(flags & CEPH_READDIR_FRAG_COMPLETE);
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800333 info->hash_order = !!(flags & CEPH_READDIR_HASH_ORDER);
Yan, Zheng79162542017-04-05 12:54:05 -0400334 info->offset_hash = !!(flags & CEPH_READDIR_OFFSET_HASH);
Yan, Zheng956d39d2016-04-27 17:48:30 +0800335 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700336 if (num == 0)
337 goto done;
338
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800339 BUG_ON(!info->dir_entries);
340 if ((unsigned long)(info->dir_entries + num) >
341 (unsigned long)info->dir_entries + info->dir_buf_size) {
Yan, Zheng54008392014-03-29 13:41:15 +0800342 pr_err("dir contents are larger than expected\n");
343 WARN_ON(1);
344 goto bad;
345 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700346
Yan, Zheng54008392014-03-29 13:41:15 +0800347 info->dir_nr = num;
Sage Weil2f2dc052009-10-06 11:31:09 -0700348 while (num) {
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800349 struct ceph_mds_reply_dir_entry *rde = info->dir_entries + i;
Sage Weil2f2dc052009-10-06 11:31:09 -0700350 /* dentry */
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800351 ceph_decode_32_safe(p, end, rde->name_len, bad);
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800352 ceph_decode_need(p, end, rde->name_len, bad);
353 rde->name = *p;
354 *p += rde->name_len;
355 dout("parsed dir dname '%.*s'\n", rde->name_len, rde->name);
Sage Weil2f2dc052009-10-06 11:31:09 -0700356
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800357 /* dentry lease */
358 err = parse_reply_info_lease(p, end, &rde->lease, features);
359 if (err)
360 goto out_bad;
Sage Weil2f2dc052009-10-06 11:31:09 -0700361 /* inode */
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800362 err = parse_reply_info_in(p, end, &rde->inode, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700363 if (err < 0)
364 goto out_bad;
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800365 /* ceph_readdir_prepopulate() will update it */
366 rde->offset = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -0700367 i++;
368 num--;
369 }
370
371done:
372 if (*p != end)
373 goto bad;
374 return 0;
375
376bad:
377 err = -EIO;
378out_bad:
379 pr_err("problem parsing dir contents %d\n", err);
380 return err;
381}
382
383/*
Herb Shiu25933ab2010-12-01 14:14:38 -0800384 * parse fcntl F_GETLK results
385 */
386static int parse_reply_info_filelock(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800387 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200388 u64 features)
Herb Shiu25933ab2010-12-01 14:14:38 -0800389{
390 if (*p + sizeof(*info->filelock_reply) > end)
391 goto bad;
392
393 info->filelock_reply = *p;
394 *p += sizeof(*info->filelock_reply);
395
396 if (unlikely(*p != end))
397 goto bad;
398 return 0;
399
400bad:
401 return -EIO;
402}
403
404/*
Sam Lang6e8575f2012-12-28 09:56:46 -0800405 * parse create results
406 */
407static int parse_reply_info_create(void **p, void *end,
408 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200409 u64 features)
Sam Lang6e8575f2012-12-28 09:56:46 -0800410{
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800411 if (features == (u64)-1 ||
412 (features & CEPH_FEATURE_REPLY_CREATE_INODE)) {
Sam Lang6e8575f2012-12-28 09:56:46 -0800413 if (*p == end) {
414 info->has_create_ino = false;
415 } else {
416 info->has_create_ino = true;
417 info->ino = ceph_decode_64(p);
418 }
419 }
420
421 if (unlikely(*p != end))
422 goto bad;
423 return 0;
424
425bad:
426 return -EIO;
427}
428
429/*
Herb Shiu25933ab2010-12-01 14:14:38 -0800430 * parse extra results
431 */
432static int parse_reply_info_extra(void **p, void *end,
Sage Weil14303d22010-12-14 17:37:52 -0800433 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200434 u64 features)
Herb Shiu25933ab2010-12-01 14:14:38 -0800435{
Jeff Layton6df8c9d2017-01-12 14:42:41 -0500436 u32 op = le32_to_cpu(info->head->op);
437
438 if (op == CEPH_MDS_OP_GETFILELOCK)
Sage Weil14303d22010-12-14 17:37:52 -0800439 return parse_reply_info_filelock(p, end, info, features);
Jeff Layton6df8c9d2017-01-12 14:42:41 -0500440 else if (op == CEPH_MDS_OP_READDIR || op == CEPH_MDS_OP_LSSNAP)
Yan, Zhengb37fe1f2019-01-09 10:10:17 +0800441 return parse_reply_info_readdir(p, end, info, features);
Jeff Layton6df8c9d2017-01-12 14:42:41 -0500442 else if (op == CEPH_MDS_OP_CREATE)
Sam Lang6e8575f2012-12-28 09:56:46 -0800443 return parse_reply_info_create(p, end, info, features);
444 else
445 return -EIO;
Herb Shiu25933ab2010-12-01 14:14:38 -0800446}
447
448/*
Sage Weil2f2dc052009-10-06 11:31:09 -0700449 * parse entire mds reply
450 */
451static int parse_reply_info(struct ceph_msg *msg,
Sage Weil14303d22010-12-14 17:37:52 -0800452 struct ceph_mds_reply_info_parsed *info,
Ilya Dryomov12b46292013-12-24 21:19:23 +0200453 u64 features)
Sage Weil2f2dc052009-10-06 11:31:09 -0700454{
455 void *p, *end;
456 u32 len;
457 int err;
458
459 info->head = msg->front.iov_base;
460 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
461 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
462
463 /* trace */
464 ceph_decode_32_safe(&p, end, len, bad);
465 if (len > 0) {
Xi Wang32852a82012-01-14 22:20:59 -0500466 ceph_decode_need(&p, end, len, bad);
Sage Weil14303d22010-12-14 17:37:52 -0800467 err = parse_reply_info_trace(&p, p+len, info, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700468 if (err < 0)
469 goto out_bad;
470 }
471
Herb Shiu25933ab2010-12-01 14:14:38 -0800472 /* extra */
Sage Weil2f2dc052009-10-06 11:31:09 -0700473 ceph_decode_32_safe(&p, end, len, bad);
474 if (len > 0) {
Xi Wang32852a82012-01-14 22:20:59 -0500475 ceph_decode_need(&p, end, len, bad);
Sage Weil14303d22010-12-14 17:37:52 -0800476 err = parse_reply_info_extra(&p, p+len, info, features);
Sage Weil2f2dc052009-10-06 11:31:09 -0700477 if (err < 0)
478 goto out_bad;
479 }
480
481 /* snap blob */
482 ceph_decode_32_safe(&p, end, len, bad);
483 info->snapblob_len = len;
484 info->snapblob = p;
485 p += len;
486
487 if (p != end)
488 goto bad;
489 return 0;
490
491bad:
492 err = -EIO;
493out_bad:
494 pr_err("mds parse_reply err %d\n", err);
495 return err;
496}
497
498static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
499{
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800500 if (!info->dir_entries)
Yan, Zheng54008392014-03-29 13:41:15 +0800501 return;
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800502 free_pages((unsigned long)info->dir_entries, get_order(info->dir_buf_size));
Sage Weil2f2dc052009-10-06 11:31:09 -0700503}
504
505
506/*
507 * sessions
508 */
John Spraya687eca2014-09-19 13:51:08 +0100509const char *ceph_session_state_name(int s)
Sage Weil2f2dc052009-10-06 11:31:09 -0700510{
511 switch (s) {
512 case CEPH_MDS_SESSION_NEW: return "new";
513 case CEPH_MDS_SESSION_OPENING: return "opening";
514 case CEPH_MDS_SESSION_OPEN: return "open";
515 case CEPH_MDS_SESSION_HUNG: return "hung";
516 case CEPH_MDS_SESSION_CLOSING: return "closing";
Sage Weil44ca18f2010-02-15 12:08:46 -0800517 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
Sage Weil2f2dc052009-10-06 11:31:09 -0700518 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
Yan, Zhengfcff4152016-09-14 16:39:51 +0800519 case CEPH_MDS_SESSION_REJECTED: return "rejected";
Sage Weil2f2dc052009-10-06 11:31:09 -0700520 default: return "???";
521 }
522}
523
524static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
525{
Elena Reshetova3997c012017-03-03 11:15:06 +0200526 if (refcount_inc_not_zero(&s->s_ref)) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700527 dout("mdsc get_session %p %d -> %d\n", s,
Elena Reshetova3997c012017-03-03 11:15:06 +0200528 refcount_read(&s->s_ref)-1, refcount_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -0700529 return s;
530 } else {
Chengguang Xu4c069a52018-01-30 16:29:17 +0800531 dout("mdsc get_session %p 0 -- FAIL\n", s);
Sage Weil2f2dc052009-10-06 11:31:09 -0700532 return NULL;
533 }
534}
535
536void ceph_put_mds_session(struct ceph_mds_session *s)
537{
538 dout("mdsc put_session %p %d -> %d\n", s,
Elena Reshetova3997c012017-03-03 11:15:06 +0200539 refcount_read(&s->s_ref), refcount_read(&s->s_ref)-1);
540 if (refcount_dec_and_test(&s->s_ref)) {
Alex Elder6c4a1912012-05-16 15:16:38 -0500541 if (s->s_auth.authorizer)
Ilya Dryomov6c1ea262016-04-11 19:34:49 +0200542 ceph_auth_destroy_authorizer(s->s_auth.authorizer);
Sage Weil2f2dc052009-10-06 11:31:09 -0700543 kfree(s);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800544 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700545}
546
547/*
548 * called under mdsc->mutex
549 */
550struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
551 int mds)
552{
553 struct ceph_mds_session *session;
554
Markus Elfringd37b1d92017-08-20 20:22:02 +0200555 if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
Sage Weil2f2dc052009-10-06 11:31:09 -0700556 return NULL;
557 session = mdsc->sessions[mds];
558 dout("lookup_mds_session %p %d\n", session,
Elena Reshetova3997c012017-03-03 11:15:06 +0200559 refcount_read(&session->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -0700560 get_session(session);
561 return session;
562}
563
564static bool __have_session(struct ceph_mds_client *mdsc, int mds)
565{
Chengguang Xu98cfda82018-03-13 10:43:45 +0800566 if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
Sage Weil2f2dc052009-10-06 11:31:09 -0700567 return false;
Chengguang Xu98cfda82018-03-13 10:43:45 +0800568 else
569 return true;
Sage Weil2f2dc052009-10-06 11:31:09 -0700570}
571
Sage Weil2600d2d2010-02-22 15:12:16 -0800572static int __verify_registered_session(struct ceph_mds_client *mdsc,
573 struct ceph_mds_session *s)
574{
575 if (s->s_mds >= mdsc->max_sessions ||
576 mdsc->sessions[s->s_mds] != s)
577 return -ENOENT;
578 return 0;
579}
580
Sage Weil2f2dc052009-10-06 11:31:09 -0700581/*
582 * create+register a new session for given mds.
583 * called under mdsc->mutex.
584 */
585static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
586 int mds)
587{
588 struct ceph_mds_session *s;
589
Yan, Zheng76201b62017-03-28 17:04:13 +0800590 if (mds >= mdsc->mdsmap->m_num_mds)
Nathaniel Yazdanic338c072013-08-04 21:04:30 -0700591 return ERR_PTR(-EINVAL);
592
Sage Weil2f2dc052009-10-06 11:31:09 -0700593 s = kzalloc(sizeof(*s), GFP_NOFS);
Dan Carpenter4736b002010-03-20 15:30:16 +0300594 if (!s)
595 return ERR_PTR(-ENOMEM);
Chengguang Xu47474d02018-03-13 23:01:07 +0800596
597 if (mds >= mdsc->max_sessions) {
598 int newmax = 1 << get_count_order(mds + 1);
599 struct ceph_mds_session **sa;
600
601 dout("%s: realloc to %d\n", __func__, newmax);
602 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
603 if (!sa)
604 goto fail_realloc;
605 if (mdsc->sessions) {
606 memcpy(sa, mdsc->sessions,
607 mdsc->max_sessions * sizeof(void *));
608 kfree(mdsc->sessions);
609 }
610 mdsc->sessions = sa;
611 mdsc->max_sessions = newmax;
612 }
613
614 dout("%s: mds%d\n", __func__, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -0700615 s->s_mdsc = mdsc;
616 s->s_mds = mds;
617 s->s_state = CEPH_MDS_SESSION_NEW;
618 s->s_ttl = 0;
619 s->s_seq = 0;
620 mutex_init(&s->s_mutex);
621
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700622 ceph_con_init(&s->s_con, s, &mds_con_ops, &mdsc->fsc->client->msgr);
Sage Weil2f2dc052009-10-06 11:31:09 -0700623
Alex Elderd8fb02a2012-01-12 17:48:10 -0800624 spin_lock_init(&s->s_gen_ttl_lock);
Yan, Zheng1e9c2eb2019-01-28 20:43:55 +0800625 s->s_cap_gen = 1;
Alex Elder1ce208a2012-01-12 17:48:11 -0800626 s->s_cap_ttl = jiffies - 1;
Alex Elderd8fb02a2012-01-12 17:48:10 -0800627
628 spin_lock_init(&s->s_cap_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700629 s->s_renew_requested = 0;
630 s->s_renew_seq = 0;
631 INIT_LIST_HEAD(&s->s_caps);
632 s->s_nr_caps = 0;
Sage Weil5dacf092009-12-21 20:40:34 -0800633 s->s_trim_caps = 0;
Elena Reshetova3997c012017-03-03 11:15:06 +0200634 refcount_set(&s->s_ref, 1);
Sage Weil2f2dc052009-10-06 11:31:09 -0700635 INIT_LIST_HEAD(&s->s_waiting);
636 INIT_LIST_HEAD(&s->s_unsafe);
637 s->s_num_cap_releases = 0;
Yan, Zheng99a9c272013-09-22 11:08:14 +0800638 s->s_cap_reconnect = 0;
Sage Weil7c1332b2010-02-16 11:39:45 -0800639 s->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700640 INIT_LIST_HEAD(&s->s_cap_releases);
Yan, Zhenge3ec8d62019-01-14 17:21:19 +0800641 INIT_WORK(&s->s_cap_release_work, ceph_cap_release_work);
642
Sage Weil2f2dc052009-10-06 11:31:09 -0700643 INIT_LIST_HEAD(&s->s_cap_flushing);
Sage Weil2f2dc052009-10-06 11:31:09 -0700644
Sage Weil2f2dc052009-10-06 11:31:09 -0700645 mdsc->sessions[mds] = s;
Yan, Zheng86d8f672015-01-09 17:00:42 +0800646 atomic_inc(&mdsc->num_sessions);
Elena Reshetova3997c012017-03-03 11:15:06 +0200647 refcount_inc(&s->s_ref); /* one ref to sessions[], one to caller */
Sage Weil42ce56e2009-11-18 11:22:36 -0800648
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700649 ceph_con_open(&s->s_con, CEPH_ENTITY_TYPE_MDS, mds,
650 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
Sage Weil42ce56e2009-11-18 11:22:36 -0800651
Sage Weil2f2dc052009-10-06 11:31:09 -0700652 return s;
Sage Weil42ce56e2009-11-18 11:22:36 -0800653
654fail_realloc:
655 kfree(s);
656 return ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -0700657}
658
659/*
660 * called under mdsc->mutex
661 */
Sage Weil2600d2d2010-02-22 15:12:16 -0800662static void __unregister_session(struct ceph_mds_client *mdsc,
Sage Weil42ce56e2009-11-18 11:22:36 -0800663 struct ceph_mds_session *s)
Sage Weil2f2dc052009-10-06 11:31:09 -0700664{
Sage Weil2600d2d2010-02-22 15:12:16 -0800665 dout("__unregister_session mds%d %p\n", s->s_mds, s);
666 BUG_ON(mdsc->sessions[s->s_mds] != s);
Sage Weil42ce56e2009-11-18 11:22:36 -0800667 mdsc->sessions[s->s_mds] = NULL;
Yan, Zhenge3ec8d62019-01-14 17:21:19 +0800668 s->s_state = 0;
Sage Weil42ce56e2009-11-18 11:22:36 -0800669 ceph_con_close(&s->s_con);
670 ceph_put_mds_session(s);
Yan, Zheng86d8f672015-01-09 17:00:42 +0800671 atomic_dec(&mdsc->num_sessions);
Sage Weil2f2dc052009-10-06 11:31:09 -0700672}
673
674/*
675 * drop session refs in request.
676 *
677 * should be last request ref, or hold mdsc->mutex
678 */
679static void put_request_session(struct ceph_mds_request *req)
680{
681 if (req->r_session) {
682 ceph_put_mds_session(req->r_session);
683 req->r_session = NULL;
684 }
685}
686
Sage Weil153c8e62009-12-07 12:31:09 -0800687void ceph_mdsc_release_request(struct kref *kref)
Sage Weil2f2dc052009-10-06 11:31:09 -0700688{
Sage Weil153c8e62009-12-07 12:31:09 -0800689 struct ceph_mds_request *req = container_of(kref,
690 struct ceph_mds_request,
691 r_kref);
Yan, Zheng54008392014-03-29 13:41:15 +0800692 destroy_reply_info(&req->r_reply_info);
Sage Weil153c8e62009-12-07 12:31:09 -0800693 if (req->r_request)
694 ceph_msg_put(req->r_request);
Yan, Zheng54008392014-03-29 13:41:15 +0800695 if (req->r_reply)
Sage Weil153c8e62009-12-07 12:31:09 -0800696 ceph_msg_put(req->r_reply);
Sage Weil153c8e62009-12-07 12:31:09 -0800697 if (req->r_inode) {
Sage Weil41b02e12011-07-26 11:31:14 -0700698 ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
Sage Weil153c8e62009-12-07 12:31:09 -0800699 iput(req->r_inode);
700 }
Jeff Layton3dd69aa2017-01-31 10:28:26 -0500701 if (req->r_parent)
702 ceph_put_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
SF Markus Elfringe96a6502014-11-02 15:20:59 +0100703 iput(req->r_target_inode);
Sage Weil153c8e62009-12-07 12:31:09 -0800704 if (req->r_dentry)
705 dput(req->r_dentry);
Sage Weil844d87c2013-02-05 13:40:09 -0800706 if (req->r_old_dentry)
707 dput(req->r_old_dentry);
708 if (req->r_old_dentry_dir) {
Sage Weil41b02e12011-07-26 11:31:14 -0700709 /*
710 * track (and drop pins for) r_old_dentry_dir
711 * separately, since r_old_dentry's d_parent may have
712 * changed between the dir mutex being dropped and
713 * this request being freed.
714 */
715 ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir),
716 CEPH_CAP_PIN);
Sage Weil41b02e12011-07-26 11:31:14 -0700717 iput(req->r_old_dentry_dir);
Sage Weil153c8e62009-12-07 12:31:09 -0800718 }
719 kfree(req->r_path1);
720 kfree(req->r_path2);
Yan, Zheng25e6bae2014-09-16 19:15:28 +0800721 if (req->r_pagelist)
722 ceph_pagelist_release(req->r_pagelist);
Sage Weil153c8e62009-12-07 12:31:09 -0800723 put_request_session(req);
Yehuda Sadeh37151662010-06-17 16:16:12 -0700724 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
Sage Weil153c8e62009-12-07 12:31:09 -0800725 kfree(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700726}
727
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200728DEFINE_RB_FUNCS(request, struct ceph_mds_request, r_tid, r_node)
729
Sage Weil2f2dc052009-10-06 11:31:09 -0700730/*
731 * lookup session, bump ref if found.
732 *
733 * called under mdsc->mutex.
734 */
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200735static struct ceph_mds_request *
736lookup_get_request(struct ceph_mds_client *mdsc, u64 tid)
Sage Weil2f2dc052009-10-06 11:31:09 -0700737{
738 struct ceph_mds_request *req;
Sage Weil44ca18f2010-02-15 12:08:46 -0800739
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200740 req = lookup_request(&mdsc->request_tree, tid);
741 if (req)
742 ceph_mdsc_get_request(req);
Sage Weil44ca18f2010-02-15 12:08:46 -0800743
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200744 return req;
Sage Weil2f2dc052009-10-06 11:31:09 -0700745}
746
747/*
748 * Register an in-flight request, and assign a tid. Link to directory
749 * are modifying (if any).
750 *
751 * Called under mdsc->mutex.
752 */
753static void __register_request(struct ceph_mds_client *mdsc,
754 struct ceph_mds_request *req,
755 struct inode *dir)
756{
Zhi Zhange30ee582018-01-24 21:24:33 +0800757 int ret = 0;
758
Sage Weil2f2dc052009-10-06 11:31:09 -0700759 req->r_tid = ++mdsc->last_tid;
Zhi Zhange30ee582018-01-24 21:24:33 +0800760 if (req->r_num_caps) {
761 ret = ceph_reserve_caps(mdsc, &req->r_caps_reservation,
762 req->r_num_caps);
763 if (ret < 0) {
764 pr_err("__register_request %p "
765 "failed to reserve caps: %d\n", req, ret);
766 /* set req->r_err to fail early from __do_request */
767 req->r_err = ret;
768 return;
769 }
770 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700771 dout("__register_request %p tid %lld\n", req, req->r_tid);
772 ceph_mdsc_get_request(req);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200773 insert_request(&mdsc->request_tree, req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700774
Sage Weilcb4276c2010-11-08 07:28:52 -0800775 req->r_uid = current_fsuid();
776 req->r_gid = current_fsgid();
777
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +0800778 if (mdsc->oldest_tid == 0 && req->r_op != CEPH_MDS_OP_SETFILELOCK)
779 mdsc->oldest_tid = req->r_tid;
780
Sage Weil2f2dc052009-10-06 11:31:09 -0700781 if (dir) {
Sage Weil3b663782011-05-18 16:12:12 -0700782 ihold(dir);
Sage Weil2f2dc052009-10-06 11:31:09 -0700783 req->r_unsafe_dir = dir;
Sage Weil2f2dc052009-10-06 11:31:09 -0700784 }
785}
786
787static void __unregister_request(struct ceph_mds_client *mdsc,
788 struct ceph_mds_request *req)
789{
790 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +0800791
Jeff Laytondf963ea2017-02-14 10:09:40 -0500792 /* Never leave an unregistered request on an unsafe list! */
793 list_del_init(&req->r_unsafe_item);
794
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +0800795 if (req->r_tid == mdsc->oldest_tid) {
796 struct rb_node *p = rb_next(&req->r_node);
797 mdsc->oldest_tid = 0;
798 while (p) {
799 struct ceph_mds_request *next_req =
800 rb_entry(p, struct ceph_mds_request, r_node);
801 if (next_req->r_op != CEPH_MDS_OP_SETFILELOCK) {
802 mdsc->oldest_tid = next_req->r_tid;
803 break;
804 }
805 p = rb_next(p);
806 }
807 }
808
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200809 erase_request(&mdsc->request_tree, req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700810
Jeff Laytonbc2de10d2017-02-01 13:49:09 -0500811 if (req->r_unsafe_dir &&
812 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700813 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
Sage Weil2f2dc052009-10-06 11:31:09 -0700814 spin_lock(&ci->i_unsafe_lock);
815 list_del_init(&req->r_unsafe_dir_item);
816 spin_unlock(&ci->i_unsafe_lock);
Yan, Zheng4c06ace2015-10-27 17:18:00 +0800817 }
Jeff Laytonbc2de10d2017-02-01 13:49:09 -0500818 if (req->r_target_inode &&
819 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Yan, Zheng68cd5b42015-10-27 18:36:06 +0800820 struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
821 spin_lock(&ci->i_unsafe_lock);
822 list_del_init(&req->r_unsafe_target_item);
823 spin_unlock(&ci->i_unsafe_lock);
824 }
Sage Weil3b663782011-05-18 16:12:12 -0700825
Yan, Zheng4c06ace2015-10-27 17:18:00 +0800826 if (req->r_unsafe_dir) {
Sage Weil3b663782011-05-18 16:12:12 -0700827 iput(req->r_unsafe_dir);
828 req->r_unsafe_dir = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700829 }
Sage Weil94aa8ae2010-03-28 21:22:50 -0700830
Yan, Zhengfc55d2c2013-10-31 09:10:47 +0800831 complete_all(&req->r_safe_completion);
832
Sage Weil94aa8ae2010-03-28 21:22:50 -0700833 ceph_mdsc_put_request(req);
Sage Weil2f2dc052009-10-06 11:31:09 -0700834}
835
836/*
Jeff Layton30c71232016-12-15 08:37:56 -0500837 * Walk back up the dentry tree until we hit a dentry representing a
838 * non-snapshot inode. We do this using the rcu_read_lock (which must be held
839 * when calling this) to ensure that the objects won't disappear while we're
840 * working with them. Once we hit a candidate dentry, we attempt to take a
841 * reference to it, and return that as the result.
842 */
Dan Carpenterf1075482017-02-23 13:39:59 +0300843static struct inode *get_nonsnap_parent(struct dentry *dentry)
844{
845 struct inode *inode = NULL;
Jeff Layton30c71232016-12-15 08:37:56 -0500846
847 while (dentry && !IS_ROOT(dentry)) {
848 inode = d_inode_rcu(dentry);
849 if (!inode || ceph_snap(inode) == CEPH_NOSNAP)
850 break;
851 dentry = dentry->d_parent;
852 }
853 if (inode)
854 inode = igrab(inode);
855 return inode;
856}
857
858/*
Sage Weil2f2dc052009-10-06 11:31:09 -0700859 * Choose mds to send request to next. If there is a hint set in the
860 * request (e.g., due to a prior forward hint from the mds), use that.
861 * Otherwise, consult frag tree and/or caps to identify the
862 * appropriate mds. If all else fails, choose randomly.
863 *
864 * Called under mdsc->mutex.
865 */
866static int __choose_mds(struct ceph_mds_client *mdsc,
867 struct ceph_mds_request *req)
868{
869 struct inode *inode;
870 struct ceph_inode_info *ci;
871 struct ceph_cap *cap;
872 int mode = req->r_direct_mode;
873 int mds = -1;
874 u32 hash = req->r_direct_hash;
Jeff Laytonbc2de10d2017-02-01 13:49:09 -0500875 bool is_hash = test_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags);
Sage Weil2f2dc052009-10-06 11:31:09 -0700876
877 /*
878 * is there a specific mds we should try? ignore hint if we have
879 * no session and the mds is not up (active or recovering).
880 */
881 if (req->r_resend_mds >= 0 &&
882 (__have_session(mdsc, req->r_resend_mds) ||
883 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
884 dout("choose_mds using resend_mds mds%d\n",
885 req->r_resend_mds);
886 return req->r_resend_mds;
887 }
888
889 if (mode == USE_RANDOM_MDS)
890 goto random;
891
892 inode = NULL;
893 if (req->r_inode) {
Yan, Zheng5d37ca12017-07-26 12:48:08 +0800894 if (ceph_snap(req->r_inode) != CEPH_SNAPDIR) {
895 inode = req->r_inode;
896 ihold(inode);
897 } else {
Yan, Zheng38f340c2017-09-22 11:41:06 +0800898 /* req->r_dentry is non-null for LSSNAP request */
899 rcu_read_lock();
900 inode = get_nonsnap_parent(req->r_dentry);
901 rcu_read_unlock();
902 dout("__choose_mds using snapdir's parent %p\n", inode);
Yan, Zheng5d37ca12017-07-26 12:48:08 +0800903 }
Yan, Zheng38f340c2017-09-22 11:41:06 +0800904 } else if (req->r_dentry) {
Sage Weild79698d2011-07-26 11:31:26 -0700905 /* ignore race with rename; old or new d_parent is okay */
Jeff Layton30c71232016-12-15 08:37:56 -0500906 struct dentry *parent;
907 struct inode *dir;
Sage Weileb6bb1c2010-08-16 09:21:27 -0700908
Jeff Layton30c71232016-12-15 08:37:56 -0500909 rcu_read_lock();
910 parent = req->r_dentry->d_parent;
Jeff Layton3dd69aa2017-01-31 10:28:26 -0500911 dir = req->r_parent ? : d_inode_rcu(parent);
Jeff Layton30c71232016-12-15 08:37:56 -0500912
913 if (!dir || dir->i_sb != mdsc->fsc->sb) {
914 /* not this fs or parent went negative */
David Howells2b0143b2015-03-17 22:25:59 +0000915 inode = d_inode(req->r_dentry);
Jeff Layton30c71232016-12-15 08:37:56 -0500916 if (inode)
917 ihold(inode);
Sage Weileb6bb1c2010-08-16 09:21:27 -0700918 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
919 /* direct snapped/virtual snapdir requests
920 * based on parent dir inode */
Jeff Layton30c71232016-12-15 08:37:56 -0500921 inode = get_nonsnap_parent(parent);
Sage Weileb6bb1c2010-08-16 09:21:27 -0700922 dout("__choose_mds using nonsnap parent %p\n", inode);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800923 } else {
Sage Weileb6bb1c2010-08-16 09:21:27 -0700924 /* dentry target */
David Howells2b0143b2015-03-17 22:25:59 +0000925 inode = d_inode(req->r_dentry);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800926 if (!inode || mode == USE_AUTH_MDS) {
927 /* dir + name */
Jeff Layton30c71232016-12-15 08:37:56 -0500928 inode = igrab(dir);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800929 hash = ceph_dentry_hash(dir, req->r_dentry);
930 is_hash = true;
Jeff Layton30c71232016-12-15 08:37:56 -0500931 } else {
932 ihold(inode);
Yan, Zhengca18bed2013-11-22 14:21:44 +0800933 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700934 }
Jeff Layton30c71232016-12-15 08:37:56 -0500935 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -0700936 }
Sage Weileb6bb1c2010-08-16 09:21:27 -0700937
Sage Weil2f2dc052009-10-06 11:31:09 -0700938 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
939 (int)hash, mode);
940 if (!inode)
941 goto random;
942 ci = ceph_inode(inode);
943
944 if (is_hash && S_ISDIR(inode->i_mode)) {
945 struct ceph_inode_frag frag;
946 int found;
947
948 ceph_choose_frag(ci, hash, &frag, &found);
949 if (found) {
950 if (mode == USE_ANY_MDS && frag.ndist > 0) {
951 u8 r;
952
953 /* choose a random replica */
954 get_random_bytes(&r, 1);
955 r %= frag.ndist;
956 mds = frag.dist[r];
957 dout("choose_mds %p %llx.%llx "
958 "frag %u mds%d (%d/%d)\n",
959 inode, ceph_vinop(inode),
Sage Weild66bbd42011-01-21 21:16:46 -0800960 frag.frag, mds,
Sage Weil2f2dc052009-10-06 11:31:09 -0700961 (int)r, frag.ndist);
Sage Weild66bbd42011-01-21 21:16:46 -0800962 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
963 CEPH_MDS_STATE_ACTIVE)
Jeff Layton30c71232016-12-15 08:37:56 -0500964 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -0700965 }
966
967 /* since this file/dir wasn't known to be
968 * replicated, then we want to look for the
969 * authoritative mds. */
970 mode = USE_AUTH_MDS;
971 if (frag.mds >= 0) {
972 /* choose auth mds */
973 mds = frag.mds;
974 dout("choose_mds %p %llx.%llx "
975 "frag %u mds%d (auth)\n",
976 inode, ceph_vinop(inode), frag.frag, mds);
Sage Weild66bbd42011-01-21 21:16:46 -0800977 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
978 CEPH_MDS_STATE_ACTIVE)
Jeff Layton30c71232016-12-15 08:37:56 -0500979 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -0700980 }
981 }
982 }
983
Sage Weilbe655592011-11-30 09:47:09 -0800984 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -0700985 cap = NULL;
986 if (mode == USE_AUTH_MDS)
987 cap = ci->i_auth_cap;
988 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
989 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
990 if (!cap) {
Sage Weilbe655592011-11-30 09:47:09 -0800991 spin_unlock(&ci->i_ceph_lock);
Jeff Layton30c71232016-12-15 08:37:56 -0500992 iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -0700993 goto random;
994 }
995 mds = cap->session->s_mds;
996 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
997 inode, ceph_vinop(inode), mds,
998 cap == ci->i_auth_cap ? "auth " : "", cap);
Sage Weilbe655592011-11-30 09:47:09 -0800999 spin_unlock(&ci->i_ceph_lock);
Jeff Layton30c71232016-12-15 08:37:56 -05001000out:
1001 iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07001002 return mds;
1003
1004random:
1005 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
1006 dout("choose_mds chose random mds%d\n", mds);
1007 return mds;
1008}
1009
1010
1011/*
1012 * session messages
1013 */
1014static struct ceph_msg *create_session_msg(u32 op, u64 seq)
1015{
1016 struct ceph_msg *msg;
1017 struct ceph_mds_session_head *h;
1018
Sage Weilb61c2762011-08-09 15:03:46 -07001019 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
1020 false);
Sage Weila79832f2010-04-01 16:06:19 -07001021 if (!msg) {
Sage Weil2f2dc052009-10-06 11:31:09 -07001022 pr_err("create_session_msg ENOMEM creating msg\n");
Sage Weila79832f2010-04-01 16:06:19 -07001023 return NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001024 }
1025 h = msg->front.iov_base;
1026 h->op = cpu_to_le32(op);
1027 h->seq = cpu_to_le64(seq);
John Spraydbd0c8b2014-09-09 19:26:01 +01001028
1029 return msg;
1030}
1031
Yan, Zheng342ce182018-05-11 18:47:29 +08001032static void encode_supported_features(void **p, void *end)
1033{
1034 static const unsigned char bits[] = CEPHFS_FEATURES_CLIENT_SUPPORTED;
1035 static const size_t count = ARRAY_SIZE(bits);
1036
1037 if (count > 0) {
1038 size_t i;
1039 size_t size = ((size_t)bits[count - 1] + 64) / 64 * 8;
1040
1041 BUG_ON(*p + 4 + size > end);
1042 ceph_encode_32(p, size);
1043 memset(*p, 0, size);
1044 for (i = 0; i < count; i++)
1045 ((unsigned char*)(*p))[i / 8] |= 1 << (bits[i] % 8);
1046 *p += size;
1047 } else {
1048 BUG_ON(*p + 4 > end);
1049 ceph_encode_32(p, 0);
1050 }
1051}
1052
John Spraydbd0c8b2014-09-09 19:26:01 +01001053/*
1054 * session message, specialization for CEPH_SESSION_REQUEST_OPEN
1055 * to include additional client metadata fields.
1056 */
1057static struct ceph_msg *create_session_open_msg(struct ceph_mds_client *mdsc, u64 seq)
1058{
1059 struct ceph_msg *msg;
1060 struct ceph_mds_session_head *h;
1061 int i = -1;
Yan, Zheng342ce182018-05-11 18:47:29 +08001062 int extra_bytes = 0;
John Spraydbd0c8b2014-09-09 19:26:01 +01001063 int metadata_key_count = 0;
1064 struct ceph_options *opt = mdsc->fsc->client->options;
Yan, Zheng3f384952016-04-21 11:09:55 +08001065 struct ceph_mount_options *fsopt = mdsc->fsc->mount_options;
Yan, Zheng342ce182018-05-11 18:47:29 +08001066 void *p, *end;
John Spraydbd0c8b2014-09-09 19:26:01 +01001067
Yan, Zhenga6a5ce42015-01-16 10:54:43 +08001068 const char* metadata[][2] = {
Yan, Zheng717e6f22017-09-11 12:10:08 +08001069 {"hostname", mdsc->nodename},
1070 {"kernel_version", init_utsname()->release},
Yan, Zheng3f384952016-04-21 11:09:55 +08001071 {"entity_id", opt->name ? : ""},
1072 {"root", fsopt->server_path ? : "/"},
John Spraydbd0c8b2014-09-09 19:26:01 +01001073 {NULL, NULL}
1074 };
1075
1076 /* Calculate serialized length of metadata */
Yan, Zheng342ce182018-05-11 18:47:29 +08001077 extra_bytes = 4; /* map length */
Markus Elfringd37b1d92017-08-20 20:22:02 +02001078 for (i = 0; metadata[i][0]; ++i) {
Yan, Zheng342ce182018-05-11 18:47:29 +08001079 extra_bytes += 8 + strlen(metadata[i][0]) +
John Spraydbd0c8b2014-09-09 19:26:01 +01001080 strlen(metadata[i][1]);
1081 metadata_key_count++;
1082 }
Yan, Zheng342ce182018-05-11 18:47:29 +08001083 /* supported feature */
1084 extra_bytes += 4 + 8;
John Spraydbd0c8b2014-09-09 19:26:01 +01001085
1086 /* Allocate the message */
Yan, Zheng342ce182018-05-11 18:47:29 +08001087 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + extra_bytes,
John Spraydbd0c8b2014-09-09 19:26:01 +01001088 GFP_NOFS, false);
1089 if (!msg) {
1090 pr_err("create_session_msg ENOMEM creating msg\n");
1091 return NULL;
1092 }
Yan, Zheng342ce182018-05-11 18:47:29 +08001093 p = msg->front.iov_base;
1094 end = p + msg->front.iov_len;
1095
1096 h = p;
John Spraydbd0c8b2014-09-09 19:26:01 +01001097 h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN);
1098 h->seq = cpu_to_le64(seq);
1099
1100 /*
1101 * Serialize client metadata into waiting buffer space, using
1102 * the format that userspace expects for map<string, string>
John Spray7cfa0312014-10-30 17:15:26 +00001103 *
1104 * ClientSession messages with metadata are v2
John Spraydbd0c8b2014-09-09 19:26:01 +01001105 */
Yan, Zheng342ce182018-05-11 18:47:29 +08001106 msg->hdr.version = cpu_to_le16(3);
John Spray7cfa0312014-10-30 17:15:26 +00001107 msg->hdr.compat_version = cpu_to_le16(1);
John Spraydbd0c8b2014-09-09 19:26:01 +01001108
1109 /* The write pointer, following the session_head structure */
Yan, Zheng342ce182018-05-11 18:47:29 +08001110 p += sizeof(*h);
John Spraydbd0c8b2014-09-09 19:26:01 +01001111
1112 /* Number of entries in the map */
1113 ceph_encode_32(&p, metadata_key_count);
1114
1115 /* Two length-prefixed strings for each entry in the map */
Markus Elfringd37b1d92017-08-20 20:22:02 +02001116 for (i = 0; metadata[i][0]; ++i) {
John Spraydbd0c8b2014-09-09 19:26:01 +01001117 size_t const key_len = strlen(metadata[i][0]);
1118 size_t const val_len = strlen(metadata[i][1]);
1119
1120 ceph_encode_32(&p, key_len);
1121 memcpy(p, metadata[i][0], key_len);
1122 p += key_len;
1123 ceph_encode_32(&p, val_len);
1124 memcpy(p, metadata[i][1], val_len);
1125 p += val_len;
1126 }
1127
Yan, Zheng342ce182018-05-11 18:47:29 +08001128 encode_supported_features(&p, end);
1129 msg->front.iov_len = p - msg->front.iov_base;
1130 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1131
Sage Weil2f2dc052009-10-06 11:31:09 -07001132 return msg;
1133}
1134
1135/*
1136 * send session open request.
1137 *
1138 * called under mdsc->mutex
1139 */
1140static int __open_session(struct ceph_mds_client *mdsc,
1141 struct ceph_mds_session *session)
1142{
1143 struct ceph_msg *msg;
1144 int mstate;
1145 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07001146
1147 /* wait for mds to go active? */
1148 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
1149 dout("open_session to mds%d (%s)\n", mds,
1150 ceph_mds_state_name(mstate));
1151 session->s_state = CEPH_MDS_SESSION_OPENING;
1152 session->s_renew_requested = jiffies;
1153
1154 /* send connect message */
John Spraydbd0c8b2014-09-09 19:26:01 +01001155 msg = create_session_open_msg(mdsc, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001156 if (!msg)
1157 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -07001158 ceph_con_send(&session->s_con, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07001159 return 0;
1160}
1161
1162/*
Sage Weiled0552a2010-06-21 13:38:25 -07001163 * open sessions for any export targets for the given mds
1164 *
1165 * called under mdsc->mutex
1166 */
Yan, Zheng5d72d132013-11-24 14:33:01 +08001167static struct ceph_mds_session *
1168__open_export_target_session(struct ceph_mds_client *mdsc, int target)
1169{
1170 struct ceph_mds_session *session;
1171
1172 session = __ceph_lookup_mds_session(mdsc, target);
1173 if (!session) {
1174 session = register_session(mdsc, target);
1175 if (IS_ERR(session))
1176 return session;
1177 }
1178 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1179 session->s_state == CEPH_MDS_SESSION_CLOSING)
1180 __open_session(mdsc, session);
1181
1182 return session;
1183}
1184
1185struct ceph_mds_session *
1186ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target)
1187{
1188 struct ceph_mds_session *session;
1189
1190 dout("open_export_target_session to mds%d\n", target);
1191
1192 mutex_lock(&mdsc->mutex);
1193 session = __open_export_target_session(mdsc, target);
1194 mutex_unlock(&mdsc->mutex);
1195
1196 return session;
1197}
1198
Sage Weiled0552a2010-06-21 13:38:25 -07001199static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
1200 struct ceph_mds_session *session)
1201{
1202 struct ceph_mds_info *mi;
1203 struct ceph_mds_session *ts;
1204 int i, mds = session->s_mds;
Sage Weiled0552a2010-06-21 13:38:25 -07001205
Yan, Zheng76201b62017-03-28 17:04:13 +08001206 if (mds >= mdsc->mdsmap->m_num_mds)
Sage Weiled0552a2010-06-21 13:38:25 -07001207 return;
Yan, Zheng5d72d132013-11-24 14:33:01 +08001208
Sage Weiled0552a2010-06-21 13:38:25 -07001209 mi = &mdsc->mdsmap->m_info[mds];
1210 dout("open_export_target_sessions for mds%d (%d targets)\n",
1211 session->s_mds, mi->num_export_targets);
1212
1213 for (i = 0; i < mi->num_export_targets; i++) {
Yan, Zheng5d72d132013-11-24 14:33:01 +08001214 ts = __open_export_target_session(mdsc, mi->export_targets[i]);
1215 if (!IS_ERR(ts))
1216 ceph_put_mds_session(ts);
Sage Weiled0552a2010-06-21 13:38:25 -07001217 }
1218}
1219
Sage Weil154f42c2010-06-21 13:45:04 -07001220void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
1221 struct ceph_mds_session *session)
1222{
1223 mutex_lock(&mdsc->mutex);
1224 __open_export_target_sessions(mdsc, session);
1225 mutex_unlock(&mdsc->mutex);
1226}
1227
Sage Weiled0552a2010-06-21 13:38:25 -07001228/*
Sage Weil2f2dc052009-10-06 11:31:09 -07001229 * session caps
1230 */
1231
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001232static void detach_cap_releases(struct ceph_mds_session *session,
1233 struct list_head *target)
Sage Weil2f2dc052009-10-06 11:31:09 -07001234{
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001235 lockdep_assert_held(&session->s_cap_lock);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001236
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001237 list_splice_init(&session->s_cap_releases, target);
1238 session->s_num_cap_releases = 0;
1239 dout("dispose_cap_releases mds%d\n", session->s_mds);
1240}
1241
1242static void dispose_cap_releases(struct ceph_mds_client *mdsc,
1243 struct list_head *dispose)
1244{
1245 while (!list_empty(dispose)) {
Yan, Zheng745a8e32015-05-14 17:22:42 +08001246 struct ceph_cap *cap;
1247 /* zero out the in-progress message */
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001248 cap = list_first_entry(dispose, struct ceph_cap, session_caps);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001249 list_del(&cap->session_caps);
1250 ceph_put_cap(mdsc, cap);
1251 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001252}
1253
Yan, Zheng1c841a92015-03-24 20:15:36 +08001254static void cleanup_session_requests(struct ceph_mds_client *mdsc,
1255 struct ceph_mds_session *session)
1256{
1257 struct ceph_mds_request *req;
1258 struct rb_node *p;
1259
1260 dout("cleanup_session_requests mds%d\n", session->s_mds);
1261 mutex_lock(&mdsc->mutex);
1262 while (!list_empty(&session->s_unsafe)) {
1263 req = list_first_entry(&session->s_unsafe,
1264 struct ceph_mds_request, r_unsafe_item);
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001265 pr_warn_ratelimited(" dropping unsafe request %llu\n",
1266 req->r_tid);
Yan, Zheng1c841a92015-03-24 20:15:36 +08001267 __unregister_request(mdsc, req);
1268 }
1269 /* zero r_attempts, so kick_requests() will re-send requests */
1270 p = rb_first(&mdsc->request_tree);
1271 while (p) {
1272 req = rb_entry(p, struct ceph_mds_request, r_node);
1273 p = rb_next(p);
1274 if (req->r_session &&
1275 req->r_session->s_mds == session->s_mds)
1276 req->r_attempts = 0;
1277 }
1278 mutex_unlock(&mdsc->mutex);
1279}
1280
Sage Weil2f2dc052009-10-06 11:31:09 -07001281/*
Sage Weilf818a732010-05-11 20:56:31 -07001282 * Helper to safely iterate over all caps associated with a session, with
1283 * special care taken to handle a racing __ceph_remove_cap().
Sage Weil2f2dc052009-10-06 11:31:09 -07001284 *
Sage Weilf818a732010-05-11 20:56:31 -07001285 * Caller must hold session s_mutex.
Sage Weil2f2dc052009-10-06 11:31:09 -07001286 */
1287static int iterate_session_caps(struct ceph_mds_session *session,
1288 int (*cb)(struct inode *, struct ceph_cap *,
1289 void *), void *arg)
1290{
Sage Weil7c1332b2010-02-16 11:39:45 -08001291 struct list_head *p;
1292 struct ceph_cap *cap;
1293 struct inode *inode, *last_inode = NULL;
1294 struct ceph_cap *old_cap = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001295 int ret;
1296
1297 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
1298 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001299 p = session->s_caps.next;
1300 while (p != &session->s_caps) {
1301 cap = list_entry(p, struct ceph_cap, session_caps);
Sage Weil2f2dc052009-10-06 11:31:09 -07001302 inode = igrab(&cap->ci->vfs_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001303 if (!inode) {
1304 p = p->next;
Sage Weil2f2dc052009-10-06 11:31:09 -07001305 continue;
Sage Weil7c1332b2010-02-16 11:39:45 -08001306 }
1307 session->s_cap_iterator = cap;
Sage Weil2f2dc052009-10-06 11:31:09 -07001308 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001309
1310 if (last_inode) {
1311 iput(last_inode);
1312 last_inode = NULL;
1313 }
1314 if (old_cap) {
Yehuda Sadeh37151662010-06-17 16:16:12 -07001315 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -08001316 old_cap = NULL;
1317 }
1318
Sage Weil2f2dc052009-10-06 11:31:09 -07001319 ret = cb(inode, cap, arg);
Sage Weil7c1332b2010-02-16 11:39:45 -08001320 last_inode = inode;
1321
Sage Weil2f2dc052009-10-06 11:31:09 -07001322 spin_lock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001323 p = p->next;
Markus Elfringd37b1d92017-08-20 20:22:02 +02001324 if (!cap->ci) {
Sage Weil7c1332b2010-02-16 11:39:45 -08001325 dout("iterate_session_caps finishing cap %p removal\n",
1326 cap);
1327 BUG_ON(cap->session != session);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001328 cap->session = NULL;
Sage Weil7c1332b2010-02-16 11:39:45 -08001329 list_del_init(&cap->session_caps);
1330 session->s_nr_caps--;
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08001331 if (cap->queue_release)
1332 __ceph_queue_cap_release(session, cap);
1333 else
Yan, Zheng745a8e32015-05-14 17:22:42 +08001334 old_cap = cap; /* put_cap it w/o locks held */
Sage Weil7c1332b2010-02-16 11:39:45 -08001335 }
Sage Weil5dacf092009-12-21 20:40:34 -08001336 if (ret < 0)
1337 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07001338 }
Sage Weil5dacf092009-12-21 20:40:34 -08001339 ret = 0;
1340out:
Sage Weil7c1332b2010-02-16 11:39:45 -08001341 session->s_cap_iterator = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07001342 spin_unlock(&session->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -08001343
SF Markus Elfringe96a6502014-11-02 15:20:59 +01001344 iput(last_inode);
Sage Weil7c1332b2010-02-16 11:39:45 -08001345 if (old_cap)
Yehuda Sadeh37151662010-06-17 16:16:12 -07001346 ceph_put_cap(session->s_mdsc, old_cap);
Sage Weil7c1332b2010-02-16 11:39:45 -08001347
Sage Weil5dacf092009-12-21 20:40:34 -08001348 return ret;
Sage Weil2f2dc052009-10-06 11:31:09 -07001349}
1350
1351static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
Sage Weil6c99f252010-05-10 16:12:25 -07001352 void *arg)
Sage Weil2f2dc052009-10-06 11:31:09 -07001353{
Yan, Zheng6c93df52016-04-15 13:56:12 +08001354 struct ceph_fs_client *fsc = (struct ceph_fs_client *)arg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001355 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001356 LIST_HEAD(to_remove);
Yan, Zheng6c93df52016-04-15 13:56:12 +08001357 bool drop = false;
1358 bool invalidate = false;
Sage Weil6c99f252010-05-10 16:12:25 -07001359
Sage Weil2f2dc052009-10-06 11:31:09 -07001360 dout("removing cap %p, ci is %p, inode is %p\n",
1361 cap, ci, &ci->vfs_inode);
Sage Weilbe655592011-11-30 09:47:09 -08001362 spin_lock(&ci->i_ceph_lock);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001363 if (cap->mds_wanted | cap->issued)
1364 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
Yan, Zhenga096b092013-09-22 10:15:58 +08001365 __ceph_remove_cap(cap, false);
Yan, Zheng571ade32015-03-24 11:36:08 +08001366 if (!ci->i_auth_cap) {
Yan, Zheng553adfd2015-06-09 15:48:57 +08001367 struct ceph_cap_flush *cf;
Yan, Zheng6c93df52016-04-15 13:56:12 +08001368 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil6c99f252010-05-10 16:12:25 -07001369
Yan, Zheng6c93df52016-04-15 13:56:12 +08001370 if (ci->i_wrbuffer_ref > 0 &&
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01001371 READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
Yan, Zheng6c93df52016-04-15 13:56:12 +08001372 invalidate = true;
1373
Yan, Zhenge4500b52016-07-06 11:12:56 +08001374 while (!list_empty(&ci->i_cap_flush_list)) {
1375 cf = list_first_entry(&ci->i_cap_flush_list,
1376 struct ceph_cap_flush, i_list);
Wei Yongjun8cdcc072016-08-13 09:10:28 +00001377 list_move(&cf->i_list, &to_remove);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001378 }
1379
Sage Weil6c99f252010-05-10 16:12:25 -07001380 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zheng8310b082015-06-09 17:20:12 +08001381
Yan, Zhenge4500b52016-07-06 11:12:56 +08001382 list_for_each_entry(cf, &to_remove, i_list)
1383 list_del(&cf->g_list);
Yan, Zheng8310b082015-06-09 17:20:12 +08001384
Sage Weil6c99f252010-05-10 16:12:25 -07001385 if (!list_empty(&ci->i_dirty_item)) {
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001386 pr_warn_ratelimited(
1387 " dropping dirty %s state for %p %lld\n",
Sage Weil6c99f252010-05-10 16:12:25 -07001388 ceph_cap_string(ci->i_dirty_caps),
1389 inode, ceph_ino(inode));
1390 ci->i_dirty_caps = 0;
1391 list_del_init(&ci->i_dirty_item);
Yan, Zheng6c93df52016-04-15 13:56:12 +08001392 drop = true;
Sage Weil6c99f252010-05-10 16:12:25 -07001393 }
1394 if (!list_empty(&ci->i_flushing_item)) {
Yan, Zheng3e0708b2015-05-22 16:38:02 +08001395 pr_warn_ratelimited(
1396 " dropping dirty+flushing %s state for %p %lld\n",
Sage Weil6c99f252010-05-10 16:12:25 -07001397 ceph_cap_string(ci->i_flushing_caps),
1398 inode, ceph_ino(inode));
1399 ci->i_flushing_caps = 0;
1400 list_del_init(&ci->i_flushing_item);
1401 mdsc->num_cap_flushing--;
Yan, Zheng6c93df52016-04-15 13:56:12 +08001402 drop = true;
Sage Weil6c99f252010-05-10 16:12:25 -07001403 }
Sage Weil6c99f252010-05-10 16:12:25 -07001404 spin_unlock(&mdsc->cap_dirty_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001405
Yan, Zhengb3f8d682017-09-11 10:58:55 +08001406 if (atomic_read(&ci->i_filelock_ref) > 0) {
1407 /* make further file lock syscall return -EIO */
1408 ci->i_ceph_flags |= CEPH_I_ERROR_FILELOCK;
1409 pr_warn_ratelimited(" dropping file locks for %p %lld\n",
1410 inode, ceph_ino(inode));
1411 }
1412
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001413 if (!ci->i_dirty_caps && ci->i_prealloc_cap_flush) {
Yan, Zhenge4500b52016-07-06 11:12:56 +08001414 list_add(&ci->i_prealloc_cap_flush->i_list, &to_remove);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001415 ci->i_prealloc_cap_flush = NULL;
1416 }
Sage Weil6c99f252010-05-10 16:12:25 -07001417 }
Sage Weilbe655592011-11-30 09:47:09 -08001418 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001419 while (!list_empty(&to_remove)) {
1420 struct ceph_cap_flush *cf;
1421 cf = list_first_entry(&to_remove,
Yan, Zhenge4500b52016-07-06 11:12:56 +08001422 struct ceph_cap_flush, i_list);
1423 list_del(&cf->i_list);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001424 ceph_free_cap_flush(cf);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001425 }
Yan, Zheng77310322016-04-08 15:27:16 +08001426
1427 wake_up_all(&ci->i_cap_wq);
Yan, Zheng6c93df52016-04-15 13:56:12 +08001428 if (invalidate)
1429 ceph_queue_invalidate(inode);
Yan, Zheng77310322016-04-08 15:27:16 +08001430 if (drop)
Sage Weil6c99f252010-05-10 16:12:25 -07001431 iput(inode);
Sage Weil2f2dc052009-10-06 11:31:09 -07001432 return 0;
1433}
1434
1435/*
1436 * caller must hold session s_mutex
1437 */
1438static void remove_session_caps(struct ceph_mds_session *session)
1439{
Yan, Zheng6c93df52016-04-15 13:56:12 +08001440 struct ceph_fs_client *fsc = session->s_mdsc->fsc;
1441 struct super_block *sb = fsc->sb;
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001442 LIST_HEAD(dispose);
1443
Sage Weil2f2dc052009-10-06 11:31:09 -07001444 dout("remove_session_caps on %p\n", session);
Yan, Zheng6c93df52016-04-15 13:56:12 +08001445 iterate_session_caps(session, remove_session_caps_cb, fsc);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001446
Yan, Zhengc8799fc2016-07-07 15:22:38 +08001447 wake_up_all(&fsc->mdsc->cap_flushing_wq);
1448
Yan, Zheng6f60f882013-07-24 12:22:11 +08001449 spin_lock(&session->s_cap_lock);
1450 if (session->s_nr_caps > 0) {
Yan, Zheng6f60f882013-07-24 12:22:11 +08001451 struct inode *inode;
1452 struct ceph_cap *cap, *prev = NULL;
1453 struct ceph_vino vino;
1454 /*
1455 * iterate_session_caps() skips inodes that are being
1456 * deleted, we need to wait until deletions are complete.
1457 * __wait_on_freeing_inode() is designed for the job,
1458 * but it is not exported, so use lookup inode function
1459 * to access it.
1460 */
1461 while (!list_empty(&session->s_caps)) {
1462 cap = list_entry(session->s_caps.next,
1463 struct ceph_cap, session_caps);
1464 if (cap == prev)
1465 break;
1466 prev = cap;
1467 vino = cap->ci->i_vino;
1468 spin_unlock(&session->s_cap_lock);
1469
Yan, Zhenged284c42013-09-02 15:19:53 +08001470 inode = ceph_find_inode(sb, vino);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001471 iput(inode);
1472
1473 spin_lock(&session->s_cap_lock);
1474 }
1475 }
Yan, Zheng745a8e32015-05-14 17:22:42 +08001476
1477 // drop cap expires and unlock s_cap_lock
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001478 detach_cap_releases(session, &dispose);
Yan, Zheng6f60f882013-07-24 12:22:11 +08001479
Sage Weil2f2dc052009-10-06 11:31:09 -07001480 BUG_ON(session->s_nr_caps > 0);
Sage Weil6c99f252010-05-10 16:12:25 -07001481 BUG_ON(!list_empty(&session->s_cap_flushing));
Jeff Laytonc8a96a32017-10-19 08:53:58 -04001482 spin_unlock(&session->s_cap_lock);
1483 dispose_cap_releases(session->s_mdsc, &dispose);
Sage Weil2f2dc052009-10-06 11:31:09 -07001484}
1485
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001486enum {
1487 RECONNECT,
1488 RENEWCAPS,
1489 FORCE_RO,
1490};
1491
Sage Weil2f2dc052009-10-06 11:31:09 -07001492/*
1493 * wake up any threads waiting on this session's caps. if the cap is
1494 * old (didn't get renewed on the client reconnect), remove it now.
1495 *
1496 * caller must hold s_mutex.
1497 */
1498static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1499 void *arg)
1500{
Sage Weil0dc25702009-11-20 13:43:45 -08001501 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001502 unsigned long ev = (unsigned long)arg;
Sage Weil0dc25702009-11-20 13:43:45 -08001503
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001504 if (ev == RECONNECT) {
Sage Weilbe655592011-11-30 09:47:09 -08001505 spin_lock(&ci->i_ceph_lock);
Sage Weil0dc25702009-11-20 13:43:45 -08001506 ci->i_wanted_max_size = 0;
1507 ci->i_requested_max_size = 0;
Sage Weilbe655592011-11-30 09:47:09 -08001508 spin_unlock(&ci->i_ceph_lock);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001509 } else if (ev == RENEWCAPS) {
1510 if (cap->cap_gen < cap->session->s_cap_gen) {
1511 /* mds did not re-issue stale cap */
1512 spin_lock(&ci->i_ceph_lock);
1513 cap->issued = cap->implemented = CEPH_CAP_PIN;
1514 /* make sure mds knows what we want */
1515 if (__ceph_caps_file_wanted(ci) & ~cap->mds_wanted)
1516 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
1517 spin_unlock(&ci->i_ceph_lock);
1518 }
1519 } else if (ev == FORCE_RO) {
Sage Weil0dc25702009-11-20 13:43:45 -08001520 }
Yan, Zhenge5360302016-05-19 19:15:19 +08001521 wake_up_all(&ci->i_cap_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07001522 return 0;
1523}
1524
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001525static void wake_up_session_caps(struct ceph_mds_session *session, int ev)
Sage Weil2f2dc052009-10-06 11:31:09 -07001526{
1527 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
Sage Weil0dc25702009-11-20 13:43:45 -08001528 iterate_session_caps(session, wake_up_session_cb,
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001529 (void *)(unsigned long)ev);
Sage Weil2f2dc052009-10-06 11:31:09 -07001530}
1531
1532/*
1533 * Send periodic message to MDS renewing all currently held caps. The
1534 * ack will reset the expiration for all caps from this session.
1535 *
1536 * caller holds s_mutex
1537 */
1538static int send_renew_caps(struct ceph_mds_client *mdsc,
1539 struct ceph_mds_session *session)
1540{
1541 struct ceph_msg *msg;
1542 int state;
1543
1544 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1545 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1546 pr_info("mds%d caps stale\n", session->s_mds);
Sage Weile4cb4cb2010-03-18 13:43:09 -07001547 session->s_renew_requested = jiffies;
Sage Weil2f2dc052009-10-06 11:31:09 -07001548
1549 /* do not try to renew caps until a recovering mds has reconnected
1550 * with its clients. */
1551 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1552 if (state < CEPH_MDS_STATE_RECONNECT) {
1553 dout("send_renew_caps ignoring mds%d (%s)\n",
1554 session->s_mds, ceph_mds_state_name(state));
1555 return 0;
1556 }
1557
1558 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1559 ceph_mds_state_name(state));
Sage Weil2f2dc052009-10-06 11:31:09 -07001560 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1561 ++session->s_renew_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001562 if (!msg)
1563 return -ENOMEM;
Sage Weil2f2dc052009-10-06 11:31:09 -07001564 ceph_con_send(&session->s_con, msg);
1565 return 0;
1566}
1567
Yan, Zheng186e4f72013-11-22 14:48:37 +08001568static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
1569 struct ceph_mds_session *session, u64 seq)
1570{
1571 struct ceph_msg *msg;
1572
1573 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
John Spraya687eca2014-09-19 13:51:08 +01001574 session->s_mds, ceph_session_state_name(session->s_state), seq);
Yan, Zheng186e4f72013-11-22 14:48:37 +08001575 msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
1576 if (!msg)
1577 return -ENOMEM;
1578 ceph_con_send(&session->s_con, msg);
1579 return 0;
1580}
1581
1582
Sage Weil2f2dc052009-10-06 11:31:09 -07001583/*
1584 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
Sage Weil0dc25702009-11-20 13:43:45 -08001585 *
1586 * Called under session->s_mutex
Sage Weil2f2dc052009-10-06 11:31:09 -07001587 */
1588static void renewed_caps(struct ceph_mds_client *mdsc,
1589 struct ceph_mds_session *session, int is_renew)
1590{
1591 int was_stale;
1592 int wake = 0;
1593
1594 spin_lock(&session->s_cap_lock);
Alex Elder1ce208a2012-01-12 17:48:11 -08001595 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
Sage Weil2f2dc052009-10-06 11:31:09 -07001596
1597 session->s_cap_ttl = session->s_renew_requested +
1598 mdsc->mdsmap->m_session_timeout*HZ;
1599
1600 if (was_stale) {
1601 if (time_before(jiffies, session->s_cap_ttl)) {
1602 pr_info("mds%d caps renewed\n", session->s_mds);
1603 wake = 1;
1604 } else {
1605 pr_info("mds%d caps still stale\n", session->s_mds);
1606 }
1607 }
1608 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1609 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1610 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1611 spin_unlock(&session->s_cap_lock);
1612
1613 if (wake)
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08001614 wake_up_session_caps(session, RENEWCAPS);
Sage Weil2f2dc052009-10-06 11:31:09 -07001615}
1616
1617/*
1618 * send a session close request
1619 */
1620static int request_close_session(struct ceph_mds_client *mdsc,
1621 struct ceph_mds_session *session)
1622{
1623 struct ceph_msg *msg;
Sage Weil2f2dc052009-10-06 11:31:09 -07001624
1625 dout("request_close_session mds%d state %s seq %lld\n",
John Spraya687eca2014-09-19 13:51:08 +01001626 session->s_mds, ceph_session_state_name(session->s_state),
Sage Weil2f2dc052009-10-06 11:31:09 -07001627 session->s_seq);
1628 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
Sage Weila79832f2010-04-01 16:06:19 -07001629 if (!msg)
1630 return -ENOMEM;
1631 ceph_con_send(&session->s_con, msg);
Yan, Zhengfcff4152016-09-14 16:39:51 +08001632 return 1;
Sage Weil2f2dc052009-10-06 11:31:09 -07001633}
1634
1635/*
1636 * Called with s_mutex held.
1637 */
1638static int __close_session(struct ceph_mds_client *mdsc,
1639 struct ceph_mds_session *session)
1640{
1641 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1642 return 0;
1643 session->s_state = CEPH_MDS_SESSION_CLOSING;
1644 return request_close_session(mdsc, session);
1645}
1646
Yan, Zheng040d7862017-11-30 11:59:22 +08001647static bool drop_negative_children(struct dentry *dentry)
1648{
1649 struct dentry *child;
1650 bool all_negative = true;
1651
1652 if (!d_is_dir(dentry))
1653 goto out;
1654
1655 spin_lock(&dentry->d_lock);
1656 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
1657 if (d_really_is_positive(child)) {
1658 all_negative = false;
1659 break;
1660 }
1661 }
1662 spin_unlock(&dentry->d_lock);
1663
1664 if (all_negative)
1665 shrink_dcache_parent(dentry);
1666out:
1667 return all_negative;
1668}
1669
Sage Weil2f2dc052009-10-06 11:31:09 -07001670/*
1671 * Trim old(er) caps.
1672 *
1673 * Because we can't cache an inode without one or more caps, we do
1674 * this indirectly: if a cap is unused, we prune its aliases, at which
1675 * point the inode will hopefully get dropped to.
1676 *
1677 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1678 * memory pressure from the MDS, though, so it needn't be perfect.
1679 */
1680static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1681{
1682 struct ceph_mds_session *session = arg;
1683 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng979abfd2013-11-22 13:56:24 +08001684 int used, wanted, oissued, mine;
Sage Weil2f2dc052009-10-06 11:31:09 -07001685
1686 if (session->s_trim_caps <= 0)
1687 return -1;
1688
Sage Weilbe655592011-11-30 09:47:09 -08001689 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001690 mine = cap->issued | cap->implemented;
1691 used = __ceph_caps_used(ci);
Yan, Zheng979abfd2013-11-22 13:56:24 +08001692 wanted = __ceph_caps_file_wanted(ci);
Sage Weil2f2dc052009-10-06 11:31:09 -07001693 oissued = __ceph_caps_issued_other(ci, cap);
1694
Yan, Zheng979abfd2013-11-22 13:56:24 +08001695 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07001696 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
Yan, Zheng979abfd2013-11-22 13:56:24 +08001697 ceph_cap_string(used), ceph_cap_string(wanted));
1698 if (cap == ci->i_auth_cap) {
Yan, Zheng622f3e22015-05-07 10:59:47 +08001699 if (ci->i_dirty_caps || ci->i_flushing_caps ||
1700 !list_empty(&ci->i_cap_snaps))
Yan, Zheng979abfd2013-11-22 13:56:24 +08001701 goto out;
1702 if ((used | wanted) & CEPH_CAP_ANY_WR)
1703 goto out;
Yan, Zheng89aa5932017-09-08 15:23:18 +08001704 /* Note: it's possible that i_filelock_ref becomes non-zero
1705 * after dropping auth caps. It doesn't hurt because reply
1706 * of lock mds request will re-add auth caps. */
1707 if (atomic_read(&ci->i_filelock_ref) > 0)
1708 goto out;
Yan, Zheng979abfd2013-11-22 13:56:24 +08001709 }
Yan, Zheng5e804ac2015-10-26 16:08:43 +08001710 /* The inode has cached pages, but it's no longer used.
1711 * we can safely drop it */
1712 if (wanted == 0 && used == CEPH_CAP_FILE_CACHE &&
1713 !(oissued & CEPH_CAP_FILE_CACHE)) {
1714 used = 0;
1715 oissued = 0;
1716 }
Yan, Zheng979abfd2013-11-22 13:56:24 +08001717 if ((used | wanted) & ~oissued & mine)
Sage Weil2f2dc052009-10-06 11:31:09 -07001718 goto out; /* we need these caps */
1719
Sage Weil2f2dc052009-10-06 11:31:09 -07001720 if (oissued) {
1721 /* we aren't the only cap.. just remove us */
Yan, Zhenga096b092013-09-22 10:15:58 +08001722 __ceph_remove_cap(cap, true);
Yan, Zheng040d7862017-11-30 11:59:22 +08001723 session->s_trim_caps--;
Sage Weil2f2dc052009-10-06 11:31:09 -07001724 } else {
Yan, Zheng040d7862017-11-30 11:59:22 +08001725 struct dentry *dentry;
Yan, Zheng5e804ac2015-10-26 16:08:43 +08001726 /* try dropping referring dentries */
Sage Weilbe655592011-11-30 09:47:09 -08001727 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng040d7862017-11-30 11:59:22 +08001728 dentry = d_find_any_alias(inode);
1729 if (dentry && drop_negative_children(dentry)) {
1730 int count;
1731 dput(dentry);
1732 d_prune_aliases(inode);
1733 count = atomic_read(&inode->i_count);
1734 if (count == 1)
1735 session->s_trim_caps--;
1736 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1737 inode, cap, count);
1738 } else {
1739 dput(dentry);
1740 }
Sage Weil2f2dc052009-10-06 11:31:09 -07001741 return 0;
1742 }
1743
1744out:
Sage Weilbe655592011-11-30 09:47:09 -08001745 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001746 return 0;
1747}
1748
1749/*
1750 * Trim session cap count down to some max number.
1751 */
Zhi Zhange30ee582018-01-24 21:24:33 +08001752int ceph_trim_caps(struct ceph_mds_client *mdsc,
1753 struct ceph_mds_session *session,
1754 int max_caps)
Sage Weil2f2dc052009-10-06 11:31:09 -07001755{
1756 int trim_caps = session->s_nr_caps - max_caps;
1757
1758 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1759 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1760 if (trim_caps > 0) {
1761 session->s_trim_caps = trim_caps;
1762 iterate_session_caps(session, trim_caps_cb, session);
1763 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1764 session->s_mds, session->s_nr_caps, max_caps,
1765 trim_caps - session->s_trim_caps);
Sage Weil5dacf092009-12-21 20:40:34 -08001766 session->s_trim_caps = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07001767 }
Yan, Zhenga56371d2014-04-01 20:34:56 +08001768
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08001769 ceph_flush_cap_releases(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07001770 return 0;
1771}
1772
Yan, Zheng8310b082015-06-09 17:20:12 +08001773static int check_caps_flush(struct ceph_mds_client *mdsc,
1774 u64 want_flush_tid)
1775{
Yan, Zheng8310b082015-06-09 17:20:12 +08001776 int ret = 1;
1777
1778 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zhenge4500b52016-07-06 11:12:56 +08001779 if (!list_empty(&mdsc->cap_flush_list)) {
1780 struct ceph_cap_flush *cf =
1781 list_first_entry(&mdsc->cap_flush_list,
1782 struct ceph_cap_flush, g_list);
1783 if (cf->tid <= want_flush_tid) {
1784 dout("check_caps_flush still flushing tid "
1785 "%llu <= %llu\n", cf->tid, want_flush_tid);
1786 ret = 0;
1787 }
Yan, Zheng8310b082015-06-09 17:20:12 +08001788 }
1789 spin_unlock(&mdsc->cap_dirty_lock);
1790 return ret;
Yan, Zhengd3383a82015-01-08 21:30:12 +08001791}
1792
Sage Weil2f2dc052009-10-06 11:31:09 -07001793/*
1794 * flush all dirty inode data to disk.
1795 *
Yan, Zheng8310b082015-06-09 17:20:12 +08001796 * returns true if we've flushed through want_flush_tid
Sage Weil2f2dc052009-10-06 11:31:09 -07001797 */
Yan, Zhengaffbc192015-05-05 21:22:13 +08001798static void wait_caps_flush(struct ceph_mds_client *mdsc,
Yan, Zheng0e294382016-07-04 18:06:41 +08001799 u64 want_flush_tid)
Sage Weil2f2dc052009-10-06 11:31:09 -07001800{
Yan, Zheng0e294382016-07-04 18:06:41 +08001801 dout("check_caps_flush want %llu\n", want_flush_tid);
Yan, Zheng8310b082015-06-09 17:20:12 +08001802
1803 wait_event(mdsc->cap_flushing_wq,
1804 check_caps_flush(mdsc, want_flush_tid));
1805
1806 dout("check_caps_flush ok, flushed thru %llu\n", want_flush_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07001807}
1808
1809/*
1810 * called under s_mutex
1811 */
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08001812static void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1813 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07001814{
Yan, Zheng745a8e32015-05-14 17:22:42 +08001815 struct ceph_msg *msg = NULL;
1816 struct ceph_mds_cap_release *head;
1817 struct ceph_mds_cap_item *item;
Jeff Layton92475f02017-04-13 11:07:04 -04001818 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
Yan, Zheng745a8e32015-05-14 17:22:42 +08001819 struct ceph_cap *cap;
1820 LIST_HEAD(tmp_list);
1821 int num_cap_releases;
Jeff Layton92475f02017-04-13 11:07:04 -04001822 __le32 barrier, *cap_barrier;
1823
1824 down_read(&osdc->lock);
1825 barrier = cpu_to_le32(osdc->epoch_barrier);
1826 up_read(&osdc->lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07001827
Sage Weil0f8605f2010-05-05 15:51:35 -07001828 spin_lock(&session->s_cap_lock);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001829again:
1830 list_splice_init(&session->s_cap_releases, &tmp_list);
1831 num_cap_releases = session->s_num_cap_releases;
1832 session->s_num_cap_releases = 0;
1833 spin_unlock(&session->s_cap_lock);
1834
1835 while (!list_empty(&tmp_list)) {
1836 if (!msg) {
1837 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001838 PAGE_SIZE, GFP_NOFS, false);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001839 if (!msg)
1840 goto out_err;
1841 head = msg->front.iov_base;
1842 head->num = cpu_to_le32(0);
1843 msg->front.iov_len = sizeof(*head);
Jeff Layton92475f02017-04-13 11:07:04 -04001844
1845 msg->hdr.version = cpu_to_le16(2);
1846 msg->hdr.compat_version = cpu_to_le16(1);
Yan, Zheng745a8e32015-05-14 17:22:42 +08001847 }
Jeff Layton92475f02017-04-13 11:07:04 -04001848
Yan, Zheng745a8e32015-05-14 17:22:42 +08001849 cap = list_first_entry(&tmp_list, struct ceph_cap,
1850 session_caps);
1851 list_del(&cap->session_caps);
1852 num_cap_releases--;
1853
1854 head = msg->front.iov_base;
1855 le32_add_cpu(&head->num, 1);
1856 item = msg->front.iov_base + msg->front.iov_len;
1857 item->ino = cpu_to_le64(cap->cap_ino);
1858 item->cap_id = cpu_to_le64(cap->cap_id);
1859 item->migrate_seq = cpu_to_le32(cap->mseq);
1860 item->seq = cpu_to_le32(cap->issue_seq);
1861 msg->front.iov_len += sizeof(*item);
1862
1863 ceph_put_cap(mdsc, cap);
1864
1865 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
Jeff Layton92475f02017-04-13 11:07:04 -04001866 // Append cap_barrier field
1867 cap_barrier = msg->front.iov_base + msg->front.iov_len;
1868 *cap_barrier = barrier;
1869 msg->front.iov_len += sizeof(*cap_barrier);
1870
Yan, Zheng745a8e32015-05-14 17:22:42 +08001871 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1872 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1873 ceph_con_send(&session->s_con, msg);
1874 msg = NULL;
1875 }
1876 }
1877
1878 BUG_ON(num_cap_releases != 0);
1879
1880 spin_lock(&session->s_cap_lock);
1881 if (!list_empty(&session->s_cap_releases))
1882 goto again;
1883 spin_unlock(&session->s_cap_lock);
1884
1885 if (msg) {
Jeff Layton92475f02017-04-13 11:07:04 -04001886 // Append cap_barrier field
1887 cap_barrier = msg->front.iov_base + msg->front.iov_len;
1888 *cap_barrier = barrier;
1889 msg->front.iov_len += sizeof(*cap_barrier);
1890
Sage Weil2f2dc052009-10-06 11:31:09 -07001891 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1892 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1893 ceph_con_send(&session->s_con, msg);
1894 }
Yan, Zheng745a8e32015-05-14 17:22:42 +08001895 return;
1896out_err:
1897 pr_err("send_cap_releases mds%d, failed to allocate message\n",
1898 session->s_mds);
1899 spin_lock(&session->s_cap_lock);
1900 list_splice(&tmp_list, &session->s_cap_releases);
1901 session->s_num_cap_releases += num_cap_releases;
Sage Weil2f2dc052009-10-06 11:31:09 -07001902 spin_unlock(&session->s_cap_lock);
1903}
1904
Yan, Zhenge3ec8d62019-01-14 17:21:19 +08001905static void ceph_cap_release_work(struct work_struct *work)
1906{
1907 struct ceph_mds_session *session =
1908 container_of(work, struct ceph_mds_session, s_cap_release_work);
1909
1910 mutex_lock(&session->s_mutex);
1911 if (session->s_state == CEPH_MDS_SESSION_OPEN ||
1912 session->s_state == CEPH_MDS_SESSION_HUNG)
1913 ceph_send_cap_releases(session->s_mdsc, session);
1914 mutex_unlock(&session->s_mutex);
1915 ceph_put_mds_session(session);
1916}
1917
1918void ceph_flush_cap_releases(struct ceph_mds_client *mdsc,
1919 struct ceph_mds_session *session)
1920{
1921 if (mdsc->stopping)
1922 return;
1923
1924 get_session(session);
1925 if (queue_work(mdsc->fsc->cap_wq,
1926 &session->s_cap_release_work)) {
1927 dout("cap release work queued\n");
1928 } else {
1929 ceph_put_mds_session(session);
1930 dout("failed to queue cap release work\n");
1931 }
1932}
1933
1934/*
1935 * caller holds session->s_cap_lock
1936 */
1937void __ceph_queue_cap_release(struct ceph_mds_session *session,
1938 struct ceph_cap *cap)
1939{
1940 list_add_tail(&cap->session_caps, &session->s_cap_releases);
1941 session->s_num_cap_releases++;
1942
1943 if (!(session->s_num_cap_releases % CEPH_CAPS_PER_RELEASE))
1944 ceph_flush_cap_releases(session->s_mdsc, session);
1945}
1946
Yan, Zheng37c4efc2019-01-31 16:55:51 +08001947static void ceph_cap_reclaim_work(struct work_struct *work)
1948{
1949 struct ceph_mds_client *mdsc =
1950 container_of(work, struct ceph_mds_client, cap_reclaim_work);
1951 int ret = ceph_trim_dentries(mdsc);
1952 if (ret == -EAGAIN)
1953 ceph_queue_cap_reclaim_work(mdsc);
1954}
1955
1956void ceph_queue_cap_reclaim_work(struct ceph_mds_client *mdsc)
1957{
1958 if (mdsc->stopping)
1959 return;
1960
1961 if (queue_work(mdsc->fsc->cap_wq, &mdsc->cap_reclaim_work)) {
1962 dout("caps reclaim work queued\n");
1963 } else {
1964 dout("failed to queue caps release work\n");
1965 }
1966}
1967
Yan, Zhengfe330322019-02-01 14:57:15 +08001968void ceph_reclaim_caps_nr(struct ceph_mds_client *mdsc, int nr)
1969{
1970 int val;
1971 if (!nr)
1972 return;
1973 val = atomic_add_return(nr, &mdsc->cap_reclaim_pending);
1974 if (!(val % CEPH_CAPS_PER_RELEASE)) {
1975 atomic_set(&mdsc->cap_reclaim_pending, 0);
1976 ceph_queue_cap_reclaim_work(mdsc);
1977 }
1978}
1979
Sage Weil2f2dc052009-10-06 11:31:09 -07001980/*
1981 * requests
1982 */
1983
Yan, Zheng54008392014-03-29 13:41:15 +08001984int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
1985 struct inode *dir)
1986{
1987 struct ceph_inode_info *ci = ceph_inode(dir);
1988 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1989 struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options;
Yan, Zheng2a5beea2016-04-28 09:37:39 +08001990 size_t size = sizeof(struct ceph_mds_reply_dir_entry);
Yan, Zheng54008392014-03-29 13:41:15 +08001991 int order, num_entries;
1992
1993 spin_lock(&ci->i_ceph_lock);
1994 num_entries = ci->i_files + ci->i_subdirs;
1995 spin_unlock(&ci->i_ceph_lock);
1996 num_entries = max(num_entries, 1);
1997 num_entries = min(num_entries, opt->max_readdir);
1998
1999 order = get_order(size * num_entries);
2000 while (order >= 0) {
Yan, Zheng2a5beea2016-04-28 09:37:39 +08002001 rinfo->dir_entries = (void*)__get_free_pages(GFP_KERNEL |
2002 __GFP_NOWARN,
2003 order);
2004 if (rinfo->dir_entries)
Yan, Zheng54008392014-03-29 13:41:15 +08002005 break;
2006 order--;
2007 }
Yan, Zheng2a5beea2016-04-28 09:37:39 +08002008 if (!rinfo->dir_entries)
Yan, Zheng54008392014-03-29 13:41:15 +08002009 return -ENOMEM;
2010
2011 num_entries = (PAGE_SIZE << order) / size;
2012 num_entries = min(num_entries, opt->max_readdir);
2013
2014 rinfo->dir_buf_size = PAGE_SIZE << order;
2015 req->r_num_caps = num_entries + 1;
2016 req->r_args.readdir.max_entries = cpu_to_le32(num_entries);
2017 req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes);
2018 return 0;
2019}
2020
Sage Weil2f2dc052009-10-06 11:31:09 -07002021/*
2022 * Create an mds request.
2023 */
2024struct ceph_mds_request *
2025ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
2026{
2027 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
Arnd Bergmann63ecae72018-07-13 22:18:35 +02002028 struct timespec64 ts;
Sage Weil2f2dc052009-10-06 11:31:09 -07002029
2030 if (!req)
2031 return ERR_PTR(-ENOMEM);
2032
Sage Weilb4556392010-05-13 12:01:13 -07002033 mutex_init(&req->r_fill_mutex);
Yehuda Sadeh37151662010-06-17 16:16:12 -07002034 req->r_mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07002035 req->r_started = jiffies;
2036 req->r_resend_mds = -1;
2037 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002038 INIT_LIST_HEAD(&req->r_unsafe_target_item);
Sage Weil2f2dc052009-10-06 11:31:09 -07002039 req->r_fmode = -1;
Sage Weil153c8e62009-12-07 12:31:09 -08002040 kref_init(&req->r_kref);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02002041 RB_CLEAR_NODE(&req->r_node);
Sage Weil2f2dc052009-10-06 11:31:09 -07002042 INIT_LIST_HEAD(&req->r_wait);
2043 init_completion(&req->r_completion);
2044 init_completion(&req->r_safe_completion);
2045 INIT_LIST_HEAD(&req->r_unsafe_item);
2046
Arnd Bergmann63ecae72018-07-13 22:18:35 +02002047 ktime_get_coarse_real_ts64(&ts);
Arnd Bergmann0ed1e902018-07-13 22:18:38 +02002048 req->r_stamp = timespec64_trunc(ts, mdsc->fsc->sb->s_time_gran);
Sage Weilb8e69062014-05-21 17:41:08 -07002049
Sage Weil2f2dc052009-10-06 11:31:09 -07002050 req->r_op = op;
2051 req->r_direct_mode = mode;
2052 return req;
2053}
2054
2055/*
Sage Weil44ca18f2010-02-15 12:08:46 -08002056 * return oldest (lowest) request, tid in request tree, 0 if none.
Sage Weil2f2dc052009-10-06 11:31:09 -07002057 *
2058 * called under mdsc->mutex.
2059 */
Sage Weil44ca18f2010-02-15 12:08:46 -08002060static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
2061{
2062 if (RB_EMPTY_ROOT(&mdsc->request_tree))
2063 return NULL;
2064 return rb_entry(rb_first(&mdsc->request_tree),
2065 struct ceph_mds_request, r_node);
2066}
2067
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08002068static inline u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07002069{
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08002070 return mdsc->oldest_tid;
Sage Weil2f2dc052009-10-06 11:31:09 -07002071}
2072
2073/*
2074 * Build a dentry's path. Allocate on heap; caller must kfree. Based
2075 * on build_path_from_dentry in fs/cifs/dir.c.
2076 *
2077 * If @stop_on_nosnap, generate path relative to the first non-snapped
2078 * inode.
2079 *
2080 * Encode hidden .snap dirs as a double /, i.e.
2081 * foo/.snap/bar -> foo//bar
2082 */
2083char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
2084 int stop_on_nosnap)
2085{
2086 struct dentry *temp;
2087 char *path;
2088 int len, pos;
Al Viro1b71fe22011-07-16 23:43:58 -04002089 unsigned seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002090
Markus Elfringd37b1d92017-08-20 20:22:02 +02002091 if (!dentry)
Sage Weil2f2dc052009-10-06 11:31:09 -07002092 return ERR_PTR(-EINVAL);
2093
2094retry:
2095 len = 0;
Al Viro1b71fe22011-07-16 23:43:58 -04002096 seq = read_seqbegin(&rename_lock);
2097 rcu_read_lock();
Sage Weil2f2dc052009-10-06 11:31:09 -07002098 for (temp = dentry; !IS_ROOT(temp);) {
David Howells2b0143b2015-03-17 22:25:59 +00002099 struct inode *inode = d_inode(temp);
Sage Weil2f2dc052009-10-06 11:31:09 -07002100 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
2101 len++; /* slash only */
2102 else if (stop_on_nosnap && inode &&
2103 ceph_snap(inode) == CEPH_NOSNAP)
2104 break;
2105 else
2106 len += 1 + temp->d_name.len;
2107 temp = temp->d_parent;
Sage Weil2f2dc052009-10-06 11:31:09 -07002108 }
Al Viro1b71fe22011-07-16 23:43:58 -04002109 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -07002110 if (len)
2111 len--; /* no leading '/' */
2112
2113 path = kmalloc(len+1, GFP_NOFS);
Markus Elfringd37b1d92017-08-20 20:22:02 +02002114 if (!path)
Sage Weil2f2dc052009-10-06 11:31:09 -07002115 return ERR_PTR(-ENOMEM);
2116 pos = len;
2117 path[pos] = 0; /* trailing null */
Al Viro1b71fe22011-07-16 23:43:58 -04002118 rcu_read_lock();
Sage Weil2f2dc052009-10-06 11:31:09 -07002119 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
Al Viro1b71fe22011-07-16 23:43:58 -04002120 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07002121
Al Viro1b71fe22011-07-16 23:43:58 -04002122 spin_lock(&temp->d_lock);
David Howells2b0143b2015-03-17 22:25:59 +00002123 inode = d_inode(temp);
Sage Weil2f2dc052009-10-06 11:31:09 -07002124 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
Sage Weil104648a2010-03-18 10:14:30 -07002125 dout("build_path path+%d: %p SNAPDIR\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002126 pos, temp);
2127 } else if (stop_on_nosnap && inode &&
2128 ceph_snap(inode) == CEPH_NOSNAP) {
Yehuda Sadeh9d5a09e2011-12-13 09:57:44 -08002129 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002130 break;
2131 } else {
2132 pos -= temp->d_name.len;
Al Viro1b71fe22011-07-16 23:43:58 -04002133 if (pos < 0) {
2134 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002135 break;
Al Viro1b71fe22011-07-16 23:43:58 -04002136 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002137 strncpy(path + pos, temp->d_name.name,
2138 temp->d_name.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07002139 }
Al Viro1b71fe22011-07-16 23:43:58 -04002140 spin_unlock(&temp->d_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07002141 if (pos)
2142 path[--pos] = '/';
2143 temp = temp->d_parent;
Sage Weil2f2dc052009-10-06 11:31:09 -07002144 }
Al Viro1b71fe22011-07-16 23:43:58 -04002145 rcu_read_unlock();
2146 if (pos != 0 || read_seqretry(&rename_lock, seq)) {
Sage Weil104648a2010-03-18 10:14:30 -07002147 pr_err("build_path did not end path lookup where "
Sage Weil2f2dc052009-10-06 11:31:09 -07002148 "expected, namelen is %d, pos is %d\n", len, pos);
2149 /* presumably this is only possible if racing with a
2150 rename of one of the parent directories (we can not
2151 lock the dentries above us to prevent this, but
2152 retrying should be harmless) */
2153 kfree(path);
2154 goto retry;
2155 }
2156
David Howells2b0143b2015-03-17 22:25:59 +00002157 *base = ceph_ino(d_inode(temp));
Sage Weil2f2dc052009-10-06 11:31:09 -07002158 *plen = len;
Sage Weil104648a2010-03-18 10:14:30 -07002159 dout("build_path on %p %d built %llx '%.*s'\n",
Al Viro84d08fa2013-07-05 18:59:33 +04002160 dentry, d_count(dentry), *base, len, path);
Sage Weil2f2dc052009-10-06 11:31:09 -07002161 return path;
2162}
2163
Jeff Laytonfd36a712016-12-15 08:37:58 -05002164static int build_dentry_path(struct dentry *dentry, struct inode *dir,
Sage Weil2f2dc052009-10-06 11:31:09 -07002165 const char **ppath, int *ppathlen, u64 *pino,
2166 int *pfreepath)
2167{
2168 char *path;
2169
Jeff Laytonc6b0b652016-12-15 08:37:57 -05002170 rcu_read_lock();
Jeff Laytonfd36a712016-12-15 08:37:58 -05002171 if (!dir)
2172 dir = d_inode_rcu(dentry->d_parent);
Jeff Laytonc6b0b652016-12-15 08:37:57 -05002173 if (dir && ceph_snap(dir) == CEPH_NOSNAP) {
2174 *pino = ceph_ino(dir);
2175 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -07002176 *ppath = dentry->d_name.name;
2177 *ppathlen = dentry->d_name.len;
2178 return 0;
2179 }
Jeff Laytonc6b0b652016-12-15 08:37:57 -05002180 rcu_read_unlock();
Sage Weil2f2dc052009-10-06 11:31:09 -07002181 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
2182 if (IS_ERR(path))
2183 return PTR_ERR(path);
2184 *ppath = path;
2185 *pfreepath = 1;
2186 return 0;
2187}
2188
2189static int build_inode_path(struct inode *inode,
2190 const char **ppath, int *ppathlen, u64 *pino,
2191 int *pfreepath)
2192{
2193 struct dentry *dentry;
2194 char *path;
2195
2196 if (ceph_snap(inode) == CEPH_NOSNAP) {
2197 *pino = ceph_ino(inode);
2198 *ppathlen = 0;
2199 return 0;
2200 }
2201 dentry = d_find_alias(inode);
2202 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
2203 dput(dentry);
2204 if (IS_ERR(path))
2205 return PTR_ERR(path);
2206 *ppath = path;
2207 *pfreepath = 1;
2208 return 0;
2209}
2210
2211/*
2212 * request arguments may be specified via an inode *, a dentry *, or
2213 * an explicit ino+path.
2214 */
2215static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
Jeff Laytonfd36a712016-12-15 08:37:58 -05002216 struct inode *rdiri, const char *rpath,
2217 u64 rino, const char **ppath, int *pathlen,
Sage Weil2f2dc052009-10-06 11:31:09 -07002218 u64 *ino, int *freepath)
2219{
2220 int r = 0;
2221
2222 if (rinode) {
2223 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
2224 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
2225 ceph_snap(rinode));
2226 } else if (rdentry) {
Jeff Laytonfd36a712016-12-15 08:37:58 -05002227 r = build_dentry_path(rdentry, rdiri, ppath, pathlen, ino,
2228 freepath);
Sage Weil2f2dc052009-10-06 11:31:09 -07002229 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
2230 *ppath);
Sage Weil795858d2011-08-15 13:02:37 -07002231 } else if (rpath || rino) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002232 *ino = rino;
2233 *ppath = rpath;
David Zafmanb0000562012-10-25 10:23:46 -07002234 *pathlen = rpath ? strlen(rpath) : 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002235 dout(" path %.*s\n", *pathlen, rpath);
2236 }
2237
2238 return r;
2239}
2240
2241/*
2242 * called under mdsc->mutex
2243 */
2244static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
2245 struct ceph_mds_request *req,
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002246 int mds, bool drop_cap_releases)
Sage Weil2f2dc052009-10-06 11:31:09 -07002247{
2248 struct ceph_msg *msg;
2249 struct ceph_mds_request_head *head;
2250 const char *path1 = NULL;
2251 const char *path2 = NULL;
2252 u64 ino1 = 0, ino2 = 0;
2253 int pathlen1 = 0, pathlen2 = 0;
2254 int freepath1 = 0, freepath2 = 0;
2255 int len;
2256 u16 releases;
2257 void *p, *end;
2258 int ret;
2259
2260 ret = set_request_path_attr(req->r_inode, req->r_dentry,
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002261 req->r_parent, req->r_path1, req->r_ino1.ino,
Sage Weil2f2dc052009-10-06 11:31:09 -07002262 &path1, &pathlen1, &ino1, &freepath1);
2263 if (ret < 0) {
2264 msg = ERR_PTR(ret);
2265 goto out;
2266 }
2267
2268 ret = set_request_path_attr(NULL, req->r_old_dentry,
Jeff Laytonfd36a712016-12-15 08:37:58 -05002269 req->r_old_dentry_dir,
Sage Weil2f2dc052009-10-06 11:31:09 -07002270 req->r_path2, req->r_ino2.ino,
2271 &path2, &pathlen2, &ino2, &freepath2);
2272 if (ret < 0) {
2273 msg = ERR_PTR(ret);
2274 goto out_free1;
2275 }
2276
2277 len = sizeof(*head) +
Sage Weilb8e69062014-05-21 17:41:08 -07002278 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) +
Arnd Bergmann777d7382015-09-30 15:04:42 +02002279 sizeof(struct ceph_timespec);
Sage Weil2f2dc052009-10-06 11:31:09 -07002280
2281 /* calculate (max) length for cap releases */
2282 len += sizeof(struct ceph_mds_request_release) *
2283 (!!req->r_inode_drop + !!req->r_dentry_drop +
2284 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
2285 if (req->r_dentry_drop)
2286 len += req->r_dentry->d_name.len;
2287 if (req->r_old_dentry_drop)
2288 len += req->r_old_dentry->d_name.len;
2289
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02002290 msg = ceph_msg_new2(CEPH_MSG_CLIENT_REQUEST, len, 1, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07002291 if (!msg) {
2292 msg = ERR_PTR(-ENOMEM);
Sage Weil2f2dc052009-10-06 11:31:09 -07002293 goto out_free2;
Sage Weila79832f2010-04-01 16:06:19 -07002294 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002295
John Spray7cfa0312014-10-30 17:15:26 +00002296 msg->hdr.version = cpu_to_le16(2);
Sage Weil6df058c2009-12-22 11:24:33 -08002297 msg->hdr.tid = cpu_to_le64(req->r_tid);
2298
Sage Weil2f2dc052009-10-06 11:31:09 -07002299 head = msg->front.iov_base;
2300 p = msg->front.iov_base + sizeof(*head);
2301 end = msg->front.iov_base + msg->front.iov_len;
2302
2303 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
2304 head->op = cpu_to_le32(req->r_op);
Eric W. Biedermanff3d0042013-01-31 04:01:53 -08002305 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid));
2306 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid));
Sage Weil2f2dc052009-10-06 11:31:09 -07002307 head->args = req->r_args;
2308
2309 ceph_encode_filepath(&p, end, ino1, path1);
2310 ceph_encode_filepath(&p, end, ino2, path2);
2311
Sage Weile979cf52010-07-15 14:58:39 -07002312 /* make note of release offset, in case we need to replay */
2313 req->r_request_release_offset = p - msg->front.iov_base;
2314
Sage Weil2f2dc052009-10-06 11:31:09 -07002315 /* cap releases */
2316 releases = 0;
2317 if (req->r_inode_drop)
2318 releases += ceph_encode_inode_release(&p,
David Howells2b0143b2015-03-17 22:25:59 +00002319 req->r_inode ? req->r_inode : d_inode(req->r_dentry),
Sage Weil2f2dc052009-10-06 11:31:09 -07002320 mds, req->r_inode_drop, req->r_inode_unless, 0);
2321 if (req->r_dentry_drop)
2322 releases += ceph_encode_dentry_release(&p, req->r_dentry,
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002323 req->r_parent, mds, req->r_dentry_drop,
Jeff Laytonca6c8ae2016-12-15 08:37:59 -05002324 req->r_dentry_unless);
Sage Weil2f2dc052009-10-06 11:31:09 -07002325 if (req->r_old_dentry_drop)
2326 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
Jeff Laytonca6c8ae2016-12-15 08:37:59 -05002327 req->r_old_dentry_dir, mds,
2328 req->r_old_dentry_drop,
2329 req->r_old_dentry_unless);
Sage Weil2f2dc052009-10-06 11:31:09 -07002330 if (req->r_old_inode_drop)
2331 releases += ceph_encode_inode_release(&p,
David Howells2b0143b2015-03-17 22:25:59 +00002332 d_inode(req->r_old_dentry),
Sage Weil2f2dc052009-10-06 11:31:09 -07002333 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002334
2335 if (drop_cap_releases) {
2336 releases = 0;
2337 p = msg->front.iov_base + req->r_request_release_offset;
2338 }
2339
Sage Weil2f2dc052009-10-06 11:31:09 -07002340 head->num_releases = cpu_to_le16(releases);
2341
Sage Weilb8e69062014-05-21 17:41:08 -07002342 /* time stamp */
Yan, Zheng1f041a82015-01-13 15:20:52 +08002343 {
2344 struct ceph_timespec ts;
Arnd Bergmann0ed1e902018-07-13 22:18:38 +02002345 ceph_encode_timespec64(&ts, &req->r_stamp);
Yan, Zheng1f041a82015-01-13 15:20:52 +08002346 ceph_encode_copy(&p, &ts, sizeof(ts));
2347 }
Sage Weilb8e69062014-05-21 17:41:08 -07002348
Sage Weil2f2dc052009-10-06 11:31:09 -07002349 BUG_ON(p > end);
2350 msg->front.iov_len = p - msg->front.iov_base;
2351 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2352
Yan, Zheng25e6bae2014-09-16 19:15:28 +08002353 if (req->r_pagelist) {
2354 struct ceph_pagelist *pagelist = req->r_pagelist;
Yan, Zheng25e6bae2014-09-16 19:15:28 +08002355 ceph_msg_data_add_pagelist(msg, pagelist);
2356 msg->hdr.data_len = cpu_to_le32(pagelist->length);
2357 } else {
2358 msg->hdr.data_len = 0;
Alex Elderebf18f42013-03-04 22:29:57 -06002359 }
Alex Elder02afca62013-02-14 12:16:43 -06002360
Sage Weil2f2dc052009-10-06 11:31:09 -07002361 msg->hdr.data_off = cpu_to_le16(0);
2362
2363out_free2:
2364 if (freepath2)
2365 kfree((char *)path2);
2366out_free1:
2367 if (freepath1)
2368 kfree((char *)path1);
2369out:
2370 return msg;
2371}
2372
2373/*
2374 * called under mdsc->mutex if error, under no mutex if
2375 * success.
2376 */
2377static void complete_request(struct ceph_mds_client *mdsc,
2378 struct ceph_mds_request *req)
2379{
2380 if (req->r_callback)
2381 req->r_callback(mdsc, req);
2382 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002383 complete_all(&req->r_completion);
Sage Weil2f2dc052009-10-06 11:31:09 -07002384}
2385
2386/*
2387 * called under mdsc->mutex
2388 */
2389static int __prepare_send_request(struct ceph_mds_client *mdsc,
2390 struct ceph_mds_request *req,
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002391 int mds, bool drop_cap_releases)
Sage Weil2f2dc052009-10-06 11:31:09 -07002392{
2393 struct ceph_mds_request_head *rhead;
2394 struct ceph_msg *msg;
2395 int flags = 0;
2396
Sage Weil2f2dc052009-10-06 11:31:09 -07002397 req->r_attempts++;
Greg Farnume55b71f2010-06-22 15:58:01 -07002398 if (req->r_inode) {
2399 struct ceph_cap *cap =
2400 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
2401
2402 if (cap)
2403 req->r_sent_on_mseq = cap->mseq;
2404 else
2405 req->r_sent_on_mseq = -1;
2406 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002407 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
2408 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
2409
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002410 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002411 void *p;
Sage Weil01a92f12010-07-15 13:24:32 -07002412 /*
2413 * Replay. Do not regenerate message (and rebuild
2414 * paths, etc.); just use the original message.
2415 * Rebuilding paths will break for renames because
2416 * d_move mangles the src name.
2417 */
2418 msg = req->r_request;
2419 rhead = msg->front.iov_base;
2420
2421 flags = le32_to_cpu(rhead->flags);
2422 flags |= CEPH_MDS_FLAG_REPLAY;
2423 rhead->flags = cpu_to_le32(flags);
2424
2425 if (req->r_target_inode)
2426 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
2427
2428 rhead->num_retry = req->r_attempts - 1;
Sage Weile979cf52010-07-15 14:58:39 -07002429
2430 /* remove cap/dentry releases from message */
2431 rhead->num_releases = 0;
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002432
2433 /* time stamp */
2434 p = msg->front.iov_base + req->r_request_release_offset;
Yan, Zheng1f041a82015-01-13 15:20:52 +08002435 {
2436 struct ceph_timespec ts;
Arnd Bergmann0ed1e902018-07-13 22:18:38 +02002437 ceph_encode_timespec64(&ts, &req->r_stamp);
Yan, Zheng1f041a82015-01-13 15:20:52 +08002438 ceph_encode_copy(&p, &ts, sizeof(ts));
2439 }
Yan, Zhengc5c9a0b2014-07-01 16:54:34 +08002440
2441 msg->front.iov_len = p - msg->front.iov_base;
2442 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
Sage Weil01a92f12010-07-15 13:24:32 -07002443 return 0;
2444 }
2445
Sage Weil2f2dc052009-10-06 11:31:09 -07002446 if (req->r_request) {
2447 ceph_msg_put(req->r_request);
2448 req->r_request = NULL;
2449 }
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002450 msg = create_request_message(mdsc, req, mds, drop_cap_releases);
Sage Weil2f2dc052009-10-06 11:31:09 -07002451 if (IS_ERR(msg)) {
Sage Weile1518c72010-05-13 11:19:06 -07002452 req->r_err = PTR_ERR(msg);
Sage Weila79832f2010-04-01 16:06:19 -07002453 return PTR_ERR(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002454 }
2455 req->r_request = msg;
2456
2457 rhead = msg->front.iov_base;
Sage Weil2f2dc052009-10-06 11:31:09 -07002458 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002459 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Sage Weil2f2dc052009-10-06 11:31:09 -07002460 flags |= CEPH_MDS_FLAG_REPLAY;
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002461 if (req->r_parent)
Sage Weil2f2dc052009-10-06 11:31:09 -07002462 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
2463 rhead->flags = cpu_to_le32(flags);
2464 rhead->num_fwd = req->r_num_fwd;
2465 rhead->num_retry = req->r_attempts - 1;
Sage Weil01a92f12010-07-15 13:24:32 -07002466 rhead->ino = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002467
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002468 dout(" r_parent = %p\n", req->r_parent);
Sage Weil2f2dc052009-10-06 11:31:09 -07002469 return 0;
2470}
2471
2472/*
2473 * send request, or put it on the appropriate wait list.
2474 */
Chengguang Xud5548492018-07-28 16:30:48 +08002475static void __do_request(struct ceph_mds_client *mdsc,
Sage Weil2f2dc052009-10-06 11:31:09 -07002476 struct ceph_mds_request *req)
2477{
2478 struct ceph_mds_session *session = NULL;
2479 int mds = -1;
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002480 int err = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002481
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002482 if (req->r_err || test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
2483 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
Yan, Zhengeb1b8af2013-09-26 14:25:36 +08002484 __unregister_request(mdsc, req);
Chengguang Xud5548492018-07-28 16:30:48 +08002485 return;
Yan, Zhengeb1b8af2013-09-26 14:25:36 +08002486 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002487
2488 if (req->r_timeout &&
2489 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
2490 dout("do_request timed out\n");
2491 err = -EIO;
2492 goto finish;
2493 }
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01002494 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002495 dout("do_request forced umount\n");
2496 err = -EIO;
2497 goto finish;
2498 }
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01002499 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_MOUNTING) {
Yan, Zhenge9e427f2016-11-10 16:02:06 +08002500 if (mdsc->mdsmap_err) {
2501 err = mdsc->mdsmap_err;
2502 dout("do_request mdsmap err %d\n", err);
2503 goto finish;
2504 }
Yan, Zhengcc8e8342017-01-04 16:21:58 +08002505 if (mdsc->mdsmap->m_epoch == 0) {
2506 dout("do_request no mdsmap, waiting for map\n");
2507 list_add(&req->r_wait, &mdsc->waiting_for_map);
Chengguang Xud5548492018-07-28 16:30:48 +08002508 return;
Yan, Zhengcc8e8342017-01-04 16:21:58 +08002509 }
Yan, Zhenge9e427f2016-11-10 16:02:06 +08002510 if (!(mdsc->fsc->mount_options->flags &
2511 CEPH_MOUNT_OPT_MOUNTWAIT) &&
2512 !ceph_mdsmap_is_cluster_available(mdsc->mdsmap)) {
2513 err = -ENOENT;
2514 pr_info("probably no mds server is up\n");
2515 goto finish;
2516 }
2517 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002518
Sage Weildc69e2e2010-11-02 13:49:00 -07002519 put_request_session(req);
2520
Sage Weil2f2dc052009-10-06 11:31:09 -07002521 mds = __choose_mds(mdsc, req);
2522 if (mds < 0 ||
2523 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
2524 dout("do_request no mds or not active, waiting for map\n");
2525 list_add(&req->r_wait, &mdsc->waiting_for_map);
Chengguang Xud5548492018-07-28 16:30:48 +08002526 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07002527 }
2528
2529 /* get, open session */
2530 session = __ceph_lookup_mds_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07002531 if (!session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002532 session = register_session(mdsc, mds);
Sage Weil9c4239562010-03-20 20:43:28 -07002533 if (IS_ERR(session)) {
2534 err = PTR_ERR(session);
2535 goto finish;
2536 }
2537 }
Sage Weildc69e2e2010-11-02 13:49:00 -07002538 req->r_session = get_session(session);
2539
Sage Weil2f2dc052009-10-06 11:31:09 -07002540 dout("do_request mds%d session %p state %s\n", mds, session,
John Spraya687eca2014-09-19 13:51:08 +01002541 ceph_session_state_name(session->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07002542 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
2543 session->s_state != CEPH_MDS_SESSION_HUNG) {
Yan, Zhengfcff4152016-09-14 16:39:51 +08002544 if (session->s_state == CEPH_MDS_SESSION_REJECTED) {
2545 err = -EACCES;
2546 goto out_session;
2547 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002548 if (session->s_state == CEPH_MDS_SESSION_NEW ||
2549 session->s_state == CEPH_MDS_SESSION_CLOSING)
2550 __open_session(mdsc, session);
2551 list_add(&req->r_wait, &session->s_waiting);
2552 goto out_session;
2553 }
2554
2555 /* send request */
Sage Weil2f2dc052009-10-06 11:31:09 -07002556 req->r_resend_mds = -1; /* forget any previous mds hint */
2557
2558 if (req->r_request_started == 0) /* note request start time */
2559 req->r_request_started = jiffies;
2560
Yan, Zheng6e6f0922015-02-27 08:54:08 +08002561 err = __prepare_send_request(mdsc, req, mds, false);
Sage Weil2f2dc052009-10-06 11:31:09 -07002562 if (!err) {
2563 ceph_msg_get(req->r_request);
2564 ceph_con_send(&session->s_con, req->r_request);
2565 }
2566
2567out_session:
2568 ceph_put_mds_session(session);
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002569finish:
2570 if (err) {
2571 dout("__do_request early error %d\n", err);
2572 req->r_err = err;
2573 complete_request(mdsc, req);
2574 __unregister_request(mdsc, req);
2575 }
Chengguang Xud5548492018-07-28 16:30:48 +08002576 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07002577}
2578
2579/*
2580 * called under mdsc->mutex
2581 */
2582static void __wake_requests(struct ceph_mds_client *mdsc,
2583 struct list_head *head)
2584{
Yan, Zhenged75ec22012-11-19 10:49:06 +08002585 struct ceph_mds_request *req;
2586 LIST_HEAD(tmp_list);
Sage Weil2f2dc052009-10-06 11:31:09 -07002587
Yan, Zhenged75ec22012-11-19 10:49:06 +08002588 list_splice_init(head, &tmp_list);
2589
2590 while (!list_empty(&tmp_list)) {
2591 req = list_entry(tmp_list.next,
2592 struct ceph_mds_request, r_wait);
Sage Weil2f2dc052009-10-06 11:31:09 -07002593 list_del_init(&req->r_wait);
Sage Weil7971bd92013-05-01 21:15:58 -07002594 dout(" wake request %p tid %llu\n", req, req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002595 __do_request(mdsc, req);
2596 }
2597}
2598
2599/*
2600 * Wake up threads with requests pending for @mds, so that they can
Sage Weil29790f22010-03-18 14:45:05 -07002601 * resubmit their requests to a possibly different mds.
Sage Weil2f2dc052009-10-06 11:31:09 -07002602 */
Sage Weil29790f22010-03-18 14:45:05 -07002603static void kick_requests(struct ceph_mds_client *mdsc, int mds)
Sage Weil2f2dc052009-10-06 11:31:09 -07002604{
Sage Weil44ca18f2010-02-15 12:08:46 -08002605 struct ceph_mds_request *req;
Yan, Zheng282c1052014-07-30 10:12:47 +08002606 struct rb_node *p = rb_first(&mdsc->request_tree);
Sage Weil2f2dc052009-10-06 11:31:09 -07002607
2608 dout("kick_requests mds%d\n", mds);
Yan, Zheng282c1052014-07-30 10:12:47 +08002609 while (p) {
Sage Weil44ca18f2010-02-15 12:08:46 -08002610 req = rb_entry(p, struct ceph_mds_request, r_node);
Yan, Zheng282c1052014-07-30 10:12:47 +08002611 p = rb_next(p);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002612 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Sage Weil44ca18f2010-02-15 12:08:46 -08002613 continue;
Yan, Zheng3de22be2015-02-04 14:26:22 +08002614 if (req->r_attempts > 0)
2615 continue; /* only new requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08002616 if (req->r_session &&
2617 req->r_session->s_mds == mds) {
2618 dout(" kicking tid %llu\n", req->r_tid);
Yan, Zheng03974e82014-09-11 14:28:56 +08002619 list_del_init(&req->r_wait);
Sage Weil44ca18f2010-02-15 12:08:46 -08002620 __do_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002621 }
2622 }
2623}
2624
2625void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
2626 struct ceph_mds_request *req)
2627{
2628 dout("submit_request on %p\n", req);
2629 mutex_lock(&mdsc->mutex);
2630 __register_request(mdsc, req, NULL);
2631 __do_request(mdsc, req);
2632 mutex_unlock(&mdsc->mutex);
2633}
2634
2635/*
2636 * Synchrously perform an mds request. Take care of all of the
2637 * session setup, forwarding, retry details.
2638 */
2639int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
2640 struct inode *dir,
2641 struct ceph_mds_request *req)
2642{
2643 int err;
2644
2645 dout("do_request on %p\n", req);
2646
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002647 /* take CAP_PIN refs for r_inode, r_parent, r_old_dentry */
Sage Weil2f2dc052009-10-06 11:31:09 -07002648 if (req->r_inode)
2649 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002650 if (req->r_parent)
2651 ceph_get_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
Sage Weil844d87c2013-02-05 13:40:09 -08002652 if (req->r_old_dentry_dir)
Sage Weil41b02e12011-07-26 11:31:14 -07002653 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
2654 CEPH_CAP_PIN);
Sage Weil2f2dc052009-10-06 11:31:09 -07002655
2656 /* issue */
2657 mutex_lock(&mdsc->mutex);
2658 __register_request(mdsc, req, dir);
2659 __do_request(mdsc, req);
2660
Sage Weile1518c72010-05-13 11:19:06 -07002661 if (req->r_err) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002662 err = req->r_err;
Sage Weile1518c72010-05-13 11:19:06 -07002663 goto out;
Sage Weil2f2dc052009-10-06 11:31:09 -07002664 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002665
Sage Weile1518c72010-05-13 11:19:06 -07002666 /* wait */
2667 mutex_unlock(&mdsc->mutex);
2668 dout("do_request waiting\n");
Ilya Dryomov5be73032015-05-19 12:05:38 +03002669 if (!req->r_timeout && req->r_wait_for_completion) {
Yan, Zheng9280be22014-10-14 10:33:35 +08002670 err = req->r_wait_for_completion(mdsc, req);
Sage Weile1518c72010-05-13 11:19:06 -07002671 } else {
Ilya Dryomov5be73032015-05-19 12:05:38 +03002672 long timeleft = wait_for_completion_killable_timeout(
2673 &req->r_completion,
2674 ceph_timeout_jiffies(req->r_timeout));
2675 if (timeleft > 0)
2676 err = 0;
2677 else if (!timeleft)
2678 err = -EIO; /* timed out */
2679 else
2680 err = timeleft; /* killed */
Sage Weile1518c72010-05-13 11:19:06 -07002681 }
2682 dout("do_request waited, got %d\n", err);
2683 mutex_lock(&mdsc->mutex);
2684
2685 /* only abort if we didn't race with a real reply */
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002686 if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
Sage Weile1518c72010-05-13 11:19:06 -07002687 err = le32_to_cpu(req->r_reply_info.head->result);
2688 } else if (err < 0) {
2689 dout("aborted request %lld with %d\n", req->r_tid, err);
Sage Weilb4556392010-05-13 12:01:13 -07002690
2691 /*
2692 * ensure we aren't running concurrently with
2693 * ceph_fill_trace or ceph_readdir_prepopulate, which
2694 * rely on locks (dir mutex) held by our caller.
2695 */
2696 mutex_lock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07002697 req->r_err = err;
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002698 set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
Sage Weilb4556392010-05-13 12:01:13 -07002699 mutex_unlock(&req->r_fill_mutex);
Sage Weile1518c72010-05-13 11:19:06 -07002700
Jeff Layton3dd69aa2017-01-31 10:28:26 -05002701 if (req->r_parent &&
Sage Weil167c9e32010-05-14 10:02:57 -07002702 (req->r_op & CEPH_MDS_OP_WRITE))
2703 ceph_invalidate_dir_request(req);
Sage Weile1518c72010-05-13 11:19:06 -07002704 } else {
2705 err = req->r_err;
2706 }
2707
2708out:
2709 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002710 dout("do_request %p done, result %d\n", req, err);
2711 return err;
2712}
2713
2714/*
Yan, Zheng2f276c52013-03-13 19:44:32 +08002715 * Invalidate dir's completeness, dentry lease state on an aborted MDS
Sage Weil167c9e32010-05-14 10:02:57 -07002716 * namespace request.
2717 */
2718void ceph_invalidate_dir_request(struct ceph_mds_request *req)
2719{
Yan, Zheng8d8f3712017-11-24 11:51:32 +08002720 struct inode *dir = req->r_parent;
2721 struct inode *old_dir = req->r_old_dentry_dir;
Sage Weil167c9e32010-05-14 10:02:57 -07002722
Yan, Zheng8d8f3712017-11-24 11:51:32 +08002723 dout("invalidate_dir_request %p %p (complete, lease(s))\n", dir, old_dir);
Sage Weil167c9e32010-05-14 10:02:57 -07002724
Yan, Zheng8d8f3712017-11-24 11:51:32 +08002725 ceph_dir_clear_complete(dir);
2726 if (old_dir)
2727 ceph_dir_clear_complete(old_dir);
Sage Weil167c9e32010-05-14 10:02:57 -07002728 if (req->r_dentry)
2729 ceph_invalidate_dentry_lease(req->r_dentry);
2730 if (req->r_old_dentry)
2731 ceph_invalidate_dentry_lease(req->r_old_dentry);
2732}
2733
2734/*
Sage Weil2f2dc052009-10-06 11:31:09 -07002735 * Handle mds reply.
2736 *
2737 * We take the session mutex and parse and process the reply immediately.
2738 * This preserves the logical ordering of replies, capabilities, etc., sent
2739 * by the MDS as they are applied to our local cache.
2740 */
2741static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2742{
2743 struct ceph_mds_client *mdsc = session->s_mdsc;
2744 struct ceph_mds_request *req;
2745 struct ceph_mds_reply_head *head = msg->front.iov_base;
2746 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
Yan, Zheng982d6012014-12-23 15:30:54 +08002747 struct ceph_snap_realm *realm;
Sage Weil2f2dc052009-10-06 11:31:09 -07002748 u64 tid;
2749 int err, result;
Sage Weil2600d2d2010-02-22 15:12:16 -08002750 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07002751
Sage Weil2f2dc052009-10-06 11:31:09 -07002752 if (msg->front.iov_len < sizeof(*head)) {
2753 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002754 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002755 return;
2756 }
2757
2758 /* get request, session */
Sage Weil6df058c2009-12-22 11:24:33 -08002759 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002760 mutex_lock(&mdsc->mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02002761 req = lookup_get_request(mdsc, tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002762 if (!req) {
2763 dout("handle_reply on unknown tid %llu\n", tid);
2764 mutex_unlock(&mdsc->mutex);
2765 return;
2766 }
2767 dout("handle_reply %p\n", req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002768
2769 /* correct session? */
Sage Weild96d6042010-03-20 20:50:58 -07002770 if (req->r_session != session) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002771 pr_err("mdsc_handle_reply got %llu on session mds%d"
2772 " not mds%d\n", tid, session->s_mds,
2773 req->r_session ? req->r_session->s_mds : -1);
2774 mutex_unlock(&mdsc->mutex);
2775 goto out;
2776 }
2777
2778 /* dup? */
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002779 if ((test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags) && !head->safe) ||
2780 (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags) && head->safe)) {
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07002781 pr_warn("got a dup %s reply on %llu from mds%d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002782 head->safe ? "safe" : "unsafe", tid, mds);
2783 mutex_unlock(&mdsc->mutex);
2784 goto out;
2785 }
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002786 if (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags)) {
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07002787 pr_warn("got unsafe after safe on %llu from mds%d\n",
Sage Weil85792d0d2010-05-13 09:06:02 -07002788 tid, mds);
2789 mutex_unlock(&mdsc->mutex);
2790 goto out;
2791 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002792
2793 result = le32_to_cpu(head->result);
2794
2795 /*
Greg Farnume55b71f2010-06-22 15:58:01 -07002796 * Handle an ESTALE
2797 * if we're not talking to the authority, send to them
2798 * if the authority has changed while we weren't looking,
2799 * send to new authority
2800 * Otherwise we just have to return an ESTALE
Sage Weil2f2dc052009-10-06 11:31:09 -07002801 */
2802 if (result == -ESTALE) {
Chengguang Xu4c069a52018-01-30 16:29:17 +08002803 dout("got ESTALE on request %llu\n", req->r_tid);
Yan, Zheng51da8e82014-07-14 10:48:29 +08002804 req->r_resend_mds = -1;
Yan, Zhengca18bed2013-11-22 14:21:44 +08002805 if (req->r_direct_mode != USE_AUTH_MDS) {
Chengguang Xu4c069a52018-01-30 16:29:17 +08002806 dout("not using auth, setting for that now\n");
Greg Farnume55b71f2010-06-22 15:58:01 -07002807 req->r_direct_mode = USE_AUTH_MDS;
Sage Weil2f2dc052009-10-06 11:31:09 -07002808 __do_request(mdsc, req);
2809 mutex_unlock(&mdsc->mutex);
2810 goto out;
Greg Farnume55b71f2010-06-22 15:58:01 -07002811 } else {
Yan, Zhengca18bed2013-11-22 14:21:44 +08002812 int mds = __choose_mds(mdsc, req);
2813 if (mds >= 0 && mds != req->r_session->s_mds) {
Chengguang Xu4c069a52018-01-30 16:29:17 +08002814 dout("but auth changed, so resending\n");
Greg Farnume55b71f2010-06-22 15:58:01 -07002815 __do_request(mdsc, req);
2816 mutex_unlock(&mdsc->mutex);
2817 goto out;
2818 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002819 }
Chengguang Xu4c069a52018-01-30 16:29:17 +08002820 dout("have to return ESTALE on request %llu\n", req->r_tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002821 }
2822
Greg Farnume55b71f2010-06-22 15:58:01 -07002823
Sage Weil2f2dc052009-10-06 11:31:09 -07002824 if (head->safe) {
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002825 set_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags);
Sage Weil2f2dc052009-10-06 11:31:09 -07002826 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002827
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002828 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07002829 /*
2830 * We already handled the unsafe response, now do the
2831 * cleanup. No need to examine the response; the MDS
2832 * doesn't include any result info in the safe
2833 * response. And even if it did, there is nothing
2834 * useful we could do with a revised return value.
2835 */
2836 dout("got safe reply %llu, mds%d\n", tid, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002837
2838 /* last unsafe request during umount? */
Sage Weil44ca18f2010-02-15 12:08:46 -08002839 if (mdsc->stopping && !__get_oldest_req(mdsc))
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002840 complete_all(&mdsc->safe_umount_waiters);
Sage Weil2f2dc052009-10-06 11:31:09 -07002841 mutex_unlock(&mdsc->mutex);
2842 goto out;
2843 }
Sage Weile1518c72010-05-13 11:19:06 -07002844 } else {
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002845 set_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags);
Sage Weil2f2dc052009-10-06 11:31:09 -07002846 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
Yan, Zheng4c06ace2015-10-27 17:18:00 +08002847 if (req->r_unsafe_dir) {
2848 struct ceph_inode_info *ci =
2849 ceph_inode(req->r_unsafe_dir);
2850 spin_lock(&ci->i_unsafe_lock);
2851 list_add_tail(&req->r_unsafe_dir_item,
2852 &ci->i_unsafe_dirops);
2853 spin_unlock(&ci->i_unsafe_lock);
2854 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002855 }
2856
2857 dout("handle_reply tid %lld result %d\n", tid, result);
2858 rinfo = &req->r_reply_info;
Yan, Zhengb37fe1f2019-01-09 10:10:17 +08002859 if (test_bit(CEPHFS_FEATURE_REPLY_ENCODING, &session->s_features))
2860 err = parse_reply_info(msg, rinfo, (u64)-1);
2861 else
2862 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
Sage Weil2f2dc052009-10-06 11:31:09 -07002863 mutex_unlock(&mdsc->mutex);
2864
2865 mutex_lock(&session->s_mutex);
2866 if (err < 0) {
Herb Shiu25933ab2010-12-01 14:14:38 -08002867 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002868 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07002869 goto out_err;
2870 }
2871
2872 /* snap trace */
Yan, Zheng982d6012014-12-23 15:30:54 +08002873 realm = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -07002874 if (rinfo->snapblob_len) {
2875 down_write(&mdsc->snap_rwsem);
2876 ceph_update_snap_trace(mdsc, rinfo->snapblob,
Yan, Zheng982d6012014-12-23 15:30:54 +08002877 rinfo->snapblob + rinfo->snapblob_len,
2878 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP,
2879 &realm);
Sage Weil2f2dc052009-10-06 11:31:09 -07002880 downgrade_write(&mdsc->snap_rwsem);
2881 } else {
2882 down_read(&mdsc->snap_rwsem);
2883 }
2884
2885 /* insert trace into our cache */
Sage Weilb4556392010-05-13 12:01:13 -07002886 mutex_lock(&req->r_fill_mutex);
Yan, Zheng315f2402016-03-07 10:34:50 +08002887 current->journal_info = req;
Jeff Laytonf5a03b02017-01-31 11:06:13 -05002888 err = ceph_fill_trace(mdsc->fsc->sb, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07002889 if (err == 0) {
Sam Lang6e8575f2012-12-28 09:56:46 -08002890 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
Yan, Zheng81c6aea2013-09-18 09:44:13 +08002891 req->r_op == CEPH_MDS_OP_LSSNAP))
Sage Weil2f2dc052009-10-06 11:31:09 -07002892 ceph_readdir_prepopulate(req, req->r_session);
Sage Weil2f2dc052009-10-06 11:31:09 -07002893 }
Yan, Zheng315f2402016-03-07 10:34:50 +08002894 current->journal_info = NULL;
Sage Weilb4556392010-05-13 12:01:13 -07002895 mutex_unlock(&req->r_fill_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002896
2897 up_read(&mdsc->snap_rwsem);
Yan, Zheng982d6012014-12-23 15:30:54 +08002898 if (realm)
2899 ceph_put_snap_realm(mdsc, realm);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002900
Yan, Zhengfe330322019-02-01 14:57:15 +08002901 if (err == 0) {
2902 if (req->r_target_inode &&
2903 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
2904 struct ceph_inode_info *ci =
2905 ceph_inode(req->r_target_inode);
2906 spin_lock(&ci->i_unsafe_lock);
2907 list_add_tail(&req->r_unsafe_target_item,
2908 &ci->i_unsafe_iops);
2909 spin_unlock(&ci->i_unsafe_lock);
2910 }
2911
2912 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002913 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002914out_err:
Sage Weile1518c72010-05-13 11:19:06 -07002915 mutex_lock(&mdsc->mutex);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002916 if (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
Sage Weile1518c72010-05-13 11:19:06 -07002917 if (err) {
2918 req->r_err = err;
2919 } else {
Jianpeng Ma5fdb1382015-08-18 10:30:38 +08002920 req->r_reply = ceph_msg_get(msg);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002921 set_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags);
Sage Weile1518c72010-05-13 11:19:06 -07002922 }
Sage Weil2f2dc052009-10-06 11:31:09 -07002923 } else {
Sage Weile1518c72010-05-13 11:19:06 -07002924 dout("reply arrived after request %lld was aborted\n", tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002925 }
Sage Weile1518c72010-05-13 11:19:06 -07002926 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07002927
Sage Weil2f2dc052009-10-06 11:31:09 -07002928 mutex_unlock(&session->s_mutex);
2929
2930 /* kick calling process */
2931 complete_request(mdsc, req);
2932out:
2933 ceph_mdsc_put_request(req);
2934 return;
2935}
2936
2937
2938
2939/*
2940 * handle mds notification that our request has been forwarded.
2941 */
Sage Weil2600d2d2010-02-22 15:12:16 -08002942static void handle_forward(struct ceph_mds_client *mdsc,
2943 struct ceph_mds_session *session,
2944 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07002945{
2946 struct ceph_mds_request *req;
Sage Weila1ea7872010-02-23 14:02:44 -08002947 u64 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002948 u32 next_mds;
2949 u32 fwd_seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07002950 int err = -EINVAL;
2951 void *p = msg->front.iov_base;
2952 void *end = p + msg->front.iov_len;
Sage Weil2f2dc052009-10-06 11:31:09 -07002953
Sage Weila1ea7872010-02-23 14:02:44 -08002954 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002955 next_mds = ceph_decode_32(&p);
2956 fwd_seq = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07002957
2958 mutex_lock(&mdsc->mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +02002959 req = lookup_get_request(mdsc, tid);
Sage Weil2f2dc052009-10-06 11:31:09 -07002960 if (!req) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07002961 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07002962 goto out; /* dup reply? */
2963 }
2964
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002965 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
Sage Weil2a8e5e32010-05-28 16:43:16 -07002966 dout("forward tid %llu aborted, unregistering\n", tid);
2967 __unregister_request(mdsc, req);
2968 } else if (fwd_seq <= req->r_num_fwd) {
2969 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07002970 tid, next_mds, req->r_num_fwd, fwd_seq);
2971 } else {
2972 /* resend. forward race not possible; mds would drop */
Sage Weil2a8e5e32010-05-28 16:43:16 -07002973 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2974 BUG_ON(req->r_err);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05002975 BUG_ON(test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags));
Yan, Zheng3de22be2015-02-04 14:26:22 +08002976 req->r_attempts = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07002977 req->r_num_fwd = fwd_seq;
2978 req->r_resend_mds = next_mds;
2979 put_request_session(req);
2980 __do_request(mdsc, req);
2981 }
2982 ceph_mdsc_put_request(req);
2983out:
2984 mutex_unlock(&mdsc->mutex);
2985 return;
2986
2987bad:
2988 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2989}
2990
Yan, Zheng84bf3952018-12-21 17:41:39 +08002991static int __decode_and_drop_session_metadata(void **p, void *end)
2992{
2993 /* map<string,string> */
2994 u32 n;
2995 ceph_decode_32_safe(p, end, n, bad);
2996 while (n-- > 0) {
2997 u32 len;
2998 ceph_decode_32_safe(p, end, len, bad);
2999 ceph_decode_need(p, end, len, bad);
3000 *p += len;
3001 ceph_decode_32_safe(p, end, len, bad);
3002 ceph_decode_need(p, end, len, bad);
3003 *p += len;
3004 }
3005 return 0;
3006bad:
3007 return -1;
3008}
3009
Sage Weil2f2dc052009-10-06 11:31:09 -07003010/*
3011 * handle a mds session control message
3012 */
3013static void handle_session(struct ceph_mds_session *session,
3014 struct ceph_msg *msg)
3015{
3016 struct ceph_mds_client *mdsc = session->s_mdsc;
Yan, Zheng84bf3952018-12-21 17:41:39 +08003017 int mds = session->s_mds;
3018 int msg_version = le16_to_cpu(msg->hdr.version);
3019 void *p = msg->front.iov_base;
3020 void *end = p + msg->front.iov_len;
3021 struct ceph_mds_session_head *h;
Sage Weil2f2dc052009-10-06 11:31:09 -07003022 u32 op;
3023 u64 seq;
Yan, Zheng84bf3952018-12-21 17:41:39 +08003024 unsigned long features = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003025 int wake = 0;
3026
Sage Weil2f2dc052009-10-06 11:31:09 -07003027 /* decode */
Yan, Zheng84bf3952018-12-21 17:41:39 +08003028 ceph_decode_need(&p, end, sizeof(*h), bad);
3029 h = p;
3030 p += sizeof(*h);
3031
Sage Weil2f2dc052009-10-06 11:31:09 -07003032 op = le32_to_cpu(h->op);
3033 seq = le64_to_cpu(h->seq);
3034
Yan, Zheng84bf3952018-12-21 17:41:39 +08003035 if (msg_version >= 3) {
3036 u32 len;
3037 /* version >= 2, metadata */
3038 if (__decode_and_drop_session_metadata(&p, end) < 0)
3039 goto bad;
3040 /* version >= 3, feature bits */
3041 ceph_decode_32_safe(&p, end, len, bad);
3042 ceph_decode_need(&p, end, len, bad);
3043 memcpy(&features, p, min_t(size_t, len, sizeof(features)));
3044 p += len;
3045 }
3046
Sage Weil2f2dc052009-10-06 11:31:09 -07003047 mutex_lock(&mdsc->mutex);
Yan, Zheng0a07fc82017-03-29 15:30:24 +08003048 if (op == CEPH_SESSION_CLOSE) {
3049 get_session(session);
Sage Weil2600d2d2010-02-22 15:12:16 -08003050 __unregister_session(mdsc, session);
Yan, Zheng0a07fc82017-03-29 15:30:24 +08003051 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003052 /* FIXME: this ttl calculation is generous */
3053 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
3054 mutex_unlock(&mdsc->mutex);
3055
3056 mutex_lock(&session->s_mutex);
3057
3058 dout("handle_session mds%d %s %p state %s seq %llu\n",
3059 mds, ceph_session_op_name(op), session,
John Spraya687eca2014-09-19 13:51:08 +01003060 ceph_session_state_name(session->s_state), seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003061
3062 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
3063 session->s_state = CEPH_MDS_SESSION_OPEN;
3064 pr_info("mds%d came back\n", session->s_mds);
3065 }
3066
3067 switch (op) {
3068 case CEPH_SESSION_OPEN:
Sage Weil29790f22010-03-18 14:45:05 -07003069 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
3070 pr_info("mds%d reconnect success\n", session->s_mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003071 session->s_state = CEPH_MDS_SESSION_OPEN;
Yan, Zheng84bf3952018-12-21 17:41:39 +08003072 session->s_features = features;
Sage Weil2f2dc052009-10-06 11:31:09 -07003073 renewed_caps(mdsc, session, 0);
3074 wake = 1;
3075 if (mdsc->stopping)
3076 __close_session(mdsc, session);
3077 break;
3078
3079 case CEPH_SESSION_RENEWCAPS:
3080 if (session->s_renew_seq == seq)
3081 renewed_caps(mdsc, session, 1);
3082 break;
3083
3084 case CEPH_SESSION_CLOSE:
Sage Weil29790f22010-03-18 14:45:05 -07003085 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
3086 pr_info("mds%d reconnect denied\n", session->s_mds);
Yan, Zheng1c841a92015-03-24 20:15:36 +08003087 cleanup_session_requests(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003088 remove_session_caps(session);
Yan, Zheng656e4382014-09-11 14:25:18 +08003089 wake = 2; /* for good measure */
Sage Weilf3c60c52010-08-11 14:51:23 -07003090 wake_up_all(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003091 break;
3092
3093 case CEPH_SESSION_STALE:
3094 pr_info("mds%d caps went stale, renewing\n",
3095 session->s_mds);
Alex Elderd8fb02a2012-01-12 17:48:10 -08003096 spin_lock(&session->s_gen_ttl_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07003097 session->s_cap_gen++;
Alex Elder1ce208a2012-01-12 17:48:11 -08003098 session->s_cap_ttl = jiffies - 1;
Alex Elderd8fb02a2012-01-12 17:48:10 -08003099 spin_unlock(&session->s_gen_ttl_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07003100 send_renew_caps(mdsc, session);
3101 break;
3102
3103 case CEPH_SESSION_RECALL_STATE:
Zhi Zhange30ee582018-01-24 21:24:33 +08003104 ceph_trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
Sage Weil2f2dc052009-10-06 11:31:09 -07003105 break;
3106
Yan, Zheng186e4f72013-11-22 14:48:37 +08003107 case CEPH_SESSION_FLUSHMSG:
3108 send_flushmsg_ack(mdsc, session, seq);
3109 break;
3110
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08003111 case CEPH_SESSION_FORCE_RO:
3112 dout("force_session_readonly %p\n", session);
3113 spin_lock(&session->s_cap_lock);
3114 session->s_readonly = true;
3115 spin_unlock(&session->s_cap_lock);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08003116 wake_up_session_caps(session, FORCE_RO);
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08003117 break;
3118
Yan, Zhengfcff4152016-09-14 16:39:51 +08003119 case CEPH_SESSION_REJECT:
3120 WARN_ON(session->s_state != CEPH_MDS_SESSION_OPENING);
3121 pr_info("mds%d rejected session\n", session->s_mds);
3122 session->s_state = CEPH_MDS_SESSION_REJECTED;
3123 cleanup_session_requests(mdsc, session);
3124 remove_session_caps(session);
3125 wake = 2; /* for good measure */
3126 break;
3127
Sage Weil2f2dc052009-10-06 11:31:09 -07003128 default:
3129 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
3130 WARN_ON(1);
3131 }
3132
3133 mutex_unlock(&session->s_mutex);
3134 if (wake) {
3135 mutex_lock(&mdsc->mutex);
3136 __wake_requests(mdsc, &session->s_waiting);
Yan, Zheng656e4382014-09-11 14:25:18 +08003137 if (wake == 2)
3138 kick_requests(mdsc, mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003139 mutex_unlock(&mdsc->mutex);
3140 }
Yan, Zheng0a07fc82017-03-29 15:30:24 +08003141 if (op == CEPH_SESSION_CLOSE)
3142 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07003143 return;
3144
3145bad:
3146 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
3147 (int)msg->front.iov_len);
Sage Weil9ec7cab2009-12-14 15:13:47 -08003148 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003149 return;
3150}
3151
3152
3153/*
3154 * called under session->mutex.
3155 */
3156static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
3157 struct ceph_mds_session *session)
3158{
3159 struct ceph_mds_request *req, *nreq;
Yan, Zheng3de22be2015-02-04 14:26:22 +08003160 struct rb_node *p;
Sage Weil2f2dc052009-10-06 11:31:09 -07003161 int err;
3162
3163 dout("replay_unsafe_requests mds%d\n", session->s_mds);
3164
3165 mutex_lock(&mdsc->mutex);
3166 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
Yan, Zheng6e6f0922015-02-27 08:54:08 +08003167 err = __prepare_send_request(mdsc, req, session->s_mds, true);
Sage Weil2f2dc052009-10-06 11:31:09 -07003168 if (!err) {
3169 ceph_msg_get(req->r_request);
3170 ceph_con_send(&session->s_con, req->r_request);
3171 }
3172 }
Yan, Zheng3de22be2015-02-04 14:26:22 +08003173
3174 /*
3175 * also re-send old requests when MDS enters reconnect stage. So that MDS
3176 * can process completed request in clientreplay stage.
3177 */
3178 p = rb_first(&mdsc->request_tree);
3179 while (p) {
3180 req = rb_entry(p, struct ceph_mds_request, r_node);
3181 p = rb_next(p);
Jeff Laytonbc2de10d2017-02-01 13:49:09 -05003182 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
Yan, Zheng3de22be2015-02-04 14:26:22 +08003183 continue;
3184 if (req->r_attempts == 0)
3185 continue; /* only old requests */
3186 if (req->r_session &&
3187 req->r_session->s_mds == session->s_mds) {
Yan, Zheng6e6f0922015-02-27 08:54:08 +08003188 err = __prepare_send_request(mdsc, req,
3189 session->s_mds, true);
Yan, Zheng3de22be2015-02-04 14:26:22 +08003190 if (!err) {
3191 ceph_msg_get(req->r_request);
3192 ceph_con_send(&session->s_con, req->r_request);
3193 }
3194 }
3195 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003196 mutex_unlock(&mdsc->mutex);
3197}
3198
Yan, Zheng81c5a142019-01-01 16:28:33 +08003199static int send_reconnect_partial(struct ceph_reconnect_state *recon_state)
3200{
3201 struct ceph_msg *reply;
3202 struct ceph_pagelist *_pagelist;
3203 struct page *page;
3204 __le32 *addr;
3205 int err = -ENOMEM;
3206
3207 if (!recon_state->allow_multi)
3208 return -ENOSPC;
3209
3210 /* can't handle message that contains both caps and realm */
3211 BUG_ON(!recon_state->nr_caps == !recon_state->nr_realms);
3212
3213 /* pre-allocate new pagelist */
3214 _pagelist = ceph_pagelist_alloc(GFP_NOFS);
3215 if (!_pagelist)
3216 return -ENOMEM;
3217
3218 reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false);
3219 if (!reply)
3220 goto fail_msg;
3221
3222 /* placeholder for nr_caps */
3223 err = ceph_pagelist_encode_32(_pagelist, 0);
3224 if (err < 0)
3225 goto fail;
3226
3227 if (recon_state->nr_caps) {
3228 /* currently encoding caps */
3229 err = ceph_pagelist_encode_32(recon_state->pagelist, 0);
3230 if (err)
3231 goto fail;
3232 } else {
3233 /* placeholder for nr_realms (currently encoding relams) */
3234 err = ceph_pagelist_encode_32(_pagelist, 0);
3235 if (err < 0)
3236 goto fail;
3237 }
3238
3239 err = ceph_pagelist_encode_8(recon_state->pagelist, 1);
3240 if (err)
3241 goto fail;
3242
3243 page = list_first_entry(&recon_state->pagelist->head, struct page, lru);
3244 addr = kmap_atomic(page);
3245 if (recon_state->nr_caps) {
3246 /* currently encoding caps */
3247 *addr = cpu_to_le32(recon_state->nr_caps);
3248 } else {
3249 /* currently encoding relams */
3250 *(addr + 1) = cpu_to_le32(recon_state->nr_realms);
3251 }
3252 kunmap_atomic(addr);
3253
3254 reply->hdr.version = cpu_to_le16(5);
3255 reply->hdr.compat_version = cpu_to_le16(4);
3256
3257 reply->hdr.data_len = cpu_to_le32(recon_state->pagelist->length);
3258 ceph_msg_data_add_pagelist(reply, recon_state->pagelist);
3259
3260 ceph_con_send(&recon_state->session->s_con, reply);
3261 ceph_pagelist_release(recon_state->pagelist);
3262
3263 recon_state->pagelist = _pagelist;
3264 recon_state->nr_caps = 0;
3265 recon_state->nr_realms = 0;
3266 recon_state->msg_version = 5;
3267 return 0;
3268fail:
3269 ceph_msg_put(reply);
3270fail_msg:
3271 ceph_pagelist_release(_pagelist);
3272 return err;
3273}
3274
Sage Weil2f2dc052009-10-06 11:31:09 -07003275/*
3276 * Encode information about a cap for a reconnect with the MDS.
3277 */
Sage Weil2f2dc052009-10-06 11:31:09 -07003278static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
3279 void *arg)
3280{
Sage Weil20cb34a2010-05-12 15:21:32 -07003281 union {
3282 struct ceph_mds_cap_reconnect v2;
3283 struct ceph_mds_cap_reconnect_v1 v1;
3284 } rec;
Yan, Zhengb3f8d682017-09-11 10:58:55 +08003285 struct ceph_inode_info *ci = cap->ci;
Sage Weil20cb34a2010-05-12 15:21:32 -07003286 struct ceph_reconnect_state *recon_state = arg;
3287 struct ceph_pagelist *pagelist = recon_state->pagelist;
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003288 int err;
Yan, Zheng3469ed02016-07-05 09:32:31 +08003289 u64 snap_follows;
Sage Weil2f2dc052009-10-06 11:31:09 -07003290
Sage Weil2f2dc052009-10-06 11:31:09 -07003291 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
3292 inode, ceph_vinop(inode), cap, cap->cap_id,
3293 ceph_cap_string(cap->issued));
Sage Weil2f2dc052009-10-06 11:31:09 -07003294
Sage Weilbe655592011-11-30 09:47:09 -08003295 spin_lock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07003296 cap->seq = 0; /* reset cap seq */
3297 cap->issue_seq = 0; /* and issue_seq */
Yan, Zheng667ca052013-05-31 16:25:36 +08003298 cap->mseq = 0; /* and migrate_seq */
Yan, Zheng99a9c272013-09-22 11:08:14 +08003299 cap->cap_gen = cap->session->s_cap_gen;
Sage Weil20cb34a2010-05-12 15:21:32 -07003300
Yan, Zheng121f22a2016-07-04 22:05:18 +08003301 if (recon_state->msg_version >= 2) {
Sage Weil20cb34a2010-05-12 15:21:32 -07003302 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
3303 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
3304 rec.v2.issued = cpu_to_le32(cap->issued);
3305 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003306 rec.v2.pathbase = 0;
Jeff Laytonec1dff22017-10-31 15:51:14 -04003307 rec.v2.flock_len = (__force __le32)
3308 ((ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) ? 0 : 1);
Sage Weil20cb34a2010-05-12 15:21:32 -07003309 } else {
3310 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
3311 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
3312 rec.v1.issued = cpu_to_le32(cap->issued);
3313 rec.v1.size = cpu_to_le64(inode->i_size);
Arnd Bergmann9bbeab42018-07-13 22:18:36 +02003314 ceph_encode_timespec64(&rec.v1.mtime, &inode->i_mtime);
3315 ceph_encode_timespec64(&rec.v1.atime, &inode->i_atime);
Sage Weil20cb34a2010-05-12 15:21:32 -07003316 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003317 rec.v1.pathbase = 0;
Sage Weil20cb34a2010-05-12 15:21:32 -07003318 }
Yan, Zheng3469ed02016-07-05 09:32:31 +08003319
3320 if (list_empty(&ci->i_cap_snaps)) {
Yan, Zheng92776fd2017-08-16 21:42:39 +08003321 snap_follows = ci->i_head_snapc ? ci->i_head_snapc->seq : 0;
Yan, Zheng3469ed02016-07-05 09:32:31 +08003322 } else {
3323 struct ceph_cap_snap *capsnap =
3324 list_first_entry(&ci->i_cap_snaps,
3325 struct ceph_cap_snap, ci_item);
3326 snap_follows = capsnap->follows;
Sage Weil93cea5b2009-12-23 12:21:51 -08003327 }
Sage Weilbe655592011-11-30 09:47:09 -08003328 spin_unlock(&ci->i_ceph_lock);
Sage Weil2f2dc052009-10-06 11:31:09 -07003329
Yan, Zheng121f22a2016-07-04 22:05:18 +08003330 if (recon_state->msg_version >= 2) {
Greg Farnum40819f62010-08-02 15:34:23 -07003331 int num_fcntl_locks, num_flock_locks;
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003332 struct ceph_filelock *flocks = NULL;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003333 size_t struct_len, total_len = sizeof(u64);
Yan, Zheng121f22a2016-07-04 22:05:18 +08003334 u8 struct_v = 0;
Greg Farnum40819f62010-08-02 15:34:23 -07003335
Jim Schutt39be95e2013-05-15 13:03:35 -05003336encode_again:
Yan, Zhengb3f8d682017-09-11 10:58:55 +08003337 if (rec.v2.flock_len) {
3338 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
3339 } else {
3340 num_fcntl_locks = 0;
3341 num_flock_locks = 0;
3342 }
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003343 if (num_fcntl_locks + num_flock_locks > 0) {
Kees Cook6da2ec52018-06-12 13:55:00 -07003344 flocks = kmalloc_array(num_fcntl_locks + num_flock_locks,
3345 sizeof(struct ceph_filelock),
3346 GFP_NOFS);
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003347 if (!flocks) {
3348 err = -ENOMEM;
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003349 goto out_err;
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003350 }
3351 err = ceph_encode_locks_to_buffer(inode, flocks,
3352 num_fcntl_locks,
3353 num_flock_locks);
3354 if (err) {
3355 kfree(flocks);
3356 flocks = NULL;
3357 if (err == -ENOSPC)
3358 goto encode_again;
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003359 goto out_err;
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003360 }
3361 } else {
Jim Schutt39be95e2013-05-15 13:03:35 -05003362 kfree(flocks);
Yan, Zheng4deb14a2017-09-11 10:36:28 +08003363 flocks = NULL;
Jim Schutt39be95e2013-05-15 13:03:35 -05003364 }
Yan, Zheng121f22a2016-07-04 22:05:18 +08003365
3366 if (recon_state->msg_version >= 3) {
3367 /* version, compat_version and struct_len */
Yan, Zheng81c5a142019-01-01 16:28:33 +08003368 total_len += 2 * sizeof(u8) + sizeof(u32);
Yan, Zheng3469ed02016-07-05 09:32:31 +08003369 struct_v = 2;
Yan, Zheng121f22a2016-07-04 22:05:18 +08003370 }
Jim Schutt39be95e2013-05-15 13:03:35 -05003371 /*
3372 * number of encoded locks is stable, so copy to pagelist
3373 */
Yan, Zheng121f22a2016-07-04 22:05:18 +08003374 struct_len = 2 * sizeof(u32) +
3375 (num_fcntl_locks + num_flock_locks) *
3376 sizeof(struct ceph_filelock);
3377 rec.v2.flock_len = cpu_to_le32(struct_len);
3378
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003379 struct_len += sizeof(u32) + sizeof(rec.v2);
Yan, Zheng121f22a2016-07-04 22:05:18 +08003380
Yan, Zheng3469ed02016-07-05 09:32:31 +08003381 if (struct_v >= 2)
3382 struct_len += sizeof(u64); /* snap_follows */
3383
Yan, Zheng121f22a2016-07-04 22:05:18 +08003384 total_len += struct_len;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003385
3386 if (pagelist->length + total_len > RECONNECT_MAX_SIZE) {
3387 err = send_reconnect_partial(recon_state);
3388 if (err)
3389 goto out_freeflocks;
3390 pagelist = recon_state->pagelist;
Yan, Zheng121f22a2016-07-04 22:05:18 +08003391 }
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003392
Yan, Zheng81c5a142019-01-01 16:28:33 +08003393 err = ceph_pagelist_reserve(pagelist, total_len);
3394 if (err)
3395 goto out_freeflocks;
3396
3397 ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003398 if (recon_state->msg_version >= 3) {
3399 ceph_pagelist_encode_8(pagelist, struct_v);
3400 ceph_pagelist_encode_8(pagelist, 1);
3401 ceph_pagelist_encode_32(pagelist, struct_len);
3402 }
3403 ceph_pagelist_encode_string(pagelist, NULL, 0);
3404 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v2));
3405 ceph_locks_to_pagelist(flocks, pagelist,
3406 num_fcntl_locks, num_flock_locks);
3407 if (struct_v >= 2)
3408 ceph_pagelist_encode_64(pagelist, snap_follows);
Yan, Zheng81c5a142019-01-01 16:28:33 +08003409out_freeflocks:
Jim Schutt39be95e2013-05-15 13:03:35 -05003410 kfree(flocks);
Sage Weil3612abb2010-09-07 15:59:27 -07003411 } else {
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003412 u64 pathbase = 0;
3413 int pathlen = 0;
3414 char *path = NULL;
3415 struct dentry *dentry;
3416
3417 dentry = d_find_alias(inode);
3418 if (dentry) {
3419 path = ceph_mdsc_build_path(dentry,
3420 &pathlen, &pathbase, 0);
3421 dput(dentry);
3422 if (IS_ERR(path)) {
3423 err = PTR_ERR(path);
3424 goto out_err;
3425 }
3426 rec.v1.pathbase = cpu_to_le64(pathbase);
Yan, Zheng121f22a2016-07-04 22:05:18 +08003427 }
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003428
3429 err = ceph_pagelist_reserve(pagelist,
Yan, Zheng81c5a142019-01-01 16:28:33 +08003430 sizeof(u64) + sizeof(u32) +
3431 pathlen + sizeof(rec.v1));
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003432 if (err) {
Yan, Zheng81c5a142019-01-01 16:28:33 +08003433 goto out_freepath;
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003434 }
3435
Yan, Zheng81c5a142019-01-01 16:28:33 +08003436 ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003437 ceph_pagelist_encode_string(pagelist, path, pathlen);
3438 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v1));
Yan, Zheng81c5a142019-01-01 16:28:33 +08003439out_freepath:
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003440 kfree(path);
Greg Farnum40819f62010-08-02 15:34:23 -07003441 }
Yan, Zheng44c99752013-09-22 10:28:10 +08003442
Yan, Zheng5ccedf12018-12-13 16:34:11 +08003443out_err:
Yan, Zheng81c5a142019-01-01 16:28:33 +08003444 if (err >= 0)
3445 recon_state->nr_caps++;
3446 return err;
3447}
3448
3449static int encode_snap_realms(struct ceph_mds_client *mdsc,
3450 struct ceph_reconnect_state *recon_state)
3451{
3452 struct rb_node *p;
3453 struct ceph_pagelist *pagelist = recon_state->pagelist;
3454 int err = 0;
3455
3456 if (recon_state->msg_version >= 4) {
3457 err = ceph_pagelist_encode_32(pagelist, mdsc->num_snap_realms);
3458 if (err < 0)
3459 goto fail;
3460 }
3461
3462 /*
3463 * snaprealms. we provide mds with the ino, seq (version), and
3464 * parent for all of our realms. If the mds has any newer info,
3465 * it will tell us.
3466 */
3467 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
3468 struct ceph_snap_realm *realm =
3469 rb_entry(p, struct ceph_snap_realm, node);
3470 struct ceph_mds_snaprealm_reconnect sr_rec;
3471
3472 if (recon_state->msg_version >= 4) {
3473 size_t need = sizeof(u8) * 2 + sizeof(u32) +
3474 sizeof(sr_rec);
3475
3476 if (pagelist->length + need > RECONNECT_MAX_SIZE) {
3477 err = send_reconnect_partial(recon_state);
3478 if (err)
3479 goto fail;
3480 pagelist = recon_state->pagelist;
3481 }
3482
3483 err = ceph_pagelist_reserve(pagelist, need);
3484 if (err)
3485 goto fail;
3486
3487 ceph_pagelist_encode_8(pagelist, 1);
3488 ceph_pagelist_encode_8(pagelist, 1);
3489 ceph_pagelist_encode_32(pagelist, sizeof(sr_rec));
3490 }
3491
3492 dout(" adding snap realm %llx seq %lld parent %llx\n",
3493 realm->ino, realm->seq, realm->parent_ino);
3494 sr_rec.ino = cpu_to_le64(realm->ino);
3495 sr_rec.seq = cpu_to_le64(realm->seq);
3496 sr_rec.parent = cpu_to_le64(realm->parent_ino);
3497
3498 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
3499 if (err)
3500 goto fail;
3501
3502 recon_state->nr_realms++;
3503 }
3504fail:
Sage Weil93cea5b2009-12-23 12:21:51 -08003505 return err;
Sage Weil2f2dc052009-10-06 11:31:09 -07003506}
3507
3508
3509/*
3510 * If an MDS fails and recovers, clients need to reconnect in order to
3511 * reestablish shared state. This includes all caps issued through
3512 * this session _and_ the snap_realm hierarchy. Because it's not
3513 * clear which snap realms the mds cares about, we send everything we
3514 * know about.. that ensures we'll then get any new info the
3515 * recovering MDS might have.
3516 *
3517 * This is a relatively heavyweight operation, but it's rare.
3518 *
3519 * called with mdsc->mutex held.
3520 */
Sage Weil34b6c852010-05-10 16:31:25 -07003521static void send_mds_reconnect(struct ceph_mds_client *mdsc,
3522 struct ceph_mds_session *session)
Sage Weil2f2dc052009-10-06 11:31:09 -07003523{
Sage Weil2f2dc052009-10-06 11:31:09 -07003524 struct ceph_msg *reply;
Sage Weil34b6c852010-05-10 16:31:25 -07003525 int mds = session->s_mds;
Sage Weil9abf82b2010-05-10 21:58:38 -07003526 int err = -ENOMEM;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003527 struct ceph_reconnect_state recon_state = {
3528 .session = session,
3529 };
Jeff Laytonc8a96a32017-10-19 08:53:58 -04003530 LIST_HEAD(dispose);
Sage Weil2f2dc052009-10-06 11:31:09 -07003531
Sage Weil34b6c852010-05-10 16:31:25 -07003532 pr_info("mds%d reconnect start\n", mds);
Sage Weil2f2dc052009-10-06 11:31:09 -07003533
Yan, Zheng81c5a142019-01-01 16:28:33 +08003534 recon_state.pagelist = ceph_pagelist_alloc(GFP_NOFS);
3535 if (!recon_state.pagelist)
Sage Weil93cea5b2009-12-23 12:21:51 -08003536 goto fail_nopagelist;
Sage Weil93cea5b2009-12-23 12:21:51 -08003537
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003538 reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07003539 if (!reply)
Sage Weil93cea5b2009-12-23 12:21:51 -08003540 goto fail_nomsg;
Sage Weil93cea5b2009-12-23 12:21:51 -08003541
Sage Weil34b6c852010-05-10 16:31:25 -07003542 mutex_lock(&session->s_mutex);
3543 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
3544 session->s_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07003545
Sage Weil2f2dc052009-10-06 11:31:09 -07003546 dout("session %p state %s\n", session,
John Spraya687eca2014-09-19 13:51:08 +01003547 ceph_session_state_name(session->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07003548
Yan, Zheng99a9c272013-09-22 11:08:14 +08003549 spin_lock(&session->s_gen_ttl_lock);
3550 session->s_cap_gen++;
3551 spin_unlock(&session->s_gen_ttl_lock);
3552
3553 spin_lock(&session->s_cap_lock);
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08003554 /* don't know if session is readonly */
3555 session->s_readonly = 0;
Yan, Zheng99a9c272013-09-22 11:08:14 +08003556 /*
3557 * notify __ceph_remove_cap() that we are composing cap reconnect.
3558 * If a cap get released before being added to the cap reconnect,
3559 * __ceph_remove_cap() should skip queuing cap release.
3560 */
3561 session->s_cap_reconnect = 1;
Sage Weile01a5942010-05-10 15:36:44 -07003562 /* drop old cap expires; we're about to reestablish that state */
Jeff Laytonc8a96a32017-10-19 08:53:58 -04003563 detach_cap_releases(session, &dispose);
3564 spin_unlock(&session->s_cap_lock);
3565 dispose_cap_releases(mdsc, &dispose);
Sage Weile01a5942010-05-10 15:36:44 -07003566
Yan, Zheng5d233712014-09-10 16:56:23 +08003567 /* trim unused caps to reduce MDS's cache rejoin time */
Yan, Zhengc0bd50e2015-04-07 15:51:08 +08003568 if (mdsc->fsc->sb->s_root)
3569 shrink_dcache_parent(mdsc->fsc->sb->s_root);
Yan, Zheng5d233712014-09-10 16:56:23 +08003570
3571 ceph_con_close(&session->s_con);
3572 ceph_con_open(&session->s_con,
3573 CEPH_ENTITY_TYPE_MDS, mds,
3574 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
3575
3576 /* replay unsafe requests */
3577 replay_unsafe_requests(mdsc, session);
3578
Yan, Zheng81c5a142019-01-01 16:28:33 +08003579 ceph_early_kick_flushing_caps(mdsc, session);
3580
Yan, Zheng5d233712014-09-10 16:56:23 +08003581 down_read(&mdsc->snap_rwsem);
3582
Yan, Zheng81c5a142019-01-01 16:28:33 +08003583 /* placeholder for nr_caps */
3584 err = ceph_pagelist_encode_32(recon_state.pagelist, 0);
Sage Weil93cea5b2009-12-23 12:21:51 -08003585 if (err)
3586 goto fail;
Sage Weil20cb34a2010-05-12 15:21:32 -07003587
Yan, Zheng81c5a142019-01-01 16:28:33 +08003588 if (test_bit(CEPHFS_FEATURE_MULTI_RECONNECT, &session->s_features)) {
Yan, Zheng121f22a2016-07-04 22:05:18 +08003589 recon_state.msg_version = 3;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003590 recon_state.allow_multi = true;
3591 } else if (session->s_con.peer_features & CEPH_FEATURE_MDSENC) {
3592 recon_state.msg_version = 3;
3593 } else {
Ilya Dryomov23c625c2018-11-08 14:55:21 +01003594 recon_state.msg_version = 2;
Yan, Zheng81c5a142019-01-01 16:28:33 +08003595 }
3596 /* trsaverse this session's caps */
Sage Weil20cb34a2010-05-12 15:21:32 -07003597 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
Sage Weil2f2dc052009-10-06 11:31:09 -07003598
Yan, Zheng99a9c272013-09-22 11:08:14 +08003599 spin_lock(&session->s_cap_lock);
3600 session->s_cap_reconnect = 0;
3601 spin_unlock(&session->s_cap_lock);
3602
Yan, Zheng81c5a142019-01-01 16:28:33 +08003603 if (err < 0)
3604 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07003605
Yan, Zheng81c5a142019-01-01 16:28:33 +08003606 /* check if all realms can be encoded into current message */
3607 if (mdsc->num_snap_realms) {
3608 size_t total_len =
3609 recon_state.pagelist->length +
3610 mdsc->num_snap_realms *
3611 sizeof(struct ceph_mds_snaprealm_reconnect);
3612 if (recon_state.msg_version >= 4) {
3613 /* number of realms */
3614 total_len += sizeof(u32);
3615 /* version, compat_version and struct_len */
3616 total_len += mdsc->num_snap_realms *
3617 (2 * sizeof(u8) + sizeof(u32));
3618 }
3619 if (total_len > RECONNECT_MAX_SIZE) {
3620 if (!recon_state.allow_multi) {
3621 err = -ENOSPC;
3622 goto fail;
3623 }
3624 if (recon_state.nr_caps) {
3625 err = send_reconnect_partial(&recon_state);
3626 if (err)
3627 goto fail;
3628 }
3629 recon_state.msg_version = 5;
3630 }
3631 }
3632
3633 err = encode_snap_realms(mdsc, &recon_state);
3634 if (err < 0)
3635 goto fail;
3636
3637 if (recon_state.msg_version >= 5) {
3638 err = ceph_pagelist_encode_8(recon_state.pagelist, 0);
3639 if (err < 0)
Sage Weil93cea5b2009-12-23 12:21:51 -08003640 goto fail;
Sage Weil2f2dc052009-10-06 11:31:09 -07003641 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003642
Yan, Zheng81c5a142019-01-01 16:28:33 +08003643 if (recon_state.nr_caps || recon_state.nr_realms) {
3644 struct page *page =
3645 list_first_entry(&recon_state.pagelist->head,
3646 struct page, lru);
Yan, Zheng44c99752013-09-22 10:28:10 +08003647 __le32 *addr = kmap_atomic(page);
Yan, Zheng81c5a142019-01-01 16:28:33 +08003648 if (recon_state.nr_caps) {
3649 WARN_ON(recon_state.nr_realms != mdsc->num_snap_realms);
3650 *addr = cpu_to_le32(recon_state.nr_caps);
3651 } else if (recon_state.msg_version >= 4) {
3652 *(addr + 1) = cpu_to_le32(recon_state.nr_realms);
3653 }
Yan, Zheng44c99752013-09-22 10:28:10 +08003654 kunmap_atomic(addr);
Alex Elderebf18f42013-03-04 22:29:57 -06003655 }
Yan, Zheng44c99752013-09-22 10:28:10 +08003656
Yan, Zheng81c5a142019-01-01 16:28:33 +08003657 reply->hdr.version = cpu_to_le16(recon_state.msg_version);
3658 if (recon_state.msg_version >= 4)
3659 reply->hdr.compat_version = cpu_to_le16(4);
Yan, Zhenge548e9b2015-06-10 15:17:56 +08003660
Yan, Zheng81c5a142019-01-01 16:28:33 +08003661 reply->hdr.data_len = cpu_to_le32(recon_state.pagelist->length);
3662 ceph_msg_data_add_pagelist(reply, recon_state.pagelist);
Yan, Zhenge548e9b2015-06-10 15:17:56 +08003663
Sage Weil2f2dc052009-10-06 11:31:09 -07003664 ceph_con_send(&session->s_con, reply);
3665
Sage Weil9abf82b2010-05-10 21:58:38 -07003666 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003667
Sage Weil9abf82b2010-05-10 21:58:38 -07003668 mutex_lock(&mdsc->mutex);
3669 __wake_requests(mdsc, &session->s_waiting);
3670 mutex_unlock(&mdsc->mutex);
3671
Sage Weil2f2dc052009-10-06 11:31:09 -07003672 up_read(&mdsc->snap_rwsem);
Yan, Zheng81c5a142019-01-01 16:28:33 +08003673 ceph_pagelist_release(recon_state.pagelist);
Sage Weil2f2dc052009-10-06 11:31:09 -07003674 return;
3675
Sage Weil93cea5b2009-12-23 12:21:51 -08003676fail:
Sage Weil2f2dc052009-10-06 11:31:09 -07003677 ceph_msg_put(reply);
Sage Weil9abf82b2010-05-10 21:58:38 -07003678 up_read(&mdsc->snap_rwsem);
3679 mutex_unlock(&session->s_mutex);
Sage Weil93cea5b2009-12-23 12:21:51 -08003680fail_nomsg:
Yan, Zheng81c5a142019-01-01 16:28:33 +08003681 ceph_pagelist_release(recon_state.pagelist);
Sage Weil93cea5b2009-12-23 12:21:51 -08003682fail_nopagelist:
Sage Weil9abf82b2010-05-10 21:58:38 -07003683 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
Sage Weil9abf82b2010-05-10 21:58:38 -07003684 return;
Sage Weil2f2dc052009-10-06 11:31:09 -07003685}
3686
3687
3688/*
3689 * compare old and new mdsmaps, kicking requests
3690 * and closing out old connections as necessary
3691 *
3692 * called under mdsc->mutex.
3693 */
3694static void check_new_map(struct ceph_mds_client *mdsc,
3695 struct ceph_mdsmap *newmap,
3696 struct ceph_mdsmap *oldmap)
3697{
3698 int i;
3699 int oldstate, newstate;
3700 struct ceph_mds_session *s;
3701
3702 dout("check_new_map new %u old %u\n",
3703 newmap->m_epoch, oldmap->m_epoch);
3704
Yan, Zheng76201b62017-03-28 17:04:13 +08003705 for (i = 0; i < oldmap->m_num_mds && i < mdsc->max_sessions; i++) {
Markus Elfringd37b1d92017-08-20 20:22:02 +02003706 if (!mdsc->sessions[i])
Sage Weil2f2dc052009-10-06 11:31:09 -07003707 continue;
3708 s = mdsc->sessions[i];
3709 oldstate = ceph_mdsmap_get_state(oldmap, i);
3710 newstate = ceph_mdsmap_get_state(newmap, i);
3711
Sage Weil0deb01c2010-06-17 14:19:01 -07003712 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
Sage Weil2f2dc052009-10-06 11:31:09 -07003713 i, ceph_mds_state_name(oldstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07003714 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
Sage Weil2f2dc052009-10-06 11:31:09 -07003715 ceph_mds_state_name(newstate),
Sage Weil0deb01c2010-06-17 14:19:01 -07003716 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
John Spraya687eca2014-09-19 13:51:08 +01003717 ceph_session_state_name(s->s_state));
Sage Weil2f2dc052009-10-06 11:31:09 -07003718
Yan, Zheng76201b62017-03-28 17:04:13 +08003719 if (i >= newmap->m_num_mds ||
Yan, Zheng3e8f43a2012-09-20 17:42:25 +08003720 memcmp(ceph_mdsmap_get_addr(oldmap, i),
Sage Weil2f2dc052009-10-06 11:31:09 -07003721 ceph_mdsmap_get_addr(newmap, i),
3722 sizeof(struct ceph_entity_addr))) {
3723 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
3724 /* the session never opened, just close it
3725 * out now */
Yan, Zheng0a07fc82017-03-29 15:30:24 +08003726 get_session(s);
Sage Weil2600d2d2010-02-22 15:12:16 -08003727 __unregister_session(mdsc, s);
Yan, Zheng0a07fc82017-03-29 15:30:24 +08003728 __wake_requests(mdsc, &s->s_waiting);
3729 ceph_put_mds_session(s);
Yan, Zheng28275282017-03-28 17:56:29 +08003730 } else if (i >= newmap->m_num_mds) {
3731 /* force close session for stopped mds */
3732 get_session(s);
3733 __unregister_session(mdsc, s);
3734 __wake_requests(mdsc, &s->s_waiting);
3735 kick_requests(mdsc, i);
3736 mutex_unlock(&mdsc->mutex);
3737
3738 mutex_lock(&s->s_mutex);
3739 cleanup_session_requests(mdsc, s);
3740 remove_session_caps(s);
3741 mutex_unlock(&s->s_mutex);
3742
3743 ceph_put_mds_session(s);
3744
3745 mutex_lock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003746 } else {
3747 /* just close it */
3748 mutex_unlock(&mdsc->mutex);
3749 mutex_lock(&s->s_mutex);
3750 mutex_lock(&mdsc->mutex);
3751 ceph_con_close(&s->s_con);
3752 mutex_unlock(&s->s_mutex);
3753 s->s_state = CEPH_MDS_SESSION_RESTARTING;
3754 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003755 } else if (oldstate == newstate) {
3756 continue; /* nothing new with this mds */
3757 }
3758
3759 /*
3760 * send reconnect?
3761 */
3762 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
Sage Weil34b6c852010-05-10 16:31:25 -07003763 newstate >= CEPH_MDS_STATE_RECONNECT) {
3764 mutex_unlock(&mdsc->mutex);
3765 send_mds_reconnect(mdsc, s);
3766 mutex_lock(&mdsc->mutex);
3767 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003768
3769 /*
Sage Weil29790f22010-03-18 14:45:05 -07003770 * kick request on any mds that has gone active.
Sage Weil2f2dc052009-10-06 11:31:09 -07003771 */
3772 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
3773 newstate >= CEPH_MDS_STATE_ACTIVE) {
Sage Weil29790f22010-03-18 14:45:05 -07003774 if (oldstate != CEPH_MDS_STATE_CREATING &&
3775 oldstate != CEPH_MDS_STATE_STARTING)
3776 pr_info("mds%d recovery completed\n", s->s_mds);
3777 kick_requests(mdsc, i);
Sage Weil2f2dc052009-10-06 11:31:09 -07003778 ceph_kick_flushing_caps(mdsc, s);
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08003779 wake_up_session_caps(s, RECONNECT);
Sage Weil2f2dc052009-10-06 11:31:09 -07003780 }
3781 }
Sage Weilcb170a22010-06-21 13:38:35 -07003782
Yan, Zheng76201b62017-03-28 17:04:13 +08003783 for (i = 0; i < newmap->m_num_mds && i < mdsc->max_sessions; i++) {
Sage Weilcb170a22010-06-21 13:38:35 -07003784 s = mdsc->sessions[i];
3785 if (!s)
3786 continue;
3787 if (!ceph_mdsmap_is_laggy(newmap, i))
3788 continue;
3789 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3790 s->s_state == CEPH_MDS_SESSION_HUNG ||
3791 s->s_state == CEPH_MDS_SESSION_CLOSING) {
3792 dout(" connecting to export targets of laggy mds%d\n",
3793 i);
3794 __open_export_target_sessions(mdsc, s);
3795 }
3796 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003797}
3798
3799
3800
3801/*
3802 * leases
3803 */
3804
3805/*
3806 * caller must hold session s_mutex, dentry->d_lock
3807 */
3808void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
3809{
3810 struct ceph_dentry_info *di = ceph_dentry(dentry);
3811
3812 ceph_put_mds_session(di->lease_session);
3813 di->lease_session = NULL;
3814}
3815
Sage Weil2600d2d2010-02-22 15:12:16 -08003816static void handle_lease(struct ceph_mds_client *mdsc,
3817 struct ceph_mds_session *session,
3818 struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07003819{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003820 struct super_block *sb = mdsc->fsc->sb;
Sage Weil2f2dc052009-10-06 11:31:09 -07003821 struct inode *inode;
Sage Weil2f2dc052009-10-06 11:31:09 -07003822 struct dentry *parent, *dentry;
3823 struct ceph_dentry_info *di;
Sage Weil2600d2d2010-02-22 15:12:16 -08003824 int mds = session->s_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -07003825 struct ceph_mds_lease *h = msg->front.iov_base;
Sage Weil1e5ea232010-06-04 10:05:40 -07003826 u32 seq;
Sage Weil2f2dc052009-10-06 11:31:09 -07003827 struct ceph_vino vino;
Sage Weil2f2dc052009-10-06 11:31:09 -07003828 struct qstr dname;
3829 int release = 0;
3830
Sage Weil2f2dc052009-10-06 11:31:09 -07003831 dout("handle_lease from mds%d\n", mds);
3832
3833 /* decode */
3834 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
3835 goto bad;
3836 vino.ino = le64_to_cpu(h->ino);
3837 vino.snap = CEPH_NOSNAP;
Sage Weil1e5ea232010-06-04 10:05:40 -07003838 seq = le32_to_cpu(h->seq);
Yan, Zheng0fcf6c02018-08-03 16:24:49 +08003839 dname.len = get_unaligned_le32(h + 1);
3840 if (msg->front.iov_len < sizeof(*h) + sizeof(u32) + dname.len)
Sage Weil2f2dc052009-10-06 11:31:09 -07003841 goto bad;
Yan, Zheng0fcf6c02018-08-03 16:24:49 +08003842 dname.name = (void *)(h + 1) + sizeof(u32);
Sage Weil2f2dc052009-10-06 11:31:09 -07003843
Sage Weil2f2dc052009-10-06 11:31:09 -07003844 /* lookup inode */
3845 inode = ceph_find_inode(sb, vino);
Sage Weil2f90b852011-07-26 11:28:25 -07003846 dout("handle_lease %s, ino %llx %p %.*s\n",
3847 ceph_lease_op_name(h->action), vino.ino, inode,
Sage Weil1e5ea232010-06-04 10:05:40 -07003848 dname.len, dname.name);
Yan, Zheng6cd3bca2014-09-17 07:45:12 +08003849
3850 mutex_lock(&session->s_mutex);
3851 session->s_seq++;
3852
Markus Elfringd37b1d92017-08-20 20:22:02 +02003853 if (!inode) {
Sage Weil2f2dc052009-10-06 11:31:09 -07003854 dout("handle_lease no inode %llx\n", vino.ino);
3855 goto release;
3856 }
Sage Weil2f2dc052009-10-06 11:31:09 -07003857
3858 /* dentry */
3859 parent = d_find_alias(inode);
3860 if (!parent) {
3861 dout("no parent dentry on inode %p\n", inode);
3862 WARN_ON(1);
3863 goto release; /* hrm... */
3864 }
Linus Torvalds8387ff22016-06-10 07:51:30 -07003865 dname.hash = full_name_hash(parent, dname.name, dname.len);
Sage Weil2f2dc052009-10-06 11:31:09 -07003866 dentry = d_lookup(parent, &dname);
3867 dput(parent);
3868 if (!dentry)
3869 goto release;
3870
3871 spin_lock(&dentry->d_lock);
3872 di = ceph_dentry(dentry);
3873 switch (h->action) {
3874 case CEPH_MDS_LEASE_REVOKE:
Sage Weil3d8eb7a2011-11-11 09:48:53 -08003875 if (di->lease_session == session) {
Sage Weil1e5ea232010-06-04 10:05:40 -07003876 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
3877 h->seq = cpu_to_le32(di->lease_seq);
Sage Weil2f2dc052009-10-06 11:31:09 -07003878 __ceph_mdsc_drop_dentry_lease(dentry);
3879 }
3880 release = 1;
3881 break;
3882
3883 case CEPH_MDS_LEASE_RENEW:
Sage Weil3d8eb7a2011-11-11 09:48:53 -08003884 if (di->lease_session == session &&
Sage Weil2f2dc052009-10-06 11:31:09 -07003885 di->lease_gen == session->s_cap_gen &&
3886 di->lease_renew_from &&
3887 di->lease_renew_after == 0) {
3888 unsigned long duration =
Nicholas Mc Guire3563dbdd2015-02-06 06:52:17 -05003889 msecs_to_jiffies(le32_to_cpu(h->duration_ms));
Sage Weil2f2dc052009-10-06 11:31:09 -07003890
Sage Weil1e5ea232010-06-04 10:05:40 -07003891 di->lease_seq = seq;
Miklos Szeredi9b16f03c2016-06-22 16:35:04 +02003892 di->time = di->lease_renew_from + duration;
Sage Weil2f2dc052009-10-06 11:31:09 -07003893 di->lease_renew_after = di->lease_renew_from +
3894 (duration >> 1);
3895 di->lease_renew_from = 0;
3896 }
3897 break;
3898 }
3899 spin_unlock(&dentry->d_lock);
3900 dput(dentry);
3901
3902 if (!release)
3903 goto out;
3904
3905release:
3906 /* let's just reuse the same message */
3907 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
3908 ceph_msg_get(msg);
3909 ceph_con_send(&session->s_con, msg);
3910
3911out:
3912 iput(inode);
3913 mutex_unlock(&session->s_mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07003914 return;
3915
3916bad:
3917 pr_err("corrupt lease message\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08003918 ceph_msg_dump(msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07003919}
3920
3921void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
3922 struct inode *inode,
3923 struct dentry *dentry, char action,
3924 u32 seq)
3925{
3926 struct ceph_msg *msg;
3927 struct ceph_mds_lease *lease;
3928 int len = sizeof(*lease) + sizeof(u32);
3929 int dnamelen = 0;
3930
3931 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
3932 inode, dentry, ceph_lease_op_name(action), session->s_mds);
3933 dnamelen = dentry->d_name.len;
3934 len += dnamelen;
3935
Sage Weilb61c2762011-08-09 15:03:46 -07003936 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07003937 if (!msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07003938 return;
3939 lease = msg->front.iov_base;
3940 lease->action = action;
Sage Weil2f2dc052009-10-06 11:31:09 -07003941 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
3942 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
3943 lease->seq = cpu_to_le32(seq);
3944 put_unaligned_le32(dnamelen, lease + 1);
3945 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
3946
3947 /*
3948 * if this is a preemptive lease RELEASE, no need to
3949 * flush request stream, since the actual request will
3950 * soon follow.
3951 */
3952 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
3953
3954 ceph_con_send(&session->s_con, msg);
3955}
3956
3957/*
Yan, Zheng7aac4532018-03-13 19:52:20 +08003958 * lock unlock sessions, to wait ongoing session activities
Sage Weil2f2dc052009-10-06 11:31:09 -07003959 */
Yan, Zheng7aac4532018-03-13 19:52:20 +08003960static void lock_unlock_sessions(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07003961{
3962 int i;
3963
Sage Weil2f2dc052009-10-06 11:31:09 -07003964 mutex_lock(&mdsc->mutex);
3965 for (i = 0; i < mdsc->max_sessions; i++) {
3966 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
3967 if (!s)
3968 continue;
3969 mutex_unlock(&mdsc->mutex);
3970 mutex_lock(&s->s_mutex);
3971 mutex_unlock(&s->s_mutex);
3972 ceph_put_mds_session(s);
3973 mutex_lock(&mdsc->mutex);
3974 }
3975 mutex_unlock(&mdsc->mutex);
3976}
3977
3978
3979
3980/*
3981 * delayed work -- periodically trim expired leases, renew caps with mds
3982 */
3983static void schedule_delayed(struct ceph_mds_client *mdsc)
3984{
3985 int delay = 5;
3986 unsigned hz = round_jiffies_relative(HZ * delay);
3987 schedule_delayed_work(&mdsc->delayed_work, hz);
3988}
3989
3990static void delayed_work(struct work_struct *work)
3991{
3992 int i;
3993 struct ceph_mds_client *mdsc =
3994 container_of(work, struct ceph_mds_client, delayed_work.work);
3995 int renew_interval;
3996 int renew_caps;
3997
3998 dout("mdsc delayed_work\n");
Yan, Zheng75c96272017-12-14 15:11:09 +08003999
Sage Weil2f2dc052009-10-06 11:31:09 -07004000 mutex_lock(&mdsc->mutex);
4001 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
4002 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
4003 mdsc->last_renew_caps);
4004 if (renew_caps)
4005 mdsc->last_renew_caps = jiffies;
4006
4007 for (i = 0; i < mdsc->max_sessions; i++) {
4008 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
Markus Elfringd37b1d92017-08-20 20:22:02 +02004009 if (!s)
Sage Weil2f2dc052009-10-06 11:31:09 -07004010 continue;
4011 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
4012 dout("resending session close request for mds%d\n",
4013 s->s_mds);
4014 request_close_session(mdsc, s);
4015 ceph_put_mds_session(s);
4016 continue;
4017 }
4018 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
4019 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
4020 s->s_state = CEPH_MDS_SESSION_HUNG;
4021 pr_info("mds%d hung\n", s->s_mds);
4022 }
4023 }
4024 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
4025 /* this mds is failed or recovering, just wait */
4026 ceph_put_mds_session(s);
4027 continue;
4028 }
4029 mutex_unlock(&mdsc->mutex);
4030
4031 mutex_lock(&s->s_mutex);
4032 if (renew_caps)
4033 send_renew_caps(mdsc, s);
4034 else
4035 ceph_con_keepalive(&s->s_con);
Sage Weilaab53dd2010-03-17 16:30:21 -07004036 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
4037 s->s_state == CEPH_MDS_SESSION_HUNG)
Sage Weil3d7ded42010-06-09 16:47:10 -07004038 ceph_send_cap_releases(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07004039 mutex_unlock(&s->s_mutex);
4040 ceph_put_mds_session(s);
4041
4042 mutex_lock(&mdsc->mutex);
4043 }
4044 mutex_unlock(&mdsc->mutex);
4045
Yan, Zheng37c4efc2019-01-31 16:55:51 +08004046 ceph_check_delayed_caps(mdsc);
4047
4048 ceph_queue_cap_reclaim_work(mdsc);
4049
4050 ceph_trim_snapid_map(mdsc);
4051
Sage Weil2f2dc052009-10-06 11:31:09 -07004052 schedule_delayed(mdsc);
4053}
4054
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004055int ceph_mdsc_init(struct ceph_fs_client *fsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07004056
Sage Weil2f2dc052009-10-06 11:31:09 -07004057{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004058 struct ceph_mds_client *mdsc;
4059
4060 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
4061 if (!mdsc)
4062 return -ENOMEM;
4063 mdsc->fsc = fsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07004064 mutex_init(&mdsc->mutex);
4065 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
Markus Elfringd37b1d92017-08-20 20:22:02 +02004066 if (!mdsc->mdsmap) {
majianpengfb3101b2013-06-25 14:48:19 +08004067 kfree(mdsc);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08004068 return -ENOMEM;
majianpengfb3101b2013-06-25 14:48:19 +08004069 }
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08004070
Chengguang Xu50c55ae2018-03-14 13:47:33 +08004071 fsc->mdsc = mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07004072 init_completion(&mdsc->safe_umount_waiters);
Sage Weilf3c60c52010-08-11 14:51:23 -07004073 init_waitqueue_head(&mdsc->session_close_wq);
Sage Weil2f2dc052009-10-06 11:31:09 -07004074 INIT_LIST_HEAD(&mdsc->waiting_for_map);
4075 mdsc->sessions = NULL;
Yan, Zheng86d8f672015-01-09 17:00:42 +08004076 atomic_set(&mdsc->num_sessions, 0);
Sage Weil2f2dc052009-10-06 11:31:09 -07004077 mdsc->max_sessions = 0;
4078 mdsc->stopping = 0;
Luis Henriquesd557c482018-01-12 17:19:29 +00004079 atomic64_set(&mdsc->quotarealms_count, 0);
Yan, Zhengaffbc192015-05-05 21:22:13 +08004080 mdsc->last_snap_seq = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07004081 init_rwsem(&mdsc->snap_rwsem);
Sage Weila105f002010-02-15 14:37:55 -08004082 mdsc->snap_realms = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07004083 INIT_LIST_HEAD(&mdsc->snap_empty);
Yan, Zheng81c5a142019-01-01 16:28:33 +08004084 mdsc->num_snap_realms = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07004085 spin_lock_init(&mdsc->snap_empty_lock);
4086 mdsc->last_tid = 0;
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08004087 mdsc->oldest_tid = 0;
Sage Weil44ca18f2010-02-15 12:08:46 -08004088 mdsc->request_tree = RB_ROOT;
Sage Weil2f2dc052009-10-06 11:31:09 -07004089 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
4090 mdsc->last_renew_caps = jiffies;
4091 INIT_LIST_HEAD(&mdsc->cap_delay_list);
4092 spin_lock_init(&mdsc->cap_delay_lock);
4093 INIT_LIST_HEAD(&mdsc->snap_flush_list);
4094 spin_lock_init(&mdsc->snap_flush_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08004095 mdsc->last_cap_flush_tid = 1;
Yan, Zhenge4500b52016-07-06 11:12:56 +08004096 INIT_LIST_HEAD(&mdsc->cap_flush_list);
Sage Weil2f2dc052009-10-06 11:31:09 -07004097 INIT_LIST_HEAD(&mdsc->cap_dirty);
Sage Weildb354052011-05-24 11:46:31 -07004098 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
Sage Weil2f2dc052009-10-06 11:31:09 -07004099 mdsc->num_cap_flushing = 0;
4100 spin_lock_init(&mdsc->cap_dirty_lock);
4101 init_waitqueue_head(&mdsc->cap_flushing_wq);
Yan, Zheng37c4efc2019-01-31 16:55:51 +08004102 INIT_WORK(&mdsc->cap_reclaim_work, ceph_cap_reclaim_work);
Yan, Zhengfe330322019-02-01 14:57:15 +08004103 atomic_set(&mdsc->cap_reclaim_pending, 0);
Yan, Zheng37c4efc2019-01-31 16:55:51 +08004104
4105 spin_lock_init(&mdsc->dentry_list_lock);
4106 INIT_LIST_HEAD(&mdsc->dentry_leases);
4107 INIT_LIST_HEAD(&mdsc->dentry_dir_leases);
Cheng Renquan2d06eeb2010-03-26 18:04:40 +08004108
Yehuda Sadeh37151662010-06-17 16:16:12 -07004109 ceph_caps_init(mdsc);
Yan, Zhengfe330322019-02-01 14:57:15 +08004110 ceph_adjust_caps_max_min(mdsc, fsc->mount_options);
Yehuda Sadeh37151662010-06-17 16:16:12 -07004111
Yan, Zheng75c96272017-12-14 15:11:09 +08004112 spin_lock_init(&mdsc->snapid_map_lock);
4113 mdsc->snapid_map_tree = RB_ROOT;
4114 INIT_LIST_HEAD(&mdsc->snapid_map_lru);
4115
Yan, Zheng10183a62015-04-27 15:33:28 +08004116 init_rwsem(&mdsc->pool_perm_rwsem);
4117 mdsc->pool_perm_tree = RB_ROOT;
4118
Yan, Zhengdfeb84d2018-07-02 15:55:23 +08004119 strscpy(mdsc->nodename, utsname()->nodename,
4120 sizeof(mdsc->nodename));
Sage Weil5f44f142009-11-18 14:52:18 -08004121 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07004122}
4123
4124/*
4125 * Wait for safe replies on open mds requests. If we time out, drop
4126 * all requests from the tree to avoid dangling dentry refs.
4127 */
4128static void wait_requests(struct ceph_mds_client *mdsc)
4129{
Ilya Dryomova319bf52015-05-15 12:02:17 +03004130 struct ceph_options *opts = mdsc->fsc->client->options;
Sage Weil2f2dc052009-10-06 11:31:09 -07004131 struct ceph_mds_request *req;
Sage Weil2f2dc052009-10-06 11:31:09 -07004132
4133 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08004134 if (__get_oldest_req(mdsc)) {
Sage Weil2f2dc052009-10-06 11:31:09 -07004135 mutex_unlock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08004136
Sage Weil2f2dc052009-10-06 11:31:09 -07004137 dout("wait_requests waiting for requests\n");
4138 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
Ilya Dryomova319bf52015-05-15 12:02:17 +03004139 ceph_timeout_jiffies(opts->mount_timeout));
Sage Weil2f2dc052009-10-06 11:31:09 -07004140
4141 /* tear down remaining requests */
Sage Weil44ca18f2010-02-15 12:08:46 -08004142 mutex_lock(&mdsc->mutex);
4143 while ((req = __get_oldest_req(mdsc))) {
Sage Weil2f2dc052009-10-06 11:31:09 -07004144 dout("wait_requests timed out on tid %llu\n",
4145 req->r_tid);
Sage Weil44ca18f2010-02-15 12:08:46 -08004146 __unregister_request(mdsc, req);
Sage Weil2f2dc052009-10-06 11:31:09 -07004147 }
4148 }
4149 mutex_unlock(&mdsc->mutex);
4150 dout("wait_requests done\n");
4151}
4152
4153/*
4154 * called before mount is ro, and before dentries are torn down.
4155 * (hmm, does this still race with new lookups?)
4156 */
4157void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
4158{
4159 dout("pre_umount\n");
4160 mdsc->stopping = 1;
4161
Yan, Zheng7aac4532018-03-13 19:52:20 +08004162 lock_unlock_sessions(mdsc);
Sage Weilafcdaea2009-10-14 14:27:38 -07004163 ceph_flush_dirty_caps(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07004164 wait_requests(mdsc);
Sage Weil17c688c2010-06-21 16:12:26 -07004165
4166 /*
4167 * wait for reply handlers to drop their request refs and
4168 * their inode/dcache refs
4169 */
4170 ceph_msgr_flush();
Sage Weil2f2dc052009-10-06 11:31:09 -07004171}
4172
4173/*
4174 * wait for all write mds requests to flush.
4175 */
4176static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
4177{
Sage Weil80fc7312010-03-16 15:28:54 -07004178 struct ceph_mds_request *req = NULL, *nextreq;
Sage Weil44ca18f2010-02-15 12:08:46 -08004179 struct rb_node *n;
Sage Weil2f2dc052009-10-06 11:31:09 -07004180
4181 mutex_lock(&mdsc->mutex);
4182 dout("wait_unsafe_requests want %lld\n", want_tid);
Sage Weil80fc7312010-03-16 15:28:54 -07004183restart:
Sage Weil44ca18f2010-02-15 12:08:46 -08004184 req = __get_oldest_req(mdsc);
4185 while (req && req->r_tid <= want_tid) {
Sage Weil80fc7312010-03-16 15:28:54 -07004186 /* find next request */
4187 n = rb_next(&req->r_node);
4188 if (n)
4189 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
4190 else
4191 nextreq = NULL;
Yan, Zhenge8a7b8b2015-05-19 18:54:40 +08004192 if (req->r_op != CEPH_MDS_OP_SETFILELOCK &&
4193 (req->r_op & CEPH_MDS_OP_WRITE)) {
Sage Weil44ca18f2010-02-15 12:08:46 -08004194 /* write op */
4195 ceph_mdsc_get_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07004196 if (nextreq)
4197 ceph_mdsc_get_request(nextreq);
Sage Weil44ca18f2010-02-15 12:08:46 -08004198 mutex_unlock(&mdsc->mutex);
4199 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
4200 req->r_tid, want_tid);
4201 wait_for_completion(&req->r_safe_completion);
4202 mutex_lock(&mdsc->mutex);
Sage Weil44ca18f2010-02-15 12:08:46 -08004203 ceph_mdsc_put_request(req);
Sage Weil80fc7312010-03-16 15:28:54 -07004204 if (!nextreq)
4205 break; /* next dne before, so we're done! */
4206 if (RB_EMPTY_NODE(&nextreq->r_node)) {
4207 /* next request was removed from tree */
4208 ceph_mdsc_put_request(nextreq);
4209 goto restart;
4210 }
4211 ceph_mdsc_put_request(nextreq); /* won't go away */
Sage Weil44ca18f2010-02-15 12:08:46 -08004212 }
Sage Weil80fc7312010-03-16 15:28:54 -07004213 req = nextreq;
Sage Weil2f2dc052009-10-06 11:31:09 -07004214 }
4215 mutex_unlock(&mdsc->mutex);
4216 dout("wait_unsafe_requests done\n");
4217}
4218
4219void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
4220{
Yan, Zheng0e294382016-07-04 18:06:41 +08004221 u64 want_tid, want_flush;
Sage Weil2f2dc052009-10-06 11:31:09 -07004222
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01004223 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
Sage Weil56b7cf92010-05-03 15:22:00 -07004224 return;
4225
Sage Weil2f2dc052009-10-06 11:31:09 -07004226 dout("sync\n");
4227 mutex_lock(&mdsc->mutex);
4228 want_tid = mdsc->last_tid;
Sage Weil2f2dc052009-10-06 11:31:09 -07004229 mutex_unlock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07004230
Sage Weilafcdaea2009-10-14 14:27:38 -07004231 ceph_flush_dirty_caps(mdsc);
Yan, Zhengd3383a82015-01-08 21:30:12 +08004232 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zheng8310b082015-06-09 17:20:12 +08004233 want_flush = mdsc->last_cap_flush_tid;
Yan, Zhengc8799fc2016-07-07 15:22:38 +08004234 if (!list_empty(&mdsc->cap_flush_list)) {
4235 struct ceph_cap_flush *cf =
4236 list_last_entry(&mdsc->cap_flush_list,
4237 struct ceph_cap_flush, g_list);
4238 cf->wake = true;
4239 }
Yan, Zhengd3383a82015-01-08 21:30:12 +08004240 spin_unlock(&mdsc->cap_dirty_lock);
4241
Yan, Zheng0e294382016-07-04 18:06:41 +08004242 dout("sync want tid %lld flush_seq %lld\n",
4243 want_tid, want_flush);
Sage Weil2f2dc052009-10-06 11:31:09 -07004244
4245 wait_unsafe_requests(mdsc, want_tid);
Yan, Zheng0e294382016-07-04 18:06:41 +08004246 wait_caps_flush(mdsc, want_flush);
Sage Weil2f2dc052009-10-06 11:31:09 -07004247}
4248
Sage Weilf3c60c52010-08-11 14:51:23 -07004249/*
4250 * true if all sessions are closed, or we force unmount
4251 */
Yan, Zhengfcff4152016-09-14 16:39:51 +08004252static bool done_closing_sessions(struct ceph_mds_client *mdsc, int skipped)
Sage Weilf3c60c52010-08-11 14:51:23 -07004253{
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01004254 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
Sage Weilf3c60c52010-08-11 14:51:23 -07004255 return true;
Yan, Zhengfcff4152016-09-14 16:39:51 +08004256 return atomic_read(&mdsc->num_sessions) <= skipped;
Sage Weilf3c60c52010-08-11 14:51:23 -07004257}
Sage Weil2f2dc052009-10-06 11:31:09 -07004258
4259/*
4260 * called after sb is ro.
4261 */
4262void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
4263{
Ilya Dryomova319bf52015-05-15 12:02:17 +03004264 struct ceph_options *opts = mdsc->fsc->client->options;
Sage Weil2f2dc052009-10-06 11:31:09 -07004265 struct ceph_mds_session *session;
4266 int i;
Yan, Zhengfcff4152016-09-14 16:39:51 +08004267 int skipped = 0;
Sage Weil2f2dc052009-10-06 11:31:09 -07004268
4269 dout("close_sessions\n");
4270
Sage Weil2f2dc052009-10-06 11:31:09 -07004271 /* close sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07004272 mutex_lock(&mdsc->mutex);
4273 for (i = 0; i < mdsc->max_sessions; i++) {
4274 session = __ceph_lookup_mds_session(mdsc, i);
4275 if (!session)
4276 continue;
Sage Weil2f2dc052009-10-06 11:31:09 -07004277 mutex_unlock(&mdsc->mutex);
Sage Weilf3c60c52010-08-11 14:51:23 -07004278 mutex_lock(&session->s_mutex);
Yan, Zhengfcff4152016-09-14 16:39:51 +08004279 if (__close_session(mdsc, session) <= 0)
4280 skipped++;
Sage Weilf3c60c52010-08-11 14:51:23 -07004281 mutex_unlock(&session->s_mutex);
4282 ceph_put_mds_session(session);
Sage Weil2f2dc052009-10-06 11:31:09 -07004283 mutex_lock(&mdsc->mutex);
4284 }
Sage Weilf3c60c52010-08-11 14:51:23 -07004285 mutex_unlock(&mdsc->mutex);
4286
4287 dout("waiting for sessions to close\n");
Yan, Zhengfcff4152016-09-14 16:39:51 +08004288 wait_event_timeout(mdsc->session_close_wq,
4289 done_closing_sessions(mdsc, skipped),
Ilya Dryomova319bf52015-05-15 12:02:17 +03004290 ceph_timeout_jiffies(opts->mount_timeout));
Sage Weil2f2dc052009-10-06 11:31:09 -07004291
4292 /* tear down remaining sessions */
Sage Weilf3c60c52010-08-11 14:51:23 -07004293 mutex_lock(&mdsc->mutex);
Sage Weil2f2dc052009-10-06 11:31:09 -07004294 for (i = 0; i < mdsc->max_sessions; i++) {
4295 if (mdsc->sessions[i]) {
4296 session = get_session(mdsc->sessions[i]);
Sage Weil2600d2d2010-02-22 15:12:16 -08004297 __unregister_session(mdsc, session);
Sage Weil2f2dc052009-10-06 11:31:09 -07004298 mutex_unlock(&mdsc->mutex);
4299 mutex_lock(&session->s_mutex);
4300 remove_session_caps(session);
4301 mutex_unlock(&session->s_mutex);
4302 ceph_put_mds_session(session);
4303 mutex_lock(&mdsc->mutex);
4304 }
4305 }
Sage Weil2f2dc052009-10-06 11:31:09 -07004306 WARN_ON(!list_empty(&mdsc->cap_delay_list));
Sage Weil2f2dc052009-10-06 11:31:09 -07004307 mutex_unlock(&mdsc->mutex);
4308
Yan, Zheng75c96272017-12-14 15:11:09 +08004309 ceph_cleanup_snapid_map(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07004310 ceph_cleanup_empty_realms(mdsc);
4311
Yan, Zheng37c4efc2019-01-31 16:55:51 +08004312 cancel_work_sync(&mdsc->cap_reclaim_work);
Sage Weil2f2dc052009-10-06 11:31:09 -07004313 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
4314
4315 dout("stopped\n");
4316}
4317
Yan, Zheng48fec5d2015-07-01 16:27:46 +08004318void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc)
4319{
4320 struct ceph_mds_session *session;
4321 int mds;
4322
4323 dout("force umount\n");
4324
4325 mutex_lock(&mdsc->mutex);
4326 for (mds = 0; mds < mdsc->max_sessions; mds++) {
4327 session = __ceph_lookup_mds_session(mdsc, mds);
4328 if (!session)
4329 continue;
4330 mutex_unlock(&mdsc->mutex);
4331 mutex_lock(&session->s_mutex);
4332 __close_session(mdsc, session);
4333 if (session->s_state == CEPH_MDS_SESSION_CLOSING) {
4334 cleanup_session_requests(mdsc, session);
4335 remove_session_caps(session);
4336 }
4337 mutex_unlock(&session->s_mutex);
4338 ceph_put_mds_session(session);
4339 mutex_lock(&mdsc->mutex);
4340 kick_requests(mdsc, mds);
4341 }
4342 __wake_requests(mdsc, &mdsc->waiting_for_map);
4343 mutex_unlock(&mdsc->mutex);
4344}
4345
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004346static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
Sage Weil2f2dc052009-10-06 11:31:09 -07004347{
4348 dout("stop\n");
4349 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
4350 if (mdsc->mdsmap)
4351 ceph_mdsmap_destroy(mdsc->mdsmap);
4352 kfree(mdsc->sessions);
Yehuda Sadeh37151662010-06-17 16:16:12 -07004353 ceph_caps_finalize(mdsc);
Yan, Zheng10183a62015-04-27 15:33:28 +08004354 ceph_pool_perm_destroy(mdsc);
Sage Weil2f2dc052009-10-06 11:31:09 -07004355}
4356
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004357void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
4358{
4359 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weilef550f62011-03-25 13:27:48 -07004360 dout("mdsc_destroy %p\n", mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07004361
Chengguang Xu50c55ae2018-03-14 13:47:33 +08004362 if (!mdsc)
4363 return;
4364
Sage Weilef550f62011-03-25 13:27:48 -07004365 /* flush out any connection work with references to us */
4366 ceph_msgr_flush();
4367
Yan, Zheng62a65f32017-06-22 16:26:34 +08004368 ceph_mdsc_stop(mdsc);
4369
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004370 fsc->mdsc = NULL;
4371 kfree(mdsc);
Sage Weilef550f62011-03-25 13:27:48 -07004372 dout("mdsc_destroy %p done\n", mdsc);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004373}
4374
Yan, Zheng430afba2016-07-08 11:25:38 +08004375void ceph_mdsc_handle_fsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
4376{
4377 struct ceph_fs_client *fsc = mdsc->fsc;
4378 const char *mds_namespace = fsc->mount_options->mds_namespace;
4379 void *p = msg->front.iov_base;
4380 void *end = p + msg->front.iov_len;
4381 u32 epoch;
4382 u32 map_len;
4383 u32 num_fs;
4384 u32 mount_fscid = (u32)-1;
4385 u8 struct_v, struct_cv;
4386 int err = -EINVAL;
4387
4388 ceph_decode_need(&p, end, sizeof(u32), bad);
4389 epoch = ceph_decode_32(&p);
4390
4391 dout("handle_fsmap epoch %u\n", epoch);
4392
4393 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
4394 struct_v = ceph_decode_8(&p);
4395 struct_cv = ceph_decode_8(&p);
4396 map_len = ceph_decode_32(&p);
4397
4398 ceph_decode_need(&p, end, sizeof(u32) * 3, bad);
4399 p += sizeof(u32) * 2; /* skip epoch and legacy_client_fscid */
4400
4401 num_fs = ceph_decode_32(&p);
4402 while (num_fs-- > 0) {
4403 void *info_p, *info_end;
4404 u32 info_len;
4405 u8 info_v, info_cv;
4406 u32 fscid, namelen;
4407
4408 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
4409 info_v = ceph_decode_8(&p);
4410 info_cv = ceph_decode_8(&p);
4411 info_len = ceph_decode_32(&p);
4412 ceph_decode_need(&p, end, info_len, bad);
4413 info_p = p;
4414 info_end = p + info_len;
4415 p = info_end;
4416
4417 ceph_decode_need(&info_p, info_end, sizeof(u32) * 2, bad);
4418 fscid = ceph_decode_32(&info_p);
4419 namelen = ceph_decode_32(&info_p);
4420 ceph_decode_need(&info_p, info_end, namelen, bad);
4421
4422 if (mds_namespace &&
4423 strlen(mds_namespace) == namelen &&
4424 !strncmp(mds_namespace, (char *)info_p, namelen)) {
4425 mount_fscid = fscid;
4426 break;
4427 }
4428 }
4429
4430 ceph_monc_got_map(&fsc->client->monc, CEPH_SUB_FSMAP, epoch);
4431 if (mount_fscid != (u32)-1) {
4432 fsc->client->monc.fs_cluster_id = mount_fscid;
4433 ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP,
4434 0, true);
4435 ceph_monc_renew_subs(&fsc->client->monc);
4436 } else {
4437 err = -ENOENT;
4438 goto err_out;
4439 }
4440 return;
Ilya Dryomov76bd6ec2017-10-16 10:32:50 +02004441
Yan, Zheng430afba2016-07-08 11:25:38 +08004442bad:
4443 pr_err("error decoding fsmap\n");
4444err_out:
4445 mutex_lock(&mdsc->mutex);
Ilya Dryomov76bd6ec2017-10-16 10:32:50 +02004446 mdsc->mdsmap_err = err;
Yan, Zheng430afba2016-07-08 11:25:38 +08004447 __wake_requests(mdsc, &mdsc->waiting_for_map);
4448 mutex_unlock(&mdsc->mutex);
Yan, Zheng430afba2016-07-08 11:25:38 +08004449}
Sage Weil2f2dc052009-10-06 11:31:09 -07004450
4451/*
4452 * handle mds map update.
4453 */
Yan, Zheng430afba2016-07-08 11:25:38 +08004454void ceph_mdsc_handle_mdsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
Sage Weil2f2dc052009-10-06 11:31:09 -07004455{
4456 u32 epoch;
4457 u32 maplen;
4458 void *p = msg->front.iov_base;
4459 void *end = p + msg->front.iov_len;
4460 struct ceph_mdsmap *newmap, *oldmap;
4461 struct ceph_fsid fsid;
4462 int err = -EINVAL;
4463
4464 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
4465 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004466 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
Sage Weil07433042009-11-18 16:50:41 -08004467 return;
Sage Weilc89136e2009-10-14 09:59:09 -07004468 epoch = ceph_decode_32(&p);
4469 maplen = ceph_decode_32(&p);
Sage Weil2f2dc052009-10-06 11:31:09 -07004470 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
4471
4472 /* do we need it? */
Sage Weil2f2dc052009-10-06 11:31:09 -07004473 mutex_lock(&mdsc->mutex);
4474 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
4475 dout("handle_map epoch %u <= our %u\n",
4476 epoch, mdsc->mdsmap->m_epoch);
4477 mutex_unlock(&mdsc->mutex);
4478 return;
4479 }
4480
4481 newmap = ceph_mdsmap_decode(&p, end);
4482 if (IS_ERR(newmap)) {
4483 err = PTR_ERR(newmap);
4484 goto bad_unlock;
4485 }
4486
4487 /* swap into place */
4488 if (mdsc->mdsmap) {
4489 oldmap = mdsc->mdsmap;
4490 mdsc->mdsmap = newmap;
4491 check_new_map(mdsc, newmap, oldmap);
4492 ceph_mdsmap_destroy(oldmap);
4493 } else {
4494 mdsc->mdsmap = newmap; /* first mds map */
4495 }
Chengguang Xu719784b2018-07-19 22:15:24 +08004496 mdsc->fsc->max_file_size = min((loff_t)mdsc->mdsmap->m_max_file_size,
4497 MAX_LFS_FILESIZE);
Sage Weil2f2dc052009-10-06 11:31:09 -07004498
4499 __wake_requests(mdsc, &mdsc->waiting_for_map);
Ilya Dryomov82dcaba2016-01-19 16:19:06 +01004500 ceph_monc_got_map(&mdsc->fsc->client->monc, CEPH_SUB_MDSMAP,
4501 mdsc->mdsmap->m_epoch);
Sage Weil2f2dc052009-10-06 11:31:09 -07004502
4503 mutex_unlock(&mdsc->mutex);
4504 schedule_delayed(mdsc);
4505 return;
4506
4507bad_unlock:
4508 mutex_unlock(&mdsc->mutex);
4509bad:
4510 pr_err("error decoding mdsmap %d\n", err);
4511 return;
4512}
4513
4514static struct ceph_connection *con_get(struct ceph_connection *con)
4515{
4516 struct ceph_mds_session *s = con->private;
4517
4518 if (get_session(s)) {
Elena Reshetova3997c012017-03-03 11:15:06 +02004519 dout("mdsc con_get %p ok (%d)\n", s, refcount_read(&s->s_ref));
Sage Weil2f2dc052009-10-06 11:31:09 -07004520 return con;
4521 }
4522 dout("mdsc con_get %p FAIL\n", s);
4523 return NULL;
4524}
4525
4526static void con_put(struct ceph_connection *con)
4527{
4528 struct ceph_mds_session *s = con->private;
4529
Elena Reshetova3997c012017-03-03 11:15:06 +02004530 dout("mdsc con_put %p (%d)\n", s, refcount_read(&s->s_ref) - 1);
Sage Weil2f2dc052009-10-06 11:31:09 -07004531 ceph_put_mds_session(s);
4532}
4533
4534/*
4535 * if the client is unresponsive for long enough, the mds will kill
4536 * the session entirely.
4537 */
4538static void peer_reset(struct ceph_connection *con)
4539{
4540 struct ceph_mds_session *s = con->private;
Sage Weil7e70f0e2010-03-18 13:59:12 -07004541 struct ceph_mds_client *mdsc = s->s_mdsc;
Sage Weil2f2dc052009-10-06 11:31:09 -07004542
Fabian Frederickf3ae1b92014-06-06 14:35:37 -07004543 pr_warn("mds%d closed our session\n", s->s_mds);
Sage Weil7e70f0e2010-03-18 13:59:12 -07004544 send_mds_reconnect(mdsc, s);
Sage Weil2f2dc052009-10-06 11:31:09 -07004545}
4546
4547static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
4548{
4549 struct ceph_mds_session *s = con->private;
4550 struct ceph_mds_client *mdsc = s->s_mdsc;
4551 int type = le16_to_cpu(msg->hdr.type);
4552
Sage Weil2600d2d2010-02-22 15:12:16 -08004553 mutex_lock(&mdsc->mutex);
4554 if (__verify_registered_session(mdsc, s) < 0) {
4555 mutex_unlock(&mdsc->mutex);
4556 goto out;
4557 }
4558 mutex_unlock(&mdsc->mutex);
4559
Sage Weil2f2dc052009-10-06 11:31:09 -07004560 switch (type) {
4561 case CEPH_MSG_MDS_MAP:
Yan, Zheng430afba2016-07-08 11:25:38 +08004562 ceph_mdsc_handle_mdsmap(mdsc, msg);
4563 break;
4564 case CEPH_MSG_FS_MAP_USER:
4565 ceph_mdsc_handle_fsmap(mdsc, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004566 break;
4567 case CEPH_MSG_CLIENT_SESSION:
4568 handle_session(s, msg);
4569 break;
4570 case CEPH_MSG_CLIENT_REPLY:
4571 handle_reply(s, msg);
4572 break;
4573 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
Sage Weil2600d2d2010-02-22 15:12:16 -08004574 handle_forward(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004575 break;
4576 case CEPH_MSG_CLIENT_CAPS:
4577 ceph_handle_caps(s, msg);
4578 break;
4579 case CEPH_MSG_CLIENT_SNAP:
Sage Weil2600d2d2010-02-22 15:12:16 -08004580 ceph_handle_snap(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004581 break;
4582 case CEPH_MSG_CLIENT_LEASE:
Sage Weil2600d2d2010-02-22 15:12:16 -08004583 handle_lease(mdsc, s, msg);
Sage Weil2f2dc052009-10-06 11:31:09 -07004584 break;
Luis Henriquesfb18a572018-01-05 10:47:18 +00004585 case CEPH_MSG_CLIENT_QUOTA:
4586 ceph_handle_quota(mdsc, s, msg);
4587 break;
Sage Weil2f2dc052009-10-06 11:31:09 -07004588
4589 default:
4590 pr_err("received unknown message type %d %s\n", type,
4591 ceph_msg_type_name(type));
4592 }
Sage Weil2600d2d2010-02-22 15:12:16 -08004593out:
Sage Weil2f2dc052009-10-06 11:31:09 -07004594 ceph_msg_put(msg);
4595}
4596
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004597/*
4598 * authentication
4599 */
Alex Eldera3530df2012-05-16 15:16:39 -05004600
4601/*
4602 * Note: returned pointer is the address of a structure that's
4603 * managed separately. Caller must *not* attempt to free it.
4604 */
4605static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05004606 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004607{
4608 struct ceph_mds_session *s = con->private;
4609 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004610 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05004611 struct ceph_auth_handshake *auth = &s->s_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004612
Alex Elder74f18692012-05-16 15:16:39 -05004613 if (force_new && auth->authorizer) {
Ilya Dryomov6c1ea262016-04-11 19:34:49 +02004614 ceph_auth_destroy_authorizer(auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05004615 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004616 }
Sage Weil27859f92013-03-25 10:26:14 -07004617 if (!auth->authorizer) {
4618 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4619 auth);
Sage Weil0bed9b52013-03-25 10:26:01 -07004620 if (ret)
4621 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07004622 } else {
4623 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4624 auth);
Alex Eldera2556512012-05-16 15:16:39 -05004625 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05004626 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004627 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004628 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05004629
Alex Eldera3530df2012-05-16 15:16:39 -05004630 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004631}
4632
Ilya Dryomov6daca132018-07-27 19:18:34 +02004633static int add_authorizer_challenge(struct ceph_connection *con,
4634 void *challenge_buf, int challenge_buf_len)
4635{
4636 struct ceph_mds_session *s = con->private;
4637 struct ceph_mds_client *mdsc = s->s_mdsc;
4638 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
4639
4640 return ceph_auth_add_authorizer_challenge(ac, s->s_auth.authorizer,
4641 challenge_buf, challenge_buf_len);
4642}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004643
Ilya Dryomov0dde5842016-12-02 16:35:09 +01004644static int verify_authorizer_reply(struct ceph_connection *con)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004645{
4646 struct ceph_mds_session *s = con->private;
4647 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004648 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004649
Ilya Dryomov0dde5842016-12-02 16:35:09 +01004650 return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004651}
4652
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004653static int invalidate_authorizer(struct ceph_connection *con)
4654{
4655 struct ceph_mds_session *s = con->private;
4656 struct ceph_mds_client *mdsc = s->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004657 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004658
Sage Weil27859f92013-03-25 10:26:14 -07004659 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004660
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004661 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004662}
4663
Alex Elder53ded492013-03-01 18:00:14 -06004664static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
4665 struct ceph_msg_header *hdr, int *skip)
4666{
4667 struct ceph_msg *msg;
4668 int type = (int) le16_to_cpu(hdr->type);
4669 int front_len = (int) le32_to_cpu(hdr->front_len);
4670
4671 if (con->in_msg)
4672 return con->in_msg;
4673
4674 *skip = 0;
4675 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
4676 if (!msg) {
4677 pr_err("unable to allocate msg type %d len %d\n",
4678 type, front_len);
4679 return NULL;
4680 }
Alex Elder53ded492013-03-01 18:00:14 -06004681
4682 return msg;
4683}
4684
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004685static int mds_sign_message(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08004686{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004687 struct ceph_mds_session *s = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08004688 struct ceph_auth_handshake *auth = &s->s_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004689
Yan, Zheng33d07332014-11-04 16:33:37 +08004690 return ceph_auth_sign_message(auth, msg);
4691}
4692
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004693static int mds_check_message_signature(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08004694{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004695 struct ceph_mds_session *s = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08004696 struct ceph_auth_handshake *auth = &s->s_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004697
Yan, Zheng33d07332014-11-04 16:33:37 +08004698 return ceph_auth_check_message_signature(auth, msg);
4699}
4700
Tobias Klauser9e327892010-05-20 10:40:19 +02004701static const struct ceph_connection_operations mds_con_ops = {
Sage Weil2f2dc052009-10-06 11:31:09 -07004702 .get = con_get,
4703 .put = con_put,
4704 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004705 .get_authorizer = get_authorizer,
Ilya Dryomov6daca132018-07-27 19:18:34 +02004706 .add_authorizer_challenge = add_authorizer_challenge,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08004707 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08004708 .invalidate_authorizer = invalidate_authorizer,
Sage Weil2f2dc052009-10-06 11:31:09 -07004709 .peer_reset = peer_reset,
Alex Elder53ded492013-03-01 18:00:14 -06004710 .alloc_msg = mds_alloc_msg,
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01004711 .sign_message = mds_sign_message,
4712 .check_message_signature = mds_check_message_signature,
Sage Weil2f2dc052009-10-06 11:31:09 -07004713};
4714
Sage Weil2f2dc052009-10-06 11:31:09 -07004715/* eof */