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