blob: 80b868f7a0fc7327f7b977f925f8eab7efe6524a [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);
Sage Weil350b1c32009-12-22 10:45:45 -080090 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 }
Sage Weil415e49a2009-12-07 13:37:03 -080097 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);
Sage Weilf24e9982009-10-06 11:31:10 -0700105}
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 }
Yehuda Sadeh93c20d92009-12-15 09:50:36 -0800155 req->r_num_prealloc_reply = num_reply;
Sage Weilf24e9982009-10-06 11:31:10 -0700156
157 req->r_osdc = osdc;
158 req->r_mempool = use_mempool;
Sage Weil415e49a2009-12-07 13:37:03 -0800159 kref_init(&req->r_kref);
Sage Weilf24e9982009-10-06 11:31:10 -0700160 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)
Sage Weil8f3bc052009-10-14 17:36:07 -0700172 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
Sage Weilf24e9982009-10-06 11:31:10 -0700173 else
174 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, 0, 0, NULL);
175 if (IS_ERR(msg)) {
Yehuda Sadeh93c20d92009-12-15 09:50:36 -0800176 ceph_msgpool_resv(&osdc->msgpool_op_reply, -num_reply);
Sage Weilf24e9982009-10-06 11:31:10 -0700177 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/*
Sage Weil81b024e2009-10-09 10:29:18 -0700305 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -0700306 */
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;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800342
Sage Weilf24e9982009-10-06 11:31:10 -0700343 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);
Sage Weil42ce56e2009-11-18 11:22:36 -0800362 if (atomic_dec_and_test(&osd->o_ref))
Sage Weilf24e9982009-10-06 11:31:10 -0700363 kfree(osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700364}
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{
Sage Weilf24e9982009-10-06 11:31:10 -0700442 mutex_lock(&osdc->request_mutex);
443 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -0800444 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -0700445
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 =
Sage Weil6b805182009-10-27 11:50:50 -0700452 jiffies + osdc->client->mount_args->osd_timeout*HZ;
Sage Weilf24e9982009-10-06 11:31:10 -0700453
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
Yehuda Sadeh93c20d92009-12-15 09:50:36 -0800474 ceph_msgpool_resv(&osdc->msgpool_op_reply, -req->r_num_prealloc_reply);
475
Sage Weil0ba64782009-10-08 16:57:16 -0700476 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 }
Sage Weilf24e9982009-10-06 11:31:10 -0700485
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;
Sage Weil51042122009-11-04 11:39:12 -0800530 struct ceph_pg pgid;
Sage Weilf24e9982009-10-06 11:31:10 -0700531 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;
Sage Weil51042122009-11-04 11:39:12 -0800540 pgid = reqhead->layout.ol_pgid;
Sage Weilf24e9982009-10-06 11:31:10 -0700541 o = ceph_calc_pg_primary(osdc->osdmap, pgid);
542
543 if ((req->r_osd && req->r_osd->o_osd == o &&
544 req->r_sent >= req->r_osd->o_incarnation) ||
545 (req->r_osd == NULL && o == -1))
546 return 0; /* no change */
547
Sage Weil51042122009-11-04 11:39:12 -0800548 dout("map_osds tid %llu pgid %d.%x osd%d (was osd%d)\n",
549 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
Sage Weilf24e9982009-10-06 11:31:10 -0700550 req->r_osd ? req->r_osd->o_osd : -1);
551
552 if (req->r_osd) {
553 __cancel_request(req);
554 list_del_init(&req->r_osd_item);
555 if (list_empty(&req->r_osd->o_requests)) {
556 /* try to re-use r_osd if possible */
557 newosd = get_osd(req->r_osd);
558 remove_osd(osdc, newosd);
559 }
560 req->r_osd = NULL;
561 }
562
563 req->r_osd = __lookup_osd(osdc, o);
564 if (!req->r_osd && o >= 0) {
565 if (newosd) {
566 req->r_osd = newosd;
567 newosd = NULL;
568 } else {
569 err = -ENOMEM;
570 req->r_osd = create_osd(osdc);
571 if (!req->r_osd)
572 goto out;
573 }
574
575 dout("map_osds osd %p is osd%d\n", req->r_osd, o);
576 req->r_osd->o_osd = o;
577 req->r_osd->o_con.peer_name.num = cpu_to_le64(o);
578 __insert_osd(osdc, req->r_osd);
579
580 ceph_con_open(&req->r_osd->o_con, &osdc->osdmap->osd_addr[o]);
581 }
582
583 if (req->r_osd)
584 list_add(&req->r_osd_item, &req->r_osd->o_requests);
585 err = 1; /* osd changed */
586
587out:
588 if (newosd)
589 put_osd(newosd);
590 return err;
591}
592
593/*
594 * caller should hold map_sem (for read) and request_mutex
595 */
596static int __send_request(struct ceph_osd_client *osdc,
597 struct ceph_osd_request *req)
598{
599 struct ceph_osd_request_head *reqhead;
600 int err;
601
602 err = __map_osds(osdc, req);
603 if (err < 0)
604 return err;
605 if (req->r_osd == NULL) {
606 dout("send_request %p no up osds in pg\n", req);
607 ceph_monc_request_next_osdmap(&osdc->client->monc);
608 return 0;
609 }
610
611 dout("send_request %p tid %llu to osd%d flags %d\n",
612 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
613
614 reqhead = req->r_request->front.iov_base;
615 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
616 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
617 reqhead->reassert_version = req->r_reassert_version;
618
Sage Weil6b805182009-10-27 11:50:50 -0700619 req->r_timeout_stamp = jiffies+osdc->client->mount_args->osd_timeout*HZ;
Sage Weilf24e9982009-10-06 11:31:10 -0700620
621 ceph_msg_get(req->r_request); /* send consumes a ref */
622 ceph_con_send(&req->r_osd->o_con, req->r_request);
623 req->r_sent = req->r_osd->o_incarnation;
624 return 0;
625}
626
627/*
628 * Timeout callback, called every N seconds when 1 or more osd
629 * requests has been active for more than N seconds. When this
630 * happens, we ping all OSDs with requests who have timed out to
631 * ensure any communications channel reset is detected. Reset the
632 * request timeouts another N seconds in the future as we go.
633 * Reschedule the timeout event another N seconds in future (unless
634 * there are no open requests).
635 */
636static void handle_timeout(struct work_struct *work)
637{
638 struct ceph_osd_client *osdc =
639 container_of(work, struct ceph_osd_client, timeout_work.work);
640 struct ceph_osd_request *req;
641 struct ceph_osd *osd;
Sage Weil6b805182009-10-27 11:50:50 -0700642 unsigned long timeout = osdc->client->mount_args->osd_timeout * HZ;
Sage Weilf24e9982009-10-06 11:31:10 -0700643 unsigned long next_timeout = timeout + jiffies;
644 struct rb_node *p;
645
646 dout("timeout\n");
647 down_read(&osdc->map_sem);
648
649 ceph_monc_request_next_osdmap(&osdc->client->monc);
650
651 mutex_lock(&osdc->request_mutex);
652 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
653 req = rb_entry(p, struct ceph_osd_request, r_node);
654
655 if (req->r_resend) {
656 int err;
657
658 dout("osdc resending prev failed %lld\n", req->r_tid);
659 err = __send_request(osdc, req);
660 if (err)
661 dout("osdc failed again on %lld\n", req->r_tid);
662 else
663 req->r_resend = false;
664 continue;
665 }
666 }
667 for (p = rb_first(&osdc->osds); p; p = rb_next(p)) {
668 osd = rb_entry(p, struct ceph_osd, o_node);
669 if (list_empty(&osd->o_requests))
670 continue;
671 req = list_first_entry(&osd->o_requests,
672 struct ceph_osd_request, r_osd_item);
673 if (time_before(jiffies, req->r_timeout_stamp))
674 continue;
675
676 dout(" tid %llu (at least) timed out on osd%d\n",
677 req->r_tid, osd->o_osd);
678 req->r_timeout_stamp = next_timeout;
679 ceph_con_keepalive(&osd->o_con);
680 }
681
682 if (osdc->timeout_tid)
683 schedule_delayed_work(&osdc->timeout_work,
684 round_jiffies_relative(timeout));
685
686 mutex_unlock(&osdc->request_mutex);
687
688 up_read(&osdc->map_sem);
689}
690
691/*
692 * handle osd op reply. either call the callback if it is specified,
693 * or do the completion to wake up the waiting thread.
694 */
Sage Weil350b1c32009-12-22 10:45:45 -0800695static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
696 struct ceph_connection *con)
Sage Weilf24e9982009-10-06 11:31:10 -0700697{
698 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
699 struct ceph_osd_request *req;
700 u64 tid;
701 int numops, object_len, flags;
702
Sage Weil6df058c2009-12-22 11:24:33 -0800703 tid = le64_to_cpu(msg->hdr.tid);
Sage Weilf24e9982009-10-06 11:31:10 -0700704 if (msg->front.iov_len < sizeof(*rhead))
705 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -0700706 numops = le32_to_cpu(rhead->num_ops);
707 object_len = le32_to_cpu(rhead->object_len);
708 if (msg->front.iov_len != sizeof(*rhead) + object_len +
709 numops * sizeof(struct ceph_osd_op))
710 goto bad;
711 dout("handle_reply %p tid %llu\n", msg, tid);
712
713 /* lookup */
714 mutex_lock(&osdc->request_mutex);
715 req = __lookup_request(osdc, tid);
716 if (req == NULL) {
717 dout("handle_reply tid %llu dne\n", tid);
718 mutex_unlock(&osdc->request_mutex);
719 return;
720 }
721 ceph_osdc_get_request(req);
722 flags = le32_to_cpu(rhead->flags);
723
Sage Weil350b1c32009-12-22 10:45:45 -0800724 /*
725 * if this connection filled our pages, drop our reference now, to
726 * avoid a (safe but slower) revoke later.
727 */
728 if (req->r_con_filling_pages == con && req->r_pages == msg->pages) {
729 dout(" got pages, dropping con_filling_pages ref %p\n", con);
730 req->r_con_filling_pages = NULL;
731 ceph_con_put(con);
732 }
733
Sage Weilf24e9982009-10-06 11:31:10 -0700734 if (req->r_reply) {
735 /*
736 * once we see the message has been received, we don't
737 * need a ref (which is only needed for revoking
738 * pages)
739 */
740 ceph_msg_put(req->r_reply);
741 req->r_reply = NULL;
742 }
743
744 if (!req->r_got_reply) {
745 unsigned bytes;
746
747 req->r_result = le32_to_cpu(rhead->result);
748 bytes = le32_to_cpu(msg->hdr.data_len);
749 dout("handle_reply result %d bytes %d\n", req->r_result,
750 bytes);
751 if (req->r_result == 0)
752 req->r_result = bytes;
753
754 /* in case this is a write and we need to replay, */
755 req->r_reassert_version = rhead->reassert_version;
756
757 req->r_got_reply = 1;
758 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
759 dout("handle_reply tid %llu dup ack\n", tid);
Sage Weil34b43a52009-12-01 12:23:54 -0800760 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -0700761 goto done;
762 }
763
764 dout("handle_reply tid %llu flags %d\n", tid, flags);
765
766 /* either this is a read, or we got the safe response */
767 if ((flags & CEPH_OSD_FLAG_ONDISK) ||
768 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
769 __unregister_request(osdc, req);
770
771 mutex_unlock(&osdc->request_mutex);
772
773 if (req->r_callback)
774 req->r_callback(req, msg);
775 else
776 complete(&req->r_completion);
777
778 if (flags & CEPH_OSD_FLAG_ONDISK) {
779 if (req->r_safe_callback)
780 req->r_safe_callback(req, msg);
781 complete(&req->r_safe_completion); /* fsync waiter */
782 }
783
784done:
785 ceph_osdc_put_request(req);
786 return;
787
788bad:
789 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
790 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
791 (int)sizeof(*rhead));
Sage Weil9ec7cab2009-12-14 15:13:47 -0800792 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -0700793}
794
795
796/*
797 * Resubmit osd requests whose osd or osd address has changed. Request
798 * a new osd map if osds are down, or we are otherwise unable to determine
799 * how to direct a request.
800 *
801 * Close connections to down osds.
802 *
803 * If @who is specified, resubmit requests for that specific osd.
804 *
805 * Caller should hold map_sem for read and request_mutex.
806 */
807static void kick_requests(struct ceph_osd_client *osdc,
808 struct ceph_osd *kickosd)
809{
810 struct ceph_osd_request *req;
811 struct rb_node *p, *n;
812 int needmap = 0;
813 int err;
814
815 dout("kick_requests osd%d\n", kickosd ? kickosd->o_osd : -1);
816 mutex_lock(&osdc->request_mutex);
817 if (!kickosd) {
818 for (p = rb_first(&osdc->osds); p; p = n) {
819 struct ceph_osd *osd =
820 rb_entry(p, struct ceph_osd, o_node);
821
822 n = rb_next(p);
823 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
Sage Weil103e2d32010-01-07 16:12:36 -0800824 memcmp(&osd->o_con.peer_addr,
825 ceph_osd_addr(osdc->osdmap,
826 osd->o_osd),
827 sizeof(struct ceph_entity_addr)) != 0)
Sage Weilf24e9982009-10-06 11:31:10 -0700828 reset_osd(osdc, osd);
829 }
830 }
831
832 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
833 req = rb_entry(p, struct ceph_osd_request, r_node);
834
835 if (req->r_resend) {
836 dout(" r_resend set on tid %llu\n", req->r_tid);
Sage Weil266673d2009-10-09 10:31:32 -0700837 __cancel_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700838 goto kick;
839 }
Sage Weil266673d2009-10-09 10:31:32 -0700840 if (req->r_osd && kickosd == req->r_osd) {
841 __cancel_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700842 goto kick;
Sage Weil266673d2009-10-09 10:31:32 -0700843 }
Sage Weilf24e9982009-10-06 11:31:10 -0700844
845 err = __map_osds(osdc, req);
846 if (err == 0)
847 continue; /* no change */
848 if (err < 0) {
849 /*
850 * FIXME: really, we should set the request
851 * error and fail if this isn't a 'nofail'
852 * request, but that's a fair bit more
853 * complicated to do. So retry!
854 */
855 dout(" setting r_resend on %llu\n", req->r_tid);
856 req->r_resend = true;
857 continue;
858 }
859 if (req->r_osd == NULL) {
860 dout("tid %llu maps to no valid osd\n", req->r_tid);
861 needmap++; /* request a newer map */
862 continue;
863 }
864
865kick:
Sage Weilc1ea8822009-10-08 16:55:47 -0700866 dout("kicking %p tid %llu osd%d\n", req, req->r_tid,
867 req->r_osd->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700868 req->r_flags |= CEPH_OSD_FLAG_RETRY;
869 err = __send_request(osdc, req);
870 if (err) {
871 dout(" setting r_resend on %llu\n", req->r_tid);
872 req->r_resend = true;
873 }
874 }
875 mutex_unlock(&osdc->request_mutex);
876
877 if (needmap) {
878 dout("%d requests for down osds, need new map\n", needmap);
879 ceph_monc_request_next_osdmap(&osdc->client->monc);
880 }
881}
882
883/*
884 * Process updated osd map.
885 *
886 * The message contains any number of incremental and full maps, normally
887 * indicating some sort of topology change in the cluster. Kick requests
888 * off to different OSDs as needed.
889 */
890void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
891{
892 void *p, *end, *next;
893 u32 nr_maps, maplen;
894 u32 epoch;
895 struct ceph_osdmap *newmap = NULL, *oldmap;
896 int err;
897 struct ceph_fsid fsid;
898
899 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
900 p = msg->front.iov_base;
901 end = p + msg->front.iov_len;
902
903 /* verify fsid */
904 ceph_decode_need(&p, end, sizeof(fsid), bad);
905 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -0800906 if (ceph_check_fsid(osdc->client, &fsid) < 0)
907 return;
Sage Weilf24e9982009-10-06 11:31:10 -0700908
909 down_write(&osdc->map_sem);
910
911 /* incremental maps */
912 ceph_decode_32_safe(&p, end, nr_maps, bad);
913 dout(" %d inc maps\n", nr_maps);
914 while (nr_maps > 0) {
915 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700916 epoch = ceph_decode_32(&p);
917 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -0700918 ceph_decode_need(&p, end, maplen, bad);
919 next = p + maplen;
920 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
921 dout("applying incremental map %u len %d\n",
922 epoch, maplen);
923 newmap = osdmap_apply_incremental(&p, next,
924 osdc->osdmap,
925 osdc->client->msgr);
926 if (IS_ERR(newmap)) {
927 err = PTR_ERR(newmap);
928 goto bad;
929 }
Sage Weil30dc6382009-12-21 14:49:37 -0800930 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -0700931 if (newmap != osdc->osdmap) {
932 ceph_osdmap_destroy(osdc->osdmap);
933 osdc->osdmap = newmap;
934 }
935 } else {
936 dout("ignoring incremental map %u len %d\n",
937 epoch, maplen);
938 }
939 p = next;
940 nr_maps--;
941 }
942 if (newmap)
943 goto done;
944
945 /* full maps */
946 ceph_decode_32_safe(&p, end, nr_maps, bad);
947 dout(" %d full maps\n", nr_maps);
948 while (nr_maps) {
949 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700950 epoch = ceph_decode_32(&p);
951 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -0700952 ceph_decode_need(&p, end, maplen, bad);
953 if (nr_maps > 1) {
954 dout("skipping non-latest full map %u len %d\n",
955 epoch, maplen);
956 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
957 dout("skipping full map %u len %d, "
958 "older than our %u\n", epoch, maplen,
959 osdc->osdmap->epoch);
960 } else {
961 dout("taking full map %u len %d\n", epoch, maplen);
962 newmap = osdmap_decode(&p, p+maplen);
963 if (IS_ERR(newmap)) {
964 err = PTR_ERR(newmap);
965 goto bad;
966 }
Sage Weil30dc6382009-12-21 14:49:37 -0800967 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -0700968 oldmap = osdc->osdmap;
969 osdc->osdmap = newmap;
970 if (oldmap)
971 ceph_osdmap_destroy(oldmap);
972 }
973 p += maplen;
974 nr_maps--;
975 }
976
977done:
978 downgrade_write(&osdc->map_sem);
979 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
980 if (newmap)
981 kick_requests(osdc, NULL);
982 up_read(&osdc->map_sem);
983 return;
984
985bad:
986 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -0800987 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -0700988 up_write(&osdc->map_sem);
989 return;
990}
991
992
993/*
994 * A read request prepares specific pages that data is to be read into.
995 * When a message is being read off the wire, we call prepare_pages to
996 * find those pages.
997 * 0 = success, -1 failure.
998 */
999static int prepare_pages(struct ceph_connection *con, struct ceph_msg *m,
1000 int want)
1001{
1002 struct ceph_osd *osd = con->private;
1003 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001004 struct ceph_osd_request *req;
1005 u64 tid;
1006 int ret = -1;
1007 int type = le16_to_cpu(m->hdr.type);
1008
1009 if (!osd)
1010 return -1;
1011 osdc = osd->o_osdc;
1012
1013 dout("prepare_pages on msg %p want %d\n", m, want);
1014 if (unlikely(type != CEPH_MSG_OSD_OPREPLY))
1015 return -1; /* hmm! */
1016
Sage Weil6df058c2009-12-22 11:24:33 -08001017 tid = le64_to_cpu(m->hdr.tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001018 mutex_lock(&osdc->request_mutex);
1019 req = __lookup_request(osdc, tid);
1020 if (!req) {
1021 dout("prepare_pages unknown tid %llu\n", tid);
1022 goto out;
1023 }
1024 dout("prepare_pages tid %llu has %d pages, want %d\n",
1025 tid, req->r_num_pages, want);
Sage Weil350b1c32009-12-22 10:45:45 -08001026 if (unlikely(req->r_num_pages < want))
1027 goto out;
1028
1029 if (req->r_con_filling_pages) {
1030 dout("revoking pages %p from old con %p\n", req->r_pages,
1031 req->r_con_filling_pages);
1032 ceph_con_revoke_pages(req->r_con_filling_pages, req->r_pages);
1033 ceph_con_put(req->r_con_filling_pages);
Sage Weilf24e9982009-10-06 11:31:10 -07001034 }
Sage Weil350b1c32009-12-22 10:45:45 -08001035 req->r_con_filling_pages = ceph_con_get(con);
1036 req->r_reply = ceph_msg_get(m); /* for duration of read over socket */
1037 m->pages = req->r_pages;
1038 m->nr_pages = req->r_num_pages;
1039 ret = 0; /* success */
Sage Weilf24e9982009-10-06 11:31:10 -07001040out:
1041 mutex_unlock(&osdc->request_mutex);
1042 return ret;
1043}
1044
1045/*
1046 * Register request, send initial attempt.
1047 */
1048int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1049 struct ceph_osd_request *req,
1050 bool nofail)
1051{
Sage Weilc1ea8822009-10-08 16:55:47 -07001052 int rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001053
1054 req->r_request->pages = req->r_pages;
1055 req->r_request->nr_pages = req->r_num_pages;
1056
1057 register_request(osdc, req);
1058
1059 down_read(&osdc->map_sem);
1060 mutex_lock(&osdc->request_mutex);
Sage Weilc1ea8822009-10-08 16:55:47 -07001061 /*
1062 * a racing kick_requests() may have sent the message for us
1063 * while we dropped request_mutex above, so only send now if
1064 * the request still han't been touched yet.
1065 */
1066 if (req->r_sent == 0) {
1067 rc = __send_request(osdc, req);
1068 if (rc) {
1069 if (nofail) {
1070 dout("osdc_start_request failed send, "
1071 " marking %lld\n", req->r_tid);
1072 req->r_resend = true;
1073 rc = 0;
1074 } else {
1075 __unregister_request(osdc, req);
1076 }
Sage Weilf24e9982009-10-06 11:31:10 -07001077 }
1078 }
1079 mutex_unlock(&osdc->request_mutex);
1080 up_read(&osdc->map_sem);
1081 return rc;
1082}
1083
1084/*
1085 * wait for a request to complete
1086 */
1087int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1088 struct ceph_osd_request *req)
1089{
1090 int rc;
1091
1092 rc = wait_for_completion_interruptible(&req->r_completion);
1093 if (rc < 0) {
1094 mutex_lock(&osdc->request_mutex);
1095 __cancel_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001096 __unregister_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001097 mutex_unlock(&osdc->request_mutex);
Sage Weil529cfcc2009-12-22 10:29:39 -08001098 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001099 return rc;
1100 }
1101
1102 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1103 return req->r_result;
1104}
1105
1106/*
1107 * sync - wait for all in-flight requests to flush. avoid starvation.
1108 */
1109void ceph_osdc_sync(struct ceph_osd_client *osdc)
1110{
1111 struct ceph_osd_request *req;
1112 u64 last_tid, next_tid = 0;
1113
1114 mutex_lock(&osdc->request_mutex);
1115 last_tid = osdc->last_tid;
1116 while (1) {
1117 req = __lookup_request_ge(osdc, next_tid);
1118 if (!req)
1119 break;
1120 if (req->r_tid > last_tid)
1121 break;
1122
1123 next_tid = req->r_tid + 1;
1124 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1125 continue;
1126
1127 ceph_osdc_get_request(req);
1128 mutex_unlock(&osdc->request_mutex);
1129 dout("sync waiting on tid %llu (last is %llu)\n",
1130 req->r_tid, last_tid);
1131 wait_for_completion(&req->r_safe_completion);
1132 mutex_lock(&osdc->request_mutex);
1133 ceph_osdc_put_request(req);
1134 }
1135 mutex_unlock(&osdc->request_mutex);
1136 dout("sync done (thru tid %llu)\n", last_tid);
1137}
1138
1139/*
1140 * init, shutdown
1141 */
1142int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1143{
1144 int err;
1145
1146 dout("init\n");
1147 osdc->client = client;
1148 osdc->osdmap = NULL;
1149 init_rwsem(&osdc->map_sem);
1150 init_completion(&osdc->map_waiters);
1151 osdc->last_requested_map = 0;
1152 mutex_init(&osdc->request_mutex);
1153 osdc->timeout_tid = 0;
1154 osdc->last_tid = 0;
1155 osdc->osds = RB_ROOT;
1156 osdc->requests = RB_ROOT;
1157 osdc->num_requests = 0;
1158 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
1159
Sage Weil5f44f142009-11-18 14:52:18 -08001160 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001161 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1162 sizeof(struct ceph_osd_request));
1163 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08001164 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001165
1166 err = ceph_msgpool_init(&osdc->msgpool_op, 4096, 10, true);
1167 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08001168 goto out_mempool;
Sage Weilf24e9982009-10-06 11:31:10 -07001169 err = ceph_msgpool_init(&osdc->msgpool_op_reply, 512, 0, false);
1170 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08001171 goto out_msgpool;
Sage Weilf24e9982009-10-06 11:31:10 -07001172 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08001173
1174out_msgpool:
1175 ceph_msgpool_destroy(&osdc->msgpool_op);
1176out_mempool:
1177 mempool_destroy(osdc->req_mempool);
1178out:
1179 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07001180}
1181
1182void ceph_osdc_stop(struct ceph_osd_client *osdc)
1183{
1184 cancel_delayed_work_sync(&osdc->timeout_work);
1185 if (osdc->osdmap) {
1186 ceph_osdmap_destroy(osdc->osdmap);
1187 osdc->osdmap = NULL;
1188 }
1189 mempool_destroy(osdc->req_mempool);
1190 ceph_msgpool_destroy(&osdc->msgpool_op);
1191 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
1192}
1193
1194/*
1195 * Read some contiguous pages. If we cross a stripe boundary, shorten
1196 * *plen. Return number of bytes read, or error.
1197 */
1198int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1199 struct ceph_vino vino, struct ceph_file_layout *layout,
1200 u64 off, u64 *plen,
1201 u32 truncate_seq, u64 truncate_size,
1202 struct page **pages, int num_pages)
1203{
1204 struct ceph_osd_request *req;
1205 int rc = 0;
1206
1207 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1208 vino.snap, off, *plen);
1209 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1210 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1211 NULL, 0, truncate_seq, truncate_size, NULL,
1212 false, 1);
1213 if (IS_ERR(req))
1214 return PTR_ERR(req);
1215
1216 /* it may be a short read due to an object boundary */
1217 req->r_pages = pages;
1218 num_pages = calc_pages_for(off, *plen);
1219 req->r_num_pages = num_pages;
1220
1221 dout("readpages final extent is %llu~%llu (%d pages)\n",
1222 off, *plen, req->r_num_pages);
1223
1224 rc = ceph_osdc_start_request(osdc, req, false);
1225 if (!rc)
1226 rc = ceph_osdc_wait_request(osdc, req);
1227
1228 ceph_osdc_put_request(req);
1229 dout("readpages result %d\n", rc);
1230 return rc;
1231}
1232
1233/*
1234 * do a synchronous write on N pages
1235 */
1236int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1237 struct ceph_file_layout *layout,
1238 struct ceph_snap_context *snapc,
1239 u64 off, u64 len,
1240 u32 truncate_seq, u64 truncate_size,
1241 struct timespec *mtime,
1242 struct page **pages, int num_pages,
1243 int flags, int do_sync, bool nofail)
1244{
1245 struct ceph_osd_request *req;
1246 int rc = 0;
1247
1248 BUG_ON(vino.snap != CEPH_NOSNAP);
1249 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1250 CEPH_OSD_OP_WRITE,
1251 flags | CEPH_OSD_FLAG_ONDISK |
1252 CEPH_OSD_FLAG_WRITE,
1253 snapc, do_sync,
1254 truncate_seq, truncate_size, mtime,
1255 nofail, 1);
1256 if (IS_ERR(req))
1257 return PTR_ERR(req);
1258
1259 /* it may be a short write due to an object boundary */
1260 req->r_pages = pages;
1261 req->r_num_pages = calc_pages_for(off, len);
1262 dout("writepages %llu~%llu (%d pages)\n", off, len,
1263 req->r_num_pages);
1264
1265 rc = ceph_osdc_start_request(osdc, req, nofail);
1266 if (!rc)
1267 rc = ceph_osdc_wait_request(osdc, req);
1268
1269 ceph_osdc_put_request(req);
1270 if (rc == 0)
1271 rc = len;
1272 dout("writepages result %d\n", rc);
1273 return rc;
1274}
1275
1276/*
1277 * handle incoming message
1278 */
1279static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1280{
1281 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01001282 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001283 int type = le16_to_cpu(msg->hdr.type);
1284
1285 if (!osd)
1286 return;
Julia Lawall32c895e2009-11-21 16:53:16 +01001287 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001288
1289 switch (type) {
1290 case CEPH_MSG_OSD_MAP:
1291 ceph_osdc_handle_map(osdc, msg);
1292 break;
1293 case CEPH_MSG_OSD_OPREPLY:
Sage Weil350b1c32009-12-22 10:45:45 -08001294 handle_reply(osdc, msg, con);
Sage Weilf24e9982009-10-06 11:31:10 -07001295 break;
1296
1297 default:
1298 pr_err("received unknown message type %d %s\n", type,
1299 ceph_msg_type_name(type));
1300 }
1301 ceph_msg_put(msg);
1302}
1303
1304static struct ceph_msg *alloc_msg(struct ceph_connection *con,
1305 struct ceph_msg_header *hdr)
1306{
1307 struct ceph_osd *osd = con->private;
1308 struct ceph_osd_client *osdc = osd->o_osdc;
1309 int type = le16_to_cpu(hdr->type);
Sage Weil8f3bc052009-10-14 17:36:07 -07001310 int front = le32_to_cpu(hdr->front_len);
Sage Weilf24e9982009-10-06 11:31:10 -07001311
1312 switch (type) {
1313 case CEPH_MSG_OSD_OPREPLY:
Sage Weil8f3bc052009-10-14 17:36:07 -07001314 return ceph_msgpool_get(&osdc->msgpool_op_reply, front);
Sage Weilf24e9982009-10-06 11:31:10 -07001315 }
1316 return ceph_alloc_msg(con, hdr);
1317}
1318
1319/*
1320 * Wrappers to refcount containing ceph_osd struct
1321 */
1322static struct ceph_connection *get_osd_con(struct ceph_connection *con)
1323{
1324 struct ceph_osd *osd = con->private;
1325 if (get_osd(osd))
1326 return con;
1327 return NULL;
1328}
1329
1330static void put_osd_con(struct ceph_connection *con)
1331{
1332 struct ceph_osd *osd = con->private;
1333 put_osd(osd);
1334}
1335
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001336/*
1337 * authentication
1338 */
1339static int get_authorizer(struct ceph_connection *con,
Sage Weil50b885b2009-12-01 14:12:07 -08001340 void **buf, int *len, int *proto,
1341 void **reply_buf, int *reply_len, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001342{
1343 struct ceph_osd *o = con->private;
1344 struct ceph_osd_client *osdc = o->o_osdc;
1345 struct ceph_auth_client *ac = osdc->client->monc.auth;
1346 int ret = 0;
1347
1348 if (force_new && o->o_authorizer) {
1349 ac->ops->destroy_authorizer(ac, o->o_authorizer);
1350 o->o_authorizer = NULL;
1351 }
1352 if (o->o_authorizer == NULL) {
1353 ret = ac->ops->create_authorizer(
1354 ac, CEPH_ENTITY_TYPE_OSD,
1355 &o->o_authorizer,
1356 &o->o_authorizer_buf,
1357 &o->o_authorizer_buf_len,
1358 &o->o_authorizer_reply_buf,
1359 &o->o_authorizer_reply_buf_len);
1360 if (ret)
1361 return ret;
1362 }
1363
1364 *proto = ac->protocol;
1365 *buf = o->o_authorizer_buf;
1366 *len = o->o_authorizer_buf_len;
1367 *reply_buf = o->o_authorizer_reply_buf;
1368 *reply_len = o->o_authorizer_reply_buf_len;
1369 return 0;
1370}
1371
1372
1373static int verify_authorizer_reply(struct ceph_connection *con, int len)
1374{
1375 struct ceph_osd *o = con->private;
1376 struct ceph_osd_client *osdc = o->o_osdc;
1377 struct ceph_auth_client *ac = osdc->client->monc.auth;
1378
1379 return ac->ops->verify_authorizer_reply(ac, o->o_authorizer, len);
1380}
1381
1382
Sage Weilf24e9982009-10-06 11:31:10 -07001383const static struct ceph_connection_operations osd_con_ops = {
1384 .get = get_osd_con,
1385 .put = put_osd_con,
1386 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001387 .get_authorizer = get_authorizer,
1388 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weilf24e9982009-10-06 11:31:10 -07001389 .alloc_msg = alloc_msg,
Sage Weil81b024e2009-10-09 10:29:18 -07001390 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07001391 .alloc_middle = ceph_alloc_middle,
1392 .prepare_pages = prepare_pages,
1393};