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