]>
Commit | Line | Data |
---|---|---|
2f2dc053 SW |
1 | #include "ceph_debug.h" |
2 | ||
3 | #include <linux/wait.h> | |
4 | #include <linux/sched.h> | |
5 | ||
6 | #include "mds_client.h" | |
7 | #include "mon_client.h" | |
8 | #include "super.h" | |
9 | #include "messenger.h" | |
10 | #include "decode.h" | |
4e7a5dcd | 11 | #include "auth.h" |
2f2dc053 SW |
12 | |
13 | /* | |
14 | * A cluster of MDS (metadata server) daemons is responsible for | |
15 | * managing the file system namespace (the directory hierarchy and | |
16 | * inodes) and for coordinating shared access to storage. Metadata is | |
17 | * partitioning hierarchically across a number of servers, and that | |
18 | * partition varies over time as the cluster adjusts the distribution | |
19 | * in order to balance load. | |
20 | * | |
21 | * The MDS client is primarily responsible to managing synchronous | |
22 | * metadata requests for operations like open, unlink, and so forth. | |
23 | * If there is a MDS failure, we find out about it when we (possibly | |
24 | * request and) receive a new MDS map, and can resubmit affected | |
25 | * requests. | |
26 | * | |
27 | * For the most part, though, we take advantage of a lossless | |
28 | * communications channel to the MDS, and do not need to worry about | |
29 | * timing out or resubmitting requests. | |
30 | * | |
31 | * We maintain a stateful "session" with each MDS we interact with. | |
32 | * Within each session, we sent periodic heartbeat messages to ensure | |
33 | * any capabilities or leases we have been issues remain valid. If | |
34 | * the session times out and goes stale, our leases and capabilities | |
35 | * are no longer valid. | |
36 | */ | |
37 | ||
38 | static void __wake_requests(struct ceph_mds_client *mdsc, | |
39 | struct list_head *head); | |
40 | ||
41 | const static struct ceph_connection_operations mds_con_ops; | |
42 | ||
43 | ||
44 | /* | |
45 | * mds reply parsing | |
46 | */ | |
47 | ||
48 | /* | |
49 | * parse individual inode info | |
50 | */ | |
51 | static int parse_reply_info_in(void **p, void *end, | |
52 | struct ceph_mds_reply_info_in *info) | |
53 | { | |
54 | int err = -EIO; | |
55 | ||
56 | info->in = *p; | |
57 | *p += sizeof(struct ceph_mds_reply_inode) + | |
58 | sizeof(*info->in->fragtree.splits) * | |
59 | le32_to_cpu(info->in->fragtree.nsplits); | |
60 | ||
61 | ceph_decode_32_safe(p, end, info->symlink_len, bad); | |
62 | ceph_decode_need(p, end, info->symlink_len, bad); | |
63 | info->symlink = *p; | |
64 | *p += info->symlink_len; | |
65 | ||
66 | ceph_decode_32_safe(p, end, info->xattr_len, bad); | |
67 | ceph_decode_need(p, end, info->xattr_len, bad); | |
68 | info->xattr_data = *p; | |
69 | *p += info->xattr_len; | |
70 | return 0; | |
71 | bad: | |
72 | return err; | |
73 | } | |
74 | ||
75 | /* | |
76 | * parse a normal reply, which may contain a (dir+)dentry and/or a | |
77 | * target inode. | |
78 | */ | |
79 | static int parse_reply_info_trace(void **p, void *end, | |
80 | struct ceph_mds_reply_info_parsed *info) | |
81 | { | |
82 | int err; | |
83 | ||
84 | if (info->head->is_dentry) { | |
85 | err = parse_reply_info_in(p, end, &info->diri); | |
86 | if (err < 0) | |
87 | goto out_bad; | |
88 | ||
89 | if (unlikely(*p + sizeof(*info->dirfrag) > end)) | |
90 | goto bad; | |
91 | info->dirfrag = *p; | |
92 | *p += sizeof(*info->dirfrag) + | |
93 | sizeof(u32)*le32_to_cpu(info->dirfrag->ndist); | |
94 | if (unlikely(*p > end)) | |
95 | goto bad; | |
96 | ||
97 | ceph_decode_32_safe(p, end, info->dname_len, bad); | |
98 | ceph_decode_need(p, end, info->dname_len, bad); | |
99 | info->dname = *p; | |
100 | *p += info->dname_len; | |
101 | info->dlease = *p; | |
102 | *p += sizeof(*info->dlease); | |
103 | } | |
104 | ||
105 | if (info->head->is_target) { | |
106 | err = parse_reply_info_in(p, end, &info->targeti); | |
107 | if (err < 0) | |
108 | goto out_bad; | |
109 | } | |
110 | ||
111 | if (unlikely(*p != end)) | |
112 | goto bad; | |
113 | return 0; | |
114 | ||
115 | bad: | |
116 | err = -EIO; | |
117 | out_bad: | |
118 | pr_err("problem parsing mds trace %d\n", err); | |
119 | return err; | |
120 | } | |
121 | ||
122 | /* | |
123 | * parse readdir results | |
124 | */ | |
125 | static int parse_reply_info_dir(void **p, void *end, | |
126 | struct ceph_mds_reply_info_parsed *info) | |
127 | { | |
128 | u32 num, i = 0; | |
129 | int err; | |
130 | ||
131 | info->dir_dir = *p; | |
132 | if (*p + sizeof(*info->dir_dir) > end) | |
133 | goto bad; | |
134 | *p += sizeof(*info->dir_dir) + | |
135 | sizeof(u32)*le32_to_cpu(info->dir_dir->ndist); | |
136 | if (*p > end) | |
137 | goto bad; | |
138 | ||
139 | ceph_decode_need(p, end, sizeof(num) + 2, bad); | |
c89136ea SW |
140 | num = ceph_decode_32(p); |
141 | info->dir_end = ceph_decode_8(p); | |
142 | info->dir_complete = ceph_decode_8(p); | |
2f2dc053 SW |
143 | if (num == 0) |
144 | goto done; | |
145 | ||
146 | /* alloc large array */ | |
147 | info->dir_nr = num; | |
148 | info->dir_in = kcalloc(num, sizeof(*info->dir_in) + | |
149 | sizeof(*info->dir_dname) + | |
150 | sizeof(*info->dir_dname_len) + | |
151 | sizeof(*info->dir_dlease), | |
152 | GFP_NOFS); | |
153 | if (info->dir_in == NULL) { | |
154 | err = -ENOMEM; | |
155 | goto out_bad; | |
156 | } | |
157 | info->dir_dname = (void *)(info->dir_in + num); | |
158 | info->dir_dname_len = (void *)(info->dir_dname + num); | |
159 | info->dir_dlease = (void *)(info->dir_dname_len + num); | |
160 | ||
161 | while (num) { | |
162 | /* dentry */ | |
163 | ceph_decode_need(p, end, sizeof(u32)*2, bad); | |
c89136ea | 164 | info->dir_dname_len[i] = ceph_decode_32(p); |
2f2dc053 SW |
165 | ceph_decode_need(p, end, info->dir_dname_len[i], bad); |
166 | info->dir_dname[i] = *p; | |
167 | *p += info->dir_dname_len[i]; | |
168 | dout("parsed dir dname '%.*s'\n", info->dir_dname_len[i], | |
169 | info->dir_dname[i]); | |
170 | info->dir_dlease[i] = *p; | |
171 | *p += sizeof(struct ceph_mds_reply_lease); | |
172 | ||
173 | /* inode */ | |
174 | err = parse_reply_info_in(p, end, &info->dir_in[i]); | |
175 | if (err < 0) | |
176 | goto out_bad; | |
177 | i++; | |
178 | num--; | |
179 | } | |
180 | ||
181 | done: | |
182 | if (*p != end) | |
183 | goto bad; | |
184 | return 0; | |
185 | ||
186 | bad: | |
187 | err = -EIO; | |
188 | out_bad: | |
189 | pr_err("problem parsing dir contents %d\n", err); | |
190 | return err; | |
191 | } | |
192 | ||
193 | /* | |
194 | * parse entire mds reply | |
195 | */ | |
196 | static int parse_reply_info(struct ceph_msg *msg, | |
197 | struct ceph_mds_reply_info_parsed *info) | |
198 | { | |
199 | void *p, *end; | |
200 | u32 len; | |
201 | int err; | |
202 | ||
203 | info->head = msg->front.iov_base; | |
204 | p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head); | |
205 | end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head); | |
206 | ||
207 | /* trace */ | |
208 | ceph_decode_32_safe(&p, end, len, bad); | |
209 | if (len > 0) { | |
210 | err = parse_reply_info_trace(&p, p+len, info); | |
211 | if (err < 0) | |
212 | goto out_bad; | |
213 | } | |
214 | ||
215 | /* dir content */ | |
216 | ceph_decode_32_safe(&p, end, len, bad); | |
217 | if (len > 0) { | |
218 | err = parse_reply_info_dir(&p, p+len, info); | |
219 | if (err < 0) | |
220 | goto out_bad; | |
221 | } | |
222 | ||
223 | /* snap blob */ | |
224 | ceph_decode_32_safe(&p, end, len, bad); | |
225 | info->snapblob_len = len; | |
226 | info->snapblob = p; | |
227 | p += len; | |
228 | ||
229 | if (p != end) | |
230 | goto bad; | |
231 | return 0; | |
232 | ||
233 | bad: | |
234 | err = -EIO; | |
235 | out_bad: | |
236 | pr_err("mds parse_reply err %d\n", err); | |
237 | return err; | |
238 | } | |
239 | ||
240 | static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info) | |
241 | { | |
242 | kfree(info->dir_in); | |
243 | } | |
244 | ||
245 | ||
246 | /* | |
247 | * sessions | |
248 | */ | |
249 | static const char *session_state_name(int s) | |
250 | { | |
251 | switch (s) { | |
252 | case CEPH_MDS_SESSION_NEW: return "new"; | |
253 | case CEPH_MDS_SESSION_OPENING: return "opening"; | |
254 | case CEPH_MDS_SESSION_OPEN: return "open"; | |
255 | case CEPH_MDS_SESSION_HUNG: return "hung"; | |
256 | case CEPH_MDS_SESSION_CLOSING: return "closing"; | |
257 | case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting"; | |
258 | default: return "???"; | |
259 | } | |
260 | } | |
261 | ||
262 | static struct ceph_mds_session *get_session(struct ceph_mds_session *s) | |
263 | { | |
264 | if (atomic_inc_not_zero(&s->s_ref)) { | |
265 | dout("mdsc get_session %p %d -> %d\n", s, | |
266 | atomic_read(&s->s_ref)-1, atomic_read(&s->s_ref)); | |
267 | return s; | |
268 | } else { | |
269 | dout("mdsc get_session %p 0 -- FAIL", s); | |
270 | return NULL; | |
271 | } | |
272 | } | |
273 | ||
274 | void ceph_put_mds_session(struct ceph_mds_session *s) | |
275 | { | |
276 | dout("mdsc put_session %p %d -> %d\n", s, | |
277 | atomic_read(&s->s_ref), atomic_read(&s->s_ref)-1); | |
4e7a5dcd SW |
278 | if (atomic_dec_and_test(&s->s_ref)) { |
279 | if (s->s_authorizer) | |
280 | s->s_mdsc->client->monc.auth->ops->destroy_authorizer( | |
281 | s->s_mdsc->client->monc.auth, s->s_authorizer); | |
2f2dc053 | 282 | kfree(s); |
4e7a5dcd | 283 | } |
2f2dc053 SW |
284 | } |
285 | ||
286 | /* | |
287 | * called under mdsc->mutex | |
288 | */ | |
289 | struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc, | |
290 | int mds) | |
291 | { | |
292 | struct ceph_mds_session *session; | |
293 | ||
294 | if (mds >= mdsc->max_sessions || mdsc->sessions[mds] == NULL) | |
295 | return NULL; | |
296 | session = mdsc->sessions[mds]; | |
297 | dout("lookup_mds_session %p %d\n", session, | |
298 | atomic_read(&session->s_ref)); | |
299 | get_session(session); | |
300 | return session; | |
301 | } | |
302 | ||
303 | static bool __have_session(struct ceph_mds_client *mdsc, int mds) | |
304 | { | |
305 | if (mds >= mdsc->max_sessions) | |
306 | return false; | |
307 | return mdsc->sessions[mds]; | |
308 | } | |
309 | ||
310 | /* | |
311 | * create+register a new session for given mds. | |
312 | * called under mdsc->mutex. | |
313 | */ | |
314 | static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc, | |
315 | int mds) | |
316 | { | |
317 | struct ceph_mds_session *s; | |
318 | ||
319 | s = kzalloc(sizeof(*s), GFP_NOFS); | |
320 | s->s_mdsc = mdsc; | |
321 | s->s_mds = mds; | |
322 | s->s_state = CEPH_MDS_SESSION_NEW; | |
323 | s->s_ttl = 0; | |
324 | s->s_seq = 0; | |
325 | mutex_init(&s->s_mutex); | |
326 | ||
327 | ceph_con_init(mdsc->client->msgr, &s->s_con); | |
328 | s->s_con.private = s; | |
329 | s->s_con.ops = &mds_con_ops; | |
330 | s->s_con.peer_name.type = CEPH_ENTITY_TYPE_MDS; | |
331 | s->s_con.peer_name.num = cpu_to_le64(mds); | |
2f2dc053 SW |
332 | |
333 | spin_lock_init(&s->s_cap_lock); | |
334 | s->s_cap_gen = 0; | |
335 | s->s_cap_ttl = 0; | |
336 | s->s_renew_requested = 0; | |
337 | s->s_renew_seq = 0; | |
338 | INIT_LIST_HEAD(&s->s_caps); | |
339 | s->s_nr_caps = 0; | |
5dacf091 | 340 | s->s_trim_caps = 0; |
2f2dc053 SW |
341 | atomic_set(&s->s_ref, 1); |
342 | INIT_LIST_HEAD(&s->s_waiting); | |
343 | INIT_LIST_HEAD(&s->s_unsafe); | |
344 | s->s_num_cap_releases = 0; | |
5dacf091 | 345 | s->s_iterating_caps = false; |
2f2dc053 SW |
346 | INIT_LIST_HEAD(&s->s_cap_releases); |
347 | INIT_LIST_HEAD(&s->s_cap_releases_done); | |
348 | INIT_LIST_HEAD(&s->s_cap_flushing); | |
349 | INIT_LIST_HEAD(&s->s_cap_snaps_flushing); | |
350 | ||
351 | dout("register_session mds%d\n", mds); | |
352 | if (mds >= mdsc->max_sessions) { | |
353 | int newmax = 1 << get_count_order(mds+1); | |
354 | struct ceph_mds_session **sa; | |
355 | ||
356 | dout("register_session realloc to %d\n", newmax); | |
357 | sa = kcalloc(newmax, sizeof(void *), GFP_NOFS); | |
358 | if (sa == NULL) | |
42ce56e5 | 359 | goto fail_realloc; |
2f2dc053 SW |
360 | if (mdsc->sessions) { |
361 | memcpy(sa, mdsc->sessions, | |
362 | mdsc->max_sessions * sizeof(void *)); | |
363 | kfree(mdsc->sessions); | |
364 | } | |
365 | mdsc->sessions = sa; | |
366 | mdsc->max_sessions = newmax; | |
367 | } | |
368 | mdsc->sessions[mds] = s; | |
369 | atomic_inc(&s->s_ref); /* one ref to sessions[], one to caller */ | |
42ce56e5 SW |
370 | |
371 | ceph_con_open(&s->s_con, ceph_mdsmap_get_addr(mdsc->mdsmap, mds)); | |
372 | ||
2f2dc053 | 373 | return s; |
42ce56e5 SW |
374 | |
375 | fail_realloc: | |
376 | kfree(s); | |
377 | return ERR_PTR(-ENOMEM); | |
2f2dc053 SW |
378 | } |
379 | ||
380 | /* | |
381 | * called under mdsc->mutex | |
382 | */ | |
42ce56e5 SW |
383 | static void unregister_session(struct ceph_mds_client *mdsc, |
384 | struct ceph_mds_session *s) | |
2f2dc053 | 385 | { |
42ce56e5 SW |
386 | dout("unregister_session mds%d %p\n", s->s_mds, s); |
387 | mdsc->sessions[s->s_mds] = NULL; | |
388 | ceph_con_close(&s->s_con); | |
389 | ceph_put_mds_session(s); | |
2f2dc053 SW |
390 | } |
391 | ||
392 | /* | |
393 | * drop session refs in request. | |
394 | * | |
395 | * should be last request ref, or hold mdsc->mutex | |
396 | */ | |
397 | static void put_request_session(struct ceph_mds_request *req) | |
398 | { | |
399 | if (req->r_session) { | |
400 | ceph_put_mds_session(req->r_session); | |
401 | req->r_session = NULL; | |
402 | } | |
403 | } | |
404 | ||
153c8e6b | 405 | void ceph_mdsc_release_request(struct kref *kref) |
2f2dc053 | 406 | { |
153c8e6b SW |
407 | struct ceph_mds_request *req = container_of(kref, |
408 | struct ceph_mds_request, | |
409 | r_kref); | |
410 | if (req->r_request) | |
411 | ceph_msg_put(req->r_request); | |
412 | if (req->r_reply) { | |
413 | ceph_msg_put(req->r_reply); | |
414 | destroy_reply_info(&req->r_reply_info); | |
415 | } | |
416 | if (req->r_inode) { | |
417 | ceph_put_cap_refs(ceph_inode(req->r_inode), | |
418 | CEPH_CAP_PIN); | |
419 | iput(req->r_inode); | |
420 | } | |
421 | if (req->r_locked_dir) | |
422 | ceph_put_cap_refs(ceph_inode(req->r_locked_dir), | |
423 | CEPH_CAP_PIN); | |
424 | if (req->r_target_inode) | |
425 | iput(req->r_target_inode); | |
426 | if (req->r_dentry) | |
427 | dput(req->r_dentry); | |
428 | if (req->r_old_dentry) { | |
429 | ceph_put_cap_refs( | |
430 | ceph_inode(req->r_old_dentry->d_parent->d_inode), | |
431 | CEPH_CAP_PIN); | |
432 | dput(req->r_old_dentry); | |
2f2dc053 | 433 | } |
153c8e6b SW |
434 | kfree(req->r_path1); |
435 | kfree(req->r_path2); | |
436 | put_request_session(req); | |
437 | ceph_unreserve_caps(&req->r_caps_reservation); | |
438 | kfree(req); | |
2f2dc053 SW |
439 | } |
440 | ||
441 | /* | |
442 | * lookup session, bump ref if found. | |
443 | * | |
444 | * called under mdsc->mutex. | |
445 | */ | |
446 | static struct ceph_mds_request *__lookup_request(struct ceph_mds_client *mdsc, | |
447 | u64 tid) | |
448 | { | |
449 | struct ceph_mds_request *req; | |
450 | req = radix_tree_lookup(&mdsc->request_tree, tid); | |
451 | if (req) | |
452 | ceph_mdsc_get_request(req); | |
453 | return req; | |
454 | } | |
455 | ||
456 | /* | |
457 | * Register an in-flight request, and assign a tid. Link to directory | |
458 | * are modifying (if any). | |
459 | * | |
460 | * Called under mdsc->mutex. | |
461 | */ | |
462 | static void __register_request(struct ceph_mds_client *mdsc, | |
463 | struct ceph_mds_request *req, | |
464 | struct inode *dir) | |
465 | { | |
466 | req->r_tid = ++mdsc->last_tid; | |
467 | if (req->r_num_caps) | |
468 | ceph_reserve_caps(&req->r_caps_reservation, req->r_num_caps); | |
469 | dout("__register_request %p tid %lld\n", req, req->r_tid); | |
470 | ceph_mdsc_get_request(req); | |
471 | radix_tree_insert(&mdsc->request_tree, req->r_tid, (void *)req); | |
472 | ||
473 | if (dir) { | |
474 | struct ceph_inode_info *ci = ceph_inode(dir); | |
475 | ||
476 | spin_lock(&ci->i_unsafe_lock); | |
477 | req->r_unsafe_dir = dir; | |
478 | list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops); | |
479 | spin_unlock(&ci->i_unsafe_lock); | |
480 | } | |
481 | } | |
482 | ||
483 | static void __unregister_request(struct ceph_mds_client *mdsc, | |
484 | struct ceph_mds_request *req) | |
485 | { | |
486 | dout("__unregister_request %p tid %lld\n", req, req->r_tid); | |
487 | radix_tree_delete(&mdsc->request_tree, req->r_tid); | |
488 | ceph_mdsc_put_request(req); | |
489 | ||
490 | if (req->r_unsafe_dir) { | |
491 | struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir); | |
492 | ||
493 | spin_lock(&ci->i_unsafe_lock); | |
494 | list_del_init(&req->r_unsafe_dir_item); | |
495 | spin_unlock(&ci->i_unsafe_lock); | |
496 | } | |
497 | } | |
498 | ||
499 | /* | |
500 | * Choose mds to send request to next. If there is a hint set in the | |
501 | * request (e.g., due to a prior forward hint from the mds), use that. | |
502 | * Otherwise, consult frag tree and/or caps to identify the | |
503 | * appropriate mds. If all else fails, choose randomly. | |
504 | * | |
505 | * Called under mdsc->mutex. | |
506 | */ | |
507 | static int __choose_mds(struct ceph_mds_client *mdsc, | |
508 | struct ceph_mds_request *req) | |
509 | { | |
510 | struct inode *inode; | |
511 | struct ceph_inode_info *ci; | |
512 | struct ceph_cap *cap; | |
513 | int mode = req->r_direct_mode; | |
514 | int mds = -1; | |
515 | u32 hash = req->r_direct_hash; | |
516 | bool is_hash = req->r_direct_is_hash; | |
517 | ||
518 | /* | |
519 | * is there a specific mds we should try? ignore hint if we have | |
520 | * no session and the mds is not up (active or recovering). | |
521 | */ | |
522 | if (req->r_resend_mds >= 0 && | |
523 | (__have_session(mdsc, req->r_resend_mds) || | |
524 | ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) { | |
525 | dout("choose_mds using resend_mds mds%d\n", | |
526 | req->r_resend_mds); | |
527 | return req->r_resend_mds; | |
528 | } | |
529 | ||
530 | if (mode == USE_RANDOM_MDS) | |
531 | goto random; | |
532 | ||
533 | inode = NULL; | |
534 | if (req->r_inode) { | |
535 | inode = req->r_inode; | |
536 | } else if (req->r_dentry) { | |
537 | if (req->r_dentry->d_inode) { | |
538 | inode = req->r_dentry->d_inode; | |
539 | } else { | |
540 | inode = req->r_dentry->d_parent->d_inode; | |
541 | hash = req->r_dentry->d_name.hash; | |
542 | is_hash = true; | |
543 | } | |
544 | } | |
545 | dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash, | |
546 | (int)hash, mode); | |
547 | if (!inode) | |
548 | goto random; | |
549 | ci = ceph_inode(inode); | |
550 | ||
551 | if (is_hash && S_ISDIR(inode->i_mode)) { | |
552 | struct ceph_inode_frag frag; | |
553 | int found; | |
554 | ||
555 | ceph_choose_frag(ci, hash, &frag, &found); | |
556 | if (found) { | |
557 | if (mode == USE_ANY_MDS && frag.ndist > 0) { | |
558 | u8 r; | |
559 | ||
560 | /* choose a random replica */ | |
561 | get_random_bytes(&r, 1); | |
562 | r %= frag.ndist; | |
563 | mds = frag.dist[r]; | |
564 | dout("choose_mds %p %llx.%llx " | |
565 | "frag %u mds%d (%d/%d)\n", | |
566 | inode, ceph_vinop(inode), | |
567 | frag.frag, frag.mds, | |
568 | (int)r, frag.ndist); | |
569 | return mds; | |
570 | } | |
571 | ||
572 | /* since this file/dir wasn't known to be | |
573 | * replicated, then we want to look for the | |
574 | * authoritative mds. */ | |
575 | mode = USE_AUTH_MDS; | |
576 | if (frag.mds >= 0) { | |
577 | /* choose auth mds */ | |
578 | mds = frag.mds; | |
579 | dout("choose_mds %p %llx.%llx " | |
580 | "frag %u mds%d (auth)\n", | |
581 | inode, ceph_vinop(inode), frag.frag, mds); | |
582 | return mds; | |
583 | } | |
584 | } | |
585 | } | |
586 | ||
587 | spin_lock(&inode->i_lock); | |
588 | cap = NULL; | |
589 | if (mode == USE_AUTH_MDS) | |
590 | cap = ci->i_auth_cap; | |
591 | if (!cap && !RB_EMPTY_ROOT(&ci->i_caps)) | |
592 | cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node); | |
593 | if (!cap) { | |
594 | spin_unlock(&inode->i_lock); | |
595 | goto random; | |
596 | } | |
597 | mds = cap->session->s_mds; | |
598 | dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n", | |
599 | inode, ceph_vinop(inode), mds, | |
600 | cap == ci->i_auth_cap ? "auth " : "", cap); | |
601 | spin_unlock(&inode->i_lock); | |
602 | return mds; | |
603 | ||
604 | random: | |
605 | mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap); | |
606 | dout("choose_mds chose random mds%d\n", mds); | |
607 | return mds; | |
608 | } | |
609 | ||
610 | ||
611 | /* | |
612 | * session messages | |
613 | */ | |
614 | static struct ceph_msg *create_session_msg(u32 op, u64 seq) | |
615 | { | |
616 | struct ceph_msg *msg; | |
617 | struct ceph_mds_session_head *h; | |
618 | ||
619 | msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), 0, 0, NULL); | |
620 | if (IS_ERR(msg)) { | |
621 | pr_err("create_session_msg ENOMEM creating msg\n"); | |
622 | return ERR_PTR(PTR_ERR(msg)); | |
623 | } | |
624 | h = msg->front.iov_base; | |
625 | h->op = cpu_to_le32(op); | |
626 | h->seq = cpu_to_le64(seq); | |
627 | return msg; | |
628 | } | |
629 | ||
630 | /* | |
631 | * send session open request. | |
632 | * | |
633 | * called under mdsc->mutex | |
634 | */ | |
635 | static int __open_session(struct ceph_mds_client *mdsc, | |
636 | struct ceph_mds_session *session) | |
637 | { | |
638 | struct ceph_msg *msg; | |
639 | int mstate; | |
640 | int mds = session->s_mds; | |
641 | int err = 0; | |
642 | ||
643 | /* wait for mds to go active? */ | |
644 | mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds); | |
645 | dout("open_session to mds%d (%s)\n", mds, | |
646 | ceph_mds_state_name(mstate)); | |
647 | session->s_state = CEPH_MDS_SESSION_OPENING; | |
648 | session->s_renew_requested = jiffies; | |
649 | ||
650 | /* send connect message */ | |
651 | msg = create_session_msg(CEPH_SESSION_REQUEST_OPEN, session->s_seq); | |
652 | if (IS_ERR(msg)) { | |
653 | err = PTR_ERR(msg); | |
654 | goto out; | |
655 | } | |
656 | ceph_con_send(&session->s_con, msg); | |
657 | ||
658 | out: | |
659 | return 0; | |
660 | } | |
661 | ||
662 | /* | |
663 | * session caps | |
664 | */ | |
665 | ||
666 | /* | |
667 | * Free preallocated cap messages assigned to this session | |
668 | */ | |
669 | static void cleanup_cap_releases(struct ceph_mds_session *session) | |
670 | { | |
671 | struct ceph_msg *msg; | |
672 | ||
673 | spin_lock(&session->s_cap_lock); | |
674 | while (!list_empty(&session->s_cap_releases)) { | |
675 | msg = list_first_entry(&session->s_cap_releases, | |
676 | struct ceph_msg, list_head); | |
677 | list_del_init(&msg->list_head); | |
678 | ceph_msg_put(msg); | |
679 | } | |
680 | while (!list_empty(&session->s_cap_releases_done)) { | |
681 | msg = list_first_entry(&session->s_cap_releases_done, | |
682 | struct ceph_msg, list_head); | |
683 | list_del_init(&msg->list_head); | |
684 | ceph_msg_put(msg); | |
685 | } | |
686 | spin_unlock(&session->s_cap_lock); | |
687 | } | |
688 | ||
689 | /* | |
690 | * Helper to safely iterate over all caps associated with a session. | |
691 | * | |
692 | * caller must hold session s_mutex | |
693 | */ | |
694 | static int iterate_session_caps(struct ceph_mds_session *session, | |
695 | int (*cb)(struct inode *, struct ceph_cap *, | |
696 | void *), void *arg) | |
697 | { | |
698 | struct ceph_cap *cap, *ncap; | |
699 | struct inode *inode; | |
700 | int ret; | |
701 | ||
702 | dout("iterate_session_caps %p mds%d\n", session, session->s_mds); | |
703 | spin_lock(&session->s_cap_lock); | |
5dacf091 | 704 | session->s_iterating_caps = true; |
2f2dc053 SW |
705 | list_for_each_entry_safe(cap, ncap, &session->s_caps, session_caps) { |
706 | inode = igrab(&cap->ci->vfs_inode); | |
707 | if (!inode) | |
708 | continue; | |
709 | spin_unlock(&session->s_cap_lock); | |
710 | ret = cb(inode, cap, arg); | |
711 | iput(inode); | |
2f2dc053 | 712 | spin_lock(&session->s_cap_lock); |
5dacf091 SW |
713 | if (ret < 0) |
714 | goto out; | |
2f2dc053 | 715 | } |
5dacf091 SW |
716 | ret = 0; |
717 | out: | |
718 | session->s_iterating_caps = false; | |
2f2dc053 | 719 | spin_unlock(&session->s_cap_lock); |
5dacf091 | 720 | return ret; |
2f2dc053 SW |
721 | } |
722 | ||
723 | static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap, | |
724 | void *arg) | |
725 | { | |
726 | struct ceph_inode_info *ci = ceph_inode(inode); | |
727 | dout("removing cap %p, ci is %p, inode is %p\n", | |
728 | cap, ci, &ci->vfs_inode); | |
729 | ceph_remove_cap(cap); | |
730 | return 0; | |
731 | } | |
732 | ||
733 | /* | |
734 | * caller must hold session s_mutex | |
735 | */ | |
736 | static void remove_session_caps(struct ceph_mds_session *session) | |
737 | { | |
738 | dout("remove_session_caps on %p\n", session); | |
739 | iterate_session_caps(session, remove_session_caps_cb, NULL); | |
740 | BUG_ON(session->s_nr_caps > 0); | |
741 | cleanup_cap_releases(session); | |
742 | } | |
743 | ||
744 | /* | |
745 | * wake up any threads waiting on this session's caps. if the cap is | |
746 | * old (didn't get renewed on the client reconnect), remove it now. | |
747 | * | |
748 | * caller must hold s_mutex. | |
749 | */ | |
750 | static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap, | |
751 | void *arg) | |
752 | { | |
0dc2570f SW |
753 | struct ceph_inode_info *ci = ceph_inode(inode); |
754 | ||
755 | wake_up(&ci->i_cap_wq); | |
756 | if (arg) { | |
757 | spin_lock(&inode->i_lock); | |
758 | ci->i_wanted_max_size = 0; | |
759 | ci->i_requested_max_size = 0; | |
760 | spin_unlock(&inode->i_lock); | |
761 | } | |
2f2dc053 SW |
762 | return 0; |
763 | } | |
764 | ||
0dc2570f SW |
765 | static void wake_up_session_caps(struct ceph_mds_session *session, |
766 | int reconnect) | |
2f2dc053 SW |
767 | { |
768 | dout("wake_up_session_caps %p mds%d\n", session, session->s_mds); | |
0dc2570f SW |
769 | iterate_session_caps(session, wake_up_session_cb, |
770 | (void *)(unsigned long)reconnect); | |
2f2dc053 SW |
771 | } |
772 | ||
773 | /* | |
774 | * Send periodic message to MDS renewing all currently held caps. The | |
775 | * ack will reset the expiration for all caps from this session. | |
776 | * | |
777 | * caller holds s_mutex | |
778 | */ | |
779 | static int send_renew_caps(struct ceph_mds_client *mdsc, | |
780 | struct ceph_mds_session *session) | |
781 | { | |
782 | struct ceph_msg *msg; | |
783 | int state; | |
784 | ||
785 | if (time_after_eq(jiffies, session->s_cap_ttl) && | |
786 | time_after_eq(session->s_cap_ttl, session->s_renew_requested)) | |
787 | pr_info("mds%d caps stale\n", session->s_mds); | |
788 | ||
789 | /* do not try to renew caps until a recovering mds has reconnected | |
790 | * with its clients. */ | |
791 | state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds); | |
792 | if (state < CEPH_MDS_STATE_RECONNECT) { | |
793 | dout("send_renew_caps ignoring mds%d (%s)\n", | |
794 | session->s_mds, ceph_mds_state_name(state)); | |
795 | return 0; | |
796 | } | |
797 | ||
798 | dout("send_renew_caps to mds%d (%s)\n", session->s_mds, | |
799 | ceph_mds_state_name(state)); | |
800 | session->s_renew_requested = jiffies; | |
801 | msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS, | |
802 | ++session->s_renew_seq); | |
803 | if (IS_ERR(msg)) | |
804 | return PTR_ERR(msg); | |
805 | ceph_con_send(&session->s_con, msg); | |
806 | return 0; | |
807 | } | |
808 | ||
809 | /* | |
810 | * Note new cap ttl, and any transition from stale -> not stale (fresh?). | |
0dc2570f SW |
811 | * |
812 | * Called under session->s_mutex | |
2f2dc053 SW |
813 | */ |
814 | static void renewed_caps(struct ceph_mds_client *mdsc, | |
815 | struct ceph_mds_session *session, int is_renew) | |
816 | { | |
817 | int was_stale; | |
818 | int wake = 0; | |
819 | ||
820 | spin_lock(&session->s_cap_lock); | |
821 | was_stale = is_renew && (session->s_cap_ttl == 0 || | |
822 | time_after_eq(jiffies, session->s_cap_ttl)); | |
823 | ||
824 | session->s_cap_ttl = session->s_renew_requested + | |
825 | mdsc->mdsmap->m_session_timeout*HZ; | |
826 | ||
827 | if (was_stale) { | |
828 | if (time_before(jiffies, session->s_cap_ttl)) { | |
829 | pr_info("mds%d caps renewed\n", session->s_mds); | |
830 | wake = 1; | |
831 | } else { | |
832 | pr_info("mds%d caps still stale\n", session->s_mds); | |
833 | } | |
834 | } | |
835 | dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n", | |
836 | session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh", | |
837 | time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh"); | |
838 | spin_unlock(&session->s_cap_lock); | |
839 | ||
840 | if (wake) | |
0dc2570f | 841 | wake_up_session_caps(session, 0); |
2f2dc053 SW |
842 | } |
843 | ||
844 | /* | |
845 | * send a session close request | |
846 | */ | |
847 | static int request_close_session(struct ceph_mds_client *mdsc, | |
848 | struct ceph_mds_session *session) | |
849 | { | |
850 | struct ceph_msg *msg; | |
851 | int err = 0; | |
852 | ||
853 | dout("request_close_session mds%d state %s seq %lld\n", | |
854 | session->s_mds, session_state_name(session->s_state), | |
855 | session->s_seq); | |
856 | msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq); | |
857 | if (IS_ERR(msg)) | |
858 | err = PTR_ERR(msg); | |
859 | else | |
860 | ceph_con_send(&session->s_con, msg); | |
861 | return err; | |
862 | } | |
863 | ||
864 | /* | |
865 | * Called with s_mutex held. | |
866 | */ | |
867 | static int __close_session(struct ceph_mds_client *mdsc, | |
868 | struct ceph_mds_session *session) | |
869 | { | |
870 | if (session->s_state >= CEPH_MDS_SESSION_CLOSING) | |
871 | return 0; | |
872 | session->s_state = CEPH_MDS_SESSION_CLOSING; | |
873 | return request_close_session(mdsc, session); | |
874 | } | |
875 | ||
876 | /* | |
877 | * Trim old(er) caps. | |
878 | * | |
879 | * Because we can't cache an inode without one or more caps, we do | |
880 | * this indirectly: if a cap is unused, we prune its aliases, at which | |
881 | * point the inode will hopefully get dropped to. | |
882 | * | |
883 | * Yes, this is a bit sloppy. Our only real goal here is to respond to | |
884 | * memory pressure from the MDS, though, so it needn't be perfect. | |
885 | */ | |
886 | static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg) | |
887 | { | |
888 | struct ceph_mds_session *session = arg; | |
889 | struct ceph_inode_info *ci = ceph_inode(inode); | |
890 | int used, oissued, mine; | |
891 | ||
892 | if (session->s_trim_caps <= 0) | |
893 | return -1; | |
894 | ||
895 | spin_lock(&inode->i_lock); | |
896 | mine = cap->issued | cap->implemented; | |
897 | used = __ceph_caps_used(ci); | |
898 | oissued = __ceph_caps_issued_other(ci, cap); | |
899 | ||
900 | dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n", | |
901 | inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued), | |
902 | ceph_cap_string(used)); | |
903 | if (ci->i_dirty_caps) | |
904 | goto out; /* dirty caps */ | |
905 | if ((used & ~oissued) & mine) | |
906 | goto out; /* we need these caps */ | |
907 | ||
908 | session->s_trim_caps--; | |
909 | if (oissued) { | |
910 | /* we aren't the only cap.. just remove us */ | |
911 | __ceph_remove_cap(cap, NULL); | |
912 | } else { | |
913 | /* try to drop referring dentries */ | |
914 | spin_unlock(&inode->i_lock); | |
915 | d_prune_aliases(inode); | |
916 | dout("trim_caps_cb %p cap %p pruned, count now %d\n", | |
917 | inode, cap, atomic_read(&inode->i_count)); | |
918 | return 0; | |
919 | } | |
920 | ||
921 | out: | |
922 | spin_unlock(&inode->i_lock); | |
923 | return 0; | |
924 | } | |
925 | ||
926 | /* | |
927 | * Trim session cap count down to some max number. | |
928 | */ | |
929 | static int trim_caps(struct ceph_mds_client *mdsc, | |
930 | struct ceph_mds_session *session, | |
931 | int max_caps) | |
932 | { | |
933 | int trim_caps = session->s_nr_caps - max_caps; | |
934 | ||
935 | dout("trim_caps mds%d start: %d / %d, trim %d\n", | |
936 | session->s_mds, session->s_nr_caps, max_caps, trim_caps); | |
937 | if (trim_caps > 0) { | |
938 | session->s_trim_caps = trim_caps; | |
939 | iterate_session_caps(session, trim_caps_cb, session); | |
940 | dout("trim_caps mds%d done: %d / %d, trimmed %d\n", | |
941 | session->s_mds, session->s_nr_caps, max_caps, | |
942 | trim_caps - session->s_trim_caps); | |
5dacf091 | 943 | session->s_trim_caps = 0; |
2f2dc053 SW |
944 | } |
945 | return 0; | |
946 | } | |
947 | ||
948 | /* | |
949 | * Allocate cap_release messages. If there is a partially full message | |
950 | * in the queue, try to allocate enough to cover it's remainder, so that | |
951 | * we can send it immediately. | |
952 | * | |
953 | * Called under s_mutex. | |
954 | */ | |
955 | static int add_cap_releases(struct ceph_mds_client *mdsc, | |
956 | struct ceph_mds_session *session, | |
957 | int extra) | |
958 | { | |
959 | struct ceph_msg *msg; | |
960 | struct ceph_mds_cap_release *head; | |
961 | int err = -ENOMEM; | |
962 | ||
963 | if (extra < 0) | |
6b805185 | 964 | extra = mdsc->client->mount_args->cap_release_safety; |
2f2dc053 SW |
965 | |
966 | spin_lock(&session->s_cap_lock); | |
967 | ||
968 | if (!list_empty(&session->s_cap_releases)) { | |
969 | msg = list_first_entry(&session->s_cap_releases, | |
970 | struct ceph_msg, | |
971 | list_head); | |
972 | head = msg->front.iov_base; | |
973 | extra += CEPH_CAPS_PER_RELEASE - le32_to_cpu(head->num); | |
974 | } | |
975 | ||
976 | while (session->s_num_cap_releases < session->s_nr_caps + extra) { | |
977 | spin_unlock(&session->s_cap_lock); | |
978 | msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE, | |
979 | 0, 0, NULL); | |
980 | if (!msg) | |
981 | goto out_unlocked; | |
982 | dout("add_cap_releases %p msg %p now %d\n", session, msg, | |
983 | (int)msg->front.iov_len); | |
984 | head = msg->front.iov_base; | |
985 | head->num = cpu_to_le32(0); | |
986 | msg->front.iov_len = sizeof(*head); | |
987 | spin_lock(&session->s_cap_lock); | |
988 | list_add(&msg->list_head, &session->s_cap_releases); | |
989 | session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE; | |
990 | } | |
991 | ||
992 | if (!list_empty(&session->s_cap_releases)) { | |
993 | msg = list_first_entry(&session->s_cap_releases, | |
994 | struct ceph_msg, | |
995 | list_head); | |
996 | head = msg->front.iov_base; | |
997 | if (head->num) { | |
998 | dout(" queueing non-full %p (%d)\n", msg, | |
999 | le32_to_cpu(head->num)); | |
1000 | list_move_tail(&msg->list_head, | |
1001 | &session->s_cap_releases_done); | |
1002 | session->s_num_cap_releases -= | |
1003 | CEPH_CAPS_PER_RELEASE - le32_to_cpu(head->num); | |
1004 | } | |
1005 | } | |
1006 | err = 0; | |
1007 | spin_unlock(&session->s_cap_lock); | |
1008 | out_unlocked: | |
1009 | return err; | |
1010 | } | |
1011 | ||
1012 | /* | |
1013 | * flush all dirty inode data to disk. | |
1014 | * | |
1015 | * returns true if we've flushed through want_flush_seq | |
1016 | */ | |
1017 | static int check_cap_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq) | |
1018 | { | |
1019 | int mds, ret = 1; | |
1020 | ||
1021 | dout("check_cap_flush want %lld\n", want_flush_seq); | |
1022 | mutex_lock(&mdsc->mutex); | |
1023 | for (mds = 0; ret && mds < mdsc->max_sessions; mds++) { | |
1024 | struct ceph_mds_session *session = mdsc->sessions[mds]; | |
1025 | ||
1026 | if (!session) | |
1027 | continue; | |
1028 | get_session(session); | |
1029 | mutex_unlock(&mdsc->mutex); | |
1030 | ||
1031 | mutex_lock(&session->s_mutex); | |
1032 | if (!list_empty(&session->s_cap_flushing)) { | |
1033 | struct ceph_inode_info *ci = | |
1034 | list_entry(session->s_cap_flushing.next, | |
1035 | struct ceph_inode_info, | |
1036 | i_flushing_item); | |
1037 | struct inode *inode = &ci->vfs_inode; | |
1038 | ||
1039 | spin_lock(&inode->i_lock); | |
1040 | if (ci->i_cap_flush_seq <= want_flush_seq) { | |
1041 | dout("check_cap_flush still flushing %p " | |
1042 | "seq %lld <= %lld to mds%d\n", inode, | |
1043 | ci->i_cap_flush_seq, want_flush_seq, | |
1044 | session->s_mds); | |
1045 | ret = 0; | |
1046 | } | |
1047 | spin_unlock(&inode->i_lock); | |
1048 | } | |
1049 | mutex_unlock(&session->s_mutex); | |
1050 | ceph_put_mds_session(session); | |
1051 | ||
1052 | if (!ret) | |
1053 | return ret; | |
1054 | mutex_lock(&mdsc->mutex); | |
1055 | } | |
1056 | ||
1057 | mutex_unlock(&mdsc->mutex); | |
1058 | dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq); | |
1059 | return ret; | |
1060 | } | |
1061 | ||
1062 | /* | |
1063 | * called under s_mutex | |
1064 | */ | |
1065 | static void send_cap_releases(struct ceph_mds_client *mdsc, | |
1066 | struct ceph_mds_session *session) | |
1067 | { | |
1068 | struct ceph_msg *msg; | |
1069 | ||
1070 | dout("send_cap_releases mds%d\n", session->s_mds); | |
1071 | while (1) { | |
1072 | spin_lock(&session->s_cap_lock); | |
1073 | if (list_empty(&session->s_cap_releases_done)) | |
1074 | break; | |
1075 | msg = list_first_entry(&session->s_cap_releases_done, | |
1076 | struct ceph_msg, list_head); | |
1077 | list_del_init(&msg->list_head); | |
1078 | spin_unlock(&session->s_cap_lock); | |
1079 | msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); | |
1080 | dout("send_cap_releases mds%d %p\n", session->s_mds, msg); | |
1081 | ceph_con_send(&session->s_con, msg); | |
1082 | } | |
1083 | spin_unlock(&session->s_cap_lock); | |
1084 | } | |
1085 | ||
1086 | /* | |
1087 | * requests | |
1088 | */ | |
1089 | ||
1090 | /* | |
1091 | * Create an mds request. | |
1092 | */ | |
1093 | struct ceph_mds_request * | |
1094 | ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode) | |
1095 | { | |
1096 | struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS); | |
1097 | ||
1098 | if (!req) | |
1099 | return ERR_PTR(-ENOMEM); | |
1100 | ||
1101 | req->r_started = jiffies; | |
1102 | req->r_resend_mds = -1; | |
1103 | INIT_LIST_HEAD(&req->r_unsafe_dir_item); | |
1104 | req->r_fmode = -1; | |
153c8e6b | 1105 | kref_init(&req->r_kref); |
2f2dc053 SW |
1106 | INIT_LIST_HEAD(&req->r_wait); |
1107 | init_completion(&req->r_completion); | |
1108 | init_completion(&req->r_safe_completion); | |
1109 | INIT_LIST_HEAD(&req->r_unsafe_item); | |
1110 | ||
1111 | req->r_op = op; | |
1112 | req->r_direct_mode = mode; | |
1113 | return req; | |
1114 | } | |
1115 | ||
1116 | /* | |
1117 | * return oldest (lowest) tid in request tree, 0 if none. | |
1118 | * | |
1119 | * called under mdsc->mutex. | |
1120 | */ | |
1121 | static u64 __get_oldest_tid(struct ceph_mds_client *mdsc) | |
1122 | { | |
1123 | struct ceph_mds_request *first; | |
1124 | if (radix_tree_gang_lookup(&mdsc->request_tree, | |
1125 | (void **)&first, 0, 1) <= 0) | |
1126 | return 0; | |
1127 | return first->r_tid; | |
1128 | } | |
1129 | ||
1130 | /* | |
1131 | * Build a dentry's path. Allocate on heap; caller must kfree. Based | |
1132 | * on build_path_from_dentry in fs/cifs/dir.c. | |
1133 | * | |
1134 | * If @stop_on_nosnap, generate path relative to the first non-snapped | |
1135 | * inode. | |
1136 | * | |
1137 | * Encode hidden .snap dirs as a double /, i.e. | |
1138 | * foo/.snap/bar -> foo//bar | |
1139 | */ | |
1140 | char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base, | |
1141 | int stop_on_nosnap) | |
1142 | { | |
1143 | struct dentry *temp; | |
1144 | char *path; | |
1145 | int len, pos; | |
1146 | ||
1147 | if (dentry == NULL) | |
1148 | return ERR_PTR(-EINVAL); | |
1149 | ||
1150 | retry: | |
1151 | len = 0; | |
1152 | for (temp = dentry; !IS_ROOT(temp);) { | |
1153 | struct inode *inode = temp->d_inode; | |
1154 | if (inode && ceph_snap(inode) == CEPH_SNAPDIR) | |
1155 | len++; /* slash only */ | |
1156 | else if (stop_on_nosnap && inode && | |
1157 | ceph_snap(inode) == CEPH_NOSNAP) | |
1158 | break; | |
1159 | else | |
1160 | len += 1 + temp->d_name.len; | |
1161 | temp = temp->d_parent; | |
1162 | if (temp == NULL) { | |
1163 | pr_err("build_path_dentry corrupt dentry %p\n", dentry); | |
1164 | return ERR_PTR(-EINVAL); | |
1165 | } | |
1166 | } | |
1167 | if (len) | |
1168 | len--; /* no leading '/' */ | |
1169 | ||
1170 | path = kmalloc(len+1, GFP_NOFS); | |
1171 | if (path == NULL) | |
1172 | return ERR_PTR(-ENOMEM); | |
1173 | pos = len; | |
1174 | path[pos] = 0; /* trailing null */ | |
1175 | for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) { | |
1176 | struct inode *inode = temp->d_inode; | |
1177 | ||
1178 | if (inode && ceph_snap(inode) == CEPH_SNAPDIR) { | |
1179 | dout("build_path_dentry path+%d: %p SNAPDIR\n", | |
1180 | pos, temp); | |
1181 | } else if (stop_on_nosnap && inode && | |
1182 | ceph_snap(inode) == CEPH_NOSNAP) { | |
1183 | break; | |
1184 | } else { | |
1185 | pos -= temp->d_name.len; | |
1186 | if (pos < 0) | |
1187 | break; | |
1188 | strncpy(path + pos, temp->d_name.name, | |
1189 | temp->d_name.len); | |
1190 | dout("build_path_dentry path+%d: %p '%.*s'\n", | |
1191 | pos, temp, temp->d_name.len, path + pos); | |
1192 | } | |
1193 | if (pos) | |
1194 | path[--pos] = '/'; | |
1195 | temp = temp->d_parent; | |
1196 | if (temp == NULL) { | |
1197 | pr_err("build_path_dentry corrupt dentry\n"); | |
1198 | kfree(path); | |
1199 | return ERR_PTR(-EINVAL); | |
1200 | } | |
1201 | } | |
1202 | if (pos != 0) { | |
1203 | pr_err("build_path_dentry did not end path lookup where " | |
1204 | "expected, namelen is %d, pos is %d\n", len, pos); | |
1205 | /* presumably this is only possible if racing with a | |
1206 | rename of one of the parent directories (we can not | |
1207 | lock the dentries above us to prevent this, but | |
1208 | retrying should be harmless) */ | |
1209 | kfree(path); | |
1210 | goto retry; | |
1211 | } | |
1212 | ||
1213 | *base = ceph_ino(temp->d_inode); | |
1214 | *plen = len; | |
1215 | dout("build_path_dentry on %p %d built %llx '%.*s'\n", | |
1216 | dentry, atomic_read(&dentry->d_count), *base, len, path); | |
1217 | return path; | |
1218 | } | |
1219 | ||
1220 | static int build_dentry_path(struct dentry *dentry, | |
1221 | const char **ppath, int *ppathlen, u64 *pino, | |
1222 | int *pfreepath) | |
1223 | { | |
1224 | char *path; | |
1225 | ||
1226 | if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) { | |
1227 | *pino = ceph_ino(dentry->d_parent->d_inode); | |
1228 | *ppath = dentry->d_name.name; | |
1229 | *ppathlen = dentry->d_name.len; | |
1230 | return 0; | |
1231 | } | |
1232 | path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1); | |
1233 | if (IS_ERR(path)) | |
1234 | return PTR_ERR(path); | |
1235 | *ppath = path; | |
1236 | *pfreepath = 1; | |
1237 | return 0; | |
1238 | } | |
1239 | ||
1240 | static int build_inode_path(struct inode *inode, | |
1241 | const char **ppath, int *ppathlen, u64 *pino, | |
1242 | int *pfreepath) | |
1243 | { | |
1244 | struct dentry *dentry; | |
1245 | char *path; | |
1246 | ||
1247 | if (ceph_snap(inode) == CEPH_NOSNAP) { | |
1248 | *pino = ceph_ino(inode); | |
1249 | *ppathlen = 0; | |
1250 | return 0; | |
1251 | } | |
1252 | dentry = d_find_alias(inode); | |
1253 | path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1); | |
1254 | dput(dentry); | |
1255 | if (IS_ERR(path)) | |
1256 | return PTR_ERR(path); | |
1257 | *ppath = path; | |
1258 | *pfreepath = 1; | |
1259 | return 0; | |
1260 | } | |
1261 | ||
1262 | /* | |
1263 | * request arguments may be specified via an inode *, a dentry *, or | |
1264 | * an explicit ino+path. | |
1265 | */ | |
1266 | static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry, | |
1267 | const char *rpath, u64 rino, | |
1268 | const char **ppath, int *pathlen, | |
1269 | u64 *ino, int *freepath) | |
1270 | { | |
1271 | int r = 0; | |
1272 | ||
1273 | if (rinode) { | |
1274 | r = build_inode_path(rinode, ppath, pathlen, ino, freepath); | |
1275 | dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode), | |
1276 | ceph_snap(rinode)); | |
1277 | } else if (rdentry) { | |
1278 | r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath); | |
1279 | dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen, | |
1280 | *ppath); | |
1281 | } else if (rpath) { | |
1282 | *ino = rino; | |
1283 | *ppath = rpath; | |
1284 | *pathlen = strlen(rpath); | |
1285 | dout(" path %.*s\n", *pathlen, rpath); | |
1286 | } | |
1287 | ||
1288 | return r; | |
1289 | } | |
1290 | ||
1291 | /* | |
1292 | * called under mdsc->mutex | |
1293 | */ | |
1294 | static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc, | |
1295 | struct ceph_mds_request *req, | |
1296 | int mds) | |
1297 | { | |
1298 | struct ceph_msg *msg; | |
1299 | struct ceph_mds_request_head *head; | |
1300 | const char *path1 = NULL; | |
1301 | const char *path2 = NULL; | |
1302 | u64 ino1 = 0, ino2 = 0; | |
1303 | int pathlen1 = 0, pathlen2 = 0; | |
1304 | int freepath1 = 0, freepath2 = 0; | |
1305 | int len; | |
1306 | u16 releases; | |
1307 | void *p, *end; | |
1308 | int ret; | |
1309 | ||
1310 | ret = set_request_path_attr(req->r_inode, req->r_dentry, | |
1311 | req->r_path1, req->r_ino1.ino, | |
1312 | &path1, &pathlen1, &ino1, &freepath1); | |
1313 | if (ret < 0) { | |
1314 | msg = ERR_PTR(ret); | |
1315 | goto out; | |
1316 | } | |
1317 | ||
1318 | ret = set_request_path_attr(NULL, req->r_old_dentry, | |
1319 | req->r_path2, req->r_ino2.ino, | |
1320 | &path2, &pathlen2, &ino2, &freepath2); | |
1321 | if (ret < 0) { | |
1322 | msg = ERR_PTR(ret); | |
1323 | goto out_free1; | |
1324 | } | |
1325 | ||
1326 | len = sizeof(*head) + | |
1327 | pathlen1 + pathlen2 + 2*(sizeof(u32) + sizeof(u64)); | |
1328 | ||
1329 | /* calculate (max) length for cap releases */ | |
1330 | len += sizeof(struct ceph_mds_request_release) * | |
1331 | (!!req->r_inode_drop + !!req->r_dentry_drop + | |
1332 | !!req->r_old_inode_drop + !!req->r_old_dentry_drop); | |
1333 | if (req->r_dentry_drop) | |
1334 | len += req->r_dentry->d_name.len; | |
1335 | if (req->r_old_dentry_drop) | |
1336 | len += req->r_old_dentry->d_name.len; | |
1337 | ||
1338 | msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, 0, 0, NULL); | |
1339 | if (IS_ERR(msg)) | |
1340 | goto out_free2; | |
1341 | ||
6df058c0 SW |
1342 | msg->hdr.tid = cpu_to_le64(req->r_tid); |
1343 | ||
2f2dc053 SW |
1344 | head = msg->front.iov_base; |
1345 | p = msg->front.iov_base + sizeof(*head); | |
1346 | end = msg->front.iov_base + msg->front.iov_len; | |
1347 | ||
1348 | head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch); | |
1349 | head->op = cpu_to_le32(req->r_op); | |
1350 | head->caller_uid = cpu_to_le32(current_fsuid()); | |
1351 | head->caller_gid = cpu_to_le32(current_fsgid()); | |
1352 | head->args = req->r_args; | |
1353 | ||
1354 | ceph_encode_filepath(&p, end, ino1, path1); | |
1355 | ceph_encode_filepath(&p, end, ino2, path2); | |
1356 | ||
1357 | /* cap releases */ | |
1358 | releases = 0; | |
1359 | if (req->r_inode_drop) | |
1360 | releases += ceph_encode_inode_release(&p, | |
1361 | req->r_inode ? req->r_inode : req->r_dentry->d_inode, | |
1362 | mds, req->r_inode_drop, req->r_inode_unless, 0); | |
1363 | if (req->r_dentry_drop) | |
1364 | releases += ceph_encode_dentry_release(&p, req->r_dentry, | |
1365 | mds, req->r_dentry_drop, req->r_dentry_unless); | |
1366 | if (req->r_old_dentry_drop) | |
1367 | releases += ceph_encode_dentry_release(&p, req->r_old_dentry, | |
1368 | mds, req->r_old_dentry_drop, req->r_old_dentry_unless); | |
1369 | if (req->r_old_inode_drop) | |
1370 | releases += ceph_encode_inode_release(&p, | |
1371 | req->r_old_dentry->d_inode, | |
1372 | mds, req->r_old_inode_drop, req->r_old_inode_unless, 0); | |
1373 | head->num_releases = cpu_to_le16(releases); | |
1374 | ||
1375 | BUG_ON(p > end); | |
1376 | msg->front.iov_len = p - msg->front.iov_base; | |
1377 | msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); | |
1378 | ||
1379 | msg->pages = req->r_pages; | |
1380 | msg->nr_pages = req->r_num_pages; | |
1381 | msg->hdr.data_len = cpu_to_le32(req->r_data_len); | |
1382 | msg->hdr.data_off = cpu_to_le16(0); | |
1383 | ||
1384 | out_free2: | |
1385 | if (freepath2) | |
1386 | kfree((char *)path2); | |
1387 | out_free1: | |
1388 | if (freepath1) | |
1389 | kfree((char *)path1); | |
1390 | out: | |
1391 | return msg; | |
1392 | } | |
1393 | ||
1394 | /* | |
1395 | * called under mdsc->mutex if error, under no mutex if | |
1396 | * success. | |
1397 | */ | |
1398 | static void complete_request(struct ceph_mds_client *mdsc, | |
1399 | struct ceph_mds_request *req) | |
1400 | { | |
1401 | if (req->r_callback) | |
1402 | req->r_callback(mdsc, req); | |
1403 | else | |
1404 | complete(&req->r_completion); | |
1405 | } | |
1406 | ||
1407 | /* | |
1408 | * called under mdsc->mutex | |
1409 | */ | |
1410 | static int __prepare_send_request(struct ceph_mds_client *mdsc, | |
1411 | struct ceph_mds_request *req, | |
1412 | int mds) | |
1413 | { | |
1414 | struct ceph_mds_request_head *rhead; | |
1415 | struct ceph_msg *msg; | |
1416 | int flags = 0; | |
1417 | ||
1418 | req->r_mds = mds; | |
1419 | req->r_attempts++; | |
1420 | dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req, | |
1421 | req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts); | |
1422 | ||
1423 | if (req->r_request) { | |
1424 | ceph_msg_put(req->r_request); | |
1425 | req->r_request = NULL; | |
1426 | } | |
1427 | msg = create_request_message(mdsc, req, mds); | |
1428 | if (IS_ERR(msg)) { | |
1429 | req->r_reply = ERR_PTR(PTR_ERR(msg)); | |
1430 | complete_request(mdsc, req); | |
1431 | return -PTR_ERR(msg); | |
1432 | } | |
1433 | req->r_request = msg; | |
1434 | ||
1435 | rhead = msg->front.iov_base; | |
2f2dc053 SW |
1436 | rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc)); |
1437 | if (req->r_got_unsafe) | |
1438 | flags |= CEPH_MDS_FLAG_REPLAY; | |
1439 | if (req->r_locked_dir) | |
1440 | flags |= CEPH_MDS_FLAG_WANT_DENTRY; | |
1441 | rhead->flags = cpu_to_le32(flags); | |
1442 | rhead->num_fwd = req->r_num_fwd; | |
1443 | rhead->num_retry = req->r_attempts - 1; | |
1444 | ||
1445 | dout(" r_locked_dir = %p\n", req->r_locked_dir); | |
1446 | ||
1447 | if (req->r_target_inode && req->r_got_unsafe) | |
1448 | rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode)); | |
1449 | else | |
1450 | rhead->ino = 0; | |
1451 | return 0; | |
1452 | } | |
1453 | ||
1454 | /* | |
1455 | * send request, or put it on the appropriate wait list. | |
1456 | */ | |
1457 | static int __do_request(struct ceph_mds_client *mdsc, | |
1458 | struct ceph_mds_request *req) | |
1459 | { | |
1460 | struct ceph_mds_session *session = NULL; | |
1461 | int mds = -1; | |
1462 | int err = -EAGAIN; | |
1463 | ||
1464 | if (req->r_reply) | |
1465 | goto out; | |
1466 | ||
1467 | if (req->r_timeout && | |
1468 | time_after_eq(jiffies, req->r_started + req->r_timeout)) { | |
1469 | dout("do_request timed out\n"); | |
1470 | err = -EIO; | |
1471 | goto finish; | |
1472 | } | |
1473 | ||
1474 | mds = __choose_mds(mdsc, req); | |
1475 | if (mds < 0 || | |
1476 | ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) { | |
1477 | dout("do_request no mds or not active, waiting for map\n"); | |
1478 | list_add(&req->r_wait, &mdsc->waiting_for_map); | |
1479 | goto out; | |
1480 | } | |
1481 | ||
1482 | /* get, open session */ | |
1483 | session = __ceph_lookup_mds_session(mdsc, mds); | |
1484 | if (!session) | |
1485 | session = register_session(mdsc, mds); | |
1486 | dout("do_request mds%d session %p state %s\n", mds, session, | |
1487 | session_state_name(session->s_state)); | |
1488 | if (session->s_state != CEPH_MDS_SESSION_OPEN && | |
1489 | session->s_state != CEPH_MDS_SESSION_HUNG) { | |
1490 | if (session->s_state == CEPH_MDS_SESSION_NEW || | |
1491 | session->s_state == CEPH_MDS_SESSION_CLOSING) | |
1492 | __open_session(mdsc, session); | |
1493 | list_add(&req->r_wait, &session->s_waiting); | |
1494 | goto out_session; | |
1495 | } | |
1496 | ||
1497 | /* send request */ | |
1498 | req->r_session = get_session(session); | |
1499 | req->r_resend_mds = -1; /* forget any previous mds hint */ | |
1500 | ||
1501 | if (req->r_request_started == 0) /* note request start time */ | |
1502 | req->r_request_started = jiffies; | |
1503 | ||
1504 | err = __prepare_send_request(mdsc, req, mds); | |
1505 | if (!err) { | |
1506 | ceph_msg_get(req->r_request); | |
1507 | ceph_con_send(&session->s_con, req->r_request); | |
1508 | } | |
1509 | ||
1510 | out_session: | |
1511 | ceph_put_mds_session(session); | |
1512 | out: | |
1513 | return err; | |
1514 | ||
1515 | finish: | |
1516 | req->r_reply = ERR_PTR(err); | |
1517 | complete_request(mdsc, req); | |
1518 | goto out; | |
1519 | } | |
1520 | ||
1521 | /* | |
1522 | * called under mdsc->mutex | |
1523 | */ | |
1524 | static void __wake_requests(struct ceph_mds_client *mdsc, | |
1525 | struct list_head *head) | |
1526 | { | |
1527 | struct ceph_mds_request *req, *nreq; | |
1528 | ||
1529 | list_for_each_entry_safe(req, nreq, head, r_wait) { | |
1530 | list_del_init(&req->r_wait); | |
1531 | __do_request(mdsc, req); | |
1532 | } | |
1533 | } | |
1534 | ||
1535 | /* | |
1536 | * Wake up threads with requests pending for @mds, so that they can | |
1537 | * resubmit their requests to a possibly different mds. If @all is set, | |
1538 | * wake up if their requests has been forwarded to @mds, too. | |
1539 | */ | |
1540 | static void kick_requests(struct ceph_mds_client *mdsc, int mds, int all) | |
1541 | { | |
1542 | struct ceph_mds_request *reqs[10]; | |
1543 | u64 nexttid = 0; | |
1544 | int i, got; | |
1545 | ||
1546 | dout("kick_requests mds%d\n", mds); | |
1547 | while (nexttid <= mdsc->last_tid) { | |
1548 | got = radix_tree_gang_lookup(&mdsc->request_tree, | |
1549 | (void **)&reqs, nexttid, 10); | |
1550 | if (got == 0) | |
1551 | break; | |
1552 | nexttid = reqs[got-1]->r_tid + 1; | |
1553 | for (i = 0; i < got; i++) { | |
1554 | if (reqs[i]->r_got_unsafe) | |
1555 | continue; | |
1556 | if (reqs[i]->r_session && | |
1557 | reqs[i]->r_session->s_mds == mds) { | |
1558 | dout(" kicking tid %llu\n", reqs[i]->r_tid); | |
1559 | put_request_session(reqs[i]); | |
1560 | __do_request(mdsc, reqs[i]); | |
1561 | } | |
1562 | } | |
1563 | } | |
1564 | } | |
1565 | ||
1566 | void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc, | |
1567 | struct ceph_mds_request *req) | |
1568 | { | |
1569 | dout("submit_request on %p\n", req); | |
1570 | mutex_lock(&mdsc->mutex); | |
1571 | __register_request(mdsc, req, NULL); | |
1572 | __do_request(mdsc, req); | |
1573 | mutex_unlock(&mdsc->mutex); | |
1574 | } | |
1575 | ||
1576 | /* | |
1577 | * Synchrously perform an mds request. Take care of all of the | |
1578 | * session setup, forwarding, retry details. | |
1579 | */ | |
1580 | int ceph_mdsc_do_request(struct ceph_mds_client *mdsc, | |
1581 | struct inode *dir, | |
1582 | struct ceph_mds_request *req) | |
1583 | { | |
1584 | int err; | |
1585 | ||
1586 | dout("do_request on %p\n", req); | |
1587 | ||
1588 | /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */ | |
1589 | if (req->r_inode) | |
1590 | ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN); | |
1591 | if (req->r_locked_dir) | |
1592 | ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN); | |
1593 | if (req->r_old_dentry) | |
1594 | ceph_get_cap_refs( | |
1595 | ceph_inode(req->r_old_dentry->d_parent->d_inode), | |
1596 | CEPH_CAP_PIN); | |
1597 | ||
1598 | /* issue */ | |
1599 | mutex_lock(&mdsc->mutex); | |
1600 | __register_request(mdsc, req, dir); | |
1601 | __do_request(mdsc, req); | |
1602 | ||
1603 | /* wait */ | |
1604 | if (!req->r_reply) { | |
1605 | mutex_unlock(&mdsc->mutex); | |
1606 | if (req->r_timeout) { | |
e2885f06 SW |
1607 | err = (long)wait_for_completion_interruptible_timeout( |
1608 | &req->r_completion, req->r_timeout); | |
1609 | if (err == 0) | |
2f2dc053 | 1610 | req->r_reply = ERR_PTR(-EIO); |
e2885f06 SW |
1611 | else if (err < 0) |
1612 | req->r_reply = ERR_PTR(err); | |
2f2dc053 | 1613 | } else { |
e2885f06 SW |
1614 | err = wait_for_completion_interruptible( |
1615 | &req->r_completion); | |
1616 | if (err) | |
1617 | req->r_reply = ERR_PTR(err); | |
2f2dc053 SW |
1618 | } |
1619 | mutex_lock(&mdsc->mutex); | |
1620 | } | |
1621 | ||
1622 | if (IS_ERR(req->r_reply)) { | |
1623 | err = PTR_ERR(req->r_reply); | |
1624 | req->r_reply = NULL; | |
1625 | ||
1626 | /* clean up */ | |
1627 | __unregister_request(mdsc, req); | |
1628 | if (!list_empty(&req->r_unsafe_item)) | |
1629 | list_del_init(&req->r_unsafe_item); | |
1630 | complete(&req->r_safe_completion); | |
1631 | } else if (req->r_err) { | |
1632 | err = req->r_err; | |
1633 | } else { | |
1634 | err = le32_to_cpu(req->r_reply_info.head->result); | |
1635 | } | |
1636 | mutex_unlock(&mdsc->mutex); | |
1637 | ||
1638 | dout("do_request %p done, result %d\n", req, err); | |
1639 | return err; | |
1640 | } | |
1641 | ||
1642 | /* | |
1643 | * Handle mds reply. | |
1644 | * | |
1645 | * We take the session mutex and parse and process the reply immediately. | |
1646 | * This preserves the logical ordering of replies, capabilities, etc., sent | |
1647 | * by the MDS as they are applied to our local cache. | |
1648 | */ | |
1649 | static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg) | |
1650 | { | |
1651 | struct ceph_mds_client *mdsc = session->s_mdsc; | |
1652 | struct ceph_mds_request *req; | |
1653 | struct ceph_mds_reply_head *head = msg->front.iov_base; | |
1654 | struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */ | |
1655 | u64 tid; | |
1656 | int err, result; | |
1657 | int mds; | |
1658 | ||
1659 | if (msg->hdr.src.name.type != CEPH_ENTITY_TYPE_MDS) | |
1660 | return; | |
1661 | if (msg->front.iov_len < sizeof(*head)) { | |
1662 | pr_err("mdsc_handle_reply got corrupt (short) reply\n"); | |
9ec7cab1 | 1663 | ceph_msg_dump(msg); |
2f2dc053 SW |
1664 | return; |
1665 | } | |
1666 | ||
1667 | /* get request, session */ | |
6df058c0 | 1668 | tid = le64_to_cpu(msg->hdr.tid); |
2f2dc053 SW |
1669 | mutex_lock(&mdsc->mutex); |
1670 | req = __lookup_request(mdsc, tid); | |
1671 | if (!req) { | |
1672 | dout("handle_reply on unknown tid %llu\n", tid); | |
1673 | mutex_unlock(&mdsc->mutex); | |
1674 | return; | |
1675 | } | |
1676 | dout("handle_reply %p\n", req); | |
1677 | mds = le64_to_cpu(msg->hdr.src.name.num); | |
1678 | ||
1679 | /* correct session? */ | |
1680 | if (!req->r_session && req->r_session != session) { | |
1681 | pr_err("mdsc_handle_reply got %llu on session mds%d" | |
1682 | " not mds%d\n", tid, session->s_mds, | |
1683 | req->r_session ? req->r_session->s_mds : -1); | |
1684 | mutex_unlock(&mdsc->mutex); | |
1685 | goto out; | |
1686 | } | |
1687 | ||
1688 | /* dup? */ | |
1689 | if ((req->r_got_unsafe && !head->safe) || | |
1690 | (req->r_got_safe && head->safe)) { | |
1691 | pr_warning("got a dup %s reply on %llu from mds%d\n", | |
1692 | head->safe ? "safe" : "unsafe", tid, mds); | |
1693 | mutex_unlock(&mdsc->mutex); | |
1694 | goto out; | |
1695 | } | |
1696 | ||
1697 | result = le32_to_cpu(head->result); | |
1698 | ||
1699 | /* | |
1700 | * Tolerate 2 consecutive ESTALEs from the same mds. | |
1701 | * FIXME: we should be looking at the cap migrate_seq. | |
1702 | */ | |
1703 | if (result == -ESTALE) { | |
1704 | req->r_direct_mode = USE_AUTH_MDS; | |
1705 | req->r_num_stale++; | |
1706 | if (req->r_num_stale <= 2) { | |
1707 | __do_request(mdsc, req); | |
1708 | mutex_unlock(&mdsc->mutex); | |
1709 | goto out; | |
1710 | } | |
1711 | } else { | |
1712 | req->r_num_stale = 0; | |
1713 | } | |
1714 | ||
1715 | if (head->safe) { | |
1716 | req->r_got_safe = true; | |
1717 | __unregister_request(mdsc, req); | |
1718 | complete(&req->r_safe_completion); | |
1719 | ||
1720 | if (req->r_got_unsafe) { | |
1721 | /* | |
1722 | * We already handled the unsafe response, now do the | |
1723 | * cleanup. No need to examine the response; the MDS | |
1724 | * doesn't include any result info in the safe | |
1725 | * response. And even if it did, there is nothing | |
1726 | * useful we could do with a revised return value. | |
1727 | */ | |
1728 | dout("got safe reply %llu, mds%d\n", tid, mds); | |
1729 | list_del_init(&req->r_unsafe_item); | |
1730 | ||
1731 | /* last unsafe request during umount? */ | |
1732 | if (mdsc->stopping && !__get_oldest_tid(mdsc)) | |
1733 | complete(&mdsc->safe_umount_waiters); | |
1734 | mutex_unlock(&mdsc->mutex); | |
1735 | goto out; | |
1736 | } | |
1737 | } | |
1738 | ||
1739 | BUG_ON(req->r_reply); | |
1740 | ||
1741 | if (!head->safe) { | |
1742 | req->r_got_unsafe = true; | |
1743 | list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe); | |
1744 | } | |
1745 | ||
1746 | dout("handle_reply tid %lld result %d\n", tid, result); | |
1747 | rinfo = &req->r_reply_info; | |
1748 | err = parse_reply_info(msg, rinfo); | |
1749 | mutex_unlock(&mdsc->mutex); | |
1750 | ||
1751 | mutex_lock(&session->s_mutex); | |
1752 | if (err < 0) { | |
1753 | pr_err("mdsc_handle_reply got corrupt reply mds%d\n", mds); | |
9ec7cab1 | 1754 | ceph_msg_dump(msg); |
2f2dc053 SW |
1755 | goto out_err; |
1756 | } | |
1757 | ||
1758 | /* snap trace */ | |
1759 | if (rinfo->snapblob_len) { | |
1760 | down_write(&mdsc->snap_rwsem); | |
1761 | ceph_update_snap_trace(mdsc, rinfo->snapblob, | |
1762 | rinfo->snapblob + rinfo->snapblob_len, | |
1763 | le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP); | |
1764 | downgrade_write(&mdsc->snap_rwsem); | |
1765 | } else { | |
1766 | down_read(&mdsc->snap_rwsem); | |
1767 | } | |
1768 | ||
1769 | /* insert trace into our cache */ | |
1770 | err = ceph_fill_trace(mdsc->client->sb, req, req->r_session); | |
1771 | if (err == 0) { | |
1772 | if (result == 0 && rinfo->dir_nr) | |
1773 | ceph_readdir_prepopulate(req, req->r_session); | |
1774 | ceph_unreserve_caps(&req->r_caps_reservation); | |
1775 | } | |
1776 | ||
1777 | up_read(&mdsc->snap_rwsem); | |
1778 | out_err: | |
1779 | if (err) { | |
1780 | req->r_err = err; | |
1781 | } else { | |
1782 | req->r_reply = msg; | |
1783 | ceph_msg_get(msg); | |
1784 | } | |
1785 | ||
1786 | add_cap_releases(mdsc, req->r_session, -1); | |
1787 | mutex_unlock(&session->s_mutex); | |
1788 | ||
1789 | /* kick calling process */ | |
1790 | complete_request(mdsc, req); | |
1791 | out: | |
1792 | ceph_mdsc_put_request(req); | |
1793 | return; | |
1794 | } | |
1795 | ||
1796 | ||
1797 | ||
1798 | /* | |
1799 | * handle mds notification that our request has been forwarded. | |
1800 | */ | |
1801 | static void handle_forward(struct ceph_mds_client *mdsc, struct ceph_msg *msg) | |
1802 | { | |
1803 | struct ceph_mds_request *req; | |
1804 | u64 tid; | |
1805 | u32 next_mds; | |
1806 | u32 fwd_seq; | |
1807 | u8 must_resend; | |
1808 | int err = -EINVAL; | |
1809 | void *p = msg->front.iov_base; | |
1810 | void *end = p + msg->front.iov_len; | |
1811 | int from_mds, state; | |
1812 | ||
1813 | if (msg->hdr.src.name.type != CEPH_ENTITY_TYPE_MDS) | |
1814 | goto bad; | |
1815 | from_mds = le64_to_cpu(msg->hdr.src.name.num); | |
1816 | ||
1817 | ceph_decode_need(&p, end, sizeof(u64)+2*sizeof(u32), bad); | |
c89136ea SW |
1818 | tid = ceph_decode_64(&p); |
1819 | next_mds = ceph_decode_32(&p); | |
1820 | fwd_seq = ceph_decode_32(&p); | |
1821 | must_resend = ceph_decode_8(&p); | |
2f2dc053 SW |
1822 | |
1823 | WARN_ON(must_resend); /* shouldn't happen. */ | |
1824 | ||
1825 | mutex_lock(&mdsc->mutex); | |
1826 | req = __lookup_request(mdsc, tid); | |
1827 | if (!req) { | |
1828 | dout("forward %llu dne\n", tid); | |
1829 | goto out; /* dup reply? */ | |
1830 | } | |
1831 | ||
1832 | state = mdsc->sessions[next_mds]->s_state; | |
1833 | if (fwd_seq <= req->r_num_fwd) { | |
1834 | dout("forward %llu to mds%d - old seq %d <= %d\n", | |
1835 | tid, next_mds, req->r_num_fwd, fwd_seq); | |
1836 | } else { | |
1837 | /* resend. forward race not possible; mds would drop */ | |
1838 | dout("forward %llu to mds%d (we resend)\n", tid, next_mds); | |
1839 | req->r_num_fwd = fwd_seq; | |
1840 | req->r_resend_mds = next_mds; | |
1841 | put_request_session(req); | |
1842 | __do_request(mdsc, req); | |
1843 | } | |
1844 | ceph_mdsc_put_request(req); | |
1845 | out: | |
1846 | mutex_unlock(&mdsc->mutex); | |
1847 | return; | |
1848 | ||
1849 | bad: | |
1850 | pr_err("mdsc_handle_forward decode error err=%d\n", err); | |
1851 | } | |
1852 | ||
1853 | /* | |
1854 | * handle a mds session control message | |
1855 | */ | |
1856 | static void handle_session(struct ceph_mds_session *session, | |
1857 | struct ceph_msg *msg) | |
1858 | { | |
1859 | struct ceph_mds_client *mdsc = session->s_mdsc; | |
1860 | u32 op; | |
1861 | u64 seq; | |
1862 | int mds; | |
1863 | struct ceph_mds_session_head *h = msg->front.iov_base; | |
1864 | int wake = 0; | |
1865 | ||
1866 | if (msg->hdr.src.name.type != CEPH_ENTITY_TYPE_MDS) | |
1867 | return; | |
1868 | mds = le64_to_cpu(msg->hdr.src.name.num); | |
1869 | ||
1870 | /* decode */ | |
1871 | if (msg->front.iov_len != sizeof(*h)) | |
1872 | goto bad; | |
1873 | op = le32_to_cpu(h->op); | |
1874 | seq = le64_to_cpu(h->seq); | |
1875 | ||
1876 | mutex_lock(&mdsc->mutex); | |
1877 | /* FIXME: this ttl calculation is generous */ | |
1878 | session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose; | |
1879 | mutex_unlock(&mdsc->mutex); | |
1880 | ||
1881 | mutex_lock(&session->s_mutex); | |
1882 | ||
1883 | dout("handle_session mds%d %s %p state %s seq %llu\n", | |
1884 | mds, ceph_session_op_name(op), session, | |
1885 | session_state_name(session->s_state), seq); | |
1886 | ||
1887 | if (session->s_state == CEPH_MDS_SESSION_HUNG) { | |
1888 | session->s_state = CEPH_MDS_SESSION_OPEN; | |
1889 | pr_info("mds%d came back\n", session->s_mds); | |
1890 | } | |
1891 | ||
1892 | switch (op) { | |
1893 | case CEPH_SESSION_OPEN: | |
1894 | session->s_state = CEPH_MDS_SESSION_OPEN; | |
1895 | renewed_caps(mdsc, session, 0); | |
1896 | wake = 1; | |
1897 | if (mdsc->stopping) | |
1898 | __close_session(mdsc, session); | |
1899 | break; | |
1900 | ||
1901 | case CEPH_SESSION_RENEWCAPS: | |
1902 | if (session->s_renew_seq == seq) | |
1903 | renewed_caps(mdsc, session, 1); | |
1904 | break; | |
1905 | ||
1906 | case CEPH_SESSION_CLOSE: | |
42ce56e5 | 1907 | unregister_session(mdsc, session); |
2f2dc053 SW |
1908 | remove_session_caps(session); |
1909 | wake = 1; /* for good measure */ | |
1910 | complete(&mdsc->session_close_waiters); | |
1911 | kick_requests(mdsc, mds, 0); /* cur only */ | |
1912 | break; | |
1913 | ||
1914 | case CEPH_SESSION_STALE: | |
1915 | pr_info("mds%d caps went stale, renewing\n", | |
1916 | session->s_mds); | |
1917 | spin_lock(&session->s_cap_lock); | |
1918 | session->s_cap_gen++; | |
1919 | session->s_cap_ttl = 0; | |
1920 | spin_unlock(&session->s_cap_lock); | |
1921 | send_renew_caps(mdsc, session); | |
1922 | break; | |
1923 | ||
1924 | case CEPH_SESSION_RECALL_STATE: | |
1925 | trim_caps(mdsc, session, le32_to_cpu(h->max_caps)); | |
1926 | break; | |
1927 | ||
1928 | default: | |
1929 | pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds); | |
1930 | WARN_ON(1); | |
1931 | } | |
1932 | ||
1933 | mutex_unlock(&session->s_mutex); | |
1934 | if (wake) { | |
1935 | mutex_lock(&mdsc->mutex); | |
1936 | __wake_requests(mdsc, &session->s_waiting); | |
1937 | mutex_unlock(&mdsc->mutex); | |
1938 | } | |
1939 | return; | |
1940 | ||
1941 | bad: | |
1942 | pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds, | |
1943 | (int)msg->front.iov_len); | |
9ec7cab1 | 1944 | ceph_msg_dump(msg); |
2f2dc053 SW |
1945 | return; |
1946 | } | |
1947 | ||
1948 | ||
1949 | /* | |
1950 | * called under session->mutex. | |
1951 | */ | |
1952 | static void replay_unsafe_requests(struct ceph_mds_client *mdsc, | |
1953 | struct ceph_mds_session *session) | |
1954 | { | |
1955 | struct ceph_mds_request *req, *nreq; | |
1956 | int err; | |
1957 | ||
1958 | dout("replay_unsafe_requests mds%d\n", session->s_mds); | |
1959 | ||
1960 | mutex_lock(&mdsc->mutex); | |
1961 | list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) { | |
1962 | err = __prepare_send_request(mdsc, req, session->s_mds); | |
1963 | if (!err) { | |
1964 | ceph_msg_get(req->r_request); | |
1965 | ceph_con_send(&session->s_con, req->r_request); | |
1966 | } | |
1967 | } | |
1968 | mutex_unlock(&mdsc->mutex); | |
1969 | } | |
1970 | ||
1971 | /* | |
1972 | * Encode information about a cap for a reconnect with the MDS. | |
1973 | */ | |
1974 | struct encode_caps_data { | |
1975 | void **pp; | |
1976 | void *end; | |
1977 | int *num_caps; | |
1978 | }; | |
1979 | ||
1980 | static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap, | |
1981 | void *arg) | |
1982 | { | |
1983 | struct ceph_mds_cap_reconnect *rec; | |
1984 | struct ceph_inode_info *ci; | |
1985 | struct encode_caps_data *data = (struct encode_caps_data *)arg; | |
1986 | void *p = *(data->pp); | |
1987 | void *end = data->end; | |
1988 | char *path; | |
1989 | int pathlen, err; | |
1990 | u64 pathbase; | |
1991 | struct dentry *dentry; | |
1992 | ||
1993 | ci = cap->ci; | |
1994 | ||
1995 | dout(" adding %p ino %llx.%llx cap %p %lld %s\n", | |
1996 | inode, ceph_vinop(inode), cap, cap->cap_id, | |
1997 | ceph_cap_string(cap->issued)); | |
1998 | ceph_decode_need(&p, end, sizeof(u64), needmore); | |
1999 | ceph_encode_64(&p, ceph_ino(inode)); | |
2000 | ||
2001 | dentry = d_find_alias(inode); | |
2002 | if (dentry) { | |
2003 | path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0); | |
2004 | if (IS_ERR(path)) { | |
2005 | err = PTR_ERR(path); | |
2006 | BUG_ON(err); | |
2007 | } | |
2008 | } else { | |
2009 | path = NULL; | |
2010 | pathlen = 0; | |
2011 | } | |
2012 | ceph_decode_need(&p, end, pathlen+4, needmore); | |
2013 | ceph_encode_string(&p, end, path, pathlen); | |
2014 | ||
2015 | ceph_decode_need(&p, end, sizeof(*rec), needmore); | |
2016 | rec = p; | |
2017 | p += sizeof(*rec); | |
2018 | BUG_ON(p > end); | |
2019 | spin_lock(&inode->i_lock); | |
2020 | cap->seq = 0; /* reset cap seq */ | |
2021 | cap->issue_seq = 0; /* and issue_seq */ | |
2022 | rec->cap_id = cpu_to_le64(cap->cap_id); | |
2023 | rec->pathbase = cpu_to_le64(pathbase); | |
2024 | rec->wanted = cpu_to_le32(__ceph_caps_wanted(ci)); | |
2025 | rec->issued = cpu_to_le32(cap->issued); | |
2026 | rec->size = cpu_to_le64(inode->i_size); | |
2027 | ceph_encode_timespec(&rec->mtime, &inode->i_mtime); | |
2028 | ceph_encode_timespec(&rec->atime, &inode->i_atime); | |
2029 | rec->snaprealm = cpu_to_le64(ci->i_snap_realm->ino); | |
2030 | spin_unlock(&inode->i_lock); | |
2031 | ||
2032 | kfree(path); | |
2033 | dput(dentry); | |
2034 | (*data->num_caps)++; | |
2035 | *(data->pp) = p; | |
2036 | return 0; | |
2037 | needmore: | |
2038 | return -ENOSPC; | |
2039 | } | |
2040 | ||
2041 | ||
2042 | /* | |
2043 | * If an MDS fails and recovers, clients need to reconnect in order to | |
2044 | * reestablish shared state. This includes all caps issued through | |
2045 | * this session _and_ the snap_realm hierarchy. Because it's not | |
2046 | * clear which snap realms the mds cares about, we send everything we | |
2047 | * know about.. that ensures we'll then get any new info the | |
2048 | * recovering MDS might have. | |
2049 | * | |
2050 | * This is a relatively heavyweight operation, but it's rare. | |
2051 | * | |
2052 | * called with mdsc->mutex held. | |
2053 | */ | |
2054 | static void send_mds_reconnect(struct ceph_mds_client *mdsc, int mds) | |
2055 | { | |
2056 | struct ceph_mds_session *session; | |
2057 | struct ceph_msg *reply; | |
2058 | int newlen, len = 4 + 1; | |
2059 | void *p, *end; | |
2060 | int err; | |
2061 | int num_caps, num_realms = 0; | |
2062 | int got; | |
2063 | u64 next_snap_ino = 0; | |
2064 | __le32 *pnum_caps, *pnum_realms; | |
2065 | struct encode_caps_data iter_args; | |
2066 | ||
2067 | pr_info("reconnect to recovering mds%d\n", mds); | |
2068 | ||
2069 | /* find session */ | |
2070 | session = __ceph_lookup_mds_session(mdsc, mds); | |
2071 | mutex_unlock(&mdsc->mutex); /* drop lock for duration */ | |
2072 | ||
2073 | if (session) { | |
2074 | mutex_lock(&session->s_mutex); | |
2075 | ||
2076 | session->s_state = CEPH_MDS_SESSION_RECONNECTING; | |
2077 | session->s_seq = 0; | |
2078 | ||
2079 | ceph_con_open(&session->s_con, | |
2080 | ceph_mdsmap_get_addr(mdsc->mdsmap, mds)); | |
2081 | ||
2082 | /* replay unsafe requests */ | |
2083 | replay_unsafe_requests(mdsc, session); | |
2084 | ||
2085 | /* estimate needed space */ | |
2086 | len += session->s_nr_caps * | |
2087 | (100+sizeof(struct ceph_mds_cap_reconnect)); | |
2088 | pr_info("estimating i need %d bytes for %d caps\n", | |
2089 | len, session->s_nr_caps); | |
2090 | } else { | |
2091 | dout("no session for mds%d, will send short reconnect\n", | |
2092 | mds); | |
2093 | } | |
2094 | ||
2095 | down_read(&mdsc->snap_rwsem); | |
2096 | ||
2097 | retry: | |
2098 | /* build reply */ | |
2099 | reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, len, 0, 0, NULL); | |
2100 | if (IS_ERR(reply)) { | |
2101 | err = PTR_ERR(reply); | |
2102 | pr_err("send_mds_reconnect ENOMEM on %d for mds%d\n", | |
2103 | len, mds); | |
2104 | goto out; | |
2105 | } | |
2106 | p = reply->front.iov_base; | |
2107 | end = p + len; | |
2108 | ||
2109 | if (!session) { | |
2110 | ceph_encode_8(&p, 1); /* session was closed */ | |
2111 | ceph_encode_32(&p, 0); | |
2112 | goto send; | |
2113 | } | |
2114 | dout("session %p state %s\n", session, | |
2115 | session_state_name(session->s_state)); | |
2116 | ||
2117 | /* traverse this session's caps */ | |
2118 | ceph_encode_8(&p, 0); | |
2119 | pnum_caps = p; | |
2120 | ceph_encode_32(&p, session->s_nr_caps); | |
2121 | num_caps = 0; | |
2122 | ||
2123 | iter_args.pp = &p; | |
2124 | iter_args.end = end; | |
2125 | iter_args.num_caps = &num_caps; | |
2126 | err = iterate_session_caps(session, encode_caps_cb, &iter_args); | |
2127 | if (err == -ENOSPC) | |
2128 | goto needmore; | |
2129 | if (err < 0) | |
2130 | goto out; | |
2131 | *pnum_caps = cpu_to_le32(num_caps); | |
2132 | ||
2133 | /* | |
2134 | * snaprealms. we provide mds with the ino, seq (version), and | |
2135 | * parent for all of our realms. If the mds has any newer info, | |
2136 | * it will tell us. | |
2137 | */ | |
2138 | next_snap_ino = 0; | |
2139 | /* save some space for the snaprealm count */ | |
2140 | pnum_realms = p; | |
2141 | ceph_decode_need(&p, end, sizeof(*pnum_realms), needmore); | |
2142 | p += sizeof(*pnum_realms); | |
2143 | num_realms = 0; | |
2144 | while (1) { | |
2145 | struct ceph_snap_realm *realm; | |
2146 | struct ceph_mds_snaprealm_reconnect *sr_rec; | |
2147 | got = radix_tree_gang_lookup(&mdsc->snap_realms, | |
2148 | (void **)&realm, next_snap_ino, 1); | |
2149 | if (!got) | |
2150 | break; | |
2151 | ||
2152 | dout(" adding snap realm %llx seq %lld parent %llx\n", | |
2153 | realm->ino, realm->seq, realm->parent_ino); | |
2154 | ceph_decode_need(&p, end, sizeof(*sr_rec), needmore); | |
2155 | sr_rec = p; | |
2156 | sr_rec->ino = cpu_to_le64(realm->ino); | |
2157 | sr_rec->seq = cpu_to_le64(realm->seq); | |
2158 | sr_rec->parent = cpu_to_le64(realm->parent_ino); | |
2159 | p += sizeof(*sr_rec); | |
2160 | num_realms++; | |
2161 | next_snap_ino = realm->ino + 1; | |
2162 | } | |
2163 | *pnum_realms = cpu_to_le32(num_realms); | |
2164 | ||
2165 | send: | |
2166 | reply->front.iov_len = p - reply->front.iov_base; | |
2167 | reply->hdr.front_len = cpu_to_le32(reply->front.iov_len); | |
2168 | dout("final len was %u (guessed %d)\n", | |
2169 | (unsigned)reply->front.iov_len, len); | |
2170 | ceph_con_send(&session->s_con, reply); | |
2171 | ||
2172 | if (session) { | |
2173 | session->s_state = CEPH_MDS_SESSION_OPEN; | |
2174 | __wake_requests(mdsc, &session->s_waiting); | |
2175 | } | |
2176 | ||
2177 | out: | |
2178 | up_read(&mdsc->snap_rwsem); | |
2179 | if (session) { | |
2180 | mutex_unlock(&session->s_mutex); | |
2181 | ceph_put_mds_session(session); | |
2182 | } | |
2183 | mutex_lock(&mdsc->mutex); | |
2184 | return; | |
2185 | ||
2186 | needmore: | |
2187 | /* | |
2188 | * we need a larger buffer. this doesn't very accurately | |
2189 | * factor in snap realms, but it's safe. | |
2190 | */ | |
2191 | num_caps += num_realms; | |
2192 | newlen = len * ((100 * (session->s_nr_caps+3)) / (num_caps + 1)) / 100; | |
2193 | pr_info("i guessed %d, and did %d of %d caps, retrying with %d\n", | |
2194 | len, num_caps, session->s_nr_caps, newlen); | |
2195 | len = newlen; | |
2196 | ceph_msg_put(reply); | |
2197 | goto retry; | |
2198 | } | |
2199 | ||
2200 | ||
2201 | /* | |
2202 | * compare old and new mdsmaps, kicking requests | |
2203 | * and closing out old connections as necessary | |
2204 | * | |
2205 | * called under mdsc->mutex. | |
2206 | */ | |
2207 | static void check_new_map(struct ceph_mds_client *mdsc, | |
2208 | struct ceph_mdsmap *newmap, | |
2209 | struct ceph_mdsmap *oldmap) | |
2210 | { | |
2211 | int i; | |
2212 | int oldstate, newstate; | |
2213 | struct ceph_mds_session *s; | |
2214 | ||
2215 | dout("check_new_map new %u old %u\n", | |
2216 | newmap->m_epoch, oldmap->m_epoch); | |
2217 | ||
2218 | for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) { | |
2219 | if (mdsc->sessions[i] == NULL) | |
2220 | continue; | |
2221 | s = mdsc->sessions[i]; | |
2222 | oldstate = ceph_mdsmap_get_state(oldmap, i); | |
2223 | newstate = ceph_mdsmap_get_state(newmap, i); | |
2224 | ||
2225 | dout("check_new_map mds%d state %s -> %s (session %s)\n", | |
2226 | i, ceph_mds_state_name(oldstate), | |
2227 | ceph_mds_state_name(newstate), | |
2228 | session_state_name(s->s_state)); | |
2229 | ||
2230 | if (memcmp(ceph_mdsmap_get_addr(oldmap, i), | |
2231 | ceph_mdsmap_get_addr(newmap, i), | |
2232 | sizeof(struct ceph_entity_addr))) { | |
2233 | if (s->s_state == CEPH_MDS_SESSION_OPENING) { | |
2234 | /* the session never opened, just close it | |
2235 | * out now */ | |
2236 | __wake_requests(mdsc, &s->s_waiting); | |
42ce56e5 | 2237 | unregister_session(mdsc, s); |
2f2dc053 SW |
2238 | } else { |
2239 | /* just close it */ | |
2240 | mutex_unlock(&mdsc->mutex); | |
2241 | mutex_lock(&s->s_mutex); | |
2242 | mutex_lock(&mdsc->mutex); | |
2243 | ceph_con_close(&s->s_con); | |
2244 | mutex_unlock(&s->s_mutex); | |
2245 | s->s_state = CEPH_MDS_SESSION_RESTARTING; | |
2246 | } | |
2247 | ||
2248 | /* kick any requests waiting on the recovering mds */ | |
2249 | kick_requests(mdsc, i, 1); | |
2250 | } else if (oldstate == newstate) { | |
2251 | continue; /* nothing new with this mds */ | |
2252 | } | |
2253 | ||
2254 | /* | |
2255 | * send reconnect? | |
2256 | */ | |
2257 | if (s->s_state == CEPH_MDS_SESSION_RESTARTING && | |
2258 | newstate >= CEPH_MDS_STATE_RECONNECT) | |
2259 | send_mds_reconnect(mdsc, i); | |
2260 | ||
2261 | /* | |
2262 | * kick requests on any mds that has gone active. | |
2263 | * | |
2264 | * kick requests on cur or forwarder: we may have sent | |
2265 | * the request to mds1, mds1 told us it forwarded it | |
2266 | * to mds2, but then we learn mds1 failed and can't be | |
2267 | * sure it successfully forwarded our request before | |
2268 | * it died. | |
2269 | */ | |
2270 | if (oldstate < CEPH_MDS_STATE_ACTIVE && | |
2271 | newstate >= CEPH_MDS_STATE_ACTIVE) { | |
fef320ff | 2272 | pr_info("mds%d reconnect completed\n", s->s_mds); |
2f2dc053 SW |
2273 | kick_requests(mdsc, i, 1); |
2274 | ceph_kick_flushing_caps(mdsc, s); | |
0dc2570f | 2275 | wake_up_session_caps(s, 1); |
2f2dc053 SW |
2276 | } |
2277 | } | |
2278 | } | |
2279 | ||
2280 | ||
2281 | ||
2282 | /* | |
2283 | * leases | |
2284 | */ | |
2285 | ||
2286 | /* | |
2287 | * caller must hold session s_mutex, dentry->d_lock | |
2288 | */ | |
2289 | void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry) | |
2290 | { | |
2291 | struct ceph_dentry_info *di = ceph_dentry(dentry); | |
2292 | ||
2293 | ceph_put_mds_session(di->lease_session); | |
2294 | di->lease_session = NULL; | |
2295 | } | |
2296 | ||
2297 | static void handle_lease(struct ceph_mds_client *mdsc, struct ceph_msg *msg) | |
2298 | { | |
2299 | struct super_block *sb = mdsc->client->sb; | |
2300 | struct inode *inode; | |
2301 | struct ceph_mds_session *session; | |
2302 | struct ceph_inode_info *ci; | |
2303 | struct dentry *parent, *dentry; | |
2304 | struct ceph_dentry_info *di; | |
2305 | int mds; | |
2306 | struct ceph_mds_lease *h = msg->front.iov_base; | |
2307 | struct ceph_vino vino; | |
2308 | int mask; | |
2309 | struct qstr dname; | |
2310 | int release = 0; | |
2311 | ||
2312 | if (msg->hdr.src.name.type != CEPH_ENTITY_TYPE_MDS) | |
2313 | return; | |
2314 | mds = le64_to_cpu(msg->hdr.src.name.num); | |
2315 | dout("handle_lease from mds%d\n", mds); | |
2316 | ||
2317 | /* decode */ | |
2318 | if (msg->front.iov_len < sizeof(*h) + sizeof(u32)) | |
2319 | goto bad; | |
2320 | vino.ino = le64_to_cpu(h->ino); | |
2321 | vino.snap = CEPH_NOSNAP; | |
2322 | mask = le16_to_cpu(h->mask); | |
2323 | dname.name = (void *)h + sizeof(*h) + sizeof(u32); | |
2324 | dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32); | |
2325 | if (dname.len != get_unaligned_le32(h+1)) | |
2326 | goto bad; | |
2327 | ||
2328 | /* find session */ | |
2329 | mutex_lock(&mdsc->mutex); | |
2330 | session = __ceph_lookup_mds_session(mdsc, mds); | |
2331 | mutex_unlock(&mdsc->mutex); | |
2332 | if (!session) { | |
2333 | pr_err("handle_lease got lease but no session mds%d\n", mds); | |
2334 | return; | |
2335 | } | |
2336 | ||
2337 | mutex_lock(&session->s_mutex); | |
2338 | session->s_seq++; | |
2339 | ||
2340 | /* lookup inode */ | |
2341 | inode = ceph_find_inode(sb, vino); | |
2342 | dout("handle_lease '%s', mask %d, ino %llx %p\n", | |
2343 | ceph_lease_op_name(h->action), mask, vino.ino, inode); | |
2344 | if (inode == NULL) { | |
2345 | dout("handle_lease no inode %llx\n", vino.ino); | |
2346 | goto release; | |
2347 | } | |
2348 | ci = ceph_inode(inode); | |
2349 | ||
2350 | /* dentry */ | |
2351 | parent = d_find_alias(inode); | |
2352 | if (!parent) { | |
2353 | dout("no parent dentry on inode %p\n", inode); | |
2354 | WARN_ON(1); | |
2355 | goto release; /* hrm... */ | |
2356 | } | |
2357 | dname.hash = full_name_hash(dname.name, dname.len); | |
2358 | dentry = d_lookup(parent, &dname); | |
2359 | dput(parent); | |
2360 | if (!dentry) | |
2361 | goto release; | |
2362 | ||
2363 | spin_lock(&dentry->d_lock); | |
2364 | di = ceph_dentry(dentry); | |
2365 | switch (h->action) { | |
2366 | case CEPH_MDS_LEASE_REVOKE: | |
2367 | if (di && di->lease_session == session) { | |
2368 | h->seq = cpu_to_le32(di->lease_seq); | |
2369 | __ceph_mdsc_drop_dentry_lease(dentry); | |
2370 | } | |
2371 | release = 1; | |
2372 | break; | |
2373 | ||
2374 | case CEPH_MDS_LEASE_RENEW: | |
2375 | if (di && di->lease_session == session && | |
2376 | di->lease_gen == session->s_cap_gen && | |
2377 | di->lease_renew_from && | |
2378 | di->lease_renew_after == 0) { | |
2379 | unsigned long duration = | |
2380 | le32_to_cpu(h->duration_ms) * HZ / 1000; | |
2381 | ||
2382 | di->lease_seq = le32_to_cpu(h->seq); | |
2383 | dentry->d_time = di->lease_renew_from + duration; | |
2384 | di->lease_renew_after = di->lease_renew_from + | |
2385 | (duration >> 1); | |
2386 | di->lease_renew_from = 0; | |
2387 | } | |
2388 | break; | |
2389 | } | |
2390 | spin_unlock(&dentry->d_lock); | |
2391 | dput(dentry); | |
2392 | ||
2393 | if (!release) | |
2394 | goto out; | |
2395 | ||
2396 | release: | |
2397 | /* let's just reuse the same message */ | |
2398 | h->action = CEPH_MDS_LEASE_REVOKE_ACK; | |
2399 | ceph_msg_get(msg); | |
2400 | ceph_con_send(&session->s_con, msg); | |
2401 | ||
2402 | out: | |
2403 | iput(inode); | |
2404 | mutex_unlock(&session->s_mutex); | |
2405 | ceph_put_mds_session(session); | |
2406 | return; | |
2407 | ||
2408 | bad: | |
2409 | pr_err("corrupt lease message\n"); | |
9ec7cab1 | 2410 | ceph_msg_dump(msg); |
2f2dc053 SW |
2411 | } |
2412 | ||
2413 | void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session, | |
2414 | struct inode *inode, | |
2415 | struct dentry *dentry, char action, | |
2416 | u32 seq) | |
2417 | { | |
2418 | struct ceph_msg *msg; | |
2419 | struct ceph_mds_lease *lease; | |
2420 | int len = sizeof(*lease) + sizeof(u32); | |
2421 | int dnamelen = 0; | |
2422 | ||
2423 | dout("lease_send_msg inode %p dentry %p %s to mds%d\n", | |
2424 | inode, dentry, ceph_lease_op_name(action), session->s_mds); | |
2425 | dnamelen = dentry->d_name.len; | |
2426 | len += dnamelen; | |
2427 | ||
2428 | msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, 0, 0, NULL); | |
2429 | if (IS_ERR(msg)) | |
2430 | return; | |
2431 | lease = msg->front.iov_base; | |
2432 | lease->action = action; | |
2433 | lease->mask = cpu_to_le16(CEPH_LOCK_DN); | |
2434 | lease->ino = cpu_to_le64(ceph_vino(inode).ino); | |
2435 | lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap); | |
2436 | lease->seq = cpu_to_le32(seq); | |
2437 | put_unaligned_le32(dnamelen, lease + 1); | |
2438 | memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen); | |
2439 | ||
2440 | /* | |
2441 | * if this is a preemptive lease RELEASE, no need to | |
2442 | * flush request stream, since the actual request will | |
2443 | * soon follow. | |
2444 | */ | |
2445 | msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE); | |
2446 | ||
2447 | ceph_con_send(&session->s_con, msg); | |
2448 | } | |
2449 | ||
2450 | /* | |
2451 | * Preemptively release a lease we expect to invalidate anyway. | |
2452 | * Pass @inode always, @dentry is optional. | |
2453 | */ | |
2454 | void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode, | |
2455 | struct dentry *dentry, int mask) | |
2456 | { | |
2457 | struct ceph_dentry_info *di; | |
2458 | struct ceph_mds_session *session; | |
2459 | u32 seq; | |
2460 | ||
2461 | BUG_ON(inode == NULL); | |
2462 | BUG_ON(dentry == NULL); | |
2463 | BUG_ON(mask != CEPH_LOCK_DN); | |
2464 | ||
2465 | /* is dentry lease valid? */ | |
2466 | spin_lock(&dentry->d_lock); | |
2467 | di = ceph_dentry(dentry); | |
2468 | if (!di || !di->lease_session || | |
2469 | di->lease_session->s_mds < 0 || | |
2470 | di->lease_gen != di->lease_session->s_cap_gen || | |
2471 | !time_before(jiffies, dentry->d_time)) { | |
2472 | dout("lease_release inode %p dentry %p -- " | |
2473 | "no lease on %d\n", | |
2474 | inode, dentry, mask); | |
2475 | spin_unlock(&dentry->d_lock); | |
2476 | return; | |
2477 | } | |
2478 | ||
2479 | /* we do have a lease on this dentry; note mds and seq */ | |
2480 | session = ceph_get_mds_session(di->lease_session); | |
2481 | seq = di->lease_seq; | |
2482 | __ceph_mdsc_drop_dentry_lease(dentry); | |
2483 | spin_unlock(&dentry->d_lock); | |
2484 | ||
2485 | dout("lease_release inode %p dentry %p mask %d to mds%d\n", | |
2486 | inode, dentry, mask, session->s_mds); | |
2487 | ceph_mdsc_lease_send_msg(session, inode, dentry, | |
2488 | CEPH_MDS_LEASE_RELEASE, seq); | |
2489 | ceph_put_mds_session(session); | |
2490 | } | |
2491 | ||
2492 | /* | |
2493 | * drop all leases (and dentry refs) in preparation for umount | |
2494 | */ | |
2495 | static void drop_leases(struct ceph_mds_client *mdsc) | |
2496 | { | |
2497 | int i; | |
2498 | ||
2499 | dout("drop_leases\n"); | |
2500 | mutex_lock(&mdsc->mutex); | |
2501 | for (i = 0; i < mdsc->max_sessions; i++) { | |
2502 | struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i); | |
2503 | if (!s) | |
2504 | continue; | |
2505 | mutex_unlock(&mdsc->mutex); | |
2506 | mutex_lock(&s->s_mutex); | |
2507 | mutex_unlock(&s->s_mutex); | |
2508 | ceph_put_mds_session(s); | |
2509 | mutex_lock(&mdsc->mutex); | |
2510 | } | |
2511 | mutex_unlock(&mdsc->mutex); | |
2512 | } | |
2513 | ||
2514 | ||
2515 | ||
2516 | /* | |
2517 | * delayed work -- periodically trim expired leases, renew caps with mds | |
2518 | */ | |
2519 | static void schedule_delayed(struct ceph_mds_client *mdsc) | |
2520 | { | |
2521 | int delay = 5; | |
2522 | unsigned hz = round_jiffies_relative(HZ * delay); | |
2523 | schedule_delayed_work(&mdsc->delayed_work, hz); | |
2524 | } | |
2525 | ||
2526 | static void delayed_work(struct work_struct *work) | |
2527 | { | |
2528 | int i; | |
2529 | struct ceph_mds_client *mdsc = | |
2530 | container_of(work, struct ceph_mds_client, delayed_work.work); | |
2531 | int renew_interval; | |
2532 | int renew_caps; | |
2533 | ||
2534 | dout("mdsc delayed_work\n"); | |
afcdaea3 | 2535 | ceph_check_delayed_caps(mdsc); |
2f2dc053 SW |
2536 | |
2537 | mutex_lock(&mdsc->mutex); | |
2538 | renew_interval = mdsc->mdsmap->m_session_timeout >> 2; | |
2539 | renew_caps = time_after_eq(jiffies, HZ*renew_interval + | |
2540 | mdsc->last_renew_caps); | |
2541 | if (renew_caps) | |
2542 | mdsc->last_renew_caps = jiffies; | |
2543 | ||
2544 | for (i = 0; i < mdsc->max_sessions; i++) { | |
2545 | struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i); | |
2546 | if (s == NULL) | |
2547 | continue; | |
2548 | if (s->s_state == CEPH_MDS_SESSION_CLOSING) { | |
2549 | dout("resending session close request for mds%d\n", | |
2550 | s->s_mds); | |
2551 | request_close_session(mdsc, s); | |
2552 | ceph_put_mds_session(s); | |
2553 | continue; | |
2554 | } | |
2555 | if (s->s_ttl && time_after(jiffies, s->s_ttl)) { | |
2556 | if (s->s_state == CEPH_MDS_SESSION_OPEN) { | |
2557 | s->s_state = CEPH_MDS_SESSION_HUNG; | |
2558 | pr_info("mds%d hung\n", s->s_mds); | |
2559 | } | |
2560 | } | |
2561 | if (s->s_state < CEPH_MDS_SESSION_OPEN) { | |
2562 | /* this mds is failed or recovering, just wait */ | |
2563 | ceph_put_mds_session(s); | |
2564 | continue; | |
2565 | } | |
2566 | mutex_unlock(&mdsc->mutex); | |
2567 | ||
2568 | mutex_lock(&s->s_mutex); | |
2569 | if (renew_caps) | |
2570 | send_renew_caps(mdsc, s); | |
2571 | else | |
2572 | ceph_con_keepalive(&s->s_con); | |
2573 | add_cap_releases(mdsc, s, -1); | |
2574 | send_cap_releases(mdsc, s); | |
2575 | mutex_unlock(&s->s_mutex); | |
2576 | ceph_put_mds_session(s); | |
2577 | ||
2578 | mutex_lock(&mdsc->mutex); | |
2579 | } | |
2580 | mutex_unlock(&mdsc->mutex); | |
2581 | ||
2582 | schedule_delayed(mdsc); | |
2583 | } | |
2584 | ||
2585 | ||
5f44f142 | 2586 | int ceph_mdsc_init(struct ceph_mds_client *mdsc, struct ceph_client *client) |
2f2dc053 SW |
2587 | { |
2588 | mdsc->client = client; | |
2589 | mutex_init(&mdsc->mutex); | |
2590 | mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS); | |
2591 | init_completion(&mdsc->safe_umount_waiters); | |
2592 | init_completion(&mdsc->session_close_waiters); | |
2593 | INIT_LIST_HEAD(&mdsc->waiting_for_map); | |
2594 | mdsc->sessions = NULL; | |
2595 | mdsc->max_sessions = 0; | |
2596 | mdsc->stopping = 0; | |
2597 | init_rwsem(&mdsc->snap_rwsem); | |
2598 | INIT_RADIX_TREE(&mdsc->snap_realms, GFP_NOFS); | |
2599 | INIT_LIST_HEAD(&mdsc->snap_empty); | |
2600 | spin_lock_init(&mdsc->snap_empty_lock); | |
2601 | mdsc->last_tid = 0; | |
2602 | INIT_RADIX_TREE(&mdsc->request_tree, GFP_NOFS); | |
2603 | INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work); | |
2604 | mdsc->last_renew_caps = jiffies; | |
2605 | INIT_LIST_HEAD(&mdsc->cap_delay_list); | |
2606 | spin_lock_init(&mdsc->cap_delay_lock); | |
2607 | INIT_LIST_HEAD(&mdsc->snap_flush_list); | |
2608 | spin_lock_init(&mdsc->snap_flush_lock); | |
2609 | mdsc->cap_flush_seq = 0; | |
2610 | INIT_LIST_HEAD(&mdsc->cap_dirty); | |
2611 | mdsc->num_cap_flushing = 0; | |
2612 | spin_lock_init(&mdsc->cap_dirty_lock); | |
2613 | init_waitqueue_head(&mdsc->cap_flushing_wq); | |
2614 | spin_lock_init(&mdsc->dentry_lru_lock); | |
2615 | INIT_LIST_HEAD(&mdsc->dentry_lru); | |
5f44f142 | 2616 | return 0; |
2f2dc053 SW |
2617 | } |
2618 | ||
2619 | /* | |
2620 | * Wait for safe replies on open mds requests. If we time out, drop | |
2621 | * all requests from the tree to avoid dangling dentry refs. | |
2622 | */ | |
2623 | static void wait_requests(struct ceph_mds_client *mdsc) | |
2624 | { | |
2625 | struct ceph_mds_request *req; | |
2626 | struct ceph_client *client = mdsc->client; | |
2627 | ||
2628 | mutex_lock(&mdsc->mutex); | |
2629 | if (__get_oldest_tid(mdsc)) { | |
2630 | mutex_unlock(&mdsc->mutex); | |
2631 | dout("wait_requests waiting for requests\n"); | |
2632 | wait_for_completion_timeout(&mdsc->safe_umount_waiters, | |
6b805185 | 2633 | client->mount_args->mount_timeout * HZ); |
2f2dc053 SW |
2634 | mutex_lock(&mdsc->mutex); |
2635 | ||
2636 | /* tear down remaining requests */ | |
2637 | while (radix_tree_gang_lookup(&mdsc->request_tree, | |
2638 | (void **)&req, 0, 1)) { | |
2639 | dout("wait_requests timed out on tid %llu\n", | |
2640 | req->r_tid); | |
2641 | radix_tree_delete(&mdsc->request_tree, req->r_tid); | |
2642 | ceph_mdsc_put_request(req); | |
2643 | } | |
2644 | } | |
2645 | mutex_unlock(&mdsc->mutex); | |
2646 | dout("wait_requests done\n"); | |
2647 | } | |
2648 | ||
2649 | /* | |
2650 | * called before mount is ro, and before dentries are torn down. | |
2651 | * (hmm, does this still race with new lookups?) | |
2652 | */ | |
2653 | void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc) | |
2654 | { | |
2655 | dout("pre_umount\n"); | |
2656 | mdsc->stopping = 1; | |
2657 | ||
2658 | drop_leases(mdsc); | |
afcdaea3 | 2659 | ceph_flush_dirty_caps(mdsc); |
2f2dc053 SW |
2660 | wait_requests(mdsc); |
2661 | } | |
2662 | ||
2663 | /* | |
2664 | * wait for all write mds requests to flush. | |
2665 | */ | |
2666 | static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid) | |
2667 | { | |
2668 | struct ceph_mds_request *req; | |
2669 | u64 next_tid = 0; | |
2670 | int got; | |
2671 | ||
2672 | mutex_lock(&mdsc->mutex); | |
2673 | dout("wait_unsafe_requests want %lld\n", want_tid); | |
2674 | while (1) { | |
2675 | got = radix_tree_gang_lookup(&mdsc->request_tree, (void **)&req, | |
2676 | next_tid, 1); | |
2677 | if (!got) | |
2678 | break; | |
2679 | if (req->r_tid > want_tid) | |
2680 | break; | |
2681 | ||
2682 | next_tid = req->r_tid + 1; | |
2683 | if ((req->r_op & CEPH_MDS_OP_WRITE) == 0) | |
2684 | continue; /* not a write op */ | |
2685 | ||
2686 | ceph_mdsc_get_request(req); | |
2687 | mutex_unlock(&mdsc->mutex); | |
2688 | dout("wait_unsafe_requests wait on %llu (want %llu)\n", | |
2689 | req->r_tid, want_tid); | |
2690 | wait_for_completion(&req->r_safe_completion); | |
2691 | mutex_lock(&mdsc->mutex); | |
2692 | ceph_mdsc_put_request(req); | |
2693 | } | |
2694 | mutex_unlock(&mdsc->mutex); | |
2695 | dout("wait_unsafe_requests done\n"); | |
2696 | } | |
2697 | ||
2698 | void ceph_mdsc_sync(struct ceph_mds_client *mdsc) | |
2699 | { | |
2700 | u64 want_tid, want_flush; | |
2701 | ||
2702 | dout("sync\n"); | |
2703 | mutex_lock(&mdsc->mutex); | |
2704 | want_tid = mdsc->last_tid; | |
2705 | want_flush = mdsc->cap_flush_seq; | |
2706 | mutex_unlock(&mdsc->mutex); | |
2707 | dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush); | |
2708 | ||
afcdaea3 | 2709 | ceph_flush_dirty_caps(mdsc); |
2f2dc053 SW |
2710 | |
2711 | wait_unsafe_requests(mdsc, want_tid); | |
2712 | wait_event(mdsc->cap_flushing_wq, check_cap_flush(mdsc, want_flush)); | |
2713 | } | |
2714 | ||
2715 | ||
2716 | /* | |
2717 | * called after sb is ro. | |
2718 | */ | |
2719 | void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc) | |
2720 | { | |
2721 | struct ceph_mds_session *session; | |
2722 | int i; | |
2723 | int n; | |
2724 | struct ceph_client *client = mdsc->client; | |
6b805185 | 2725 | unsigned long started, timeout = client->mount_args->mount_timeout * HZ; |
2f2dc053 SW |
2726 | |
2727 | dout("close_sessions\n"); | |
2728 | ||
2729 | mutex_lock(&mdsc->mutex); | |
2730 | ||
2731 | /* close sessions */ | |
2732 | started = jiffies; | |
2733 | while (time_before(jiffies, started + timeout)) { | |
2734 | dout("closing sessions\n"); | |
2735 | n = 0; | |
2736 | for (i = 0; i < mdsc->max_sessions; i++) { | |
2737 | session = __ceph_lookup_mds_session(mdsc, i); | |
2738 | if (!session) | |
2739 | continue; | |
2740 | mutex_unlock(&mdsc->mutex); | |
2741 | mutex_lock(&session->s_mutex); | |
2742 | __close_session(mdsc, session); | |
2743 | mutex_unlock(&session->s_mutex); | |
2744 | ceph_put_mds_session(session); | |
2745 | mutex_lock(&mdsc->mutex); | |
2746 | n++; | |
2747 | } | |
2748 | if (n == 0) | |
2749 | break; | |
2750 | ||
2751 | if (client->mount_state == CEPH_MOUNT_SHUTDOWN) | |
2752 | break; | |
2753 | ||
2754 | dout("waiting for sessions to close\n"); | |
2755 | mutex_unlock(&mdsc->mutex); | |
2756 | wait_for_completion_timeout(&mdsc->session_close_waiters, | |
2757 | timeout); | |
2758 | mutex_lock(&mdsc->mutex); | |
2759 | } | |
2760 | ||
2761 | /* tear down remaining sessions */ | |
2762 | for (i = 0; i < mdsc->max_sessions; i++) { | |
2763 | if (mdsc->sessions[i]) { | |
2764 | session = get_session(mdsc->sessions[i]); | |
42ce56e5 | 2765 | unregister_session(mdsc, session); |
2f2dc053 SW |
2766 | mutex_unlock(&mdsc->mutex); |
2767 | mutex_lock(&session->s_mutex); | |
2768 | remove_session_caps(session); | |
2769 | mutex_unlock(&session->s_mutex); | |
2770 | ceph_put_mds_session(session); | |
2771 | mutex_lock(&mdsc->mutex); | |
2772 | } | |
2773 | } | |
2774 | ||
2775 | WARN_ON(!list_empty(&mdsc->cap_delay_list)); | |
2776 | ||
2777 | mutex_unlock(&mdsc->mutex); | |
2778 | ||
2779 | ceph_cleanup_empty_realms(mdsc); | |
2780 | ||
2781 | cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */ | |
2782 | ||
2783 | dout("stopped\n"); | |
2784 | } | |
2785 | ||
2786 | void ceph_mdsc_stop(struct ceph_mds_client *mdsc) | |
2787 | { | |
2788 | dout("stop\n"); | |
2789 | cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */ | |
2790 | if (mdsc->mdsmap) | |
2791 | ceph_mdsmap_destroy(mdsc->mdsmap); | |
2792 | kfree(mdsc->sessions); | |
2793 | } | |
2794 | ||
2795 | ||
2796 | /* | |
2797 | * handle mds map update. | |
2798 | */ | |
2799 | void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg) | |
2800 | { | |
2801 | u32 epoch; | |
2802 | u32 maplen; | |
2803 | void *p = msg->front.iov_base; | |
2804 | void *end = p + msg->front.iov_len; | |
2805 | struct ceph_mdsmap *newmap, *oldmap; | |
2806 | struct ceph_fsid fsid; | |
2807 | int err = -EINVAL; | |
2808 | ||
2809 | ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad); | |
2810 | ceph_decode_copy(&p, &fsid, sizeof(fsid)); | |
0743304d SW |
2811 | if (ceph_check_fsid(mdsc->client, &fsid) < 0) |
2812 | return; | |
c89136ea SW |
2813 | epoch = ceph_decode_32(&p); |
2814 | maplen = ceph_decode_32(&p); | |
2f2dc053 SW |
2815 | dout("handle_map epoch %u len %d\n", epoch, (int)maplen); |
2816 | ||
2817 | /* do we need it? */ | |
2818 | ceph_monc_got_mdsmap(&mdsc->client->monc, epoch); | |
2819 | mutex_lock(&mdsc->mutex); | |
2820 | if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) { | |
2821 | dout("handle_map epoch %u <= our %u\n", | |
2822 | epoch, mdsc->mdsmap->m_epoch); | |
2823 | mutex_unlock(&mdsc->mutex); | |
2824 | return; | |
2825 | } | |
2826 | ||
2827 | newmap = ceph_mdsmap_decode(&p, end); | |
2828 | if (IS_ERR(newmap)) { | |
2829 | err = PTR_ERR(newmap); | |
2830 | goto bad_unlock; | |
2831 | } | |
2832 | ||
2833 | /* swap into place */ | |
2834 | if (mdsc->mdsmap) { | |
2835 | oldmap = mdsc->mdsmap; | |
2836 | mdsc->mdsmap = newmap; | |
2837 | check_new_map(mdsc, newmap, oldmap); | |
2838 | ceph_mdsmap_destroy(oldmap); | |
2839 | } else { | |
2840 | mdsc->mdsmap = newmap; /* first mds map */ | |
2841 | } | |
2842 | mdsc->client->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size; | |
2843 | ||
2844 | __wake_requests(mdsc, &mdsc->waiting_for_map); | |
2845 | ||
2846 | mutex_unlock(&mdsc->mutex); | |
2847 | schedule_delayed(mdsc); | |
2848 | return; | |
2849 | ||
2850 | bad_unlock: | |
2851 | mutex_unlock(&mdsc->mutex); | |
2852 | bad: | |
2853 | pr_err("error decoding mdsmap %d\n", err); | |
2854 | return; | |
2855 | } | |
2856 | ||
2857 | static struct ceph_connection *con_get(struct ceph_connection *con) | |
2858 | { | |
2859 | struct ceph_mds_session *s = con->private; | |
2860 | ||
2861 | if (get_session(s)) { | |
2862 | dout("mdsc con_get %p %d -> %d\n", s, | |
2863 | atomic_read(&s->s_ref) - 1, atomic_read(&s->s_ref)); | |
2864 | return con; | |
2865 | } | |
2866 | dout("mdsc con_get %p FAIL\n", s); | |
2867 | return NULL; | |
2868 | } | |
2869 | ||
2870 | static void con_put(struct ceph_connection *con) | |
2871 | { | |
2872 | struct ceph_mds_session *s = con->private; | |
2873 | ||
2874 | dout("mdsc con_put %p %d -> %d\n", s, atomic_read(&s->s_ref), | |
2875 | atomic_read(&s->s_ref) - 1); | |
2876 | ceph_put_mds_session(s); | |
2877 | } | |
2878 | ||
2879 | /* | |
2880 | * if the client is unresponsive for long enough, the mds will kill | |
2881 | * the session entirely. | |
2882 | */ | |
2883 | static void peer_reset(struct ceph_connection *con) | |
2884 | { | |
2885 | struct ceph_mds_session *s = con->private; | |
2886 | ||
2887 | pr_err("mds%d gave us the boot. IMPLEMENT RECONNECT.\n", | |
2888 | s->s_mds); | |
2889 | } | |
2890 | ||
2891 | static void dispatch(struct ceph_connection *con, struct ceph_msg *msg) | |
2892 | { | |
2893 | struct ceph_mds_session *s = con->private; | |
2894 | struct ceph_mds_client *mdsc = s->s_mdsc; | |
2895 | int type = le16_to_cpu(msg->hdr.type); | |
2896 | ||
2897 | switch (type) { | |
2898 | case CEPH_MSG_MDS_MAP: | |
2899 | ceph_mdsc_handle_map(mdsc, msg); | |
2900 | break; | |
2901 | case CEPH_MSG_CLIENT_SESSION: | |
2902 | handle_session(s, msg); | |
2903 | break; | |
2904 | case CEPH_MSG_CLIENT_REPLY: | |
2905 | handle_reply(s, msg); | |
2906 | break; | |
2907 | case CEPH_MSG_CLIENT_REQUEST_FORWARD: | |
2908 | handle_forward(mdsc, msg); | |
2909 | break; | |
2910 | case CEPH_MSG_CLIENT_CAPS: | |
2911 | ceph_handle_caps(s, msg); | |
2912 | break; | |
2913 | case CEPH_MSG_CLIENT_SNAP: | |
2914 | ceph_handle_snap(mdsc, msg); | |
2915 | break; | |
2916 | case CEPH_MSG_CLIENT_LEASE: | |
2917 | handle_lease(mdsc, msg); | |
2918 | break; | |
2919 | ||
2920 | default: | |
2921 | pr_err("received unknown message type %d %s\n", type, | |
2922 | ceph_msg_type_name(type)); | |
2923 | } | |
2924 | ceph_msg_put(msg); | |
2925 | } | |
2926 | ||
4e7a5dcd SW |
2927 | /* |
2928 | * authentication | |
2929 | */ | |
2930 | static int get_authorizer(struct ceph_connection *con, | |
2931 | void **buf, int *len, int *proto, | |
2932 | void **reply_buf, int *reply_len, int force_new) | |
2933 | { | |
2934 | struct ceph_mds_session *s = con->private; | |
2935 | struct ceph_mds_client *mdsc = s->s_mdsc; | |
2936 | struct ceph_auth_client *ac = mdsc->client->monc.auth; | |
2937 | int ret = 0; | |
2938 | ||
2939 | if (force_new && s->s_authorizer) { | |
2940 | ac->ops->destroy_authorizer(ac, s->s_authorizer); | |
2941 | s->s_authorizer = NULL; | |
2942 | } | |
2943 | if (s->s_authorizer == NULL) { | |
2944 | if (ac->ops->create_authorizer) { | |
2945 | ret = ac->ops->create_authorizer( | |
2946 | ac, CEPH_ENTITY_TYPE_MDS, | |
2947 | &s->s_authorizer, | |
2948 | &s->s_authorizer_buf, | |
2949 | &s->s_authorizer_buf_len, | |
2950 | &s->s_authorizer_reply_buf, | |
2951 | &s->s_authorizer_reply_buf_len); | |
2952 | if (ret) | |
2953 | return ret; | |
2954 | } | |
2955 | } | |
2956 | ||
2957 | *proto = ac->protocol; | |
2958 | *buf = s->s_authorizer_buf; | |
2959 | *len = s->s_authorizer_buf_len; | |
2960 | *reply_buf = s->s_authorizer_reply_buf; | |
2961 | *reply_len = s->s_authorizer_reply_buf_len; | |
2962 | return 0; | |
2963 | } | |
2964 | ||
2965 | ||
2966 | static int verify_authorizer_reply(struct ceph_connection *con, int len) | |
2967 | { | |
2968 | struct ceph_mds_session *s = con->private; | |
2969 | struct ceph_mds_client *mdsc = s->s_mdsc; | |
2970 | struct ceph_auth_client *ac = mdsc->client->monc.auth; | |
2971 | ||
2972 | return ac->ops->verify_authorizer_reply(ac, s->s_authorizer, len); | |
2973 | } | |
2974 | ||
2f2dc053 SW |
2975 | const static struct ceph_connection_operations mds_con_ops = { |
2976 | .get = con_get, | |
2977 | .put = con_put, | |
2978 | .dispatch = dispatch, | |
4e7a5dcd SW |
2979 | .get_authorizer = get_authorizer, |
2980 | .verify_authorizer_reply = verify_authorizer_reply, | |
2f2dc053 SW |
2981 | .peer_reset = peer_reset, |
2982 | .alloc_msg = ceph_alloc_msg, | |
2983 | .alloc_middle = ceph_alloc_middle, | |
2984 | }; | |
2985 | ||
2986 | ||
2987 | ||
2988 | ||
2989 | /* eof */ |