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