libceph: remove dead code in osd_req_encode_op()
[firefly-linux-kernel-4.4.55.git] / net / ceph / osd_client.c
1 #include <linux/ceph/ceph_debug.h>
2
3 #include <linux/module.h>
4 #include <linux/err.h>
5 #include <linux/highmem.h>
6 #include <linux/mm.h>
7 #include <linux/pagemap.h>
8 #include <linux/slab.h>
9 #include <linux/uaccess.h>
10 #ifdef CONFIG_BLOCK
11 #include <linux/bio.h>
12 #endif
13
14 #include <linux/ceph/libceph.h>
15 #include <linux/ceph/osd_client.h>
16 #include <linux/ceph/messenger.h>
17 #include <linux/ceph/decode.h>
18 #include <linux/ceph/auth.h>
19 #include <linux/ceph/pagelist.h>
20
21 #define OSD_OP_FRONT_LEN        4096
22 #define OSD_OPREPLY_FRONT_LEN   512
23
24 static const struct ceph_connection_operations osd_con_ops;
25
26 static void __send_queued(struct ceph_osd_client *osdc);
27 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
28 static void __register_request(struct ceph_osd_client *osdc,
29                                struct ceph_osd_request *req);
30 static void __unregister_linger_request(struct ceph_osd_client *osdc,
31                                         struct ceph_osd_request *req);
32 static void __send_request(struct ceph_osd_client *osdc,
33                            struct ceph_osd_request *req);
34
35 static int op_has_extent(int op)
36 {
37         return (op == CEPH_OSD_OP_READ ||
38                 op == CEPH_OSD_OP_WRITE);
39 }
40
41 /*
42  * Implement client access to distributed object storage cluster.
43  *
44  * All data objects are stored within a cluster/cloud of OSDs, or
45  * "object storage devices."  (Note that Ceph OSDs have _nothing_ to
46  * do with the T10 OSD extensions to SCSI.)  Ceph OSDs are simply
47  * remote daemons serving up and coordinating consistent and safe
48  * access to storage.
49  *
50  * Cluster membership and the mapping of data objects onto storage devices
51  * are described by the osd map.
52  *
53  * We keep track of pending OSD requests (read, write), resubmit
54  * requests to different OSDs when the cluster topology/data layout
55  * change, or retry the affected requests when the communications
56  * channel with an OSD is reset.
57  */
58
59 /*
60  * calculate the mapping of a file extent onto an object, and fill out the
61  * request accordingly.  shorten extent as necessary if it crosses an
62  * object boundary.
63  *
64  * fill osd op in request message.
65  */
66 static int calc_layout(struct ceph_vino vino,
67                        struct ceph_file_layout *layout,
68                        u64 off, u64 *plen,
69                        struct ceph_osd_request *req,
70                        struct ceph_osd_req_op *op)
71 {
72         u64 orig_len = *plen;
73         u64 bno = 0;
74         u64 objoff = 0;
75         u64 objlen = 0;
76         int r;
77
78         /* object extent? */
79         r = ceph_calc_file_object_mapping(layout, off, orig_len, &bno,
80                                           &objoff, &objlen);
81         if (r < 0)
82                 return r;
83         if (objlen < orig_len) {
84                 *plen = objlen;
85                 dout(" skipping last %llu, final file extent %llu~%llu\n",
86                      orig_len - *plen, off, *plen);
87         }
88
89         if (op_has_extent(op->op)) {
90                 u32 osize = le32_to_cpu(layout->fl_object_size);
91                 op->extent.offset = objoff;
92                 op->extent.length = objlen;
93                 if (op->extent.truncate_size <= off - objoff) {
94                         op->extent.truncate_size = 0;
95                 } else {
96                         op->extent.truncate_size -= off - objoff;
97                         if (op->extent.truncate_size > osize)
98                                 op->extent.truncate_size = osize;
99                 }
100         }
101         req->r_num_pages = calc_pages_for(off, *plen);
102         req->r_page_alignment = off & ~PAGE_MASK;
103         if (op->op == CEPH_OSD_OP_WRITE)
104                 op->payload_len = *plen;
105
106         dout("calc_layout bno=%llx %llu~%llu (%d pages)\n",
107              bno, objoff, objlen, req->r_num_pages);
108
109         snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx", vino.ino, bno);
110         req->r_oid_len = strlen(req->r_oid);
111
112         return r;
113 }
114
115 /*
116  * requests
117  */
118 void ceph_osdc_release_request(struct kref *kref)
119 {
120         struct ceph_osd_request *req = container_of(kref,
121                                                     struct ceph_osd_request,
122                                                     r_kref);
123
124         if (req->r_request)
125                 ceph_msg_put(req->r_request);
126         if (req->r_con_filling_msg) {
127                 dout("%s revoking msg %p from con %p\n", __func__,
128                      req->r_reply, req->r_con_filling_msg);
129                 ceph_msg_revoke_incoming(req->r_reply);
130                 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
131                 req->r_con_filling_msg = NULL;
132         }
133         if (req->r_reply)
134                 ceph_msg_put(req->r_reply);
135         if (req->r_own_pages)
136                 ceph_release_page_vector(req->r_pages,
137                                          req->r_num_pages);
138         ceph_put_snap_context(req->r_snapc);
139         ceph_pagelist_release(&req->r_trail);
140         if (req->r_mempool)
141                 mempool_free(req, req->r_osdc->req_mempool);
142         else
143                 kfree(req);
144 }
145 EXPORT_SYMBOL(ceph_osdc_release_request);
146
147 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
148                                                struct ceph_snap_context *snapc,
149                                                unsigned int num_op,
150                                                bool use_mempool,
151                                                gfp_t gfp_flags)
152 {
153         struct ceph_osd_request *req;
154         struct ceph_msg *msg;
155         size_t msg_size = sizeof(struct ceph_osd_request_head);
156
157         msg_size += num_op*sizeof(struct ceph_osd_op);
158
159         if (use_mempool) {
160                 req = mempool_alloc(osdc->req_mempool, gfp_flags);
161                 memset(req, 0, sizeof(*req));
162         } else {
163                 req = kzalloc(sizeof(*req), gfp_flags);
164         }
165         if (req == NULL)
166                 return NULL;
167
168         req->r_osdc = osdc;
169         req->r_mempool = use_mempool;
170
171         kref_init(&req->r_kref);
172         init_completion(&req->r_completion);
173         init_completion(&req->r_safe_completion);
174         RB_CLEAR_NODE(&req->r_node);
175         INIT_LIST_HEAD(&req->r_unsafe_item);
176         INIT_LIST_HEAD(&req->r_linger_item);
177         INIT_LIST_HEAD(&req->r_linger_osd);
178         INIT_LIST_HEAD(&req->r_req_lru_item);
179         INIT_LIST_HEAD(&req->r_osd_item);
180
181         /* create reply message */
182         if (use_mempool)
183                 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
184         else
185                 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
186                                    OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
187         if (!msg) {
188                 ceph_osdc_put_request(req);
189                 return NULL;
190         }
191         req->r_reply = msg;
192
193         ceph_pagelist_init(&req->r_trail);
194
195         /* create request message; allow space for oid */
196         msg_size += MAX_OBJ_NAME_SIZE;
197         if (snapc)
198                 msg_size += sizeof(u64) * snapc->num_snaps;
199         if (use_mempool)
200                 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
201         else
202                 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
203         if (!msg) {
204                 ceph_osdc_put_request(req);
205                 return NULL;
206         }
207
208         memset(msg->front.iov_base, 0, msg->front.iov_len);
209
210         req->r_request = msg;
211
212         return req;
213 }
214 EXPORT_SYMBOL(ceph_osdc_alloc_request);
215
216 static void osd_req_encode_op(struct ceph_osd_request *req,
217                               struct ceph_osd_op *dst,
218                               struct ceph_osd_req_op *src)
219 {
220         dst->op = cpu_to_le16(src->op);
221
222         switch (src->op) {
223         case CEPH_OSD_OP_READ:
224         case CEPH_OSD_OP_WRITE:
225                 dst->extent.offset =
226                         cpu_to_le64(src->extent.offset);
227                 dst->extent.length =
228                         cpu_to_le64(src->extent.length);
229                 dst->extent.truncate_size =
230                         cpu_to_le64(src->extent.truncate_size);
231                 dst->extent.truncate_seq =
232                         cpu_to_le32(src->extent.truncate_seq);
233                 break;
234         case CEPH_OSD_OP_CALL:
235                 dst->cls.class_len = src->cls.class_len;
236                 dst->cls.method_len = src->cls.method_len;
237                 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
238
239                 ceph_pagelist_append(&req->r_trail, src->cls.class_name,
240                                      src->cls.class_len);
241                 ceph_pagelist_append(&req->r_trail, src->cls.method_name,
242                                      src->cls.method_len);
243                 ceph_pagelist_append(&req->r_trail, src->cls.indata,
244                                      src->cls.indata_len);
245                 break;
246         case CEPH_OSD_OP_STARTSYNC:
247                 break;
248         case CEPH_OSD_OP_NOTIFY_ACK:
249         case CEPH_OSD_OP_WATCH:
250                 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
251                 dst->watch.ver = cpu_to_le64(src->watch.ver);
252                 dst->watch.flag = src->watch.flag;
253                 break;
254         default:
255                 pr_err("unrecognized osd opcode %d\n", dst->op);
256                 WARN_ON(1);
257                 break;
258         case CEPH_OSD_OP_STAT:
259         case CEPH_OSD_OP_MAPEXT:
260         case CEPH_OSD_OP_MASKTRUNC:
261         case CEPH_OSD_OP_SPARSE_READ:
262         case CEPH_OSD_OP_NOTIFY:
263         case CEPH_OSD_OP_ASSERT_VER:
264         case CEPH_OSD_OP_WRITEFULL:
265         case CEPH_OSD_OP_TRUNCATE:
266         case CEPH_OSD_OP_ZERO:
267         case CEPH_OSD_OP_DELETE:
268         case CEPH_OSD_OP_APPEND:
269         case CEPH_OSD_OP_SETTRUNC:
270         case CEPH_OSD_OP_TRIMTRUNC:
271         case CEPH_OSD_OP_TMAPUP:
272         case CEPH_OSD_OP_TMAPPUT:
273         case CEPH_OSD_OP_TMAPGET:
274         case CEPH_OSD_OP_CREATE:
275         case CEPH_OSD_OP_ROLLBACK:
276         case CEPH_OSD_OP_OMAPGETKEYS:
277         case CEPH_OSD_OP_OMAPGETVALS:
278         case CEPH_OSD_OP_OMAPGETHEADER:
279         case CEPH_OSD_OP_OMAPGETVALSBYKEYS:
280         case CEPH_OSD_OP_MODE_RD:
281         case CEPH_OSD_OP_OMAPSETVALS:
282         case CEPH_OSD_OP_OMAPSETHEADER:
283         case CEPH_OSD_OP_OMAPCLEAR:
284         case CEPH_OSD_OP_OMAPRMKEYS:
285         case CEPH_OSD_OP_OMAP_CMP:
286         case CEPH_OSD_OP_CLONERANGE:
287         case CEPH_OSD_OP_ASSERT_SRC_VERSION:
288         case CEPH_OSD_OP_SRC_CMPXATTR:
289         case CEPH_OSD_OP_GETXATTR:
290         case CEPH_OSD_OP_GETXATTRS:
291         case CEPH_OSD_OP_CMPXATTR:
292         case CEPH_OSD_OP_SETXATTR:
293         case CEPH_OSD_OP_SETXATTRS:
294         case CEPH_OSD_OP_RESETXATTRS:
295         case CEPH_OSD_OP_RMXATTR:
296         case CEPH_OSD_OP_PULL:
297         case CEPH_OSD_OP_PUSH:
298         case CEPH_OSD_OP_BALANCEREADS:
299         case CEPH_OSD_OP_UNBALANCEREADS:
300         case CEPH_OSD_OP_SCRUB:
301         case CEPH_OSD_OP_SCRUB_RESERVE:
302         case CEPH_OSD_OP_SCRUB_UNRESERVE:
303         case CEPH_OSD_OP_SCRUB_STOP:
304         case CEPH_OSD_OP_SCRUB_MAP:
305         case CEPH_OSD_OP_WRLOCK:
306         case CEPH_OSD_OP_WRUNLOCK:
307         case CEPH_OSD_OP_RDLOCK:
308         case CEPH_OSD_OP_RDUNLOCK:
309         case CEPH_OSD_OP_UPLOCK:
310         case CEPH_OSD_OP_DNLOCK:
311         case CEPH_OSD_OP_PGLS:
312         case CEPH_OSD_OP_PGLS_FILTER:
313                 pr_err("unsupported osd opcode %s\n",
314                         ceph_osd_op_name(dst->op));
315                 WARN_ON(1);
316                 break;
317         }
318         dst->payload_len = cpu_to_le32(src->payload_len);
319 }
320
321 /*
322  * build new request AND message
323  *
324  */
325 void ceph_osdc_build_request(struct ceph_osd_request *req,
326                              u64 off, u64 len, unsigned int num_op,
327                              struct ceph_osd_req_op *src_ops,
328                              struct ceph_snap_context *snapc, u64 snap_id,
329                              struct timespec *mtime)
330 {
331         struct ceph_msg *msg = req->r_request;
332         struct ceph_osd_request_head *head;
333         struct ceph_osd_req_op *src_op;
334         struct ceph_osd_op *op;
335         void *p;
336         size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
337         int flags = req->r_flags;
338         u64 data_len = 0;
339         int i;
340
341         WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
342
343         head = msg->front.iov_base;
344         head->snapid = cpu_to_le64(snap_id);
345         op = (void *)(head + 1);
346         p = (void *)(op + num_op);
347
348         req->r_snapc = ceph_get_snap_context(snapc);
349
350         head->client_inc = cpu_to_le32(1); /* always, for now. */
351         head->flags = cpu_to_le32(flags);
352         if (flags & CEPH_OSD_FLAG_WRITE)
353                 ceph_encode_timespec(&head->mtime, mtime);
354         BUG_ON(num_op > (unsigned int) ((u16) -1));
355         head->num_ops = cpu_to_le16(num_op);
356
357         /* fill in oid */
358         head->object_len = cpu_to_le32(req->r_oid_len);
359         memcpy(p, req->r_oid, req->r_oid_len);
360         p += req->r_oid_len;
361
362         src_op = src_ops;
363         while (num_op--)
364                 osd_req_encode_op(req, op++, src_op++);
365
366         data_len += req->r_trail.length;
367
368         if (snapc) {
369                 head->snap_seq = cpu_to_le64(snapc->seq);
370                 head->num_snaps = cpu_to_le32(snapc->num_snaps);
371                 for (i = 0; i < snapc->num_snaps; i++) {
372                         put_unaligned_le64(snapc->snaps[i], p);
373                         p += sizeof(u64);
374                 }
375         }
376
377         if (flags & CEPH_OSD_FLAG_WRITE) {
378                 req->r_request->hdr.data_off = cpu_to_le16(off);
379                 req->r_request->hdr.data_len = cpu_to_le32(len + data_len);
380         } else if (data_len) {
381                 req->r_request->hdr.data_off = 0;
382                 req->r_request->hdr.data_len = cpu_to_le32(data_len);
383         }
384
385         req->r_request->page_alignment = req->r_page_alignment;
386
387         BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
388         msg_size = p - msg->front.iov_base;
389         msg->front.iov_len = msg_size;
390         msg->hdr.front_len = cpu_to_le32(msg_size);
391         return;
392 }
393 EXPORT_SYMBOL(ceph_osdc_build_request);
394
395 /*
396  * build new request AND message, calculate layout, and adjust file
397  * extent as needed.
398  *
399  * if the file was recently truncated, we include information about its
400  * old and new size so that the object can be updated appropriately.  (we
401  * avoid synchronously deleting truncated objects because it's slow.)
402  *
403  * if @do_sync, include a 'startsync' command so that the osd will flush
404  * data quickly.
405  */
406 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
407                                                struct ceph_file_layout *layout,
408                                                struct ceph_vino vino,
409                                                u64 off, u64 *plen,
410                                                int opcode, int flags,
411                                                struct ceph_snap_context *snapc,
412                                                int do_sync,
413                                                u32 truncate_seq,
414                                                u64 truncate_size,
415                                                struct timespec *mtime,
416                                                bool use_mempool,
417                                                int page_align)
418 {
419         struct ceph_osd_req_op ops[2];
420         struct ceph_osd_request *req;
421         unsigned int num_op = 1;
422         int r;
423
424         memset(&ops, 0, sizeof ops);
425
426         ops[0].op = opcode;
427         ops[0].extent.truncate_seq = truncate_seq;
428         ops[0].extent.truncate_size = truncate_size;
429
430         if (do_sync) {
431                 ops[1].op = CEPH_OSD_OP_STARTSYNC;
432                 num_op++;
433         }
434
435         req = ceph_osdc_alloc_request(osdc, snapc, num_op, use_mempool,
436                                         GFP_NOFS);
437         if (!req)
438                 return ERR_PTR(-ENOMEM);
439         req->r_flags = flags;
440
441         /* calculate max write size */
442         r = calc_layout(vino, layout, off, plen, req, ops);
443         if (r < 0)
444                 return ERR_PTR(r);
445         req->r_file_layout = *layout;  /* keep a copy */
446
447         /* in case it differs from natural (file) alignment that
448            calc_layout filled in for us */
449         req->r_num_pages = calc_pages_for(page_align, *plen);
450         req->r_page_alignment = page_align;
451
452         ceph_osdc_build_request(req, off, *plen, num_op, ops,
453                                 snapc, vino.snap, mtime);
454
455         return req;
456 }
457 EXPORT_SYMBOL(ceph_osdc_new_request);
458
459 /*
460  * We keep osd requests in an rbtree, sorted by ->r_tid.
461  */
462 static void __insert_request(struct ceph_osd_client *osdc,
463                              struct ceph_osd_request *new)
464 {
465         struct rb_node **p = &osdc->requests.rb_node;
466         struct rb_node *parent = NULL;
467         struct ceph_osd_request *req = NULL;
468
469         while (*p) {
470                 parent = *p;
471                 req = rb_entry(parent, struct ceph_osd_request, r_node);
472                 if (new->r_tid < req->r_tid)
473                         p = &(*p)->rb_left;
474                 else if (new->r_tid > req->r_tid)
475                         p = &(*p)->rb_right;
476                 else
477                         BUG();
478         }
479
480         rb_link_node(&new->r_node, parent, p);
481         rb_insert_color(&new->r_node, &osdc->requests);
482 }
483
484 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
485                                                  u64 tid)
486 {
487         struct ceph_osd_request *req;
488         struct rb_node *n = osdc->requests.rb_node;
489
490         while (n) {
491                 req = rb_entry(n, struct ceph_osd_request, r_node);
492                 if (tid < req->r_tid)
493                         n = n->rb_left;
494                 else if (tid > req->r_tid)
495                         n = n->rb_right;
496                 else
497                         return req;
498         }
499         return NULL;
500 }
501
502 static struct ceph_osd_request *
503 __lookup_request_ge(struct ceph_osd_client *osdc,
504                     u64 tid)
505 {
506         struct ceph_osd_request *req;
507         struct rb_node *n = osdc->requests.rb_node;
508
509         while (n) {
510                 req = rb_entry(n, struct ceph_osd_request, r_node);
511                 if (tid < req->r_tid) {
512                         if (!n->rb_left)
513                                 return req;
514                         n = n->rb_left;
515                 } else if (tid > req->r_tid) {
516                         n = n->rb_right;
517                 } else {
518                         return req;
519                 }
520         }
521         return NULL;
522 }
523
524 /*
525  * Resubmit requests pending on the given osd.
526  */
527 static void __kick_osd_requests(struct ceph_osd_client *osdc,
528                                 struct ceph_osd *osd)
529 {
530         struct ceph_osd_request *req, *nreq;
531         int err;
532
533         dout("__kick_osd_requests osd%d\n", osd->o_osd);
534         err = __reset_osd(osdc, osd);
535         if (err)
536                 return;
537
538         list_for_each_entry(req, &osd->o_requests, r_osd_item) {
539                 list_move(&req->r_req_lru_item, &osdc->req_unsent);
540                 dout("requeued %p tid %llu osd%d\n", req, req->r_tid,
541                      osd->o_osd);
542                 if (!req->r_linger)
543                         req->r_flags |= CEPH_OSD_FLAG_RETRY;
544         }
545
546         list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
547                                  r_linger_osd) {
548                 /*
549                  * reregister request prior to unregistering linger so
550                  * that r_osd is preserved.
551                  */
552                 BUG_ON(!list_empty(&req->r_req_lru_item));
553                 __register_request(osdc, req);
554                 list_add(&req->r_req_lru_item, &osdc->req_unsent);
555                 list_add(&req->r_osd_item, &req->r_osd->o_requests);
556                 __unregister_linger_request(osdc, req);
557                 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
558                      osd->o_osd);
559         }
560 }
561
562 /*
563  * If the osd connection drops, we need to resubmit all requests.
564  */
565 static void osd_reset(struct ceph_connection *con)
566 {
567         struct ceph_osd *osd = con->private;
568         struct ceph_osd_client *osdc;
569
570         if (!osd)
571                 return;
572         dout("osd_reset osd%d\n", osd->o_osd);
573         osdc = osd->o_osdc;
574         down_read(&osdc->map_sem);
575         mutex_lock(&osdc->request_mutex);
576         __kick_osd_requests(osdc, osd);
577         __send_queued(osdc);
578         mutex_unlock(&osdc->request_mutex);
579         up_read(&osdc->map_sem);
580 }
581
582 /*
583  * Track open sessions with osds.
584  */
585 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
586 {
587         struct ceph_osd *osd;
588
589         osd = kzalloc(sizeof(*osd), GFP_NOFS);
590         if (!osd)
591                 return NULL;
592
593         atomic_set(&osd->o_ref, 1);
594         osd->o_osdc = osdc;
595         osd->o_osd = onum;
596         RB_CLEAR_NODE(&osd->o_node);
597         INIT_LIST_HEAD(&osd->o_requests);
598         INIT_LIST_HEAD(&osd->o_linger_requests);
599         INIT_LIST_HEAD(&osd->o_osd_lru);
600         osd->o_incarnation = 1;
601
602         ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
603
604         INIT_LIST_HEAD(&osd->o_keepalive_item);
605         return osd;
606 }
607
608 static struct ceph_osd *get_osd(struct ceph_osd *osd)
609 {
610         if (atomic_inc_not_zero(&osd->o_ref)) {
611                 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
612                      atomic_read(&osd->o_ref));
613                 return osd;
614         } else {
615                 dout("get_osd %p FAIL\n", osd);
616                 return NULL;
617         }
618 }
619
620 static void put_osd(struct ceph_osd *osd)
621 {
622         dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
623              atomic_read(&osd->o_ref) - 1);
624         if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
625                 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
626
627                 if (ac->ops && ac->ops->destroy_authorizer)
628                         ac->ops->destroy_authorizer(ac, osd->o_auth.authorizer);
629                 kfree(osd);
630         }
631 }
632
633 /*
634  * remove an osd from our map
635  */
636 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
637 {
638         dout("__remove_osd %p\n", osd);
639         BUG_ON(!list_empty(&osd->o_requests));
640         rb_erase(&osd->o_node, &osdc->osds);
641         list_del_init(&osd->o_osd_lru);
642         ceph_con_close(&osd->o_con);
643         put_osd(osd);
644 }
645
646 static void remove_all_osds(struct ceph_osd_client *osdc)
647 {
648         dout("%s %p\n", __func__, osdc);
649         mutex_lock(&osdc->request_mutex);
650         while (!RB_EMPTY_ROOT(&osdc->osds)) {
651                 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
652                                                 struct ceph_osd, o_node);
653                 __remove_osd(osdc, osd);
654         }
655         mutex_unlock(&osdc->request_mutex);
656 }
657
658 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
659                               struct ceph_osd *osd)
660 {
661         dout("__move_osd_to_lru %p\n", osd);
662         BUG_ON(!list_empty(&osd->o_osd_lru));
663         list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
664         osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
665 }
666
667 static void __remove_osd_from_lru(struct ceph_osd *osd)
668 {
669         dout("__remove_osd_from_lru %p\n", osd);
670         if (!list_empty(&osd->o_osd_lru))
671                 list_del_init(&osd->o_osd_lru);
672 }
673
674 static void remove_old_osds(struct ceph_osd_client *osdc)
675 {
676         struct ceph_osd *osd, *nosd;
677
678         dout("__remove_old_osds %p\n", osdc);
679         mutex_lock(&osdc->request_mutex);
680         list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
681                 if (time_before(jiffies, osd->lru_ttl))
682                         break;
683                 __remove_osd(osdc, osd);
684         }
685         mutex_unlock(&osdc->request_mutex);
686 }
687
688 /*
689  * reset osd connect
690  */
691 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
692 {
693         struct ceph_entity_addr *peer_addr;
694
695         dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
696         if (list_empty(&osd->o_requests) &&
697             list_empty(&osd->o_linger_requests)) {
698                 __remove_osd(osdc, osd);
699
700                 return -ENODEV;
701         }
702
703         peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
704         if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
705                         !ceph_con_opened(&osd->o_con)) {
706                 struct ceph_osd_request *req;
707
708                 dout(" osd addr hasn't changed and connection never opened,"
709                      " letting msgr retry");
710                 /* touch each r_stamp for handle_timeout()'s benfit */
711                 list_for_each_entry(req, &osd->o_requests, r_osd_item)
712                         req->r_stamp = jiffies;
713
714                 return -EAGAIN;
715         }
716
717         ceph_con_close(&osd->o_con);
718         ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
719         osd->o_incarnation++;
720
721         return 0;
722 }
723
724 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
725 {
726         struct rb_node **p = &osdc->osds.rb_node;
727         struct rb_node *parent = NULL;
728         struct ceph_osd *osd = NULL;
729
730         dout("__insert_osd %p osd%d\n", new, new->o_osd);
731         while (*p) {
732                 parent = *p;
733                 osd = rb_entry(parent, struct ceph_osd, o_node);
734                 if (new->o_osd < osd->o_osd)
735                         p = &(*p)->rb_left;
736                 else if (new->o_osd > osd->o_osd)
737                         p = &(*p)->rb_right;
738                 else
739                         BUG();
740         }
741
742         rb_link_node(&new->o_node, parent, p);
743         rb_insert_color(&new->o_node, &osdc->osds);
744 }
745
746 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
747 {
748         struct ceph_osd *osd;
749         struct rb_node *n = osdc->osds.rb_node;
750
751         while (n) {
752                 osd = rb_entry(n, struct ceph_osd, o_node);
753                 if (o < osd->o_osd)
754                         n = n->rb_left;
755                 else if (o > osd->o_osd)
756                         n = n->rb_right;
757                 else
758                         return osd;
759         }
760         return NULL;
761 }
762
763 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
764 {
765         schedule_delayed_work(&osdc->timeout_work,
766                         osdc->client->options->osd_keepalive_timeout * HZ);
767 }
768
769 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
770 {
771         cancel_delayed_work(&osdc->timeout_work);
772 }
773
774 /*
775  * Register request, assign tid.  If this is the first request, set up
776  * the timeout event.
777  */
778 static void __register_request(struct ceph_osd_client *osdc,
779                                struct ceph_osd_request *req)
780 {
781         req->r_tid = ++osdc->last_tid;
782         req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
783         dout("__register_request %p tid %lld\n", req, req->r_tid);
784         __insert_request(osdc, req);
785         ceph_osdc_get_request(req);
786         osdc->num_requests++;
787         if (osdc->num_requests == 1) {
788                 dout(" first request, scheduling timeout\n");
789                 __schedule_osd_timeout(osdc);
790         }
791 }
792
793 static void register_request(struct ceph_osd_client *osdc,
794                              struct ceph_osd_request *req)
795 {
796         mutex_lock(&osdc->request_mutex);
797         __register_request(osdc, req);
798         mutex_unlock(&osdc->request_mutex);
799 }
800
801 /*
802  * called under osdc->request_mutex
803  */
804 static void __unregister_request(struct ceph_osd_client *osdc,
805                                  struct ceph_osd_request *req)
806 {
807         if (RB_EMPTY_NODE(&req->r_node)) {
808                 dout("__unregister_request %p tid %lld not registered\n",
809                         req, req->r_tid);
810                 return;
811         }
812
813         dout("__unregister_request %p tid %lld\n", req, req->r_tid);
814         rb_erase(&req->r_node, &osdc->requests);
815         osdc->num_requests--;
816
817         if (req->r_osd) {
818                 /* make sure the original request isn't in flight. */
819                 ceph_msg_revoke(req->r_request);
820
821                 list_del_init(&req->r_osd_item);
822                 if (list_empty(&req->r_osd->o_requests) &&
823                     list_empty(&req->r_osd->o_linger_requests)) {
824                         dout("moving osd to %p lru\n", req->r_osd);
825                         __move_osd_to_lru(osdc, req->r_osd);
826                 }
827                 if (list_empty(&req->r_linger_item))
828                         req->r_osd = NULL;
829         }
830
831         list_del_init(&req->r_req_lru_item);
832         ceph_osdc_put_request(req);
833
834         if (osdc->num_requests == 0) {
835                 dout(" no requests, canceling timeout\n");
836                 __cancel_osd_timeout(osdc);
837         }
838 }
839
840 /*
841  * Cancel a previously queued request message
842  */
843 static void __cancel_request(struct ceph_osd_request *req)
844 {
845         if (req->r_sent && req->r_osd) {
846                 ceph_msg_revoke(req->r_request);
847                 req->r_sent = 0;
848         }
849 }
850
851 static void __register_linger_request(struct ceph_osd_client *osdc,
852                                     struct ceph_osd_request *req)
853 {
854         dout("__register_linger_request %p\n", req);
855         list_add_tail(&req->r_linger_item, &osdc->req_linger);
856         if (req->r_osd)
857                 list_add_tail(&req->r_linger_osd,
858                               &req->r_osd->o_linger_requests);
859 }
860
861 static void __unregister_linger_request(struct ceph_osd_client *osdc,
862                                         struct ceph_osd_request *req)
863 {
864         dout("__unregister_linger_request %p\n", req);
865         list_del_init(&req->r_linger_item);
866         if (req->r_osd) {
867                 list_del_init(&req->r_linger_osd);
868
869                 if (list_empty(&req->r_osd->o_requests) &&
870                     list_empty(&req->r_osd->o_linger_requests)) {
871                         dout("moving osd to %p lru\n", req->r_osd);
872                         __move_osd_to_lru(osdc, req->r_osd);
873                 }
874                 if (list_empty(&req->r_osd_item))
875                         req->r_osd = NULL;
876         }
877 }
878
879 void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
880                                          struct ceph_osd_request *req)
881 {
882         mutex_lock(&osdc->request_mutex);
883         if (req->r_linger) {
884                 __unregister_linger_request(osdc, req);
885                 ceph_osdc_put_request(req);
886         }
887         mutex_unlock(&osdc->request_mutex);
888 }
889 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
890
891 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
892                                   struct ceph_osd_request *req)
893 {
894         if (!req->r_linger) {
895                 dout("set_request_linger %p\n", req);
896                 req->r_linger = 1;
897                 /*
898                  * caller is now responsible for calling
899                  * unregister_linger_request
900                  */
901                 ceph_osdc_get_request(req);
902         }
903 }
904 EXPORT_SYMBOL(ceph_osdc_set_request_linger);
905
906 /*
907  * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
908  * (as needed), and set the request r_osd appropriately.  If there is
909  * no up osd, set r_osd to NULL.  Move the request to the appropriate list
910  * (unsent, homeless) or leave on in-flight lru.
911  *
912  * Return 0 if unchanged, 1 if changed, or negative on error.
913  *
914  * Caller should hold map_sem for read and request_mutex.
915  */
916 static int __map_request(struct ceph_osd_client *osdc,
917                          struct ceph_osd_request *req, int force_resend)
918 {
919         struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
920         struct ceph_pg pgid;
921         int acting[CEPH_PG_MAX_SIZE];
922         int o = -1, num = 0;
923         int err;
924
925         dout("map_request %p tid %lld\n", req, req->r_tid);
926         err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
927                                       &req->r_file_layout, osdc->osdmap);
928         if (err) {
929                 list_move(&req->r_req_lru_item, &osdc->req_notarget);
930                 return err;
931         }
932         pgid = reqhead->layout.ol_pgid;
933         req->r_pgid = pgid;
934
935         err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
936         if (err > 0) {
937                 o = acting[0];
938                 num = err;
939         }
940
941         if ((!force_resend &&
942              req->r_osd && req->r_osd->o_osd == o &&
943              req->r_sent >= req->r_osd->o_incarnation &&
944              req->r_num_pg_osds == num &&
945              memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
946             (req->r_osd == NULL && o == -1))
947                 return 0;  /* no change */
948
949         dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
950              req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
951              req->r_osd ? req->r_osd->o_osd : -1);
952
953         /* record full pg acting set */
954         memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
955         req->r_num_pg_osds = num;
956
957         if (req->r_osd) {
958                 __cancel_request(req);
959                 list_del_init(&req->r_osd_item);
960                 req->r_osd = NULL;
961         }
962
963         req->r_osd = __lookup_osd(osdc, o);
964         if (!req->r_osd && o >= 0) {
965                 err = -ENOMEM;
966                 req->r_osd = create_osd(osdc, o);
967                 if (!req->r_osd) {
968                         list_move(&req->r_req_lru_item, &osdc->req_notarget);
969                         goto out;
970                 }
971
972                 dout("map_request osd %p is osd%d\n", req->r_osd, o);
973                 __insert_osd(osdc, req->r_osd);
974
975                 ceph_con_open(&req->r_osd->o_con,
976                               CEPH_ENTITY_TYPE_OSD, o,
977                               &osdc->osdmap->osd_addr[o]);
978         }
979
980         if (req->r_osd) {
981                 __remove_osd_from_lru(req->r_osd);
982                 list_add(&req->r_osd_item, &req->r_osd->o_requests);
983                 list_move(&req->r_req_lru_item, &osdc->req_unsent);
984         } else {
985                 list_move(&req->r_req_lru_item, &osdc->req_notarget);
986         }
987         err = 1;   /* osd or pg changed */
988
989 out:
990         return err;
991 }
992
993 /*
994  * caller should hold map_sem (for read) and request_mutex
995  */
996 static void __send_request(struct ceph_osd_client *osdc,
997                            struct ceph_osd_request *req)
998 {
999         struct ceph_osd_request_head *reqhead;
1000
1001         dout("send_request %p tid %llu to osd%d flags %d\n",
1002              req, req->r_tid, req->r_osd->o_osd, req->r_flags);
1003
1004         reqhead = req->r_request->front.iov_base;
1005         reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
1006         reqhead->flags |= cpu_to_le32(req->r_flags);  /* e.g., RETRY */
1007         reqhead->reassert_version = req->r_reassert_version;
1008
1009         req->r_stamp = jiffies;
1010         list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1011
1012         ceph_msg_get(req->r_request); /* send consumes a ref */
1013         ceph_con_send(&req->r_osd->o_con, req->r_request);
1014         req->r_sent = req->r_osd->o_incarnation;
1015 }
1016
1017 /*
1018  * Send any requests in the queue (req_unsent).
1019  */
1020 static void __send_queued(struct ceph_osd_client *osdc)
1021 {
1022         struct ceph_osd_request *req, *tmp;
1023
1024         dout("__send_queued\n");
1025         list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
1026                 __send_request(osdc, req);
1027 }
1028
1029 /*
1030  * Timeout callback, called every N seconds when 1 or more osd
1031  * requests has been active for more than N seconds.  When this
1032  * happens, we ping all OSDs with requests who have timed out to
1033  * ensure any communications channel reset is detected.  Reset the
1034  * request timeouts another N seconds in the future as we go.
1035  * Reschedule the timeout event another N seconds in future (unless
1036  * there are no open requests).
1037  */
1038 static void handle_timeout(struct work_struct *work)
1039 {
1040         struct ceph_osd_client *osdc =
1041                 container_of(work, struct ceph_osd_client, timeout_work.work);
1042         struct ceph_osd_request *req;
1043         struct ceph_osd *osd;
1044         unsigned long keepalive =
1045                 osdc->client->options->osd_keepalive_timeout * HZ;
1046         struct list_head slow_osds;
1047         dout("timeout\n");
1048         down_read(&osdc->map_sem);
1049
1050         ceph_monc_request_next_osdmap(&osdc->client->monc);
1051
1052         mutex_lock(&osdc->request_mutex);
1053
1054         /*
1055          * ping osds that are a bit slow.  this ensures that if there
1056          * is a break in the TCP connection we will notice, and reopen
1057          * a connection with that osd (from the fault callback).
1058          */
1059         INIT_LIST_HEAD(&slow_osds);
1060         list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1061                 if (time_before(jiffies, req->r_stamp + keepalive))
1062                         break;
1063
1064                 osd = req->r_osd;
1065                 BUG_ON(!osd);
1066                 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1067                      req->r_tid, osd->o_osd);
1068                 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1069         }
1070         while (!list_empty(&slow_osds)) {
1071                 osd = list_entry(slow_osds.next, struct ceph_osd,
1072                                  o_keepalive_item);
1073                 list_del_init(&osd->o_keepalive_item);
1074                 ceph_con_keepalive(&osd->o_con);
1075         }
1076
1077         __schedule_osd_timeout(osdc);
1078         __send_queued(osdc);
1079         mutex_unlock(&osdc->request_mutex);
1080         up_read(&osdc->map_sem);
1081 }
1082
1083 static void handle_osds_timeout(struct work_struct *work)
1084 {
1085         struct ceph_osd_client *osdc =
1086                 container_of(work, struct ceph_osd_client,
1087                              osds_timeout_work.work);
1088         unsigned long delay =
1089                 osdc->client->options->osd_idle_ttl * HZ >> 2;
1090
1091         dout("osds timeout\n");
1092         down_read(&osdc->map_sem);
1093         remove_old_osds(osdc);
1094         up_read(&osdc->map_sem);
1095
1096         schedule_delayed_work(&osdc->osds_timeout_work,
1097                               round_jiffies_relative(delay));
1098 }
1099
1100 static void complete_request(struct ceph_osd_request *req)
1101 {
1102         if (req->r_safe_callback)
1103                 req->r_safe_callback(req, NULL);
1104         complete_all(&req->r_safe_completion);  /* fsync waiter */
1105 }
1106
1107 /*
1108  * handle osd op reply.  either call the callback if it is specified,
1109  * or do the completion to wake up the waiting thread.
1110  */
1111 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1112                          struct ceph_connection *con)
1113 {
1114         struct ceph_osd_reply_head *rhead = msg->front.iov_base;
1115         struct ceph_osd_request *req;
1116         u64 tid;
1117         int numops, object_len, flags;
1118         s32 result;
1119
1120         tid = le64_to_cpu(msg->hdr.tid);
1121         if (msg->front.iov_len < sizeof(*rhead))
1122                 goto bad;
1123         numops = le32_to_cpu(rhead->num_ops);
1124         object_len = le32_to_cpu(rhead->object_len);
1125         result = le32_to_cpu(rhead->result);
1126         if (msg->front.iov_len != sizeof(*rhead) + object_len +
1127             numops * sizeof(struct ceph_osd_op))
1128                 goto bad;
1129         dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
1130         /* lookup */
1131         mutex_lock(&osdc->request_mutex);
1132         req = __lookup_request(osdc, tid);
1133         if (req == NULL) {
1134                 dout("handle_reply tid %llu dne\n", tid);
1135                 mutex_unlock(&osdc->request_mutex);
1136                 return;
1137         }
1138         ceph_osdc_get_request(req);
1139         flags = le32_to_cpu(rhead->flags);
1140
1141         /*
1142          * if this connection filled our message, drop our reference now, to
1143          * avoid a (safe but slower) revoke later.
1144          */
1145         if (req->r_con_filling_msg == con && req->r_reply == msg) {
1146                 dout(" dropping con_filling_msg ref %p\n", con);
1147                 req->r_con_filling_msg = NULL;
1148                 con->ops->put(con);
1149         }
1150
1151         if (!req->r_got_reply) {
1152                 unsigned int bytes;
1153
1154                 req->r_result = le32_to_cpu(rhead->result);
1155                 bytes = le32_to_cpu(msg->hdr.data_len);
1156                 dout("handle_reply result %d bytes %d\n", req->r_result,
1157                      bytes);
1158                 if (req->r_result == 0)
1159                         req->r_result = bytes;
1160
1161                 /* in case this is a write and we need to replay, */
1162                 req->r_reassert_version = rhead->reassert_version;
1163
1164                 req->r_got_reply = 1;
1165         } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1166                 dout("handle_reply tid %llu dup ack\n", tid);
1167                 mutex_unlock(&osdc->request_mutex);
1168                 goto done;
1169         }
1170
1171         dout("handle_reply tid %llu flags %d\n", tid, flags);
1172
1173         if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1174                 __register_linger_request(osdc, req);
1175
1176         /* either this is a read, or we got the safe response */
1177         if (result < 0 ||
1178             (flags & CEPH_OSD_FLAG_ONDISK) ||
1179             ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1180                 __unregister_request(osdc, req);
1181
1182         mutex_unlock(&osdc->request_mutex);
1183
1184         if (req->r_callback)
1185                 req->r_callback(req, msg);
1186         else
1187                 complete_all(&req->r_completion);
1188
1189         if (flags & CEPH_OSD_FLAG_ONDISK)
1190                 complete_request(req);
1191
1192 done:
1193         dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1194         ceph_osdc_put_request(req);
1195         return;
1196
1197 bad:
1198         pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1199                (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
1200                (int)sizeof(*rhead));
1201         ceph_msg_dump(msg);
1202 }
1203
1204 static void reset_changed_osds(struct ceph_osd_client *osdc)
1205 {
1206         struct rb_node *p, *n;
1207
1208         for (p = rb_first(&osdc->osds); p; p = n) {
1209                 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
1210
1211                 n = rb_next(p);
1212                 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1213                     memcmp(&osd->o_con.peer_addr,
1214                            ceph_osd_addr(osdc->osdmap,
1215                                          osd->o_osd),
1216                            sizeof(struct ceph_entity_addr)) != 0)
1217                         __reset_osd(osdc, osd);
1218         }
1219 }
1220
1221 /*
1222  * Requeue requests whose mapping to an OSD has changed.  If requests map to
1223  * no osd, request a new map.
1224  *
1225  * Caller should hold map_sem for read.
1226  */
1227 static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
1228 {
1229         struct ceph_osd_request *req, *nreq;
1230         struct rb_node *p;
1231         int needmap = 0;
1232         int err;
1233
1234         dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
1235         mutex_lock(&osdc->request_mutex);
1236         for (p = rb_first(&osdc->requests); p; ) {
1237                 req = rb_entry(p, struct ceph_osd_request, r_node);
1238                 p = rb_next(p);
1239
1240                 /*
1241                  * For linger requests that have not yet been
1242                  * registered, move them to the linger list; they'll
1243                  * be sent to the osd in the loop below.  Unregister
1244                  * the request before re-registering it as a linger
1245                  * request to ensure the __map_request() below
1246                  * will decide it needs to be sent.
1247                  */
1248                 if (req->r_linger && list_empty(&req->r_linger_item)) {
1249                         dout("%p tid %llu restart on osd%d\n",
1250                              req, req->r_tid,
1251                              req->r_osd ? req->r_osd->o_osd : -1);
1252                         __unregister_request(osdc, req);
1253                         __register_linger_request(osdc, req);
1254                         continue;
1255                 }
1256
1257                 err = __map_request(osdc, req, force_resend);
1258                 if (err < 0)
1259                         continue;  /* error */
1260                 if (req->r_osd == NULL) {
1261                         dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1262                         needmap++;  /* request a newer map */
1263                 } else if (err > 0) {
1264                         if (!req->r_linger) {
1265                                 dout("%p tid %llu requeued on osd%d\n", req,
1266                                      req->r_tid,
1267                                      req->r_osd ? req->r_osd->o_osd : -1);
1268                                 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1269                         }
1270                 }
1271         }
1272
1273         list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1274                                  r_linger_item) {
1275                 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1276
1277                 err = __map_request(osdc, req, force_resend);
1278                 dout("__map_request returned %d\n", err);
1279                 if (err == 0)
1280                         continue;  /* no change and no osd was specified */
1281                 if (err < 0)
1282                         continue;  /* hrm! */
1283                 if (req->r_osd == NULL) {
1284                         dout("tid %llu maps to no valid osd\n", req->r_tid);
1285                         needmap++;  /* request a newer map */
1286                         continue;
1287                 }
1288
1289                 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1290                      req->r_osd ? req->r_osd->o_osd : -1);
1291                 __register_request(osdc, req);
1292                 __unregister_linger_request(osdc, req);
1293         }
1294         mutex_unlock(&osdc->request_mutex);
1295
1296         if (needmap) {
1297                 dout("%d requests for down osds, need new map\n", needmap);
1298                 ceph_monc_request_next_osdmap(&osdc->client->monc);
1299         }
1300         reset_changed_osds(osdc);
1301 }
1302
1303
1304 /*
1305  * Process updated osd map.
1306  *
1307  * The message contains any number of incremental and full maps, normally
1308  * indicating some sort of topology change in the cluster.  Kick requests
1309  * off to different OSDs as needed.
1310  */
1311 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1312 {
1313         void *p, *end, *next;
1314         u32 nr_maps, maplen;
1315         u32 epoch;
1316         struct ceph_osdmap *newmap = NULL, *oldmap;
1317         int err;
1318         struct ceph_fsid fsid;
1319
1320         dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1321         p = msg->front.iov_base;
1322         end = p + msg->front.iov_len;
1323
1324         /* verify fsid */
1325         ceph_decode_need(&p, end, sizeof(fsid), bad);
1326         ceph_decode_copy(&p, &fsid, sizeof(fsid));
1327         if (ceph_check_fsid(osdc->client, &fsid) < 0)
1328                 return;
1329
1330         down_write(&osdc->map_sem);
1331
1332         /* incremental maps */
1333         ceph_decode_32_safe(&p, end, nr_maps, bad);
1334         dout(" %d inc maps\n", nr_maps);
1335         while (nr_maps > 0) {
1336                 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1337                 epoch = ceph_decode_32(&p);
1338                 maplen = ceph_decode_32(&p);
1339                 ceph_decode_need(&p, end, maplen, bad);
1340                 next = p + maplen;
1341                 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1342                         dout("applying incremental map %u len %d\n",
1343                              epoch, maplen);
1344                         newmap = osdmap_apply_incremental(&p, next,
1345                                                           osdc->osdmap,
1346                                                           &osdc->client->msgr);
1347                         if (IS_ERR(newmap)) {
1348                                 err = PTR_ERR(newmap);
1349                                 goto bad;
1350                         }
1351                         BUG_ON(!newmap);
1352                         if (newmap != osdc->osdmap) {
1353                                 ceph_osdmap_destroy(osdc->osdmap);
1354                                 osdc->osdmap = newmap;
1355                         }
1356                         kick_requests(osdc, 0);
1357                 } else {
1358                         dout("ignoring incremental map %u len %d\n",
1359                              epoch, maplen);
1360                 }
1361                 p = next;
1362                 nr_maps--;
1363         }
1364         if (newmap)
1365                 goto done;
1366
1367         /* full maps */
1368         ceph_decode_32_safe(&p, end, nr_maps, bad);
1369         dout(" %d full maps\n", nr_maps);
1370         while (nr_maps) {
1371                 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1372                 epoch = ceph_decode_32(&p);
1373                 maplen = ceph_decode_32(&p);
1374                 ceph_decode_need(&p, end, maplen, bad);
1375                 if (nr_maps > 1) {
1376                         dout("skipping non-latest full map %u len %d\n",
1377                              epoch, maplen);
1378                 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1379                         dout("skipping full map %u len %d, "
1380                              "older than our %u\n", epoch, maplen,
1381                              osdc->osdmap->epoch);
1382                 } else {
1383                         int skipped_map = 0;
1384
1385                         dout("taking full map %u len %d\n", epoch, maplen);
1386                         newmap = osdmap_decode(&p, p+maplen);
1387                         if (IS_ERR(newmap)) {
1388                                 err = PTR_ERR(newmap);
1389                                 goto bad;
1390                         }
1391                         BUG_ON(!newmap);
1392                         oldmap = osdc->osdmap;
1393                         osdc->osdmap = newmap;
1394                         if (oldmap) {
1395                                 if (oldmap->epoch + 1 < newmap->epoch)
1396                                         skipped_map = 1;
1397                                 ceph_osdmap_destroy(oldmap);
1398                         }
1399                         kick_requests(osdc, skipped_map);
1400                 }
1401                 p += maplen;
1402                 nr_maps--;
1403         }
1404
1405 done:
1406         downgrade_write(&osdc->map_sem);
1407         ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1408
1409         /*
1410          * subscribe to subsequent osdmap updates if full to ensure
1411          * we find out when we are no longer full and stop returning
1412          * ENOSPC.
1413          */
1414         if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1415                 ceph_monc_request_next_osdmap(&osdc->client->monc);
1416
1417         mutex_lock(&osdc->request_mutex);
1418         __send_queued(osdc);
1419         mutex_unlock(&osdc->request_mutex);
1420         up_read(&osdc->map_sem);
1421         wake_up_all(&osdc->client->auth_wq);
1422         return;
1423
1424 bad:
1425         pr_err("osdc handle_map corrupt msg\n");
1426         ceph_msg_dump(msg);
1427         up_write(&osdc->map_sem);
1428         return;
1429 }
1430
1431 /*
1432  * watch/notify callback event infrastructure
1433  *
1434  * These callbacks are used both for watch and notify operations.
1435  */
1436 static void __release_event(struct kref *kref)
1437 {
1438         struct ceph_osd_event *event =
1439                 container_of(kref, struct ceph_osd_event, kref);
1440
1441         dout("__release_event %p\n", event);
1442         kfree(event);
1443 }
1444
1445 static void get_event(struct ceph_osd_event *event)
1446 {
1447         kref_get(&event->kref);
1448 }
1449
1450 void ceph_osdc_put_event(struct ceph_osd_event *event)
1451 {
1452         kref_put(&event->kref, __release_event);
1453 }
1454 EXPORT_SYMBOL(ceph_osdc_put_event);
1455
1456 static void __insert_event(struct ceph_osd_client *osdc,
1457                              struct ceph_osd_event *new)
1458 {
1459         struct rb_node **p = &osdc->event_tree.rb_node;
1460         struct rb_node *parent = NULL;
1461         struct ceph_osd_event *event = NULL;
1462
1463         while (*p) {
1464                 parent = *p;
1465                 event = rb_entry(parent, struct ceph_osd_event, node);
1466                 if (new->cookie < event->cookie)
1467                         p = &(*p)->rb_left;
1468                 else if (new->cookie > event->cookie)
1469                         p = &(*p)->rb_right;
1470                 else
1471                         BUG();
1472         }
1473
1474         rb_link_node(&new->node, parent, p);
1475         rb_insert_color(&new->node, &osdc->event_tree);
1476 }
1477
1478 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1479                                                 u64 cookie)
1480 {
1481         struct rb_node **p = &osdc->event_tree.rb_node;
1482         struct rb_node *parent = NULL;
1483         struct ceph_osd_event *event = NULL;
1484
1485         while (*p) {
1486                 parent = *p;
1487                 event = rb_entry(parent, struct ceph_osd_event, node);
1488                 if (cookie < event->cookie)
1489                         p = &(*p)->rb_left;
1490                 else if (cookie > event->cookie)
1491                         p = &(*p)->rb_right;
1492                 else
1493                         return event;
1494         }
1495         return NULL;
1496 }
1497
1498 static void __remove_event(struct ceph_osd_event *event)
1499 {
1500         struct ceph_osd_client *osdc = event->osdc;
1501
1502         if (!RB_EMPTY_NODE(&event->node)) {
1503                 dout("__remove_event removed %p\n", event);
1504                 rb_erase(&event->node, &osdc->event_tree);
1505                 ceph_osdc_put_event(event);
1506         } else {
1507                 dout("__remove_event didn't remove %p\n", event);
1508         }
1509 }
1510
1511 int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1512                            void (*event_cb)(u64, u64, u8, void *),
1513                            void *data, struct ceph_osd_event **pevent)
1514 {
1515         struct ceph_osd_event *event;
1516
1517         event = kmalloc(sizeof(*event), GFP_NOIO);
1518         if (!event)
1519                 return -ENOMEM;
1520
1521         dout("create_event %p\n", event);
1522         event->cb = event_cb;
1523         event->one_shot = 0;
1524         event->data = data;
1525         event->osdc = osdc;
1526         INIT_LIST_HEAD(&event->osd_node);
1527         RB_CLEAR_NODE(&event->node);
1528         kref_init(&event->kref);   /* one ref for us */
1529         kref_get(&event->kref);    /* one ref for the caller */
1530
1531         spin_lock(&osdc->event_lock);
1532         event->cookie = ++osdc->event_count;
1533         __insert_event(osdc, event);
1534         spin_unlock(&osdc->event_lock);
1535
1536         *pevent = event;
1537         return 0;
1538 }
1539 EXPORT_SYMBOL(ceph_osdc_create_event);
1540
1541 void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1542 {
1543         struct ceph_osd_client *osdc = event->osdc;
1544
1545         dout("cancel_event %p\n", event);
1546         spin_lock(&osdc->event_lock);
1547         __remove_event(event);
1548         spin_unlock(&osdc->event_lock);
1549         ceph_osdc_put_event(event); /* caller's */
1550 }
1551 EXPORT_SYMBOL(ceph_osdc_cancel_event);
1552
1553
1554 static void do_event_work(struct work_struct *work)
1555 {
1556         struct ceph_osd_event_work *event_work =
1557                 container_of(work, struct ceph_osd_event_work, work);
1558         struct ceph_osd_event *event = event_work->event;
1559         u64 ver = event_work->ver;
1560         u64 notify_id = event_work->notify_id;
1561         u8 opcode = event_work->opcode;
1562
1563         dout("do_event_work completing %p\n", event);
1564         event->cb(ver, notify_id, opcode, event->data);
1565         dout("do_event_work completed %p\n", event);
1566         ceph_osdc_put_event(event);
1567         kfree(event_work);
1568 }
1569
1570
1571 /*
1572  * Process osd watch notifications
1573  */
1574 static void handle_watch_notify(struct ceph_osd_client *osdc,
1575                                 struct ceph_msg *msg)
1576 {
1577         void *p, *end;
1578         u8 proto_ver;
1579         u64 cookie, ver, notify_id;
1580         u8 opcode;
1581         struct ceph_osd_event *event;
1582         struct ceph_osd_event_work *event_work;
1583
1584         p = msg->front.iov_base;
1585         end = p + msg->front.iov_len;
1586
1587         ceph_decode_8_safe(&p, end, proto_ver, bad);
1588         ceph_decode_8_safe(&p, end, opcode, bad);
1589         ceph_decode_64_safe(&p, end, cookie, bad);
1590         ceph_decode_64_safe(&p, end, ver, bad);
1591         ceph_decode_64_safe(&p, end, notify_id, bad);
1592
1593         spin_lock(&osdc->event_lock);
1594         event = __find_event(osdc, cookie);
1595         if (event) {
1596                 BUG_ON(event->one_shot);
1597                 get_event(event);
1598         }
1599         spin_unlock(&osdc->event_lock);
1600         dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1601              cookie, ver, event);
1602         if (event) {
1603                 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
1604                 if (!event_work) {
1605                         dout("ERROR: could not allocate event_work\n");
1606                         goto done_err;
1607                 }
1608                 INIT_WORK(&event_work->work, do_event_work);
1609                 event_work->event = event;
1610                 event_work->ver = ver;
1611                 event_work->notify_id = notify_id;
1612                 event_work->opcode = opcode;
1613                 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1614                         dout("WARNING: failed to queue notify event work\n");
1615                         goto done_err;
1616                 }
1617         }
1618
1619         return;
1620
1621 done_err:
1622         ceph_osdc_put_event(event);
1623         return;
1624
1625 bad:
1626         pr_err("osdc handle_watch_notify corrupt msg\n");
1627         return;
1628 }
1629
1630 /*
1631  * Register request, send initial attempt.
1632  */
1633 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1634                             struct ceph_osd_request *req,
1635                             bool nofail)
1636 {
1637         int rc = 0;
1638
1639         req->r_request->pages = req->r_pages;
1640         req->r_request->nr_pages = req->r_num_pages;
1641 #ifdef CONFIG_BLOCK
1642         req->r_request->bio = req->r_bio;
1643 #endif
1644         req->r_request->trail = &req->r_trail;
1645
1646         register_request(osdc, req);
1647
1648         down_read(&osdc->map_sem);
1649         mutex_lock(&osdc->request_mutex);
1650         /*
1651          * a racing kick_requests() may have sent the message for us
1652          * while we dropped request_mutex above, so only send now if
1653          * the request still han't been touched yet.
1654          */
1655         if (req->r_sent == 0) {
1656                 rc = __map_request(osdc, req, 0);
1657                 if (rc < 0) {
1658                         if (nofail) {
1659                                 dout("osdc_start_request failed map, "
1660                                      " will retry %lld\n", req->r_tid);
1661                                 rc = 0;
1662                         }
1663                         goto out_unlock;
1664                 }
1665                 if (req->r_osd == NULL) {
1666                         dout("send_request %p no up osds in pg\n", req);
1667                         ceph_monc_request_next_osdmap(&osdc->client->monc);
1668                 } else {
1669                         __send_request(osdc, req);
1670                 }
1671                 rc = 0;
1672         }
1673
1674 out_unlock:
1675         mutex_unlock(&osdc->request_mutex);
1676         up_read(&osdc->map_sem);
1677         return rc;
1678 }
1679 EXPORT_SYMBOL(ceph_osdc_start_request);
1680
1681 /*
1682  * wait for a request to complete
1683  */
1684 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1685                            struct ceph_osd_request *req)
1686 {
1687         int rc;
1688
1689         rc = wait_for_completion_interruptible(&req->r_completion);
1690         if (rc < 0) {
1691                 mutex_lock(&osdc->request_mutex);
1692                 __cancel_request(req);
1693                 __unregister_request(osdc, req);
1694                 mutex_unlock(&osdc->request_mutex);
1695                 complete_request(req);
1696                 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
1697                 return rc;
1698         }
1699
1700         dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1701         return req->r_result;
1702 }
1703 EXPORT_SYMBOL(ceph_osdc_wait_request);
1704
1705 /*
1706  * sync - wait for all in-flight requests to flush.  avoid starvation.
1707  */
1708 void ceph_osdc_sync(struct ceph_osd_client *osdc)
1709 {
1710         struct ceph_osd_request *req;
1711         u64 last_tid, next_tid = 0;
1712
1713         mutex_lock(&osdc->request_mutex);
1714         last_tid = osdc->last_tid;
1715         while (1) {
1716                 req = __lookup_request_ge(osdc, next_tid);
1717                 if (!req)
1718                         break;
1719                 if (req->r_tid > last_tid)
1720                         break;
1721
1722                 next_tid = req->r_tid + 1;
1723                 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1724                         continue;
1725
1726                 ceph_osdc_get_request(req);
1727                 mutex_unlock(&osdc->request_mutex);
1728                 dout("sync waiting on tid %llu (last is %llu)\n",
1729                      req->r_tid, last_tid);
1730                 wait_for_completion(&req->r_safe_completion);
1731                 mutex_lock(&osdc->request_mutex);
1732                 ceph_osdc_put_request(req);
1733         }
1734         mutex_unlock(&osdc->request_mutex);
1735         dout("sync done (thru tid %llu)\n", last_tid);
1736 }
1737 EXPORT_SYMBOL(ceph_osdc_sync);
1738
1739 /*
1740  * init, shutdown
1741  */
1742 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1743 {
1744         int err;
1745
1746         dout("init\n");
1747         osdc->client = client;
1748         osdc->osdmap = NULL;
1749         init_rwsem(&osdc->map_sem);
1750         init_completion(&osdc->map_waiters);
1751         osdc->last_requested_map = 0;
1752         mutex_init(&osdc->request_mutex);
1753         osdc->last_tid = 0;
1754         osdc->osds = RB_ROOT;
1755         INIT_LIST_HEAD(&osdc->osd_lru);
1756         osdc->requests = RB_ROOT;
1757         INIT_LIST_HEAD(&osdc->req_lru);
1758         INIT_LIST_HEAD(&osdc->req_unsent);
1759         INIT_LIST_HEAD(&osdc->req_notarget);
1760         INIT_LIST_HEAD(&osdc->req_linger);
1761         osdc->num_requests = 0;
1762         INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
1763         INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
1764         spin_lock_init(&osdc->event_lock);
1765         osdc->event_tree = RB_ROOT;
1766         osdc->event_count = 0;
1767
1768         schedule_delayed_work(&osdc->osds_timeout_work,
1769            round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
1770
1771         err = -ENOMEM;
1772         osdc->req_mempool = mempool_create_kmalloc_pool(10,
1773                                         sizeof(struct ceph_osd_request));
1774         if (!osdc->req_mempool)
1775                 goto out;
1776
1777         err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
1778                                 OSD_OP_FRONT_LEN, 10, true,
1779                                 "osd_op");
1780         if (err < 0)
1781                 goto out_mempool;
1782         err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
1783                                 OSD_OPREPLY_FRONT_LEN, 10, true,
1784                                 "osd_op_reply");
1785         if (err < 0)
1786                 goto out_msgpool;
1787
1788         osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
1789         if (IS_ERR(osdc->notify_wq)) {
1790                 err = PTR_ERR(osdc->notify_wq);
1791                 osdc->notify_wq = NULL;
1792                 goto out_msgpool;
1793         }
1794         return 0;
1795
1796 out_msgpool:
1797         ceph_msgpool_destroy(&osdc->msgpool_op);
1798 out_mempool:
1799         mempool_destroy(osdc->req_mempool);
1800 out:
1801         return err;
1802 }
1803
1804 void ceph_osdc_stop(struct ceph_osd_client *osdc)
1805 {
1806         flush_workqueue(osdc->notify_wq);
1807         destroy_workqueue(osdc->notify_wq);
1808         cancel_delayed_work_sync(&osdc->timeout_work);
1809         cancel_delayed_work_sync(&osdc->osds_timeout_work);
1810         if (osdc->osdmap) {
1811                 ceph_osdmap_destroy(osdc->osdmap);
1812                 osdc->osdmap = NULL;
1813         }
1814         remove_all_osds(osdc);
1815         mempool_destroy(osdc->req_mempool);
1816         ceph_msgpool_destroy(&osdc->msgpool_op);
1817         ceph_msgpool_destroy(&osdc->msgpool_op_reply);
1818 }
1819
1820 /*
1821  * Read some contiguous pages.  If we cross a stripe boundary, shorten
1822  * *plen.  Return number of bytes read, or error.
1823  */
1824 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1825                         struct ceph_vino vino, struct ceph_file_layout *layout,
1826                         u64 off, u64 *plen,
1827                         u32 truncate_seq, u64 truncate_size,
1828                         struct page **pages, int num_pages, int page_align)
1829 {
1830         struct ceph_osd_request *req;
1831         int rc = 0;
1832
1833         dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1834              vino.snap, off, *plen);
1835         req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1836                                     CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1837                                     NULL, 0, truncate_seq, truncate_size, NULL,
1838                                     false, page_align);
1839         if (IS_ERR(req))
1840                 return PTR_ERR(req);
1841
1842         /* it may be a short read due to an object boundary */
1843         req->r_pages = pages;
1844
1845         dout("readpages  final extent is %llu~%llu (%d pages align %d)\n",
1846              off, *plen, req->r_num_pages, page_align);
1847
1848         rc = ceph_osdc_start_request(osdc, req, false);
1849         if (!rc)
1850                 rc = ceph_osdc_wait_request(osdc, req);
1851
1852         ceph_osdc_put_request(req);
1853         dout("readpages result %d\n", rc);
1854         return rc;
1855 }
1856 EXPORT_SYMBOL(ceph_osdc_readpages);
1857
1858 /*
1859  * do a synchronous write on N pages
1860  */
1861 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1862                          struct ceph_file_layout *layout,
1863                          struct ceph_snap_context *snapc,
1864                          u64 off, u64 len,
1865                          u32 truncate_seq, u64 truncate_size,
1866                          struct timespec *mtime,
1867                          struct page **pages, int num_pages)
1868 {
1869         struct ceph_osd_request *req;
1870         int rc = 0;
1871         int page_align = off & ~PAGE_MASK;
1872
1873         BUG_ON(vino.snap != CEPH_NOSNAP);
1874         req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1875                                     CEPH_OSD_OP_WRITE,
1876                                     CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
1877                                     snapc, 0,
1878                                     truncate_seq, truncate_size, mtime,
1879                                     true, page_align);
1880         if (IS_ERR(req))
1881                 return PTR_ERR(req);
1882
1883         /* it may be a short write due to an object boundary */
1884         req->r_pages = pages;
1885         dout("writepages %llu~%llu (%d pages)\n", off, len,
1886              req->r_num_pages);
1887
1888         rc = ceph_osdc_start_request(osdc, req, true);
1889         if (!rc)
1890                 rc = ceph_osdc_wait_request(osdc, req);
1891
1892         ceph_osdc_put_request(req);
1893         if (rc == 0)
1894                 rc = len;
1895         dout("writepages result %d\n", rc);
1896         return rc;
1897 }
1898 EXPORT_SYMBOL(ceph_osdc_writepages);
1899
1900 /*
1901  * handle incoming message
1902  */
1903 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1904 {
1905         struct ceph_osd *osd = con->private;
1906         struct ceph_osd_client *osdc;
1907         int type = le16_to_cpu(msg->hdr.type);
1908
1909         if (!osd)
1910                 goto out;
1911         osdc = osd->o_osdc;
1912
1913         switch (type) {
1914         case CEPH_MSG_OSD_MAP:
1915                 ceph_osdc_handle_map(osdc, msg);
1916                 break;
1917         case CEPH_MSG_OSD_OPREPLY:
1918                 handle_reply(osdc, msg, con);
1919                 break;
1920         case CEPH_MSG_WATCH_NOTIFY:
1921                 handle_watch_notify(osdc, msg);
1922                 break;
1923
1924         default:
1925                 pr_err("received unknown message type %d %s\n", type,
1926                        ceph_msg_type_name(type));
1927         }
1928 out:
1929         ceph_msg_put(msg);
1930 }
1931
1932 /*
1933  * lookup and return message for incoming reply.  set up reply message
1934  * pages.
1935  */
1936 static struct ceph_msg *get_reply(struct ceph_connection *con,
1937                                   struct ceph_msg_header *hdr,
1938                                   int *skip)
1939 {
1940         struct ceph_osd *osd = con->private;
1941         struct ceph_osd_client *osdc = osd->o_osdc;
1942         struct ceph_msg *m;
1943         struct ceph_osd_request *req;
1944         int front = le32_to_cpu(hdr->front_len);
1945         int data_len = le32_to_cpu(hdr->data_len);
1946         u64 tid;
1947
1948         tid = le64_to_cpu(hdr->tid);
1949         mutex_lock(&osdc->request_mutex);
1950         req = __lookup_request(osdc, tid);
1951         if (!req) {
1952                 *skip = 1;
1953                 m = NULL;
1954                 dout("get_reply unknown tid %llu from osd%d\n", tid,
1955                      osd->o_osd);
1956                 goto out;
1957         }
1958
1959         if (req->r_con_filling_msg) {
1960                 dout("%s revoking msg %p from old con %p\n", __func__,
1961                      req->r_reply, req->r_con_filling_msg);
1962                 ceph_msg_revoke_incoming(req->r_reply);
1963                 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
1964                 req->r_con_filling_msg = NULL;
1965         }
1966
1967         if (front > req->r_reply->front.iov_len) {
1968                 pr_warning("get_reply front %d > preallocated %d\n",
1969                            front, (int)req->r_reply->front.iov_len);
1970                 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
1971                 if (!m)
1972                         goto out;
1973                 ceph_msg_put(req->r_reply);
1974                 req->r_reply = m;
1975         }
1976         m = ceph_msg_get(req->r_reply);
1977
1978         if (data_len > 0) {
1979                 int want = calc_pages_for(req->r_page_alignment, data_len);
1980
1981                 if (req->r_pages && unlikely(req->r_num_pages < want)) {
1982                         pr_warning("tid %lld reply has %d bytes %d pages, we"
1983                                    " had only %d pages ready\n", tid, data_len,
1984                                    want, req->r_num_pages);
1985                         *skip = 1;
1986                         ceph_msg_put(m);
1987                         m = NULL;
1988                         goto out;
1989                 }
1990                 m->pages = req->r_pages;
1991                 m->nr_pages = req->r_num_pages;
1992                 m->page_alignment = req->r_page_alignment;
1993 #ifdef CONFIG_BLOCK
1994                 m->bio = req->r_bio;
1995 #endif
1996         }
1997         *skip = 0;
1998         req->r_con_filling_msg = con->ops->get(con);
1999         dout("get_reply tid %lld %p\n", tid, m);
2000
2001 out:
2002         mutex_unlock(&osdc->request_mutex);
2003         return m;
2004
2005 }
2006
2007 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2008                                   struct ceph_msg_header *hdr,
2009                                   int *skip)
2010 {
2011         struct ceph_osd *osd = con->private;
2012         int type = le16_to_cpu(hdr->type);
2013         int front = le32_to_cpu(hdr->front_len);
2014
2015         *skip = 0;
2016         switch (type) {
2017         case CEPH_MSG_OSD_MAP:
2018         case CEPH_MSG_WATCH_NOTIFY:
2019                 return ceph_msg_new(type, front, GFP_NOFS, false);
2020         case CEPH_MSG_OSD_OPREPLY:
2021                 return get_reply(con, hdr, skip);
2022         default:
2023                 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2024                         osd->o_osd);
2025                 *skip = 1;
2026                 return NULL;
2027         }
2028 }
2029
2030 /*
2031  * Wrappers to refcount containing ceph_osd struct
2032  */
2033 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2034 {
2035         struct ceph_osd *osd = con->private;
2036         if (get_osd(osd))
2037                 return con;
2038         return NULL;
2039 }
2040
2041 static void put_osd_con(struct ceph_connection *con)
2042 {
2043         struct ceph_osd *osd = con->private;
2044         put_osd(osd);
2045 }
2046
2047 /*
2048  * authentication
2049  */
2050 /*
2051  * Note: returned pointer is the address of a structure that's
2052  * managed separately.  Caller must *not* attempt to free it.
2053  */
2054 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
2055                                         int *proto, int force_new)
2056 {
2057         struct ceph_osd *o = con->private;
2058         struct ceph_osd_client *osdc = o->o_osdc;
2059         struct ceph_auth_client *ac = osdc->client->monc.auth;
2060         struct ceph_auth_handshake *auth = &o->o_auth;
2061
2062         if (force_new && auth->authorizer) {
2063                 if (ac->ops && ac->ops->destroy_authorizer)
2064                         ac->ops->destroy_authorizer(ac, auth->authorizer);
2065                 auth->authorizer = NULL;
2066         }
2067         if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) {
2068                 int ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2069                                                         auth);
2070                 if (ret)
2071                         return ERR_PTR(ret);
2072         }
2073         *proto = ac->protocol;
2074
2075         return auth;
2076 }
2077
2078
2079 static int verify_authorizer_reply(struct ceph_connection *con, int len)
2080 {
2081         struct ceph_osd *o = con->private;
2082         struct ceph_osd_client *osdc = o->o_osdc;
2083         struct ceph_auth_client *ac = osdc->client->monc.auth;
2084
2085         /*
2086          * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
2087          * XXX which do we do:  succeed or fail?
2088          */
2089         return ac->ops->verify_authorizer_reply(ac, o->o_auth.authorizer, len);
2090 }
2091
2092 static int invalidate_authorizer(struct ceph_connection *con)
2093 {
2094         struct ceph_osd *o = con->private;
2095         struct ceph_osd_client *osdc = o->o_osdc;
2096         struct ceph_auth_client *ac = osdc->client->monc.auth;
2097
2098         if (ac->ops && ac->ops->invalidate_authorizer)
2099                 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2100
2101         return ceph_monc_validate_auth(&osdc->client->monc);
2102 }
2103
2104 static const struct ceph_connection_operations osd_con_ops = {
2105         .get = get_osd_con,
2106         .put = put_osd_con,
2107         .dispatch = dispatch,
2108         .get_authorizer = get_authorizer,
2109         .verify_authorizer_reply = verify_authorizer_reply,
2110         .invalidate_authorizer = invalidate_authorizer,
2111         .alloc_msg = alloc_msg,
2112         .fault = osd_reset,
2113 };