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