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