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