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