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