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