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