libceph: compute incoming bytes once
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / ceph / osd_client.c
CommitLineData
3d14c5d2 1#include <linux/ceph/ceph_debug.h>
f24e9980 2
3d14c5d2 3#include <linux/module.h>
f24e9980
SW
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>
68b4476b
YS
10#ifdef CONFIG_BLOCK
11#include <linux/bio.h>
12#endif
f24e9980 13
3d14c5d2
YS
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>
f24e9980 20
c16e7869
SW
21#define OSD_OP_FRONT_LEN 4096
22#define OSD_OPREPLY_FRONT_LEN 512
0d59ab81 23
9e32789f 24static const struct ceph_connection_operations osd_con_ops;
f24e9980 25
f9d25199 26static void __send_queued(struct ceph_osd_client *osdc);
6f6c7006 27static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
a40c4f10
YS
28static void __register_request(struct ceph_osd_client *osdc,
29 struct ceph_osd_request *req);
30static void __unregister_linger_request(struct ceph_osd_client *osdc,
31 struct ceph_osd_request *req);
56e925b6
SW
32static void __send_request(struct ceph_osd_client *osdc,
33 struct ceph_osd_request *req);
f24e9980
SW
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 */
dbe0fc41 60static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
a19dadfb 61 u64 *objnum, u64 *objoff, u64 *objlen)
f24e9980 62{
60e56f13 63 u64 orig_len = *plen;
d63b77f4 64 int r;
f24e9980 65
60e56f13 66 /* object extent? */
75d1c941
AE
67 r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
68 objoff, objlen);
d63b77f4
SW
69 if (r < 0)
70 return r;
75d1c941
AE
71 if (*objlen < orig_len) {
72 *plen = *objlen;
60e56f13
AE
73 dout(" skipping last %llu, final file extent %llu~%llu\n",
74 orig_len - *plen, off, *plen);
75 }
76
75d1c941 77 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
f24e9980 78
3ff5f385 79 return 0;
f24e9980
SW
80}
81
f24e9980
SW
82/*
83 * requests
84 */
415e49a9 85void ceph_osdc_release_request(struct kref *kref)
f24e9980 86{
e0c59487 87 int num_pages;
415e49a9
SW
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);
ace6d3a9 94 if (req->r_reply) {
8921d114 95 ceph_msg_revoke_incoming(req->r_reply);
ab8cb34a 96 ceph_msg_put(req->r_reply);
ace6d3a9 97 }
0fff87ec
AE
98
99 if (req->r_data_in.type == CEPH_OSD_DATA_TYPE_PAGES &&
e0c59487
AE
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 }
0fff87ec 105 if (req->r_data_out.type == CEPH_OSD_DATA_TYPE_PAGES &&
e0c59487
AE
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 }
0fff87ec 111
415e49a9
SW
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);
f24e9980 117}
3d14c5d2 118EXPORT_SYMBOL(ceph_osdc_release_request);
68b4476b 119
3499e8a5 120struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
f24e9980 121 struct ceph_snap_context *snapc,
1b83bef2 122 unsigned int num_ops,
3499e8a5 123 bool use_mempool,
54a54007 124 gfp_t gfp_flags)
f24e9980
SW
125{
126 struct ceph_osd_request *req;
127 struct ceph_msg *msg;
1b83bef2
SW
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;
f24e9980
SW
139
140 if (use_mempool) {
3499e8a5 141 req = mempool_alloc(osdc->req_mempool, gfp_flags);
f24e9980
SW
142 memset(req, 0, sizeof(*req));
143 } else {
3499e8a5 144 req = kzalloc(sizeof(*req), gfp_flags);
f24e9980
SW
145 }
146 if (req == NULL)
a79832f2 147 return NULL;
f24e9980 148
f24e9980
SW
149 req->r_osdc = osdc;
150 req->r_mempool = use_mempool;
68b4476b 151
415e49a9 152 kref_init(&req->r_kref);
f24e9980
SW
153 init_completion(&req->r_completion);
154 init_completion(&req->r_safe_completion);
a978fa20 155 RB_CLEAR_NODE(&req->r_node);
f24e9980 156 INIT_LIST_HEAD(&req->r_unsafe_item);
a40c4f10
YS
157 INIT_LIST_HEAD(&req->r_linger_item);
158 INIT_LIST_HEAD(&req->r_linger_osd);
935b639a 159 INIT_LIST_HEAD(&req->r_req_lru_item);
cd43045c
SW
160 INIT_LIST_HEAD(&req->r_osd_item);
161
c16e7869
SW
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,
b61c2763 167 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
a79832f2 168 if (!msg) {
c16e7869 169 ceph_osdc_put_request(req);
a79832f2 170 return NULL;
c16e7869
SW
171 }
172 req->r_reply = msg;
173
0fff87ec
AE
174 req->r_data_in.type = CEPH_OSD_DATA_TYPE_NONE;
175 req->r_data_out.type = CEPH_OSD_DATA_TYPE_NONE;
d50b409f 176
c16e7869 177 /* create request message; allow space for oid */
f24e9980 178 if (use_mempool)
8f3bc053 179 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
f24e9980 180 else
b61c2763 181 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
a79832f2 182 if (!msg) {
f24e9980 183 ceph_osdc_put_request(req);
a79832f2 184 return NULL;
f24e9980 185 }
68b4476b 186
f24e9980 187 memset(msg->front.iov_base, 0, msg->front.iov_len);
3499e8a5
YS
188
189 req->r_request = msg;
3499e8a5
YS
190
191 return req;
192}
3d14c5d2 193EXPORT_SYMBOL(ceph_osdc_alloc_request);
3499e8a5 194
a8dd0a37 195static bool osd_req_opcode_valid(u16 opcode)
68b4476b 196{
a8dd0a37 197 switch (opcode) {
68b4476b 198 case CEPH_OSD_OP_READ:
a8dd0a37 199 case CEPH_OSD_OP_STAT:
4c46459c
AE
200 case CEPH_OSD_OP_MAPEXT:
201 case CEPH_OSD_OP_MASKTRUNC:
202 case CEPH_OSD_OP_SPARSE_READ:
a9f36c3e 203 case CEPH_OSD_OP_NOTIFY:
a8dd0a37 204 case CEPH_OSD_OP_NOTIFY_ACK:
4c46459c 205 case CEPH_OSD_OP_ASSERT_VER:
a8dd0a37 206 case CEPH_OSD_OP_WRITE:
4c46459c
AE
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:
a8dd0a37 212 case CEPH_OSD_OP_STARTSYNC:
4c46459c
AE
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:
a9f36c3e 219 case CEPH_OSD_OP_ROLLBACK:
a8dd0a37 220 case CEPH_OSD_OP_WATCH:
4c46459c
AE
221 case CEPH_OSD_OP_OMAPGETKEYS:
222 case CEPH_OSD_OP_OMAPGETVALS:
223 case CEPH_OSD_OP_OMAPGETHEADER:
224 case CEPH_OSD_OP_OMAPGETVALSBYKEYS:
4c46459c
AE
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:
a9f36c3e 233 case CEPH_OSD_OP_GETXATTR:
4c46459c 234 case CEPH_OSD_OP_GETXATTRS:
a9f36c3e
AE
235 case CEPH_OSD_OP_CMPXATTR:
236 case CEPH_OSD_OP_SETXATTR:
4c46459c
AE
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:
a8dd0a37 255 case CEPH_OSD_OP_CALL:
4c46459c
AE
256 case CEPH_OSD_OP_PGLS:
257 case CEPH_OSD_OP_PGLS_FILTER:
a8dd0a37
AE
258 return true;
259 default:
260 return false;
261 }
262}
263
33803f33
AE
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 */
269void 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
278void 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}
297EXPORT_SYMBOL(osd_req_op_extent_init);
298
e5975c7c
AE
299void 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}
310EXPORT_SYMBOL(osd_req_op_extent_update);
311
33803f33
AE
312void 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}
344EXPORT_SYMBOL(osd_req_op_cls_init);
345
346void 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}
359EXPORT_SYMBOL(osd_req_op_watch_init);
360
a8dd0a37
AE
361static 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:
4c46459c 416 pr_err("unsupported osd opcode %s\n",
8f63ca2d 417 ceph_osd_op_name(src->op));
4c46459c 418 WARN_ON(1);
a8dd0a37
AE
419
420 return 0;
68b4476b 421 }
a8dd0a37 422 dst->op = cpu_to_le16(src->op);
68b4476b 423 dst->payload_len = cpu_to_le32(src->payload_len);
175face2
AE
424
425 return out_data_len;
68b4476b
YS
426}
427
3499e8a5
YS
428/*
429 * build new request AND message
430 *
431 */
432void ceph_osdc_build_request(struct ceph_osd_request *req,
175face2 433 u64 off, unsigned int num_ops,
68b4476b 434 struct ceph_osd_req_op *src_ops,
4d6b250b 435 struct ceph_snap_context *snapc, u64 snap_id,
af77f26c 436 struct timespec *mtime)
3499e8a5
YS
437{
438 struct ceph_msg *msg = req->r_request;
68b4476b 439 struct ceph_osd_req_op *src_op;
3499e8a5 440 void *p;
1b83bef2 441 size_t msg_size;
3499e8a5 442 int flags = req->r_flags;
f44246e3 443 u64 data_len;
68b4476b 444 int i;
3499e8a5 445
1b83bef2
SW
446 req->r_num_ops = num_ops;
447 req->r_snapid = snap_id;
f24e9980
SW
448 req->r_snapc = ceph_get_snap_context(snapc);
449
1b83bef2
SW
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);
af77f26c 481 memcpy(p, req->r_oid, req->r_oid_len);
1b83bef2 482 dout("oid '%.*s' len %d\n", req->r_oid_len, req->r_oid, req->r_oid_len);
af77f26c 483 p += req->r_oid_len;
f24e9980 484
175face2 485 /* ops--can imply data */
1b83bef2 486 ceph_encode_16(&p, num_ops);
68b4476b 487 src_op = src_ops;
1b83bef2 488 req->r_request_ops = p;
175face2 489 data_len = 0;
1b83bef2 490 for (i = 0; i < num_ops; i++, src_op++) {
175face2 491 data_len += osd_req_encode_op(req, p, src_op);
1b83bef2
SW
492 p += sizeof(struct ceph_osd_op);
493 }
68b4476b 494
1b83bef2
SW
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) {
f24e9980 500 for (i = 0; i < snapc->num_snaps; i++) {
1b83bef2 501 ceph_encode_64(&p, req->r_snapc->snaps[i]);
f24e9980
SW
502 }
503 }
504
1b83bef2
SW
505 req->r_request_attempts = p;
506 p += 4;
507
175face2 508 /* data */
0baa1bd9
AE
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 }
f44246e3 521 req->r_request->hdr.data_len = cpu_to_le32(data_len);
c5c6b19d 522
f24e9980 523 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
6f863e71
SW
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);
1b83bef2 527
acead002 528 dout("build_request msg_size was %d\n", (int)msg_size);
3499e8a5 529}
3d14c5d2 530EXPORT_SYMBOL(ceph_osdc_build_request);
3499e8a5
YS
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 */
543struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
544 struct ceph_file_layout *layout,
545 struct ceph_vino vino,
acead002
AE
546 u64 off, u64 *plen, int num_ops,
547 struct ceph_osd_req_op *ops,
3499e8a5
YS
548 int opcode, int flags,
549 struct ceph_snap_context *snapc,
3499e8a5
YS
550 u32 truncate_seq,
551 u64 truncate_size,
153e5167 552 bool use_mempool)
3499e8a5 553{
68b4476b 554 struct ceph_osd_request *req;
75d1c941
AE
555 u64 objnum = 0;
556 u64 objoff = 0;
557 u64 objlen = 0;
d18d1e28
AE
558 u32 object_size;
559 u64 object_base;
6816282d 560 int r;
68b4476b 561
d18d1e28 562 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE);
68b4476b 563
acead002 564 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
ae7ca4a3 565 GFP_NOFS);
4ad12621 566 if (!req)
6816282d 567 return ERR_PTR(-ENOMEM);
d178a9e7 568 req->r_flags = flags;
3499e8a5
YS
569
570 /* calculate max write size */
a19dadfb 571 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
3ff5f385
AE
572 if (r < 0) {
573 ceph_osdc_put_request(req);
6816282d 574 return ERR_PTR(r);
3ff5f385 575 }
a19dadfb 576
d18d1e28
AE
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;
a19dadfb 585 }
d18d1e28 586
b0270324
AE
587 osd_req_op_extent_init(&ops[0], opcode, objoff, objlen,
588 truncate_size, truncate_seq);
acead002
AE
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)
b0270324 595 osd_req_op_init(&ops[1], CEPH_OSD_OP_STARTSYNC);
d18d1e28 596
3499e8a5
YS
597 req->r_file_layout = *layout; /* keep a copy */
598
75d1c941
AE
599 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx",
600 vino.ino, objnum);
dbe0fc41
AE
601 req->r_oid_len = strlen(req->r_oid);
602
f24e9980
SW
603 return req;
604}
3d14c5d2 605EXPORT_SYMBOL(ceph_osdc_new_request);
f24e9980
SW
606
607/*
608 * We keep osd requests in an rbtree, sorted by ->r_tid.
609 */
610static 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
632static 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
650static 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
6f6c7006
SW
672/*
673 * Resubmit requests pending on the given osd.
674 */
675static void __kick_osd_requests(struct ceph_osd_client *osdc,
676 struct ceph_osd *osd)
677{
a40c4f10 678 struct ceph_osd_request *req, *nreq;
e02493c0 679 LIST_HEAD(resend);
6f6c7006
SW
680 int err;
681
682 dout("__kick_osd_requests osd%d\n", osd->o_osd);
683 err = __reset_osd(osdc, osd);
685a7555 684 if (err)
6f6c7006 685 return;
e02493c0
AE
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 */
6f6c7006 703 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
e02493c0
AE
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,
6f6c7006 708 osd->o_osd);
a40c4f10
YS
709 if (!req->r_linger)
710 req->r_flags |= CEPH_OSD_FLAG_RETRY;
711 }
e02493c0 712 list_splice(&resend, &osdc->req_unsent);
a40c4f10 713
e02493c0
AE
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 */
a40c4f10
YS
719 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
720 r_linger_osd) {
77f38e0e
SW
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));
a40c4f10 726 __register_request(osdc, req);
e02493c0 727 list_add_tail(&req->r_req_lru_item, &osdc->req_unsent);
ad885927 728 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
77f38e0e 729 __unregister_linger_request(osdc, req);
a40c4f10
YS
730 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
731 osd->o_osd);
6f6c7006
SW
732 }
733}
734
f24e9980 735/*
81b024e7 736 * If the osd connection drops, we need to resubmit all requests.
f24e9980
SW
737 */
738static 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;
f24e9980 747 down_read(&osdc->map_sem);
83aff95e
SW
748 mutex_lock(&osdc->request_mutex);
749 __kick_osd_requests(osdc, osd);
f9d25199 750 __send_queued(osdc);
83aff95e 751 mutex_unlock(&osdc->request_mutex);
f24e9980
SW
752 up_read(&osdc->map_sem);
753}
754
755/*
756 * Track open sessions with osds.
757 */
e10006f8 758static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
f24e9980
SW
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;
e10006f8 768 osd->o_osd = onum;
f407731d 769 RB_CLEAR_NODE(&osd->o_node);
f24e9980 770 INIT_LIST_HEAD(&osd->o_requests);
a40c4f10 771 INIT_LIST_HEAD(&osd->o_linger_requests);
f5a2041b 772 INIT_LIST_HEAD(&osd->o_osd_lru);
f24e9980
SW
773 osd->o_incarnation = 1;
774
b7a9e5dd 775 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
4e7a5dcd 776
422d2cb8 777 INIT_LIST_HEAD(&osd->o_keepalive_item);
f24e9980
SW
778 return osd;
779}
780
781static 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
793static 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);
a255651d 797 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
79494d1b
SW
798 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
799
27859f97 800 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
f24e9980 801 kfree(osd);
79494d1b 802 }
f24e9980
SW
803}
804
805/*
806 * remove an osd from our map
807 */
f5a2041b 808static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
f24e9980 809{
f5a2041b 810 dout("__remove_osd %p\n", osd);
f24e9980
SW
811 BUG_ON(!list_empty(&osd->o_requests));
812 rb_erase(&osd->o_node, &osdc->osds);
f5a2041b 813 list_del_init(&osd->o_osd_lru);
f24e9980
SW
814 ceph_con_close(&osd->o_con);
815 put_osd(osd);
816}
817
aca420bc
SW
818static void remove_all_osds(struct ceph_osd_client *osdc)
819{
048a9d2d 820 dout("%s %p\n", __func__, osdc);
aca420bc
SW
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
f5a2041b
YS
830static 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);
3d14c5d2 836 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
f5a2041b
YS
837}
838
839static 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
aca420bc 846static void remove_old_osds(struct ceph_osd_client *osdc)
f5a2041b
YS
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) {
aca420bc 853 if (time_before(jiffies, osd->lru_ttl))
f5a2041b
YS
854 break;
855 __remove_osd(osdc, osd);
856 }
857 mutex_unlock(&osdc->request_mutex);
858}
859
f24e9980
SW
860/*
861 * reset osd connect
862 */
f5a2041b 863static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
f24e9980 864{
c3acb181 865 struct ceph_entity_addr *peer_addr;
f24e9980 866
f5a2041b 867 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
a40c4f10
YS
868 if (list_empty(&osd->o_requests) &&
869 list_empty(&osd->o_linger_requests)) {
f5a2041b 870 __remove_osd(osdc, osd);
c3acb181
AE
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
87b315a5
SW
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;
c3acb181
AE
885
886 return -EAGAIN;
f24e9980 887 }
c3acb181
AE
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;
f24e9980
SW
894}
895
896static 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
aca420bc 902 dout("__insert_osd %p osd%d\n", new, new->o_osd);
f24e9980
SW
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
918static 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
422d2cb8
YS
935static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
936{
937 schedule_delayed_work(&osdc->timeout_work,
3d14c5d2 938 osdc->client->options->osd_keepalive_timeout * HZ);
422d2cb8
YS
939}
940
941static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
942{
943 cancel_delayed_work(&osdc->timeout_work);
944}
f24e9980
SW
945
946/*
947 * Register request, assign tid. If this is the first request, set up
948 * the timeout event.
949 */
a40c4f10
YS
950static void __register_request(struct ceph_osd_client *osdc,
951 struct ceph_osd_request *req)
f24e9980 952{
f24e9980 953 req->r_tid = ++osdc->last_tid;
6df058c0 954 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
77f38e0e 955 dout("__register_request %p tid %lld\n", req, req->r_tid);
f24e9980
SW
956 __insert_request(osdc, req);
957 ceph_osdc_get_request(req);
958 osdc->num_requests++;
f24e9980 959 if (osdc->num_requests == 1) {
422d2cb8
YS
960 dout(" first request, scheduling timeout\n");
961 __schedule_osd_timeout(osdc);
f24e9980 962 }
a40c4f10
YS
963}
964
f24e9980
SW
965/*
966 * called under osdc->request_mutex
967 */
968static void __unregister_request(struct ceph_osd_client *osdc,
969 struct ceph_osd_request *req)
970{
35f9f8a0
SW
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
f24e9980
SW
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
0ba6478d
SW
981 if (req->r_osd) {
982 /* make sure the original request isn't in flight. */
6740a845 983 ceph_msg_revoke(req->r_request);
0ba6478d
SW
984
985 list_del_init(&req->r_osd_item);
a40c4f10
YS
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);
f5a2041b 989 __move_osd_to_lru(osdc, req->r_osd);
a40c4f10 990 }
fbdb9190 991 if (list_empty(&req->r_linger_item))
a40c4f10 992 req->r_osd = NULL;
0ba6478d 993 }
f24e9980 994
7d5f2481 995 list_del_init(&req->r_req_lru_item);
f24e9980
SW
996 ceph_osdc_put_request(req);
997
422d2cb8
YS
998 if (osdc->num_requests == 0) {
999 dout(" no requests, canceling timeout\n");
1000 __cancel_osd_timeout(osdc);
f24e9980
SW
1001 }
1002}
1003
1004/*
1005 * Cancel a previously queued request message
1006 */
1007static void __cancel_request(struct ceph_osd_request *req)
1008{
6bc18876 1009 if (req->r_sent && req->r_osd) {
6740a845 1010 ceph_msg_revoke(req->r_request);
f24e9980
SW
1011 req->r_sent = 0;
1012 }
1013}
1014
a40c4f10
YS
1015static 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);
6194ea89
SW
1020 if (req->r_osd)
1021 list_add_tail(&req->r_linger_osd,
1022 &req->r_osd->o_linger_requests);
a40c4f10
YS
1023}
1024
1025static void __unregister_linger_request(struct ceph_osd_client *osdc,
1026 struct ceph_osd_request *req)
1027{
1028 dout("__unregister_linger_request %p\n", req);
61c74035 1029 list_del_init(&req->r_linger_item);
a40c4f10 1030 if (req->r_osd) {
a40c4f10
YS
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 }
fbdb9190
SW
1038 if (list_empty(&req->r_osd_item))
1039 req->r_osd = NULL;
a40c4f10
YS
1040 }
1041}
1042
1043void 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}
1053EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
1054
1055void 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}
1068EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1069
f24e9980
SW
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
25985edc 1073 * no up osd, set r_osd to NULL. Move the request to the appropriate list
6f6c7006 1074 * (unsent, homeless) or leave on in-flight lru.
f24e9980
SW
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 */
6f6c7006 1080static int __map_request(struct ceph_osd_client *osdc,
38d6453c 1081 struct ceph_osd_request *req, int force_resend)
f24e9980 1082{
5b191d99 1083 struct ceph_pg pgid;
d85b7056
SW
1084 int acting[CEPH_PG_MAX_SIZE];
1085 int o = -1, num = 0;
f24e9980 1086 int err;
f24e9980 1087
6f6c7006 1088 dout("map_request %p tid %lld\n", req, req->r_tid);
41766f87
AE
1089 err = ceph_calc_ceph_pg(&pgid, req->r_oid, osdc->osdmap,
1090 ceph_file_layout_pg_pool(req->r_file_layout));
6f6c7006
SW
1091 if (err) {
1092 list_move(&req->r_req_lru_item, &osdc->req_notarget);
f24e9980 1093 return err;
6f6c7006 1094 }
7740a42f
SW
1095 req->r_pgid = pgid;
1096
d85b7056
SW
1097 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
1098 if (err > 0) {
1099 o = acting[0];
1100 num = err;
1101 }
f24e9980 1102
38d6453c
SW
1103 if ((!force_resend &&
1104 req->r_osd && req->r_osd->o_osd == o &&
d85b7056
SW
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) ||
f24e9980
SW
1108 (req->r_osd == NULL && o == -1))
1109 return 0; /* no change */
1110
5b191d99
SW
1111 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1112 req->r_tid, pgid.pool, pgid.seed, o,
f24e9980
SW
1113 req->r_osd ? req->r_osd->o_osd : -1);
1114
d85b7056
SW
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
f24e9980
SW
1119 if (req->r_osd) {
1120 __cancel_request(req);
1121 list_del_init(&req->r_osd_item);
f24e9980
SW
1122 req->r_osd = NULL;
1123 }
1124
1125 req->r_osd = __lookup_osd(osdc, o);
1126 if (!req->r_osd && o >= 0) {
c99eb1c7 1127 err = -ENOMEM;
e10006f8 1128 req->r_osd = create_osd(osdc, o);
6f6c7006
SW
1129 if (!req->r_osd) {
1130 list_move(&req->r_req_lru_item, &osdc->req_notarget);
c99eb1c7 1131 goto out;
6f6c7006 1132 }
f24e9980 1133
6f6c7006 1134 dout("map_request osd %p is osd%d\n", req->r_osd, o);
f24e9980
SW
1135 __insert_osd(osdc, req->r_osd);
1136
b7a9e5dd
SW
1137 ceph_con_open(&req->r_osd->o_con,
1138 CEPH_ENTITY_TYPE_OSD, o,
1139 &osdc->osdmap->osd_addr[o]);
f24e9980
SW
1140 }
1141
f5a2041b
YS
1142 if (req->r_osd) {
1143 __remove_osd_from_lru(req->r_osd);
ad885927
AE
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);
6f6c7006 1146 } else {
ad885927 1147 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
f5a2041b 1148 }
d85b7056 1149 err = 1; /* osd or pg changed */
f24e9980
SW
1150
1151out:
f24e9980
SW
1152 return err;
1153}
1154
1155/*
1156 * caller should hold map_sem (for read) and request_mutex
1157 */
56e925b6
SW
1158static void __send_request(struct ceph_osd_client *osdc,
1159 struct ceph_osd_request *req)
f24e9980 1160{
1b83bef2 1161 void *p;
f24e9980 1162
1b83bef2
SW
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));
2169aea6 1177
3dd72fc0 1178 req->r_stamp = jiffies;
07a27e22 1179 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
f24e9980
SW
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;
f24e9980
SW
1184}
1185
6f6c7006
SW
1186/*
1187 * Send any requests in the queue (req_unsent).
1188 */
f9d25199 1189static void __send_queued(struct ceph_osd_client *osdc)
6f6c7006
SW
1190{
1191 struct ceph_osd_request *req, *tmp;
1192
f9d25199
AE
1193 dout("__send_queued\n");
1194 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
6f6c7006 1195 __send_request(osdc, req);
6f6c7006
SW
1196}
1197
f24e9980
SW
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 */
1207static 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);
83aff95e 1211 struct ceph_osd_request *req;
f24e9980 1212 struct ceph_osd *osd;
422d2cb8 1213 unsigned long keepalive =
3d14c5d2 1214 osdc->client->options->osd_keepalive_timeout * HZ;
422d2cb8 1215 struct list_head slow_osds;
f24e9980
SW
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);
f24e9980 1222
422d2cb8
YS
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) {
3dd72fc0 1230 if (time_before(jiffies, req->r_stamp + keepalive))
422d2cb8
YS
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",
f24e9980 1236 req->r_tid, osd->o_osd);
422d2cb8
YS
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);
f24e9980
SW
1243 ceph_con_keepalive(&osd->o_con);
1244 }
1245
422d2cb8 1246 __schedule_osd_timeout(osdc);
f9d25199 1247 __send_queued(osdc);
f24e9980 1248 mutex_unlock(&osdc->request_mutex);
f24e9980
SW
1249 up_read(&osdc->map_sem);
1250}
1251
f5a2041b
YS
1252static 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 =
3d14c5d2 1258 osdc->client->options->osd_idle_ttl * HZ >> 2;
f5a2041b
YS
1259
1260 dout("osds timeout\n");
1261 down_read(&osdc->map_sem);
aca420bc 1262 remove_old_osds(osdc);
f5a2041b
YS
1263 up_read(&osdc->map_sem);
1264
1265 schedule_delayed_work(&osdc->osds_timeout_work,
1266 round_jiffies_relative(delay));
1267}
1268
25845472
SW
1269static 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
f24e9980
SW
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 */
350b1c32
SW
1280static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1281 struct ceph_connection *con)
f24e9980 1282{
1b83bef2 1283 void *p, *end;
f24e9980
SW
1284 struct ceph_osd_request *req;
1285 u64 tid;
1b83bef2
SW
1286 int object_len;
1287 int numops, payload_len, flags;
0ceed5db 1288 s32 result;
1b83bef2
SW
1289 s32 retry_attempt;
1290 struct ceph_pg pg;
1291 int err;
1292 u32 reassert_epoch;
1293 u64 reassert_version;
1294 u32 osdmap_epoch;
0d5af164 1295 int already_completed;
9fc6e064 1296 u32 bytes;
1b83bef2 1297 int i;
f24e9980 1298
6df058c0 1299 tid = le64_to_cpu(msg->hdr.tid);
1b83bef2
SW
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
ef4859d6 1310 err = ceph_decode_pgid(&p, end, &pg);
1b83bef2 1311 if (err)
f24e9980 1312 goto bad;
1b83bef2
SW
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
f24e9980
SW
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);
8058fd45 1326 goto bad_mutex;
f24e9980
SW
1327 }
1328 ceph_osdc_get_request(req);
1b83bef2
SW
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 }
9fc6e064
AE
1351 bytes = le32_to_cpu(msg->hdr.data_len);
1352 if (payload_len != bytes) {
1b83bef2 1353 pr_warning("sum of op payload lens %d != data_len %d",
9fc6e064 1354 payload_len, bytes);
1b83bef2
SW
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);
f24e9980 1362
f24e9980 1363 if (!req->r_got_reply) {
f24e9980 1364
1b83bef2 1365 req->r_result = result;
f24e9980
SW
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, */
1b83bef2
SW
1372 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1373 req->r_reassert_version.version = cpu_to_le64(reassert_version);
f24e9980
SW
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);
34b43a56 1378 mutex_unlock(&osdc->request_mutex);
f24e9980
SW
1379 goto done;
1380 }
1381
1382 dout("handle_reply tid %llu flags %d\n", tid, flags);
1383
a40c4f10
YS
1384 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1385 __register_linger_request(osdc, req);
1386
f24e9980 1387 /* either this is a read, or we got the safe response */
0ceed5db
SW
1388 if (result < 0 ||
1389 (flags & CEPH_OSD_FLAG_ONDISK) ||
f24e9980
SW
1390 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1391 __unregister_request(osdc, req);
1392
0d5af164
AE
1393 already_completed = req->r_completed;
1394 req->r_completed = 1;
f24e9980 1395 mutex_unlock(&osdc->request_mutex);
0d5af164
AE
1396 if (already_completed)
1397 goto done;
f24e9980
SW
1398
1399 if (req->r_callback)
1400 req->r_callback(req, msg);
1401 else
03066f23 1402 complete_all(&req->r_completion);
f24e9980 1403
25845472
SW
1404 if (flags & CEPH_OSD_FLAG_ONDISK)
1405 complete_request(req);
f24e9980
SW
1406
1407done:
a40c4f10 1408 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
f24e9980
SW
1409 ceph_osdc_put_request(req);
1410 return;
1411
1b83bef2
SW
1412bad_put:
1413 ceph_osdc_put_request(req);
8058fd45
AE
1414bad_mutex:
1415 mutex_unlock(&osdc->request_mutex);
f24e9980 1416bad:
1b83bef2
SW
1417 pr_err("corrupt osd_op_reply got %d %d\n",
1418 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
9ec7cab1 1419 ceph_msg_dump(msg);
f24e9980
SW
1420}
1421
6f6c7006 1422static void reset_changed_osds(struct ceph_osd_client *osdc)
f24e9980 1423{
f24e9980 1424 struct rb_node *p, *n;
f24e9980 1425
6f6c7006
SW
1426 for (p = rb_first(&osdc->osds); p; p = n) {
1427 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
f24e9980 1428
6f6c7006
SW
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);
f24e9980 1436 }
422d2cb8
YS
1437}
1438
1439/*
6f6c7006
SW
1440 * Requeue requests whose mapping to an OSD has changed. If requests map to
1441 * no osd, request a new map.
422d2cb8 1442 *
e6d50f67 1443 * Caller should hold map_sem for read.
422d2cb8 1444 */
38d6453c 1445static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
422d2cb8 1446{
a40c4f10 1447 struct ceph_osd_request *req, *nreq;
6f6c7006
SW
1448 struct rb_node *p;
1449 int needmap = 0;
1450 int err;
422d2cb8 1451
38d6453c 1452 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
422d2cb8 1453 mutex_lock(&osdc->request_mutex);
6194ea89 1454 for (p = rb_first(&osdc->requests); p; ) {
6f6c7006 1455 req = rb_entry(p, struct ceph_osd_request, r_node);
6194ea89 1456 p = rb_next(p);
ab60b16d
AE
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
38d6453c 1475 err = __map_request(osdc, req, force_resend);
6f6c7006
SW
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) {
6194ea89
SW
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);
a40c4f10 1486 req->r_flags |= CEPH_OSD_FLAG_RETRY;
6194ea89
SW
1487 }
1488 }
a40c4f10
YS
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
38d6453c 1495 err = __map_request(osdc, req, force_resend);
ab60b16d 1496 dout("__map_request returned %d\n", err);
a40c4f10
YS
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;
6f6c7006 1505 }
a40c4f10
YS
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);
a40c4f10 1509 __register_request(osdc, req);
c89ce05e 1510 __unregister_linger_request(osdc, req);
6f6c7006 1511 }
f24e9980
SW
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 }
e6d50f67 1518 reset_changed_osds(osdc);
422d2cb8 1519}
6f6c7006
SW
1520
1521
f24e9980
SW
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 */
1529void 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));
0743304d
SW
1545 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1546 return;
f24e9980
SW
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);
c89136ea
SW
1555 epoch = ceph_decode_32(&p);
1556 maplen = ceph_decode_32(&p);
f24e9980
SW
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,
15d9882c 1564 &osdc->client->msgr);
f24e9980
SW
1565 if (IS_ERR(newmap)) {
1566 err = PTR_ERR(newmap);
1567 goto bad;
1568 }
30dc6381 1569 BUG_ON(!newmap);
f24e9980
SW
1570 if (newmap != osdc->osdmap) {
1571 ceph_osdmap_destroy(osdc->osdmap);
1572 osdc->osdmap = newmap;
1573 }
38d6453c 1574 kick_requests(osdc, 0);
f24e9980
SW
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);
c89136ea
SW
1590 epoch = ceph_decode_32(&p);
1591 maplen = ceph_decode_32(&p);
f24e9980
SW
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 {
38d6453c
SW
1601 int skipped_map = 0;
1602
f24e9980
SW
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 }
30dc6381 1609 BUG_ON(!newmap);
f24e9980
SW
1610 oldmap = osdc->osdmap;
1611 osdc->osdmap = newmap;
38d6453c
SW
1612 if (oldmap) {
1613 if (oldmap->epoch + 1 < newmap->epoch)
1614 skipped_map = 1;
f24e9980 1615 ceph_osdmap_destroy(oldmap);
38d6453c
SW
1616 }
1617 kick_requests(osdc, skipped_map);
f24e9980
SW
1618 }
1619 p += maplen;
1620 nr_maps--;
1621 }
1622
1623done:
1624 downgrade_write(&osdc->map_sem);
1625 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
cd634fb6
SW
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
f9d25199
AE
1635 mutex_lock(&osdc->request_mutex);
1636 __send_queued(osdc);
1637 mutex_unlock(&osdc->request_mutex);
f24e9980 1638 up_read(&osdc->map_sem);
03066f23 1639 wake_up_all(&osdc->client->auth_wq);
f24e9980
SW
1640 return;
1641
1642bad:
1643 pr_err("osdc handle_map corrupt msg\n");
9ec7cab1 1644 ceph_msg_dump(msg);
f24e9980
SW
1645 up_write(&osdc->map_sem);
1646 return;
1647}
1648
a40c4f10
YS
1649/*
1650 * watch/notify callback event infrastructure
1651 *
1652 * These callbacks are used both for watch and notify operations.
1653 */
1654static 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
1663static void get_event(struct ceph_osd_event *event)
1664{
1665 kref_get(&event->kref);
1666}
1667
1668void ceph_osdc_put_event(struct ceph_osd_event *event)
1669{
1670 kref_put(&event->kref, __release_event);
1671}
1672EXPORT_SYMBOL(ceph_osdc_put_event);
1673
1674static 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
1696static 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
1716static 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
1729int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1730 void (*event_cb)(u64, u64, u8, void *),
3c663bbd 1731 void *data, struct ceph_osd_event **pevent)
a40c4f10
YS
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;
3c663bbd 1741 event->one_shot = 0;
a40c4f10
YS
1742 event->data = data;
1743 event->osdc = osdc;
1744 INIT_LIST_HEAD(&event->osd_node);
3ee5234d 1745 RB_CLEAR_NODE(&event->node);
a40c4f10
YS
1746 kref_init(&event->kref); /* one ref for us */
1747 kref_get(&event->kref); /* one ref for the caller */
a40c4f10
YS
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}
1757EXPORT_SYMBOL(ceph_osdc_create_event);
1758
1759void 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}
1769EXPORT_SYMBOL(ceph_osdc_cancel_event);
1770
1771
1772static 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);
a40c4f10
YS
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 */
3c663bbd
AE
1792static void handle_watch_notify(struct ceph_osd_client *osdc,
1793 struct ceph_msg *msg)
a40c4f10
YS
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) {
3c663bbd 1814 BUG_ON(event->one_shot);
a40c4f10 1815 get_event(event);
a40c4f10
YS
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);
a40c4f10
YS
1822 if (!event_work) {
1823 dout("ERROR: could not allocate event_work\n");
1824 goto done_err;
1825 }
6b0ae409 1826 INIT_WORK(&event_work->work, do_event_work);
a40c4f10
YS
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
1839done_err:
a40c4f10
YS
1840 ceph_osdc_put_event(event);
1841 return;
1842
1843bad:
1844 pr_err("osdc handle_watch_notify corrupt msg\n");
1845 return;
1846}
1847
70636773
AE
1848static void ceph_osdc_msg_data_set(struct ceph_msg *msg,
1849 struct ceph_osd_data *osd_data)
f24e9980 1850{
0fff87ec 1851 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
f1baeb2b 1852 BUG_ON(osd_data->length > (u64) SIZE_MAX);
ebf18f47 1853 if (osd_data->length)
70636773
AE
1854 ceph_msg_data_set_pages(msg, osd_data->pages,
1855 osd_data->length, osd_data->alignment);
9a5e6d09
AE
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);
68b4476b 1859#ifdef CONFIG_BLOCK
0fff87ec 1860 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
a1930804 1861 ceph_msg_data_set_bio(msg, osd_data->bio, osd_data->bio_length);
68b4476b 1862#endif
2ac2b7a6 1863 } else {
0fff87ec 1864 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
2ac2b7a6 1865 }
70636773
AE
1866}
1867
1868/*
1869 * Register request, send initial attempt.
1870 */
1871int 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);
f24e9980 1881
f24e9980
SW
1882 down_read(&osdc->map_sem);
1883 mutex_lock(&osdc->request_mutex);
dc4b870c 1884 __register_request(osdc, req);
92451b49
AE
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;
f24e9980 1892 }
92451b49 1893 goto out_unlock;
f24e9980 1894 }
92451b49
AE
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 {
7e2766a1 1899 __send_queued(osdc);
92451b49
AE
1900 }
1901 rc = 0;
234af26f 1902out_unlock:
f24e9980
SW
1903 mutex_unlock(&osdc->request_mutex);
1904 up_read(&osdc->map_sem);
1905 return rc;
1906}
3d14c5d2 1907EXPORT_SYMBOL(ceph_osdc_start_request);
f24e9980
SW
1908
1909/*
1910 * wait for a request to complete
1911 */
1912int 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);
529cfcc4 1921 __unregister_request(osdc, req);
f24e9980 1922 mutex_unlock(&osdc->request_mutex);
25845472 1923 complete_request(req);
529cfcc4 1924 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
f24e9980
SW
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}
3d14c5d2 1931EXPORT_SYMBOL(ceph_osdc_wait_request);
f24e9980
SW
1932
1933/*
1934 * sync - wait for all in-flight requests to flush. avoid starvation.
1935 */
1936void 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}
3d14c5d2 1965EXPORT_SYMBOL(ceph_osdc_sync);
f24e9980
SW
1966
1967/*
1968 * init, shutdown
1969 */
1970int 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);
f24e9980
SW
1981 osdc->last_tid = 0;
1982 osdc->osds = RB_ROOT;
f5a2041b 1983 INIT_LIST_HEAD(&osdc->osd_lru);
f24e9980 1984 osdc->requests = RB_ROOT;
422d2cb8 1985 INIT_LIST_HEAD(&osdc->req_lru);
6f6c7006
SW
1986 INIT_LIST_HEAD(&osdc->req_unsent);
1987 INIT_LIST_HEAD(&osdc->req_notarget);
a40c4f10 1988 INIT_LIST_HEAD(&osdc->req_linger);
f24e9980
SW
1989 osdc->num_requests = 0;
1990 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
f5a2041b 1991 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
a40c4f10
YS
1992 spin_lock_init(&osdc->event_lock);
1993 osdc->event_tree = RB_ROOT;
1994 osdc->event_count = 0;
f5a2041b
YS
1995
1996 schedule_delayed_work(&osdc->osds_timeout_work,
3d14c5d2 1997 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
f24e9980 1998
5f44f142 1999 err = -ENOMEM;
f24e9980
SW
2000 osdc->req_mempool = mempool_create_kmalloc_pool(10,
2001 sizeof(struct ceph_osd_request));
2002 if (!osdc->req_mempool)
5f44f142 2003 goto out;
f24e9980 2004
d50b409f
SW
2005 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2006 OSD_OP_FRONT_LEN, 10, true,
4f48280e 2007 "osd_op");
f24e9980 2008 if (err < 0)
5f44f142 2009 goto out_mempool;
d50b409f 2010 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
4f48280e
SW
2011 OSD_OPREPLY_FRONT_LEN, 10, true,
2012 "osd_op_reply");
c16e7869
SW
2013 if (err < 0)
2014 goto out_msgpool;
a40c4f10
YS
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 }
f24e9980 2022 return 0;
5f44f142 2023
c16e7869
SW
2024out_msgpool:
2025 ceph_msgpool_destroy(&osdc->msgpool_op);
5f44f142
SW
2026out_mempool:
2027 mempool_destroy(osdc->req_mempool);
2028out:
2029 return err;
f24e9980
SW
2030}
2031
2032void ceph_osdc_stop(struct ceph_osd_client *osdc)
2033{
a40c4f10
YS
2034 flush_workqueue(osdc->notify_wq);
2035 destroy_workqueue(osdc->notify_wq);
f24e9980 2036 cancel_delayed_work_sync(&osdc->timeout_work);
f5a2041b 2037 cancel_delayed_work_sync(&osdc->osds_timeout_work);
f24e9980
SW
2038 if (osdc->osdmap) {
2039 ceph_osdmap_destroy(osdc->osdmap);
2040 osdc->osdmap = NULL;
2041 }
aca420bc 2042 remove_all_osds(osdc);
f24e9980
SW
2043 mempool_destroy(osdc->req_mempool);
2044 ceph_msgpool_destroy(&osdc->msgpool_op);
c16e7869 2045 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
f24e9980
SW
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 */
2052int 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,
b7495fc2 2056 struct page **pages, int num_pages, int page_align)
f24e9980
SW
2057{
2058 struct ceph_osd_request *req;
0fff87ec 2059 struct ceph_osd_data *osd_data;
acead002 2060 struct ceph_osd_req_op op;
f24e9980
SW
2061 int rc = 0;
2062
2063 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2064 vino.snap, off, *plen);
acead002 2065 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 1, &op,
f24e9980 2066 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
acead002 2067 NULL, truncate_seq, truncate_size,
153e5167 2068 false);
6816282d
SW
2069 if (IS_ERR(req))
2070 return PTR_ERR(req);
f24e9980
SW
2071
2072 /* it may be a short read due to an object boundary */
0fff87ec
AE
2073
2074 osd_data = &req->r_data_in;
2075 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
2076 osd_data->pages = pages;
e0c59487 2077 osd_data->length = *plen;
0fff87ec 2078 osd_data->alignment = page_align;
f24e9980 2079
e0c59487
AE
2080 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
2081 off, *plen, osd_data->length, page_align);
f24e9980 2082
02ee07d3
AE
2083 ceph_osdc_build_request(req, off, 1, &op, NULL, vino.snap, NULL);
2084
f24e9980
SW
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}
3d14c5d2 2093EXPORT_SYMBOL(ceph_osdc_readpages);
f24e9980
SW
2094
2095/*
2096 * do a synchronous write on N pages
2097 */
2098int 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,
24808826 2104 struct page **pages, int num_pages)
f24e9980
SW
2105{
2106 struct ceph_osd_request *req;
0fff87ec 2107 struct ceph_osd_data *osd_data;
acead002 2108 struct ceph_osd_req_op op;
f24e9980 2109 int rc = 0;
b7495fc2 2110 int page_align = off & ~PAGE_MASK;
f24e9980 2111
acead002
AE
2112 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
2113 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 1, &op,
f24e9980 2114 CEPH_OSD_OP_WRITE,
24808826 2115 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
acead002 2116 snapc, truncate_seq, truncate_size,
153e5167 2117 true);
6816282d
SW
2118 if (IS_ERR(req))
2119 return PTR_ERR(req);
f24e9980
SW
2120
2121 /* it may be a short write due to an object boundary */
0fff87ec
AE
2122 osd_data = &req->r_data_out;
2123 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
2124 osd_data->pages = pages;
e0c59487 2125 osd_data->length = len;
0fff87ec 2126 osd_data->alignment = page_align;
e0c59487 2127 dout("writepages %llu~%llu (%llu bytes)\n", off, len, osd_data->length);
f24e9980 2128
02ee07d3
AE
2129 ceph_osdc_build_request(req, off, 1, &op, snapc, CEPH_NOSNAP, mtime);
2130
87f979d3 2131 rc = ceph_osdc_start_request(osdc, req, true);
f24e9980
SW
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}
3d14c5d2 2141EXPORT_SYMBOL(ceph_osdc_writepages);
f24e9980
SW
2142
2143/*
2144 * handle incoming message
2145 */
2146static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2147{
2148 struct ceph_osd *osd = con->private;
32c895e7 2149 struct ceph_osd_client *osdc;
f24e9980
SW
2150 int type = le16_to_cpu(msg->hdr.type);
2151
2152 if (!osd)
4a32f93d 2153 goto out;
32c895e7 2154 osdc = osd->o_osdc;
f24e9980
SW
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:
350b1c32 2161 handle_reply(osdc, msg, con);
f24e9980 2162 break;
a40c4f10
YS
2163 case CEPH_MSG_WATCH_NOTIFY:
2164 handle_watch_notify(osdc, msg);
2165 break;
f24e9980
SW
2166
2167 default:
2168 pr_err("received unknown message type %d %s\n", type,
2169 ceph_msg_type_name(type));
2170 }
4a32f93d 2171out:
f24e9980
SW
2172 ceph_msg_put(msg);
2173}
2174
5b3a4db3 2175/*
21b667f6
SW
2176 * lookup and return message for incoming reply. set up reply message
2177 * pages.
5b3a4db3
SW
2178 */
2179static struct ceph_msg *get_reply(struct ceph_connection *con,
2450418c
YS
2180 struct ceph_msg_header *hdr,
2181 int *skip)
f24e9980
SW
2182{
2183 struct ceph_osd *osd = con->private;
2184 struct ceph_osd_client *osdc = osd->o_osdc;
2450418c 2185 struct ceph_msg *m;
0547a9b3 2186 struct ceph_osd_request *req;
5b3a4db3
SW
2187 int front = le32_to_cpu(hdr->front_len);
2188 int data_len = le32_to_cpu(hdr->data_len);
0547a9b3 2189 u64 tid;
f24e9980 2190
0547a9b3
YS
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;
756a16a5
SW
2197 dout("get_reply unknown tid %llu from osd%d\n", tid,
2198 osd->o_osd);
0547a9b3
YS
2199 goto out;
2200 }
c16e7869 2201
ace6d3a9 2202 if (req->r_reply->con)
8921d114 2203 dout("%s revoking msg %p from old con %p\n", __func__,
ace6d3a9
AE
2204 req->r_reply, req->r_reply->con);
2205 ceph_msg_revoke_incoming(req->r_reply);
0547a9b3 2206
c16e7869
SW
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);
b61c2763 2210 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
a79832f2 2211 if (!m)
c16e7869
SW
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
0547a9b3 2218 if (data_len > 0) {
0fff87ec
AE
2219 struct ceph_osd_data *osd_data = &req->r_data_in;
2220
2221 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
0fff87ec 2222 if (osd_data->pages &&
e0c59487 2223 unlikely(osd_data->length < data_len)) {
2ac2b7a6 2224
e0c59487
AE
2225 pr_warning("tid %lld reply has %d bytes "
2226 "we had only %llu bytes ready\n",
2227 tid, data_len, osd_data->length);
2ac2b7a6
AE
2228 *skip = 1;
2229 ceph_msg_put(m);
2230 m = NULL;
2231 goto out;
2232 }
2ac2b7a6 2233 }
0547a9b3 2234 }
5b3a4db3 2235 *skip = 0;
c16e7869 2236 dout("get_reply tid %lld %p\n", tid, m);
0547a9b3
YS
2237
2238out:
2239 mutex_unlock(&osdc->request_mutex);
2450418c 2240 return m;
5b3a4db3
SW
2241
2242}
2243
2244static 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
1c20f2d2 2252 *skip = 0;
5b3a4db3
SW
2253 switch (type) {
2254 case CEPH_MSG_OSD_MAP:
a40c4f10 2255 case CEPH_MSG_WATCH_NOTIFY:
b61c2763 2256 return ceph_msg_new(type, front, GFP_NOFS, false);
5b3a4db3
SW
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 }
f24e9980
SW
2265}
2266
2267/*
2268 * Wrappers to refcount containing ceph_osd struct
2269 */
2270static 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
2278static void put_osd_con(struct ceph_connection *con)
2279{
2280 struct ceph_osd *osd = con->private;
2281 put_osd(osd);
2282}
2283
4e7a5dcd
SW
2284/*
2285 * authentication
2286 */
a3530df3
AE
2287/*
2288 * Note: returned pointer is the address of a structure that's
2289 * managed separately. Caller must *not* attempt to free it.
2290 */
2291static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
8f43fb53 2292 int *proto, int force_new)
4e7a5dcd
SW
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;
74f1869f 2297 struct ceph_auth_handshake *auth = &o->o_auth;
4e7a5dcd 2298
74f1869f 2299 if (force_new && auth->authorizer) {
27859f97 2300 ceph_auth_destroy_authorizer(ac, auth->authorizer);
74f1869f
AE
2301 auth->authorizer = NULL;
2302 }
27859f97
SW
2303 if (!auth->authorizer) {
2304 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2305 auth);
4e7a5dcd 2306 if (ret)
a3530df3 2307 return ERR_PTR(ret);
27859f97
SW
2308 } else {
2309 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
0bed9b5c
SW
2310 auth);
2311 if (ret)
2312 return ERR_PTR(ret);
4e7a5dcd 2313 }
4e7a5dcd 2314 *proto = ac->protocol;
74f1869f 2315
a3530df3 2316 return auth;
4e7a5dcd
SW
2317}
2318
2319
2320static 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
27859f97 2326 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
4e7a5dcd
SW
2327}
2328
9bd2e6f8
SW
2329static 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
27859f97 2335 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
9bd2e6f8
SW
2336 return ceph_monc_validate_auth(&osdc->client->monc);
2337}
4e7a5dcd 2338
9e32789f 2339static const struct ceph_connection_operations osd_con_ops = {
f24e9980
SW
2340 .get = get_osd_con,
2341 .put = put_osd_con,
2342 .dispatch = dispatch,
4e7a5dcd
SW
2343 .get_authorizer = get_authorizer,
2344 .verify_authorizer_reply = verify_authorizer_reply,
9bd2e6f8 2345 .invalidate_authorizer = invalidate_authorizer,
f24e9980 2346 .alloc_msg = alloc_msg,
81b024e7 2347 .fault = osd_reset,
f24e9980 2348};