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