]>
Commit | Line | Data |
---|---|---|
3d14c5d2 | 1 | #include <linux/ceph/ceph_debug.h> |
2f2dc053 | 2 | |
496e5955 | 3 | #include <linux/fs.h> |
2f2dc053 | 4 | #include <linux/wait.h> |
5a0e3ad6 | 5 | #include <linux/slab.h> |
54008399 | 6 | #include <linux/gfp.h> |
2f2dc053 | 7 | #include <linux/sched.h> |
3d14c5d2 YS |
8 | #include <linux/debugfs.h> |
9 | #include <linux/seq_file.h> | |
dbd0c8bf | 10 | #include <linux/utsname.h> |
3e0708b9 | 11 | #include <linux/ratelimit.h> |
2f2dc053 | 12 | |
2f2dc053 | 13 | #include "super.h" |
3d14c5d2 YS |
14 | #include "mds_client.h" |
15 | ||
1fe60e51 | 16 | #include <linux/ceph/ceph_features.h> |
3d14c5d2 YS |
17 | #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> | |
2f2dc053 SW |
22 | |
23 | /* | |
24 | * A cluster of MDS (metadata server) daemons is responsible for | |
25 | * managing the file system namespace (the directory hierarchy and | |
26 | * inodes) and for coordinating shared access to storage. Metadata is | |
27 | * partitioning hierarchically across a number of servers, and that | |
28 | * partition varies over time as the cluster adjusts the distribution | |
29 | * in order to balance load. | |
30 | * | |
31 | * The MDS client is primarily responsible to managing synchronous | |
32 | * metadata requests for operations like open, unlink, and so forth. | |
33 | * If there is a MDS failure, we find out about it when we (possibly | |
34 | * request and) receive a new MDS map, and can resubmit affected | |
35 | * requests. | |
36 | * | |
37 | * For the most part, though, we take advantage of a lossless | |
38 | * communications channel to the MDS, and do not need to worry about | |
39 | * timing out or resubmitting requests. | |
40 | * | |
41 | * We maintain a stateful "session" with each MDS we interact with. | |
42 | * Within each session, we sent periodic heartbeat messages to ensure | |
43 | * any capabilities or leases we have been issues remain valid. If | |
44 | * the session times out and goes stale, our leases and capabilities | |
45 | * are no longer valid. | |
46 | */ | |
47 | ||
20cb34ae | 48 | struct ceph_reconnect_state { |
44c99757 | 49 | int nr_caps; |
20cb34ae | 50 | struct ceph_pagelist *pagelist; |
121f22a1 | 51 | unsigned msg_version; |
20cb34ae SW |
52 | }; |
53 | ||
2f2dc053 SW |
54 | static void __wake_requests(struct ceph_mds_client *mdsc, |
55 | struct list_head *head); | |
56 | ||
9e32789f | 57 | static const struct ceph_connection_operations mds_con_ops; |
2f2dc053 SW |
58 | |
59 | ||
60 | /* | |
61 | * mds reply parsing | |
62 | */ | |
63 | ||
64 | /* | |
65 | * parse individual inode info | |
66 | */ | |
67 | static int parse_reply_info_in(void **p, void *end, | |
14303d20 | 68 | struct ceph_mds_reply_info_in *info, |
12b4629a | 69 | u64 features) |
2f2dc053 SW |
70 | { |
71 | int err = -EIO; | |
72 | ||
73 | info->in = *p; | |
74 | *p += sizeof(struct ceph_mds_reply_inode) + | |
75 | sizeof(*info->in->fragtree.splits) * | |
76 | le32_to_cpu(info->in->fragtree.nsplits); | |
77 | ||
78 | ceph_decode_32_safe(p, end, info->symlink_len, bad); | |
79 | ceph_decode_need(p, end, info->symlink_len, bad); | |
80 | info->symlink = *p; | |
81 | *p += info->symlink_len; | |
82 | ||
14303d20 SW |
83 | if (features & CEPH_FEATURE_DIRLAYOUTHASH) |
84 | ceph_decode_copy_safe(p, end, &info->dir_layout, | |
85 | sizeof(info->dir_layout), bad); | |
86 | else | |
87 | memset(&info->dir_layout, 0, sizeof(info->dir_layout)); | |
88 | ||
2f2dc053 SW |
89 | ceph_decode_32_safe(p, end, info->xattr_len, bad); |
90 | ceph_decode_need(p, end, info->xattr_len, bad); | |
91 | info->xattr_data = *p; | |
92 | *p += info->xattr_len; | |
fb01d1f8 YZ |
93 | |
94 | if (features & CEPH_FEATURE_MDS_INLINE_DATA) { | |
95 | ceph_decode_64_safe(p, end, info->inline_version, bad); | |
96 | ceph_decode_32_safe(p, end, info->inline_len, bad); | |
97 | ceph_decode_need(p, end, info->inline_len, bad); | |
98 | info->inline_data = *p; | |
99 | *p += info->inline_len; | |
100 | } else | |
101 | info->inline_version = CEPH_INLINE_NONE; | |
102 | ||
779fe0fb YZ |
103 | info->pool_ns_len = 0; |
104 | info->pool_ns_data = NULL; | |
5ea5c5e0 YZ |
105 | if (features & CEPH_FEATURE_FS_FILE_LAYOUT_V2) { |
106 | ceph_decode_32_safe(p, end, info->pool_ns_len, bad); | |
779fe0fb YZ |
107 | if (info->pool_ns_len > 0) { |
108 | ceph_decode_need(p, end, info->pool_ns_len, bad); | |
109 | info->pool_ns_data = *p; | |
110 | *p += info->pool_ns_len; | |
111 | } | |
5ea5c5e0 YZ |
112 | } |
113 | ||
2f2dc053 SW |
114 | return 0; |
115 | bad: | |
116 | return err; | |
117 | } | |
118 | ||
119 | /* | |
120 | * parse a normal reply, which may contain a (dir+)dentry and/or a | |
121 | * target inode. | |
122 | */ | |
123 | static int parse_reply_info_trace(void **p, void *end, | |
14303d20 | 124 | struct ceph_mds_reply_info_parsed *info, |
12b4629a | 125 | u64 features) |
2f2dc053 SW |
126 | { |
127 | int err; | |
128 | ||
129 | if (info->head->is_dentry) { | |
14303d20 | 130 | err = parse_reply_info_in(p, end, &info->diri, features); |
2f2dc053 SW |
131 | if (err < 0) |
132 | goto out_bad; | |
133 | ||
134 | if (unlikely(*p + sizeof(*info->dirfrag) > end)) | |
135 | goto bad; | |
136 | info->dirfrag = *p; | |
137 | *p += sizeof(*info->dirfrag) + | |
138 | sizeof(u32)*le32_to_cpu(info->dirfrag->ndist); | |
139 | if (unlikely(*p > end)) | |
140 | goto bad; | |
141 | ||
142 | ceph_decode_32_safe(p, end, info->dname_len, bad); | |
143 | ceph_decode_need(p, end, info->dname_len, bad); | |
144 | info->dname = *p; | |
145 | *p += info->dname_len; | |
146 | info->dlease = *p; | |
147 | *p += sizeof(*info->dlease); | |
148 | } | |
149 | ||
150 | if (info->head->is_target) { | |
14303d20 | 151 | err = parse_reply_info_in(p, end, &info->targeti, features); |
2f2dc053 SW |
152 | if (err < 0) |
153 | goto out_bad; | |
154 | } | |
155 | ||
156 | if (unlikely(*p != end)) | |
157 | goto bad; | |
158 | return 0; | |
159 | ||
160 | bad: | |
161 | err = -EIO; | |
162 | out_bad: | |
163 | pr_err("problem parsing mds trace %d\n", err); | |
164 | return err; | |
165 | } | |
166 | ||
167 | /* | |
168 | * parse readdir results | |
169 | */ | |
170 | static int parse_reply_info_dir(void **p, void *end, | |
14303d20 | 171 | struct ceph_mds_reply_info_parsed *info, |
12b4629a | 172 | u64 features) |
2f2dc053 SW |
173 | { |
174 | u32 num, i = 0; | |
175 | int err; | |
176 | ||
177 | info->dir_dir = *p; | |
178 | if (*p + sizeof(*info->dir_dir) > end) | |
179 | goto bad; | |
180 | *p += sizeof(*info->dir_dir) + | |
181 | sizeof(u32)*le32_to_cpu(info->dir_dir->ndist); | |
182 | if (*p > end) | |
183 | goto bad; | |
184 | ||
185 | ceph_decode_need(p, end, sizeof(num) + 2, bad); | |
c89136ea | 186 | num = ceph_decode_32(p); |
956d39d6 YZ |
187 | { |
188 | u16 flags = ceph_decode_16(p); | |
189 | info->dir_end = !!(flags & CEPH_READDIR_FRAG_END); | |
190 | info->dir_complete = !!(flags & CEPH_READDIR_FRAG_COMPLETE); | |
f3c4ebe6 | 191 | info->hash_order = !!(flags & CEPH_READDIR_HASH_ORDER); |
956d39d6 | 192 | } |
2f2dc053 SW |
193 | if (num == 0) |
194 | goto done; | |
195 | ||
2a5beea3 YZ |
196 | BUG_ON(!info->dir_entries); |
197 | if ((unsigned long)(info->dir_entries + num) > | |
198 | (unsigned long)info->dir_entries + info->dir_buf_size) { | |
54008399 YZ |
199 | pr_err("dir contents are larger than expected\n"); |
200 | WARN_ON(1); | |
201 | goto bad; | |
202 | } | |
2f2dc053 | 203 | |
54008399 | 204 | info->dir_nr = num; |
2f2dc053 | 205 | while (num) { |
2a5beea3 | 206 | struct ceph_mds_reply_dir_entry *rde = info->dir_entries + i; |
2f2dc053 SW |
207 | /* dentry */ |
208 | ceph_decode_need(p, end, sizeof(u32)*2, bad); | |
2a5beea3 YZ |
209 | rde->name_len = ceph_decode_32(p); |
210 | ceph_decode_need(p, end, rde->name_len, bad); | |
211 | rde->name = *p; | |
212 | *p += rde->name_len; | |
213 | dout("parsed dir dname '%.*s'\n", rde->name_len, rde->name); | |
214 | rde->lease = *p; | |
2f2dc053 SW |
215 | *p += sizeof(struct ceph_mds_reply_lease); |
216 | ||
217 | /* inode */ | |
2a5beea3 | 218 | err = parse_reply_info_in(p, end, &rde->inode, features); |
2f2dc053 SW |
219 | if (err < 0) |
220 | goto out_bad; | |
8974eebd YZ |
221 | /* ceph_readdir_prepopulate() will update it */ |
222 | rde->offset = 0; | |
2f2dc053 SW |
223 | i++; |
224 | num--; | |
225 | } | |
226 | ||
227 | done: | |
228 | if (*p != end) | |
229 | goto bad; | |
230 | return 0; | |
231 | ||
232 | bad: | |
233 | err = -EIO; | |
234 | out_bad: | |
235 | pr_err("problem parsing dir contents %d\n", err); | |
236 | return err; | |
237 | } | |
238 | ||
25933abd HS |
239 | /* |
240 | * parse fcntl F_GETLK results | |
241 | */ | |
242 | static int parse_reply_info_filelock(void **p, void *end, | |
14303d20 | 243 | struct ceph_mds_reply_info_parsed *info, |
12b4629a | 244 | u64 features) |
25933abd HS |
245 | { |
246 | if (*p + sizeof(*info->filelock_reply) > end) | |
247 | goto bad; | |
248 | ||
249 | info->filelock_reply = *p; | |
250 | *p += sizeof(*info->filelock_reply); | |
251 | ||
252 | if (unlikely(*p != end)) | |
253 | goto bad; | |
254 | return 0; | |
255 | ||
256 | bad: | |
257 | return -EIO; | |
258 | } | |
259 | ||
6e8575fa SL |
260 | /* |
261 | * parse create results | |
262 | */ | |
263 | static int parse_reply_info_create(void **p, void *end, | |
264 | struct ceph_mds_reply_info_parsed *info, | |
12b4629a | 265 | u64 features) |
6e8575fa SL |
266 | { |
267 | if (features & CEPH_FEATURE_REPLY_CREATE_INODE) { | |
268 | if (*p == end) { | |
269 | info->has_create_ino = false; | |
270 | } else { | |
271 | info->has_create_ino = true; | |
272 | info->ino = ceph_decode_64(p); | |
273 | } | |
274 | } | |
275 | ||
276 | if (unlikely(*p != end)) | |
277 | goto bad; | |
278 | return 0; | |
279 | ||
280 | bad: | |
281 | return -EIO; | |
282 | } | |
283 | ||
25933abd HS |
284 | /* |
285 | * parse extra results | |
286 | */ | |
287 | static int parse_reply_info_extra(void **p, void *end, | |
14303d20 | 288 | struct ceph_mds_reply_info_parsed *info, |
12b4629a | 289 | u64 features) |
25933abd | 290 | { |
6df8c9d8 JL |
291 | u32 op = le32_to_cpu(info->head->op); |
292 | ||
293 | if (op == CEPH_MDS_OP_GETFILELOCK) | |
14303d20 | 294 | return parse_reply_info_filelock(p, end, info, features); |
6df8c9d8 | 295 | else if (op == CEPH_MDS_OP_READDIR || op == CEPH_MDS_OP_LSSNAP) |
14303d20 | 296 | return parse_reply_info_dir(p, end, info, features); |
6df8c9d8 | 297 | else if (op == CEPH_MDS_OP_CREATE) |
6e8575fa SL |
298 | return parse_reply_info_create(p, end, info, features); |
299 | else | |
300 | return -EIO; | |
25933abd HS |
301 | } |
302 | ||
2f2dc053 SW |
303 | /* |
304 | * parse entire mds reply | |
305 | */ | |
306 | static int parse_reply_info(struct ceph_msg *msg, | |
14303d20 | 307 | struct ceph_mds_reply_info_parsed *info, |
12b4629a | 308 | u64 features) |
2f2dc053 SW |
309 | { |
310 | void *p, *end; | |
311 | u32 len; | |
312 | int err; | |
313 | ||
314 | info->head = msg->front.iov_base; | |
315 | p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head); | |
316 | end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head); | |
317 | ||
318 | /* trace */ | |
319 | ceph_decode_32_safe(&p, end, len, bad); | |
320 | if (len > 0) { | |
32852a81 | 321 | ceph_decode_need(&p, end, len, bad); |
14303d20 | 322 | err = parse_reply_info_trace(&p, p+len, info, features); |
2f2dc053 SW |
323 | if (err < 0) |
324 | goto out_bad; | |
325 | } | |
326 | ||
25933abd | 327 | /* extra */ |
2f2dc053 SW |
328 | ceph_decode_32_safe(&p, end, len, bad); |
329 | if (len > 0) { | |
32852a81 | 330 | ceph_decode_need(&p, end, len, bad); |
14303d20 | 331 | err = parse_reply_info_extra(&p, p+len, info, features); |
2f2dc053 SW |
332 | if (err < 0) |
333 | goto out_bad; | |
334 | } | |
335 | ||
336 | /* snap blob */ | |
337 | ceph_decode_32_safe(&p, end, len, bad); | |
338 | info->snapblob_len = len; | |
339 | info->snapblob = p; | |
340 | p += len; | |
341 | ||
342 | if (p != end) | |
343 | goto bad; | |
344 | return 0; | |
345 | ||
346 | bad: | |
347 | err = -EIO; | |
348 | out_bad: | |
349 | pr_err("mds parse_reply err %d\n", err); | |
350 | return err; | |
351 | } | |
352 | ||
353 | static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info) | |
354 | { | |
2a5beea3 | 355 | if (!info->dir_entries) |
54008399 | 356 | return; |
2a5beea3 | 357 | free_pages((unsigned long)info->dir_entries, get_order(info->dir_buf_size)); |
2f2dc053 SW |
358 | } |
359 | ||
360 | ||
361 | /* | |
362 | * sessions | |
363 | */ | |
a687ecaf | 364 | const char *ceph_session_state_name(int s) |
2f2dc053 SW |
365 | { |
366 | switch (s) { | |
367 | case CEPH_MDS_SESSION_NEW: return "new"; | |
368 | case CEPH_MDS_SESSION_OPENING: return "opening"; | |
369 | case CEPH_MDS_SESSION_OPEN: return "open"; | |
370 | case CEPH_MDS_SESSION_HUNG: return "hung"; | |
371 | case CEPH_MDS_SESSION_CLOSING: return "closing"; | |
44ca18f2 | 372 | case CEPH_MDS_SESSION_RESTARTING: return "restarting"; |
2f2dc053 | 373 | case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting"; |
fcff415c | 374 | case CEPH_MDS_SESSION_REJECTED: return "rejected"; |
2f2dc053 SW |
375 | default: return "???"; |
376 | } | |
377 | } | |
378 | ||
379 | static struct ceph_mds_session *get_session(struct ceph_mds_session *s) | |
380 | { | |
381 | if (atomic_inc_not_zero(&s->s_ref)) { | |
382 | dout("mdsc get_session %p %d -> %d\n", s, | |
383 | atomic_read(&s->s_ref)-1, atomic_read(&s->s_ref)); | |
384 | return s; | |
385 | } else { | |
386 | dout("mdsc get_session %p 0 -- FAIL", s); | |
387 | return NULL; | |
388 | } | |
389 | } | |
390 | ||
391 | void ceph_put_mds_session(struct ceph_mds_session *s) | |
392 | { | |
393 | dout("mdsc put_session %p %d -> %d\n", s, | |
394 | atomic_read(&s->s_ref), atomic_read(&s->s_ref)-1); | |
4e7a5dcd | 395 | if (atomic_dec_and_test(&s->s_ref)) { |
6c4a1915 | 396 | if (s->s_auth.authorizer) |
6c1ea260 | 397 | ceph_auth_destroy_authorizer(s->s_auth.authorizer); |
2f2dc053 | 398 | kfree(s); |
4e7a5dcd | 399 | } |
2f2dc053 SW |
400 | } |
401 | ||
402 | /* | |
403 | * called under mdsc->mutex | |
404 | */ | |
405 | struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc, | |
406 | int mds) | |
407 | { | |
408 | struct ceph_mds_session *session; | |
409 | ||
410 | if (mds >= mdsc->max_sessions || mdsc->sessions[mds] == NULL) | |
411 | return NULL; | |
412 | session = mdsc->sessions[mds]; | |
413 | dout("lookup_mds_session %p %d\n", session, | |
414 | atomic_read(&session->s_ref)); | |
415 | get_session(session); | |
416 | return session; | |
417 | } | |
418 | ||
419 | static bool __have_session(struct ceph_mds_client *mdsc, int mds) | |
420 | { | |
421 | if (mds >= mdsc->max_sessions) | |
422 | return false; | |
423 | return mdsc->sessions[mds]; | |
424 | } | |
425 | ||
2600d2dd SW |
426 | static int __verify_registered_session(struct ceph_mds_client *mdsc, |
427 | struct ceph_mds_session *s) | |
428 | { | |
429 | if (s->s_mds >= mdsc->max_sessions || | |
430 | mdsc->sessions[s->s_mds] != s) | |
431 | return -ENOENT; | |
432 | return 0; | |
433 | } | |
434 | ||
2f2dc053 SW |
435 | /* |
436 | * create+register a new session for given mds. | |
437 | * called under mdsc->mutex. | |
438 | */ | |
439 | static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc, | |
440 | int mds) | |
441 | { | |
442 | struct ceph_mds_session *s; | |
443 | ||
c338c07c NY |
444 | if (mds >= mdsc->mdsmap->m_max_mds) |
445 | return ERR_PTR(-EINVAL); | |
446 | ||
2f2dc053 | 447 | s = kzalloc(sizeof(*s), GFP_NOFS); |
4736b009 DC |
448 | if (!s) |
449 | return ERR_PTR(-ENOMEM); | |
2f2dc053 SW |
450 | s->s_mdsc = mdsc; |
451 | s->s_mds = mds; | |
452 | s->s_state = CEPH_MDS_SESSION_NEW; | |
453 | s->s_ttl = 0; | |
454 | s->s_seq = 0; | |
455 | mutex_init(&s->s_mutex); | |
456 | ||
b7a9e5dd | 457 | ceph_con_init(&s->s_con, s, &mds_con_ops, &mdsc->fsc->client->msgr); |
2f2dc053 | 458 | |
d8fb02ab | 459 | spin_lock_init(&s->s_gen_ttl_lock); |
2f2dc053 | 460 | s->s_cap_gen = 0; |
1ce208a6 | 461 | s->s_cap_ttl = jiffies - 1; |
d8fb02ab AE |
462 | |
463 | spin_lock_init(&s->s_cap_lock); | |
2f2dc053 SW |
464 | s->s_renew_requested = 0; |
465 | s->s_renew_seq = 0; | |
466 | INIT_LIST_HEAD(&s->s_caps); | |
467 | s->s_nr_caps = 0; | |
5dacf091 | 468 | s->s_trim_caps = 0; |
2f2dc053 SW |
469 | atomic_set(&s->s_ref, 1); |
470 | INIT_LIST_HEAD(&s->s_waiting); | |
471 | INIT_LIST_HEAD(&s->s_unsafe); | |
472 | s->s_num_cap_releases = 0; | |
99a9c273 | 473 | s->s_cap_reconnect = 0; |
7c1332b8 | 474 | s->s_cap_iterator = NULL; |
2f2dc053 | 475 | INIT_LIST_HEAD(&s->s_cap_releases); |
2f2dc053 | 476 | INIT_LIST_HEAD(&s->s_cap_flushing); |
2f2dc053 SW |
477 | |
478 | dout("register_session mds%d\n", mds); | |
479 | if (mds >= mdsc->max_sessions) { | |
480 | int newmax = 1 << get_count_order(mds+1); | |
481 | struct ceph_mds_session **sa; | |
482 | ||
483 | dout("register_session realloc to %d\n", newmax); | |
484 | sa = kcalloc(newmax, sizeof(void *), GFP_NOFS); | |
485 | if (sa == NULL) | |
42ce56e5 | 486 | goto fail_realloc; |
2f2dc053 SW |
487 | if (mdsc->sessions) { |
488 | memcpy(sa, mdsc->sessions, | |
489 | mdsc->max_sessions * sizeof(void *)); | |
490 | kfree(mdsc->sessions); | |
491 | } | |
492 | mdsc->sessions = sa; | |
493 | mdsc->max_sessions = newmax; | |
494 | } | |
495 | mdsc->sessions[mds] = s; | |
86d8f67b | 496 | atomic_inc(&mdsc->num_sessions); |
2f2dc053 | 497 | atomic_inc(&s->s_ref); /* one ref to sessions[], one to caller */ |
42ce56e5 | 498 | |
b7a9e5dd SW |
499 | ceph_con_open(&s->s_con, CEPH_ENTITY_TYPE_MDS, mds, |
500 | ceph_mdsmap_get_addr(mdsc->mdsmap, mds)); | |
42ce56e5 | 501 | |
2f2dc053 | 502 | return s; |
42ce56e5 SW |
503 | |
504 | fail_realloc: | |
505 | kfree(s); | |
506 | return ERR_PTR(-ENOMEM); | |
2f2dc053 SW |
507 | } |
508 | ||
509 | /* | |
510 | * called under mdsc->mutex | |
511 | */ | |
2600d2dd | 512 | static void __unregister_session(struct ceph_mds_client *mdsc, |
42ce56e5 | 513 | struct ceph_mds_session *s) |
2f2dc053 | 514 | { |
2600d2dd SW |
515 | dout("__unregister_session mds%d %p\n", s->s_mds, s); |
516 | BUG_ON(mdsc->sessions[s->s_mds] != s); | |
42ce56e5 SW |
517 | mdsc->sessions[s->s_mds] = NULL; |
518 | ceph_con_close(&s->s_con); | |
519 | ceph_put_mds_session(s); | |
86d8f67b | 520 | atomic_dec(&mdsc->num_sessions); |
2f2dc053 SW |
521 | } |
522 | ||
523 | /* | |
524 | * drop session refs in request. | |
525 | * | |
526 | * should be last request ref, or hold mdsc->mutex | |
527 | */ | |
528 | static void put_request_session(struct ceph_mds_request *req) | |
529 | { | |
530 | if (req->r_session) { | |
531 | ceph_put_mds_session(req->r_session); | |
532 | req->r_session = NULL; | |
533 | } | |
534 | } | |
535 | ||
153c8e6b | 536 | void ceph_mdsc_release_request(struct kref *kref) |
2f2dc053 | 537 | { |
153c8e6b SW |
538 | struct ceph_mds_request *req = container_of(kref, |
539 | struct ceph_mds_request, | |
540 | r_kref); | |
54008399 | 541 | destroy_reply_info(&req->r_reply_info); |
153c8e6b SW |
542 | if (req->r_request) |
543 | ceph_msg_put(req->r_request); | |
54008399 | 544 | if (req->r_reply) |
153c8e6b | 545 | ceph_msg_put(req->r_reply); |
153c8e6b | 546 | if (req->r_inode) { |
41b02e1f | 547 | ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN); |
153c8e6b SW |
548 | iput(req->r_inode); |
549 | } | |
550 | if (req->r_locked_dir) | |
41b02e1f | 551 | ceph_put_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN); |
e96a650a | 552 | iput(req->r_target_inode); |
153c8e6b SW |
553 | if (req->r_dentry) |
554 | dput(req->r_dentry); | |
844d87c3 SW |
555 | if (req->r_old_dentry) |
556 | dput(req->r_old_dentry); | |
557 | if (req->r_old_dentry_dir) { | |
41b02e1f SW |
558 | /* |
559 | * track (and drop pins for) r_old_dentry_dir | |
560 | * separately, since r_old_dentry's d_parent may have | |
561 | * changed between the dir mutex being dropped and | |
562 | * this request being freed. | |
563 | */ | |
564 | ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir), | |
565 | CEPH_CAP_PIN); | |
41b02e1f | 566 | iput(req->r_old_dentry_dir); |
2f2dc053 | 567 | } |
153c8e6b SW |
568 | kfree(req->r_path1); |
569 | kfree(req->r_path2); | |
25e6bae3 YZ |
570 | if (req->r_pagelist) |
571 | ceph_pagelist_release(req->r_pagelist); | |
153c8e6b | 572 | put_request_session(req); |
37151668 | 573 | ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation); |
153c8e6b | 574 | kfree(req); |
2f2dc053 SW |
575 | } |
576 | ||
fcd00b68 ID |
577 | DEFINE_RB_FUNCS(request, struct ceph_mds_request, r_tid, r_node) |
578 | ||
2f2dc053 SW |
579 | /* |
580 | * lookup session, bump ref if found. | |
581 | * | |
582 | * called under mdsc->mutex. | |
583 | */ | |
fcd00b68 ID |
584 | static struct ceph_mds_request * |
585 | lookup_get_request(struct ceph_mds_client *mdsc, u64 tid) | |
2f2dc053 SW |
586 | { |
587 | struct ceph_mds_request *req; | |
44ca18f2 | 588 | |
fcd00b68 ID |
589 | req = lookup_request(&mdsc->request_tree, tid); |
590 | if (req) | |
591 | ceph_mdsc_get_request(req); | |
44ca18f2 | 592 | |
fcd00b68 | 593 | return req; |
2f2dc053 SW |
594 | } |
595 | ||
596 | /* | |
597 | * Register an in-flight request, and assign a tid. Link to directory | |
598 | * are modifying (if any). | |
599 | * | |
600 | * Called under mdsc->mutex. | |
601 | */ | |
602 | static void __register_request(struct ceph_mds_client *mdsc, | |
603 | struct ceph_mds_request *req, | |
604 | struct inode *dir) | |
605 | { | |
606 | req->r_tid = ++mdsc->last_tid; | |
607 | if (req->r_num_caps) | |
37151668 YS |
608 | ceph_reserve_caps(mdsc, &req->r_caps_reservation, |
609 | req->r_num_caps); | |
2f2dc053 SW |
610 | dout("__register_request %p tid %lld\n", req, req->r_tid); |
611 | ceph_mdsc_get_request(req); | |
fcd00b68 | 612 | insert_request(&mdsc->request_tree, req); |
2f2dc053 | 613 | |
cb4276cc SW |
614 | req->r_uid = current_fsuid(); |
615 | req->r_gid = current_fsgid(); | |
616 | ||
e8a7b8b1 YZ |
617 | if (mdsc->oldest_tid == 0 && req->r_op != CEPH_MDS_OP_SETFILELOCK) |
618 | mdsc->oldest_tid = req->r_tid; | |
619 | ||
2f2dc053 | 620 | if (dir) { |
3b663780 | 621 | ihold(dir); |
2f2dc053 | 622 | req->r_unsafe_dir = dir; |
2f2dc053 SW |
623 | } |
624 | } | |
625 | ||
626 | static void __unregister_request(struct ceph_mds_client *mdsc, | |
627 | struct ceph_mds_request *req) | |
628 | { | |
629 | dout("__unregister_request %p tid %lld\n", req, req->r_tid); | |
e8a7b8b1 YZ |
630 | |
631 | if (req->r_tid == mdsc->oldest_tid) { | |
632 | struct rb_node *p = rb_next(&req->r_node); | |
633 | mdsc->oldest_tid = 0; | |
634 | while (p) { | |
635 | struct ceph_mds_request *next_req = | |
636 | rb_entry(p, struct ceph_mds_request, r_node); | |
637 | if (next_req->r_op != CEPH_MDS_OP_SETFILELOCK) { | |
638 | mdsc->oldest_tid = next_req->r_tid; | |
639 | break; | |
640 | } | |
641 | p = rb_next(p); | |
642 | } | |
643 | } | |
644 | ||
fcd00b68 | 645 | erase_request(&mdsc->request_tree, req); |
2f2dc053 | 646 | |
4c06ace8 | 647 | if (req->r_unsafe_dir && req->r_got_unsafe) { |
2f2dc053 | 648 | struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir); |
2f2dc053 SW |
649 | spin_lock(&ci->i_unsafe_lock); |
650 | list_del_init(&req->r_unsafe_dir_item); | |
651 | spin_unlock(&ci->i_unsafe_lock); | |
4c06ace8 | 652 | } |
68cd5b4b YZ |
653 | if (req->r_target_inode && req->r_got_unsafe) { |
654 | struct ceph_inode_info *ci = ceph_inode(req->r_target_inode); | |
655 | spin_lock(&ci->i_unsafe_lock); | |
656 | list_del_init(&req->r_unsafe_target_item); | |
657 | spin_unlock(&ci->i_unsafe_lock); | |
658 | } | |
3b663780 | 659 | |
4c06ace8 | 660 | if (req->r_unsafe_dir) { |
3b663780 SW |
661 | iput(req->r_unsafe_dir); |
662 | req->r_unsafe_dir = NULL; | |
2f2dc053 | 663 | } |
94aa8ae1 | 664 | |
fc55d2c9 YZ |
665 | complete_all(&req->r_safe_completion); |
666 | ||
94aa8ae1 | 667 | ceph_mdsc_put_request(req); |
2f2dc053 SW |
668 | } |
669 | ||
30c71233 JL |
670 | /* |
671 | * Walk back up the dentry tree until we hit a dentry representing a | |
672 | * non-snapshot inode. We do this using the rcu_read_lock (which must be held | |
673 | * when calling this) to ensure that the objects won't disappear while we're | |
674 | * working with them. Once we hit a candidate dentry, we attempt to take a | |
675 | * reference to it, and return that as the result. | |
676 | */ | |
677 | static struct inode *get_nonsnap_parent(struct dentry *dentry) { struct inode | |
678 | *inode = NULL; | |
679 | ||
680 | while (dentry && !IS_ROOT(dentry)) { | |
681 | inode = d_inode_rcu(dentry); | |
682 | if (!inode || ceph_snap(inode) == CEPH_NOSNAP) | |
683 | break; | |
684 | dentry = dentry->d_parent; | |
685 | } | |
686 | if (inode) | |
687 | inode = igrab(inode); | |
688 | return inode; | |
689 | } | |
690 | ||
2f2dc053 SW |
691 | /* |
692 | * Choose mds to send request to next. If there is a hint set in the | |
693 | * request (e.g., due to a prior forward hint from the mds), use that. | |
694 | * Otherwise, consult frag tree and/or caps to identify the | |
695 | * appropriate mds. If all else fails, choose randomly. | |
696 | * | |
697 | * Called under mdsc->mutex. | |
698 | */ | |
699 | static int __choose_mds(struct ceph_mds_client *mdsc, | |
700 | struct ceph_mds_request *req) | |
701 | { | |
702 | struct inode *inode; | |
703 | struct ceph_inode_info *ci; | |
704 | struct ceph_cap *cap; | |
705 | int mode = req->r_direct_mode; | |
706 | int mds = -1; | |
707 | u32 hash = req->r_direct_hash; | |
708 | bool is_hash = req->r_direct_is_hash; | |
709 | ||
710 | /* | |
711 | * is there a specific mds we should try? ignore hint if we have | |
712 | * no session and the mds is not up (active or recovering). | |
713 | */ | |
714 | if (req->r_resend_mds >= 0 && | |
715 | (__have_session(mdsc, req->r_resend_mds) || | |
716 | ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) { | |
717 | dout("choose_mds using resend_mds mds%d\n", | |
718 | req->r_resend_mds); | |
719 | return req->r_resend_mds; | |
720 | } | |
721 | ||
722 | if (mode == USE_RANDOM_MDS) | |
723 | goto random; | |
724 | ||
725 | inode = NULL; | |
726 | if (req->r_inode) { | |
727 | inode = req->r_inode; | |
30c71233 | 728 | ihold(inode); |
2f2dc053 | 729 | } else if (req->r_dentry) { |
d79698da | 730 | /* ignore race with rename; old or new d_parent is okay */ |
30c71233 JL |
731 | struct dentry *parent; |
732 | struct inode *dir; | |
733 | ||
734 | rcu_read_lock(); | |
735 | parent = req->r_dentry->d_parent; | |
736 | dir = req->r_locked_dir ? : d_inode_rcu(parent); | |
eb6bb1c5 | 737 | |
30c71233 JL |
738 | if (!dir || dir->i_sb != mdsc->fsc->sb) { |
739 | /* not this fs or parent went negative */ | |
2b0143b5 | 740 | inode = d_inode(req->r_dentry); |
30c71233 JL |
741 | if (inode) |
742 | ihold(inode); | |
eb6bb1c5 SW |
743 | } else if (ceph_snap(dir) != CEPH_NOSNAP) { |
744 | /* direct snapped/virtual snapdir requests | |
745 | * based on parent dir inode */ | |
30c71233 | 746 | inode = get_nonsnap_parent(parent); |
eb6bb1c5 | 747 | dout("__choose_mds using nonsnap parent %p\n", inode); |
ca18bede | 748 | } else { |
eb6bb1c5 | 749 | /* dentry target */ |
2b0143b5 | 750 | inode = d_inode(req->r_dentry); |
ca18bede YZ |
751 | if (!inode || mode == USE_AUTH_MDS) { |
752 | /* dir + name */ | |
30c71233 | 753 | inode = igrab(dir); |
ca18bede YZ |
754 | hash = ceph_dentry_hash(dir, req->r_dentry); |
755 | is_hash = true; | |
30c71233 JL |
756 | } else { |
757 | ihold(inode); | |
ca18bede | 758 | } |
2f2dc053 | 759 | } |
30c71233 | 760 | rcu_read_unlock(); |
2f2dc053 | 761 | } |
eb6bb1c5 | 762 | |
2f2dc053 SW |
763 | dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash, |
764 | (int)hash, mode); | |
765 | if (!inode) | |
766 | goto random; | |
767 | ci = ceph_inode(inode); | |
768 | ||
769 | if (is_hash && S_ISDIR(inode->i_mode)) { | |
770 | struct ceph_inode_frag frag; | |
771 | int found; | |
772 | ||
773 | ceph_choose_frag(ci, hash, &frag, &found); | |
774 | if (found) { | |
775 | if (mode == USE_ANY_MDS && frag.ndist > 0) { | |
776 | u8 r; | |
777 | ||
778 | /* choose a random replica */ | |
779 | get_random_bytes(&r, 1); | |
780 | r %= frag.ndist; | |
781 | mds = frag.dist[r]; | |
782 | dout("choose_mds %p %llx.%llx " | |
783 | "frag %u mds%d (%d/%d)\n", | |
784 | inode, ceph_vinop(inode), | |
d66bbd44 | 785 | frag.frag, mds, |
2f2dc053 | 786 | (int)r, frag.ndist); |
d66bbd44 SW |
787 | if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >= |
788 | CEPH_MDS_STATE_ACTIVE) | |
30c71233 | 789 | goto out; |
2f2dc053 SW |
790 | } |
791 | ||
792 | /* since this file/dir wasn't known to be | |
793 | * replicated, then we want to look for the | |
794 | * authoritative mds. */ | |
795 | mode = USE_AUTH_MDS; | |
796 | if (frag.mds >= 0) { | |
797 | /* choose auth mds */ | |
798 | mds = frag.mds; | |
799 | dout("choose_mds %p %llx.%llx " | |
800 | "frag %u mds%d (auth)\n", | |
801 | inode, ceph_vinop(inode), frag.frag, mds); | |
d66bbd44 SW |
802 | if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >= |
803 | CEPH_MDS_STATE_ACTIVE) | |
30c71233 | 804 | goto out; |
2f2dc053 SW |
805 | } |
806 | } | |
807 | } | |
808 | ||
be655596 | 809 | spin_lock(&ci->i_ceph_lock); |
2f2dc053 SW |
810 | cap = NULL; |
811 | if (mode == USE_AUTH_MDS) | |
812 | cap = ci->i_auth_cap; | |
813 | if (!cap && !RB_EMPTY_ROOT(&ci->i_caps)) | |
814 | cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node); | |
815 | if (!cap) { | |
be655596 | 816 | spin_unlock(&ci->i_ceph_lock); |
30c71233 | 817 | iput(inode); |
2f2dc053 SW |
818 | goto random; |
819 | } | |
820 | mds = cap->session->s_mds; | |
821 | dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n", | |
822 | inode, ceph_vinop(inode), mds, | |
823 | cap == ci->i_auth_cap ? "auth " : "", cap); | |
be655596 | 824 | spin_unlock(&ci->i_ceph_lock); |
30c71233 JL |
825 | out: |
826 | iput(inode); | |
2f2dc053 SW |
827 | return mds; |
828 | ||
829 | random: | |
830 | mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap); | |
831 | dout("choose_mds chose random mds%d\n", mds); | |
832 | return mds; | |
833 | } | |
834 | ||
835 | ||
836 | /* | |
837 | * session messages | |
838 | */ | |
839 | static struct ceph_msg *create_session_msg(u32 op, u64 seq) | |
840 | { | |
841 | struct ceph_msg *msg; | |
842 | struct ceph_mds_session_head *h; | |
843 | ||
b61c2763 SW |
844 | msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS, |
845 | false); | |
a79832f2 | 846 | if (!msg) { |
2f2dc053 | 847 | pr_err("create_session_msg ENOMEM creating msg\n"); |
a79832f2 | 848 | return NULL; |
2f2dc053 SW |
849 | } |
850 | h = msg->front.iov_base; | |
851 | h->op = cpu_to_le32(op); | |
852 | h->seq = cpu_to_le64(seq); | |
dbd0c8bf JS |
853 | |
854 | return msg; | |
855 | } | |
856 | ||
857 | /* | |
858 | * session message, specialization for CEPH_SESSION_REQUEST_OPEN | |
859 | * to include additional client metadata fields. | |
860 | */ | |
861 | static struct ceph_msg *create_session_open_msg(struct ceph_mds_client *mdsc, u64 seq) | |
862 | { | |
863 | struct ceph_msg *msg; | |
864 | struct ceph_mds_session_head *h; | |
865 | int i = -1; | |
866 | int metadata_bytes = 0; | |
867 | int metadata_key_count = 0; | |
868 | struct ceph_options *opt = mdsc->fsc->client->options; | |
3f384954 | 869 | struct ceph_mount_options *fsopt = mdsc->fsc->mount_options; |
dbd0c8bf JS |
870 | void *p; |
871 | ||
a6a5ce4f | 872 | const char* metadata[][2] = { |
dbd0c8bf | 873 | {"hostname", utsname()->nodename}, |
a6a5ce4f | 874 | {"kernel_version", utsname()->release}, |
3f384954 YZ |
875 | {"entity_id", opt->name ? : ""}, |
876 | {"root", fsopt->server_path ? : "/"}, | |
dbd0c8bf JS |
877 | {NULL, NULL} |
878 | }; | |
879 | ||
880 | /* Calculate serialized length of metadata */ | |
881 | metadata_bytes = 4; /* map length */ | |
882 | for (i = 0; metadata[i][0] != NULL; ++i) { | |
883 | metadata_bytes += 8 + strlen(metadata[i][0]) + | |
884 | strlen(metadata[i][1]); | |
885 | metadata_key_count++; | |
886 | } | |
887 | ||
888 | /* Allocate the message */ | |
889 | msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + metadata_bytes, | |
890 | GFP_NOFS, false); | |
891 | if (!msg) { | |
892 | pr_err("create_session_msg ENOMEM creating msg\n"); | |
893 | return NULL; | |
894 | } | |
895 | h = msg->front.iov_base; | |
896 | h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN); | |
897 | h->seq = cpu_to_le64(seq); | |
898 | ||
899 | /* | |
900 | * Serialize client metadata into waiting buffer space, using | |
901 | * the format that userspace expects for map<string, string> | |
7cfa0313 JS |
902 | * |
903 | * ClientSession messages with metadata are v2 | |
dbd0c8bf | 904 | */ |
7cfa0313 JS |
905 | msg->hdr.version = cpu_to_le16(2); |
906 | msg->hdr.compat_version = cpu_to_le16(1); | |
dbd0c8bf JS |
907 | |
908 | /* The write pointer, following the session_head structure */ | |
909 | p = msg->front.iov_base + sizeof(*h); | |
910 | ||
911 | /* Number of entries in the map */ | |
912 | ceph_encode_32(&p, metadata_key_count); | |
913 | ||
914 | /* Two length-prefixed strings for each entry in the map */ | |
915 | for (i = 0; metadata[i][0] != NULL; ++i) { | |
916 | size_t const key_len = strlen(metadata[i][0]); | |
917 | size_t const val_len = strlen(metadata[i][1]); | |
918 | ||
919 | ceph_encode_32(&p, key_len); | |
920 | memcpy(p, metadata[i][0], key_len); | |
921 | p += key_len; | |
922 | ceph_encode_32(&p, val_len); | |
923 | memcpy(p, metadata[i][1], val_len); | |
924 | p += val_len; | |
925 | } | |
926 | ||
2f2dc053 SW |
927 | return msg; |
928 | } | |
929 | ||
930 | /* | |
931 | * send session open request. | |
932 | * | |
933 | * called under mdsc->mutex | |
934 | */ | |
935 | static int __open_session(struct ceph_mds_client *mdsc, | |
936 | struct ceph_mds_session *session) | |
937 | { | |
938 | struct ceph_msg *msg; | |
939 | int mstate; | |
940 | int mds = session->s_mds; | |
2f2dc053 SW |
941 | |
942 | /* wait for mds to go active? */ | |
943 | mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds); | |
944 | dout("open_session to mds%d (%s)\n", mds, | |
945 | ceph_mds_state_name(mstate)); | |
946 | session->s_state = CEPH_MDS_SESSION_OPENING; | |
947 | session->s_renew_requested = jiffies; | |
948 | ||
949 | /* send connect message */ | |
dbd0c8bf | 950 | msg = create_session_open_msg(mdsc, session->s_seq); |
a79832f2 SW |
951 | if (!msg) |
952 | return -ENOMEM; | |
2f2dc053 | 953 | ceph_con_send(&session->s_con, msg); |
2f2dc053 SW |
954 | return 0; |
955 | } | |
956 | ||
ed0552a1 SW |
957 | /* |
958 | * open sessions for any export targets for the given mds | |
959 | * | |
960 | * called under mdsc->mutex | |
961 | */ | |
5d72d13c YZ |
962 | static struct ceph_mds_session * |
963 | __open_export_target_session(struct ceph_mds_client *mdsc, int target) | |
964 | { | |
965 | struct ceph_mds_session *session; | |
966 | ||
967 | session = __ceph_lookup_mds_session(mdsc, target); | |
968 | if (!session) { | |
969 | session = register_session(mdsc, target); | |
970 | if (IS_ERR(session)) | |
971 | return session; | |
972 | } | |
973 | if (session->s_state == CEPH_MDS_SESSION_NEW || | |
974 | session->s_state == CEPH_MDS_SESSION_CLOSING) | |
975 | __open_session(mdsc, session); | |
976 | ||
977 | return session; | |
978 | } | |
979 | ||
980 | struct ceph_mds_session * | |
981 | ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target) | |
982 | { | |
983 | struct ceph_mds_session *session; | |
984 | ||
985 | dout("open_export_target_session to mds%d\n", target); | |
986 | ||
987 | mutex_lock(&mdsc->mutex); | |
988 | session = __open_export_target_session(mdsc, target); | |
989 | mutex_unlock(&mdsc->mutex); | |
990 | ||
991 | return session; | |
992 | } | |
993 | ||
ed0552a1 SW |
994 | static void __open_export_target_sessions(struct ceph_mds_client *mdsc, |
995 | struct ceph_mds_session *session) | |
996 | { | |
997 | struct ceph_mds_info *mi; | |
998 | struct ceph_mds_session *ts; | |
999 | int i, mds = session->s_mds; | |
ed0552a1 SW |
1000 | |
1001 | if (mds >= mdsc->mdsmap->m_max_mds) | |
1002 | return; | |
5d72d13c | 1003 | |
ed0552a1 SW |
1004 | mi = &mdsc->mdsmap->m_info[mds]; |
1005 | dout("open_export_target_sessions for mds%d (%d targets)\n", | |
1006 | session->s_mds, mi->num_export_targets); | |
1007 | ||
1008 | for (i = 0; i < mi->num_export_targets; i++) { | |
5d72d13c YZ |
1009 | ts = __open_export_target_session(mdsc, mi->export_targets[i]); |
1010 | if (!IS_ERR(ts)) | |
1011 | ceph_put_mds_session(ts); | |
ed0552a1 SW |
1012 | } |
1013 | } | |
1014 | ||
154f42c2 SW |
1015 | void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc, |
1016 | struct ceph_mds_session *session) | |
1017 | { | |
1018 | mutex_lock(&mdsc->mutex); | |
1019 | __open_export_target_sessions(mdsc, session); | |
1020 | mutex_unlock(&mdsc->mutex); | |
1021 | } | |
1022 | ||
2f2dc053 SW |
1023 | /* |
1024 | * session caps | |
1025 | */ | |
1026 | ||
745a8e3b YZ |
1027 | /* caller holds s_cap_lock, we drop it */ |
1028 | static void cleanup_cap_releases(struct ceph_mds_client *mdsc, | |
1029 | struct ceph_mds_session *session) | |
1030 | __releases(session->s_cap_lock) | |
2f2dc053 | 1031 | { |
745a8e3b YZ |
1032 | LIST_HEAD(tmp_list); |
1033 | list_splice_init(&session->s_cap_releases, &tmp_list); | |
1034 | session->s_num_cap_releases = 0; | |
1035 | spin_unlock(&session->s_cap_lock); | |
2f2dc053 | 1036 | |
745a8e3b YZ |
1037 | dout("cleanup_cap_releases mds%d\n", session->s_mds); |
1038 | while (!list_empty(&tmp_list)) { | |
1039 | struct ceph_cap *cap; | |
1040 | /* zero out the in-progress message */ | |
1041 | cap = list_first_entry(&tmp_list, | |
1042 | struct ceph_cap, session_caps); | |
1043 | list_del(&cap->session_caps); | |
1044 | ceph_put_cap(mdsc, cap); | |
2f2dc053 | 1045 | } |
2f2dc053 SW |
1046 | } |
1047 | ||
1c841a96 YZ |
1048 | static void cleanup_session_requests(struct ceph_mds_client *mdsc, |
1049 | struct ceph_mds_session *session) | |
1050 | { | |
1051 | struct ceph_mds_request *req; | |
1052 | struct rb_node *p; | |
1053 | ||
1054 | dout("cleanup_session_requests mds%d\n", session->s_mds); | |
1055 | mutex_lock(&mdsc->mutex); | |
1056 | while (!list_empty(&session->s_unsafe)) { | |
1057 | req = list_first_entry(&session->s_unsafe, | |
1058 | struct ceph_mds_request, r_unsafe_item); | |
1059 | list_del_init(&req->r_unsafe_item); | |
3e0708b9 YZ |
1060 | pr_warn_ratelimited(" dropping unsafe request %llu\n", |
1061 | req->r_tid); | |
1c841a96 YZ |
1062 | __unregister_request(mdsc, req); |
1063 | } | |
1064 | /* zero r_attempts, so kick_requests() will re-send requests */ | |
1065 | p = rb_first(&mdsc->request_tree); | |
1066 | while (p) { | |
1067 | req = rb_entry(p, struct ceph_mds_request, r_node); | |
1068 | p = rb_next(p); | |
1069 | if (req->r_session && | |
1070 | req->r_session->s_mds == session->s_mds) | |
1071 | req->r_attempts = 0; | |
1072 | } | |
1073 | mutex_unlock(&mdsc->mutex); | |
1074 | } | |
1075 | ||
2f2dc053 | 1076 | /* |
f818a736 SW |
1077 | * Helper to safely iterate over all caps associated with a session, with |
1078 | * special care taken to handle a racing __ceph_remove_cap(). | |
2f2dc053 | 1079 | * |
f818a736 | 1080 | * Caller must hold session s_mutex. |
2f2dc053 SW |
1081 | */ |
1082 | static int iterate_session_caps(struct ceph_mds_session *session, | |
1083 | int (*cb)(struct inode *, struct ceph_cap *, | |
1084 | void *), void *arg) | |
1085 | { | |
7c1332b8 SW |
1086 | struct list_head *p; |
1087 | struct ceph_cap *cap; | |
1088 | struct inode *inode, *last_inode = NULL; | |
1089 | struct ceph_cap *old_cap = NULL; | |
2f2dc053 SW |
1090 | int ret; |
1091 | ||
1092 | dout("iterate_session_caps %p mds%d\n", session, session->s_mds); | |
1093 | spin_lock(&session->s_cap_lock); | |
7c1332b8 SW |
1094 | p = session->s_caps.next; |
1095 | while (p != &session->s_caps) { | |
1096 | cap = list_entry(p, struct ceph_cap, session_caps); | |
2f2dc053 | 1097 | inode = igrab(&cap->ci->vfs_inode); |
7c1332b8 SW |
1098 | if (!inode) { |
1099 | p = p->next; | |
2f2dc053 | 1100 | continue; |
7c1332b8 SW |
1101 | } |
1102 | session->s_cap_iterator = cap; | |
2f2dc053 | 1103 | spin_unlock(&session->s_cap_lock); |
7c1332b8 SW |
1104 | |
1105 | if (last_inode) { | |
1106 | iput(last_inode); | |
1107 | last_inode = NULL; | |
1108 | } | |
1109 | if (old_cap) { | |
37151668 | 1110 | ceph_put_cap(session->s_mdsc, old_cap); |
7c1332b8 SW |
1111 | old_cap = NULL; |
1112 | } | |
1113 | ||
2f2dc053 | 1114 | ret = cb(inode, cap, arg); |
7c1332b8 SW |
1115 | last_inode = inode; |
1116 | ||
2f2dc053 | 1117 | spin_lock(&session->s_cap_lock); |
7c1332b8 SW |
1118 | p = p->next; |
1119 | if (cap->ci == NULL) { | |
1120 | dout("iterate_session_caps finishing cap %p removal\n", | |
1121 | cap); | |
1122 | BUG_ON(cap->session != session); | |
745a8e3b | 1123 | cap->session = NULL; |
7c1332b8 SW |
1124 | list_del_init(&cap->session_caps); |
1125 | session->s_nr_caps--; | |
745a8e3b YZ |
1126 | if (cap->queue_release) { |
1127 | list_add_tail(&cap->session_caps, | |
1128 | &session->s_cap_releases); | |
1129 | session->s_num_cap_releases++; | |
1130 | } else { | |
1131 | old_cap = cap; /* put_cap it w/o locks held */ | |
1132 | } | |
7c1332b8 | 1133 | } |
5dacf091 SW |
1134 | if (ret < 0) |
1135 | goto out; | |
2f2dc053 | 1136 | } |
5dacf091 SW |
1137 | ret = 0; |
1138 | out: | |
7c1332b8 | 1139 | session->s_cap_iterator = NULL; |
2f2dc053 | 1140 | spin_unlock(&session->s_cap_lock); |
7c1332b8 | 1141 | |
e96a650a | 1142 | iput(last_inode); |
7c1332b8 | 1143 | if (old_cap) |
37151668 | 1144 | ceph_put_cap(session->s_mdsc, old_cap); |
7c1332b8 | 1145 | |
5dacf091 | 1146 | return ret; |
2f2dc053 SW |
1147 | } |
1148 | ||
1149 | static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap, | |
6c99f254 | 1150 | void *arg) |
2f2dc053 | 1151 | { |
6c93df5d | 1152 | struct ceph_fs_client *fsc = (struct ceph_fs_client *)arg; |
2f2dc053 | 1153 | struct ceph_inode_info *ci = ceph_inode(inode); |
553adfd9 | 1154 | LIST_HEAD(to_remove); |
6c93df5d YZ |
1155 | bool drop = false; |
1156 | bool invalidate = false; | |
6c99f254 | 1157 | |
2f2dc053 SW |
1158 | dout("removing cap %p, ci is %p, inode is %p\n", |
1159 | cap, ci, &ci->vfs_inode); | |
be655596 | 1160 | spin_lock(&ci->i_ceph_lock); |
a096b09a | 1161 | __ceph_remove_cap(cap, false); |
571ade33 | 1162 | if (!ci->i_auth_cap) { |
553adfd9 | 1163 | struct ceph_cap_flush *cf; |
6c93df5d | 1164 | struct ceph_mds_client *mdsc = fsc->mdsc; |
6c99f254 | 1165 | |
77310320 YZ |
1166 | ci->i_ceph_flags |= CEPH_I_CAP_DROPPED; |
1167 | ||
6c93df5d YZ |
1168 | if (ci->i_wrbuffer_ref > 0 && |
1169 | ACCESS_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) | |
1170 | invalidate = true; | |
1171 | ||
e4500b5e YZ |
1172 | while (!list_empty(&ci->i_cap_flush_list)) { |
1173 | cf = list_first_entry(&ci->i_cap_flush_list, | |
1174 | struct ceph_cap_flush, i_list); | |
8cdcc07d | 1175 | list_move(&cf->i_list, &to_remove); |
553adfd9 YZ |
1176 | } |
1177 | ||
6c99f254 | 1178 | spin_lock(&mdsc->cap_dirty_lock); |
8310b089 | 1179 | |
e4500b5e YZ |
1180 | list_for_each_entry(cf, &to_remove, i_list) |
1181 | list_del(&cf->g_list); | |
8310b089 | 1182 | |
6c99f254 | 1183 | if (!list_empty(&ci->i_dirty_item)) { |
3e0708b9 YZ |
1184 | pr_warn_ratelimited( |
1185 | " dropping dirty %s state for %p %lld\n", | |
6c99f254 SW |
1186 | ceph_cap_string(ci->i_dirty_caps), |
1187 | inode, ceph_ino(inode)); | |
1188 | ci->i_dirty_caps = 0; | |
1189 | list_del_init(&ci->i_dirty_item); | |
6c93df5d | 1190 | drop = true; |
6c99f254 SW |
1191 | } |
1192 | if (!list_empty(&ci->i_flushing_item)) { | |
3e0708b9 YZ |
1193 | pr_warn_ratelimited( |
1194 | " dropping dirty+flushing %s state for %p %lld\n", | |
6c99f254 SW |
1195 | ceph_cap_string(ci->i_flushing_caps), |
1196 | inode, ceph_ino(inode)); | |
1197 | ci->i_flushing_caps = 0; | |
1198 | list_del_init(&ci->i_flushing_item); | |
1199 | mdsc->num_cap_flushing--; | |
6c93df5d | 1200 | drop = true; |
6c99f254 | 1201 | } |
6c99f254 | 1202 | spin_unlock(&mdsc->cap_dirty_lock); |
553adfd9 | 1203 | |
f66fd9f0 | 1204 | if (!ci->i_dirty_caps && ci->i_prealloc_cap_flush) { |
e4500b5e | 1205 | list_add(&ci->i_prealloc_cap_flush->i_list, &to_remove); |
f66fd9f0 YZ |
1206 | ci->i_prealloc_cap_flush = NULL; |
1207 | } | |
6c99f254 | 1208 | } |
be655596 | 1209 | spin_unlock(&ci->i_ceph_lock); |
553adfd9 YZ |
1210 | while (!list_empty(&to_remove)) { |
1211 | struct ceph_cap_flush *cf; | |
1212 | cf = list_first_entry(&to_remove, | |
e4500b5e YZ |
1213 | struct ceph_cap_flush, i_list); |
1214 | list_del(&cf->i_list); | |
f66fd9f0 | 1215 | ceph_free_cap_flush(cf); |
553adfd9 | 1216 | } |
77310320 YZ |
1217 | |
1218 | wake_up_all(&ci->i_cap_wq); | |
6c93df5d YZ |
1219 | if (invalidate) |
1220 | ceph_queue_invalidate(inode); | |
77310320 | 1221 | if (drop) |
6c99f254 | 1222 | iput(inode); |
2f2dc053 SW |
1223 | return 0; |
1224 | } | |
1225 | ||
1226 | /* | |
1227 | * caller must hold session s_mutex | |
1228 | */ | |
1229 | static void remove_session_caps(struct ceph_mds_session *session) | |
1230 | { | |
6c93df5d YZ |
1231 | struct ceph_fs_client *fsc = session->s_mdsc->fsc; |
1232 | struct super_block *sb = fsc->sb; | |
2f2dc053 | 1233 | dout("remove_session_caps on %p\n", session); |
6c93df5d | 1234 | iterate_session_caps(session, remove_session_caps_cb, fsc); |
6f60f889 | 1235 | |
c8799fc4 YZ |
1236 | wake_up_all(&fsc->mdsc->cap_flushing_wq); |
1237 | ||
6f60f889 YZ |
1238 | spin_lock(&session->s_cap_lock); |
1239 | if (session->s_nr_caps > 0) { | |
6f60f889 YZ |
1240 | struct inode *inode; |
1241 | struct ceph_cap *cap, *prev = NULL; | |
1242 | struct ceph_vino vino; | |
1243 | /* | |
1244 | * iterate_session_caps() skips inodes that are being | |
1245 | * deleted, we need to wait until deletions are complete. | |
1246 | * __wait_on_freeing_inode() is designed for the job, | |
1247 | * but it is not exported, so use lookup inode function | |
1248 | * to access it. | |
1249 | */ | |
1250 | while (!list_empty(&session->s_caps)) { | |
1251 | cap = list_entry(session->s_caps.next, | |
1252 | struct ceph_cap, session_caps); | |
1253 | if (cap == prev) | |
1254 | break; | |
1255 | prev = cap; | |
1256 | vino = cap->ci->i_vino; | |
1257 | spin_unlock(&session->s_cap_lock); | |
1258 | ||
ed284c49 | 1259 | inode = ceph_find_inode(sb, vino); |
6f60f889 YZ |
1260 | iput(inode); |
1261 | ||
1262 | spin_lock(&session->s_cap_lock); | |
1263 | } | |
1264 | } | |
745a8e3b YZ |
1265 | |
1266 | // drop cap expires and unlock s_cap_lock | |
1267 | cleanup_cap_releases(session->s_mdsc, session); | |
6f60f889 | 1268 | |
2f2dc053 | 1269 | BUG_ON(session->s_nr_caps > 0); |
6c99f254 | 1270 | BUG_ON(!list_empty(&session->s_cap_flushing)); |
2f2dc053 SW |
1271 | } |
1272 | ||
1273 | /* | |
1274 | * wake up any threads waiting on this session's caps. if the cap is | |
1275 | * old (didn't get renewed on the client reconnect), remove it now. | |
1276 | * | |
1277 | * caller must hold s_mutex. | |
1278 | */ | |
1279 | static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap, | |
1280 | void *arg) | |
1281 | { | |
0dc2570f SW |
1282 | struct ceph_inode_info *ci = ceph_inode(inode); |
1283 | ||
0dc2570f | 1284 | if (arg) { |
be655596 | 1285 | spin_lock(&ci->i_ceph_lock); |
0dc2570f SW |
1286 | ci->i_wanted_max_size = 0; |
1287 | ci->i_requested_max_size = 0; | |
be655596 | 1288 | spin_unlock(&ci->i_ceph_lock); |
0dc2570f | 1289 | } |
e5360309 | 1290 | wake_up_all(&ci->i_cap_wq); |
2f2dc053 SW |
1291 | return 0; |
1292 | } | |
1293 | ||
0dc2570f SW |
1294 | static void wake_up_session_caps(struct ceph_mds_session *session, |
1295 | int reconnect) | |
2f2dc053 SW |
1296 | { |
1297 | dout("wake_up_session_caps %p mds%d\n", session, session->s_mds); | |
0dc2570f SW |
1298 | iterate_session_caps(session, wake_up_session_cb, |
1299 | (void *)(unsigned long)reconnect); | |
2f2dc053 SW |
1300 | } |
1301 | ||
1302 | /* | |
1303 | * Send periodic message to MDS renewing all currently held caps. The | |
1304 | * ack will reset the expiration for all caps from this session. | |
1305 | * | |
1306 | * caller holds s_mutex | |
1307 | */ | |
1308 | static int send_renew_caps(struct ceph_mds_client *mdsc, | |
1309 | struct ceph_mds_session *session) | |
1310 | { | |
1311 | struct ceph_msg *msg; | |
1312 | int state; | |
1313 | ||
1314 | if (time_after_eq(jiffies, session->s_cap_ttl) && | |
1315 | time_after_eq(session->s_cap_ttl, session->s_renew_requested)) | |
1316 | pr_info("mds%d caps stale\n", session->s_mds); | |
e4cb4cb8 | 1317 | session->s_renew_requested = jiffies; |
2f2dc053 SW |
1318 | |
1319 | /* do not try to renew caps until a recovering mds has reconnected | |
1320 | * with its clients. */ | |
1321 | state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds); | |
1322 | if (state < CEPH_MDS_STATE_RECONNECT) { | |
1323 | dout("send_renew_caps ignoring mds%d (%s)\n", | |
1324 | session->s_mds, ceph_mds_state_name(state)); | |
1325 | return 0; | |
1326 | } | |
1327 | ||
1328 | dout("send_renew_caps to mds%d (%s)\n", session->s_mds, | |
1329 | ceph_mds_state_name(state)); | |
2f2dc053 SW |
1330 | msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS, |
1331 | ++session->s_renew_seq); | |
a79832f2 SW |
1332 | if (!msg) |
1333 | return -ENOMEM; | |
2f2dc053 SW |
1334 | ceph_con_send(&session->s_con, msg); |
1335 | return 0; | |
1336 | } | |
1337 | ||
186e4f7a YZ |
1338 | static int send_flushmsg_ack(struct ceph_mds_client *mdsc, |
1339 | struct ceph_mds_session *session, u64 seq) | |
1340 | { | |
1341 | struct ceph_msg *msg; | |
1342 | ||
1343 | dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n", | |
a687ecaf | 1344 | session->s_mds, ceph_session_state_name(session->s_state), seq); |
186e4f7a YZ |
1345 | msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq); |
1346 | if (!msg) | |
1347 | return -ENOMEM; | |
1348 | ceph_con_send(&session->s_con, msg); | |
1349 | return 0; | |
1350 | } | |
1351 | ||
1352 | ||
2f2dc053 SW |
1353 | /* |
1354 | * Note new cap ttl, and any transition from stale -> not stale (fresh?). | |
0dc2570f SW |
1355 | * |
1356 | * Called under session->s_mutex | |
2f2dc053 SW |
1357 | */ |
1358 | static void renewed_caps(struct ceph_mds_client *mdsc, | |
1359 | struct ceph_mds_session *session, int is_renew) | |
1360 | { | |
1361 | int was_stale; | |
1362 | int wake = 0; | |
1363 | ||
1364 | spin_lock(&session->s_cap_lock); | |
1ce208a6 | 1365 | was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl); |
2f2dc053 SW |
1366 | |
1367 | session->s_cap_ttl = session->s_renew_requested + | |
1368 | mdsc->mdsmap->m_session_timeout*HZ; | |
1369 | ||
1370 | if (was_stale) { | |
1371 | if (time_before(jiffies, session->s_cap_ttl)) { | |
1372 | pr_info("mds%d caps renewed\n", session->s_mds); | |
1373 | wake = 1; | |
1374 | } else { | |
1375 | pr_info("mds%d caps still stale\n", session->s_mds); | |
1376 | } | |
1377 | } | |
1378 | dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n", | |
1379 | session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh", | |
1380 | time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh"); | |
1381 | spin_unlock(&session->s_cap_lock); | |
1382 | ||
1383 | if (wake) | |
0dc2570f | 1384 | wake_up_session_caps(session, 0); |
2f2dc053 SW |
1385 | } |
1386 | ||
1387 | /* | |
1388 | * send a session close request | |
1389 | */ | |
1390 | static int request_close_session(struct ceph_mds_client *mdsc, | |
1391 | struct ceph_mds_session *session) | |
1392 | { | |
1393 | struct ceph_msg *msg; | |
2f2dc053 SW |
1394 | |
1395 | dout("request_close_session mds%d state %s seq %lld\n", | |
a687ecaf | 1396 | session->s_mds, ceph_session_state_name(session->s_state), |
2f2dc053 SW |
1397 | session->s_seq); |
1398 | msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq); | |
a79832f2 SW |
1399 | if (!msg) |
1400 | return -ENOMEM; | |
1401 | ceph_con_send(&session->s_con, msg); | |
fcff415c | 1402 | return 1; |
2f2dc053 SW |
1403 | } |
1404 | ||
1405 | /* | |
1406 | * Called with s_mutex held. | |
1407 | */ | |
1408 | static int __close_session(struct ceph_mds_client *mdsc, | |
1409 | struct ceph_mds_session *session) | |
1410 | { | |
1411 | if (session->s_state >= CEPH_MDS_SESSION_CLOSING) | |
1412 | return 0; | |
1413 | session->s_state = CEPH_MDS_SESSION_CLOSING; | |
1414 | return request_close_session(mdsc, session); | |
1415 | } | |
1416 | ||
1417 | /* | |
1418 | * Trim old(er) caps. | |
1419 | * | |
1420 | * Because we can't cache an inode without one or more caps, we do | |
1421 | * this indirectly: if a cap is unused, we prune its aliases, at which | |
1422 | * point the inode will hopefully get dropped to. | |
1423 | * | |
1424 | * Yes, this is a bit sloppy. Our only real goal here is to respond to | |
1425 | * memory pressure from the MDS, though, so it needn't be perfect. | |
1426 | */ | |
1427 | static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg) | |
1428 | { | |
1429 | struct ceph_mds_session *session = arg; | |
1430 | struct ceph_inode_info *ci = ceph_inode(inode); | |
979abfdd | 1431 | int used, wanted, oissued, mine; |
2f2dc053 SW |
1432 | |
1433 | if (session->s_trim_caps <= 0) | |
1434 | return -1; | |
1435 | ||
be655596 | 1436 | spin_lock(&ci->i_ceph_lock); |
2f2dc053 SW |
1437 | mine = cap->issued | cap->implemented; |
1438 | used = __ceph_caps_used(ci); | |
979abfdd | 1439 | wanted = __ceph_caps_file_wanted(ci); |
2f2dc053 SW |
1440 | oissued = __ceph_caps_issued_other(ci, cap); |
1441 | ||
979abfdd | 1442 | dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n", |
2f2dc053 | 1443 | inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued), |
979abfdd YZ |
1444 | ceph_cap_string(used), ceph_cap_string(wanted)); |
1445 | if (cap == ci->i_auth_cap) { | |
622f3e25 YZ |
1446 | if (ci->i_dirty_caps || ci->i_flushing_caps || |
1447 | !list_empty(&ci->i_cap_snaps)) | |
979abfdd YZ |
1448 | goto out; |
1449 | if ((used | wanted) & CEPH_CAP_ANY_WR) | |
1450 | goto out; | |
1451 | } | |
5e804ac4 YZ |
1452 | /* The inode has cached pages, but it's no longer used. |
1453 | * we can safely drop it */ | |
1454 | if (wanted == 0 && used == CEPH_CAP_FILE_CACHE && | |
1455 | !(oissued & CEPH_CAP_FILE_CACHE)) { | |
1456 | used = 0; | |
1457 | oissued = 0; | |
1458 | } | |
979abfdd | 1459 | if ((used | wanted) & ~oissued & mine) |
2f2dc053 SW |
1460 | goto out; /* we need these caps */ |
1461 | ||
1462 | session->s_trim_caps--; | |
1463 | if (oissued) { | |
1464 | /* we aren't the only cap.. just remove us */ | |
a096b09a | 1465 | __ceph_remove_cap(cap, true); |
2f2dc053 | 1466 | } else { |
5e804ac4 | 1467 | /* try dropping referring dentries */ |
be655596 | 1468 | spin_unlock(&ci->i_ceph_lock); |
2f2dc053 SW |
1469 | d_prune_aliases(inode); |
1470 | dout("trim_caps_cb %p cap %p pruned, count now %d\n", | |
1471 | inode, cap, atomic_read(&inode->i_count)); | |
1472 | return 0; | |
1473 | } | |
1474 | ||
1475 | out: | |
be655596 | 1476 | spin_unlock(&ci->i_ceph_lock); |
2f2dc053 SW |
1477 | return 0; |
1478 | } | |
1479 | ||
1480 | /* | |
1481 | * Trim session cap count down to some max number. | |
1482 | */ | |
1483 | static int trim_caps(struct ceph_mds_client *mdsc, | |
1484 | struct ceph_mds_session *session, | |
1485 | int max_caps) | |
1486 | { | |
1487 | int trim_caps = session->s_nr_caps - max_caps; | |
1488 | ||
1489 | dout("trim_caps mds%d start: %d / %d, trim %d\n", | |
1490 | session->s_mds, session->s_nr_caps, max_caps, trim_caps); | |
1491 | if (trim_caps > 0) { | |
1492 | session->s_trim_caps = trim_caps; | |
1493 | iterate_session_caps(session, trim_caps_cb, session); | |
1494 | dout("trim_caps mds%d done: %d / %d, trimmed %d\n", | |
1495 | session->s_mds, session->s_nr_caps, max_caps, | |
1496 | trim_caps - session->s_trim_caps); | |
5dacf091 | 1497 | session->s_trim_caps = 0; |
2f2dc053 | 1498 | } |
a56371d9 | 1499 | |
a56371d9 | 1500 | ceph_send_cap_releases(mdsc, session); |
2f2dc053 SW |
1501 | return 0; |
1502 | } | |
1503 | ||
8310b089 YZ |
1504 | static int check_caps_flush(struct ceph_mds_client *mdsc, |
1505 | u64 want_flush_tid) | |
1506 | { | |
8310b089 YZ |
1507 | int ret = 1; |
1508 | ||
1509 | spin_lock(&mdsc->cap_dirty_lock); | |
e4500b5e YZ |
1510 | if (!list_empty(&mdsc->cap_flush_list)) { |
1511 | struct ceph_cap_flush *cf = | |
1512 | list_first_entry(&mdsc->cap_flush_list, | |
1513 | struct ceph_cap_flush, g_list); | |
1514 | if (cf->tid <= want_flush_tid) { | |
1515 | dout("check_caps_flush still flushing tid " | |
1516 | "%llu <= %llu\n", cf->tid, want_flush_tid); | |
1517 | ret = 0; | |
1518 | } | |
8310b089 YZ |
1519 | } |
1520 | spin_unlock(&mdsc->cap_dirty_lock); | |
1521 | return ret; | |
d3383a8e YZ |
1522 | } |
1523 | ||
2f2dc053 SW |
1524 | /* |
1525 | * flush all dirty inode data to disk. | |
1526 | * | |
8310b089 | 1527 | * returns true if we've flushed through want_flush_tid |
2f2dc053 | 1528 | */ |
affbc19a | 1529 | static void wait_caps_flush(struct ceph_mds_client *mdsc, |
0e294387 | 1530 | u64 want_flush_tid) |
2f2dc053 | 1531 | { |
0e294387 | 1532 | dout("check_caps_flush want %llu\n", want_flush_tid); |
8310b089 YZ |
1533 | |
1534 | wait_event(mdsc->cap_flushing_wq, | |
1535 | check_caps_flush(mdsc, want_flush_tid)); | |
1536 | ||
1537 | dout("check_caps_flush ok, flushed thru %llu\n", want_flush_tid); | |
2f2dc053 SW |
1538 | } |
1539 | ||
1540 | /* | |
1541 | * called under s_mutex | |
1542 | */ | |
3d7ded4d SW |
1543 | void ceph_send_cap_releases(struct ceph_mds_client *mdsc, |
1544 | struct ceph_mds_session *session) | |
2f2dc053 | 1545 | { |
745a8e3b YZ |
1546 | struct ceph_msg *msg = NULL; |
1547 | struct ceph_mds_cap_release *head; | |
1548 | struct ceph_mds_cap_item *item; | |
1549 | struct ceph_cap *cap; | |
1550 | LIST_HEAD(tmp_list); | |
1551 | int num_cap_releases; | |
2f2dc053 | 1552 | |
0f8605f2 | 1553 | spin_lock(&session->s_cap_lock); |
745a8e3b YZ |
1554 | again: |
1555 | list_splice_init(&session->s_cap_releases, &tmp_list); | |
1556 | num_cap_releases = session->s_num_cap_releases; | |
1557 | session->s_num_cap_releases = 0; | |
2f2dc053 | 1558 | spin_unlock(&session->s_cap_lock); |
e01a5946 | 1559 | |
745a8e3b YZ |
1560 | while (!list_empty(&tmp_list)) { |
1561 | if (!msg) { | |
1562 | msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, | |
09cbfeaf | 1563 | PAGE_SIZE, GFP_NOFS, false); |
745a8e3b YZ |
1564 | if (!msg) |
1565 | goto out_err; | |
1566 | head = msg->front.iov_base; | |
1567 | head->num = cpu_to_le32(0); | |
1568 | msg->front.iov_len = sizeof(*head); | |
1569 | } | |
1570 | cap = list_first_entry(&tmp_list, struct ceph_cap, | |
1571 | session_caps); | |
1572 | list_del(&cap->session_caps); | |
1573 | num_cap_releases--; | |
e01a5946 | 1574 | |
00bd8edb | 1575 | head = msg->front.iov_base; |
745a8e3b YZ |
1576 | le32_add_cpu(&head->num, 1); |
1577 | item = msg->front.iov_base + msg->front.iov_len; | |
1578 | item->ino = cpu_to_le64(cap->cap_ino); | |
1579 | item->cap_id = cpu_to_le64(cap->cap_id); | |
1580 | item->migrate_seq = cpu_to_le32(cap->mseq); | |
1581 | item->seq = cpu_to_le32(cap->issue_seq); | |
1582 | msg->front.iov_len += sizeof(*item); | |
1583 | ||
1584 | ceph_put_cap(mdsc, cap); | |
1585 | ||
1586 | if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) { | |
1587 | msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); | |
1588 | dout("send_cap_releases mds%d %p\n", session->s_mds, msg); | |
1589 | ceph_con_send(&session->s_con, msg); | |
1590 | msg = NULL; | |
1591 | } | |
00bd8edb | 1592 | } |
e01a5946 | 1593 | |
745a8e3b | 1594 | BUG_ON(num_cap_releases != 0); |
e01a5946 | 1595 | |
745a8e3b YZ |
1596 | spin_lock(&session->s_cap_lock); |
1597 | if (!list_empty(&session->s_cap_releases)) | |
1598 | goto again; | |
1599 | spin_unlock(&session->s_cap_lock); | |
1600 | ||
1601 | if (msg) { | |
1602 | msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); | |
1603 | dout("send_cap_releases mds%d %p\n", session->s_mds, msg); | |
1604 | ceph_con_send(&session->s_con, msg); | |
e01a5946 | 1605 | } |
745a8e3b YZ |
1606 | return; |
1607 | out_err: | |
1608 | pr_err("send_cap_releases mds%d, failed to allocate message\n", | |
1609 | session->s_mds); | |
1610 | spin_lock(&session->s_cap_lock); | |
1611 | list_splice(&tmp_list, &session->s_cap_releases); | |
1612 | session->s_num_cap_releases += num_cap_releases; | |
1613 | spin_unlock(&session->s_cap_lock); | |
e01a5946 SW |
1614 | } |
1615 | ||
2f2dc053 SW |
1616 | /* |
1617 | * requests | |
1618 | */ | |
1619 | ||
54008399 YZ |
1620 | int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req, |
1621 | struct inode *dir) | |
1622 | { | |
1623 | struct ceph_inode_info *ci = ceph_inode(dir); | |
1624 | struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info; | |
1625 | struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options; | |
2a5beea3 | 1626 | size_t size = sizeof(struct ceph_mds_reply_dir_entry); |
54008399 YZ |
1627 | int order, num_entries; |
1628 | ||
1629 | spin_lock(&ci->i_ceph_lock); | |
1630 | num_entries = ci->i_files + ci->i_subdirs; | |
1631 | spin_unlock(&ci->i_ceph_lock); | |
1632 | num_entries = max(num_entries, 1); | |
1633 | num_entries = min(num_entries, opt->max_readdir); | |
1634 | ||
1635 | order = get_order(size * num_entries); | |
1636 | while (order >= 0) { | |
2a5beea3 YZ |
1637 | rinfo->dir_entries = (void*)__get_free_pages(GFP_KERNEL | |
1638 | __GFP_NOWARN, | |
1639 | order); | |
1640 | if (rinfo->dir_entries) | |
54008399 YZ |
1641 | break; |
1642 | order--; | |
1643 | } | |
2a5beea3 | 1644 | if (!rinfo->dir_entries) |
54008399 YZ |
1645 | return -ENOMEM; |
1646 | ||
1647 | num_entries = (PAGE_SIZE << order) / size; | |
1648 | num_entries = min(num_entries, opt->max_readdir); | |
1649 | ||
1650 | rinfo->dir_buf_size = PAGE_SIZE << order; | |
1651 | req->r_num_caps = num_entries + 1; | |
1652 | req->r_args.readdir.max_entries = cpu_to_le32(num_entries); | |
1653 | req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes); | |
1654 | return 0; | |
1655 | } | |
1656 | ||
2f2dc053 SW |
1657 | /* |
1658 | * Create an mds request. | |
1659 | */ | |
1660 | struct ceph_mds_request * | |
1661 | ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode) | |
1662 | { | |
1663 | struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS); | |
1664 | ||
1665 | if (!req) | |
1666 | return ERR_PTR(-ENOMEM); | |
1667 | ||
b4556396 | 1668 | mutex_init(&req->r_fill_mutex); |
37151668 | 1669 | req->r_mdsc = mdsc; |
2f2dc053 SW |
1670 | req->r_started = jiffies; |
1671 | req->r_resend_mds = -1; | |
1672 | INIT_LIST_HEAD(&req->r_unsafe_dir_item); | |
68cd5b4b | 1673 | INIT_LIST_HEAD(&req->r_unsafe_target_item); |
2f2dc053 | 1674 | req->r_fmode = -1; |
153c8e6b | 1675 | kref_init(&req->r_kref); |
fcd00b68 | 1676 | RB_CLEAR_NODE(&req->r_node); |
2f2dc053 SW |
1677 | INIT_LIST_HEAD(&req->r_wait); |
1678 | init_completion(&req->r_completion); | |
1679 | init_completion(&req->r_safe_completion); | |
1680 | INIT_LIST_HEAD(&req->r_unsafe_item); | |
1681 | ||
8bbd4714 | 1682 | req->r_stamp = current_fs_time(mdsc->fsc->sb); |
b8e69066 | 1683 | |
2f2dc053 SW |
1684 | req->r_op = op; |
1685 | req->r_direct_mode = mode; | |
1686 | return req; | |
1687 | } | |
1688 | ||
1689 | /* | |
44ca18f2 | 1690 | * return oldest (lowest) request, tid in request tree, 0 if none. |
2f2dc053 SW |
1691 | * |
1692 | * called under mdsc->mutex. | |
1693 | */ | |
44ca18f2 SW |
1694 | static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc) |
1695 | { | |
1696 | if (RB_EMPTY_ROOT(&mdsc->request_tree)) | |
1697 | return NULL; | |
1698 | return rb_entry(rb_first(&mdsc->request_tree), | |
1699 | struct ceph_mds_request, r_node); | |
1700 | } | |
1701 | ||
e8a7b8b1 | 1702 | static inline u64 __get_oldest_tid(struct ceph_mds_client *mdsc) |
2f2dc053 | 1703 | { |
e8a7b8b1 | 1704 | return mdsc->oldest_tid; |
2f2dc053 SW |
1705 | } |
1706 | ||
1707 | /* | |
1708 | * Build a dentry's path. Allocate on heap; caller must kfree. Based | |
1709 | * on build_path_from_dentry in fs/cifs/dir.c. | |
1710 | * | |
1711 | * If @stop_on_nosnap, generate path relative to the first non-snapped | |
1712 | * inode. | |
1713 | * | |
1714 | * Encode hidden .snap dirs as a double /, i.e. | |
1715 | * foo/.snap/bar -> foo//bar | |
1716 | */ | |
1717 | char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base, | |
1718 | int stop_on_nosnap) | |
1719 | { | |
1720 | struct dentry *temp; | |
1721 | char *path; | |
1722 | int len, pos; | |
1b71fe2e | 1723 | unsigned seq; |
2f2dc053 SW |
1724 | |
1725 | if (dentry == NULL) | |
1726 | return ERR_PTR(-EINVAL); | |
1727 | ||
1728 | retry: | |
1729 | len = 0; | |
1b71fe2e AV |
1730 | seq = read_seqbegin(&rename_lock); |
1731 | rcu_read_lock(); | |
2f2dc053 | 1732 | for (temp = dentry; !IS_ROOT(temp);) { |
2b0143b5 | 1733 | struct inode *inode = d_inode(temp); |
2f2dc053 SW |
1734 | if (inode && ceph_snap(inode) == CEPH_SNAPDIR) |
1735 | len++; /* slash only */ | |
1736 | else if (stop_on_nosnap && inode && | |
1737 | ceph_snap(inode) == CEPH_NOSNAP) | |
1738 | break; | |
1739 | else | |
1740 | len += 1 + temp->d_name.len; | |
1741 | temp = temp->d_parent; | |
2f2dc053 | 1742 | } |
1b71fe2e | 1743 | rcu_read_unlock(); |
2f2dc053 SW |
1744 | if (len) |
1745 | len--; /* no leading '/' */ | |
1746 | ||
1747 | path = kmalloc(len+1, GFP_NOFS); | |
1748 | if (path == NULL) | |
1749 | return ERR_PTR(-ENOMEM); | |
1750 | pos = len; | |
1751 | path[pos] = 0; /* trailing null */ | |
1b71fe2e | 1752 | rcu_read_lock(); |
2f2dc053 | 1753 | for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) { |
1b71fe2e | 1754 | struct inode *inode; |
2f2dc053 | 1755 | |
1b71fe2e | 1756 | spin_lock(&temp->d_lock); |
2b0143b5 | 1757 | inode = d_inode(temp); |
2f2dc053 | 1758 | if (inode && ceph_snap(inode) == CEPH_SNAPDIR) { |
104648ad | 1759 | dout("build_path path+%d: %p SNAPDIR\n", |
2f2dc053 SW |
1760 | pos, temp); |
1761 | } else if (stop_on_nosnap && inode && | |
1762 | ceph_snap(inode) == CEPH_NOSNAP) { | |
9d5a09e6 | 1763 | spin_unlock(&temp->d_lock); |
2f2dc053 SW |
1764 | break; |
1765 | } else { | |
1766 | pos -= temp->d_name.len; | |
1b71fe2e AV |
1767 | if (pos < 0) { |
1768 | spin_unlock(&temp->d_lock); | |
2f2dc053 | 1769 | break; |
1b71fe2e | 1770 | } |
2f2dc053 SW |
1771 | strncpy(path + pos, temp->d_name.name, |
1772 | temp->d_name.len); | |
2f2dc053 | 1773 | } |
1b71fe2e | 1774 | spin_unlock(&temp->d_lock); |
2f2dc053 SW |
1775 | if (pos) |
1776 | path[--pos] = '/'; | |
1777 | temp = temp->d_parent; | |
2f2dc053 | 1778 | } |
1b71fe2e AV |
1779 | rcu_read_unlock(); |
1780 | if (pos != 0 || read_seqretry(&rename_lock, seq)) { | |
104648ad | 1781 | pr_err("build_path did not end path lookup where " |
2f2dc053 SW |
1782 | "expected, namelen is %d, pos is %d\n", len, pos); |
1783 | /* presumably this is only possible if racing with a | |
1784 | rename of one of the parent directories (we can not | |
1785 | lock the dentries above us to prevent this, but | |
1786 | retrying should be harmless) */ | |
1787 | kfree(path); | |
1788 | goto retry; | |
1789 | } | |
1790 | ||
2b0143b5 | 1791 | *base = ceph_ino(d_inode(temp)); |
2f2dc053 | 1792 | *plen = len; |
104648ad | 1793 | dout("build_path on %p %d built %llx '%.*s'\n", |
84d08fa8 | 1794 | dentry, d_count(dentry), *base, len, path); |
2f2dc053 SW |
1795 | return path; |
1796 | } | |
1797 | ||
fd36a717 | 1798 | static int build_dentry_path(struct dentry *dentry, struct inode *dir, |
2f2dc053 SW |
1799 | const char **ppath, int *ppathlen, u64 *pino, |
1800 | int *pfreepath) | |
1801 | { | |
1802 | char *path; | |
1803 | ||
c6b0b656 | 1804 | rcu_read_lock(); |
fd36a717 JL |
1805 | if (!dir) |
1806 | dir = d_inode_rcu(dentry->d_parent); | |
c6b0b656 JL |
1807 | if (dir && ceph_snap(dir) == CEPH_NOSNAP) { |
1808 | *pino = ceph_ino(dir); | |
1809 | rcu_read_unlock(); | |
2f2dc053 SW |
1810 | *ppath = dentry->d_name.name; |
1811 | *ppathlen = dentry->d_name.len; | |
1812 | return 0; | |
1813 | } | |
c6b0b656 | 1814 | rcu_read_unlock(); |
2f2dc053 SW |
1815 | path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1); |
1816 | if (IS_ERR(path)) | |
1817 | return PTR_ERR(path); | |
1818 | *ppath = path; | |
1819 | *pfreepath = 1; | |
1820 | return 0; | |
1821 | } | |
1822 | ||
1823 | static int build_inode_path(struct inode *inode, | |
1824 | const char **ppath, int *ppathlen, u64 *pino, | |
1825 | int *pfreepath) | |
1826 | { | |
1827 | struct dentry *dentry; | |
1828 | char *path; | |
1829 | ||
1830 | if (ceph_snap(inode) == CEPH_NOSNAP) { | |
1831 | *pino = ceph_ino(inode); | |
1832 | *ppathlen = 0; | |
1833 | return 0; | |
1834 | } | |
1835 | dentry = d_find_alias(inode); | |
1836 | path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1); | |
1837 | dput(dentry); | |
1838 | if (IS_ERR(path)) | |
1839 | return PTR_ERR(path); | |
1840 | *ppath = path; | |
1841 | *pfreepath = 1; | |
1842 | return 0; | |
1843 | } | |
1844 | ||
1845 | /* | |
1846 | * request arguments may be specified via an inode *, a dentry *, or | |
1847 | * an explicit ino+path. | |
1848 | */ | |
1849 | static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry, | |
fd36a717 JL |
1850 | struct inode *rdiri, const char *rpath, |
1851 | u64 rino, const char **ppath, int *pathlen, | |
2f2dc053 SW |
1852 | u64 *ino, int *freepath) |
1853 | { | |
1854 | int r = 0; | |
1855 | ||
1856 | if (rinode) { | |
1857 | r = build_inode_path(rinode, ppath, pathlen, ino, freepath); | |
1858 | dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode), | |
1859 | ceph_snap(rinode)); | |
1860 | } else if (rdentry) { | |
fd36a717 JL |
1861 | r = build_dentry_path(rdentry, rdiri, ppath, pathlen, ino, |
1862 | freepath); | |
2f2dc053 SW |
1863 | dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen, |
1864 | *ppath); | |
795858db | 1865 | } else if (rpath || rino) { |
2f2dc053 SW |
1866 | *ino = rino; |
1867 | *ppath = rpath; | |
b000056a | 1868 | *pathlen = rpath ? strlen(rpath) : 0; |
2f2dc053 SW |
1869 | dout(" path %.*s\n", *pathlen, rpath); |
1870 | } | |
1871 | ||
1872 | return r; | |
1873 | } | |
1874 | ||
1875 | /* | |
1876 | * called under mdsc->mutex | |
1877 | */ | |
1878 | static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc, | |
1879 | struct ceph_mds_request *req, | |
6e6f0923 | 1880 | int mds, bool drop_cap_releases) |
2f2dc053 SW |
1881 | { |
1882 | struct ceph_msg *msg; | |
1883 | struct ceph_mds_request_head *head; | |
1884 | const char *path1 = NULL; | |
1885 | const char *path2 = NULL; | |
1886 | u64 ino1 = 0, ino2 = 0; | |
1887 | int pathlen1 = 0, pathlen2 = 0; | |
1888 | int freepath1 = 0, freepath2 = 0; | |
1889 | int len; | |
1890 | u16 releases; | |
1891 | void *p, *end; | |
1892 | int ret; | |
1893 | ||
1894 | ret = set_request_path_attr(req->r_inode, req->r_dentry, | |
fd36a717 | 1895 | req->r_locked_dir, req->r_path1, req->r_ino1.ino, |
2f2dc053 SW |
1896 | &path1, &pathlen1, &ino1, &freepath1); |
1897 | if (ret < 0) { | |
1898 | msg = ERR_PTR(ret); | |
1899 | goto out; | |
1900 | } | |
1901 | ||
1902 | ret = set_request_path_attr(NULL, req->r_old_dentry, | |
fd36a717 | 1903 | req->r_old_dentry_dir, |
2f2dc053 SW |
1904 | req->r_path2, req->r_ino2.ino, |
1905 | &path2, &pathlen2, &ino2, &freepath2); | |
1906 | if (ret < 0) { | |
1907 | msg = ERR_PTR(ret); | |
1908 | goto out_free1; | |
1909 | } | |
1910 | ||
1911 | len = sizeof(*head) + | |
b8e69066 | 1912 | pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) + |
777d738a | 1913 | sizeof(struct ceph_timespec); |
2f2dc053 SW |
1914 | |
1915 | /* calculate (max) length for cap releases */ | |
1916 | len += sizeof(struct ceph_mds_request_release) * | |
1917 | (!!req->r_inode_drop + !!req->r_dentry_drop + | |
1918 | !!req->r_old_inode_drop + !!req->r_old_dentry_drop); | |
1919 | if (req->r_dentry_drop) | |
1920 | len += req->r_dentry->d_name.len; | |
1921 | if (req->r_old_dentry_drop) | |
1922 | len += req->r_old_dentry->d_name.len; | |
1923 | ||
b61c2763 | 1924 | msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS, false); |
a79832f2 SW |
1925 | if (!msg) { |
1926 | msg = ERR_PTR(-ENOMEM); | |
2f2dc053 | 1927 | goto out_free2; |
a79832f2 | 1928 | } |
2f2dc053 | 1929 | |
7cfa0313 | 1930 | msg->hdr.version = cpu_to_le16(2); |
6df058c0 SW |
1931 | msg->hdr.tid = cpu_to_le64(req->r_tid); |
1932 | ||
2f2dc053 SW |
1933 | head = msg->front.iov_base; |
1934 | p = msg->front.iov_base + sizeof(*head); | |
1935 | end = msg->front.iov_base + msg->front.iov_len; | |
1936 | ||
1937 | head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch); | |
1938 | head->op = cpu_to_le32(req->r_op); | |
ff3d0046 EB |
1939 | head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid)); |
1940 | head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid)); | |
2f2dc053 SW |
1941 | head->args = req->r_args; |
1942 | ||
1943 | ceph_encode_filepath(&p, end, ino1, path1); | |
1944 | ceph_encode_filepath(&p, end, ino2, path2); | |
1945 | ||
e979cf50 SW |
1946 | /* make note of release offset, in case we need to replay */ |
1947 | req->r_request_release_offset = p - msg->front.iov_base; | |
1948 | ||
2f2dc053 SW |
1949 | /* cap releases */ |
1950 | releases = 0; | |
1951 | if (req->r_inode_drop) | |
1952 | releases += ceph_encode_inode_release(&p, | |
2b0143b5 | 1953 | req->r_inode ? req->r_inode : d_inode(req->r_dentry), |
2f2dc053 SW |
1954 | mds, req->r_inode_drop, req->r_inode_unless, 0); |
1955 | if (req->r_dentry_drop) | |
1956 | releases += ceph_encode_dentry_release(&p, req->r_dentry, | |
1957 | mds, req->r_dentry_drop, req->r_dentry_unless); | |
1958 | if (req->r_old_dentry_drop) | |
1959 | releases += ceph_encode_dentry_release(&p, req->r_old_dentry, | |
1960 | mds, req->r_old_dentry_drop, req->r_old_dentry_unless); | |
1961 | if (req->r_old_inode_drop) | |
1962 | releases += ceph_encode_inode_release(&p, | |
2b0143b5 | 1963 | d_inode(req->r_old_dentry), |
2f2dc053 | 1964 | mds, req->r_old_inode_drop, req->r_old_inode_unless, 0); |
6e6f0923 YZ |
1965 | |
1966 | if (drop_cap_releases) { | |
1967 | releases = 0; | |
1968 | p = msg->front.iov_base + req->r_request_release_offset; | |
1969 | } | |
1970 | ||
2f2dc053 SW |
1971 | head->num_releases = cpu_to_le16(releases); |
1972 | ||
b8e69066 | 1973 | /* time stamp */ |
1f041a89 YZ |
1974 | { |
1975 | struct ceph_timespec ts; | |
1976 | ceph_encode_timespec(&ts, &req->r_stamp); | |
1977 | ceph_encode_copy(&p, &ts, sizeof(ts)); | |
1978 | } | |
b8e69066 | 1979 | |
2f2dc053 SW |
1980 | BUG_ON(p > end); |
1981 | msg->front.iov_len = p - msg->front.iov_base; | |
1982 | msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); | |
1983 | ||
25e6bae3 YZ |
1984 | if (req->r_pagelist) { |
1985 | struct ceph_pagelist *pagelist = req->r_pagelist; | |
1986 | atomic_inc(&pagelist->refcnt); | |
1987 | ceph_msg_data_add_pagelist(msg, pagelist); | |
1988 | msg->hdr.data_len = cpu_to_le32(pagelist->length); | |
1989 | } else { | |
1990 | msg->hdr.data_len = 0; | |
ebf18f47 | 1991 | } |
02afca6c | 1992 | |
2f2dc053 SW |
1993 | msg->hdr.data_off = cpu_to_le16(0); |
1994 | ||
1995 | out_free2: | |
1996 | if (freepath2) | |
1997 | kfree((char *)path2); | |
1998 | out_free1: | |
1999 | if (freepath1) | |
2000 | kfree((char *)path1); | |
2001 | out: | |
2002 | return msg; | |
2003 | } | |
2004 | ||
2005 | /* | |
2006 | * called under mdsc->mutex if error, under no mutex if | |
2007 | * success. | |
2008 | */ | |
2009 | static void complete_request(struct ceph_mds_client *mdsc, | |
2010 | struct ceph_mds_request *req) | |
2011 | { | |
2012 | if (req->r_callback) | |
2013 | req->r_callback(mdsc, req); | |
2014 | else | |
03066f23 | 2015 | complete_all(&req->r_completion); |
2f2dc053 SW |
2016 | } |
2017 | ||
2018 | /* | |
2019 | * called under mdsc->mutex | |
2020 | */ | |
2021 | static int __prepare_send_request(struct ceph_mds_client *mdsc, | |
2022 | struct ceph_mds_request *req, | |
6e6f0923 | 2023 | int mds, bool drop_cap_releases) |
2f2dc053 SW |
2024 | { |
2025 | struct ceph_mds_request_head *rhead; | |
2026 | struct ceph_msg *msg; | |
2027 | int flags = 0; | |
2028 | ||
2f2dc053 | 2029 | req->r_attempts++; |
e55b71f8 GF |
2030 | if (req->r_inode) { |
2031 | struct ceph_cap *cap = | |
2032 | ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds); | |
2033 | ||
2034 | if (cap) | |
2035 | req->r_sent_on_mseq = cap->mseq; | |
2036 | else | |
2037 | req->r_sent_on_mseq = -1; | |
2038 | } | |
2f2dc053 SW |
2039 | dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req, |
2040 | req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts); | |
2041 | ||
01a92f17 | 2042 | if (req->r_got_unsafe) { |
c5c9a0bf | 2043 | void *p; |
01a92f17 SW |
2044 | /* |
2045 | * Replay. Do not regenerate message (and rebuild | |
2046 | * paths, etc.); just use the original message. | |
2047 | * Rebuilding paths will break for renames because | |
2048 | * d_move mangles the src name. | |
2049 | */ | |
2050 | msg = req->r_request; | |
2051 | rhead = msg->front.iov_base; | |
2052 | ||
2053 | flags = le32_to_cpu(rhead->flags); | |
2054 | flags |= CEPH_MDS_FLAG_REPLAY; | |
2055 | rhead->flags = cpu_to_le32(flags); | |
2056 | ||
2057 | if (req->r_target_inode) | |
2058 | rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode)); | |
2059 | ||
2060 | rhead->num_retry = req->r_attempts - 1; | |
e979cf50 SW |
2061 | |
2062 | /* remove cap/dentry releases from message */ | |
2063 | rhead->num_releases = 0; | |
c5c9a0bf YZ |
2064 | |
2065 | /* time stamp */ | |
2066 | p = msg->front.iov_base + req->r_request_release_offset; | |
1f041a89 YZ |
2067 | { |
2068 | struct ceph_timespec ts; | |
2069 | ceph_encode_timespec(&ts, &req->r_stamp); | |
2070 | ceph_encode_copy(&p, &ts, sizeof(ts)); | |
2071 | } | |
c5c9a0bf YZ |
2072 | |
2073 | msg->front.iov_len = p - msg->front.iov_base; | |
2074 | msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); | |
01a92f17 SW |
2075 | return 0; |
2076 | } | |
2077 | ||
2f2dc053 SW |
2078 | if (req->r_request) { |
2079 | ceph_msg_put(req->r_request); | |
2080 | req->r_request = NULL; | |
2081 | } | |
6e6f0923 | 2082 | msg = create_request_message(mdsc, req, mds, drop_cap_releases); |
2f2dc053 | 2083 | if (IS_ERR(msg)) { |
e1518c7c | 2084 | req->r_err = PTR_ERR(msg); |
a79832f2 | 2085 | return PTR_ERR(msg); |
2f2dc053 SW |
2086 | } |
2087 | req->r_request = msg; | |
2088 | ||
2089 | rhead = msg->front.iov_base; | |
2f2dc053 SW |
2090 | rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc)); |
2091 | if (req->r_got_unsafe) | |
2092 | flags |= CEPH_MDS_FLAG_REPLAY; | |
2093 | if (req->r_locked_dir) | |
2094 | flags |= CEPH_MDS_FLAG_WANT_DENTRY; | |
2095 | rhead->flags = cpu_to_le32(flags); | |
2096 | rhead->num_fwd = req->r_num_fwd; | |
2097 | rhead->num_retry = req->r_attempts - 1; | |
01a92f17 | 2098 | rhead->ino = 0; |
2f2dc053 SW |
2099 | |
2100 | dout(" r_locked_dir = %p\n", req->r_locked_dir); | |
2f2dc053 SW |
2101 | return 0; |
2102 | } | |
2103 | ||
2104 | /* | |
2105 | * send request, or put it on the appropriate wait list. | |
2106 | */ | |
2107 | static int __do_request(struct ceph_mds_client *mdsc, | |
2108 | struct ceph_mds_request *req) | |
2109 | { | |
2110 | struct ceph_mds_session *session = NULL; | |
2111 | int mds = -1; | |
48fec5d0 | 2112 | int err = 0; |
2f2dc053 | 2113 | |
eb1b8af3 YZ |
2114 | if (req->r_err || req->r_got_result) { |
2115 | if (req->r_aborted) | |
2116 | __unregister_request(mdsc, req); | |
2f2dc053 | 2117 | goto out; |
eb1b8af3 | 2118 | } |
2f2dc053 SW |
2119 | |
2120 | if (req->r_timeout && | |
2121 | time_after_eq(jiffies, req->r_started + req->r_timeout)) { | |
2122 | dout("do_request timed out\n"); | |
2123 | err = -EIO; | |
2124 | goto finish; | |
2125 | } | |
48fec5d0 YZ |
2126 | if (ACCESS_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) { |
2127 | dout("do_request forced umount\n"); | |
2128 | err = -EIO; | |
2129 | goto finish; | |
2130 | } | |
e9e427f0 YZ |
2131 | if (ACCESS_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_MOUNTING) { |
2132 | if (mdsc->mdsmap_err) { | |
2133 | err = mdsc->mdsmap_err; | |
2134 | dout("do_request mdsmap err %d\n", err); | |
2135 | goto finish; | |
2136 | } | |
cc8e8342 YZ |
2137 | if (mdsc->mdsmap->m_epoch == 0) { |
2138 | dout("do_request no mdsmap, waiting for map\n"); | |
2139 | list_add(&req->r_wait, &mdsc->waiting_for_map); | |
2140 | goto finish; | |
2141 | } | |
e9e427f0 YZ |
2142 | if (!(mdsc->fsc->mount_options->flags & |
2143 | CEPH_MOUNT_OPT_MOUNTWAIT) && | |
2144 | !ceph_mdsmap_is_cluster_available(mdsc->mdsmap)) { | |
2145 | err = -ENOENT; | |
2146 | pr_info("probably no mds server is up\n"); | |
2147 | goto finish; | |
2148 | } | |
2149 | } | |
2f2dc053 | 2150 | |
dc69e2e9 SW |
2151 | put_request_session(req); |
2152 | ||
2f2dc053 SW |
2153 | mds = __choose_mds(mdsc, req); |
2154 | if (mds < 0 || | |
2155 | ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) { | |
2156 | dout("do_request no mds or not active, waiting for map\n"); | |
2157 | list_add(&req->r_wait, &mdsc->waiting_for_map); | |
2158 | goto out; | |
2159 | } | |
2160 | ||
2161 | /* get, open session */ | |
2162 | session = __ceph_lookup_mds_session(mdsc, mds); | |
9c423956 | 2163 | if (!session) { |
2f2dc053 | 2164 | session = register_session(mdsc, mds); |
9c423956 SW |
2165 | if (IS_ERR(session)) { |
2166 | err = PTR_ERR(session); | |
2167 | goto finish; | |
2168 | } | |
2169 | } | |
dc69e2e9 SW |
2170 | req->r_session = get_session(session); |
2171 | ||
2f2dc053 | 2172 | dout("do_request mds%d session %p state %s\n", mds, session, |
a687ecaf | 2173 | ceph_session_state_name(session->s_state)); |
2f2dc053 SW |
2174 | if (session->s_state != CEPH_MDS_SESSION_OPEN && |
2175 | session->s_state != CEPH_MDS_SESSION_HUNG) { | |
fcff415c YZ |
2176 | if (session->s_state == CEPH_MDS_SESSION_REJECTED) { |
2177 | err = -EACCES; | |
2178 | goto out_session; | |
2179 | } | |
2f2dc053 SW |
2180 | if (session->s_state == CEPH_MDS_SESSION_NEW || |
2181 | session->s_state == CEPH_MDS_SESSION_CLOSING) | |
2182 | __open_session(mdsc, session); | |
2183 | list_add(&req->r_wait, &session->s_waiting); | |
2184 | goto out_session; | |
2185 | } | |
2186 | ||
2187 | /* send request */ | |
2f2dc053 SW |
2188 | req->r_resend_mds = -1; /* forget any previous mds hint */ |
2189 | ||
2190 | if (req->r_request_started == 0) /* note request start time */ | |
2191 | req->r_request_started = jiffies; | |
2192 | ||
6e6f0923 | 2193 | err = __prepare_send_request(mdsc, req, mds, false); |
2f2dc053 SW |
2194 | if (!err) { |
2195 | ceph_msg_get(req->r_request); | |
2196 | ceph_con_send(&session->s_con, req->r_request); | |
2197 | } | |
2198 | ||
2199 | out_session: | |
2200 | ceph_put_mds_session(session); | |
48fec5d0 YZ |
2201 | finish: |
2202 | if (err) { | |
2203 | dout("__do_request early error %d\n", err); | |
2204 | req->r_err = err; | |
2205 | complete_request(mdsc, req); | |
2206 | __unregister_request(mdsc, req); | |
2207 | } | |
2f2dc053 SW |
2208 | out: |
2209 | return err; | |
2f2dc053 SW |
2210 | } |
2211 | ||
2212 | /* | |
2213 | * called under mdsc->mutex | |
2214 | */ | |
2215 | static void __wake_requests(struct ceph_mds_client *mdsc, | |
2216 | struct list_head *head) | |
2217 | { | |
ed75ec2c YZ |
2218 | struct ceph_mds_request *req; |
2219 | LIST_HEAD(tmp_list); | |
2220 | ||
2221 | list_splice_init(head, &tmp_list); | |
2f2dc053 | 2222 | |
ed75ec2c YZ |
2223 | while (!list_empty(&tmp_list)) { |
2224 | req = list_entry(tmp_list.next, | |
2225 | struct ceph_mds_request, r_wait); | |
2f2dc053 | 2226 | list_del_init(&req->r_wait); |
7971bd92 | 2227 | dout(" wake request %p tid %llu\n", req, req->r_tid); |
2f2dc053 SW |
2228 | __do_request(mdsc, req); |
2229 | } | |
2230 | } | |
2231 | ||
2232 | /* | |
2233 | * Wake up threads with requests pending for @mds, so that they can | |
29790f26 | 2234 | * resubmit their requests to a possibly different mds. |
2f2dc053 | 2235 | */ |
29790f26 | 2236 | static void kick_requests(struct ceph_mds_client *mdsc, int mds) |
2f2dc053 | 2237 | { |
44ca18f2 | 2238 | struct ceph_mds_request *req; |
282c1052 | 2239 | struct rb_node *p = rb_first(&mdsc->request_tree); |
2f2dc053 SW |
2240 | |
2241 | dout("kick_requests mds%d\n", mds); | |
282c1052 | 2242 | while (p) { |
44ca18f2 | 2243 | req = rb_entry(p, struct ceph_mds_request, r_node); |
282c1052 | 2244 | p = rb_next(p); |
44ca18f2 SW |
2245 | if (req->r_got_unsafe) |
2246 | continue; | |
3de22be6 YZ |
2247 | if (req->r_attempts > 0) |
2248 | continue; /* only new requests */ | |
44ca18f2 SW |
2249 | if (req->r_session && |
2250 | req->r_session->s_mds == mds) { | |
2251 | dout(" kicking tid %llu\n", req->r_tid); | |
03974e81 | 2252 | list_del_init(&req->r_wait); |
44ca18f2 | 2253 | __do_request(mdsc, req); |
2f2dc053 SW |
2254 | } |
2255 | } | |
2256 | } | |
2257 | ||
2258 | void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc, | |
2259 | struct ceph_mds_request *req) | |
2260 | { | |
2261 | dout("submit_request on %p\n", req); | |
2262 | mutex_lock(&mdsc->mutex); | |
2263 | __register_request(mdsc, req, NULL); | |
2264 | __do_request(mdsc, req); | |
2265 | mutex_unlock(&mdsc->mutex); | |
2266 | } | |
2267 | ||
2268 | /* | |
2269 | * Synchrously perform an mds request. Take care of all of the | |
2270 | * session setup, forwarding, retry details. | |
2271 | */ | |
2272 | int ceph_mdsc_do_request(struct ceph_mds_client *mdsc, | |
2273 | struct inode *dir, | |
2274 | struct ceph_mds_request *req) | |
2275 | { | |
2276 | int err; | |
2277 | ||
2278 | dout("do_request on %p\n", req); | |
2279 | ||
2280 | /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */ | |
2281 | if (req->r_inode) | |
2282 | ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN); | |
2283 | if (req->r_locked_dir) | |
2284 | ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN); | |
844d87c3 | 2285 | if (req->r_old_dentry_dir) |
41b02e1f SW |
2286 | ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir), |
2287 | CEPH_CAP_PIN); | |
2f2dc053 SW |
2288 | |
2289 | /* issue */ | |
2290 | mutex_lock(&mdsc->mutex); | |
2291 | __register_request(mdsc, req, dir); | |
2292 | __do_request(mdsc, req); | |
2293 | ||
e1518c7c SW |
2294 | if (req->r_err) { |
2295 | err = req->r_err; | |
e1518c7c | 2296 | goto out; |
2f2dc053 SW |
2297 | } |
2298 | ||
e1518c7c SW |
2299 | /* wait */ |
2300 | mutex_unlock(&mdsc->mutex); | |
2301 | dout("do_request waiting\n"); | |
5be73034 | 2302 | if (!req->r_timeout && req->r_wait_for_completion) { |
9280be24 | 2303 | err = req->r_wait_for_completion(mdsc, req); |
e1518c7c | 2304 | } else { |
5be73034 ID |
2305 | long timeleft = wait_for_completion_killable_timeout( |
2306 | &req->r_completion, | |
2307 | ceph_timeout_jiffies(req->r_timeout)); | |
2308 | if (timeleft > 0) | |
2309 | err = 0; | |
2310 | else if (!timeleft) | |
2311 | err = -EIO; /* timed out */ | |
2312 | else | |
2313 | err = timeleft; /* killed */ | |
e1518c7c SW |
2314 | } |
2315 | dout("do_request waited, got %d\n", err); | |
2316 | mutex_lock(&mdsc->mutex); | |
5b1daecd | 2317 | |
e1518c7c SW |
2318 | /* only abort if we didn't race with a real reply */ |
2319 | if (req->r_got_result) { | |
2320 | err = le32_to_cpu(req->r_reply_info.head->result); | |
2321 | } else if (err < 0) { | |
2322 | dout("aborted request %lld with %d\n", req->r_tid, err); | |
b4556396 SW |
2323 | |
2324 | /* | |
2325 | * ensure we aren't running concurrently with | |
2326 | * ceph_fill_trace or ceph_readdir_prepopulate, which | |
2327 | * rely on locks (dir mutex) held by our caller. | |
2328 | */ | |
2329 | mutex_lock(&req->r_fill_mutex); | |
e1518c7c SW |
2330 | req->r_err = err; |
2331 | req->r_aborted = true; | |
b4556396 | 2332 | mutex_unlock(&req->r_fill_mutex); |
5b1daecd | 2333 | |
e1518c7c | 2334 | if (req->r_locked_dir && |
167c9e35 SW |
2335 | (req->r_op & CEPH_MDS_OP_WRITE)) |
2336 | ceph_invalidate_dir_request(req); | |
2f2dc053 | 2337 | } else { |
e1518c7c | 2338 | err = req->r_err; |
2f2dc053 | 2339 | } |
2f2dc053 | 2340 | |
e1518c7c SW |
2341 | out: |
2342 | mutex_unlock(&mdsc->mutex); | |
2f2dc053 SW |
2343 | dout("do_request %p done, result %d\n", req, err); |
2344 | return err; | |
2345 | } | |
2346 | ||
167c9e35 | 2347 | /* |
2f276c51 | 2348 | * Invalidate dir's completeness, dentry lease state on an aborted MDS |
167c9e35 SW |
2349 | * namespace request. |
2350 | */ | |
2351 | void ceph_invalidate_dir_request(struct ceph_mds_request *req) | |
2352 | { | |
2353 | struct inode *inode = req->r_locked_dir; | |
167c9e35 | 2354 | |
2f276c51 | 2355 | dout("invalidate_dir_request %p (complete, lease(s))\n", inode); |
167c9e35 | 2356 | |
2f276c51 | 2357 | ceph_dir_clear_complete(inode); |
167c9e35 SW |
2358 | if (req->r_dentry) |
2359 | ceph_invalidate_dentry_lease(req->r_dentry); | |
2360 | if (req->r_old_dentry) | |
2361 | ceph_invalidate_dentry_lease(req->r_old_dentry); | |
2362 | } | |
2363 | ||
2f2dc053 SW |
2364 | /* |
2365 | * Handle mds reply. | |
2366 | * | |
2367 | * We take the session mutex and parse and process the reply immediately. | |
2368 | * This preserves the logical ordering of replies, capabilities, etc., sent | |
2369 | * by the MDS as they are applied to our local cache. | |
2370 | */ | |
2371 | static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg) | |
2372 | { | |
2373 | struct ceph_mds_client *mdsc = session->s_mdsc; | |
2374 | struct ceph_mds_request *req; | |
2375 | struct ceph_mds_reply_head *head = msg->front.iov_base; | |
2376 | struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */ | |
982d6011 | 2377 | struct ceph_snap_realm *realm; |
2f2dc053 SW |
2378 | u64 tid; |
2379 | int err, result; | |
2600d2dd | 2380 | int mds = session->s_mds; |
2f2dc053 | 2381 | |
2f2dc053 SW |
2382 | if (msg->front.iov_len < sizeof(*head)) { |
2383 | pr_err("mdsc_handle_reply got corrupt (short) reply\n"); | |
9ec7cab1 | 2384 | ceph_msg_dump(msg); |
2f2dc053 SW |
2385 | return; |
2386 | } | |
2387 | ||
2388 | /* get request, session */ | |
6df058c0 | 2389 | tid = le64_to_cpu(msg->hdr.tid); |
2f2dc053 | 2390 | mutex_lock(&mdsc->mutex); |
fcd00b68 | 2391 | req = lookup_get_request(mdsc, tid); |
2f2dc053 SW |
2392 | if (!req) { |
2393 | dout("handle_reply on unknown tid %llu\n", tid); | |
2394 | mutex_unlock(&mdsc->mutex); | |
2395 | return; | |
2396 | } | |
2397 | dout("handle_reply %p\n", req); | |
2f2dc053 SW |
2398 | |
2399 | /* correct session? */ | |
d96d6049 | 2400 | if (req->r_session != session) { |
2f2dc053 SW |
2401 | pr_err("mdsc_handle_reply got %llu on session mds%d" |
2402 | " not mds%d\n", tid, session->s_mds, | |
2403 | req->r_session ? req->r_session->s_mds : -1); | |
2404 | mutex_unlock(&mdsc->mutex); | |
2405 | goto out; | |
2406 | } | |
2407 | ||
2408 | /* dup? */ | |
2409 | if ((req->r_got_unsafe && !head->safe) || | |
2410 | (req->r_got_safe && head->safe)) { | |
f3ae1b97 | 2411 | pr_warn("got a dup %s reply on %llu from mds%d\n", |
2f2dc053 SW |
2412 | head->safe ? "safe" : "unsafe", tid, mds); |
2413 | mutex_unlock(&mdsc->mutex); | |
2414 | goto out; | |
2415 | } | |
1550d34e | 2416 | if (req->r_got_safe) { |
f3ae1b97 | 2417 | pr_warn("got unsafe after safe on %llu from mds%d\n", |
85792d0d SW |
2418 | tid, mds); |
2419 | mutex_unlock(&mdsc->mutex); | |
2420 | goto out; | |
2421 | } | |
2f2dc053 SW |
2422 | |
2423 | result = le32_to_cpu(head->result); | |
2424 | ||
2425 | /* | |
e55b71f8 GF |
2426 | * Handle an ESTALE |
2427 | * if we're not talking to the authority, send to them | |
2428 | * if the authority has changed while we weren't looking, | |
2429 | * send to new authority | |
2430 | * Otherwise we just have to return an ESTALE | |
2f2dc053 SW |
2431 | */ |
2432 | if (result == -ESTALE) { | |
e55b71f8 | 2433 | dout("got ESTALE on request %llu", req->r_tid); |
51da8e8c | 2434 | req->r_resend_mds = -1; |
ca18bede | 2435 | if (req->r_direct_mode != USE_AUTH_MDS) { |
e55b71f8 GF |
2436 | dout("not using auth, setting for that now"); |
2437 | req->r_direct_mode = USE_AUTH_MDS; | |
2f2dc053 SW |
2438 | __do_request(mdsc, req); |
2439 | mutex_unlock(&mdsc->mutex); | |
2440 | goto out; | |
e55b71f8 | 2441 | } else { |
ca18bede YZ |
2442 | int mds = __choose_mds(mdsc, req); |
2443 | if (mds >= 0 && mds != req->r_session->s_mds) { | |
2444 | dout("but auth changed, so resending"); | |
e55b71f8 GF |
2445 | __do_request(mdsc, req); |
2446 | mutex_unlock(&mdsc->mutex); | |
2447 | goto out; | |
2448 | } | |
2f2dc053 | 2449 | } |
e55b71f8 | 2450 | dout("have to return ESTALE on request %llu", req->r_tid); |
2f2dc053 SW |
2451 | } |
2452 | ||
e55b71f8 | 2453 | |
2f2dc053 SW |
2454 | if (head->safe) { |
2455 | req->r_got_safe = true; | |
2456 | __unregister_request(mdsc, req); | |
2f2dc053 SW |
2457 | |
2458 | if (req->r_got_unsafe) { | |
2459 | /* | |
2460 | * We already handled the unsafe response, now do the | |
2461 | * cleanup. No need to examine the response; the MDS | |
2462 | * doesn't include any result info in the safe | |
2463 | * response. And even if it did, there is nothing | |
2464 | * useful we could do with a revised return value. | |
2465 | */ | |
2466 | dout("got safe reply %llu, mds%d\n", tid, mds); | |
2467 | list_del_init(&req->r_unsafe_item); | |
2468 | ||
2469 | /* last unsafe request during umount? */ | |
44ca18f2 | 2470 | if (mdsc->stopping && !__get_oldest_req(mdsc)) |
03066f23 | 2471 | complete_all(&mdsc->safe_umount_waiters); |
2f2dc053 SW |
2472 | mutex_unlock(&mdsc->mutex); |
2473 | goto out; | |
2474 | } | |
e1518c7c | 2475 | } else { |
2f2dc053 SW |
2476 | req->r_got_unsafe = true; |
2477 | list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe); | |
4c06ace8 YZ |
2478 | if (req->r_unsafe_dir) { |
2479 | struct ceph_inode_info *ci = | |
2480 | ceph_inode(req->r_unsafe_dir); | |
2481 | spin_lock(&ci->i_unsafe_lock); | |
2482 | list_add_tail(&req->r_unsafe_dir_item, | |
2483 | &ci->i_unsafe_dirops); | |
2484 | spin_unlock(&ci->i_unsafe_lock); | |
2485 | } | |
2f2dc053 SW |
2486 | } |
2487 | ||
2488 | dout("handle_reply tid %lld result %d\n", tid, result); | |
2489 | rinfo = &req->r_reply_info; | |
14303d20 | 2490 | err = parse_reply_info(msg, rinfo, session->s_con.peer_features); |
2f2dc053 SW |
2491 | mutex_unlock(&mdsc->mutex); |
2492 | ||
2493 | mutex_lock(&session->s_mutex); | |
2494 | if (err < 0) { | |
25933abd | 2495 | pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid); |
9ec7cab1 | 2496 | ceph_msg_dump(msg); |
2f2dc053 SW |
2497 | goto out_err; |
2498 | } | |
2499 | ||
2500 | /* snap trace */ | |
982d6011 | 2501 | realm = NULL; |
2f2dc053 SW |
2502 | if (rinfo->snapblob_len) { |
2503 | down_write(&mdsc->snap_rwsem); | |
2504 | ceph_update_snap_trace(mdsc, rinfo->snapblob, | |
982d6011 YZ |
2505 | rinfo->snapblob + rinfo->snapblob_len, |
2506 | le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP, | |
2507 | &realm); | |
2f2dc053 SW |
2508 | downgrade_write(&mdsc->snap_rwsem); |
2509 | } else { | |
2510 | down_read(&mdsc->snap_rwsem); | |
2511 | } | |
2512 | ||
2513 | /* insert trace into our cache */ | |
b4556396 | 2514 | mutex_lock(&req->r_fill_mutex); |
315f2408 | 2515 | current->journal_info = req; |
3d14c5d2 | 2516 | err = ceph_fill_trace(mdsc->fsc->sb, req, req->r_session); |
2f2dc053 | 2517 | if (err == 0) { |
6e8575fa | 2518 | if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR || |
81c6aea5 | 2519 | req->r_op == CEPH_MDS_OP_LSSNAP)) |
2f2dc053 | 2520 | ceph_readdir_prepopulate(req, req->r_session); |
37151668 | 2521 | ceph_unreserve_caps(mdsc, &req->r_caps_reservation); |
2f2dc053 | 2522 | } |
315f2408 | 2523 | current->journal_info = NULL; |
b4556396 | 2524 | mutex_unlock(&req->r_fill_mutex); |
2f2dc053 SW |
2525 | |
2526 | up_read(&mdsc->snap_rwsem); | |
982d6011 YZ |
2527 | if (realm) |
2528 | ceph_put_snap_realm(mdsc, realm); | |
68cd5b4b YZ |
2529 | |
2530 | if (err == 0 && req->r_got_unsafe && req->r_target_inode) { | |
2531 | struct ceph_inode_info *ci = ceph_inode(req->r_target_inode); | |
2532 | spin_lock(&ci->i_unsafe_lock); | |
2533 | list_add_tail(&req->r_unsafe_target_item, &ci->i_unsafe_iops); | |
2534 | spin_unlock(&ci->i_unsafe_lock); | |
2535 | } | |
2f2dc053 | 2536 | out_err: |
e1518c7c SW |
2537 | mutex_lock(&mdsc->mutex); |
2538 | if (!req->r_aborted) { | |
2539 | if (err) { | |
2540 | req->r_err = err; | |
2541 | } else { | |
5fdb1389 | 2542 | req->r_reply = ceph_msg_get(msg); |
e1518c7c SW |
2543 | req->r_got_result = true; |
2544 | } | |
2f2dc053 | 2545 | } else { |
e1518c7c | 2546 | dout("reply arrived after request %lld was aborted\n", tid); |
2f2dc053 | 2547 | } |
e1518c7c | 2548 | mutex_unlock(&mdsc->mutex); |
2f2dc053 | 2549 | |
2f2dc053 SW |
2550 | mutex_unlock(&session->s_mutex); |
2551 | ||
2552 | /* kick calling process */ | |
2553 | complete_request(mdsc, req); | |
2554 | out: | |
2555 | ceph_mdsc_put_request(req); | |
2556 | return; | |
2557 | } | |
2558 | ||
2559 | ||
2560 | ||
2561 | /* | |
2562 | * handle mds notification that our request has been forwarded. | |
2563 | */ | |
2600d2dd SW |
2564 | static void handle_forward(struct ceph_mds_client *mdsc, |
2565 | struct ceph_mds_session *session, | |
2566 | struct ceph_msg *msg) | |
2f2dc053 SW |
2567 | { |
2568 | struct ceph_mds_request *req; | |
a1ea787c | 2569 | u64 tid = le64_to_cpu(msg->hdr.tid); |
2f2dc053 SW |
2570 | u32 next_mds; |
2571 | u32 fwd_seq; | |
2f2dc053 SW |
2572 | int err = -EINVAL; |
2573 | void *p = msg->front.iov_base; | |
2574 | void *end = p + msg->front.iov_len; | |
2f2dc053 | 2575 | |
a1ea787c | 2576 | ceph_decode_need(&p, end, 2*sizeof(u32), bad); |
c89136ea SW |
2577 | next_mds = ceph_decode_32(&p); |
2578 | fwd_seq = ceph_decode_32(&p); | |
2f2dc053 SW |
2579 | |
2580 | mutex_lock(&mdsc->mutex); | |
fcd00b68 | 2581 | req = lookup_get_request(mdsc, tid); |
2f2dc053 | 2582 | if (!req) { |
2a8e5e36 | 2583 | dout("forward tid %llu to mds%d - req dne\n", tid, next_mds); |
2f2dc053 SW |
2584 | goto out; /* dup reply? */ |
2585 | } | |
2586 | ||
2a8e5e36 SW |
2587 | if (req->r_aborted) { |
2588 | dout("forward tid %llu aborted, unregistering\n", tid); | |
2589 | __unregister_request(mdsc, req); | |
2590 | } else if (fwd_seq <= req->r_num_fwd) { | |
2591 | dout("forward tid %llu to mds%d - old seq %d <= %d\n", | |
2f2dc053 SW |
2592 | tid, next_mds, req->r_num_fwd, fwd_seq); |
2593 | } else { | |
2594 | /* resend. forward race not possible; mds would drop */ | |
2a8e5e36 SW |
2595 | dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds); |
2596 | BUG_ON(req->r_err); | |
2597 | BUG_ON(req->r_got_result); | |
3de22be6 | 2598 | req->r_attempts = 0; |
2f2dc053 SW |
2599 | req->r_num_fwd = fwd_seq; |
2600 | req->r_resend_mds = next_mds; | |
2601 | put_request_session(req); | |
2602 | __do_request(mdsc, req); | |
2603 | } | |
2604 | ceph_mdsc_put_request(req); | |
2605 | out: | |
2606 | mutex_unlock(&mdsc->mutex); | |
2607 | return; | |
2608 | ||
2609 | bad: | |
2610 | pr_err("mdsc_handle_forward decode error err=%d\n", err); | |
2611 | } | |
2612 | ||
2613 | /* | |
2614 | * handle a mds session control message | |
2615 | */ | |
2616 | static void handle_session(struct ceph_mds_session *session, | |
2617 | struct ceph_msg *msg) | |
2618 | { | |
2619 | struct ceph_mds_client *mdsc = session->s_mdsc; | |
2620 | u32 op; | |
2621 | u64 seq; | |
2600d2dd | 2622 | int mds = session->s_mds; |
2f2dc053 SW |
2623 | struct ceph_mds_session_head *h = msg->front.iov_base; |
2624 | int wake = 0; | |
2625 | ||
2f2dc053 SW |
2626 | /* decode */ |
2627 | if (msg->front.iov_len != sizeof(*h)) | |
2628 | goto bad; | |
2629 | op = le32_to_cpu(h->op); | |
2630 | seq = le64_to_cpu(h->seq); | |
2631 | ||
2632 | mutex_lock(&mdsc->mutex); | |
2600d2dd SW |
2633 | if (op == CEPH_SESSION_CLOSE) |
2634 | __unregister_session(mdsc, session); | |
2f2dc053 SW |
2635 | /* FIXME: this ttl calculation is generous */ |
2636 | session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose; | |
2637 | mutex_unlock(&mdsc->mutex); | |
2638 | ||
2639 | mutex_lock(&session->s_mutex); | |
2640 | ||
2641 | dout("handle_session mds%d %s %p state %s seq %llu\n", | |
2642 | mds, ceph_session_op_name(op), session, | |
a687ecaf | 2643 | ceph_session_state_name(session->s_state), seq); |
2f2dc053 SW |
2644 | |
2645 | if (session->s_state == CEPH_MDS_SESSION_HUNG) { | |
2646 | session->s_state = CEPH_MDS_SESSION_OPEN; | |
2647 | pr_info("mds%d came back\n", session->s_mds); | |
2648 | } | |
2649 | ||
2650 | switch (op) { | |
2651 | case CEPH_SESSION_OPEN: | |
29790f26 SW |
2652 | if (session->s_state == CEPH_MDS_SESSION_RECONNECTING) |
2653 | pr_info("mds%d reconnect success\n", session->s_mds); | |
2f2dc053 SW |
2654 | session->s_state = CEPH_MDS_SESSION_OPEN; |
2655 | renewed_caps(mdsc, session, 0); | |
2656 | wake = 1; | |
2657 | if (mdsc->stopping) | |
2658 | __close_session(mdsc, session); | |
2659 | break; | |
2660 | ||
2661 | case CEPH_SESSION_RENEWCAPS: | |
2662 | if (session->s_renew_seq == seq) | |
2663 | renewed_caps(mdsc, session, 1); | |
2664 | break; | |
2665 | ||
2666 | case CEPH_SESSION_CLOSE: | |
29790f26 SW |
2667 | if (session->s_state == CEPH_MDS_SESSION_RECONNECTING) |
2668 | pr_info("mds%d reconnect denied\n", session->s_mds); | |
1c841a96 | 2669 | cleanup_session_requests(mdsc, session); |
2f2dc053 | 2670 | remove_session_caps(session); |
656e4382 | 2671 | wake = 2; /* for good measure */ |
f3c60c59 | 2672 | wake_up_all(&mdsc->session_close_wq); |
2f2dc053 SW |
2673 | break; |
2674 | ||
2675 | case CEPH_SESSION_STALE: | |
2676 | pr_info("mds%d caps went stale, renewing\n", | |
2677 | session->s_mds); | |
d8fb02ab | 2678 | spin_lock(&session->s_gen_ttl_lock); |
2f2dc053 | 2679 | session->s_cap_gen++; |
1ce208a6 | 2680 | session->s_cap_ttl = jiffies - 1; |
d8fb02ab | 2681 | spin_unlock(&session->s_gen_ttl_lock); |
2f2dc053 SW |
2682 | send_renew_caps(mdsc, session); |
2683 | break; | |
2684 | ||
2685 | case CEPH_SESSION_RECALL_STATE: | |
2686 | trim_caps(mdsc, session, le32_to_cpu(h->max_caps)); | |
2687 | break; | |
2688 | ||
186e4f7a YZ |
2689 | case CEPH_SESSION_FLUSHMSG: |
2690 | send_flushmsg_ack(mdsc, session, seq); | |
2691 | break; | |
2692 | ||
03f4fcb0 YZ |
2693 | case CEPH_SESSION_FORCE_RO: |
2694 | dout("force_session_readonly %p\n", session); | |
2695 | spin_lock(&session->s_cap_lock); | |
2696 | session->s_readonly = true; | |
2697 | spin_unlock(&session->s_cap_lock); | |
2698 | wake_up_session_caps(session, 0); | |
2699 | break; | |
2700 | ||
fcff415c YZ |
2701 | case CEPH_SESSION_REJECT: |
2702 | WARN_ON(session->s_state != CEPH_MDS_SESSION_OPENING); | |
2703 | pr_info("mds%d rejected session\n", session->s_mds); | |
2704 | session->s_state = CEPH_MDS_SESSION_REJECTED; | |
2705 | cleanup_session_requests(mdsc, session); | |
2706 | remove_session_caps(session); | |
2707 | wake = 2; /* for good measure */ | |
2708 | break; | |
2709 | ||
2f2dc053 SW |
2710 | default: |
2711 | pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds); | |
2712 | WARN_ON(1); | |
2713 | } | |
2714 | ||
2715 | mutex_unlock(&session->s_mutex); | |
2716 | if (wake) { | |
2717 | mutex_lock(&mdsc->mutex); | |
2718 | __wake_requests(mdsc, &session->s_waiting); | |
656e4382 YZ |
2719 | if (wake == 2) |
2720 | kick_requests(mdsc, mds); | |
2f2dc053 SW |
2721 | mutex_unlock(&mdsc->mutex); |
2722 | } | |
2723 | return; | |
2724 | ||
2725 | bad: | |
2726 | pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds, | |
2727 | (int)msg->front.iov_len); | |
9ec7cab1 | 2728 | ceph_msg_dump(msg); |
2f2dc053 SW |
2729 | return; |
2730 | } | |
2731 | ||
2732 | ||
2733 | /* | |
2734 | * called under session->mutex. | |
2735 | */ | |
2736 | static void replay_unsafe_requests(struct ceph_mds_client *mdsc, | |
2737 | struct ceph_mds_session *session) | |
2738 | { | |
2739 | struct ceph_mds_request *req, *nreq; | |
3de22be6 | 2740 | struct rb_node *p; |
2f2dc053 SW |
2741 | int err; |
2742 | ||
2743 | dout("replay_unsafe_requests mds%d\n", session->s_mds); | |
2744 | ||
2745 | mutex_lock(&mdsc->mutex); | |
2746 | list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) { | |
6e6f0923 | 2747 | err = __prepare_send_request(mdsc, req, session->s_mds, true); |
2f2dc053 SW |
2748 | if (!err) { |
2749 | ceph_msg_get(req->r_request); | |
2750 | ceph_con_send(&session->s_con, req->r_request); | |
2751 | } | |
2752 | } | |
3de22be6 YZ |
2753 | |
2754 | /* | |
2755 | * also re-send old requests when MDS enters reconnect stage. So that MDS | |
2756 | * can process completed request in clientreplay stage. | |
2757 | */ | |
2758 | p = rb_first(&mdsc->request_tree); | |
2759 | while (p) { | |
2760 | req = rb_entry(p, struct ceph_mds_request, r_node); | |
2761 | p = rb_next(p); | |
2762 | if (req->r_got_unsafe) | |
2763 | continue; | |
2764 | if (req->r_attempts == 0) | |
2765 | continue; /* only old requests */ | |
2766 | if (req->r_session && | |
2767 | req->r_session->s_mds == session->s_mds) { | |
6e6f0923 YZ |
2768 | err = __prepare_send_request(mdsc, req, |
2769 | session->s_mds, true); | |
3de22be6 YZ |
2770 | if (!err) { |
2771 | ceph_msg_get(req->r_request); | |
2772 | ceph_con_send(&session->s_con, req->r_request); | |
2773 | } | |
2774 | } | |
2775 | } | |
2f2dc053 SW |
2776 | mutex_unlock(&mdsc->mutex); |
2777 | } | |
2778 | ||
2779 | /* | |
2780 | * Encode information about a cap for a reconnect with the MDS. | |
2781 | */ | |
2f2dc053 SW |
2782 | static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap, |
2783 | void *arg) | |
2784 | { | |
20cb34ae SW |
2785 | union { |
2786 | struct ceph_mds_cap_reconnect v2; | |
2787 | struct ceph_mds_cap_reconnect_v1 v1; | |
2788 | } rec; | |
2f2dc053 | 2789 | struct ceph_inode_info *ci; |
20cb34ae SW |
2790 | struct ceph_reconnect_state *recon_state = arg; |
2791 | struct ceph_pagelist *pagelist = recon_state->pagelist; | |
2f2dc053 SW |
2792 | char *path; |
2793 | int pathlen, err; | |
2794 | u64 pathbase; | |
3469ed0d | 2795 | u64 snap_follows; |
2f2dc053 SW |
2796 | struct dentry *dentry; |
2797 | ||
2798 | ci = cap->ci; | |
2799 | ||
2800 | dout(" adding %p ino %llx.%llx cap %p %lld %s\n", | |
2801 | inode, ceph_vinop(inode), cap, cap->cap_id, | |
2802 | ceph_cap_string(cap->issued)); | |
93cea5be SW |
2803 | err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode)); |
2804 | if (err) | |
2805 | return err; | |
2f2dc053 SW |
2806 | |
2807 | dentry = d_find_alias(inode); | |
2808 | if (dentry) { | |
2809 | path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0); | |
2810 | if (IS_ERR(path)) { | |
2811 | err = PTR_ERR(path); | |
e072f8aa | 2812 | goto out_dput; |
2f2dc053 SW |
2813 | } |
2814 | } else { | |
2815 | path = NULL; | |
2816 | pathlen = 0; | |
4eacd4cb | 2817 | pathbase = 0; |
2f2dc053 | 2818 | } |
2f2dc053 | 2819 | |
be655596 | 2820 | spin_lock(&ci->i_ceph_lock); |
2f2dc053 SW |
2821 | cap->seq = 0; /* reset cap seq */ |
2822 | cap->issue_seq = 0; /* and issue_seq */ | |
667ca05c | 2823 | cap->mseq = 0; /* and migrate_seq */ |
99a9c273 | 2824 | cap->cap_gen = cap->session->s_cap_gen; |
20cb34ae | 2825 | |
121f22a1 | 2826 | if (recon_state->msg_version >= 2) { |
20cb34ae SW |
2827 | rec.v2.cap_id = cpu_to_le64(cap->cap_id); |
2828 | rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci)); | |
2829 | rec.v2.issued = cpu_to_le32(cap->issued); | |
2830 | rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino); | |
2831 | rec.v2.pathbase = cpu_to_le64(pathbase); | |
2832 | rec.v2.flock_len = 0; | |
20cb34ae SW |
2833 | } else { |
2834 | rec.v1.cap_id = cpu_to_le64(cap->cap_id); | |
2835 | rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci)); | |
2836 | rec.v1.issued = cpu_to_le32(cap->issued); | |
2837 | rec.v1.size = cpu_to_le64(inode->i_size); | |
2838 | ceph_encode_timespec(&rec.v1.mtime, &inode->i_mtime); | |
2839 | ceph_encode_timespec(&rec.v1.atime, &inode->i_atime); | |
2840 | rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino); | |
2841 | rec.v1.pathbase = cpu_to_le64(pathbase); | |
20cb34ae | 2842 | } |
3469ed0d YZ |
2843 | |
2844 | if (list_empty(&ci->i_cap_snaps)) { | |
2845 | snap_follows = 0; | |
2846 | } else { | |
2847 | struct ceph_cap_snap *capsnap = | |
2848 | list_first_entry(&ci->i_cap_snaps, | |
2849 | struct ceph_cap_snap, ci_item); | |
2850 | snap_follows = capsnap->follows; | |
20cb34ae | 2851 | } |
be655596 | 2852 | spin_unlock(&ci->i_ceph_lock); |
2f2dc053 | 2853 | |
121f22a1 | 2854 | if (recon_state->msg_version >= 2) { |
40819f6f | 2855 | int num_fcntl_locks, num_flock_locks; |
39be95e9 | 2856 | struct ceph_filelock *flocks; |
121f22a1 YZ |
2857 | size_t struct_len, total_len = 0; |
2858 | u8 struct_v = 0; | |
39be95e9 JS |
2859 | |
2860 | encode_again: | |
39be95e9 | 2861 | ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks); |
39be95e9 JS |
2862 | flocks = kmalloc((num_fcntl_locks+num_flock_locks) * |
2863 | sizeof(struct ceph_filelock), GFP_NOFS); | |
2864 | if (!flocks) { | |
2865 | err = -ENOMEM; | |
2866 | goto out_free; | |
2867 | } | |
39be95e9 JS |
2868 | err = ceph_encode_locks_to_buffer(inode, flocks, |
2869 | num_fcntl_locks, | |
2870 | num_flock_locks); | |
39be95e9 JS |
2871 | if (err) { |
2872 | kfree(flocks); | |
2873 | if (err == -ENOSPC) | |
2874 | goto encode_again; | |
2875 | goto out_free; | |
2876 | } | |
121f22a1 YZ |
2877 | |
2878 | if (recon_state->msg_version >= 3) { | |
2879 | /* version, compat_version and struct_len */ | |
2880 | total_len = 2 * sizeof(u8) + sizeof(u32); | |
3469ed0d | 2881 | struct_v = 2; |
121f22a1 | 2882 | } |
39be95e9 JS |
2883 | /* |
2884 | * number of encoded locks is stable, so copy to pagelist | |
2885 | */ | |
121f22a1 YZ |
2886 | struct_len = 2 * sizeof(u32) + |
2887 | (num_fcntl_locks + num_flock_locks) * | |
2888 | sizeof(struct ceph_filelock); | |
2889 | rec.v2.flock_len = cpu_to_le32(struct_len); | |
2890 | ||
2891 | struct_len += sizeof(rec.v2); | |
2892 | struct_len += sizeof(u32) + pathlen; | |
2893 | ||
3469ed0d YZ |
2894 | if (struct_v >= 2) |
2895 | struct_len += sizeof(u64); /* snap_follows */ | |
2896 | ||
121f22a1 YZ |
2897 | total_len += struct_len; |
2898 | err = ceph_pagelist_reserve(pagelist, total_len); | |
2899 | ||
2900 | if (!err) { | |
2901 | if (recon_state->msg_version >= 3) { | |
2902 | ceph_pagelist_encode_8(pagelist, struct_v); | |
2903 | ceph_pagelist_encode_8(pagelist, 1); | |
2904 | ceph_pagelist_encode_32(pagelist, struct_len); | |
2905 | } | |
2906 | ceph_pagelist_encode_string(pagelist, path, pathlen); | |
2907 | ceph_pagelist_append(pagelist, &rec, sizeof(rec.v2)); | |
2908 | ceph_locks_to_pagelist(flocks, pagelist, | |
2909 | num_fcntl_locks, | |
2910 | num_flock_locks); | |
3469ed0d YZ |
2911 | if (struct_v >= 2) |
2912 | ceph_pagelist_encode_64(pagelist, snap_follows); | |
121f22a1 | 2913 | } |
39be95e9 | 2914 | kfree(flocks); |
3612abbd | 2915 | } else { |
121f22a1 YZ |
2916 | size_t size = sizeof(u32) + pathlen + sizeof(rec.v1); |
2917 | err = ceph_pagelist_reserve(pagelist, size); | |
2918 | if (!err) { | |
2919 | ceph_pagelist_encode_string(pagelist, path, pathlen); | |
2920 | ceph_pagelist_append(pagelist, &rec, sizeof(rec.v1)); | |
2921 | } | |
40819f6f | 2922 | } |
44c99757 YZ |
2923 | |
2924 | recon_state->nr_caps++; | |
e072f8aa | 2925 | out_free: |
2f2dc053 | 2926 | kfree(path); |
e072f8aa | 2927 | out_dput: |
2f2dc053 | 2928 | dput(dentry); |
93cea5be | 2929 | return err; |
2f2dc053 SW |
2930 | } |
2931 | ||
2932 | ||
2933 | /* | |
2934 | * If an MDS fails and recovers, clients need to reconnect in order to | |
2935 | * reestablish shared state. This includes all caps issued through | |
2936 | * this session _and_ the snap_realm hierarchy. Because it's not | |
2937 | * clear which snap realms the mds cares about, we send everything we | |
2938 | * know about.. that ensures we'll then get any new info the | |
2939 | * recovering MDS might have. | |
2940 | * | |
2941 | * This is a relatively heavyweight operation, but it's rare. | |
2942 | * | |
2943 | * called with mdsc->mutex held. | |
2944 | */ | |
34b6c855 SW |
2945 | static void send_mds_reconnect(struct ceph_mds_client *mdsc, |
2946 | struct ceph_mds_session *session) | |
2f2dc053 | 2947 | { |
2f2dc053 | 2948 | struct ceph_msg *reply; |
a105f00c | 2949 | struct rb_node *p; |
34b6c855 | 2950 | int mds = session->s_mds; |
9abf82b8 | 2951 | int err = -ENOMEM; |
44c99757 | 2952 | int s_nr_caps; |
93cea5be | 2953 | struct ceph_pagelist *pagelist; |
20cb34ae | 2954 | struct ceph_reconnect_state recon_state; |
2f2dc053 | 2955 | |
34b6c855 | 2956 | pr_info("mds%d reconnect start\n", mds); |
2f2dc053 | 2957 | |
93cea5be SW |
2958 | pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS); |
2959 | if (!pagelist) | |
2960 | goto fail_nopagelist; | |
2961 | ceph_pagelist_init(pagelist); | |
2962 | ||
b61c2763 | 2963 | reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS, false); |
a79832f2 | 2964 | if (!reply) |
93cea5be | 2965 | goto fail_nomsg; |
93cea5be | 2966 | |
34b6c855 SW |
2967 | mutex_lock(&session->s_mutex); |
2968 | session->s_state = CEPH_MDS_SESSION_RECONNECTING; | |
2969 | session->s_seq = 0; | |
2f2dc053 | 2970 | |
2f2dc053 | 2971 | dout("session %p state %s\n", session, |
a687ecaf | 2972 | ceph_session_state_name(session->s_state)); |
2f2dc053 | 2973 | |
99a9c273 YZ |
2974 | spin_lock(&session->s_gen_ttl_lock); |
2975 | session->s_cap_gen++; | |
2976 | spin_unlock(&session->s_gen_ttl_lock); | |
2977 | ||
2978 | spin_lock(&session->s_cap_lock); | |
03f4fcb0 YZ |
2979 | /* don't know if session is readonly */ |
2980 | session->s_readonly = 0; | |
99a9c273 YZ |
2981 | /* |
2982 | * notify __ceph_remove_cap() that we are composing cap reconnect. | |
2983 | * If a cap get released before being added to the cap reconnect, | |
2984 | * __ceph_remove_cap() should skip queuing cap release. | |
2985 | */ | |
2986 | session->s_cap_reconnect = 1; | |
e01a5946 | 2987 | /* drop old cap expires; we're about to reestablish that state */ |
745a8e3b | 2988 | cleanup_cap_releases(mdsc, session); |
e01a5946 | 2989 | |
5d23371f | 2990 | /* trim unused caps to reduce MDS's cache rejoin time */ |
c0bd50e2 YZ |
2991 | if (mdsc->fsc->sb->s_root) |
2992 | shrink_dcache_parent(mdsc->fsc->sb->s_root); | |
5d23371f YZ |
2993 | |
2994 | ceph_con_close(&session->s_con); | |
2995 | ceph_con_open(&session->s_con, | |
2996 | CEPH_ENTITY_TYPE_MDS, mds, | |
2997 | ceph_mdsmap_get_addr(mdsc->mdsmap, mds)); | |
2998 | ||
2999 | /* replay unsafe requests */ | |
3000 | replay_unsafe_requests(mdsc, session); | |
3001 | ||
3002 | down_read(&mdsc->snap_rwsem); | |
3003 | ||
2f2dc053 | 3004 | /* traverse this session's caps */ |
44c99757 YZ |
3005 | s_nr_caps = session->s_nr_caps; |
3006 | err = ceph_pagelist_encode_32(pagelist, s_nr_caps); | |
93cea5be SW |
3007 | if (err) |
3008 | goto fail; | |
20cb34ae | 3009 | |
44c99757 | 3010 | recon_state.nr_caps = 0; |
20cb34ae | 3011 | recon_state.pagelist = pagelist; |
121f22a1 YZ |
3012 | if (session->s_con.peer_features & CEPH_FEATURE_MDSENC) |
3013 | recon_state.msg_version = 3; | |
3014 | else if (session->s_con.peer_features & CEPH_FEATURE_FLOCK) | |
3015 | recon_state.msg_version = 2; | |
3016 | else | |
3017 | recon_state.msg_version = 1; | |
20cb34ae | 3018 | err = iterate_session_caps(session, encode_caps_cb, &recon_state); |
2f2dc053 | 3019 | if (err < 0) |
9abf82b8 | 3020 | goto fail; |
2f2dc053 | 3021 | |
99a9c273 YZ |
3022 | spin_lock(&session->s_cap_lock); |
3023 | session->s_cap_reconnect = 0; | |
3024 | spin_unlock(&session->s_cap_lock); | |
3025 | ||
2f2dc053 SW |
3026 | /* |
3027 | * snaprealms. we provide mds with the ino, seq (version), and | |
3028 | * parent for all of our realms. If the mds has any newer info, | |
3029 | * it will tell us. | |
3030 | */ | |
a105f00c SW |
3031 | for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) { |
3032 | struct ceph_snap_realm *realm = | |
3033 | rb_entry(p, struct ceph_snap_realm, node); | |
93cea5be | 3034 | struct ceph_mds_snaprealm_reconnect sr_rec; |
2f2dc053 SW |
3035 | |
3036 | dout(" adding snap realm %llx seq %lld parent %llx\n", | |
3037 | realm->ino, realm->seq, realm->parent_ino); | |
93cea5be SW |
3038 | sr_rec.ino = cpu_to_le64(realm->ino); |
3039 | sr_rec.seq = cpu_to_le64(realm->seq); | |
3040 | sr_rec.parent = cpu_to_le64(realm->parent_ino); | |
3041 | err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec)); | |
3042 | if (err) | |
3043 | goto fail; | |
2f2dc053 | 3044 | } |
2f2dc053 | 3045 | |
121f22a1 | 3046 | reply->hdr.version = cpu_to_le16(recon_state.msg_version); |
44c99757 YZ |
3047 | |
3048 | /* raced with cap release? */ | |
3049 | if (s_nr_caps != recon_state.nr_caps) { | |
3050 | struct page *page = list_first_entry(&pagelist->head, | |
3051 | struct page, lru); | |
3052 | __le32 *addr = kmap_atomic(page); | |
3053 | *addr = cpu_to_le32(recon_state.nr_caps); | |
3054 | kunmap_atomic(addr); | |
ebf18f47 | 3055 | } |
44c99757 YZ |
3056 | |
3057 | reply->hdr.data_len = cpu_to_le32(pagelist->length); | |
3058 | ceph_msg_data_add_pagelist(reply, pagelist); | |
e548e9b9 YZ |
3059 | |
3060 | ceph_early_kick_flushing_caps(mdsc, session); | |
3061 | ||
2f2dc053 SW |
3062 | ceph_con_send(&session->s_con, reply); |
3063 | ||
9abf82b8 SW |
3064 | mutex_unlock(&session->s_mutex); |
3065 | ||
3066 | mutex_lock(&mdsc->mutex); | |
3067 | __wake_requests(mdsc, &session->s_waiting); | |
3068 | mutex_unlock(&mdsc->mutex); | |
3069 | ||
2f2dc053 | 3070 | up_read(&mdsc->snap_rwsem); |
2f2dc053 SW |
3071 | return; |
3072 | ||
93cea5be | 3073 | fail: |
2f2dc053 | 3074 | ceph_msg_put(reply); |
9abf82b8 SW |
3075 | up_read(&mdsc->snap_rwsem); |
3076 | mutex_unlock(&session->s_mutex); | |
93cea5be SW |
3077 | fail_nomsg: |
3078 | ceph_pagelist_release(pagelist); | |
93cea5be | 3079 | fail_nopagelist: |
9abf82b8 | 3080 | pr_err("error %d preparing reconnect for mds%d\n", err, mds); |
9abf82b8 | 3081 | return; |
2f2dc053 SW |
3082 | } |
3083 | ||
3084 | ||
3085 | /* | |
3086 | * compare old and new mdsmaps, kicking requests | |
3087 | * and closing out old connections as necessary | |
3088 | * | |
3089 | * called under mdsc->mutex. | |
3090 | */ | |
3091 | static void check_new_map(struct ceph_mds_client *mdsc, | |
3092 | struct ceph_mdsmap *newmap, | |
3093 | struct ceph_mdsmap *oldmap) | |
3094 | { | |
3095 | int i; | |
3096 | int oldstate, newstate; | |
3097 | struct ceph_mds_session *s; | |
3098 | ||
3099 | dout("check_new_map new %u old %u\n", | |
3100 | newmap->m_epoch, oldmap->m_epoch); | |
3101 | ||
3102 | for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) { | |
3103 | if (mdsc->sessions[i] == NULL) | |
3104 | continue; | |
3105 | s = mdsc->sessions[i]; | |
3106 | oldstate = ceph_mdsmap_get_state(oldmap, i); | |
3107 | newstate = ceph_mdsmap_get_state(newmap, i); | |
3108 | ||
0deb01c9 | 3109 | dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n", |
2f2dc053 | 3110 | i, ceph_mds_state_name(oldstate), |
0deb01c9 | 3111 | ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "", |
2f2dc053 | 3112 | ceph_mds_state_name(newstate), |
0deb01c9 | 3113 | ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "", |
a687ecaf | 3114 | ceph_session_state_name(s->s_state)); |
2f2dc053 | 3115 | |
3e8f43a0 YZ |
3116 | if (i >= newmap->m_max_mds || |
3117 | memcmp(ceph_mdsmap_get_addr(oldmap, i), | |
2f2dc053 SW |
3118 | ceph_mdsmap_get_addr(newmap, i), |
3119 | sizeof(struct ceph_entity_addr))) { | |
3120 | if (s->s_state == CEPH_MDS_SESSION_OPENING) { | |
3121 | /* the session never opened, just close it | |
3122 | * out now */ | |
3123 | __wake_requests(mdsc, &s->s_waiting); | |
2600d2dd | 3124 | __unregister_session(mdsc, s); |
2f2dc053 SW |
3125 | } else { |
3126 | /* just close it */ | |
3127 | mutex_unlock(&mdsc->mutex); | |
3128 | mutex_lock(&s->s_mutex); | |
3129 | mutex_lock(&mdsc->mutex); | |
3130 | ceph_con_close(&s->s_con); | |
3131 | mutex_unlock(&s->s_mutex); | |
3132 | s->s_state = CEPH_MDS_SESSION_RESTARTING; | |
3133 | } | |
2f2dc053 SW |
3134 | } else if (oldstate == newstate) { |
3135 | continue; /* nothing new with this mds */ | |
3136 | } | |
3137 | ||
3138 | /* | |
3139 | * send reconnect? | |
3140 | */ | |
3141 | if (s->s_state == CEPH_MDS_SESSION_RESTARTING && | |
34b6c855 SW |
3142 | newstate >= CEPH_MDS_STATE_RECONNECT) { |
3143 | mutex_unlock(&mdsc->mutex); | |
3144 | send_mds_reconnect(mdsc, s); | |
3145 | mutex_lock(&mdsc->mutex); | |
3146 | } | |
2f2dc053 SW |
3147 | |
3148 | /* | |
29790f26 | 3149 | * kick request on any mds that has gone active. |
2f2dc053 SW |
3150 | */ |
3151 | if (oldstate < CEPH_MDS_STATE_ACTIVE && | |
3152 | newstate >= CEPH_MDS_STATE_ACTIVE) { | |
29790f26 SW |
3153 | if (oldstate != CEPH_MDS_STATE_CREATING && |
3154 | oldstate != CEPH_MDS_STATE_STARTING) | |
3155 | pr_info("mds%d recovery completed\n", s->s_mds); | |
3156 | kick_requests(mdsc, i); | |
2f2dc053 | 3157 | ceph_kick_flushing_caps(mdsc, s); |
0dc2570f | 3158 | wake_up_session_caps(s, 1); |
2f2dc053 SW |
3159 | } |
3160 | } | |
cb170a22 SW |
3161 | |
3162 | for (i = 0; i < newmap->m_max_mds && i < mdsc->max_sessions; i++) { | |
3163 | s = mdsc->sessions[i]; | |
3164 | if (!s) | |
3165 | continue; | |
3166 | if (!ceph_mdsmap_is_laggy(newmap, i)) | |
3167 | continue; | |
3168 | if (s->s_state == CEPH_MDS_SESSION_OPEN || | |
3169 | s->s_state == CEPH_MDS_SESSION_HUNG || | |
3170 | s->s_state == CEPH_MDS_SESSION_CLOSING) { | |
3171 | dout(" connecting to export targets of laggy mds%d\n", | |
3172 | i); | |
3173 | __open_export_target_sessions(mdsc, s); | |
3174 | } | |
3175 | } | |
2f2dc053 SW |
3176 | } |
3177 | ||
3178 | ||
3179 | ||
3180 | /* | |
3181 | * leases | |
3182 | */ | |
3183 | ||
3184 | /* | |
3185 | * caller must hold session s_mutex, dentry->d_lock | |
3186 | */ | |
3187 | void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry) | |
3188 | { | |
3189 | struct ceph_dentry_info *di = ceph_dentry(dentry); | |
3190 | ||
3191 | ceph_put_mds_session(di->lease_session); | |
3192 | di->lease_session = NULL; | |
3193 | } | |
3194 | ||
2600d2dd SW |
3195 | static void handle_lease(struct ceph_mds_client *mdsc, |
3196 | struct ceph_mds_session *session, | |
3197 | struct ceph_msg *msg) | |
2f2dc053 | 3198 | { |
3d14c5d2 | 3199 | struct super_block *sb = mdsc->fsc->sb; |
2f2dc053 | 3200 | struct inode *inode; |
2f2dc053 SW |
3201 | struct dentry *parent, *dentry; |
3202 | struct ceph_dentry_info *di; | |
2600d2dd | 3203 | int mds = session->s_mds; |
2f2dc053 | 3204 | struct ceph_mds_lease *h = msg->front.iov_base; |
1e5ea23d | 3205 | u32 seq; |
2f2dc053 | 3206 | struct ceph_vino vino; |
2f2dc053 SW |
3207 | struct qstr dname; |
3208 | int release = 0; | |
3209 | ||
2f2dc053 SW |
3210 | dout("handle_lease from mds%d\n", mds); |
3211 | ||
3212 | /* decode */ | |
3213 | if (msg->front.iov_len < sizeof(*h) + sizeof(u32)) | |
3214 | goto bad; | |
3215 | vino.ino = le64_to_cpu(h->ino); | |
3216 | vino.snap = CEPH_NOSNAP; | |
1e5ea23d | 3217 | seq = le32_to_cpu(h->seq); |
2f2dc053 SW |
3218 | dname.name = (void *)h + sizeof(*h) + sizeof(u32); |
3219 | dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32); | |
3220 | if (dname.len != get_unaligned_le32(h+1)) | |
3221 | goto bad; | |
3222 | ||
2f2dc053 SW |
3223 | /* lookup inode */ |
3224 | inode = ceph_find_inode(sb, vino); | |
2f90b852 SW |
3225 | dout("handle_lease %s, ino %llx %p %.*s\n", |
3226 | ceph_lease_op_name(h->action), vino.ino, inode, | |
1e5ea23d | 3227 | dname.len, dname.name); |
6cd3bcad YZ |
3228 | |
3229 | mutex_lock(&session->s_mutex); | |
3230 | session->s_seq++; | |
3231 | ||
2f2dc053 SW |
3232 | if (inode == NULL) { |
3233 | dout("handle_lease no inode %llx\n", vino.ino); | |
3234 | goto release; | |
3235 | } | |
2f2dc053 SW |
3236 | |
3237 | /* dentry */ | |
3238 | parent = d_find_alias(inode); | |
3239 | if (!parent) { | |
3240 | dout("no parent dentry on inode %p\n", inode); | |
3241 | WARN_ON(1); | |
3242 | goto release; /* hrm... */ | |
3243 | } | |
8387ff25 | 3244 | dname.hash = full_name_hash(parent, dname.name, dname.len); |
2f2dc053 SW |
3245 | dentry = d_lookup(parent, &dname); |
3246 | dput(parent); | |
3247 | if (!dentry) | |
3248 | goto release; | |
3249 | ||
3250 | spin_lock(&dentry->d_lock); | |
3251 | di = ceph_dentry(dentry); | |
3252 | switch (h->action) { | |
3253 | case CEPH_MDS_LEASE_REVOKE: | |
3d8eb7a9 | 3254 | if (di->lease_session == session) { |
1e5ea23d SW |
3255 | if (ceph_seq_cmp(di->lease_seq, seq) > 0) |
3256 | h->seq = cpu_to_le32(di->lease_seq); | |
2f2dc053 SW |
3257 | __ceph_mdsc_drop_dentry_lease(dentry); |
3258 | } | |
3259 | release = 1; | |
3260 | break; | |
3261 | ||
3262 | case CEPH_MDS_LEASE_RENEW: | |
3d8eb7a9 | 3263 | if (di->lease_session == session && |
2f2dc053 SW |
3264 | di->lease_gen == session->s_cap_gen && |
3265 | di->lease_renew_from && | |
3266 | di->lease_renew_after == 0) { | |
3267 | unsigned long duration = | |
3563dbdd | 3268 | msecs_to_jiffies(le32_to_cpu(h->duration_ms)); |
2f2dc053 | 3269 | |
1e5ea23d | 3270 | di->lease_seq = seq; |
9b16f03c | 3271 | di->time = di->lease_renew_from + duration; |
2f2dc053 SW |
3272 | di->lease_renew_after = di->lease_renew_from + |
3273 | (duration >> 1); | |
3274 | di->lease_renew_from = 0; | |
3275 | } | |
3276 | break; | |
3277 | } | |
3278 | spin_unlock(&dentry->d_lock); | |
3279 | dput(dentry); | |
3280 | ||
3281 | if (!release) | |
3282 | goto out; | |
3283 | ||
3284 | release: | |
3285 | /* let's just reuse the same message */ | |
3286 | h->action = CEPH_MDS_LEASE_REVOKE_ACK; | |
3287 | ceph_msg_get(msg); | |
3288 | ceph_con_send(&session->s_con, msg); | |
3289 | ||
3290 | out: | |
3291 | iput(inode); | |
3292 | mutex_unlock(&session->s_mutex); | |
2f2dc053 SW |
3293 | return; |
3294 | ||
3295 | bad: | |
3296 | pr_err("corrupt lease message\n"); | |
9ec7cab1 | 3297 | ceph_msg_dump(msg); |
2f2dc053 SW |
3298 | } |
3299 | ||
3300 | void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session, | |
3301 | struct inode *inode, | |
3302 | struct dentry *dentry, char action, | |
3303 | u32 seq) | |
3304 | { | |
3305 | struct ceph_msg *msg; | |
3306 | struct ceph_mds_lease *lease; | |
3307 | int len = sizeof(*lease) + sizeof(u32); | |
3308 | int dnamelen = 0; | |
3309 | ||
3310 | dout("lease_send_msg inode %p dentry %p %s to mds%d\n", | |
3311 | inode, dentry, ceph_lease_op_name(action), session->s_mds); | |
3312 | dnamelen = dentry->d_name.len; | |
3313 | len += dnamelen; | |
3314 | ||
b61c2763 | 3315 | msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false); |
a79832f2 | 3316 | if (!msg) |
2f2dc053 SW |
3317 | return; |
3318 | lease = msg->front.iov_base; | |
3319 | lease->action = action; | |
2f2dc053 SW |
3320 | lease->ino = cpu_to_le64(ceph_vino(inode).ino); |
3321 | lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap); | |
3322 | lease->seq = cpu_to_le32(seq); | |
3323 | put_unaligned_le32(dnamelen, lease + 1); | |
3324 | memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen); | |
3325 | ||
3326 | /* | |
3327 | * if this is a preemptive lease RELEASE, no need to | |
3328 | * flush request stream, since the actual request will | |
3329 | * soon follow. | |
3330 | */ | |
3331 | msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE); | |
3332 | ||
3333 | ceph_con_send(&session->s_con, msg); | |
3334 | } | |
3335 | ||
2f2dc053 SW |
3336 | /* |
3337 | * drop all leases (and dentry refs) in preparation for umount | |
3338 | */ | |
3339 | static void drop_leases(struct ceph_mds_client *mdsc) | |
3340 | { | |
3341 | int i; | |
3342 | ||
3343 | dout("drop_leases\n"); | |
3344 | mutex_lock(&mdsc->mutex); | |
3345 | for (i = 0; i < mdsc->max_sessions; i++) { | |
3346 | struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i); | |
3347 | if (!s) | |
3348 | continue; | |
3349 | mutex_unlock(&mdsc->mutex); | |
3350 | mutex_lock(&s->s_mutex); | |
3351 | mutex_unlock(&s->s_mutex); | |
3352 | ceph_put_mds_session(s); | |
3353 | mutex_lock(&mdsc->mutex); | |
3354 | } | |
3355 | mutex_unlock(&mdsc->mutex); | |
3356 | } | |
3357 | ||
3358 | ||
3359 | ||
3360 | /* | |
3361 | * delayed work -- periodically trim expired leases, renew caps with mds | |
3362 | */ | |
3363 | static void schedule_delayed(struct ceph_mds_client *mdsc) | |
3364 | { | |
3365 | int delay = 5; | |
3366 | unsigned hz = round_jiffies_relative(HZ * delay); | |
3367 | schedule_delayed_work(&mdsc->delayed_work, hz); | |
3368 | } | |
3369 | ||
3370 | static void delayed_work(struct work_struct *work) | |
3371 | { | |
3372 | int i; | |
3373 | struct ceph_mds_client *mdsc = | |
3374 | container_of(work, struct ceph_mds_client, delayed_work.work); | |
3375 | int renew_interval; | |
3376 | int renew_caps; | |
3377 | ||
3378 | dout("mdsc delayed_work\n"); | |
afcdaea3 | 3379 | ceph_check_delayed_caps(mdsc); |
2f2dc053 SW |
3380 | |
3381 | mutex_lock(&mdsc->mutex); | |
3382 | renew_interval = mdsc->mdsmap->m_session_timeout >> 2; | |
3383 | renew_caps = time_after_eq(jiffies, HZ*renew_interval + | |
3384 | mdsc->last_renew_caps); | |
3385 | if (renew_caps) | |
3386 | mdsc->last_renew_caps = jiffies; | |
3387 | ||
3388 | for (i = 0; i < mdsc->max_sessions; i++) { | |
3389 | struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i); | |
3390 | if (s == NULL) | |
3391 | continue; | |
3392 | if (s->s_state == CEPH_MDS_SESSION_CLOSING) { | |
3393 | dout("resending session close request for mds%d\n", | |
3394 | s->s_mds); | |
3395 | request_close_session(mdsc, s); | |
3396 | ceph_put_mds_session(s); | |
3397 | continue; | |
3398 | } | |
3399 | if (s->s_ttl && time_after(jiffies, s->s_ttl)) { | |
3400 | if (s->s_state == CEPH_MDS_SESSION_OPEN) { | |
3401 | s->s_state = CEPH_MDS_SESSION_HUNG; | |
3402 | pr_info("mds%d hung\n", s->s_mds); | |
3403 | } | |
3404 | } | |
3405 | if (s->s_state < CEPH_MDS_SESSION_OPEN) { | |
3406 | /* this mds is failed or recovering, just wait */ | |
3407 | ceph_put_mds_session(s); | |
3408 | continue; | |
3409 | } | |
3410 | mutex_unlock(&mdsc->mutex); | |
3411 | ||
3412 | mutex_lock(&s->s_mutex); | |
3413 | if (renew_caps) | |
3414 | send_renew_caps(mdsc, s); | |
3415 | else | |
3416 | ceph_con_keepalive(&s->s_con); | |
aab53dd9 SW |
3417 | if (s->s_state == CEPH_MDS_SESSION_OPEN || |
3418 | s->s_state == CEPH_MDS_SESSION_HUNG) | |
3d7ded4d | 3419 | ceph_send_cap_releases(mdsc, s); |
2f2dc053 SW |
3420 | mutex_unlock(&s->s_mutex); |
3421 | ceph_put_mds_session(s); | |
3422 | ||
3423 | mutex_lock(&mdsc->mutex); | |
3424 | } | |
3425 | mutex_unlock(&mdsc->mutex); | |
3426 | ||
3427 | schedule_delayed(mdsc); | |
3428 | } | |
3429 | ||
3d14c5d2 | 3430 | int ceph_mdsc_init(struct ceph_fs_client *fsc) |
2f2dc053 | 3431 | |
2f2dc053 | 3432 | { |
3d14c5d2 YS |
3433 | struct ceph_mds_client *mdsc; |
3434 | ||
3435 | mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS); | |
3436 | if (!mdsc) | |
3437 | return -ENOMEM; | |
3438 | mdsc->fsc = fsc; | |
3439 | fsc->mdsc = mdsc; | |
2f2dc053 SW |
3440 | mutex_init(&mdsc->mutex); |
3441 | mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS); | |
fb3101b6 | 3442 | if (mdsc->mdsmap == NULL) { |
3443 | kfree(mdsc); | |
2d06eeb8 | 3444 | return -ENOMEM; |
fb3101b6 | 3445 | } |
2d06eeb8 | 3446 | |
2f2dc053 | 3447 | init_completion(&mdsc->safe_umount_waiters); |
f3c60c59 | 3448 | init_waitqueue_head(&mdsc->session_close_wq); |
2f2dc053 SW |
3449 | INIT_LIST_HEAD(&mdsc->waiting_for_map); |
3450 | mdsc->sessions = NULL; | |
86d8f67b | 3451 | atomic_set(&mdsc->num_sessions, 0); |
2f2dc053 SW |
3452 | mdsc->max_sessions = 0; |
3453 | mdsc->stopping = 0; | |
affbc19a | 3454 | mdsc->last_snap_seq = 0; |
2f2dc053 | 3455 | init_rwsem(&mdsc->snap_rwsem); |
a105f00c | 3456 | mdsc->snap_realms = RB_ROOT; |
2f2dc053 SW |
3457 | INIT_LIST_HEAD(&mdsc->snap_empty); |
3458 | spin_lock_init(&mdsc->snap_empty_lock); | |
3459 | mdsc->last_tid = 0; | |
e8a7b8b1 | 3460 | mdsc->oldest_tid = 0; |
44ca18f2 | 3461 | mdsc->request_tree = RB_ROOT; |
2f2dc053 SW |
3462 | INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work); |
3463 | mdsc->last_renew_caps = jiffies; | |
3464 | INIT_LIST_HEAD(&mdsc->cap_delay_list); | |
3465 | spin_lock_init(&mdsc->cap_delay_lock); | |
3466 | INIT_LIST_HEAD(&mdsc->snap_flush_list); | |
3467 | spin_lock_init(&mdsc->snap_flush_lock); | |
553adfd9 | 3468 | mdsc->last_cap_flush_tid = 1; |
e4500b5e | 3469 | INIT_LIST_HEAD(&mdsc->cap_flush_list); |
2f2dc053 | 3470 | INIT_LIST_HEAD(&mdsc->cap_dirty); |
db354052 | 3471 | INIT_LIST_HEAD(&mdsc->cap_dirty_migrating); |
2f2dc053 SW |
3472 | mdsc->num_cap_flushing = 0; |
3473 | spin_lock_init(&mdsc->cap_dirty_lock); | |
3474 | init_waitqueue_head(&mdsc->cap_flushing_wq); | |
3475 | spin_lock_init(&mdsc->dentry_lru_lock); | |
3476 | INIT_LIST_HEAD(&mdsc->dentry_lru); | |
2d06eeb8 | 3477 | |
37151668 | 3478 | ceph_caps_init(mdsc); |
3d14c5d2 | 3479 | ceph_adjust_min_caps(mdsc, fsc->min_caps); |
37151668 | 3480 | |
10183a69 YZ |
3481 | init_rwsem(&mdsc->pool_perm_rwsem); |
3482 | mdsc->pool_perm_tree = RB_ROOT; | |
3483 | ||
5f44f142 | 3484 | return 0; |
2f2dc053 SW |
3485 | } |
3486 | ||
3487 | /* | |
3488 | * Wait for safe replies on open mds requests. If we time out, drop | |
3489 | * all requests from the tree to avoid dangling dentry refs. | |
3490 | */ | |
3491 | static void wait_requests(struct ceph_mds_client *mdsc) | |
3492 | { | |
a319bf56 | 3493 | struct ceph_options *opts = mdsc->fsc->client->options; |
2f2dc053 | 3494 | struct ceph_mds_request *req; |
2f2dc053 SW |
3495 | |
3496 | mutex_lock(&mdsc->mutex); | |
44ca18f2 | 3497 | if (__get_oldest_req(mdsc)) { |
2f2dc053 | 3498 | mutex_unlock(&mdsc->mutex); |
44ca18f2 | 3499 | |
2f2dc053 SW |
3500 | dout("wait_requests waiting for requests\n"); |
3501 | wait_for_completion_timeout(&mdsc->safe_umount_waiters, | |
a319bf56 | 3502 | ceph_timeout_jiffies(opts->mount_timeout)); |
2f2dc053 SW |
3503 | |
3504 | /* tear down remaining requests */ | |
44ca18f2 SW |
3505 | mutex_lock(&mdsc->mutex); |
3506 | while ((req = __get_oldest_req(mdsc))) { | |
2f2dc053 SW |
3507 | dout("wait_requests timed out on tid %llu\n", |
3508 | req->r_tid); | |
44ca18f2 | 3509 | __unregister_request(mdsc, req); |
2f2dc053 SW |
3510 | } |
3511 | } | |
3512 | mutex_unlock(&mdsc->mutex); | |
3513 | dout("wait_requests done\n"); | |
3514 | } | |
3515 | ||
3516 | /* | |
3517 | * called before mount is ro, and before dentries are torn down. | |
3518 | * (hmm, does this still race with new lookups?) | |
3519 | */ | |
3520 | void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc) | |
3521 | { | |
3522 | dout("pre_umount\n"); | |
3523 | mdsc->stopping = 1; | |
3524 | ||
3525 | drop_leases(mdsc); | |
afcdaea3 | 3526 | ceph_flush_dirty_caps(mdsc); |
2f2dc053 | 3527 | wait_requests(mdsc); |
17c688c3 SW |
3528 | |
3529 | /* | |
3530 | * wait for reply handlers to drop their request refs and | |
3531 | * their inode/dcache refs | |
3532 | */ | |
3533 | ceph_msgr_flush(); | |
2f2dc053 SW |
3534 | } |
3535 | ||
3536 | /* | |
3537 | * wait for all write mds requests to flush. | |
3538 | */ | |
3539 | static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid) | |
3540 | { | |
80fc7314 | 3541 | struct ceph_mds_request *req = NULL, *nextreq; |
44ca18f2 | 3542 | struct rb_node *n; |
2f2dc053 SW |
3543 | |
3544 | mutex_lock(&mdsc->mutex); | |
3545 | dout("wait_unsafe_requests want %lld\n", want_tid); | |
80fc7314 | 3546 | restart: |
44ca18f2 SW |
3547 | req = __get_oldest_req(mdsc); |
3548 | while (req && req->r_tid <= want_tid) { | |
80fc7314 SW |
3549 | /* find next request */ |
3550 | n = rb_next(&req->r_node); | |
3551 | if (n) | |
3552 | nextreq = rb_entry(n, struct ceph_mds_request, r_node); | |
3553 | else | |
3554 | nextreq = NULL; | |
e8a7b8b1 YZ |
3555 | if (req->r_op != CEPH_MDS_OP_SETFILELOCK && |
3556 | (req->r_op & CEPH_MDS_OP_WRITE)) { | |
44ca18f2 SW |
3557 | /* write op */ |
3558 | ceph_mdsc_get_request(req); | |
80fc7314 SW |
3559 | if (nextreq) |
3560 | ceph_mdsc_get_request(nextreq); | |
44ca18f2 SW |
3561 | mutex_unlock(&mdsc->mutex); |
3562 | dout("wait_unsafe_requests wait on %llu (want %llu)\n", | |
3563 | req->r_tid, want_tid); | |
3564 | wait_for_completion(&req->r_safe_completion); | |
3565 | mutex_lock(&mdsc->mutex); | |
44ca18f2 | 3566 | ceph_mdsc_put_request(req); |
80fc7314 SW |
3567 | if (!nextreq) |
3568 | break; /* next dne before, so we're done! */ | |
3569 | if (RB_EMPTY_NODE(&nextreq->r_node)) { | |
3570 | /* next request was removed from tree */ | |
3571 | ceph_mdsc_put_request(nextreq); | |
3572 | goto restart; | |
3573 | } | |
3574 | ceph_mdsc_put_request(nextreq); /* won't go away */ | |
44ca18f2 | 3575 | } |
80fc7314 | 3576 | req = nextreq; |
2f2dc053 SW |
3577 | } |
3578 | mutex_unlock(&mdsc->mutex); | |
3579 | dout("wait_unsafe_requests done\n"); | |
3580 | } | |
3581 | ||
3582 | void ceph_mdsc_sync(struct ceph_mds_client *mdsc) | |
3583 | { | |
0e294387 | 3584 | u64 want_tid, want_flush; |
2f2dc053 | 3585 | |
48fec5d0 | 3586 | if (ACCESS_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) |
56b7cf95 SW |
3587 | return; |
3588 | ||
2f2dc053 SW |
3589 | dout("sync\n"); |
3590 | mutex_lock(&mdsc->mutex); | |
3591 | want_tid = mdsc->last_tid; | |
2f2dc053 | 3592 | mutex_unlock(&mdsc->mutex); |
2f2dc053 | 3593 | |
afcdaea3 | 3594 | ceph_flush_dirty_caps(mdsc); |
d3383a8e | 3595 | spin_lock(&mdsc->cap_dirty_lock); |
8310b089 | 3596 | want_flush = mdsc->last_cap_flush_tid; |
c8799fc4 YZ |
3597 | if (!list_empty(&mdsc->cap_flush_list)) { |
3598 | struct ceph_cap_flush *cf = | |
3599 | list_last_entry(&mdsc->cap_flush_list, | |
3600 | struct ceph_cap_flush, g_list); | |
3601 | cf->wake = true; | |
3602 | } | |
d3383a8e YZ |
3603 | spin_unlock(&mdsc->cap_dirty_lock); |
3604 | ||
0e294387 YZ |
3605 | dout("sync want tid %lld flush_seq %lld\n", |
3606 | want_tid, want_flush); | |
2f2dc053 SW |
3607 | |
3608 | wait_unsafe_requests(mdsc, want_tid); | |
0e294387 | 3609 | wait_caps_flush(mdsc, want_flush); |
2f2dc053 SW |
3610 | } |
3611 | ||
f3c60c59 SW |
3612 | /* |
3613 | * true if all sessions are closed, or we force unmount | |
3614 | */ | |
fcff415c | 3615 | static bool done_closing_sessions(struct ceph_mds_client *mdsc, int skipped) |
f3c60c59 | 3616 | { |
48fec5d0 | 3617 | if (ACCESS_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) |
f3c60c59 | 3618 | return true; |
fcff415c | 3619 | return atomic_read(&mdsc->num_sessions) <= skipped; |
f3c60c59 | 3620 | } |
2f2dc053 SW |
3621 | |
3622 | /* | |
3623 | * called after sb is ro. | |
3624 | */ | |
3625 | void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc) | |
3626 | { | |
a319bf56 | 3627 | struct ceph_options *opts = mdsc->fsc->client->options; |
2f2dc053 SW |
3628 | struct ceph_mds_session *session; |
3629 | int i; | |
fcff415c | 3630 | int skipped = 0; |
2f2dc053 SW |
3631 | |
3632 | dout("close_sessions\n"); | |
3633 | ||
2f2dc053 | 3634 | /* close sessions */ |
f3c60c59 SW |
3635 | mutex_lock(&mdsc->mutex); |
3636 | for (i = 0; i < mdsc->max_sessions; i++) { | |
3637 | session = __ceph_lookup_mds_session(mdsc, i); | |
3638 | if (!session) | |
3639 | continue; | |
2f2dc053 | 3640 | mutex_unlock(&mdsc->mutex); |
f3c60c59 | 3641 | mutex_lock(&session->s_mutex); |
fcff415c YZ |
3642 | if (__close_session(mdsc, session) <= 0) |
3643 | skipped++; | |
f3c60c59 SW |
3644 | mutex_unlock(&session->s_mutex); |
3645 | ceph_put_mds_session(session); | |
2f2dc053 SW |
3646 | mutex_lock(&mdsc->mutex); |
3647 | } | |
f3c60c59 SW |
3648 | mutex_unlock(&mdsc->mutex); |
3649 | ||
3650 | dout("waiting for sessions to close\n"); | |
fcff415c YZ |
3651 | wait_event_timeout(mdsc->session_close_wq, |
3652 | done_closing_sessions(mdsc, skipped), | |
a319bf56 | 3653 | ceph_timeout_jiffies(opts->mount_timeout)); |
2f2dc053 SW |
3654 | |
3655 | /* tear down remaining sessions */ | |
f3c60c59 | 3656 | mutex_lock(&mdsc->mutex); |
2f2dc053 SW |
3657 | for (i = 0; i < mdsc->max_sessions; i++) { |
3658 | if (mdsc->sessions[i]) { | |
3659 | session = get_session(mdsc->sessions[i]); | |
2600d2dd | 3660 | __unregister_session(mdsc, session); |
2f2dc053 SW |
3661 | mutex_unlock(&mdsc->mutex); |
3662 | mutex_lock(&session->s_mutex); | |
3663 | remove_session_caps(session); | |
3664 | mutex_unlock(&session->s_mutex); | |
3665 | ceph_put_mds_session(session); | |
3666 | mutex_lock(&mdsc->mutex); | |
3667 | } | |
3668 | } | |
2f2dc053 | 3669 | WARN_ON(!list_empty(&mdsc->cap_delay_list)); |
2f2dc053 SW |
3670 | mutex_unlock(&mdsc->mutex); |
3671 | ||
3672 | ceph_cleanup_empty_realms(mdsc); | |
3673 | ||
3674 | cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */ | |
3675 | ||
3676 | dout("stopped\n"); | |
3677 | } | |
3678 | ||
48fec5d0 YZ |
3679 | void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc) |
3680 | { | |
3681 | struct ceph_mds_session *session; | |
3682 | int mds; | |
3683 | ||
3684 | dout("force umount\n"); | |
3685 | ||
3686 | mutex_lock(&mdsc->mutex); | |
3687 | for (mds = 0; mds < mdsc->max_sessions; mds++) { | |
3688 | session = __ceph_lookup_mds_session(mdsc, mds); | |
3689 | if (!session) | |
3690 | continue; | |
3691 | mutex_unlock(&mdsc->mutex); | |
3692 | mutex_lock(&session->s_mutex); | |
3693 | __close_session(mdsc, session); | |
3694 | if (session->s_state == CEPH_MDS_SESSION_CLOSING) { | |
3695 | cleanup_session_requests(mdsc, session); | |
3696 | remove_session_caps(session); | |
3697 | } | |
3698 | mutex_unlock(&session->s_mutex); | |
3699 | ceph_put_mds_session(session); | |
3700 | mutex_lock(&mdsc->mutex); | |
3701 | kick_requests(mdsc, mds); | |
3702 | } | |
3703 | __wake_requests(mdsc, &mdsc->waiting_for_map); | |
3704 | mutex_unlock(&mdsc->mutex); | |
3705 | } | |
3706 | ||
3d14c5d2 | 3707 | static void ceph_mdsc_stop(struct ceph_mds_client *mdsc) |
2f2dc053 SW |
3708 | { |
3709 | dout("stop\n"); | |
3710 | cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */ | |
3711 | if (mdsc->mdsmap) | |
3712 | ceph_mdsmap_destroy(mdsc->mdsmap); | |
3713 | kfree(mdsc->sessions); | |
37151668 | 3714 | ceph_caps_finalize(mdsc); |
10183a69 | 3715 | ceph_pool_perm_destroy(mdsc); |
2f2dc053 SW |
3716 | } |
3717 | ||
3d14c5d2 YS |
3718 | void ceph_mdsc_destroy(struct ceph_fs_client *fsc) |
3719 | { | |
3720 | struct ceph_mds_client *mdsc = fsc->mdsc; | |
3721 | ||
ef550f6f | 3722 | dout("mdsc_destroy %p\n", mdsc); |
3d14c5d2 | 3723 | ceph_mdsc_stop(mdsc); |
ef550f6f SW |
3724 | |
3725 | /* flush out any connection work with references to us */ | |
3726 | ceph_msgr_flush(); | |
3727 | ||
3d14c5d2 YS |
3728 | fsc->mdsc = NULL; |
3729 | kfree(mdsc); | |
ef550f6f | 3730 | dout("mdsc_destroy %p done\n", mdsc); |
3d14c5d2 YS |
3731 | } |
3732 | ||
430afbad YZ |
3733 | void ceph_mdsc_handle_fsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg) |
3734 | { | |
3735 | struct ceph_fs_client *fsc = mdsc->fsc; | |
3736 | const char *mds_namespace = fsc->mount_options->mds_namespace; | |
3737 | void *p = msg->front.iov_base; | |
3738 | void *end = p + msg->front.iov_len; | |
3739 | u32 epoch; | |
3740 | u32 map_len; | |
3741 | u32 num_fs; | |
3742 | u32 mount_fscid = (u32)-1; | |
3743 | u8 struct_v, struct_cv; | |
3744 | int err = -EINVAL; | |
3745 | ||
3746 | ceph_decode_need(&p, end, sizeof(u32), bad); | |
3747 | epoch = ceph_decode_32(&p); | |
3748 | ||
3749 | dout("handle_fsmap epoch %u\n", epoch); | |
3750 | ||
3751 | ceph_decode_need(&p, end, 2 + sizeof(u32), bad); | |
3752 | struct_v = ceph_decode_8(&p); | |
3753 | struct_cv = ceph_decode_8(&p); | |
3754 | map_len = ceph_decode_32(&p); | |
3755 | ||
3756 | ceph_decode_need(&p, end, sizeof(u32) * 3, bad); | |
3757 | p += sizeof(u32) * 2; /* skip epoch and legacy_client_fscid */ | |
3758 | ||
3759 | num_fs = ceph_decode_32(&p); | |
3760 | while (num_fs-- > 0) { | |
3761 | void *info_p, *info_end; | |
3762 | u32 info_len; | |
3763 | u8 info_v, info_cv; | |
3764 | u32 fscid, namelen; | |
3765 | ||
3766 | ceph_decode_need(&p, end, 2 + sizeof(u32), bad); | |
3767 | info_v = ceph_decode_8(&p); | |
3768 | info_cv = ceph_decode_8(&p); | |
3769 | info_len = ceph_decode_32(&p); | |
3770 | ceph_decode_need(&p, end, info_len, bad); | |
3771 | info_p = p; | |
3772 | info_end = p + info_len; | |
3773 | p = info_end; | |
3774 | ||
3775 | ceph_decode_need(&info_p, info_end, sizeof(u32) * 2, bad); | |
3776 | fscid = ceph_decode_32(&info_p); | |
3777 | namelen = ceph_decode_32(&info_p); | |
3778 | ceph_decode_need(&info_p, info_end, namelen, bad); | |
3779 | ||
3780 | if (mds_namespace && | |
3781 | strlen(mds_namespace) == namelen && | |
3782 | !strncmp(mds_namespace, (char *)info_p, namelen)) { | |
3783 | mount_fscid = fscid; | |
3784 | break; | |
3785 | } | |
3786 | } | |
3787 | ||
3788 | ceph_monc_got_map(&fsc->client->monc, CEPH_SUB_FSMAP, epoch); | |
3789 | if (mount_fscid != (u32)-1) { | |
3790 | fsc->client->monc.fs_cluster_id = mount_fscid; | |
3791 | ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP, | |
3792 | 0, true); | |
3793 | ceph_monc_renew_subs(&fsc->client->monc); | |
3794 | } else { | |
3795 | err = -ENOENT; | |
3796 | goto err_out; | |
3797 | } | |
3798 | return; | |
3799 | bad: | |
3800 | pr_err("error decoding fsmap\n"); | |
3801 | err_out: | |
3802 | mutex_lock(&mdsc->mutex); | |
3803 | mdsc->mdsmap_err = -ENOENT; | |
3804 | __wake_requests(mdsc, &mdsc->waiting_for_map); | |
3805 | mutex_unlock(&mdsc->mutex); | |
3806 | return; | |
3807 | } | |
2f2dc053 SW |
3808 | |
3809 | /* | |
3810 | * handle mds map update. | |
3811 | */ | |
430afbad | 3812 | void ceph_mdsc_handle_mdsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg) |
2f2dc053 SW |
3813 | { |
3814 | u32 epoch; | |
3815 | u32 maplen; | |
3816 | void *p = msg->front.iov_base; | |
3817 | void *end = p + msg->front.iov_len; | |
3818 | struct ceph_mdsmap *newmap, *oldmap; | |
3819 | struct ceph_fsid fsid; | |
3820 | int err = -EINVAL; | |
3821 | ||
3822 | ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad); | |
3823 | ceph_decode_copy(&p, &fsid, sizeof(fsid)); | |
3d14c5d2 | 3824 | if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0) |
0743304d | 3825 | return; |
c89136ea SW |
3826 | epoch = ceph_decode_32(&p); |
3827 | maplen = ceph_decode_32(&p); | |
2f2dc053 SW |
3828 | dout("handle_map epoch %u len %d\n", epoch, (int)maplen); |
3829 | ||
3830 | /* do we need it? */ | |
2f2dc053 SW |
3831 | mutex_lock(&mdsc->mutex); |
3832 | if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) { | |
3833 | dout("handle_map epoch %u <= our %u\n", | |
3834 | epoch, mdsc->mdsmap->m_epoch); | |
3835 | mutex_unlock(&mdsc->mutex); | |
3836 | return; | |
3837 | } | |
3838 | ||
3839 | newmap = ceph_mdsmap_decode(&p, end); | |
3840 | if (IS_ERR(newmap)) { | |
3841 | err = PTR_ERR(newmap); | |
3842 | goto bad_unlock; | |
3843 | } | |
3844 | ||
3845 | /* swap into place */ | |
3846 | if (mdsc->mdsmap) { | |
3847 | oldmap = mdsc->mdsmap; | |
3848 | mdsc->mdsmap = newmap; | |
3849 | check_new_map(mdsc, newmap, oldmap); | |
3850 | ceph_mdsmap_destroy(oldmap); | |
3851 | } else { | |
3852 | mdsc->mdsmap = newmap; /* first mds map */ | |
3853 | } | |
3d14c5d2 | 3854 | mdsc->fsc->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size; |
2f2dc053 SW |
3855 | |
3856 | __wake_requests(mdsc, &mdsc->waiting_for_map); | |
82dcabad ID |
3857 | ceph_monc_got_map(&mdsc->fsc->client->monc, CEPH_SUB_MDSMAP, |
3858 | mdsc->mdsmap->m_epoch); | |
2f2dc053 SW |
3859 | |
3860 | mutex_unlock(&mdsc->mutex); | |
3861 | schedule_delayed(mdsc); | |
3862 | return; | |
3863 | ||
3864 | bad_unlock: | |
3865 | mutex_unlock(&mdsc->mutex); | |
3866 | bad: | |
3867 | pr_err("error decoding mdsmap %d\n", err); | |
3868 | return; | |
3869 | } | |
3870 | ||
3871 | static struct ceph_connection *con_get(struct ceph_connection *con) | |
3872 | { | |
3873 | struct ceph_mds_session *s = con->private; | |
3874 | ||
3875 | if (get_session(s)) { | |
2600d2dd | 3876 | dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref)); |
2f2dc053 SW |
3877 | return con; |
3878 | } | |
3879 | dout("mdsc con_get %p FAIL\n", s); | |
3880 | return NULL; | |
3881 | } | |
3882 | ||
3883 | static void con_put(struct ceph_connection *con) | |
3884 | { | |
3885 | struct ceph_mds_session *s = con->private; | |
3886 | ||
7d8e18a6 | 3887 | dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref) - 1); |
2f2dc053 SW |
3888 | ceph_put_mds_session(s); |
3889 | } | |
3890 | ||
3891 | /* | |
3892 | * if the client is unresponsive for long enough, the mds will kill | |
3893 | * the session entirely. | |
3894 | */ | |
3895 | static void peer_reset(struct ceph_connection *con) | |
3896 | { | |
3897 | struct ceph_mds_session *s = con->private; | |
7e70f0ed | 3898 | struct ceph_mds_client *mdsc = s->s_mdsc; |
2f2dc053 | 3899 | |
f3ae1b97 | 3900 | pr_warn("mds%d closed our session\n", s->s_mds); |
7e70f0ed | 3901 | send_mds_reconnect(mdsc, s); |
2f2dc053 SW |
3902 | } |
3903 | ||
3904 | static void dispatch(struct ceph_connection *con, struct ceph_msg *msg) | |
3905 | { | |
3906 | struct ceph_mds_session *s = con->private; | |
3907 | struct ceph_mds_client *mdsc = s->s_mdsc; | |
3908 | int type = le16_to_cpu(msg->hdr.type); | |
3909 | ||
2600d2dd SW |
3910 | mutex_lock(&mdsc->mutex); |
3911 | if (__verify_registered_session(mdsc, s) < 0) { | |
3912 | mutex_unlock(&mdsc->mutex); | |
3913 | goto out; | |
3914 | } | |
3915 | mutex_unlock(&mdsc->mutex); | |
3916 | ||
2f2dc053 SW |
3917 | switch (type) { |
3918 | case CEPH_MSG_MDS_MAP: | |
430afbad YZ |
3919 | ceph_mdsc_handle_mdsmap(mdsc, msg); |
3920 | break; | |
3921 | case CEPH_MSG_FS_MAP_USER: | |
3922 | ceph_mdsc_handle_fsmap(mdsc, msg); | |
2f2dc053 SW |
3923 | break; |
3924 | case CEPH_MSG_CLIENT_SESSION: | |
3925 | handle_session(s, msg); | |
3926 | break; | |
3927 | case CEPH_MSG_CLIENT_REPLY: | |
3928 | handle_reply(s, msg); | |
3929 | break; | |
3930 | case CEPH_MSG_CLIENT_REQUEST_FORWARD: | |
2600d2dd | 3931 | handle_forward(mdsc, s, msg); |
2f2dc053 SW |
3932 | break; |
3933 | case CEPH_MSG_CLIENT_CAPS: | |
3934 | ceph_handle_caps(s, msg); | |
3935 | break; | |
3936 | case CEPH_MSG_CLIENT_SNAP: | |
2600d2dd | 3937 | ceph_handle_snap(mdsc, s, msg); |
2f2dc053 SW |
3938 | break; |
3939 | case CEPH_MSG_CLIENT_LEASE: | |
2600d2dd | 3940 | handle_lease(mdsc, s, msg); |
2f2dc053 SW |
3941 | break; |
3942 | ||
3943 | default: | |
3944 | pr_err("received unknown message type %d %s\n", type, | |
3945 | ceph_msg_type_name(type)); | |
3946 | } | |
2600d2dd | 3947 | out: |
2f2dc053 SW |
3948 | ceph_msg_put(msg); |
3949 | } | |
3950 | ||
4e7a5dcd SW |
3951 | /* |
3952 | * authentication | |
3953 | */ | |
a3530df3 AE |
3954 | |
3955 | /* | |
3956 | * Note: returned pointer is the address of a structure that's | |
3957 | * managed separately. Caller must *not* attempt to free it. | |
3958 | */ | |
3959 | static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con, | |
8f43fb53 | 3960 | int *proto, int force_new) |
4e7a5dcd SW |
3961 | { |
3962 | struct ceph_mds_session *s = con->private; | |
3963 | struct ceph_mds_client *mdsc = s->s_mdsc; | |
3d14c5d2 | 3964 | struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth; |
74f1869f | 3965 | struct ceph_auth_handshake *auth = &s->s_auth; |
4e7a5dcd | 3966 | |
74f1869f | 3967 | if (force_new && auth->authorizer) { |
6c1ea260 | 3968 | ceph_auth_destroy_authorizer(auth->authorizer); |
74f1869f | 3969 | auth->authorizer = NULL; |
4e7a5dcd | 3970 | } |
27859f97 SW |
3971 | if (!auth->authorizer) { |
3972 | int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS, | |
3973 | auth); | |
0bed9b5c SW |
3974 | if (ret) |
3975 | return ERR_PTR(ret); | |
27859f97 SW |
3976 | } else { |
3977 | int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS, | |
3978 | auth); | |
a255651d | 3979 | if (ret) |
a3530df3 | 3980 | return ERR_PTR(ret); |
4e7a5dcd | 3981 | } |
4e7a5dcd | 3982 | *proto = ac->protocol; |
74f1869f | 3983 | |
a3530df3 | 3984 | return auth; |
4e7a5dcd SW |
3985 | } |
3986 | ||
3987 | ||
0dde5848 | 3988 | static int verify_authorizer_reply(struct ceph_connection *con) |
4e7a5dcd SW |
3989 | { |
3990 | struct ceph_mds_session *s = con->private; | |
3991 | struct ceph_mds_client *mdsc = s->s_mdsc; | |
3d14c5d2 | 3992 | struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth; |
4e7a5dcd | 3993 | |
0dde5848 | 3994 | return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer); |
4e7a5dcd SW |
3995 | } |
3996 | ||
9bd2e6f8 SW |
3997 | static int invalidate_authorizer(struct ceph_connection *con) |
3998 | { | |
3999 | struct ceph_mds_session *s = con->private; | |
4000 | struct ceph_mds_client *mdsc = s->s_mdsc; | |
3d14c5d2 | 4001 | struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth; |
9bd2e6f8 | 4002 | |
27859f97 | 4003 | ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS); |
9bd2e6f8 | 4004 | |
3d14c5d2 | 4005 | return ceph_monc_validate_auth(&mdsc->fsc->client->monc); |
9bd2e6f8 SW |
4006 | } |
4007 | ||
53ded495 AE |
4008 | static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con, |
4009 | struct ceph_msg_header *hdr, int *skip) | |
4010 | { | |
4011 | struct ceph_msg *msg; | |
4012 | int type = (int) le16_to_cpu(hdr->type); | |
4013 | int front_len = (int) le32_to_cpu(hdr->front_len); | |
4014 | ||
4015 | if (con->in_msg) | |
4016 | return con->in_msg; | |
4017 | ||
4018 | *skip = 0; | |
4019 | msg = ceph_msg_new(type, front_len, GFP_NOFS, false); | |
4020 | if (!msg) { | |
4021 | pr_err("unable to allocate msg type %d len %d\n", | |
4022 | type, front_len); | |
4023 | return NULL; | |
4024 | } | |
53ded495 AE |
4025 | |
4026 | return msg; | |
4027 | } | |
4028 | ||
79dbd1ba | 4029 | static int mds_sign_message(struct ceph_msg *msg) |
33d07337 | 4030 | { |
79dbd1ba | 4031 | struct ceph_mds_session *s = msg->con->private; |
33d07337 | 4032 | struct ceph_auth_handshake *auth = &s->s_auth; |
79dbd1ba | 4033 | |
33d07337 YZ |
4034 | return ceph_auth_sign_message(auth, msg); |
4035 | } | |
4036 | ||
79dbd1ba | 4037 | static int mds_check_message_signature(struct ceph_msg *msg) |
33d07337 | 4038 | { |
79dbd1ba | 4039 | struct ceph_mds_session *s = msg->con->private; |
33d07337 | 4040 | struct ceph_auth_handshake *auth = &s->s_auth; |
79dbd1ba | 4041 | |
33d07337 YZ |
4042 | return ceph_auth_check_message_signature(auth, msg); |
4043 | } | |
4044 | ||
9e32789f | 4045 | static const struct ceph_connection_operations mds_con_ops = { |
2f2dc053 SW |
4046 | .get = con_get, |
4047 | .put = con_put, | |
4048 | .dispatch = dispatch, | |
4e7a5dcd SW |
4049 | .get_authorizer = get_authorizer, |
4050 | .verify_authorizer_reply = verify_authorizer_reply, | |
9bd2e6f8 | 4051 | .invalidate_authorizer = invalidate_authorizer, |
2f2dc053 | 4052 | .peer_reset = peer_reset, |
53ded495 | 4053 | .alloc_msg = mds_alloc_msg, |
79dbd1ba ID |
4054 | .sign_message = mds_sign_message, |
4055 | .check_message_signature = mds_check_message_signature, | |
2f2dc053 SW |
4056 | }; |
4057 | ||
2f2dc053 | 4058 | /* eof */ |