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