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