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