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