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