1 #include <linux/ceph/ceph_debug.h>
2 
3 #include <linux/module.h>
4 #include <linux/err.h>
5 #include <linux/highmem.h>
6 #include <linux/mm.h>
7 #include <linux/pagemap.h>
8 #include <linux/slab.h>
9 #include <linux/uaccess.h>
10 #ifdef CONFIG_BLOCK
11 #include <linux/bio.h>
12 #endif
13 
14 #include <linux/ceph/libceph.h>
15 #include <linux/ceph/osd_client.h>
16 #include <linux/ceph/messenger.h>
17 #include <linux/ceph/decode.h>
18 #include <linux/ceph/auth.h>
19 #include <linux/ceph/pagelist.h>
20 
21 #define OSD_OP_FRONT_LEN	4096
22 #define OSD_OPREPLY_FRONT_LEN	512
23 
24 static const struct ceph_connection_operations osd_con_ops;
25 
26 static void send_queued(struct ceph_osd_client *osdc);
27 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
28 static void __register_request(struct ceph_osd_client *osdc,
29 			       struct ceph_osd_request *req);
30 static void __unregister_linger_request(struct ceph_osd_client *osdc,
31 					struct ceph_osd_request *req);
32 static void __send_request(struct ceph_osd_client *osdc,
33 			   struct ceph_osd_request *req);
34 
op_needs_trail(int op)35 static int op_needs_trail(int op)
36 {
37 	switch (op) {
38 	case CEPH_OSD_OP_GETXATTR:
39 	case CEPH_OSD_OP_SETXATTR:
40 	case CEPH_OSD_OP_CMPXATTR:
41 	case CEPH_OSD_OP_CALL:
42 	case CEPH_OSD_OP_NOTIFY:
43 		return 1;
44 	default:
45 		return 0;
46 	}
47 }
48 
op_has_extent(int op)49 static int op_has_extent(int op)
50 {
51 	return (op == CEPH_OSD_OP_READ ||
52 		op == CEPH_OSD_OP_WRITE);
53 }
54 
ceph_calc_raw_layout(struct ceph_osd_client * osdc,struct ceph_file_layout * layout,u64 snapid,u64 off,u64 * plen,u64 * bno,struct ceph_osd_request * req,struct ceph_osd_req_op * op)55 int ceph_calc_raw_layout(struct ceph_osd_client *osdc,
56 			struct ceph_file_layout *layout,
57 			u64 snapid,
58 			u64 off, u64 *plen, u64 *bno,
59 			struct ceph_osd_request *req,
60 			struct ceph_osd_req_op *op)
61 {
62 	struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
63 	u64 orig_len = *plen;
64 	u64 objoff, objlen;    /* extent in object */
65 	int r;
66 
67 	reqhead->snapid = cpu_to_le64(snapid);
68 
69 	/* object extent? */
70 	r = ceph_calc_file_object_mapping(layout, off, plen, bno,
71 					  &objoff, &objlen);
72 	if (r < 0)
73 		return r;
74 	if (*plen < orig_len)
75 		dout(" skipping last %llu, final file extent %llu~%llu\n",
76 		     orig_len - *plen, off, *plen);
77 
78 	if (op_has_extent(op->op)) {
79 		op->extent.offset = objoff;
80 		op->extent.length = objlen;
81 	}
82 	req->r_num_pages = calc_pages_for(off, *plen);
83 	req->r_page_alignment = off & ~PAGE_MASK;
84 	if (op->op == CEPH_OSD_OP_WRITE)
85 		op->payload_len = *plen;
86 
87 	dout("calc_layout bno=%llx %llu~%llu (%d pages)\n",
88 	     *bno, objoff, objlen, req->r_num_pages);
89 	return 0;
90 }
91 EXPORT_SYMBOL(ceph_calc_raw_layout);
92 
93 /*
94  * Implement client access to distributed object storage cluster.
95  *
96  * All data objects are stored within a cluster/cloud of OSDs, or
97  * "object storage devices."  (Note that Ceph OSDs have _nothing_ to
98  * do with the T10 OSD extensions to SCSI.)  Ceph OSDs are simply
99  * remote daemons serving up and coordinating consistent and safe
100  * access to storage.
101  *
102  * Cluster membership and the mapping of data objects onto storage devices
103  * are described by the osd map.
104  *
105  * We keep track of pending OSD requests (read, write), resubmit
106  * requests to different OSDs when the cluster topology/data layout
107  * change, or retry the affected requests when the communications
108  * channel with an OSD is reset.
109  */
110 
111 /*
112  * calculate the mapping of a file extent onto an object, and fill out the
113  * request accordingly.  shorten extent as necessary if it crosses an
114  * object boundary.
115  *
116  * fill osd op in request message.
117  */
calc_layout(struct ceph_osd_client * osdc,struct ceph_vino vino,struct ceph_file_layout * layout,u64 off,u64 * plen,struct ceph_osd_request * req,struct ceph_osd_req_op * op)118 static int calc_layout(struct ceph_osd_client *osdc,
119 		       struct ceph_vino vino,
120 		       struct ceph_file_layout *layout,
121 		       u64 off, u64 *plen,
122 		       struct ceph_osd_request *req,
123 		       struct ceph_osd_req_op *op)
124 {
125 	u64 bno;
126 	int r;
127 
128 	r = ceph_calc_raw_layout(osdc, layout, vino.snap, off,
129 				 plen, &bno, req, op);
130 	if (r < 0)
131 		return r;
132 
133 	snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx", vino.ino, bno);
134 	req->r_oid_len = strlen(req->r_oid);
135 
136 	return r;
137 }
138 
139 /*
140  * requests
141  */
ceph_osdc_release_request(struct kref * kref)142 void ceph_osdc_release_request(struct kref *kref)
143 {
144 	struct ceph_osd_request *req = container_of(kref,
145 						    struct ceph_osd_request,
146 						    r_kref);
147 
148 	if (req->r_request)
149 		ceph_msg_put(req->r_request);
150 	if (req->r_con_filling_msg) {
151 		dout("%s revoking pages %p from con %p\n", __func__,
152 		     req->r_pages, req->r_con_filling_msg);
153 		ceph_msg_revoke_incoming(req->r_reply);
154 		req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
155 	}
156 	if (req->r_reply)
157 		ceph_msg_put(req->r_reply);
158 	if (req->r_own_pages)
159 		ceph_release_page_vector(req->r_pages,
160 					 req->r_num_pages);
161 #ifdef CONFIG_BLOCK
162 	if (req->r_bio)
163 		bio_put(req->r_bio);
164 #endif
165 	ceph_put_snap_context(req->r_snapc);
166 	if (req->r_trail) {
167 		ceph_pagelist_release(req->r_trail);
168 		kfree(req->r_trail);
169 	}
170 	if (req->r_mempool)
171 		mempool_free(req, req->r_osdc->req_mempool);
172 	else
173 		kfree(req);
174 }
175 EXPORT_SYMBOL(ceph_osdc_release_request);
176 
get_num_ops(struct ceph_osd_req_op * ops,int * needs_trail)177 static int get_num_ops(struct ceph_osd_req_op *ops, int *needs_trail)
178 {
179 	int i = 0;
180 
181 	if (needs_trail)
182 		*needs_trail = 0;
183 	while (ops[i].op) {
184 		if (needs_trail && op_needs_trail(ops[i].op))
185 			*needs_trail = 1;
186 		i++;
187 	}
188 
189 	return i;
190 }
191 
ceph_osdc_alloc_request(struct ceph_osd_client * osdc,int flags,struct ceph_snap_context * snapc,struct ceph_osd_req_op * ops,bool use_mempool,gfp_t gfp_flags,struct page ** pages,struct bio * bio)192 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
193 					       int flags,
194 					       struct ceph_snap_context *snapc,
195 					       struct ceph_osd_req_op *ops,
196 					       bool use_mempool,
197 					       gfp_t gfp_flags,
198 					       struct page **pages,
199 					       struct bio *bio)
200 {
201 	struct ceph_osd_request *req;
202 	struct ceph_msg *msg;
203 	int needs_trail;
204 	int num_op = get_num_ops(ops, &needs_trail);
205 	size_t msg_size = sizeof(struct ceph_osd_request_head);
206 
207 	msg_size += num_op*sizeof(struct ceph_osd_op);
208 
209 	if (use_mempool) {
210 		req = mempool_alloc(osdc->req_mempool, gfp_flags);
211 		memset(req, 0, sizeof(*req));
212 	} else {
213 		req = kzalloc(sizeof(*req), gfp_flags);
214 	}
215 	if (req == NULL)
216 		return NULL;
217 
218 	req->r_osdc = osdc;
219 	req->r_mempool = use_mempool;
220 
221 	kref_init(&req->r_kref);
222 	init_completion(&req->r_completion);
223 	init_completion(&req->r_safe_completion);
224 	RB_CLEAR_NODE(&req->r_node);
225 	INIT_LIST_HEAD(&req->r_unsafe_item);
226 	INIT_LIST_HEAD(&req->r_linger_item);
227 	INIT_LIST_HEAD(&req->r_linger_osd);
228 	INIT_LIST_HEAD(&req->r_req_lru_item);
229 	req->r_flags = flags;
230 
231 	WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
232 
233 	/* create reply message */
234 	if (use_mempool)
235 		msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
236 	else
237 		msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
238 				   OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
239 	if (!msg) {
240 		ceph_osdc_put_request(req);
241 		return NULL;
242 	}
243 	req->r_reply = msg;
244 
245 	/* allocate space for the trailing data */
246 	if (needs_trail) {
247 		req->r_trail = kmalloc(sizeof(struct ceph_pagelist), gfp_flags);
248 		if (!req->r_trail) {
249 			ceph_osdc_put_request(req);
250 			return NULL;
251 		}
252 		ceph_pagelist_init(req->r_trail);
253 	}
254 
255 	/* create request message; allow space for oid */
256 	msg_size += MAX_OBJ_NAME_SIZE;
257 	if (snapc)
258 		msg_size += sizeof(u64) * snapc->num_snaps;
259 	if (use_mempool)
260 		msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
261 	else
262 		msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
263 	if (!msg) {
264 		ceph_osdc_put_request(req);
265 		return NULL;
266 	}
267 
268 	memset(msg->front.iov_base, 0, msg->front.iov_len);
269 
270 	req->r_request = msg;
271 	req->r_pages = pages;
272 #ifdef CONFIG_BLOCK
273 	if (bio) {
274 		req->r_bio = bio;
275 		bio_get(req->r_bio);
276 	}
277 #endif
278 
279 	return req;
280 }
281 EXPORT_SYMBOL(ceph_osdc_alloc_request);
282 
osd_req_encode_op(struct ceph_osd_request * req,struct ceph_osd_op * dst,struct ceph_osd_req_op * src)283 static void osd_req_encode_op(struct ceph_osd_request *req,
284 			      struct ceph_osd_op *dst,
285 			      struct ceph_osd_req_op *src)
286 {
287 	dst->op = cpu_to_le16(src->op);
288 
289 	switch (src->op) {
290 	case CEPH_OSD_OP_READ:
291 	case CEPH_OSD_OP_WRITE:
292 		dst->extent.offset =
293 			cpu_to_le64(src->extent.offset);
294 		dst->extent.length =
295 			cpu_to_le64(src->extent.length);
296 		dst->extent.truncate_size =
297 			cpu_to_le64(src->extent.truncate_size);
298 		dst->extent.truncate_seq =
299 			cpu_to_le32(src->extent.truncate_seq);
300 		break;
301 
302 	case CEPH_OSD_OP_GETXATTR:
303 	case CEPH_OSD_OP_SETXATTR:
304 	case CEPH_OSD_OP_CMPXATTR:
305 		BUG_ON(!req->r_trail);
306 
307 		dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
308 		dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
309 		dst->xattr.cmp_op = src->xattr.cmp_op;
310 		dst->xattr.cmp_mode = src->xattr.cmp_mode;
311 		ceph_pagelist_append(req->r_trail, src->xattr.name,
312 				     src->xattr.name_len);
313 		ceph_pagelist_append(req->r_trail, src->xattr.val,
314 				     src->xattr.value_len);
315 		break;
316 	case CEPH_OSD_OP_CALL:
317 		BUG_ON(!req->r_trail);
318 
319 		dst->cls.class_len = src->cls.class_len;
320 		dst->cls.method_len = src->cls.method_len;
321 		dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
322 
323 		ceph_pagelist_append(req->r_trail, src->cls.class_name,
324 				     src->cls.class_len);
325 		ceph_pagelist_append(req->r_trail, src->cls.method_name,
326 				     src->cls.method_len);
327 		ceph_pagelist_append(req->r_trail, src->cls.indata,
328 				     src->cls.indata_len);
329 		break;
330 	case CEPH_OSD_OP_ROLLBACK:
331 		dst->snap.snapid = cpu_to_le64(src->snap.snapid);
332 		break;
333 	case CEPH_OSD_OP_STARTSYNC:
334 		break;
335 	case CEPH_OSD_OP_NOTIFY:
336 		{
337 			__le32 prot_ver = cpu_to_le32(src->watch.prot_ver);
338 			__le32 timeout = cpu_to_le32(src->watch.timeout);
339 
340 			BUG_ON(!req->r_trail);
341 
342 			ceph_pagelist_append(req->r_trail,
343 						&prot_ver, sizeof(prot_ver));
344 			ceph_pagelist_append(req->r_trail,
345 						&timeout, sizeof(timeout));
346 		}
347 	case CEPH_OSD_OP_NOTIFY_ACK:
348 	case CEPH_OSD_OP_WATCH:
349 		dst->watch.cookie = cpu_to_le64(src->watch.cookie);
350 		dst->watch.ver = cpu_to_le64(src->watch.ver);
351 		dst->watch.flag = src->watch.flag;
352 		break;
353 	default:
354 		pr_err("unrecognized osd opcode %d\n", dst->op);
355 		WARN_ON(1);
356 		break;
357 	}
358 	dst->payload_len = cpu_to_le32(src->payload_len);
359 }
360 
361 /*
362  * build new request AND message
363  *
364  */
ceph_osdc_build_request(struct ceph_osd_request * req,u64 off,u64 * plen,struct ceph_osd_req_op * src_ops,struct ceph_snap_context * snapc,struct timespec * mtime,const char * oid,int oid_len)365 void ceph_osdc_build_request(struct ceph_osd_request *req,
366 			     u64 off, u64 *plen,
367 			     struct ceph_osd_req_op *src_ops,
368 			     struct ceph_snap_context *snapc,
369 			     struct timespec *mtime,
370 			     const char *oid,
371 			     int oid_len)
372 {
373 	struct ceph_msg *msg = req->r_request;
374 	struct ceph_osd_request_head *head;
375 	struct ceph_osd_req_op *src_op;
376 	struct ceph_osd_op *op;
377 	void *p;
378 	int num_op = get_num_ops(src_ops, NULL);
379 	size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
380 	int flags = req->r_flags;
381 	u64 data_len = 0;
382 	int i;
383 
384 	head = msg->front.iov_base;
385 	op = (void *)(head + 1);
386 	p = (void *)(op + num_op);
387 
388 	req->r_snapc = ceph_get_snap_context(snapc);
389 
390 	head->client_inc = cpu_to_le32(1); /* always, for now. */
391 	head->flags = cpu_to_le32(flags);
392 	if (flags & CEPH_OSD_FLAG_WRITE)
393 		ceph_encode_timespec(&head->mtime, mtime);
394 	head->num_ops = cpu_to_le16(num_op);
395 
396 
397 	/* fill in oid */
398 	head->object_len = cpu_to_le32(oid_len);
399 	memcpy(p, oid, oid_len);
400 	p += oid_len;
401 
402 	src_op = src_ops;
403 	while (src_op->op) {
404 		osd_req_encode_op(req, op, src_op);
405 		src_op++;
406 		op++;
407 	}
408 
409 	if (req->r_trail)
410 		data_len += req->r_trail->length;
411 
412 	if (snapc) {
413 		head->snap_seq = cpu_to_le64(snapc->seq);
414 		head->num_snaps = cpu_to_le32(snapc->num_snaps);
415 		for (i = 0; i < snapc->num_snaps; i++) {
416 			put_unaligned_le64(snapc->snaps[i], p);
417 			p += sizeof(u64);
418 		}
419 	}
420 
421 	if (flags & CEPH_OSD_FLAG_WRITE) {
422 		req->r_request->hdr.data_off = cpu_to_le16(off);
423 		req->r_request->hdr.data_len = cpu_to_le32(*plen + data_len);
424 	} else if (data_len) {
425 		req->r_request->hdr.data_off = 0;
426 		req->r_request->hdr.data_len = cpu_to_le32(data_len);
427 	}
428 
429 	req->r_request->page_alignment = req->r_page_alignment;
430 
431 	BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
432 	msg_size = p - msg->front.iov_base;
433 	msg->front.iov_len = msg_size;
434 	msg->hdr.front_len = cpu_to_le32(msg_size);
435 	return;
436 }
437 EXPORT_SYMBOL(ceph_osdc_build_request);
438 
439 /*
440  * build new request AND message, calculate layout, and adjust file
441  * extent as needed.
442  *
443  * if the file was recently truncated, we include information about its
444  * old and new size so that the object can be updated appropriately.  (we
445  * avoid synchronously deleting truncated objects because it's slow.)
446  *
447  * if @do_sync, include a 'startsync' command so that the osd will flush
448  * data quickly.
449  */
ceph_osdc_new_request(struct ceph_osd_client * osdc,struct ceph_file_layout * layout,struct ceph_vino vino,u64 off,u64 * plen,int opcode,int flags,struct ceph_snap_context * snapc,int do_sync,u32 truncate_seq,u64 truncate_size,struct timespec * mtime,bool use_mempool,int num_reply,int page_align)450 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
451 					       struct ceph_file_layout *layout,
452 					       struct ceph_vino vino,
453 					       u64 off, u64 *plen,
454 					       int opcode, int flags,
455 					       struct ceph_snap_context *snapc,
456 					       int do_sync,
457 					       u32 truncate_seq,
458 					       u64 truncate_size,
459 					       struct timespec *mtime,
460 					       bool use_mempool, int num_reply,
461 					       int page_align)
462 {
463 	struct ceph_osd_req_op ops[3];
464 	struct ceph_osd_request *req;
465 	int r;
466 
467 	ops[0].op = opcode;
468 	ops[0].extent.truncate_seq = truncate_seq;
469 	ops[0].extent.truncate_size = truncate_size;
470 	ops[0].payload_len = 0;
471 
472 	if (do_sync) {
473 		ops[1].op = CEPH_OSD_OP_STARTSYNC;
474 		ops[1].payload_len = 0;
475 		ops[2].op = 0;
476 	} else
477 		ops[1].op = 0;
478 
479 	req = ceph_osdc_alloc_request(osdc, flags,
480 					 snapc, ops,
481 					 use_mempool,
482 					 GFP_NOFS, NULL, NULL);
483 	if (!req)
484 		return ERR_PTR(-ENOMEM);
485 
486 	/* calculate max write size */
487 	r = calc_layout(osdc, vino, layout, off, plen, req, ops);
488 	if (r < 0)
489 		return ERR_PTR(r);
490 	req->r_file_layout = *layout;  /* keep a copy */
491 
492 	/* in case it differs from natural (file) alignment that
493 	   calc_layout filled in for us */
494 	req->r_num_pages = calc_pages_for(page_align, *plen);
495 	req->r_page_alignment = page_align;
496 
497 	ceph_osdc_build_request(req, off, plen, ops,
498 				snapc,
499 				mtime,
500 				req->r_oid, req->r_oid_len);
501 
502 	return req;
503 }
504 EXPORT_SYMBOL(ceph_osdc_new_request);
505 
506 /*
507  * We keep osd requests in an rbtree, sorted by ->r_tid.
508  */
__insert_request(struct ceph_osd_client * osdc,struct ceph_osd_request * new)509 static void __insert_request(struct ceph_osd_client *osdc,
510 			     struct ceph_osd_request *new)
511 {
512 	struct rb_node **p = &osdc->requests.rb_node;
513 	struct rb_node *parent = NULL;
514 	struct ceph_osd_request *req = NULL;
515 
516 	while (*p) {
517 		parent = *p;
518 		req = rb_entry(parent, struct ceph_osd_request, r_node);
519 		if (new->r_tid < req->r_tid)
520 			p = &(*p)->rb_left;
521 		else if (new->r_tid > req->r_tid)
522 			p = &(*p)->rb_right;
523 		else
524 			BUG();
525 	}
526 
527 	rb_link_node(&new->r_node, parent, p);
528 	rb_insert_color(&new->r_node, &osdc->requests);
529 }
530 
__lookup_request(struct ceph_osd_client * osdc,u64 tid)531 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
532 						 u64 tid)
533 {
534 	struct ceph_osd_request *req;
535 	struct rb_node *n = osdc->requests.rb_node;
536 
537 	while (n) {
538 		req = rb_entry(n, struct ceph_osd_request, r_node);
539 		if (tid < req->r_tid)
540 			n = n->rb_left;
541 		else if (tid > req->r_tid)
542 			n = n->rb_right;
543 		else
544 			return req;
545 	}
546 	return NULL;
547 }
548 
549 static struct ceph_osd_request *
__lookup_request_ge(struct ceph_osd_client * osdc,u64 tid)550 __lookup_request_ge(struct ceph_osd_client *osdc,
551 		    u64 tid)
552 {
553 	struct ceph_osd_request *req;
554 	struct rb_node *n = osdc->requests.rb_node;
555 
556 	while (n) {
557 		req = rb_entry(n, struct ceph_osd_request, r_node);
558 		if (tid < req->r_tid) {
559 			if (!n->rb_left)
560 				return req;
561 			n = n->rb_left;
562 		} else if (tid > req->r_tid) {
563 			n = n->rb_right;
564 		} else {
565 			return req;
566 		}
567 	}
568 	return NULL;
569 }
570 
571 /*
572  * Resubmit requests pending on the given osd.
573  */
__kick_osd_requests(struct ceph_osd_client * osdc,struct ceph_osd * osd)574 static void __kick_osd_requests(struct ceph_osd_client *osdc,
575 				struct ceph_osd *osd)
576 {
577 	struct ceph_osd_request *req, *nreq;
578 	int err;
579 
580 	dout("__kick_osd_requests osd%d\n", osd->o_osd);
581 	err = __reset_osd(osdc, osd);
582 	if (err)
583 		return;
584 
585 	list_for_each_entry(req, &osd->o_requests, r_osd_item) {
586 		list_move(&req->r_req_lru_item, &osdc->req_unsent);
587 		dout("requeued %p tid %llu osd%d\n", req, req->r_tid,
588 		     osd->o_osd);
589 		if (!req->r_linger)
590 			req->r_flags |= CEPH_OSD_FLAG_RETRY;
591 	}
592 
593 	list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
594 				 r_linger_osd) {
595 		/*
596 		 * reregister request prior to unregistering linger so
597 		 * that r_osd is preserved.
598 		 */
599 		BUG_ON(!list_empty(&req->r_req_lru_item));
600 		__register_request(osdc, req);
601 		list_add(&req->r_req_lru_item, &osdc->req_unsent);
602 		list_add(&req->r_osd_item, &req->r_osd->o_requests);
603 		__unregister_linger_request(osdc, req);
604 		dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
605 		     osd->o_osd);
606 	}
607 }
608 
609 /*
610  * If the osd connection drops, we need to resubmit all requests.
611  */
osd_reset(struct ceph_connection * con)612 static void osd_reset(struct ceph_connection *con)
613 {
614 	struct ceph_osd *osd = con->private;
615 	struct ceph_osd_client *osdc;
616 
617 	if (!osd)
618 		return;
619 	dout("osd_reset osd%d\n", osd->o_osd);
620 	osdc = osd->o_osdc;
621 	down_read(&osdc->map_sem);
622 	mutex_lock(&osdc->request_mutex);
623 	__kick_osd_requests(osdc, osd);
624 	mutex_unlock(&osdc->request_mutex);
625 	send_queued(osdc);
626 	up_read(&osdc->map_sem);
627 }
628 
629 /*
630  * Track open sessions with osds.
631  */
create_osd(struct ceph_osd_client * osdc,int onum)632 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
633 {
634 	struct ceph_osd *osd;
635 
636 	osd = kzalloc(sizeof(*osd), GFP_NOFS);
637 	if (!osd)
638 		return NULL;
639 
640 	atomic_set(&osd->o_ref, 1);
641 	osd->o_osdc = osdc;
642 	osd->o_osd = onum;
643 	RB_CLEAR_NODE(&osd->o_node);
644 	INIT_LIST_HEAD(&osd->o_requests);
645 	INIT_LIST_HEAD(&osd->o_linger_requests);
646 	INIT_LIST_HEAD(&osd->o_osd_lru);
647 	osd->o_incarnation = 1;
648 
649 	ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
650 
651 	INIT_LIST_HEAD(&osd->o_keepalive_item);
652 	return osd;
653 }
654 
get_osd(struct ceph_osd * osd)655 static struct ceph_osd *get_osd(struct ceph_osd *osd)
656 {
657 	if (atomic_inc_not_zero(&osd->o_ref)) {
658 		dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
659 		     atomic_read(&osd->o_ref));
660 		return osd;
661 	} else {
662 		dout("get_osd %p FAIL\n", osd);
663 		return NULL;
664 	}
665 }
666 
put_osd(struct ceph_osd * osd)667 static void put_osd(struct ceph_osd *osd)
668 {
669 	dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
670 	     atomic_read(&osd->o_ref) - 1);
671 	if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
672 		struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
673 
674 		ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
675 		kfree(osd);
676 	}
677 }
678 
679 /*
680  * remove an osd from our map
681  */
__remove_osd(struct ceph_osd_client * osdc,struct ceph_osd * osd)682 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
683 {
684 	dout("__remove_osd %p\n", osd);
685 	BUG_ON(!list_empty(&osd->o_requests));
686 	rb_erase(&osd->o_node, &osdc->osds);
687 	list_del_init(&osd->o_osd_lru);
688 	ceph_con_close(&osd->o_con);
689 	put_osd(osd);
690 }
691 
remove_all_osds(struct ceph_osd_client * osdc)692 static void remove_all_osds(struct ceph_osd_client *osdc)
693 {
694 	dout("__remove_old_osds %p\n", osdc);
695 	mutex_lock(&osdc->request_mutex);
696 	while (!RB_EMPTY_ROOT(&osdc->osds)) {
697 		struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
698 						struct ceph_osd, o_node);
699 		__remove_osd(osdc, osd);
700 	}
701 	mutex_unlock(&osdc->request_mutex);
702 }
703 
__move_osd_to_lru(struct ceph_osd_client * osdc,struct ceph_osd * osd)704 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
705 			      struct ceph_osd *osd)
706 {
707 	dout("__move_osd_to_lru %p\n", osd);
708 	BUG_ON(!list_empty(&osd->o_osd_lru));
709 	list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
710 	osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
711 }
712 
__remove_osd_from_lru(struct ceph_osd * osd)713 static void __remove_osd_from_lru(struct ceph_osd *osd)
714 {
715 	dout("__remove_osd_from_lru %p\n", osd);
716 	if (!list_empty(&osd->o_osd_lru))
717 		list_del_init(&osd->o_osd_lru);
718 }
719 
remove_old_osds(struct ceph_osd_client * osdc)720 static void remove_old_osds(struct ceph_osd_client *osdc)
721 {
722 	struct ceph_osd *osd, *nosd;
723 
724 	dout("__remove_old_osds %p\n", osdc);
725 	mutex_lock(&osdc->request_mutex);
726 	list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
727 		if (time_before(jiffies, osd->lru_ttl))
728 			break;
729 		__remove_osd(osdc, osd);
730 	}
731 	mutex_unlock(&osdc->request_mutex);
732 }
733 
734 /*
735  * reset osd connect
736  */
__reset_osd(struct ceph_osd_client * osdc,struct ceph_osd * osd)737 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
738 {
739 	struct ceph_osd_request *req;
740 	int ret = 0;
741 
742 	dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
743 	if (list_empty(&osd->o_requests) &&
744 	    list_empty(&osd->o_linger_requests)) {
745 		__remove_osd(osdc, osd);
746 		ret = -ENODEV;
747 	} else if (memcmp(&osdc->osdmap->osd_addr[osd->o_osd],
748 			  &osd->o_con.peer_addr,
749 			  sizeof(osd->o_con.peer_addr)) == 0 &&
750 		   !ceph_con_opened(&osd->o_con)) {
751 		dout(" osd addr hasn't changed and connection never opened,"
752 		     " letting msgr retry");
753 		/* touch each r_stamp for handle_timeout()'s benfit */
754 		list_for_each_entry(req, &osd->o_requests, r_osd_item)
755 			req->r_stamp = jiffies;
756 		ret = -EAGAIN;
757 	} else {
758 		ceph_con_close(&osd->o_con);
759 		ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
760 			      &osdc->osdmap->osd_addr[osd->o_osd]);
761 		osd->o_incarnation++;
762 	}
763 	return ret;
764 }
765 
__insert_osd(struct ceph_osd_client * osdc,struct ceph_osd * new)766 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
767 {
768 	struct rb_node **p = &osdc->osds.rb_node;
769 	struct rb_node *parent = NULL;
770 	struct ceph_osd *osd = NULL;
771 
772 	dout("__insert_osd %p osd%d\n", new, new->o_osd);
773 	while (*p) {
774 		parent = *p;
775 		osd = rb_entry(parent, struct ceph_osd, o_node);
776 		if (new->o_osd < osd->o_osd)
777 			p = &(*p)->rb_left;
778 		else if (new->o_osd > osd->o_osd)
779 			p = &(*p)->rb_right;
780 		else
781 			BUG();
782 	}
783 
784 	rb_link_node(&new->o_node, parent, p);
785 	rb_insert_color(&new->o_node, &osdc->osds);
786 }
787 
__lookup_osd(struct ceph_osd_client * osdc,int o)788 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
789 {
790 	struct ceph_osd *osd;
791 	struct rb_node *n = osdc->osds.rb_node;
792 
793 	while (n) {
794 		osd = rb_entry(n, struct ceph_osd, o_node);
795 		if (o < osd->o_osd)
796 			n = n->rb_left;
797 		else if (o > osd->o_osd)
798 			n = n->rb_right;
799 		else
800 			return osd;
801 	}
802 	return NULL;
803 }
804 
__schedule_osd_timeout(struct ceph_osd_client * osdc)805 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
806 {
807 	schedule_delayed_work(&osdc->timeout_work,
808 			osdc->client->options->osd_keepalive_timeout * HZ);
809 }
810 
__cancel_osd_timeout(struct ceph_osd_client * osdc)811 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
812 {
813 	cancel_delayed_work(&osdc->timeout_work);
814 }
815 
816 /*
817  * Register request, assign tid.  If this is the first request, set up
818  * the timeout event.
819  */
__register_request(struct ceph_osd_client * osdc,struct ceph_osd_request * req)820 static void __register_request(struct ceph_osd_client *osdc,
821 			       struct ceph_osd_request *req)
822 {
823 	req->r_tid = ++osdc->last_tid;
824 	req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
825 	dout("__register_request %p tid %lld\n", req, req->r_tid);
826 	__insert_request(osdc, req);
827 	ceph_osdc_get_request(req);
828 	osdc->num_requests++;
829 	if (osdc->num_requests == 1) {
830 		dout(" first request, scheduling timeout\n");
831 		__schedule_osd_timeout(osdc);
832 	}
833 }
834 
register_request(struct ceph_osd_client * osdc,struct ceph_osd_request * req)835 static void register_request(struct ceph_osd_client *osdc,
836 			     struct ceph_osd_request *req)
837 {
838 	mutex_lock(&osdc->request_mutex);
839 	__register_request(osdc, req);
840 	mutex_unlock(&osdc->request_mutex);
841 }
842 
843 /*
844  * called under osdc->request_mutex
845  */
__unregister_request(struct ceph_osd_client * osdc,struct ceph_osd_request * req)846 static void __unregister_request(struct ceph_osd_client *osdc,
847 				 struct ceph_osd_request *req)
848 {
849 	if (RB_EMPTY_NODE(&req->r_node)) {
850 		dout("__unregister_request %p tid %lld not registered\n",
851 			req, req->r_tid);
852 		return;
853 	}
854 
855 	dout("__unregister_request %p tid %lld\n", req, req->r_tid);
856 	rb_erase(&req->r_node, &osdc->requests);
857 	osdc->num_requests--;
858 
859 	if (req->r_osd) {
860 		/* make sure the original request isn't in flight. */
861 		ceph_msg_revoke(req->r_request);
862 
863 		list_del_init(&req->r_osd_item);
864 		if (list_empty(&req->r_osd->o_requests) &&
865 		    list_empty(&req->r_osd->o_linger_requests)) {
866 			dout("moving osd to %p lru\n", req->r_osd);
867 			__move_osd_to_lru(osdc, req->r_osd);
868 		}
869 		if (list_empty(&req->r_linger_item))
870 			req->r_osd = NULL;
871 	}
872 
873 	list_del_init(&req->r_req_lru_item);
874 	ceph_osdc_put_request(req);
875 
876 	if (osdc->num_requests == 0) {
877 		dout(" no requests, canceling timeout\n");
878 		__cancel_osd_timeout(osdc);
879 	}
880 }
881 
882 /*
883  * Cancel a previously queued request message
884  */
__cancel_request(struct ceph_osd_request * req)885 static void __cancel_request(struct ceph_osd_request *req)
886 {
887 	if (req->r_sent && req->r_osd) {
888 		ceph_msg_revoke(req->r_request);
889 		req->r_sent = 0;
890 	}
891 }
892 
__register_linger_request(struct ceph_osd_client * osdc,struct ceph_osd_request * req)893 static void __register_linger_request(struct ceph_osd_client *osdc,
894 				    struct ceph_osd_request *req)
895 {
896 	dout("__register_linger_request %p\n", req);
897 	list_add_tail(&req->r_linger_item, &osdc->req_linger);
898 	if (req->r_osd)
899 		list_add_tail(&req->r_linger_osd,
900 			      &req->r_osd->o_linger_requests);
901 }
902 
__unregister_linger_request(struct ceph_osd_client * osdc,struct ceph_osd_request * req)903 static void __unregister_linger_request(struct ceph_osd_client *osdc,
904 					struct ceph_osd_request *req)
905 {
906 	dout("__unregister_linger_request %p\n", req);
907 	list_del_init(&req->r_linger_item);
908 	if (req->r_osd) {
909 		list_del_init(&req->r_linger_osd);
910 
911 		if (list_empty(&req->r_osd->o_requests) &&
912 		    list_empty(&req->r_osd->o_linger_requests)) {
913 			dout("moving osd to %p lru\n", req->r_osd);
914 			__move_osd_to_lru(osdc, req->r_osd);
915 		}
916 		if (list_empty(&req->r_osd_item))
917 			req->r_osd = NULL;
918 	}
919 }
920 
ceph_osdc_unregister_linger_request(struct ceph_osd_client * osdc,struct ceph_osd_request * req)921 void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
922 					 struct ceph_osd_request *req)
923 {
924 	mutex_lock(&osdc->request_mutex);
925 	if (req->r_linger) {
926 		__unregister_linger_request(osdc, req);
927 		ceph_osdc_put_request(req);
928 	}
929 	mutex_unlock(&osdc->request_mutex);
930 }
931 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
932 
ceph_osdc_set_request_linger(struct ceph_osd_client * osdc,struct ceph_osd_request * req)933 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
934 				  struct ceph_osd_request *req)
935 {
936 	if (!req->r_linger) {
937 		dout("set_request_linger %p\n", req);
938 		req->r_linger = 1;
939 		/*
940 		 * caller is now responsible for calling
941 		 * unregister_linger_request
942 		 */
943 		ceph_osdc_get_request(req);
944 	}
945 }
946 EXPORT_SYMBOL(ceph_osdc_set_request_linger);
947 
948 /*
949  * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
950  * (as needed), and set the request r_osd appropriately.  If there is
951  * no up osd, set r_osd to NULL.  Move the request to the appropriate list
952  * (unsent, homeless) or leave on in-flight lru.
953  *
954  * Return 0 if unchanged, 1 if changed, or negative on error.
955  *
956  * Caller should hold map_sem for read and request_mutex.
957  */
__map_request(struct ceph_osd_client * osdc,struct ceph_osd_request * req,int force_resend)958 static int __map_request(struct ceph_osd_client *osdc,
959 			 struct ceph_osd_request *req, int force_resend)
960 {
961 	struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
962 	struct ceph_pg pgid;
963 	int acting[CEPH_PG_MAX_SIZE];
964 	int o = -1, num = 0;
965 	int err;
966 
967 	dout("map_request %p tid %lld\n", req, req->r_tid);
968 	err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
969 				      &req->r_file_layout, osdc->osdmap);
970 	if (err) {
971 		list_move(&req->r_req_lru_item, &osdc->req_notarget);
972 		return err;
973 	}
974 	pgid = reqhead->layout.ol_pgid;
975 	req->r_pgid = pgid;
976 
977 	err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
978 	if (err > 0) {
979 		o = acting[0];
980 		num = err;
981 	}
982 
983 	if ((!force_resend &&
984 	     req->r_osd && req->r_osd->o_osd == o &&
985 	     req->r_sent >= req->r_osd->o_incarnation &&
986 	     req->r_num_pg_osds == num &&
987 	     memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
988 	    (req->r_osd == NULL && o == -1))
989 		return 0;  /* no change */
990 
991 	dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
992 	     req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
993 	     req->r_osd ? req->r_osd->o_osd : -1);
994 
995 	/* record full pg acting set */
996 	memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
997 	req->r_num_pg_osds = num;
998 
999 	if (req->r_osd) {
1000 		__cancel_request(req);
1001 		list_del_init(&req->r_osd_item);
1002 		req->r_osd = NULL;
1003 	}
1004 
1005 	req->r_osd = __lookup_osd(osdc, o);
1006 	if (!req->r_osd && o >= 0) {
1007 		err = -ENOMEM;
1008 		req->r_osd = create_osd(osdc, o);
1009 		if (!req->r_osd) {
1010 			list_move(&req->r_req_lru_item, &osdc->req_notarget);
1011 			goto out;
1012 		}
1013 
1014 		dout("map_request osd %p is osd%d\n", req->r_osd, o);
1015 		__insert_osd(osdc, req->r_osd);
1016 
1017 		ceph_con_open(&req->r_osd->o_con,
1018 			      CEPH_ENTITY_TYPE_OSD, o,
1019 			      &osdc->osdmap->osd_addr[o]);
1020 	}
1021 
1022 	if (req->r_osd) {
1023 		__remove_osd_from_lru(req->r_osd);
1024 		list_add(&req->r_osd_item, &req->r_osd->o_requests);
1025 		list_move(&req->r_req_lru_item, &osdc->req_unsent);
1026 	} else {
1027 		list_move(&req->r_req_lru_item, &osdc->req_notarget);
1028 	}
1029 	err = 1;   /* osd or pg changed */
1030 
1031 out:
1032 	return err;
1033 }
1034 
1035 /*
1036  * caller should hold map_sem (for read) and request_mutex
1037  */
__send_request(struct ceph_osd_client * osdc,struct ceph_osd_request * req)1038 static void __send_request(struct ceph_osd_client *osdc,
1039 			   struct ceph_osd_request *req)
1040 {
1041 	struct ceph_osd_request_head *reqhead;
1042 
1043 	dout("send_request %p tid %llu to osd%d flags %d\n",
1044 	     req, req->r_tid, req->r_osd->o_osd, req->r_flags);
1045 
1046 	reqhead = req->r_request->front.iov_base;
1047 	reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
1048 	reqhead->flags |= cpu_to_le32(req->r_flags);  /* e.g., RETRY */
1049 	reqhead->reassert_version = req->r_reassert_version;
1050 
1051 	req->r_stamp = jiffies;
1052 	list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1053 
1054 	ceph_msg_get(req->r_request); /* send consumes a ref */
1055 	ceph_con_send(&req->r_osd->o_con, req->r_request);
1056 	req->r_sent = req->r_osd->o_incarnation;
1057 }
1058 
1059 /*
1060  * Send any requests in the queue (req_unsent).
1061  */
send_queued(struct ceph_osd_client * osdc)1062 static void send_queued(struct ceph_osd_client *osdc)
1063 {
1064 	struct ceph_osd_request *req, *tmp;
1065 
1066 	dout("send_queued\n");
1067 	mutex_lock(&osdc->request_mutex);
1068 	list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) {
1069 		__send_request(osdc, req);
1070 	}
1071 	mutex_unlock(&osdc->request_mutex);
1072 }
1073 
1074 /*
1075  * Timeout callback, called every N seconds when 1 or more osd
1076  * requests has been active for more than N seconds.  When this
1077  * happens, we ping all OSDs with requests who have timed out to
1078  * ensure any communications channel reset is detected.  Reset the
1079  * request timeouts another N seconds in the future as we go.
1080  * Reschedule the timeout event another N seconds in future (unless
1081  * there are no open requests).
1082  */
handle_timeout(struct work_struct * work)1083 static void handle_timeout(struct work_struct *work)
1084 {
1085 	struct ceph_osd_client *osdc =
1086 		container_of(work, struct ceph_osd_client, timeout_work.work);
1087 	struct ceph_osd_request *req;
1088 	struct ceph_osd *osd;
1089 	unsigned long keepalive =
1090 		osdc->client->options->osd_keepalive_timeout * HZ;
1091 	struct list_head slow_osds;
1092 	dout("timeout\n");
1093 	down_read(&osdc->map_sem);
1094 
1095 	ceph_monc_request_next_osdmap(&osdc->client->monc);
1096 
1097 	mutex_lock(&osdc->request_mutex);
1098 
1099 	/*
1100 	 * ping osds that are a bit slow.  this ensures that if there
1101 	 * is a break in the TCP connection we will notice, and reopen
1102 	 * a connection with that osd (from the fault callback).
1103 	 */
1104 	INIT_LIST_HEAD(&slow_osds);
1105 	list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1106 		if (time_before(jiffies, req->r_stamp + keepalive))
1107 			break;
1108 
1109 		osd = req->r_osd;
1110 		BUG_ON(!osd);
1111 		dout(" tid %llu is slow, will send keepalive on osd%d\n",
1112 		     req->r_tid, osd->o_osd);
1113 		list_move_tail(&osd->o_keepalive_item, &slow_osds);
1114 	}
1115 	while (!list_empty(&slow_osds)) {
1116 		osd = list_entry(slow_osds.next, struct ceph_osd,
1117 				 o_keepalive_item);
1118 		list_del_init(&osd->o_keepalive_item);
1119 		ceph_con_keepalive(&osd->o_con);
1120 	}
1121 
1122 	__schedule_osd_timeout(osdc);
1123 	mutex_unlock(&osdc->request_mutex);
1124 	send_queued(osdc);
1125 	up_read(&osdc->map_sem);
1126 }
1127 
handle_osds_timeout(struct work_struct * work)1128 static void handle_osds_timeout(struct work_struct *work)
1129 {
1130 	struct ceph_osd_client *osdc =
1131 		container_of(work, struct ceph_osd_client,
1132 			     osds_timeout_work.work);
1133 	unsigned long delay =
1134 		osdc->client->options->osd_idle_ttl * HZ >> 2;
1135 
1136 	dout("osds timeout\n");
1137 	down_read(&osdc->map_sem);
1138 	remove_old_osds(osdc);
1139 	up_read(&osdc->map_sem);
1140 
1141 	schedule_delayed_work(&osdc->osds_timeout_work,
1142 			      round_jiffies_relative(delay));
1143 }
1144 
complete_request(struct ceph_osd_request * req)1145 static void complete_request(struct ceph_osd_request *req)
1146 {
1147 	if (req->r_safe_callback)
1148 		req->r_safe_callback(req, NULL);
1149 	complete_all(&req->r_safe_completion);  /* fsync waiter */
1150 }
1151 
1152 /*
1153  * handle osd op reply.  either call the callback if it is specified,
1154  * or do the completion to wake up the waiting thread.
1155  */
handle_reply(struct ceph_osd_client * osdc,struct ceph_msg * msg,struct ceph_connection * con)1156 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1157 			 struct ceph_connection *con)
1158 {
1159 	struct ceph_osd_reply_head *rhead = msg->front.iov_base;
1160 	struct ceph_osd_request *req;
1161 	u64 tid;
1162 	int numops, object_len, flags;
1163 	s32 result;
1164 
1165 	tid = le64_to_cpu(msg->hdr.tid);
1166 	if (msg->front.iov_len < sizeof(*rhead))
1167 		goto bad;
1168 	numops = le32_to_cpu(rhead->num_ops);
1169 	object_len = le32_to_cpu(rhead->object_len);
1170 	result = le32_to_cpu(rhead->result);
1171 	if (msg->front.iov_len != sizeof(*rhead) + object_len +
1172 	    numops * sizeof(struct ceph_osd_op))
1173 		goto bad;
1174 	dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
1175 	/* lookup */
1176 	mutex_lock(&osdc->request_mutex);
1177 	req = __lookup_request(osdc, tid);
1178 	if (req == NULL) {
1179 		dout("handle_reply tid %llu dne\n", tid);
1180 		mutex_unlock(&osdc->request_mutex);
1181 		return;
1182 	}
1183 	ceph_osdc_get_request(req);
1184 	flags = le32_to_cpu(rhead->flags);
1185 
1186 	/*
1187 	 * if this connection filled our message, drop our reference now, to
1188 	 * avoid a (safe but slower) revoke later.
1189 	 */
1190 	if (req->r_con_filling_msg == con && req->r_reply == msg) {
1191 		dout(" dropping con_filling_msg ref %p\n", con);
1192 		req->r_con_filling_msg = NULL;
1193 		con->ops->put(con);
1194 	}
1195 
1196 	if (!req->r_got_reply) {
1197 		unsigned bytes;
1198 
1199 		req->r_result = le32_to_cpu(rhead->result);
1200 		bytes = le32_to_cpu(msg->hdr.data_len);
1201 		dout("handle_reply result %d bytes %d\n", req->r_result,
1202 		     bytes);
1203 		if (req->r_result == 0)
1204 			req->r_result = bytes;
1205 
1206 		/* in case this is a write and we need to replay, */
1207 		req->r_reassert_version = rhead->reassert_version;
1208 
1209 		req->r_got_reply = 1;
1210 	} else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1211 		dout("handle_reply tid %llu dup ack\n", tid);
1212 		mutex_unlock(&osdc->request_mutex);
1213 		goto done;
1214 	}
1215 
1216 	dout("handle_reply tid %llu flags %d\n", tid, flags);
1217 
1218 	if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1219 		__register_linger_request(osdc, req);
1220 
1221 	/* either this is a read, or we got the safe response */
1222 	if (result < 0 ||
1223 	    (flags & CEPH_OSD_FLAG_ONDISK) ||
1224 	    ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1225 		__unregister_request(osdc, req);
1226 
1227 	mutex_unlock(&osdc->request_mutex);
1228 
1229 	if (req->r_callback)
1230 		req->r_callback(req, msg);
1231 	else
1232 		complete_all(&req->r_completion);
1233 
1234 	if (flags & CEPH_OSD_FLAG_ONDISK)
1235 		complete_request(req);
1236 
1237 done:
1238 	dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1239 	ceph_osdc_put_request(req);
1240 	return;
1241 
1242 bad:
1243 	pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1244 	       (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
1245 	       (int)sizeof(*rhead));
1246 	ceph_msg_dump(msg);
1247 }
1248 
reset_changed_osds(struct ceph_osd_client * osdc)1249 static void reset_changed_osds(struct ceph_osd_client *osdc)
1250 {
1251 	struct rb_node *p, *n;
1252 
1253 	for (p = rb_first(&osdc->osds); p; p = n) {
1254 		struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
1255 
1256 		n = rb_next(p);
1257 		if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1258 		    memcmp(&osd->o_con.peer_addr,
1259 			   ceph_osd_addr(osdc->osdmap,
1260 					 osd->o_osd),
1261 			   sizeof(struct ceph_entity_addr)) != 0)
1262 			__reset_osd(osdc, osd);
1263 	}
1264 }
1265 
1266 /*
1267  * Requeue requests whose mapping to an OSD has changed.  If requests map to
1268  * no osd, request a new map.
1269  *
1270  * Caller should hold map_sem for read.
1271  */
kick_requests(struct ceph_osd_client * osdc,bool force_resend,bool force_resend_writes)1272 static void kick_requests(struct ceph_osd_client *osdc, bool force_resend,
1273 			  bool force_resend_writes)
1274 {
1275 	struct ceph_osd_request *req, *nreq;
1276 	struct rb_node *p;
1277 	int needmap = 0;
1278 	int err;
1279 	bool force_resend_req;
1280 
1281 	dout("kick_requests %s %s\n", force_resend ? " (force resend)" : "",
1282 		force_resend_writes ? " (force resend writes)" : "");
1283 	mutex_lock(&osdc->request_mutex);
1284 	for (p = rb_first(&osdc->requests); p; ) {
1285 		req = rb_entry(p, struct ceph_osd_request, r_node);
1286 		p = rb_next(p);
1287 
1288 		/*
1289 		 * For linger requests that have not yet been
1290 		 * registered, move them to the linger list; they'll
1291 		 * be sent to the osd in the loop below.  Unregister
1292 		 * the request before re-registering it as a linger
1293 		 * request to ensure the __map_request() below
1294 		 * will decide it needs to be sent.
1295 		 */
1296 		if (req->r_linger && list_empty(&req->r_linger_item)) {
1297 			dout("%p tid %llu restart on osd%d\n",
1298 			     req, req->r_tid,
1299 			     req->r_osd ? req->r_osd->o_osd : -1);
1300 			__unregister_request(osdc, req);
1301 			__register_linger_request(osdc, req);
1302 			continue;
1303 		}
1304 
1305 		force_resend_req = force_resend ||
1306 			(force_resend_writes &&
1307 				req->r_flags & CEPH_OSD_FLAG_WRITE);
1308 		err = __map_request(osdc, req, force_resend_req);
1309 		if (err < 0)
1310 			continue;  /* error */
1311 		if (req->r_osd == NULL) {
1312 			dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1313 			needmap++;  /* request a newer map */
1314 		} else if (err > 0) {
1315 			if (!req->r_linger) {
1316 				dout("%p tid %llu requeued on osd%d\n", req,
1317 				     req->r_tid,
1318 				     req->r_osd ? req->r_osd->o_osd : -1);
1319 				req->r_flags |= CEPH_OSD_FLAG_RETRY;
1320 			}
1321 		}
1322 	}
1323 
1324 	list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1325 				 r_linger_item) {
1326 		dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1327 
1328 		err = __map_request(osdc, req,
1329 				    force_resend || force_resend_writes);
1330 		dout("__map_request returned %d\n", err);
1331 		if (err == 0)
1332 			continue;  /* no change and no osd was specified */
1333 		if (err < 0)
1334 			continue;  /* hrm! */
1335 		if (req->r_osd == NULL) {
1336 			dout("tid %llu maps to no valid osd\n", req->r_tid);
1337 			needmap++;  /* request a newer map */
1338 			continue;
1339 		}
1340 
1341 		dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1342 		     req->r_osd ? req->r_osd->o_osd : -1);
1343 		__register_request(osdc, req);
1344 		__unregister_linger_request(osdc, req);
1345 	}
1346 	reset_changed_osds(osdc);
1347 	mutex_unlock(&osdc->request_mutex);
1348 
1349 	if (needmap) {
1350 		dout("%d requests for down osds, need new map\n", needmap);
1351 		ceph_monc_request_next_osdmap(&osdc->client->monc);
1352 	}
1353 }
1354 
1355 
1356 /*
1357  * Process updated osd map.
1358  *
1359  * The message contains any number of incremental and full maps, normally
1360  * indicating some sort of topology change in the cluster.  Kick requests
1361  * off to different OSDs as needed.
1362  */
ceph_osdc_handle_map(struct ceph_osd_client * osdc,struct ceph_msg * msg)1363 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1364 {
1365 	void *p, *end, *next;
1366 	u32 nr_maps, maplen;
1367 	u32 epoch;
1368 	struct ceph_osdmap *newmap = NULL, *oldmap;
1369 	int err;
1370 	struct ceph_fsid fsid;
1371 	bool was_full;
1372 
1373 	dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1374 	p = msg->front.iov_base;
1375 	end = p + msg->front.iov_len;
1376 
1377 	/* verify fsid */
1378 	ceph_decode_need(&p, end, sizeof(fsid), bad);
1379 	ceph_decode_copy(&p, &fsid, sizeof(fsid));
1380 	if (ceph_check_fsid(osdc->client, &fsid) < 0)
1381 		return;
1382 
1383 	down_write(&osdc->map_sem);
1384 
1385 	was_full = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
1386 
1387 	/* incremental maps */
1388 	ceph_decode_32_safe(&p, end, nr_maps, bad);
1389 	dout(" %d inc maps\n", nr_maps);
1390 	while (nr_maps > 0) {
1391 		ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1392 		epoch = ceph_decode_32(&p);
1393 		maplen = ceph_decode_32(&p);
1394 		ceph_decode_need(&p, end, maplen, bad);
1395 		next = p + maplen;
1396 		if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1397 			dout("applying incremental map %u len %d\n",
1398 			     epoch, maplen);
1399 			newmap = osdmap_apply_incremental(&p, next,
1400 							  osdc->osdmap,
1401 							  &osdc->client->msgr);
1402 			if (IS_ERR(newmap)) {
1403 				err = PTR_ERR(newmap);
1404 				goto bad;
1405 			}
1406 			BUG_ON(!newmap);
1407 			if (newmap != osdc->osdmap) {
1408 				ceph_osdmap_destroy(osdc->osdmap);
1409 				osdc->osdmap = newmap;
1410 			}
1411 			was_full = was_full ||
1412 				ceph_osdmap_flag(osdc->osdmap,
1413 						 CEPH_OSDMAP_FULL);
1414 			kick_requests(osdc, 0, was_full);
1415 		} else {
1416 			dout("ignoring incremental map %u len %d\n",
1417 			     epoch, maplen);
1418 		}
1419 		p = next;
1420 		nr_maps--;
1421 	}
1422 	if (newmap)
1423 		goto done;
1424 
1425 	/* full maps */
1426 	ceph_decode_32_safe(&p, end, nr_maps, bad);
1427 	dout(" %d full maps\n", nr_maps);
1428 	while (nr_maps) {
1429 		ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1430 		epoch = ceph_decode_32(&p);
1431 		maplen = ceph_decode_32(&p);
1432 		ceph_decode_need(&p, end, maplen, bad);
1433 		if (nr_maps > 1) {
1434 			dout("skipping non-latest full map %u len %d\n",
1435 			     epoch, maplen);
1436 		} else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1437 			dout("skipping full map %u len %d, "
1438 			     "older than our %u\n", epoch, maplen,
1439 			     osdc->osdmap->epoch);
1440 		} else {
1441 			int skipped_map = 0;
1442 
1443 			dout("taking full map %u len %d\n", epoch, maplen);
1444 			newmap = osdmap_decode(&p, p+maplen);
1445 			if (IS_ERR(newmap)) {
1446 				err = PTR_ERR(newmap);
1447 				goto bad;
1448 			}
1449 			BUG_ON(!newmap);
1450 			oldmap = osdc->osdmap;
1451 			osdc->osdmap = newmap;
1452 			if (oldmap) {
1453 				if (oldmap->epoch + 1 < newmap->epoch)
1454 					skipped_map = 1;
1455 				ceph_osdmap_destroy(oldmap);
1456 			}
1457 			was_full = was_full ||
1458 				ceph_osdmap_flag(osdc->osdmap,
1459 						 CEPH_OSDMAP_FULL);
1460 			kick_requests(osdc, skipped_map, was_full);
1461 		}
1462 		p += maplen;
1463 		nr_maps--;
1464 	}
1465 
1466 done:
1467 	downgrade_write(&osdc->map_sem);
1468 	ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1469 
1470 	/*
1471 	 * subscribe to subsequent osdmap updates if full to ensure
1472 	 * we find out when we are no longer full and stop returning
1473 	 * ENOSPC.
1474 	 */
1475 	if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1476 		ceph_monc_request_next_osdmap(&osdc->client->monc);
1477 
1478 	send_queued(osdc);
1479 	up_read(&osdc->map_sem);
1480 	wake_up_all(&osdc->client->auth_wq);
1481 	return;
1482 
1483 bad:
1484 	pr_err("osdc handle_map corrupt msg\n");
1485 	ceph_msg_dump(msg);
1486 	up_write(&osdc->map_sem);
1487 	return;
1488 }
1489 
1490 /*
1491  * watch/notify callback event infrastructure
1492  *
1493  * These callbacks are used both for watch and notify operations.
1494  */
__release_event(struct kref * kref)1495 static void __release_event(struct kref *kref)
1496 {
1497 	struct ceph_osd_event *event =
1498 		container_of(kref, struct ceph_osd_event, kref);
1499 
1500 	dout("__release_event %p\n", event);
1501 	kfree(event);
1502 }
1503 
get_event(struct ceph_osd_event * event)1504 static void get_event(struct ceph_osd_event *event)
1505 {
1506 	kref_get(&event->kref);
1507 }
1508 
ceph_osdc_put_event(struct ceph_osd_event * event)1509 void ceph_osdc_put_event(struct ceph_osd_event *event)
1510 {
1511 	kref_put(&event->kref, __release_event);
1512 }
1513 EXPORT_SYMBOL(ceph_osdc_put_event);
1514 
__insert_event(struct ceph_osd_client * osdc,struct ceph_osd_event * new)1515 static void __insert_event(struct ceph_osd_client *osdc,
1516 			     struct ceph_osd_event *new)
1517 {
1518 	struct rb_node **p = &osdc->event_tree.rb_node;
1519 	struct rb_node *parent = NULL;
1520 	struct ceph_osd_event *event = NULL;
1521 
1522 	while (*p) {
1523 		parent = *p;
1524 		event = rb_entry(parent, struct ceph_osd_event, node);
1525 		if (new->cookie < event->cookie)
1526 			p = &(*p)->rb_left;
1527 		else if (new->cookie > event->cookie)
1528 			p = &(*p)->rb_right;
1529 		else
1530 			BUG();
1531 	}
1532 
1533 	rb_link_node(&new->node, parent, p);
1534 	rb_insert_color(&new->node, &osdc->event_tree);
1535 }
1536 
__find_event(struct ceph_osd_client * osdc,u64 cookie)1537 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1538 					        u64 cookie)
1539 {
1540 	struct rb_node **p = &osdc->event_tree.rb_node;
1541 	struct rb_node *parent = NULL;
1542 	struct ceph_osd_event *event = NULL;
1543 
1544 	while (*p) {
1545 		parent = *p;
1546 		event = rb_entry(parent, struct ceph_osd_event, node);
1547 		if (cookie < event->cookie)
1548 			p = &(*p)->rb_left;
1549 		else if (cookie > event->cookie)
1550 			p = &(*p)->rb_right;
1551 		else
1552 			return event;
1553 	}
1554 	return NULL;
1555 }
1556 
__remove_event(struct ceph_osd_event * event)1557 static void __remove_event(struct ceph_osd_event *event)
1558 {
1559 	struct ceph_osd_client *osdc = event->osdc;
1560 
1561 	if (!RB_EMPTY_NODE(&event->node)) {
1562 		dout("__remove_event removed %p\n", event);
1563 		rb_erase(&event->node, &osdc->event_tree);
1564 		ceph_osdc_put_event(event);
1565 	} else {
1566 		dout("__remove_event didn't remove %p\n", event);
1567 	}
1568 }
1569 
ceph_osdc_create_event(struct ceph_osd_client * osdc,void (* event_cb)(u64,u64,u8,void *),int one_shot,void * data,struct ceph_osd_event ** pevent)1570 int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1571 			   void (*event_cb)(u64, u64, u8, void *),
1572 			   int one_shot, void *data,
1573 			   struct ceph_osd_event **pevent)
1574 {
1575 	struct ceph_osd_event *event;
1576 
1577 	event = kmalloc(sizeof(*event), GFP_NOIO);
1578 	if (!event)
1579 		return -ENOMEM;
1580 
1581 	dout("create_event %p\n", event);
1582 	event->cb = event_cb;
1583 	event->one_shot = one_shot;
1584 	event->data = data;
1585 	event->osdc = osdc;
1586 	INIT_LIST_HEAD(&event->osd_node);
1587 	RB_CLEAR_NODE(&event->node);
1588 	kref_init(&event->kref);   /* one ref for us */
1589 	kref_get(&event->kref);    /* one ref for the caller */
1590 	init_completion(&event->completion);
1591 
1592 	spin_lock(&osdc->event_lock);
1593 	event->cookie = ++osdc->event_count;
1594 	__insert_event(osdc, event);
1595 	spin_unlock(&osdc->event_lock);
1596 
1597 	*pevent = event;
1598 	return 0;
1599 }
1600 EXPORT_SYMBOL(ceph_osdc_create_event);
1601 
ceph_osdc_cancel_event(struct ceph_osd_event * event)1602 void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1603 {
1604 	struct ceph_osd_client *osdc = event->osdc;
1605 
1606 	dout("cancel_event %p\n", event);
1607 	spin_lock(&osdc->event_lock);
1608 	__remove_event(event);
1609 	spin_unlock(&osdc->event_lock);
1610 	ceph_osdc_put_event(event); /* caller's */
1611 }
1612 EXPORT_SYMBOL(ceph_osdc_cancel_event);
1613 
1614 
do_event_work(struct work_struct * work)1615 static void do_event_work(struct work_struct *work)
1616 {
1617 	struct ceph_osd_event_work *event_work =
1618 		container_of(work, struct ceph_osd_event_work, work);
1619 	struct ceph_osd_event *event = event_work->event;
1620 	u64 ver = event_work->ver;
1621 	u64 notify_id = event_work->notify_id;
1622 	u8 opcode = event_work->opcode;
1623 
1624 	dout("do_event_work completing %p\n", event);
1625 	event->cb(ver, notify_id, opcode, event->data);
1626 	complete(&event->completion);
1627 	dout("do_event_work completed %p\n", event);
1628 	ceph_osdc_put_event(event);
1629 	kfree(event_work);
1630 }
1631 
1632 
1633 /*
1634  * Process osd watch notifications
1635  */
handle_watch_notify(struct ceph_osd_client * osdc,struct ceph_msg * msg)1636 void handle_watch_notify(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1637 {
1638 	void *p, *end;
1639 	u8 proto_ver;
1640 	u64 cookie, ver, notify_id;
1641 	u8 opcode;
1642 	struct ceph_osd_event *event;
1643 	struct ceph_osd_event_work *event_work;
1644 
1645 	p = msg->front.iov_base;
1646 	end = p + msg->front.iov_len;
1647 
1648 	ceph_decode_8_safe(&p, end, proto_ver, bad);
1649 	ceph_decode_8_safe(&p, end, opcode, bad);
1650 	ceph_decode_64_safe(&p, end, cookie, bad);
1651 	ceph_decode_64_safe(&p, end, ver, bad);
1652 	ceph_decode_64_safe(&p, end, notify_id, bad);
1653 
1654 	spin_lock(&osdc->event_lock);
1655 	event = __find_event(osdc, cookie);
1656 	if (event) {
1657 		get_event(event);
1658 		if (event->one_shot)
1659 			__remove_event(event);
1660 	}
1661 	spin_unlock(&osdc->event_lock);
1662 	dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1663 	     cookie, ver, event);
1664 	if (event) {
1665 		event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
1666 		if (!event_work) {
1667 			dout("ERROR: could not allocate event_work\n");
1668 			goto done_err;
1669 		}
1670 		INIT_WORK(&event_work->work, do_event_work);
1671 		event_work->event = event;
1672 		event_work->ver = ver;
1673 		event_work->notify_id = notify_id;
1674 		event_work->opcode = opcode;
1675 		if (!queue_work(osdc->notify_wq, &event_work->work)) {
1676 			dout("WARNING: failed to queue notify event work\n");
1677 			goto done_err;
1678 		}
1679 	}
1680 
1681 	return;
1682 
1683 done_err:
1684 	complete(&event->completion);
1685 	ceph_osdc_put_event(event);
1686 	return;
1687 
1688 bad:
1689 	pr_err("osdc handle_watch_notify corrupt msg\n");
1690 	return;
1691 }
1692 
ceph_osdc_wait_event(struct ceph_osd_event * event,unsigned long timeout)1693 int ceph_osdc_wait_event(struct ceph_osd_event *event, unsigned long timeout)
1694 {
1695 	int err;
1696 
1697 	dout("wait_event %p\n", event);
1698 	err = wait_for_completion_interruptible_timeout(&event->completion,
1699 							timeout * HZ);
1700 	ceph_osdc_put_event(event);
1701 	if (err > 0)
1702 		err = 0;
1703 	dout("wait_event %p returns %d\n", event, err);
1704 	return err;
1705 }
1706 EXPORT_SYMBOL(ceph_osdc_wait_event);
1707 
1708 /*
1709  * Register request, send initial attempt.
1710  */
ceph_osdc_start_request(struct ceph_osd_client * osdc,struct ceph_osd_request * req,bool nofail)1711 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1712 			    struct ceph_osd_request *req,
1713 			    bool nofail)
1714 {
1715 	int rc = 0;
1716 
1717 	req->r_request->pages = req->r_pages;
1718 	req->r_request->nr_pages = req->r_num_pages;
1719 #ifdef CONFIG_BLOCK
1720 	req->r_request->bio = req->r_bio;
1721 #endif
1722 	req->r_request->trail = req->r_trail;
1723 
1724 	register_request(osdc, req);
1725 
1726 	down_read(&osdc->map_sem);
1727 	mutex_lock(&osdc->request_mutex);
1728 	/*
1729 	 * a racing kick_requests() may have sent the message for us
1730 	 * while we dropped request_mutex above, so only send now if
1731 	 * the request still han't been touched yet.
1732 	 */
1733 	if (req->r_sent == 0) {
1734 		rc = __map_request(osdc, req, 0);
1735 		if (rc < 0) {
1736 			if (nofail) {
1737 				dout("osdc_start_request failed map, "
1738 				     " will retry %lld\n", req->r_tid);
1739 				rc = 0;
1740 			} else {
1741 				__unregister_request(osdc, req);
1742 			}
1743 			goto out_unlock;
1744 		}
1745 		if (req->r_osd == NULL) {
1746 			dout("send_request %p no up osds in pg\n", req);
1747 			ceph_monc_request_next_osdmap(&osdc->client->monc);
1748 		} else {
1749 			__send_request(osdc, req);
1750 		}
1751 		rc = 0;
1752 	}
1753 
1754 out_unlock:
1755 	mutex_unlock(&osdc->request_mutex);
1756 	up_read(&osdc->map_sem);
1757 	return rc;
1758 }
1759 EXPORT_SYMBOL(ceph_osdc_start_request);
1760 
1761 /*
1762  * wait for a request to complete
1763  */
ceph_osdc_wait_request(struct ceph_osd_client * osdc,struct ceph_osd_request * req)1764 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1765 			   struct ceph_osd_request *req)
1766 {
1767 	int rc;
1768 
1769 	rc = wait_for_completion_interruptible(&req->r_completion);
1770 	if (rc < 0) {
1771 		mutex_lock(&osdc->request_mutex);
1772 		__cancel_request(req);
1773 		__unregister_request(osdc, req);
1774 		mutex_unlock(&osdc->request_mutex);
1775 		complete_request(req);
1776 		dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
1777 		return rc;
1778 	}
1779 
1780 	dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1781 	return req->r_result;
1782 }
1783 EXPORT_SYMBOL(ceph_osdc_wait_request);
1784 
1785 /*
1786  * sync - wait for all in-flight requests to flush.  avoid starvation.
1787  */
ceph_osdc_sync(struct ceph_osd_client * osdc)1788 void ceph_osdc_sync(struct ceph_osd_client *osdc)
1789 {
1790 	struct ceph_osd_request *req;
1791 	u64 last_tid, next_tid = 0;
1792 
1793 	mutex_lock(&osdc->request_mutex);
1794 	last_tid = osdc->last_tid;
1795 	while (1) {
1796 		req = __lookup_request_ge(osdc, next_tid);
1797 		if (!req)
1798 			break;
1799 		if (req->r_tid > last_tid)
1800 			break;
1801 
1802 		next_tid = req->r_tid + 1;
1803 		if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1804 			continue;
1805 
1806 		ceph_osdc_get_request(req);
1807 		mutex_unlock(&osdc->request_mutex);
1808 		dout("sync waiting on tid %llu (last is %llu)\n",
1809 		     req->r_tid, last_tid);
1810 		wait_for_completion(&req->r_safe_completion);
1811 		mutex_lock(&osdc->request_mutex);
1812 		ceph_osdc_put_request(req);
1813 	}
1814 	mutex_unlock(&osdc->request_mutex);
1815 	dout("sync done (thru tid %llu)\n", last_tid);
1816 }
1817 EXPORT_SYMBOL(ceph_osdc_sync);
1818 
1819 /*
1820  * init, shutdown
1821  */
ceph_osdc_init(struct ceph_osd_client * osdc,struct ceph_client * client)1822 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1823 {
1824 	int err;
1825 
1826 	dout("init\n");
1827 	osdc->client = client;
1828 	osdc->osdmap = NULL;
1829 	init_rwsem(&osdc->map_sem);
1830 	init_completion(&osdc->map_waiters);
1831 	osdc->last_requested_map = 0;
1832 	mutex_init(&osdc->request_mutex);
1833 	osdc->last_tid = 0;
1834 	osdc->osds = RB_ROOT;
1835 	INIT_LIST_HEAD(&osdc->osd_lru);
1836 	osdc->requests = RB_ROOT;
1837 	INIT_LIST_HEAD(&osdc->req_lru);
1838 	INIT_LIST_HEAD(&osdc->req_unsent);
1839 	INIT_LIST_HEAD(&osdc->req_notarget);
1840 	INIT_LIST_HEAD(&osdc->req_linger);
1841 	osdc->num_requests = 0;
1842 	INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
1843 	INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
1844 	spin_lock_init(&osdc->event_lock);
1845 	osdc->event_tree = RB_ROOT;
1846 	osdc->event_count = 0;
1847 
1848 	schedule_delayed_work(&osdc->osds_timeout_work,
1849 	   round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
1850 
1851 	err = -ENOMEM;
1852 	osdc->req_mempool = mempool_create_kmalloc_pool(10,
1853 					sizeof(struct ceph_osd_request));
1854 	if (!osdc->req_mempool)
1855 		goto out;
1856 
1857 	err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
1858 				OSD_OP_FRONT_LEN, 10, true,
1859 				"osd_op");
1860 	if (err < 0)
1861 		goto out_mempool;
1862 	err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
1863 				OSD_OPREPLY_FRONT_LEN, 10, true,
1864 				"osd_op_reply");
1865 	if (err < 0)
1866 		goto out_msgpool;
1867 
1868 	osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
1869 	if (IS_ERR(osdc->notify_wq)) {
1870 		err = PTR_ERR(osdc->notify_wq);
1871 		osdc->notify_wq = NULL;
1872 		goto out_msgpool;
1873 	}
1874 	return 0;
1875 
1876 out_msgpool:
1877 	ceph_msgpool_destroy(&osdc->msgpool_op);
1878 out_mempool:
1879 	mempool_destroy(osdc->req_mempool);
1880 out:
1881 	return err;
1882 }
1883 EXPORT_SYMBOL(ceph_osdc_init);
1884 
ceph_osdc_stop(struct ceph_osd_client * osdc)1885 void ceph_osdc_stop(struct ceph_osd_client *osdc)
1886 {
1887 	flush_workqueue(osdc->notify_wq);
1888 	destroy_workqueue(osdc->notify_wq);
1889 	cancel_delayed_work_sync(&osdc->timeout_work);
1890 	cancel_delayed_work_sync(&osdc->osds_timeout_work);
1891 	if (osdc->osdmap) {
1892 		ceph_osdmap_destroy(osdc->osdmap);
1893 		osdc->osdmap = NULL;
1894 	}
1895 	remove_all_osds(osdc);
1896 	mempool_destroy(osdc->req_mempool);
1897 	ceph_msgpool_destroy(&osdc->msgpool_op);
1898 	ceph_msgpool_destroy(&osdc->msgpool_op_reply);
1899 }
1900 EXPORT_SYMBOL(ceph_osdc_stop);
1901 
1902 /*
1903  * Read some contiguous pages.  If we cross a stripe boundary, shorten
1904  * *plen.  Return number of bytes read, or error.
1905  */
ceph_osdc_readpages(struct ceph_osd_client * osdc,struct ceph_vino vino,struct ceph_file_layout * layout,u64 off,u64 * plen,u32 truncate_seq,u64 truncate_size,struct page ** pages,int num_pages,int page_align)1906 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1907 			struct ceph_vino vino, struct ceph_file_layout *layout,
1908 			u64 off, u64 *plen,
1909 			u32 truncate_seq, u64 truncate_size,
1910 			struct page **pages, int num_pages, int page_align)
1911 {
1912 	struct ceph_osd_request *req;
1913 	int rc = 0;
1914 
1915 	dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1916 	     vino.snap, off, *plen);
1917 	req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1918 				    CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1919 				    NULL, 0, truncate_seq, truncate_size, NULL,
1920 				    false, 1, page_align);
1921 	if (IS_ERR(req))
1922 		return PTR_ERR(req);
1923 
1924 	/* it may be a short read due to an object boundary */
1925 	req->r_pages = pages;
1926 
1927 	dout("readpages  final extent is %llu~%llu (%d pages align %d)\n",
1928 	     off, *plen, req->r_num_pages, page_align);
1929 
1930 	rc = ceph_osdc_start_request(osdc, req, false);
1931 	if (!rc)
1932 		rc = ceph_osdc_wait_request(osdc, req);
1933 
1934 	ceph_osdc_put_request(req);
1935 	dout("readpages result %d\n", rc);
1936 	return rc;
1937 }
1938 EXPORT_SYMBOL(ceph_osdc_readpages);
1939 
1940 /*
1941  * do a synchronous write on N pages
1942  */
ceph_osdc_writepages(struct ceph_osd_client * osdc,struct ceph_vino vino,struct ceph_file_layout * layout,struct ceph_snap_context * snapc,u64 off,u64 len,u32 truncate_seq,u64 truncate_size,struct timespec * mtime,struct page ** pages,int num_pages,int flags,int do_sync,bool nofail)1943 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1944 			 struct ceph_file_layout *layout,
1945 			 struct ceph_snap_context *snapc,
1946 			 u64 off, u64 len,
1947 			 u32 truncate_seq, u64 truncate_size,
1948 			 struct timespec *mtime,
1949 			 struct page **pages, int num_pages,
1950 			 int flags, int do_sync, bool nofail)
1951 {
1952 	struct ceph_osd_request *req;
1953 	int rc = 0;
1954 	int page_align = off & ~PAGE_MASK;
1955 
1956 	BUG_ON(vino.snap != CEPH_NOSNAP);
1957 	req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1958 				    CEPH_OSD_OP_WRITE,
1959 				    flags | CEPH_OSD_FLAG_ONDISK |
1960 					    CEPH_OSD_FLAG_WRITE,
1961 				    snapc, do_sync,
1962 				    truncate_seq, truncate_size, mtime,
1963 				    nofail, 1, page_align);
1964 	if (IS_ERR(req))
1965 		return PTR_ERR(req);
1966 
1967 	/* it may be a short write due to an object boundary */
1968 	req->r_pages = pages;
1969 	dout("writepages %llu~%llu (%d pages)\n", off, len,
1970 	     req->r_num_pages);
1971 
1972 	rc = ceph_osdc_start_request(osdc, req, nofail);
1973 	if (!rc)
1974 		rc = ceph_osdc_wait_request(osdc, req);
1975 
1976 	ceph_osdc_put_request(req);
1977 	if (rc == 0)
1978 		rc = len;
1979 	dout("writepages result %d\n", rc);
1980 	return rc;
1981 }
1982 EXPORT_SYMBOL(ceph_osdc_writepages);
1983 
1984 /*
1985  * handle incoming message
1986  */
dispatch(struct ceph_connection * con,struct ceph_msg * msg)1987 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1988 {
1989 	struct ceph_osd *osd = con->private;
1990 	struct ceph_osd_client *osdc;
1991 	int type = le16_to_cpu(msg->hdr.type);
1992 
1993 	if (!osd)
1994 		goto out;
1995 	osdc = osd->o_osdc;
1996 
1997 	switch (type) {
1998 	case CEPH_MSG_OSD_MAP:
1999 		ceph_osdc_handle_map(osdc, msg);
2000 		break;
2001 	case CEPH_MSG_OSD_OPREPLY:
2002 		handle_reply(osdc, msg, con);
2003 		break;
2004 	case CEPH_MSG_WATCH_NOTIFY:
2005 		handle_watch_notify(osdc, msg);
2006 		break;
2007 
2008 	default:
2009 		pr_err("received unknown message type %d %s\n", type,
2010 		       ceph_msg_type_name(type));
2011 	}
2012 out:
2013 	ceph_msg_put(msg);
2014 }
2015 
2016 /*
2017  * lookup and return message for incoming reply.  set up reply message
2018  * pages.
2019  */
get_reply(struct ceph_connection * con,struct ceph_msg_header * hdr,int * skip)2020 static struct ceph_msg *get_reply(struct ceph_connection *con,
2021 				  struct ceph_msg_header *hdr,
2022 				  int *skip)
2023 {
2024 	struct ceph_osd *osd = con->private;
2025 	struct ceph_osd_client *osdc = osd->o_osdc;
2026 	struct ceph_msg *m;
2027 	struct ceph_osd_request *req;
2028 	int front = le32_to_cpu(hdr->front_len);
2029 	int data_len = le32_to_cpu(hdr->data_len);
2030 	u64 tid;
2031 
2032 	tid = le64_to_cpu(hdr->tid);
2033 	mutex_lock(&osdc->request_mutex);
2034 	req = __lookup_request(osdc, tid);
2035 	if (!req) {
2036 		*skip = 1;
2037 		m = NULL;
2038 		pr_info("get_reply unknown tid %llu from osd%d\n", tid,
2039 			osd->o_osd);
2040 		goto out;
2041 	}
2042 
2043 	if (req->r_con_filling_msg) {
2044 		dout("%s revoking msg %p from old con %p\n", __func__,
2045 		     req->r_reply, req->r_con_filling_msg);
2046 		ceph_msg_revoke_incoming(req->r_reply);
2047 		req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
2048 		req->r_con_filling_msg = NULL;
2049 	}
2050 
2051 	if (front > req->r_reply->front.iov_len) {
2052 		pr_warning("get_reply front %d > preallocated %d\n",
2053 			   front, (int)req->r_reply->front.iov_len);
2054 		m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
2055 		if (!m)
2056 			goto out;
2057 		ceph_msg_put(req->r_reply);
2058 		req->r_reply = m;
2059 	}
2060 	m = ceph_msg_get(req->r_reply);
2061 
2062 	if (data_len > 0) {
2063 		int want = calc_pages_for(req->r_page_alignment, data_len);
2064 
2065 		if (unlikely(req->r_num_pages < want)) {
2066 			pr_warning("tid %lld reply has %d bytes %d pages, we"
2067 				   " had only %d pages ready\n", tid, data_len,
2068 				   want, req->r_num_pages);
2069 			*skip = 1;
2070 			ceph_msg_put(m);
2071 			m = NULL;
2072 			goto out;
2073 		}
2074 		m->pages = req->r_pages;
2075 		m->nr_pages = req->r_num_pages;
2076 		m->page_alignment = req->r_page_alignment;
2077 #ifdef CONFIG_BLOCK
2078 		m->bio = req->r_bio;
2079 #endif
2080 	}
2081 	*skip = 0;
2082 	req->r_con_filling_msg = con->ops->get(con);
2083 	dout("get_reply tid %lld %p\n", tid, m);
2084 
2085 out:
2086 	mutex_unlock(&osdc->request_mutex);
2087 	return m;
2088 
2089 }
2090 
alloc_msg(struct ceph_connection * con,struct ceph_msg_header * hdr,int * skip)2091 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2092 				  struct ceph_msg_header *hdr,
2093 				  int *skip)
2094 {
2095 	struct ceph_osd *osd = con->private;
2096 	int type = le16_to_cpu(hdr->type);
2097 	int front = le32_to_cpu(hdr->front_len);
2098 
2099 	*skip = 0;
2100 	switch (type) {
2101 	case CEPH_MSG_OSD_MAP:
2102 	case CEPH_MSG_WATCH_NOTIFY:
2103 		return ceph_msg_new(type, front, GFP_NOFS, false);
2104 	case CEPH_MSG_OSD_OPREPLY:
2105 		return get_reply(con, hdr, skip);
2106 	default:
2107 		pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2108 			osd->o_osd);
2109 		*skip = 1;
2110 		return NULL;
2111 	}
2112 }
2113 
2114 /*
2115  * Wrappers to refcount containing ceph_osd struct
2116  */
get_osd_con(struct ceph_connection * con)2117 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2118 {
2119 	struct ceph_osd *osd = con->private;
2120 	if (get_osd(osd))
2121 		return con;
2122 	return NULL;
2123 }
2124 
put_osd_con(struct ceph_connection * con)2125 static void put_osd_con(struct ceph_connection *con)
2126 {
2127 	struct ceph_osd *osd = con->private;
2128 	put_osd(osd);
2129 }
2130 
2131 /*
2132  * authentication
2133  */
2134 /*
2135  * Note: returned pointer is the address of a structure that's
2136  * managed separately.  Caller must *not* attempt to free it.
2137  */
get_authorizer(struct ceph_connection * con,int * proto,int force_new)2138 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
2139 					int *proto, int force_new)
2140 {
2141 	struct ceph_osd *o = con->private;
2142 	struct ceph_osd_client *osdc = o->o_osdc;
2143 	struct ceph_auth_client *ac = osdc->client->monc.auth;
2144 	struct ceph_auth_handshake *auth = &o->o_auth;
2145 
2146 	if (force_new && auth->authorizer) {
2147 		ceph_auth_destroy_authorizer(ac, auth->authorizer);
2148 		auth->authorizer = NULL;
2149 	}
2150 	if (!auth->authorizer) {
2151 		int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2152 						      auth);
2153 		if (ret)
2154 			return ERR_PTR(ret);
2155 	} else {
2156 		int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2157 						     auth);
2158 		if (ret)
2159 			return ERR_PTR(ret);
2160 	}
2161 	*proto = ac->protocol;
2162 
2163 	return auth;
2164 }
2165 
2166 
verify_authorizer_reply(struct ceph_connection * con,int len)2167 static int verify_authorizer_reply(struct ceph_connection *con, int len)
2168 {
2169 	struct ceph_osd *o = con->private;
2170 	struct ceph_osd_client *osdc = o->o_osdc;
2171 	struct ceph_auth_client *ac = osdc->client->monc.auth;
2172 
2173 	return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
2174 }
2175 
invalidate_authorizer(struct ceph_connection * con)2176 static int invalidate_authorizer(struct ceph_connection *con)
2177 {
2178 	struct ceph_osd *o = con->private;
2179 	struct ceph_osd_client *osdc = o->o_osdc;
2180 	struct ceph_auth_client *ac = osdc->client->monc.auth;
2181 
2182 	ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2183 	return ceph_monc_validate_auth(&osdc->client->monc);
2184 }
2185 
2186 static const struct ceph_connection_operations osd_con_ops = {
2187 	.get = get_osd_con,
2188 	.put = put_osd_con,
2189 	.dispatch = dispatch,
2190 	.get_authorizer = get_authorizer,
2191 	.verify_authorizer_reply = verify_authorizer_reply,
2192 	.invalidate_authorizer = invalidate_authorizer,
2193 	.alloc_msg = alloc_msg,
2194 	.fault = osd_reset,
2195 };
2196