ceph-rbd: osdc support for osd call and rollback operations
[linux-2.6-block.git] / fs / ceph / osd_client.c
CommitLineData
f24e9980
SW
1#include "ceph_debug.h"
2
3#include <linux/err.h>
4#include <linux/highmem.h>
5#include <linux/mm.h>
6#include <linux/pagemap.h>
7#include <linux/slab.h>
8#include <linux/uaccess.h>
68b4476b
YS
9#ifdef CONFIG_BLOCK
10#include <linux/bio.h>
11#endif
f24e9980
SW
12
13#include "super.h"
14#include "osd_client.h"
15#include "messenger.h"
16#include "decode.h"
4e7a5dcd 17#include "auth.h"
68b4476b 18#include "pagelist.h"
f24e9980 19
c16e7869
SW
20#define OSD_OP_FRONT_LEN 4096
21#define OSD_OPREPLY_FRONT_LEN 512
0d59ab81 22
9e32789f 23static const struct ceph_connection_operations osd_con_ops;
422d2cb8
YS
24static int __kick_requests(struct ceph_osd_client *osdc,
25 struct ceph_osd *kickosd);
f24e9980
SW
26
27static void kick_requests(struct ceph_osd_client *osdc, struct ceph_osd *osd);
28
68b4476b
YS
29static int op_needs_trail(int op)
30{
31 switch (op) {
32 case CEPH_OSD_OP_GETXATTR:
33 case CEPH_OSD_OP_SETXATTR:
34 case CEPH_OSD_OP_CMPXATTR:
35 case CEPH_OSD_OP_CALL:
36 return 1;
37 default:
38 return 0;
39 }
40}
41
42static int op_has_extent(int op)
43{
44 return (op == CEPH_OSD_OP_READ ||
45 op == CEPH_OSD_OP_WRITE);
46}
47
3499e8a5
YS
48void ceph_calc_raw_layout(struct ceph_osd_client *osdc,
49 struct ceph_file_layout *layout,
50 u64 snapid,
68b4476b
YS
51 u64 off, u64 *plen, u64 *bno,
52 struct ceph_osd_request *req,
53 struct ceph_osd_req_op *op)
3499e8a5
YS
54{
55 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
68b4476b 56 u64 orig_len = *plen;
3499e8a5
YS
57 u64 objoff, objlen; /* extent in object */
58
59 reqhead->snapid = cpu_to_le64(snapid);
60
61 /* object extent? */
68b4476b 62 ceph_calc_file_object_mapping(layout, off, plen, bno,
3499e8a5 63 &objoff, &objlen);
68b4476b 64 if (*plen < orig_len)
3499e8a5 65 dout(" skipping last %llu, final file extent %llu~%llu\n",
68b4476b 66 orig_len - *plen, off, *plen);
3499e8a5 67
68b4476b
YS
68 if (op_has_extent(op->op)) {
69 op->extent.offset = objoff;
70 op->extent.length = objlen;
71 }
72 req->r_num_pages = calc_pages_for(off, *plen);
3499e8a5
YS
73
74 dout("calc_layout bno=%llx %llu~%llu (%d pages)\n",
75 *bno, objoff, objlen, req->r_num_pages);
76
77}
78
f24e9980
SW
79/*
80 * Implement client access to distributed object storage cluster.
81 *
82 * All data objects are stored within a cluster/cloud of OSDs, or
83 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
84 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
85 * remote daemons serving up and coordinating consistent and safe
86 * access to storage.
87 *
88 * Cluster membership and the mapping of data objects onto storage devices
89 * are described by the osd map.
90 *
91 * We keep track of pending OSD requests (read, write), resubmit
92 * requests to different OSDs when the cluster topology/data layout
93 * change, or retry the affected requests when the communications
94 * channel with an OSD is reset.
95 */
96
97/*
98 * calculate the mapping of a file extent onto an object, and fill out the
99 * request accordingly. shorten extent as necessary if it crosses an
100 * object boundary.
101 *
102 * fill osd op in request message.
103 */
104static void calc_layout(struct ceph_osd_client *osdc,
3499e8a5
YS
105 struct ceph_vino vino,
106 struct ceph_file_layout *layout,
f24e9980 107 u64 off, u64 *plen,
68b4476b
YS
108 struct ceph_osd_request *req,
109 struct ceph_osd_req_op *op)
f24e9980 110{
f24e9980
SW
111 u64 bno;
112
68b4476b
YS
113 ceph_calc_raw_layout(osdc, layout, vino.snap, off,
114 plen, &bno, req, op);
f24e9980
SW
115
116 sprintf(req->r_oid, "%llx.%08llx", vino.ino, bno);
117 req->r_oid_len = strlen(req->r_oid);
f24e9980
SW
118}
119
f24e9980
SW
120/*
121 * requests
122 */
415e49a9 123void ceph_osdc_release_request(struct kref *kref)
f24e9980 124{
415e49a9
SW
125 struct ceph_osd_request *req = container_of(kref,
126 struct ceph_osd_request,
127 r_kref);
128
129 if (req->r_request)
130 ceph_msg_put(req->r_request);
131 if (req->r_reply)
132 ceph_msg_put(req->r_reply);
0d59ab81 133 if (req->r_con_filling_msg) {
350b1c32 134 dout("release_request revoking pages %p from con %p\n",
0d59ab81
YS
135 req->r_pages, req->r_con_filling_msg);
136 ceph_con_revoke_message(req->r_con_filling_msg,
137 req->r_reply);
138 ceph_con_put(req->r_con_filling_msg);
350b1c32 139 }
415e49a9
SW
140 if (req->r_own_pages)
141 ceph_release_page_vector(req->r_pages,
142 req->r_num_pages);
68b4476b
YS
143#ifdef CONFIG_BLOCK
144 if (req->r_bio)
145 bio_put(req->r_bio);
146#endif
415e49a9 147 ceph_put_snap_context(req->r_snapc);
68b4476b
YS
148 if (req->r_trail) {
149 ceph_pagelist_release(req->r_trail);
150 kfree(req->r_trail);
151 }
415e49a9
SW
152 if (req->r_mempool)
153 mempool_free(req, req->r_osdc->req_mempool);
154 else
155 kfree(req);
f24e9980
SW
156}
157
68b4476b
YS
158static int op_needs_trail(int op)
159{
160 switch (op) {
161 case CEPH_OSD_OP_GETXATTR:
162 case CEPH_OSD_OP_SETXATTR:
163 case CEPH_OSD_OP_CMPXATTR:
ae1533b6 164 case CEPH_OSD_OP_CALL:
68b4476b
YS
165 return 1;
166 default:
167 return 0;
168 }
169}
170
171static int get_num_ops(struct ceph_osd_req_op *ops, int *needs_trail)
172{
173 int i = 0;
174
175 if (needs_trail)
176 *needs_trail = 0;
177 while (ops[i].op) {
178 if (needs_trail && op_needs_trail(ops[i].op))
179 *needs_trail = 1;
180 i++;
181 }
182
183 return i;
184}
185
3499e8a5
YS
186struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
187 int flags,
f24e9980 188 struct ceph_snap_context *snapc,
68b4476b 189 struct ceph_osd_req_op *ops,
3499e8a5
YS
190 bool use_mempool,
191 gfp_t gfp_flags,
68b4476b
YS
192 struct page **pages,
193 struct bio *bio)
f24e9980
SW
194{
195 struct ceph_osd_request *req;
196 struct ceph_msg *msg;
68b4476b
YS
197 int needs_trail;
198 int num_op = get_num_ops(ops, &needs_trail);
199 size_t msg_size = sizeof(struct ceph_osd_request_head);
3499e8a5 200
68b4476b 201 msg_size += num_op*sizeof(struct ceph_osd_op);
f24e9980
SW
202
203 if (use_mempool) {
3499e8a5 204 req = mempool_alloc(osdc->req_mempool, gfp_flags);
f24e9980
SW
205 memset(req, 0, sizeof(*req));
206 } else {
3499e8a5 207 req = kzalloc(sizeof(*req), gfp_flags);
f24e9980
SW
208 }
209 if (req == NULL)
a79832f2 210 return NULL;
f24e9980 211
f24e9980
SW
212 req->r_osdc = osdc;
213 req->r_mempool = use_mempool;
68b4476b 214
415e49a9 215 kref_init(&req->r_kref);
f24e9980
SW
216 init_completion(&req->r_completion);
217 init_completion(&req->r_safe_completion);
218 INIT_LIST_HEAD(&req->r_unsafe_item);
219 req->r_flags = flags;
220
221 WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
222
c16e7869
SW
223 /* create reply message */
224 if (use_mempool)
225 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
226 else
227 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
3499e8a5 228 OSD_OPREPLY_FRONT_LEN, gfp_flags);
a79832f2 229 if (!msg) {
c16e7869 230 ceph_osdc_put_request(req);
a79832f2 231 return NULL;
c16e7869
SW
232 }
233 req->r_reply = msg;
234
68b4476b
YS
235 /* allocate space for the trailing data */
236 if (needs_trail) {
237 req->r_trail = kmalloc(sizeof(struct ceph_pagelist), gfp_flags);
238 if (!req->r_trail) {
239 ceph_osdc_put_request(req);
240 return NULL;
241 }
242 ceph_pagelist_init(req->r_trail);
243 }
c16e7869 244 /* create request message; allow space for oid */
f24e9980
SW
245 msg_size += 40;
246 if (snapc)
247 msg_size += sizeof(u64) * snapc->num_snaps;
248 if (use_mempool)
8f3bc053 249 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
f24e9980 250 else
3499e8a5 251 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags);
a79832f2 252 if (!msg) {
f24e9980 253 ceph_osdc_put_request(req);
a79832f2 254 return NULL;
f24e9980 255 }
68b4476b 256
f24e9980
SW
257 msg->hdr.type = cpu_to_le16(CEPH_MSG_OSD_OP);
258 memset(msg->front.iov_base, 0, msg->front.iov_len);
3499e8a5
YS
259
260 req->r_request = msg;
261 req->r_pages = pages;
68b4476b
YS
262#ifdef CONFIG_BLOCK
263 if (bio) {
264 req->r_bio = bio;
265 bio_get(req->r_bio);
266 }
267#endif
3499e8a5
YS
268
269 return req;
270}
271
68b4476b
YS
272static void osd_req_encode_op(struct ceph_osd_request *req,
273 struct ceph_osd_op *dst,
274 struct ceph_osd_req_op *src)
275{
276 dst->op = cpu_to_le16(src->op);
277
278 switch (dst->op) {
279 case CEPH_OSD_OP_READ:
280 case CEPH_OSD_OP_WRITE:
281 dst->extent.offset =
282 cpu_to_le64(src->extent.offset);
283 dst->extent.length =
284 cpu_to_le64(src->extent.length);
285 dst->extent.truncate_size =
286 cpu_to_le64(src->extent.truncate_size);
287 dst->extent.truncate_seq =
288 cpu_to_le32(src->extent.truncate_seq);
289 break;
290
291 case CEPH_OSD_OP_GETXATTR:
292 case CEPH_OSD_OP_SETXATTR:
293 case CEPH_OSD_OP_CMPXATTR:
294 BUG_ON(!req->r_trail);
295
296 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
297 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
298 dst->xattr.cmp_op = src->xattr.cmp_op;
299 dst->xattr.cmp_mode = src->xattr.cmp_mode;
300 ceph_pagelist_append(req->r_trail, src->xattr.name,
301 src->xattr.name_len);
302 ceph_pagelist_append(req->r_trail, src->xattr.val,
303 src->xattr.value_len);
304 break;
ae1533b6
YS
305 case CEPH_OSD_OP_CALL:
306 BUG_ON(!req->r_trail);
307
308 dst->cls.class_len = src->cls.class_len;
309 dst->cls.method_len = src->cls.method_len;
310 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
311
312 ceph_pagelist_append(req->r_trail, src->cls.class_name,
313 src->cls.class_len);
314 ceph_pagelist_append(req->r_trail, src->cls.method_name,
315 src->cls.method_len);
316 ceph_pagelist_append(req->r_trail, src->cls.indata,
317 src->cls.indata_len);
318 break;
319 case CEPH_OSD_OP_ROLLBACK:
320 dst->snap.snapid = cpu_to_le64(src->snap.snapid);
321 break;
68b4476b
YS
322 case CEPH_OSD_OP_STARTSYNC:
323 break;
324 default:
325 pr_err("unrecognized osd opcode %d\n", dst->op);
326 WARN_ON(1);
327 break;
328 }
329 dst->payload_len = cpu_to_le32(src->payload_len);
330}
331
3499e8a5
YS
332/*
333 * build new request AND message
334 *
335 */
336void ceph_osdc_build_request(struct ceph_osd_request *req,
68b4476b
YS
337 u64 off, u64 *plen,
338 struct ceph_osd_req_op *src_ops,
339 struct ceph_snap_context *snapc,
340 struct timespec *mtime,
341 const char *oid,
342 int oid_len)
3499e8a5
YS
343{
344 struct ceph_msg *msg = req->r_request;
345 struct ceph_osd_request_head *head;
68b4476b 346 struct ceph_osd_req_op *src_op;
3499e8a5
YS
347 struct ceph_osd_op *op;
348 void *p;
68b4476b 349 int num_op = get_num_ops(src_ops, NULL);
3499e8a5 350 size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
3499e8a5 351 int flags = req->r_flags;
68b4476b
YS
352 u64 data_len = 0;
353 int i;
3499e8a5 354
f24e9980
SW
355 head = msg->front.iov_base;
356 op = (void *)(head + 1);
357 p = (void *)(op + num_op);
358
f24e9980
SW
359 req->r_snapc = ceph_get_snap_context(snapc);
360
361 head->client_inc = cpu_to_le32(1); /* always, for now. */
362 head->flags = cpu_to_le32(flags);
363 if (flags & CEPH_OSD_FLAG_WRITE)
364 ceph_encode_timespec(&head->mtime, mtime);
365 head->num_ops = cpu_to_le16(num_op);
f24e9980 366
f24e9980
SW
367
368 /* fill in oid */
3499e8a5
YS
369 head->object_len = cpu_to_le32(oid_len);
370 memcpy(p, oid, oid_len);
371 p += oid_len;
f24e9980 372
68b4476b
YS
373 src_op = src_ops;
374 while (src_op->op) {
375 osd_req_encode_op(req, op, src_op);
376 src_op++;
f24e9980 377 op++;
f24e9980 378 }
68b4476b
YS
379
380 if (req->r_trail)
381 data_len += req->r_trail->length;
382
f24e9980
SW
383 if (snapc) {
384 head->snap_seq = cpu_to_le64(snapc->seq);
385 head->num_snaps = cpu_to_le32(snapc->num_snaps);
386 for (i = 0; i < snapc->num_snaps; i++) {
387 put_unaligned_le64(snapc->snaps[i], p);
388 p += sizeof(u64);
389 }
390 }
391
68b4476b
YS
392 if (flags & CEPH_OSD_FLAG_WRITE) {
393 req->r_request->hdr.data_off = cpu_to_le16(off);
394 req->r_request->hdr.data_len = cpu_to_le32(*plen + data_len);
395 } else if (data_len) {
396 req->r_request->hdr.data_off = 0;
397 req->r_request->hdr.data_len = cpu_to_le32(data_len);
398 }
399
f24e9980 400 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
6f863e71
SW
401 msg_size = p - msg->front.iov_base;
402 msg->front.iov_len = msg_size;
403 msg->hdr.front_len = cpu_to_le32(msg_size);
3499e8a5
YS
404 return;
405}
406
407/*
408 * build new request AND message, calculate layout, and adjust file
409 * extent as needed.
410 *
411 * if the file was recently truncated, we include information about its
412 * old and new size so that the object can be updated appropriately. (we
413 * avoid synchronously deleting truncated objects because it's slow.)
414 *
415 * if @do_sync, include a 'startsync' command so that the osd will flush
416 * data quickly.
417 */
418struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
419 struct ceph_file_layout *layout,
420 struct ceph_vino vino,
421 u64 off, u64 *plen,
422 int opcode, int flags,
423 struct ceph_snap_context *snapc,
424 int do_sync,
425 u32 truncate_seq,
426 u64 truncate_size,
427 struct timespec *mtime,
428 bool use_mempool, int num_reply)
429{
68b4476b
YS
430 struct ceph_osd_req_op ops[3];
431 struct ceph_osd_request *req;
432
433 ops[0].op = opcode;
434 ops[0].extent.truncate_seq = truncate_seq;
435 ops[0].extent.truncate_size = truncate_size;
436 ops[0].payload_len = 0;
437
438 if (do_sync) {
439 ops[1].op = CEPH_OSD_OP_STARTSYNC;
440 ops[1].payload_len = 0;
441 ops[2].op = 0;
442 } else
443 ops[1].op = 0;
444
445 req = ceph_osdc_alloc_request(osdc, flags,
446 snapc, ops,
3499e8a5 447 use_mempool,
68b4476b 448 GFP_NOFS, NULL, NULL);
3499e8a5
YS
449 if (IS_ERR(req))
450 return req;
451
452 /* calculate max write size */
68b4476b 453 calc_layout(osdc, vino, layout, off, plen, req, ops);
3499e8a5
YS
454 req->r_file_layout = *layout; /* keep a copy */
455
68b4476b
YS
456 ceph_osdc_build_request(req, off, plen, ops,
457 snapc,
3499e8a5
YS
458 mtime,
459 req->r_oid, req->r_oid_len);
460
f24e9980
SW
461 return req;
462}
463
464/*
465 * We keep osd requests in an rbtree, sorted by ->r_tid.
466 */
467static void __insert_request(struct ceph_osd_client *osdc,
468 struct ceph_osd_request *new)
469{
470 struct rb_node **p = &osdc->requests.rb_node;
471 struct rb_node *parent = NULL;
472 struct ceph_osd_request *req = NULL;
473
474 while (*p) {
475 parent = *p;
476 req = rb_entry(parent, struct ceph_osd_request, r_node);
477 if (new->r_tid < req->r_tid)
478 p = &(*p)->rb_left;
479 else if (new->r_tid > req->r_tid)
480 p = &(*p)->rb_right;
481 else
482 BUG();
483 }
484
485 rb_link_node(&new->r_node, parent, p);
486 rb_insert_color(&new->r_node, &osdc->requests);
487}
488
489static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
490 u64 tid)
491{
492 struct ceph_osd_request *req;
493 struct rb_node *n = osdc->requests.rb_node;
494
495 while (n) {
496 req = rb_entry(n, struct ceph_osd_request, r_node);
497 if (tid < req->r_tid)
498 n = n->rb_left;
499 else if (tid > req->r_tid)
500 n = n->rb_right;
501 else
502 return req;
503 }
504 return NULL;
505}
506
507static struct ceph_osd_request *
508__lookup_request_ge(struct ceph_osd_client *osdc,
509 u64 tid)
510{
511 struct ceph_osd_request *req;
512 struct rb_node *n = osdc->requests.rb_node;
513
514 while (n) {
515 req = rb_entry(n, struct ceph_osd_request, r_node);
516 if (tid < req->r_tid) {
517 if (!n->rb_left)
518 return req;
519 n = n->rb_left;
520 } else if (tid > req->r_tid) {
521 n = n->rb_right;
522 } else {
523 return req;
524 }
525 }
526 return NULL;
527}
528
529
530/*
81b024e7 531 * If the osd connection drops, we need to resubmit all requests.
f24e9980
SW
532 */
533static void osd_reset(struct ceph_connection *con)
534{
535 struct ceph_osd *osd = con->private;
536 struct ceph_osd_client *osdc;
537
538 if (!osd)
539 return;
540 dout("osd_reset osd%d\n", osd->o_osd);
541 osdc = osd->o_osdc;
f24e9980
SW
542 down_read(&osdc->map_sem);
543 kick_requests(osdc, osd);
544 up_read(&osdc->map_sem);
545}
546
547/*
548 * Track open sessions with osds.
549 */
550static struct ceph_osd *create_osd(struct ceph_osd_client *osdc)
551{
552 struct ceph_osd *osd;
553
554 osd = kzalloc(sizeof(*osd), GFP_NOFS);
555 if (!osd)
556 return NULL;
557
558 atomic_set(&osd->o_ref, 1);
559 osd->o_osdc = osdc;
560 INIT_LIST_HEAD(&osd->o_requests);
f5a2041b 561 INIT_LIST_HEAD(&osd->o_osd_lru);
f24e9980
SW
562 osd->o_incarnation = 1;
563
564 ceph_con_init(osdc->client->msgr, &osd->o_con);
565 osd->o_con.private = osd;
566 osd->o_con.ops = &osd_con_ops;
567 osd->o_con.peer_name.type = CEPH_ENTITY_TYPE_OSD;
4e7a5dcd 568
422d2cb8 569 INIT_LIST_HEAD(&osd->o_keepalive_item);
f24e9980
SW
570 return osd;
571}
572
573static struct ceph_osd *get_osd(struct ceph_osd *osd)
574{
575 if (atomic_inc_not_zero(&osd->o_ref)) {
576 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
577 atomic_read(&osd->o_ref));
578 return osd;
579 } else {
580 dout("get_osd %p FAIL\n", osd);
581 return NULL;
582 }
583}
584
585static void put_osd(struct ceph_osd *osd)
586{
587 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
588 atomic_read(&osd->o_ref) - 1);
79494d1b
SW
589 if (atomic_dec_and_test(&osd->o_ref)) {
590 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
591
592 if (osd->o_authorizer)
593 ac->ops->destroy_authorizer(ac, osd->o_authorizer);
f24e9980 594 kfree(osd);
79494d1b 595 }
f24e9980
SW
596}
597
598/*
599 * remove an osd from our map
600 */
f5a2041b 601static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
f24e9980 602{
f5a2041b 603 dout("__remove_osd %p\n", osd);
f24e9980
SW
604 BUG_ON(!list_empty(&osd->o_requests));
605 rb_erase(&osd->o_node, &osdc->osds);
f5a2041b 606 list_del_init(&osd->o_osd_lru);
f24e9980
SW
607 ceph_con_close(&osd->o_con);
608 put_osd(osd);
609}
610
f5a2041b
YS
611static void __move_osd_to_lru(struct ceph_osd_client *osdc,
612 struct ceph_osd *osd)
613{
614 dout("__move_osd_to_lru %p\n", osd);
615 BUG_ON(!list_empty(&osd->o_osd_lru));
616 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
617 osd->lru_ttl = jiffies + osdc->client->mount_args->osd_idle_ttl * HZ;
618}
619
620static void __remove_osd_from_lru(struct ceph_osd *osd)
621{
622 dout("__remove_osd_from_lru %p\n", osd);
623 if (!list_empty(&osd->o_osd_lru))
624 list_del_init(&osd->o_osd_lru);
625}
626
627static void remove_old_osds(struct ceph_osd_client *osdc, int remove_all)
628{
629 struct ceph_osd *osd, *nosd;
630
631 dout("__remove_old_osds %p\n", osdc);
632 mutex_lock(&osdc->request_mutex);
633 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
634 if (!remove_all && time_before(jiffies, osd->lru_ttl))
635 break;
636 __remove_osd(osdc, osd);
637 }
638 mutex_unlock(&osdc->request_mutex);
639}
640
f24e9980
SW
641/*
642 * reset osd connect
643 */
f5a2041b 644static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
f24e9980 645{
87b315a5 646 struct ceph_osd_request *req;
f24e9980
SW
647 int ret = 0;
648
f5a2041b 649 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
f24e9980 650 if (list_empty(&osd->o_requests)) {
f5a2041b 651 __remove_osd(osdc, osd);
87b315a5
SW
652 } else if (memcmp(&osdc->osdmap->osd_addr[osd->o_osd],
653 &osd->o_con.peer_addr,
654 sizeof(osd->o_con.peer_addr)) == 0 &&
655 !ceph_con_opened(&osd->o_con)) {
656 dout(" osd addr hasn't changed and connection never opened,"
657 " letting msgr retry");
658 /* touch each r_stamp for handle_timeout()'s benfit */
659 list_for_each_entry(req, &osd->o_requests, r_osd_item)
660 req->r_stamp = jiffies;
661 ret = -EAGAIN;
f24e9980
SW
662 } else {
663 ceph_con_close(&osd->o_con);
664 ceph_con_open(&osd->o_con, &osdc->osdmap->osd_addr[osd->o_osd]);
665 osd->o_incarnation++;
666 }
667 return ret;
668}
669
670static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
671{
672 struct rb_node **p = &osdc->osds.rb_node;
673 struct rb_node *parent = NULL;
674 struct ceph_osd *osd = NULL;
675
676 while (*p) {
677 parent = *p;
678 osd = rb_entry(parent, struct ceph_osd, o_node);
679 if (new->o_osd < osd->o_osd)
680 p = &(*p)->rb_left;
681 else if (new->o_osd > osd->o_osd)
682 p = &(*p)->rb_right;
683 else
684 BUG();
685 }
686
687 rb_link_node(&new->o_node, parent, p);
688 rb_insert_color(&new->o_node, &osdc->osds);
689}
690
691static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
692{
693 struct ceph_osd *osd;
694 struct rb_node *n = osdc->osds.rb_node;
695
696 while (n) {
697 osd = rb_entry(n, struct ceph_osd, o_node);
698 if (o < osd->o_osd)
699 n = n->rb_left;
700 else if (o > osd->o_osd)
701 n = n->rb_right;
702 else
703 return osd;
704 }
705 return NULL;
706}
707
422d2cb8
YS
708static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
709{
710 schedule_delayed_work(&osdc->timeout_work,
711 osdc->client->mount_args->osd_keepalive_timeout * HZ);
712}
713
714static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
715{
716 cancel_delayed_work(&osdc->timeout_work);
717}
f24e9980
SW
718
719/*
720 * Register request, assign tid. If this is the first request, set up
721 * the timeout event.
722 */
723static void register_request(struct ceph_osd_client *osdc,
724 struct ceph_osd_request *req)
725{
f24e9980
SW
726 mutex_lock(&osdc->request_mutex);
727 req->r_tid = ++osdc->last_tid;
6df058c0 728 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
422d2cb8 729 INIT_LIST_HEAD(&req->r_req_lru_item);
f24e9980
SW
730
731 dout("register_request %p tid %lld\n", req, req->r_tid);
732 __insert_request(osdc, req);
733 ceph_osdc_get_request(req);
734 osdc->num_requests++;
735
f24e9980 736 if (osdc->num_requests == 1) {
422d2cb8
YS
737 dout(" first request, scheduling timeout\n");
738 __schedule_osd_timeout(osdc);
f24e9980
SW
739 }
740 mutex_unlock(&osdc->request_mutex);
741}
742
743/*
744 * called under osdc->request_mutex
745 */
746static void __unregister_request(struct ceph_osd_client *osdc,
747 struct ceph_osd_request *req)
748{
749 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
750 rb_erase(&req->r_node, &osdc->requests);
751 osdc->num_requests--;
752
0ba6478d
SW
753 if (req->r_osd) {
754 /* make sure the original request isn't in flight. */
755 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
756
757 list_del_init(&req->r_osd_item);
758 if (list_empty(&req->r_osd->o_requests))
f5a2041b 759 __move_osd_to_lru(osdc, req->r_osd);
0ba6478d
SW
760 req->r_osd = NULL;
761 }
f24e9980
SW
762
763 ceph_osdc_put_request(req);
764
422d2cb8
YS
765 list_del_init(&req->r_req_lru_item);
766 if (osdc->num_requests == 0) {
767 dout(" no requests, canceling timeout\n");
768 __cancel_osd_timeout(osdc);
f24e9980
SW
769 }
770}
771
772/*
773 * Cancel a previously queued request message
774 */
775static void __cancel_request(struct ceph_osd_request *req)
776{
6bc18876 777 if (req->r_sent && req->r_osd) {
f24e9980
SW
778 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
779 req->r_sent = 0;
780 }
422d2cb8 781 list_del_init(&req->r_req_lru_item);
f24e9980
SW
782}
783
784/*
785 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
786 * (as needed), and set the request r_osd appropriately. If there is
787 * no up osd, set r_osd to NULL.
788 *
789 * Return 0 if unchanged, 1 if changed, or negative on error.
790 *
791 * Caller should hold map_sem for read and request_mutex.
792 */
793static int __map_osds(struct ceph_osd_client *osdc,
794 struct ceph_osd_request *req)
795{
796 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
51042122 797 struct ceph_pg pgid;
d85b7056
SW
798 int acting[CEPH_PG_MAX_SIZE];
799 int o = -1, num = 0;
f24e9980 800 int err;
f24e9980
SW
801
802 dout("map_osds %p tid %lld\n", req, req->r_tid);
803 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
804 &req->r_file_layout, osdc->osdmap);
805 if (err)
806 return err;
51042122 807 pgid = reqhead->layout.ol_pgid;
7740a42f
SW
808 req->r_pgid = pgid;
809
d85b7056
SW
810 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
811 if (err > 0) {
812 o = acting[0];
813 num = err;
814 }
f24e9980
SW
815
816 if ((req->r_osd && req->r_osd->o_osd == o &&
d85b7056
SW
817 req->r_sent >= req->r_osd->o_incarnation &&
818 req->r_num_pg_osds == num &&
819 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
f24e9980
SW
820 (req->r_osd == NULL && o == -1))
821 return 0; /* no change */
822
51042122
SW
823 dout("map_osds tid %llu pgid %d.%x osd%d (was osd%d)\n",
824 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
f24e9980
SW
825 req->r_osd ? req->r_osd->o_osd : -1);
826
d85b7056
SW
827 /* record full pg acting set */
828 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
829 req->r_num_pg_osds = num;
830
f24e9980
SW
831 if (req->r_osd) {
832 __cancel_request(req);
833 list_del_init(&req->r_osd_item);
f24e9980
SW
834 req->r_osd = NULL;
835 }
836
837 req->r_osd = __lookup_osd(osdc, o);
838 if (!req->r_osd && o >= 0) {
c99eb1c7
SW
839 err = -ENOMEM;
840 req->r_osd = create_osd(osdc);
841 if (!req->r_osd)
842 goto out;
f24e9980
SW
843
844 dout("map_osds osd %p is osd%d\n", req->r_osd, o);
845 req->r_osd->o_osd = o;
846 req->r_osd->o_con.peer_name.num = cpu_to_le64(o);
847 __insert_osd(osdc, req->r_osd);
848
849 ceph_con_open(&req->r_osd->o_con, &osdc->osdmap->osd_addr[o]);
850 }
851
f5a2041b
YS
852 if (req->r_osd) {
853 __remove_osd_from_lru(req->r_osd);
f24e9980 854 list_add(&req->r_osd_item, &req->r_osd->o_requests);
f5a2041b 855 }
d85b7056 856 err = 1; /* osd or pg changed */
f24e9980
SW
857
858out:
f24e9980
SW
859 return err;
860}
861
862/*
863 * caller should hold map_sem (for read) and request_mutex
864 */
865static int __send_request(struct ceph_osd_client *osdc,
866 struct ceph_osd_request *req)
867{
868 struct ceph_osd_request_head *reqhead;
869 int err;
870
871 err = __map_osds(osdc, req);
872 if (err < 0)
873 return err;
874 if (req->r_osd == NULL) {
875 dout("send_request %p no up osds in pg\n", req);
876 ceph_monc_request_next_osdmap(&osdc->client->monc);
877 return 0;
878 }
879
880 dout("send_request %p tid %llu to osd%d flags %d\n",
881 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
882
883 reqhead = req->r_request->front.iov_base;
884 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
885 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
886 reqhead->reassert_version = req->r_reassert_version;
887
3dd72fc0 888 req->r_stamp = jiffies;
07a27e22 889 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
f24e9980
SW
890
891 ceph_msg_get(req->r_request); /* send consumes a ref */
892 ceph_con_send(&req->r_osd->o_con, req->r_request);
893 req->r_sent = req->r_osd->o_incarnation;
894 return 0;
895}
896
897/*
898 * Timeout callback, called every N seconds when 1 or more osd
899 * requests has been active for more than N seconds. When this
900 * happens, we ping all OSDs with requests who have timed out to
901 * ensure any communications channel reset is detected. Reset the
902 * request timeouts another N seconds in the future as we go.
903 * Reschedule the timeout event another N seconds in future (unless
904 * there are no open requests).
905 */
906static void handle_timeout(struct work_struct *work)
907{
908 struct ceph_osd_client *osdc =
909 container_of(work, struct ceph_osd_client, timeout_work.work);
422d2cb8 910 struct ceph_osd_request *req, *last_req = NULL;
f24e9980 911 struct ceph_osd *osd;
6b805185 912 unsigned long timeout = osdc->client->mount_args->osd_timeout * HZ;
422d2cb8
YS
913 unsigned long keepalive =
914 osdc->client->mount_args->osd_keepalive_timeout * HZ;
3dd72fc0 915 unsigned long last_stamp = 0;
f24e9980 916 struct rb_node *p;
422d2cb8 917 struct list_head slow_osds;
f24e9980
SW
918
919 dout("timeout\n");
920 down_read(&osdc->map_sem);
921
922 ceph_monc_request_next_osdmap(&osdc->client->monc);
923
924 mutex_lock(&osdc->request_mutex);
925 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
926 req = rb_entry(p, struct ceph_osd_request, r_node);
927
928 if (req->r_resend) {
929 int err;
930
931 dout("osdc resending prev failed %lld\n", req->r_tid);
932 err = __send_request(osdc, req);
933 if (err)
934 dout("osdc failed again on %lld\n", req->r_tid);
935 else
936 req->r_resend = false;
937 continue;
938 }
939 }
f24e9980 940
422d2cb8
YS
941 /*
942 * reset osds that appear to be _really_ unresponsive. this
943 * is a failsafe measure.. we really shouldn't be getting to
944 * this point if the system is working properly. the monitors
945 * should mark the osd as failed and we should find out about
946 * it from an updated osd map.
947 */
f26e681d 948 while (timeout && !list_empty(&osdc->req_lru)) {
422d2cb8
YS
949 req = list_entry(osdc->req_lru.next, struct ceph_osd_request,
950 r_req_lru_item);
951
3dd72fc0 952 if (time_before(jiffies, req->r_stamp + timeout))
422d2cb8
YS
953 break;
954
3dd72fc0 955 BUG_ON(req == last_req && req->r_stamp == last_stamp);
422d2cb8 956 last_req = req;
3dd72fc0 957 last_stamp = req->r_stamp;
422d2cb8
YS
958
959 osd = req->r_osd;
960 BUG_ON(!osd);
961 pr_warning(" tid %llu timed out on osd%d, will reset osd\n",
962 req->r_tid, osd->o_osd);
963 __kick_requests(osdc, osd);
964 }
965
966 /*
967 * ping osds that are a bit slow. this ensures that if there
968 * is a break in the TCP connection we will notice, and reopen
969 * a connection with that osd (from the fault callback).
970 */
971 INIT_LIST_HEAD(&slow_osds);
972 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
3dd72fc0 973 if (time_before(jiffies, req->r_stamp + keepalive))
422d2cb8
YS
974 break;
975
976 osd = req->r_osd;
977 BUG_ON(!osd);
978 dout(" tid %llu is slow, will send keepalive on osd%d\n",
f24e9980 979 req->r_tid, osd->o_osd);
422d2cb8
YS
980 list_move_tail(&osd->o_keepalive_item, &slow_osds);
981 }
982 while (!list_empty(&slow_osds)) {
983 osd = list_entry(slow_osds.next, struct ceph_osd,
984 o_keepalive_item);
985 list_del_init(&osd->o_keepalive_item);
f24e9980
SW
986 ceph_con_keepalive(&osd->o_con);
987 }
988
422d2cb8 989 __schedule_osd_timeout(osdc);
f24e9980
SW
990 mutex_unlock(&osdc->request_mutex);
991
992 up_read(&osdc->map_sem);
993}
994
f5a2041b
YS
995static void handle_osds_timeout(struct work_struct *work)
996{
997 struct ceph_osd_client *osdc =
998 container_of(work, struct ceph_osd_client,
999 osds_timeout_work.work);
1000 unsigned long delay =
1001 osdc->client->mount_args->osd_idle_ttl * HZ >> 2;
1002
1003 dout("osds timeout\n");
1004 down_read(&osdc->map_sem);
1005 remove_old_osds(osdc, 0);
1006 up_read(&osdc->map_sem);
1007
1008 schedule_delayed_work(&osdc->osds_timeout_work,
1009 round_jiffies_relative(delay));
1010}
1011
f24e9980
SW
1012/*
1013 * handle osd op reply. either call the callback if it is specified,
1014 * or do the completion to wake up the waiting thread.
1015 */
350b1c32
SW
1016static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1017 struct ceph_connection *con)
f24e9980
SW
1018{
1019 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
1020 struct ceph_osd_request *req;
1021 u64 tid;
1022 int numops, object_len, flags;
0ceed5db 1023 s32 result;
f24e9980 1024
6df058c0 1025 tid = le64_to_cpu(msg->hdr.tid);
f24e9980
SW
1026 if (msg->front.iov_len < sizeof(*rhead))
1027 goto bad;
f24e9980
SW
1028 numops = le32_to_cpu(rhead->num_ops);
1029 object_len = le32_to_cpu(rhead->object_len);
0ceed5db 1030 result = le32_to_cpu(rhead->result);
f24e9980
SW
1031 if (msg->front.iov_len != sizeof(*rhead) + object_len +
1032 numops * sizeof(struct ceph_osd_op))
1033 goto bad;
0ceed5db 1034 dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
f24e9980
SW
1035
1036 /* lookup */
1037 mutex_lock(&osdc->request_mutex);
1038 req = __lookup_request(osdc, tid);
1039 if (req == NULL) {
1040 dout("handle_reply tid %llu dne\n", tid);
1041 mutex_unlock(&osdc->request_mutex);
1042 return;
1043 }
1044 ceph_osdc_get_request(req);
1045 flags = le32_to_cpu(rhead->flags);
1046
350b1c32 1047 /*
0d59ab81 1048 * if this connection filled our message, drop our reference now, to
350b1c32
SW
1049 * avoid a (safe but slower) revoke later.
1050 */
0d59ab81 1051 if (req->r_con_filling_msg == con && req->r_reply == msg) {
c16e7869 1052 dout(" dropping con_filling_msg ref %p\n", con);
0d59ab81 1053 req->r_con_filling_msg = NULL;
350b1c32
SW
1054 ceph_con_put(con);
1055 }
1056
f24e9980
SW
1057 if (!req->r_got_reply) {
1058 unsigned bytes;
1059
1060 req->r_result = le32_to_cpu(rhead->result);
1061 bytes = le32_to_cpu(msg->hdr.data_len);
1062 dout("handle_reply result %d bytes %d\n", req->r_result,
1063 bytes);
1064 if (req->r_result == 0)
1065 req->r_result = bytes;
1066
1067 /* in case this is a write and we need to replay, */
1068 req->r_reassert_version = rhead->reassert_version;
1069
1070 req->r_got_reply = 1;
1071 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1072 dout("handle_reply tid %llu dup ack\n", tid);
34b43a56 1073 mutex_unlock(&osdc->request_mutex);
f24e9980
SW
1074 goto done;
1075 }
1076
1077 dout("handle_reply tid %llu flags %d\n", tid, flags);
1078
1079 /* either this is a read, or we got the safe response */
0ceed5db
SW
1080 if (result < 0 ||
1081 (flags & CEPH_OSD_FLAG_ONDISK) ||
f24e9980
SW
1082 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1083 __unregister_request(osdc, req);
1084
1085 mutex_unlock(&osdc->request_mutex);
1086
1087 if (req->r_callback)
1088 req->r_callback(req, msg);
1089 else
03066f23 1090 complete_all(&req->r_completion);
f24e9980
SW
1091
1092 if (flags & CEPH_OSD_FLAG_ONDISK) {
1093 if (req->r_safe_callback)
1094 req->r_safe_callback(req, msg);
03066f23 1095 complete_all(&req->r_safe_completion); /* fsync waiter */
f24e9980
SW
1096 }
1097
1098done:
1099 ceph_osdc_put_request(req);
1100 return;
1101
1102bad:
1103 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1104 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
1105 (int)sizeof(*rhead));
9ec7cab1 1106 ceph_msg_dump(msg);
f24e9980
SW
1107}
1108
1109
422d2cb8 1110static int __kick_requests(struct ceph_osd_client *osdc,
f24e9980
SW
1111 struct ceph_osd *kickosd)
1112{
1113 struct ceph_osd_request *req;
1114 struct rb_node *p, *n;
1115 int needmap = 0;
1116 int err;
1117
1118 dout("kick_requests osd%d\n", kickosd ? kickosd->o_osd : -1);
153a008b 1119 if (kickosd) {
87b315a5
SW
1120 err = __reset_osd(osdc, kickosd);
1121 if (err == -EAGAIN)
1122 return 1;
153a008b 1123 } else {
f24e9980
SW
1124 for (p = rb_first(&osdc->osds); p; p = n) {
1125 struct ceph_osd *osd =
1126 rb_entry(p, struct ceph_osd, o_node);
1127
1128 n = rb_next(p);
1129 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
103e2d3a
SW
1130 memcmp(&osd->o_con.peer_addr,
1131 ceph_osd_addr(osdc->osdmap,
1132 osd->o_osd),
1133 sizeof(struct ceph_entity_addr)) != 0)
f5a2041b 1134 __reset_osd(osdc, osd);
f24e9980
SW
1135 }
1136 }
1137
1138 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
1139 req = rb_entry(p, struct ceph_osd_request, r_node);
1140
1141 if (req->r_resend) {
1142 dout(" r_resend set on tid %llu\n", req->r_tid);
266673db 1143 __cancel_request(req);
f24e9980
SW
1144 goto kick;
1145 }
266673db
SW
1146 if (req->r_osd && kickosd == req->r_osd) {
1147 __cancel_request(req);
f24e9980 1148 goto kick;
266673db 1149 }
f24e9980
SW
1150
1151 err = __map_osds(osdc, req);
1152 if (err == 0)
1153 continue; /* no change */
1154 if (err < 0) {
1155 /*
1156 * FIXME: really, we should set the request
1157 * error and fail if this isn't a 'nofail'
1158 * request, but that's a fair bit more
1159 * complicated to do. So retry!
1160 */
1161 dout(" setting r_resend on %llu\n", req->r_tid);
1162 req->r_resend = true;
1163 continue;
1164 }
1165 if (req->r_osd == NULL) {
1166 dout("tid %llu maps to no valid osd\n", req->r_tid);
1167 needmap++; /* request a newer map */
1168 continue;
1169 }
1170
1171kick:
c1ea8823 1172 dout("kicking %p tid %llu osd%d\n", req, req->r_tid,
12eadc19 1173 req->r_osd ? req->r_osd->o_osd : -1);
f24e9980
SW
1174 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1175 err = __send_request(osdc, req);
1176 if (err) {
1177 dout(" setting r_resend on %llu\n", req->r_tid);
1178 req->r_resend = true;
1179 }
1180 }
422d2cb8
YS
1181
1182 return needmap;
1183}
1184
1185/*
1186 * Resubmit osd requests whose osd or osd address has changed. Request
1187 * a new osd map if osds are down, or we are otherwise unable to determine
1188 * how to direct a request.
1189 *
1190 * Close connections to down osds.
1191 *
1192 * If @who is specified, resubmit requests for that specific osd.
1193 *
1194 * Caller should hold map_sem for read and request_mutex.
1195 */
1196static void kick_requests(struct ceph_osd_client *osdc,
1197 struct ceph_osd *kickosd)
1198{
1199 int needmap;
1200
1201 mutex_lock(&osdc->request_mutex);
1202 needmap = __kick_requests(osdc, kickosd);
f24e9980
SW
1203 mutex_unlock(&osdc->request_mutex);
1204
1205 if (needmap) {
1206 dout("%d requests for down osds, need new map\n", needmap);
1207 ceph_monc_request_next_osdmap(&osdc->client->monc);
1208 }
f24e9980 1209
422d2cb8 1210}
f24e9980
SW
1211/*
1212 * Process updated osd map.
1213 *
1214 * The message contains any number of incremental and full maps, normally
1215 * indicating some sort of topology change in the cluster. Kick requests
1216 * off to different OSDs as needed.
1217 */
1218void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1219{
1220 void *p, *end, *next;
1221 u32 nr_maps, maplen;
1222 u32 epoch;
1223 struct ceph_osdmap *newmap = NULL, *oldmap;
1224 int err;
1225 struct ceph_fsid fsid;
1226
1227 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1228 p = msg->front.iov_base;
1229 end = p + msg->front.iov_len;
1230
1231 /* verify fsid */
1232 ceph_decode_need(&p, end, sizeof(fsid), bad);
1233 ceph_decode_copy(&p, &fsid, sizeof(fsid));
0743304d
SW
1234 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1235 return;
f24e9980
SW
1236
1237 down_write(&osdc->map_sem);
1238
1239 /* incremental maps */
1240 ceph_decode_32_safe(&p, end, nr_maps, bad);
1241 dout(" %d inc maps\n", nr_maps);
1242 while (nr_maps > 0) {
1243 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
c89136ea
SW
1244 epoch = ceph_decode_32(&p);
1245 maplen = ceph_decode_32(&p);
f24e9980
SW
1246 ceph_decode_need(&p, end, maplen, bad);
1247 next = p + maplen;
1248 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1249 dout("applying incremental map %u len %d\n",
1250 epoch, maplen);
1251 newmap = osdmap_apply_incremental(&p, next,
1252 osdc->osdmap,
1253 osdc->client->msgr);
1254 if (IS_ERR(newmap)) {
1255 err = PTR_ERR(newmap);
1256 goto bad;
1257 }
30dc6381 1258 BUG_ON(!newmap);
f24e9980
SW
1259 if (newmap != osdc->osdmap) {
1260 ceph_osdmap_destroy(osdc->osdmap);
1261 osdc->osdmap = newmap;
1262 }
1263 } else {
1264 dout("ignoring incremental map %u len %d\n",
1265 epoch, maplen);
1266 }
1267 p = next;
1268 nr_maps--;
1269 }
1270 if (newmap)
1271 goto done;
1272
1273 /* full maps */
1274 ceph_decode_32_safe(&p, end, nr_maps, bad);
1275 dout(" %d full maps\n", nr_maps);
1276 while (nr_maps) {
1277 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
c89136ea
SW
1278 epoch = ceph_decode_32(&p);
1279 maplen = ceph_decode_32(&p);
f24e9980
SW
1280 ceph_decode_need(&p, end, maplen, bad);
1281 if (nr_maps > 1) {
1282 dout("skipping non-latest full map %u len %d\n",
1283 epoch, maplen);
1284 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1285 dout("skipping full map %u len %d, "
1286 "older than our %u\n", epoch, maplen,
1287 osdc->osdmap->epoch);
1288 } else {
1289 dout("taking full map %u len %d\n", epoch, maplen);
1290 newmap = osdmap_decode(&p, p+maplen);
1291 if (IS_ERR(newmap)) {
1292 err = PTR_ERR(newmap);
1293 goto bad;
1294 }
30dc6381 1295 BUG_ON(!newmap);
f24e9980
SW
1296 oldmap = osdc->osdmap;
1297 osdc->osdmap = newmap;
1298 if (oldmap)
1299 ceph_osdmap_destroy(oldmap);
1300 }
1301 p += maplen;
1302 nr_maps--;
1303 }
1304
1305done:
1306 downgrade_write(&osdc->map_sem);
1307 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1308 if (newmap)
1309 kick_requests(osdc, NULL);
1310 up_read(&osdc->map_sem);
03066f23 1311 wake_up_all(&osdc->client->auth_wq);
f24e9980
SW
1312 return;
1313
1314bad:
1315 pr_err("osdc handle_map corrupt msg\n");
9ec7cab1 1316 ceph_msg_dump(msg);
f24e9980
SW
1317 up_write(&osdc->map_sem);
1318 return;
1319}
1320
f24e9980
SW
1321/*
1322 * Register request, send initial attempt.
1323 */
1324int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1325 struct ceph_osd_request *req,
1326 bool nofail)
1327{
c1ea8823 1328 int rc = 0;
f24e9980
SW
1329
1330 req->r_request->pages = req->r_pages;
1331 req->r_request->nr_pages = req->r_num_pages;
68b4476b
YS
1332#ifdef CONFIG_BLOCK
1333 req->r_request->bio = req->r_bio;
1334#endif
1335 req->r_request->trail = req->r_trail;
f24e9980
SW
1336
1337 register_request(osdc, req);
1338
1339 down_read(&osdc->map_sem);
1340 mutex_lock(&osdc->request_mutex);
c1ea8823
SW
1341 /*
1342 * a racing kick_requests() may have sent the message for us
1343 * while we dropped request_mutex above, so only send now if
1344 * the request still han't been touched yet.
1345 */
1346 if (req->r_sent == 0) {
1347 rc = __send_request(osdc, req);
1348 if (rc) {
1349 if (nofail) {
1350 dout("osdc_start_request failed send, "
1351 " marking %lld\n", req->r_tid);
1352 req->r_resend = true;
1353 rc = 0;
1354 } else {
1355 __unregister_request(osdc, req);
1356 }
f24e9980
SW
1357 }
1358 }
1359 mutex_unlock(&osdc->request_mutex);
1360 up_read(&osdc->map_sem);
1361 return rc;
1362}
1363
1364/*
1365 * wait for a request to complete
1366 */
1367int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1368 struct ceph_osd_request *req)
1369{
1370 int rc;
1371
1372 rc = wait_for_completion_interruptible(&req->r_completion);
1373 if (rc < 0) {
1374 mutex_lock(&osdc->request_mutex);
1375 __cancel_request(req);
529cfcc4 1376 __unregister_request(osdc, req);
f24e9980 1377 mutex_unlock(&osdc->request_mutex);
529cfcc4 1378 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
f24e9980
SW
1379 return rc;
1380 }
1381
1382 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1383 return req->r_result;
1384}
1385
1386/*
1387 * sync - wait for all in-flight requests to flush. avoid starvation.
1388 */
1389void ceph_osdc_sync(struct ceph_osd_client *osdc)
1390{
1391 struct ceph_osd_request *req;
1392 u64 last_tid, next_tid = 0;
1393
1394 mutex_lock(&osdc->request_mutex);
1395 last_tid = osdc->last_tid;
1396 while (1) {
1397 req = __lookup_request_ge(osdc, next_tid);
1398 if (!req)
1399 break;
1400 if (req->r_tid > last_tid)
1401 break;
1402
1403 next_tid = req->r_tid + 1;
1404 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1405 continue;
1406
1407 ceph_osdc_get_request(req);
1408 mutex_unlock(&osdc->request_mutex);
1409 dout("sync waiting on tid %llu (last is %llu)\n",
1410 req->r_tid, last_tid);
1411 wait_for_completion(&req->r_safe_completion);
1412 mutex_lock(&osdc->request_mutex);
1413 ceph_osdc_put_request(req);
1414 }
1415 mutex_unlock(&osdc->request_mutex);
1416 dout("sync done (thru tid %llu)\n", last_tid);
1417}
1418
1419/*
1420 * init, shutdown
1421 */
1422int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1423{
1424 int err;
1425
1426 dout("init\n");
1427 osdc->client = client;
1428 osdc->osdmap = NULL;
1429 init_rwsem(&osdc->map_sem);
1430 init_completion(&osdc->map_waiters);
1431 osdc->last_requested_map = 0;
1432 mutex_init(&osdc->request_mutex);
f24e9980
SW
1433 osdc->last_tid = 0;
1434 osdc->osds = RB_ROOT;
f5a2041b 1435 INIT_LIST_HEAD(&osdc->osd_lru);
f24e9980 1436 osdc->requests = RB_ROOT;
422d2cb8 1437 INIT_LIST_HEAD(&osdc->req_lru);
f24e9980
SW
1438 osdc->num_requests = 0;
1439 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
f5a2041b
YS
1440 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
1441
1442 schedule_delayed_work(&osdc->osds_timeout_work,
1443 round_jiffies_relative(osdc->client->mount_args->osd_idle_ttl * HZ));
f24e9980 1444
5f44f142 1445 err = -ENOMEM;
f24e9980
SW
1446 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1447 sizeof(struct ceph_osd_request));
1448 if (!osdc->req_mempool)
5f44f142 1449 goto out;
f24e9980 1450
4f48280e
SW
1451 err = ceph_msgpool_init(&osdc->msgpool_op, OSD_OP_FRONT_LEN, 10, true,
1452 "osd_op");
f24e9980 1453 if (err < 0)
5f44f142 1454 goto out_mempool;
c16e7869 1455 err = ceph_msgpool_init(&osdc->msgpool_op_reply,
4f48280e
SW
1456 OSD_OPREPLY_FRONT_LEN, 10, true,
1457 "osd_op_reply");
c16e7869
SW
1458 if (err < 0)
1459 goto out_msgpool;
f24e9980 1460 return 0;
5f44f142 1461
c16e7869
SW
1462out_msgpool:
1463 ceph_msgpool_destroy(&osdc->msgpool_op);
5f44f142
SW
1464out_mempool:
1465 mempool_destroy(osdc->req_mempool);
1466out:
1467 return err;
f24e9980
SW
1468}
1469
1470void ceph_osdc_stop(struct ceph_osd_client *osdc)
1471{
1472 cancel_delayed_work_sync(&osdc->timeout_work);
f5a2041b 1473 cancel_delayed_work_sync(&osdc->osds_timeout_work);
f24e9980
SW
1474 if (osdc->osdmap) {
1475 ceph_osdmap_destroy(osdc->osdmap);
1476 osdc->osdmap = NULL;
1477 }
f5a2041b 1478 remove_old_osds(osdc, 1);
f24e9980
SW
1479 mempool_destroy(osdc->req_mempool);
1480 ceph_msgpool_destroy(&osdc->msgpool_op);
c16e7869 1481 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
f24e9980
SW
1482}
1483
1484/*
1485 * Read some contiguous pages. If we cross a stripe boundary, shorten
1486 * *plen. Return number of bytes read, or error.
1487 */
1488int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1489 struct ceph_vino vino, struct ceph_file_layout *layout,
1490 u64 off, u64 *plen,
1491 u32 truncate_seq, u64 truncate_size,
1492 struct page **pages, int num_pages)
1493{
1494 struct ceph_osd_request *req;
1495 int rc = 0;
1496
1497 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1498 vino.snap, off, *plen);
1499 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1500 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1501 NULL, 0, truncate_seq, truncate_size, NULL,
1502 false, 1);
a79832f2
SW
1503 if (!req)
1504 return -ENOMEM;
f24e9980
SW
1505
1506 /* it may be a short read due to an object boundary */
1507 req->r_pages = pages;
f24e9980
SW
1508
1509 dout("readpages final extent is %llu~%llu (%d pages)\n",
1510 off, *plen, req->r_num_pages);
1511
1512 rc = ceph_osdc_start_request(osdc, req, false);
1513 if (!rc)
1514 rc = ceph_osdc_wait_request(osdc, req);
1515
1516 ceph_osdc_put_request(req);
1517 dout("readpages result %d\n", rc);
1518 return rc;
1519}
1520
1521/*
1522 * do a synchronous write on N pages
1523 */
1524int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1525 struct ceph_file_layout *layout,
1526 struct ceph_snap_context *snapc,
1527 u64 off, u64 len,
1528 u32 truncate_seq, u64 truncate_size,
1529 struct timespec *mtime,
1530 struct page **pages, int num_pages,
1531 int flags, int do_sync, bool nofail)
1532{
1533 struct ceph_osd_request *req;
1534 int rc = 0;
1535
1536 BUG_ON(vino.snap != CEPH_NOSNAP);
1537 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1538 CEPH_OSD_OP_WRITE,
1539 flags | CEPH_OSD_FLAG_ONDISK |
1540 CEPH_OSD_FLAG_WRITE,
1541 snapc, do_sync,
1542 truncate_seq, truncate_size, mtime,
1543 nofail, 1);
a79832f2
SW
1544 if (!req)
1545 return -ENOMEM;
f24e9980
SW
1546
1547 /* it may be a short write due to an object boundary */
1548 req->r_pages = pages;
f24e9980
SW
1549 dout("writepages %llu~%llu (%d pages)\n", off, len,
1550 req->r_num_pages);
1551
1552 rc = ceph_osdc_start_request(osdc, req, nofail);
1553 if (!rc)
1554 rc = ceph_osdc_wait_request(osdc, req);
1555
1556 ceph_osdc_put_request(req);
1557 if (rc == 0)
1558 rc = len;
1559 dout("writepages result %d\n", rc);
1560 return rc;
1561}
1562
1563/*
1564 * handle incoming message
1565 */
1566static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1567{
1568 struct ceph_osd *osd = con->private;
32c895e7 1569 struct ceph_osd_client *osdc;
f24e9980
SW
1570 int type = le16_to_cpu(msg->hdr.type);
1571
1572 if (!osd)
4a32f93d 1573 goto out;
32c895e7 1574 osdc = osd->o_osdc;
f24e9980
SW
1575
1576 switch (type) {
1577 case CEPH_MSG_OSD_MAP:
1578 ceph_osdc_handle_map(osdc, msg);
1579 break;
1580 case CEPH_MSG_OSD_OPREPLY:
350b1c32 1581 handle_reply(osdc, msg, con);
f24e9980
SW
1582 break;
1583
1584 default:
1585 pr_err("received unknown message type %d %s\n", type,
1586 ceph_msg_type_name(type));
1587 }
4a32f93d 1588out:
f24e9980
SW
1589 ceph_msg_put(msg);
1590}
1591
5b3a4db3 1592/*
21b667f6
SW
1593 * lookup and return message for incoming reply. set up reply message
1594 * pages.
5b3a4db3
SW
1595 */
1596static struct ceph_msg *get_reply(struct ceph_connection *con,
2450418c
YS
1597 struct ceph_msg_header *hdr,
1598 int *skip)
f24e9980
SW
1599{
1600 struct ceph_osd *osd = con->private;
1601 struct ceph_osd_client *osdc = osd->o_osdc;
2450418c 1602 struct ceph_msg *m;
0547a9b3 1603 struct ceph_osd_request *req;
5b3a4db3
SW
1604 int front = le32_to_cpu(hdr->front_len);
1605 int data_len = le32_to_cpu(hdr->data_len);
0547a9b3 1606 u64 tid;
f24e9980 1607
0547a9b3
YS
1608 tid = le64_to_cpu(hdr->tid);
1609 mutex_lock(&osdc->request_mutex);
1610 req = __lookup_request(osdc, tid);
1611 if (!req) {
1612 *skip = 1;
1613 m = NULL;
c16e7869 1614 pr_info("get_reply unknown tid %llu from osd%d\n", tid,
5b3a4db3 1615 osd->o_osd);
0547a9b3
YS
1616 goto out;
1617 }
c16e7869
SW
1618
1619 if (req->r_con_filling_msg) {
1620 dout("get_reply revoking msg %p from old con %p\n",
1621 req->r_reply, req->r_con_filling_msg);
1622 ceph_con_revoke_message(req->r_con_filling_msg, req->r_reply);
1623 ceph_con_put(req->r_con_filling_msg);
6f46cb29 1624 req->r_con_filling_msg = NULL;
0547a9b3
YS
1625 }
1626
c16e7869
SW
1627 if (front > req->r_reply->front.iov_len) {
1628 pr_warning("get_reply front %d > preallocated %d\n",
1629 front, (int)req->r_reply->front.iov_len);
34d23762 1630 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS);
a79832f2 1631 if (!m)
c16e7869
SW
1632 goto out;
1633 ceph_msg_put(req->r_reply);
1634 req->r_reply = m;
1635 }
1636 m = ceph_msg_get(req->r_reply);
1637
0547a9b3 1638 if (data_len > 0) {
21b667f6
SW
1639 unsigned data_off = le16_to_cpu(hdr->data_off);
1640 int want = calc_pages_for(data_off & ~PAGE_MASK, data_len);
1641
1642 if (unlikely(req->r_num_pages < want)) {
1643 pr_warning("tid %lld reply %d > expected %d pages\n",
1644 tid, want, m->nr_pages);
0547a9b3
YS
1645 *skip = 1;
1646 ceph_msg_put(m);
a79832f2 1647 m = NULL;
21b667f6 1648 goto out;
0547a9b3 1649 }
21b667f6
SW
1650 m->pages = req->r_pages;
1651 m->nr_pages = req->r_num_pages;
68b4476b
YS
1652#ifdef CONFIG_BLOCK
1653 m->bio = req->r_bio;
1654#endif
0547a9b3 1655 }
5b3a4db3 1656 *skip = 0;
c16e7869
SW
1657 req->r_con_filling_msg = ceph_con_get(con);
1658 dout("get_reply tid %lld %p\n", tid, m);
0547a9b3
YS
1659
1660out:
1661 mutex_unlock(&osdc->request_mutex);
2450418c 1662 return m;
5b3a4db3
SW
1663
1664}
1665
1666static struct ceph_msg *alloc_msg(struct ceph_connection *con,
1667 struct ceph_msg_header *hdr,
1668 int *skip)
1669{
1670 struct ceph_osd *osd = con->private;
1671 int type = le16_to_cpu(hdr->type);
1672 int front = le32_to_cpu(hdr->front_len);
1673
1674 switch (type) {
1675 case CEPH_MSG_OSD_MAP:
34d23762 1676 return ceph_msg_new(type, front, GFP_NOFS);
5b3a4db3
SW
1677 case CEPH_MSG_OSD_OPREPLY:
1678 return get_reply(con, hdr, skip);
1679 default:
1680 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
1681 osd->o_osd);
1682 *skip = 1;
1683 return NULL;
1684 }
f24e9980
SW
1685}
1686
1687/*
1688 * Wrappers to refcount containing ceph_osd struct
1689 */
1690static struct ceph_connection *get_osd_con(struct ceph_connection *con)
1691{
1692 struct ceph_osd *osd = con->private;
1693 if (get_osd(osd))
1694 return con;
1695 return NULL;
1696}
1697
1698static void put_osd_con(struct ceph_connection *con)
1699{
1700 struct ceph_osd *osd = con->private;
1701 put_osd(osd);
1702}
1703
4e7a5dcd
SW
1704/*
1705 * authentication
1706 */
1707static int get_authorizer(struct ceph_connection *con,
213c99ee
SW
1708 void **buf, int *len, int *proto,
1709 void **reply_buf, int *reply_len, int force_new)
4e7a5dcd
SW
1710{
1711 struct ceph_osd *o = con->private;
1712 struct ceph_osd_client *osdc = o->o_osdc;
1713 struct ceph_auth_client *ac = osdc->client->monc.auth;
1714 int ret = 0;
1715
1716 if (force_new && o->o_authorizer) {
1717 ac->ops->destroy_authorizer(ac, o->o_authorizer);
1718 o->o_authorizer = NULL;
1719 }
1720 if (o->o_authorizer == NULL) {
1721 ret = ac->ops->create_authorizer(
1722 ac, CEPH_ENTITY_TYPE_OSD,
1723 &o->o_authorizer,
1724 &o->o_authorizer_buf,
1725 &o->o_authorizer_buf_len,
1726 &o->o_authorizer_reply_buf,
1727 &o->o_authorizer_reply_buf_len);
1728 if (ret)
213c99ee 1729 return ret;
4e7a5dcd
SW
1730 }
1731
1732 *proto = ac->protocol;
1733 *buf = o->o_authorizer_buf;
1734 *len = o->o_authorizer_buf_len;
1735 *reply_buf = o->o_authorizer_reply_buf;
1736 *reply_len = o->o_authorizer_reply_buf_len;
1737 return 0;
1738}
1739
1740
1741static int verify_authorizer_reply(struct ceph_connection *con, int len)
1742{
1743 struct ceph_osd *o = con->private;
1744 struct ceph_osd_client *osdc = o->o_osdc;
1745 struct ceph_auth_client *ac = osdc->client->monc.auth;
1746
1747 return ac->ops->verify_authorizer_reply(ac, o->o_authorizer, len);
1748}
1749
9bd2e6f8
SW
1750static int invalidate_authorizer(struct ceph_connection *con)
1751{
1752 struct ceph_osd *o = con->private;
1753 struct ceph_osd_client *osdc = o->o_osdc;
1754 struct ceph_auth_client *ac = osdc->client->monc.auth;
1755
1756 if (ac->ops->invalidate_authorizer)
1757 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
1758
1759 return ceph_monc_validate_auth(&osdc->client->monc);
1760}
4e7a5dcd 1761
9e32789f 1762static const struct ceph_connection_operations osd_con_ops = {
f24e9980
SW
1763 .get = get_osd_con,
1764 .put = put_osd_con,
1765 .dispatch = dispatch,
4e7a5dcd
SW
1766 .get_authorizer = get_authorizer,
1767 .verify_authorizer_reply = verify_authorizer_reply,
9bd2e6f8 1768 .invalidate_authorizer = invalidate_authorizer,
f24e9980 1769 .alloc_msg = alloc_msg,
81b024e7 1770 .fault = osd_reset,
f24e9980 1771};